ab17d00d3317460386a405fedfe1bf982a357f2c
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for 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 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify" 
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
113   "*A regexp to match subjects to be excluded from loose thread gathering.
114 As loose thread gathering is done on subjects only, that means that
115 there can be many false gatherings performed.  By rooting out certain
116 common subjects, gathering might become saner."
117   :group 'gnus-thread
118   :type 'regexp)
119
120 (defcustom gnus-summary-gather-subject-limit nil
121   "*Maximum length of subject comparisons when gathering loose threads.
122 Use nil to compare full subjects.  Setting this variable to a low
123 number will help gather threads that have been corrupted by
124 newsreaders chopping off subject lines, but it might also mean that
125 unrelated articles that have subject that happen to begin with the
126 same few characters will be incorrectly gathered.
127
128 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
129 comparing subjects."
130   :group 'gnus-thread
131   :type '(choice (const :tag "off" nil)
132                  (const fuzzy)
133                  (sexp :menu-tag "on" t)))
134
135 (defcustom gnus-simplify-subject-functions nil
136   "List of functions taking a string argument that simplify subjects.
137 The functions are applied recursively.
138
139 Useful functions to put in this list include: `gnus-simplify-subject-re',
140 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
141   :group 'gnus-thread
142   :type '(repeat function))
143
144 (defcustom gnus-simplify-ignored-prefixes nil
145   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  regexp))
149
150 (defcustom gnus-build-sparse-threads nil
151   "*If non-nil, fill in the gaps in threads.
152 If `some', only fill in the gaps that are needed to tie loose threads
153 together.  If `more', fill in all leaf nodes that Gnus can find.  If
154 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  (const some)
158                  (const more)
159                  (sexp :menu-tag "all" t)))
160
161 (defcustom gnus-summary-thread-gathering-function
162   'gnus-gather-threads-by-subject
163   "*Function used for gathering loose threads.
164 There are two pre-defined functions: `gnus-gather-threads-by-subject',
165 which only takes Subjects into consideration; and
166 `gnus-gather-threads-by-references', which compared the References
167 headers of the articles to find matches."
168   :group 'gnus-thread
169   :type '(radio (function-item gnus-gather-threads-by-subject)
170                 (function-item gnus-gather-threads-by-references)
171                 (function :tag "other")))
172
173 (defcustom gnus-summary-same-subject ""
174   "*String indicating that the current article has the same subject as the previous.
175 This variable will only be used if the value of
176 `gnus-summary-make-false-root' is `empty'."
177   :group 'gnus-summary-format
178   :type 'string)
179
180 (defcustom gnus-summary-goto-unread t
181   "*If t, many commands will go to the next unread article.
182 This applies to marking commands as well as other commands that
183 \"naturally\" select the next article, like, for instance, `SPC' at
184 the end of an article.
185
186 If nil, the marking commands do NOT go to the next unread article
187 (they go to the next article instead).  If `never', commands that
188 usually go to the next unread article, will go to the next article,
189 whether it is read or not."
190   :group 'gnus-summary-marks
191   :link '(custom-manual "(gnus)Setting Marks")
192   :type '(choice (const :tag "off" nil)
193                  (const never)
194                  (sexp :menu-tag "on" t)))
195
196 (defcustom gnus-summary-default-score 0
197   "*Default article score level.
198 All scores generated by the score files will be added to this score.
199 If this variable is nil, scoring will be disabled."
200   :group 'gnus-score-default
201   :type '(choice (const :tag "disable")
202                  integer))
203
204 (defcustom gnus-summary-default-high-score 0
205   "*Default threshold for a high scored article.
206 An article will be highlighted as high scored if its score is greater
207 than this score."
208   :group 'gnus-score-default
209   :type 'integer)
210
211 (defcustom gnus-summary-default-low-score 0
212   "*Default threshold for a low scored article.
213 An article will be highlighted as low scored if its score is smaller
214 than this score."
215   :group 'gnus-score-default
216   :type 'integer)
217
218 (defcustom gnus-summary-zcore-fuzz 0
219   "*Fuzziness factor for the zcore in the summary buffer.
220 Articles with scores closer than this to `gnus-summary-default-score'
221 will not be marked."
222   :group 'gnus-summary-format
223   :type 'integer)
224
225 (defcustom gnus-simplify-subject-fuzzy-regexp nil
226   "*Strings to be removed when doing fuzzy matches.
227 This can either be a regular expression or list of regular expressions
228 that will be removed from subject strings if fuzzy subject
229 simplification is selected."
230   :group 'gnus-thread
231   :type '(repeat regexp))
232
233 (defcustom gnus-show-threads t
234   "*If non-nil, display threads in summary mode."
235   :group 'gnus-thread
236   :type 'boolean)
237
238 (defcustom gnus-thread-hide-subtree nil
239   "*If non-nil, hide all threads initially.
240 This can be a predicate specifier which says which threads to hide.
241 If threads are hidden, you have to run the command
242 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
243 to expose hidden threads."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-killed t
248   "*If non-nil, hide killed threads automatically."
249   :group 'gnus-thread
250   :type 'boolean)
251
252 (defcustom gnus-thread-ignore-subject t
253   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
254 If nil, articles that have different subjects from their parents will
255 start separate threads."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-operation-ignore-subject t
260   "*If non-nil, subjects will be ignored when doing thread commands.
261 This affects commands like `gnus-summary-kill-thread' and
262 `gnus-summary-lower-thread'.
263
264 If this variable is nil, articles in the same thread with different
265 subjects will not be included in the operation in question.  If this
266 variable is `fuzzy', only articles that have subjects that are fuzzily
267 equal will be included."
268   :group 'gnus-thread
269   :type '(choice (const :tag "off" nil)
270                  (const fuzzy)
271                  (sexp :tag "on" t)))
272
273 (defcustom gnus-thread-indent-level 4
274   "*Number that says how much each sub-thread should be indented."
275   :group 'gnus-thread
276   :type 'integer)
277
278 (defcustom gnus-auto-extend-newsgroup t
279   "*If non-nil, extend newsgroup forward and backward when requested."
280   :group 'gnus-summary-choose
281   :type 'boolean)
282
283 (defcustom gnus-auto-select-first t
284   "*If non-nil, select the article under point.
285 Which article this is is controlled by the `gnus-auto-select-subject'
286 variable.
287
288 If you want to prevent automatic selection of articles in some
289 newsgroups, set the variable to nil in `gnus-select-group-hook'."
290   :group 'gnus-group-select
291   :type '(choice (const :tag "none" nil)
292                  (sexp :menu-tag "first" t)))
293
294 (defcustom gnus-auto-select-subject 'unread
295   "*Says what subject to place under point when entering a group.
296
297 This variable can either be the symbols `first' (place point on the
298 first subject), `unread' (place point on the subject line of the first
299 unread article), `best' (place point on the subject line of the
300 higest-scored article), `unseen' (place point on the subject line of
301 the first unseen article), 'unseen-or-unread' (place point on the subject
302 line of the first unseen article or, if all article have been seen, on the
303 subject line of the first unread article), or a function to be called to
304 place point on some subject line."
305   :group 'gnus-group-select
306   :type '(choice (const best)
307                  (const unread)
308                  (const first)
309                  (const unseen)
310                  (const unseen-or-unread)))
311
312 (defcustom gnus-auto-select-next t
313   "*If non-nil, offer to go to the next group from the end of the previous.
314 If the value is t and the next newsgroup is empty, Gnus will exit
315 summary mode and go back to group mode.  If the value is neither nil
316 nor t, Gnus will select the following unread newsgroup.  In
317 particular, if the value is the symbol `quietly', the next unread
318 newsgroup will be selected without any confirmation, and if it is
319 `almost-quietly', the next group will be selected without any
320 confirmation if you are located on the last article in the group.
321 Finally, if this variable is `slightly-quietly', the `Z n' command
322 will go to the next group without confirmation."
323   :group 'gnus-summary-maneuvering
324   :type '(choice (const :tag "off" nil)
325                  (const quietly)
326                  (const almost-quietly)
327                  (const slightly-quietly)
328                  (sexp :menu-tag "on" t)))
329
330 (defcustom gnus-auto-select-same nil
331   "*If non-nil, select the next article with the same subject.
332 If there are no more articles with the same subject, go to
333 the first unread article."
334   :group 'gnus-summary-maneuvering
335   :type 'boolean)
336
337 (defcustom gnus-summary-check-current nil
338   "*If non-nil, consider the current article when moving.
339 The \"unread\" movement commands will stay on the same line if the
340 current article is unread."
341   :group 'gnus-summary-maneuvering
342   :type 'boolean)
343
344 (defcustom gnus-auto-center-summary t
345   "*If non-nil, always center the current summary buffer.
346 In particular, if `vertical' do only vertical recentering.  If non-nil
347 and non-`vertical', do both horizontal and vertical recentering."
348   :group 'gnus-summary-maneuvering
349   :type '(choice (const :tag "none" nil)
350                  (const vertical)
351                  (integer :tag "height")
352                  (sexp :menu-tag "both" t)))
353
354 (defcustom gnus-show-all-headers nil
355   "*If non-nil, don't hide any headers."
356   :group 'gnus-article-hiding
357   :group 'gnus-article-headers
358   :type 'boolean)
359
360 (defcustom gnus-summary-ignore-duplicates nil
361   "*If non-nil, ignore articles with identical Message-ID headers."
362   :group 'gnus-summary
363   :type 'boolean)
364
365 (defcustom gnus-single-article-buffer t
366   "*If non-nil, display all articles in the same buffer.
367 If nil, each group will get its own article buffer."
368   :group 'gnus-article-various
369   :type 'boolean)
370
371 (defcustom gnus-break-pages t
372   "*If non-nil, do page breaking on articles.
373 The page delimiter is specified by the `gnus-page-delimiter'
374 variable."
375   :group 'gnus-article-various
376   :type 'boolean)
377
378 (defcustom gnus-move-split-methods nil
379   "*Variable used to suggest where articles are to be moved to.
380 It uses the same syntax as the `gnus-split-methods' variable.
381 However, whereas `gnus-split-methods' specifies file names as targets,
382 this variable specifies group names."
383   :group 'gnus-summary-mail
384   :type '(repeat (choice (list :value (fun) function)
385                          (cons :value ("" "") regexp (repeat string))
386                          (sexp :value nil))))
387
388 (defcustom gnus-unread-mark ?           ;Whitespace
389   "*Mark used for unread articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-ticked-mark ?!
394   "*Mark used for ticked articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-dormant-mark ??
399   "*Mark used for dormant articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-del-mark ?r
404   "*Mark used for del'd articles."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-read-mark ?R
409   "*Mark used for read articles."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-expirable-mark ?E
414   "*Mark used for expirable articles."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-killed-mark ?K
419   "*Mark used for killed articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-souped-mark ?F
424   "*Mark used for souped articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-kill-file-mark ?X
429   "*Mark used for articles killed by kill files."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-low-score-mark ?Y
434   "*Mark used for articles with a low score."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-catchup-mark ?C
439   "*Mark used for articles that are caught up."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-replied-mark ?A
444   "*Mark used for articles that have been replied to."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-forwarded-mark ?F
449   "*Mark used for articles that have been forwarded."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-recent-mark ?N
454   "*Mark used for articles that are recent."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-cached-mark ?*
459   "*Mark used for articles that are in the cache."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-saved-mark ?S
464   "*Mark used for articles that have been saved."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-unseen-mark ?.
469   "*Mark used for articles that haven't been seen."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-no-mark ?               ;Whitespace
474   "*Mark used for articles that have no other secondary mark."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-ancient-mark ?O
479   "*Mark used for ancient articles."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-sparse-mark ?Q
484   "*Mark used for sparsely reffed articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-canceled-mark ?G
489   "*Mark used for canceled articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-duplicate-mark ?M
494   "*Mark used for duplicate articles."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-undownloaded-mark ?@
499   "*Mark used for articles that weren't downloaded."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-downloadable-mark ?%
504   "*Mark used for articles that are to be downloaded."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-unsendable-mark ?=
509   "*Mark used for articles that won't be sent."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-score-over-mark ?+
514   "*Score mark used for articles with high scores."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-score-below-mark ?-
519   "*Score mark used for articles with low scores."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-empty-thread-mark ?     ;Whitespace
524   "*There is no thread under the article."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-not-empty-thread-mark ?=
529   "*There is a thread under the article."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-view-pseudo-asynchronously nil
534   "*If non-nil, Gnus will view pseudo-articles asynchronously."
535   :group 'gnus-extract-view
536   :type 'boolean)
537
538 (defcustom gnus-auto-expirable-marks
539   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
540         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
541         gnus-souped-mark gnus-duplicate-mark)
542   "*The list of marks converted into expiration if a group is auto-expirable."
543   :version "21.1"
544   :group 'gnus-summary
545   :type '(repeat character))
546
547 (defcustom gnus-inhibit-user-auto-expire t
548   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
549   :version "21.1"
550   :group 'gnus-summary
551   :type 'boolean)
552
553 (defcustom gnus-view-pseudos nil
554   "*If `automatic', pseudo-articles will be viewed automatically.
555 If `not-confirm', pseudos will be viewed automatically, and the user
556 will not be asked to confirm the command."
557   :group 'gnus-extract-view
558   :type '(choice (const :tag "off" nil)
559                  (const automatic)
560                  (const not-confirm)))
561
562 (defcustom gnus-view-pseudos-separately t
563   "*If non-nil, one pseudo-article will be created for each file to be viewed.
564 If nil, all files that use the same viewing command will be given as a
565 list of parameters to that command."
566   :group 'gnus-extract-view
567   :type 'boolean)
568
569 (defcustom gnus-insert-pseudo-articles t
570   "*If non-nil, insert pseudo-articles when decoding articles."
571   :group 'gnus-extract-view
572   :type 'boolean)
573
574 (defcustom gnus-summary-dummy-line-format
575   "  %(:                          :%) %S\n"
576   "*The format specification for the dummy roots in the summary buffer.
577 It works along the same lines as a normal formatting string,
578 with some simple extensions.
579
580 %S  The subject
581
582 General format specifiers can also be used.
583 See `(gnus)Formatting Variables'."
584   :link '(custom-manual "(gnus)Formatting Variables")
585   :group 'gnus-threading
586   :type 'string)
587
588 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
589   "*The format specification for the summary mode line.
590 It works along the same lines as a normal formatting string,
591 with some simple extensions:
592
593 %G  Group name
594 %p  Unprefixed group name
595 %A  Current article number
596 %z  Current article score
597 %V  Gnus version
598 %U  Number of unread articles in the group
599 %e  Number of unselected articles in the group
600 %Z  A string with unread/unselected article counts
601 %g  Shortish group name
602 %S  Subject of the current article
603 %u  User-defined spec
604 %s  Current score file name
605 %d  Number of dormant articles
606 %r  Number of articles that have been marked as read in this session
607 %E  Number of articles expunged by the score files"
608   :group 'gnus-summary-format
609   :type 'string)
610
611 (defcustom gnus-list-identifiers nil
612   "Regexp that matches list identifiers to be removed from subject.
613 This can also be a list of regexps."
614   :version "21.1"
615   :group 'gnus-summary-format
616   :group 'gnus-article-hiding
617   :type '(choice (const :tag "none" nil)
618                  (regexp :value ".*")
619                  (repeat :value (".*") regexp)))
620
621 (defcustom gnus-summary-mark-below 0
622   "*Mark all articles with a score below this variable as read.
623 This variable is local to each summary buffer and usually set by the
624 score file."
625   :group 'gnus-score-default
626   :type 'integer)
627
628 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
629   "*List of functions used for sorting articles in the summary buffer.
630
631 Each function takes two articles and returns non-nil if the first
632 article should be sorted before the other.  If you use more than one
633 function, the primary sort function should be the last.  You should
634 probably always include `gnus-article-sort-by-number' in the list of
635 sorting functions -- preferably first.  Also note that sorting by date
636 is often much slower than sorting by number, and the sorting order is
637 very similar.  (Sorting by date means sorting by the time the message
638 was sent, sorting by number means sorting by arrival time.)
639
640 Ready-made functions include `gnus-article-sort-by-number',
641 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
642 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
643
644 When threading is turned on, the variable `gnus-thread-sort-functions'
645 controls how articles are sorted."
646   :group 'gnus-summary-sort
647   :type '(repeat (choice (function-item gnus-article-sort-by-number)
648                          (function-item gnus-article-sort-by-author)
649                          (function-item gnus-article-sort-by-subject)
650                          (function-item gnus-article-sort-by-date)
651                          (function-item gnus-article-sort-by-score)
652                          (function :tag "other"))))
653
654 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
655   "*List of functions used for sorting threads in the summary buffer.
656 By default, threads are sorted by article number.
657
658 Each function takes two threads and returns non-nil if the first
659 thread should be sorted before the other.  If you use more than one
660 function, the primary sort function should be the last.  You should
661 probably always include `gnus-thread-sort-by-number' in the list of
662 sorting functions -- preferably first.  Also note that sorting by date
663 is often much slower than sorting by number, and the sorting order is
664 very similar.  (Sorting by date means sorting by the time the message
665 was sent, sorting by number means sorting by arrival time.)
666
667 Ready-made functions include `gnus-thread-sort-by-number',
668 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
669 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
670 `gnus-thread-sort-by-most-recent-number',
671 `gnus-thread-sort-by-most-recent-date', and
672 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
673
674 When threading is turned off, the variable
675 `gnus-article-sort-functions' controls how articles are sorted."
676   :group 'gnus-summary-sort
677   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
678                          (function-item gnus-thread-sort-by-author)
679                          (function-item gnus-thread-sort-by-subject)
680                          (function-item gnus-thread-sort-by-date)
681                          (function-item gnus-thread-sort-by-score)
682                          (function-item gnus-thread-sort-by-total-score)
683                          (function :tag "other"))))
684
685 (defcustom gnus-thread-score-function '+
686   "*Function used for calculating the total score of a thread.
687
688 The function is called with the scores of the article and each
689 subthread and should then return the score of the thread.
690
691 Some functions you can use are `+', `max', or `min'."
692   :group 'gnus-summary-sort
693   :type 'function)
694
695 (defcustom gnus-summary-expunge-below nil
696   "All articles that have a score less than this variable will be expunged.
697 This variable is local to the summary buffers."
698   :group 'gnus-score-default
699   :type '(choice (const :tag "off" nil)
700                  integer))
701
702 (defcustom gnus-thread-expunge-below nil
703   "All threads that have a total score less than this variable will be expunged.
704 See `gnus-thread-score-function' for en explanation of what a
705 \"thread score\" is.
706
707 This variable is local to the summary buffers."
708   :group 'gnus-threading
709   :group 'gnus-score-default
710   :type '(choice (const :tag "off" nil)
711                  integer))
712
713 (defcustom gnus-summary-mode-hook nil
714   "*A hook for Gnus summary mode.
715 This hook is run before any variables are set in the summary buffer."
716   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
717   :group 'gnus-summary-various
718   :type 'hook)
719
720 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
721 (when (featurep 'xemacs)
722   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
723   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
724   (add-hook 'gnus-summary-mode-hook
725             'gnus-xmas-switch-horizontal-scrollbar-off))
726
727 (defcustom gnus-summary-menu-hook nil
728   "*Hook run after the creation of the summary mode menu."
729   :group 'gnus-summary-visual
730   :type 'hook)
731
732 (defcustom gnus-summary-exit-hook nil
733   "*A hook called on exit from the summary buffer.
734 It will be called with point in the group buffer."
735   :group 'gnus-summary-exit
736   :type 'hook)
737
738 (defcustom gnus-summary-prepare-hook nil
739   "*A hook called after the summary buffer has been generated.
740 If you want to modify the summary buffer, you can use this hook."
741   :group 'gnus-summary-various
742   :type 'hook)
743
744 (defcustom gnus-summary-prepared-hook nil
745   "*A hook called as the last thing after the summary buffer has been generated."
746   :group 'gnus-summary-various
747   :type 'hook)
748
749 (defcustom gnus-summary-generate-hook nil
750   "*A hook run just before generating the summary buffer.
751 This hook is commonly used to customize threading variables and the
752 like."
753   :group 'gnus-summary-various
754   :type 'hook)
755
756 (defcustom gnus-select-group-hook nil
757   "*A hook called when a newsgroup is selected.
758
759 If you'd like to simplify subjects like the
760 `gnus-summary-next-same-subject' command does, you can use the
761 following hook:
762
763  (add-hook gnus-select-group-hook
764            (lambda ()
765              (mapcar (lambda (header)
766                        (mail-header-set-subject
767                         header
768                         (gnus-simplify-subject
769                          (mail-header-subject header) 're-only)))
770                      gnus-newsgroup-headers)))"
771   :group 'gnus-group-select
772   :type 'hook)
773
774 (defcustom gnus-select-article-hook nil
775   "*A hook called when an article is selected."
776   :group 'gnus-summary-choose
777   :type 'hook)
778
779 (defcustom gnus-visual-mark-article-hook
780   (list 'gnus-highlight-selected-summary)
781   "*Hook run after selecting an article in the summary buffer.
782 It is meant to be used for highlighting the article in some way.  It
783 is not run if `gnus-visual' is nil."
784   :group 'gnus-summary-visual
785   :type 'hook)
786
787 (defcustom gnus-parse-headers-hook nil
788   "*A hook called before parsing the headers."
789   :group 'gnus-various
790   :type 'hook)
791
792 (defcustom gnus-exit-group-hook nil
793   "*A hook called when exiting summary mode.
794 This hook is not called from the non-updating exit commands like `Q'."
795   :group 'gnus-various
796   :type 'hook)
797
798 (defcustom gnus-summary-update-hook
799   (list 'gnus-summary-highlight-line)
800   "*A hook called when a summary line is changed.
801 The hook will not be called if `gnus-visual' is nil.
802
803 The default function `gnus-summary-highlight-line' will
804 highlight the line according to the `gnus-summary-highlight'
805 variable."
806   :group 'gnus-summary-visual
807   :type 'hook)
808
809 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
810   "*A hook called when an article is selected for the first time.
811 The hook is intended to mark an article as read (or unread)
812 automatically when it is selected."
813   :group 'gnus-summary-choose
814   :type 'hook)
815
816 (defcustom gnus-group-no-more-groups-hook nil
817   "*A hook run when returning to group mode having no more (unread) groups."
818   :group 'gnus-group-select
819   :type 'hook)
820
821 (defcustom gnus-ps-print-hook nil
822   "*A hook run before ps-printing something from Gnus."
823   :group 'gnus-summary
824   :type 'hook)
825
826 (defcustom gnus-summary-display-arrow
827   (and (fboundp 'display-graphic-p)
828        (display-graphic-p))
829   "*If non-nil, display an arrow highlighting the current article."
830   :version "21.1"
831   :group 'gnus-summary
832   :type 'boolean)
833
834 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
835   "Face used for highlighting the current article in the summary buffer."
836   :group 'gnus-summary-visual
837   :type 'face)
838
839 (defcustom gnus-summary-highlight
840   '(((eq mark gnus-canceled-mark)
841      . gnus-summary-cancelled-face)
842     ((and (> score default-high)
843           (or (eq mark gnus-dormant-mark)
844               (eq mark gnus-ticked-mark)))
845      . gnus-summary-high-ticked-face)
846     ((and (< score default-low)
847           (or (eq mark gnus-dormant-mark)
848               (eq mark gnus-ticked-mark)))
849      . gnus-summary-low-ticked-face)
850     ((or (eq mark gnus-dormant-mark)
851          (eq mark gnus-ticked-mark))
852      . gnus-summary-normal-ticked-face)
853     ((and (> score default-high) (eq mark gnus-ancient-mark))
854      . gnus-summary-high-ancient-face)
855     ((and (< score default-low) (eq mark gnus-ancient-mark))
856      . gnus-summary-low-ancient-face)
857     ((eq mark gnus-ancient-mark)
858      . gnus-summary-normal-ancient-face)
859     ((and (> score default-high) (eq mark gnus-unread-mark))
860      . gnus-summary-high-unread-face)
861     ((and (< score default-low) (eq mark gnus-unread-mark))
862      . gnus-summary-low-unread-face)
863     ((eq mark gnus-unread-mark)
864      . gnus-summary-normal-unread-face)
865     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
866                                                   gnus-undownloaded-mark)))
867      . gnus-summary-high-unread-face)
868     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
869                                                  gnus-undownloaded-mark)))
870      . gnus-summary-low-unread-face)
871     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
872           (memq article gnus-newsgroup-unreads))
873      . gnus-summary-normal-unread-face)
874     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
875      . gnus-summary-normal-read-face)
876     ((> score default-high)
877      . gnus-summary-high-read-face)
878     ((< score default-low)
879      . gnus-summary-low-read-face)
880     (t
881      . gnus-summary-normal-read-face))
882   "*Controls the highlighting of summary buffer lines.
883
884 A list of (FORM . FACE) pairs.  When deciding how a a particular
885 summary line should be displayed, each form is evaluated.  The content
886 of the face field after the first true form is used.  You can change
887 how those summary lines are displayed, by editing the face field.
888
889 You can use the following variables in the FORM field.
890
891 score:        The article's score
892 default:      The default article score.
893 default-high: The default score for high scored articles.
894 default-low:  The default score for low scored articles.
895 below:        The score below which articles are automatically marked as read.
896 mark:         The articles mark."
897   :group 'gnus-summary-visual
898   :type '(repeat (cons (sexp :tag "Form" nil)
899                        face)))
900
901 (defcustom gnus-alter-header-function nil
902   "Function called to allow alteration of article header structures.
903 The function is called with one parameter, the article header vector,
904 which it may alter in any way.")
905
906 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
907   "Variable that says which function should be used to decode a string with encoded words.")
908
909 (defcustom gnus-extra-headers '(To Newsgroups)
910   "*Extra headers to parse."
911   :version "21.1"
912   :group 'gnus-summary
913   :type '(repeat symbol))
914
915 (defcustom gnus-ignored-from-addresses
916   (and user-mail-address (regexp-quote user-mail-address))
917   "*Regexp of From headers that may be suppressed in favor of To headers."
918   :version "21.1"
919   :group 'gnus-summary
920   :type 'regexp)
921
922 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
923   "List of charsets that should be ignored.
924 When these charsets are used in the \"charset\" parameter, the
925 default charset will be used instead."
926   :version "21.1"
927   :type '(repeat symbol)
928   :group 'gnus-charset)
929
930 (gnus-define-group-parameter
931  ignored-charsets
932  :type list
933  :function-document
934  "Return the ignored charsets of GROUP."
935  :variable gnus-group-ignored-charsets-alist
936  :variable-default
937  '(("alt\\.chinese\\.text" iso-8859-1))
938  :variable-document
939  "Alist of regexps (to match group names) and charsets that should be ignored.
940 When these charsets are used in the \"charset\" parameter, the
941 default charset will be used instead."
942  :variable-group gnus-charset
943  :variable-type '(repeat (cons (regexp :tag "Group")
944                                (repeat symbol)))
945  :parameter-type '(choice :tag "Ignored charsets"
946                           :value nil
947                           (repeat (symbol)))
948  :parameter-document       "\
949 List of charsets that should be ignored.
950
951 When these charsets are used in the \"charset\" parameter, the
952 default charset will be used instead.")
953
954 (defcustom gnus-group-highlight-words-alist nil
955   "Alist of group regexps and highlight regexps.
956 This variable uses the same syntax as `gnus-emphasis-alist'."
957   :version "21.1"
958   :type '(repeat (cons (regexp :tag "Group")
959                        (repeat (list (regexp :tag "Highlight regexp")
960                                      (number :tag "Group for entire word" 0)
961                                      (number :tag "Group for displayed part" 0)
962                                      (symbol :tag "Face"
963                                              gnus-emphasis-highlight-words)))))
964   :group 'gnus-summary-visual)
965
966 (defcustom gnus-summary-show-article-charset-alist
967   nil
968   "Alist of number and charset.
969 The article will be shown with the charset corresponding to the
970 numbered argument.
971 For example: ((1 . cn-gb-2312) (2 . big5))."
972   :version "21.1"
973   :type '(repeat (cons (number :tag "Argument" 1)
974                        (symbol :tag "Charset")))
975   :group 'gnus-charset)
976
977 (defcustom gnus-preserve-marks t
978   "Whether marks are preserved when moving, copying and respooling messages."
979   :version "21.1"
980   :type 'boolean
981   :group 'gnus-summary-marks)
982
983 (defcustom gnus-alter-articles-to-read-function nil
984   "Function to be called to alter the list of articles to be selected."
985   :type '(choice (const nil) function)
986   :group 'gnus-summary)
987
988 (defcustom gnus-orphan-score nil
989   "*All orphans get this score added.  Set in the score file."
990   :group 'gnus-score-default
991   :type '(choice (const nil)
992                  integer))
993
994 (defcustom gnus-summary-save-parts-default-mime "image/.*"
995   "*A regexp to match MIME parts when saving multiple parts of a message
996 with gnus-summary-save-parts (X m). This regexp will be used by default
997 when prompting the user for which type of files to save."
998   :group 'gnus-summary
999   :type 'regexp)
1000
1001 (defcustom gnus-read-all-available-headers nil
1002   "Whether Gnus should parse all headers made available to it.
1003 This is mostly relevant for slow backends where the user may
1004 wish to widen the summary buffer to include all headers
1005 that were fetched.  Say, for nnultimate groups."
1006   :group 'gnus-summary
1007   :type '(choice boolean regexp))
1008
1009 (defcustom gnus-summary-muttprint-program "muttprint"
1010   "Command (and optional arguments) used to run Muttprint."
1011   :version "21.3"
1012   :group 'gnus-summary
1013   :type 'string)
1014
1015 (defcustom gnus-article-loose-mime nil
1016   "If non-nil, don't require MIME-Version header.
1017 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1018 supply the MIME-Version header or deliberately strip it From the mail.
1019 Set it to non-nil, Gnus will treat some articles as MIME even if
1020 the MIME-Version header is missed."
1021   :version "21.3"
1022   :type 'boolean
1023   :group 'gnus-article)
1024
1025 ;;; Internal variables
1026
1027 (defvar gnus-summary-display-cache nil)
1028 (defvar gnus-article-mime-handles nil)
1029 (defvar gnus-article-decoded-p nil)
1030 (defvar gnus-article-charset nil)
1031 (defvar gnus-article-ignored-charsets nil)
1032 (defvar gnus-scores-exclude-files nil)
1033 (defvar gnus-page-broken nil)
1034 (defvar gnus-inhibit-mime-unbuttonizing nil)
1035
1036 (defvar gnus-original-article nil)
1037 (defvar gnus-article-internal-prepare-hook nil)
1038 (defvar gnus-newsgroup-process-stack nil)
1039
1040 (defvar gnus-thread-indent-array nil)
1041 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1042 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1043   "Function called to sort the articles within a thread after it has been gathered together.")
1044
1045 (defvar gnus-summary-save-parts-type-history nil)
1046 (defvar gnus-summary-save-parts-last-directory nil)
1047
1048 ;; Avoid highlighting in kill files.
1049 (defvar gnus-summary-inhibit-highlight nil)
1050 (defvar gnus-newsgroup-selected-overlay nil)
1051 (defvar gnus-inhibit-limiting nil)
1052 (defvar gnus-newsgroup-adaptive-score-file nil)
1053 (defvar gnus-current-score-file nil)
1054 (defvar gnus-current-move-group nil)
1055 (defvar gnus-current-copy-group nil)
1056 (defvar gnus-current-crosspost-group nil)
1057 (defvar gnus-newsgroup-display nil)
1058
1059 (defvar gnus-newsgroup-dependencies nil)
1060 (defvar gnus-newsgroup-adaptive nil)
1061 (defvar gnus-summary-display-article-function nil)
1062 (defvar gnus-summary-highlight-line-function nil
1063   "Function called after highlighting a summary line.")
1064
1065 (defvar gnus-summary-line-format-alist
1066   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1067     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1068     (?s gnus-tmp-subject-or-nil ?s)
1069     (?n gnus-tmp-name ?s)
1070     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1071         ?s)
1072     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1073             gnus-tmp-from) ?s)
1074     (?F gnus-tmp-from ?s)
1075     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1076     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1077     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1078     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1079     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1080     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1081     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1082     (?L gnus-tmp-lines ?s)
1083     (?I gnus-tmp-indentation ?s)
1084     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1085     (?R gnus-tmp-replied ?c)
1086     (?\[ gnus-tmp-opening-bracket ?c)
1087     (?\] gnus-tmp-closing-bracket ?c)
1088     (?\> (make-string gnus-tmp-level ? ) ?s)
1089     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1090     (?i gnus-tmp-score ?d)
1091     (?z gnus-tmp-score-char ?c)
1092     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1093     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1094     (?U gnus-tmp-unread ?c)
1095     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1096         ?s)
1097     (?t (gnus-summary-number-of-articles-in-thread
1098          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1099         ?d)
1100     (?e (gnus-summary-number-of-articles-in-thread
1101          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1102         ?c)
1103     (?u gnus-tmp-user-defined ?s)
1104     (?P (gnus-pick-line-number) ?d)
1105     (?B gnus-tmp-thread-tree-header-string ?s)
1106     (user-date (gnus-user-date
1107                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1108   "An alist of format specifications that can appear in summary lines.
1109 These are paired with what variables they correspond with, along with
1110 the type of the variable (string, integer, character, etc).")
1111
1112 (defvar gnus-summary-dummy-line-format-alist
1113   `((?S gnus-tmp-subject ?s)
1114     (?N gnus-tmp-number ?d)
1115     (?u gnus-tmp-user-defined ?s)))
1116
1117 (defvar gnus-summary-mode-line-format-alist
1118   `((?G gnus-tmp-group-name ?s)
1119     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1120     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1121     (?A gnus-tmp-article-number ?d)
1122     (?Z gnus-tmp-unread-and-unselected ?s)
1123     (?V gnus-version ?s)
1124     (?U gnus-tmp-unread-and-unticked ?d)
1125     (?S gnus-tmp-subject ?s)
1126     (?e gnus-tmp-unselected ?d)
1127     (?u gnus-tmp-user-defined ?s)
1128     (?d (length gnus-newsgroup-dormant) ?d)
1129     (?t (length gnus-newsgroup-marked) ?d)
1130     (?r (length gnus-newsgroup-reads) ?d)
1131     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1132     (?E gnus-newsgroup-expunged-tally ?d)
1133     (?s (gnus-current-score-file-nondirectory) ?s)))
1134
1135 (defvar gnus-last-search-regexp nil
1136   "Default regexp for article search command.")
1137
1138 (defvar gnus-last-shell-command nil
1139   "Default shell command on article.")
1140
1141 (defvar gnus-newsgroup-begin nil)
1142 (defvar gnus-newsgroup-end nil)
1143 (defvar gnus-newsgroup-last-rmail nil)
1144 (defvar gnus-newsgroup-last-mail nil)
1145 (defvar gnus-newsgroup-last-folder nil)
1146 (defvar gnus-newsgroup-last-file nil)
1147 (defvar gnus-newsgroup-auto-expire nil)
1148 (defvar gnus-newsgroup-active nil)
1149
1150 (defvar gnus-newsgroup-data nil)
1151 (defvar gnus-newsgroup-data-reverse nil)
1152 (defvar gnus-newsgroup-limit nil)
1153 (defvar gnus-newsgroup-limits nil)
1154
1155 (defvar gnus-newsgroup-unreads nil
1156   "Sorted list of unread articles in the current newsgroup.")
1157
1158 (defvar gnus-newsgroup-unselected nil
1159   "Sorted list of unselected unread articles in the current newsgroup.")
1160
1161 (defvar gnus-newsgroup-reads nil
1162   "Alist of read articles and article marks in the current newsgroup.")
1163
1164 (defvar gnus-newsgroup-expunged-tally nil)
1165
1166 (defvar gnus-newsgroup-marked nil
1167   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1168
1169 (defvar gnus-newsgroup-killed nil
1170   "List of ranges of articles that have been through the scoring process.")
1171
1172 (defvar gnus-newsgroup-cached nil
1173   "Sorted list of articles that come from the article cache.")
1174
1175 (defvar gnus-newsgroup-saved nil
1176   "List of articles that have been saved.")
1177
1178 (defvar gnus-newsgroup-kill-headers nil)
1179
1180 (defvar gnus-newsgroup-replied nil
1181   "List of articles that have been replied to in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-forwarded nil
1184   "List of articles that have been forwarded in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-recent nil
1187   "List of articles that have are recent in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-expirable nil
1190   "Sorted list of articles in the current newsgroup that can be expired.")
1191
1192 (defvar gnus-newsgroup-processable nil
1193   "List of articles in the current newsgroup that can be processed.")
1194
1195 (defvar gnus-newsgroup-downloadable nil
1196   "Sorted list of articles in the current newsgroup that can be processed.")
1197
1198 (defvar gnus-newsgroup-undownloaded nil
1199   "List of articles in the current newsgroup that haven't been downloaded..")
1200
1201 (defvar gnus-newsgroup-unsendable nil
1202   "List of articles in the current newsgroup that won't be sent.")
1203
1204 (defvar gnus-newsgroup-bookmarks nil
1205   "List of articles in the current newsgroup that have bookmarks.")
1206
1207 (defvar gnus-newsgroup-dormant nil
1208   "Sorted list of dormant articles in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-unseen nil
1211   "List of unseen articles in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-seen nil
1214   "Range of seen articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-articles nil
1217   "List of articles in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-scored nil
1220   "List of scored articles in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-headers nil
1223   "List of article headers in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-threads nil)
1226
1227 (defvar gnus-newsgroup-prepared nil
1228   "Whether the current group has been prepared properly.")
1229
1230 (defvar gnus-newsgroup-ancient nil
1231   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-sparse nil)
1234
1235 (defvar gnus-current-article nil)
1236 (defvar gnus-article-current nil)
1237 (defvar gnus-current-headers nil)
1238 (defvar gnus-have-all-headers nil)
1239 (defvar gnus-last-article nil)
1240 (defvar gnus-newsgroup-history nil)
1241 (defvar gnus-newsgroup-charset nil)
1242 (defvar gnus-newsgroup-ephemeral-charset nil)
1243 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1244
1245 (defvar gnus-article-before-search nil)
1246
1247 (defconst gnus-summary-local-variables
1248   '(gnus-newsgroup-name
1249     gnus-newsgroup-begin gnus-newsgroup-end
1250     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1251     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1252     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1253     gnus-newsgroup-unselected gnus-newsgroup-marked
1254     gnus-newsgroup-reads gnus-newsgroup-saved
1255     gnus-newsgroup-replied gnus-newsgroup-forwarded
1256     gnus-newsgroup-recent
1257     gnus-newsgroup-expirable
1258     gnus-newsgroup-processable gnus-newsgroup-killed
1259     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1260     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1261     gnus-newsgroup-seen gnus-newsgroup-articles
1262     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1263     gnus-newsgroup-headers gnus-newsgroup-threads
1264     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1265     gnus-current-article gnus-current-headers gnus-have-all-headers
1266     gnus-last-article gnus-article-internal-prepare-hook
1267     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1268     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1269     gnus-thread-expunge-below
1270     gnus-score-alist gnus-current-score-file
1271     (gnus-summary-expunge-below . global)
1272     (gnus-summary-mark-below . global)
1273     (gnus-orphan-score . global)
1274     gnus-newsgroup-active gnus-scores-exclude-files
1275     gnus-newsgroup-history gnus-newsgroup-ancient
1276     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1277     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1278     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1279     (gnus-newsgroup-expunged-tally . 0)
1280     gnus-cache-removable-articles gnus-newsgroup-cached
1281     gnus-newsgroup-data gnus-newsgroup-data-reverse
1282     gnus-newsgroup-limit gnus-newsgroup-limits
1283     gnus-newsgroup-charset gnus-newsgroup-display)
1284   "Variables that are buffer-local to the summary buffers.")
1285
1286 (defvar gnus-newsgroup-variables nil
1287   "A list of variables that have separate values in different newsgroups.
1288 A list of newsgroup (summary buffer) local variables, or cons of
1289 variables and their default values (when the default values are not
1290 nil), that should be made global while the summary buffer is active.
1291 These variables can be used to set variables in the group parameters
1292 while still allowing them to affect operations done in other
1293 buffers. For example:
1294
1295 \(setq gnus-newsgroup-variables
1296      '(message-use-followup-to
1297        (gnus-visible-headers .
1298          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1299 ")
1300
1301 ;; Byte-compiler warning.
1302 (eval-when-compile (defvar gnus-article-mode-map))
1303
1304 ;; MIME stuff.
1305
1306 (defvar gnus-decode-encoded-word-methods
1307   '(mail-decode-encoded-word-string)
1308   "List of methods used to decode encoded words.
1309
1310 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1311 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1312 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1313 whose names match REGEXP.
1314
1315 For example:
1316 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1317  mail-decode-encoded-word-string
1318  (\"chinese\" . rfc1843-decode-string))")
1319
1320 (defvar gnus-decode-encoded-word-methods-cache nil)
1321
1322 (defun gnus-multi-decode-encoded-word-string (string)
1323   "Apply the functions from `gnus-encoded-word-methods' that match."
1324   (unless (and gnus-decode-encoded-word-methods-cache
1325                (eq gnus-newsgroup-name
1326                    (car gnus-decode-encoded-word-methods-cache)))
1327     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1328     (mapcar (lambda (x)
1329               (if (symbolp x)
1330                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1331                 (if (and gnus-newsgroup-name
1332                          (string-match (car x) gnus-newsgroup-name))
1333                     (nconc gnus-decode-encoded-word-methods-cache
1334                            (list (cdr x))))))
1335             gnus-decode-encoded-word-methods))
1336   (let ((xlist gnus-decode-encoded-word-methods-cache))
1337     (pop xlist)
1338     (while xlist
1339       (setq string (funcall (pop xlist) string))))
1340   string)
1341
1342 ;; Subject simplification.
1343
1344 (defun gnus-simplify-whitespace (str)
1345   "Remove excessive whitespace from STR."
1346   (let ((mystr str))
1347     ;; Multiple spaces.
1348     (while (string-match "[ \t][ \t]+" mystr)
1349       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1350                           " "
1351                           (substring mystr (match-end 0)))))
1352     ;; Leading spaces.
1353     (when (string-match "^[ \t]+" mystr)
1354       (setq mystr (substring mystr (match-end 0))))
1355     ;; Trailing spaces.
1356     (when (string-match "[ \t]+$" mystr)
1357       (setq mystr (substring mystr 0 (match-beginning 0))))
1358     mystr))
1359
1360 (defsubst gnus-simplify-subject-re (subject)
1361   "Remove \"Re:\" from subject lines."
1362   (if (string-match message-subject-re-regexp subject)
1363       (substring subject (match-end 0))
1364     subject))
1365
1366 (defun gnus-simplify-subject (subject &optional re-only)
1367   "Remove `Re:' and words in parentheses.
1368 If RE-ONLY is non-nil, strip leading `Re:'s only."
1369   (let ((case-fold-search t))           ;Ignore case.
1370     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1371     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1372       (setq subject (substring subject (match-end 0))))
1373     ;; Remove uninteresting prefixes.
1374     (when (and (not re-only)
1375                gnus-simplify-ignored-prefixes
1376                (string-match gnus-simplify-ignored-prefixes subject))
1377       (setq subject (substring subject (match-end 0))))
1378     ;; Remove words in parentheses from end.
1379     (unless re-only
1380       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1381         (setq subject (substring subject 0 (match-beginning 0)))))
1382     ;; Return subject string.
1383     subject))
1384
1385 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1386 ;; all whitespace.
1387 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1388   (goto-char (point-min))
1389   (while (re-search-forward regexp nil t)
1390     (replace-match (or newtext ""))))
1391
1392 (defun gnus-simplify-buffer-fuzzy ()
1393   "Simplify string in the buffer fuzzily.
1394 The string in the accessible portion of the current buffer is simplified.
1395 It is assumed to be a single-line subject.
1396 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1397 matter is removed.  Additional things can be deleted by setting
1398 `gnus-simplify-subject-fuzzy-regexp'."
1399   (let ((case-fold-search t)
1400         (modified-tick))
1401     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1402
1403     (while (not (eq modified-tick (buffer-modified-tick)))
1404       (setq modified-tick (buffer-modified-tick))
1405       (cond
1406        ((listp gnus-simplify-subject-fuzzy-regexp)
1407         (mapcar 'gnus-simplify-buffer-fuzzy-step
1408                 gnus-simplify-subject-fuzzy-regexp))
1409        (gnus-simplify-subject-fuzzy-regexp
1410         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1411       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1412       (gnus-simplify-buffer-fuzzy-step
1413        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1414       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1415
1416     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1417     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1418     (gnus-simplify-buffer-fuzzy-step " $")
1419     (gnus-simplify-buffer-fuzzy-step "^ +")))
1420
1421 (defun gnus-simplify-subject-fuzzy (subject)
1422   "Simplify a subject string fuzzily.
1423 See `gnus-simplify-buffer-fuzzy' for details."
1424   (save-excursion
1425     (gnus-set-work-buffer)
1426     (let ((case-fold-search t))
1427       ;; Remove uninteresting prefixes.
1428       (when (and gnus-simplify-ignored-prefixes
1429                  (string-match gnus-simplify-ignored-prefixes subject))
1430         (setq subject (substring subject (match-end 0))))
1431       (insert subject)
1432       (inline (gnus-simplify-buffer-fuzzy))
1433       (buffer-string))))
1434
1435 (defsubst gnus-simplify-subject-fully (subject)
1436   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1437   (cond
1438    (gnus-simplify-subject-functions
1439     (gnus-map-function gnus-simplify-subject-functions subject))
1440    ((null gnus-summary-gather-subject-limit)
1441     (gnus-simplify-subject-re subject))
1442    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1443     (gnus-simplify-subject-fuzzy subject))
1444    ((numberp gnus-summary-gather-subject-limit)
1445     (gnus-limit-string (gnus-simplify-subject-re subject)
1446                        gnus-summary-gather-subject-limit))
1447    (t
1448     subject)))
1449
1450 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1451   "Check whether two subjects are equal.
1452 If optional argument simple-first is t, first argument is already
1453 simplified."
1454   (cond
1455    ((null simple-first)
1456     (equal (gnus-simplify-subject-fully s1)
1457            (gnus-simplify-subject-fully s2)))
1458    (t
1459     (equal s1
1460            (gnus-simplify-subject-fully s2)))))
1461
1462 (defun gnus-summary-bubble-group ()
1463   "Increase the score of the current group.
1464 This is a handy function to add to `gnus-summary-exit-hook' to
1465 increase the score of each group you read."
1466   (gnus-group-add-score gnus-newsgroup-name))
1467
1468 \f
1469 ;;;
1470 ;;; Gnus summary mode
1471 ;;;
1472
1473 (put 'gnus-summary-mode 'mode-class 'special)
1474
1475 (defvar gnus-article-commands-menu)
1476
1477 (when t
1478   ;; Non-orthogonal keys
1479
1480   (gnus-define-keys gnus-summary-mode-map
1481     " " gnus-summary-next-page
1482     "\177" gnus-summary-prev-page
1483     [delete] gnus-summary-prev-page
1484     [backspace] gnus-summary-prev-page
1485     "\r" gnus-summary-scroll-up
1486     "\M-\r" gnus-summary-scroll-down
1487     "n" gnus-summary-next-unread-article
1488     "p" gnus-summary-prev-unread-article
1489     "N" gnus-summary-next-article
1490     "P" gnus-summary-prev-article
1491     "\M-\C-n" gnus-summary-next-same-subject
1492     "\M-\C-p" gnus-summary-prev-same-subject
1493     "\M-n" gnus-summary-next-unread-subject
1494     "\M-p" gnus-summary-prev-unread-subject
1495     "." gnus-summary-first-unread-article
1496     "," gnus-summary-best-unread-article
1497     "\M-s" gnus-summary-search-article-forward
1498     "\M-r" gnus-summary-search-article-backward
1499     "<" gnus-summary-beginning-of-article
1500     ">" gnus-summary-end-of-article
1501     "j" gnus-summary-goto-article
1502     "^" gnus-summary-refer-parent-article
1503     "\M-^" gnus-summary-refer-article
1504     "u" gnus-summary-tick-article-forward
1505     "!" gnus-summary-tick-article-forward
1506     "U" gnus-summary-tick-article-backward
1507     "d" gnus-summary-mark-as-read-forward
1508     "D" gnus-summary-mark-as-read-backward
1509     "E" gnus-summary-mark-as-expirable
1510     "\M-u" gnus-summary-clear-mark-forward
1511     "\M-U" gnus-summary-clear-mark-backward
1512     "k" gnus-summary-kill-same-subject-and-select
1513     "\C-k" gnus-summary-kill-same-subject
1514     "\M-\C-k" gnus-summary-kill-thread
1515     "\M-\C-l" gnus-summary-lower-thread
1516     "e" gnus-summary-edit-article
1517     "#" gnus-summary-mark-as-processable
1518     "\M-#" gnus-summary-unmark-as-processable
1519     "\M-\C-t" gnus-summary-toggle-threads
1520     "\M-\C-s" gnus-summary-show-thread
1521     "\M-\C-h" gnus-summary-hide-thread
1522     "\M-\C-f" gnus-summary-next-thread
1523     "\M-\C-b" gnus-summary-prev-thread
1524     [(meta down)] gnus-summary-next-thread
1525     [(meta up)] gnus-summary-prev-thread
1526     "\M-\C-u" gnus-summary-up-thread
1527     "\M-\C-d" gnus-summary-down-thread
1528     "&" gnus-summary-execute-command
1529     "c" gnus-summary-catchup-and-exit
1530     "\C-w" gnus-summary-mark-region-as-read
1531     "\C-t" gnus-summary-toggle-truncation
1532     "?" gnus-summary-mark-as-dormant
1533     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1534     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1535     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1536     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1537     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1538     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1539     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1540     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1541     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1542     "=" gnus-summary-expand-window
1543     "\C-x\C-s" gnus-summary-reselect-current-group
1544     "\M-g" gnus-summary-rescan-group
1545     "w" gnus-summary-stop-page-breaking
1546     "\C-c\C-r" gnus-summary-caesar-message
1547     "f" gnus-summary-followup
1548     "F" gnus-summary-followup-with-original
1549     "C" gnus-summary-cancel-article
1550     "r" gnus-summary-reply
1551     "R" gnus-summary-reply-with-original
1552     "\C-c\C-f" gnus-summary-mail-forward
1553     "o" gnus-summary-save-article
1554     "\C-o" gnus-summary-save-article-mail
1555     "|" gnus-summary-pipe-output
1556     "\M-k" gnus-summary-edit-local-kill
1557     "\M-K" gnus-summary-edit-global-kill
1558     ;; "V" gnus-version
1559     "\C-c\C-d" gnus-summary-describe-group
1560     "q" gnus-summary-exit
1561     "Q" gnus-summary-exit-no-update
1562     "\C-c\C-i" gnus-info-find-node
1563     gnus-mouse-2 gnus-mouse-pick-article
1564     "m" gnus-summary-mail-other-window
1565     "a" gnus-summary-post-news
1566     "i" gnus-summary-news-other-window
1567     "x" gnus-summary-limit-to-unread
1568     "s" gnus-summary-isearch-article
1569     "t" gnus-summary-toggle-header
1570     "g" gnus-summary-show-article
1571     "l" gnus-summary-goto-last-article
1572     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1573     "\C-d" gnus-summary-enter-digest-group
1574     "\M-\C-d" gnus-summary-read-document
1575     "\M-\C-e" gnus-summary-edit-parameters
1576     "\M-\C-a" gnus-summary-customize-parameters
1577     "\C-c\C-b" gnus-bug
1578     "*" gnus-cache-enter-article
1579     "\M-*" gnus-cache-remove-article
1580     "\M-&" gnus-summary-universal-argument
1581     "\C-l" gnus-recenter
1582     "I" gnus-summary-increase-score
1583     "L" gnus-summary-lower-score
1584     "\M-i" gnus-symbolic-argument
1585     "h" gnus-summary-select-article-buffer
1586
1587     "b" gnus-article-view-part
1588     "\M-t" gnus-summary-toggle-display-buttonized
1589
1590     "V" gnus-summary-score-map
1591     "X" gnus-uu-extract-map
1592     "S" gnus-summary-send-map)
1593
1594   ;; Sort of orthogonal keymap
1595   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1596     "t" gnus-summary-tick-article-forward
1597     "!" gnus-summary-tick-article-forward
1598     "d" gnus-summary-mark-as-read-forward
1599     "r" gnus-summary-mark-as-read-forward
1600     "c" gnus-summary-clear-mark-forward
1601     " " gnus-summary-clear-mark-forward
1602     "e" gnus-summary-mark-as-expirable
1603     "x" gnus-summary-mark-as-expirable
1604     "?" gnus-summary-mark-as-dormant
1605     "b" gnus-summary-set-bookmark
1606     "B" gnus-summary-remove-bookmark
1607     "#" gnus-summary-mark-as-processable
1608     "\M-#" gnus-summary-unmark-as-processable
1609     "S" gnus-summary-limit-include-expunged
1610     "C" gnus-summary-catchup
1611     "H" gnus-summary-catchup-to-here
1612     "h" gnus-summary-catchup-from-here
1613     "\C-c" gnus-summary-catchup-all
1614     "k" gnus-summary-kill-same-subject-and-select
1615     "K" gnus-summary-kill-same-subject
1616     "P" gnus-uu-mark-map)
1617
1618   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1619     "c" gnus-summary-clear-above
1620     "u" gnus-summary-tick-above
1621     "m" gnus-summary-mark-above
1622     "k" gnus-summary-kill-below)
1623
1624   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1625     "/" gnus-summary-limit-to-subject
1626     "n" gnus-summary-limit-to-articles
1627     "w" gnus-summary-pop-limit
1628     "s" gnus-summary-limit-to-subject
1629     "a" gnus-summary-limit-to-author
1630     "u" gnus-summary-limit-to-unread
1631     "m" gnus-summary-limit-to-marks
1632     "M" gnus-summary-limit-exclude-marks
1633     "v" gnus-summary-limit-to-score
1634     "*" gnus-summary-limit-include-cached
1635     "D" gnus-summary-limit-include-dormant
1636     "T" gnus-summary-limit-include-thread
1637     "d" gnus-summary-limit-exclude-dormant
1638     "t" gnus-summary-limit-to-age
1639     "x" gnus-summary-limit-to-extra
1640     "p" gnus-summary-limit-to-display-predicate
1641     "E" gnus-summary-limit-include-expunged
1642     "c" gnus-summary-limit-exclude-childless-dormant
1643     "C" gnus-summary-limit-mark-excluded-as-read
1644     "o" gnus-summary-insert-old-articles
1645     "N" gnus-summary-insert-new-articles)
1646
1647   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1648     "n" gnus-summary-next-unread-article
1649     "p" gnus-summary-prev-unread-article
1650     "N" gnus-summary-next-article
1651     "P" gnus-summary-prev-article
1652     "\C-n" gnus-summary-next-same-subject
1653     "\C-p" gnus-summary-prev-same-subject
1654     "\M-n" gnus-summary-next-unread-subject
1655     "\M-p" gnus-summary-prev-unread-subject
1656     "f" gnus-summary-first-unread-article
1657     "b" gnus-summary-best-unread-article
1658     "j" gnus-summary-goto-article
1659     "g" gnus-summary-goto-subject
1660     "l" gnus-summary-goto-last-article
1661     "o" gnus-summary-pop-article)
1662
1663   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1664     "k" gnus-summary-kill-thread
1665     "l" gnus-summary-lower-thread
1666     "i" gnus-summary-raise-thread
1667     "T" gnus-summary-toggle-threads
1668     "t" gnus-summary-rethread-current
1669     "^" gnus-summary-reparent-thread
1670     "s" gnus-summary-show-thread
1671     "S" gnus-summary-show-all-threads
1672     "h" gnus-summary-hide-thread
1673     "H" gnus-summary-hide-all-threads
1674     "n" gnus-summary-next-thread
1675     "p" gnus-summary-prev-thread
1676     "u" gnus-summary-up-thread
1677     "o" gnus-summary-top-thread
1678     "d" gnus-summary-down-thread
1679     "#" gnus-uu-mark-thread
1680     "\M-#" gnus-uu-unmark-thread)
1681
1682   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1683     "g" gnus-summary-prepare
1684     "c" gnus-summary-insert-cached-articles)
1685
1686   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1687     "c" gnus-summary-catchup-and-exit
1688     "C" gnus-summary-catchup-all-and-exit
1689     "E" gnus-summary-exit-no-update
1690     "Q" gnus-summary-exit
1691     "Z" gnus-summary-exit
1692     "n" gnus-summary-catchup-and-goto-next-group
1693     "R" gnus-summary-reselect-current-group
1694     "G" gnus-summary-rescan-group
1695     "N" gnus-summary-next-group
1696     "s" gnus-summary-save-newsrc
1697     "P" gnus-summary-prev-group)
1698
1699   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1700     " " gnus-summary-next-page
1701     "n" gnus-summary-next-page
1702     "\177" gnus-summary-prev-page
1703     [delete] gnus-summary-prev-page
1704     "p" gnus-summary-prev-page
1705     "\r" gnus-summary-scroll-up
1706     "\M-\r" gnus-summary-scroll-down
1707     "<" gnus-summary-beginning-of-article
1708     ">" gnus-summary-end-of-article
1709     "b" gnus-summary-beginning-of-article
1710     "e" gnus-summary-end-of-article
1711     "^" gnus-summary-refer-parent-article
1712     "r" gnus-summary-refer-parent-article
1713     "D" gnus-summary-enter-digest-group
1714     "R" gnus-summary-refer-references
1715     "T" gnus-summary-refer-thread
1716     "g" gnus-summary-show-article
1717     "s" gnus-summary-isearch-article
1718     "P" gnus-summary-print-article
1719     "M" gnus-mailing-list-insinuate
1720     "t" gnus-article-babel)
1721
1722   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1723     "b" gnus-article-add-buttons
1724     "B" gnus-article-add-buttons-to-head
1725     "o" gnus-article-treat-overstrike
1726     "e" gnus-article-emphasize
1727     "w" gnus-article-fill-cited-article
1728     "Q" gnus-article-fill-long-lines
1729     "C" gnus-article-capitalize-sentences
1730     "c" gnus-article-remove-cr
1731     "q" gnus-article-de-quoted-unreadable
1732     "6" gnus-article-de-base64-unreadable
1733     "Z" gnus-article-decode-HZ
1734     "h" gnus-article-wash-html
1735     "u" gnus-article-unsplit-urls
1736     "s" gnus-summary-force-verify-and-decrypt
1737     "f" gnus-article-display-x-face
1738     "l" gnus-summary-stop-page-breaking
1739     "r" gnus-summary-caesar-message
1740     "t" gnus-summary-toggle-header
1741     "g" gnus-treat-smiley
1742     "v" gnus-summary-verbose-headers
1743     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1744     "p" gnus-article-verify-x-pgp-sig
1745     "d" gnus-article-treat-dumbquotes
1746     "k" gnus-article-outlook-deuglify-article)
1747
1748   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1749     "a" gnus-article-hide
1750     "h" gnus-article-hide-headers
1751     "b" gnus-article-hide-boring-headers
1752     "s" gnus-article-hide-signature
1753     "c" gnus-article-hide-citation
1754     "C" gnus-article-hide-citation-in-followups
1755     "l" gnus-article-hide-list-identifiers
1756     "p" gnus-article-hide-pgp
1757     "B" gnus-article-strip-banner
1758     "P" gnus-article-hide-pem
1759     "\C-c" gnus-article-hide-citation-maybe)
1760
1761   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1762     "a" gnus-article-highlight
1763     "h" gnus-article-highlight-headers
1764     "c" gnus-article-highlight-citation
1765     "s" gnus-article-highlight-signature)
1766
1767   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1768     "f" gnus-article-treat-fold-headers
1769     "u" gnus-article-treat-unfold-headers
1770     "n" gnus-article-treat-fold-newsgroups)
1771
1772   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1773     "x" gnus-article-display-x-face
1774     "s" gnus-treat-smiley
1775     "D" gnus-article-remove-images
1776     "f" gnus-treat-from-picon
1777     "m" gnus-treat-mail-picon
1778     "n" gnus-treat-newsgroups-picon)
1779
1780   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1781     "w" gnus-article-decode-mime-words
1782     "c" gnus-article-decode-charset
1783     "v" gnus-mime-view-all-parts
1784     "b" gnus-article-view-part)
1785
1786   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1787     "z" gnus-article-date-ut
1788     "u" gnus-article-date-ut
1789     "l" gnus-article-date-local
1790     "p" gnus-article-date-english
1791     "e" gnus-article-date-lapsed
1792     "o" gnus-article-date-original
1793     "i" gnus-article-date-iso8601
1794     "s" gnus-article-date-user)
1795
1796   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1797     "t" gnus-article-remove-trailing-blank-lines
1798     "l" gnus-article-strip-leading-blank-lines
1799     "m" gnus-article-strip-multiple-blank-lines
1800     "a" gnus-article-strip-blank-lines
1801     "A" gnus-article-strip-all-blank-lines
1802     "s" gnus-article-strip-leading-space
1803     "e" gnus-article-strip-trailing-space
1804     "w" gnus-article-remove-leading-whitespace)
1805
1806   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1807     "v" gnus-version
1808     "f" gnus-summary-fetch-faq
1809     "d" gnus-summary-describe-group
1810     "h" gnus-summary-describe-briefly
1811     "i" gnus-info-find-node)
1812
1813   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1814     "e" gnus-summary-expire-articles
1815     "\M-\C-e" gnus-summary-expire-articles-now
1816     "\177" gnus-summary-delete-article
1817     [delete] gnus-summary-delete-article
1818     [backspace] gnus-summary-delete-article
1819     "m" gnus-summary-move-article
1820     "r" gnus-summary-respool-article
1821     "w" gnus-summary-edit-article
1822     "c" gnus-summary-copy-article
1823     "B" gnus-summary-crosspost-article
1824     "q" gnus-summary-respool-query
1825     "t" gnus-summary-respool-trace
1826     "i" gnus-summary-import-article
1827     "I" gnus-summary-create-article
1828     "p" gnus-summary-article-posted-p)
1829
1830   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1831     "o" gnus-summary-save-article
1832     "m" gnus-summary-save-article-mail
1833     "F" gnus-summary-write-article-file
1834     "r" gnus-summary-save-article-rmail
1835     "f" gnus-summary-save-article-file
1836     "b" gnus-summary-save-article-body-file
1837     "h" gnus-summary-save-article-folder
1838     "v" gnus-summary-save-article-vm
1839     "p" gnus-summary-pipe-output
1840     "P" gnus-summary-muttprint
1841     "s" gnus-soup-add-article)
1842
1843   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1844     "b" gnus-summary-display-buttonized
1845     "m" gnus-summary-repair-multipart
1846     "v" gnus-article-view-part
1847     "o" gnus-article-save-part
1848     "c" gnus-article-copy-part
1849     "C" gnus-article-view-part-as-charset
1850     "e" gnus-article-view-part-externally
1851     "E" gnus-article-encrypt-body
1852     "i" gnus-article-inline-part
1853     "|" gnus-article-pipe-part)
1854
1855   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1856     "p" gnus-summary-mark-as-processable
1857     "u" gnus-summary-unmark-as-processable
1858     "U" gnus-summary-unmark-all-processable
1859     "v" gnus-uu-mark-over
1860     "s" gnus-uu-mark-series
1861     "r" gnus-uu-mark-region
1862     "g" gnus-uu-unmark-region
1863     "R" gnus-uu-mark-by-regexp
1864     "G" gnus-uu-unmark-by-regexp
1865     "t" gnus-uu-mark-thread
1866     "T" gnus-uu-unmark-thread
1867     "a" gnus-uu-mark-all
1868     "b" gnus-uu-mark-buffer
1869     "S" gnus-uu-mark-sparse
1870     "k" gnus-summary-kill-process-mark
1871     "y" gnus-summary-yank-process-mark
1872     "w" gnus-summary-save-process-mark
1873     "i" gnus-uu-invert-processable)
1874
1875   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1876     ;;"x" gnus-uu-extract-any
1877     "m" gnus-summary-save-parts
1878     "u" gnus-uu-decode-uu
1879     "U" gnus-uu-decode-uu-and-save
1880     "s" gnus-uu-decode-unshar
1881     "S" gnus-uu-decode-unshar-and-save
1882     "o" gnus-uu-decode-save
1883     "O" gnus-uu-decode-save
1884     "b" gnus-uu-decode-binhex
1885     "B" gnus-uu-decode-binhex
1886     "p" gnus-uu-decode-postscript
1887     "P" gnus-uu-decode-postscript-and-save)
1888
1889   (gnus-define-keys
1890       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1891     "u" gnus-uu-decode-uu-view
1892     "U" gnus-uu-decode-uu-and-save-view
1893     "s" gnus-uu-decode-unshar-view
1894     "S" gnus-uu-decode-unshar-and-save-view
1895     "o" gnus-uu-decode-save-view
1896     "O" gnus-uu-decode-save-view
1897     "b" gnus-uu-decode-binhex-view
1898     "B" gnus-uu-decode-binhex-view
1899     "p" gnus-uu-decode-postscript-view
1900     "P" gnus-uu-decode-postscript-and-save-view))
1901
1902 (defvar gnus-article-post-menu nil)
1903
1904 (defun gnus-summary-make-menu-bar ()
1905   (gnus-turn-off-edit-menu 'summary)
1906
1907   (unless (boundp 'gnus-summary-misc-menu)
1908
1909     (easy-menu-define
1910       gnus-summary-kill-menu gnus-summary-mode-map ""
1911       (cons
1912        "Score"
1913        (nconc
1914         (list
1915          ["Customize" gnus-score-customize t])
1916         (gnus-make-score-map 'increase)
1917         (gnus-make-score-map 'lower)
1918         '(("Mark"
1919            ["Kill below" gnus-summary-kill-below t]
1920            ["Mark above" gnus-summary-mark-above t]
1921            ["Tick above" gnus-summary-tick-above t]
1922            ["Clear above" gnus-summary-clear-above t])
1923           ["Current score" gnus-summary-current-score t]
1924           ["Set score" gnus-summary-set-score t]
1925           ["Switch current score file..." gnus-score-change-score-file t]
1926           ["Set mark below..." gnus-score-set-mark-below t]
1927           ["Set expunge below..." gnus-score-set-expunge-below t]
1928           ["Edit current score file" gnus-score-edit-current-scores t]
1929           ["Edit score file" gnus-score-edit-file t]
1930           ["Trace score" gnus-score-find-trace t]
1931           ["Find words" gnus-score-find-favourite-words t]
1932           ["Rescore buffer" gnus-summary-rescore t]
1933           ["Increase score..." gnus-summary-increase-score t]
1934           ["Lower score..." gnus-summary-lower-score t]))))
1935
1936     ;; Define both the Article menu in the summary buffer and the
1937     ;; equivalent Commands menu in the article buffer here for
1938     ;; consistency.
1939     (let ((innards
1940            `(("Hide"
1941               ["All" gnus-article-hide t]
1942               ["Headers" gnus-article-hide-headers t]
1943               ["Signature" gnus-article-hide-signature t]
1944               ["Citation" gnus-article-hide-citation t]
1945               ["List identifiers" gnus-article-hide-list-identifiers t]
1946               ["PGP" gnus-article-hide-pgp t]
1947               ["Banner" gnus-article-strip-banner t]
1948               ["Boring headers" gnus-article-hide-boring-headers t])
1949              ("Highlight"
1950               ["All" gnus-article-highlight t]
1951               ["Headers" gnus-article-highlight-headers t]
1952               ["Signature" gnus-article-highlight-signature t]
1953               ["Citation" gnus-article-highlight-citation t])
1954              ("MIME"
1955               ["Words" gnus-article-decode-mime-words t]
1956               ["Charset" gnus-article-decode-charset t]
1957               ["QP" gnus-article-de-quoted-unreadable t]
1958               ["Base64" gnus-article-de-base64-unreadable t]
1959               ["View all" gnus-mime-view-all-parts t]
1960               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1961               ["Encrypt body" gnus-article-encrypt-body t]
1962               ["Extract all parts" gnus-summary-save-parts t])
1963              ("Date"
1964               ["Local" gnus-article-date-local t]
1965               ["ISO8601" gnus-article-date-iso8601 t]
1966               ["UT" gnus-article-date-ut t]
1967               ["Original" gnus-article-date-original t]
1968               ["Lapsed" gnus-article-date-lapsed t]
1969               ["User-defined" gnus-article-date-user t])
1970              ("Display"
1971               ["Remove images" gnus-article-remove-images t]
1972               ["Toggle smiley" gnus-treat-smiley t]
1973               ["Show X-Face" gnus-article-display-x-face t]
1974               ["Show picons in From" gnus-treat-from-picon t]
1975               ["Show picons in mail headers" gnus-treat-mail-picon t]
1976               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
1977               ("View as different encoding"
1978                ,@(mapcar
1979                   (lambda (cs)
1980                     ;; Since easymenu under FSF Emacs doesn't allow lambda
1981                     ;; forms for menu commands, we should provide intern'ed
1982                     ;; function symbols.
1983                     (let ((command (intern (format "\
1984 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
1985                       (fset command
1986                             `(lambda ()
1987                                (interactive)
1988                                (let ((gnus-summary-show-article-charset-alist
1989                                       '((1 . ,cs))))
1990                                  (gnus-summary-show-article 1))))
1991                       `[,(symbol-name cs) ,command t]))
1992                   (sort (if (fboundp 'coding-system-list)
1993                             (coding-system-list)
1994                           (mapcar 'car mm-mime-mule-charset-alist))
1995                         (lambda (a b)
1996                           (string< (symbol-name a)
1997                                    (symbol-name b)))))))
1998              ("Washing"
1999               ("Remove Blanks"
2000                ["Leading" gnus-article-strip-leading-blank-lines t]
2001                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2002                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2003                ["All of the above" gnus-article-strip-blank-lines t]
2004                ["All" gnus-article-strip-all-blank-lines t]
2005                ["Leading space" gnus-article-strip-leading-space t]
2006                ["Trailing space" gnus-article-strip-trailing-space t]
2007                ["Leading space in headers"
2008                 gnus-article-remove-leading-whitespace t])
2009               ["Overstrike" gnus-article-treat-overstrike t]
2010               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2011               ["Emphasis" gnus-article-emphasize t]
2012               ["Word wrap" gnus-article-fill-cited-article t]
2013               ["Fill long lines" gnus-article-fill-long-lines t]
2014               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2015               ["CR" gnus-article-remove-cr t]
2016               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2017               ["Base64" gnus-article-de-base64-unreadable t]
2018               ["Rot 13" gnus-summary-caesar-message
2019                ,@(if (featurep 'xemacs) '(t)
2020                    '(:help "\"Caesar rotate\" article by 13"))]
2021               ["Unix pipe" gnus-summary-pipe-message t]
2022               ["Add buttons" gnus-article-add-buttons t]
2023               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2024               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2025               ["Verbose header" gnus-summary-verbose-headers t]
2026               ["Toggle header" gnus-summary-toggle-header t]
2027               ["Unfold headers" gnus-article-treat-unfold-headers t]
2028               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2029               ["Html" gnus-article-wash-html t]
2030               ["URLs" gnus-article-unsplit-urls t]
2031               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2032               ["HZ" gnus-article-decode-HZ t]
2033               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2034               )
2035              ("Output"
2036               ["Save in default format" gnus-summary-save-article
2037                ,@(if (featurep 'xemacs) '(t)
2038                    '(:help "Save article using default method"))]
2039               ["Save in file" gnus-summary-save-article-file
2040                ,@(if (featurep 'xemacs) '(t)
2041                    '(:help "Save article in file"))]
2042               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2043               ["Save in MH folder" gnus-summary-save-article-folder t]
2044               ["Save in VM folder" gnus-summary-save-article-vm t]
2045               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2046               ["Save body in file" gnus-summary-save-article-body-file t]
2047               ["Pipe through a filter" gnus-summary-pipe-output t]
2048               ["Add to SOUP packet" gnus-soup-add-article t]
2049               ["Print with Muttprint" gnus-summary-muttprint t]
2050               ["Print" gnus-summary-print-article t])
2051              ("Backend"
2052               ["Respool article..." gnus-summary-respool-article t]
2053               ["Move article..." gnus-summary-move-article
2054                (gnus-check-backend-function
2055                 'request-move-article gnus-newsgroup-name)]
2056               ["Copy article..." gnus-summary-copy-article t]
2057               ["Crosspost article..." gnus-summary-crosspost-article
2058                (gnus-check-backend-function
2059                 'request-replace-article gnus-newsgroup-name)]
2060               ["Import file..." gnus-summary-import-article t]
2061               ["Create article..." gnus-summary-create-article t]
2062               ["Check if posted" gnus-summary-article-posted-p t]
2063               ["Edit article" gnus-summary-edit-article
2064                (not (gnus-group-read-only-p))]
2065               ["Delete article" gnus-summary-delete-article
2066                (gnus-check-backend-function
2067                 'request-expire-articles gnus-newsgroup-name)]
2068               ["Query respool" gnus-summary-respool-query t]
2069               ["Trace respool" gnus-summary-respool-trace t]
2070               ["Delete expirable articles" gnus-summary-expire-articles-now
2071                (gnus-check-backend-function
2072                 'request-expire-articles gnus-newsgroup-name)])
2073              ("Extract"
2074               ["Uudecode" gnus-uu-decode-uu
2075                ,@(if (featurep 'xemacs) '(t)
2076                    '(:help "Decode uuencoded article(s)"))]
2077               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2078               ["Unshar" gnus-uu-decode-unshar t]
2079               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2080               ["Save" gnus-uu-decode-save t]
2081               ["Binhex" gnus-uu-decode-binhex t]
2082               ["Postscript" gnus-uu-decode-postscript t]
2083               ["All MIME parts" gnus-summary-save-parts t])
2084              ("Cache"
2085               ["Enter article" gnus-cache-enter-article t]
2086               ["Remove article" gnus-cache-remove-article t])
2087              ["Translate" gnus-article-babel t]
2088              ["Select article buffer" gnus-summary-select-article-buffer t]
2089              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2090              ["Isearch article..." gnus-summary-isearch-article t]
2091              ["Beginning of the article" gnus-summary-beginning-of-article t]
2092              ["End of the article" gnus-summary-end-of-article t]
2093              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2094              ["Fetch referenced articles" gnus-summary-refer-references t]
2095              ["Fetch current thread" gnus-summary-refer-thread t]
2096              ["Fetch article with id..." gnus-summary-refer-article t]
2097              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2098              ["Redisplay" gnus-summary-show-article t]
2099              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2100       (easy-menu-define
2101         gnus-summary-article-menu gnus-summary-mode-map ""
2102         (cons "Article" innards))
2103
2104       (if (not (keymapp gnus-summary-article-menu))
2105           (easy-menu-define
2106             gnus-article-commands-menu gnus-article-mode-map ""
2107             (cons "Commands" innards))
2108         ;; in Emacs, don't share menu.
2109         (setq gnus-article-commands-menu
2110               (copy-keymap gnus-summary-article-menu))
2111         (define-key gnus-article-mode-map [menu-bar commands]
2112           (cons "Commands" gnus-article-commands-menu))))
2113
2114     (easy-menu-define
2115       gnus-summary-thread-menu gnus-summary-mode-map ""
2116       '("Threads"
2117         ["Toggle threading" gnus-summary-toggle-threads t]
2118         ["Hide threads" gnus-summary-hide-all-threads t]
2119         ["Show threads" gnus-summary-show-all-threads t]
2120         ["Hide thread" gnus-summary-hide-thread t]
2121         ["Show thread" gnus-summary-show-thread t]
2122         ["Go to next thread" gnus-summary-next-thread t]
2123         ["Go to previous thread" gnus-summary-prev-thread t]
2124         ["Go down thread" gnus-summary-down-thread t]
2125         ["Go up thread" gnus-summary-up-thread t]
2126         ["Top of thread" gnus-summary-top-thread t]
2127         ["Mark thread as read" gnus-summary-kill-thread t]
2128         ["Lower thread score" gnus-summary-lower-thread t]
2129         ["Raise thread score" gnus-summary-raise-thread t]
2130         ["Rethread current" gnus-summary-rethread-current t]))
2131
2132     (easy-menu-define
2133       gnus-summary-post-menu gnus-summary-mode-map ""
2134       `("Post"
2135         ["Send a message (mail or news)" gnus-summary-post-news
2136          ,@(if (featurep 'xemacs) '(t)
2137              '(:help "Post an article"))]
2138         ["Followup" gnus-summary-followup
2139          ,@(if (featurep 'xemacs) '(t)
2140              '(:help "Post followup to this article"))]
2141         ["Followup and yank" gnus-summary-followup-with-original
2142          ,@(if (featurep 'xemacs) '(t)
2143              '(:help "Post followup to this article, quoting its contents"))]
2144         ["Supersede article" gnus-summary-supersede-article t]
2145         ["Cancel article" gnus-summary-cancel-article
2146          ,@(if (featurep 'xemacs) '(t)
2147              '(:help "Cancel an article you posted"))]
2148         ["Reply" gnus-summary-reply t]
2149         ["Reply and yank" gnus-summary-reply-with-original t]
2150         ["Wide reply" gnus-summary-wide-reply t]
2151         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2152          ,@(if (featurep 'xemacs) '(t)
2153              '(:help "Mail a reply, quoting this article"))]
2154         ["Very wide reply" gnus-summary-very-wide-reply t]
2155         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2156          ,@(if (featurep 'xemacs) '(t)
2157              '(:help "Mail a very wide reply, quoting this article"))]
2158         ["Mail forward" gnus-summary-mail-forward t]
2159         ["Post forward" gnus-summary-post-forward t]
2160         ["Digest and mail" gnus-uu-digest-mail-forward t]
2161         ["Digest and post" gnus-uu-digest-post-forward t]
2162         ["Resend message" gnus-summary-resend-message t]
2163         ["Resend message edit" gnus-summary-resend-message-edit t]
2164         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2165         ["Send a mail" gnus-summary-mail-other-window t]
2166         ["Create a local message" gnus-summary-news-other-window t]
2167         ["Uuencode and post" gnus-uu-post-news
2168          ,@(if (featurep 'xemacs) '(t)
2169              '(:help "Post a uuencoded article"))]
2170         ["Followup via news" gnus-summary-followup-to-mail t]
2171         ["Followup via news and yank"
2172          gnus-summary-followup-to-mail-with-original t]
2173         ;;("Draft"
2174         ;;["Send" gnus-summary-send-draft t]
2175         ;;["Send bounced" gnus-resend-bounced-mail t])
2176         ))
2177
2178     (cond
2179      ((not (keymapp gnus-summary-post-menu))
2180       (setq gnus-article-post-menu gnus-summary-post-menu))
2181      ((not gnus-article-post-menu)
2182       ;; Don't share post menu.
2183       (setq gnus-article-post-menu
2184             (copy-keymap gnus-summary-post-menu))))
2185     (define-key gnus-article-mode-map [menu-bar post]
2186       (cons "Post" gnus-article-post-menu))
2187
2188     (easy-menu-define
2189       gnus-summary-misc-menu gnus-summary-mode-map ""
2190       `("Gnus"
2191         ("Mark Read"
2192          ["Mark as read" gnus-summary-mark-as-read-forward t]
2193          ["Mark same subject and select"
2194           gnus-summary-kill-same-subject-and-select t]
2195          ["Mark same subject" gnus-summary-kill-same-subject t]
2196          ["Catchup" gnus-summary-catchup
2197           ,@(if (featurep 'xemacs) '(t)
2198               '(:help "Mark unread articles in this group as read"))]
2199          ["Catchup all" gnus-summary-catchup-all t]
2200          ["Catchup to here" gnus-summary-catchup-to-here t]
2201          ["Catchup from here" gnus-summary-catchup-from-here t]
2202          ["Catchup region" gnus-summary-mark-region-as-read t]
2203          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2204         ("Mark Various"
2205          ["Tick" gnus-summary-tick-article-forward t]
2206          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2207          ["Remove marks" gnus-summary-clear-mark-forward t]
2208          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2209          ["Set bookmark" gnus-summary-set-bookmark t]
2210          ["Remove bookmark" gnus-summary-remove-bookmark t])
2211         ("Limit to"
2212          ["Marks..." gnus-summary-limit-to-marks t]
2213          ["Subject..." gnus-summary-limit-to-subject t]
2214          ["Author..." gnus-summary-limit-to-author t]
2215          ["Age..." gnus-summary-limit-to-age t]
2216          ["Extra..." gnus-summary-limit-to-extra t]
2217          ["Score" gnus-summary-limit-to-score t]
2218          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2219          ["Unread" gnus-summary-limit-to-unread t]
2220          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2221          ["Articles" gnus-summary-limit-to-articles t]
2222          ["Pop limit" gnus-summary-pop-limit t]
2223          ["Show dormant" gnus-summary-limit-include-dormant t]
2224          ["Hide childless dormant"
2225           gnus-summary-limit-exclude-childless-dormant t]
2226          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2227          ["Hide marked" gnus-summary-limit-exclude-marks t]
2228          ["Show expunged" gnus-summary-limit-include-expunged t])
2229         ("Process Mark"
2230          ["Set mark" gnus-summary-mark-as-processable t]
2231          ["Remove mark" gnus-summary-unmark-as-processable t]
2232          ["Remove all marks" gnus-summary-unmark-all-processable t]
2233          ["Mark above" gnus-uu-mark-over t]
2234          ["Mark series" gnus-uu-mark-series t]
2235          ["Mark region" gnus-uu-mark-region t]
2236          ["Unmark region" gnus-uu-unmark-region t]
2237          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2238          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2239          ["Mark all" gnus-uu-mark-all t]
2240          ["Mark buffer" gnus-uu-mark-buffer t]
2241          ["Mark sparse" gnus-uu-mark-sparse t]
2242          ["Mark thread" gnus-uu-mark-thread t]
2243          ["Unmark thread" gnus-uu-unmark-thread t]
2244          ("Process Mark Sets"
2245           ["Kill" gnus-summary-kill-process-mark t]
2246           ["Yank" gnus-summary-yank-process-mark
2247            gnus-newsgroup-process-stack]
2248           ["Save" gnus-summary-save-process-mark t]))
2249         ("Scroll article"
2250          ["Page forward" gnus-summary-next-page
2251           ,@(if (featurep 'xemacs) '(t)
2252               '(:help "Show next page of article"))]
2253          ["Page backward" gnus-summary-prev-page
2254           ,@(if (featurep 'xemacs) '(t)
2255               '(:help "Show previous page of article"))]
2256          ["Line forward" gnus-summary-scroll-up t])
2257         ("Move"
2258          ["Next unread article" gnus-summary-next-unread-article t]
2259          ["Previous unread article" gnus-summary-prev-unread-article t]
2260          ["Next article" gnus-summary-next-article t]
2261          ["Previous article" gnus-summary-prev-article t]
2262          ["Next unread subject" gnus-summary-next-unread-subject t]
2263          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2264          ["Next article same subject" gnus-summary-next-same-subject t]
2265          ["Previous article same subject" gnus-summary-prev-same-subject t]
2266          ["First unread article" gnus-summary-first-unread-article t]
2267          ["Best unread article" gnus-summary-best-unread-article t]
2268          ["Go to subject number..." gnus-summary-goto-subject t]
2269          ["Go to article number..." gnus-summary-goto-article t]
2270          ["Go to the last article" gnus-summary-goto-last-article t]
2271          ["Pop article off history" gnus-summary-pop-article t])
2272         ("Sort"
2273          ["Sort by number" gnus-summary-sort-by-number t]
2274          ["Sort by author" gnus-summary-sort-by-author t]
2275          ["Sort by subject" gnus-summary-sort-by-subject t]
2276          ["Sort by date" gnus-summary-sort-by-date t]
2277          ["Sort by score" gnus-summary-sort-by-score t]
2278          ["Sort by lines" gnus-summary-sort-by-lines t]
2279          ["Sort by characters" gnus-summary-sort-by-chars t]
2280          ["Original sort" gnus-summary-sort-by-original t])
2281         ("Help"
2282          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2283          ["Describe group" gnus-summary-describe-group t]
2284          ["Read manual" gnus-info-find-node t])
2285         ("Modes"
2286          ["Pick and read" gnus-pick-mode t]
2287          ["Binary" gnus-binary-mode t])
2288         ("Regeneration"
2289          ["Regenerate" gnus-summary-prepare t]
2290          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2291          ["Toggle threading" gnus-summary-toggle-threads t])
2292         ["See old articles" gnus-summary-insert-old-articles t]
2293         ["See new articles" gnus-summary-insert-new-articles t]
2294         ["Filter articles..." gnus-summary-execute-command t]
2295         ["Run command on subjects..." gnus-summary-universal-argument t]
2296         ["Search articles forward..." gnus-summary-search-article-forward t]
2297         ["Search articles backward..." gnus-summary-search-article-backward t]
2298         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2299         ["Expand window" gnus-summary-expand-window t]
2300         ["Expire expirable articles" gnus-summary-expire-articles
2301          (gnus-check-backend-function
2302           'request-expire-articles gnus-newsgroup-name)]
2303         ["Edit local kill file" gnus-summary-edit-local-kill t]
2304         ["Edit main kill file" gnus-summary-edit-global-kill t]
2305         ["Edit group parameters" gnus-summary-edit-parameters t]
2306         ["Customize group parameters" gnus-summary-customize-parameters t]
2307         ["Send a bug report" gnus-bug t]
2308         ("Exit"
2309          ["Catchup and exit" gnus-summary-catchup-and-exit
2310           ,@(if (featurep 'xemacs) '(t)
2311               '(:help "Mark unread articles in this group as read, then exit"))]
2312          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2313          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2314          ["Exit group" gnus-summary-exit
2315           ,@(if (featurep 'xemacs) '(t)
2316               '(:help "Exit current group, return to group selection mode"))]
2317          ["Exit group without updating" gnus-summary-exit-no-update t]
2318          ["Exit and goto next group" gnus-summary-next-group t]
2319          ["Exit and goto prev group" gnus-summary-prev-group t]
2320          ["Reselect group" gnus-summary-reselect-current-group t]
2321          ["Rescan group" gnus-summary-rescan-group t]
2322          ["Update dribble" gnus-summary-save-newsrc t])))
2323
2324     (gnus-run-hooks 'gnus-summary-menu-hook)))
2325
2326 (defvar gnus-summary-tool-bar-map nil)
2327
2328 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2329 ;; NB: A new function tool-bar-local-item-from-menu is added in Emacs
2330 ;; 21.2.50+.  Considering many users use Emacs 21, use
2331 ;; tool-bar-add-item-from-menu here.
2332 (defun gnus-summary-make-tool-bar ()
2333   (if (and
2334        (condition-case nil (require 'tool-bar) (error nil))
2335        (fboundp 'tool-bar-add-item-from-menu)
2336        (default-value 'tool-bar-mode)
2337        (not gnus-summary-tool-bar-map))
2338       (setq gnus-summary-tool-bar-map
2339             (let ((tool-bar-map (make-sparse-keymap))
2340                   (load-path (mm-image-load-path)))
2341               (tool-bar-add-item-from-menu
2342                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2343               (tool-bar-add-item-from-menu
2344                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2345               (tool-bar-add-item-from-menu
2346                'gnus-summary-post-news "post" gnus-summary-mode-map)
2347               (tool-bar-add-item-from-menu
2348                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2349               (tool-bar-add-item-from-menu
2350                'gnus-summary-followup "followup" gnus-summary-mode-map)
2351               (tool-bar-add-item-from-menu
2352                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2353               (tool-bar-add-item-from-menu
2354                'gnus-summary-reply "reply" gnus-summary-mode-map)
2355               (tool-bar-add-item-from-menu
2356                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2357               (tool-bar-add-item-from-menu
2358                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2359               (tool-bar-add-item-from-menu
2360                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2361               (tool-bar-add-item-from-menu
2362                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2363               (tool-bar-add-item-from-menu
2364                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2365               (tool-bar-add-item-from-menu
2366                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2367               (tool-bar-add-item-from-menu
2368                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2369               (tool-bar-add-item-from-menu
2370                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2371               tool-bar-map)))
2372   (if gnus-summary-tool-bar-map
2373       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2374
2375 (defun gnus-score-set-default (var value)
2376   "A version of set that updates the GNU Emacs menu-bar."
2377   (set var value)
2378   ;; It is the message that forces the active status to be updated.
2379   (message ""))
2380
2381 (defun gnus-make-score-map (type)
2382   "Make a summary score map of type TYPE."
2383   (if t
2384       nil
2385     (let ((headers '(("author" "from" string)
2386                      ("subject" "subject" string)
2387                      ("article body" "body" string)
2388                      ("article head" "head" string)
2389                      ("xref" "xref" string)
2390                      ("extra header" "extra" string)
2391                      ("lines" "lines" number)
2392                      ("followups to author" "followup" string)))
2393           (types '((number ("less than" <)
2394                            ("greater than" >)
2395                            ("equal" =))
2396                    (string ("substring" s)
2397                            ("exact string" e)
2398                            ("fuzzy string" f)
2399                            ("regexp" r))))
2400           (perms '(("temporary" (current-time-string))
2401                    ("permanent" nil)
2402                    ("immediate" now)))
2403           header)
2404       (list
2405        (apply
2406         'nconc
2407         (list
2408          (if (eq type 'lower)
2409              "Lower score"
2410            "Increase score"))
2411         (let (outh)
2412           (while headers
2413             (setq header (car headers))
2414             (setq outh
2415                   (cons
2416                    (apply
2417                     'nconc
2418                     (list (car header))
2419                     (let ((ts (cdr (assoc (nth 2 header) types)))
2420                           outt)
2421                       (while ts
2422                         (setq outt
2423                               (cons
2424                                (apply
2425                                 'nconc
2426                                 (list (caar ts))
2427                                 (let ((ps perms)
2428                                       outp)
2429                                   (while ps
2430                                     (setq outp
2431                                           (cons
2432                                            (vector
2433                                             (caar ps)
2434                                             (list
2435                                              'gnus-summary-score-entry
2436                                              (nth 1 header)
2437                                              (if (or (string= (nth 1 header)
2438                                                               "head")
2439                                                      (string= (nth 1 header)
2440                                                               "body"))
2441                                                  ""
2442                                                (list 'gnus-summary-header
2443                                                      (nth 1 header)))
2444                                              (list 'quote (nth 1 (car ts)))
2445                                              (list 'gnus-score-delta-default
2446                                                    nil)
2447                                              (nth 1 (car ps))
2448                                              t)
2449                                             t)
2450                                            outp))
2451                                     (setq ps (cdr ps)))
2452                                   (list (nreverse outp))))
2453                                outt))
2454                         (setq ts (cdr ts)))
2455                       (list (nreverse outt))))
2456                    outh))
2457             (setq headers (cdr headers)))
2458           (list (nreverse outh))))))))
2459
2460 \f
2461
2462 (defun gnus-summary-mode (&optional group)
2463   "Major mode for reading articles.
2464
2465 All normal editing commands are switched off.
2466 \\<gnus-summary-mode-map>
2467 Each line in this buffer represents one article.  To read an
2468 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2469 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2470 respectively.
2471
2472 You can also post articles and send mail from this buffer.  To
2473 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2474 of an article, type `\\[gnus-summary-reply]'.
2475
2476 There are approx. one gazillion commands you can execute in this
2477 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2478
2479 The following commands are available:
2480
2481 \\{gnus-summary-mode-map}"
2482   (interactive)
2483   (kill-all-local-variables)
2484   (when (gnus-visual-p 'summary-menu 'menu)
2485     (gnus-summary-make-menu-bar)
2486     (gnus-summary-make-tool-bar))
2487   (gnus-summary-make-local-variables)
2488   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2489     (gnus-summary-make-local-variables))
2490   (gnus-make-thread-indent-array)
2491   (gnus-simplify-mode-line)
2492   (setq major-mode 'gnus-summary-mode)
2493   (setq mode-name "Summary")
2494   (make-local-variable 'minor-mode-alist)
2495   (use-local-map gnus-summary-mode-map)
2496   (buffer-disable-undo)
2497   (setq buffer-read-only t)             ;Disable modification
2498   (setq truncate-lines t)
2499   (setq selective-display t)
2500   (setq selective-display-ellipses t)   ;Display `...'
2501   (gnus-summary-set-display-table)
2502   (gnus-set-default-directory)
2503   (setq gnus-newsgroup-name group)
2504   (make-local-variable 'gnus-summary-line-format)
2505   (make-local-variable 'gnus-summary-line-format-spec)
2506   (make-local-variable 'gnus-summary-dummy-line-format)
2507   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2508   (make-local-variable 'gnus-summary-mark-positions)
2509   (make-local-hook 'pre-command-hook)
2510   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2511   (gnus-run-hooks 'gnus-summary-mode-hook)
2512   (turn-on-gnus-mailing-list-mode)
2513   (mm-enable-multibyte)
2514   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2515   (gnus-update-summary-mark-positions))
2516
2517 (defun gnus-summary-make-local-variables ()
2518   "Make all the local summary buffer variables."
2519   (let (global)
2520     (dolist (local gnus-summary-local-variables)
2521       (if (consp local)
2522           (progn
2523             (if (eq (cdr local) 'global)
2524                 ;; Copy the global value of the variable.
2525                 (setq global (symbol-value (car local)))
2526               ;; Use the value from the list.
2527               (setq global (eval (cdr local))))
2528             (set (make-local-variable (car local)) global))
2529         ;; Simple nil-valued local variable.
2530         (set (make-local-variable local) nil)))))
2531
2532 (defun gnus-summary-clear-local-variables ()
2533   (let ((locals gnus-summary-local-variables))
2534     (while locals
2535       (if (consp (car locals))
2536           (and (vectorp (caar locals))
2537                (set (caar locals) nil))
2538         (and (vectorp (car locals))
2539              (set (car locals) nil)))
2540       (setq locals (cdr locals)))))
2541
2542 ;; Summary data functions.
2543
2544 (defmacro gnus-data-number (data)
2545   `(car ,data))
2546
2547 (defmacro gnus-data-set-number (data number)
2548   `(setcar ,data ,number))
2549
2550 (defmacro gnus-data-mark (data)
2551   `(nth 1 ,data))
2552
2553 (defmacro gnus-data-set-mark (data mark)
2554   `(setcar (nthcdr 1 ,data) ,mark))
2555
2556 (defmacro gnus-data-pos (data)
2557   `(nth 2 ,data))
2558
2559 (defmacro gnus-data-set-pos (data pos)
2560   `(setcar (nthcdr 2 ,data) ,pos))
2561
2562 (defmacro gnus-data-header (data)
2563   `(nth 3 ,data))
2564
2565 (defmacro gnus-data-set-header (data header)
2566   `(setf (nth 3 ,data) ,header))
2567
2568 (defmacro gnus-data-level (data)
2569   `(nth 4 ,data))
2570
2571 (defmacro gnus-data-unread-p (data)
2572   `(= (nth 1 ,data) gnus-unread-mark))
2573
2574 (defmacro gnus-data-read-p (data)
2575   `(/= (nth 1 ,data) gnus-unread-mark))
2576
2577 (defmacro gnus-data-pseudo-p (data)
2578   `(consp (nth 3 ,data)))
2579
2580 (defmacro gnus-data-find (number)
2581   `(assq ,number gnus-newsgroup-data))
2582
2583 (defmacro gnus-data-find-list (number &optional data)
2584   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2585      (memq (assq ,number bdata)
2586            bdata)))
2587
2588 (defmacro gnus-data-make (number mark pos header level)
2589   `(list ,number ,mark ,pos ,header ,level))
2590
2591 (defun gnus-data-enter (after-article number mark pos header level offset)
2592   (let ((data (gnus-data-find-list after-article)))
2593     (unless data
2594       (error "No such article: %d" after-article))
2595     (setcdr data (cons (gnus-data-make number mark pos header level)
2596                        (cdr data)))
2597     (setq gnus-newsgroup-data-reverse nil)
2598     (gnus-data-update-list (cddr data) offset)))
2599
2600 (defun gnus-data-enter-list (after-article list &optional offset)
2601   (when list
2602     (let ((data (and after-article (gnus-data-find-list after-article)))
2603           (ilist list))
2604       (if (not (or data
2605                    after-article))
2606           (let ((odata gnus-newsgroup-data))
2607             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2608             (when offset
2609               (gnus-data-update-list odata offset)))
2610       ;; Find the last element in the list to be spliced into the main
2611         ;; list.
2612         (while (cdr list)
2613           (setq list (cdr list)))
2614         (if (not data)
2615             (progn
2616               (setcdr list gnus-newsgroup-data)
2617               (setq gnus-newsgroup-data ilist)
2618               (when offset
2619                 (gnus-data-update-list (cdr list) offset)))
2620           (setcdr list (cdr data))
2621           (setcdr data ilist)
2622           (when offset
2623             (gnus-data-update-list (cdr list) offset))))
2624       (setq gnus-newsgroup-data-reverse nil))))
2625
2626 (defun gnus-data-remove (article &optional offset)
2627   (let ((data gnus-newsgroup-data))
2628     (if (= (gnus-data-number (car data)) article)
2629         (progn
2630           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2631                 gnus-newsgroup-data-reverse nil)
2632           (when offset
2633             (gnus-data-update-list gnus-newsgroup-data offset)))
2634       (while (cdr data)
2635         (when (= (gnus-data-number (cadr data)) article)
2636           (setcdr data (cddr data))
2637           (when offset
2638             (gnus-data-update-list (cdr data) offset))
2639           (setq data nil
2640                 gnus-newsgroup-data-reverse nil))
2641         (setq data (cdr data))))))
2642
2643 (defmacro gnus-data-list (backward)
2644   `(if ,backward
2645        (or gnus-newsgroup-data-reverse
2646            (setq gnus-newsgroup-data-reverse
2647                  (reverse gnus-newsgroup-data)))
2648      gnus-newsgroup-data))
2649
2650 (defun gnus-data-update-list (data offset)
2651   "Add OFFSET to the POS of all data entries in DATA."
2652   (setq gnus-newsgroup-data-reverse nil)
2653   (while data
2654     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2655     (setq data (cdr data))))
2656
2657 (defun gnus-summary-article-pseudo-p (article)
2658   "Say whether this article is a pseudo article or not."
2659   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2660
2661 (defmacro gnus-summary-article-sparse-p (article)
2662   "Say whether this article is a sparse article or not."
2663   `(memq ,article gnus-newsgroup-sparse))
2664
2665 (defmacro gnus-summary-article-ancient-p (article)
2666   "Say whether this article is a sparse article or not."
2667   `(memq ,article gnus-newsgroup-ancient))
2668
2669 (defun gnus-article-parent-p (number)
2670   "Say whether this article is a parent or not."
2671   (let ((data (gnus-data-find-list number)))
2672     (and (cdr data)              ; There has to be an article after...
2673          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2674             (gnus-data-level (nth 1 data))))))
2675
2676 (defun gnus-article-children (number)
2677   "Return a list of all children to NUMBER."
2678   (let* ((data (gnus-data-find-list number))
2679          (level (gnus-data-level (car data)))
2680          children)
2681     (setq data (cdr data))
2682     (while (and data
2683                 (= (gnus-data-level (car data)) (1+ level)))
2684       (push (gnus-data-number (car data)) children)
2685       (setq data (cdr data)))
2686     children))
2687
2688 (defmacro gnus-summary-skip-intangible ()
2689   "If the current article is intangible, then jump to a different article."
2690   '(let ((to (get-text-property (point) 'gnus-intangible)))
2691      (and to (gnus-summary-goto-subject to))))
2692
2693 (defmacro gnus-summary-article-intangible-p ()
2694   "Say whether this article is intangible or not."
2695   '(get-text-property (point) 'gnus-intangible))
2696
2697 (defun gnus-article-read-p (article)
2698   "Say whether ARTICLE is read or not."
2699   (not (or (memq article gnus-newsgroup-marked)
2700            (memq article gnus-newsgroup-unreads)
2701            (memq article gnus-newsgroup-unselected)
2702            (memq article gnus-newsgroup-dormant))))
2703
2704 ;; Some summary mode macros.
2705
2706 (defmacro gnus-summary-article-number ()
2707   "The article number of the article on the current line.
2708 If there isn's an article number here, then we return the current
2709 article number."
2710   '(progn
2711      (gnus-summary-skip-intangible)
2712      (or (get-text-property (point) 'gnus-number)
2713          (gnus-summary-last-subject))))
2714
2715 (defmacro gnus-summary-article-header (&optional number)
2716   "Return the header of article NUMBER."
2717   `(gnus-data-header (gnus-data-find
2718                       ,(or number '(gnus-summary-article-number)))))
2719
2720 (defmacro gnus-summary-thread-level (&optional number)
2721   "Return the level of thread that starts with article NUMBER."
2722   `(if (and (eq gnus-summary-make-false-root 'dummy)
2723             (get-text-property (point) 'gnus-intangible))
2724        0
2725      (gnus-data-level (gnus-data-find
2726                        ,(or number '(gnus-summary-article-number))))))
2727
2728 (defmacro gnus-summary-article-mark (&optional number)
2729   "Return the mark of article NUMBER."
2730   `(gnus-data-mark (gnus-data-find
2731                     ,(or number '(gnus-summary-article-number)))))
2732
2733 (defmacro gnus-summary-article-pos (&optional number)
2734   "Return the position of the line of article NUMBER."
2735   `(gnus-data-pos (gnus-data-find
2736                    ,(or number '(gnus-summary-article-number)))))
2737
2738 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2739 (defmacro gnus-summary-article-subject (&optional number)
2740   "Return current subject string or nil if nothing."
2741   `(let ((headers
2742           ,(if number
2743                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2744              '(gnus-data-header (assq (gnus-summary-article-number)
2745                                       gnus-newsgroup-data)))))
2746      (and headers
2747           (vectorp headers)
2748           (mail-header-subject headers))))
2749
2750 (defmacro gnus-summary-article-score (&optional number)
2751   "Return current article score."
2752   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2753                   gnus-newsgroup-scored))
2754        gnus-summary-default-score 0))
2755
2756 (defun gnus-summary-article-children (&optional number)
2757   "Return a list of article numbers that are children of article NUMBER."
2758   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2759          (level (gnus-data-level (car data)))
2760          l children)
2761     (while (and (setq data (cdr data))
2762                 (> (setq l (gnus-data-level (car data))) level))
2763       (and (= (1+ level) l)
2764            (push (gnus-data-number (car data))
2765                  children)))
2766     (nreverse children)))
2767
2768 (defun gnus-summary-article-parent (&optional number)
2769   "Return the article number of the parent of article NUMBER."
2770   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2771                                     (gnus-data-list t)))
2772          (level (gnus-data-level (car data))))
2773     (if (zerop level)
2774         ()                              ; This is a root.
2775       ;; We search until we find an article with a level less than
2776       ;; this one.  That function has to be the parent.
2777       (while (and (setq data (cdr data))
2778                   (not (< (gnus-data-level (car data)) level))))
2779       (and data (gnus-data-number (car data))))))
2780
2781 (defun gnus-unread-mark-p (mark)
2782   "Say whether MARK is the unread mark."
2783   (= mark gnus-unread-mark))
2784
2785 (defun gnus-read-mark-p (mark)
2786   "Say whether MARK is one of the marks that mark as read.
2787 This is all marks except unread, ticked, dormant, and expirable."
2788   (not (or (= mark gnus-unread-mark)
2789            (= mark gnus-ticked-mark)
2790            (= mark gnus-dormant-mark)
2791            (= mark gnus-expirable-mark))))
2792
2793 (defmacro gnus-article-mark (number)
2794   "Return the MARK of article NUMBER.
2795 This macro should only be used when computing the mark the \"first\"
2796 time; i.e., when generating the summary lines.  After that,
2797 `gnus-summary-article-mark' should be used to examine the
2798 marks of articles."
2799   `(cond
2800     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2801     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2802     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2803     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2804     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2805     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2806     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2807     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2808            gnus-ancient-mark))))
2809
2810 ;; Saving hidden threads.
2811
2812 (defmacro gnus-save-hidden-threads (&rest forms)
2813   "Save hidden threads, eval FORMS, and restore the hidden threads."
2814   (let ((config (make-symbol "config")))
2815     `(let ((,config (gnus-hidden-threads-configuration)))
2816        (unwind-protect
2817            (save-excursion
2818              ,@forms)
2819          (gnus-restore-hidden-threads-configuration ,config)))))
2820 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2821 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2822
2823 (defun gnus-data-compute-positions ()
2824   "Compute the positions of all articles."
2825   (setq gnus-newsgroup-data-reverse nil)
2826   (let ((data gnus-newsgroup-data))
2827     (save-excursion
2828       (gnus-save-hidden-threads
2829         (gnus-summary-show-all-threads)
2830         (goto-char (point-min))
2831         (while data
2832           (while (get-text-property (point) 'gnus-intangible)
2833             (forward-line 1))
2834           (gnus-data-set-pos (car data) (+ (point) 3))
2835           (setq data (cdr data))
2836           (forward-line 1))))))
2837
2838 (defun gnus-hidden-threads-configuration ()
2839   "Return the current hidden threads configuration."
2840   (save-excursion
2841     (let (config)
2842       (goto-char (point-min))
2843       (while (search-forward "\r" nil t)
2844         (push (1- (point)) config))
2845       config)))
2846
2847 (defun gnus-restore-hidden-threads-configuration (config)
2848   "Restore hidden threads configuration from CONFIG."
2849   (save-excursion
2850     (let (point buffer-read-only)
2851       (while (setq point (pop config))
2852         (when (and (< point (point-max))
2853                    (goto-char point)
2854                    (eq (char-after) ?\n))
2855           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2856
2857 ;; Various summary mode internalish functions.
2858
2859 (defun gnus-mouse-pick-article (e)
2860   (interactive "e")
2861   (mouse-set-point e)
2862   (gnus-summary-next-page nil t))
2863
2864 (defun gnus-summary-set-display-table ()
2865   "Change the display table.
2866 Odd characters have a tendency to mess
2867 up nicely formatted displays - we make all possible glyphs
2868 display only a single character."
2869
2870   ;; We start from the standard display table, if any.
2871   (let ((table (or (copy-sequence standard-display-table)
2872                    (make-display-table)))
2873         (i 32))
2874     ;; Nix out all the control chars...
2875     (while (>= (setq i (1- i)) 0)
2876       (aset table i [??]))
2877    ;; ... but not newline and cr, of course.  (cr is necessary for the
2878     ;; selective display).
2879     (aset table ?\n nil)
2880     (aset table ?\r nil)
2881     ;; We keep TAB as well.
2882     (aset table ?\t nil)
2883     ;; We nix out any glyphs over 126 that are not set already.
2884     (let ((i 256))
2885       (while (>= (setq i (1- i)) 127)
2886         ;; Only modify if the entry is nil.
2887         (unless (aref table i)
2888           (aset table i [??]))))
2889     (setq buffer-display-table table)))
2890
2891 (defun gnus-summary-set-article-display-arrow (pos)
2892   "Update the overlay arrow to point to line at position POS."
2893   (when (and gnus-summary-display-arrow
2894              (boundp 'overlay-arrow-position)
2895              (boundp 'overlay-arrow-string))
2896     (save-excursion
2897       (goto-char pos)
2898       (beginning-of-line)
2899       (unless overlay-arrow-position
2900         (setq overlay-arrow-position (make-marker)))
2901       (setq overlay-arrow-string "=>"
2902             overlay-arrow-position (set-marker overlay-arrow-position
2903                                                (point)
2904                                                (current-buffer))))))
2905
2906 (defun gnus-summary-buffer-name (group)
2907   "Return the summary buffer name of GROUP."
2908   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2909
2910 (defun gnus-summary-setup-buffer (group)
2911   "Initialize summary buffer."
2912   (let ((buffer (gnus-summary-buffer-name group))
2913         (dead-name (concat "*Dead Summary "
2914                            (gnus-group-decoded-name group) "*")))
2915     ;; If a dead summary buffer exists, we kill it.
2916     (when (gnus-buffer-live-p dead-name)
2917       (gnus-kill-buffer dead-name))
2918     (if (get-buffer buffer)
2919         (progn
2920           (set-buffer buffer)
2921           (setq gnus-summary-buffer (current-buffer))
2922           (not gnus-newsgroup-prepared))
2923       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2924       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2925       (gnus-summary-mode group)
2926       (when gnus-carpal
2927         (gnus-carpal-setup-buffer 'summary))
2928       (unless gnus-single-article-buffer
2929         (make-local-variable 'gnus-article-buffer)
2930         (make-local-variable 'gnus-article-current)
2931         (make-local-variable 'gnus-original-article-buffer))
2932       (setq gnus-newsgroup-name group)
2933       ;; Set any local variables in the group parameters.
2934       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2935       t)))
2936
2937 (defun gnus-set-global-variables ()
2938   "Set the global equivalents of the buffer-local variables.
2939 They are set to the latest values they had.  These reflect the summary
2940 buffer that was in action when the last article was fetched."
2941   (when (eq major-mode 'gnus-summary-mode)
2942     (setq gnus-summary-buffer (current-buffer))
2943     (let ((name gnus-newsgroup-name)
2944           (marked gnus-newsgroup-marked)
2945           (unread gnus-newsgroup-unreads)
2946           (headers gnus-current-headers)
2947           (data gnus-newsgroup-data)
2948           (summary gnus-summary-buffer)
2949           (article-buffer gnus-article-buffer)
2950           (original gnus-original-article-buffer)
2951           (gac gnus-article-current)
2952           (reffed gnus-reffed-article-number)
2953           (score-file gnus-current-score-file)
2954           (default-charset gnus-newsgroup-charset)
2955           vlist)
2956       (let ((locals gnus-newsgroup-variables))
2957         (while locals
2958           (if (consp (car locals))
2959               (push (eval (caar locals)) vlist)
2960             (push (eval (car locals)) vlist))
2961           (setq locals (cdr locals)))
2962         (setq vlist (nreverse vlist)))
2963       (save-excursion
2964         (set-buffer gnus-group-buffer)
2965         (setq gnus-newsgroup-name name
2966               gnus-newsgroup-marked marked
2967               gnus-newsgroup-unreads unread
2968               gnus-current-headers headers
2969               gnus-newsgroup-data data
2970               gnus-article-current gac
2971               gnus-summary-buffer summary
2972               gnus-article-buffer article-buffer
2973               gnus-original-article-buffer original
2974               gnus-reffed-article-number reffed
2975               gnus-current-score-file score-file
2976               gnus-newsgroup-charset default-charset)
2977         (let ((locals gnus-newsgroup-variables))
2978           (while locals
2979             (if (consp (car locals))
2980                 (set (caar locals) (pop vlist))
2981               (set (car locals) (pop vlist)))
2982             (setq locals (cdr locals))))
2983         ;; The article buffer also has local variables.
2984         (when (gnus-buffer-live-p gnus-article-buffer)
2985           (set-buffer gnus-article-buffer)
2986           (setq gnus-summary-buffer summary))))))
2987
2988 (defun gnus-summary-article-unread-p (article)
2989   "Say whether ARTICLE is unread or not."
2990   (memq article gnus-newsgroup-unreads))
2991
2992 (defun gnus-summary-first-article-p (&optional article)
2993   "Return whether ARTICLE is the first article in the buffer."
2994   (if (not (setq article (or article (gnus-summary-article-number))))
2995       nil
2996     (eq article (caar gnus-newsgroup-data))))
2997
2998 (defun gnus-summary-last-article-p (&optional article)
2999   "Return whether ARTICLE is the last article in the buffer."
3000   (if (not (setq article (or article (gnus-summary-article-number))))
3001       ;; All non-existent numbers are the last article.  :-)
3002       t
3003     (not (cdr (gnus-data-find-list article)))))
3004
3005 (defun gnus-make-thread-indent-array ()
3006   (let ((n 200))
3007     (unless (and gnus-thread-indent-array
3008                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3009       (setq gnus-thread-indent-array (make-vector 201 "")
3010             gnus-thread-indent-array-level gnus-thread-indent-level)
3011       (while (>= n 0)
3012         (aset gnus-thread-indent-array n
3013               (make-string (* n gnus-thread-indent-level) ? ))
3014         (setq n (1- n))))))
3015
3016 (defun gnus-update-summary-mark-positions ()
3017   "Compute where the summary marks are to go."
3018   (save-excursion
3019     (when (gnus-buffer-exists-p gnus-summary-buffer)
3020       (set-buffer gnus-summary-buffer))
3021     (let ((gnus-replied-mark 129)
3022           (gnus-score-below-mark 130)
3023           (gnus-score-over-mark 130)
3024           (gnus-download-mark 131)
3025           (spec gnus-summary-line-format-spec)
3026           gnus-visual pos)
3027       (save-excursion
3028         (gnus-set-work-buffer)
3029         (let ((gnus-summary-line-format-spec spec)
3030               (gnus-newsgroup-downloadable '((0 . t))))
3031           (gnus-summary-insert-line
3032            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3033            0 nil 128 t nil "" nil 1)
3034           (goto-char (point-min))
3035           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3036                                              (- (point) 2)))))
3037           (goto-char (point-min))
3038           (push (cons 'replied (and (search-forward "\201" nil t)
3039                                     (- (point) 2)))
3040                 pos)
3041           (goto-char (point-min))
3042           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
3043                 pos)
3044           (goto-char (point-min))
3045           (push (cons 'download
3046                       (and (search-forward "\203" nil t) (- (point) 2)))
3047                 pos)))
3048       (setq gnus-summary-mark-positions pos))))
3049
3050 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3051   "Insert a dummy root in the summary buffer."
3052   (beginning-of-line)
3053   (gnus-add-text-properties
3054    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3055    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3056
3057 (defun gnus-summary-extract-address-component (from)
3058   (or (car (funcall gnus-extract-address-components from))
3059       from))
3060
3061 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3062   (let ((mail-parse-charset gnus-newsgroup-charset)
3063         ; Is it really necessary to do this next part for each summary line?
3064         ; Luckily, doesn't seem to slow things down much.
3065         (mail-parse-ignored-charsets
3066          (save-excursion (set-buffer gnus-summary-buffer)
3067                          gnus-newsgroup-ignored-charsets)))
3068     (or
3069      (and gnus-ignored-from-addresses
3070           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3071           (let ((extra-headers (mail-header-extra header))
3072                 to
3073                 newsgroups)
3074             (cond
3075              ((setq to (cdr (assq 'To extra-headers)))
3076               (concat "-> "
3077                       (inline
3078                         (gnus-summary-extract-address-component
3079                          (funcall gnus-decode-encoded-word-function to)))))
3080              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3081               (concat "=> " newsgroups)))))
3082      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3083
3084 (defun gnus-summary-insert-line (gnus-tmp-header
3085                                  gnus-tmp-level gnus-tmp-current
3086                                  gnus-tmp-unread gnus-tmp-replied
3087                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3088                                  &optional gnus-tmp-dummy gnus-tmp-score
3089                                  gnus-tmp-process)
3090   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3091          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3092          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3093          (gnus-tmp-score-char
3094           (if (or (null gnus-summary-default-score)
3095                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3096                       gnus-summary-zcore-fuzz))
3097               ?                         ;Whitespace
3098             (if (< gnus-tmp-score gnus-summary-default-score)
3099                 gnus-score-below-mark gnus-score-over-mark)))
3100          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3101          (gnus-tmp-replied
3102           (cond (gnus-tmp-process gnus-process-mark)
3103                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3104                  gnus-cached-mark)
3105                 (gnus-tmp-replied gnus-replied-mark)
3106                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3107                  gnus-forwarded-mark)
3108                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3109                  gnus-saved-mark)
3110                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3111                  gnus-recent-mark)
3112                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3113                  gnus-unseen-mark)
3114                 (t gnus-no-mark)))
3115          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3116          (gnus-tmp-name
3117           (cond
3118            ((string-match "<[^>]+> *$" gnus-tmp-from)
3119             (let ((beg (match-beginning 0)))
3120               (or (and (string-match "^\".+\"" gnus-tmp-from)
3121                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3122                   (substring gnus-tmp-from 0 beg))))
3123            ((string-match "(.+)" gnus-tmp-from)
3124             (substring gnus-tmp-from
3125                        (1+ (match-beginning 0)) (1- (match-end 0))))
3126            (t gnus-tmp-from)))
3127          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3128          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3129          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3130          (buffer-read-only nil))
3131     (when (string= gnus-tmp-name "")
3132       (setq gnus-tmp-name gnus-tmp-from))
3133     (unless (numberp gnus-tmp-lines)
3134       (setq gnus-tmp-lines -1))
3135     (if (= gnus-tmp-lines -1)
3136         (setq gnus-tmp-lines "?")
3137       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3138     (gnus-put-text-property
3139      (point)
3140      (progn (eval gnus-summary-line-format-spec) (point))
3141      'gnus-number gnus-tmp-number)
3142     (when (gnus-visual-p 'summary-highlight 'highlight)
3143       (forward-line -1)
3144       (gnus-run-hooks 'gnus-summary-update-hook)
3145       (forward-line 1))))
3146
3147 (defun gnus-summary-update-line (&optional dont-update)
3148   "Update summary line after change."
3149   (when (and gnus-summary-default-score
3150              (not gnus-summary-inhibit-highlight))
3151     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3152            (article (gnus-summary-article-number))
3153            (score (gnus-summary-article-score article)))
3154       (unless dont-update
3155         (if (and gnus-summary-mark-below
3156                  (< (gnus-summary-article-score)
3157                     gnus-summary-mark-below))
3158             ;; This article has a low score, so we mark it as read.
3159             (when (memq article gnus-newsgroup-unreads)
3160               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3161           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3162             ;; This article was previously marked as read on account
3163             ;; of a low score, but now it has risen, so we mark it as
3164             ;; unread.
3165             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3166         (gnus-summary-update-mark
3167          (if (or (null gnus-summary-default-score)
3168                  (<= (abs (- score gnus-summary-default-score))
3169                      gnus-summary-zcore-fuzz))
3170              ?                          ;Whitespace
3171            (if (< score gnus-summary-default-score)
3172                gnus-score-below-mark gnus-score-over-mark))
3173          'score))
3174       ;; Do visual highlighting.
3175       (when (gnus-visual-p 'summary-highlight 'highlight)
3176         (gnus-run-hooks 'gnus-summary-update-hook)))))
3177
3178 (defvar gnus-tmp-new-adopts nil)
3179
3180 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3181   "Return the number of articles in THREAD.
3182 This may be 0 in some cases -- if none of the articles in
3183 the thread are to be displayed."
3184   (let* ((number
3185          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3186           (cond
3187            ((not (listp thread))
3188             1)
3189            ((and (consp thread) (cdr thread))
3190             (apply
3191              '+ 1 (mapcar
3192                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3193            ((null thread)
3194             1)
3195            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3196             1)
3197            (t 0))))
3198     (when (and level (zerop level) gnus-tmp-new-adopts)
3199       (incf number
3200             (apply '+ (mapcar
3201                        'gnus-summary-number-of-articles-in-thread
3202                        gnus-tmp-new-adopts))))
3203     (if char
3204         (if (> number 1) gnus-not-empty-thread-mark
3205           gnus-empty-thread-mark)
3206       number)))
3207
3208 (defun gnus-summary-set-local-parameters (group)
3209   "Go through the local params of GROUP and set all variable specs in that list."
3210   (let ((params (gnus-group-find-parameter group))
3211         (vars '(quit-config))           ; Ignore quit-config.
3212         elem)
3213     (while params
3214       (setq elem (car params)
3215             params (cdr params))
3216       (and (consp elem)                 ; Has to be a cons.
3217            (consp (cdr elem))           ; The cdr has to be a list.
3218            (symbolp (car elem))         ; Has to be a symbol in there.
3219            (not (memq (car elem) vars))
3220            (ignore-errors               ; So we set it.
3221              (push (car elem) vars)
3222              (make-local-variable (car elem))
3223              (set (car elem) (eval (nth 1 elem))))))))
3224
3225 (defun gnus-summary-read-group (group &optional show-all no-article
3226                                       kill-buffer no-display backward
3227                                       select-articles)
3228   "Start reading news in newsgroup GROUP.
3229 If SHOW-ALL is non-nil, already read articles are also listed.
3230 If NO-ARTICLE is non-nil, no article is selected initially.
3231 If NO-DISPLAY, don't generate a summary buffer."
3232   (let (result)
3233     (while (and group
3234                 (null (setq result
3235                             (let ((gnus-auto-select-next nil))
3236                               (or (gnus-summary-read-group-1
3237                                    group show-all no-article
3238                                    kill-buffer no-display
3239                                    select-articles)
3240                                   (setq show-all nil
3241                                         select-articles nil)))))
3242                 (eq gnus-auto-select-next 'quietly))
3243       (set-buffer gnus-group-buffer)
3244       ;; The entry function called above goes to the next
3245       ;; group automatically, so we go two groups back
3246       ;; if we are searching for the previous group.
3247       (when backward
3248         (gnus-group-prev-unread-group 2))
3249       (if (not (equal group (gnus-group-group-name)))
3250           (setq group (gnus-group-group-name))
3251         (setq group nil)))
3252     result))
3253
3254 (defun gnus-summary-read-group-1 (group show-all no-article
3255                                         kill-buffer no-display
3256                                         &optional select-articles)
3257   ;; Killed foreign groups can't be entered.
3258   ;;  (when (and (not (gnus-group-native-p group))
3259   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3260   ;;    (error "Dead non-native groups can't be entered"))
3261   (gnus-message 5 "Retrieving newsgroup: %s..."
3262                 (gnus-group-decoded-name group))
3263   (let* ((new-group (gnus-summary-setup-buffer group))
3264          (quit-config (gnus-group-quit-config group))
3265          (did-select (and new-group (gnus-select-newsgroup
3266                                      group show-all select-articles))))
3267     (cond
3268      ;; This summary buffer exists already, so we just select it.
3269      ((not new-group)
3270       (gnus-set-global-variables)
3271       (when kill-buffer
3272         (gnus-kill-or-deaden-summary kill-buffer))
3273       (gnus-configure-windows 'summary 'force)
3274       (gnus-set-mode-line 'summary)
3275       (gnus-summary-position-point)
3276       (message "")
3277       t)
3278      ;; We couldn't select this group.
3279      ((null did-select)
3280       (when (and (eq major-mode 'gnus-summary-mode)
3281                  (not (equal (current-buffer) kill-buffer)))
3282         (kill-buffer (current-buffer))
3283         (if (not quit-config)
3284             (progn
3285               ;; Update the info -- marks might need to be removed,
3286               ;; for instance.
3287               (gnus-summary-update-info)
3288               (set-buffer gnus-group-buffer)
3289               (gnus-group-jump-to-group group)
3290               (gnus-group-next-unread-group 1))
3291           (gnus-handle-ephemeral-exit quit-config)))
3292       (let ((grpinfo (gnus-get-info group)))
3293         (if (null (gnus-info-read grpinfo))
3294             (gnus-message 3 "Group %s contains no messages"
3295                           (gnus-group-decoded-name group))
3296           (gnus-message 3 "Can't select group")))
3297       nil)
3298      ;; The user did a `C-g' while prompting for number of articles,
3299      ;; so we exit this group.
3300      ((eq did-select 'quit)
3301       (and (eq major-mode 'gnus-summary-mode)
3302            (not (equal (current-buffer) kill-buffer))
3303            (kill-buffer (current-buffer)))
3304       (when kill-buffer
3305         (gnus-kill-or-deaden-summary kill-buffer))
3306       (if (not quit-config)
3307           (progn
3308             (set-buffer gnus-group-buffer)
3309             (gnus-group-jump-to-group group)
3310             (gnus-group-next-unread-group 1)
3311             (gnus-configure-windows 'group 'force))
3312         (gnus-handle-ephemeral-exit quit-config))
3313       ;; Finally signal the quit.
3314       (signal 'quit nil))
3315      ;; The group was successfully selected.
3316      (t
3317       (gnus-set-global-variables)
3318       ;; Save the active value in effect when the group was entered.
3319       (setq gnus-newsgroup-active
3320             (gnus-copy-sequence
3321              (gnus-active gnus-newsgroup-name)))
3322       ;; You can change the summary buffer in some way with this hook.
3323       (gnus-run-hooks 'gnus-select-group-hook)
3324       (gnus-update-format-specifications
3325        nil 'summary 'summary-mode 'summary-dummy)
3326       (gnus-update-summary-mark-positions)
3327       ;; Do score processing.
3328       (when gnus-use-scoring
3329         (gnus-possibly-score-headers))
3330       ;; Check whether to fill in the gaps in the threads.
3331       (when gnus-build-sparse-threads
3332         (gnus-build-sparse-threads))
3333       ;; Find the initial limit.
3334       (if gnus-show-threads
3335           (if show-all
3336               (let ((gnus-newsgroup-dormant nil))
3337                 (gnus-summary-initial-limit show-all))
3338             (gnus-summary-initial-limit show-all))
3339         ;; When untreaded, all articles are always shown.
3340         (setq gnus-newsgroup-limit
3341               (mapcar
3342                (lambda (header) (mail-header-number header))
3343                gnus-newsgroup-headers)))
3344       ;; Generate the summary buffer.
3345       (unless no-display
3346         (gnus-summary-prepare))
3347       (when gnus-use-trees
3348         (gnus-tree-open group)
3349         (setq gnus-summary-highlight-line-function
3350               'gnus-tree-highlight-article))
3351       ;; If the summary buffer is empty, but there are some low-scored
3352       ;; articles or some excluded dormants, we include these in the
3353       ;; buffer.
3354       (when (and (zerop (buffer-size))
3355                  (not no-display))
3356         (cond (gnus-newsgroup-dormant
3357                (gnus-summary-limit-include-dormant))
3358               ((and gnus-newsgroup-scored show-all)
3359                (gnus-summary-limit-include-expunged t))))
3360       ;; Function `gnus-apply-kill-file' must be called in this hook.
3361       (gnus-run-hooks 'gnus-apply-kill-hook)
3362       (if (and (zerop (buffer-size))
3363                (not no-display))
3364           (progn
3365             ;; This newsgroup is empty.
3366             (gnus-summary-catchup-and-exit nil t)
3367             (gnus-message 6 "No unread news")
3368             (when kill-buffer
3369               (gnus-kill-or-deaden-summary kill-buffer))
3370             ;; Return nil from this function.
3371             nil)
3372         ;; Hide conversation thread subtrees.  We cannot do this in
3373         ;; gnus-summary-prepare-hook since kill processing may not
3374         ;; work with hidden articles.
3375         (gnus-summary-maybe-hide-threads)
3376         (when kill-buffer
3377           (gnus-kill-or-deaden-summary kill-buffer))
3378         (gnus-summary-auto-select-subject)
3379         ;; Show first unread article if requested.
3380         (if (and (not no-article)
3381                  (not no-display)
3382                  gnus-newsgroup-unreads
3383                  gnus-auto-select-first)
3384             (progn
3385               (gnus-configure-windows 'summary)
3386               (let ((art (gnus-summary-article-number)))
3387                 (unless (or (memq art gnus-newsgroup-undownloaded)
3388                             (memq art gnus-newsgroup-downloadable))
3389                   (gnus-summary-goto-article art))))
3390           ;; Don't select any articles.
3391           (gnus-summary-position-point)
3392           (gnus-configure-windows 'summary 'force)
3393           (gnus-set-mode-line 'summary))
3394         (when (get-buffer-window gnus-group-buffer t)
3395           ;; Gotta use windows, because recenter does weird stuff if
3396           ;; the current buffer ain't the displayed window.
3397           (let ((owin (selected-window)))
3398             (select-window (get-buffer-window gnus-group-buffer t))
3399             (when (gnus-group-goto-group group)
3400               (recenter))
3401             (select-window owin)))
3402         ;; Mark this buffer as "prepared".
3403         (setq gnus-newsgroup-prepared t)
3404         (gnus-run-hooks 'gnus-summary-prepared-hook)
3405         t)))))
3406
3407 (defun gnus-summary-auto-select-subject ()
3408   "Select the subject line on initial group entry."
3409   (goto-char (point-min))
3410   (cond
3411    ((eq gnus-auto-select-subject 'best)
3412     (gnus-summary-best-unread-subject))
3413    ((eq gnus-auto-select-subject 'unread)
3414     (gnus-summary-first-unread-subject))
3415    ((eq gnus-auto-select-subject 'unseen)
3416     (gnus-summary-first-unseen-subject))
3417    ((eq gnus-auto-select-subject 'unseen-or-unread)
3418     (gnus-summary-first-unseen-or-unread-subject))
3419    ((eq gnus-auto-select-subject 'first)
3420     ;; Do nothing.
3421     )
3422    ((gnus-functionp gnus-auto-select-subject)
3423     (funcall gnus-auto-select-subject))))
3424
3425 (defun gnus-summary-prepare ()
3426   "Generate the summary buffer."
3427   (interactive)
3428   (let ((buffer-read-only nil))
3429     (erase-buffer)
3430     (setq gnus-newsgroup-data nil
3431           gnus-newsgroup-data-reverse nil)
3432     (gnus-run-hooks 'gnus-summary-generate-hook)
3433     ;; Generate the buffer, either with threads or without.
3434     (when gnus-newsgroup-headers
3435       (gnus-summary-prepare-threads
3436        (if gnus-show-threads
3437            (gnus-sort-gathered-threads
3438             (funcall gnus-summary-thread-gathering-function
3439                      (gnus-sort-threads
3440                       (gnus-cut-threads (gnus-make-threads)))))
3441          ;; Unthreaded display.
3442          (gnus-sort-articles gnus-newsgroup-headers))))
3443     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3444     ;; Call hooks for modifying summary buffer.
3445     (goto-char (point-min))
3446     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3447
3448 (defsubst gnus-general-simplify-subject (subject)
3449   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3450   (setq subject
3451         (cond
3452          ;; Truncate the subject.
3453          (gnus-simplify-subject-functions
3454           (gnus-map-function gnus-simplify-subject-functions subject))
3455          ((numberp gnus-summary-gather-subject-limit)
3456           (setq subject (gnus-simplify-subject-re subject))
3457           (if (> (length subject) gnus-summary-gather-subject-limit)
3458               (substring subject 0 gnus-summary-gather-subject-limit)
3459             subject))
3460          ;; Fuzzily simplify it.
3461          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3462           (gnus-simplify-subject-fuzzy subject))
3463          ;; Just remove the leading "Re:".
3464          (t
3465           (gnus-simplify-subject-re subject))))
3466
3467   (if (and gnus-summary-gather-exclude-subject
3468            (string-match gnus-summary-gather-exclude-subject subject))
3469       nil                         ; This article shouldn't be gathered
3470     subject))
3471
3472 (defun gnus-summary-simplify-subject-query ()
3473   "Query where the respool algorithm would put this article."
3474   (interactive)
3475   (gnus-summary-select-article)
3476   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3477
3478 (defun gnus-gather-threads-by-subject (threads)
3479   "Gather threads by looking at Subject headers."
3480   (if (not gnus-summary-make-false-root)
3481       threads
3482     (let ((hashtb (gnus-make-hashtable 1024))
3483           (prev threads)
3484           (result threads)
3485           subject hthread whole-subject)
3486       (while threads
3487         (setq subject (gnus-general-simplify-subject
3488                        (setq whole-subject (mail-header-subject
3489                                             (caar threads)))))
3490         (when subject
3491           (if (setq hthread (gnus-gethash subject hashtb))
3492               (progn
3493                 ;; We enter a dummy root into the thread, if we
3494                 ;; haven't done that already.
3495                 (unless (stringp (caar hthread))
3496                   (setcar hthread (list whole-subject (car hthread))))
3497                 ;; We add this new gathered thread to this gathered
3498                 ;; thread.
3499                 (setcdr (car hthread)
3500                         (nconc (cdar hthread) (list (car threads))))
3501                 ;; Remove it from the list of threads.
3502                 (setcdr prev (cdr threads))
3503                 (setq threads prev))
3504             ;; Enter this thread into the hash table.
3505             (gnus-sethash subject threads hashtb)))
3506         (setq prev threads)
3507         (setq threads (cdr threads)))
3508       result)))
3509
3510 (defun gnus-gather-threads-by-references (threads)
3511   "Gather threads by looking at References headers."
3512   (let ((idhashtb (gnus-make-hashtable 1024))
3513         (thhashtb (gnus-make-hashtable 1024))
3514         (prev threads)
3515         (result threads)
3516         ids references id gthread gid entered ref)
3517     (while threads
3518       (when (setq references (mail-header-references (caar threads)))
3519         (setq id (mail-header-id (caar threads))
3520               ids (inline (gnus-split-references references))
3521               entered nil)
3522         (while (setq ref (pop ids))
3523           (setq ids (delete ref ids))
3524           (if (not (setq gid (gnus-gethash ref idhashtb)))
3525               (progn
3526                 (gnus-sethash ref id idhashtb)
3527                 (gnus-sethash id threads thhashtb))
3528             (setq gthread (gnus-gethash gid thhashtb))
3529             (unless entered
3530               ;; We enter a dummy root into the thread, if we
3531               ;; haven't done that already.
3532               (unless (stringp (caar gthread))
3533                 (setcar gthread (list (mail-header-subject (caar gthread))
3534                                       (car gthread))))
3535               ;; We add this new gathered thread to this gathered
3536               ;; thread.
3537               (setcdr (car gthread)
3538                       (nconc (cdar gthread) (list (car threads)))))
3539             ;; Add it into the thread hash table.
3540             (gnus-sethash id gthread thhashtb)
3541             (setq entered t)
3542             ;; Remove it from the list of threads.
3543             (setcdr prev (cdr threads))
3544             (setq threads prev))))
3545       (setq prev threads)
3546       (setq threads (cdr threads)))
3547     result))
3548
3549 (defun gnus-sort-gathered-threads (threads)
3550   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3551   (let ((result threads))
3552     (while threads
3553       (when (stringp (caar threads))
3554         (setcdr (car threads)
3555                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3556       (setq threads (cdr threads)))
3557     result))
3558
3559 (defun gnus-thread-loop-p (root thread)
3560   "Say whether ROOT is in THREAD."
3561   (let ((stack (list thread))
3562         (infloop 0)
3563         th)
3564     (while (setq thread (pop stack))
3565       (setq th (cdr thread))
3566       (while (and th
3567                   (not (eq (caar th) root)))
3568         (pop th))
3569       (if th
3570           ;; We have found a loop.
3571           (let (ref-dep)
3572             (setcdr thread (delq (car th) (cdr thread)))
3573             (if (boundp (setq ref-dep (intern "none"
3574                                               gnus-newsgroup-dependencies)))
3575                 (setcdr (symbol-value ref-dep)
3576                         (nconc (cdr (symbol-value ref-dep))
3577                                (list (car th))))
3578               (set ref-dep (list nil (car th))))
3579             (setq infloop 1
3580                   stack nil))
3581         ;; Push all the subthreads onto the stack.
3582         (push (cdr thread) stack)))
3583     infloop))
3584
3585 (defun gnus-make-threads ()
3586   "Go through the dependency hashtb and find the roots.  Return all threads."
3587   (let (threads)
3588     (while (catch 'infloop
3589              (mapatoms
3590               (lambda (refs)
3591                 ;; Deal with self-referencing References loops.
3592                 (when (and (car (symbol-value refs))
3593                            (not (zerop
3594                                  (apply
3595                                   '+
3596                                   (mapcar
3597                                    (lambda (thread)
3598                                      (gnus-thread-loop-p
3599                                       (car (symbol-value refs)) thread))
3600                                    (cdr (symbol-value refs)))))))
3601                   (setq threads nil)
3602                   (throw 'infloop t))
3603                 (unless (car (symbol-value refs))
3604                   ;; These threads do not refer back to any other
3605                   ;; articles, so they're roots.
3606                   (setq threads (append (cdr (symbol-value refs)) threads))))
3607               gnus-newsgroup-dependencies)))
3608     threads))
3609
3610 ;; Build the thread tree.
3611 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3612   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3613
3614 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3615 if it was already present.
3616
3617 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3618 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3619 Message-IDs will be renamed to a unique Message-ID before being
3620 entered.
3621
3622 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3623   (let* ((id (mail-header-id header))
3624          (id-dep (and id (intern id dependencies)))
3625          ref ref-dep ref-header replaced)
3626     ;; Enter this `header' in the `dependencies' table.
3627     (cond
3628      ((not id-dep)
3629       (setq header nil))
3630      ;; The first two cases do the normal part: enter a new `header'
3631      ;; in the `dependencies' table.
3632      ((not (boundp id-dep))
3633       (set id-dep (list header)))
3634      ((null (car (symbol-value id-dep)))
3635       (setcar (symbol-value id-dep) header))
3636
3637      ;; From here the `header' was already present in the
3638      ;; `dependencies' table.
3639      (force-new
3640       ;; Overrides an existing entry;
3641       ;; just set the header part of the entry.
3642       (setcar (symbol-value id-dep) header)
3643       (setq replaced t))
3644
3645      ;; Renames the existing `header' to a unique Message-ID.
3646      ((not gnus-summary-ignore-duplicates)
3647       ;; An article with this Message-ID has already been seen.
3648       ;; We rename the Message-ID.
3649       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3650            (list header))
3651       (mail-header-set-id header id))
3652
3653      ;; The last case ignores an existing entry, except it adds any
3654      ;; additional Xrefs (in case the two articles came from different
3655      ;; servers.
3656      ;; Also sets `header' to `nil' meaning that the `dependencies'
3657      ;; table was *not* modified.
3658      (t
3659       (mail-header-set-xref
3660        (car (symbol-value id-dep))
3661        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3662                    "")
3663                (or (mail-header-xref header) "")))
3664       (setq header nil)))
3665
3666     (when (and header (not replaced))
3667       ;; First check that we are not creating a References loop.
3668       (setq ref (gnus-parent-id (mail-header-references header)))
3669       (while (and ref
3670                   (setq ref-dep (intern-soft ref dependencies))
3671                   (boundp ref-dep)
3672                   (setq ref-header (car (symbol-value ref-dep))))
3673         (if (string= id ref)
3674             ;; Yuk!  This is a reference loop.  Make the article be a
3675             ;; root article.
3676             (progn
3677               (mail-header-set-references (car (symbol-value id-dep)) "none")
3678               (setq ref nil))
3679           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3680       (setq ref (gnus-parent-id (mail-header-references header)))
3681       (setq ref-dep (intern (or ref "none") dependencies))
3682       (if (boundp ref-dep)
3683           (setcdr (symbol-value ref-dep)
3684                   (nconc (cdr (symbol-value ref-dep))
3685                          (list (symbol-value id-dep))))
3686         (set ref-dep (list nil (symbol-value id-dep)))))
3687     header))
3688
3689 (defun gnus-extract-message-id-from-in-reply-to (string)
3690   (if (string-match "<[^>]+>" string)
3691       (substring string (match-beginning 0) (match-end 0))
3692     nil))
3693
3694 (defun gnus-build-sparse-threads ()
3695   (let ((headers gnus-newsgroup-headers)
3696         (mail-parse-charset gnus-newsgroup-charset)
3697         (gnus-summary-ignore-duplicates t)
3698         header references generation relations
3699         subject child end new-child date)
3700     ;; First we create an alist of generations/relations, where
3701     ;; generations is how much we trust the relation, and the relation
3702     ;; is parent/child.
3703     (gnus-message 7 "Making sparse threads...")
3704     (save-excursion
3705       (nnheader-set-temp-buffer " *gnus sparse threads*")
3706       (while (setq header (pop headers))
3707         (when (and (setq references (mail-header-references header))
3708                    (not (string= references "")))
3709           (insert references)
3710           (setq child (mail-header-id header)
3711                 subject (mail-header-subject header)
3712                 date (mail-header-date header)
3713                 generation 0)
3714           (while (search-backward ">" nil t)
3715             (setq end (1+ (point)))
3716             (when (search-backward "<" nil t)
3717               (setq new-child (buffer-substring (point) end))
3718               (push (list (incf generation)
3719                           child (setq child new-child)
3720                           subject date)
3721                     relations)))
3722           (when child
3723             (push (list (1+ generation) child nil subject) relations))
3724           (erase-buffer)))
3725       (kill-buffer (current-buffer)))
3726     ;; Sort over trustworthiness.
3727     (mapcar
3728      (lambda (relation)
3729        (when (gnus-dependencies-add-header
3730               (make-full-mail-header
3731                gnus-reffed-article-number
3732                (nth 3 relation) "" (or (nth 4 relation) "")
3733                (nth 1 relation)
3734                (or (nth 2 relation) "") 0 0 "")
3735               gnus-newsgroup-dependencies nil)
3736          (push gnus-reffed-article-number gnus-newsgroup-limit)
3737          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3738          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3739                gnus-newsgroup-reads)
3740          (decf gnus-reffed-article-number)))
3741      (sort relations 'car-less-than-car))
3742     (gnus-message 7 "Making sparse threads...done")))
3743
3744 (defun gnus-build-old-threads ()
3745   ;; Look at all the articles that refer back to old articles, and
3746   ;; fetch the headers for the articles that aren't there.  This will
3747   ;; build complete threads - if the roots haven't been expired by the
3748   ;; server, that is.
3749   (let ((mail-parse-charset gnus-newsgroup-charset)
3750         id heads)
3751     (mapatoms
3752      (lambda (refs)
3753        (when (not (car (symbol-value refs)))
3754          (setq heads (cdr (symbol-value refs)))
3755          (while heads
3756            (if (memq (mail-header-number (caar heads))
3757                      gnus-newsgroup-dormant)
3758                (setq heads (cdr heads))
3759              (setq id (symbol-name refs))
3760              (while (and (setq id (gnus-build-get-header id))
3761                          (not (car (gnus-id-to-thread id)))))
3762              (setq heads nil)))))
3763      gnus-newsgroup-dependencies)))
3764
3765 ;; This function has to be called with point after the article number
3766 ;; on the beginning of the line.
3767 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3768   (let ((eol (gnus-point-at-eol))
3769         (buffer (current-buffer))
3770         header references in-reply-to)
3771
3772     ;; overview: [num subject from date id refs chars lines misc]
3773     (unwind-protect
3774         (progn
3775           (narrow-to-region (point) eol)
3776           (unless (eobp)
3777             (forward-char))
3778
3779           (setq header
3780                 (make-full-mail-header
3781                  number                 ; number
3782                  (funcall gnus-decode-encoded-word-function
3783                           (nnheader-nov-field)) ; subject
3784                  (funcall gnus-decode-encoded-word-function
3785                           (nnheader-nov-field)) ; from
3786                  (nnheader-nov-field)   ; date
3787                  (nnheader-nov-read-message-id) ; id
3788                  (setq references (nnheader-nov-field)) ; refs
3789                  (nnheader-nov-read-integer) ; chars
3790                  (nnheader-nov-read-integer) ; lines
3791                  (unless (eobp)
3792                    (if (looking-at "Xref: ")
3793                        (goto-char (match-end 0)))
3794                    (nnheader-nov-field)) ; Xref
3795                  (nnheader-nov-parse-extra)))) ; extra
3796
3797       (widen))
3798
3799     (when (and (string= references "")
3800                (setq in-reply-to (mail-header-extra header))
3801                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3802       (mail-header-set-references
3803        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3804
3805     (when gnus-alter-header-function
3806       (funcall gnus-alter-header-function header))
3807     (gnus-dependencies-add-header header dependencies force-new)))
3808
3809 (defun gnus-build-get-header (id)
3810   "Look through the buffer of NOV lines and find the header to ID.
3811 Enter this line into the dependencies hash table, and return
3812 the id of the parent article (if any)."
3813   (let ((deps gnus-newsgroup-dependencies)
3814         found header)
3815     (prog1
3816         (save-excursion
3817           (set-buffer nntp-server-buffer)
3818           (let ((case-fold-search nil))
3819             (goto-char (point-min))
3820             (while (and (not found)
3821                         (search-forward id nil t))
3822               (beginning-of-line)
3823               (setq found (looking-at
3824                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3825                                    (regexp-quote id))))
3826               (or found (beginning-of-line 2)))
3827             (when found
3828               (beginning-of-line)
3829               (and
3830                (setq header (gnus-nov-parse-line
3831                              (read (current-buffer)) deps))
3832                (gnus-parent-id (mail-header-references header))))))
3833       (when header
3834         (let ((number (mail-header-number header)))
3835           (push number gnus-newsgroup-limit)
3836           (push header gnus-newsgroup-headers)
3837           (if (memq number gnus-newsgroup-unselected)
3838               (progn
3839                 (setq gnus-newsgroup-unreads
3840                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3841                                                number))
3842                 (setq gnus-newsgroup-unselected
3843                       (delq number gnus-newsgroup-unselected)))
3844             (push number gnus-newsgroup-ancient)))))))
3845
3846 (defun gnus-build-all-threads ()
3847   "Read all the headers."
3848   (let ((gnus-summary-ignore-duplicates t)
3849         (mail-parse-charset gnus-newsgroup-charset)
3850         (dependencies gnus-newsgroup-dependencies)
3851         header article)
3852     (save-excursion
3853       (set-buffer nntp-server-buffer)
3854       (let ((case-fold-search nil))
3855         (goto-char (point-min))
3856         (while (not (eobp))
3857           (ignore-errors
3858             (setq article (read (current-buffer))
3859                   header (gnus-nov-parse-line article dependencies)))
3860           (when header
3861             (save-excursion
3862               (set-buffer gnus-summary-buffer)
3863               (push header gnus-newsgroup-headers)
3864               (if (memq (setq article (mail-header-number header))
3865                         gnus-newsgroup-unselected)
3866                   (progn
3867                     (setq gnus-newsgroup-unreads
3868                           (gnus-add-to-sorted-list
3869                            gnus-newsgroup-unreads article))
3870                     (setq gnus-newsgroup-unselected
3871                           (delq article gnus-newsgroup-unselected)))
3872                 (push article gnus-newsgroup-ancient)))
3873             (forward-line 1)))))))
3874
3875 (defun gnus-summary-update-article-line (article header)
3876   "Update the line for ARTICLE using HEADERS."
3877   (let* ((id (mail-header-id header))
3878          (thread (gnus-id-to-thread id)))
3879     (unless thread
3880       (error "Article in no thread"))
3881     ;; Update the thread.
3882     (setcar thread header)
3883     (gnus-summary-goto-subject article)
3884     (let* ((datal (gnus-data-find-list article))
3885            (data (car datal))
3886            (length (when (cdr datal)
3887                      (- (gnus-data-pos data)
3888                         (gnus-data-pos (cadr datal)))))
3889            (buffer-read-only nil)
3890            (level (gnus-summary-thread-level)))
3891       (gnus-delete-line)
3892       (gnus-summary-insert-line
3893        header level nil (gnus-article-mark article)
3894        (memq article gnus-newsgroup-replied)
3895        (memq article gnus-newsgroup-expirable)
3896        ;; Only insert the Subject string when it's different
3897        ;; from the previous Subject string.
3898        (if (and
3899             gnus-show-threads
3900             (gnus-subject-equal
3901              (condition-case ()
3902                  (mail-header-subject
3903                   (gnus-data-header
3904                    (cadr
3905                     (gnus-data-find-list
3906                      article
3907                      (gnus-data-list t)))))
3908                ;; Error on the side of excessive subjects.
3909                (error ""))
3910              (mail-header-subject header)))
3911            ""
3912          (mail-header-subject header))
3913        nil (cdr (assq article gnus-newsgroup-scored))
3914        (memq article gnus-newsgroup-processable))
3915       (when length
3916         (gnus-data-update-list
3917          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3918
3919 (defun gnus-summary-update-article (article &optional iheader)
3920   "Update ARTICLE in the summary buffer."
3921   (set-buffer gnus-summary-buffer)
3922   (let* ((header (gnus-summary-article-header article))
3923          (id (mail-header-id header))
3924          (data (gnus-data-find article))
3925          (thread (gnus-id-to-thread id))
3926          (references (mail-header-references header))
3927          (parent
3928           (gnus-id-to-thread
3929            (or (gnus-parent-id
3930                 (when (and references
3931                            (not (equal "" references)))
3932                   references))
3933                "none")))
3934          (buffer-read-only nil)
3935          (old (car thread)))
3936     (when thread
3937       (unless iheader
3938         (setcar thread nil)
3939         (when parent
3940           (delq thread parent)))
3941       (if (gnus-summary-insert-subject id header)
3942        ;; Set the (possibly) new article number in the data structure.
3943           (gnus-data-set-number data (gnus-id-to-article id))
3944         (setcar thread old)
3945         nil))))
3946
3947 (defun gnus-rebuild-thread (id &optional line)
3948   "Rebuild the thread containing ID.
3949 If LINE, insert the rebuilt thread starting on line LINE."
3950   (let ((buffer-read-only nil)
3951         old-pos current thread data)
3952     (if (not gnus-show-threads)
3953         (setq thread (list (car (gnus-id-to-thread id))))
3954       ;; Get the thread this article is part of.
3955       (setq thread (gnus-remove-thread id)))
3956     (setq old-pos (gnus-point-at-bol))
3957     (setq current (save-excursion
3958                     (and (re-search-backward "[\r\n]" nil t)
3959                          (gnus-summary-article-number))))
3960     ;; If this is a gathered thread, we have to go some re-gathering.
3961     (when (stringp (car thread))
3962       (let ((subject (car thread))
3963             roots thr)
3964         (setq thread (cdr thread))
3965         (while thread
3966           (unless (memq (setq thr (gnus-id-to-thread
3967                                    (gnus-root-id
3968                                     (mail-header-id (caar thread)))))
3969                         roots)
3970             (push thr roots))
3971           (setq thread (cdr thread)))
3972         ;; We now have all (unique) roots.
3973         (if (= (length roots) 1)
3974             ;; All the loose roots are now one solid root.
3975             (setq thread (car roots))
3976           (setq thread (cons subject (gnus-sort-threads roots))))))
3977     (let (threads)
3978       ;; We then insert this thread into the summary buffer.
3979       (when line
3980         (goto-char (point-min))
3981         (forward-line (1- line)))
3982       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3983         (if gnus-show-threads
3984             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3985           (gnus-summary-prepare-unthreaded thread))
3986         (setq data (nreverse gnus-newsgroup-data))
3987         (setq threads gnus-newsgroup-threads))
3988       ;; We splice the new data into the data structure.
3989       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3990       ;;!!! then we want to insert at the beginning of the buffer.
3991       ;;!!! That happens to be true with Gnus now, but that may
3992       ;;!!! change in the future.  Perhaps.
3993       (gnus-data-enter-list
3994        (if line nil current) data (- (point) old-pos))
3995       (setq gnus-newsgroup-threads
3996             (nconc threads gnus-newsgroup-threads))
3997       (gnus-data-compute-positions))))
3998
3999 (defun gnus-number-to-header (number)
4000   "Return the header for article NUMBER."
4001   (let ((headers gnus-newsgroup-headers))
4002     (while (and headers
4003                 (not (= number (mail-header-number (car headers)))))
4004       (pop headers))
4005     (when headers
4006       (car headers))))
4007
4008 (defun gnus-parent-headers (in-headers &optional generation)
4009   "Return the headers of the GENERATIONeth parent of HEADERS."
4010   (unless generation
4011     (setq generation 1))
4012   (let ((parent t)
4013         (headers in-headers)
4014         references)
4015     (while (and parent
4016                 (not (zerop generation))
4017                 (setq references (mail-header-references headers)))
4018       (setq headers (if (and references
4019                              (setq parent (gnus-parent-id references)))
4020                         (car (gnus-id-to-thread parent))
4021                       nil))
4022       (decf generation))
4023     (and (not (eq headers in-headers))
4024          headers)))
4025
4026 (defun gnus-id-to-thread (id)
4027   "Return the (sub-)thread where ID appears."
4028   (gnus-gethash id gnus-newsgroup-dependencies))
4029
4030 (defun gnus-id-to-article (id)
4031   "Return the article number of ID."
4032   (let ((thread (gnus-id-to-thread id)))
4033     (when (and thread
4034                (car thread))
4035       (mail-header-number (car thread)))))
4036
4037 (defun gnus-id-to-header (id)
4038   "Return the article headers of ID."
4039   (car (gnus-id-to-thread id)))
4040
4041 (defun gnus-article-displayed-root-p (article)
4042   "Say whether ARTICLE is a root(ish) article."
4043   (let ((level (gnus-summary-thread-level article))
4044         (refs (mail-header-references  (gnus-summary-article-header article)))
4045         particle)
4046     (cond
4047      ((null level) nil)
4048      ((zerop level) t)
4049      ((null refs) t)
4050      ((null (gnus-parent-id refs)) t)
4051      ((and (= 1 level)
4052            (null (setq particle (gnus-id-to-article
4053                                  (gnus-parent-id refs))))
4054            (null (gnus-summary-thread-level particle)))))))
4055
4056 (defun gnus-root-id (id)
4057   "Return the id of the root of the thread where ID appears."
4058   (let (last-id prev)
4059     (while (and id (setq prev (car (gnus-id-to-thread id))))
4060       (setq last-id id
4061             id (gnus-parent-id (mail-header-references prev))))
4062     last-id))
4063
4064 (defun gnus-articles-in-thread (thread)
4065   "Return the list of articles in THREAD."
4066   (cons (mail-header-number (car thread))
4067         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4068
4069 (defun gnus-remove-thread (id &optional dont-remove)
4070   "Remove the thread that has ID in it."
4071   (let (headers thread last-id)
4072     ;; First go up in this thread until we find the root.
4073     (setq last-id (gnus-root-id id)
4074           headers (message-flatten-list (gnus-id-to-thread last-id)))
4075     ;; We have now found the real root of this thread.  It might have
4076     ;; been gathered into some loose thread, so we have to search
4077     ;; through the threads to find the thread we wanted.
4078     (let ((threads gnus-newsgroup-threads)
4079           sub)
4080       (while threads
4081         (setq sub (car threads))
4082         (if (stringp (car sub))
4083             ;; This is a gathered thread, so we look at the roots
4084             ;; below it to find whether this article is in this
4085             ;; gathered root.
4086             (progn
4087               (setq sub (cdr sub))
4088               (while sub
4089                 (when (member (caar sub) headers)
4090                   (setq thread (car threads)
4091                         threads nil
4092                         sub nil))
4093                 (setq sub (cdr sub))))
4094           ;; It's an ordinary thread, so we check it.
4095           (when (eq (car sub) (car headers))
4096             (setq thread sub
4097                   threads nil)))
4098         (setq threads (cdr threads)))
4099       ;; If this article is in no thread, then it's a root.
4100       (if thread
4101           (unless dont-remove
4102             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4103         (setq thread (gnus-id-to-thread last-id)))
4104       (when thread
4105         (prog1
4106             thread                      ; We return this thread.
4107           (unless dont-remove
4108             (if (stringp (car thread))
4109                 (progn
4110                   ;; If we use dummy roots, then we have to remove the
4111                   ;; dummy root as well.
4112                   (when (eq gnus-summary-make-false-root 'dummy)
4113                     ;; We go to the dummy root by going to
4114                     ;; the first sub-"thread", and then one line up.
4115                     (gnus-summary-goto-article
4116                      (mail-header-number (caadr thread)))
4117                     (forward-line -1)
4118                     (gnus-delete-line)
4119                     (gnus-data-compute-positions))
4120                   (setq thread (cdr thread))
4121                   (while thread
4122                     (gnus-remove-thread-1 (car thread))
4123                     (setq thread (cdr thread))))
4124               (gnus-remove-thread-1 thread))))))))
4125
4126 (defun gnus-remove-thread-1 (thread)
4127   "Remove the thread THREAD recursively."
4128   (let ((number (mail-header-number (pop thread)))
4129         d)
4130     (setq thread (reverse thread))
4131     (while thread
4132       (gnus-remove-thread-1 (pop thread)))
4133     (when (setq d (gnus-data-find number))
4134       (goto-char (gnus-data-pos d))
4135       (gnus-summary-show-thread)
4136       (gnus-data-remove
4137        number
4138        (- (gnus-point-at-bol)
4139           (prog1
4140               (1+ (gnus-point-at-eol))
4141             (gnus-delete-line)))))))
4142
4143 (defun gnus-sort-threads-1 (threads func)
4144   (sort (mapcar (lambda (thread)
4145                   (cons (car thread)
4146                         (and (cdr thread)
4147                              (gnus-sort-threads-1 (cdr thread) func))))
4148                 threads) func))
4149
4150 (defun gnus-sort-threads (threads)
4151   "Sort THREADS."
4152   (if (not gnus-thread-sort-functions)
4153       threads
4154     (gnus-message 8 "Sorting threads...")
4155     (prog1
4156         (gnus-sort-threads-1
4157          threads
4158          (gnus-make-sort-function gnus-thread-sort-functions))
4159       (gnus-message 8 "Sorting threads...done"))))
4160
4161 (defun gnus-sort-articles (articles)
4162   "Sort ARTICLES."
4163   (when gnus-article-sort-functions
4164     (gnus-message 7 "Sorting articles...")
4165     (prog1
4166         (setq gnus-newsgroup-headers
4167               (sort articles (gnus-make-sort-function
4168                               gnus-article-sort-functions)))
4169       (gnus-message 7 "Sorting articles...done"))))
4170
4171 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4172 (defmacro gnus-thread-header (thread)
4173   "Return header of first article in THREAD.
4174 Note that THREAD must never, ever be anything else than a variable -
4175 using some other form will lead to serious barfage."
4176   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4177   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4178   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4179         (vector thread) 2))
4180
4181 (defsubst gnus-article-sort-by-number (h1 h2)
4182   "Sort articles by article number."
4183   (< (mail-header-number h1)
4184      (mail-header-number h2)))
4185
4186 (defun gnus-thread-sort-by-number (h1 h2)
4187   "Sort threads by root article number."
4188   (gnus-article-sort-by-number
4189    (gnus-thread-header h1) (gnus-thread-header h2)))
4190
4191 (defsubst gnus-article-sort-by-lines (h1 h2)
4192   "Sort articles by article Lines header."
4193   (< (mail-header-lines h1)
4194      (mail-header-lines h2)))
4195
4196 (defun gnus-thread-sort-by-lines (h1 h2)
4197   "Sort threads by root article Lines header."
4198   (gnus-article-sort-by-lines
4199    (gnus-thread-header h1) (gnus-thread-header h2)))
4200
4201 (defsubst gnus-article-sort-by-chars (h1 h2)
4202   "Sort articles by octet length."
4203   (< (mail-header-chars h1)
4204      (mail-header-chars h2)))
4205
4206 (defun gnus-thread-sort-by-chars (h1 h2)
4207   "Sort threads by root article octet length."
4208   (gnus-article-sort-by-chars
4209    (gnus-thread-header h1) (gnus-thread-header h2)))
4210
4211 (defsubst gnus-article-sort-by-author (h1 h2)
4212   "Sort articles by root author."
4213   (string-lessp
4214    (let ((extract (funcall
4215                    gnus-extract-address-components
4216                    (mail-header-from h1))))
4217      (or (car extract) (cadr extract) ""))
4218    (let ((extract (funcall
4219                    gnus-extract-address-components
4220                    (mail-header-from h2))))
4221      (or (car extract) (cadr extract) ""))))
4222
4223 (defun gnus-thread-sort-by-author (h1 h2)
4224   "Sort threads by root author."
4225   (gnus-article-sort-by-author
4226    (gnus-thread-header h1)  (gnus-thread-header h2)))
4227
4228 (defsubst gnus-article-sort-by-subject (h1 h2)
4229   "Sort articles by root subject."
4230   (string-lessp
4231    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4232    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4233
4234 (defun gnus-thread-sort-by-subject (h1 h2)
4235   "Sort threads by root subject."
4236   (gnus-article-sort-by-subject
4237    (gnus-thread-header h1) (gnus-thread-header h2)))
4238
4239 (defsubst gnus-article-sort-by-date (h1 h2)
4240   "Sort articles by root article date."
4241   (time-less-p
4242    (gnus-date-get-time (mail-header-date h1))
4243    (gnus-date-get-time (mail-header-date h2))))
4244
4245 (defun gnus-thread-sort-by-date (h1 h2)
4246   "Sort threads by root article date."
4247   (gnus-article-sort-by-date
4248    (gnus-thread-header h1) (gnus-thread-header h2)))
4249
4250 (defsubst gnus-article-sort-by-score (h1 h2)
4251   "Sort articles by root article score.
4252 Unscored articles will be counted as having a score of zero."
4253   (> (or (cdr (assq (mail-header-number h1)
4254                     gnus-newsgroup-scored))
4255          gnus-summary-default-score 0)
4256      (or (cdr (assq (mail-header-number h2)
4257                     gnus-newsgroup-scored))
4258          gnus-summary-default-score 0)))
4259
4260 (defun gnus-thread-sort-by-score (h1 h2)
4261   "Sort threads by root article score."
4262   (gnus-article-sort-by-score
4263    (gnus-thread-header h1) (gnus-thread-header h2)))
4264
4265 (defun gnus-thread-sort-by-total-score (h1 h2)
4266   "Sort threads by the sum of all scores in the thread.
4267 Unscored articles will be counted as having a score of zero."
4268   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4269
4270 (defun gnus-thread-total-score (thread)
4271   ;; This function find the total score of THREAD.
4272   (cond
4273    ((null thread)
4274     0)
4275    ((consp thread)
4276     (if (stringp (car thread))
4277         (apply gnus-thread-score-function 0
4278                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4279       (gnus-thread-total-score-1 thread)))
4280    (t
4281     (gnus-thread-total-score-1 (list thread)))))
4282
4283 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4284   "Sort threads such that the thread with the most recently arrived article comes first."
4285   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4286
4287 (defun gnus-thread-highest-number (thread)
4288   "Return the highest article number in THREAD."
4289   (apply 'max (mapcar (lambda (header)
4290                         (mail-header-number header))
4291                       (message-flatten-list thread))))
4292
4293 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4294   "Sort threads such that the thread with the most recently dated article comes first."
4295   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4296
4297 (defun gnus-thread-latest-date (thread)
4298   "Return the highest article date in THREAD."
4299   (let ((previous-time 0))
4300     (apply 'max (mapcar
4301                  (lambda (header)
4302                    (setq previous-time
4303                          (time-to-seconds
4304                           (mail-header-parse-date
4305                            (condition-case ()
4306                                (mail-header-date header)
4307                              (error previous-time))))))
4308                  (sort
4309                   (message-flatten-list thread)
4310                   (lambda (h1 h2)
4311                     (< (mail-header-number h1)
4312                        (mail-header-number h2))))))))
4313
4314 (defun gnus-thread-total-score-1 (root)
4315   ;; This function find the total score of the thread below ROOT.
4316   (setq root (car root))
4317   (apply gnus-thread-score-function
4318          (or (append
4319               (mapcar 'gnus-thread-total-score
4320                       (cdr (gnus-id-to-thread (mail-header-id root))))
4321               (when (> (mail-header-number root) 0)
4322                 (list (or (cdr (assq (mail-header-number root)
4323                                      gnus-newsgroup-scored))
4324                           gnus-summary-default-score 0))))
4325              (list gnus-summary-default-score)
4326              '(0))))
4327
4328 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4329 (defvar gnus-tmp-prev-subject nil)
4330 (defvar gnus-tmp-false-parent nil)
4331 (defvar gnus-tmp-root-expunged nil)
4332 (defvar gnus-tmp-dummy-line nil)
4333
4334 (eval-when-compile (defvar gnus-tmp-header))
4335 (defun gnus-extra-header (type &optional header)
4336   "Return the extra header of TYPE."
4337   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4338       ""))
4339
4340 (defvar gnus-tmp-thread-tree-header-string "")
4341
4342 (defcustom gnus-sum-thread-tree-root "> "
4343   "With %B spec, used for the root of a thread.
4344 If nil, use subject instead."
4345   :type 'string
4346   :group 'gnus-thread)
4347 (defcustom gnus-sum-thread-tree-single-indent ""
4348   "With %B spec, used for a thread with just one message.
4349 If nil, use subject instead."
4350   :type 'string
4351   :group 'gnus-thread)
4352 (defcustom gnus-sum-thread-tree-vertical "| "
4353   "With %B spec, used for drawing a vertical line."
4354   :type 'string
4355   :group 'gnus-thread)
4356 (defcustom gnus-sum-thread-tree-indent "  "
4357   "With %B spec, used for indenting."
4358   :type 'string
4359   :group 'gnus-thread)
4360 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4361   "With %B spec, used for a leaf with brothers."
4362   :type 'string
4363   :group 'gnus-thread)
4364 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4365   "With %B spec, used for a leaf without brothers."
4366   :type 'string
4367   :group 'gnus-thread)
4368
4369 (defun gnus-summary-prepare-threads (threads)
4370   "Prepare summary buffer from THREADS and indentation LEVEL.
4371 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4372 or a straight list of headers."
4373   (gnus-message 7 "Generating summary...")
4374
4375   (setq gnus-newsgroup-threads threads)
4376   (beginning-of-line)
4377
4378   (let ((gnus-tmp-level 0)
4379         (default-score (or gnus-summary-default-score 0))
4380         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4381         thread number subject stack state gnus-tmp-gathered beg-match
4382         new-roots gnus-tmp-new-adopts thread-end
4383         gnus-tmp-header gnus-tmp-unread
4384         gnus-tmp-replied gnus-tmp-subject-or-nil
4385         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4386         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4387         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4388         tree-stack)
4389
4390     (setq gnus-tmp-prev-subject nil)
4391
4392     (if (vectorp (car threads))
4393         ;; If this is a straight (sic) list of headers, then a
4394         ;; threaded summary display isn't required, so we just create
4395         ;; an unthreaded one.
4396         (gnus-summary-prepare-unthreaded threads)
4397
4398       ;; Do the threaded display.
4399
4400       (while (or threads stack gnus-tmp-new-adopts new-roots)
4401
4402         (if (and (= gnus-tmp-level 0)
4403                  (or (not stack)
4404                      (= (caar stack) 0))
4405                  (not gnus-tmp-false-parent)
4406                  (or gnus-tmp-new-adopts new-roots))
4407             (if gnus-tmp-new-adopts
4408                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4409                       thread (list (car gnus-tmp-new-adopts))
4410                       gnus-tmp-header (caar thread)
4411                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4412               (when new-roots
4413                 (setq thread (list (car new-roots))
4414                       gnus-tmp-header (caar thread)
4415                       new-roots (cdr new-roots))))
4416
4417           (if threads
4418               ;; If there are some threads, we do them before the
4419               ;; threads on the stack.
4420               (setq thread threads
4421                     gnus-tmp-header (caar thread))
4422             ;; There were no current threads, so we pop something off
4423             ;; the stack.
4424             (setq state (car stack)
4425                   gnus-tmp-level (car state)
4426                   tree-stack (cadr state)
4427                   thread (caddr state)
4428                   stack (cdr stack)
4429                   gnus-tmp-header (caar thread))))
4430
4431         (setq gnus-tmp-false-parent nil)
4432         (setq gnus-tmp-root-expunged nil)
4433         (setq thread-end nil)
4434
4435         (if (stringp gnus-tmp-header)
4436             ;; The header is a dummy root.
4437             (cond
4438              ((eq gnus-summary-make-false-root 'adopt)
4439               ;; We let the first article adopt the rest.
4440               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4441                                                (cddar thread)))
4442               (setq gnus-tmp-gathered
4443                     (nconc (mapcar
4444                             (lambda (h) (mail-header-number (car h)))
4445                             (cddar thread))
4446                            gnus-tmp-gathered))
4447               (setq thread (cons (list (caar thread)
4448                                        (cadar thread))
4449                                  (cdr thread)))
4450               (setq gnus-tmp-level -1
4451                     gnus-tmp-false-parent t))
4452              ((eq gnus-summary-make-false-root 'empty)
4453               ;; We print adopted articles with empty subject fields.
4454               (setq gnus-tmp-gathered
4455                     (nconc (mapcar
4456                             (lambda (h) (mail-header-number (car h)))
4457                             (cddar thread))
4458                            gnus-tmp-gathered))
4459               (setq gnus-tmp-level -1))
4460              ((eq gnus-summary-make-false-root 'dummy)
4461               ;; We remember that we probably want to output a dummy
4462               ;; root.
4463               (setq gnus-tmp-dummy-line gnus-tmp-header)
4464               (setq gnus-tmp-prev-subject gnus-tmp-header))
4465              (t
4466               ;; We do not make a root for the gathered
4467               ;; sub-threads at all.
4468               (setq gnus-tmp-level -1)))
4469
4470           (setq number (mail-header-number gnus-tmp-header)
4471                 subject (mail-header-subject gnus-tmp-header))
4472
4473           (cond
4474            ;; If the thread has changed subject, we might want to make
4475            ;; this subthread into a root.
4476            ((and (null gnus-thread-ignore-subject)
4477                  (not (zerop gnus-tmp-level))
4478                  gnus-tmp-prev-subject
4479                  (not (inline
4480                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4481             (setq new-roots (nconc new-roots (list (car thread)))
4482                   thread-end t
4483                   gnus-tmp-header nil))
4484            ;; If the article lies outside the current limit,
4485            ;; then we do not display it.
4486            ((not (memq number gnus-newsgroup-limit))
4487             (setq gnus-tmp-gathered
4488                   (nconc (mapcar
4489                           (lambda (h) (mail-header-number (car h)))
4490                           (cdar thread))
4491                          gnus-tmp-gathered))
4492             (setq gnus-tmp-new-adopts (if (cdar thread)
4493                                           (append gnus-tmp-new-adopts
4494                                                   (cdar thread))
4495                                         gnus-tmp-new-adopts)
4496                   thread-end t
4497                   gnus-tmp-header nil)
4498             (when (zerop gnus-tmp-level)
4499               (setq gnus-tmp-root-expunged t)))
4500            ;; Perhaps this article is to be marked as read?
4501            ((and gnus-summary-mark-below
4502                  (< (or (cdr (assq number gnus-newsgroup-scored))
4503                         default-score)
4504                     gnus-summary-mark-below)
4505                  ;; Don't touch sparse articles.
4506                  (not (gnus-summary-article-sparse-p number))
4507                  (not (gnus-summary-article-ancient-p number)))
4508             (setq gnus-newsgroup-unreads
4509                   (delq number gnus-newsgroup-unreads))
4510             (if gnus-newsgroup-auto-expire
4511                 (setq gnus-newsgroup-expirable
4512                       (gnus-add-to-sorted-list
4513                        gnus-newsgroup-expirable number))
4514               (push (cons number gnus-low-score-mark)
4515                     gnus-newsgroup-reads))))
4516
4517           (when gnus-tmp-header
4518             ;; We may have an old dummy line to output before this
4519             ;; article.
4520             (when (and gnus-tmp-dummy-line
4521                        (gnus-subject-equal
4522                         gnus-tmp-dummy-line
4523                         (mail-header-subject gnus-tmp-header)))
4524               (gnus-summary-insert-dummy-line
4525                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4526               (setq gnus-tmp-dummy-line nil))
4527
4528             ;; Compute the mark.
4529             (setq gnus-tmp-unread (gnus-article-mark number))
4530
4531             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4532                                   gnus-tmp-header gnus-tmp-level)
4533                   gnus-newsgroup-data)
4534
4535             ;; Actually insert the line.
4536             (setq
4537              gnus-tmp-subject-or-nil
4538              (cond
4539               ((and gnus-thread-ignore-subject
4540                     gnus-tmp-prev-subject
4541                     (not (inline (gnus-subject-equal
4542                                   gnus-tmp-prev-subject subject))))
4543                subject)
4544               ((zerop gnus-tmp-level)
4545                (if (and (eq gnus-summary-make-false-root 'empty)
4546                         (memq number gnus-tmp-gathered)
4547                         gnus-tmp-prev-subject
4548                         (inline (gnus-subject-equal
4549                                  gnus-tmp-prev-subject subject)))
4550                    gnus-summary-same-subject
4551                  subject))
4552               (t gnus-summary-same-subject)))
4553             (if (and (eq gnus-summary-make-false-root 'adopt)
4554                      (= gnus-tmp-level 1)
4555                      (memq number gnus-tmp-gathered))
4556                 (setq gnus-tmp-opening-bracket ?\<
4557                       gnus-tmp-closing-bracket ?\>)
4558               (setq gnus-tmp-opening-bracket ?\[
4559                     gnus-tmp-closing-bracket ?\]))
4560             (setq
4561              gnus-tmp-indentation
4562              (aref gnus-thread-indent-array gnus-tmp-level)
4563              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4564              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4565                                 gnus-summary-default-score 0)
4566              gnus-tmp-score-char
4567              (if (or (null gnus-summary-default-score)
4568                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4569                          gnus-summary-zcore-fuzz))
4570                  ?                      ;Whitespace
4571                (if (< gnus-tmp-score gnus-summary-default-score)
4572                    gnus-score-below-mark gnus-score-over-mark))
4573              gnus-tmp-replied
4574              (cond ((memq number gnus-newsgroup-processable)
4575                     gnus-process-mark)
4576                    ((memq number gnus-newsgroup-cached)
4577                     gnus-cached-mark)
4578                    ((memq number gnus-newsgroup-replied)
4579                     gnus-replied-mark)
4580                    ((memq number gnus-newsgroup-forwarded)
4581                     gnus-forwarded-mark)
4582                    ((memq number gnus-newsgroup-saved)
4583                     gnus-saved-mark)
4584                    ((memq number gnus-newsgroup-recent)
4585                     gnus-recent-mark)
4586                    ((memq number gnus-newsgroup-unseen)
4587                     gnus-unseen-mark)
4588                    (t gnus-no-mark))
4589              gnus-tmp-from (mail-header-from gnus-tmp-header)
4590              gnus-tmp-name
4591              (cond
4592               ((string-match "<[^>]+> *$" gnus-tmp-from)
4593                (setq beg-match (match-beginning 0))
4594                (or (and (string-match "^\".+\"" gnus-tmp-from)
4595                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4596                    (substring gnus-tmp-from 0 beg-match)))
4597               ((string-match "(.+)" gnus-tmp-from)
4598                (substring gnus-tmp-from
4599                           (1+ (match-beginning 0)) (1- (match-end 0))))
4600               (t gnus-tmp-from))
4601              gnus-tmp-thread-tree-header-string
4602              (cond
4603               ((not gnus-show-threads) "")
4604               ((zerop gnus-tmp-level)
4605                (if (cdar thread)
4606                    (or gnus-sum-thread-tree-root subject)
4607                  (or gnus-sum-thread-tree-single-indent subject)))
4608               (t
4609                (concat (apply 'concat
4610                               (mapcar (lambda (item)
4611                                         (if (= item 1)
4612                                             gnus-sum-thread-tree-vertical
4613                                           gnus-sum-thread-tree-indent))
4614                                       (cdr (reverse tree-stack))))
4615                        (if (nth 1 thread)
4616                            gnus-sum-thread-tree-leaf-with-other
4617                          gnus-sum-thread-tree-single-leaf)))))
4618             (when (string= gnus-tmp-name "")
4619               (setq gnus-tmp-name gnus-tmp-from))
4620             (unless (numberp gnus-tmp-lines)
4621               (setq gnus-tmp-lines -1))
4622             (if (= gnus-tmp-lines -1)
4623                 (setq gnus-tmp-lines "?")
4624               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4625             (gnus-put-text-property
4626              (point)
4627              (progn (eval gnus-summary-line-format-spec) (point))
4628              'gnus-number number)
4629             (when gnus-visual-p
4630               (forward-line -1)
4631               (gnus-run-hooks 'gnus-summary-update-hook)
4632               (forward-line 1))
4633
4634             (setq gnus-tmp-prev-subject subject)))
4635
4636         (when (nth 1 thread)
4637           (push (list (max 0 gnus-tmp-level)
4638                       (copy-list tree-stack)
4639                       (nthcdr 1 thread))
4640                 stack))
4641         (push (if (nth 1 thread) 1 0) tree-stack)
4642         (incf gnus-tmp-level)
4643         (setq threads (if thread-end nil (cdar thread)))
4644         (unless threads
4645           (setq gnus-tmp-level 0)))))
4646   (gnus-message 7 "Generating summary...done"))
4647
4648 (defun gnus-summary-prepare-unthreaded (headers)
4649   "Generate an unthreaded summary buffer based on HEADERS."
4650   (let (header number mark)
4651
4652     (beginning-of-line)
4653
4654     (while headers
4655       ;; We may have to root out some bad articles...
4656       (when (memq (setq number (mail-header-number
4657                                 (setq header (pop headers))))
4658                   gnus-newsgroup-limit)
4659         ;; Mark article as read when it has a low score.
4660         (when (and gnus-summary-mark-below
4661                    (< (or (cdr (assq number gnus-newsgroup-scored))
4662                           gnus-summary-default-score 0)
4663                       gnus-summary-mark-below)
4664                    (not (gnus-summary-article-ancient-p number)))
4665           (setq gnus-newsgroup-unreads
4666                 (delq number gnus-newsgroup-unreads))
4667           (if gnus-newsgroup-auto-expire
4668               (push number gnus-newsgroup-expirable)
4669             (push (cons number gnus-low-score-mark)
4670                   gnus-newsgroup-reads)))
4671
4672         (setq mark (gnus-article-mark number))
4673         (push (gnus-data-make number mark (1+ (point)) header 0)
4674               gnus-newsgroup-data)
4675         (gnus-summary-insert-line
4676          header 0 number
4677          mark (memq number gnus-newsgroup-replied)
4678          (memq number gnus-newsgroup-expirable)
4679          (mail-header-subject header) nil
4680          (cdr (assq number gnus-newsgroup-scored))
4681          (memq number gnus-newsgroup-processable))))))
4682
4683 (defun gnus-summary-remove-list-identifiers ()
4684   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4685   (let ((regexp (if (consp gnus-list-identifiers)
4686                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4687                   gnus-list-identifiers))
4688         changed subject)
4689     (when regexp
4690       (dolist (header gnus-newsgroup-headers)
4691         (setq subject (mail-header-subject header)
4692               changed nil)
4693         (while (string-match
4694                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4695                 subject)
4696           (setq subject
4697                 (concat (substring subject 0 (match-beginning 2))
4698                         (substring subject (match-end 0)))
4699                 changed t))
4700         (when (and changed
4701                    (string-match
4702                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4703           (setq subject
4704                 (concat (substring subject 0 (match-beginning 1))
4705                         (substring subject (match-end 1)))))
4706         (when changed
4707           (mail-header-set-subject header subject))))))
4708
4709 (defun gnus-fetch-headers (articles)
4710   "Fetch headers of ARTICLES."
4711   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4712     (gnus-message 5 "Fetching headers for %s..." name)
4713     (prog1
4714         (if (eq 'nov
4715                 (setq gnus-headers-retrieved-by
4716                       (gnus-retrieve-headers
4717                        articles gnus-newsgroup-name
4718                        ;; We might want to fetch old headers, but
4719                        ;; not if there is only 1 article.
4720                        (and (or (and
4721                                  (not (eq gnus-fetch-old-headers 'some))
4722                                  (not (numberp gnus-fetch-old-headers)))
4723                                 (> (length articles) 1))
4724                             gnus-fetch-old-headers))))
4725             (gnus-get-newsgroup-headers-xover
4726              articles nil nil gnus-newsgroup-name t)
4727           (gnus-get-newsgroup-headers))
4728       (gnus-message 5 "Fetching headers for %s...done" name))))
4729
4730 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4731   "Select newsgroup GROUP.
4732 If READ-ALL is non-nil, all articles in the group are selected.
4733 If SELECT-ARTICLES, only select those articles from GROUP."
4734   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4735          ;;!!! Dirty hack; should be removed.
4736          (gnus-summary-ignore-duplicates
4737           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4738               t
4739             gnus-summary-ignore-duplicates))
4740          (info (nth 2 entry))
4741          articles fetched-articles cached)
4742
4743     (unless (gnus-check-server
4744              (set (make-local-variable 'gnus-current-select-method)
4745                   (gnus-find-method-for-group group)))
4746       (error "Couldn't open server"))
4747
4748     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4749         (gnus-activate-group group)     ; Or we can activate it...
4750         (progn                          ; Or we bug out.
4751           (when (equal major-mode 'gnus-summary-mode)
4752             (kill-buffer (current-buffer)))
4753           (error "Couldn't activate group %s: %s"
4754                  group (gnus-status-message group))))
4755
4756     (unless (gnus-request-group group t)
4757       (when (equal major-mode 'gnus-summary-mode)
4758         (kill-buffer (current-buffer)))
4759       (error "Couldn't request group %s: %s"
4760              group (gnus-status-message group)))
4761
4762     (setq gnus-newsgroup-name group
4763           gnus-newsgroup-unselected nil
4764           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4765
4766     (let ((display (gnus-group-find-parameter group 'display)))
4767       (setq gnus-newsgroup-display
4768             (cond
4769              ((not (zerop (or (car-safe read-all) 0)))
4770               ;; The user entered the group with C-u SPC/RET, let's show
4771               ;; all articles.
4772               'gnus-not-ignore)
4773              ((eq display 'all)
4774               'gnus-not-ignore)
4775              ((arrayp display)
4776               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4777              ((numberp display)
4778               ;; The following is probably the "correct" solution, but
4779               ;; it makes Gnus fetch all headers and then limit the
4780               ;; articles (which is slow), so instead we hack the
4781               ;; select-articles parameter instead. -- Simon Josefsson
4782               ;; <jas@kth.se>
4783               ;;
4784               ;; (gnus-byte-compile
4785               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4786               ;;                         display)))))
4787               (setq select-articles
4788                     (gnus-uncompress-range
4789                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4790                              (if (> tmp 0)
4791                                  tmp
4792                                1))
4793                            (cdr (gnus-active group)))))
4794               nil)
4795              (t
4796               nil))))
4797
4798     (gnus-summary-setup-default-charset)
4799
4800     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4801     (when (gnus-virtual-group-p group)
4802       (setq cached gnus-newsgroup-cached))
4803
4804     (setq gnus-newsgroup-unreads
4805           (gnus-sorted-ndifference
4806            (gnus-sorted-ndifference gnus-newsgroup-unreads
4807                                     gnus-newsgroup-marked)
4808            gnus-newsgroup-dormant))
4809
4810     (setq gnus-newsgroup-processable nil)
4811
4812     (gnus-update-read-articles group gnus-newsgroup-unreads)
4813
4814     ;; Adjust and set lists of article marks.
4815     (when info
4816       (gnus-adjust-marked-articles info))
4817
4818     (if (setq articles select-articles)
4819         (setq gnus-newsgroup-unselected
4820               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4821       (setq articles (gnus-articles-to-read group read-all)))
4822
4823     (cond
4824      ((null articles)
4825       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4826       'quit)
4827      ((eq articles 0) nil)
4828      (t
4829       ;; Init the dependencies hash table.
4830       (setq gnus-newsgroup-dependencies
4831             (gnus-make-hashtable (length articles)))
4832       (gnus-set-global-variables)
4833       ;; Retrieve the headers and read them in.
4834       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4835
4836       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4837       (when cached
4838         (setq gnus-newsgroup-cached cached))
4839
4840       ;; Suppress duplicates?
4841       (when gnus-suppress-duplicates
4842         (gnus-dup-suppress-articles))
4843
4844       ;; Set the initial limit.
4845       (setq gnus-newsgroup-limit (copy-sequence articles))
4846       ;; Remove canceled articles from the list of unread articles.
4847       (setq fetched-articles
4848             (mapcar (lambda (headers) (mail-header-number headers))
4849                     gnus-newsgroup-headers))
4850       (setq gnus-newsgroup-articles fetched-articles)
4851       (setq gnus-newsgroup-unreads
4852             (gnus-sorted-nintersection
4853              gnus-newsgroup-unreads fetched-articles))
4854       (gnus-compute-unseen-list)
4855
4856       ;; Removed marked articles that do not exist.
4857       (gnus-update-missing-marks
4858        (gnus-sorted-difference articles fetched-articles))
4859       ;; We might want to build some more threads first.
4860       (when (and gnus-fetch-old-headers
4861                  (eq gnus-headers-retrieved-by 'nov))
4862         (if (eq gnus-fetch-old-headers 'invisible)
4863             (gnus-build-all-threads)
4864           (gnus-build-old-threads)))
4865       ;; Let the Gnus agent mark articles as read.
4866       (when gnus-agent
4867         (gnus-agent-get-undownloaded-list))
4868       ;; Remove list identifiers from subject
4869       (when gnus-list-identifiers
4870         (gnus-summary-remove-list-identifiers))
4871       ;; Check whether auto-expire is to be done in this group.
4872       (setq gnus-newsgroup-auto-expire
4873             (gnus-group-auto-expirable-p group))
4874       ;; Set up the article buffer now, if necessary.
4875       (unless gnus-single-article-buffer
4876         (gnus-article-setup-buffer))
4877       ;; First and last article in this newsgroup.
4878       (when gnus-newsgroup-headers
4879         (setq gnus-newsgroup-begin
4880               (mail-header-number (car gnus-newsgroup-headers))
4881               gnus-newsgroup-end
4882               (mail-header-number
4883                (gnus-last-element gnus-newsgroup-headers))))
4884       ;; GROUP is successfully selected.
4885       (or gnus-newsgroup-headers t)))))
4886
4887 (defun gnus-compute-unseen-list ()
4888   ;; The `seen' marks are treated specially.
4889   (if (not gnus-newsgroup-seen)
4890       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4891     (setq gnus-newsgroup-unseen
4892           (gnus-inverse-list-range-intersection
4893            gnus-newsgroup-articles gnus-newsgroup-seen))))
4894
4895 (defun gnus-summary-display-make-predicate (display)
4896   (require 'gnus-agent)
4897   (when (= (length display) 1)
4898     (setq display (car display)))
4899   (unless gnus-summary-display-cache
4900     (dolist (elem (append '((unread . unread)
4901                             (read . read)
4902                             (unseen . unseen))
4903                           gnus-article-mark-lists))
4904       (push (cons (cdr elem)
4905                   (gnus-byte-compile
4906                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4907             gnus-summary-display-cache)))
4908   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4909         (gnus-category-predicate-cache gnus-summary-display-cache))
4910     (gnus-get-predicate display)))
4911
4912 ;; Uses the dynamically bound `number' variable.
4913 (defvar number)
4914 (defun gnus-article-marked-p (type &optional article)
4915   (let ((article (or article number)))
4916     (cond
4917      ((eq type 'tick)
4918       (memq article gnus-newsgroup-marked))
4919      ((eq type 'unsend)
4920       (memq article gnus-newsgroup-unsendable))
4921      ((eq type 'undownload)
4922       (memq article gnus-newsgroup-undownloaded))
4923      ((eq type 'download)
4924       (memq article gnus-newsgroup-downloadable))
4925      ((eq type 'unread)
4926       (memq article gnus-newsgroup-unreads))
4927      ((eq type 'read)
4928       (memq article gnus-newsgroup-reads))
4929      ((eq type 'dormant)
4930       (memq article gnus-newsgroup-dormant) )
4931      ((eq type 'expire)
4932       (memq article gnus-newsgroup-expirable))
4933      ((eq type 'reply)
4934       (memq article gnus-newsgroup-replied))
4935      ((eq type 'killed)
4936       (memq article gnus-newsgroup-killed))
4937      ((eq type 'bookmark)
4938       (assq article gnus-newsgroup-bookmarks))
4939      ((eq type 'score)
4940       (assq article gnus-newsgroup-scored))
4941      ((eq type 'save)
4942       (memq article gnus-newsgroup-saved))
4943      ((eq type 'cache)
4944       (memq article gnus-newsgroup-cached))
4945      ((eq type 'forward)
4946       (memq article gnus-newsgroup-forwarded))
4947      ((eq type 'seen)
4948       (not (memq article gnus-newsgroup-unseen)))
4949      ((eq type 'recent)
4950       (memq article gnus-newsgroup-recent))
4951      (t t))))
4952
4953 (defun gnus-articles-to-read (group &optional read-all)
4954   "Find out what articles the user wants to read."
4955   (let* ((articles
4956           ;; Select all articles if `read-all' is non-nil, or if there
4957           ;; are no unread articles.
4958           (if (or read-all
4959                   (and (zerop (length gnus-newsgroup-marked))
4960                        (zerop (length gnus-newsgroup-unreads)))
4961                   ;; Fetch all if the predicate is non-nil.
4962                   gnus-newsgroup-display)
4963               ;; We want to select the headers for all the articles in
4964               ;; the group, so we select either all the active
4965               ;; articles in the group, or (if that's nil), the
4966               ;; articles in the cache.
4967               (or
4968                (gnus-uncompress-range (gnus-active group))
4969                (gnus-cache-articles-in-group group))
4970             ;; Select only the "normal" subset of articles.
4971             (gnus-sorted-nunion
4972              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
4973              gnus-newsgroup-unreads)))
4974          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4975          (scored (length scored-list))
4976          (number (length articles))
4977          (marked (+ (length gnus-newsgroup-marked)
4978                     (length gnus-newsgroup-dormant)))
4979          (select
4980           (cond
4981            ((numberp read-all)
4982             read-all)
4983            ((numberp gnus-newsgroup-display)
4984             gnus-newsgroup-display)
4985            (t
4986             (condition-case ()
4987                 (cond
4988                  ((and (or (<= scored marked) (= scored number))
4989                        (numberp gnus-large-newsgroup)
4990                        (> number gnus-large-newsgroup))
4991                   (let* ((cursor-in-echo-area nil)
4992                          (initial (gnus-parameter-large-newsgroup-initial 
4993                                    gnus-newsgroup-name))
4994                          (input
4995                           (read-string
4996                            (format
4997                             "How many articles from %s (%s %d): "
4998                             (gnus-limit-string
4999                              (gnus-group-decoded-name gnus-newsgroup-name)
5000                              35)
5001                             (if initial "max" "default")
5002                             number)
5003                            (if initial
5004                                (cons (number-to-string initial)
5005                                      0)))))
5006                     (if (string-match "^[ \t]*$" input) number input)))
5007                  ((and (> scored marked) (< scored number)
5008                        (> (- scored number) 20))
5009                   (let ((input
5010                          (read-string
5011                           (format "%s %s (%d scored, %d total): "
5012                                   "How many articles from"
5013                                   (gnus-group-decoded-name group)
5014                                   scored number))))
5015                     (if (string-match "^[ \t]*$" input)
5016                         number input)))
5017                  (t number))
5018               (quit
5019                (message "Quit getting the articles to read")
5020                nil))))))
5021     (setq select (if (stringp select) (string-to-number select) select))
5022     (if (or (null select) (zerop select))
5023         select
5024       (if (and (not (zerop scored)) (<= (abs select) scored))
5025           (progn
5026             (setq articles (sort scored-list '<))
5027             (setq number (length articles)))
5028         (setq articles (copy-sequence articles)))
5029
5030       (when (< (abs select) number)
5031         (if (< select 0)
5032             ;; Select the N oldest articles.
5033             (setcdr (nthcdr (1- (abs select)) articles) nil)
5034           ;; Select the N most recent articles.
5035           (setq articles (nthcdr (- number select) articles))))
5036       (setq gnus-newsgroup-unselected
5037             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5038       (when gnus-alter-articles-to-read-function
5039         (setq gnus-newsgroup-unreads
5040               (sort
5041                (funcall gnus-alter-articles-to-read-function
5042                         gnus-newsgroup-name gnus-newsgroup-unreads)
5043                '<)))
5044       articles)))
5045
5046 (defun gnus-killed-articles (killed articles)
5047   (let (out)
5048     (while articles
5049       (when (inline (gnus-member-of-range (car articles) killed))
5050         (push (car articles) out))
5051       (setq articles (cdr articles)))
5052     out))
5053
5054 (defun gnus-uncompress-marks (marks)
5055   "Uncompress the mark ranges in MARKS."
5056   (let ((uncompressed '(score bookmark))
5057         out)
5058     (while marks
5059       (if (memq (caar marks) uncompressed)
5060           (push (car marks) out)
5061         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5062       (setq marks (cdr marks)))
5063     out))
5064
5065 (defun gnus-article-mark-to-type (mark)
5066   "Return the type of MARK."
5067   (or (cadr (assq mark gnus-article-special-mark-lists))
5068       'list))
5069
5070 (defun gnus-article-unpropagatable-p (mark)
5071   "Return whether MARK should be propagated to backend."
5072   (memq mark gnus-article-unpropagated-mark-lists))
5073
5074 (defun gnus-adjust-marked-articles (info)
5075   "Set all article lists and remove all marks that are no longer valid."
5076   (let* ((marked-lists (gnus-info-marks info))
5077          (active (gnus-active (gnus-info-group info)))
5078          (min (car active))
5079          (max (cdr active))
5080          (types gnus-article-mark-lists)
5081          marks var articles article mark mark-type)
5082
5083     (dolist (marks marked-lists)
5084       (setq mark (car marks)
5085             mark-type (gnus-article-mark-to-type mark)
5086             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5087
5088       ;; We set the variable according to the type of the marks list,
5089       ;; and then adjust the marks to a subset of the active articles.
5090       (cond
5091        ;; Adjust "simple" lists.
5092        ((eq mark-type 'list)
5093         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5094         (when (memq mark '(tick dormant expire reply save))
5095           (while articles
5096             (when (or (< (setq article (pop articles)) min) (> article max))
5097               (set var (delq article (symbol-value var)))))))
5098        ;; Adjust assocs.
5099        ((eq mark-type 'tuple)
5100         (set var (setq articles (cdr marks)))
5101         (when (not (listp (cdr (symbol-value var))))
5102           (set var (list (symbol-value var))))
5103         (when (not (listp (cdr articles)))
5104           (setq articles (list articles)))
5105         (while articles
5106           (when (or (not (consp (setq article (pop articles))))
5107                     (< (car article) min)
5108                     (> (car article) max))
5109             (set var (delq article (symbol-value var))))))
5110        ;; Adjust ranges (sloppily).
5111        ((eq mark-type 'range)
5112         (cond
5113          ((eq mark 'seen)
5114           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5115           ;; It should be (seen (NUM1 . NUM2)).
5116           (when (numberp (cddr marks))
5117             (setcdr marks (list (cdr marks))))
5118           (setq articles (cdr marks))
5119           (while (and articles
5120                       (or (and (consp (car articles))
5121                                (> min (cdar articles)))
5122                           (and (numberp (car articles))
5123                                (> min (car articles)))))
5124             (pop articles))
5125           (set var articles))))))))
5126
5127 (defun gnus-update-missing-marks (missing)
5128   "Go through the list of MISSING articles and remove them from the mark lists."
5129   (when missing
5130     (let (var m)
5131       ;; Go through all types.
5132       (dolist (elem gnus-article-mark-lists)
5133         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5134           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5135           (when (symbol-value var)
5136             ;; This list has articles.  So we delete all missing
5137             ;; articles from it.
5138             (setq m missing)
5139             (while m
5140               (set var (delq (pop m) (symbol-value var))))))))))
5141
5142 (defun gnus-update-marks ()
5143   "Enter the various lists of marked articles into the newsgroup info list."
5144   (let ((types gnus-article-mark-lists)
5145         (info (gnus-get-info gnus-newsgroup-name))
5146         type list newmarked symbol delta-marks)
5147     (when info
5148       ;; Add all marks lists to the list of marks lists.
5149       (while (setq type (pop types))
5150         (setq list (symbol-value
5151                     (setq symbol
5152                           (intern (format "gnus-newsgroup-%s" (car type))))))
5153
5154         (when list
5155           ;; Get rid of the entries of the articles that have the
5156           ;; default score.
5157           (when (and (eq (cdr type) 'score)
5158                      gnus-save-score
5159                      list)
5160             (let* ((arts list)
5161                    (prev (cons nil list))
5162                    (all prev))
5163               (while arts
5164                 (if (or (not (consp (car arts)))
5165                         (= (cdar arts) gnus-summary-default-score))
5166                     (setcdr prev (cdr arts))
5167                   (setq prev arts))
5168                 (setq arts (cdr arts)))
5169               (setq list (cdr all)))))
5170
5171         (when (eq (cdr type) 'seen)
5172           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5173
5174         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5175           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5176
5177         (when (and (gnus-check-backend-function
5178                     'request-set-mark gnus-newsgroup-name)
5179                    (not (gnus-article-unpropagatable-p (cdr type))))
5180           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5181                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5182                  (add (gnus-remove-from-range
5183                        (gnus-copy-sequence list) old)))
5184             (when add
5185               (push (list add 'add (list (cdr type))) delta-marks))
5186             (when del
5187               (push (list del 'del (list (cdr type))) delta-marks))))
5188
5189         (when list
5190           (push (cons (cdr type) list) newmarked)))
5191
5192       (when delta-marks
5193         (unless (gnus-check-group gnus-newsgroup-name)
5194           (error "Can't open server for %s" gnus-newsgroup-name))
5195         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5196
5197       ;; Enter these new marks into the info of the group.
5198       (if (nthcdr 3 info)
5199           (setcar (nthcdr 3 info) newmarked)
5200         ;; Add the marks lists to the end of the info.
5201         (when newmarked
5202           (setcdr (nthcdr 2 info) (list newmarked))))
5203
5204       ;; Cut off the end of the info if there's nothing else there.
5205       (let ((i 5))
5206         (while (and (> i 2)
5207                     (not (nth i info)))
5208           (when (nthcdr (decf i) info)
5209             (setcdr (nthcdr i info) nil)))))))
5210
5211 (defun gnus-set-mode-line (where)
5212   "Set the mode line of the article or summary buffers.
5213 If WHERE is `summary', the summary mode line format will be used."
5214   ;; Is this mode line one we keep updated?
5215   (when (and (memq where gnus-updated-mode-lines)
5216              (symbol-value
5217               (intern (format "gnus-%s-mode-line-format-spec" where))))
5218     (let (mode-string)
5219       (save-excursion
5220         ;; We evaluate this in the summary buffer since these
5221         ;; variables are buffer-local to that buffer.
5222         (set-buffer gnus-summary-buffer)
5223        ;; We bind all these variables that are used in the `eval' form
5224         ;; below.
5225         (let* ((mformat (symbol-value
5226                          (intern
5227                           (format "gnus-%s-mode-line-format-spec" where))))
5228                (gnus-tmp-group-name (gnus-group-decoded-name
5229                                      gnus-newsgroup-name))
5230                (gnus-tmp-article-number (or gnus-current-article 0))
5231                (gnus-tmp-unread gnus-newsgroup-unreads)
5232                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5233                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5234                (gnus-tmp-unread-and-unselected
5235                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5236                             (zerop gnus-tmp-unselected))
5237                        "")
5238                       ((zerop gnus-tmp-unselected)
5239                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5240                       (t (format "{%d(+%d) more}"
5241                                  gnus-tmp-unread-and-unticked
5242                                  gnus-tmp-unselected))))
5243                (gnus-tmp-subject
5244                 (if (and gnus-current-headers
5245                          (vectorp gnus-current-headers))
5246                     (gnus-mode-string-quote
5247                      (mail-header-subject gnus-current-headers))
5248                   ""))
5249                bufname-length max-len
5250                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5251           (setq mode-string (eval mformat))
5252           (setq bufname-length (if (string-match "%b" mode-string)
5253                                    (- (length
5254                                        (buffer-name
5255                                         (if (eq where 'summary)
5256                                             nil
5257                                           (get-buffer gnus-article-buffer))))
5258                                       2)
5259                                  0))
5260           (setq max-len (max 4 (if gnus-mode-non-string-length
5261                                    (- (window-width)
5262                                       gnus-mode-non-string-length
5263                                       bufname-length)
5264                                  (length mode-string))))
5265           ;; We might have to chop a bit of the string off...
5266           (when (> (length mode-string) max-len)
5267             (setq mode-string
5268                   (concat (truncate-string-to-width mode-string (- max-len 3))
5269                           "...")))
5270           ;; Pad the mode string a bit.
5271           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5272       ;; Update the mode line.
5273       (setq mode-line-buffer-identification
5274             (gnus-mode-line-buffer-identification (list mode-string)))
5275       (set-buffer-modified-p t))))
5276
5277 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5278   "Go through the HEADERS list and add all Xrefs to a hash table.
5279 The resulting hash table is returned, or nil if no Xrefs were found."
5280   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5281          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5282          (xref-hashtb (gnus-make-hashtable))
5283          start group entry number xrefs header)
5284     (while headers
5285       (setq header (pop headers))
5286       (when (and (setq xrefs (mail-header-xref header))
5287                  (not (memq (setq number (mail-header-number header))
5288                             unreads)))
5289         (setq start 0)
5290         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5291           (setq start (match-end 0))
5292           (setq group (if prefix
5293                           (concat prefix (substring xrefs (match-beginning 1)
5294                                                     (match-end 1)))
5295                         (substring xrefs (match-beginning 1) (match-end 1))))
5296           (setq number
5297                 (string-to-int (substring xrefs (match-beginning 2)
5298                                           (match-end 2))))
5299           (if (setq entry (gnus-gethash group xref-hashtb))
5300               (setcdr entry (cons number (cdr entry)))
5301             (gnus-sethash group (cons number nil) xref-hashtb)))))
5302     (and start xref-hashtb)))
5303
5304 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5305   "Look through all the headers and mark the Xrefs as read."
5306   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5307         name entry info xref-hashtb idlist method nth4)
5308     (save-excursion
5309       (set-buffer gnus-group-buffer)
5310       (when (setq xref-hashtb
5311                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5312         (mapatoms
5313          (lambda (group)
5314            (unless (string= from-newsgroup (setq name (symbol-name group)))
5315              (setq idlist (symbol-value group))
5316              ;; Dead groups are not updated.
5317              (and (prog1
5318                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5319                             info (nth 2 entry))
5320                     (when (stringp (setq nth4 (gnus-info-method info)))
5321                       (setq nth4 (gnus-server-to-method nth4))))
5322                   ;; Only do the xrefs if the group has the same
5323                   ;; select method as the group we have just read.
5324                   (or (gnus-methods-equal-p
5325                        nth4 (gnus-find-method-for-group from-newsgroup))
5326                       virtual
5327                       (equal nth4 (setq method (gnus-find-method-for-group
5328                                                 from-newsgroup)))
5329                       (and (equal (car nth4) (car method))
5330                            (equal (nth 1 nth4) (nth 1 method))))
5331                   gnus-use-cross-reference
5332                   (or (not (eq gnus-use-cross-reference t))
5333                       virtual
5334                       ;; Only do cross-references on subscribed
5335                       ;; groups, if that is what is wanted.
5336                       (<= (gnus-info-level info) gnus-level-subscribed))
5337                   (gnus-group-make-articles-read name idlist))))
5338          xref-hashtb)))))
5339
5340 (defun gnus-compute-read-articles (group articles)
5341   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5342          (info (nth 2 entry))
5343          (active (gnus-active group))
5344          ninfo)
5345     (when entry
5346       ;; First peel off all invalid article numbers.
5347       (when active
5348         (let ((ids articles)
5349               id first)
5350           (while (setq id (pop ids))
5351             (when (and first (> id (cdr active)))
5352               ;; We'll end up in this situation in one particular
5353               ;; obscure situation.  If you re-scan a group and get
5354               ;; a new article that is cross-posted to a different
5355               ;; group that has not been re-scanned, you might get
5356               ;; crossposted article that has a higher number than
5357               ;; Gnus believes possible.  So we re-activate this
5358               ;; group as well.  This might mean doing the
5359               ;; crossposting thingy will *increase* the number
5360               ;; of articles in some groups.  Tsk, tsk.
5361               (setq active (or (gnus-activate-group group) active)))
5362             (when (or (> id (cdr active))
5363                       (< id (car active)))
5364               (setq articles (delq id articles))))))
5365       ;; If the read list is nil, we init it.
5366       (if (and active
5367                (null (gnus-info-read info))
5368                (> (car active) 1))
5369           (setq ninfo (cons 1 (1- (car active))))
5370         (setq ninfo (gnus-info-read info)))
5371       ;; Then we add the read articles to the range.
5372       (gnus-add-to-range
5373        ninfo (setq articles (sort articles '<))))))
5374
5375 (defun gnus-group-make-articles-read (group articles)
5376   "Update the info of GROUP to say that ARTICLES are read."
5377   (let* ((num 0)
5378          (entry (gnus-gethash group gnus-newsrc-hashtb))
5379          (info (nth 2 entry))
5380          (active (gnus-active group))
5381          range)
5382     (when entry
5383       (setq range (gnus-compute-read-articles group articles))
5384       (save-excursion
5385         (set-buffer gnus-group-buffer)
5386         (gnus-undo-register
5387           `(progn
5388              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5389              (gnus-info-set-read ',info ',(gnus-info-read info))
5390              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5391              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5392              (gnus-group-update-group ,group t))))
5393       ;; Add the read articles to the range.
5394       (gnus-info-set-read info range)
5395       (gnus-request-set-mark group (list (list range 'add '(read))))
5396       ;; Then we have to re-compute how many unread
5397       ;; articles there are in this group.
5398       (when active
5399         (cond
5400          ((not range)
5401           (setq num (- (1+ (cdr active)) (car active))))
5402          ((not (listp (cdr range)))
5403           (setq num (- (cdr active) (- (1+ (cdr range))
5404                                        (car range)))))
5405          (t
5406           (while range
5407             (if (numberp (car range))
5408                 (setq num (1+ num))
5409               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5410             (setq range (cdr range)))
5411           (setq num (- (cdr active) num))))
5412         ;; Update the number of unread articles.
5413         (setcar entry num)
5414         ;; Update the group buffer.
5415         (gnus-group-update-group group t)))))
5416
5417 (defvar gnus-newsgroup-none-id 0)
5418
5419 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5420   (let ((cur nntp-server-buffer)
5421         (dependencies
5422          (or dependencies
5423              (save-excursion (set-buffer gnus-summary-buffer)
5424                              gnus-newsgroup-dependencies)))
5425         headers id end ref
5426         (mail-parse-charset gnus-newsgroup-charset)
5427         (mail-parse-ignored-charsets
5428          (save-excursion (condition-case nil
5429                              (set-buffer gnus-summary-buffer)
5430                            (error))
5431                          gnus-newsgroup-ignored-charsets)))
5432     (save-excursion
5433       (set-buffer nntp-server-buffer)
5434       ;; Translate all TAB characters into SPACE characters.
5435       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5436       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5437       (gnus-run-hooks 'gnus-parse-headers-hook)
5438       (let ((case-fold-search t)
5439             in-reply-to header p lines chars)
5440         (goto-char (point-min))
5441         ;; Search to the beginning of the next header.  Error messages
5442         ;; do not begin with 2 or 3.
5443         (while (re-search-forward "^[23][0-9]+ " nil t)
5444           (setq id nil
5445                 ref nil)
5446           ;; This implementation of this function, with nine
5447           ;; search-forwards instead of the one re-search-forward and
5448           ;; a case (which basically was the old function) is actually
5449           ;; about twice as fast, even though it looks messier.  You
5450           ;; can't have everything, I guess.  Speed and elegance
5451           ;; doesn't always go hand in hand.
5452           (setq
5453            header
5454            (vector
5455             ;; Number.
5456             (prog1
5457                 (read cur)
5458               (end-of-line)
5459               (setq p (point))
5460               (narrow-to-region (point)
5461                                 (or (and (search-forward "\n.\n" nil t)
5462                                          (- (point) 2))
5463                                     (point))))
5464             ;; Subject.
5465             (progn
5466               (goto-char p)
5467               (if (search-forward "\nsubject:" nil t)
5468                   (funcall gnus-decode-encoded-word-function
5469                            (nnheader-header-value))
5470                 "(none)"))
5471             ;; From.
5472             (progn
5473               (goto-char p)
5474               (if (search-forward "\nfrom:" nil t)
5475                   (funcall gnus-decode-encoded-word-function
5476                            (nnheader-header-value))
5477                 "(nobody)"))
5478             ;; Date.
5479             (progn
5480               (goto-char p)
5481               (if (search-forward "\ndate:" nil t)
5482                   (nnheader-header-value) ""))
5483             ;; Message-ID.
5484             (progn
5485               (goto-char p)
5486               (setq id (if (re-search-forward
5487                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5488                            ;; We do it this way to make sure the Message-ID
5489                            ;; is (somewhat) syntactically valid.
5490                            (buffer-substring (match-beginning 1)
5491                                              (match-end 1))
5492                          ;; If there was no message-id, we just fake one
5493                          ;; to make subsequent routines simpler.
5494                          (nnheader-generate-fake-message-id))))
5495             ;; References.
5496             (progn
5497               (goto-char p)
5498               (if (search-forward "\nreferences:" nil t)
5499                   (progn
5500                     (setq end (point))
5501                     (prog1
5502                         (nnheader-header-value)
5503                       (setq ref
5504                             (buffer-substring
5505                              (progn
5506                                (end-of-line)
5507                                (search-backward ">" end t)
5508                                (1+ (point)))
5509                              (progn
5510                                (search-backward "<" end t)
5511                                (point))))))
5512                 ;; Get the references from the in-reply-to header if there
5513                 ;; were no references and the in-reply-to header looks
5514                 ;; promising.
5515                 (if (and (search-forward "\nin-reply-to:" nil t)
5516                          (setq in-reply-to (nnheader-header-value))
5517                          (string-match "<[^>]+>" in-reply-to))
5518                     (let (ref2)
5519                       (setq ref (substring in-reply-to (match-beginning 0)
5520                                            (match-end 0)))
5521                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5522                         (setq ref2 (substring in-reply-to (match-beginning 0)
5523                                               (match-end 0)))
5524                         (when (> (length ref2) (length ref))
5525                           (setq ref ref2)))
5526                       ref)
5527                   (setq ref nil))))
5528             ;; Chars.
5529             (progn
5530               (goto-char p)
5531               (if (search-forward "\nchars: " nil t)
5532                   (if (numberp (setq chars (ignore-errors (read cur))))
5533                       chars -1)
5534                 -1))
5535             ;; Lines.
5536             (progn
5537               (goto-char p)
5538               (if (search-forward "\nlines: " nil t)
5539                   (if (numberp (setq lines (ignore-errors (read cur))))
5540                       lines -1)
5541                 -1))
5542             ;; Xref.
5543             (progn
5544               (goto-char p)
5545               (and (search-forward "\nxref:" nil t)
5546                    (nnheader-header-value)))
5547             ;; Extra.
5548             (when gnus-extra-headers
5549               (let ((extra gnus-extra-headers)
5550                     out)
5551                 (while extra
5552                   (goto-char p)
5553                   (when (search-forward
5554                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5555                     (push (cons (car extra) (nnheader-header-value))
5556                           out))
5557                   (pop extra))
5558                 out))))
5559           (when (equal id ref)
5560             (setq ref nil))
5561
5562           (when gnus-alter-header-function
5563             (funcall gnus-alter-header-function header)
5564             (setq id (mail-header-id header)
5565                   ref (gnus-parent-id (mail-header-references header))))
5566
5567           (when (setq header
5568                       (gnus-dependencies-add-header
5569                        header dependencies force-new))
5570             (push header headers))
5571           (goto-char (point-max))
5572           (widen))
5573         (nreverse headers)))))
5574
5575 ;; Goes through the xover lines and returns a list of vectors
5576 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5577                                                   force-new dependencies
5578                                                   group also-fetch-heads)
5579   "Parse the news overview data in the server buffer.
5580 Return a list of headers that match SEQUENCE (see
5581 `nntp-retrieve-headers')."
5582   ;; Get the Xref when the users reads the articles since most/some
5583   ;; NNTP servers do not include Xrefs when using XOVER.
5584   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5585   (let ((mail-parse-charset gnus-newsgroup-charset)
5586         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5587         (cur nntp-server-buffer)
5588         (dependencies (or dependencies gnus-newsgroup-dependencies))
5589         (allp (cond
5590                ((eq gnus-read-all-available-headers t)
5591                 t)
5592                ((stringp gnus-read-all-available-headers)
5593                 (string-match gnus-read-all-available-headers group))
5594                (t
5595                 nil)))
5596         number headers header)
5597     (save-excursion
5598       (set-buffer nntp-server-buffer)
5599       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5600       ;; Allow the user to mangle the headers before parsing them.
5601       (gnus-run-hooks 'gnus-parse-headers-hook)
5602       (goto-char (point-min))
5603       (gnus-parse-without-error
5604         (while (and (or sequence allp)
5605                     (not (eobp)))
5606           (setq number (read cur))
5607           (when (not allp)
5608             (while (and sequence
5609                         (< (car sequence) number))
5610               (setq sequence (cdr sequence))))
5611           (when (and (or allp
5612                          (and sequence
5613                               (eq number (car sequence))))
5614                      (progn
5615                        (setq sequence (cdr sequence))
5616                        (setq header (inline
5617                                       (gnus-nov-parse-line
5618                                        number dependencies force-new)))))
5619             (push header headers))
5620           (forward-line 1)))
5621       ;; A common bug in inn is that if you have posted an article and
5622       ;; then retrieves the active file, it will answer correctly --
5623       ;; the new article is included.  However, a NOV entry for the
5624       ;; article may not have been generated yet, so this may fail.
5625       ;; We work around this problem by retrieving the last few
5626       ;; headers using HEAD.
5627       (if (or (not also-fetch-heads)
5628               (not sequence))
5629           ;; We (probably) got all the headers.
5630           (nreverse headers)
5631         (let ((gnus-nov-is-evil t))
5632           (nconc
5633            (nreverse headers)
5634            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5635              (gnus-get-newsgroup-headers))))))))
5636
5637 (defun gnus-article-get-xrefs ()
5638   "Fill in the Xref value in `gnus-current-headers', if necessary.
5639 This is meant to be called in `gnus-article-internal-prepare-hook'."
5640   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5641                                  gnus-current-headers)))
5642     (or (not gnus-use-cross-reference)
5643         (not headers)
5644         (and (mail-header-xref headers)
5645              (not (string= (mail-header-xref headers) "")))
5646         (let ((case-fold-search t)
5647               xref)
5648           (save-restriction
5649             (nnheader-narrow-to-headers)
5650             (goto-char (point-min))
5651             (when (or (and (not (eobp))
5652                            (eq (downcase (char-after)) ?x)
5653                            (looking-at "Xref:"))
5654                       (search-forward "\nXref:" nil t))
5655               (goto-char (1+ (match-end 0)))
5656               (setq xref (buffer-substring (point)
5657                                            (progn (end-of-line) (point))))
5658               (mail-header-set-xref headers xref)))))))
5659
5660 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5661   "Find article ID and insert the summary line for that article.
5662 OLD-HEADER can either be a header or a line number to insert
5663 the subject line on."
5664   (let* ((line (and (numberp old-header) old-header))
5665          (old-header (and (vectorp old-header) old-header))
5666          (header (cond ((and old-header use-old-header)
5667                         old-header)
5668                        ((and (numberp id)
5669                              (gnus-number-to-header id))
5670                         (gnus-number-to-header id))
5671                        (t
5672                         (gnus-read-header id))))
5673          (number (and (numberp id) id))
5674          d)
5675     (when header
5676       ;; Rebuild the thread that this article is part of and go to the
5677       ;; article we have fetched.
5678       (when (and (not gnus-show-threads)
5679                  old-header)
5680         (when (and number
5681                    (setq d (gnus-data-find (mail-header-number old-header))))
5682           (goto-char (gnus-data-pos d))
5683           (gnus-data-remove
5684            number
5685            (- (gnus-point-at-bol)
5686               (prog1
5687                   (1+ (gnus-point-at-eol))
5688                 (gnus-delete-line))))))
5689       (when old-header
5690         (mail-header-set-number header (mail-header-number old-header)))
5691       (setq gnus-newsgroup-sparse
5692             (delq (setq number (mail-header-number header))
5693                   gnus-newsgroup-sparse))
5694       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5695       (push number gnus-newsgroup-limit)
5696       (gnus-rebuild-thread (mail-header-id header) line)
5697       (gnus-summary-goto-subject number nil t))
5698     (when (and (numberp number)
5699                (> number 0))
5700       ;; We have to update the boundaries even if we can't fetch the
5701       ;; article if ID is a number -- so that the next `P' or `N'
5702       ;; command will fetch the previous (or next) article even
5703       ;; if the one we tried to fetch this time has been canceled.
5704       (when (> number gnus-newsgroup-end)
5705         (setq gnus-newsgroup-end number))
5706       (when (< number gnus-newsgroup-begin)
5707         (setq gnus-newsgroup-begin number))
5708       (setq gnus-newsgroup-unselected
5709             (delq number gnus-newsgroup-unselected)))
5710     ;; Report back a success?
5711     (and header (mail-header-number header))))
5712
5713 ;;; Process/prefix in the summary buffer
5714
5715 (defun gnus-summary-work-articles (n)
5716   "Return a list of articles to be worked upon.
5717 The prefix argument, the list of process marked articles, and the
5718 current article will be taken into consideration."
5719   (save-excursion
5720     (set-buffer gnus-summary-buffer)
5721     (cond
5722      (n
5723       ;; A numerical prefix has been given.
5724       (setq n (prefix-numeric-value n))
5725       (let ((backward (< n 0))
5726             (n (abs (prefix-numeric-value n)))
5727             articles article)
5728         (save-excursion
5729           (while
5730               (and (> n 0)
5731                    (push (setq article (gnus-summary-article-number))
5732                          articles)
5733                    (if backward
5734                        (gnus-summary-find-prev nil article)
5735                      (gnus-summary-find-next nil article)))
5736             (decf n)))
5737         (nreverse articles)))
5738      ((and (gnus-region-active-p) (mark))
5739       (message "region active")
5740       ;; Work on the region between point and mark.
5741       (let ((max (max (point) (mark)))
5742             articles article)
5743         (save-excursion
5744           (goto-char (min (min (point) (mark))))
5745           (while
5746               (and
5747                (push (setq article (gnus-summary-article-number)) articles)
5748                (gnus-summary-find-next nil article)
5749                (< (point) max)))
5750           (nreverse articles))))
5751      (gnus-newsgroup-processable
5752       ;; There are process-marked articles present.
5753       ;; Save current state.
5754       (gnus-summary-save-process-mark)
5755       ;; Return the list.
5756       (reverse gnus-newsgroup-processable))
5757      (t
5758       ;; Just return the current article.
5759       (list (gnus-summary-article-number))))))
5760
5761 (defmacro gnus-summary-iterate (arg &rest forms)
5762   "Iterate over the process/prefixed articles and do FORMS.
5763 ARG is the interactive prefix given to the command.  FORMS will be
5764 executed with point over the summary line of the articles."
5765   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5766     `(let ((,articles (gnus-summary-work-articles ,arg)))
5767        (while ,articles
5768          (gnus-summary-goto-subject (car ,articles))
5769          ,@forms
5770          (pop ,articles)))))
5771
5772 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5773 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5774
5775 (defun gnus-summary-save-process-mark ()
5776   "Push the current set of process marked articles on the stack."
5777   (interactive)
5778   (push (copy-sequence gnus-newsgroup-processable)
5779         gnus-newsgroup-process-stack))
5780
5781 (defun gnus-summary-kill-process-mark ()
5782   "Push the current set of process marked articles on the stack and unmark."
5783   (interactive)
5784   (gnus-summary-save-process-mark)
5785   (gnus-summary-unmark-all-processable))
5786
5787 (defun gnus-summary-yank-process-mark ()
5788   "Pop the last process mark state off the stack and restore it."
5789   (interactive)
5790   (unless gnus-newsgroup-process-stack
5791     (error "Empty mark stack"))
5792   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5793
5794 (defun gnus-summary-process-mark-set (set)
5795   "Make SET into the current process marked articles."
5796   (gnus-summary-unmark-all-processable)
5797   (while set
5798     (gnus-summary-set-process-mark (pop set))))
5799
5800 ;;; Searching and stuff
5801
5802 (defun gnus-summary-search-group (&optional backward use-level)
5803   "Search for next unread newsgroup.
5804 If optional argument BACKWARD is non-nil, search backward instead."
5805   (save-excursion
5806     (set-buffer gnus-group-buffer)
5807     (when (gnus-group-search-forward
5808            backward nil (if use-level (gnus-group-group-level) nil))
5809       (gnus-group-group-name))))
5810
5811 (defun gnus-summary-best-group (&optional exclude-group)
5812   "Find the name of the best unread group.
5813 If EXCLUDE-GROUP, do not go to this group."
5814   (save-excursion
5815     (set-buffer gnus-group-buffer)
5816     (save-excursion
5817       (gnus-group-best-unread-group exclude-group))))
5818
5819 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5820   (if backward (gnus-summary-find-prev)
5821     (let* ((dummy (gnus-summary-article-intangible-p))
5822            (article (or article (gnus-summary-article-number)))
5823            (arts (gnus-data-find-list article))
5824            result)
5825       (when (and (not dummy)
5826                  (or (not gnus-summary-check-current)
5827                      (not unread)
5828                      (not (gnus-data-unread-p (car arts)))))
5829         (setq arts (cdr arts)))
5830       (when (setq result
5831                   (if unread
5832                       (progn
5833                         (while arts
5834                           (when (or (and undownloaded
5835                                          (eq gnus-undownloaded-mark
5836                                              (gnus-data-mark (car arts))))
5837                                     (gnus-data-unread-p (car arts)))
5838                             (setq result (car arts)
5839                                   arts nil))
5840                           (setq arts (cdr arts)))
5841                         result)
5842                     (car arts)))
5843         (goto-char (gnus-data-pos result))
5844         (gnus-data-number result)))))
5845
5846 (defun gnus-summary-find-prev (&optional unread article)
5847   (let* ((eobp (eobp))
5848          (article (or article (gnus-summary-article-number)))
5849          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5850          result)
5851     (when (and (not eobp)
5852                (or (not gnus-summary-check-current)
5853                    (not unread)
5854                    (not (gnus-data-unread-p (car arts)))))
5855       (setq arts (cdr arts)))
5856     (when (setq result
5857                 (if unread
5858                     (progn
5859                       (while arts
5860                         (when (gnus-data-unread-p (car arts))
5861                           (setq result (car arts)
5862                                 arts nil))
5863                         (setq arts (cdr arts)))
5864                       result)
5865                   (car arts)))
5866       (goto-char (gnus-data-pos result))
5867       (gnus-data-number result))))
5868
5869 (defun gnus-summary-find-subject (subject &optional unread backward article)
5870   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5871          (article (or article (gnus-summary-article-number)))
5872          (articles (gnus-data-list backward))
5873          (arts (gnus-data-find-list article articles))
5874          result)
5875     (when (or (not gnus-summary-check-current)
5876               (not unread)
5877               (not (gnus-data-unread-p (car arts))))
5878       (setq arts (cdr arts)))
5879     (while arts
5880       (and (or (not unread)
5881                (gnus-data-unread-p (car arts)))
5882            (vectorp (gnus-data-header (car arts)))
5883            (gnus-subject-equal
5884             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5885            (setq result (car arts)
5886                  arts nil))
5887       (setq arts (cdr arts)))
5888     (and result
5889          (goto-char (gnus-data-pos result))
5890          (gnus-data-number result))))
5891
5892 (defun gnus-summary-search-forward (&optional unread subject backward)
5893   "Search forward for an article.
5894 If UNREAD, look for unread articles.  If SUBJECT, look for
5895 articles with that subject.  If BACKWARD, search backward instead."
5896   (cond (subject (gnus-summary-find-subject subject unread backward))
5897         (backward (gnus-summary-find-prev unread))
5898         (t (gnus-summary-find-next unread))))
5899
5900 (defun gnus-recenter (&optional n)
5901   "Center point in window and redisplay frame.
5902 Also do horizontal recentering."
5903   (interactive "P")
5904   (when (and gnus-auto-center-summary
5905              (not (eq gnus-auto-center-summary 'vertical)))
5906     (gnus-horizontal-recenter))
5907   (recenter n))
5908
5909 (defun gnus-summary-recenter ()
5910   "Center point in the summary window.
5911 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5912 displayed, no centering will be performed."
5913   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5914 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5915   (interactive)
5916   (let* ((top (cond ((< (window-height) 4) 0)
5917                     ((< (window-height) 7) 1)
5918                     (t (if (numberp gnus-auto-center-summary)
5919                            gnus-auto-center-summary
5920                          2))))
5921          (height (1- (window-height)))
5922          (bottom (save-excursion (goto-char (point-max))
5923                                  (forward-line (- height))
5924                                  (point)))
5925          (window (get-buffer-window (current-buffer))))
5926     ;; The user has to want it.
5927     (when gnus-auto-center-summary
5928       (when (get-buffer-window gnus-article-buffer)
5929         ;; Only do recentering when the article buffer is displayed,
5930       ;; Set the window start to either `bottom', which is the biggest
5931         ;; possible valid number, or the second line from the top,
5932         ;; whichever is the least.
5933         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5934           (if (> bottom top-pos)
5935               ;; Keep the second line from the top visible
5936               (set-window-start window top-pos t)
5937             ;; Try to keep the bottom line visible; if it's partially
5938             ;; obscured, either scroll one more line to make it fully
5939             ;; visible, or revert to using TOP-POS.
5940             (save-excursion
5941               (goto-char (point-max))
5942               (forward-line -1)
5943               (let ((last-line-start (point)))
5944                 (goto-char bottom)
5945                 (set-window-start window (point) t)
5946                 (when (not (pos-visible-in-window-p last-line-start window))
5947                   (forward-line 1)
5948                   (set-window-start window (min (point) top-pos) t)))))))
5949       ;; Do horizontal recentering while we're at it.
5950       (when (and (get-buffer-window (current-buffer) t)
5951                  (not (eq gnus-auto-center-summary 'vertical)))
5952         (let ((selected (selected-window)))
5953           (select-window (get-buffer-window (current-buffer) t))
5954           (gnus-summary-position-point)
5955           (gnus-horizontal-recenter)
5956           (select-window selected))))))
5957
5958 (defun gnus-summary-jump-to-group (newsgroup)
5959   "Move point to NEWSGROUP in group mode buffer."
5960   ;; Keep update point of group mode buffer if visible.
5961   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5962       (save-window-excursion
5963         ;; Take care of tree window mode.
5964         (when (get-buffer-window gnus-group-buffer)
5965           (pop-to-buffer gnus-group-buffer))
5966         (gnus-group-jump-to-group newsgroup))
5967     (save-excursion
5968       ;; Take care of tree window mode.
5969       (if (get-buffer-window gnus-group-buffer)
5970           (pop-to-buffer gnus-group-buffer)
5971         (set-buffer gnus-group-buffer))
5972       (gnus-group-jump-to-group newsgroup))))
5973
5974 ;; This function returns a list of article numbers based on the
5975 ;; difference between the ranges of read articles in this group and
5976 ;; the range of active articles.
5977 (defun gnus-list-of-unread-articles (group)
5978   (let* ((read (gnus-info-read (gnus-get-info group)))
5979          (active (or (gnus-active group) (gnus-activate-group group)))
5980          (last (cdr active))
5981          first nlast unread)
5982     ;; If none are read, then all are unread.
5983     (if (not read)
5984         (setq first (car active))
5985       ;; If the range of read articles is a single range, then the
5986       ;; first unread article is the article after the last read
5987       ;; article.  Sounds logical, doesn't it?
5988       (if (and (not (listp (cdr read)))
5989                (or (< (car read) (car active))
5990                    (progn (setq read (list read))
5991                           nil)))
5992           (setq first (max (car active) (1+ (cdr read))))
5993         ;; `read' is a list of ranges.
5994         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5995                                   (caar read)))
5996                   1)
5997           (setq first (car active)))
5998         (while read
5999           (when first
6000             (while (< first nlast)
6001               (push first unread)
6002               (setq first (1+ first))))
6003           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6004           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6005           (setq read (cdr read)))))
6006     ;; And add the last unread articles.
6007     (while (<= first last)
6008       (push first unread)
6009       (setq first (1+ first)))
6010     ;; Return the list of unread articles.
6011     (delq 0 (nreverse unread))))
6012
6013 (defun gnus-list-of-read-articles (group)
6014   "Return a list of unread, unticked and non-dormant articles."
6015   (let* ((info (gnus-get-info group))
6016          (marked (gnus-info-marks info))
6017          (active (gnus-active group)))
6018     (and info active
6019          (gnus-list-range-difference
6020           (gnus-list-range-difference
6021            (gnus-sorted-complement
6022             (gnus-uncompress-range active)
6023             (gnus-list-of-unread-articles group))
6024            (cdr (assq 'dormant marked)))
6025           (cdr (assq 'tick marked))))))
6026
6027 ;; Various summary commands
6028
6029 (defun gnus-summary-select-article-buffer ()
6030   "Reconfigure windows to show article buffer."
6031   (interactive)
6032   (if (not (gnus-buffer-live-p gnus-article-buffer))
6033       (error "There is no article buffer for this summary buffer")
6034     (gnus-configure-windows 'article)
6035     (select-window (get-buffer-window gnus-article-buffer))))
6036
6037 (defun gnus-summary-universal-argument (arg)
6038   "Perform any operation on all articles that are process/prefixed."
6039   (interactive "P")
6040   (let ((articles (gnus-summary-work-articles arg))
6041         func article)
6042     (if (eq
6043          (setq
6044           func
6045           (key-binding
6046            (read-key-sequence
6047             (substitute-command-keys
6048              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6049          'undefined)
6050         (gnus-error 1 "Undefined key")
6051       (save-excursion
6052         (while articles
6053           (gnus-summary-goto-subject (setq article (pop articles)))
6054           (let (gnus-newsgroup-processable)
6055             (command-execute func))
6056           (gnus-summary-remove-process-mark article)))))
6057   (gnus-summary-position-point))
6058
6059 (defun gnus-summary-toggle-truncation (&optional arg)
6060   "Toggle truncation of summary lines.
6061 With arg, turn line truncation on iff arg is positive."
6062   (interactive "P")
6063   (setq truncate-lines
6064         (if (null arg) (not truncate-lines)
6065           (> (prefix-numeric-value arg) 0)))
6066   (redraw-display))
6067
6068 (defun gnus-summary-reselect-current-group (&optional all rescan)
6069   "Exit and then reselect the current newsgroup.
6070 The prefix argument ALL means to select all articles."
6071   (interactive "P")
6072   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6073     (error "Ephemeral groups can't be reselected"))
6074   (let ((current-subject (gnus-summary-article-number))
6075         (group gnus-newsgroup-name))
6076     (setq gnus-newsgroup-begin nil)
6077     (gnus-summary-exit)
6078     ;; We have to adjust the point of group mode buffer because
6079     ;; point was moved to the next unread newsgroup by exiting.
6080     (gnus-summary-jump-to-group group)
6081     (when rescan
6082       (save-excursion
6083         (gnus-group-get-new-news-this-group 1)))
6084     (gnus-group-read-group all t)
6085     (gnus-summary-goto-subject current-subject nil t)))
6086
6087 (defun gnus-summary-rescan-group (&optional all)
6088   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6089   (interactive "P")
6090   (gnus-summary-reselect-current-group all t))
6091
6092 (defun gnus-summary-update-info (&optional non-destructive)
6093   (save-excursion
6094     (let ((group gnus-newsgroup-name))
6095       (when group
6096         (when gnus-newsgroup-kill-headers
6097           (setq gnus-newsgroup-killed
6098                 (gnus-compress-sequence
6099                  (gnus-sorted-union
6100                   (gnus-list-range-intersection
6101                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6102                   gnus-newsgroup-unreads)
6103                  t)))
6104         (unless (listp (cdr gnus-newsgroup-killed))
6105           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6106         (let ((headers gnus-newsgroup-headers))
6107           ;; Set the new ranges of read articles.
6108           (save-excursion
6109             (set-buffer gnus-group-buffer)
6110             (gnus-undo-force-boundary))
6111           (gnus-update-read-articles
6112            group (gnus-sorted-union
6113                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6114           ;; Set the current article marks.
6115           (let ((gnus-newsgroup-scored
6116                  (if (and (not gnus-save-score)
6117                           (not non-destructive))
6118                      nil
6119                    gnus-newsgroup-scored)))
6120             (save-excursion
6121               (gnus-update-marks)))
6122           ;; Do the cross-ref thing.
6123           (when gnus-use-cross-reference
6124             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6125           ;; Do not switch windows but change the buffer to work.
6126           (set-buffer gnus-group-buffer)
6127           (unless (gnus-ephemeral-group-p group)
6128             (gnus-group-update-group group)))))))
6129
6130 (defun gnus-summary-save-newsrc (&optional force)
6131   "Save the current number of read/marked articles in the dribble buffer.
6132 The dribble buffer will then be saved.
6133 If FORCE (the prefix), also save the .newsrc file(s)."
6134   (interactive "P")
6135   (gnus-summary-update-info t)
6136   (if force
6137       (gnus-save-newsrc-file)
6138     (gnus-dribble-save)))
6139
6140 (defun gnus-summary-exit (&optional temporary)
6141   "Exit reading current newsgroup, and then return to group selection mode.
6142 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6143   (interactive)
6144   (gnus-set-global-variables)
6145   (when (gnus-buffer-live-p gnus-article-buffer)
6146     (save-excursion
6147       (set-buffer gnus-article-buffer)
6148       (mm-destroy-parts gnus-article-mime-handles)
6149       ;; Set it to nil for safety reason.
6150       (setq gnus-article-mime-handle-alist nil)
6151       (setq gnus-article-mime-handles nil)))
6152   (gnus-kill-save-kill-buffer)
6153   (gnus-async-halt-prefetch)
6154   (let* ((group gnus-newsgroup-name)
6155          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6156          (mode major-mode)
6157          (group-point nil)
6158          (buf (current-buffer)))
6159     (unless quit-config
6160       ;; Do adaptive scoring, and possibly save score files.
6161       (when gnus-newsgroup-adaptive
6162         (gnus-score-adaptive))
6163       (when gnus-use-scoring
6164         (gnus-score-save)))
6165     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6166     ;; If we have several article buffers, we kill them at exit.
6167     (unless gnus-single-article-buffer
6168       (gnus-kill-buffer gnus-original-article-buffer)
6169       (setq gnus-article-current nil))
6170     (when gnus-use-cache
6171       (gnus-cache-possibly-remove-articles)
6172       (gnus-cache-save-buffers))
6173     (gnus-async-prefetch-remove-group group)
6174     (when gnus-suppress-duplicates
6175       (gnus-dup-enter-articles))
6176     (when gnus-use-trees
6177       (gnus-tree-close group))
6178     (when gnus-use-cache
6179       (gnus-cache-write-active))
6180     ;; Remove entries for this group.
6181     (nnmail-purge-split-history (gnus-group-real-name group))
6182     ;; Make all changes in this group permanent.
6183     (unless quit-config
6184       (gnus-run-hooks 'gnus-exit-group-hook)
6185       (gnus-summary-update-info))
6186     (gnus-close-group group)
6187     ;; Make sure where we were, and go to next newsgroup.
6188     (set-buffer gnus-group-buffer)
6189     (unless quit-config
6190       (gnus-group-jump-to-group group))
6191     (gnus-run-hooks 'gnus-summary-exit-hook)
6192     (unless (or quit-config
6193                 ;; If this group has disappeared from the summary
6194                 ;; buffer, don't skip forwards.
6195                 (not (string= group (gnus-group-group-name))))
6196       (gnus-group-next-unread-group 1))
6197     (setq group-point (point))
6198     (if temporary
6199         nil                             ;Nothing to do.
6200       ;; If we have several article buffers, we kill them at exit.
6201       (unless gnus-single-article-buffer
6202         (gnus-kill-buffer gnus-article-buffer)
6203         (gnus-kill-buffer gnus-original-article-buffer)
6204         (setq gnus-article-current nil))
6205       (set-buffer buf)
6206       (if (not gnus-kill-summary-on-exit)
6207           (progn
6208             (gnus-deaden-summary)
6209             (setq mode nil))
6210        ;; We set all buffer-local variables to nil.  It is unclear why
6211         ;; this is needed, but if we don't, buffer-local variables are
6212         ;; not garbage-collected, it seems.  This would the lead to en
6213         ;; ever-growing Emacs.
6214         (gnus-summary-clear-local-variables)
6215         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6216           (gnus-summary-clear-local-variables))
6217         (when (get-buffer gnus-article-buffer)
6218           (bury-buffer gnus-article-buffer))
6219         ;; We clear the global counterparts of the buffer-local
6220         ;; variables as well, just to be on the safe side.
6221         (set-buffer gnus-group-buffer)
6222         (gnus-summary-clear-local-variables)
6223         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6224           (gnus-summary-clear-local-variables)))
6225       (setq gnus-current-select-method gnus-select-method)
6226       (pop-to-buffer gnus-group-buffer)
6227       (if (not quit-config)
6228           (progn
6229             (goto-char group-point)
6230             (gnus-configure-windows 'group 'force))
6231         (gnus-handle-ephemeral-exit quit-config))
6232       ;; Return to group mode buffer.
6233       (when (eq mode 'gnus-summary-mode)
6234         (gnus-kill-buffer buf))
6235       ;; Clear the current group name.
6236       (unless quit-config
6237         (setq gnus-newsgroup-name nil)))))
6238
6239 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6240 (defun gnus-summary-exit-no-update (&optional no-questions)
6241   "Quit reading current newsgroup without updating read article info."
6242   (interactive)
6243   (let* ((group gnus-newsgroup-name)
6244          (quit-config (gnus-group-quit-config group)))
6245     (when (or no-questions
6246               gnus-expert-user
6247               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6248       (gnus-async-halt-prefetch)
6249       (mapcar 'funcall
6250               (delq 'gnus-summary-expire-articles
6251                     (copy-sequence gnus-summary-prepare-exit-hook)))
6252       (when (gnus-buffer-live-p gnus-article-buffer)
6253         (save-excursion
6254           (set-buffer gnus-article-buffer)
6255           (mm-destroy-parts gnus-article-mime-handles)
6256           ;; Set it to nil for safety reason.
6257           (setq gnus-article-mime-handle-alist nil)
6258           (setq gnus-article-mime-handles nil)))
6259       ;; If we have several article buffers, we kill them at exit.
6260       (unless gnus-single-article-buffer
6261         (gnus-kill-buffer gnus-article-buffer)
6262         (gnus-kill-buffer gnus-original-article-buffer)
6263         (setq gnus-article-current nil))
6264       (if (not gnus-kill-summary-on-exit)
6265           (gnus-deaden-summary)
6266         (gnus-close-group group)
6267         (gnus-summary-clear-local-variables)
6268         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6269           (gnus-summary-clear-local-variables))
6270         (set-buffer gnus-group-buffer)
6271         (gnus-summary-clear-local-variables)
6272         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6273           (gnus-summary-clear-local-variables))
6274         (when (get-buffer gnus-summary-buffer)
6275           (kill-buffer gnus-summary-buffer)))
6276       (unless gnus-single-article-buffer
6277         (setq gnus-article-current nil))
6278       (when gnus-use-trees
6279         (gnus-tree-close group))
6280       (gnus-async-prefetch-remove-group group)
6281       (when (get-buffer gnus-article-buffer)
6282         (bury-buffer gnus-article-buffer))
6283       ;; Return to the group buffer.
6284       (gnus-configure-windows 'group 'force)
6285       ;; Clear the current group name.
6286       (setq gnus-newsgroup-name nil)
6287       (when (equal (gnus-group-group-name) group)
6288         (gnus-group-next-unread-group 1))
6289       (when quit-config
6290         (gnus-handle-ephemeral-exit quit-config)))))
6291
6292 (defun gnus-handle-ephemeral-exit (quit-config)
6293   "Handle movement when leaving an ephemeral group.
6294 The state which existed when entering the ephemeral is reset."
6295   (if (not (buffer-name (car quit-config)))
6296       (gnus-configure-windows 'group 'force)
6297     (set-buffer (car quit-config))
6298     (cond ((eq major-mode 'gnus-summary-mode)
6299            (gnus-set-global-variables))
6300           ((eq major-mode 'gnus-article-mode)
6301            (save-excursion
6302              ;; The `gnus-summary-buffer' variable may point
6303              ;; to the old summary buffer when using a single
6304              ;; article buffer.
6305              (unless (gnus-buffer-live-p gnus-summary-buffer)
6306                (set-buffer gnus-group-buffer))
6307              (set-buffer gnus-summary-buffer)
6308              (gnus-set-global-variables))))
6309     (if (or (eq (cdr quit-config) 'article)
6310             (eq (cdr quit-config) 'pick))
6311         (progn
6312           ;; The current article may be from the ephemeral group
6313           ;; thus it is best that we reload this article
6314           (gnus-summary-show-article)
6315           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6316               (gnus-configure-windows 'pick 'force)
6317             (gnus-configure-windows (cdr quit-config) 'force)))
6318       (gnus-configure-windows (cdr quit-config) 'force))
6319     (when (eq major-mode 'gnus-summary-mode)
6320       (gnus-summary-next-subject 1 nil t)
6321       (gnus-summary-recenter)
6322       (gnus-summary-position-point))))
6323
6324 ;;; Dead summaries.
6325
6326 (defvar gnus-dead-summary-mode-map nil)
6327
6328 (unless gnus-dead-summary-mode-map
6329   (setq gnus-dead-summary-mode-map (make-keymap))
6330   (suppress-keymap gnus-dead-summary-mode-map)
6331   (substitute-key-definition
6332    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6333   (dolist (key '("\C-d" "\r" "\177" [delete]))
6334     (define-key gnus-dead-summary-mode-map
6335       key 'gnus-summary-wake-up-the-dead))
6336   (dolist (key '("q" "Q"))
6337     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6338
6339 (defvar gnus-dead-summary-mode nil
6340   "Minor mode for Gnus summary buffers.")
6341
6342 (defun gnus-dead-summary-mode (&optional arg)
6343   "Minor mode for Gnus summary buffers."
6344   (interactive "P")
6345   (when (eq major-mode 'gnus-summary-mode)
6346     (make-local-variable 'gnus-dead-summary-mode)
6347     (setq gnus-dead-summary-mode
6348           (if (null arg) (not gnus-dead-summary-mode)
6349             (> (prefix-numeric-value arg) 0)))
6350     (when gnus-dead-summary-mode
6351       (gnus-add-minor-mode
6352        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6353
6354 (defun gnus-deaden-summary ()
6355   "Make the current summary buffer into a dead summary buffer."
6356   ;; Kill any previous dead summary buffer.
6357   (when (and gnus-dead-summary
6358              (buffer-name gnus-dead-summary))
6359     (save-excursion
6360       (set-buffer gnus-dead-summary)
6361       (when gnus-dead-summary-mode
6362         (kill-buffer (current-buffer)))))
6363   ;; Make this the current dead summary.
6364   (setq gnus-dead-summary (current-buffer))
6365   (gnus-dead-summary-mode 1)
6366   (let ((name (buffer-name)))
6367     (when (string-match "Summary" name)
6368       (rename-buffer
6369        (concat (substring name 0 (match-beginning 0)) "Dead "
6370                (substring name (match-beginning 0)))
6371        t)
6372       (bury-buffer))))
6373
6374 (defun gnus-kill-or-deaden-summary (buffer)
6375   "Kill or deaden the summary BUFFER."
6376   (save-excursion
6377     (when (and (buffer-name buffer)
6378                (not gnus-single-article-buffer))
6379       (save-excursion
6380         (set-buffer buffer)
6381         (gnus-kill-buffer gnus-article-buffer)
6382         (gnus-kill-buffer gnus-original-article-buffer)))
6383     (cond
6384      ;; Kill the buffer.
6385      (gnus-kill-summary-on-exit
6386       (when (and gnus-use-trees
6387                  (gnus-buffer-exists-p buffer))
6388         (save-excursion
6389           (set-buffer buffer)
6390           (gnus-tree-close gnus-newsgroup-name)))
6391       (gnus-kill-buffer buffer))
6392      ;; Deaden the buffer.
6393      ((gnus-buffer-exists-p buffer)
6394       (save-excursion
6395         (set-buffer buffer)
6396         (gnus-deaden-summary))))))
6397
6398 (defun gnus-summary-wake-up-the-dead (&rest args)
6399   "Wake up the dead summary buffer."
6400   (interactive)
6401   (gnus-dead-summary-mode -1)
6402   (let ((name (buffer-name)))
6403     (when (string-match "Dead " name)
6404       (rename-buffer
6405        (concat (substring name 0 (match-beginning 0))
6406                (substring name (match-end 0)))
6407        t)))
6408   (gnus-message 3 "This dead summary is now alive again"))
6409
6410 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6411 (defun gnus-summary-fetch-faq (&optional faq-dir)
6412   "Fetch the FAQ for the current group.
6413 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6414 in."
6415   (interactive
6416    (list
6417     (when current-prefix-arg
6418       (completing-read
6419        "Faq dir: " (and (listp gnus-group-faq-directory)
6420                         (mapcar (lambda (file) (list file))
6421                                 gnus-group-faq-directory))))))
6422   (let (gnus-faq-buffer)
6423     (when (setq gnus-faq-buffer
6424                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6425       (gnus-configure-windows 'summary-faq))))
6426
6427 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6428 (defun gnus-summary-describe-group (&optional force)
6429   "Describe the current newsgroup."
6430   (interactive "P")
6431   (gnus-group-describe-group force gnus-newsgroup-name))
6432
6433 (defun gnus-summary-describe-briefly ()
6434   "Describe summary mode commands briefly."
6435   (interactive)
6436   (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")))
6437
6438 ;; Walking around group mode buffer from summary mode.
6439
6440 (defun gnus-summary-next-group (&optional no-article target-group backward)
6441   "Exit current newsgroup and then select next unread newsgroup.
6442 If prefix argument NO-ARTICLE is non-nil, no article is selected
6443 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6444 previous group instead."
6445   (interactive "P")
6446   ;; Stop pre-fetching.
6447   (gnus-async-halt-prefetch)
6448   (let ((current-group gnus-newsgroup-name)
6449         (current-buffer (current-buffer))
6450         entered)
6451    ;; First we semi-exit this group to update Xrefs and all variables.
6452     ;; We can't do a real exit, because the window conf must remain
6453     ;; the same in case the user is prompted for info, and we don't
6454     ;; want the window conf to change before that...
6455     (gnus-summary-exit t)
6456     (while (not entered)
6457       ;; Then we find what group we are supposed to enter.
6458       (set-buffer gnus-group-buffer)
6459       (gnus-group-jump-to-group current-group)
6460       (setq target-group
6461             (or target-group
6462                 (if (eq gnus-keep-same-level 'best)
6463                     (gnus-summary-best-group gnus-newsgroup-name)
6464                   (gnus-summary-search-group backward gnus-keep-same-level))))
6465       (if (not target-group)
6466           ;; There are no further groups, so we return to the group
6467           ;; buffer.
6468           (progn
6469             (gnus-message 5 "Returning to the group buffer")
6470             (setq entered t)
6471             (when (gnus-buffer-live-p current-buffer)
6472               (set-buffer current-buffer)
6473               (gnus-summary-exit))
6474             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6475         ;; We try to enter the target group.
6476         (gnus-group-jump-to-group target-group)
6477         (let ((unreads (gnus-group-group-unread)))
6478           (if (and (or (eq t unreads)
6479                        (and unreads (not (zerop unreads))))
6480                    (gnus-summary-read-group
6481                     target-group nil no-article
6482                     (and (buffer-name current-buffer) current-buffer)
6483                     nil backward))
6484               (setq entered t)
6485             (setq current-group target-group
6486                   target-group nil)))))))
6487
6488 (defun gnus-summary-prev-group (&optional no-article)
6489   "Exit current newsgroup and then select previous unread newsgroup.
6490 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6491   (interactive "P")
6492   (gnus-summary-next-group no-article nil t))
6493
6494 ;; Walking around summary lines.
6495
6496 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6497   "Go to the first unread subject.
6498 If UNREAD is non-nil, go to the first unread article.
6499 Returns the article selected or nil if there are no unread articles."
6500   (interactive "P")
6501   (prog1
6502       (cond
6503        ;; Empty summary.
6504        ((null gnus-newsgroup-data)
6505         (gnus-message 3 "No articles in the group")
6506         nil)
6507        ;; Pick the first article.
6508        ((not unread)
6509         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6510         (gnus-data-number (car gnus-newsgroup-data)))
6511        ;; No unread articles.
6512        ((null gnus-newsgroup-unreads)
6513         (gnus-message 3 "No more unread articles")
6514         nil)
6515        ;; Find the first unread article.
6516        (t
6517         (let ((data gnus-newsgroup-data))
6518           (while (and data
6519                       (and (not (and undownloaded
6520                                      (eq gnus-undownloaded-mark
6521                                          (gnus-data-mark (car data)))))
6522                            (if unseen
6523                                (or (not (memq
6524                                          (gnus-data-number (car data))
6525                                          gnus-newsgroup-unseen))
6526                                    (not (gnus-data-unread-p (car data))))
6527                              (not (gnus-data-unread-p (car data))))))
6528             (setq data (cdr data)))
6529           (when data
6530             (goto-char (gnus-data-pos (car data)))
6531             (gnus-data-number (car data))))))
6532     (gnus-summary-position-point)))
6533
6534 (defun gnus-summary-next-subject (n &optional unread dont-display)
6535   "Go to next N'th summary line.
6536 If N is negative, go to the previous N'th subject line.
6537 If UNREAD is non-nil, only unread articles are selected.
6538 The difference between N and the actual number of steps taken is
6539 returned."
6540   (interactive "p")
6541   (let ((backward (< n 0))
6542         (n (abs n)))
6543     (while (and (> n 0)
6544                 (if backward
6545                     (gnus-summary-find-prev unread)
6546                   (gnus-summary-find-next unread)))
6547       (unless (zerop (setq n (1- n)))
6548         (gnus-summary-show-thread)))
6549     (when (/= 0 n)
6550       (gnus-message 7 "No more%s articles"
6551                     (if unread " unread" "")))
6552     (unless dont-display
6553       (gnus-summary-recenter)
6554       (gnus-summary-position-point))
6555     n))
6556
6557 (defun gnus-summary-next-unread-subject (n)
6558   "Go to next N'th unread summary line."
6559   (interactive "p")
6560   (gnus-summary-next-subject n t))
6561
6562 (defun gnus-summary-prev-subject (n &optional unread)
6563   "Go to previous N'th summary line.
6564 If optional argument UNREAD is non-nil, only unread article is selected."
6565   (interactive "p")
6566   (gnus-summary-next-subject (- n) unread))
6567
6568 (defun gnus-summary-prev-unread-subject (n)
6569   "Go to previous N'th unread summary line."
6570   (interactive "p")
6571   (gnus-summary-next-subject (- n) t))
6572
6573 (defun gnus-summary-goto-subject (article &optional force silent)
6574   "Go the subject line of ARTICLE.
6575 If FORCE, also allow jumping to articles not currently shown."
6576   (interactive "nArticle number: ")
6577   (unless (numberp article)
6578     (error "Article %s is not a number" article))
6579   (let ((b (point))
6580         (data (gnus-data-find article)))
6581     ;; We read in the article if we have to.
6582     (and (not data)
6583          force
6584          (gnus-summary-insert-subject
6585           article
6586           (if (or (numberp force) (vectorp force)) force)
6587           t)
6588          (setq data (gnus-data-find article)))
6589     (goto-char b)
6590     (if (not data)
6591         (progn
6592           (unless silent
6593             (gnus-message 3 "Can't find article %d" article))
6594           nil)
6595       (let ((pt (gnus-data-pos data)))
6596         (goto-char pt)
6597         (gnus-summary-set-article-display-arrow pt))
6598       (gnus-summary-position-point)
6599       article)))
6600
6601 ;; Walking around summary lines with displaying articles.
6602
6603 (defun gnus-summary-expand-window (&optional arg)
6604   "Make the summary buffer take up the entire Emacs frame.
6605 Given a prefix, will force an `article' buffer configuration."
6606   (interactive "P")
6607   (if arg
6608       (gnus-configure-windows 'article 'force)
6609     (gnus-configure-windows 'summary 'force)))
6610
6611 (defun gnus-summary-display-article (article &optional all-header)
6612   "Display ARTICLE in article buffer."
6613   (when (gnus-buffer-live-p gnus-article-buffer)
6614     (with-current-buffer gnus-article-buffer
6615       (mm-enable-multibyte)))
6616   (gnus-set-global-variables)
6617   (when (gnus-buffer-live-p gnus-article-buffer)
6618     (with-current-buffer gnus-article-buffer
6619       (setq gnus-article-charset gnus-newsgroup-charset)
6620       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6621       (mm-enable-multibyte)))
6622   (if (null article)
6623       nil
6624     (prog1
6625         (if gnus-summary-display-article-function
6626             (funcall gnus-summary-display-article-function article all-header)
6627           (gnus-article-prepare article all-header))
6628       (gnus-run-hooks 'gnus-select-article-hook)
6629       (when (and gnus-current-article
6630                  (not (zerop gnus-current-article)))
6631         (gnus-summary-goto-subject gnus-current-article))
6632       (gnus-summary-recenter)
6633       (when (and gnus-use-trees gnus-show-threads)
6634         (gnus-possibly-generate-tree article)
6635         (gnus-highlight-selected-tree article))
6636       ;; Successfully display article.
6637       (gnus-article-set-window-start
6638        (cdr (assq article gnus-newsgroup-bookmarks))))))
6639
6640 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6641   "Select the current article.
6642 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6643 non-nil, the article will be re-fetched even if it already present in
6644 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6645 be displayed."
6646   ;; Make sure we are in the summary buffer to work around bbdb bug.
6647   (unless (eq major-mode 'gnus-summary-mode)
6648     (set-buffer gnus-summary-buffer))
6649   (let ((article (or article (gnus-summary-article-number)))
6650         (all-headers (not (not all-headers))) ;Must be t or nil.
6651         gnus-summary-display-article-function)
6652     (and (not pseudo)
6653          (gnus-summary-article-pseudo-p article)
6654          (error "This is a pseudo-article"))
6655     (save-excursion
6656       (set-buffer gnus-summary-buffer)
6657       (if (or (and gnus-single-article-buffer
6658                    (or (null gnus-current-article)
6659                        (null gnus-article-current)
6660                        (null (get-buffer gnus-article-buffer))
6661                        (not (eq article (cdr gnus-article-current)))
6662                        (not (equal (car gnus-article-current)
6663                                    gnus-newsgroup-name))))
6664               (and (not gnus-single-article-buffer)
6665                    (or (null gnus-current-article)
6666                        (not (eq gnus-current-article article))))
6667               force)
6668           ;; The requested article is different from the current article.
6669           (progn
6670             (gnus-summary-display-article article all-headers)
6671             (when (gnus-buffer-live-p gnus-article-buffer)
6672               (with-current-buffer gnus-article-buffer
6673                 (if (not gnus-article-decoded-p) ;; a local variable
6674                     (mm-disable-multibyte))))
6675             (gnus-article-set-window-start
6676              (cdr (assq article gnus-newsgroup-bookmarks)))
6677             article)
6678         'old))))
6679
6680 (defun gnus-summary-force-verify-and-decrypt ()
6681   (interactive)
6682   (let ((mm-verify-option 'known)
6683         (mm-decrypt-option 'known)
6684         (gnus-buttonized-mime-types (append (list "multipart/signed" 
6685                                                   "multipart/encrypted")
6686                                             gnus-buttonized-mime-types)))
6687     (gnus-summary-select-article nil 'force)))
6688
6689 (defun gnus-summary-set-current-mark (&optional current-mark)
6690   "Obsolete function."
6691   nil)
6692
6693 (defun gnus-summary-next-article (&optional unread subject backward push)
6694   "Select the next article.
6695 If UNREAD, only unread articles are selected.
6696 If SUBJECT, only articles with SUBJECT are selected.
6697 If BACKWARD, the previous article is selected instead of the next."
6698   (interactive "P")
6699   (cond
6700    ;; Is there such an article?
6701    ((and (gnus-summary-search-forward unread subject backward)
6702          (or (gnus-summary-display-article (gnus-summary-article-number))
6703              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6704     (gnus-summary-position-point))
6705    ;; If not, we try the first unread, if that is wanted.
6706    ((and subject
6707          gnus-auto-select-same
6708          (gnus-summary-first-unread-article))
6709     (gnus-summary-position-point)
6710     (gnus-message 6 "Wrapped"))
6711    ;; Try to get next/previous article not displayed in this group.
6712    ((and gnus-auto-extend-newsgroup
6713          (not unread) (not subject))
6714     (gnus-summary-goto-article
6715      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6716      nil (count-lines (point-min) (point))))
6717    ;; Go to next/previous group.
6718    (t
6719     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6720       (gnus-summary-jump-to-group gnus-newsgroup-name))
6721     (let ((cmd last-command-char)
6722           (point
6723            (save-excursion
6724              (set-buffer gnus-group-buffer)
6725              (point)))
6726           (group
6727            (if (eq gnus-keep-same-level 'best)
6728                (gnus-summary-best-group gnus-newsgroup-name)
6729              (gnus-summary-search-group backward gnus-keep-same-level))))
6730       ;; For some reason, the group window gets selected.  We change
6731       ;; it back.
6732       (select-window (get-buffer-window (current-buffer)))
6733       ;; Select next unread newsgroup automagically.
6734       (cond
6735        ((or (not gnus-auto-select-next)
6736             (not cmd))
6737         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6738        ((or (eq gnus-auto-select-next 'quietly)
6739             (and (eq gnus-auto-select-next 'slightly-quietly)
6740                  push)
6741             (and (eq gnus-auto-select-next 'almost-quietly)
6742                  (gnus-summary-last-article-p)))
6743         ;; Select quietly.
6744         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6745             (gnus-summary-exit)
6746           (gnus-message 7 "No more%s articles (%s)..."
6747                         (if unread " unread" "")
6748                         (if group (concat "selecting " group)
6749                           "exiting"))
6750           (gnus-summary-next-group nil group backward)))
6751        (t
6752         (when (gnus-key-press-event-p last-input-event)
6753           (gnus-summary-walk-group-buffer
6754            gnus-newsgroup-name cmd unread backward point))))))))
6755
6756 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6757   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6758                       (?\C-p (gnus-group-prev-unread-group 1))))
6759         (cursor-in-echo-area t)
6760         keve key group ended)
6761     (save-excursion
6762       (set-buffer gnus-group-buffer)
6763       (goto-char start)
6764       (setq group
6765             (if (eq gnus-keep-same-level 'best)
6766                 (gnus-summary-best-group gnus-newsgroup-name)
6767               (gnus-summary-search-group backward gnus-keep-same-level))))
6768     (while (not ended)
6769       (gnus-message
6770        5 "No more%s articles%s" (if unread " unread" "")
6771        (if (and group
6772                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6773            (format " (Type %s for %s [%s])"
6774                    (single-key-description cmd) group
6775                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6776          (format " (Type %s to exit %s)"
6777                  (single-key-description cmd)
6778                  gnus-newsgroup-name)))
6779       ;; Confirm auto selection.
6780       (setq key (car (setq keve (gnus-read-event-char))))
6781       (setq ended t)
6782       (cond
6783        ((assq key keystrokes)
6784         (let ((obuf (current-buffer)))
6785           (switch-to-buffer gnus-group-buffer)
6786           (when group
6787             (gnus-group-jump-to-group group))
6788           (eval (cadr (assq key keystrokes)))
6789           (setq group (gnus-group-group-name))
6790           (switch-to-buffer obuf))
6791         (setq ended nil))
6792        ((equal key cmd)
6793         (if (or (not group)
6794                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6795             (gnus-summary-exit)
6796           (gnus-summary-next-group nil group backward)))
6797        (t
6798         (push (cdr keve) unread-command-events))))))
6799
6800 (defun gnus-summary-next-unread-article ()
6801   "Select unread article after current one."
6802   (interactive)
6803   (gnus-summary-next-article
6804    (or (not (eq gnus-summary-goto-unread 'never))
6805        (gnus-summary-last-article-p (gnus-summary-article-number)))
6806    (and gnus-auto-select-same
6807         (gnus-summary-article-subject))))
6808
6809 (defun gnus-summary-prev-article (&optional unread subject)
6810   "Select the article after the current one.
6811 If UNREAD is non-nil, only unread articles are selected."
6812   (interactive "P")
6813   (gnus-summary-next-article unread subject t))
6814
6815 (defun gnus-summary-prev-unread-article ()
6816   "Select unread article before current one."
6817   (interactive)
6818   (gnus-summary-prev-article
6819    (or (not (eq gnus-summary-goto-unread 'never))
6820        (gnus-summary-first-article-p (gnus-summary-article-number)))
6821    (and gnus-auto-select-same
6822         (gnus-summary-article-subject))))
6823
6824 (defun gnus-summary-next-page (&optional lines circular)
6825   "Show next page of the selected article.
6826 If at the end of the current article, select the next article.
6827 LINES says how many lines should be scrolled up.
6828
6829 If CIRCULAR is non-nil, go to the start of the article instead of
6830 selecting the next article when reaching the end of the current
6831 article."
6832   (interactive "P")
6833   (setq gnus-summary-buffer (current-buffer))
6834   (gnus-set-global-variables)
6835   (let ((article (gnus-summary-article-number))
6836         (article-window (get-buffer-window gnus-article-buffer t))
6837         endp)
6838     ;; If the buffer is empty, we have no article.
6839     (unless article
6840       (error "No article to select"))
6841     (gnus-configure-windows 'article)
6842     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6843         (if (and (eq gnus-summary-goto-unread 'never)
6844                  (not (gnus-summary-last-article-p article)))
6845             (gnus-summary-next-article)
6846           (gnus-summary-next-unread-article))
6847       (if (or (null gnus-current-article)
6848               (null gnus-article-current)
6849               (/= article (cdr gnus-article-current))
6850               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6851           ;; Selected subject is different from current article's.
6852           (gnus-summary-display-article article)
6853         (when article-window
6854           (gnus-eval-in-buffer-window gnus-article-buffer
6855             (setq endp (gnus-article-next-page lines)))
6856           (when endp
6857             (cond (circular
6858                    (gnus-summary-beginning-of-article))
6859                   (lines
6860                    (gnus-message 3 "End of message"))
6861                   ((null lines)
6862                    (if (and (eq gnus-summary-goto-unread 'never)
6863                             (not (gnus-summary-last-article-p article)))
6864                        (gnus-summary-next-article)
6865                      (gnus-summary-next-unread-article))))))))
6866     (gnus-summary-recenter)
6867     (gnus-summary-position-point)))
6868
6869 (defun gnus-summary-prev-page (&optional lines move)
6870   "Show previous page of selected article.
6871 Argument LINES specifies lines to be scrolled down.
6872 If MOVE, move to the previous unread article if point is at
6873 the beginning of the buffer."
6874   (interactive "P")
6875   (let ((article (gnus-summary-article-number))
6876         (article-window (get-buffer-window gnus-article-buffer t))
6877         endp)
6878     (gnus-configure-windows 'article)
6879     (if (or (null gnus-current-article)
6880             (null gnus-article-current)
6881             (/= article (cdr gnus-article-current))
6882             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6883         ;; Selected subject is different from current article's.
6884         (gnus-summary-display-article article)
6885       (gnus-summary-recenter)
6886       (when article-window
6887         (gnus-eval-in-buffer-window gnus-article-buffer
6888           (setq endp (gnus-article-prev-page lines)))
6889         (when (and move endp)
6890           (cond (lines
6891                  (gnus-message 3 "Beginning of message"))
6892                 ((null lines)
6893                  (if (and (eq gnus-summary-goto-unread 'never)
6894                           (not (gnus-summary-first-article-p article)))
6895                      (gnus-summary-prev-article)
6896                    (gnus-summary-prev-unread-article))))))))
6897   (gnus-summary-position-point))
6898
6899 (defun gnus-summary-prev-page-or-article (&optional lines)
6900   "Show previous page of selected article.
6901 Argument LINES specifies lines to be scrolled down.
6902 If at the beginning of the article, go to the next article."
6903   (interactive "P")
6904   (gnus-summary-prev-page lines t))
6905
6906 (defun gnus-summary-scroll-up (lines)
6907   "Scroll up (or down) one line current article.
6908 Argument LINES specifies lines to be scrolled up (or down if negative)."
6909   (interactive "p")
6910   (gnus-configure-windows 'article)
6911   (gnus-summary-show-thread)
6912   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6913     (gnus-eval-in-buffer-window gnus-article-buffer
6914       (cond ((> lines 0)
6915              (when (gnus-article-next-page lines)
6916                (gnus-message 3 "End of message")))
6917             ((< lines 0)
6918              (gnus-article-prev-page (- lines))))))
6919   (gnus-summary-recenter)
6920   (gnus-summary-position-point))
6921
6922 (defun gnus-summary-scroll-down (lines)
6923   "Scroll down (or up) one line current article.
6924 Argument LINES specifies lines to be scrolled down (or up if negative)."
6925   (interactive "p")
6926   (gnus-summary-scroll-up (- lines)))
6927
6928 (defun gnus-summary-next-same-subject ()
6929   "Select next article which has the same subject as current one."
6930   (interactive)
6931   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6932
6933 (defun gnus-summary-prev-same-subject ()
6934   "Select previous article which has the same subject as current one."
6935   (interactive)
6936   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6937
6938 (defun gnus-summary-next-unread-same-subject ()
6939   "Select next unread article which has the same subject as current one."
6940   (interactive)
6941   (gnus-summary-next-article t (gnus-summary-article-subject)))
6942
6943 (defun gnus-summary-prev-unread-same-subject ()
6944   "Select previous unread article which has the same subject as current one."
6945   (interactive)
6946   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6947
6948 (defun gnus-summary-first-unread-article ()
6949   "Select the first unread article.
6950 Return nil if there are no unread articles."
6951   (interactive)
6952   (prog1
6953       (when (gnus-summary-first-subject t)
6954         (gnus-summary-show-thread)
6955         (gnus-summary-first-subject t)
6956         (gnus-summary-display-article (gnus-summary-article-number)))
6957     (gnus-summary-position-point)))
6958
6959 (defun gnus-summary-first-unread-subject ()
6960   "Place the point on the subject line of the first unread article.
6961 Return nil if there are no unread articles."
6962   (interactive)
6963   (prog1
6964       (when (gnus-summary-first-subject t)
6965         (gnus-summary-show-thread)
6966         (gnus-summary-first-subject t))
6967     (gnus-summary-position-point)))
6968
6969 (defun gnus-summary-first-unseen-subject ()
6970   "Place the point on the subject line of the first unseen article.
6971 Return nil if there are no unseen articles."
6972   (interactive)
6973   (prog1
6974       (when (gnus-summary-first-subject t t t)
6975         (gnus-summary-show-thread)
6976         (gnus-summary-first-subject t t t))
6977     (gnus-summary-position-point)))
6978
6979 (defun gnus-summary-first-unseen-or-unread-subject ()
6980   "Place the point on the subject line of the first unseen article.
6981 Return nil if there are no unseen articles."
6982   (interactive)
6983   (prog1
6984       (unless (when (gnus-summary-first-subject t t t)
6985                 (gnus-summary-show-thread)
6986                 (gnus-summary-first-subject t t t))
6987         (when (gnus-summary-first-subject t)
6988           (gnus-summary-show-thread)
6989           (gnus-summary-first-subject t)))
6990     (gnus-summary-position-point)))
6991
6992 (defun gnus-summary-first-article ()
6993   "Select the first article.
6994 Return nil if there are no articles."
6995   (interactive)
6996   (prog1
6997       (when (gnus-summary-first-subject)
6998         (gnus-summary-show-thread)
6999         (gnus-summary-first-subject)
7000         (gnus-summary-display-article (gnus-summary-article-number)))
7001     (gnus-summary-position-point)))
7002
7003 (defun gnus-summary-best-unread-article (&optional arg)
7004   "Select the unread article with the highest score.
7005 If given a prefix argument, select the next unread article that has a
7006 score higher than the default score."
7007   (interactive "P")
7008   (let ((article (if arg
7009                      (gnus-summary-better-unread-subject)
7010                    (gnus-summary-best-unread-subject))))
7011     (if article
7012         (gnus-summary-goto-article article)
7013       (error "No unread articles"))))
7014
7015 (defun gnus-summary-best-unread-subject ()
7016   "Select the unread subject with the highest score."
7017   (interactive)
7018   (let ((best -1000000)
7019         (data gnus-newsgroup-data)
7020         article score)
7021     (while data
7022       (and (gnus-data-unread-p (car data))
7023            (> (setq score
7024                     (gnus-summary-article-score (gnus-data-number (car data))))
7025               best)
7026            (setq best score
7027                  article (gnus-data-number (car data))))
7028       (setq data (cdr data)))
7029     (when article
7030       (gnus-summary-goto-subject article))
7031     (gnus-summary-position-point)
7032     article))
7033
7034 (defun gnus-summary-better-unread-subject ()
7035   "Select the first unread subject that has a score over the default score."
7036   (interactive)
7037   (let ((data gnus-newsgroup-data)
7038         article score)
7039     (while (and (setq article (gnus-data-number (car data)))
7040                 (or (gnus-data-read-p (car data))
7041                     (not (> (gnus-summary-article-score article)
7042                             gnus-summary-default-score))))
7043       (setq data (cdr data)))
7044     (when article
7045       (gnus-summary-goto-subject article))
7046     (gnus-summary-position-point)
7047     article))
7048
7049 (defun gnus-summary-last-subject ()
7050   "Go to the last displayed subject line in the group."
7051   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7052     (when article
7053       (gnus-summary-goto-subject article))))
7054
7055 (defun gnus-summary-goto-article (article &optional all-headers force)
7056   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7057 If ALL-HEADERS is non-nil, no header lines are hidden.
7058 If FORCE, go to the article even if it isn't displayed.  If FORCE
7059 is a number, it is the line the article is to be displayed on."
7060   (interactive
7061    (list
7062     (completing-read
7063      "Article number or Message-ID: "
7064      (mapcar (lambda (number) (list (int-to-string number)))
7065              gnus-newsgroup-limit))
7066     current-prefix-arg
7067     t))
7068   (prog1
7069       (if (and (stringp article)
7070                (string-match "@" article))
7071           (gnus-summary-refer-article article)
7072         (when (stringp article)
7073           (setq article (string-to-number article)))
7074         (if (gnus-summary-goto-subject article force)
7075             (gnus-summary-display-article article all-headers)
7076           (gnus-message 4 "Couldn't go to article %s" article) nil))
7077     (gnus-summary-position-point)))
7078
7079 (defun gnus-summary-goto-last-article ()
7080   "Go to the previously read article."
7081   (interactive)
7082   (prog1
7083       (when gnus-last-article
7084         (gnus-summary-goto-article gnus-last-article nil t))
7085     (gnus-summary-position-point)))
7086
7087 (defun gnus-summary-pop-article (number)
7088   "Pop one article off the history and go to the previous.
7089 NUMBER articles will be popped off."
7090   (interactive "p")
7091   (let (to)
7092     (setq gnus-newsgroup-history
7093           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7094     (if to
7095         (gnus-summary-goto-article (car to) nil t)
7096       (error "Article history empty")))
7097   (gnus-summary-position-point))
7098
7099 ;; Summary commands and functions for limiting the summary buffer.
7100
7101 (defun gnus-summary-limit-to-articles (n)
7102   "Limit the summary buffer to the next N articles.
7103 If not given a prefix, use the process marked articles instead."
7104   (interactive "P")
7105   (prog1
7106       (let ((articles (gnus-summary-work-articles n)))
7107         (setq gnus-newsgroup-processable nil)
7108         (gnus-summary-limit articles))
7109     (gnus-summary-position-point)))
7110
7111 (defun gnus-summary-pop-limit (&optional total)
7112   "Restore the previous limit.
7113 If given a prefix, remove all limits."
7114   (interactive "P")
7115   (when total
7116     (setq gnus-newsgroup-limits
7117           (list (mapcar (lambda (h) (mail-header-number h))
7118                         gnus-newsgroup-headers))))
7119   (unless gnus-newsgroup-limits
7120     (error "No limit to pop"))
7121   (prog1
7122       (gnus-summary-limit nil 'pop)
7123     (gnus-summary-position-point)))
7124
7125 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7126   "Limit the summary buffer to articles that have subjects that match a regexp.
7127 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7128   (interactive
7129    (list (read-string (if current-prefix-arg
7130                           "Exclude subject (regexp): "
7131                         "Limit to subject (regexp): "))
7132          nil current-prefix-arg))
7133   (unless header
7134     (setq header "subject"))
7135   (when (not (equal "" subject))
7136     (prog1
7137         (let ((articles (gnus-summary-find-matching
7138                          (or header "subject") subject 'all nil nil
7139                          not-matching)))
7140           (unless articles
7141             (error "Found no matches for \"%s\"" subject))
7142           (gnus-summary-limit articles))
7143       (gnus-summary-position-point))))
7144
7145 (defun gnus-summary-limit-to-author (from &optional not-matching)
7146   "Limit the summary buffer to articles that have authors that match a regexp.
7147 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7148   (interactive
7149    (list (read-string (if current-prefix-arg
7150                           "Exclude author (regexp): "
7151                         "Limit to author (regexp): "))
7152          current-prefix-arg))
7153   (gnus-summary-limit-to-subject from "from" not-matching))
7154
7155 (defun gnus-summary-limit-to-age (age &optional younger-p)
7156   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7157 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7158 articles that are younger than AGE days."
7159   (interactive
7160    (let ((younger current-prefix-arg)
7161          (days-got nil)
7162          days)
7163      (while (not days-got)
7164        (setq days (if younger
7165                       (read-string "Limit to articles within (in days): ")
7166                     (read-string "Limit to articles older than (in days): ")))
7167        (when (> (length days) 0)
7168          (setq days (read days)))
7169        (if (numberp days)
7170            (progn
7171              (setq days-got t)
7172              (if (< days 0)
7173                  (progn
7174                    (setq younger (not younger))
7175                    (setq days (* days -1)))))
7176          (message "Please enter a number.")
7177          (sleep-for 1)))
7178      (list days younger)))
7179   (prog1
7180       (let ((data gnus-newsgroup-data)
7181             (cutoff (days-to-time age))
7182             articles d date is-younger)
7183         (while (setq d (pop data))
7184           (when (and (vectorp (gnus-data-header d))
7185                      (setq date (mail-header-date (gnus-data-header d))))
7186             (setq is-younger (time-less-p
7187                               (time-since (condition-case ()
7188                                               (date-to-time date)
7189                                             (error '(0 0))))
7190                               cutoff))
7191             (when (if younger-p
7192                       is-younger
7193                     (not is-younger))
7194               (push (gnus-data-number d) articles))))
7195         (gnus-summary-limit (nreverse articles)))
7196     (gnus-summary-position-point)))
7197
7198 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7199   "Limit the summary buffer to articles that match an 'extra' header."
7200   (interactive
7201    (let ((header
7202           (intern
7203            (gnus-completing-read-with-default
7204             (symbol-name (car gnus-extra-headers))
7205             (if current-prefix-arg
7206                 "Exclude extra header:"
7207               "Limit extra header:")
7208             (mapcar (lambda (x)
7209                       (cons (symbol-name x) x))
7210                     gnus-extra-headers)
7211             nil
7212             t))))
7213      (list header
7214            (read-string (format "%s header %s (regexp): "
7215                                 (if current-prefix-arg "Exclude" "Limit to")
7216                                 header))
7217            current-prefix-arg)))
7218   (when (not (equal "" regexp))
7219     (prog1
7220         (let ((articles (gnus-summary-find-matching
7221                          (cons 'extra header) regexp 'all nil nil
7222                          not-matching)))
7223           (unless articles
7224             (error "Found no matches for \"%s\"" regexp))
7225           (gnus-summary-limit articles))
7226       (gnus-summary-position-point))))
7227
7228 (defun gnus-summary-limit-to-display-predicate ()
7229   "Limit the summary buffer to the predicated in the `display' group parameter."
7230   (interactive)
7231   (unless gnus-newsgroup-display
7232     (error "There is no `display' group parameter"))
7233   (let (articles)
7234     (dolist (number gnus-newsgroup-articles)
7235       (when (funcall gnus-newsgroup-display)
7236         (push number articles)))
7237     (gnus-summary-limit articles))
7238   (gnus-summary-position-point))
7239
7240 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7241 (make-obsolete
7242  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7243
7244 (defun gnus-summary-limit-to-unread (&optional all)
7245   "Limit the summary buffer to articles that are not marked as read.
7246 If ALL is non-nil, limit strictly to unread articles."
7247   (interactive "P")
7248   (if all
7249       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7250     (gnus-summary-limit-to-marks
7251      ;; Concat all the marks that say that an article is read and have
7252      ;; those removed.
7253      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7254            gnus-killed-mark gnus-kill-file-mark
7255            gnus-low-score-mark gnus-expirable-mark
7256            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7257            gnus-duplicate-mark gnus-souped-mark)
7258      'reverse)))
7259
7260 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7261 (make-obsolete 'gnus-summary-delete-marked-with
7262                'gnus-summary-limit-exclude-marks)
7263
7264 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7265   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7266 If REVERSE, limit the summary buffer to articles that are marked
7267 with MARKS.  MARKS can either be a string of marks or a list of marks.
7268 Returns how many articles were removed."
7269   (interactive "sMarks: ")
7270   (gnus-summary-limit-to-marks marks t))
7271
7272 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7273   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7274 If REVERSE (the prefix), limit the summary buffer to articles that are
7275 not marked with MARKS.  MARKS can either be a string of marks or a
7276 list of marks.
7277 Returns how many articles were removed."
7278   (interactive "sMarks: \nP")
7279   (prog1
7280       (let ((data gnus-newsgroup-data)
7281             (marks (if (listp marks) marks
7282                      (append marks nil))) ; Transform to list.
7283             articles)
7284         (while data
7285           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7286                   (memq (gnus-data-mark (car data)) marks))
7287             (push (gnus-data-number (car data)) articles))
7288           (setq data (cdr data)))
7289         (gnus-summary-limit articles))
7290     (gnus-summary-position-point)))
7291
7292 (defun gnus-summary-limit-to-score (score)
7293   "Limit to articles with score at or above SCORE."
7294   (interactive "NLimit to articles with score of at least: ")
7295   (let ((data gnus-newsgroup-data)
7296         articles)
7297     (while data
7298       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7299                 score)
7300         (push (gnus-data-number (car data)) articles))
7301       (setq data (cdr data)))
7302     (prog1
7303         (gnus-summary-limit articles)
7304       (gnus-summary-position-point))))
7305
7306 (defun gnus-summary-limit-include-thread (id)
7307   "Display all the hidden articles that is in the thread with ID in it.
7308 When called interactively, ID is the Message-ID of the current
7309 article."
7310   (interactive (list (mail-header-id (gnus-summary-article-header))))
7311   (let ((articles (gnus-articles-in-thread
7312                    (gnus-id-to-thread (gnus-root-id id)))))
7313     (prog1
7314         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7315       (gnus-summary-limit-include-matching-articles
7316        "subject"
7317        (regexp-quote (gnus-simplify-subject-re
7318                       (mail-header-subject (gnus-id-to-header id)))))
7319       (gnus-summary-position-point))))
7320
7321 (defun gnus-summary-limit-include-matching-articles (header regexp)
7322   "Display all the hidden articles that have HEADERs that match REGEXP."
7323   (interactive (list (read-string "Match on header: ")
7324                      (read-string "Regexp: ")))
7325   (let ((articles (gnus-find-matching-articles header regexp)))
7326     (prog1
7327         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7328       (gnus-summary-position-point))))
7329
7330 (defun gnus-summary-limit-include-dormant ()
7331   "Display all the hidden articles that are marked as dormant.
7332 Note that this command only works on a subset of the articles currently
7333 fetched for this group."
7334   (interactive)
7335   (unless gnus-newsgroup-dormant
7336     (error "There are no dormant articles in this group"))
7337   (prog1
7338       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7339     (gnus-summary-position-point)))
7340
7341 (defun gnus-summary-limit-exclude-dormant ()
7342   "Hide all dormant articles."
7343   (interactive)
7344   (prog1
7345       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7346     (gnus-summary-position-point)))
7347
7348 (defun gnus-summary-limit-exclude-childless-dormant ()
7349   "Hide all dormant articles that have no children."
7350   (interactive)
7351   (let ((data (gnus-data-list t))
7352         articles d children)
7353     ;; Find all articles that are either not dormant or have
7354     ;; children.
7355     (while (setq d (pop data))
7356       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7357                 (and (setq children
7358                            (gnus-article-children (gnus-data-number d)))
7359                      (let (found)
7360                        (while children
7361                          (when (memq (car children) articles)
7362                            (setq children nil
7363                                  found t))
7364                          (pop children))
7365                        found)))
7366         (push (gnus-data-number d) articles)))
7367     ;; Do the limiting.
7368     (prog1
7369         (gnus-summary-limit articles)
7370       (gnus-summary-position-point))))
7371
7372 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7373   "Mark all unread excluded articles as read.
7374 If ALL, mark even excluded ticked and dormants as read."
7375   (interactive "P")
7376   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7377   (let ((articles (gnus-sorted-ndifference
7378                    (sort
7379                     (mapcar (lambda (h) (mail-header-number h))
7380                             gnus-newsgroup-headers)
7381                     '<)
7382                    gnus-newsgroup-limit))
7383         article)
7384     (setq gnus-newsgroup-unreads
7385           (gnus-sorted-intersection gnus-newsgroup-unreads
7386                                     gnus-newsgroup-limit))
7387     (if all
7388         (setq gnus-newsgroup-dormant nil
7389               gnus-newsgroup-marked nil
7390               gnus-newsgroup-reads
7391               (nconc
7392                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7393                gnus-newsgroup-reads))
7394       (while (setq article (pop articles))
7395         (unless (or (memq article gnus-newsgroup-dormant)
7396                     (memq article gnus-newsgroup-marked))
7397           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7398
7399 (defun gnus-summary-limit (articles &optional pop)
7400   (if pop
7401       ;; We pop the previous limit off the stack and use that.
7402       (setq articles (car gnus-newsgroup-limits)
7403             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7404     ;; We use the new limit, so we push the old limit on the stack.
7405     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7406   ;; Set the limit.
7407   (setq gnus-newsgroup-limit articles)
7408   (let ((total (length gnus-newsgroup-data))
7409         (data (gnus-data-find-list (gnus-summary-article-number)))
7410         (gnus-summary-mark-below nil)   ; Inhibit this.
7411         found)
7412     ;; This will do all the work of generating the new summary buffer
7413     ;; according to the new limit.
7414     (gnus-summary-prepare)
7415     ;; Hide any threads, possibly.
7416     (gnus-summary-maybe-hide-threads)
7417     ;; Try to return to the article you were at, or one in the
7418     ;; neighborhood.
7419     (when data
7420       ;; We try to find some article after the current one.
7421       (while data
7422         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7423           (setq data nil
7424                 found t))
7425         (setq data (cdr data))))
7426     (unless found
7427       ;; If there is no data, that means that we were after the last
7428       ;; article.  The same goes when we can't find any articles
7429       ;; after the current one.
7430       (goto-char (point-max))
7431       (gnus-summary-find-prev))
7432     (gnus-set-mode-line 'summary)
7433     ;; We return how many articles were removed from the summary
7434     ;; buffer as a result of the new limit.
7435     (- total (length gnus-newsgroup-data))))
7436
7437 (defsubst gnus-invisible-cut-children (threads)
7438   (let ((num 0))
7439     (while threads
7440       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7441         (incf num))
7442       (pop threads))
7443     (< num 2)))
7444
7445 (defsubst gnus-cut-thread (thread)
7446   "Go forwards in the thread until we find an article that we want to display."
7447   (when (or (eq gnus-fetch-old-headers 'some)
7448             (eq gnus-fetch-old-headers 'invisible)
7449             (numberp gnus-fetch-old-headers)
7450             (eq gnus-build-sparse-threads 'some)
7451             (eq gnus-build-sparse-threads 'more))
7452     ;; Deal with old-fetched headers and sparse threads.
7453     (while (and
7454             thread
7455             (or
7456              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7457              (gnus-summary-article-ancient-p
7458               (mail-header-number (car thread))))
7459             (if (or (<= (length (cdr thread)) 1)
7460                     (eq gnus-fetch-old-headers 'invisible))
7461                 (setq gnus-newsgroup-limit
7462                       (delq (mail-header-number (car thread))
7463                             gnus-newsgroup-limit)
7464                       thread (cadr thread))
7465               (when (gnus-invisible-cut-children (cdr thread))
7466                 (let ((th (cdr thread)))
7467                   (while th
7468                     (if (memq (mail-header-number (caar th))
7469                               gnus-newsgroup-limit)
7470                         (setq thread (car th)
7471                               th nil)
7472                       (setq th (cdr th))))))))))
7473   thread)
7474
7475 (defun gnus-cut-threads (threads)
7476   "Cut off all uninteresting articles from the beginning of threads."
7477   (when (or (eq gnus-fetch-old-headers 'some)
7478             (eq gnus-fetch-old-headers 'invisible)
7479             (numberp gnus-fetch-old-headers)
7480             (eq gnus-build-sparse-threads 'some)
7481             (eq gnus-build-sparse-threads 'more))
7482     (let ((th threads))
7483       (while th
7484         (setcar th (gnus-cut-thread (car th)))
7485         (setq th (cdr th)))))
7486   ;; Remove nixed out threads.
7487   (delq nil threads))
7488
7489 (defun gnus-summary-initial-limit (&optional show-if-empty)
7490   "Figure out what the initial limit is supposed to be on group entry.
7491 This entails weeding out unwanted dormants, low-scored articles,
7492 fetch-old-headers verbiage, and so on."
7493   ;; Most groups have nothing to remove.
7494   (if (or gnus-inhibit-limiting
7495           (and (null gnus-newsgroup-dormant)
7496                (eq gnus-newsgroup-display 'gnus-not-ignore)
7497                (not (eq gnus-fetch-old-headers 'some))
7498                (not (numberp gnus-fetch-old-headers))
7499                (not (eq gnus-fetch-old-headers 'invisible))
7500                (null gnus-summary-expunge-below)
7501                (not (eq gnus-build-sparse-threads 'some))
7502                (not (eq gnus-build-sparse-threads 'more))
7503                (null gnus-thread-expunge-below)
7504                (not gnus-use-nocem)))
7505       ()                                ; Do nothing.
7506     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7507     (setq gnus-newsgroup-limit nil)
7508     (mapatoms
7509      (lambda (node)
7510        (unless (car (symbol-value node))
7511          ;; These threads have no parents -- they are roots.
7512          (let ((nodes (cdr (symbol-value node)))
7513                thread)
7514            (while nodes
7515              (if (and gnus-thread-expunge-below
7516                       (< (gnus-thread-total-score (car nodes))
7517                          gnus-thread-expunge-below))
7518                  (gnus-expunge-thread (pop nodes))
7519                (setq thread (pop nodes))
7520                (gnus-summary-limit-children thread))))))
7521      gnus-newsgroup-dependencies)
7522     ;; If this limitation resulted in an empty group, we might
7523     ;; pop the previous limit and use it instead.
7524     (when (and (not gnus-newsgroup-limit)
7525                show-if-empty)
7526       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7527     gnus-newsgroup-limit))
7528
7529 (defun gnus-summary-limit-children (thread)
7530   "Return 1 if this subthread is visible and 0 if it is not."
7531   ;; First we get the number of visible children to this thread.  This
7532   ;; is done by recursing down the thread using this function, so this
7533   ;; will really go down to a leaf article first, before slowly
7534   ;; working its way up towards the root.
7535   (when thread
7536     (let ((children
7537            (if (cdr thread)
7538                (apply '+ (mapcar 'gnus-summary-limit-children
7539                                  (cdr thread)))
7540              0))
7541           (number (mail-header-number (car thread)))
7542           score)
7543       (if (and
7544            (not (memq number gnus-newsgroup-marked))
7545            (or
7546             ;; If this article is dormant and has absolutely no visible
7547             ;; children, then this article isn't visible.
7548             (and (memq number gnus-newsgroup-dormant)
7549                  (zerop children))
7550             ;; If this is "fetch-old-headered" and there is no
7551             ;; visible children, then we don't want this article.
7552             (and (or (eq gnus-fetch-old-headers 'some)
7553                      (numberp gnus-fetch-old-headers))
7554                  (gnus-summary-article-ancient-p number)
7555                  (zerop children))
7556             ;; If this is "fetch-old-headered" and `invisible', then
7557             ;; we don't want this article.
7558             (and (eq gnus-fetch-old-headers 'invisible)
7559                  (gnus-summary-article-ancient-p number))
7560             ;; If this is a sparsely inserted article with no children,
7561             ;; we don't want it.
7562             (and (eq gnus-build-sparse-threads 'some)
7563                  (gnus-summary-article-sparse-p number)
7564                  (zerop children))
7565             ;; If we use expunging, and this article is really
7566             ;; low-scored, then we don't want this article.
7567             (when (and gnus-summary-expunge-below
7568                        (< (setq score
7569                                 (or (cdr (assq number gnus-newsgroup-scored))
7570                                     gnus-summary-default-score))
7571                           gnus-summary-expunge-below))
7572               ;; We increase the expunge-tally here, but that has
7573               ;; nothing to do with the limits, really.
7574               (incf gnus-newsgroup-expunged-tally)
7575               ;; We also mark as read here, if that's wanted.
7576               (when (and gnus-summary-mark-below
7577                          (< score gnus-summary-mark-below))
7578                 (setq gnus-newsgroup-unreads
7579                       (delq number gnus-newsgroup-unreads))
7580                 (if gnus-newsgroup-auto-expire
7581                     (push number gnus-newsgroup-expirable)
7582                   (push (cons number gnus-low-score-mark)
7583                         gnus-newsgroup-reads)))
7584               t)
7585             ;; Do the `display' group parameter.
7586             (and gnus-newsgroup-display
7587                  (not (funcall gnus-newsgroup-display)))
7588             ;; Check NoCeM things.
7589             (if (and gnus-use-nocem
7590                      (gnus-nocem-unwanted-article-p
7591                       (mail-header-id (car thread))))
7592                 (progn
7593                   (setq gnus-newsgroup-unreads
7594                         (delq number gnus-newsgroup-unreads))
7595                   t))))
7596           ;; Nope, invisible article.
7597           0
7598         ;; Ok, this article is to be visible, so we add it to the limit
7599         ;; and return 1.
7600         (push number gnus-newsgroup-limit)
7601         1))))
7602
7603 (defun gnus-expunge-thread (thread)
7604   "Mark all articles in THREAD as read."
7605   (let* ((number (mail-header-number (car thread))))
7606     (incf gnus-newsgroup-expunged-tally)
7607     ;; We also mark as read here, if that's wanted.
7608     (setq gnus-newsgroup-unreads
7609           (delq number gnus-newsgroup-unreads))
7610     (if gnus-newsgroup-auto-expire
7611         (push number gnus-newsgroup-expirable)
7612       (push (cons number gnus-low-score-mark)
7613             gnus-newsgroup-reads)))
7614   ;; Go recursively through all subthreads.
7615   (mapcar 'gnus-expunge-thread (cdr thread)))
7616
7617 ;; Summary article oriented commands
7618
7619 (defun gnus-summary-refer-parent-article (n)
7620   "Refer parent article N times.
7621 If N is negative, go to ancestor -N instead.
7622 The difference between N and the number of articles fetched is returned."
7623   (interactive "p")
7624   (let ((skip 1)
7625         error header ref)
7626     (when (not (natnump n))
7627       (setq skip (abs n)
7628             n 1))
7629     (while (and (> n 0)
7630                 (not error))
7631       (setq header (gnus-summary-article-header))
7632       (if (and (eq (mail-header-number header)
7633                    (cdr gnus-article-current))
7634                (equal gnus-newsgroup-name
7635                       (car gnus-article-current)))
7636           ;; If we try to find the parent of the currently
7637           ;; displayed article, then we take a look at the actual
7638           ;; References header, since this is slightly more
7639           ;; reliable than the References field we got from the
7640           ;; server.
7641           (save-excursion
7642             (set-buffer gnus-original-article-buffer)
7643             (nnheader-narrow-to-headers)
7644             (unless (setq ref (message-fetch-field "references"))
7645               (setq ref (message-fetch-field "in-reply-to")))
7646             (widen))
7647         (setq ref
7648               ;; It's not the current article, so we take a bet on
7649               ;; the value we got from the server.
7650               (mail-header-references header)))
7651       (if (and ref
7652                (not (equal ref "")))
7653           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7654             (gnus-message 1 "Couldn't find parent"))
7655         (gnus-message 1 "No references in article %d"
7656                       (gnus-summary-article-number))
7657         (setq error t))
7658       (decf n))
7659     (gnus-summary-position-point)
7660     n))
7661
7662 (defun gnus-summary-refer-references ()
7663   "Fetch all articles mentioned in the References header.
7664 Return the number of articles fetched."
7665   (interactive)
7666   (let ((ref (mail-header-references (gnus-summary-article-header)))
7667         (current (gnus-summary-article-number))
7668         (n 0))
7669     (if (or (not ref)
7670             (equal ref ""))
7671         (error "No References in the current article")
7672       ;; For each Message-ID in the References header...
7673       (while (string-match "<[^>]*>" ref)
7674         (incf n)
7675         ;; ... fetch that article.
7676         (gnus-summary-refer-article
7677          (prog1 (match-string 0 ref)
7678            (setq ref (substring ref (match-end 0))))))
7679       (gnus-summary-goto-subject current)
7680       (gnus-summary-position-point)
7681       n)))
7682
7683 (defun gnus-summary-refer-thread (&optional limit)
7684   "Fetch all articles in the current thread.
7685 If LIMIT (the numerical prefix), fetch that many old headers instead
7686 of what's specified by the `gnus-refer-thread-limit' variable."
7687   (interactive "P")
7688   (let ((id (mail-header-id (gnus-summary-article-header)))
7689         (limit (if limit (prefix-numeric-value limit)
7690                  gnus-refer-thread-limit)))
7691     ;; We want to fetch LIMIT *old* headers, but we also have to
7692     ;; re-fetch all the headers in the current buffer, because many of
7693     ;; them may be undisplayed.  So we adjust LIMIT.
7694     (when (numberp limit)
7695       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7696     (unless (eq gnus-fetch-old-headers 'invisible)
7697       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7698       ;; Retrieve the headers and read them in.
7699       (if (eq (gnus-retrieve-headers
7700                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7701               'nov)
7702           (gnus-build-all-threads)
7703         (error "Can't fetch thread from backends that don't support NOV"))
7704       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7705     (gnus-summary-limit-include-thread id)))
7706
7707 (defun gnus-summary-refer-article (message-id)
7708   "Fetch an article specified by MESSAGE-ID."
7709   (interactive "sMessage-ID: ")
7710   (when (and (stringp message-id)
7711              (not (zerop (length message-id))))
7712     ;; Construct the correct Message-ID if necessary.
7713     ;; Suggested by tale@pawl.rpi.edu.
7714     (unless (string-match "^<" message-id)
7715       (setq message-id (concat "<" message-id)))
7716     (unless (string-match ">$" message-id)
7717       (setq message-id (concat message-id ">")))
7718     (let* ((header (gnus-id-to-header message-id))
7719            (sparse (and header
7720                         (gnus-summary-article-sparse-p
7721                          (mail-header-number header))
7722                         (memq (mail-header-number header)
7723                               gnus-newsgroup-limit)))
7724            number)
7725       (cond
7726        ;; If the article is present in the buffer we just go to it.
7727        ((and header
7728              (or (not (gnus-summary-article-sparse-p
7729                        (mail-header-number header)))
7730                  sparse))
7731         (prog1
7732             (gnus-summary-goto-article
7733              (mail-header-number header) nil t)
7734           (when sparse
7735             (gnus-summary-update-article (mail-header-number header)))))
7736        (t
7737         ;; We fetch the article.
7738         (catch 'found
7739           (dolist (gnus-override-method (gnus-refer-article-methods))
7740             (gnus-check-server gnus-override-method)
7741             ;; Fetch the header, and display the article.
7742             (when (setq number (gnus-summary-insert-subject message-id))
7743               (gnus-summary-select-article nil nil nil number)
7744               (throw 'found t)))
7745           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7746
7747 (defun gnus-refer-article-methods ()
7748   "Return a list of referrable methods."
7749   (cond
7750    ;; No method, so we default to current and native.
7751    ((null gnus-refer-article-method)
7752     (list gnus-current-select-method gnus-select-method))
7753    ;; Current.
7754    ((eq 'current gnus-refer-article-method)
7755     (list gnus-current-select-method))
7756    ;; List of select methods.
7757    ((not (and (symbolp (car gnus-refer-article-method))
7758               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7759     (let (out)
7760       (dolist (method gnus-refer-article-method)
7761         (push (if (eq 'current method)
7762                   gnus-current-select-method
7763                 method)
7764               out))
7765       (nreverse out)))
7766    ;; One single select method.
7767    (t
7768     (list gnus-refer-article-method))))
7769
7770 (defun gnus-summary-edit-parameters ()
7771   "Edit the group parameters of the current group."
7772   (interactive)
7773   (gnus-group-edit-group gnus-newsgroup-name 'params))
7774
7775 (defun gnus-summary-customize-parameters ()
7776   "Customize the group parameters of the current group."
7777   (interactive)
7778   (gnus-group-customize gnus-newsgroup-name))
7779
7780 (defun gnus-summary-enter-digest-group (&optional force)
7781   "Enter an nndoc group based on the current article.
7782 If FORCE, force a digest interpretation.  If not, try
7783 to guess what the document format is."
7784   (interactive "P")
7785   (let ((conf gnus-current-window-configuration))
7786     (save-excursion
7787       (gnus-summary-select-article))
7788     (setq gnus-current-window-configuration conf)
7789     (let* ((name (format "%s-%d"
7790                          (gnus-group-prefixed-name
7791                           gnus-newsgroup-name (list 'nndoc ""))
7792                          (save-excursion
7793                            (set-buffer gnus-summary-buffer)
7794                            gnus-current-article)))
7795            (ogroup gnus-newsgroup-name)
7796            (params (append (gnus-info-params (gnus-get-info ogroup))
7797                            (list (cons 'to-group ogroup))
7798                            (list (cons 'save-article-group ogroup))))
7799            (case-fold-search t)
7800            (buf (current-buffer))
7801            dig to-address)
7802       (save-excursion
7803         (set-buffer gnus-original-article-buffer)
7804         ;; Have the digest group inherit the main mail address of
7805         ;; the parent article.
7806         (when (setq to-address (or (message-fetch-field "reply-to")
7807                                    (message-fetch-field "from")))
7808           (setq params (append
7809                         (list (cons 'to-address
7810                                     (funcall gnus-decode-encoded-word-function
7811                                              to-address))))))
7812         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7813         (insert-buffer-substring gnus-original-article-buffer)
7814         ;; Remove lines that may lead nndoc to misinterpret the
7815         ;; document type.
7816         (narrow-to-region
7817          (goto-char (point-min))
7818          (or (search-forward "\n\n" nil t) (point)))
7819         (goto-char (point-min))
7820         (delete-matching-lines "^Path:\\|^From ")
7821         (widen))
7822       (unwind-protect
7823           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7824                     (gnus-newsgroup-ephemeral-ignored-charsets
7825                      gnus-newsgroup-ignored-charsets))
7826                 (gnus-group-read-ephemeral-group
7827                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7828                               (nndoc-article-type
7829                                ,(if force 'mbox 'guess)))
7830                  t nil nil nil
7831                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7832                                                         "ADAPT")))))
7833               ;; Make all postings to this group go to the parent group.
7834               (nconc (gnus-info-params (gnus-get-info name))
7835                      params)
7836             ;; Couldn't select this doc group.
7837             (switch-to-buffer buf)
7838             (gnus-set-global-variables)
7839             (gnus-configure-windows 'summary)
7840             (gnus-message 3 "Article couldn't be entered?"))
7841         (kill-buffer dig)))))
7842
7843 (defun gnus-summary-read-document (n)
7844   "Open a new group based on the current article(s).
7845 This will allow you to read digests and other similar
7846 documents as newsgroups.
7847 Obeys the standard process/prefix convention."
7848   (interactive "P")
7849   (let* ((articles (gnus-summary-work-articles n))
7850          (ogroup gnus-newsgroup-name)
7851          (params (append (gnus-info-params (gnus-get-info ogroup))
7852                          (list (cons 'to-group ogroup))))
7853          article group egroup groups vgroup)
7854     (while (setq article (pop articles))
7855       (setq group (format "%s-%d" gnus-newsgroup-name article))
7856       (gnus-summary-remove-process-mark article)
7857       (when (gnus-summary-display-article article)
7858         (save-excursion
7859           (with-temp-buffer
7860             (insert-buffer-substring gnus-original-article-buffer)
7861             ;; Remove some headers that may lead nndoc to make
7862             ;; the wrong guess.
7863             (message-narrow-to-head)
7864             (goto-char (point-min))
7865             (delete-matching-lines "^\\(Path\\):\\|^From ")
7866             (widen)
7867             (if (setq egroup
7868                       (gnus-group-read-ephemeral-group
7869                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7870                                      (nndoc-article-type guess))
7871                        t nil t))
7872                 (progn
7873             ;; Make all postings to this group go to the parent group.
7874                   (nconc (gnus-info-params (gnus-get-info egroup))
7875                          params)
7876                   (push egroup groups))
7877               ;; Couldn't select this doc group.
7878               (gnus-error 3 "Article couldn't be entered"))))))
7879     ;; Now we have selected all the documents.
7880     (cond
7881      ((not groups)
7882       (error "None of the articles could be interpreted as documents"))
7883      ((gnus-group-read-ephemeral-group
7884        (setq vgroup (format
7885                      "nnvirtual:%s-%s" gnus-newsgroup-name
7886                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7887        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7888        t
7889        (cons (current-buffer) 'summary)))
7890      (t
7891       (error "Couldn't select virtual nndoc group")))))
7892
7893 (defun gnus-summary-isearch-article (&optional regexp-p)
7894   "Do incremental search forward on the current article.
7895 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7896   (interactive "P")
7897   (gnus-summary-select-article)
7898   (gnus-configure-windows 'article)
7899   (gnus-eval-in-buffer-window gnus-article-buffer
7900     (save-restriction
7901       (widen)
7902       (isearch-forward regexp-p))))
7903
7904 (defun gnus-summary-search-article-forward (regexp &optional backward)
7905   "Search for an article containing REGEXP forward.
7906 If BACKWARD, search backward instead."
7907   (interactive
7908    (list (read-string
7909           (format "Search article %s (regexp%s): "
7910                   (if current-prefix-arg "backward" "forward")
7911                   (if gnus-last-search-regexp
7912                       (concat ", default " gnus-last-search-regexp)
7913                     "")))
7914          current-prefix-arg))
7915   (if (string-equal regexp "")
7916       (setq regexp (or gnus-last-search-regexp ""))
7917     (setq gnus-last-search-regexp regexp)
7918     (setq gnus-article-before-search gnus-current-article))
7919   ;; Intentionally set gnus-last-article.
7920   (setq gnus-last-article gnus-article-before-search)
7921   (let ((gnus-last-article gnus-last-article))
7922     (if (gnus-summary-search-article regexp backward)
7923         (gnus-summary-show-thread)
7924       (error "Search failed: \"%s\"" regexp))))
7925
7926 (defun gnus-summary-search-article-backward (regexp)
7927   "Search for an article containing REGEXP backward."
7928   (interactive
7929    (list (read-string
7930           (format "Search article backward (regexp%s): "
7931                   (if gnus-last-search-regexp
7932                       (concat ", default " gnus-last-search-regexp)
7933                     "")))))
7934   (gnus-summary-search-article-forward regexp 'backward))
7935
7936 (defun gnus-summary-search-article (regexp &optional backward)
7937   "Search for an article containing REGEXP.
7938 Optional argument BACKWARD means do search for backward.
7939 `gnus-select-article-hook' is not called during the search."
7940   ;; We have to require this here to make sure that the following
7941   ;; dynamic binding isn't shadowed by autoloading.
7942   (require 'gnus-async)
7943   (require 'gnus-art)
7944   (let ((gnus-select-article-hook nil)  ;Disable hook.
7945         (gnus-article-prepare-hook nil)
7946         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7947         (gnus-use-article-prefetch nil)
7948         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7949         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7950         (sum (current-buffer))
7951         (gnus-display-mime-function nil)
7952         (found nil)
7953         point)
7954     (gnus-save-hidden-threads
7955       (gnus-summary-select-article)
7956       (set-buffer gnus-article-buffer)
7957       (goto-char (window-point (get-buffer-window (current-buffer))))
7958       (when backward
7959         (forward-line -1))
7960       (while (not found)
7961         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7962         (if (if backward
7963                 (re-search-backward regexp nil t)
7964               (re-search-forward regexp nil t))
7965             ;; We found the regexp.
7966             (progn
7967               (setq found 'found)
7968               (beginning-of-line)
7969               (set-window-start
7970                (get-buffer-window (current-buffer))
7971                (point))
7972               (forward-line 1)
7973               (set-window-point
7974                (get-buffer-window (current-buffer))
7975                (point))
7976               (set-buffer sum)
7977               (setq point (point)))
7978           ;; We didn't find it, so we go to the next article.
7979           (set-buffer sum)
7980           (setq found 'not)
7981           (while (eq found 'not)
7982             (if (not (if backward (gnus-summary-find-prev)
7983                        (gnus-summary-find-next)))
7984                 ;; No more articles.
7985                 (setq found t)
7986               ;; Select the next article and adjust point.
7987               (unless (gnus-summary-article-sparse-p
7988                        (gnus-summary-article-number))
7989                 (setq found nil)
7990                 (gnus-summary-select-article)
7991                 (set-buffer gnus-article-buffer)
7992                 (widen)
7993                 (goto-char (if backward (point-max) (point-min))))))))
7994       (gnus-message 7 ""))
7995     ;; Return whether we found the regexp.
7996     (when (eq found 'found)
7997       (goto-char point)
7998       (gnus-summary-show-thread)
7999       (gnus-summary-goto-subject gnus-current-article)
8000       (gnus-summary-position-point)
8001       t)))
8002
8003 (defun gnus-find-matching-articles (header regexp)
8004   "Return a list of all articles that match REGEXP on HEADER.
8005 This search includes all articles in the current group that Gnus has
8006 fetched headers for, whether they are displayed or not."
8007   (let ((articles nil)
8008         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8009         (case-fold-search t))
8010     (dolist (header gnus-newsgroup-headers)
8011       (when (string-match regexp (funcall func header))
8012         (push (mail-header-number header) articles)))
8013     (nreverse articles)))
8014
8015 (defun gnus-summary-find-matching (header regexp &optional backward unread
8016                                           not-case-fold not-matching)
8017   "Return a list of all articles that match REGEXP on HEADER.
8018 The search stars on the current article and goes forwards unless
8019 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8020 If UNREAD is non-nil, only unread articles will
8021 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8022 in the comparisons. If NOT-MATCHING, return a list of all articles that
8023 not match REGEXP on HEADER."
8024   (let ((case-fold-search (not not-case-fold))
8025         articles d func)
8026     (if (consp header)
8027         (if (eq (car header) 'extra)
8028             (setq func
8029                   `(lambda (h)
8030                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8031                          "")))
8032           (error "%s is an invalid header" header))
8033       (unless (fboundp (intern (concat "mail-header-" header)))
8034         (error "%s is not a valid header" header))
8035       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8036     (dolist (d (if (eq backward 'all)
8037                    gnus-newsgroup-data
8038                  (gnus-data-find-list
8039                   (gnus-summary-article-number)
8040                   (gnus-data-list backward))))
8041       (when (and (or (not unread)       ; We want all articles...
8042                      (gnus-data-unread-p d)) ; Or just unreads.
8043                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8044                  (if not-matching
8045                      (not (string-match
8046                            regexp
8047                            (funcall func (gnus-data-header d))))
8048                    (string-match regexp
8049                                  (funcall func (gnus-data-header d)))))
8050         (push (gnus-data-number d) articles))) ; Success!
8051     (nreverse articles)))
8052
8053 (defun gnus-summary-execute-command (header regexp command &optional backward)
8054   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8055 If HEADER is an empty string (or nil), the match is done on the entire
8056 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8057   (interactive
8058    (list (let ((completion-ignore-case t))
8059            (completing-read
8060             "Header name: "
8061             (mapcar (lambda (header) (list (format "%s" header)))
8062                     (append
8063                      '("Number" "Subject" "From" "Lines" "Date"
8064                        "Message-ID" "Xref" "References" "Body")
8065                      gnus-extra-headers))
8066             nil 'require-match))
8067          (read-string "Regexp: ")
8068          (read-key-sequence "Command: ")
8069          current-prefix-arg))
8070   (when (equal header "Body")
8071     (setq header ""))
8072   ;; Hidden thread subtrees must be searched as well.
8073   (gnus-summary-show-all-threads)
8074   ;; We don't want to change current point nor window configuration.
8075   (save-excursion
8076     (save-window-excursion
8077       (gnus-message 6 "Executing %s..." (key-description command))
8078 ;; We'd like to execute COMMAND interactively so as to give arguments.
8079       (gnus-execute header regexp
8080                     `(call-interactively ',(key-binding command))
8081                     backward)
8082       (gnus-message 6 "Executing %s...done" (key-description command)))))
8083
8084 (defun gnus-summary-beginning-of-article ()
8085   "Scroll the article back to the beginning."
8086   (interactive)
8087   (gnus-summary-select-article)
8088   (gnus-configure-windows 'article)
8089   (gnus-eval-in-buffer-window gnus-article-buffer
8090     (widen)
8091     (goto-char (point-min))
8092     (when gnus-page-broken
8093       (gnus-narrow-to-page))))
8094
8095 (defun gnus-summary-end-of-article ()
8096   "Scroll to the end of the article."
8097   (interactive)
8098   (gnus-summary-select-article)
8099   (gnus-configure-windows 'article)
8100   (gnus-eval-in-buffer-window gnus-article-buffer
8101     (widen)
8102     (goto-char (point-max))
8103     (recenter -3)
8104     (when gnus-page-broken
8105       (gnus-narrow-to-page))))
8106
8107 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8108   "Truncate to LEN and quote all \"(\"'s in STRING."
8109   (gnus-replace-in-string (if (and len (> (length string) len))
8110                               (substring string 0 len)
8111                             string)
8112                           "[()]" "\\\\\\&"))
8113
8114 (defun gnus-summary-print-article (&optional filename n)
8115   "Generate and print a PostScript image of the N next (mail) articles.
8116
8117 If N is negative, print the N previous articles.  If N is nil and articles
8118 have been marked with the process mark, print these instead.
8119
8120 If the optional first argument FILENAME is nil, send the image to the
8121 printer.  If FILENAME is a string, save the PostScript image in a file with
8122 that name.  If FILENAME is a number, prompt the user for the name of the file
8123 to save in."
8124   (interactive (list (ps-print-preprint current-prefix-arg)))
8125   (dolist (article (gnus-summary-work-articles n))
8126     (gnus-summary-select-article nil nil 'pseudo article)
8127     (gnus-eval-in-buffer-window gnus-article-buffer
8128       (gnus-print-buffer))
8129     (gnus-summary-remove-process-mark article))
8130   (ps-despool filename))
8131
8132 (defun gnus-print-buffer ()
8133   (let ((buffer (generate-new-buffer " *print*")))
8134     (unwind-protect
8135         (progn
8136           (copy-to-buffer buffer (point-min) (point-max))
8137           (set-buffer buffer)
8138           (gnus-article-delete-invisible-text)
8139           (gnus-remove-text-with-property 'gnus-decoration)
8140           (when (gnus-visual-p 'article-highlight 'highlight)
8141             ;; Copy-to-buffer doesn't copy overlay.  So redo
8142             ;; highlight.
8143             (let ((gnus-article-buffer buffer))
8144               (gnus-article-highlight-citation t)
8145               (gnus-article-highlight-signature)))
8146           (let ((ps-left-header
8147                  (list
8148                   (concat "("
8149                           (gnus-summary-print-truncate-and-quote
8150                            (mail-header-subject gnus-current-headers)
8151                            66) ")")
8152                   (concat "("
8153                           (gnus-summary-print-truncate-and-quote
8154                            (mail-header-from gnus-current-headers)
8155                            45) ")")))
8156                 (ps-right-header
8157                  (list
8158                   "/pagenumberstring load"
8159                   (concat "("
8160                           (mail-header-date gnus-current-headers) ")"))))
8161             (gnus-run-hooks 'gnus-ps-print-hook)
8162             (save-excursion
8163               (if window-system
8164                   (ps-spool-buffer-with-faces)
8165                 (ps-spool-buffer)))))
8166       (kill-buffer buffer))))
8167
8168 (defun gnus-summary-show-article (&optional arg)
8169   "Force redisplaying of the current article.
8170 If ARG (the prefix) is a number, show the article with the charset
8171 defined in `gnus-summary-show-article-charset-alist', or the charset
8172 input.
8173 If ARG (the prefix) is non-nil and not a number, show the raw article
8174 without any article massaging functions being run.  Normally, the key strokes
8175 are `C-u g'."
8176   (interactive "P")
8177   (cond
8178    ((numberp arg)
8179     (gnus-summary-show-article t)
8180     (let ((gnus-newsgroup-charset
8181            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8182                (mm-read-coding-system
8183                 "View as charset: " ;; actually it is coding system.
8184                 (save-excursion
8185                   (set-buffer gnus-article-buffer)
8186                   (mm-detect-coding-region (point) (point-max))))))
8187           (gnus-newsgroup-ignored-charsets 'gnus-all))
8188       (gnus-summary-select-article nil 'force)
8189       (let ((deps gnus-newsgroup-dependencies)
8190             head header lines)
8191         (save-excursion
8192           (set-buffer gnus-original-article-buffer)
8193           (save-restriction
8194             (message-narrow-to-head)
8195             (setq head (buffer-string))
8196             (goto-char (point-min))
8197             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8198               (goto-char (point-max))
8199               (widen)
8200               (setq lines (1- (count-lines (point) (point-max))))))
8201           (with-temp-buffer
8202             (insert (format "211 %d Article retrieved.\n"
8203                             (cdr gnus-article-current)))
8204             (insert head)
8205             (if lines (insert (format "Lines: %d\n" lines)))
8206             (insert ".\n")
8207             (let ((nntp-server-buffer (current-buffer)))
8208               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8209         (gnus-data-set-header
8210          (gnus-data-find (cdr gnus-article-current))
8211          header)
8212         (gnus-summary-update-article-line
8213          (cdr gnus-article-current) header)
8214         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8215           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8216    ((not arg)
8217     ;; Select the article the normal way.
8218     (gnus-summary-select-article nil 'force))
8219    (t
8220     ;; We have to require this here to make sure that the following
8221     ;; dynamic binding isn't shadowed by autoloading.
8222     (require 'gnus-async)
8223     (require 'gnus-art)
8224     ;; Bind the article treatment functions to nil.
8225     (let ((gnus-have-all-headers t)
8226           gnus-article-prepare-hook
8227           gnus-article-decode-hook
8228           gnus-display-mime-function
8229           gnus-break-pages)
8230       ;; Destroy any MIME parts.
8231       (when (gnus-buffer-live-p gnus-article-buffer)
8232         (save-excursion
8233           (set-buffer gnus-article-buffer)
8234           (mm-destroy-parts gnus-article-mime-handles)
8235           ;; Set it to nil for safety reason.
8236           (setq gnus-article-mime-handle-alist nil)
8237           (setq gnus-article-mime-handles nil)))
8238       (gnus-summary-select-article nil 'force))))
8239   (gnus-summary-goto-subject gnus-current-article)
8240   (gnus-summary-position-point))
8241
8242 (defun gnus-summary-show-raw-article ()
8243   "Show the raw article without any article massaging functions being run."
8244   (interactive)
8245   (gnus-summary-show-article t))
8246
8247 (defun gnus-summary-verbose-headers (&optional arg)
8248   "Toggle permanent full header display.
8249 If ARG is a positive number, turn header display on.
8250 If ARG is a negative number, turn header display off."
8251   (interactive "P")
8252   (setq gnus-show-all-headers
8253         (cond ((or (not (numberp arg))
8254                    (zerop arg))
8255                (not gnus-show-all-headers))
8256               ((natnump arg)
8257                t)))
8258   (gnus-summary-show-article))
8259
8260 (defun gnus-summary-toggle-header (&optional arg)
8261   "Show the headers if they are hidden, or hide them if they are shown.
8262 If ARG is a positive number, show the entire header.
8263 If ARG is a negative number, hide the unwanted header lines."
8264   (interactive "P")
8265   (save-excursion
8266     (set-buffer gnus-article-buffer)
8267     (save-restriction
8268       (let* ((buffer-read-only nil)
8269              (inhibit-point-motion-hooks t)
8270              hidden e)
8271         (save-restriction
8272           (article-narrow-to-head)
8273           (setq e (point-max)
8274                 hidden (if (numberp arg)
8275                            (>= arg 0)
8276                          (gnus-article-hidden-text-p 'headers))))
8277         (delete-region (point-min) e)
8278         (goto-char (point-min))
8279         (save-excursion
8280           (set-buffer gnus-original-article-buffer)
8281           (goto-char (point-min))
8282           (setq e (search-forward "\n\n" nil t)
8283                 e (if e (1- e) (point-max))))
8284         (insert-buffer-substring gnus-original-article-buffer 1 e)
8285         (save-restriction
8286           (narrow-to-region (point-min) (point))
8287           (article-decode-encoded-words)
8288           (if  hidden
8289               (let ((gnus-treat-hide-headers nil)
8290                     (gnus-treat-hide-boring-headers nil))
8291                 (gnus-delete-wash-type 'headers)
8292                 (gnus-treat-article 'head))
8293             (gnus-treat-article 'head)))
8294         (gnus-set-mode-line 'article)))))
8295
8296 (defun gnus-summary-show-all-headers ()
8297   "Make all header lines visible."
8298   (interactive)
8299   (gnus-summary-toggle-header 1))
8300
8301 (defun gnus-summary-caesar-message (&optional arg)
8302   "Caesar rotate the current article by 13.
8303 The numerical prefix specifies how many places to rotate each letter
8304 forward."
8305   (interactive "P")
8306   (gnus-summary-select-article)
8307   (let ((mail-header-separator ""))
8308     (gnus-eval-in-buffer-window gnus-article-buffer
8309       (save-restriction
8310         (widen)
8311         (let ((start (window-start))
8312               buffer-read-only)
8313           (message-caesar-buffer-body arg)
8314           (set-window-start (get-buffer-window (current-buffer)) start))))))
8315
8316 (defun gnus-summary-stop-page-breaking ()
8317   "Stop page breaking in the current article."
8318   (interactive)
8319   (gnus-summary-select-article)
8320   (gnus-eval-in-buffer-window gnus-article-buffer
8321     (widen)
8322     (when (gnus-visual-p 'page-marker)
8323       (let ((buffer-read-only nil))
8324         (gnus-remove-text-with-property 'gnus-prev)
8325         (gnus-remove-text-with-property 'gnus-next))
8326       (setq gnus-page-broken nil))))
8327
8328 (defun gnus-summary-move-article (&optional n to-newsgroup
8329                                             select-method action)
8330   "Move the current article to a different newsgroup.
8331 If N is a positive number, move the N next articles.
8332 If N is a negative number, move the N previous articles.
8333 If N is nil and any articles have been marked with the process mark,
8334 move those articles instead.
8335 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8336 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8337 re-spool using this method.
8338
8339 When called interactively with TO-NEWSGROUP being nil, the value of
8340 the variable `gnus-move-split-methods' is used for finding a default
8341 for the target newsgroup.
8342
8343 For this function to work, both the current newsgroup and the
8344 newsgroup that you want to move to have to support the `request-move'
8345 and `request-accept' functions.
8346
8347 ACTION can be either `move' (the default), `crosspost' or `copy'."
8348   (interactive "P")
8349   (unless action
8350     (setq action 'move))
8351   ;; Check whether the source group supports the required functions.
8352   (cond ((and (eq action 'move)
8353               (not (gnus-check-backend-function
8354                     'request-move-article gnus-newsgroup-name)))
8355          (error "The current group does not support article moving"))
8356         ((and (eq action 'crosspost)
8357               (not (gnus-check-backend-function
8358                     'request-replace-article gnus-newsgroup-name)))
8359          (error "The current group does not support article editing")))
8360   (let ((articles (gnus-summary-work-articles n))
8361         (prefix (if (gnus-check-backend-function
8362                      'request-move-article gnus-newsgroup-name)
8363                     (gnus-group-real-prefix gnus-newsgroup-name)
8364                   ""))
8365         (names '((move "Move" "Moving")
8366                  (copy "Copy" "Copying")
8367                  (crosspost "Crosspost" "Crossposting")))
8368         (copy-buf (save-excursion
8369                     (nnheader-set-temp-buffer " *copy article*")))
8370         art-group to-method new-xref article to-groups)
8371     (unless (assq action names)
8372       (error "Unknown action %s" action))
8373     ;; Read the newsgroup name.
8374     (when (and (not to-newsgroup)
8375                (not select-method))
8376       (if (and gnus-move-split-methods
8377                (not
8378                 (and (memq gnus-current-article articles)
8379                      (gnus-buffer-live-p gnus-original-article-buffer))))
8380           ;; When `gnus-move-split-methods' is non-nil, we have to
8381           ;; select an article to give `gnus-read-move-group-name' an
8382           ;; opportunity to suggest an appropriate default.  However,
8383           ;; we needn't render or mark the article.
8384           (let ((gnus-display-mime-function nil)
8385                 (gnus-article-prepare-hook nil)
8386                 (gnus-mark-article-hook nil))
8387             (gnus-summary-select-article nil nil nil (car articles))))
8388       (setq to-newsgroup
8389             (gnus-read-move-group-name
8390              (cadr (assq action names))
8391              (symbol-value (intern (format "gnus-current-%s-group" action)))
8392              articles prefix))
8393       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8394     (setq to-method (or select-method
8395                         (gnus-server-to-method
8396                          (gnus-group-method to-newsgroup))))
8397     ;; Check the method we are to move this article to...
8398     (unless (gnus-check-backend-function
8399              'request-accept-article (car to-method))
8400       (error "%s does not support article copying" (car to-method)))
8401     (unless (gnus-check-server to-method)
8402       (error "Can't open server %s" (car to-method)))
8403     (gnus-message 6 "%s to %s: %s..."
8404                   (caddr (assq action names))
8405                   (or (car select-method) to-newsgroup) articles)
8406     (while articles
8407       (setq article (pop articles))
8408       (setq
8409        art-group
8410        (cond
8411         ;; Move the article.
8412         ((eq action 'move)
8413          ;; Remove this article from future suppression.
8414          (gnus-dup-unsuppress-article article)
8415          (gnus-request-move-article
8416           article                       ; Article to move
8417           gnus-newsgroup-name           ; From newsgroup
8418           (nth 1 (gnus-find-method-for-group
8419                   gnus-newsgroup-name)) ; Server
8420           (list 'gnus-request-accept-article
8421                 to-newsgroup (list 'quote select-method)
8422                 (not articles) t)       ; Accept form
8423           (not articles)))              ; Only save nov last time
8424         ;; Copy the article.
8425         ((eq action 'copy)
8426          (save-excursion
8427            (set-buffer copy-buf)
8428            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8429              (gnus-request-accept-article
8430               to-newsgroup select-method (not articles) t))))
8431         ;; Crosspost the article.
8432         ((eq action 'crosspost)
8433          (let ((xref (message-tokenize-header
8434                       (mail-header-xref (gnus-summary-article-header article))
8435                       " ")))
8436            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8437                                   ":" (number-to-string article)))
8438            (unless xref
8439              (setq xref (list (system-name))))
8440            (setq new-xref
8441                  (concat
8442                   (mapconcat 'identity
8443                              (delete "Xref:" (delete new-xref xref))
8444                              " ")
8445                   " " new-xref))
8446            (save-excursion
8447              (set-buffer copy-buf)
8448              ;; First put the article in the destination group.
8449              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8450              (when (consp (setq art-group
8451                                 (gnus-request-accept-article
8452                                  to-newsgroup select-method (not articles))))
8453                (setq new-xref (concat new-xref " " (car art-group)
8454                                       ":"
8455                                       (number-to-string (cdr art-group))))
8456                ;; Now we have the new Xrefs header, so we insert
8457                ;; it and replace the new article.
8458                (nnheader-replace-header "Xref" new-xref)
8459                (gnus-request-replace-article
8460                 (cdr art-group) to-newsgroup (current-buffer))
8461                art-group))))))
8462       (cond
8463        ((not art-group)
8464         (gnus-message 1 "Couldn't %s article %s: %s"
8465                       (cadr (assq action names)) article
8466                       (nnheader-get-report (car to-method))))
8467        ((eq art-group 'junk)
8468         (when (eq action 'move)
8469           (gnus-summary-mark-article article gnus-canceled-mark)
8470           (gnus-message 4 "Deleted article %s" article)))
8471        (t
8472         (let* ((pto-group (gnus-group-prefixed-name
8473                            (car art-group) to-method))
8474                (entry
8475                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8476                (info (nth 2 entry))
8477                (to-group (gnus-info-group info))
8478                to-marks)
8479           ;; Update the group that has been moved to.
8480           (when (and info
8481                      (memq action '(move copy)))
8482             (unless (member to-group to-groups)
8483               (push to-group to-groups))
8484
8485             (unless (memq article gnus-newsgroup-unreads)
8486               (push 'read to-marks)
8487               (gnus-info-set-read
8488                info (gnus-add-to-range (gnus-info-read info)
8489                                        (list (cdr art-group)))))
8490
8491             ;; See whether the article is to be put in the cache.
8492             (let ((marks gnus-article-mark-lists)
8493                   (to-article (cdr art-group)))
8494
8495               ;; Enter the article into the cache in the new group,
8496               ;; if that is required.
8497               (when gnus-use-cache
8498                 (gnus-cache-possibly-enter-article
8499                  to-group to-article
8500                  (memq article gnus-newsgroup-marked)
8501                  (memq article gnus-newsgroup-dormant)
8502                  (memq article gnus-newsgroup-unreads)))
8503
8504               (when gnus-preserve-marks
8505                 ;; Copy any marks over to the new group.
8506                 (when (and (equal to-group gnus-newsgroup-name)
8507                            (not (memq article gnus-newsgroup-unreads)))
8508                   ;; Mark this article as read in this group.
8509                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8510                   (setcdr (gnus-active to-group) to-article)
8511                   (setcdr gnus-newsgroup-active to-article))
8512
8513                 (while marks
8514                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8515                     (when (memq article (symbol-value
8516                                          (intern (format "gnus-newsgroup-%s"
8517                                                          (caar marks)))))
8518                       (push (cdar marks) to-marks)
8519                       ;; If the other group is the same as this group,
8520                       ;; then we have to add the mark to the list.
8521                       (when (equal to-group gnus-newsgroup-name)
8522                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8523                              (cons to-article
8524                                    (symbol-value
8525                                     (intern (format "gnus-newsgroup-%s"
8526                                                     (caar marks)))))))
8527                       ;; Copy the marks to other group.
8528                       (gnus-add-marked-articles
8529                        to-group (cdar marks) (list to-article) info)))
8530                   (setq marks (cdr marks)))
8531
8532                 (gnus-request-set-mark to-group (list (list (list to-article)
8533                                                             'add
8534                                                             to-marks))))
8535
8536               (gnus-dribble-enter
8537                (concat "(gnus-group-set-info '"
8538                        (gnus-prin1-to-string (gnus-get-info to-group))
8539                        ")"))))
8540
8541           ;; Update the Xref header in this article to point to
8542           ;; the new crossposted article we have just created.
8543           (when (eq action 'crosspost)
8544             (save-excursion
8545               (set-buffer copy-buf)
8546               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8547               (nnheader-replace-header "Xref" new-xref)
8548               (gnus-request-replace-article
8549                article gnus-newsgroup-name (current-buffer)))))
8550
8551         ;;;!!!Why is this necessary?
8552         (set-buffer gnus-summary-buffer)
8553
8554         (gnus-summary-goto-subject article)
8555         (when (eq action 'move)
8556           (gnus-summary-mark-article article gnus-canceled-mark))))
8557       (gnus-summary-remove-process-mark article))
8558     ;; Re-activate all groups that have been moved to.
8559     (save-excursion
8560       (set-buffer gnus-group-buffer)
8561       (let ((gnus-group-marked to-groups))
8562         (gnus-group-get-new-news-this-group nil t)))
8563
8564     (gnus-kill-buffer copy-buf)
8565     (gnus-summary-position-point)
8566     (gnus-set-mode-line 'summary)))
8567
8568 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8569   "Move the current article to a different newsgroup.
8570 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8571 When called interactively, if TO-NEWSGROUP is nil, use the value of
8572 the variable `gnus-move-split-methods' for finding a default target
8573 newsgroup.
8574 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8575 re-spool using this method."
8576   (interactive "P")
8577   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8578
8579 (defun gnus-summary-crosspost-article (&optional n)
8580   "Crosspost the current article to some other group."
8581   (interactive "P")
8582   (gnus-summary-move-article n nil nil 'crosspost))
8583
8584 (defcustom gnus-summary-respool-default-method nil
8585   "Default method type for respooling an article.
8586 If nil, use to the current newsgroup method."
8587   :type 'symbol
8588   :group 'gnus-summary-mail)
8589
8590 (defun gnus-summary-respool-article (&optional n method)
8591   "Respool the current article.
8592 The article will be squeezed through the mail spooling process again,
8593 which means that it will be put in some mail newsgroup or other
8594 depending on `nnmail-split-methods'.
8595 If N is a positive number, respool the N next articles.
8596 If N is a negative number, respool the N previous articles.
8597 If N is nil and any articles have been marked with the process mark,
8598 respool those articles instead.
8599
8600 Respooling can be done both from mail groups and \"real\" newsgroups.
8601 In the former case, the articles in question will be moved from the
8602 current group into whatever groups they are destined to.  In the
8603 latter case, they will be copied into the relevant groups."
8604   (interactive
8605    (list current-prefix-arg
8606          (let* ((methods (gnus-methods-using 'respool))
8607                 (methname
8608                  (symbol-name (or gnus-summary-respool-default-method
8609                                   (car (gnus-find-method-for-group
8610                                         gnus-newsgroup-name)))))
8611                 (method
8612                  (gnus-completing-read-with-default
8613                   methname "What backend do you want to use when respooling?"
8614                   methods nil t nil 'gnus-mail-method-history))
8615                 ms)
8616            (cond
8617             ((zerop (length (setq ms (gnus-servers-using-backend
8618                                       (intern method)))))
8619              (list (intern method) ""))
8620             ((= 1 (length ms))
8621              (car ms))
8622             (t
8623              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8624                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8625                            ms-alist))))))))
8626   (unless method
8627     (error "No method given for respooling"))
8628   (if (assoc (symbol-name
8629               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8630              (gnus-methods-using 'respool))
8631       (gnus-summary-move-article n nil method)
8632     (gnus-summary-copy-article n nil method)))
8633
8634 (defun gnus-summary-import-article (file &optional edit)
8635   "Import an arbitrary file into a mail newsgroup."
8636   (interactive "fImport file: \nP")
8637   (let ((group gnus-newsgroup-name)
8638         (now (current-time))
8639         atts lines group-art)
8640     (unless (gnus-check-backend-function 'request-accept-article group)
8641       (error "%s does not support article importing" group))
8642     (or (file-readable-p file)
8643         (not (file-regular-p file))
8644         (error "Can't read %s" file))
8645     (save-excursion
8646       (set-buffer (gnus-get-buffer-create " *import file*"))
8647       (erase-buffer)
8648       (nnheader-insert-file-contents file)
8649       (goto-char (point-min))
8650       (if (nnheader-article-p)
8651           (save-restriction
8652             (goto-char (point-min))
8653             (search-forward "\n\n" nil t)
8654             (narrow-to-region (point-min) (1- (point)))
8655             (goto-char (point-min))
8656             (unless (re-search-forward "^date:" nil t)
8657               (goto-char (point-max))
8658               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8659        ;; This doesn't look like an article, so we fudge some headers.
8660         (setq atts (file-attributes file)
8661               lines (count-lines (point-min) (point-max)))
8662         (insert "From: " (read-string "From: ") "\n"
8663                 "Subject: " (read-string "Subject: ") "\n"
8664                 "Date: " (message-make-date (nth 5 atts)) "\n"
8665                 "Message-ID: " (message-make-message-id) "\n"
8666                 "Lines: " (int-to-string lines) "\n"
8667                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8668       (setq group-art (gnus-request-accept-article group nil t))
8669       (kill-buffer (current-buffer)))
8670     (setq gnus-newsgroup-active (gnus-activate-group group))
8671     (forward-line 1)
8672     (gnus-summary-goto-article (cdr group-art) nil t)
8673     (when edit
8674       (gnus-summary-edit-article))))
8675
8676 (defun gnus-summary-create-article ()
8677   "Create an article in a mail newsgroup."
8678   (interactive)
8679   (let ((group gnus-newsgroup-name)
8680         (now (current-time))
8681         group-art)
8682     (unless (gnus-check-backend-function 'request-accept-article group)
8683       (error "%s does not support article importing" group))
8684     (save-excursion
8685       (set-buffer (gnus-get-buffer-create " *import file*"))
8686       (erase-buffer)
8687       (goto-char (point-min))
8688       ;; This doesn't look like an article, so we fudge some headers.
8689       (insert "From: " (read-string "From: ") "\n"
8690               "Subject: " (read-string "Subject: ") "\n"
8691               "Date: " (message-make-date now) "\n"
8692               "Message-ID: " (message-make-message-id) "\n")
8693       (setq group-art (gnus-request-accept-article group nil t))
8694       (kill-buffer (current-buffer)))
8695     (setq gnus-newsgroup-active (gnus-activate-group group))
8696     (forward-line 1)
8697     (gnus-summary-goto-article (cdr group-art) nil t)
8698     (gnus-summary-edit-article)))
8699
8700 (defun gnus-summary-article-posted-p ()
8701   "Say whether the current (mail) article is available from news as well.
8702 This will be the case if the article has both been mailed and posted."
8703   (interactive)
8704   (let ((id (mail-header-references (gnus-summary-article-header)))
8705         (gnus-override-method (car (gnus-refer-article-methods))))
8706     (if (gnus-request-head id "")
8707         (gnus-message 2 "The current message was found on %s"
8708                       gnus-override-method)
8709       (gnus-message 2 "The current message couldn't be found on %s"
8710                     gnus-override-method)
8711       nil)))
8712
8713 (defun gnus-summary-expire-articles (&optional now)
8714   "Expire all articles that are marked as expirable in the current group."
8715   (interactive)
8716   (when (gnus-check-backend-function
8717          'request-expire-articles gnus-newsgroup-name)
8718     ;; This backend supports expiry.
8719     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8720            (expirable (if total
8721                           (progn
8722                             ;; We need to update the info for
8723                             ;; this group for `gnus-list-of-read-articles'
8724                             ;; to give us the right answer.
8725                             (gnus-run-hooks 'gnus-exit-group-hook)
8726                             (gnus-summary-update-info)
8727                             (gnus-list-of-read-articles gnus-newsgroup-name))
8728                         (setq gnus-newsgroup-expirable
8729                               (sort gnus-newsgroup-expirable '<))))
8730            (expiry-wait (if now 'immediate
8731                           (gnus-group-find-parameter
8732                            gnus-newsgroup-name 'expiry-wait)))
8733            (nnmail-expiry-target
8734             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8735                 nnmail-expiry-target))
8736            es)
8737       (when expirable
8738         ;; There are expirable articles in this group, so we run them
8739         ;; through the expiry process.
8740         (gnus-message 6 "Expiring articles...")
8741         (unless (gnus-check-group gnus-newsgroup-name)
8742           (error "Can't open server for %s" gnus-newsgroup-name))
8743         ;; The list of articles that weren't expired is returned.
8744         (save-excursion
8745           (if expiry-wait
8746               (let ((nnmail-expiry-wait-function nil)
8747                     (nnmail-expiry-wait expiry-wait))
8748                 (setq es (gnus-request-expire-articles
8749                           expirable gnus-newsgroup-name)))
8750             (setq es (gnus-request-expire-articles
8751                       expirable gnus-newsgroup-name)))
8752           (unless total
8753             (setq gnus-newsgroup-expirable es))
8754           ;; We go through the old list of expirable, and mark all
8755           ;; really expired articles as nonexistent.
8756           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8757             (let ((gnus-use-cache nil))
8758               (dolist (article expirable)
8759                 (when (and (not (memq article es))
8760                            (gnus-data-find article))
8761                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8762         (gnus-message 6 "Expiring articles...done")))))
8763
8764 (defun gnus-summary-expire-articles-now ()
8765   "Expunge all expirable articles in the current group.
8766 This means that *all* articles that are marked as expirable will be
8767 deleted forever, right now."
8768   (interactive)
8769   (or gnus-expert-user
8770       (gnus-yes-or-no-p
8771        "Are you really, really, really sure you want to delete all these messages? ")
8772       (error "Phew!"))
8773   (gnus-summary-expire-articles t))
8774
8775 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8776 (defun gnus-summary-delete-article (&optional n)
8777   "Delete the N next (mail) articles.
8778 This command actually deletes articles.  This is not a marking
8779 command.  The article will disappear forever from your life, never to
8780 return.
8781 If N is negative, delete backwards.
8782 If N is nil and articles have been marked with the process mark,
8783 delete these instead."
8784   (interactive "P")
8785   (unless (gnus-check-backend-function 'request-expire-articles
8786                                        gnus-newsgroup-name)
8787     (error "The current newsgroup does not support article deletion"))
8788   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8789     (error "Couldn't open server"))
8790   ;; Compute the list of articles to delete.
8791   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8792         not-deleted)
8793     (if (and gnus-novice-user
8794              (not (gnus-yes-or-no-p
8795                    (format "Do you really want to delete %s forever? "
8796                            (if (> (length articles) 1)
8797                                (format "these %s articles" (length articles))
8798                              "this article")))))
8799         ()
8800       ;; Delete the articles.
8801       (setq not-deleted (gnus-request-expire-articles
8802                          articles gnus-newsgroup-name 'force))
8803       (while articles
8804         (gnus-summary-remove-process-mark (car articles))
8805         ;; The backend might not have been able to delete the article
8806         ;; after all.
8807         (unless (memq (car articles) not-deleted)
8808           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8809         (setq articles (cdr articles)))
8810       (when not-deleted
8811         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8812     (gnus-summary-position-point)
8813     (gnus-set-mode-line 'summary)
8814     not-deleted))
8815
8816 (defun gnus-summary-edit-article (&optional arg)
8817   "Edit the current article.
8818 This will have permanent effect only in mail groups.
8819 If ARG is nil, edit the decoded articles.
8820 If ARG is 1, edit the raw articles.
8821 If ARG is 2, edit the raw articles even in read-only groups.
8822 If ARG is 3, edit the articles with the current handles.
8823 Otherwise, allow editing of articles even in read-only
8824 groups."
8825   (interactive "P")
8826   (let (force raw current-handles)
8827     (cond
8828      ((null arg))
8829      ((eq arg 1)
8830       (setq raw t))
8831      ((eq arg 2)
8832       (setq raw t
8833             force t))
8834      ((eq arg 3)
8835       (setq current-handles
8836             (and (gnus-buffer-live-p gnus-article-buffer)
8837                  (with-current-buffer gnus-article-buffer
8838                    (prog1
8839                        gnus-article-mime-handles
8840                      (setq gnus-article-mime-handles nil))))))
8841      (t
8842       (setq force t)))
8843     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8844       (error "Can't edit the raw article in group nndraft:drafts"))
8845     (save-excursion
8846       (set-buffer gnus-summary-buffer)
8847       (let ((mail-parse-charset gnus-newsgroup-charset)
8848             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8849         (gnus-set-global-variables)
8850         (when (and (not force)
8851                    (gnus-group-read-only-p))
8852           (error "The current newsgroup does not support article editing"))
8853         (gnus-summary-show-article t)
8854         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8855           (with-current-buffer gnus-article-buffer
8856             (mm-enable-multibyte)))
8857         (if (equal gnus-newsgroup-name "nndraft:drafts")
8858             (setq raw t))
8859         (gnus-article-edit-article
8860          (if raw 'ignore
8861            `(lambda ()
8862               (let ((mbl mml-buffer-list))
8863                 (setq mml-buffer-list nil)
8864                 (mime-to-mml ,'current-handles)
8865                 (let ((mbl1 mml-buffer-list))
8866                   (setq mml-buffer-list mbl)
8867                   (set (make-local-variable 'mml-buffer-list) mbl1))
8868                 (make-local-hook 'kill-buffer-hook)
8869                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8870          `(lambda (no-highlight)
8871             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8872                   (message-options message-options)
8873                   (message-options-set-recipient)
8874                   (mail-parse-ignored-charsets
8875                    ',gnus-newsgroup-ignored-charsets))
8876               ,(if (not raw) '(progn
8877                                 (mml-to-mime)
8878                                 (mml-destroy-buffers)
8879                                 (remove-hook 'kill-buffer-hook
8880                                              'mml-destroy-buffers t)
8881                                 (kill-local-variable 'mml-buffer-list)))
8882               (gnus-summary-edit-article-done
8883                ,(or (mail-header-references gnus-current-headers) "")
8884                ,(gnus-group-read-only-p)
8885                ,gnus-summary-buffer no-highlight))))))))
8886
8887 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8888
8889 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8890                                                  no-highlight)
8891   "Make edits to the current article permanent."
8892   (interactive)
8893   (save-excursion
8894    ;; The buffer restriction contains the entire article if it exists.
8895     (when (article-goto-body)
8896       (let ((lines (count-lines (point) (point-max)))
8897             (length (- (point-max) (point)))
8898             (case-fold-search t)
8899             (body (copy-marker (point))))
8900         (goto-char (point-min))
8901         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8902           (delete-region (match-beginning 1) (match-end 1))
8903           (insert (number-to-string length)))
8904         (goto-char (point-min))
8905         (when (re-search-forward
8906                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8907           (delete-region (match-beginning 1) (match-end 1))
8908           (insert (number-to-string length)))
8909         (goto-char (point-min))
8910         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8911           (delete-region (match-beginning 1) (match-end 1))
8912           (insert (number-to-string lines))))))
8913   ;; Replace the article.
8914   (let ((buf (current-buffer)))
8915     (with-temp-buffer
8916       (insert-buffer-substring buf)
8917
8918       (if (and (not read-only)
8919                (not (gnus-request-replace-article
8920                      (cdr gnus-article-current) (car gnus-article-current)
8921                      (current-buffer) t)))
8922           (error "Couldn't replace article")
8923         ;; Update the summary buffer.
8924         (if (and references
8925                  (equal (message-tokenize-header references " ")
8926                         (message-tokenize-header
8927                          (or (message-fetch-field "references") "") " ")))
8928             ;; We only have to update this line.
8929             (save-excursion
8930               (save-restriction
8931                 (message-narrow-to-head)
8932                 (let ((head (buffer-string))
8933                       header)
8934                   (with-temp-buffer
8935                     (insert (format "211 %d Article retrieved.\n"
8936                                     (cdr gnus-article-current)))
8937                     (insert head)
8938                     (insert ".\n")
8939                     (let ((nntp-server-buffer (current-buffer)))
8940                       (setq header (car (gnus-get-newsgroup-headers
8941                                          nil t))))
8942                     (save-excursion
8943                       (set-buffer gnus-summary-buffer)
8944                       (gnus-data-set-header
8945                        (gnus-data-find (cdr gnus-article-current))
8946                        header)
8947                       (gnus-summary-update-article-line
8948                        (cdr gnus-article-current) header)
8949                       (if (gnus-summary-goto-subject
8950                            (cdr gnus-article-current) nil t)
8951                           (gnus-summary-update-secondary-mark
8952                            (cdr gnus-article-current))))))))
8953           ;; Update threads.
8954           (set-buffer (or buffer gnus-summary-buffer))
8955           (gnus-summary-update-article (cdr gnus-article-current))
8956           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8957               (gnus-summary-update-secondary-mark
8958                (cdr gnus-article-current))))
8959         ;; Prettify the article buffer again.
8960         (unless no-highlight
8961           (save-excursion
8962             (set-buffer gnus-article-buffer)
8963             ;;;!!! Fix this -- article should be rehighlighted.
8964             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8965             (set-buffer gnus-original-article-buffer)
8966             (gnus-request-article
8967              (cdr gnus-article-current)
8968              (car gnus-article-current) (current-buffer))))
8969         ;; Prettify the summary buffer line.
8970         (when (gnus-visual-p 'summary-highlight 'highlight)
8971           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8972
8973 (defun gnus-summary-edit-wash (key)
8974   "Perform editing command KEY in the article buffer."
8975   (interactive
8976    (list
8977     (progn
8978       (message "%s" (concat (this-command-keys) "- "))
8979       (read-char))))
8980   (message "")
8981   (gnus-summary-edit-article)
8982   (execute-kbd-macro (concat (this-command-keys) key))
8983   (gnus-article-edit-done))
8984
8985 ;;; Respooling
8986
8987 (defun gnus-summary-respool-query (&optional silent trace)
8988   "Query where the respool algorithm would put this article."
8989   (interactive)
8990   (let (gnus-mark-article-hook)
8991     (gnus-summary-select-article)
8992     (save-excursion
8993       (set-buffer gnus-original-article-buffer)
8994       (save-restriction
8995         (message-narrow-to-head)
8996         (let ((groups (nnmail-article-group 'identity trace)))
8997           (unless silent
8998             (if groups
8999                 (message "This message would go to %s"
9000                          (mapconcat 'car groups ", "))
9001               (message "This message would go to no groups"))
9002             groups))))))
9003
9004 (defun gnus-summary-respool-trace ()
9005   "Trace where the respool algorithm would put this article.
9006 Display a buffer showing all fancy splitting patterns which matched."
9007   (interactive)
9008   (gnus-summary-respool-query nil t))
9009
9010 ;; Summary marking commands.
9011
9012 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9013   "Mark articles which has the same subject as read, and then select the next.
9014 If UNMARK is positive, remove any kind of mark.
9015 If UNMARK is negative, tick articles."
9016   (interactive "P")
9017   (when unmark
9018     (setq unmark (prefix-numeric-value unmark)))
9019   (let ((count
9020          (gnus-summary-mark-same-subject
9021           (gnus-summary-article-subject) unmark)))
9022     ;; Select next unread article.  If auto-select-same mode, should
9023     ;; select the first unread article.
9024     (gnus-summary-next-article t (and gnus-auto-select-same
9025                                       (gnus-summary-article-subject)))
9026     (gnus-message 7 "%d article%s marked as %s"
9027                   count (if (= count 1) " is" "s are")
9028                   (if unmark "unread" "read"))))
9029
9030 (defun gnus-summary-kill-same-subject (&optional unmark)
9031   "Mark articles which has the same subject as read.
9032 If UNMARK is positive, remove any kind of mark.
9033 If UNMARK is negative, tick articles."
9034   (interactive "P")
9035   (when unmark
9036     (setq unmark (prefix-numeric-value unmark)))
9037   (let ((count
9038          (gnus-summary-mark-same-subject
9039           (gnus-summary-article-subject) unmark)))
9040     ;; If marked as read, go to next unread subject.
9041     (when (null unmark)
9042       ;; Go to next unread subject.
9043       (gnus-summary-next-subject 1 t))
9044     (gnus-message 7 "%d articles are marked as %s"
9045                   count (if unmark "unread" "read"))))
9046
9047 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9048   "Mark articles with same SUBJECT as read, and return marked number.
9049 If optional argument UNMARK is positive, remove any kinds of marks.
9050 If optional argument UNMARK is negative, mark articles as unread instead."
9051   (let ((count 1))
9052     (save-excursion
9053       (cond
9054        ((null unmark)                   ; Mark as read.
9055         (while (and
9056                 (progn
9057                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9058                   (gnus-summary-show-thread) t)
9059                 (gnus-summary-find-subject subject))
9060           (setq count (1+ count))))
9061        ((> unmark 0)                    ; Tick.
9062         (while (and
9063                 (progn
9064                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9065                   (gnus-summary-show-thread) t)
9066                 (gnus-summary-find-subject subject))
9067           (setq count (1+ count))))
9068        (t                               ; Mark as unread.
9069         (while (and
9070                 (progn
9071                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9072                   (gnus-summary-show-thread) t)
9073                 (gnus-summary-find-subject subject))
9074           (setq count (1+ count)))))
9075       (gnus-set-mode-line 'summary)
9076       ;; Return the number of marked articles.
9077       count)))
9078
9079 (defun gnus-summary-mark-as-processable (n &optional unmark)
9080   "Set the process mark on the next N articles.
9081 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9082 the process mark instead.  The difference between N and the actual
9083 number of articles marked is returned."
9084   (interactive "P")
9085   (if (and (null n) (gnus-region-active-p))
9086       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9087     (setq n (prefix-numeric-value n))
9088     (let ((backward (< n 0))
9089           (n (abs n)))
9090       (while (and
9091               (> n 0)
9092               (if unmark
9093                   (gnus-summary-remove-process-mark
9094                    (gnus-summary-article-number))
9095                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9096               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9097         (setq n (1- n)))
9098       (when (/= 0 n)
9099         (gnus-message 7 "No more articles"))
9100       (gnus-summary-recenter)
9101       (gnus-summary-position-point)
9102       n)))
9103
9104 (defun gnus-summary-unmark-as-processable (n)
9105   "Remove the process mark from the next N articles.
9106 If N is negative, unmark backward instead.  The difference between N and
9107 the actual number of articles unmarked is returned."
9108   (interactive "P")
9109   (gnus-summary-mark-as-processable n t))
9110
9111 (defun gnus-summary-unmark-all-processable ()
9112   "Remove the process mark from all articles."
9113   (interactive)
9114   (save-excursion
9115     (while gnus-newsgroup-processable
9116       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9117   (gnus-summary-position-point))
9118
9119 (defun gnus-summary-add-mark (article type)
9120   "Mark ARTICLE with a mark of TYPE."
9121   (let ((vtype (car (assq type gnus-article-mark-lists)))
9122         var)
9123     (if (not vtype)
9124         (error "No such mark type: %s" type)
9125       (setq var (intern (format "gnus-newsgroup-%s" type)))
9126       (set var (cons article (symbol-value var)))
9127       (if (memq type '(processable cached replied forwarded recent saved))
9128           (gnus-summary-update-secondary-mark article)
9129         ;;; !!! This is bogus.  We should find out what primary
9130         ;;; !!! mark we want to set.
9131         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9132
9133 (defun gnus-summary-mark-as-expirable (n)
9134   "Mark N articles forward as expirable.
9135 If N is negative, mark backward instead.  The difference between N and
9136 the actual number of articles marked is returned."
9137   (interactive "p")
9138   (gnus-summary-mark-forward n gnus-expirable-mark))
9139
9140 (defun gnus-summary-mark-article-as-replied (article)
9141   "Mark ARTICLE as replied to and update the summary line.
9142 ARTICLE can also be a list of articles."
9143   (interactive (list (gnus-summary-article-number)))
9144   (let ((articles (if (listp article) article (list article))))
9145     (dolist (article articles)
9146       (push article gnus-newsgroup-replied)
9147       (let ((buffer-read-only nil))
9148         (when (gnus-summary-goto-subject article nil t)
9149           (gnus-summary-update-secondary-mark article))))))
9150
9151 (defun gnus-summary-mark-article-as-forwarded (article)
9152   "Mark ARTICLE as forwarded and update the summary line.
9153 ARTICLE can also be a list of articles."
9154   (let ((articles (if (listp article) article (list article))))
9155     (dolist (article articles)
9156       (push article gnus-newsgroup-forwarded)
9157       (let ((buffer-read-only nil))
9158         (when (gnus-summary-goto-subject article nil t)
9159           (gnus-summary-update-secondary-mark article))))))
9160
9161 (defun gnus-summary-set-bookmark (article)
9162   "Set a bookmark in current article."
9163   (interactive (list (gnus-summary-article-number)))
9164   (when (or (not (get-buffer gnus-article-buffer))
9165             (not gnus-current-article)
9166             (not gnus-article-current)
9167             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9168     (error "No current article selected"))
9169   ;; Remove old bookmark, if one exists.
9170   (let ((old (assq article gnus-newsgroup-bookmarks)))
9171     (when old
9172       (setq gnus-newsgroup-bookmarks
9173             (delq old gnus-newsgroup-bookmarks))))
9174   ;; Set the new bookmark, which is on the form
9175   ;; (article-number . line-number-in-body).
9176   (push
9177    (cons article
9178          (save-excursion
9179            (set-buffer gnus-article-buffer)
9180            (count-lines
9181             (min (point)
9182                  (save-excursion
9183                    (goto-char (point-min))
9184                    (search-forward "\n\n" nil t)
9185                    (point)))
9186             (point))))
9187    gnus-newsgroup-bookmarks)
9188   (gnus-message 6 "A bookmark has been added to the current article."))
9189
9190 (defun gnus-summary-remove-bookmark (article)
9191   "Remove the bookmark from the current article."
9192   (interactive (list (gnus-summary-article-number)))
9193   ;; Remove old bookmark, if one exists.
9194   (let ((old (assq article gnus-newsgroup-bookmarks)))
9195     (if old
9196         (progn
9197           (setq gnus-newsgroup-bookmarks
9198                 (delq old gnus-newsgroup-bookmarks))
9199           (gnus-message 6 "Removed bookmark."))
9200       (gnus-message 6 "No bookmark in current article."))))
9201
9202 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9203 (defun gnus-summary-mark-as-dormant (n)
9204   "Mark N articles forward as dormant.
9205 If N is negative, mark backward instead.  The difference between N and
9206 the actual number of articles marked is returned."
9207   (interactive "p")
9208   (gnus-summary-mark-forward n gnus-dormant-mark))
9209
9210 (defun gnus-summary-set-process-mark (article)
9211   "Set the process mark on ARTICLE and update the summary line."
9212   (setq gnus-newsgroup-processable
9213         (cons article
9214               (delq article gnus-newsgroup-processable)))
9215   (when (gnus-summary-goto-subject article)
9216     (gnus-summary-show-thread)
9217     (gnus-summary-goto-subject article)
9218     (gnus-summary-update-secondary-mark article)))
9219
9220 (defun gnus-summary-remove-process-mark (article)
9221   "Remove the process mark from ARTICLE and update the summary line."
9222   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9223   (when (gnus-summary-goto-subject article)
9224     (gnus-summary-show-thread)
9225     (gnus-summary-goto-subject article)
9226     (gnus-summary-update-secondary-mark article)))
9227
9228 (defun gnus-summary-set-saved-mark (article)
9229   "Set the process mark on ARTICLE and update the summary line."
9230   (push article gnus-newsgroup-saved)
9231   (when (gnus-summary-goto-subject article)
9232     (gnus-summary-update-secondary-mark article)))
9233
9234 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9235   "Mark N articles as read forwards.
9236 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9237 The difference between N and the actual number of articles marked is
9238 returned.
9239 Iff NO-EXPIRE, auto-expiry will be inhibited."
9240   (interactive "p")
9241   (gnus-summary-show-thread)
9242   (let ((backward (< n 0))
9243         (gnus-summary-goto-unread
9244          (and gnus-summary-goto-unread
9245               (not (eq gnus-summary-goto-unread 'never))
9246               (not (memq mark (list gnus-unread-mark
9247                                     gnus-ticked-mark gnus-dormant-mark)))))
9248         (n (abs n))
9249         (mark (or mark gnus-del-mark)))
9250     (while (and (> n 0)
9251                 (gnus-summary-mark-article nil mark no-expire)
9252                 (zerop (gnus-summary-next-subject
9253                         (if backward -1 1)
9254                         (and gnus-summary-goto-unread
9255                              (not (eq gnus-summary-goto-unread 'never)))
9256                         t)))
9257       (setq n (1- n)))
9258     (when (/= 0 n)
9259       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9260     (gnus-summary-recenter)
9261     (gnus-summary-position-point)
9262     (gnus-set-mode-line 'summary)
9263     n))
9264
9265 (defun gnus-summary-mark-article-as-read (mark)
9266   "Mark the current article quickly as read with MARK."
9267   (let ((article (gnus-summary-article-number)))
9268     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9269     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9270     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9271     (push (cons article mark) gnus-newsgroup-reads)
9272     ;; Possibly remove from cache, if that is used.
9273     (when gnus-use-cache
9274       (gnus-cache-enter-remove-article article))
9275     ;; Allow the backend to change the mark.
9276     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9277     ;; Check for auto-expiry.
9278     (when (and gnus-newsgroup-auto-expire
9279                (memq mark gnus-auto-expirable-marks))
9280       (setq mark gnus-expirable-mark)
9281       ;; Let the backend know about the mark change.
9282       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9283       (push article gnus-newsgroup-expirable))
9284     ;; Set the mark in the buffer.
9285     (gnus-summary-update-mark mark 'unread)
9286     t))
9287
9288 (defun gnus-summary-mark-article-as-unread (mark)
9289   "Mark the current article quickly as unread with MARK."
9290   (let* ((article (gnus-summary-article-number))
9291          (old-mark (gnus-summary-article-mark article)))
9292     ;; Allow the backend to change the mark.
9293     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9294     (if (eq mark old-mark)
9295         t
9296       (if (<= article 0)
9297           (progn
9298             (gnus-error 1 "Can't mark negative article numbers")
9299             nil)
9300         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9301         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9302         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9303         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9304         (cond ((= mark gnus-ticked-mark)
9305                (setq gnus-newsgroup-marked
9306                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9307                                               article)))
9308               ((= mark gnus-dormant-mark)
9309                (setq gnus-newsgroup-dormant
9310                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9311                                               article)))
9312               (t
9313                (setq gnus-newsgroup-unreads
9314                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9315                                               article))))
9316         (gnus-pull article gnus-newsgroup-reads)
9317
9318         ;; See whether the article is to be put in the cache.
9319         (and gnus-use-cache
9320              (vectorp (gnus-summary-article-header article))
9321              (save-excursion
9322                (gnus-cache-possibly-enter-article
9323                 gnus-newsgroup-name article
9324                 (= mark gnus-ticked-mark)
9325                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9326
9327         ;; Fix the mark.
9328         (gnus-summary-update-mark mark 'unread)
9329         t))))
9330
9331 (defun gnus-summary-mark-article (&optional article mark no-expire)
9332   "Mark ARTICLE with MARK.  MARK can be any character.
9333 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9334 `??' (dormant) and `?E' (expirable).
9335 If MARK is nil, then the default character `?r' is used.
9336 If ARTICLE is nil, then the article on the current line will be
9337 marked.
9338 Iff NO-EXPIRE, auto-expiry will be inhibited."
9339   ;; The mark might be a string.
9340   (when (stringp mark)
9341     (setq mark (aref mark 0)))
9342   ;; If no mark is given, then we check auto-expiring.
9343   (when (null mark)
9344     (setq mark gnus-del-mark))
9345   (when (and (not no-expire)
9346              gnus-newsgroup-auto-expire
9347              (memq mark gnus-auto-expirable-marks))
9348     (setq mark gnus-expirable-mark))
9349   (let ((article (or article (gnus-summary-article-number)))
9350         (old-mark (gnus-summary-article-mark article)))
9351     ;; Allow the backend to change the mark.
9352     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9353     (if (eq mark old-mark)
9354         t
9355       (unless article
9356         (error "No article on current line"))
9357       (if (not (if (or (= mark gnus-unread-mark)
9358                        (= mark gnus-ticked-mark)
9359                        (= mark gnus-dormant-mark))
9360                    (gnus-mark-article-as-unread article mark)
9361                  (gnus-mark-article-as-read article mark)))
9362           t
9363         ;; See whether the article is to be put in the cache.
9364         (and gnus-use-cache
9365              (not (= mark gnus-canceled-mark))
9366              (vectorp (gnus-summary-article-header article))
9367              (save-excursion
9368                (gnus-cache-possibly-enter-article
9369                 gnus-newsgroup-name article
9370                 (= mark gnus-ticked-mark)
9371                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9372
9373         (when (gnus-summary-goto-subject article nil t)
9374           (let ((buffer-read-only nil))
9375             (gnus-summary-show-thread)
9376             ;; Fix the mark.
9377             (gnus-summary-update-mark mark 'unread)
9378             t))))))
9379
9380 (defun gnus-summary-update-secondary-mark (article)
9381   "Update the secondary (read, process, cache) mark."
9382   (gnus-summary-update-mark
9383    (cond ((memq article gnus-newsgroup-processable)
9384           gnus-process-mark)
9385          ((memq article gnus-newsgroup-cached)
9386           gnus-cached-mark)
9387          ((memq article gnus-newsgroup-replied)
9388           gnus-replied-mark)
9389          ((memq article gnus-newsgroup-forwarded)
9390           gnus-forwarded-mark)
9391          ((memq article gnus-newsgroup-saved)
9392           gnus-saved-mark)
9393          ((memq article gnus-newsgroup-recent)
9394           gnus-recent-mark)
9395          ((memq article gnus-newsgroup-unseen)
9396           gnus-unseen-mark)
9397          (t gnus-no-mark))
9398    'replied)
9399   (when (gnus-visual-p 'summary-highlight 'highlight)
9400     (gnus-run-hooks 'gnus-summary-update-hook))
9401   t)
9402
9403 (defun gnus-summary-update-mark (mark type)
9404   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9405         (buffer-read-only nil))
9406     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9407     (when forward
9408       (when (looking-at "\r")
9409         (incf forward))
9410       (when (<= (+ forward (point)) (point-max))
9411         ;; Go to the right position on the line.
9412         (goto-char (+ forward (point)))
9413         ;; Replace the old mark with the new mark.
9414         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9415         ;; Optionally update the marks by some user rule.
9416         (when (eq type 'unread)
9417           (gnus-data-set-mark
9418            (gnus-data-find (gnus-summary-article-number)) mark)
9419           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9420
9421 (defun gnus-mark-article-as-read (article &optional mark)
9422   "Enter ARTICLE in the pertinent lists and remove it from others."
9423   ;; Make the article expirable.
9424   (let ((mark (or mark gnus-del-mark)))
9425     (setq gnus-newsgroup-expirable
9426           (if (= mark gnus-expirable-mark)
9427               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9428             (delq article gnus-newsgroup-expirable)))
9429     ;; Remove from unread and marked lists.
9430     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9431     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9432     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9433     (push (cons article mark) gnus-newsgroup-reads)
9434     ;; Possibly remove from cache, if that is used.
9435     (when gnus-use-cache
9436       (gnus-cache-enter-remove-article article))
9437     t))
9438
9439 (defun gnus-mark-article-as-unread (article &optional mark)
9440   "Enter ARTICLE in the pertinent lists and remove it from others."
9441   (let ((mark (or mark gnus-ticked-mark)))
9442     (if (<= article 0)
9443         (progn
9444           (gnus-error 1 "Can't mark negative article numbers")
9445           nil)
9446       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9447             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9448             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9449             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9450
9451       ;; Unsuppress duplicates?
9452       (when gnus-suppress-duplicates
9453         (gnus-dup-unsuppress-article article))
9454
9455       (cond ((= mark gnus-ticked-mark)
9456              (setq gnus-newsgroup-marked
9457                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9458             ((= mark gnus-dormant-mark)
9459              (setq gnus-newsgroup-dormant
9460                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9461             (t
9462              (setq gnus-newsgroup-unreads
9463                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9464       (gnus-pull article gnus-newsgroup-reads)
9465       t)))
9466
9467 (defalias 'gnus-summary-mark-as-unread-forward
9468   'gnus-summary-tick-article-forward)
9469 (make-obsolete 'gnus-summary-mark-as-unread-forward
9470                'gnus-summary-tick-article-forward)
9471 (defun gnus-summary-tick-article-forward (n)
9472   "Tick N articles forwards.
9473 If N is negative, tick backwards instead.
9474 The difference between N and the number of articles ticked is returned."
9475   (interactive "p")
9476   (gnus-summary-mark-forward n gnus-ticked-mark))
9477
9478 (defalias 'gnus-summary-mark-as-unread-backward
9479   'gnus-summary-tick-article-backward)
9480 (make-obsolete 'gnus-summary-mark-as-unread-backward
9481                'gnus-summary-tick-article-backward)
9482 (defun gnus-summary-tick-article-backward (n)
9483   "Tick N articles backwards.
9484 The difference between N and the number of articles ticked is returned."
9485   (interactive "p")
9486   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9487
9488 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9489 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9490 (defun gnus-summary-tick-article (&optional article clear-mark)
9491   "Mark current article as unread.
9492 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9493 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9494   (interactive)
9495   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9496                                        gnus-ticked-mark)))
9497
9498 (defun gnus-summary-mark-as-read-forward (n)
9499   "Mark N articles as read forwards.
9500 If N is negative, mark backwards instead.
9501 The difference between N and the actual number of articles marked is
9502 returned."
9503   (interactive "p")
9504   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9505
9506 (defun gnus-summary-mark-as-read-backward (n)
9507   "Mark the N articles as read backwards.
9508 The difference between N and the actual number of articles marked is
9509 returned."
9510   (interactive "p")
9511   (gnus-summary-mark-forward
9512    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9513
9514 (defun gnus-summary-mark-as-read (&optional article mark)
9515   "Mark current article as read.
9516 ARTICLE specifies the article to be marked as read.
9517 MARK specifies a string to be inserted at the beginning of the line."
9518   (gnus-summary-mark-article article mark))
9519
9520 (defun gnus-summary-clear-mark-forward (n)
9521   "Clear marks from N articles forward.
9522 If N is negative, clear backward instead.
9523 The difference between N and the number of marks cleared is returned."
9524   (interactive "p")
9525   (gnus-summary-mark-forward n gnus-unread-mark))
9526
9527 (defun gnus-summary-clear-mark-backward (n)
9528   "Clear marks from N articles backward.
9529 The difference between N and the number of marks cleared is returned."
9530   (interactive "p")
9531   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9532
9533 (defun gnus-summary-mark-unread-as-read ()
9534   "Intended to be used by `gnus-summary-mark-article-hook'."
9535   (when (memq gnus-current-article gnus-newsgroup-unreads)
9536     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9537
9538 (defun gnus-summary-mark-read-and-unread-as-read ()
9539   "Intended to be used by `gnus-summary-mark-article-hook'."
9540   (let ((mark (gnus-summary-article-mark)))
9541     (when (or (gnus-unread-mark-p mark)
9542               (gnus-read-mark-p mark))
9543       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9544
9545 (defun gnus-summary-mark-unread-as-ticked ()
9546   "Intended to be used by `gnus-summary-mark-article-hook'."
9547   (when (memq gnus-current-article gnus-newsgroup-unreads)
9548     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9549
9550 (defun gnus-summary-mark-region-as-read (point mark all)
9551   "Mark all unread articles between point and mark as read.
9552 If given a prefix, mark all articles between point and mark as read,
9553 even ticked and dormant ones."
9554   (interactive "r\nP")
9555   (save-excursion
9556     (let (article)
9557       (goto-char point)
9558       (beginning-of-line)
9559       (while (and
9560               (< (point) mark)
9561               (progn
9562                 (when (or all
9563                           (memq (setq article (gnus-summary-article-number))
9564                                 gnus-newsgroup-unreads))
9565                   (gnus-summary-mark-article article gnus-del-mark))
9566                 t)
9567               (gnus-summary-find-next))))))
9568
9569 (defun gnus-summary-mark-below (score mark)
9570   "Mark articles with score less than SCORE with MARK."
9571   (interactive "P\ncMark: ")
9572   (setq score (if score
9573                   (prefix-numeric-value score)
9574                 (or gnus-summary-default-score 0)))
9575   (save-excursion
9576     (set-buffer gnus-summary-buffer)
9577     (goto-char (point-min))
9578     (while
9579         (progn
9580           (and (< (gnus-summary-article-score) score)
9581                (gnus-summary-mark-article nil mark))
9582           (gnus-summary-find-next)))))
9583
9584 (defun gnus-summary-kill-below (&optional score)
9585   "Mark articles with score below SCORE as read."
9586   (interactive "P")
9587   (gnus-summary-mark-below score gnus-killed-mark))
9588
9589 (defun gnus-summary-clear-above (&optional score)
9590   "Clear all marks from articles with score above SCORE."
9591   (interactive "P")
9592   (gnus-summary-mark-above score gnus-unread-mark))
9593
9594 (defun gnus-summary-tick-above (&optional score)
9595   "Tick all articles with score above SCORE."
9596   (interactive "P")
9597   (gnus-summary-mark-above score gnus-ticked-mark))
9598
9599 (defun gnus-summary-mark-above (score mark)
9600   "Mark articles with score over SCORE with MARK."
9601   (interactive "P\ncMark: ")
9602   (setq score (if score
9603                   (prefix-numeric-value score)
9604                 (or gnus-summary-default-score 0)))
9605   (save-excursion
9606     (set-buffer gnus-summary-buffer)
9607     (goto-char (point-min))
9608     (while (and (progn
9609                   (when (> (gnus-summary-article-score) score)
9610                     (gnus-summary-mark-article nil mark))
9611                   t)
9612                 (gnus-summary-find-next)))))
9613
9614 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9615 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9616 (defun gnus-summary-limit-include-expunged (&optional no-error)
9617   "Display all the hidden articles that were expunged for low scores."
9618   (interactive)
9619   (let ((buffer-read-only nil))
9620     (let ((scored gnus-newsgroup-scored)
9621           headers h)
9622       (while scored
9623         (unless (gnus-summary-article-header (caar scored))
9624           (and (setq h (gnus-number-to-header (caar scored)))
9625                (< (cdar scored) gnus-summary-expunge-below)
9626                (push h headers)))
9627         (setq scored (cdr scored)))
9628       (if (not headers)
9629           (when (not no-error)
9630             (error "No expunged articles hidden"))
9631         (goto-char (point-min))
9632         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9633         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9634         (mapcar (lambda (x) (push (mail-header-number x)
9635                                   gnus-newsgroup-limit))
9636                 headers)
9637         (gnus-summary-prepare-unthreaded (nreverse headers))
9638         (goto-char (point-min))
9639         (gnus-summary-position-point)
9640         t))))
9641
9642 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9643   "Mark all unread articles in this newsgroup as read.
9644 If prefix argument ALL is non-nil, ticked and dormant articles will
9645 also be marked as read.
9646 If QUIETLY is non-nil, no questions will be asked.
9647 If TO-HERE is non-nil, it should be a point in the buffer.  All
9648 articles before (after, if REVERSE is set) this point will be marked as read.
9649 Note that this function will only catch up the unread article
9650 in the current summary buffer limitation.
9651 The number of articles marked as read is returned."
9652   (interactive "P")
9653   (prog1
9654       (save-excursion
9655         (when (or quietly
9656                   (not gnus-interactive-catchup) ;Without confirmation?
9657                   gnus-expert-user
9658                   (gnus-y-or-n-p
9659                    (if all
9660                        "Mark absolutely all articles as read? "
9661                      "Mark all unread articles as read? ")))
9662           (if (and not-mark
9663                    (not gnus-newsgroup-adaptive)
9664                    (not gnus-newsgroup-auto-expire)
9665                    (not gnus-suppress-duplicates)
9666                    (or (not gnus-use-cache)
9667                        (eq gnus-use-cache 'passive)))
9668               (progn
9669                 (when all
9670                   (setq gnus-newsgroup-marked nil
9671                         gnus-newsgroup-dormant nil))
9672                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9673             ;; We actually mark all articles as canceled, which we
9674             ;; have to do when using auto-expiry or adaptive scoring.
9675             (gnus-summary-show-all-threads)
9676             (if (and to-here reverse)
9677                 (progn
9678                   (goto-char to-here)
9679                   (while (and
9680                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9681                           (gnus-summary-find-next (not all) nil nil t))))
9682               (when (gnus-summary-first-subject (not all) t)
9683                 (while (and
9684                         (if to-here (< (point) to-here) t)
9685                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9686                         (gnus-summary-find-next (not all) nil nil t)))))
9687             (gnus-set-mode-line 'summary))
9688           t))
9689     (gnus-summary-position-point)))
9690
9691 (defun gnus-summary-catchup-to-here (&optional all)
9692   "Mark all unticked articles before the current one as read.
9693 If ALL is non-nil, also mark ticked and dormant articles as read."
9694   (interactive "P")
9695   (save-excursion
9696     (gnus-save-hidden-threads
9697       (let ((beg (point)))
9698         ;; We check that there are unread articles.
9699         (when (or all (gnus-summary-find-prev))
9700           (gnus-summary-catchup all t beg)))))
9701   (gnus-summary-position-point))
9702
9703 (defun gnus-summary-catchup-from-here (&optional all)
9704   "Mark all unticked articles after the current one as read.
9705 If ALL is non-nil, also mark ticked and dormant articles as read."
9706   (interactive "P")
9707   (save-excursion
9708     (gnus-save-hidden-threads
9709       (let ((beg (point)))
9710         ;; We check that there are unread articles.
9711         (when (or all (gnus-summary-find-next))
9712           (gnus-summary-catchup all t beg nil t)))))
9713
9714   (gnus-summary-position-point))
9715 (defun gnus-summary-catchup-all (&optional quietly)
9716   "Mark all articles in this newsgroup as read."
9717   (interactive "P")
9718   (gnus-summary-catchup t quietly))
9719
9720 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9721   "Mark all unread articles in this group as read, then exit.
9722 If prefix argument ALL is non-nil, all articles are marked as read.
9723 If QUIETLY is non-nil, no questions will be asked."
9724   (interactive "P")
9725   (when (gnus-summary-catchup all quietly nil 'fast)
9726     ;; Select next newsgroup or exit.
9727     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9728              (eq gnus-auto-select-next 'quietly))
9729         (gnus-summary-next-group nil)
9730       (gnus-summary-exit))))
9731
9732 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9733   "Mark all articles in this newsgroup as read, and then exit."
9734   (interactive "P")
9735   (gnus-summary-catchup-and-exit t quietly))
9736
9737 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9738   "Mark all articles in this group as read and select the next group.
9739 If given a prefix, mark all articles, unread as well as ticked, as
9740 read."
9741   (interactive "P")
9742   (save-excursion
9743     (gnus-summary-catchup all))
9744   (gnus-summary-next-group))
9745
9746 ;;;
9747 ;;; with article
9748 ;;;
9749
9750 (defmacro gnus-with-article (article &rest forms)
9751   "Select ARTICLE and perform FORMS in the original article buffer.
9752 Then replace the article with the result."
9753   `(progn
9754      ;; We don't want the article to be marked as read.
9755      (let (gnus-mark-article-hook)
9756        (gnus-summary-select-article t t nil ,article))
9757      (set-buffer gnus-original-article-buffer)
9758      ,@forms
9759      (if (not (gnus-check-backend-function
9760                'request-replace-article (car gnus-article-current)))
9761          (gnus-message 5 "Read-only group; not replacing")
9762        (unless (gnus-request-replace-article
9763                 ,article (car gnus-article-current)
9764                 (current-buffer) t)
9765          (error "Couldn't replace article")))
9766      ;; The cache and backlog have to be flushed somewhat.
9767      (when gnus-keep-backlog
9768        (gnus-backlog-remove-article
9769         (car gnus-article-current) (cdr gnus-article-current)))
9770      (when gnus-use-cache
9771        (gnus-cache-update-article
9772         (car gnus-article-current) (cdr gnus-article-current)))))
9773
9774 (put 'gnus-with-article 'lisp-indent-function 1)
9775 (put 'gnus-with-article 'edebug-form-spec '(form body))
9776
9777 ;; Thread-based commands.
9778
9779 (defun gnus-summary-articles-in-thread (&optional article)
9780   "Return a list of all articles in the current thread.
9781 If ARTICLE is non-nil, return all articles in the thread that starts
9782 with that article."
9783   (let* ((article (or article (gnus-summary-article-number)))
9784          (data (gnus-data-find-list article))
9785          (top-level (gnus-data-level (car data)))
9786          (top-subject
9787           (cond ((null gnus-thread-operation-ignore-subject)
9788                  (gnus-simplify-subject-re
9789                   (mail-header-subject (gnus-data-header (car data)))))
9790                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9791                  (gnus-simplify-subject-fuzzy
9792                   (mail-header-subject (gnus-data-header (car data)))))
9793                 (t nil)))
9794          (end-point (save-excursion
9795                       (if (gnus-summary-go-to-next-thread)
9796                           (point) (point-max))))
9797          articles)
9798     (while (and data
9799                 (< (gnus-data-pos (car data)) end-point))
9800       (when (or (not top-subject)
9801                 (string= top-subject
9802                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9803                              (gnus-simplify-subject-fuzzy
9804                               (mail-header-subject
9805                                (gnus-data-header (car data))))
9806                            (gnus-simplify-subject-re
9807                             (mail-header-subject
9808                              (gnus-data-header (car data)))))))
9809         (push (gnus-data-number (car data)) articles))
9810       (unless (and (setq data (cdr data))
9811                    (> (gnus-data-level (car data)) top-level))
9812         (setq data nil)))
9813     ;; Return the list of articles.
9814     (nreverse articles)))
9815
9816 (defun gnus-summary-rethread-current ()
9817   "Rethread the thread the current article is part of."
9818   (interactive)
9819   (let* ((gnus-show-threads t)
9820          (article (gnus-summary-article-number))
9821          (id (mail-header-id (gnus-summary-article-header)))
9822          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9823     (unless id
9824       (error "No article on the current line"))
9825     (gnus-rebuild-thread id)
9826     (gnus-summary-goto-subject article)))
9827
9828 (defun gnus-summary-reparent-thread ()
9829   "Make the current article child of the marked (or previous) article.
9830
9831 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9832 is non-nil or the Subject: of both articles are the same."
9833   (interactive)
9834   (unless (not (gnus-group-read-only-p))
9835     (error "The current newsgroup does not support article editing"))
9836   (unless (<= (length gnus-newsgroup-processable) 1)
9837     (error "No more than one article may be marked"))
9838   (save-window-excursion
9839     (let ((gnus-article-buffer " *reparent*")
9840           (current-article (gnus-summary-article-number))
9841           ;; First grab the marked article, otherwise one line up.
9842           (parent-article (if (not (null gnus-newsgroup-processable))
9843                               (car gnus-newsgroup-processable)
9844                             (save-excursion
9845                               (if (eq (forward-line -1) 0)
9846                                   (gnus-summary-article-number)
9847                                 (error "Beginning of summary buffer"))))))
9848       (unless (not (eq current-article parent-article))
9849         (error "An article may not be self-referential"))
9850       (let ((message-id (mail-header-id
9851                          (gnus-summary-article-header parent-article))))
9852         (unless (and message-id (not (equal message-id "")))
9853           (error "No message-id in desired parent"))
9854         (gnus-with-article current-article
9855           (save-restriction
9856             (goto-char (point-min))
9857             (message-narrow-to-head)
9858             (if (re-search-forward "^References: " nil t)
9859                 (progn
9860                   (re-search-forward "^[^ \t]" nil t)
9861                   (forward-line -1)
9862                   (end-of-line)
9863                   (insert " " message-id))
9864               (insert "References: " message-id "\n"))))
9865         (set-buffer gnus-summary-buffer)
9866         (gnus-summary-unmark-all-processable)
9867         (gnus-summary-update-article current-article)
9868         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9869             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9870         (gnus-summary-rethread-current)
9871         (gnus-message 3 "Article %d is now the child of article %d"
9872                       current-article parent-article)))))
9873
9874 (defun gnus-summary-toggle-threads (&optional arg)
9875   "Toggle showing conversation threads.
9876 If ARG is positive number, turn showing conversation threads on."
9877   (interactive "P")
9878   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9879     (setq gnus-show-threads
9880           (if (null arg) (not gnus-show-threads)
9881             (> (prefix-numeric-value arg) 0)))
9882     (gnus-summary-prepare)
9883     (gnus-summary-goto-subject current)
9884     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9885     (gnus-summary-position-point)))
9886
9887 (defun gnus-summary-show-all-threads ()
9888   "Show all threads."
9889   (interactive)
9890   (save-excursion
9891     (let ((buffer-read-only nil))
9892       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9893   (gnus-summary-position-point))
9894
9895 (defun gnus-summary-show-thread ()
9896   "Show thread subtrees.
9897 Returns nil if no thread was there to be shown."
9898   (interactive)
9899   (let ((buffer-read-only nil)
9900         (orig (point))
9901         ;; first goto end then to beg, to have point at beg after let
9902         (end (progn (end-of-line) (point)))
9903         (beg (progn (beginning-of-line) (point))))
9904     (prog1
9905         ;; Any hidden lines here?
9906         (search-forward "\r" end t)
9907       (subst-char-in-region beg end ?\^M ?\n t)
9908       (goto-char orig)
9909       (gnus-summary-position-point))))
9910
9911 (defun gnus-summary-maybe-hide-threads ()
9912   "If requested, hide the threads that should be hidden."
9913   (when (and gnus-show-threads
9914              gnus-thread-hide-subtree)
9915     (gnus-summary-hide-all-threads
9916      (if (or (consp gnus-thread-hide-subtree)
9917              (gnus-functionp gnus-thread-hide-subtree))
9918          (gnus-make-predicate gnus-thread-hide-subtree)
9919        nil))))
9920
9921 ;;; Hiding predicates.
9922
9923 (defun gnus-article-unread-p (header)
9924   (memq (mail-header-number header) gnus-newsgroup-unreads))
9925
9926 (defun gnus-article-unseen-p (header)
9927   (memq (mail-header-number header) gnus-newsgroup-unseen))
9928
9929 (defun gnus-map-articles (predicate articles)
9930   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9931   (apply 'gnus-or (mapcar predicate
9932                           (mapcar 'gnus-summary-article-header articles))))
9933
9934 (defun gnus-summary-hide-all-threads (&optional predicate)
9935   "Hide all thread subtrees.
9936 If PREDICATE is supplied, threads that satisfy this predicate
9937 will not be hidden."
9938   (interactive)
9939   (save-excursion
9940     (goto-char (point-min))
9941     (let ((end nil))
9942       (while (not end)
9943         (when (or (not predicate)
9944                   (gnus-map-articles
9945                    predicate (gnus-summary-article-children)))
9946             (gnus-summary-hide-thread))
9947         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9948   (gnus-summary-position-point))
9949
9950 (defun gnus-summary-hide-thread ()
9951   "Hide thread subtrees.
9952 If PREDICATE is supplied, threads that satisfy this predicate
9953 will not be hidden.
9954 Returns nil if no threads were there to be hidden."
9955   (interactive)
9956   (let ((buffer-read-only nil)
9957         (start (point))
9958         (article (gnus-summary-article-number)))
9959     (goto-char start)
9960     ;; Go forward until either the buffer ends or the subthread
9961     ;; ends.
9962     (when (and (not (eobp))
9963                (or (zerop (gnus-summary-next-thread 1 t))
9964                    (goto-char (point-max))))
9965       (prog1
9966           (if (and (> (point) start)
9967                    (search-backward "\n" start t))
9968               (progn
9969                 (subst-char-in-region start (point) ?\n ?\^M)
9970                 (gnus-summary-goto-subject article))
9971             (goto-char start)
9972             nil)))))
9973
9974 (defun gnus-summary-go-to-next-thread (&optional previous)
9975   "Go to the same level (or less) next thread.
9976 If PREVIOUS is non-nil, go to previous thread instead.
9977 Return the article number moved to, or nil if moving was impossible."
9978   (let ((level (gnus-summary-thread-level))
9979         (way (if previous -1 1))
9980         (beg (point)))
9981     (forward-line way)
9982     (while (and (not (eobp))
9983                 (< level (gnus-summary-thread-level)))
9984       (forward-line way))
9985     (if (eobp)
9986         (progn
9987           (goto-char beg)
9988           nil)
9989       (setq beg (point))
9990       (prog1
9991           (gnus-summary-article-number)
9992         (goto-char beg)))))
9993
9994 (defun gnus-summary-next-thread (n &optional silent)
9995   "Go to the same level next N'th thread.
9996 If N is negative, search backward instead.
9997 Returns the difference between N and the number of skips actually
9998 done.
9999
10000 If SILENT, don't output messages."
10001   (interactive "p")
10002   (let ((backward (< n 0))
10003         (n (abs n)))
10004     (while (and (> n 0)
10005                 (gnus-summary-go-to-next-thread backward))
10006       (decf n))
10007     (unless silent
10008       (gnus-summary-position-point))
10009     (when (and (not silent) (/= 0 n))
10010       (gnus-message 7 "No more threads"))
10011     n))
10012
10013 (defun gnus-summary-prev-thread (n)
10014   "Go to the same level previous N'th thread.
10015 Returns the difference between N and the number of skips actually
10016 done."
10017   (interactive "p")
10018   (gnus-summary-next-thread (- n)))
10019
10020 (defun gnus-summary-go-down-thread ()
10021   "Go down one level in the current thread."
10022   (let ((children (gnus-summary-article-children)))
10023     (when children
10024       (gnus-summary-goto-subject (car children)))))
10025
10026 (defun gnus-summary-go-up-thread ()
10027   "Go up one level in the current thread."
10028   (let ((parent (gnus-summary-article-parent)))
10029     (when parent
10030       (gnus-summary-goto-subject parent))))
10031
10032 (defun gnus-summary-down-thread (n)
10033   "Go down thread N steps.
10034 If N is negative, go up instead.
10035 Returns the difference between N and how many steps down that were
10036 taken."
10037   (interactive "p")
10038   (let ((up (< n 0))
10039         (n (abs n)))
10040     (while (and (> n 0)
10041                 (if up (gnus-summary-go-up-thread)
10042                   (gnus-summary-go-down-thread)))
10043       (setq n (1- n)))
10044     (gnus-summary-position-point)
10045     (when (/= 0 n)
10046       (gnus-message 7 "Can't go further"))
10047     n))
10048
10049 (defun gnus-summary-up-thread (n)
10050   "Go up thread N steps.
10051 If N is negative, go down instead.
10052 Returns the difference between N and how many steps down that were
10053 taken."
10054   (interactive "p")
10055   (gnus-summary-down-thread (- n)))
10056
10057 (defun gnus-summary-top-thread ()
10058   "Go to the top of the thread."
10059   (interactive)
10060   (while (gnus-summary-go-up-thread))
10061   (gnus-summary-article-number))
10062
10063 (defun gnus-summary-kill-thread (&optional unmark)
10064   "Mark articles under current thread as read.
10065 If the prefix argument is positive, remove any kinds of marks.
10066 If the prefix argument is negative, tick articles instead."
10067   (interactive "P")
10068   (when unmark
10069     (setq unmark (prefix-numeric-value unmark)))
10070   (let ((articles (gnus-summary-articles-in-thread)))
10071     (save-excursion
10072       ;; Expand the thread.
10073       (gnus-summary-show-thread)
10074       ;; Mark all the articles.
10075       (while articles
10076         (gnus-summary-goto-subject (car articles))
10077         (cond ((null unmark)
10078                (gnus-summary-mark-article-as-read gnus-killed-mark))
10079               ((> unmark 0)
10080                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10081               (t
10082                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10083         (setq articles (cdr articles))))
10084     ;; Hide killed subtrees.
10085     (and (null unmark)
10086          gnus-thread-hide-killed
10087          (gnus-summary-hide-thread))
10088     ;; If marked as read, go to next unread subject.
10089     (when (null unmark)
10090       ;; Go to next unread subject.
10091       (gnus-summary-next-subject 1 t)))
10092   (gnus-set-mode-line 'summary))
10093
10094 ;; Summary sorting commands
10095
10096 (defun gnus-summary-sort-by-number (&optional reverse)
10097   "Sort the summary buffer by article number.
10098 Argument REVERSE means reverse order."
10099   (interactive "P")
10100   (gnus-summary-sort 'number reverse))
10101
10102 (defun gnus-summary-sort-by-author (&optional reverse)
10103   "Sort the summary buffer by author name alphabetically.
10104 If `case-fold-search' is non-nil, case of letters is ignored.
10105 Argument REVERSE means reverse order."
10106   (interactive "P")
10107   (gnus-summary-sort 'author reverse))
10108
10109 (defun gnus-summary-sort-by-subject (&optional reverse)
10110   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10111 If `case-fold-search' is non-nil, case of letters is ignored.
10112 Argument REVERSE means reverse order."
10113   (interactive "P")
10114   (gnus-summary-sort 'subject reverse))
10115
10116 (defun gnus-summary-sort-by-date (&optional reverse)
10117   "Sort the summary buffer by date.
10118 Argument REVERSE means reverse order."
10119   (interactive "P")
10120   (gnus-summary-sort 'date reverse))
10121
10122 (defun gnus-summary-sort-by-score (&optional reverse)
10123   "Sort the summary buffer by score.
10124 Argument REVERSE means reverse order."
10125   (interactive "P")
10126   (gnus-summary-sort 'score reverse))
10127
10128 (defun gnus-summary-sort-by-lines (&optional reverse)
10129   "Sort the summary buffer by the number of lines.
10130 Argument REVERSE means reverse order."
10131   (interactive "P")
10132   (gnus-summary-sort 'lines reverse))
10133
10134 (defun gnus-summary-sort-by-chars (&optional reverse)
10135   "Sort the summary buffer by article length.
10136 Argument REVERSE means reverse order."
10137   (interactive "P")
10138   (gnus-summary-sort 'chars reverse))
10139
10140 (defun gnus-summary-sort-by-original (&optional reverse)
10141   "Sort the summary buffer using the default sorting method.
10142 Argument REVERSE means reverse order."
10143   (interactive "P")
10144   (let* ((buffer-read-only)
10145          (gnus-summary-prepare-hook nil))
10146     ;; We do the sorting by regenerating the threads.
10147     (gnus-summary-prepare)
10148     ;; Hide subthreads if needed.
10149     (gnus-summary-maybe-hide-threads)))
10150
10151 (defun gnus-summary-sort (predicate reverse)
10152   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10153   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10154          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10155          (gnus-thread-sort-functions
10156           (if (not reverse)
10157               thread
10158             `(lambda (t1 t2)
10159                (,thread t2 t1))))
10160          (gnus-sort-gathered-threads-function
10161           gnus-thread-sort-functions)
10162          (gnus-article-sort-functions
10163           (if (not reverse)
10164               article
10165             `(lambda (t1 t2)
10166                (,article t2 t1))))
10167          (buffer-read-only)
10168          (gnus-summary-prepare-hook nil))
10169     ;; We do the sorting by regenerating the threads.
10170     (gnus-summary-prepare)
10171     ;; Hide subthreads if needed.
10172     (gnus-summary-maybe-hide-threads)))
10173
10174 ;; Summary saving commands.
10175
10176 (defun gnus-summary-save-article (&optional n not-saved)
10177   "Save the current article using the default saver function.
10178 If N is a positive number, save the N next articles.
10179 If N is a negative number, save the N previous articles.
10180 If N is nil and any articles have been marked with the process mark,
10181 save those articles instead.
10182 The variable `gnus-default-article-saver' specifies the saver function."
10183   (interactive "P")
10184   (let* ((articles (gnus-summary-work-articles n))
10185          (save-buffer (save-excursion
10186                         (nnheader-set-temp-buffer " *Gnus Save*")))
10187          (num (length articles))
10188          header file)
10189     (dolist (article articles)
10190       (setq header (gnus-summary-article-header article))
10191       (if (not (vectorp header))
10192           ;; This is a pseudo-article.
10193           (if (assq 'name header)
10194               (gnus-copy-file (cdr (assq 'name header)))
10195             (gnus-message 1 "Article %d is unsaveable" article))
10196         ;; This is a real article.
10197         (save-window-excursion
10198           (let ((gnus-display-mime-function nil)
10199                 (gnus-article-prepare-hook nil))
10200             (gnus-summary-select-article t nil nil article)))
10201         (save-excursion
10202           (set-buffer save-buffer)
10203           (erase-buffer)
10204           (insert-buffer-substring gnus-original-article-buffer))
10205         (setq file (gnus-article-save save-buffer file num))
10206         (gnus-summary-remove-process-mark article)
10207         (unless not-saved
10208           (gnus-summary-set-saved-mark article))))
10209     (gnus-kill-buffer save-buffer)
10210     (gnus-summary-position-point)
10211     (gnus-set-mode-line 'summary)
10212     n))
10213
10214 (defun gnus-summary-pipe-output (&optional arg)
10215   "Pipe the current article to a subprocess.
10216 If N is a positive number, pipe the N next articles.
10217 If N is a negative number, pipe the N previous articles.
10218 If N is nil and any articles have been marked with the process mark,
10219 pipe those articles instead."
10220   (interactive "P")
10221   (require 'gnus-art)
10222   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10223     (gnus-summary-save-article arg t))
10224   (let ((buffer (get-buffer "*Shell Command Output*")))
10225     (if (and buffer
10226              (with-current-buffer buffer (> (point-max) (point-min))))
10227         (gnus-configure-windows 'pipe))))
10228
10229 (defun gnus-summary-save-article-mail (&optional arg)
10230   "Append the current article to an mail file.
10231 If N is a positive number, save the N next articles.
10232 If N is a negative number, save the N previous articles.
10233 If N is nil and any articles have been marked with the process mark,
10234 save those articles instead."
10235   (interactive "P")
10236   (require 'gnus-art)
10237   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10238     (gnus-summary-save-article arg)))
10239
10240 (defun gnus-summary-save-article-rmail (&optional arg)
10241   "Append the current article to an rmail file.
10242 If N is a positive number, save the N next articles.
10243 If N is a negative number, save the N previous articles.
10244 If N is nil and any articles have been marked with the process mark,
10245 save those articles instead."
10246   (interactive "P")
10247   (require 'gnus-art)
10248   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10249     (gnus-summary-save-article arg)))
10250
10251 (defun gnus-summary-save-article-file (&optional arg)
10252   "Append the current article to a file.
10253 If N is a positive number, save the N next articles.
10254 If N is a negative number, save the N previous articles.
10255 If N is nil and any articles have been marked with the process mark,
10256 save those articles instead."
10257   (interactive "P")
10258   (require 'gnus-art)
10259   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10260     (gnus-summary-save-article arg)))
10261
10262 (defun gnus-summary-write-article-file (&optional arg)
10263   "Write the current article to a file, deleting the previous file.
10264 If N is a positive number, save the N next articles.
10265 If N is a negative number, save the N previous articles.
10266 If N is nil and any articles have been marked with the process mark,
10267 save those articles instead."
10268   (interactive "P")
10269   (require 'gnus-art)
10270   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10271     (gnus-summary-save-article arg)))
10272
10273 (defun gnus-summary-save-article-body-file (&optional arg)
10274   "Append the current article body to a file.
10275 If N is a positive number, save the N next articles.
10276 If N is a negative number, save the N previous articles.
10277 If N is nil and any articles have been marked with the process mark,
10278 save those articles instead."
10279   (interactive "P")
10280   (require 'gnus-art)
10281   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10282     (gnus-summary-save-article arg)))
10283
10284 (defun gnus-summary-muttprint (&optional arg)
10285   "Print the current article using Muttprint.
10286 If N is a positive number, save the N next articles.
10287 If N is a negative number, save the N previous articles.
10288 If N is nil and any articles have been marked with the process mark,
10289 save those articles instead."
10290   (interactive "P")
10291   (require 'gnus-art)
10292   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10293     (gnus-summary-save-article arg t)))
10294
10295 (defun gnus-summary-pipe-message (program)
10296   "Pipe the current article through PROGRAM."
10297   (interactive "sProgram: ")
10298   (gnus-summary-select-article)
10299   (let ((mail-header-separator ""))
10300     (gnus-eval-in-buffer-window gnus-article-buffer
10301       (save-restriction
10302         (widen)
10303         (let ((start (window-start))
10304               buffer-read-only)
10305           (message-pipe-buffer-body program)
10306           (set-window-start (get-buffer-window (current-buffer)) start))))))
10307
10308 (defun gnus-get-split-value (methods)
10309   "Return a value based on the split METHODS."
10310   (let (split-name method result match)
10311     (when methods
10312       (save-excursion
10313         (set-buffer gnus-original-article-buffer)
10314         (save-restriction
10315           (nnheader-narrow-to-headers)
10316           (while (and methods (not split-name))
10317             (goto-char (point-min))
10318             (setq method (pop methods))
10319             (setq match (car method))
10320             (when (cond
10321                    ((stringp match)
10322                     ;; Regular expression.
10323                     (ignore-errors
10324                       (re-search-forward match nil t)))
10325                    ((gnus-functionp match)
10326                     ;; Function.
10327                     (save-restriction
10328                       (widen)
10329                       (setq result (funcall match gnus-newsgroup-name))))
10330                    ((consp match)
10331                     ;; Form.
10332                     (save-restriction
10333                       (widen)
10334                       (setq result (eval match)))))
10335               (setq split-name (cdr method))
10336               (cond ((stringp result)
10337                      (push (expand-file-name
10338                             result gnus-article-save-directory)
10339                            split-name))
10340                     ((consp result)
10341                      (setq split-name (append result split-name)))))))))
10342     (nreverse split-name)))
10343
10344 (defun gnus-valid-move-group-p (group)
10345   (and (boundp group)
10346        (symbol-name group)
10347        (symbol-value group)
10348        (gnus-get-function (gnus-find-method-for-group
10349                            (symbol-name group)) 'request-accept-article t)))
10350
10351 (defun gnus-read-move-group-name (prompt default articles prefix)
10352   "Read a group name."
10353   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10354          (minibuffer-confirm-incomplete nil) ; XEmacs
10355          (prom
10356           (format "%s %s to:"
10357                   prompt
10358                   (if (> (length articles) 1)
10359                       (format "these %d articles" (length articles))
10360                     "this article")))
10361          (to-newsgroup
10362           (cond
10363            ((null split-name)
10364             (gnus-completing-read-with-default
10365              default prom
10366              gnus-active-hashtb
10367              'gnus-valid-move-group-p
10368              nil prefix
10369              'gnus-group-history))
10370            ((= 1 (length split-name))
10371             (gnus-completing-read-with-default
10372              (car split-name) prom
10373              gnus-active-hashtb
10374              'gnus-valid-move-group-p
10375              nil nil
10376              'gnus-group-history))
10377            (t
10378             (gnus-completing-read-with-default
10379              nil prom
10380              (mapcar (lambda (el) (list el))
10381                      (nreverse split-name))
10382              nil nil nil
10383              'gnus-group-history))))
10384          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10385     (when to-newsgroup
10386       (if (or (string= to-newsgroup "")
10387               (string= to-newsgroup prefix))
10388           (setq to-newsgroup default))
10389       (unless to-newsgroup
10390         (error "No group name entered"))
10391       (or (gnus-active to-newsgroup)
10392           (gnus-activate-group to-newsgroup nil nil to-method)
10393           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10394                                      to-newsgroup))
10395               (or (and (gnus-request-create-group to-newsgroup to-method)
10396                        (gnus-activate-group
10397                         to-newsgroup nil nil to-method)
10398                        (gnus-subscribe-group to-newsgroup))
10399                   (error "Couldn't create group %s" to-newsgroup)))
10400           (error "No such group: %s" to-newsgroup)))
10401     to-newsgroup))
10402
10403 (defun gnus-summary-save-parts (type dir n &optional reverse)
10404   "Save parts matching TYPE to DIR.
10405 If REVERSE, save parts that do not match TYPE."
10406   (interactive
10407    (list (read-string "Save parts of type: "
10408                       (or (car gnus-summary-save-parts-type-history)
10409                           gnus-summary-save-parts-default-mime)
10410                       'gnus-summary-save-parts-type-history)
10411          (setq gnus-summary-save-parts-last-directory
10412                (read-file-name "Save to directory: "
10413                                gnus-summary-save-parts-last-directory
10414                                nil t))
10415          current-prefix-arg))
10416   (gnus-summary-iterate n
10417     (let ((gnus-display-mime-function nil)
10418           (gnus-inhibit-treatment t))
10419       (gnus-summary-select-article))
10420     (save-excursion
10421       (set-buffer gnus-article-buffer)
10422       (let ((handles (or gnus-article-mime-handles
10423                          (mm-dissect-buffer nil gnus-article-loose-mime)
10424                          (mm-uu-dissect))))
10425         (when handles
10426           (gnus-summary-save-parts-1 type dir handles reverse)
10427           (unless gnus-article-mime-handles ;; Don't destroy this case.
10428             (mm-destroy-parts handles)))))))
10429
10430 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10431   (if (stringp (car handle))
10432       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10433               (cdr handle))
10434     (when (if reverse
10435               (not (string-match type (mm-handle-media-type handle)))
10436             (string-match type (mm-handle-media-type handle)))
10437       (let ((file (expand-file-name
10438                    (file-name-nondirectory
10439                     (or
10440                      (mail-content-type-get
10441                       (mm-handle-disposition handle) 'filename)
10442                      (concat gnus-newsgroup-name
10443                              "." (number-to-string
10444                                   (cdr gnus-article-current)))))
10445                    dir)))
10446         (unless (file-exists-p file)
10447           (mm-save-part-to-file handle file))))))
10448
10449 ;; Summary extract commands
10450
10451 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10452   (let ((buffer-read-only nil)
10453         (article (gnus-summary-article-number))
10454         after-article b e)
10455     (unless (gnus-summary-goto-subject article)
10456       (error "No such article: %d" article))
10457     (gnus-summary-position-point)
10458     ;; If all commands are to be bunched up on one line, we collect
10459     ;; them here.
10460     (unless gnus-view-pseudos-separately
10461       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10462             files action)
10463         (while ps
10464           (setq action (cdr (assq 'action (car ps))))
10465           (setq files (list (cdr (assq 'name (car ps)))))
10466           (while (and ps (cdr ps)
10467                       (string= (or action "1")
10468                                (or (cdr (assq 'action (cadr ps))) "2")))
10469             (push (cdr (assq 'name (cadr ps))) files)
10470             (setcdr ps (cddr ps)))
10471           (when files
10472             (when (not (string-match "%s" action))
10473               (push " " files))
10474             (push " " files)
10475             (when (assq 'execute (car ps))
10476               (setcdr (assq 'execute (car ps))
10477                       (funcall (if (string-match "%s" action)
10478                                    'format 'concat)
10479                                action
10480                                (mapconcat
10481                                 (lambda (f)
10482                                   (if (equal f " ")
10483                                       f
10484                                     (mm-quote-arg f)))
10485                                 files " ")))))
10486           (setq ps (cdr ps)))))
10487     (if (and gnus-view-pseudos (not not-view))
10488         (while pslist
10489           (when (assq 'execute (car pslist))
10490             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10491                                   (eq gnus-view-pseudos 'not-confirm)))
10492           (setq pslist (cdr pslist)))
10493       (save-excursion
10494         (while pslist
10495           (setq after-article (or (cdr (assq 'article (car pslist)))
10496                                   (gnus-summary-article-number)))
10497           (gnus-summary-goto-subject after-article)
10498           (forward-line 1)
10499           (setq b (point))
10500           (insert "    " (file-name-nondirectory
10501                           (cdr (assq 'name (car pslist))))
10502                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10503           (setq e (point))
10504           (forward-line -1)             ; back to `b'
10505           (gnus-add-text-properties
10506            b (1- e) (list 'gnus-number gnus-reffed-article-number
10507                           gnus-mouse-face-prop gnus-mouse-face))
10508           (gnus-data-enter
10509            after-article gnus-reffed-article-number
10510            gnus-unread-mark b (car pslist) 0 (- e b))
10511           (setq gnus-newsgroup-unreads
10512                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10513                                          gnus-reffed-article-number))
10514           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10515           (setq pslist (cdr pslist)))))))
10516
10517 (defun gnus-pseudos< (p1 p2)
10518   (let ((c1 (cdr (assq 'action p1)))
10519         (c2 (cdr (assq 'action p2))))
10520     (and c1 c2 (string< c1 c2))))
10521
10522 (defun gnus-request-pseudo-article (props)
10523   (cond ((assq 'execute props)
10524          (gnus-execute-command (cdr (assq 'execute props)))))
10525   (let ((gnus-current-article (gnus-summary-article-number)))
10526     (gnus-run-hooks 'gnus-mark-article-hook)))
10527
10528 (defun gnus-execute-command (command &optional automatic)
10529   (save-excursion
10530     (gnus-article-setup-buffer)
10531     (set-buffer gnus-article-buffer)
10532     (setq buffer-read-only nil)
10533     (let ((command (if automatic command
10534                      (read-string "Command: " (cons command 0)))))
10535       (erase-buffer)
10536       (insert "$ " command "\n\n")
10537       (if gnus-view-pseudo-asynchronously
10538           (start-process "gnus-execute" (current-buffer) shell-file-name
10539                          shell-command-switch command)
10540         (call-process shell-file-name nil t nil
10541                       shell-command-switch command)))))
10542
10543 ;; Summary kill commands.
10544
10545 (defun gnus-summary-edit-global-kill (article)
10546   "Edit the \"global\" kill file."
10547   (interactive (list (gnus-summary-article-number)))
10548   (gnus-group-edit-global-kill article))
10549
10550 (defun gnus-summary-edit-local-kill ()
10551   "Edit a local kill file applied to the current newsgroup."
10552   (interactive)
10553   (setq gnus-current-headers (gnus-summary-article-header))
10554   (gnus-group-edit-local-kill
10555    (gnus-summary-article-number) gnus-newsgroup-name))
10556
10557 ;;; Header reading.
10558
10559 (defun gnus-read-header (id &optional header)
10560   "Read the headers of article ID and enter them into the Gnus system."
10561   (let ((group gnus-newsgroup-name)
10562         (gnus-override-method
10563          (or
10564           gnus-override-method
10565           (and (gnus-news-group-p gnus-newsgroup-name)
10566                (car (gnus-refer-article-methods)))))
10567         where)
10568     ;; First we check to see whether the header in question is already
10569     ;; fetched.
10570     (if (stringp id)
10571         ;; This is a Message-ID.
10572         (setq header (or header (gnus-id-to-header id)))
10573       ;; This is an article number.
10574       (setq header (or header (gnus-summary-article-header id))))
10575     (if (and header
10576              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10577         ;; We have found the header.
10578         header
10579       ;; If this is a sparse article, we have to nix out its
10580       ;; previous entry in the thread hashtb.
10581       (when (and header
10582                  (gnus-summary-article-sparse-p (mail-header-number header)))
10583         (let* ((parent (gnus-parent-id (mail-header-references header)))
10584                (thread (and parent (gnus-id-to-thread parent))))
10585           (when thread
10586             (delq (assq header thread) thread))))
10587       ;; We have to really fetch the header to this article.
10588       (save-excursion
10589         (set-buffer nntp-server-buffer)
10590         (when (setq where (gnus-request-head id group))
10591           (nnheader-fold-continuation-lines)
10592           (goto-char (point-max))
10593           (insert ".\n")
10594           (goto-char (point-min))
10595           (insert "211 ")
10596           (princ (cond
10597                   ((numberp id) id)
10598                   ((cdr where) (cdr where))
10599                   (header (mail-header-number header))
10600                   (t gnus-reffed-article-number))
10601                  (current-buffer))
10602           (insert " Article retrieved.\n"))
10603         (if (or (not where)
10604                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10605             ()                          ; Malformed head.
10606           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10607             (when (and (stringp id)
10608                        (not (string= (gnus-group-real-name group)
10609                                      (car where))))
10610               ;; If we fetched by Message-ID and the article came
10611               ;; from a different group, we fudge some bogus article
10612               ;; numbers for this article.
10613               (mail-header-set-number header gnus-reffed-article-number))
10614             (save-excursion
10615               (set-buffer gnus-summary-buffer)
10616               (decf gnus-reffed-article-number)
10617               (gnus-remove-header (mail-header-number header))
10618               (push header gnus-newsgroup-headers)
10619               (setq gnus-current-headers header)
10620               (push (mail-header-number header) gnus-newsgroup-limit)))
10621           header)))))
10622
10623 (defun gnus-remove-header (number)
10624   "Remove header NUMBER from `gnus-newsgroup-headers'."
10625   (if (and gnus-newsgroup-headers
10626            (= number (mail-header-number (car gnus-newsgroup-headers))))
10627       (pop gnus-newsgroup-headers)
10628     (let ((headers gnus-newsgroup-headers))
10629       (while (and (cdr headers)
10630                   (not (= number (mail-header-number (cadr headers)))))
10631         (pop headers))
10632       (when (cdr headers)
10633         (setcdr headers (cddr headers))))))
10634
10635 ;;;
10636 ;;; summary highlights
10637 ;;;
10638
10639 (defun gnus-highlight-selected-summary ()
10640   "Highlight selected article in summary buffer."
10641   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10642   (when gnus-summary-selected-face
10643     (save-excursion
10644       (let* ((beg (progn (beginning-of-line) (point)))
10645              (end (progn (end-of-line) (point)))
10646              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10647              (from (if (get-text-property beg gnus-mouse-face-prop)
10648                        beg
10649                      (or (next-single-property-change
10650                           beg gnus-mouse-face-prop nil end)
10651                          beg)))
10652              (to
10653               (if (= from end)
10654                   (- from 2)
10655                 (or (next-single-property-change
10656                      from gnus-mouse-face-prop nil end)
10657                     end))))
10658         ;; If no mouse-face prop on line we will have to = from = end,
10659         ;; so we highlight the entire line instead.
10660         (when (= (+ to 2) from)
10661           (setq from beg)
10662           (setq to end))
10663         (if gnus-newsgroup-selected-overlay
10664             ;; Move old overlay.
10665             (gnus-move-overlay
10666              gnus-newsgroup-selected-overlay from to (current-buffer))
10667           ;; Create new overlay.
10668           (gnus-overlay-put
10669            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10670            'face gnus-summary-selected-face))))))
10671
10672 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10673 (defun gnus-summary-highlight-line ()
10674   "Highlight current line according to `gnus-summary-highlight'."
10675   (let* ((list gnus-summary-highlight)
10676          (beg (gnus-point-at-bol))
10677          (article (gnus-summary-article-number))
10678          (score (or (cdr (assq (or article gnus-current-article)
10679                                gnus-newsgroup-scored))
10680                     gnus-summary-default-score 0))
10681          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10682          (inhibit-read-only t)
10683          (default gnus-summary-default-score)
10684          (default-high gnus-summary-default-high-score)
10685          (default-low gnus-summary-default-low-score))
10686     ;; Eval the cars of the lists until we find a match.
10687     (while (and list
10688                 (not (eval (caar list))))
10689       (setq list (cdr list)))
10690     (let ((face (cdar list)))
10691       (unless (eq face (get-text-property beg 'face))
10692         (gnus-put-text-property-excluding-characters-with-faces
10693          beg (gnus-point-at-eol) 'face
10694          (setq face (if (boundp face) (symbol-value face) face)))
10695         (when gnus-summary-highlight-line-function
10696           (funcall gnus-summary-highlight-line-function article face))))))
10697
10698 (defun gnus-update-read-articles (group unread &optional compute)
10699   "Update the list of read articles in GROUP.
10700 UNREAD is a sorted list."
10701   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10702          (entry (gnus-gethash group gnus-newsrc-hashtb))
10703          (info (nth 2 entry))
10704          (prev 1)
10705          read)
10706     (if (or (not info) (not active))
10707         ;; There is no info on this group if it was, in fact,
10708         ;; killed.  Gnus stores no information on killed groups, so
10709         ;; there's nothing to be done.
10710         ;; One could store the information somewhere temporarily,
10711         ;; perhaps...  Hmmm...
10712         ()
10713       ;; Remove any negative articles numbers.
10714       (while (and unread (< (car unread) 0))
10715         (setq unread (cdr unread)))
10716       ;; Remove any expired article numbers
10717       (while (and unread (< (car unread) (car active)))
10718         (setq unread (cdr unread)))
10719       ;; Compute the ranges of read articles by looking at the list of
10720       ;; unread articles.
10721       (while unread
10722         (when (/= (car unread) prev)
10723           (push (if (= prev (1- (car unread))) prev
10724                   (cons prev (1- (car unread))))
10725                 read))
10726         (setq prev (1+ (car unread)))
10727         (setq unread (cdr unread)))
10728       (when (<= prev (cdr active))
10729         (push (cons prev (cdr active)) read))
10730       (setq read (if (> (length read) 1) (nreverse read) read))
10731       (if compute
10732           read
10733         (save-excursion
10734           (let (setmarkundo)
10735             ;; Propagate the read marks to the backend.
10736             (when (gnus-check-backend-function 'request-set-mark group)
10737               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10738                     (add (gnus-remove-from-range read (gnus-info-read info))))
10739                 (when (or add del)
10740                   (unless (gnus-check-group group)
10741                     (error "Can't open server for %s" group))
10742                   (gnus-request-set-mark
10743                    group (delq nil (list (if add (list add 'add '(read)))
10744                                          (if del (list del 'del '(read))))))
10745                   (setq setmarkundo
10746                         `(gnus-request-set-mark
10747                           ,group
10748                           ',(delq nil (list
10749                                        (if del (list del 'add '(read)))
10750                                        (if add (list add 'del '(read))))))))))
10751             (set-buffer gnus-group-buffer)
10752             (gnus-undo-register
10753               `(progn
10754                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10755                  (gnus-info-set-read ',info ',(gnus-info-read info))
10756                  (gnus-get-unread-articles-in-group ',info
10757                                                     (gnus-active ,group))
10758                  (gnus-group-update-group ,group t)
10759                  ,setmarkundo))))
10760         ;; Enter this list into the group info.
10761         (gnus-info-set-read info read)
10762         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10763         (gnus-get-unread-articles-in-group info (gnus-active group))
10764         t))))
10765
10766 (defun gnus-offer-save-summaries ()
10767   "Offer to save all active summary buffers."
10768   (let (buffers)
10769     ;; Go through all buffers and find all summaries.
10770     (dolist (buffer (buffer-list))
10771       (when (and (setq buffer (buffer-name buffer))
10772                  (string-match "Summary" buffer)
10773                  (save-excursion
10774                    (set-buffer buffer)
10775                    ;; We check that this is, indeed, a summary buffer.
10776                    (and (eq major-mode 'gnus-summary-mode)
10777                         ;; Also make sure this isn't bogus.
10778                         gnus-newsgroup-prepared
10779                         ;; Also make sure that this isn't a
10780                         ;; dead summary buffer.
10781                         (not gnus-dead-summary-mode))))
10782         (push buffer buffers)))
10783     ;; Go through all these summary buffers and offer to save them.
10784     (when buffers
10785       (save-excursion
10786         (map-y-or-n-p
10787          "Update summary buffer %s? "
10788          (lambda (buf)
10789            (switch-to-buffer buf)
10790            (gnus-summary-exit))
10791          buffers)))))
10792
10793 (defun gnus-summary-setup-default-charset ()
10794   "Setup newsgroup default charset."
10795   (if (equal gnus-newsgroup-name "nndraft:drafts")
10796       (setq gnus-newsgroup-charset nil)
10797     (let* ((ignored-charsets
10798             (or gnus-newsgroup-ephemeral-ignored-charsets
10799                 (append
10800                  (and gnus-newsgroup-name
10801                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10802                  gnus-newsgroup-ignored-charsets))))
10803       (setq gnus-newsgroup-charset
10804             (or gnus-newsgroup-ephemeral-charset
10805                 (and gnus-newsgroup-name
10806                      (gnus-parameter-charset gnus-newsgroup-name))
10807                 gnus-default-charset))
10808       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10809            ignored-charsets))))
10810
10811 ;;;
10812 ;;; Mime Commands
10813 ;;;
10814
10815 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10816   "Display the current article buffer fully MIME-buttonized.
10817 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10818 treated as multipart/mixed."
10819   (interactive "P")
10820   (require 'gnus-art)
10821   (let ((gnus-unbuttonized-mime-types nil)
10822         (gnus-mime-display-multipart-as-mixed show-all-parts))
10823     (gnus-summary-show-article)))
10824
10825 (defun gnus-summary-repair-multipart (article)
10826   "Add a Content-Type header to a multipart article without one."
10827   (interactive (list (gnus-summary-article-number)))
10828   (gnus-with-article article
10829     (message-narrow-to-head)
10830     (message-remove-header "Mime-Version")
10831     (goto-char (point-max))
10832     (insert "Mime-Version: 1.0\n")
10833     (widen)
10834     (when (search-forward "\n--" nil t)
10835       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10836         (message-narrow-to-head)
10837         (message-remove-header "Content-Type")
10838         (goto-char (point-max))
10839         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10840                         separator))
10841         (widen))))
10842   (let (gnus-mark-article-hook)
10843     (gnus-summary-select-article t t nil article)))
10844
10845 (defun gnus-summary-toggle-display-buttonized ()
10846   "Toggle the buttonizing of the article buffer."
10847   (interactive)
10848   (require 'gnus-art)
10849   (if (setq gnus-inhibit-mime-unbuttonizing
10850             (not gnus-inhibit-mime-unbuttonizing))
10851       (let ((gnus-unbuttonized-mime-types nil))
10852         (gnus-summary-show-article))
10853     (gnus-summary-show-article)))
10854
10855 ;;;
10856 ;;; Generic summary marking commands
10857 ;;;
10858
10859 (defvar gnus-summary-marking-alist
10860   '((read gnus-del-mark "d")
10861     (unread gnus-unread-mark "u")
10862     (ticked gnus-ticked-mark "!")
10863     (dormant gnus-dormant-mark "?")
10864     (expirable gnus-expirable-mark "e"))
10865   "An alist of names/marks/keystrokes.")
10866
10867 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10868 (defvar gnus-summary-mark-map)
10869
10870 (defun gnus-summary-make-all-marking-commands ()
10871   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10872   (dolist (elem gnus-summary-marking-alist)
10873     (apply 'gnus-summary-make-marking-command elem)))
10874
10875 (defun gnus-summary-make-marking-command (name mark keystroke)
10876   (let ((map (make-sparse-keymap)))
10877     (define-key gnus-summary-generic-mark-map keystroke map)
10878     (dolist (lway `((next "next" next nil "n")
10879                     (next-unread "next unread" next t "N")
10880                     (prev "previous" prev nil "p")
10881                     (prev-unread "previous unread" prev t "P")
10882                     (nomove "" nil nil ,keystroke)))
10883       (let ((func (gnus-summary-make-marking-command-1
10884                    mark (car lway) lway name)))
10885         (setq func (eval func))
10886         (define-key map (nth 4 lway) func)))))
10887
10888 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10889   `(defun ,(intern
10890             (format "gnus-summary-put-mark-as-%s%s"
10891                     name (if (eq way 'nomove)
10892                              ""
10893                            (concat "-" (symbol-name way)))))
10894      (n)
10895      ,(format
10896        "Mark the current article as %s%s.
10897 If N, the prefix, then repeat N times.
10898 If N is negative, move in reverse order.
10899 The difference between N and the actual number of articles marked is
10900 returned."
10901        name (cadr lway))
10902      (interactive "p")
10903      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10904
10905 (defun gnus-summary-generic-mark (n mark move unread)
10906   "Mark N articles with MARK."
10907   (unless (eq major-mode 'gnus-summary-mode)
10908     (error "This command can only be used in the summary buffer"))
10909   (gnus-summary-show-thread)
10910   (let ((nummove
10911          (cond
10912           ((eq move 'next) 1)
10913           ((eq move 'prev) -1)
10914           (t 0))))
10915     (if (zerop nummove)
10916         (setq n 1)
10917       (when (< n 0)
10918         (setq n (abs n)
10919               nummove (* -1 nummove))))
10920     (while (and (> n 0)
10921                 (gnus-summary-mark-article nil mark)
10922                 (zerop (gnus-summary-next-subject nummove unread t)))
10923       (setq n (1- n)))
10924     (when (/= 0 n)
10925       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10926     (gnus-summary-recenter)
10927     (gnus-summary-position-point)
10928     (gnus-set-mode-line 'summary)
10929     n))
10930
10931 (defun gnus-summary-insert-articles (articles)
10932   (when (setq articles
10933               (gnus-sorted-difference articles
10934                                       (mapcar (lambda (h)
10935                                                 (mail-header-number h))
10936                                               gnus-newsgroup-headers)))
10937     (setq gnus-newsgroup-headers
10938           (merge 'list
10939                  gnus-newsgroup-headers
10940                  (gnus-fetch-headers articles)
10941                  'gnus-article-sort-by-number))
10942     ;; Suppress duplicates?
10943     (when gnus-suppress-duplicates
10944       (gnus-dup-suppress-articles))
10945
10946     ;; We might want to build some more threads first.
10947     (when (and gnus-fetch-old-headers
10948                (eq gnus-headers-retrieved-by 'nov))
10949       (if (eq gnus-fetch-old-headers 'invisible)
10950           (gnus-build-all-threads)
10951         (gnus-build-old-threads)))
10952     ;; Let the Gnus agent mark articles as read.
10953     (when gnus-agent
10954       (gnus-agent-get-undownloaded-list))
10955     ;; Remove list identifiers from subject
10956     (when gnus-list-identifiers
10957       (gnus-summary-remove-list-identifiers))
10958     ;; First and last article in this newsgroup.
10959     (when gnus-newsgroup-headers
10960       (setq gnus-newsgroup-begin
10961             (mail-header-number (car gnus-newsgroup-headers))
10962             gnus-newsgroup-end
10963             (mail-header-number
10964              (gnus-last-element gnus-newsgroup-headers))))
10965     (when gnus-use-scoring
10966       (gnus-possibly-score-headers))))
10967
10968 (defun gnus-summary-insert-old-articles (&optional all)
10969   "Insert all old articles in this group.
10970 If ALL is non-nil, already read articles become readable.
10971 If ALL is a number, fetch this number of articles."
10972   (interactive "P")
10973   (prog1
10974       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10975             older len)
10976         (setq older
10977               (gnus-sorted-difference
10978                (gnus-uncompress-range (list gnus-newsgroup-active))
10979                old))
10980         (setq len (length older))
10981         (cond
10982          ((null older) nil)
10983          ((numberp all)
10984           (if (< all len)
10985               (setq older (last older all))))
10986          (all nil)
10987          (t
10988           (if (and (numberp gnus-large-newsgroup)
10989                    (> len gnus-large-newsgroup))
10990               (let* ((cursor-in-echo-area nil)
10991                      (initial (gnus-parameter-large-newsgroup-initial 
10992                                gnus-newsgroup-name))
10993                      (input
10994                       (read-string
10995                        (format
10996                         "How many articles from %s (%s %d): "
10997                         (gnus-limit-string
10998                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
10999                         (if initial "max" "default")
11000                         len)
11001                        (if initial
11002                            (cons (number-to-string initial)
11003                                  0)))))
11004                 (unless (string-match "^[ \t]*$" input)
11005                   (setq all (string-to-number input))
11006                   (if (< all len)
11007                       (setq older (last older all))))))))
11008         (if (not older)
11009             (message "No old news.")
11010           (gnus-summary-insert-articles older)
11011           (gnus-summary-limit (gnus-sorted-nunion old older))))
11012     (gnus-summary-position-point)))
11013
11014 (defun gnus-summary-insert-new-articles ()
11015   "Insert all new articles in this group."
11016   (interactive)
11017   (prog1
11018       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11019             (old-active gnus-newsgroup-active)
11020             (nnmail-fetched-sources (list t))
11021             i new)
11022         (setq gnus-newsgroup-active
11023               (gnus-activate-group gnus-newsgroup-name 'scan))
11024         (setq i (1+ (cdr old-active)))
11025         (while (<= i (cdr gnus-newsgroup-active))
11026           (push i new)
11027           (incf i))
11028         (if (not new)
11029             (message "No gnus is bad news.")
11030           (setq new (nreverse new))
11031           (gnus-summary-insert-articles new)
11032           (setq gnus-newsgroup-unreads
11033                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11034           (gnus-summary-limit (gnus-sorted-nunion old new))))
11035     (gnus-summary-position-point)))
11036
11037 (gnus-summary-make-all-marking-commands)
11038
11039 (gnus-ems-redefine)
11040
11041 (provide 'gnus-sum)
11042
11043 (run-hooks 'gnus-sum-load-hook)
11044
11045 ;;; gnus-sum.el ends here