454c768c6bb81da959b20b846dff37260d05fec4
[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 (defun gnus-summary-make-tool-bar ()
2330   (if (and (fboundp 'tool-bar-add-item-from-menu)
2331            (default-value 'tool-bar-mode)
2332            (not gnus-summary-tool-bar-map))
2333       (setq gnus-summary-tool-bar-map
2334             (let ((tool-bar-map (make-sparse-keymap))
2335                   (load-path (mm-image-load-path)))
2336               (tool-bar-add-item-from-menu
2337                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2338               (tool-bar-add-item-from-menu
2339                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2340               (tool-bar-add-item-from-menu
2341                'gnus-summary-post-news "post" gnus-summary-mode-map)
2342               (tool-bar-add-item-from-menu
2343                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2344               (tool-bar-add-item-from-menu
2345                'gnus-summary-followup "followup" gnus-summary-mode-map)
2346               (tool-bar-add-item-from-menu
2347                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2348               (tool-bar-add-item-from-menu
2349                'gnus-summary-reply "reply" gnus-summary-mode-map)
2350               (tool-bar-add-item-from-menu
2351                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2352               (tool-bar-add-item-from-menu
2353                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2354               (tool-bar-add-item-from-menu
2355                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2356               (tool-bar-add-item-from-menu
2357                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2358               (tool-bar-add-item-from-menu
2359                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2360               (tool-bar-add-item-from-menu
2361                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2362               (tool-bar-add-item-from-menu
2363                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2364               (tool-bar-add-item-from-menu
2365                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2366               tool-bar-map)))
2367   (if gnus-summary-tool-bar-map
2368       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2369
2370 (defun gnus-score-set-default (var value)
2371   "A version of set that updates the GNU Emacs menu-bar."
2372   (set var value)
2373   ;; It is the message that forces the active status to be updated.
2374   (message ""))
2375
2376 (defun gnus-make-score-map (type)
2377   "Make a summary score map of type TYPE."
2378   (if t
2379       nil
2380     (let ((headers '(("author" "from" string)
2381                      ("subject" "subject" string)
2382                      ("article body" "body" string)
2383                      ("article head" "head" string)
2384                      ("xref" "xref" string)
2385                      ("extra header" "extra" string)
2386                      ("lines" "lines" number)
2387                      ("followups to author" "followup" string)))
2388           (types '((number ("less than" <)
2389                            ("greater than" >)
2390                            ("equal" =))
2391                    (string ("substring" s)
2392                            ("exact string" e)
2393                            ("fuzzy string" f)
2394                            ("regexp" r))))
2395           (perms '(("temporary" (current-time-string))
2396                    ("permanent" nil)
2397                    ("immediate" now)))
2398           header)
2399       (list
2400        (apply
2401         'nconc
2402         (list
2403          (if (eq type 'lower)
2404              "Lower score"
2405            "Increase score"))
2406         (let (outh)
2407           (while headers
2408             (setq header (car headers))
2409             (setq outh
2410                   (cons
2411                    (apply
2412                     'nconc
2413                     (list (car header))
2414                     (let ((ts (cdr (assoc (nth 2 header) types)))
2415                           outt)
2416                       (while ts
2417                         (setq outt
2418                               (cons
2419                                (apply
2420                                 'nconc
2421                                 (list (caar ts))
2422                                 (let ((ps perms)
2423                                       outp)
2424                                   (while ps
2425                                     (setq outp
2426                                           (cons
2427                                            (vector
2428                                             (caar ps)
2429                                             (list
2430                                              'gnus-summary-score-entry
2431                                              (nth 1 header)
2432                                              (if (or (string= (nth 1 header)
2433                                                               "head")
2434                                                      (string= (nth 1 header)
2435                                                               "body"))
2436                                                  ""
2437                                                (list 'gnus-summary-header
2438                                                      (nth 1 header)))
2439                                              (list 'quote (nth 1 (car ts)))
2440                                              (list 'gnus-score-delta-default
2441                                                    nil)
2442                                              (nth 1 (car ps))
2443                                              t)
2444                                             t)
2445                                            outp))
2446                                     (setq ps (cdr ps)))
2447                                   (list (nreverse outp))))
2448                                outt))
2449                         (setq ts (cdr ts)))
2450                       (list (nreverse outt))))
2451                    outh))
2452             (setq headers (cdr headers)))
2453           (list (nreverse outh))))))))
2454
2455 \f
2456
2457 (defun gnus-summary-mode (&optional group)
2458   "Major mode for reading articles.
2459
2460 All normal editing commands are switched off.
2461 \\<gnus-summary-mode-map>
2462 Each line in this buffer represents one article.  To read an
2463 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2464 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2465 respectively.
2466
2467 You can also post articles and send mail from this buffer.  To
2468 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2469 of an article, type `\\[gnus-summary-reply]'.
2470
2471 There are approx. one gazillion commands you can execute in this
2472 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2473
2474 The following commands are available:
2475
2476 \\{gnus-summary-mode-map}"
2477   (interactive)
2478   (kill-all-local-variables)
2479   (when (gnus-visual-p 'summary-menu 'menu)
2480     (gnus-summary-make-menu-bar)
2481     (gnus-summary-make-tool-bar))
2482   (gnus-summary-make-local-variables)
2483   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2484     (gnus-summary-make-local-variables))
2485   (gnus-make-thread-indent-array)
2486   (gnus-simplify-mode-line)
2487   (setq major-mode 'gnus-summary-mode)
2488   (setq mode-name "Summary")
2489   (make-local-variable 'minor-mode-alist)
2490   (use-local-map gnus-summary-mode-map)
2491   (buffer-disable-undo)
2492   (setq buffer-read-only t)             ;Disable modification
2493   (setq truncate-lines t)
2494   (setq selective-display t)
2495   (setq selective-display-ellipses t)   ;Display `...'
2496   (gnus-summary-set-display-table)
2497   (gnus-set-default-directory)
2498   (setq gnus-newsgroup-name group)
2499   (make-local-variable 'gnus-summary-line-format)
2500   (make-local-variable 'gnus-summary-line-format-spec)
2501   (make-local-variable 'gnus-summary-dummy-line-format)
2502   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2503   (make-local-variable 'gnus-summary-mark-positions)
2504   (make-local-hook 'pre-command-hook)
2505   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2506   (gnus-run-hooks 'gnus-summary-mode-hook)
2507   (turn-on-gnus-mailing-list-mode)
2508   (mm-enable-multibyte)
2509   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2510   (gnus-update-summary-mark-positions))
2511
2512 (defun gnus-summary-make-local-variables ()
2513   "Make all the local summary buffer variables."
2514   (let (global)
2515     (dolist (local gnus-summary-local-variables)
2516       (if (consp local)
2517           (progn
2518             (if (eq (cdr local) 'global)
2519                 ;; Copy the global value of the variable.
2520                 (setq global (symbol-value (car local)))
2521               ;; Use the value from the list.
2522               (setq global (eval (cdr local))))
2523             (set (make-local-variable (car local)) global))
2524         ;; Simple nil-valued local variable.
2525         (set (make-local-variable local) nil)))))
2526
2527 (defun gnus-summary-clear-local-variables ()
2528   (let ((locals gnus-summary-local-variables))
2529     (while locals
2530       (if (consp (car locals))
2531           (and (vectorp (caar locals))
2532                (set (caar locals) nil))
2533         (and (vectorp (car locals))
2534              (set (car locals) nil)))
2535       (setq locals (cdr locals)))))
2536
2537 ;; Summary data functions.
2538
2539 (defmacro gnus-data-number (data)
2540   `(car ,data))
2541
2542 (defmacro gnus-data-set-number (data number)
2543   `(setcar ,data ,number))
2544
2545 (defmacro gnus-data-mark (data)
2546   `(nth 1 ,data))
2547
2548 (defmacro gnus-data-set-mark (data mark)
2549   `(setcar (nthcdr 1 ,data) ,mark))
2550
2551 (defmacro gnus-data-pos (data)
2552   `(nth 2 ,data))
2553
2554 (defmacro gnus-data-set-pos (data pos)
2555   `(setcar (nthcdr 2 ,data) ,pos))
2556
2557 (defmacro gnus-data-header (data)
2558   `(nth 3 ,data))
2559
2560 (defmacro gnus-data-set-header (data header)
2561   `(setf (nth 3 ,data) ,header))
2562
2563 (defmacro gnus-data-level (data)
2564   `(nth 4 ,data))
2565
2566 (defmacro gnus-data-unread-p (data)
2567   `(= (nth 1 ,data) gnus-unread-mark))
2568
2569 (defmacro gnus-data-read-p (data)
2570   `(/= (nth 1 ,data) gnus-unread-mark))
2571
2572 (defmacro gnus-data-pseudo-p (data)
2573   `(consp (nth 3 ,data)))
2574
2575 (defmacro gnus-data-find (number)
2576   `(assq ,number gnus-newsgroup-data))
2577
2578 (defmacro gnus-data-find-list (number &optional data)
2579   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2580      (memq (assq ,number bdata)
2581            bdata)))
2582
2583 (defmacro gnus-data-make (number mark pos header level)
2584   `(list ,number ,mark ,pos ,header ,level))
2585
2586 (defun gnus-data-enter (after-article number mark pos header level offset)
2587   (let ((data (gnus-data-find-list after-article)))
2588     (unless data
2589       (error "No such article: %d" after-article))
2590     (setcdr data (cons (gnus-data-make number mark pos header level)
2591                        (cdr data)))
2592     (setq gnus-newsgroup-data-reverse nil)
2593     (gnus-data-update-list (cddr data) offset)))
2594
2595 (defun gnus-data-enter-list (after-article list &optional offset)
2596   (when list
2597     (let ((data (and after-article (gnus-data-find-list after-article)))
2598           (ilist list))
2599       (if (not (or data
2600                    after-article))
2601           (let ((odata gnus-newsgroup-data))
2602             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2603             (when offset
2604               (gnus-data-update-list odata offset)))
2605       ;; Find the last element in the list to be spliced into the main
2606         ;; list.
2607         (while (cdr list)
2608           (setq list (cdr list)))
2609         (if (not data)
2610             (progn
2611               (setcdr list gnus-newsgroup-data)
2612               (setq gnus-newsgroup-data ilist)
2613               (when offset
2614                 (gnus-data-update-list (cdr list) offset)))
2615           (setcdr list (cdr data))
2616           (setcdr data ilist)
2617           (when offset
2618             (gnus-data-update-list (cdr list) offset))))
2619       (setq gnus-newsgroup-data-reverse nil))))
2620
2621 (defun gnus-data-remove (article &optional offset)
2622   (let ((data gnus-newsgroup-data))
2623     (if (= (gnus-data-number (car data)) article)
2624         (progn
2625           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2626                 gnus-newsgroup-data-reverse nil)
2627           (when offset
2628             (gnus-data-update-list gnus-newsgroup-data offset)))
2629       (while (cdr data)
2630         (when (= (gnus-data-number (cadr data)) article)
2631           (setcdr data (cddr data))
2632           (when offset
2633             (gnus-data-update-list (cdr data) offset))
2634           (setq data nil
2635                 gnus-newsgroup-data-reverse nil))
2636         (setq data (cdr data))))))
2637
2638 (defmacro gnus-data-list (backward)
2639   `(if ,backward
2640        (or gnus-newsgroup-data-reverse
2641            (setq gnus-newsgroup-data-reverse
2642                  (reverse gnus-newsgroup-data)))
2643      gnus-newsgroup-data))
2644
2645 (defun gnus-data-update-list (data offset)
2646   "Add OFFSET to the POS of all data entries in DATA."
2647   (setq gnus-newsgroup-data-reverse nil)
2648   (while data
2649     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2650     (setq data (cdr data))))
2651
2652 (defun gnus-summary-article-pseudo-p (article)
2653   "Say whether this article is a pseudo article or not."
2654   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2655
2656 (defmacro gnus-summary-article-sparse-p (article)
2657   "Say whether this article is a sparse article or not."
2658   `(memq ,article gnus-newsgroup-sparse))
2659
2660 (defmacro gnus-summary-article-ancient-p (article)
2661   "Say whether this article is a sparse article or not."
2662   `(memq ,article gnus-newsgroup-ancient))
2663
2664 (defun gnus-article-parent-p (number)
2665   "Say whether this article is a parent or not."
2666   (let ((data (gnus-data-find-list number)))
2667     (and (cdr data)              ; There has to be an article after...
2668          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2669             (gnus-data-level (nth 1 data))))))
2670
2671 (defun gnus-article-children (number)
2672   "Return a list of all children to NUMBER."
2673   (let* ((data (gnus-data-find-list number))
2674          (level (gnus-data-level (car data)))
2675          children)
2676     (setq data (cdr data))
2677     (while (and data
2678                 (= (gnus-data-level (car data)) (1+ level)))
2679       (push (gnus-data-number (car data)) children)
2680       (setq data (cdr data)))
2681     children))
2682
2683 (defmacro gnus-summary-skip-intangible ()
2684   "If the current article is intangible, then jump to a different article."
2685   '(let ((to (get-text-property (point) 'gnus-intangible)))
2686      (and to (gnus-summary-goto-subject to))))
2687
2688 (defmacro gnus-summary-article-intangible-p ()
2689   "Say whether this article is intangible or not."
2690   '(get-text-property (point) 'gnus-intangible))
2691
2692 (defun gnus-article-read-p (article)
2693   "Say whether ARTICLE is read or not."
2694   (not (or (memq article gnus-newsgroup-marked)
2695            (memq article gnus-newsgroup-unreads)
2696            (memq article gnus-newsgroup-unselected)
2697            (memq article gnus-newsgroup-dormant))))
2698
2699 ;; Some summary mode macros.
2700
2701 (defmacro gnus-summary-article-number ()
2702   "The article number of the article on the current line.
2703 If there isn's an article number here, then we return the current
2704 article number."
2705   '(progn
2706      (gnus-summary-skip-intangible)
2707      (or (get-text-property (point) 'gnus-number)
2708          (gnus-summary-last-subject))))
2709
2710 (defmacro gnus-summary-article-header (&optional number)
2711   "Return the header of article NUMBER."
2712   `(gnus-data-header (gnus-data-find
2713                       ,(or number '(gnus-summary-article-number)))))
2714
2715 (defmacro gnus-summary-thread-level (&optional number)
2716   "Return the level of thread that starts with article NUMBER."
2717   `(if (and (eq gnus-summary-make-false-root 'dummy)
2718             (get-text-property (point) 'gnus-intangible))
2719        0
2720      (gnus-data-level (gnus-data-find
2721                        ,(or number '(gnus-summary-article-number))))))
2722
2723 (defmacro gnus-summary-article-mark (&optional number)
2724   "Return the mark of article NUMBER."
2725   `(gnus-data-mark (gnus-data-find
2726                     ,(or number '(gnus-summary-article-number)))))
2727
2728 (defmacro gnus-summary-article-pos (&optional number)
2729   "Return the position of the line of article NUMBER."
2730   `(gnus-data-pos (gnus-data-find
2731                    ,(or number '(gnus-summary-article-number)))))
2732
2733 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2734 (defmacro gnus-summary-article-subject (&optional number)
2735   "Return current subject string or nil if nothing."
2736   `(let ((headers
2737           ,(if number
2738                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2739              '(gnus-data-header (assq (gnus-summary-article-number)
2740                                       gnus-newsgroup-data)))))
2741      (and headers
2742           (vectorp headers)
2743           (mail-header-subject headers))))
2744
2745 (defmacro gnus-summary-article-score (&optional number)
2746   "Return current article score."
2747   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2748                   gnus-newsgroup-scored))
2749        gnus-summary-default-score 0))
2750
2751 (defun gnus-summary-article-children (&optional number)
2752   "Return a list of article numbers that are children of article NUMBER."
2753   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2754          (level (gnus-data-level (car data)))
2755          l children)
2756     (while (and (setq data (cdr data))
2757                 (> (setq l (gnus-data-level (car data))) level))
2758       (and (= (1+ level) l)
2759            (push (gnus-data-number (car data))
2760                  children)))
2761     (nreverse children)))
2762
2763 (defun gnus-summary-article-parent (&optional number)
2764   "Return the article number of the parent of article NUMBER."
2765   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2766                                     (gnus-data-list t)))
2767          (level (gnus-data-level (car data))))
2768     (if (zerop level)
2769         ()                              ; This is a root.
2770       ;; We search until we find an article with a level less than
2771       ;; this one.  That function has to be the parent.
2772       (while (and (setq data (cdr data))
2773                   (not (< (gnus-data-level (car data)) level))))
2774       (and data (gnus-data-number (car data))))))
2775
2776 (defun gnus-unread-mark-p (mark)
2777   "Say whether MARK is the unread mark."
2778   (= mark gnus-unread-mark))
2779
2780 (defun gnus-read-mark-p (mark)
2781   "Say whether MARK is one of the marks that mark as read.
2782 This is all marks except unread, ticked, dormant, and expirable."
2783   (not (or (= mark gnus-unread-mark)
2784            (= mark gnus-ticked-mark)
2785            (= mark gnus-dormant-mark)
2786            (= mark gnus-expirable-mark))))
2787
2788 (defmacro gnus-article-mark (number)
2789   "Return the MARK of article NUMBER.
2790 This macro should only be used when computing the mark the \"first\"
2791 time; i.e., when generating the summary lines.  After that,
2792 `gnus-summary-article-mark' should be used to examine the
2793 marks of articles."
2794   `(cond
2795     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2796     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2797     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2798     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2799     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2800     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2801     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2802     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2803            gnus-ancient-mark))))
2804
2805 ;; Saving hidden threads.
2806
2807 (defmacro gnus-save-hidden-threads (&rest forms)
2808   "Save hidden threads, eval FORMS, and restore the hidden threads."
2809   (let ((config (make-symbol "config")))
2810     `(let ((,config (gnus-hidden-threads-configuration)))
2811        (unwind-protect
2812            (save-excursion
2813              ,@forms)
2814          (gnus-restore-hidden-threads-configuration ,config)))))
2815 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2816 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2817
2818 (defun gnus-data-compute-positions ()
2819   "Compute the positions of all articles."
2820   (setq gnus-newsgroup-data-reverse nil)
2821   (let ((data gnus-newsgroup-data))
2822     (save-excursion
2823       (gnus-save-hidden-threads
2824         (gnus-summary-show-all-threads)
2825         (goto-char (point-min))
2826         (while data
2827           (while (get-text-property (point) 'gnus-intangible)
2828             (forward-line 1))
2829           (gnus-data-set-pos (car data) (+ (point) 3))
2830           (setq data (cdr data))
2831           (forward-line 1))))))
2832
2833 (defun gnus-hidden-threads-configuration ()
2834   "Return the current hidden threads configuration."
2835   (save-excursion
2836     (let (config)
2837       (goto-char (point-min))
2838       (while (search-forward "\r" nil t)
2839         (push (1- (point)) config))
2840       config)))
2841
2842 (defun gnus-restore-hidden-threads-configuration (config)
2843   "Restore hidden threads configuration from CONFIG."
2844   (save-excursion
2845     (let (point buffer-read-only)
2846       (while (setq point (pop config))
2847         (when (and (< point (point-max))
2848                    (goto-char point)
2849                    (eq (char-after) ?\n))
2850           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2851
2852 ;; Various summary mode internalish functions.
2853
2854 (defun gnus-mouse-pick-article (e)
2855   (interactive "e")
2856   (mouse-set-point e)
2857   (gnus-summary-next-page nil t))
2858
2859 (defun gnus-summary-set-display-table ()
2860   "Change the display table.
2861 Odd characters have a tendency to mess
2862 up nicely formatted displays - we make all possible glyphs
2863 display only a single character."
2864
2865   ;; We start from the standard display table, if any.
2866   (let ((table (or (copy-sequence standard-display-table)
2867                    (make-display-table)))
2868         (i 32))
2869     ;; Nix out all the control chars...
2870     (while (>= (setq i (1- i)) 0)
2871       (aset table i [??]))
2872    ;; ... but not newline and cr, of course.  (cr is necessary for the
2873     ;; selective display).
2874     (aset table ?\n nil)
2875     (aset table ?\r nil)
2876     ;; We keep TAB as well.
2877     (aset table ?\t nil)
2878     ;; We nix out any glyphs over 126 that are not set already.
2879     (let ((i 256))
2880       (while (>= (setq i (1- i)) 127)
2881         ;; Only modify if the entry is nil.
2882         (unless (aref table i)
2883           (aset table i [??]))))
2884     (setq buffer-display-table table)))
2885
2886 (defun gnus-summary-set-article-display-arrow (pos)
2887   "Update the overlay arrow to point to line at position POS."
2888   (when (and gnus-summary-display-arrow
2889              (boundp 'overlay-arrow-position)
2890              (boundp 'overlay-arrow-string))
2891     (save-excursion
2892       (goto-char pos)
2893       (beginning-of-line)
2894       (unless overlay-arrow-position
2895         (setq overlay-arrow-position (make-marker)))
2896       (setq overlay-arrow-string "=>"
2897             overlay-arrow-position (set-marker overlay-arrow-position
2898                                                (point)
2899                                                (current-buffer))))))
2900
2901 (defun gnus-summary-buffer-name (group)
2902   "Return the summary buffer name of GROUP."
2903   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2904
2905 (defun gnus-summary-setup-buffer (group)
2906   "Initialize summary buffer."
2907   (let ((buffer (gnus-summary-buffer-name group))
2908         (dead-name (concat "*Dead Summary "
2909                            (gnus-group-decoded-name group) "*")))
2910     ;; If a dead summary buffer exists, we kill it.
2911     (when (gnus-buffer-live-p dead-name)
2912       (gnus-kill-buffer dead-name))
2913     (if (get-buffer buffer)
2914         (progn
2915           (set-buffer buffer)
2916           (setq gnus-summary-buffer (current-buffer))
2917           (not gnus-newsgroup-prepared))
2918       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2919       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2920       (gnus-summary-mode group)
2921       (when gnus-carpal
2922         (gnus-carpal-setup-buffer 'summary))
2923       (unless gnus-single-article-buffer
2924         (make-local-variable 'gnus-article-buffer)
2925         (make-local-variable 'gnus-article-current)
2926         (make-local-variable 'gnus-original-article-buffer))
2927       (setq gnus-newsgroup-name group)
2928       ;; Set any local variables in the group parameters.
2929       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2930       t)))
2931
2932 (defun gnus-set-global-variables ()
2933   "Set the global equivalents of the buffer-local variables.
2934 They are set to the latest values they had.  These reflect the summary
2935 buffer that was in action when the last article was fetched."
2936   (when (eq major-mode 'gnus-summary-mode)
2937     (setq gnus-summary-buffer (current-buffer))
2938     (let ((name gnus-newsgroup-name)
2939           (marked gnus-newsgroup-marked)
2940           (unread gnus-newsgroup-unreads)
2941           (headers gnus-current-headers)
2942           (data gnus-newsgroup-data)
2943           (summary gnus-summary-buffer)
2944           (article-buffer gnus-article-buffer)
2945           (original gnus-original-article-buffer)
2946           (gac gnus-article-current)
2947           (reffed gnus-reffed-article-number)
2948           (score-file gnus-current-score-file)
2949           (default-charset gnus-newsgroup-charset)
2950           vlist)
2951       (let ((locals gnus-newsgroup-variables))
2952         (while locals
2953           (if (consp (car locals))
2954               (push (eval (caar locals)) vlist)
2955             (push (eval (car locals)) vlist))
2956           (setq locals (cdr locals)))
2957         (setq vlist (nreverse vlist)))
2958       (save-excursion
2959         (set-buffer gnus-group-buffer)
2960         (setq gnus-newsgroup-name name
2961               gnus-newsgroup-marked marked
2962               gnus-newsgroup-unreads unread
2963               gnus-current-headers headers
2964               gnus-newsgroup-data data
2965               gnus-article-current gac
2966               gnus-summary-buffer summary
2967               gnus-article-buffer article-buffer
2968               gnus-original-article-buffer original
2969               gnus-reffed-article-number reffed
2970               gnus-current-score-file score-file
2971               gnus-newsgroup-charset default-charset)
2972         (let ((locals gnus-newsgroup-variables))
2973           (while locals
2974             (if (consp (car locals))
2975                 (set (caar locals) (pop vlist))
2976               (set (car locals) (pop vlist)))
2977             (setq locals (cdr locals))))
2978         ;; The article buffer also has local variables.
2979         (when (gnus-buffer-live-p gnus-article-buffer)
2980           (set-buffer gnus-article-buffer)
2981           (setq gnus-summary-buffer summary))))))
2982
2983 (defun gnus-summary-article-unread-p (article)
2984   "Say whether ARTICLE is unread or not."
2985   (memq article gnus-newsgroup-unreads))
2986
2987 (defun gnus-summary-first-article-p (&optional article)
2988   "Return whether ARTICLE is the first article in the buffer."
2989   (if (not (setq article (or article (gnus-summary-article-number))))
2990       nil
2991     (eq article (caar gnus-newsgroup-data))))
2992
2993 (defun gnus-summary-last-article-p (&optional article)
2994   "Return whether ARTICLE is the last article in the buffer."
2995   (if (not (setq article (or article (gnus-summary-article-number))))
2996       ;; All non-existent numbers are the last article.  :-)
2997       t
2998     (not (cdr (gnus-data-find-list article)))))
2999
3000 (defun gnus-make-thread-indent-array ()
3001   (let ((n 200))
3002     (unless (and gnus-thread-indent-array
3003                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3004       (setq gnus-thread-indent-array (make-vector 201 "")
3005             gnus-thread-indent-array-level gnus-thread-indent-level)
3006       (while (>= n 0)
3007         (aset gnus-thread-indent-array n
3008               (make-string (* n gnus-thread-indent-level) ? ))
3009         (setq n (1- n))))))
3010
3011 (defun gnus-update-summary-mark-positions ()
3012   "Compute where the summary marks are to go."
3013   (save-excursion
3014     (when (gnus-buffer-exists-p gnus-summary-buffer)
3015       (set-buffer gnus-summary-buffer))
3016     (let ((gnus-replied-mark 129)
3017           (gnus-score-below-mark 130)
3018           (gnus-score-over-mark 130)
3019           (gnus-download-mark 131)
3020           (spec gnus-summary-line-format-spec)
3021           gnus-visual pos)
3022       (save-excursion
3023         (gnus-set-work-buffer)
3024         (let ((gnus-summary-line-format-spec spec)
3025               (gnus-newsgroup-downloadable '((0 . t))))
3026           (gnus-summary-insert-line
3027            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3028            0 nil 128 t nil "" nil 1)
3029           (goto-char (point-min))
3030           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3031                                              (- (point) 2)))))
3032           (goto-char (point-min))
3033           (push (cons 'replied (and (search-forward "\201" nil t)
3034                                     (- (point) 2)))
3035                 pos)
3036           (goto-char (point-min))
3037           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
3038                 pos)
3039           (goto-char (point-min))
3040           (push (cons 'download
3041                       (and (search-forward "\203" nil t) (- (point) 2)))
3042                 pos)))
3043       (setq gnus-summary-mark-positions pos))))
3044
3045 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3046   "Insert a dummy root in the summary buffer."
3047   (beginning-of-line)
3048   (gnus-add-text-properties
3049    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3050    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3051
3052 (defun gnus-summary-extract-address-component (from)
3053   (or (car (funcall gnus-extract-address-components from))
3054       from))
3055
3056 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3057   (let ((mail-parse-charset gnus-newsgroup-charset)
3058         ; Is it really necessary to do this next part for each summary line?
3059         ; Luckily, doesn't seem to slow things down much.
3060         (mail-parse-ignored-charsets
3061          (save-excursion (set-buffer gnus-summary-buffer)
3062                          gnus-newsgroup-ignored-charsets)))
3063     (or
3064      (and gnus-ignored-from-addresses
3065           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3066           (let ((extra-headers (mail-header-extra header))
3067                 to
3068                 newsgroups)
3069             (cond
3070              ((setq to (cdr (assq 'To extra-headers)))
3071               (concat "-> "
3072                       (inline
3073                         (gnus-summary-extract-address-component
3074                          (funcall gnus-decode-encoded-word-function to)))))
3075              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3076               (concat "=> " newsgroups)))))
3077      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3078
3079 (defun gnus-summary-insert-line (gnus-tmp-header
3080                                  gnus-tmp-level gnus-tmp-current
3081                                  gnus-tmp-unread gnus-tmp-replied
3082                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3083                                  &optional gnus-tmp-dummy gnus-tmp-score
3084                                  gnus-tmp-process)
3085   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3086          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3087          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3088          (gnus-tmp-score-char
3089           (if (or (null gnus-summary-default-score)
3090                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3091                       gnus-summary-zcore-fuzz))
3092               ?                         ;Whitespace
3093             (if (< gnus-tmp-score gnus-summary-default-score)
3094                 gnus-score-below-mark gnus-score-over-mark)))
3095          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3096          (gnus-tmp-replied
3097           (cond (gnus-tmp-process gnus-process-mark)
3098                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3099                  gnus-cached-mark)
3100                 (gnus-tmp-replied gnus-replied-mark)
3101                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3102                  gnus-forwarded-mark)
3103                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3104                  gnus-saved-mark)
3105                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3106                  gnus-recent-mark)
3107                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3108                  gnus-unseen-mark)
3109                 (t gnus-no-mark)))
3110          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3111          (gnus-tmp-name
3112           (cond
3113            ((string-match "<[^>]+> *$" gnus-tmp-from)
3114             (let ((beg (match-beginning 0)))
3115               (or (and (string-match "^\".+\"" gnus-tmp-from)
3116                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3117                   (substring gnus-tmp-from 0 beg))))
3118            ((string-match "(.+)" gnus-tmp-from)
3119             (substring gnus-tmp-from
3120                        (1+ (match-beginning 0)) (1- (match-end 0))))
3121            (t gnus-tmp-from)))
3122          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3123          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3124          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3125          (buffer-read-only nil))
3126     (when (string= gnus-tmp-name "")
3127       (setq gnus-tmp-name gnus-tmp-from))
3128     (unless (numberp gnus-tmp-lines)
3129       (setq gnus-tmp-lines -1))
3130     (if (= gnus-tmp-lines -1)
3131         (setq gnus-tmp-lines "?")
3132       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3133     (gnus-put-text-property
3134      (point)
3135      (progn (eval gnus-summary-line-format-spec) (point))
3136      'gnus-number gnus-tmp-number)
3137     (when (gnus-visual-p 'summary-highlight 'highlight)
3138       (forward-line -1)
3139       (gnus-run-hooks 'gnus-summary-update-hook)
3140       (forward-line 1))))
3141
3142 (defun gnus-summary-update-line (&optional dont-update)
3143   "Update summary line after change."
3144   (when (and gnus-summary-default-score
3145              (not gnus-summary-inhibit-highlight))
3146     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3147            (article (gnus-summary-article-number))
3148            (score (gnus-summary-article-score article)))
3149       (unless dont-update
3150         (if (and gnus-summary-mark-below
3151                  (< (gnus-summary-article-score)
3152                     gnus-summary-mark-below))
3153             ;; This article has a low score, so we mark it as read.
3154             (when (memq article gnus-newsgroup-unreads)
3155               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3156           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3157             ;; This article was previously marked as read on account
3158             ;; of a low score, but now it has risen, so we mark it as
3159             ;; unread.
3160             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3161         (gnus-summary-update-mark
3162          (if (or (null gnus-summary-default-score)
3163                  (<= (abs (- score gnus-summary-default-score))
3164                      gnus-summary-zcore-fuzz))
3165              ?                          ;Whitespace
3166            (if (< score gnus-summary-default-score)
3167                gnus-score-below-mark gnus-score-over-mark))
3168          'score))
3169       ;; Do visual highlighting.
3170       (when (gnus-visual-p 'summary-highlight 'highlight)
3171         (gnus-run-hooks 'gnus-summary-update-hook)))))
3172
3173 (defvar gnus-tmp-new-adopts nil)
3174
3175 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3176   "Return the number of articles in THREAD.
3177 This may be 0 in some cases -- if none of the articles in
3178 the thread are to be displayed."
3179   (let* ((number
3180          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3181           (cond
3182            ((not (listp thread))
3183             1)
3184            ((and (consp thread) (cdr thread))
3185             (apply
3186              '+ 1 (mapcar
3187                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3188            ((null thread)
3189             1)
3190            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3191             1)
3192            (t 0))))
3193     (when (and level (zerop level) gnus-tmp-new-adopts)
3194       (incf number
3195             (apply '+ (mapcar
3196                        'gnus-summary-number-of-articles-in-thread
3197                        gnus-tmp-new-adopts))))
3198     (if char
3199         (if (> number 1) gnus-not-empty-thread-mark
3200           gnus-empty-thread-mark)
3201       number)))
3202
3203 (defun gnus-summary-set-local-parameters (group)
3204   "Go through the local params of GROUP and set all variable specs in that list."
3205   (let ((params (gnus-group-find-parameter group))
3206         (vars '(quit-config))           ; Ignore quit-config.
3207         elem)
3208     (while params
3209       (setq elem (car params)
3210             params (cdr params))
3211       (and (consp elem)                 ; Has to be a cons.
3212            (consp (cdr elem))           ; The cdr has to be a list.
3213            (symbolp (car elem))         ; Has to be a symbol in there.
3214            (not (memq (car elem) vars))
3215            (ignore-errors               ; So we set it.
3216              (push (car elem) vars)
3217              (make-local-variable (car elem))
3218              (set (car elem) (eval (nth 1 elem))))))))
3219
3220 (defun gnus-summary-read-group (group &optional show-all no-article
3221                                       kill-buffer no-display backward
3222                                       select-articles)
3223   "Start reading news in newsgroup GROUP.
3224 If SHOW-ALL is non-nil, already read articles are also listed.
3225 If NO-ARTICLE is non-nil, no article is selected initially.
3226 If NO-DISPLAY, don't generate a summary buffer."
3227   (let (result)
3228     (while (and group
3229                 (null (setq result
3230                             (let ((gnus-auto-select-next nil))
3231                               (or (gnus-summary-read-group-1
3232                                    group show-all no-article
3233                                    kill-buffer no-display
3234                                    select-articles)
3235                                   (setq show-all nil
3236                                         select-articles nil)))))
3237                 (eq gnus-auto-select-next 'quietly))
3238       (set-buffer gnus-group-buffer)
3239       ;; The entry function called above goes to the next
3240       ;; group automatically, so we go two groups back
3241       ;; if we are searching for the previous group.
3242       (when backward
3243         (gnus-group-prev-unread-group 2))
3244       (if (not (equal group (gnus-group-group-name)))
3245           (setq group (gnus-group-group-name))
3246         (setq group nil)))
3247     result))
3248
3249 (defun gnus-summary-read-group-1 (group show-all no-article
3250                                         kill-buffer no-display
3251                                         &optional select-articles)
3252   ;; Killed foreign groups can't be entered.
3253   ;;  (when (and (not (gnus-group-native-p group))
3254   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3255   ;;    (error "Dead non-native groups can't be entered"))
3256   (gnus-message 5 "Retrieving newsgroup: %s..."
3257                 (gnus-group-decoded-name group))
3258   (let* ((new-group (gnus-summary-setup-buffer group))
3259          (quit-config (gnus-group-quit-config group))
3260          (did-select (and new-group (gnus-select-newsgroup
3261                                      group show-all select-articles))))
3262     (cond
3263      ;; This summary buffer exists already, so we just select it.
3264      ((not new-group)
3265       (gnus-set-global-variables)
3266       (when kill-buffer
3267         (gnus-kill-or-deaden-summary kill-buffer))
3268       (gnus-configure-windows 'summary 'force)
3269       (gnus-set-mode-line 'summary)
3270       (gnus-summary-position-point)
3271       (message "")
3272       t)
3273      ;; We couldn't select this group.
3274      ((null did-select)
3275       (when (and (eq major-mode 'gnus-summary-mode)
3276                  (not (equal (current-buffer) kill-buffer)))
3277         (kill-buffer (current-buffer))
3278         (if (not quit-config)
3279             (progn
3280               ;; Update the info -- marks might need to be removed,
3281               ;; for instance.
3282               (gnus-summary-update-info)
3283               (set-buffer gnus-group-buffer)
3284               (gnus-group-jump-to-group group)
3285               (gnus-group-next-unread-group 1))
3286           (gnus-handle-ephemeral-exit quit-config)))
3287       (let ((grpinfo (gnus-get-info group)))
3288         (if (null (gnus-info-read grpinfo))
3289             (gnus-message 3 "Group %s contains no messages"
3290                           (gnus-group-decoded-name group))
3291           (gnus-message 3 "Can't select group")))
3292       nil)
3293      ;; The user did a `C-g' while prompting for number of articles,
3294      ;; so we exit this group.
3295      ((eq did-select 'quit)
3296       (and (eq major-mode 'gnus-summary-mode)
3297            (not (equal (current-buffer) kill-buffer))
3298            (kill-buffer (current-buffer)))
3299       (when kill-buffer
3300         (gnus-kill-or-deaden-summary kill-buffer))
3301       (if (not quit-config)
3302           (progn
3303             (set-buffer gnus-group-buffer)
3304             (gnus-group-jump-to-group group)
3305             (gnus-group-next-unread-group 1)
3306             (gnus-configure-windows 'group 'force))
3307         (gnus-handle-ephemeral-exit quit-config))
3308       ;; Finally signal the quit.
3309       (signal 'quit nil))
3310      ;; The group was successfully selected.
3311      (t
3312       (gnus-set-global-variables)
3313       ;; Save the active value in effect when the group was entered.
3314       (setq gnus-newsgroup-active
3315             (gnus-copy-sequence
3316              (gnus-active gnus-newsgroup-name)))
3317       ;; You can change the summary buffer in some way with this hook.
3318       (gnus-run-hooks 'gnus-select-group-hook)
3319       (gnus-update-format-specifications
3320        nil 'summary 'summary-mode 'summary-dummy)
3321       (gnus-update-summary-mark-positions)
3322       ;; Do score processing.
3323       (when gnus-use-scoring
3324         (gnus-possibly-score-headers))
3325       ;; Check whether to fill in the gaps in the threads.
3326       (when gnus-build-sparse-threads
3327         (gnus-build-sparse-threads))
3328       ;; Find the initial limit.
3329       (if gnus-show-threads
3330           (if show-all
3331               (let ((gnus-newsgroup-dormant nil))
3332                 (gnus-summary-initial-limit show-all))
3333             (gnus-summary-initial-limit show-all))
3334         ;; When untreaded, all articles are always shown.
3335         (setq gnus-newsgroup-limit
3336               (mapcar
3337                (lambda (header) (mail-header-number header))
3338                gnus-newsgroup-headers)))
3339       ;; Generate the summary buffer.
3340       (unless no-display
3341         (gnus-summary-prepare))
3342       (when gnus-use-trees
3343         (gnus-tree-open group)
3344         (setq gnus-summary-highlight-line-function
3345               'gnus-tree-highlight-article))
3346       ;; If the summary buffer is empty, but there are some low-scored
3347       ;; articles or some excluded dormants, we include these in the
3348       ;; buffer.
3349       (when (and (zerop (buffer-size))
3350                  (not no-display))
3351         (cond (gnus-newsgroup-dormant
3352                (gnus-summary-limit-include-dormant))
3353               ((and gnus-newsgroup-scored show-all)
3354                (gnus-summary-limit-include-expunged t))))
3355       ;; Function `gnus-apply-kill-file' must be called in this hook.
3356       (gnus-run-hooks 'gnus-apply-kill-hook)
3357       (if (and (zerop (buffer-size))
3358                (not no-display))
3359           (progn
3360             ;; This newsgroup is empty.
3361             (gnus-summary-catchup-and-exit nil t)
3362             (gnus-message 6 "No unread news")
3363             (when kill-buffer
3364               (gnus-kill-or-deaden-summary kill-buffer))
3365             ;; Return nil from this function.
3366             nil)
3367         ;; Hide conversation thread subtrees.  We cannot do this in
3368         ;; gnus-summary-prepare-hook since kill processing may not
3369         ;; work with hidden articles.
3370         (gnus-summary-maybe-hide-threads)
3371         (when kill-buffer
3372           (gnus-kill-or-deaden-summary kill-buffer))
3373         (gnus-summary-auto-select-subject)
3374         ;; Show first unread article if requested.
3375         (if (and (not no-article)
3376                  (not no-display)
3377                  gnus-newsgroup-unreads
3378                  gnus-auto-select-first)
3379             (progn
3380               (gnus-configure-windows 'summary)
3381               (let ((art (gnus-summary-article-number)))
3382                 (unless (or (memq art gnus-newsgroup-undownloaded)
3383                             (memq art gnus-newsgroup-downloadable))
3384                   (gnus-summary-goto-article art))))
3385           ;; Don't select any articles.
3386           (gnus-summary-position-point)
3387           (gnus-configure-windows 'summary 'force)
3388           (gnus-set-mode-line 'summary))
3389         (when (get-buffer-window gnus-group-buffer t)
3390           ;; Gotta use windows, because recenter does weird stuff if
3391           ;; the current buffer ain't the displayed window.
3392           (let ((owin (selected-window)))
3393             (select-window (get-buffer-window gnus-group-buffer t))
3394             (when (gnus-group-goto-group group)
3395               (recenter))
3396             (select-window owin)))
3397         ;; Mark this buffer as "prepared".
3398         (setq gnus-newsgroup-prepared t)
3399         (gnus-run-hooks 'gnus-summary-prepared-hook)
3400         t)))))
3401
3402 (defun gnus-summary-auto-select-subject ()
3403   "Select the subject line on initial group entry."
3404   (goto-char (point-min))
3405   (cond
3406    ((eq gnus-auto-select-subject 'best)
3407     (gnus-summary-best-unread-subject))
3408    ((eq gnus-auto-select-subject 'unread)
3409     (gnus-summary-first-unread-subject))
3410    ((eq gnus-auto-select-subject 'unseen)
3411     (gnus-summary-first-unseen-subject))
3412    ((eq gnus-auto-select-subject 'unseen-or-unread)
3413     (gnus-summary-first-unseen-or-unread-subject))
3414    ((eq gnus-auto-select-subject 'first)
3415     ;; Do nothing.
3416     )
3417    ((gnus-functionp gnus-auto-select-subject)
3418     (funcall gnus-auto-select-subject))))
3419
3420 (defun gnus-summary-prepare ()
3421   "Generate the summary buffer."
3422   (interactive)
3423   (let ((buffer-read-only nil))
3424     (erase-buffer)
3425     (setq gnus-newsgroup-data nil
3426           gnus-newsgroup-data-reverse nil)
3427     (gnus-run-hooks 'gnus-summary-generate-hook)
3428     ;; Generate the buffer, either with threads or without.
3429     (when gnus-newsgroup-headers
3430       (gnus-summary-prepare-threads
3431        (if gnus-show-threads
3432            (gnus-sort-gathered-threads
3433             (funcall gnus-summary-thread-gathering-function
3434                      (gnus-sort-threads
3435                       (gnus-cut-threads (gnus-make-threads)))))
3436          ;; Unthreaded display.
3437          (gnus-sort-articles gnus-newsgroup-headers))))
3438     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3439     ;; Call hooks for modifying summary buffer.
3440     (goto-char (point-min))
3441     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3442
3443 (defsubst gnus-general-simplify-subject (subject)
3444   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3445   (setq subject
3446         (cond
3447          ;; Truncate the subject.
3448          (gnus-simplify-subject-functions
3449           (gnus-map-function gnus-simplify-subject-functions subject))
3450          ((numberp gnus-summary-gather-subject-limit)
3451           (setq subject (gnus-simplify-subject-re subject))
3452           (if (> (length subject) gnus-summary-gather-subject-limit)
3453               (substring subject 0 gnus-summary-gather-subject-limit)
3454             subject))
3455          ;; Fuzzily simplify it.
3456          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3457           (gnus-simplify-subject-fuzzy subject))
3458          ;; Just remove the leading "Re:".
3459          (t
3460           (gnus-simplify-subject-re subject))))
3461
3462   (if (and gnus-summary-gather-exclude-subject
3463            (string-match gnus-summary-gather-exclude-subject subject))
3464       nil                         ; This article shouldn't be gathered
3465     subject))
3466
3467 (defun gnus-summary-simplify-subject-query ()
3468   "Query where the respool algorithm would put this article."
3469   (interactive)
3470   (gnus-summary-select-article)
3471   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3472
3473 (defun gnus-gather-threads-by-subject (threads)
3474   "Gather threads by looking at Subject headers."
3475   (if (not gnus-summary-make-false-root)
3476       threads
3477     (let ((hashtb (gnus-make-hashtable 1024))
3478           (prev threads)
3479           (result threads)
3480           subject hthread whole-subject)
3481       (while threads
3482         (setq subject (gnus-general-simplify-subject
3483                        (setq whole-subject (mail-header-subject
3484                                             (caar threads)))))
3485         (when subject
3486           (if (setq hthread (gnus-gethash subject hashtb))
3487               (progn
3488                 ;; We enter a dummy root into the thread, if we
3489                 ;; haven't done that already.
3490                 (unless (stringp (caar hthread))
3491                   (setcar hthread (list whole-subject (car hthread))))
3492                 ;; We add this new gathered thread to this gathered
3493                 ;; thread.
3494                 (setcdr (car hthread)
3495                         (nconc (cdar hthread) (list (car threads))))
3496                 ;; Remove it from the list of threads.
3497                 (setcdr prev (cdr threads))
3498                 (setq threads prev))
3499             ;; Enter this thread into the hash table.
3500             (gnus-sethash subject threads hashtb)))
3501         (setq prev threads)
3502         (setq threads (cdr threads)))
3503       result)))
3504
3505 (defun gnus-gather-threads-by-references (threads)
3506   "Gather threads by looking at References headers."
3507   (let ((idhashtb (gnus-make-hashtable 1024))
3508         (thhashtb (gnus-make-hashtable 1024))
3509         (prev threads)
3510         (result threads)
3511         ids references id gthread gid entered ref)
3512     (while threads
3513       (when (setq references (mail-header-references (caar threads)))
3514         (setq id (mail-header-id (caar threads))
3515               ids (inline (gnus-split-references references))
3516               entered nil)
3517         (while (setq ref (pop ids))
3518           (setq ids (delete ref ids))
3519           (if (not (setq gid (gnus-gethash ref idhashtb)))
3520               (progn
3521                 (gnus-sethash ref id idhashtb)
3522                 (gnus-sethash id threads thhashtb))
3523             (setq gthread (gnus-gethash gid thhashtb))
3524             (unless entered
3525               ;; We enter a dummy root into the thread, if we
3526               ;; haven't done that already.
3527               (unless (stringp (caar gthread))
3528                 (setcar gthread (list (mail-header-subject (caar gthread))
3529                                       (car gthread))))
3530               ;; We add this new gathered thread to this gathered
3531               ;; thread.
3532               (setcdr (car gthread)
3533                       (nconc (cdar gthread) (list (car threads)))))
3534             ;; Add it into the thread hash table.
3535             (gnus-sethash id gthread thhashtb)
3536             (setq entered t)
3537             ;; Remove it from the list of threads.
3538             (setcdr prev (cdr threads))
3539             (setq threads prev))))
3540       (setq prev threads)
3541       (setq threads (cdr threads)))
3542     result))
3543
3544 (defun gnus-sort-gathered-threads (threads)
3545   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3546   (let ((result threads))
3547     (while threads
3548       (when (stringp (caar threads))
3549         (setcdr (car threads)
3550                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3551       (setq threads (cdr threads)))
3552     result))
3553
3554 (defun gnus-thread-loop-p (root thread)
3555   "Say whether ROOT is in THREAD."
3556   (let ((stack (list thread))
3557         (infloop 0)
3558         th)
3559     (while (setq thread (pop stack))
3560       (setq th (cdr thread))
3561       (while (and th
3562                   (not (eq (caar th) root)))
3563         (pop th))
3564       (if th
3565           ;; We have found a loop.
3566           (let (ref-dep)
3567             (setcdr thread (delq (car th) (cdr thread)))
3568             (if (boundp (setq ref-dep (intern "none"
3569                                               gnus-newsgroup-dependencies)))
3570                 (setcdr (symbol-value ref-dep)
3571                         (nconc (cdr (symbol-value ref-dep))
3572                                (list (car th))))
3573               (set ref-dep (list nil (car th))))
3574             (setq infloop 1
3575                   stack nil))
3576         ;; Push all the subthreads onto the stack.
3577         (push (cdr thread) stack)))
3578     infloop))
3579
3580 (defun gnus-make-threads ()
3581   "Go through the dependency hashtb and find the roots.  Return all threads."
3582   (let (threads)
3583     (while (catch 'infloop
3584              (mapatoms
3585               (lambda (refs)
3586                 ;; Deal with self-referencing References loops.
3587                 (when (and (car (symbol-value refs))
3588                            (not (zerop
3589                                  (apply
3590                                   '+
3591                                   (mapcar
3592                                    (lambda (thread)
3593                                      (gnus-thread-loop-p
3594                                       (car (symbol-value refs)) thread))
3595                                    (cdr (symbol-value refs)))))))
3596                   (setq threads nil)
3597                   (throw 'infloop t))
3598                 (unless (car (symbol-value refs))
3599                   ;; These threads do not refer back to any other
3600                   ;; articles, so they're roots.
3601                   (setq threads (append (cdr (symbol-value refs)) threads))))
3602               gnus-newsgroup-dependencies)))
3603     threads))
3604
3605 ;; Build the thread tree.
3606 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3607   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3608
3609 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3610 if it was already present.
3611
3612 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3613 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3614 Message-IDs will be renamed to a unique Message-ID before being
3615 entered.
3616
3617 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3618   (let* ((id (mail-header-id header))
3619          (id-dep (and id (intern id dependencies)))
3620          ref ref-dep ref-header replaced)
3621     ;; Enter this `header' in the `dependencies' table.
3622     (cond
3623      ((not id-dep)
3624       (setq header nil))
3625      ;; The first two cases do the normal part: enter a new `header'
3626      ;; in the `dependencies' table.
3627      ((not (boundp id-dep))
3628       (set id-dep (list header)))
3629      ((null (car (symbol-value id-dep)))
3630       (setcar (symbol-value id-dep) header))
3631
3632      ;; From here the `header' was already present in the
3633      ;; `dependencies' table.
3634      (force-new
3635       ;; Overrides an existing entry;
3636       ;; just set the header part of the entry.
3637       (setcar (symbol-value id-dep) header)
3638       (setq replaced t))
3639
3640      ;; Renames the existing `header' to a unique Message-ID.
3641      ((not gnus-summary-ignore-duplicates)
3642       ;; An article with this Message-ID has already been seen.
3643       ;; We rename the Message-ID.
3644       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3645            (list header))
3646       (mail-header-set-id header id))
3647
3648      ;; The last case ignores an existing entry, except it adds any
3649      ;; additional Xrefs (in case the two articles came from different
3650      ;; servers.
3651      ;; Also sets `header' to `nil' meaning that the `dependencies'
3652      ;; table was *not* modified.
3653      (t
3654       (mail-header-set-xref
3655        (car (symbol-value id-dep))
3656        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3657                    "")
3658                (or (mail-header-xref header) "")))
3659       (setq header nil)))
3660
3661     (when (and header (not replaced))
3662       ;; First check that we are not creating a References loop.
3663       (setq ref (gnus-parent-id (mail-header-references header)))
3664       (while (and ref
3665                   (setq ref-dep (intern-soft ref dependencies))
3666                   (boundp ref-dep)
3667                   (setq ref-header (car (symbol-value ref-dep))))
3668         (if (string= id ref)
3669             ;; Yuk!  This is a reference loop.  Make the article be a
3670             ;; root article.
3671             (progn
3672               (mail-header-set-references (car (symbol-value id-dep)) "none")
3673               (setq ref nil))
3674           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3675       (setq ref (gnus-parent-id (mail-header-references header)))
3676       (setq ref-dep (intern (or ref "none") dependencies))
3677       (if (boundp ref-dep)
3678           (setcdr (symbol-value ref-dep)
3679                   (nconc (cdr (symbol-value ref-dep))
3680                          (list (symbol-value id-dep))))
3681         (set ref-dep (list nil (symbol-value id-dep)))))
3682     header))
3683
3684 (defun gnus-extract-message-id-from-in-reply-to (string)
3685   (if (string-match "<[^>]+>" string)
3686       (substring string (match-beginning 0) (match-end 0))
3687     nil))
3688
3689 (defun gnus-build-sparse-threads ()
3690   (let ((headers gnus-newsgroup-headers)
3691         (mail-parse-charset gnus-newsgroup-charset)
3692         (gnus-summary-ignore-duplicates t)
3693         header references generation relations
3694         subject child end new-child date)
3695     ;; First we create an alist of generations/relations, where
3696     ;; generations is how much we trust the relation, and the relation
3697     ;; is parent/child.
3698     (gnus-message 7 "Making sparse threads...")
3699     (save-excursion
3700       (nnheader-set-temp-buffer " *gnus sparse threads*")
3701       (while (setq header (pop headers))
3702         (when (and (setq references (mail-header-references header))
3703                    (not (string= references "")))
3704           (insert references)
3705           (setq child (mail-header-id header)
3706                 subject (mail-header-subject header)
3707                 date (mail-header-date header)
3708                 generation 0)
3709           (while (search-backward ">" nil t)
3710             (setq end (1+ (point)))
3711             (when (search-backward "<" nil t)
3712               (setq new-child (buffer-substring (point) end))
3713               (push (list (incf generation)
3714                           child (setq child new-child)
3715                           subject date)
3716                     relations)))
3717           (when child
3718             (push (list (1+ generation) child nil subject) relations))
3719           (erase-buffer)))
3720       (kill-buffer (current-buffer)))
3721     ;; Sort over trustworthiness.
3722     (mapcar
3723      (lambda (relation)
3724        (when (gnus-dependencies-add-header
3725               (make-full-mail-header
3726                gnus-reffed-article-number
3727                (nth 3 relation) "" (or (nth 4 relation) "")
3728                (nth 1 relation)
3729                (or (nth 2 relation) "") 0 0 "")
3730               gnus-newsgroup-dependencies nil)
3731          (push gnus-reffed-article-number gnus-newsgroup-limit)
3732          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3733          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3734                gnus-newsgroup-reads)
3735          (decf gnus-reffed-article-number)))
3736      (sort relations 'car-less-than-car))
3737     (gnus-message 7 "Making sparse threads...done")))
3738
3739 (defun gnus-build-old-threads ()
3740   ;; Look at all the articles that refer back to old articles, and
3741   ;; fetch the headers for the articles that aren't there.  This will
3742   ;; build complete threads - if the roots haven't been expired by the
3743   ;; server, that is.
3744   (let ((mail-parse-charset gnus-newsgroup-charset)
3745         id heads)
3746     (mapatoms
3747      (lambda (refs)
3748        (when (not (car (symbol-value refs)))
3749          (setq heads (cdr (symbol-value refs)))
3750          (while heads
3751            (if (memq (mail-header-number (caar heads))
3752                      gnus-newsgroup-dormant)
3753                (setq heads (cdr heads))
3754              (setq id (symbol-name refs))
3755              (while (and (setq id (gnus-build-get-header id))
3756                          (not (car (gnus-id-to-thread id)))))
3757              (setq heads nil)))))
3758      gnus-newsgroup-dependencies)))
3759
3760 ;; This function has to be called with point after the article number
3761 ;; on the beginning of the line.
3762 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3763   (let ((eol (gnus-point-at-eol))
3764         (buffer (current-buffer))
3765         header references in-reply-to)
3766
3767     ;; overview: [num subject from date id refs chars lines misc]
3768     (unwind-protect
3769         (progn
3770           (narrow-to-region (point) eol)
3771           (unless (eobp)
3772             (forward-char))
3773
3774           (setq header
3775                 (make-full-mail-header
3776                  number                 ; number
3777                  (funcall gnus-decode-encoded-word-function
3778                           (nnheader-nov-field)) ; subject
3779                  (funcall gnus-decode-encoded-word-function
3780                           (nnheader-nov-field)) ; from
3781                  (nnheader-nov-field)   ; date
3782                  (nnheader-nov-read-message-id) ; id
3783                  (setq references (nnheader-nov-field)) ; refs
3784                  (nnheader-nov-read-integer) ; chars
3785                  (nnheader-nov-read-integer) ; lines
3786                  (unless (eobp)
3787                    (if (looking-at "Xref: ")
3788                        (goto-char (match-end 0)))
3789                    (nnheader-nov-field)) ; Xref
3790                  (nnheader-nov-parse-extra)))) ; extra
3791
3792       (widen))
3793
3794     (when (and (string= references "")
3795                (setq in-reply-to (mail-header-extra header))
3796                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3797       (mail-header-set-references
3798        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3799
3800     (when gnus-alter-header-function
3801       (funcall gnus-alter-header-function header))
3802     (gnus-dependencies-add-header header dependencies force-new)))
3803
3804 (defun gnus-build-get-header (id)
3805   "Look through the buffer of NOV lines and find the header to ID.
3806 Enter this line into the dependencies hash table, and return
3807 the id of the parent article (if any)."
3808   (let ((deps gnus-newsgroup-dependencies)
3809         found header)
3810     (prog1
3811         (save-excursion
3812           (set-buffer nntp-server-buffer)
3813           (let ((case-fold-search nil))
3814             (goto-char (point-min))
3815             (while (and (not found)
3816                         (search-forward id nil t))
3817               (beginning-of-line)
3818               (setq found (looking-at
3819                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3820                                    (regexp-quote id))))
3821               (or found (beginning-of-line 2)))
3822             (when found
3823               (beginning-of-line)
3824               (and
3825                (setq header (gnus-nov-parse-line
3826                              (read (current-buffer)) deps))
3827                (gnus-parent-id (mail-header-references header))))))
3828       (when header
3829         (let ((number (mail-header-number header)))
3830           (push number gnus-newsgroup-limit)
3831           (push header gnus-newsgroup-headers)
3832           (if (memq number gnus-newsgroup-unselected)
3833               (progn
3834                 (setq gnus-newsgroup-unreads
3835                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3836                                                number))
3837                 (setq gnus-newsgroup-unselected
3838                       (delq number gnus-newsgroup-unselected)))
3839             (push number gnus-newsgroup-ancient)))))))
3840
3841 (defun gnus-build-all-threads ()
3842   "Read all the headers."
3843   (let ((gnus-summary-ignore-duplicates t)
3844         (mail-parse-charset gnus-newsgroup-charset)
3845         (dependencies gnus-newsgroup-dependencies)
3846         header article)
3847     (save-excursion
3848       (set-buffer nntp-server-buffer)
3849       (let ((case-fold-search nil))
3850         (goto-char (point-min))
3851         (while (not (eobp))
3852           (ignore-errors
3853             (setq article (read (current-buffer))
3854                   header (gnus-nov-parse-line article dependencies)))
3855           (when header
3856             (save-excursion
3857               (set-buffer gnus-summary-buffer)
3858               (push header gnus-newsgroup-headers)
3859               (if (memq (setq article (mail-header-number header))
3860                         gnus-newsgroup-unselected)
3861                   (progn
3862                     (setq gnus-newsgroup-unreads
3863                           (gnus-add-to-sorted-list
3864                            gnus-newsgroup-unreads article))
3865                     (setq gnus-newsgroup-unselected
3866                           (delq article gnus-newsgroup-unselected)))
3867                 (push article gnus-newsgroup-ancient)))
3868             (forward-line 1)))))))
3869
3870 (defun gnus-summary-update-article-line (article header)
3871   "Update the line for ARTICLE using HEADERS."
3872   (let* ((id (mail-header-id header))
3873          (thread (gnus-id-to-thread id)))
3874     (unless thread
3875       (error "Article in no thread"))
3876     ;; Update the thread.
3877     (setcar thread header)
3878     (gnus-summary-goto-subject article)
3879     (let* ((datal (gnus-data-find-list article))
3880            (data (car datal))
3881            (length (when (cdr datal)
3882                      (- (gnus-data-pos data)
3883                         (gnus-data-pos (cadr datal)))))
3884            (buffer-read-only nil)
3885            (level (gnus-summary-thread-level)))
3886       (gnus-delete-line)
3887       (gnus-summary-insert-line
3888        header level nil (gnus-article-mark article)
3889        (memq article gnus-newsgroup-replied)
3890        (memq article gnus-newsgroup-expirable)
3891        ;; Only insert the Subject string when it's different
3892        ;; from the previous Subject string.
3893        (if (and
3894             gnus-show-threads
3895             (gnus-subject-equal
3896              (condition-case ()
3897                  (mail-header-subject
3898                   (gnus-data-header
3899                    (cadr
3900                     (gnus-data-find-list
3901                      article
3902                      (gnus-data-list t)))))
3903                ;; Error on the side of excessive subjects.
3904                (error ""))
3905              (mail-header-subject header)))
3906            ""
3907          (mail-header-subject header))
3908        nil (cdr (assq article gnus-newsgroup-scored))
3909        (memq article gnus-newsgroup-processable))
3910       (when length
3911         (gnus-data-update-list
3912          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3913
3914 (defun gnus-summary-update-article (article &optional iheader)
3915   "Update ARTICLE in the summary buffer."
3916   (set-buffer gnus-summary-buffer)
3917   (let* ((header (gnus-summary-article-header article))
3918          (id (mail-header-id header))
3919          (data (gnus-data-find article))
3920          (thread (gnus-id-to-thread id))
3921          (references (mail-header-references header))
3922          (parent
3923           (gnus-id-to-thread
3924            (or (gnus-parent-id
3925                 (when (and references
3926                            (not (equal "" references)))
3927                   references))
3928                "none")))
3929          (buffer-read-only nil)
3930          (old (car thread)))
3931     (when thread
3932       (unless iheader
3933         (setcar thread nil)
3934         (when parent
3935           (delq thread parent)))
3936       (if (gnus-summary-insert-subject id header)
3937        ;; Set the (possibly) new article number in the data structure.
3938           (gnus-data-set-number data (gnus-id-to-article id))
3939         (setcar thread old)
3940         nil))))
3941
3942 (defun gnus-rebuild-thread (id &optional line)
3943   "Rebuild the thread containing ID.
3944 If LINE, insert the rebuilt thread starting on line LINE."
3945   (let ((buffer-read-only nil)
3946         old-pos current thread data)
3947     (if (not gnus-show-threads)
3948         (setq thread (list (car (gnus-id-to-thread id))))
3949       ;; Get the thread this article is part of.
3950       (setq thread (gnus-remove-thread id)))
3951     (setq old-pos (gnus-point-at-bol))
3952     (setq current (save-excursion
3953                     (and (re-search-backward "[\r\n]" nil t)
3954                          (gnus-summary-article-number))))
3955     ;; If this is a gathered thread, we have to go some re-gathering.
3956     (when (stringp (car thread))
3957       (let ((subject (car thread))
3958             roots thr)
3959         (setq thread (cdr thread))
3960         (while thread
3961           (unless (memq (setq thr (gnus-id-to-thread
3962                                    (gnus-root-id
3963                                     (mail-header-id (caar thread)))))
3964                         roots)
3965             (push thr roots))
3966           (setq thread (cdr thread)))
3967         ;; We now have all (unique) roots.
3968         (if (= (length roots) 1)
3969             ;; All the loose roots are now one solid root.
3970             (setq thread (car roots))
3971           (setq thread (cons subject (gnus-sort-threads roots))))))
3972     (let (threads)
3973       ;; We then insert this thread into the summary buffer.
3974       (when line
3975         (goto-char (point-min))
3976         (forward-line (1- line)))
3977       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3978         (if gnus-show-threads
3979             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3980           (gnus-summary-prepare-unthreaded thread))
3981         (setq data (nreverse gnus-newsgroup-data))
3982         (setq threads gnus-newsgroup-threads))
3983       ;; We splice the new data into the data structure.
3984       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3985       ;;!!! then we want to insert at the beginning of the buffer.
3986       ;;!!! That happens to be true with Gnus now, but that may
3987       ;;!!! change in the future.  Perhaps.
3988       (gnus-data-enter-list
3989        (if line nil current) data (- (point) old-pos))
3990       (setq gnus-newsgroup-threads
3991             (nconc threads gnus-newsgroup-threads))
3992       (gnus-data-compute-positions))))
3993
3994 (defun gnus-number-to-header (number)
3995   "Return the header for article NUMBER."
3996   (let ((headers gnus-newsgroup-headers))
3997     (while (and headers
3998                 (not (= number (mail-header-number (car headers)))))
3999       (pop headers))
4000     (when headers
4001       (car headers))))
4002
4003 (defun gnus-parent-headers (in-headers &optional generation)
4004   "Return the headers of the GENERATIONeth parent of HEADERS."
4005   (unless generation
4006     (setq generation 1))
4007   (let ((parent t)
4008         (headers in-headers)
4009         references)
4010     (while (and parent
4011                 (not (zerop generation))
4012                 (setq references (mail-header-references headers)))
4013       (setq headers (if (and references
4014                              (setq parent (gnus-parent-id references)))
4015                         (car (gnus-id-to-thread parent))
4016                       nil))
4017       (decf generation))
4018     (and (not (eq headers in-headers))
4019          headers)))
4020
4021 (defun gnus-id-to-thread (id)
4022   "Return the (sub-)thread where ID appears."
4023   (gnus-gethash id gnus-newsgroup-dependencies))
4024
4025 (defun gnus-id-to-article (id)
4026   "Return the article number of ID."
4027   (let ((thread (gnus-id-to-thread id)))
4028     (when (and thread
4029                (car thread))
4030       (mail-header-number (car thread)))))
4031
4032 (defun gnus-id-to-header (id)
4033   "Return the article headers of ID."
4034   (car (gnus-id-to-thread id)))
4035
4036 (defun gnus-article-displayed-root-p (article)
4037   "Say whether ARTICLE is a root(ish) article."
4038   (let ((level (gnus-summary-thread-level article))
4039         (refs (mail-header-references  (gnus-summary-article-header article)))
4040         particle)
4041     (cond
4042      ((null level) nil)
4043      ((zerop level) t)
4044      ((null refs) t)
4045      ((null (gnus-parent-id refs)) t)
4046      ((and (= 1 level)
4047            (null (setq particle (gnus-id-to-article
4048                                  (gnus-parent-id refs))))
4049            (null (gnus-summary-thread-level particle)))))))
4050
4051 (defun gnus-root-id (id)
4052   "Return the id of the root of the thread where ID appears."
4053   (let (last-id prev)
4054     (while (and id (setq prev (car (gnus-id-to-thread id))))
4055       (setq last-id id
4056             id (gnus-parent-id (mail-header-references prev))))
4057     last-id))
4058
4059 (defun gnus-articles-in-thread (thread)
4060   "Return the list of articles in THREAD."
4061   (cons (mail-header-number (car thread))
4062         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4063
4064 (defun gnus-remove-thread (id &optional dont-remove)
4065   "Remove the thread that has ID in it."
4066   (let (headers thread last-id)
4067     ;; First go up in this thread until we find the root.
4068     (setq last-id (gnus-root-id id)
4069           headers (message-flatten-list (gnus-id-to-thread last-id)))
4070     ;; We have now found the real root of this thread.  It might have
4071     ;; been gathered into some loose thread, so we have to search
4072     ;; through the threads to find the thread we wanted.
4073     (let ((threads gnus-newsgroup-threads)
4074           sub)
4075       (while threads
4076         (setq sub (car threads))
4077         (if (stringp (car sub))
4078             ;; This is a gathered thread, so we look at the roots
4079             ;; below it to find whether this article is in this
4080             ;; gathered root.
4081             (progn
4082               (setq sub (cdr sub))
4083               (while sub
4084                 (when (member (caar sub) headers)
4085                   (setq thread (car threads)
4086                         threads nil
4087                         sub nil))
4088                 (setq sub (cdr sub))))
4089           ;; It's an ordinary thread, so we check it.
4090           (when (eq (car sub) (car headers))
4091             (setq thread sub
4092                   threads nil)))
4093         (setq threads (cdr threads)))
4094       ;; If this article is in no thread, then it's a root.
4095       (if thread
4096           (unless dont-remove
4097             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4098         (setq thread (gnus-id-to-thread last-id)))
4099       (when thread
4100         (prog1
4101             thread                      ; We return this thread.
4102           (unless dont-remove
4103             (if (stringp (car thread))
4104                 (progn
4105                   ;; If we use dummy roots, then we have to remove the
4106                   ;; dummy root as well.
4107                   (when (eq gnus-summary-make-false-root 'dummy)
4108                     ;; We go to the dummy root by going to
4109                     ;; the first sub-"thread", and then one line up.
4110                     (gnus-summary-goto-article
4111                      (mail-header-number (caadr thread)))
4112                     (forward-line -1)
4113                     (gnus-delete-line)
4114                     (gnus-data-compute-positions))
4115                   (setq thread (cdr thread))
4116                   (while thread
4117                     (gnus-remove-thread-1 (car thread))
4118                     (setq thread (cdr thread))))
4119               (gnus-remove-thread-1 thread))))))))
4120
4121 (defun gnus-remove-thread-1 (thread)
4122   "Remove the thread THREAD recursively."
4123   (let ((number (mail-header-number (pop thread)))
4124         d)
4125     (setq thread (reverse thread))
4126     (while thread
4127       (gnus-remove-thread-1 (pop thread)))
4128     (when (setq d (gnus-data-find number))
4129       (goto-char (gnus-data-pos d))
4130       (gnus-summary-show-thread)
4131       (gnus-data-remove
4132        number
4133        (- (gnus-point-at-bol)
4134           (prog1
4135               (1+ (gnus-point-at-eol))
4136             (gnus-delete-line)))))))
4137
4138 (defun gnus-sort-threads-1 (threads func)
4139   (sort (mapcar (lambda (thread)
4140                   (cons (car thread)
4141                         (and (cdr thread)
4142                              (gnus-sort-threads-1 (cdr thread) func))))
4143                 threads) func))
4144
4145 (defun gnus-sort-threads (threads)
4146   "Sort THREADS."
4147   (if (not gnus-thread-sort-functions)
4148       threads
4149     (gnus-message 8 "Sorting threads...")
4150     (prog1
4151         (gnus-sort-threads-1
4152          threads
4153          (gnus-make-sort-function gnus-thread-sort-functions))
4154       (gnus-message 8 "Sorting threads...done"))))
4155
4156 (defun gnus-sort-articles (articles)
4157   "Sort ARTICLES."
4158   (when gnus-article-sort-functions
4159     (gnus-message 7 "Sorting articles...")
4160     (prog1
4161         (setq gnus-newsgroup-headers
4162               (sort articles (gnus-make-sort-function
4163                               gnus-article-sort-functions)))
4164       (gnus-message 7 "Sorting articles...done"))))
4165
4166 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4167 (defmacro gnus-thread-header (thread)
4168   "Return header of first article in THREAD.
4169 Note that THREAD must never, ever be anything else than a variable -
4170 using some other form will lead to serious barfage."
4171   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4172   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4173   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4174         (vector thread) 2))
4175
4176 (defsubst gnus-article-sort-by-number (h1 h2)
4177   "Sort articles by article number."
4178   (< (mail-header-number h1)
4179      (mail-header-number h2)))
4180
4181 (defun gnus-thread-sort-by-number (h1 h2)
4182   "Sort threads by root article number."
4183   (gnus-article-sort-by-number
4184    (gnus-thread-header h1) (gnus-thread-header h2)))
4185
4186 (defsubst gnus-article-sort-by-lines (h1 h2)
4187   "Sort articles by article Lines header."
4188   (< (mail-header-lines h1)
4189      (mail-header-lines h2)))
4190
4191 (defun gnus-thread-sort-by-lines (h1 h2)
4192   "Sort threads by root article Lines header."
4193   (gnus-article-sort-by-lines
4194    (gnus-thread-header h1) (gnus-thread-header h2)))
4195
4196 (defsubst gnus-article-sort-by-chars (h1 h2)
4197   "Sort articles by octet length."
4198   (< (mail-header-chars h1)
4199      (mail-header-chars h2)))
4200
4201 (defun gnus-thread-sort-by-chars (h1 h2)
4202   "Sort threads by root article octet length."
4203   (gnus-article-sort-by-chars
4204    (gnus-thread-header h1) (gnus-thread-header h2)))
4205
4206 (defsubst gnus-article-sort-by-author (h1 h2)
4207   "Sort articles by root author."
4208   (string-lessp
4209    (let ((extract (funcall
4210                    gnus-extract-address-components
4211                    (mail-header-from h1))))
4212      (or (car extract) (cadr extract) ""))
4213    (let ((extract (funcall
4214                    gnus-extract-address-components
4215                    (mail-header-from h2))))
4216      (or (car extract) (cadr extract) ""))))
4217
4218 (defun gnus-thread-sort-by-author (h1 h2)
4219   "Sort threads by root author."
4220   (gnus-article-sort-by-author
4221    (gnus-thread-header h1)  (gnus-thread-header h2)))
4222
4223 (defsubst gnus-article-sort-by-subject (h1 h2)
4224   "Sort articles by root subject."
4225   (string-lessp
4226    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4227    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4228
4229 (defun gnus-thread-sort-by-subject (h1 h2)
4230   "Sort threads by root subject."
4231   (gnus-article-sort-by-subject
4232    (gnus-thread-header h1) (gnus-thread-header h2)))
4233
4234 (defsubst gnus-article-sort-by-date (h1 h2)
4235   "Sort articles by root article date."
4236   (time-less-p
4237    (gnus-date-get-time (mail-header-date h1))
4238    (gnus-date-get-time (mail-header-date h2))))
4239
4240 (defun gnus-thread-sort-by-date (h1 h2)
4241   "Sort threads by root article date."
4242   (gnus-article-sort-by-date
4243    (gnus-thread-header h1) (gnus-thread-header h2)))
4244
4245 (defsubst gnus-article-sort-by-score (h1 h2)
4246   "Sort articles by root article score.
4247 Unscored articles will be counted as having a score of zero."
4248   (> (or (cdr (assq (mail-header-number h1)
4249                     gnus-newsgroup-scored))
4250          gnus-summary-default-score 0)
4251      (or (cdr (assq (mail-header-number h2)
4252                     gnus-newsgroup-scored))
4253          gnus-summary-default-score 0)))
4254
4255 (defun gnus-thread-sort-by-score (h1 h2)
4256   "Sort threads by root article score."
4257   (gnus-article-sort-by-score
4258    (gnus-thread-header h1) (gnus-thread-header h2)))
4259
4260 (defun gnus-thread-sort-by-total-score (h1 h2)
4261   "Sort threads by the sum of all scores in the thread.
4262 Unscored articles will be counted as having a score of zero."
4263   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4264
4265 (defun gnus-thread-total-score (thread)
4266   ;; This function find the total score of THREAD.
4267   (cond
4268    ((null thread)
4269     0)
4270    ((consp thread)
4271     (if (stringp (car thread))
4272         (apply gnus-thread-score-function 0
4273                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4274       (gnus-thread-total-score-1 thread)))
4275    (t
4276     (gnus-thread-total-score-1 (list thread)))))
4277
4278 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4279   "Sort threads such that the thread with the most recently arrived article comes first."
4280   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4281
4282 (defun gnus-thread-highest-number (thread)
4283   "Return the highest article number in THREAD."
4284   (apply 'max (mapcar (lambda (header)
4285                         (mail-header-number header))
4286                       (message-flatten-list thread))))
4287
4288 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4289   "Sort threads such that the thread with the most recently dated article comes first."
4290   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4291
4292 (defun gnus-thread-latest-date (thread)
4293   "Return the highest article date in THREAD."
4294   (let ((previous-time 0))
4295     (apply 'max (mapcar
4296                  (lambda (header)
4297                    (setq previous-time
4298                          (time-to-seconds
4299                           (mail-header-parse-date
4300                            (condition-case ()
4301                                (mail-header-date header)
4302                              (error previous-time))))))
4303                  (sort
4304                   (message-flatten-list thread)
4305                   (lambda (h1 h2)
4306                     (< (mail-header-number h1)
4307                        (mail-header-number h2))))))))
4308
4309 (defun gnus-thread-total-score-1 (root)
4310   ;; This function find the total score of the thread below ROOT.
4311   (setq root (car root))
4312   (apply gnus-thread-score-function
4313          (or (append
4314               (mapcar 'gnus-thread-total-score
4315                       (cdr (gnus-id-to-thread (mail-header-id root))))
4316               (when (> (mail-header-number root) 0)
4317                 (list (or (cdr (assq (mail-header-number root)
4318                                      gnus-newsgroup-scored))
4319                           gnus-summary-default-score 0))))
4320              (list gnus-summary-default-score)
4321              '(0))))
4322
4323 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4324 (defvar gnus-tmp-prev-subject nil)
4325 (defvar gnus-tmp-false-parent nil)
4326 (defvar gnus-tmp-root-expunged nil)
4327 (defvar gnus-tmp-dummy-line nil)
4328
4329 (eval-when-compile (defvar gnus-tmp-header))
4330 (defun gnus-extra-header (type &optional header)
4331   "Return the extra header of TYPE."
4332   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4333       ""))
4334
4335 (defvar gnus-tmp-thread-tree-header-string "")
4336
4337 (defcustom gnus-sum-thread-tree-root "> "
4338   "With %B spec, used for the root of a thread.
4339 If nil, use subject instead."
4340   :type 'string
4341   :group 'gnus-thread)
4342 (defcustom gnus-sum-thread-tree-single-indent ""
4343   "With %B spec, used for a thread with just one message.
4344 If nil, use subject instead."
4345   :type 'string
4346   :group 'gnus-thread)
4347 (defcustom gnus-sum-thread-tree-vertical "| "
4348   "With %B spec, used for drawing a vertical line."
4349   :type 'string
4350   :group 'gnus-thread)
4351 (defcustom gnus-sum-thread-tree-indent "  "
4352   "With %B spec, used for indenting."
4353   :type 'string
4354   :group 'gnus-thread)
4355 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4356   "With %B spec, used for a leaf with brothers."
4357   :type 'string
4358   :group 'gnus-thread)
4359 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4360   "With %B spec, used for a leaf without brothers."
4361   :type 'string
4362   :group 'gnus-thread)
4363
4364 (defun gnus-summary-prepare-threads (threads)
4365   "Prepare summary buffer from THREADS and indentation LEVEL.
4366 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4367 or a straight list of headers."
4368   (gnus-message 7 "Generating summary...")
4369
4370   (setq gnus-newsgroup-threads threads)
4371   (beginning-of-line)
4372
4373   (let ((gnus-tmp-level 0)
4374         (default-score (or gnus-summary-default-score 0))
4375         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4376         thread number subject stack state gnus-tmp-gathered beg-match
4377         new-roots gnus-tmp-new-adopts thread-end
4378         gnus-tmp-header gnus-tmp-unread
4379         gnus-tmp-replied gnus-tmp-subject-or-nil
4380         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4381         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4382         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4383         tree-stack)
4384
4385     (setq gnus-tmp-prev-subject nil)
4386
4387     (if (vectorp (car threads))
4388         ;; If this is a straight (sic) list of headers, then a
4389         ;; threaded summary display isn't required, so we just create
4390         ;; an unthreaded one.
4391         (gnus-summary-prepare-unthreaded threads)
4392
4393       ;; Do the threaded display.
4394
4395       (while (or threads stack gnus-tmp-new-adopts new-roots)
4396
4397         (if (and (= gnus-tmp-level 0)
4398                  (or (not stack)
4399                      (= (caar stack) 0))
4400                  (not gnus-tmp-false-parent)
4401                  (or gnus-tmp-new-adopts new-roots))
4402             (if gnus-tmp-new-adopts
4403                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4404                       thread (list (car gnus-tmp-new-adopts))
4405                       gnus-tmp-header (caar thread)
4406                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4407               (when new-roots
4408                 (setq thread (list (car new-roots))
4409                       gnus-tmp-header (caar thread)
4410                       new-roots (cdr new-roots))))
4411
4412           (if threads
4413               ;; If there are some threads, we do them before the
4414               ;; threads on the stack.
4415               (setq thread threads
4416                     gnus-tmp-header (caar thread))
4417             ;; There were no current threads, so we pop something off
4418             ;; the stack.
4419             (setq state (car stack)
4420                   gnus-tmp-level (car state)
4421                   tree-stack (cadr state)
4422                   thread (caddr state)
4423                   stack (cdr stack)
4424                   gnus-tmp-header (caar thread))))
4425
4426         (setq gnus-tmp-false-parent nil)
4427         (setq gnus-tmp-root-expunged nil)
4428         (setq thread-end nil)
4429
4430         (if (stringp gnus-tmp-header)
4431             ;; The header is a dummy root.
4432             (cond
4433              ((eq gnus-summary-make-false-root 'adopt)
4434               ;; We let the first article adopt the rest.
4435               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4436                                                (cddar thread)))
4437               (setq gnus-tmp-gathered
4438                     (nconc (mapcar
4439                             (lambda (h) (mail-header-number (car h)))
4440                             (cddar thread))
4441                            gnus-tmp-gathered))
4442               (setq thread (cons (list (caar thread)
4443                                        (cadar thread))
4444                                  (cdr thread)))
4445               (setq gnus-tmp-level -1
4446                     gnus-tmp-false-parent t))
4447              ((eq gnus-summary-make-false-root 'empty)
4448               ;; We print adopted articles with empty subject fields.
4449               (setq gnus-tmp-gathered
4450                     (nconc (mapcar
4451                             (lambda (h) (mail-header-number (car h)))
4452                             (cddar thread))
4453                            gnus-tmp-gathered))
4454               (setq gnus-tmp-level -1))
4455              ((eq gnus-summary-make-false-root 'dummy)
4456               ;; We remember that we probably want to output a dummy
4457               ;; root.
4458               (setq gnus-tmp-dummy-line gnus-tmp-header)
4459               (setq gnus-tmp-prev-subject gnus-tmp-header))
4460              (t
4461               ;; We do not make a root for the gathered
4462               ;; sub-threads at all.
4463               (setq gnus-tmp-level -1)))
4464
4465           (setq number (mail-header-number gnus-tmp-header)
4466                 subject (mail-header-subject gnus-tmp-header))
4467
4468           (cond
4469            ;; If the thread has changed subject, we might want to make
4470            ;; this subthread into a root.
4471            ((and (null gnus-thread-ignore-subject)
4472                  (not (zerop gnus-tmp-level))
4473                  gnus-tmp-prev-subject
4474                  (not (inline
4475                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4476             (setq new-roots (nconc new-roots (list (car thread)))
4477                   thread-end t
4478                   gnus-tmp-header nil))
4479            ;; If the article lies outside the current limit,
4480            ;; then we do not display it.
4481            ((not (memq number gnus-newsgroup-limit))
4482             (setq gnus-tmp-gathered
4483                   (nconc (mapcar
4484                           (lambda (h) (mail-header-number (car h)))
4485                           (cdar thread))
4486                          gnus-tmp-gathered))
4487             (setq gnus-tmp-new-adopts (if (cdar thread)
4488                                           (append gnus-tmp-new-adopts
4489                                                   (cdar thread))
4490                                         gnus-tmp-new-adopts)
4491                   thread-end t
4492                   gnus-tmp-header nil)
4493             (when (zerop gnus-tmp-level)
4494               (setq gnus-tmp-root-expunged t)))
4495            ;; Perhaps this article is to be marked as read?
4496            ((and gnus-summary-mark-below
4497                  (< (or (cdr (assq number gnus-newsgroup-scored))
4498                         default-score)
4499                     gnus-summary-mark-below)
4500                  ;; Don't touch sparse articles.
4501                  (not (gnus-summary-article-sparse-p number))
4502                  (not (gnus-summary-article-ancient-p number)))
4503             (setq gnus-newsgroup-unreads
4504                   (delq number gnus-newsgroup-unreads))
4505             (if gnus-newsgroup-auto-expire
4506                 (setq gnus-newsgroup-expirable
4507                       (gnus-add-to-sorted-list
4508                        gnus-newsgroup-expirable number))
4509               (push (cons number gnus-low-score-mark)
4510                     gnus-newsgroup-reads))))
4511
4512           (when gnus-tmp-header
4513             ;; We may have an old dummy line to output before this
4514             ;; article.
4515             (when (and gnus-tmp-dummy-line
4516                        (gnus-subject-equal
4517                         gnus-tmp-dummy-line
4518                         (mail-header-subject gnus-tmp-header)))
4519               (gnus-summary-insert-dummy-line
4520                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4521               (setq gnus-tmp-dummy-line nil))
4522
4523             ;; Compute the mark.
4524             (setq gnus-tmp-unread (gnus-article-mark number))
4525
4526             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4527                                   gnus-tmp-header gnus-tmp-level)
4528                   gnus-newsgroup-data)
4529
4530             ;; Actually insert the line.
4531             (setq
4532              gnus-tmp-subject-or-nil
4533              (cond
4534               ((and gnus-thread-ignore-subject
4535                     gnus-tmp-prev-subject
4536                     (not (inline (gnus-subject-equal
4537                                   gnus-tmp-prev-subject subject))))
4538                subject)
4539               ((zerop gnus-tmp-level)
4540                (if (and (eq gnus-summary-make-false-root 'empty)
4541                         (memq number gnus-tmp-gathered)
4542                         gnus-tmp-prev-subject
4543                         (inline (gnus-subject-equal
4544                                  gnus-tmp-prev-subject subject)))
4545                    gnus-summary-same-subject
4546                  subject))
4547               (t gnus-summary-same-subject)))
4548             (if (and (eq gnus-summary-make-false-root 'adopt)
4549                      (= gnus-tmp-level 1)
4550                      (memq number gnus-tmp-gathered))
4551                 (setq gnus-tmp-opening-bracket ?\<
4552                       gnus-tmp-closing-bracket ?\>)
4553               (setq gnus-tmp-opening-bracket ?\[
4554                     gnus-tmp-closing-bracket ?\]))
4555             (setq
4556              gnus-tmp-indentation
4557              (aref gnus-thread-indent-array gnus-tmp-level)
4558              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4559              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4560                                 gnus-summary-default-score 0)
4561              gnus-tmp-score-char
4562              (if (or (null gnus-summary-default-score)
4563                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4564                          gnus-summary-zcore-fuzz))
4565                  ?                      ;Whitespace
4566                (if (< gnus-tmp-score gnus-summary-default-score)
4567                    gnus-score-below-mark gnus-score-over-mark))
4568              gnus-tmp-replied
4569              (cond ((memq number gnus-newsgroup-processable)
4570                     gnus-process-mark)
4571                    ((memq number gnus-newsgroup-cached)
4572                     gnus-cached-mark)
4573                    ((memq number gnus-newsgroup-replied)
4574                     gnus-replied-mark)
4575                    ((memq number gnus-newsgroup-forwarded)
4576                     gnus-forwarded-mark)
4577                    ((memq number gnus-newsgroup-saved)
4578                     gnus-saved-mark)
4579                    ((memq number gnus-newsgroup-recent)
4580                     gnus-recent-mark)
4581                    ((memq number gnus-newsgroup-unseen)
4582                     gnus-unseen-mark)
4583                    (t gnus-no-mark))
4584              gnus-tmp-from (mail-header-from gnus-tmp-header)
4585              gnus-tmp-name
4586              (cond
4587               ((string-match "<[^>]+> *$" gnus-tmp-from)
4588                (setq beg-match (match-beginning 0))
4589                (or (and (string-match "^\".+\"" gnus-tmp-from)
4590                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4591                    (substring gnus-tmp-from 0 beg-match)))
4592               ((string-match "(.+)" gnus-tmp-from)
4593                (substring gnus-tmp-from
4594                           (1+ (match-beginning 0)) (1- (match-end 0))))
4595               (t gnus-tmp-from))
4596              gnus-tmp-thread-tree-header-string
4597              (cond
4598               ((not gnus-show-threads) "")
4599               ((zerop gnus-tmp-level)
4600                (if (cdar thread)
4601                    (or gnus-sum-thread-tree-root subject)
4602                  (or gnus-sum-thread-tree-single-indent subject)))
4603               (t
4604                (concat (apply 'concat
4605                               (mapcar (lambda (item)
4606                                         (if (= item 1)
4607                                             gnus-sum-thread-tree-vertical
4608                                           gnus-sum-thread-tree-indent))
4609                                       (cdr (reverse tree-stack))))
4610                        (if (nth 1 thread)
4611                            gnus-sum-thread-tree-leaf-with-other
4612                          gnus-sum-thread-tree-single-leaf)))))
4613             (when (string= gnus-tmp-name "")
4614               (setq gnus-tmp-name gnus-tmp-from))
4615             (unless (numberp gnus-tmp-lines)
4616               (setq gnus-tmp-lines -1))
4617             (if (= gnus-tmp-lines -1)
4618                 (setq gnus-tmp-lines "?")
4619               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4620             (gnus-put-text-property
4621              (point)
4622              (progn (eval gnus-summary-line-format-spec) (point))
4623              'gnus-number number)
4624             (when gnus-visual-p
4625               (forward-line -1)
4626               (gnus-run-hooks 'gnus-summary-update-hook)
4627               (forward-line 1))
4628
4629             (setq gnus-tmp-prev-subject subject)))
4630
4631         (when (nth 1 thread)
4632           (push (list (max 0 gnus-tmp-level)
4633                       (copy-list tree-stack)
4634                       (nthcdr 1 thread))
4635                 stack))
4636         (push (if (nth 1 thread) 1 0) tree-stack)
4637         (incf gnus-tmp-level)
4638         (setq threads (if thread-end nil (cdar thread)))
4639         (unless threads
4640           (setq gnus-tmp-level 0)))))
4641   (gnus-message 7 "Generating summary...done"))
4642
4643 (defun gnus-summary-prepare-unthreaded (headers)
4644   "Generate an unthreaded summary buffer based on HEADERS."
4645   (let (header number mark)
4646
4647     (beginning-of-line)
4648
4649     (while headers
4650       ;; We may have to root out some bad articles...
4651       (when (memq (setq number (mail-header-number
4652                                 (setq header (pop headers))))
4653                   gnus-newsgroup-limit)
4654         ;; Mark article as read when it has a low score.
4655         (when (and gnus-summary-mark-below
4656                    (< (or (cdr (assq number gnus-newsgroup-scored))
4657                           gnus-summary-default-score 0)
4658                       gnus-summary-mark-below)
4659                    (not (gnus-summary-article-ancient-p number)))
4660           (setq gnus-newsgroup-unreads
4661                 (delq number gnus-newsgroup-unreads))
4662           (if gnus-newsgroup-auto-expire
4663               (push number gnus-newsgroup-expirable)
4664             (push (cons number gnus-low-score-mark)
4665                   gnus-newsgroup-reads)))
4666
4667         (setq mark (gnus-article-mark number))
4668         (push (gnus-data-make number mark (1+ (point)) header 0)
4669               gnus-newsgroup-data)
4670         (gnus-summary-insert-line
4671          header 0 number
4672          mark (memq number gnus-newsgroup-replied)
4673          (memq number gnus-newsgroup-expirable)
4674          (mail-header-subject header) nil
4675          (cdr (assq number gnus-newsgroup-scored))
4676          (memq number gnus-newsgroup-processable))))))
4677
4678 (defun gnus-summary-remove-list-identifiers ()
4679   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4680   (let ((regexp (if (consp gnus-list-identifiers)
4681                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4682                   gnus-list-identifiers))
4683         changed subject)
4684     (when regexp
4685       (dolist (header gnus-newsgroup-headers)
4686         (setq subject (mail-header-subject header)
4687               changed nil)
4688         (while (string-match
4689                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4690                 subject)
4691           (setq subject
4692                 (concat (substring subject 0 (match-beginning 2))
4693                         (substring subject (match-end 0)))
4694                 changed t))
4695         (when (and changed
4696                    (string-match
4697                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4698           (setq subject
4699                 (concat (substring subject 0 (match-beginning 1))
4700                         (substring subject (match-end 1)))))
4701         (when changed
4702           (mail-header-set-subject header subject))))))
4703
4704 (defun gnus-fetch-headers (articles)
4705   "Fetch headers of ARTICLES."
4706   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4707     (gnus-message 5 "Fetching headers for %s..." name)
4708     (prog1
4709         (if (eq 'nov
4710                 (setq gnus-headers-retrieved-by
4711                       (gnus-retrieve-headers
4712                        articles gnus-newsgroup-name
4713                        ;; We might want to fetch old headers, but
4714                        ;; not if there is only 1 article.
4715                        (and (or (and
4716                                  (not (eq gnus-fetch-old-headers 'some))
4717                                  (not (numberp gnus-fetch-old-headers)))
4718                                 (> (length articles) 1))
4719                             gnus-fetch-old-headers))))
4720             (gnus-get-newsgroup-headers-xover
4721              articles nil nil gnus-newsgroup-name t)
4722           (gnus-get-newsgroup-headers))
4723       (gnus-message 5 "Fetching headers for %s...done" name))))
4724
4725 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4726   "Select newsgroup GROUP.
4727 If READ-ALL is non-nil, all articles in the group are selected.
4728 If SELECT-ARTICLES, only select those articles from GROUP."
4729   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4730          ;;!!! Dirty hack; should be removed.
4731          (gnus-summary-ignore-duplicates
4732           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4733               t
4734             gnus-summary-ignore-duplicates))
4735          (info (nth 2 entry))
4736          articles fetched-articles cached)
4737
4738     (unless (gnus-check-server
4739              (set (make-local-variable 'gnus-current-select-method)
4740                   (gnus-find-method-for-group group)))
4741       (error "Couldn't open server"))
4742
4743     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4744         (gnus-activate-group group)     ; Or we can activate it...
4745         (progn                          ; Or we bug out.
4746           (when (equal major-mode 'gnus-summary-mode)
4747             (kill-buffer (current-buffer)))
4748           (error "Couldn't activate group %s: %s"
4749                  group (gnus-status-message group))))
4750
4751     (unless (gnus-request-group group t)
4752       (when (equal major-mode 'gnus-summary-mode)
4753         (kill-buffer (current-buffer)))
4754       (error "Couldn't request group %s: %s"
4755              group (gnus-status-message group)))
4756
4757     (setq gnus-newsgroup-name group
4758           gnus-newsgroup-unselected nil
4759           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4760
4761     (let ((display (gnus-group-find-parameter group 'display)))
4762       (setq gnus-newsgroup-display
4763             (cond
4764              ((not (zerop (or (car-safe read-all) 0)))
4765               ;; The user entered the group with C-u SPC/RET, let's show
4766               ;; all articles.
4767               'gnus-not-ignore)
4768              ((eq display 'all)
4769               'gnus-not-ignore)
4770              ((arrayp display)
4771               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4772              ((numberp display)
4773               ;; The following is probably the "correct" solution, but
4774               ;; it makes Gnus fetch all headers and then limit the
4775               ;; articles (which is slow), so instead we hack the
4776               ;; select-articles parameter instead. -- Simon Josefsson
4777               ;; <jas@kth.se>
4778               ;;
4779               ;; (gnus-byte-compile
4780               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4781               ;;                         display)))))
4782               (setq select-articles
4783                     (gnus-uncompress-range
4784                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4785                              (if (> tmp 0)
4786                                  tmp
4787                                1))
4788                            (cdr (gnus-active group)))))
4789               nil)
4790              (t
4791               nil))))
4792
4793     (gnus-summary-setup-default-charset)
4794
4795     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4796     (when (gnus-virtual-group-p group)
4797       (setq cached gnus-newsgroup-cached))
4798
4799     (setq gnus-newsgroup-unreads
4800           (gnus-sorted-ndifference
4801            (gnus-sorted-ndifference gnus-newsgroup-unreads
4802                                     gnus-newsgroup-marked)
4803            gnus-newsgroup-dormant))
4804
4805     (setq gnus-newsgroup-processable nil)
4806
4807     (gnus-update-read-articles group gnus-newsgroup-unreads)
4808
4809     ;; Adjust and set lists of article marks.
4810     (when info
4811       (gnus-adjust-marked-articles info))
4812
4813     (if (setq articles select-articles)
4814         (setq gnus-newsgroup-unselected
4815               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4816       (setq articles (gnus-articles-to-read group read-all)))
4817
4818     (cond
4819      ((null articles)
4820       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4821       'quit)
4822      ((eq articles 0) nil)
4823      (t
4824       ;; Init the dependencies hash table.
4825       (setq gnus-newsgroup-dependencies
4826             (gnus-make-hashtable (length articles)))
4827       (gnus-set-global-variables)
4828       ;; Retrieve the headers and read them in.
4829       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4830
4831       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4832       (when cached
4833         (setq gnus-newsgroup-cached cached))
4834
4835       ;; Suppress duplicates?
4836       (when gnus-suppress-duplicates
4837         (gnus-dup-suppress-articles))
4838
4839       ;; Set the initial limit.
4840       (setq gnus-newsgroup-limit (copy-sequence articles))
4841       ;; Remove canceled articles from the list of unread articles.
4842       (setq fetched-articles
4843             (mapcar (lambda (headers) (mail-header-number headers))
4844                     gnus-newsgroup-headers))
4845       (setq gnus-newsgroup-articles fetched-articles)
4846       (setq gnus-newsgroup-unreads
4847             (gnus-sorted-nintersection
4848              gnus-newsgroup-unreads fetched-articles))
4849       (gnus-compute-unseen-list)
4850
4851       ;; Removed marked articles that do not exist.
4852       (gnus-update-missing-marks
4853        (gnus-sorted-difference articles fetched-articles))
4854       ;; We might want to build some more threads first.
4855       (when (and gnus-fetch-old-headers
4856                  (eq gnus-headers-retrieved-by 'nov))
4857         (if (eq gnus-fetch-old-headers 'invisible)
4858             (gnus-build-all-threads)
4859           (gnus-build-old-threads)))
4860       ;; Let the Gnus agent mark articles as read.
4861       (when gnus-agent
4862         (gnus-agent-get-undownloaded-list))
4863       ;; Remove list identifiers from subject
4864       (when gnus-list-identifiers
4865         (gnus-summary-remove-list-identifiers))
4866       ;; Check whether auto-expire is to be done in this group.
4867       (setq gnus-newsgroup-auto-expire
4868             (gnus-group-auto-expirable-p group))
4869       ;; Set up the article buffer now, if necessary.
4870       (unless gnus-single-article-buffer
4871         (gnus-article-setup-buffer))
4872       ;; First and last article in this newsgroup.
4873       (when gnus-newsgroup-headers
4874         (setq gnus-newsgroup-begin
4875               (mail-header-number (car gnus-newsgroup-headers))
4876               gnus-newsgroup-end
4877               (mail-header-number
4878                (gnus-last-element gnus-newsgroup-headers))))
4879       ;; GROUP is successfully selected.
4880       (or gnus-newsgroup-headers t)))))
4881
4882 (defun gnus-compute-unseen-list ()
4883   ;; The `seen' marks are treated specially.
4884   (if (not gnus-newsgroup-seen)
4885       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4886     (setq gnus-newsgroup-unseen
4887           (gnus-inverse-list-range-intersection
4888            gnus-newsgroup-articles gnus-newsgroup-seen))))
4889
4890 (defun gnus-summary-display-make-predicate (display)
4891   (require 'gnus-agent)
4892   (when (= (length display) 1)
4893     (setq display (car display)))
4894   (unless gnus-summary-display-cache
4895     (dolist (elem (append '((unread . unread)
4896                             (read . read)
4897                             (unseen . unseen))
4898                           gnus-article-mark-lists))
4899       (push (cons (cdr elem)
4900                   (gnus-byte-compile
4901                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4902             gnus-summary-display-cache)))
4903   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4904         (gnus-category-predicate-cache gnus-summary-display-cache))
4905     (gnus-get-predicate display)))
4906
4907 ;; Uses the dynamically bound `number' variable.
4908 (defvar number)
4909 (defun gnus-article-marked-p (type &optional article)
4910   (let ((article (or article number)))
4911     (cond
4912      ((eq type 'tick)
4913       (memq article gnus-newsgroup-marked))
4914      ((eq type 'unsend)
4915       (memq article gnus-newsgroup-unsendable))
4916      ((eq type 'undownload)
4917       (memq article gnus-newsgroup-undownloaded))
4918      ((eq type 'download)
4919       (memq article gnus-newsgroup-downloadable))
4920      ((eq type 'unread)
4921       (memq article gnus-newsgroup-unreads))
4922      ((eq type 'read)
4923       (memq article gnus-newsgroup-reads))
4924      ((eq type 'dormant)
4925       (memq article gnus-newsgroup-dormant) )
4926      ((eq type 'expire)
4927       (memq article gnus-newsgroup-expirable))
4928      ((eq type 'reply)
4929       (memq article gnus-newsgroup-replied))
4930      ((eq type 'killed)
4931       (memq article gnus-newsgroup-killed))
4932      ((eq type 'bookmark)
4933       (assq article gnus-newsgroup-bookmarks))
4934      ((eq type 'score)
4935       (assq article gnus-newsgroup-scored))
4936      ((eq type 'save)
4937       (memq article gnus-newsgroup-saved))
4938      ((eq type 'cache)
4939       (memq article gnus-newsgroup-cached))
4940      ((eq type 'forward)
4941       (memq article gnus-newsgroup-forwarded))
4942      ((eq type 'seen)
4943       (not (memq article gnus-newsgroup-unseen)))
4944      ((eq type 'recent)
4945       (memq article gnus-newsgroup-recent))
4946      (t t))))
4947
4948 (defun gnus-articles-to-read (group &optional read-all)
4949   "Find out what articles the user wants to read."
4950   (let* ((articles
4951           ;; Select all articles if `read-all' is non-nil, or if there
4952           ;; are no unread articles.
4953           (if (or read-all
4954                   (and (zerop (length gnus-newsgroup-marked))
4955                        (zerop (length gnus-newsgroup-unreads)))
4956                   ;; Fetch all if the predicate is non-nil.
4957                   gnus-newsgroup-display)
4958               ;; We want to select the headers for all the articles in
4959               ;; the group, so we select either all the active
4960               ;; articles in the group, or (if that's nil), the
4961               ;; articles in the cache.
4962               (or
4963                (gnus-uncompress-range (gnus-active group))
4964                (gnus-cache-articles-in-group group))
4965             ;; Select only the "normal" subset of articles.
4966             (gnus-sorted-nunion
4967              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
4968              gnus-newsgroup-unreads)))
4969          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4970          (scored (length scored-list))
4971          (number (length articles))
4972          (marked (+ (length gnus-newsgroup-marked)
4973                     (length gnus-newsgroup-dormant)))
4974          (select
4975           (cond
4976            ((numberp read-all)
4977             read-all)
4978            ((numberp gnus-newsgroup-display)
4979             gnus-newsgroup-display)
4980            (t
4981             (condition-case ()
4982                 (cond
4983                  ((and (or (<= scored marked) (= scored number))
4984                        (numberp gnus-large-newsgroup)
4985                        (> number gnus-large-newsgroup))
4986                   (let* ((cursor-in-echo-area nil)
4987                          (initial (gnus-parameter-large-newsgroup-initial 
4988                                    gnus-newsgroup-name))
4989                          (input
4990                           (read-string
4991                            (format
4992                             "How many articles from %s (%s %d): "
4993                             (gnus-limit-string
4994                              (gnus-group-decoded-name gnus-newsgroup-name)
4995                              35)
4996                             (if initial "max" "default")
4997                             number)
4998                            (if initial
4999                                (cons (number-to-string initial)
5000                                      0)))))
5001                     (if (string-match "^[ \t]*$" input) number input)))
5002                  ((and (> scored marked) (< scored number)
5003                        (> (- scored number) 20))
5004                   (let ((input
5005                          (read-string
5006                           (format "%s %s (%d scored, %d total): "
5007                                   "How many articles from"
5008                                   (gnus-group-decoded-name group)
5009                                   scored number))))
5010                     (if (string-match "^[ \t]*$" input)
5011                         number input)))
5012                  (t number))
5013               (quit
5014                (message "Quit getting the articles to read")
5015                nil))))))
5016     (setq select (if (stringp select) (string-to-number select) select))
5017     (if (or (null select) (zerop select))
5018         select
5019       (if (and (not (zerop scored)) (<= (abs select) scored))
5020           (progn
5021             (setq articles (sort scored-list '<))
5022             (setq number (length articles)))
5023         (setq articles (copy-sequence articles)))
5024
5025       (when (< (abs select) number)
5026         (if (< select 0)
5027             ;; Select the N oldest articles.
5028             (setcdr (nthcdr (1- (abs select)) articles) nil)
5029           ;; Select the N most recent articles.
5030           (setq articles (nthcdr (- number select) articles))))
5031       (setq gnus-newsgroup-unselected
5032             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5033       (when gnus-alter-articles-to-read-function
5034         (setq gnus-newsgroup-unreads
5035               (sort
5036                (funcall gnus-alter-articles-to-read-function
5037                         gnus-newsgroup-name gnus-newsgroup-unreads)
5038                '<)))
5039       articles)))
5040
5041 (defun gnus-killed-articles (killed articles)
5042   (let (out)
5043     (while articles
5044       (when (inline (gnus-member-of-range (car articles) killed))
5045         (push (car articles) out))
5046       (setq articles (cdr articles)))
5047     out))
5048
5049 (defun gnus-uncompress-marks (marks)
5050   "Uncompress the mark ranges in MARKS."
5051   (let ((uncompressed '(score bookmark))
5052         out)
5053     (while marks
5054       (if (memq (caar marks) uncompressed)
5055           (push (car marks) out)
5056         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5057       (setq marks (cdr marks)))
5058     out))
5059
5060 (defun gnus-article-mark-to-type (mark)
5061   "Return the type of MARK."
5062   (or (cadr (assq mark gnus-article-special-mark-lists))
5063       'list))
5064
5065 (defun gnus-article-unpropagatable-p (mark)
5066   "Return whether MARK should be propagated to backend."
5067   (memq mark gnus-article-unpropagated-mark-lists))
5068
5069 (defun gnus-adjust-marked-articles (info)
5070   "Set all article lists and remove all marks that are no longer valid."
5071   (let* ((marked-lists (gnus-info-marks info))
5072          (active (gnus-active (gnus-info-group info)))
5073          (min (car active))
5074          (max (cdr active))
5075          (types gnus-article-mark-lists)
5076          marks var articles article mark mark-type)
5077
5078     (dolist (marks marked-lists)
5079       (setq mark (car marks)
5080             mark-type (gnus-article-mark-to-type mark)
5081             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5082
5083       ;; We set the variable according to the type of the marks list,
5084       ;; and then adjust the marks to a subset of the active articles.
5085       (cond
5086        ;; Adjust "simple" lists.
5087        ((eq mark-type 'list)
5088         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5089         (when (memq mark '(tick dormant expire reply save))
5090           (while articles
5091             (when (or (< (setq article (pop articles)) min) (> article max))
5092               (set var (delq article (symbol-value var)))))))
5093        ;; Adjust assocs.
5094        ((eq mark-type 'tuple)
5095         (set var (setq articles (cdr marks)))
5096         (when (not (listp (cdr (symbol-value var))))
5097           (set var (list (symbol-value var))))
5098         (when (not (listp (cdr articles)))
5099           (setq articles (list articles)))
5100         (while articles
5101           (when (or (not (consp (setq article (pop articles))))
5102                     (< (car article) min)
5103                     (> (car article) max))
5104             (set var (delq article (symbol-value var))))))
5105        ;; Adjust ranges (sloppily).
5106        ((eq mark-type 'range)
5107         (cond
5108          ((eq mark 'seen)
5109           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5110           ;; It should be (seen (NUM1 . NUM2)).
5111           (when (numberp (cddr marks))
5112             (setcdr marks (list (cdr marks))))
5113           (setq articles (cdr marks))
5114           (while (and articles
5115                       (or (and (consp (car articles))
5116                                (> min (cdar articles)))
5117                           (and (numberp (car articles))
5118                                (> min (car articles)))))
5119             (pop articles))
5120           (set var articles))))))))
5121
5122 (defun gnus-update-missing-marks (missing)
5123   "Go through the list of MISSING articles and remove them from the mark lists."
5124   (when missing
5125     (let (var m)
5126       ;; Go through all types.
5127       (dolist (elem gnus-article-mark-lists)
5128         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5129           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5130           (when (symbol-value var)
5131             ;; This list has articles.  So we delete all missing
5132             ;; articles from it.
5133             (setq m missing)
5134             (while m
5135               (set var (delq (pop m) (symbol-value var))))))))))
5136
5137 (defun gnus-update-marks ()
5138   "Enter the various lists of marked articles into the newsgroup info list."
5139   (let ((types gnus-article-mark-lists)
5140         (info (gnus-get-info gnus-newsgroup-name))
5141         type list newmarked symbol delta-marks)
5142     (when info
5143       ;; Add all marks lists to the list of marks lists.
5144       (while (setq type (pop types))
5145         (setq list (symbol-value
5146                     (setq symbol
5147                           (intern (format "gnus-newsgroup-%s" (car type))))))
5148
5149         (when list
5150           ;; Get rid of the entries of the articles that have the
5151           ;; default score.
5152           (when (and (eq (cdr type) 'score)
5153                      gnus-save-score
5154                      list)
5155             (let* ((arts list)
5156                    (prev (cons nil list))
5157                    (all prev))
5158               (while arts
5159                 (if (or (not (consp (car arts)))
5160                         (= (cdar arts) gnus-summary-default-score))
5161                     (setcdr prev (cdr arts))
5162                   (setq prev arts))
5163                 (setq arts (cdr arts)))
5164               (setq list (cdr all)))))
5165
5166         (when (eq (cdr type) 'seen)
5167           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5168
5169         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5170           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5171
5172         (when (and (gnus-check-backend-function
5173                     'request-set-mark gnus-newsgroup-name)
5174                    (not (gnus-article-unpropagatable-p (cdr type))))
5175           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5176                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5177                  (add (gnus-remove-from-range
5178                        (gnus-copy-sequence list) old)))
5179             (when add
5180               (push (list add 'add (list (cdr type))) delta-marks))
5181             (when del
5182               (push (list del 'del (list (cdr type))) delta-marks))))
5183
5184         (when list
5185           (push (cons (cdr type) list) newmarked)))
5186
5187       (when delta-marks
5188         (unless (gnus-check-group gnus-newsgroup-name)
5189           (error "Can't open server for %s" gnus-newsgroup-name))
5190         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5191
5192       ;; Enter these new marks into the info of the group.
5193       (if (nthcdr 3 info)
5194           (setcar (nthcdr 3 info) newmarked)
5195         ;; Add the marks lists to the end of the info.
5196         (when newmarked
5197           (setcdr (nthcdr 2 info) (list newmarked))))
5198
5199       ;; Cut off the end of the info if there's nothing else there.
5200       (let ((i 5))
5201         (while (and (> i 2)
5202                     (not (nth i info)))
5203           (when (nthcdr (decf i) info)
5204             (setcdr (nthcdr i info) nil)))))))
5205
5206 (defun gnus-set-mode-line (where)
5207   "Set the mode line of the article or summary buffers.
5208 If WHERE is `summary', the summary mode line format will be used."
5209   ;; Is this mode line one we keep updated?
5210   (when (and (memq where gnus-updated-mode-lines)
5211              (symbol-value
5212               (intern (format "gnus-%s-mode-line-format-spec" where))))
5213     (let (mode-string)
5214       (save-excursion
5215         ;; We evaluate this in the summary buffer since these
5216         ;; variables are buffer-local to that buffer.
5217         (set-buffer gnus-summary-buffer)
5218        ;; We bind all these variables that are used in the `eval' form
5219         ;; below.
5220         (let* ((mformat (symbol-value
5221                          (intern
5222                           (format "gnus-%s-mode-line-format-spec" where))))
5223                (gnus-tmp-group-name (gnus-group-decoded-name
5224                                      gnus-newsgroup-name))
5225                (gnus-tmp-article-number (or gnus-current-article 0))
5226                (gnus-tmp-unread gnus-newsgroup-unreads)
5227                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5228                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5229                (gnus-tmp-unread-and-unselected
5230                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5231                             (zerop gnus-tmp-unselected))
5232                        "")
5233                       ((zerop gnus-tmp-unselected)
5234                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5235                       (t (format "{%d(+%d) more}"
5236                                  gnus-tmp-unread-and-unticked
5237                                  gnus-tmp-unselected))))
5238                (gnus-tmp-subject
5239                 (if (and gnus-current-headers
5240                          (vectorp gnus-current-headers))
5241                     (gnus-mode-string-quote
5242                      (mail-header-subject gnus-current-headers))
5243                   ""))
5244                bufname-length max-len
5245                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5246           (setq mode-string (eval mformat))
5247           (setq bufname-length (if (string-match "%b" mode-string)
5248                                    (- (length
5249                                        (buffer-name
5250                                         (if (eq where 'summary)
5251                                             nil
5252                                           (get-buffer gnus-article-buffer))))
5253                                       2)
5254                                  0))
5255           (setq max-len (max 4 (if gnus-mode-non-string-length
5256                                    (- (window-width)
5257                                       gnus-mode-non-string-length
5258                                       bufname-length)
5259                                  (length mode-string))))
5260           ;; We might have to chop a bit of the string off...
5261           (when (> (length mode-string) max-len)
5262             (setq mode-string
5263                   (concat (truncate-string-to-width mode-string (- max-len 3))
5264                           "...")))
5265           ;; Pad the mode string a bit.
5266           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5267       ;; Update the mode line.
5268       (setq mode-line-buffer-identification
5269             (gnus-mode-line-buffer-identification (list mode-string)))
5270       (set-buffer-modified-p t))))
5271
5272 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5273   "Go through the HEADERS list and add all Xrefs to a hash table.
5274 The resulting hash table is returned, or nil if no Xrefs were found."
5275   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5276          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5277          (xref-hashtb (gnus-make-hashtable))
5278          start group entry number xrefs header)
5279     (while headers
5280       (setq header (pop headers))
5281       (when (and (setq xrefs (mail-header-xref header))
5282                  (not (memq (setq number (mail-header-number header))
5283                             unreads)))
5284         (setq start 0)
5285         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5286           (setq start (match-end 0))
5287           (setq group (if prefix
5288                           (concat prefix (substring xrefs (match-beginning 1)
5289                                                     (match-end 1)))
5290                         (substring xrefs (match-beginning 1) (match-end 1))))
5291           (setq number
5292                 (string-to-int (substring xrefs (match-beginning 2)
5293                                           (match-end 2))))
5294           (if (setq entry (gnus-gethash group xref-hashtb))
5295               (setcdr entry (cons number (cdr entry)))
5296             (gnus-sethash group (cons number nil) xref-hashtb)))))
5297     (and start xref-hashtb)))
5298
5299 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5300   "Look through all the headers and mark the Xrefs as read."
5301   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5302         name entry info xref-hashtb idlist method nth4)
5303     (save-excursion
5304       (set-buffer gnus-group-buffer)
5305       (when (setq xref-hashtb
5306                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5307         (mapatoms
5308          (lambda (group)
5309            (unless (string= from-newsgroup (setq name (symbol-name group)))
5310              (setq idlist (symbol-value group))
5311              ;; Dead groups are not updated.
5312              (and (prog1
5313                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5314                             info (nth 2 entry))
5315                     (when (stringp (setq nth4 (gnus-info-method info)))
5316                       (setq nth4 (gnus-server-to-method nth4))))
5317                   ;; Only do the xrefs if the group has the same
5318                   ;; select method as the group we have just read.
5319                   (or (gnus-methods-equal-p
5320                        nth4 (gnus-find-method-for-group from-newsgroup))
5321                       virtual
5322                       (equal nth4 (setq method (gnus-find-method-for-group
5323                                                 from-newsgroup)))
5324                       (and (equal (car nth4) (car method))
5325                            (equal (nth 1 nth4) (nth 1 method))))
5326                   gnus-use-cross-reference
5327                   (or (not (eq gnus-use-cross-reference t))
5328                       virtual
5329                       ;; Only do cross-references on subscribed
5330                       ;; groups, if that is what is wanted.
5331                       (<= (gnus-info-level info) gnus-level-subscribed))
5332                   (gnus-group-make-articles-read name idlist))))
5333          xref-hashtb)))))
5334
5335 (defun gnus-compute-read-articles (group articles)
5336   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5337          (info (nth 2 entry))
5338          (active (gnus-active group))
5339          ninfo)
5340     (when entry
5341       ;; First peel off all invalid article numbers.
5342       (when active
5343         (let ((ids articles)
5344               id first)
5345           (while (setq id (pop ids))
5346             (when (and first (> id (cdr active)))
5347               ;; We'll end up in this situation in one particular
5348               ;; obscure situation.  If you re-scan a group and get
5349               ;; a new article that is cross-posted to a different
5350               ;; group that has not been re-scanned, you might get
5351               ;; crossposted article that has a higher number than
5352               ;; Gnus believes possible.  So we re-activate this
5353               ;; group as well.  This might mean doing the
5354               ;; crossposting thingy will *increase* the number
5355               ;; of articles in some groups.  Tsk, tsk.
5356               (setq active (or (gnus-activate-group group) active)))
5357             (when (or (> id (cdr active))
5358                       (< id (car active)))
5359               (setq articles (delq id articles))))))
5360       ;; If the read list is nil, we init it.
5361       (if (and active
5362                (null (gnus-info-read info))
5363                (> (car active) 1))
5364           (setq ninfo (cons 1 (1- (car active))))
5365         (setq ninfo (gnus-info-read info)))
5366       ;; Then we add the read articles to the range.
5367       (gnus-add-to-range
5368        ninfo (setq articles (sort articles '<))))))
5369
5370 (defun gnus-group-make-articles-read (group articles)
5371   "Update the info of GROUP to say that ARTICLES are read."
5372   (let* ((num 0)
5373          (entry (gnus-gethash group gnus-newsrc-hashtb))
5374          (info (nth 2 entry))
5375          (active (gnus-active group))
5376          range)
5377     (when entry
5378       (setq range (gnus-compute-read-articles group articles))
5379       (save-excursion
5380         (set-buffer gnus-group-buffer)
5381         (gnus-undo-register
5382           `(progn
5383              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5384              (gnus-info-set-read ',info ',(gnus-info-read info))
5385              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5386              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5387              (gnus-group-update-group ,group t))))
5388       ;; Add the read articles to the range.
5389       (gnus-info-set-read info range)
5390       (gnus-request-set-mark group (list (list range 'add '(read))))
5391       ;; Then we have to re-compute how many unread
5392       ;; articles there are in this group.
5393       (when active
5394         (cond
5395          ((not range)
5396           (setq num (- (1+ (cdr active)) (car active))))
5397          ((not (listp (cdr range)))
5398           (setq num (- (cdr active) (- (1+ (cdr range))
5399                                        (car range)))))
5400          (t
5401           (while range
5402             (if (numberp (car range))
5403                 (setq num (1+ num))
5404               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5405             (setq range (cdr range)))
5406           (setq num (- (cdr active) num))))
5407         ;; Update the number of unread articles.
5408         (setcar entry num)
5409         ;; Update the group buffer.
5410         (gnus-group-update-group group t)))))
5411
5412 (defvar gnus-newsgroup-none-id 0)
5413
5414 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5415   (let ((cur nntp-server-buffer)
5416         (dependencies
5417          (or dependencies
5418              (save-excursion (set-buffer gnus-summary-buffer)
5419                              gnus-newsgroup-dependencies)))
5420         headers id end ref
5421         (mail-parse-charset gnus-newsgroup-charset)
5422         (mail-parse-ignored-charsets
5423          (save-excursion (condition-case nil
5424                              (set-buffer gnus-summary-buffer)
5425                            (error))
5426                          gnus-newsgroup-ignored-charsets)))
5427     (save-excursion
5428       (set-buffer nntp-server-buffer)
5429       ;; Translate all TAB characters into SPACE characters.
5430       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5431       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5432       (gnus-run-hooks 'gnus-parse-headers-hook)
5433       (let ((case-fold-search t)
5434             in-reply-to header p lines chars)
5435         (goto-char (point-min))
5436         ;; Search to the beginning of the next header.  Error messages
5437         ;; do not begin with 2 or 3.
5438         (while (re-search-forward "^[23][0-9]+ " nil t)
5439           (setq id nil
5440                 ref nil)
5441           ;; This implementation of this function, with nine
5442           ;; search-forwards instead of the one re-search-forward and
5443           ;; a case (which basically was the old function) is actually
5444           ;; about twice as fast, even though it looks messier.  You
5445           ;; can't have everything, I guess.  Speed and elegance
5446           ;; doesn't always go hand in hand.
5447           (setq
5448            header
5449            (vector
5450             ;; Number.
5451             (prog1
5452                 (read cur)
5453               (end-of-line)
5454               (setq p (point))
5455               (narrow-to-region (point)
5456                                 (or (and (search-forward "\n.\n" nil t)
5457                                          (- (point) 2))
5458                                     (point))))
5459             ;; Subject.
5460             (progn
5461               (goto-char p)
5462               (if (search-forward "\nsubject:" nil t)
5463                   (funcall gnus-decode-encoded-word-function
5464                            (nnheader-header-value))
5465                 "(none)"))
5466             ;; From.
5467             (progn
5468               (goto-char p)
5469               (if (search-forward "\nfrom:" nil t)
5470                   (funcall gnus-decode-encoded-word-function
5471                            (nnheader-header-value))
5472                 "(nobody)"))
5473             ;; Date.
5474             (progn
5475               (goto-char p)
5476               (if (search-forward "\ndate:" nil t)
5477                   (nnheader-header-value) ""))
5478             ;; Message-ID.
5479             (progn
5480               (goto-char p)
5481               (setq id (if (re-search-forward
5482                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5483                            ;; We do it this way to make sure the Message-ID
5484                            ;; is (somewhat) syntactically valid.
5485                            (buffer-substring (match-beginning 1)
5486                                              (match-end 1))
5487                          ;; If there was no message-id, we just fake one
5488                          ;; to make subsequent routines simpler.
5489                          (nnheader-generate-fake-message-id))))
5490             ;; References.
5491             (progn
5492               (goto-char p)
5493               (if (search-forward "\nreferences:" nil t)
5494                   (progn
5495                     (setq end (point))
5496                     (prog1
5497                         (nnheader-header-value)
5498                       (setq ref
5499                             (buffer-substring
5500                              (progn
5501                                (end-of-line)
5502                                (search-backward ">" end t)
5503                                (1+ (point)))
5504                              (progn
5505                                (search-backward "<" end t)
5506                                (point))))))
5507                 ;; Get the references from the in-reply-to header if there
5508                 ;; were no references and the in-reply-to header looks
5509                 ;; promising.
5510                 (if (and (search-forward "\nin-reply-to:" nil t)
5511                          (setq in-reply-to (nnheader-header-value))
5512                          (string-match "<[^>]+>" in-reply-to))
5513                     (let (ref2)
5514                       (setq ref (substring in-reply-to (match-beginning 0)
5515                                            (match-end 0)))
5516                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5517                         (setq ref2 (substring in-reply-to (match-beginning 0)
5518                                               (match-end 0)))
5519                         (when (> (length ref2) (length ref))
5520                           (setq ref ref2)))
5521                       ref)
5522                   (setq ref nil))))
5523             ;; Chars.
5524             (progn
5525               (goto-char p)
5526               (if (search-forward "\nchars: " nil t)
5527                   (if (numberp (setq chars (ignore-errors (read cur))))
5528                       chars -1)
5529                 -1))
5530             ;; Lines.
5531             (progn
5532               (goto-char p)
5533               (if (search-forward "\nlines: " nil t)
5534                   (if (numberp (setq lines (ignore-errors (read cur))))
5535                       lines -1)
5536                 -1))
5537             ;; Xref.
5538             (progn
5539               (goto-char p)
5540               (and (search-forward "\nxref:" nil t)
5541                    (nnheader-header-value)))
5542             ;; Extra.
5543             (when gnus-extra-headers
5544               (let ((extra gnus-extra-headers)
5545                     out)
5546                 (while extra
5547                   (goto-char p)
5548                   (when (search-forward
5549                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5550                     (push (cons (car extra) (nnheader-header-value))
5551                           out))
5552                   (pop extra))
5553                 out))))
5554           (when (equal id ref)
5555             (setq ref nil))
5556
5557           (when gnus-alter-header-function
5558             (funcall gnus-alter-header-function header)
5559             (setq id (mail-header-id header)
5560                   ref (gnus-parent-id (mail-header-references header))))
5561
5562           (when (setq header
5563                       (gnus-dependencies-add-header
5564                        header dependencies force-new))
5565             (push header headers))
5566           (goto-char (point-max))
5567           (widen))
5568         (nreverse headers)))))
5569
5570 ;; Goes through the xover lines and returns a list of vectors
5571 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5572                                                   force-new dependencies
5573                                                   group also-fetch-heads)
5574   "Parse the news overview data in the server buffer.
5575 Return a list of headers that match SEQUENCE (see
5576 `nntp-retrieve-headers')."
5577   ;; Get the Xref when the users reads the articles since most/some
5578   ;; NNTP servers do not include Xrefs when using XOVER.
5579   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5580   (let ((mail-parse-charset gnus-newsgroup-charset)
5581         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5582         (cur nntp-server-buffer)
5583         (dependencies (or dependencies gnus-newsgroup-dependencies))
5584         (allp (cond
5585                ((eq gnus-read-all-available-headers t)
5586                 t)
5587                ((stringp gnus-read-all-available-headers)
5588                 (string-match gnus-read-all-available-headers group))
5589                (t
5590                 nil)))
5591         number headers header)
5592     (save-excursion
5593       (set-buffer nntp-server-buffer)
5594       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5595       ;; Allow the user to mangle the headers before parsing them.
5596       (gnus-run-hooks 'gnus-parse-headers-hook)
5597       (goto-char (point-min))
5598       (gnus-parse-without-error
5599         (while (and (or sequence allp)
5600                     (not (eobp)))
5601           (setq number (read cur))
5602           (when (not allp)
5603             (while (and sequence
5604                         (< (car sequence) number))
5605               (setq sequence (cdr sequence))))
5606           (when (and (or allp
5607                          (and sequence
5608                               (eq number (car sequence))))
5609                      (progn
5610                        (setq sequence (cdr sequence))
5611                        (setq header (inline
5612                                       (gnus-nov-parse-line
5613                                        number dependencies force-new)))))
5614             (push header headers))
5615           (forward-line 1)))
5616       ;; A common bug in inn is that if you have posted an article and
5617       ;; then retrieves the active file, it will answer correctly --
5618       ;; the new article is included.  However, a NOV entry for the
5619       ;; article may not have been generated yet, so this may fail.
5620       ;; We work around this problem by retrieving the last few
5621       ;; headers using HEAD.
5622       (if (or (not also-fetch-heads)
5623               (not sequence))
5624           ;; We (probably) got all the headers.
5625           (nreverse headers)
5626         (let ((gnus-nov-is-evil t))
5627           (nconc
5628            (nreverse headers)
5629            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5630              (gnus-get-newsgroup-headers))))))))
5631
5632 (defun gnus-article-get-xrefs ()
5633   "Fill in the Xref value in `gnus-current-headers', if necessary.
5634 This is meant to be called in `gnus-article-internal-prepare-hook'."
5635   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5636                                  gnus-current-headers)))
5637     (or (not gnus-use-cross-reference)
5638         (not headers)
5639         (and (mail-header-xref headers)
5640              (not (string= (mail-header-xref headers) "")))
5641         (let ((case-fold-search t)
5642               xref)
5643           (save-restriction
5644             (nnheader-narrow-to-headers)
5645             (goto-char (point-min))
5646             (when (or (and (not (eobp))
5647                            (eq (downcase (char-after)) ?x)
5648                            (looking-at "Xref:"))
5649                       (search-forward "\nXref:" nil t))
5650               (goto-char (1+ (match-end 0)))
5651               (setq xref (buffer-substring (point)
5652                                            (progn (end-of-line) (point))))
5653               (mail-header-set-xref headers xref)))))))
5654
5655 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5656   "Find article ID and insert the summary line for that article.
5657 OLD-HEADER can either be a header or a line number to insert
5658 the subject line on."
5659   (let* ((line (and (numberp old-header) old-header))
5660          (old-header (and (vectorp old-header) old-header))
5661          (header (cond ((and old-header use-old-header)
5662                         old-header)
5663                        ((and (numberp id)
5664                              (gnus-number-to-header id))
5665                         (gnus-number-to-header id))
5666                        (t
5667                         (gnus-read-header id))))
5668          (number (and (numberp id) id))
5669          d)
5670     (when header
5671       ;; Rebuild the thread that this article is part of and go to the
5672       ;; article we have fetched.
5673       (when (and (not gnus-show-threads)
5674                  old-header)
5675         (when (and number
5676                    (setq d (gnus-data-find (mail-header-number old-header))))
5677           (goto-char (gnus-data-pos d))
5678           (gnus-data-remove
5679            number
5680            (- (gnus-point-at-bol)
5681               (prog1
5682                   (1+ (gnus-point-at-eol))
5683                 (gnus-delete-line))))))
5684       (when old-header
5685         (mail-header-set-number header (mail-header-number old-header)))
5686       (setq gnus-newsgroup-sparse
5687             (delq (setq number (mail-header-number header))
5688                   gnus-newsgroup-sparse))
5689       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5690       (push number gnus-newsgroup-limit)
5691       (gnus-rebuild-thread (mail-header-id header) line)
5692       (gnus-summary-goto-subject number nil t))
5693     (when (and (numberp number)
5694                (> number 0))
5695       ;; We have to update the boundaries even if we can't fetch the
5696       ;; article if ID is a number -- so that the next `P' or `N'
5697       ;; command will fetch the previous (or next) article even
5698       ;; if the one we tried to fetch this time has been canceled.
5699       (when (> number gnus-newsgroup-end)
5700         (setq gnus-newsgroup-end number))
5701       (when (< number gnus-newsgroup-begin)
5702         (setq gnus-newsgroup-begin number))
5703       (setq gnus-newsgroup-unselected
5704             (delq number gnus-newsgroup-unselected)))
5705     ;; Report back a success?
5706     (and header (mail-header-number header))))
5707
5708 ;;; Process/prefix in the summary buffer
5709
5710 (defun gnus-summary-work-articles (n)
5711   "Return a list of articles to be worked upon.
5712 The prefix argument, the list of process marked articles, and the
5713 current article will be taken into consideration."
5714   (save-excursion
5715     (set-buffer gnus-summary-buffer)
5716     (cond
5717      (n
5718       ;; A numerical prefix has been given.
5719       (setq n (prefix-numeric-value n))
5720       (let ((backward (< n 0))
5721             (n (abs (prefix-numeric-value n)))
5722             articles article)
5723         (save-excursion
5724           (while
5725               (and (> n 0)
5726                    (push (setq article (gnus-summary-article-number))
5727                          articles)
5728                    (if backward
5729                        (gnus-summary-find-prev nil article)
5730                      (gnus-summary-find-next nil article)))
5731             (decf n)))
5732         (nreverse articles)))
5733      ((and (gnus-region-active-p) (mark))
5734       (message "region active")
5735       ;; Work on the region between point and mark.
5736       (let ((max (max (point) (mark)))
5737             articles article)
5738         (save-excursion
5739           (goto-char (min (min (point) (mark))))
5740           (while
5741               (and
5742                (push (setq article (gnus-summary-article-number)) articles)
5743                (gnus-summary-find-next nil article)
5744                (< (point) max)))
5745           (nreverse articles))))
5746      (gnus-newsgroup-processable
5747       ;; There are process-marked articles present.
5748       ;; Save current state.
5749       (gnus-summary-save-process-mark)
5750       ;; Return the list.
5751       (reverse gnus-newsgroup-processable))
5752      (t
5753       ;; Just return the current article.
5754       (list (gnus-summary-article-number))))))
5755
5756 (defmacro gnus-summary-iterate (arg &rest forms)
5757   "Iterate over the process/prefixed articles and do FORMS.
5758 ARG is the interactive prefix given to the command.  FORMS will be
5759 executed with point over the summary line of the articles."
5760   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5761     `(let ((,articles (gnus-summary-work-articles ,arg)))
5762        (while ,articles
5763          (gnus-summary-goto-subject (car ,articles))
5764          ,@forms
5765          (pop ,articles)))))
5766
5767 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5768 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5769
5770 (defun gnus-summary-save-process-mark ()
5771   "Push the current set of process marked articles on the stack."
5772   (interactive)
5773   (push (copy-sequence gnus-newsgroup-processable)
5774         gnus-newsgroup-process-stack))
5775
5776 (defun gnus-summary-kill-process-mark ()
5777   "Push the current set of process marked articles on the stack and unmark."
5778   (interactive)
5779   (gnus-summary-save-process-mark)
5780   (gnus-summary-unmark-all-processable))
5781
5782 (defun gnus-summary-yank-process-mark ()
5783   "Pop the last process mark state off the stack and restore it."
5784   (interactive)
5785   (unless gnus-newsgroup-process-stack
5786     (error "Empty mark stack"))
5787   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5788
5789 (defun gnus-summary-process-mark-set (set)
5790   "Make SET into the current process marked articles."
5791   (gnus-summary-unmark-all-processable)
5792   (while set
5793     (gnus-summary-set-process-mark (pop set))))
5794
5795 ;;; Searching and stuff
5796
5797 (defun gnus-summary-search-group (&optional backward use-level)
5798   "Search for next unread newsgroup.
5799 If optional argument BACKWARD is non-nil, search backward instead."
5800   (save-excursion
5801     (set-buffer gnus-group-buffer)
5802     (when (gnus-group-search-forward
5803            backward nil (if use-level (gnus-group-group-level) nil))
5804       (gnus-group-group-name))))
5805
5806 (defun gnus-summary-best-group (&optional exclude-group)
5807   "Find the name of the best unread group.
5808 If EXCLUDE-GROUP, do not go to this group."
5809   (save-excursion
5810     (set-buffer gnus-group-buffer)
5811     (save-excursion
5812       (gnus-group-best-unread-group exclude-group))))
5813
5814 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5815   (if backward (gnus-summary-find-prev)
5816     (let* ((dummy (gnus-summary-article-intangible-p))
5817            (article (or article (gnus-summary-article-number)))
5818            (arts (gnus-data-find-list article))
5819            result)
5820       (when (and (not dummy)
5821                  (or (not gnus-summary-check-current)
5822                      (not unread)
5823                      (not (gnus-data-unread-p (car arts)))))
5824         (setq arts (cdr arts)))
5825       (when (setq result
5826                   (if unread
5827                       (progn
5828                         (while arts
5829                           (when (or (and undownloaded
5830                                          (eq gnus-undownloaded-mark
5831                                              (gnus-data-mark (car arts))))
5832                                     (gnus-data-unread-p (car arts)))
5833                             (setq result (car arts)
5834                                   arts nil))
5835                           (setq arts (cdr arts)))
5836                         result)
5837                     (car arts)))
5838         (goto-char (gnus-data-pos result))
5839         (gnus-data-number result)))))
5840
5841 (defun gnus-summary-find-prev (&optional unread article)
5842   (let* ((eobp (eobp))
5843          (article (or article (gnus-summary-article-number)))
5844          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5845          result)
5846     (when (and (not eobp)
5847                (or (not gnus-summary-check-current)
5848                    (not unread)
5849                    (not (gnus-data-unread-p (car arts)))))
5850       (setq arts (cdr arts)))
5851     (when (setq result
5852                 (if unread
5853                     (progn
5854                       (while arts
5855                         (when (gnus-data-unread-p (car arts))
5856                           (setq result (car arts)
5857                                 arts nil))
5858                         (setq arts (cdr arts)))
5859                       result)
5860                   (car arts)))
5861       (goto-char (gnus-data-pos result))
5862       (gnus-data-number result))))
5863
5864 (defun gnus-summary-find-subject (subject &optional unread backward article)
5865   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5866          (article (or article (gnus-summary-article-number)))
5867          (articles (gnus-data-list backward))
5868          (arts (gnus-data-find-list article articles))
5869          result)
5870     (when (or (not gnus-summary-check-current)
5871               (not unread)
5872               (not (gnus-data-unread-p (car arts))))
5873       (setq arts (cdr arts)))
5874     (while arts
5875       (and (or (not unread)
5876                (gnus-data-unread-p (car arts)))
5877            (vectorp (gnus-data-header (car arts)))
5878            (gnus-subject-equal
5879             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5880            (setq result (car arts)
5881                  arts nil))
5882       (setq arts (cdr arts)))
5883     (and result
5884          (goto-char (gnus-data-pos result))
5885          (gnus-data-number result))))
5886
5887 (defun gnus-summary-search-forward (&optional unread subject backward)
5888   "Search forward for an article.
5889 If UNREAD, look for unread articles.  If SUBJECT, look for
5890 articles with that subject.  If BACKWARD, search backward instead."
5891   (cond (subject (gnus-summary-find-subject subject unread backward))
5892         (backward (gnus-summary-find-prev unread))
5893         (t (gnus-summary-find-next unread))))
5894
5895 (defun gnus-recenter (&optional n)
5896   "Center point in window and redisplay frame.
5897 Also do horizontal recentering."
5898   (interactive "P")
5899   (when (and gnus-auto-center-summary
5900              (not (eq gnus-auto-center-summary 'vertical)))
5901     (gnus-horizontal-recenter))
5902   (recenter n))
5903
5904 (defun gnus-summary-recenter ()
5905   "Center point in the summary window.
5906 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5907 displayed, no centering will be performed."
5908   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5909 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5910   (interactive)
5911   (let* ((top (cond ((< (window-height) 4) 0)
5912                     ((< (window-height) 7) 1)
5913                     (t (if (numberp gnus-auto-center-summary)
5914                            gnus-auto-center-summary
5915                          2))))
5916          (height (1- (window-height)))
5917          (bottom (save-excursion (goto-char (point-max))
5918                                  (forward-line (- height))
5919                                  (point)))
5920          (window (get-buffer-window (current-buffer))))
5921     ;; The user has to want it.
5922     (when gnus-auto-center-summary
5923       (when (get-buffer-window gnus-article-buffer)
5924         ;; Only do recentering when the article buffer is displayed,
5925       ;; Set the window start to either `bottom', which is the biggest
5926         ;; possible valid number, or the second line from the top,
5927         ;; whichever is the least.
5928         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5929           (if (> bottom top-pos)
5930               ;; Keep the second line from the top visible
5931               (set-window-start window top-pos t)
5932             ;; Try to keep the bottom line visible; if it's partially
5933             ;; obscured, either scroll one more line to make it fully
5934             ;; visible, or revert to using TOP-POS.
5935             (save-excursion
5936               (goto-char (point-max))
5937               (forward-line -1)
5938               (let ((last-line-start (point)))
5939                 (goto-char bottom)
5940                 (set-window-start window (point) t)
5941                 (when (not (pos-visible-in-window-p last-line-start window))
5942                   (forward-line 1)
5943                   (set-window-start window (min (point) top-pos) t)))))))
5944       ;; Do horizontal recentering while we're at it.
5945       (when (and (get-buffer-window (current-buffer) t)
5946                  (not (eq gnus-auto-center-summary 'vertical)))
5947         (let ((selected (selected-window)))
5948           (select-window (get-buffer-window (current-buffer) t))
5949           (gnus-summary-position-point)
5950           (gnus-horizontal-recenter)
5951           (select-window selected))))))
5952
5953 (defun gnus-summary-jump-to-group (newsgroup)
5954   "Move point to NEWSGROUP in group mode buffer."
5955   ;; Keep update point of group mode buffer if visible.
5956   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5957       (save-window-excursion
5958         ;; Take care of tree window mode.
5959         (when (get-buffer-window gnus-group-buffer)
5960           (pop-to-buffer gnus-group-buffer))
5961         (gnus-group-jump-to-group newsgroup))
5962     (save-excursion
5963       ;; Take care of tree window mode.
5964       (if (get-buffer-window gnus-group-buffer)
5965           (pop-to-buffer gnus-group-buffer)
5966         (set-buffer gnus-group-buffer))
5967       (gnus-group-jump-to-group newsgroup))))
5968
5969 ;; This function returns a list of article numbers based on the
5970 ;; difference between the ranges of read articles in this group and
5971 ;; the range of active articles.
5972 (defun gnus-list-of-unread-articles (group)
5973   (let* ((read (gnus-info-read (gnus-get-info group)))
5974          (active (or (gnus-active group) (gnus-activate-group group)))
5975          (last (cdr active))
5976          first nlast unread)
5977     ;; If none are read, then all are unread.
5978     (if (not read)
5979         (setq first (car active))
5980       ;; If the range of read articles is a single range, then the
5981       ;; first unread article is the article after the last read
5982       ;; article.  Sounds logical, doesn't it?
5983       (if (and (not (listp (cdr read)))
5984                (or (< (car read) (car active))
5985                    (progn (setq read (list read))
5986                           nil)))
5987           (setq first (max (car active) (1+ (cdr read))))
5988         ;; `read' is a list of ranges.
5989         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5990                                   (caar read)))
5991                   1)
5992           (setq first (car active)))
5993         (while read
5994           (when first
5995             (while (< first nlast)
5996               (push first unread)
5997               (setq first (1+ first))))
5998           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5999           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6000           (setq read (cdr read)))))
6001     ;; And add the last unread articles.
6002     (while (<= first last)
6003       (push first unread)
6004       (setq first (1+ first)))
6005     ;; Return the list of unread articles.
6006     (delq 0 (nreverse unread))))
6007
6008 (defun gnus-list-of-read-articles (group)
6009   "Return a list of unread, unticked and non-dormant articles."
6010   (let* ((info (gnus-get-info group))
6011          (marked (gnus-info-marks info))
6012          (active (gnus-active group)))
6013     (and info active
6014          (gnus-list-range-difference
6015           (gnus-list-range-difference
6016            (gnus-sorted-complement
6017             (gnus-uncompress-range active)
6018             (gnus-list-of-unread-articles group))
6019            (cdr (assq 'dormant marked)))
6020           (cdr (assq 'tick marked))))))
6021
6022 ;; Various summary commands
6023
6024 (defun gnus-summary-select-article-buffer ()
6025   "Reconfigure windows to show article buffer."
6026   (interactive)
6027   (if (not (gnus-buffer-live-p gnus-article-buffer))
6028       (error "There is no article buffer for this summary buffer")
6029     (gnus-configure-windows 'article)
6030     (select-window (get-buffer-window gnus-article-buffer))))
6031
6032 (defun gnus-summary-universal-argument (arg)
6033   "Perform any operation on all articles that are process/prefixed."
6034   (interactive "P")
6035   (let ((articles (gnus-summary-work-articles arg))
6036         func article)
6037     (if (eq
6038          (setq
6039           func
6040           (key-binding
6041            (read-key-sequence
6042             (substitute-command-keys
6043              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6044          'undefined)
6045         (gnus-error 1 "Undefined key")
6046       (save-excursion
6047         (while articles
6048           (gnus-summary-goto-subject (setq article (pop articles)))
6049           (let (gnus-newsgroup-processable)
6050             (command-execute func))
6051           (gnus-summary-remove-process-mark article)))))
6052   (gnus-summary-position-point))
6053
6054 (defun gnus-summary-toggle-truncation (&optional arg)
6055   "Toggle truncation of summary lines.
6056 With arg, turn line truncation on iff arg is positive."
6057   (interactive "P")
6058   (setq truncate-lines
6059         (if (null arg) (not truncate-lines)
6060           (> (prefix-numeric-value arg) 0)))
6061   (redraw-display))
6062
6063 (defun gnus-summary-reselect-current-group (&optional all rescan)
6064   "Exit and then reselect the current newsgroup.
6065 The prefix argument ALL means to select all articles."
6066   (interactive "P")
6067   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6068     (error "Ephemeral groups can't be reselected"))
6069   (let ((current-subject (gnus-summary-article-number))
6070         (group gnus-newsgroup-name))
6071     (setq gnus-newsgroup-begin nil)
6072     (gnus-summary-exit)
6073     ;; We have to adjust the point of group mode buffer because
6074     ;; point was moved to the next unread newsgroup by exiting.
6075     (gnus-summary-jump-to-group group)
6076     (when rescan
6077       (save-excursion
6078         (gnus-group-get-new-news-this-group 1)))
6079     (gnus-group-read-group all t)
6080     (gnus-summary-goto-subject current-subject nil t)))
6081
6082 (defun gnus-summary-rescan-group (&optional all)
6083   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6084   (interactive "P")
6085   (gnus-summary-reselect-current-group all t))
6086
6087 (defun gnus-summary-update-info (&optional non-destructive)
6088   (save-excursion
6089     (let ((group gnus-newsgroup-name))
6090       (when group
6091         (when gnus-newsgroup-kill-headers
6092           (setq gnus-newsgroup-killed
6093                 (gnus-compress-sequence
6094                  (gnus-sorted-union
6095                   (gnus-list-range-intersection
6096                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6097                   gnus-newsgroup-unreads)
6098                  t)))
6099         (unless (listp (cdr gnus-newsgroup-killed))
6100           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6101         (let ((headers gnus-newsgroup-headers))
6102           ;; Set the new ranges of read articles.
6103           (save-excursion
6104             (set-buffer gnus-group-buffer)
6105             (gnus-undo-force-boundary))
6106           (gnus-update-read-articles
6107            group (gnus-sorted-union
6108                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6109           ;; Set the current article marks.
6110           (let ((gnus-newsgroup-scored
6111                  (if (and (not gnus-save-score)
6112                           (not non-destructive))
6113                      nil
6114                    gnus-newsgroup-scored)))
6115             (save-excursion
6116               (gnus-update-marks)))
6117           ;; Do the cross-ref thing.
6118           (when gnus-use-cross-reference
6119             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6120           ;; Do not switch windows but change the buffer to work.
6121           (set-buffer gnus-group-buffer)
6122           (unless (gnus-ephemeral-group-p group)
6123             (gnus-group-update-group group)))))))
6124
6125 (defun gnus-summary-save-newsrc (&optional force)
6126   "Save the current number of read/marked articles in the dribble buffer.
6127 The dribble buffer will then be saved.
6128 If FORCE (the prefix), also save the .newsrc file(s)."
6129   (interactive "P")
6130   (gnus-summary-update-info t)
6131   (if force
6132       (gnus-save-newsrc-file)
6133     (gnus-dribble-save)))
6134
6135 (defun gnus-summary-exit (&optional temporary)
6136   "Exit reading current newsgroup, and then return to group selection mode.
6137 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6138   (interactive)
6139   (gnus-set-global-variables)
6140   (when (gnus-buffer-live-p gnus-article-buffer)
6141     (save-excursion
6142       (set-buffer gnus-article-buffer)
6143       (mm-destroy-parts gnus-article-mime-handles)
6144       ;; Set it to nil for safety reason.
6145       (setq gnus-article-mime-handle-alist nil)
6146       (setq gnus-article-mime-handles nil)))
6147   (gnus-kill-save-kill-buffer)
6148   (gnus-async-halt-prefetch)
6149   (let* ((group gnus-newsgroup-name)
6150          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6151          (mode major-mode)
6152          (group-point nil)
6153          (buf (current-buffer)))
6154     (unless quit-config
6155       ;; Do adaptive scoring, and possibly save score files.
6156       (when gnus-newsgroup-adaptive
6157         (gnus-score-adaptive))
6158       (when gnus-use-scoring
6159         (gnus-score-save)))
6160     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6161     ;; If we have several article buffers, we kill them at exit.
6162     (unless gnus-single-article-buffer
6163       (gnus-kill-buffer gnus-original-article-buffer)
6164       (setq gnus-article-current nil))
6165     (when gnus-use-cache
6166       (gnus-cache-possibly-remove-articles)
6167       (gnus-cache-save-buffers))
6168     (gnus-async-prefetch-remove-group group)
6169     (when gnus-suppress-duplicates
6170       (gnus-dup-enter-articles))
6171     (when gnus-use-trees
6172       (gnus-tree-close group))
6173     (when gnus-use-cache
6174       (gnus-cache-write-active))
6175     ;; Remove entries for this group.
6176     (nnmail-purge-split-history (gnus-group-real-name group))
6177     ;; Make all changes in this group permanent.
6178     (unless quit-config
6179       (gnus-run-hooks 'gnus-exit-group-hook)
6180       (gnus-summary-update-info))
6181     (gnus-close-group group)
6182     ;; Make sure where we were, and go to next newsgroup.
6183     (set-buffer gnus-group-buffer)
6184     (unless quit-config
6185       (gnus-group-jump-to-group group))
6186     (gnus-run-hooks 'gnus-summary-exit-hook)
6187     (unless (or quit-config
6188                 ;; If this group has disappeared from the summary
6189                 ;; buffer, don't skip forwards.
6190                 (not (string= group (gnus-group-group-name))))
6191       (gnus-group-next-unread-group 1))
6192     (setq group-point (point))
6193     (if temporary
6194         nil                             ;Nothing to do.
6195       ;; If we have several article buffers, we kill them at exit.
6196       (unless gnus-single-article-buffer
6197         (gnus-kill-buffer gnus-article-buffer)
6198         (gnus-kill-buffer gnus-original-article-buffer)
6199         (setq gnus-article-current nil))
6200       (set-buffer buf)
6201       (if (not gnus-kill-summary-on-exit)
6202           (progn
6203             (gnus-deaden-summary)
6204             (setq mode nil))
6205        ;; We set all buffer-local variables to nil.  It is unclear why
6206         ;; this is needed, but if we don't, buffer-local variables are
6207         ;; not garbage-collected, it seems.  This would the lead to en
6208         ;; ever-growing Emacs.
6209         (gnus-summary-clear-local-variables)
6210         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6211           (gnus-summary-clear-local-variables))
6212         (when (get-buffer gnus-article-buffer)
6213           (bury-buffer gnus-article-buffer))
6214         ;; We clear the global counterparts of the buffer-local
6215         ;; variables as well, just to be on the safe side.
6216         (set-buffer gnus-group-buffer)
6217         (gnus-summary-clear-local-variables)
6218         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6219           (gnus-summary-clear-local-variables)))
6220       (setq gnus-current-select-method gnus-select-method)
6221       (pop-to-buffer gnus-group-buffer)
6222       (if (not quit-config)
6223           (progn
6224             (goto-char group-point)
6225             (gnus-configure-windows 'group 'force))
6226         (gnus-handle-ephemeral-exit quit-config))
6227       ;; Return to group mode buffer.
6228       (when (eq mode 'gnus-summary-mode)
6229         (gnus-kill-buffer buf))
6230       ;; Clear the current group name.
6231       (unless quit-config
6232         (setq gnus-newsgroup-name nil)))))
6233
6234 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6235 (defun gnus-summary-exit-no-update (&optional no-questions)
6236   "Quit reading current newsgroup without updating read article info."
6237   (interactive)
6238   (let* ((group gnus-newsgroup-name)
6239          (quit-config (gnus-group-quit-config group)))
6240     (when (or no-questions
6241               gnus-expert-user
6242               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6243       (gnus-async-halt-prefetch)
6244       (mapcar 'funcall
6245               (delq 'gnus-summary-expire-articles
6246                     (copy-sequence gnus-summary-prepare-exit-hook)))
6247       (when (gnus-buffer-live-p gnus-article-buffer)
6248         (save-excursion
6249           (set-buffer gnus-article-buffer)
6250           (mm-destroy-parts gnus-article-mime-handles)
6251           ;; Set it to nil for safety reason.
6252           (setq gnus-article-mime-handle-alist nil)
6253           (setq gnus-article-mime-handles nil)))
6254       ;; If we have several article buffers, we kill them at exit.
6255       (unless gnus-single-article-buffer
6256         (gnus-kill-buffer gnus-article-buffer)
6257         (gnus-kill-buffer gnus-original-article-buffer)
6258         (setq gnus-article-current nil))
6259       (if (not gnus-kill-summary-on-exit)
6260           (gnus-deaden-summary)
6261         (gnus-close-group group)
6262         (gnus-summary-clear-local-variables)
6263         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6264           (gnus-summary-clear-local-variables))
6265         (set-buffer gnus-group-buffer)
6266         (gnus-summary-clear-local-variables)
6267         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6268           (gnus-summary-clear-local-variables))
6269         (when (get-buffer gnus-summary-buffer)
6270           (kill-buffer gnus-summary-buffer)))
6271       (unless gnus-single-article-buffer
6272         (setq gnus-article-current nil))
6273       (when gnus-use-trees
6274         (gnus-tree-close group))
6275       (gnus-async-prefetch-remove-group group)
6276       (when (get-buffer gnus-article-buffer)
6277         (bury-buffer gnus-article-buffer))
6278       ;; Return to the group buffer.
6279       (gnus-configure-windows 'group 'force)
6280       ;; Clear the current group name.
6281       (setq gnus-newsgroup-name nil)
6282       (when (equal (gnus-group-group-name) group)
6283         (gnus-group-next-unread-group 1))
6284       (when quit-config
6285         (gnus-handle-ephemeral-exit quit-config)))))
6286
6287 (defun gnus-handle-ephemeral-exit (quit-config)
6288   "Handle movement when leaving an ephemeral group.
6289 The state which existed when entering the ephemeral is reset."
6290   (if (not (buffer-name (car quit-config)))
6291       (gnus-configure-windows 'group 'force)
6292     (set-buffer (car quit-config))
6293     (cond ((eq major-mode 'gnus-summary-mode)
6294            (gnus-set-global-variables))
6295           ((eq major-mode 'gnus-article-mode)
6296            (save-excursion
6297              ;; The `gnus-summary-buffer' variable may point
6298              ;; to the old summary buffer when using a single
6299              ;; article buffer.
6300              (unless (gnus-buffer-live-p gnus-summary-buffer)
6301                (set-buffer gnus-group-buffer))
6302              (set-buffer gnus-summary-buffer)
6303              (gnus-set-global-variables))))
6304     (if (or (eq (cdr quit-config) 'article)
6305             (eq (cdr quit-config) 'pick))
6306         (progn
6307           ;; The current article may be from the ephemeral group
6308           ;; thus it is best that we reload this article
6309           (gnus-summary-show-article)
6310           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6311               (gnus-configure-windows 'pick 'force)
6312             (gnus-configure-windows (cdr quit-config) 'force)))
6313       (gnus-configure-windows (cdr quit-config) 'force))
6314     (when (eq major-mode 'gnus-summary-mode)
6315       (gnus-summary-next-subject 1 nil t)
6316       (gnus-summary-recenter)
6317       (gnus-summary-position-point))))
6318
6319 ;;; Dead summaries.
6320
6321 (defvar gnus-dead-summary-mode-map nil)
6322
6323 (unless gnus-dead-summary-mode-map
6324   (setq gnus-dead-summary-mode-map (make-keymap))
6325   (suppress-keymap gnus-dead-summary-mode-map)
6326   (substitute-key-definition
6327    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6328   (dolist (key '("\C-d" "\r" "\177" [delete]))
6329     (define-key gnus-dead-summary-mode-map
6330       key 'gnus-summary-wake-up-the-dead))
6331   (dolist (key '("q" "Q"))
6332     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6333
6334 (defvar gnus-dead-summary-mode nil
6335   "Minor mode for Gnus summary buffers.")
6336
6337 (defun gnus-dead-summary-mode (&optional arg)
6338   "Minor mode for Gnus summary buffers."
6339   (interactive "P")
6340   (when (eq major-mode 'gnus-summary-mode)
6341     (make-local-variable 'gnus-dead-summary-mode)
6342     (setq gnus-dead-summary-mode
6343           (if (null arg) (not gnus-dead-summary-mode)
6344             (> (prefix-numeric-value arg) 0)))
6345     (when gnus-dead-summary-mode
6346       (gnus-add-minor-mode
6347        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6348
6349 (defun gnus-deaden-summary ()
6350   "Make the current summary buffer into a dead summary buffer."
6351   ;; Kill any previous dead summary buffer.
6352   (when (and gnus-dead-summary
6353              (buffer-name gnus-dead-summary))
6354     (save-excursion
6355       (set-buffer gnus-dead-summary)
6356       (when gnus-dead-summary-mode
6357         (kill-buffer (current-buffer)))))
6358   ;; Make this the current dead summary.
6359   (setq gnus-dead-summary (current-buffer))
6360   (gnus-dead-summary-mode 1)
6361   (let ((name (buffer-name)))
6362     (when (string-match "Summary" name)
6363       (rename-buffer
6364        (concat (substring name 0 (match-beginning 0)) "Dead "
6365                (substring name (match-beginning 0)))
6366        t)
6367       (bury-buffer))))
6368
6369 (defun gnus-kill-or-deaden-summary (buffer)
6370   "Kill or deaden the summary BUFFER."
6371   (save-excursion
6372     (when (and (buffer-name buffer)
6373                (not gnus-single-article-buffer))
6374       (save-excursion
6375         (set-buffer buffer)
6376         (gnus-kill-buffer gnus-article-buffer)
6377         (gnus-kill-buffer gnus-original-article-buffer)))
6378     (cond
6379      ;; Kill the buffer.
6380      (gnus-kill-summary-on-exit
6381       (when (and gnus-use-trees
6382                  (gnus-buffer-exists-p buffer))
6383         (save-excursion
6384           (set-buffer buffer)
6385           (gnus-tree-close gnus-newsgroup-name)))
6386       (gnus-kill-buffer buffer))
6387      ;; Deaden the buffer.
6388      ((gnus-buffer-exists-p buffer)
6389       (save-excursion
6390         (set-buffer buffer)
6391         (gnus-deaden-summary))))))
6392
6393 (defun gnus-summary-wake-up-the-dead (&rest args)
6394   "Wake up the dead summary buffer."
6395   (interactive)
6396   (gnus-dead-summary-mode -1)
6397   (let ((name (buffer-name)))
6398     (when (string-match "Dead " name)
6399       (rename-buffer
6400        (concat (substring name 0 (match-beginning 0))
6401                (substring name (match-end 0)))
6402        t)))
6403   (gnus-message 3 "This dead summary is now alive again"))
6404
6405 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6406 (defun gnus-summary-fetch-faq (&optional faq-dir)
6407   "Fetch the FAQ for the current group.
6408 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6409 in."
6410   (interactive
6411    (list
6412     (when current-prefix-arg
6413       (completing-read
6414        "Faq dir: " (and (listp gnus-group-faq-directory)
6415                         (mapcar (lambda (file) (list file))
6416                                 gnus-group-faq-directory))))))
6417   (let (gnus-faq-buffer)
6418     (when (setq gnus-faq-buffer
6419                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6420       (gnus-configure-windows 'summary-faq))))
6421
6422 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6423 (defun gnus-summary-describe-group (&optional force)
6424   "Describe the current newsgroup."
6425   (interactive "P")
6426   (gnus-group-describe-group force gnus-newsgroup-name))
6427
6428 (defun gnus-summary-describe-briefly ()
6429   "Describe summary mode commands briefly."
6430   (interactive)
6431   (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")))
6432
6433 ;; Walking around group mode buffer from summary mode.
6434
6435 (defun gnus-summary-next-group (&optional no-article target-group backward)
6436   "Exit current newsgroup and then select next unread newsgroup.
6437 If prefix argument NO-ARTICLE is non-nil, no article is selected
6438 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6439 previous group instead."
6440   (interactive "P")
6441   ;; Stop pre-fetching.
6442   (gnus-async-halt-prefetch)
6443   (let ((current-group gnus-newsgroup-name)
6444         (current-buffer (current-buffer))
6445         entered)
6446    ;; First we semi-exit this group to update Xrefs and all variables.
6447     ;; We can't do a real exit, because the window conf must remain
6448     ;; the same in case the user is prompted for info, and we don't
6449     ;; want the window conf to change before that...
6450     (gnus-summary-exit t)
6451     (while (not entered)
6452       ;; Then we find what group we are supposed to enter.
6453       (set-buffer gnus-group-buffer)
6454       (gnus-group-jump-to-group current-group)
6455       (setq target-group
6456             (or target-group
6457                 (if (eq gnus-keep-same-level 'best)
6458                     (gnus-summary-best-group gnus-newsgroup-name)
6459                   (gnus-summary-search-group backward gnus-keep-same-level))))
6460       (if (not target-group)
6461           ;; There are no further groups, so we return to the group
6462           ;; buffer.
6463           (progn
6464             (gnus-message 5 "Returning to the group buffer")
6465             (setq entered t)
6466             (when (gnus-buffer-live-p current-buffer)
6467               (set-buffer current-buffer)
6468               (gnus-summary-exit))
6469             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6470         ;; We try to enter the target group.
6471         (gnus-group-jump-to-group target-group)
6472         (let ((unreads (gnus-group-group-unread)))
6473           (if (and (or (eq t unreads)
6474                        (and unreads (not (zerop unreads))))
6475                    (gnus-summary-read-group
6476                     target-group nil no-article
6477                     (and (buffer-name current-buffer) current-buffer)
6478                     nil backward))
6479               (setq entered t)
6480             (setq current-group target-group
6481                   target-group nil)))))))
6482
6483 (defun gnus-summary-prev-group (&optional no-article)
6484   "Exit current newsgroup and then select previous unread newsgroup.
6485 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6486   (interactive "P")
6487   (gnus-summary-next-group no-article nil t))
6488
6489 ;; Walking around summary lines.
6490
6491 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6492   "Go to the first unread subject.
6493 If UNREAD is non-nil, go to the first unread article.
6494 Returns the article selected or nil if there are no unread articles."
6495   (interactive "P")
6496   (prog1
6497       (cond
6498        ;; Empty summary.
6499        ((null gnus-newsgroup-data)
6500         (gnus-message 3 "No articles in the group")
6501         nil)
6502        ;; Pick the first article.
6503        ((not unread)
6504         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6505         (gnus-data-number (car gnus-newsgroup-data)))
6506        ;; No unread articles.
6507        ((null gnus-newsgroup-unreads)
6508         (gnus-message 3 "No more unread articles")
6509         nil)
6510        ;; Find the first unread article.
6511        (t
6512         (let ((data gnus-newsgroup-data))
6513           (while (and data
6514                       (and (not (and undownloaded
6515                                      (eq gnus-undownloaded-mark
6516                                          (gnus-data-mark (car data)))))
6517                            (if unseen
6518                                (or (not (memq
6519                                          (gnus-data-number (car data))
6520                                          gnus-newsgroup-unseen))
6521                                    (not (gnus-data-unread-p (car data))))
6522                              (not (gnus-data-unread-p (car data))))))
6523             (setq data (cdr data)))
6524           (when data
6525             (goto-char (gnus-data-pos (car data)))
6526             (gnus-data-number (car data))))))
6527     (gnus-summary-position-point)))
6528
6529 (defun gnus-summary-next-subject (n &optional unread dont-display)
6530   "Go to next N'th summary line.
6531 If N is negative, go to the previous N'th subject line.
6532 If UNREAD is non-nil, only unread articles are selected.
6533 The difference between N and the actual number of steps taken is
6534 returned."
6535   (interactive "p")
6536   (let ((backward (< n 0))
6537         (n (abs n)))
6538     (while (and (> n 0)
6539                 (if backward
6540                     (gnus-summary-find-prev unread)
6541                   (gnus-summary-find-next unread)))
6542       (unless (zerop (setq n (1- n)))
6543         (gnus-summary-show-thread)))
6544     (when (/= 0 n)
6545       (gnus-message 7 "No more%s articles"
6546                     (if unread " unread" "")))
6547     (unless dont-display
6548       (gnus-summary-recenter)
6549       (gnus-summary-position-point))
6550     n))
6551
6552 (defun gnus-summary-next-unread-subject (n)
6553   "Go to next N'th unread summary line."
6554   (interactive "p")
6555   (gnus-summary-next-subject n t))
6556
6557 (defun gnus-summary-prev-subject (n &optional unread)
6558   "Go to previous N'th summary line.
6559 If optional argument UNREAD is non-nil, only unread article is selected."
6560   (interactive "p")
6561   (gnus-summary-next-subject (- n) unread))
6562
6563 (defun gnus-summary-prev-unread-subject (n)
6564   "Go to previous N'th unread summary line."
6565   (interactive "p")
6566   (gnus-summary-next-subject (- n) t))
6567
6568 (defun gnus-summary-goto-subject (article &optional force silent)
6569   "Go the subject line of ARTICLE.
6570 If FORCE, also allow jumping to articles not currently shown."
6571   (interactive "nArticle number: ")
6572   (unless (numberp article)
6573     (error "Article %s is not a number" article))
6574   (let ((b (point))
6575         (data (gnus-data-find article)))
6576     ;; We read in the article if we have to.
6577     (and (not data)
6578          force
6579          (gnus-summary-insert-subject
6580           article
6581           (if (or (numberp force) (vectorp force)) force)
6582           t)
6583          (setq data (gnus-data-find article)))
6584     (goto-char b)
6585     (if (not data)
6586         (progn
6587           (unless silent
6588             (gnus-message 3 "Can't find article %d" article))
6589           nil)
6590       (let ((pt (gnus-data-pos data)))
6591         (goto-char pt)
6592         (gnus-summary-set-article-display-arrow pt))
6593       (gnus-summary-position-point)
6594       article)))
6595
6596 ;; Walking around summary lines with displaying articles.
6597
6598 (defun gnus-summary-expand-window (&optional arg)
6599   "Make the summary buffer take up the entire Emacs frame.
6600 Given a prefix, will force an `article' buffer configuration."
6601   (interactive "P")
6602   (if arg
6603       (gnus-configure-windows 'article 'force)
6604     (gnus-configure-windows 'summary 'force)))
6605
6606 (defun gnus-summary-display-article (article &optional all-header)
6607   "Display ARTICLE in article buffer."
6608   (when (gnus-buffer-live-p gnus-article-buffer)
6609     (with-current-buffer gnus-article-buffer
6610       (mm-enable-multibyte)))
6611   (gnus-set-global-variables)
6612   (when (gnus-buffer-live-p gnus-article-buffer)
6613     (with-current-buffer gnus-article-buffer
6614       (setq gnus-article-charset gnus-newsgroup-charset)
6615       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6616       (mm-enable-multibyte)))
6617   (if (null article)
6618       nil
6619     (prog1
6620         (if gnus-summary-display-article-function
6621             (funcall gnus-summary-display-article-function article all-header)
6622           (gnus-article-prepare article all-header))
6623       (gnus-run-hooks 'gnus-select-article-hook)
6624       (when (and gnus-current-article
6625                  (not (zerop gnus-current-article)))
6626         (gnus-summary-goto-subject gnus-current-article))
6627       (gnus-summary-recenter)
6628       (when (and gnus-use-trees gnus-show-threads)
6629         (gnus-possibly-generate-tree article)
6630         (gnus-highlight-selected-tree article))
6631       ;; Successfully display article.
6632       (gnus-article-set-window-start
6633        (cdr (assq article gnus-newsgroup-bookmarks))))))
6634
6635 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6636   "Select the current article.
6637 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6638 non-nil, the article will be re-fetched even if it already present in
6639 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6640 be displayed."
6641   ;; Make sure we are in the summary buffer to work around bbdb bug.
6642   (unless (eq major-mode 'gnus-summary-mode)
6643     (set-buffer gnus-summary-buffer))
6644   (let ((article (or article (gnus-summary-article-number)))
6645         (all-headers (not (not all-headers))) ;Must be t or nil.
6646         gnus-summary-display-article-function)
6647     (and (not pseudo)
6648          (gnus-summary-article-pseudo-p article)
6649          (error "This is a pseudo-article"))
6650     (save-excursion
6651       (set-buffer gnus-summary-buffer)
6652       (if (or (and gnus-single-article-buffer
6653                    (or (null gnus-current-article)
6654                        (null gnus-article-current)
6655                        (null (get-buffer gnus-article-buffer))
6656                        (not (eq article (cdr gnus-article-current)))
6657                        (not (equal (car gnus-article-current)
6658                                    gnus-newsgroup-name))))
6659               (and (not gnus-single-article-buffer)
6660                    (or (null gnus-current-article)
6661                        (not (eq gnus-current-article article))))
6662               force)
6663           ;; The requested article is different from the current article.
6664           (progn
6665             (gnus-summary-display-article article all-headers)
6666             (when (gnus-buffer-live-p gnus-article-buffer)
6667               (with-current-buffer gnus-article-buffer
6668                 (if (not gnus-article-decoded-p) ;; a local variable
6669                     (mm-disable-multibyte))))
6670             (gnus-article-set-window-start
6671              (cdr (assq article gnus-newsgroup-bookmarks)))
6672             article)
6673         'old))))
6674
6675 (defun gnus-summary-force-verify-and-decrypt ()
6676   (interactive)
6677   (let ((mm-verify-option 'known)
6678         (mm-decrypt-option 'known))
6679     (gnus-summary-select-article nil 'force)))
6680
6681 (defun gnus-summary-set-current-mark (&optional current-mark)
6682   "Obsolete function."
6683   nil)
6684
6685 (defun gnus-summary-next-article (&optional unread subject backward push)
6686   "Select the next article.
6687 If UNREAD, only unread articles are selected.
6688 If SUBJECT, only articles with SUBJECT are selected.
6689 If BACKWARD, the previous article is selected instead of the next."
6690   (interactive "P")
6691   (cond
6692    ;; Is there such an article?
6693    ((and (gnus-summary-search-forward unread subject backward)
6694          (or (gnus-summary-display-article (gnus-summary-article-number))
6695              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6696     (gnus-summary-position-point))
6697    ;; If not, we try the first unread, if that is wanted.
6698    ((and subject
6699          gnus-auto-select-same
6700          (gnus-summary-first-unread-article))
6701     (gnus-summary-position-point)
6702     (gnus-message 6 "Wrapped"))
6703    ;; Try to get next/previous article not displayed in this group.
6704    ((and gnus-auto-extend-newsgroup
6705          (not unread) (not subject))
6706     (gnus-summary-goto-article
6707      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6708      nil (count-lines (point-min) (point))))
6709    ;; Go to next/previous group.
6710    (t
6711     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6712       (gnus-summary-jump-to-group gnus-newsgroup-name))
6713     (let ((cmd last-command-char)
6714           (point
6715            (save-excursion
6716              (set-buffer gnus-group-buffer)
6717              (point)))
6718           (group
6719            (if (eq gnus-keep-same-level 'best)
6720                (gnus-summary-best-group gnus-newsgroup-name)
6721              (gnus-summary-search-group backward gnus-keep-same-level))))
6722       ;; For some reason, the group window gets selected.  We change
6723       ;; it back.
6724       (select-window (get-buffer-window (current-buffer)))
6725       ;; Select next unread newsgroup automagically.
6726       (cond
6727        ((or (not gnus-auto-select-next)
6728             (not cmd))
6729         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6730        ((or (eq gnus-auto-select-next 'quietly)
6731             (and (eq gnus-auto-select-next 'slightly-quietly)
6732                  push)
6733             (and (eq gnus-auto-select-next 'almost-quietly)
6734                  (gnus-summary-last-article-p)))
6735         ;; Select quietly.
6736         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6737             (gnus-summary-exit)
6738           (gnus-message 7 "No more%s articles (%s)..."
6739                         (if unread " unread" "")
6740                         (if group (concat "selecting " group)
6741                           "exiting"))
6742           (gnus-summary-next-group nil group backward)))
6743        (t
6744         (when (gnus-key-press-event-p last-input-event)
6745           (gnus-summary-walk-group-buffer
6746            gnus-newsgroup-name cmd unread backward point))))))))
6747
6748 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6749   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6750                       (?\C-p (gnus-group-prev-unread-group 1))))
6751         (cursor-in-echo-area t)
6752         keve key group ended)
6753     (save-excursion
6754       (set-buffer gnus-group-buffer)
6755       (goto-char start)
6756       (setq group
6757             (if (eq gnus-keep-same-level 'best)
6758                 (gnus-summary-best-group gnus-newsgroup-name)
6759               (gnus-summary-search-group backward gnus-keep-same-level))))
6760     (while (not ended)
6761       (gnus-message
6762        5 "No more%s articles%s" (if unread " unread" "")
6763        (if (and group
6764                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6765            (format " (Type %s for %s [%s])"
6766                    (single-key-description cmd) group
6767                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6768          (format " (Type %s to exit %s)"
6769                  (single-key-description cmd)
6770                  gnus-newsgroup-name)))
6771       ;; Confirm auto selection.
6772       (setq key (car (setq keve (gnus-read-event-char))))
6773       (setq ended t)
6774       (cond
6775        ((assq key keystrokes)
6776         (let ((obuf (current-buffer)))
6777           (switch-to-buffer gnus-group-buffer)
6778           (when group
6779             (gnus-group-jump-to-group group))
6780           (eval (cadr (assq key keystrokes)))
6781           (setq group (gnus-group-group-name))
6782           (switch-to-buffer obuf))
6783         (setq ended nil))
6784        ((equal key cmd)
6785         (if (or (not group)
6786                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6787             (gnus-summary-exit)
6788           (gnus-summary-next-group nil group backward)))
6789        (t
6790         (push (cdr keve) unread-command-events))))))
6791
6792 (defun gnus-summary-next-unread-article ()
6793   "Select unread article after current one."
6794   (interactive)
6795   (gnus-summary-next-article
6796    (or (not (eq gnus-summary-goto-unread 'never))
6797        (gnus-summary-last-article-p (gnus-summary-article-number)))
6798    (and gnus-auto-select-same
6799         (gnus-summary-article-subject))))
6800
6801 (defun gnus-summary-prev-article (&optional unread subject)
6802   "Select the article after the current one.
6803 If UNREAD is non-nil, only unread articles are selected."
6804   (interactive "P")
6805   (gnus-summary-next-article unread subject t))
6806
6807 (defun gnus-summary-prev-unread-article ()
6808   "Select unread article before current one."
6809   (interactive)
6810   (gnus-summary-prev-article
6811    (or (not (eq gnus-summary-goto-unread 'never))
6812        (gnus-summary-first-article-p (gnus-summary-article-number)))
6813    (and gnus-auto-select-same
6814         (gnus-summary-article-subject))))
6815
6816 (defun gnus-summary-next-page (&optional lines circular)
6817   "Show next page of the selected article.
6818 If at the end of the current article, select the next article.
6819 LINES says how many lines should be scrolled up.
6820
6821 If CIRCULAR is non-nil, go to the start of the article instead of
6822 selecting the next article when reaching the end of the current
6823 article."
6824   (interactive "P")
6825   (setq gnus-summary-buffer (current-buffer))
6826   (gnus-set-global-variables)
6827   (let ((article (gnus-summary-article-number))
6828         (article-window (get-buffer-window gnus-article-buffer t))
6829         endp)
6830     ;; If the buffer is empty, we have no article.
6831     (unless article
6832       (error "No article to select"))
6833     (gnus-configure-windows 'article)
6834     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6835         (if (and (eq gnus-summary-goto-unread 'never)
6836                  (not (gnus-summary-last-article-p article)))
6837             (gnus-summary-next-article)
6838           (gnus-summary-next-unread-article))
6839       (if (or (null gnus-current-article)
6840               (null gnus-article-current)
6841               (/= article (cdr gnus-article-current))
6842               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6843           ;; Selected subject is different from current article's.
6844           (gnus-summary-display-article article)
6845         (when article-window
6846           (gnus-eval-in-buffer-window gnus-article-buffer
6847             (setq endp (gnus-article-next-page lines)))
6848           (when endp
6849             (cond (circular
6850                    (gnus-summary-beginning-of-article))
6851                   (lines
6852                    (gnus-message 3 "End of message"))
6853                   ((null lines)
6854                    (if (and (eq gnus-summary-goto-unread 'never)
6855                             (not (gnus-summary-last-article-p article)))
6856                        (gnus-summary-next-article)
6857                      (gnus-summary-next-unread-article))))))))
6858     (gnus-summary-recenter)
6859     (gnus-summary-position-point)))
6860
6861 (defun gnus-summary-prev-page (&optional lines move)
6862   "Show previous page of selected article.
6863 Argument LINES specifies lines to be scrolled down.
6864 If MOVE, move to the previous unread article if point is at
6865 the beginning of the buffer."
6866   (interactive "P")
6867   (let ((article (gnus-summary-article-number))
6868         (article-window (get-buffer-window gnus-article-buffer t))
6869         endp)
6870     (gnus-configure-windows 'article)
6871     (if (or (null gnus-current-article)
6872             (null gnus-article-current)
6873             (/= article (cdr gnus-article-current))
6874             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6875         ;; Selected subject is different from current article's.
6876         (gnus-summary-display-article article)
6877       (gnus-summary-recenter)
6878       (when article-window
6879         (gnus-eval-in-buffer-window gnus-article-buffer
6880           (setq endp (gnus-article-prev-page lines)))
6881         (when (and move endp)
6882           (cond (lines
6883                  (gnus-message 3 "Beginning of message"))
6884                 ((null lines)
6885                  (if (and (eq gnus-summary-goto-unread 'never)
6886                           (not (gnus-summary-first-article-p article)))
6887                      (gnus-summary-prev-article)
6888                    (gnus-summary-prev-unread-article))))))))
6889   (gnus-summary-position-point))
6890
6891 (defun gnus-summary-prev-page-or-article (&optional lines)
6892   "Show previous page of selected article.
6893 Argument LINES specifies lines to be scrolled down.
6894 If at the beginning of the article, go to the next article."
6895   (interactive "P")
6896   (gnus-summary-prev-page lines t))
6897
6898 (defun gnus-summary-scroll-up (lines)
6899   "Scroll up (or down) one line current article.
6900 Argument LINES specifies lines to be scrolled up (or down if negative)."
6901   (interactive "p")
6902   (gnus-configure-windows 'article)
6903   (gnus-summary-show-thread)
6904   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6905     (gnus-eval-in-buffer-window gnus-article-buffer
6906       (cond ((> lines 0)
6907              (when (gnus-article-next-page lines)
6908                (gnus-message 3 "End of message")))
6909             ((< lines 0)
6910              (gnus-article-prev-page (- lines))))))
6911   (gnus-summary-recenter)
6912   (gnus-summary-position-point))
6913
6914 (defun gnus-summary-scroll-down (lines)
6915   "Scroll down (or up) one line current article.
6916 Argument LINES specifies lines to be scrolled down (or up if negative)."
6917   (interactive "p")
6918   (gnus-summary-scroll-up (- lines)))
6919
6920 (defun gnus-summary-next-same-subject ()
6921   "Select next article which has the same subject as current one."
6922   (interactive)
6923   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6924
6925 (defun gnus-summary-prev-same-subject ()
6926   "Select previous article which has the same subject as current one."
6927   (interactive)
6928   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6929
6930 (defun gnus-summary-next-unread-same-subject ()
6931   "Select next unread article which has the same subject as current one."
6932   (interactive)
6933   (gnus-summary-next-article t (gnus-summary-article-subject)))
6934
6935 (defun gnus-summary-prev-unread-same-subject ()
6936   "Select previous unread article which has the same subject as current one."
6937   (interactive)
6938   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6939
6940 (defun gnus-summary-first-unread-article ()
6941   "Select the first unread article.
6942 Return nil if there are no unread articles."
6943   (interactive)
6944   (prog1
6945       (when (gnus-summary-first-subject t)
6946         (gnus-summary-show-thread)
6947         (gnus-summary-first-subject t)
6948         (gnus-summary-display-article (gnus-summary-article-number)))
6949     (gnus-summary-position-point)))
6950
6951 (defun gnus-summary-first-unread-subject ()
6952   "Place the point on the subject line of the first unread article.
6953 Return nil if there are no unread articles."
6954   (interactive)
6955   (prog1
6956       (when (gnus-summary-first-subject t)
6957         (gnus-summary-show-thread)
6958         (gnus-summary-first-subject t))
6959     (gnus-summary-position-point)))
6960
6961 (defun gnus-summary-first-unseen-subject ()
6962   "Place the point on the subject line of the first unseen article.
6963 Return nil if there are no unseen articles."
6964   (interactive)
6965   (prog1
6966       (when (gnus-summary-first-subject t t t)
6967         (gnus-summary-show-thread)
6968         (gnus-summary-first-subject t t t))
6969     (gnus-summary-position-point)))
6970
6971 (defun gnus-summary-first-unseen-or-unread-subject ()
6972   "Place the point on the subject line of the first unseen article.
6973 Return nil if there are no unseen articles."
6974   (interactive)
6975   (prog1
6976       (unless (when (gnus-summary-first-subject t t t)
6977                 (gnus-summary-show-thread)
6978                 (gnus-summary-first-subject t t t))
6979         (when (gnus-summary-first-subject t)
6980           (gnus-summary-show-thread)
6981           (gnus-summary-first-subject t)))
6982     (gnus-summary-position-point)))
6983
6984 (defun gnus-summary-first-article ()
6985   "Select the first article.
6986 Return nil if there are no articles."
6987   (interactive)
6988   (prog1
6989       (when (gnus-summary-first-subject)
6990         (gnus-summary-show-thread)
6991         (gnus-summary-first-subject)
6992         (gnus-summary-display-article (gnus-summary-article-number)))
6993     (gnus-summary-position-point)))
6994
6995 (defun gnus-summary-best-unread-article (&optional arg)
6996   "Select the unread article with the highest score.
6997 If given a prefix argument, select the next unread article that has a
6998 score higher than the default score."
6999   (interactive "P")
7000   (let ((article (if arg
7001                      (gnus-summary-better-unread-subject)
7002                    (gnus-summary-best-unread-subject))))
7003     (if article
7004         (gnus-summary-goto-article article)
7005       (error "No unread articles"))))
7006
7007 (defun gnus-summary-best-unread-subject ()
7008   "Select the unread subject with the highest score."
7009   (interactive)
7010   (let ((best -1000000)
7011         (data gnus-newsgroup-data)
7012         article score)
7013     (while data
7014       (and (gnus-data-unread-p (car data))
7015            (> (setq score
7016                     (gnus-summary-article-score (gnus-data-number (car data))))
7017               best)
7018            (setq best score
7019                  article (gnus-data-number (car data))))
7020       (setq data (cdr data)))
7021     (when article
7022       (gnus-summary-goto-subject article))
7023     (gnus-summary-position-point)
7024     article))
7025
7026 (defun gnus-summary-better-unread-subject ()
7027   "Select the first unread subject that has a score over the default score."
7028   (interactive)
7029   (let ((data gnus-newsgroup-data)
7030         article score)
7031     (while (and (setq article (gnus-data-number (car data)))
7032                 (or (gnus-data-read-p (car data))
7033                     (not (> (gnus-summary-article-score article)
7034                             gnus-summary-default-score))))
7035       (setq data (cdr data)))
7036     (when article
7037       (gnus-summary-goto-subject article))
7038     (gnus-summary-position-point)
7039     article))
7040
7041 (defun gnus-summary-last-subject ()
7042   "Go to the last displayed subject line in the group."
7043   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7044     (when article
7045       (gnus-summary-goto-subject article))))
7046
7047 (defun gnus-summary-goto-article (article &optional all-headers force)
7048   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7049 If ALL-HEADERS is non-nil, no header lines are hidden.
7050 If FORCE, go to the article even if it isn't displayed.  If FORCE
7051 is a number, it is the line the article is to be displayed on."
7052   (interactive
7053    (list
7054     (completing-read
7055      "Article number or Message-ID: "
7056      (mapcar (lambda (number) (list (int-to-string number)))
7057              gnus-newsgroup-limit))
7058     current-prefix-arg
7059     t))
7060   (prog1
7061       (if (and (stringp article)
7062                (string-match "@" article))
7063           (gnus-summary-refer-article article)
7064         (when (stringp article)
7065           (setq article (string-to-number article)))
7066         (if (gnus-summary-goto-subject article force)
7067             (gnus-summary-display-article article all-headers)
7068           (gnus-message 4 "Couldn't go to article %s" article) nil))
7069     (gnus-summary-position-point)))
7070
7071 (defun gnus-summary-goto-last-article ()
7072   "Go to the previously read article."
7073   (interactive)
7074   (prog1
7075       (when gnus-last-article
7076         (gnus-summary-goto-article gnus-last-article nil t))
7077     (gnus-summary-position-point)))
7078
7079 (defun gnus-summary-pop-article (number)
7080   "Pop one article off the history and go to the previous.
7081 NUMBER articles will be popped off."
7082   (interactive "p")
7083   (let (to)
7084     (setq gnus-newsgroup-history
7085           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7086     (if to
7087         (gnus-summary-goto-article (car to) nil t)
7088       (error "Article history empty")))
7089   (gnus-summary-position-point))
7090
7091 ;; Summary commands and functions for limiting the summary buffer.
7092
7093 (defun gnus-summary-limit-to-articles (n)
7094   "Limit the summary buffer to the next N articles.
7095 If not given a prefix, use the process marked articles instead."
7096   (interactive "P")
7097   (prog1
7098       (let ((articles (gnus-summary-work-articles n)))
7099         (setq gnus-newsgroup-processable nil)
7100         (gnus-summary-limit articles))
7101     (gnus-summary-position-point)))
7102
7103 (defun gnus-summary-pop-limit (&optional total)
7104   "Restore the previous limit.
7105 If given a prefix, remove all limits."
7106   (interactive "P")
7107   (when total
7108     (setq gnus-newsgroup-limits
7109           (list (mapcar (lambda (h) (mail-header-number h))
7110                         gnus-newsgroup-headers))))
7111   (unless gnus-newsgroup-limits
7112     (error "No limit to pop"))
7113   (prog1
7114       (gnus-summary-limit nil 'pop)
7115     (gnus-summary-position-point)))
7116
7117 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7118   "Limit the summary buffer to articles that have subjects that match a regexp.
7119 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7120   (interactive
7121    (list (read-string (if current-prefix-arg
7122                           "Exclude subject (regexp): "
7123                         "Limit to subject (regexp): "))
7124          nil current-prefix-arg))
7125   (unless header
7126     (setq header "subject"))
7127   (when (not (equal "" subject))
7128     (prog1
7129         (let ((articles (gnus-summary-find-matching
7130                          (or header "subject") subject 'all nil nil
7131                          not-matching)))
7132           (unless articles
7133             (error "Found no matches for \"%s\"" subject))
7134           (gnus-summary-limit articles))
7135       (gnus-summary-position-point))))
7136
7137 (defun gnus-summary-limit-to-author (from &optional not-matching)
7138   "Limit the summary buffer to articles that have authors that match a regexp.
7139 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7140   (interactive
7141    (list (read-string (if current-prefix-arg
7142                           "Exclude author (regexp): "
7143                         "Limit to author (regexp): "))
7144          current-prefix-arg))
7145   (gnus-summary-limit-to-subject from "from" not-matching))
7146
7147 (defun gnus-summary-limit-to-age (age &optional younger-p)
7148   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7149 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7150 articles that are younger than AGE days."
7151   (interactive
7152    (let ((younger current-prefix-arg)
7153          (days-got nil)
7154          days)
7155      (while (not days-got)
7156        (setq days (if younger
7157                       (read-string "Limit to articles within (in days): ")
7158                     (read-string "Limit to articles older than (in days): ")))
7159        (when (> (length days) 0)
7160          (setq days (read days)))
7161        (if (numberp days)
7162            (progn
7163              (setq days-got t)
7164              (if (< days 0)
7165                  (progn
7166                    (setq younger (not younger))
7167                    (setq days (* days -1)))))
7168          (message "Please enter a number.")
7169          (sleep-for 1)))
7170      (list days younger)))
7171   (prog1
7172       (let ((data gnus-newsgroup-data)
7173             (cutoff (days-to-time age))
7174             articles d date is-younger)
7175         (while (setq d (pop data))
7176           (when (and (vectorp (gnus-data-header d))
7177                      (setq date (mail-header-date (gnus-data-header d))))
7178             (setq is-younger (time-less-p
7179                               (time-since (condition-case ()
7180                                               (date-to-time date)
7181                                             (error '(0 0))))
7182                               cutoff))
7183             (when (if younger-p
7184                       is-younger
7185                     (not is-younger))
7186               (push (gnus-data-number d) articles))))
7187         (gnus-summary-limit (nreverse articles)))
7188     (gnus-summary-position-point)))
7189
7190 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7191   "Limit the summary buffer to articles that match an 'extra' header."
7192   (interactive
7193    (let ((header
7194           (intern
7195            (gnus-completing-read-with-default
7196             (symbol-name (car gnus-extra-headers))
7197             (if current-prefix-arg
7198                 "Exclude extra header:"
7199               "Limit extra header:")
7200             (mapcar (lambda (x)
7201                       (cons (symbol-name x) x))
7202                     gnus-extra-headers)
7203             nil
7204             t))))
7205      (list header
7206            (read-string (format "%s header %s (regexp): "
7207                                 (if current-prefix-arg "Exclude" "Limit to")
7208                                 header))
7209            current-prefix-arg)))
7210   (when (not (equal "" regexp))
7211     (prog1
7212         (let ((articles (gnus-summary-find-matching
7213                          (cons 'extra header) regexp 'all nil nil
7214                          not-matching)))
7215           (unless articles
7216             (error "Found no matches for \"%s\"" regexp))
7217           (gnus-summary-limit articles))
7218       (gnus-summary-position-point))))
7219
7220 (defun gnus-summary-limit-to-display-predicate ()
7221   "Limit the summary buffer to the predicated in the `display' group parameter."
7222   (interactive)
7223   (unless gnus-newsgroup-display
7224     (error "There is no `display' group parameter"))
7225   (let (articles)
7226     (dolist (number gnus-newsgroup-articles)
7227       (when (funcall gnus-newsgroup-display)
7228         (push number articles)))
7229     (gnus-summary-limit articles))
7230   (gnus-summary-position-point))
7231
7232 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7233 (make-obsolete
7234  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7235
7236 (defun gnus-summary-limit-to-unread (&optional all)
7237   "Limit the summary buffer to articles that are not marked as read.
7238 If ALL is non-nil, limit strictly to unread articles."
7239   (interactive "P")
7240   (if all
7241       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7242     (gnus-summary-limit-to-marks
7243      ;; Concat all the marks that say that an article is read and have
7244      ;; those removed.
7245      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7246            gnus-killed-mark gnus-kill-file-mark
7247            gnus-low-score-mark gnus-expirable-mark
7248            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7249            gnus-duplicate-mark gnus-souped-mark)
7250      'reverse)))
7251
7252 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7253 (make-obsolete 'gnus-summary-delete-marked-with
7254                'gnus-summary-limit-exlude-marks)
7255
7256 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7257   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7258 If REVERSE, limit the summary buffer to articles that are marked
7259 with MARKS.  MARKS can either be a string of marks or a list of marks.
7260 Returns how many articles were removed."
7261   (interactive "sMarks: ")
7262   (gnus-summary-limit-to-marks marks t))
7263
7264 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7265   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7266 If REVERSE (the prefix), limit the summary buffer to articles that are
7267 not marked with MARKS.  MARKS can either be a string of marks or a
7268 list of marks.
7269 Returns how many articles were removed."
7270   (interactive "sMarks: \nP")
7271   (prog1
7272       (let ((data gnus-newsgroup-data)
7273             (marks (if (listp marks) marks
7274                      (append marks nil))) ; Transform to list.
7275             articles)
7276         (while data
7277           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7278                   (memq (gnus-data-mark (car data)) marks))
7279             (push (gnus-data-number (car data)) articles))
7280           (setq data (cdr data)))
7281         (gnus-summary-limit articles))
7282     (gnus-summary-position-point)))
7283
7284 (defun gnus-summary-limit-to-score (score)
7285   "Limit to articles with score at or above SCORE."
7286   (interactive "NLimit to articles with score of at least: ")
7287   (let ((data gnus-newsgroup-data)
7288         articles)
7289     (while data
7290       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7291                 score)
7292         (push (gnus-data-number (car data)) articles))
7293       (setq data (cdr data)))
7294     (prog1
7295         (gnus-summary-limit articles)
7296       (gnus-summary-position-point))))
7297
7298 (defun gnus-summary-limit-include-thread (id)
7299   "Display all the hidden articles that is in the thread with ID in it.
7300 When called interactively, ID is the Message-ID of the current
7301 article."
7302   (interactive (list (mail-header-id (gnus-summary-article-header))))
7303   (let ((articles (gnus-articles-in-thread
7304                    (gnus-id-to-thread (gnus-root-id id)))))
7305     (prog1
7306         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7307       (gnus-summary-limit-include-matching-articles
7308        "subject"
7309        (regexp-quote (gnus-simplify-subject-re
7310                       (mail-header-subject (gnus-id-to-header id)))))
7311       (gnus-summary-position-point))))
7312
7313 (defun gnus-summary-limit-include-matching-articles (header regexp)
7314   "Display all the hidden articles that have HEADERs that match REGEXP."
7315   (interactive (list (read-string "Match on header: ")
7316                      (read-string "Regexp: ")))
7317   (let ((articles (gnus-find-matching-articles header regexp)))
7318     (prog1
7319         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7320       (gnus-summary-position-point))))
7321
7322 (defun gnus-summary-limit-include-dormant ()
7323   "Display all the hidden articles that are marked as dormant.
7324 Note that this command only works on a subset of the articles currently
7325 fetched for this group."
7326   (interactive)
7327   (unless gnus-newsgroup-dormant
7328     (error "There are no dormant articles in this group"))
7329   (prog1
7330       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7331     (gnus-summary-position-point)))
7332
7333 (defun gnus-summary-limit-exclude-dormant ()
7334   "Hide all dormant articles."
7335   (interactive)
7336   (prog1
7337       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7338     (gnus-summary-position-point)))
7339
7340 (defun gnus-summary-limit-exclude-childless-dormant ()
7341   "Hide all dormant articles that have no children."
7342   (interactive)
7343   (let ((data (gnus-data-list t))
7344         articles d children)
7345     ;; Find all articles that are either not dormant or have
7346     ;; children.
7347     (while (setq d (pop data))
7348       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7349                 (and (setq children
7350                            (gnus-article-children (gnus-data-number d)))
7351                      (let (found)
7352                        (while children
7353                          (when (memq (car children) articles)
7354                            (setq children nil
7355                                  found t))
7356                          (pop children))
7357                        found)))
7358         (push (gnus-data-number d) articles)))
7359     ;; Do the limiting.
7360     (prog1
7361         (gnus-summary-limit articles)
7362       (gnus-summary-position-point))))
7363
7364 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7365   "Mark all unread excluded articles as read.
7366 If ALL, mark even excluded ticked and dormants as read."
7367   (interactive "P")
7368   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7369   (let ((articles (gnus-sorted-ndifference
7370                    (sort
7371                     (mapcar (lambda (h) (mail-header-number h))
7372                             gnus-newsgroup-headers)
7373                     '<)
7374                    gnus-newsgroup-limit))
7375         article)
7376     (setq gnus-newsgroup-unreads
7377           (gnus-sorted-intersection gnus-newsgroup-unreads
7378                                     gnus-newsgroup-limit))
7379     (if all
7380         (setq gnus-newsgroup-dormant nil
7381               gnus-newsgroup-marked nil
7382               gnus-newsgroup-reads
7383               (nconc
7384                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7385                gnus-newsgroup-reads))
7386       (while (setq article (pop articles))
7387         (unless (or (memq article gnus-newsgroup-dormant)
7388                     (memq article gnus-newsgroup-marked))
7389           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7390
7391 (defun gnus-summary-limit (articles &optional pop)
7392   (if pop
7393       ;; We pop the previous limit off the stack and use that.
7394       (setq articles (car gnus-newsgroup-limits)
7395             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7396     ;; We use the new limit, so we push the old limit on the stack.
7397     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7398   ;; Set the limit.
7399   (setq gnus-newsgroup-limit articles)
7400   (let ((total (length gnus-newsgroup-data))
7401         (data (gnus-data-find-list (gnus-summary-article-number)))
7402         (gnus-summary-mark-below nil)   ; Inhibit this.
7403         found)
7404     ;; This will do all the work of generating the new summary buffer
7405     ;; according to the new limit.
7406     (gnus-summary-prepare)
7407     ;; Hide any threads, possibly.
7408     (gnus-summary-maybe-hide-threads)
7409     ;; Try to return to the article you were at, or one in the
7410     ;; neighborhood.
7411     (when data
7412       ;; We try to find some article after the current one.
7413       (while data
7414         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7415           (setq data nil
7416                 found t))
7417         (setq data (cdr data))))
7418     (unless found
7419       ;; If there is no data, that means that we were after the last
7420       ;; article.  The same goes when we can't find any articles
7421       ;; after the current one.
7422       (goto-char (point-max))
7423       (gnus-summary-find-prev))
7424     (gnus-set-mode-line 'summary)
7425     ;; We return how many articles were removed from the summary
7426     ;; buffer as a result of the new limit.
7427     (- total (length gnus-newsgroup-data))))
7428
7429 (defsubst gnus-invisible-cut-children (threads)
7430   (let ((num 0))
7431     (while threads
7432       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7433         (incf num))
7434       (pop threads))
7435     (< num 2)))
7436
7437 (defsubst gnus-cut-thread (thread)
7438   "Go forwards in the thread until we find an article that we want to display."
7439   (when (or (eq gnus-fetch-old-headers 'some)
7440             (eq gnus-fetch-old-headers 'invisible)
7441             (numberp gnus-fetch-old-headers)
7442             (eq gnus-build-sparse-threads 'some)
7443             (eq gnus-build-sparse-threads 'more))
7444     ;; Deal with old-fetched headers and sparse threads.
7445     (while (and
7446             thread
7447             (or
7448              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7449              (gnus-summary-article-ancient-p
7450               (mail-header-number (car thread))))
7451             (if (or (<= (length (cdr thread)) 1)
7452                     (eq gnus-fetch-old-headers 'invisible))
7453                 (setq gnus-newsgroup-limit
7454                       (delq (mail-header-number (car thread))
7455                             gnus-newsgroup-limit)
7456                       thread (cadr thread))
7457               (when (gnus-invisible-cut-children (cdr thread))
7458                 (let ((th (cdr thread)))
7459                   (while th
7460                     (if (memq (mail-header-number (caar th))
7461                               gnus-newsgroup-limit)
7462                         (setq thread (car th)
7463                               th nil)
7464                       (setq th (cdr th))))))))))
7465   thread)
7466
7467 (defun gnus-cut-threads (threads)
7468   "Cut off all uninteresting articles from the beginning of threads."
7469   (when (or (eq gnus-fetch-old-headers 'some)
7470             (eq gnus-fetch-old-headers 'invisible)
7471             (numberp gnus-fetch-old-headers)
7472             (eq gnus-build-sparse-threads 'some)
7473             (eq gnus-build-sparse-threads 'more))
7474     (let ((th threads))
7475       (while th
7476         (setcar th (gnus-cut-thread (car th)))
7477         (setq th (cdr th)))))
7478   ;; Remove nixed out threads.
7479   (delq nil threads))
7480
7481 (defun gnus-summary-initial-limit (&optional show-if-empty)
7482   "Figure out what the initial limit is supposed to be on group entry.
7483 This entails weeding out unwanted dormants, low-scored articles,
7484 fetch-old-headers verbiage, and so on."
7485   ;; Most groups have nothing to remove.
7486   (if (or gnus-inhibit-limiting
7487           (and (null gnus-newsgroup-dormant)
7488                (eq gnus-newsgroup-display 'gnus-not-ignore)
7489                (not (eq gnus-fetch-old-headers 'some))
7490                (not (numberp gnus-fetch-old-headers))
7491                (not (eq gnus-fetch-old-headers 'invisible))
7492                (null gnus-summary-expunge-below)
7493                (not (eq gnus-build-sparse-threads 'some))
7494                (not (eq gnus-build-sparse-threads 'more))
7495                (null gnus-thread-expunge-below)
7496                (not gnus-use-nocem)))
7497       ()                                ; Do nothing.
7498     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7499     (setq gnus-newsgroup-limit nil)
7500     (mapatoms
7501      (lambda (node)
7502        (unless (car (symbol-value node))
7503          ;; These threads have no parents -- they are roots.
7504          (let ((nodes (cdr (symbol-value node)))
7505                thread)
7506            (while nodes
7507              (if (and gnus-thread-expunge-below
7508                       (< (gnus-thread-total-score (car nodes))
7509                          gnus-thread-expunge-below))
7510                  (gnus-expunge-thread (pop nodes))
7511                (setq thread (pop nodes))
7512                (gnus-summary-limit-children thread))))))
7513      gnus-newsgroup-dependencies)
7514     ;; If this limitation resulted in an empty group, we might
7515     ;; pop the previous limit and use it instead.
7516     (when (and (not gnus-newsgroup-limit)
7517                show-if-empty)
7518       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7519     gnus-newsgroup-limit))
7520
7521 (defun gnus-summary-limit-children (thread)
7522   "Return 1 if this subthread is visible and 0 if it is not."
7523   ;; First we get the number of visible children to this thread.  This
7524   ;; is done by recursing down the thread using this function, so this
7525   ;; will really go down to a leaf article first, before slowly
7526   ;; working its way up towards the root.
7527   (when thread
7528     (let ((children
7529            (if (cdr thread)
7530                (apply '+ (mapcar 'gnus-summary-limit-children
7531                                  (cdr thread)))
7532              0))
7533           (number (mail-header-number (car thread)))
7534           score)
7535       (if (and
7536            (not (memq number gnus-newsgroup-marked))
7537            (or
7538             ;; If this article is dormant and has absolutely no visible
7539             ;; children, then this article isn't visible.
7540             (and (memq number gnus-newsgroup-dormant)
7541                  (zerop children))
7542             ;; If this is "fetch-old-headered" and there is no
7543             ;; visible children, then we don't want this article.
7544             (and (or (eq gnus-fetch-old-headers 'some)
7545                      (numberp gnus-fetch-old-headers))
7546                  (gnus-summary-article-ancient-p number)
7547                  (zerop children))
7548             ;; If this is "fetch-old-headered" and `invisible', then
7549             ;; we don't want this article.
7550             (and (eq gnus-fetch-old-headers 'invisible)
7551                  (gnus-summary-article-ancient-p number))
7552             ;; If this is a sparsely inserted article with no children,
7553             ;; we don't want it.
7554             (and (eq gnus-build-sparse-threads 'some)
7555                  (gnus-summary-article-sparse-p number)
7556                  (zerop children))
7557             ;; If we use expunging, and this article is really
7558             ;; low-scored, then we don't want this article.
7559             (when (and gnus-summary-expunge-below
7560                        (< (setq score
7561                                 (or (cdr (assq number gnus-newsgroup-scored))
7562                                     gnus-summary-default-score))
7563                           gnus-summary-expunge-below))
7564               ;; We increase the expunge-tally here, but that has
7565               ;; nothing to do with the limits, really.
7566               (incf gnus-newsgroup-expunged-tally)
7567               ;; We also mark as read here, if that's wanted.
7568               (when (and gnus-summary-mark-below
7569                          (< score gnus-summary-mark-below))
7570                 (setq gnus-newsgroup-unreads
7571                       (delq number gnus-newsgroup-unreads))
7572                 (if gnus-newsgroup-auto-expire
7573                     (push number gnus-newsgroup-expirable)
7574                   (push (cons number gnus-low-score-mark)
7575                         gnus-newsgroup-reads)))
7576               t)
7577             ;; Do the `display' group parameter.
7578             (and gnus-newsgroup-display
7579                  (not (funcall gnus-newsgroup-display)))
7580             ;; Check NoCeM things.
7581             (if (and gnus-use-nocem
7582                      (gnus-nocem-unwanted-article-p
7583                       (mail-header-id (car thread))))
7584                 (progn
7585                   (setq gnus-newsgroup-unreads
7586                         (delq number gnus-newsgroup-unreads))
7587                   t))))
7588           ;; Nope, invisible article.
7589           0
7590         ;; Ok, this article is to be visible, so we add it to the limit
7591         ;; and return 1.
7592         (push number gnus-newsgroup-limit)
7593         1))))
7594
7595 (defun gnus-expunge-thread (thread)
7596   "Mark all articles in THREAD as read."
7597   (let* ((number (mail-header-number (car thread))))
7598     (incf gnus-newsgroup-expunged-tally)
7599     ;; We also mark as read here, if that's wanted.
7600     (setq gnus-newsgroup-unreads
7601           (delq number gnus-newsgroup-unreads))
7602     (if gnus-newsgroup-auto-expire
7603         (push number gnus-newsgroup-expirable)
7604       (push (cons number gnus-low-score-mark)
7605             gnus-newsgroup-reads)))
7606   ;; Go recursively through all subthreads.
7607   (mapcar 'gnus-expunge-thread (cdr thread)))
7608
7609 ;; Summary article oriented commands
7610
7611 (defun gnus-summary-refer-parent-article (n)
7612   "Refer parent article N times.
7613 If N is negative, go to ancestor -N instead.
7614 The difference between N and the number of articles fetched is returned."
7615   (interactive "p")
7616   (let ((skip 1)
7617         error header ref)
7618     (when (not (natnump n))
7619       (setq skip (abs n)
7620             n 1))
7621     (while (and (> n 0)
7622                 (not error))
7623       (setq header (gnus-summary-article-header))
7624       (if (and (eq (mail-header-number header)
7625                    (cdr gnus-article-current))
7626                (equal gnus-newsgroup-name
7627                       (car gnus-article-current)))
7628           ;; If we try to find the parent of the currently
7629           ;; displayed article, then we take a look at the actual
7630           ;; References header, since this is slightly more
7631           ;; reliable than the References field we got from the
7632           ;; server.
7633           (save-excursion
7634             (set-buffer gnus-original-article-buffer)
7635             (nnheader-narrow-to-headers)
7636             (unless (setq ref (message-fetch-field "references"))
7637               (setq ref (message-fetch-field "in-reply-to")))
7638             (widen))
7639         (setq ref
7640               ;; It's not the current article, so we take a bet on
7641               ;; the value we got from the server.
7642               (mail-header-references header)))
7643       (if (and ref
7644                (not (equal ref "")))
7645           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7646             (gnus-message 1 "Couldn't find parent"))
7647         (gnus-message 1 "No references in article %d"
7648                       (gnus-summary-article-number))
7649         (setq error t))
7650       (decf n))
7651     (gnus-summary-position-point)
7652     n))
7653
7654 (defun gnus-summary-refer-references ()
7655   "Fetch all articles mentioned in the References header.
7656 Return the number of articles fetched."
7657   (interactive)
7658   (let ((ref (mail-header-references (gnus-summary-article-header)))
7659         (current (gnus-summary-article-number))
7660         (n 0))
7661     (if (or (not ref)
7662             (equal ref ""))
7663         (error "No References in the current article")
7664       ;; For each Message-ID in the References header...
7665       (while (string-match "<[^>]*>" ref)
7666         (incf n)
7667         ;; ... fetch that article.
7668         (gnus-summary-refer-article
7669          (prog1 (match-string 0 ref)
7670            (setq ref (substring ref (match-end 0))))))
7671       (gnus-summary-goto-subject current)
7672       (gnus-summary-position-point)
7673       n)))
7674
7675 (defun gnus-summary-refer-thread (&optional limit)
7676   "Fetch all articles in the current thread.
7677 If LIMIT (the numerical prefix), fetch that many old headers instead
7678 of what's specified by the `gnus-refer-thread-limit' variable."
7679   (interactive "P")
7680   (let ((id (mail-header-id (gnus-summary-article-header)))
7681         (limit (if limit (prefix-numeric-value limit)
7682                  gnus-refer-thread-limit)))
7683     ;; We want to fetch LIMIT *old* headers, but we also have to
7684     ;; re-fetch all the headers in the current buffer, because many of
7685     ;; them may be undisplayed.  So we adjust LIMIT.
7686     (when (numberp limit)
7687       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7688     (unless (eq gnus-fetch-old-headers 'invisible)
7689       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7690       ;; Retrieve the headers and read them in.
7691       (if (eq (gnus-retrieve-headers
7692                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7693               'nov)
7694           (gnus-build-all-threads)
7695         (error "Can't fetch thread from backends that don't support NOV"))
7696       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7697     (gnus-summary-limit-include-thread id)))
7698
7699 (defun gnus-summary-refer-article (message-id)
7700   "Fetch an article specified by MESSAGE-ID."
7701   (interactive "sMessage-ID: ")
7702   (when (and (stringp message-id)
7703              (not (zerop (length message-id))))
7704     ;; Construct the correct Message-ID if necessary.
7705     ;; Suggested by tale@pawl.rpi.edu.
7706     (unless (string-match "^<" message-id)
7707       (setq message-id (concat "<" message-id)))
7708     (unless (string-match ">$" message-id)
7709       (setq message-id (concat message-id ">")))
7710     (let* ((header (gnus-id-to-header message-id))
7711            (sparse (and header
7712                         (gnus-summary-article-sparse-p
7713                          (mail-header-number header))
7714                         (memq (mail-header-number header)
7715                               gnus-newsgroup-limit)))
7716            number)
7717       (cond
7718        ;; If the article is present in the buffer we just go to it.
7719        ((and header
7720              (or (not (gnus-summary-article-sparse-p
7721                        (mail-header-number header)))
7722                  sparse))
7723         (prog1
7724             (gnus-summary-goto-article
7725              (mail-header-number header) nil t)
7726           (when sparse
7727             (gnus-summary-update-article (mail-header-number header)))))
7728        (t
7729         ;; We fetch the article.
7730         (catch 'found
7731           (dolist (gnus-override-method (gnus-refer-article-methods))
7732             (gnus-check-server gnus-override-method)
7733             ;; Fetch the header, and display the article.
7734             (when (setq number (gnus-summary-insert-subject message-id))
7735               (gnus-summary-select-article nil nil nil number)
7736               (throw 'found t)))
7737           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7738
7739 (defun gnus-refer-article-methods ()
7740   "Return a list of referrable methods."
7741   (cond
7742    ;; No method, so we default to current and native.
7743    ((null gnus-refer-article-method)
7744     (list gnus-current-select-method gnus-select-method))
7745    ;; Current.
7746    ((eq 'current gnus-refer-article-method)
7747     (list gnus-current-select-method))
7748    ;; List of select methods.
7749    ((not (and (symbolp (car gnus-refer-article-method))
7750               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7751     (let (out)
7752       (dolist (method gnus-refer-article-method)
7753         (push (if (eq 'current method)
7754                   gnus-current-select-method
7755                 method)
7756               out))
7757       (nreverse out)))
7758    ;; One single select method.
7759    (t
7760     (list gnus-refer-article-method))))
7761
7762 (defun gnus-summary-edit-parameters ()
7763   "Edit the group parameters of the current group."
7764   (interactive)
7765   (gnus-group-edit-group gnus-newsgroup-name 'params))
7766
7767 (defun gnus-summary-customize-parameters ()
7768   "Customize the group parameters of the current group."
7769   (interactive)
7770   (gnus-group-customize gnus-newsgroup-name))
7771
7772 (defun gnus-summary-enter-digest-group (&optional force)
7773   "Enter an nndoc group based on the current article.
7774 If FORCE, force a digest interpretation.  If not, try
7775 to guess what the document format is."
7776   (interactive "P")
7777   (let ((conf gnus-current-window-configuration))
7778     (save-excursion
7779       (gnus-summary-select-article))
7780     (setq gnus-current-window-configuration conf)
7781     (let* ((name (format "%s-%d"
7782                          (gnus-group-prefixed-name
7783                           gnus-newsgroup-name (list 'nndoc ""))
7784                          (save-excursion
7785                            (set-buffer gnus-summary-buffer)
7786                            gnus-current-article)))
7787            (ogroup gnus-newsgroup-name)
7788            (params (append (gnus-info-params (gnus-get-info ogroup))
7789                            (list (cons 'to-group ogroup))
7790                            (list (cons 'save-article-group ogroup))))
7791            (case-fold-search t)
7792            (buf (current-buffer))
7793            dig to-address)
7794       (save-excursion
7795         (set-buffer gnus-original-article-buffer)
7796         ;; Have the digest group inherit the main mail address of
7797         ;; the parent article.
7798         (when (setq to-address (or (message-fetch-field "reply-to")
7799                                    (message-fetch-field "from")))
7800           (setq params (append
7801                         (list (cons 'to-address
7802                                     (funcall gnus-decode-encoded-word-function
7803                                              to-address))))))
7804         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7805         (insert-buffer-substring gnus-original-article-buffer)
7806         ;; Remove lines that may lead nndoc to misinterpret the
7807         ;; document type.
7808         (narrow-to-region
7809          (goto-char (point-min))
7810          (or (search-forward "\n\n" nil t) (point)))
7811         (goto-char (point-min))
7812         (delete-matching-lines "^Path:\\|^From ")
7813         (widen))
7814       (unwind-protect
7815           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7816                     (gnus-newsgroup-ephemeral-ignored-charsets
7817                      gnus-newsgroup-ignored-charsets))
7818                 (gnus-group-read-ephemeral-group
7819                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7820                               (nndoc-article-type
7821                                ,(if force 'mbox 'guess)))
7822                  t nil nil nil
7823                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7824                                                         "ADAPT")))))
7825               ;; Make all postings to this group go to the parent group.
7826               (nconc (gnus-info-params (gnus-get-info name))
7827                      params)
7828             ;; Couldn't select this doc group.
7829             (switch-to-buffer buf)
7830             (gnus-set-global-variables)
7831             (gnus-configure-windows 'summary)
7832             (gnus-message 3 "Article couldn't be entered?"))
7833         (kill-buffer dig)))))
7834
7835 (defun gnus-summary-read-document (n)
7836   "Open a new group based on the current article(s).
7837 This will allow you to read digests and other similar
7838 documents as newsgroups.
7839 Obeys the standard process/prefix convention."
7840   (interactive "P")
7841   (let* ((articles (gnus-summary-work-articles n))
7842          (ogroup gnus-newsgroup-name)
7843          (params (append (gnus-info-params (gnus-get-info ogroup))
7844                          (list (cons 'to-group ogroup))))
7845          article group egroup groups vgroup)
7846     (while (setq article (pop articles))
7847       (setq group (format "%s-%d" gnus-newsgroup-name article))
7848       (gnus-summary-remove-process-mark article)
7849       (when (gnus-summary-display-article article)
7850         (save-excursion
7851           (with-temp-buffer
7852             (insert-buffer-substring gnus-original-article-buffer)
7853             ;; Remove some headers that may lead nndoc to make
7854             ;; the wrong guess.
7855             (message-narrow-to-head)
7856             (goto-char (point-min))
7857             (delete-matching-lines "^\\(Path\\):\\|^From ")
7858             (widen)
7859             (if (setq egroup
7860                       (gnus-group-read-ephemeral-group
7861                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7862                                      (nndoc-article-type guess))
7863                        t nil t))
7864                 (progn
7865             ;; Make all postings to this group go to the parent group.
7866                   (nconc (gnus-info-params (gnus-get-info egroup))
7867                          params)
7868                   (push egroup groups))
7869               ;; Couldn't select this doc group.
7870               (gnus-error 3 "Article couldn't be entered"))))))
7871     ;; Now we have selected all the documents.
7872     (cond
7873      ((not groups)
7874       (error "None of the articles could be interpreted as documents"))
7875      ((gnus-group-read-ephemeral-group
7876        (setq vgroup (format
7877                      "nnvirtual:%s-%s" gnus-newsgroup-name
7878                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7879        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7880        t
7881        (cons (current-buffer) 'summary)))
7882      (t
7883       (error "Couldn't select virtual nndoc group")))))
7884
7885 (defun gnus-summary-isearch-article (&optional regexp-p)
7886   "Do incremental search forward on the current article.
7887 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7888   (interactive "P")
7889   (gnus-summary-select-article)
7890   (gnus-configure-windows 'article)
7891   (gnus-eval-in-buffer-window gnus-article-buffer
7892     (save-restriction
7893       (widen)
7894       (isearch-forward regexp-p))))
7895
7896 (defun gnus-summary-search-article-forward (regexp &optional backward)
7897   "Search for an article containing REGEXP forward.
7898 If BACKWARD, search backward instead."
7899   (interactive
7900    (list (read-string
7901           (format "Search article %s (regexp%s): "
7902                   (if current-prefix-arg "backward" "forward")
7903                   (if gnus-last-search-regexp
7904                       (concat ", default " gnus-last-search-regexp)
7905                     "")))
7906          current-prefix-arg))
7907   (if (string-equal regexp "")
7908       (setq regexp (or gnus-last-search-regexp ""))
7909     (setq gnus-last-search-regexp regexp)
7910     (setq gnus-article-before-search gnus-current-article))
7911   ;; Intentionally set gnus-last-article.
7912   (setq gnus-last-article gnus-article-before-search)
7913   (let ((gnus-last-article gnus-last-article))
7914     (if (gnus-summary-search-article regexp backward)
7915         (gnus-summary-show-thread)
7916       (error "Search failed: \"%s\"" regexp))))
7917
7918 (defun gnus-summary-search-article-backward (regexp)
7919   "Search for an article containing REGEXP backward."
7920   (interactive
7921    (list (read-string
7922           (format "Search article backward (regexp%s): "
7923                   (if gnus-last-search-regexp
7924                       (concat ", default " gnus-last-search-regexp)
7925                     "")))))
7926   (gnus-summary-search-article-forward regexp 'backward))
7927
7928 (defun gnus-summary-search-article (regexp &optional backward)
7929   "Search for an article containing REGEXP.
7930 Optional argument BACKWARD means do search for backward.
7931 `gnus-select-article-hook' is not called during the search."
7932   ;; We have to require this here to make sure that the following
7933   ;; dynamic binding isn't shadowed by autoloading.
7934   (require 'gnus-async)
7935   (require 'gnus-art)
7936   (let ((gnus-select-article-hook nil)  ;Disable hook.
7937         (gnus-article-prepare-hook nil)
7938         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7939         (gnus-use-article-prefetch nil)
7940         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7941         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7942         (sum (current-buffer))
7943         (gnus-display-mime-function nil)
7944         (found nil)
7945         point)
7946     (gnus-save-hidden-threads
7947       (gnus-summary-select-article)
7948       (set-buffer gnus-article-buffer)
7949       (goto-char (window-point (get-buffer-window (current-buffer))))
7950       (when backward
7951         (forward-line -1))
7952       (while (not found)
7953         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7954         (if (if backward
7955                 (re-search-backward regexp nil t)
7956               (re-search-forward regexp nil t))
7957             ;; We found the regexp.
7958             (progn
7959               (setq found 'found)
7960               (beginning-of-line)
7961               (set-window-start
7962                (get-buffer-window (current-buffer))
7963                (point))
7964               (forward-line 1)
7965               (set-window-point
7966                (get-buffer-window (current-buffer))
7967                (point))
7968               (set-buffer sum)
7969               (setq point (point)))
7970           ;; We didn't find it, so we go to the next article.
7971           (set-buffer sum)
7972           (setq found 'not)
7973           (while (eq found 'not)
7974             (if (not (if backward (gnus-summary-find-prev)
7975                        (gnus-summary-find-next)))
7976                 ;; No more articles.
7977                 (setq found t)
7978               ;; Select the next article and adjust point.
7979               (unless (gnus-summary-article-sparse-p
7980                        (gnus-summary-article-number))
7981                 (setq found nil)
7982                 (gnus-summary-select-article)
7983                 (set-buffer gnus-article-buffer)
7984                 (widen)
7985                 (goto-char (if backward (point-max) (point-min))))))))
7986       (gnus-message 7 ""))
7987     ;; Return whether we found the regexp.
7988     (when (eq found 'found)
7989       (goto-char point)
7990       (gnus-summary-show-thread)
7991       (gnus-summary-goto-subject gnus-current-article)
7992       (gnus-summary-position-point)
7993       t)))
7994
7995 (defun gnus-find-matching-articles (header regexp)
7996   "Return a list of all articles that match REGEXP on HEADER.
7997 This search includes all articles in the current group that Gnus has
7998 fetched headers for, whether they are displayed or not."
7999   (let ((articles nil)
8000         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8001         (case-fold-search t))
8002     (dolist (header gnus-newsgroup-headers)
8003       (when (string-match regexp (funcall func header))
8004         (push (mail-header-number header) articles)))
8005     (nreverse articles)))
8006
8007 (defun gnus-summary-find-matching (header regexp &optional backward unread
8008                                           not-case-fold not-matching)
8009   "Return a list of all articles that match REGEXP on HEADER.
8010 The search stars on the current article and goes forwards unless
8011 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8012 If UNREAD is non-nil, only unread articles will
8013 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8014 in the comparisons. If NOT-MATCHING, return a list of all articles that
8015 not match REGEXP on HEADER."
8016   (let ((case-fold-search (not not-case-fold))
8017         articles d func)
8018     (if (consp header)
8019         (if (eq (car header) 'extra)
8020             (setq func
8021                   `(lambda (h)
8022                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8023                          "")))
8024           (error "%s is an invalid header" header))
8025       (unless (fboundp (intern (concat "mail-header-" header)))
8026         (error "%s is not a valid header" header))
8027       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8028     (dolist (d (if (eq backward 'all)
8029                    gnus-newsgroup-data
8030                  (gnus-data-find-list
8031                   (gnus-summary-article-number)
8032                   (gnus-data-list backward))))
8033       (when (and (or (not unread)       ; We want all articles...
8034                      (gnus-data-unread-p d)) ; Or just unreads.
8035                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8036                  (if not-matching
8037                      (not (string-match
8038                            regexp
8039                            (funcall func (gnus-data-header d))))
8040                    (string-match regexp
8041                                  (funcall func (gnus-data-header d)))))
8042         (push (gnus-data-number d) articles))) ; Success!
8043     (nreverse articles)))
8044
8045 (defun gnus-summary-execute-command (header regexp command &optional backward)
8046   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8047 If HEADER is an empty string (or nil), the match is done on the entire
8048 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8049   (interactive
8050    (list (let ((completion-ignore-case t))
8051            (completing-read
8052             "Header name: "
8053             (mapcar (lambda (header) (list (format "%s" header)))
8054                     (append
8055                      '("Number" "Subject" "From" "Lines" "Date"
8056                        "Message-ID" "Xref" "References" "Body")
8057                      gnus-extra-headers))
8058             nil 'require-match))
8059          (read-string "Regexp: ")
8060          (read-key-sequence "Command: ")
8061          current-prefix-arg))
8062   (when (equal header "Body")
8063     (setq header ""))
8064   ;; Hidden thread subtrees must be searched as well.
8065   (gnus-summary-show-all-threads)
8066   ;; We don't want to change current point nor window configuration.
8067   (save-excursion
8068     (save-window-excursion
8069       (gnus-message 6 "Executing %s..." (key-description command))
8070 ;; We'd like to execute COMMAND interactively so as to give arguments.
8071       (gnus-execute header regexp
8072                     `(call-interactively ',(key-binding command))
8073                     backward)
8074       (gnus-message 6 "Executing %s...done" (key-description command)))))
8075
8076 (defun gnus-summary-beginning-of-article ()
8077   "Scroll the article back to the beginning."
8078   (interactive)
8079   (gnus-summary-select-article)
8080   (gnus-configure-windows 'article)
8081   (gnus-eval-in-buffer-window gnus-article-buffer
8082     (widen)
8083     (goto-char (point-min))
8084     (when gnus-page-broken
8085       (gnus-narrow-to-page))))
8086
8087 (defun gnus-summary-end-of-article ()
8088   "Scroll to the end of the article."
8089   (interactive)
8090   (gnus-summary-select-article)
8091   (gnus-configure-windows 'article)
8092   (gnus-eval-in-buffer-window gnus-article-buffer
8093     (widen)
8094     (goto-char (point-max))
8095     (recenter -3)
8096     (when gnus-page-broken
8097       (gnus-narrow-to-page))))
8098
8099 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8100   "Truncate to LEN and quote all \"(\"'s in STRING."
8101   (gnus-replace-in-string (if (and len (> (length string) len))
8102                               (substring string 0 len)
8103                             string)
8104                           "[()]" "\\\\\\&"))
8105
8106 (defun gnus-summary-print-article (&optional filename n)
8107   "Generate and print a PostScript image of the N next (mail) articles.
8108
8109 If N is negative, print the N previous articles.  If N is nil and articles
8110 have been marked with the process mark, print these instead.
8111
8112 If the optional first argument FILENAME is nil, send the image to the
8113 printer.  If FILENAME is a string, save the PostScript image in a file with
8114 that name.  If FILENAME is a number, prompt the user for the name of the file
8115 to save in."
8116   (interactive (list (ps-print-preprint current-prefix-arg)))
8117   (dolist (article (gnus-summary-work-articles n))
8118     (gnus-summary-select-article nil nil 'pseudo article)
8119     (gnus-eval-in-buffer-window gnus-article-buffer
8120       (gnus-print-buffer))
8121     (gnus-summary-remove-process-mark article))
8122   (ps-despool filename))
8123
8124 (defun gnus-print-buffer ()
8125   (let ((buffer (generate-new-buffer " *print*")))
8126     (unwind-protect
8127         (progn
8128           (copy-to-buffer buffer (point-min) (point-max))
8129           (set-buffer buffer)
8130           (gnus-article-delete-invisible-text)
8131           (gnus-remove-text-with-property 'gnus-decoration)
8132           (when (gnus-visual-p 'article-highlight 'highlight)
8133             ;; Copy-to-buffer doesn't copy overlay.  So redo
8134             ;; highlight.
8135             (let ((gnus-article-buffer buffer))
8136               (gnus-article-highlight-citation t)
8137               (gnus-article-highlight-signature)))
8138           (let ((ps-left-header
8139                  (list
8140                   (concat "("
8141                           (gnus-summary-print-truncate-and-quote
8142                            (mail-header-subject gnus-current-headers)
8143                            66) ")")
8144                   (concat "("
8145                           (gnus-summary-print-truncate-and-quote
8146                            (mail-header-from gnus-current-headers)
8147                            45) ")")))
8148                 (ps-right-header
8149                  (list
8150                   "/pagenumberstring load"
8151                   (concat "("
8152                           (mail-header-date gnus-current-headers) ")"))))
8153             (gnus-run-hooks 'gnus-ps-print-hook)
8154             (save-excursion
8155               (if window-system
8156                   (ps-spool-buffer-with-faces)
8157                 (ps-spool-buffer)))))
8158       (kill-buffer buffer))))
8159
8160 (defun gnus-summary-show-article (&optional arg)
8161   "Force redisplaying of the current article.
8162 If ARG (the prefix) is a number, show the article with the charset
8163 defined in `gnus-summary-show-article-charset-alist', or the charset
8164 input.
8165 If ARG (the prefix) is non-nil and not a number, show the raw article
8166 without any article massaging functions being run.  Normally, the key strokes
8167 are `C-u g'."
8168   (interactive "P")
8169   (cond
8170    ((numberp arg)
8171     (gnus-summary-show-article t)
8172     (let ((gnus-newsgroup-charset
8173            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8174                (mm-read-coding-system
8175                 "View as charset: " ;; actually it is coding system.
8176                 (save-excursion
8177                   (set-buffer gnus-article-buffer)
8178                   (mm-detect-coding-region (point) (point-max))))))
8179           (gnus-newsgroup-ignored-charsets 'gnus-all))
8180       (gnus-summary-select-article nil 'force)
8181       (let ((deps gnus-newsgroup-dependencies)
8182             head header lines)
8183         (save-excursion
8184           (set-buffer gnus-original-article-buffer)
8185           (save-restriction
8186             (message-narrow-to-head)
8187             (setq head (buffer-string))
8188             (goto-char (point-min))
8189             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8190               (goto-char (point-max))
8191               (widen)
8192               (setq lines (1- (count-lines (point) (point-max))))))
8193           (with-temp-buffer
8194             (insert (format "211 %d Article retrieved.\n"
8195                             (cdr gnus-article-current)))
8196             (insert head)
8197             (if lines (insert (format "Lines: %d\n" lines)))
8198             (insert ".\n")
8199             (let ((nntp-server-buffer (current-buffer)))
8200               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8201         (gnus-data-set-header
8202          (gnus-data-find (cdr gnus-article-current))
8203          header)
8204         (gnus-summary-update-article-line
8205          (cdr gnus-article-current) header)
8206         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8207           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8208    ((not arg)
8209     ;; Select the article the normal way.
8210     (gnus-summary-select-article nil 'force))
8211    (t
8212     ;; We have to require this here to make sure that the following
8213     ;; dynamic binding isn't shadowed by autoloading.
8214     (require 'gnus-async)
8215     (require 'gnus-art)
8216     ;; Bind the article treatment functions to nil.
8217     (let ((gnus-have-all-headers t)
8218           gnus-article-prepare-hook
8219           gnus-article-decode-hook
8220           gnus-display-mime-function
8221           gnus-break-pages)
8222       ;; Destroy any MIME parts.
8223       (when (gnus-buffer-live-p gnus-article-buffer)
8224         (save-excursion
8225           (set-buffer gnus-article-buffer)
8226           (mm-destroy-parts gnus-article-mime-handles)
8227           ;; Set it to nil for safety reason.
8228           (setq gnus-article-mime-handle-alist nil)
8229           (setq gnus-article-mime-handles nil)))
8230       (gnus-summary-select-article nil 'force))))
8231   (gnus-summary-goto-subject gnus-current-article)
8232   (gnus-summary-position-point))
8233
8234 (defun gnus-summary-show-raw-article ()
8235   "Show the raw article without any article massaging functions being run."
8236   (interactive)
8237   (gnus-summary-show-article t))
8238
8239 (defun gnus-summary-verbose-headers (&optional arg)
8240   "Toggle permanent full header display.
8241 If ARG is a positive number, turn header display on.
8242 If ARG is a negative number, turn header display off."
8243   (interactive "P")
8244   (setq gnus-show-all-headers
8245         (cond ((or (not (numberp arg))
8246                    (zerop arg))
8247                (not gnus-show-all-headers))
8248               ((natnump arg)
8249                t)))
8250   (gnus-summary-show-article))
8251
8252 (defun gnus-summary-toggle-header (&optional arg)
8253   "Show the headers if they are hidden, or hide them if they are shown.
8254 If ARG is a positive number, show the entire header.
8255 If ARG is a negative number, hide the unwanted header lines."
8256   (interactive "P")
8257   (save-excursion
8258     (set-buffer gnus-article-buffer)
8259     (save-restriction
8260       (let* ((buffer-read-only nil)
8261              (inhibit-point-motion-hooks t)
8262              hidden e)
8263         (save-restriction
8264           (article-narrow-to-head)
8265           (setq e (point-max)
8266                 hidden (if (numberp arg)
8267                            (>= arg 0)
8268                          (gnus-article-hidden-text-p 'headers))))
8269         (delete-region (point-min) e)
8270         (goto-char (point-min))
8271         (save-excursion
8272           (set-buffer gnus-original-article-buffer)
8273           (goto-char (point-min))
8274           (setq e (search-forward "\n\n" nil t)
8275                 e (if e (1- e) (point-max))))
8276         (insert-buffer-substring gnus-original-article-buffer 1 e)
8277         (save-restriction
8278           (narrow-to-region (point-min) (point))
8279           (article-decode-encoded-words)
8280           (if  hidden
8281               (let ((gnus-treat-hide-headers nil)
8282                     (gnus-treat-hide-boring-headers nil))
8283                 (gnus-delete-wash-type 'headers)
8284                 (gnus-treat-article 'head))
8285             (gnus-treat-article 'head)))
8286         (gnus-set-mode-line 'article)))))
8287
8288 (defun gnus-summary-show-all-headers ()
8289   "Make all header lines visible."
8290   (interactive)
8291   (gnus-summary-toggle-header 1))
8292
8293 (defun gnus-summary-caesar-message (&optional arg)
8294   "Caesar rotate the current article by 13.
8295 The numerical prefix specifies how many places to rotate each letter
8296 forward."
8297   (interactive "P")
8298   (gnus-summary-select-article)
8299   (let ((mail-header-separator ""))
8300     (gnus-eval-in-buffer-window gnus-article-buffer
8301       (save-restriction
8302         (widen)
8303         (let ((start (window-start))
8304               buffer-read-only)
8305           (message-caesar-buffer-body arg)
8306           (set-window-start (get-buffer-window (current-buffer)) start))))))
8307
8308 (defun gnus-summary-stop-page-breaking ()
8309   "Stop page breaking in the current article."
8310   (interactive)
8311   (gnus-summary-select-article)
8312   (gnus-eval-in-buffer-window gnus-article-buffer
8313     (widen)
8314     (when (gnus-visual-p 'page-marker)
8315       (let ((buffer-read-only nil))
8316         (gnus-remove-text-with-property 'gnus-prev)
8317         (gnus-remove-text-with-property 'gnus-next))
8318       (setq gnus-page-broken nil))))
8319
8320 (defun gnus-summary-move-article (&optional n to-newsgroup
8321                                             select-method action)
8322   "Move the current article to a different newsgroup.
8323 If N is a positive number, move the N next articles.
8324 If N is a negative number, move the N previous articles.
8325 If N is nil and any articles have been marked with the process mark,
8326 move those articles instead.
8327 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8328 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8329 re-spool using this method.
8330
8331 When called interactively with TO-NEWSGROUP being nil, the value of
8332 the variable `gnus-move-split-methods' is used for finding a default
8333 for the target newsgroup.
8334
8335 For this function to work, both the current newsgroup and the
8336 newsgroup that you want to move to have to support the `request-move'
8337 and `request-accept' functions.
8338
8339 ACTION can be either `move' (the default), `crosspost' or `copy'."
8340   (interactive "P")
8341   (unless action
8342     (setq action 'move))
8343   ;; Check whether the source group supports the required functions.
8344   (cond ((and (eq action 'move)
8345               (not (gnus-check-backend-function
8346                     'request-move-article gnus-newsgroup-name)))
8347          (error "The current group does not support article moving"))
8348         ((and (eq action 'crosspost)
8349               (not (gnus-check-backend-function
8350                     'request-replace-article gnus-newsgroup-name)))
8351          (error "The current group does not support article editing")))
8352   (let ((articles (gnus-summary-work-articles n))
8353         (prefix (if (gnus-check-backend-function
8354                      'request-move-article gnus-newsgroup-name)
8355                     (gnus-group-real-prefix gnus-newsgroup-name)
8356                   ""))
8357         (names '((move "Move" "Moving")
8358                  (copy "Copy" "Copying")
8359                  (crosspost "Crosspost" "Crossposting")))
8360         (copy-buf (save-excursion
8361                     (nnheader-set-temp-buffer " *copy article*")))
8362         art-group to-method new-xref article to-groups)
8363     (unless (assq action names)
8364       (error "Unknown action %s" action))
8365     ;; Read the newsgroup name.
8366     (when (and (not to-newsgroup)
8367                (not select-method))
8368       (if (and gnus-move-split-methods
8369                (not
8370                 (and (memq gnus-current-article articles)
8371                      (gnus-buffer-live-p gnus-original-article-buffer))))
8372           ;; When `gnus-move-split-methods' is non-nil, we have to
8373           ;; select an article to give `gnus-read-move-group-name' an
8374           ;; opportunity to suggest an appropriate default.  However,
8375           ;; we needn't render or mark the article.
8376           (let ((gnus-display-mime-function nil)
8377                 (gnus-article-prepare-hook nil)
8378                 (gnus-mark-article-hook nil))
8379             (gnus-summary-select-article nil nil nil (car articles))))
8380       (setq to-newsgroup
8381             (gnus-read-move-group-name
8382              (cadr (assq action names))
8383              (symbol-value (intern (format "gnus-current-%s-group" action)))
8384              articles prefix))
8385       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8386     (setq to-method (or select-method
8387                         (gnus-server-to-method
8388                          (gnus-group-method to-newsgroup))))
8389     ;; Check the method we are to move this article to...
8390     (unless (gnus-check-backend-function
8391              'request-accept-article (car to-method))
8392       (error "%s does not support article copying" (car to-method)))
8393     (unless (gnus-check-server to-method)
8394       (error "Can't open server %s" (car to-method)))
8395     (gnus-message 6 "%s to %s: %s..."
8396                   (caddr (assq action names))
8397                   (or (car select-method) to-newsgroup) articles)
8398     (while articles
8399       (setq article (pop articles))
8400       (setq
8401        art-group
8402        (cond
8403         ;; Move the article.
8404         ((eq action 'move)
8405          ;; Remove this article from future suppression.
8406          (gnus-dup-unsuppress-article article)
8407          (gnus-request-move-article
8408           article                       ; Article to move
8409           gnus-newsgroup-name           ; From newsgroup
8410           (nth 1 (gnus-find-method-for-group
8411                   gnus-newsgroup-name)) ; Server
8412           (list 'gnus-request-accept-article
8413                 to-newsgroup (list 'quote select-method)
8414                 (not articles) t)       ; Accept form
8415           (not articles)))              ; Only save nov last time
8416         ;; Copy the article.
8417         ((eq action 'copy)
8418          (save-excursion
8419            (set-buffer copy-buf)
8420            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8421              (gnus-request-accept-article
8422               to-newsgroup select-method (not articles) t))))
8423         ;; Crosspost the article.
8424         ((eq action 'crosspost)
8425          (let ((xref (message-tokenize-header
8426                       (mail-header-xref (gnus-summary-article-header article))
8427                       " ")))
8428            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8429                                   ":" (number-to-string article)))
8430            (unless xref
8431              (setq xref (list (system-name))))
8432            (setq new-xref
8433                  (concat
8434                   (mapconcat 'identity
8435                              (delete "Xref:" (delete new-xref xref))
8436                              " ")
8437                   " " new-xref))
8438            (save-excursion
8439              (set-buffer copy-buf)
8440              ;; First put the article in the destination group.
8441              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8442              (when (consp (setq art-group
8443                                 (gnus-request-accept-article
8444                                  to-newsgroup select-method (not articles))))
8445                (setq new-xref (concat new-xref " " (car art-group)
8446                                       ":"
8447                                       (number-to-string (cdr art-group))))
8448                ;; Now we have the new Xrefs header, so we insert
8449                ;; it and replace the new article.
8450                (nnheader-replace-header "Xref" new-xref)
8451                (gnus-request-replace-article
8452                 (cdr art-group) to-newsgroup (current-buffer))
8453                art-group))))))
8454       (cond
8455        ((not art-group)
8456         (gnus-message 1 "Couldn't %s article %s: %s"
8457                       (cadr (assq action names)) article
8458                       (nnheader-get-report (car to-method))))
8459        ((eq art-group 'junk)
8460         (when (eq action 'move)
8461           (gnus-summary-mark-article article gnus-canceled-mark)
8462           (gnus-message 4 "Deleted article %s" article)))
8463        (t
8464         (let* ((pto-group (gnus-group-prefixed-name
8465                            (car art-group) to-method))
8466                (entry
8467                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8468                (info (nth 2 entry))
8469                (to-group (gnus-info-group info))
8470                to-marks)
8471           ;; Update the group that has been moved to.
8472           (when (and info
8473                      (memq action '(move copy)))
8474             (unless (member to-group to-groups)
8475               (push to-group to-groups))
8476
8477             (unless (memq article gnus-newsgroup-unreads)
8478               (push 'read to-marks)
8479               (gnus-info-set-read
8480                info (gnus-add-to-range (gnus-info-read info)
8481                                        (list (cdr art-group)))))
8482
8483             ;; See whether the article is to be put in the cache.
8484             (let ((marks gnus-article-mark-lists)
8485                   (to-article (cdr art-group)))
8486
8487               ;; Enter the article into the cache in the new group,
8488               ;; if that is required.
8489               (when gnus-use-cache
8490                 (gnus-cache-possibly-enter-article
8491                  to-group to-article
8492                  (memq article gnus-newsgroup-marked)
8493                  (memq article gnus-newsgroup-dormant)
8494                  (memq article gnus-newsgroup-unreads)))
8495
8496               (when gnus-preserve-marks
8497                 ;; Copy any marks over to the new group.
8498                 (when (and (equal to-group gnus-newsgroup-name)
8499                            (not (memq article gnus-newsgroup-unreads)))
8500                   ;; Mark this article as read in this group.
8501                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8502                   (setcdr (gnus-active to-group) to-article)
8503                   (setcdr gnus-newsgroup-active to-article))
8504
8505                 (while marks
8506                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8507                     (when (memq article (symbol-value
8508                                          (intern (format "gnus-newsgroup-%s"
8509                                                          (caar marks)))))
8510                       (push (cdar marks) to-marks)
8511                       ;; If the other group is the same as this group,
8512                       ;; then we have to add the mark to the list.
8513                       (when (equal to-group gnus-newsgroup-name)
8514                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8515                              (cons to-article
8516                                    (symbol-value
8517                                     (intern (format "gnus-newsgroup-%s"
8518                                                     (caar marks)))))))
8519                       ;; Copy the marks to other group.
8520                       (gnus-add-marked-articles
8521                        to-group (cdar marks) (list to-article) info)))
8522                   (setq marks (cdr marks)))
8523
8524                 (gnus-request-set-mark to-group (list (list (list to-article)
8525                                                             'add
8526                                                             to-marks))))
8527
8528               (gnus-dribble-enter
8529                (concat "(gnus-group-set-info '"
8530                        (gnus-prin1-to-string (gnus-get-info to-group))
8531                        ")"))))
8532
8533           ;; Update the Xref header in this article to point to
8534           ;; the new crossposted article we have just created.
8535           (when (eq action 'crosspost)
8536             (save-excursion
8537               (set-buffer copy-buf)
8538               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8539               (nnheader-replace-header "Xref" new-xref)
8540               (gnus-request-replace-article
8541                article gnus-newsgroup-name (current-buffer)))))
8542
8543         ;;;!!!Why is this necessary?
8544         (set-buffer gnus-summary-buffer)
8545
8546         (gnus-summary-goto-subject article)
8547         (when (eq action 'move)
8548           (gnus-summary-mark-article article gnus-canceled-mark))))
8549       (gnus-summary-remove-process-mark article))
8550     ;; Re-activate all groups that have been moved to.
8551     (save-excursion
8552       (set-buffer gnus-group-buffer)
8553       (let ((gnus-group-marked to-groups))
8554         (gnus-group-get-new-news-this-group nil t)))
8555
8556     (gnus-kill-buffer copy-buf)
8557     (gnus-summary-position-point)
8558     (gnus-set-mode-line 'summary)))
8559
8560 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8561   "Move the current article to a different newsgroup.
8562 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8563 When called interactively, if TO-NEWSGROUP is nil, use the value of
8564 the variable `gnus-move-split-methods' for finding a default target
8565 newsgroup.
8566 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8567 re-spool using this method."
8568   (interactive "P")
8569   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8570
8571 (defun gnus-summary-crosspost-article (&optional n)
8572   "Crosspost the current article to some other group."
8573   (interactive "P")
8574   (gnus-summary-move-article n nil nil 'crosspost))
8575
8576 (defcustom gnus-summary-respool-default-method nil
8577   "Default method type for respooling an article.
8578 If nil, use to the current newsgroup method."
8579   :type 'symbol
8580   :group 'gnus-summary-mail)
8581
8582 (defun gnus-summary-respool-article (&optional n method)
8583   "Respool the current article.
8584 The article will be squeezed through the mail spooling process again,
8585 which means that it will be put in some mail newsgroup or other
8586 depending on `nnmail-split-methods'.
8587 If N is a positive number, respool the N next articles.
8588 If N is a negative number, respool the N previous articles.
8589 If N is nil and any articles have been marked with the process mark,
8590 respool those articles instead.
8591
8592 Respooling can be done both from mail groups and \"real\" newsgroups.
8593 In the former case, the articles in question will be moved from the
8594 current group into whatever groups they are destined to.  In the
8595 latter case, they will be copied into the relevant groups."
8596   (interactive
8597    (list current-prefix-arg
8598          (let* ((methods (gnus-methods-using 'respool))
8599                 (methname
8600                  (symbol-name (or gnus-summary-respool-default-method
8601                                   (car (gnus-find-method-for-group
8602                                         gnus-newsgroup-name)))))
8603                 (method
8604                  (gnus-completing-read-with-default
8605                   methname "What backend do you want to use when respooling?"
8606                   methods nil t nil 'gnus-mail-method-history))
8607                 ms)
8608            (cond
8609             ((zerop (length (setq ms (gnus-servers-using-backend
8610                                       (intern method)))))
8611              (list (intern method) ""))
8612             ((= 1 (length ms))
8613              (car ms))
8614             (t
8615              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8616                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8617                            ms-alist))))))))
8618   (unless method
8619     (error "No method given for respooling"))
8620   (if (assoc (symbol-name
8621               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8622              (gnus-methods-using 'respool))
8623       (gnus-summary-move-article n nil method)
8624     (gnus-summary-copy-article n nil method)))
8625
8626 (defun gnus-summary-import-article (file &optional edit)
8627   "Import an arbitrary file into a mail newsgroup."
8628   (interactive "fImport file: \nP")
8629   (let ((group gnus-newsgroup-name)
8630         (now (current-time))
8631         atts lines group-art)
8632     (unless (gnus-check-backend-function 'request-accept-article group)
8633       (error "%s does not support article importing" group))
8634     (or (file-readable-p file)
8635         (not (file-regular-p file))
8636         (error "Can't read %s" file))
8637     (save-excursion
8638       (set-buffer (gnus-get-buffer-create " *import file*"))
8639       (erase-buffer)
8640       (nnheader-insert-file-contents file)
8641       (goto-char (point-min))
8642       (if (nnheader-article-p)
8643           (save-restriction
8644             (goto-char (point-min))
8645             (search-forward "\n\n" nil t)
8646             (narrow-to-region (point-min) (1- (point)))
8647             (goto-char (point-min))
8648             (unless (re-search-forward "^date:" nil t)
8649               (goto-char (point-max))
8650               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8651        ;; This doesn't look like an article, so we fudge some headers.
8652         (setq atts (file-attributes file)
8653               lines (count-lines (point-min) (point-max)))
8654         (insert "From: " (read-string "From: ") "\n"
8655                 "Subject: " (read-string "Subject: ") "\n"
8656                 "Date: " (message-make-date (nth 5 atts)) "\n"
8657                 "Message-ID: " (message-make-message-id) "\n"
8658                 "Lines: " (int-to-string lines) "\n"
8659                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8660       (setq group-art (gnus-request-accept-article group nil t))
8661       (kill-buffer (current-buffer)))
8662     (setq gnus-newsgroup-active (gnus-activate-group group))
8663     (forward-line 1)
8664     (gnus-summary-goto-article (cdr group-art) nil t)
8665     (when edit
8666       (gnus-summary-edit-article))))
8667
8668 (defun gnus-summary-create-article ()
8669   "Create an article in a mail newsgroup."
8670   (interactive)
8671   (let ((group gnus-newsgroup-name)
8672         (now (current-time))
8673         group-art)
8674     (unless (gnus-check-backend-function 'request-accept-article group)
8675       (error "%s does not support article importing" group))
8676     (save-excursion
8677       (set-buffer (gnus-get-buffer-create " *import file*"))
8678       (erase-buffer)
8679       (goto-char (point-min))
8680       ;; This doesn't look like an article, so we fudge some headers.
8681       (insert "From: " (read-string "From: ") "\n"
8682               "Subject: " (read-string "Subject: ") "\n"
8683               "Date: " (message-make-date now) "\n"
8684               "Message-ID: " (message-make-message-id) "\n")
8685       (setq group-art (gnus-request-accept-article group nil t))
8686       (kill-buffer (current-buffer)))
8687     (setq gnus-newsgroup-active (gnus-activate-group group))
8688     (forward-line 1)
8689     (gnus-summary-goto-article (cdr group-art) nil t)
8690     (gnus-summary-edit-article)))
8691
8692 (defun gnus-summary-article-posted-p ()
8693   "Say whether the current (mail) article is available from news as well.
8694 This will be the case if the article has both been mailed and posted."
8695   (interactive)
8696   (let ((id (mail-header-references (gnus-summary-article-header)))
8697         (gnus-override-method (car (gnus-refer-article-methods))))
8698     (if (gnus-request-head id "")
8699         (gnus-message 2 "The current message was found on %s"
8700                       gnus-override-method)
8701       (gnus-message 2 "The current message couldn't be found on %s"
8702                     gnus-override-method)
8703       nil)))
8704
8705 (defun gnus-summary-expire-articles (&optional now)
8706   "Expire all articles that are marked as expirable in the current group."
8707   (interactive)
8708   (when (gnus-check-backend-function
8709          'request-expire-articles gnus-newsgroup-name)
8710     ;; This backend supports expiry.
8711     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8712            (expirable (if total
8713                           (progn
8714                             ;; We need to update the info for
8715                             ;; this group for `gnus-list-of-read-articles'
8716                             ;; to give us the right answer.
8717                             (gnus-run-hooks 'gnus-exit-group-hook)
8718                             (gnus-summary-update-info)
8719                             (gnus-list-of-read-articles gnus-newsgroup-name))
8720                         (setq gnus-newsgroup-expirable
8721                               (sort gnus-newsgroup-expirable '<))))
8722            (expiry-wait (if now 'immediate
8723                           (gnus-group-find-parameter
8724                            gnus-newsgroup-name 'expiry-wait)))
8725            (nnmail-expiry-target
8726             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8727                 nnmail-expiry-target))
8728            es)
8729       (when expirable
8730         ;; There are expirable articles in this group, so we run them
8731         ;; through the expiry process.
8732         (gnus-message 6 "Expiring articles...")
8733         (unless (gnus-check-group gnus-newsgroup-name)
8734           (error "Can't open server for %s" gnus-newsgroup-name))
8735         ;; The list of articles that weren't expired is returned.
8736         (save-excursion
8737           (if expiry-wait
8738               (let ((nnmail-expiry-wait-function nil)
8739                     (nnmail-expiry-wait expiry-wait))
8740                 (setq es (gnus-request-expire-articles
8741                           expirable gnus-newsgroup-name)))
8742             (setq es (gnus-request-expire-articles
8743                       expirable gnus-newsgroup-name)))
8744           (unless total
8745             (setq gnus-newsgroup-expirable es))
8746           ;; We go through the old list of expirable, and mark all
8747           ;; really expired articles as nonexistent.
8748           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8749             (let ((gnus-use-cache nil))
8750               (dolist (article expirable)
8751                 (when (and (not (memq article es))
8752                            (gnus-data-find article))
8753                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8754         (gnus-message 6 "Expiring articles...done")))))
8755
8756 (defun gnus-summary-expire-articles-now ()
8757   "Expunge all expirable articles in the current group.
8758 This means that *all* articles that are marked as expirable will be
8759 deleted forever, right now."
8760   (interactive)
8761   (or gnus-expert-user
8762       (gnus-yes-or-no-p
8763        "Are you really, really, really sure you want to delete all these messages? ")
8764       (error "Phew!"))
8765   (gnus-summary-expire-articles t))
8766
8767 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8768 (defun gnus-summary-delete-article (&optional n)
8769   "Delete the N next (mail) articles.
8770 This command actually deletes articles.  This is not a marking
8771 command.  The article will disappear forever from your life, never to
8772 return.
8773 If N is negative, delete backwards.
8774 If N is nil and articles have been marked with the process mark,
8775 delete these instead."
8776   (interactive "P")
8777   (unless (gnus-check-backend-function 'request-expire-articles
8778                                        gnus-newsgroup-name)
8779     (error "The current newsgroup does not support article deletion"))
8780   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8781     (error "Couldn't open server"))
8782   ;; Compute the list of articles to delete.
8783   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8784         not-deleted)
8785     (if (and gnus-novice-user
8786              (not (gnus-yes-or-no-p
8787                    (format "Do you really want to delete %s forever? "
8788                            (if (> (length articles) 1)
8789                                (format "these %s articles" (length articles))
8790                              "this article")))))
8791         ()
8792       ;; Delete the articles.
8793       (setq not-deleted (gnus-request-expire-articles
8794                          articles gnus-newsgroup-name 'force))
8795       (while articles
8796         (gnus-summary-remove-process-mark (car articles))
8797         ;; The backend might not have been able to delete the article
8798         ;; after all.
8799         (unless (memq (car articles) not-deleted)
8800           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8801         (setq articles (cdr articles)))
8802       (when not-deleted
8803         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8804     (gnus-summary-position-point)
8805     (gnus-set-mode-line 'summary)
8806     not-deleted))
8807
8808 (defun gnus-summary-edit-article (&optional arg)
8809   "Edit the current article.
8810 This will have permanent effect only in mail groups.
8811 If ARG is nil, edit the decoded articles.
8812 If ARG is 1, edit the raw articles.
8813 If ARG is 2, edit the raw articles even in read-only groups.
8814 If ARG is 3, edit the articles with the current handles.
8815 Otherwise, allow editing of articles even in read-only
8816 groups."
8817   (interactive "P")
8818   (let (force raw current-handles)
8819     (cond
8820      ((null arg))
8821      ((eq arg 1)
8822       (setq raw t))
8823      ((eq arg 2)
8824       (setq raw t
8825             force t))
8826      ((eq arg 3)
8827       (setq current-handles
8828             (and (gnus-buffer-live-p gnus-article-buffer)
8829                  (with-current-buffer gnus-article-buffer
8830                    (prog1
8831                        gnus-article-mime-handles
8832                      (setq gnus-article-mime-handles nil))))))
8833      (t
8834       (setq force t)))
8835     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8836       (error "Can't edit the raw article in group nndraft:drafts"))
8837     (save-excursion
8838       (set-buffer gnus-summary-buffer)
8839       (let ((mail-parse-charset gnus-newsgroup-charset)
8840             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8841         (gnus-set-global-variables)
8842         (when (and (not force)
8843                    (gnus-group-read-only-p))
8844           (error "The current newsgroup does not support article editing"))
8845         (gnus-summary-show-article t)
8846         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8847           (with-current-buffer gnus-article-buffer
8848             (mm-enable-multibyte)))
8849         (if (equal gnus-newsgroup-name "nndraft:drafts")
8850             (setq raw t))
8851         (gnus-article-edit-article
8852          (if raw 'ignore
8853            `(lambda ()
8854               (let ((mbl mml-buffer-list))
8855                 (setq mml-buffer-list nil)
8856                 (mime-to-mml ,'current-handles)
8857                 (let ((mbl1 mml-buffer-list))
8858                   (setq mml-buffer-list mbl)
8859                   (set (make-local-variable 'mml-buffer-list) mbl1))
8860                 (make-local-hook 'kill-buffer-hook)
8861                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8862          `(lambda (no-highlight)
8863             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8864                   (message-options message-options)
8865                   (message-options-set-recipient)
8866                   (mail-parse-ignored-charsets
8867                    ',gnus-newsgroup-ignored-charsets))
8868               ,(if (not raw) '(progn
8869                                 (mml-to-mime)
8870                                 (mml-destroy-buffers)
8871                                 (remove-hook 'kill-buffer-hook
8872                                              'mml-destroy-buffers t)
8873                                 (kill-local-variable 'mml-buffer-list)))
8874               (gnus-summary-edit-article-done
8875                ,(or (mail-header-references gnus-current-headers) "")
8876                ,(gnus-group-read-only-p)
8877                ,gnus-summary-buffer no-highlight))))))))
8878
8879 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8880
8881 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8882                                                  no-highlight)
8883   "Make edits to the current article permanent."
8884   (interactive)
8885   (save-excursion
8886    ;; The buffer restriction contains the entire article if it exists.
8887     (when (article-goto-body)
8888       (let ((lines (count-lines (point) (point-max)))
8889             (length (- (point-max) (point)))
8890             (case-fold-search t)
8891             (body (copy-marker (point))))
8892         (goto-char (point-min))
8893         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8894           (delete-region (match-beginning 1) (match-end 1))
8895           (insert (number-to-string length)))
8896         (goto-char (point-min))
8897         (when (re-search-forward
8898                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8899           (delete-region (match-beginning 1) (match-end 1))
8900           (insert (number-to-string length)))
8901         (goto-char (point-min))
8902         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8903           (delete-region (match-beginning 1) (match-end 1))
8904           (insert (number-to-string lines))))))
8905   ;; Replace the article.
8906   (let ((buf (current-buffer)))
8907     (with-temp-buffer
8908       (insert-buffer-substring buf)
8909
8910       (if (and (not read-only)
8911                (not (gnus-request-replace-article
8912                      (cdr gnus-article-current) (car gnus-article-current)
8913                      (current-buffer) t)))
8914           (error "Couldn't replace article")
8915         ;; Update the summary buffer.
8916         (if (and references
8917                  (equal (message-tokenize-header references " ")
8918                         (message-tokenize-header
8919                          (or (message-fetch-field "references") "") " ")))
8920             ;; We only have to update this line.
8921             (save-excursion
8922               (save-restriction
8923                 (message-narrow-to-head)
8924                 (let ((head (buffer-string))
8925                       header)
8926                   (with-temp-buffer
8927                     (insert (format "211 %d Article retrieved.\n"
8928                                     (cdr gnus-article-current)))
8929                     (insert head)
8930                     (insert ".\n")
8931                     (let ((nntp-server-buffer (current-buffer)))
8932                       (setq header (car (gnus-get-newsgroup-headers
8933                                          nil t))))
8934                     (save-excursion
8935                       (set-buffer gnus-summary-buffer)
8936                       (gnus-data-set-header
8937                        (gnus-data-find (cdr gnus-article-current))
8938                        header)
8939                       (gnus-summary-update-article-line
8940                        (cdr gnus-article-current) header)
8941                       (if (gnus-summary-goto-subject
8942                            (cdr gnus-article-current) nil t)
8943                           (gnus-summary-update-secondary-mark
8944                            (cdr gnus-article-current))))))))
8945           ;; Update threads.
8946           (set-buffer (or buffer gnus-summary-buffer))
8947           (gnus-summary-update-article (cdr gnus-article-current))
8948           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8949               (gnus-summary-update-secondary-mark
8950                (cdr gnus-article-current))))
8951         ;; Prettify the article buffer again.
8952         (unless no-highlight
8953           (save-excursion
8954             (set-buffer gnus-article-buffer)
8955             ;;;!!! Fix this -- article should be rehighlighted.
8956             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8957             (set-buffer gnus-original-article-buffer)
8958             (gnus-request-article
8959              (cdr gnus-article-current)
8960              (car gnus-article-current) (current-buffer))))
8961         ;; Prettify the summary buffer line.
8962         (when (gnus-visual-p 'summary-highlight 'highlight)
8963           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8964
8965 (defun gnus-summary-edit-wash (key)
8966   "Perform editing command KEY in the article buffer."
8967   (interactive
8968    (list
8969     (progn
8970       (message "%s" (concat (this-command-keys) "- "))
8971       (read-char))))
8972   (message "")
8973   (gnus-summary-edit-article)
8974   (execute-kbd-macro (concat (this-command-keys) key))
8975   (gnus-article-edit-done))
8976
8977 ;;; Respooling
8978
8979 (defun gnus-summary-respool-query (&optional silent trace)
8980   "Query where the respool algorithm would put this article."
8981   (interactive)
8982   (let (gnus-mark-article-hook)
8983     (gnus-summary-select-article)
8984     (save-excursion
8985       (set-buffer gnus-original-article-buffer)
8986       (save-restriction
8987         (message-narrow-to-head)
8988         (let ((groups (nnmail-article-group 'identity trace)))
8989           (unless silent
8990             (if groups
8991                 (message "This message would go to %s"
8992                          (mapconcat 'car groups ", "))
8993               (message "This message would go to no groups"))
8994             groups))))))
8995
8996 (defun gnus-summary-respool-trace ()
8997   "Trace where the respool algorithm would put this article.
8998 Display a buffer showing all fancy splitting patterns which matched."
8999   (interactive)
9000   (gnus-summary-respool-query nil t))
9001
9002 ;; Summary marking commands.
9003
9004 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9005   "Mark articles which has the same subject as read, and then select the next.
9006 If UNMARK is positive, remove any kind of mark.
9007 If UNMARK is negative, tick articles."
9008   (interactive "P")
9009   (when unmark
9010     (setq unmark (prefix-numeric-value unmark)))
9011   (let ((count
9012          (gnus-summary-mark-same-subject
9013           (gnus-summary-article-subject) unmark)))
9014     ;; Select next unread article.  If auto-select-same mode, should
9015     ;; select the first unread article.
9016     (gnus-summary-next-article t (and gnus-auto-select-same
9017                                       (gnus-summary-article-subject)))
9018     (gnus-message 7 "%d article%s marked as %s"
9019                   count (if (= count 1) " is" "s are")
9020                   (if unmark "unread" "read"))))
9021
9022 (defun gnus-summary-kill-same-subject (&optional unmark)
9023   "Mark articles which has the same subject as read.
9024 If UNMARK is positive, remove any kind of mark.
9025 If UNMARK is negative, tick articles."
9026   (interactive "P")
9027   (when unmark
9028     (setq unmark (prefix-numeric-value unmark)))
9029   (let ((count
9030          (gnus-summary-mark-same-subject
9031           (gnus-summary-article-subject) unmark)))
9032     ;; If marked as read, go to next unread subject.
9033     (when (null unmark)
9034       ;; Go to next unread subject.
9035       (gnus-summary-next-subject 1 t))
9036     (gnus-message 7 "%d articles are marked as %s"
9037                   count (if unmark "unread" "read"))))
9038
9039 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9040   "Mark articles with same SUBJECT as read, and return marked number.
9041 If optional argument UNMARK is positive, remove any kinds of marks.
9042 If optional argument UNMARK is negative, mark articles as unread instead."
9043   (let ((count 1))
9044     (save-excursion
9045       (cond
9046        ((null unmark)                   ; Mark as read.
9047         (while (and
9048                 (progn
9049                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9050                   (gnus-summary-show-thread) t)
9051                 (gnus-summary-find-subject subject))
9052           (setq count (1+ count))))
9053        ((> unmark 0)                    ; Tick.
9054         (while (and
9055                 (progn
9056                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9057                   (gnus-summary-show-thread) t)
9058                 (gnus-summary-find-subject subject))
9059           (setq count (1+ count))))
9060        (t                               ; Mark as unread.
9061         (while (and
9062                 (progn
9063                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9064                   (gnus-summary-show-thread) t)
9065                 (gnus-summary-find-subject subject))
9066           (setq count (1+ count)))))
9067       (gnus-set-mode-line 'summary)
9068       ;; Return the number of marked articles.
9069       count)))
9070
9071 (defun gnus-summary-mark-as-processable (n &optional unmark)
9072   "Set the process mark on the next N articles.
9073 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9074 the process mark instead.  The difference between N and the actual
9075 number of articles marked is returned."
9076   (interactive "P")
9077   (if (and (null n) (gnus-region-active-p))
9078       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9079     (setq n (prefix-numeric-value n))
9080     (let ((backward (< n 0))
9081           (n (abs n)))
9082       (while (and
9083               (> n 0)
9084               (if unmark
9085                   (gnus-summary-remove-process-mark
9086                    (gnus-summary-article-number))
9087                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9088               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9089         (setq n (1- n)))
9090       (when (/= 0 n)
9091         (gnus-message 7 "No more articles"))
9092       (gnus-summary-recenter)
9093       (gnus-summary-position-point)
9094       n)))
9095
9096 (defun gnus-summary-unmark-as-processable (n)
9097   "Remove the process mark from the next N articles.
9098 If N is negative, unmark backward instead.  The difference between N and
9099 the actual number of articles unmarked is returned."
9100   (interactive "P")
9101   (gnus-summary-mark-as-processable n t))
9102
9103 (defun gnus-summary-unmark-all-processable ()
9104   "Remove the process mark from all articles."
9105   (interactive)
9106   (save-excursion
9107     (while gnus-newsgroup-processable
9108       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9109   (gnus-summary-position-point))
9110
9111 (defun gnus-summary-add-mark (article type)
9112   "Mark ARTICLE with a mark of TYPE."
9113   (let ((vtype (car (assq type gnus-article-mark-lists)))
9114         var)
9115     (if (not vtype)
9116         (error "No such mark type: %s" type)
9117       (setq var (intern (format "gnus-newsgroup-%s" type)))
9118       (set var (cons article (symbol-value var)))
9119       (if (memq type '(processable cached replied forwarded recent saved))
9120           (gnus-summary-update-secondary-mark article)
9121         ;;; !!! This is bogus.  We should find out what primary
9122         ;;; !!! mark we want to set.
9123         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9124
9125 (defun gnus-summary-mark-as-expirable (n)
9126   "Mark N articles forward as expirable.
9127 If N is negative, mark backward instead.  The difference between N and
9128 the actual number of articles marked is returned."
9129   (interactive "p")
9130   (gnus-summary-mark-forward n gnus-expirable-mark))
9131
9132 (defun gnus-summary-mark-article-as-replied (article)
9133   "Mark ARTICLE as replied to and update the summary line.
9134 ARTICLE can also be a list of articles."
9135   (interactive (list (gnus-summary-article-number)))
9136   (let ((articles (if (listp article) article (list article))))
9137     (dolist (article articles)
9138       (push article gnus-newsgroup-replied)
9139       (let ((buffer-read-only nil))
9140         (when (gnus-summary-goto-subject article nil t)
9141           (gnus-summary-update-secondary-mark article))))))
9142
9143 (defun gnus-summary-mark-article-as-forwarded (article)
9144   "Mark ARTICLE as forwarded and update the summary line.
9145 ARTICLE can also be a list of articles."
9146   (let ((articles (if (listp article) article (list article))))
9147     (dolist (article articles)
9148       (push article gnus-newsgroup-forwarded)
9149       (let ((buffer-read-only nil))
9150         (when (gnus-summary-goto-subject article nil t)
9151           (gnus-summary-update-secondary-mark article))))))
9152
9153 (defun gnus-summary-set-bookmark (article)
9154   "Set a bookmark in current article."
9155   (interactive (list (gnus-summary-article-number)))
9156   (when (or (not (get-buffer gnus-article-buffer))
9157             (not gnus-current-article)
9158             (not gnus-article-current)
9159             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9160     (error "No current article selected"))
9161   ;; Remove old bookmark, if one exists.
9162   (let ((old (assq article gnus-newsgroup-bookmarks)))
9163     (when old
9164       (setq gnus-newsgroup-bookmarks
9165             (delq old gnus-newsgroup-bookmarks))))
9166   ;; Set the new bookmark, which is on the form
9167   ;; (article-number . line-number-in-body).
9168   (push
9169    (cons article
9170          (save-excursion
9171            (set-buffer gnus-article-buffer)
9172            (count-lines
9173             (min (point)
9174                  (save-excursion
9175                    (goto-char (point-min))
9176                    (search-forward "\n\n" nil t)
9177                    (point)))
9178             (point))))
9179    gnus-newsgroup-bookmarks)
9180   (gnus-message 6 "A bookmark has been added to the current article."))
9181
9182 (defun gnus-summary-remove-bookmark (article)
9183   "Remove the bookmark from the current article."
9184   (interactive (list (gnus-summary-article-number)))
9185   ;; Remove old bookmark, if one exists.
9186   (let ((old (assq article gnus-newsgroup-bookmarks)))
9187     (if old
9188         (progn
9189           (setq gnus-newsgroup-bookmarks
9190                 (delq old gnus-newsgroup-bookmarks))
9191           (gnus-message 6 "Removed bookmark."))
9192       (gnus-message 6 "No bookmark in current article."))))
9193
9194 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9195 (defun gnus-summary-mark-as-dormant (n)
9196   "Mark N articles forward as dormant.
9197 If N is negative, mark backward instead.  The difference between N and
9198 the actual number of articles marked is returned."
9199   (interactive "p")
9200   (gnus-summary-mark-forward n gnus-dormant-mark))
9201
9202 (defun gnus-summary-set-process-mark (article)
9203   "Set the process mark on ARTICLE and update the summary line."
9204   (setq gnus-newsgroup-processable
9205         (cons article
9206               (delq article gnus-newsgroup-processable)))
9207   (when (gnus-summary-goto-subject article)
9208     (gnus-summary-show-thread)
9209     (gnus-summary-goto-subject article)
9210     (gnus-summary-update-secondary-mark article)))
9211
9212 (defun gnus-summary-remove-process-mark (article)
9213   "Remove the process mark from ARTICLE and update the summary line."
9214   (setq gnus-newsgroup-processable (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-set-saved-mark (article)
9221   "Set the process mark on ARTICLE and update the summary line."
9222   (push article gnus-newsgroup-saved)
9223   (when (gnus-summary-goto-subject article)
9224     (gnus-summary-update-secondary-mark article)))
9225
9226 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9227   "Mark N articles as read forwards.
9228 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9229 The difference between N and the actual number of articles marked is
9230 returned.
9231 Iff NO-EXPIRE, auto-expiry will be inhibited."
9232   (interactive "p")
9233   (gnus-summary-show-thread)
9234   (let ((backward (< n 0))
9235         (gnus-summary-goto-unread
9236          (and gnus-summary-goto-unread
9237               (not (eq gnus-summary-goto-unread 'never))
9238               (not (memq mark (list gnus-unread-mark
9239                                     gnus-ticked-mark gnus-dormant-mark)))))
9240         (n (abs n))
9241         (mark (or mark gnus-del-mark)))
9242     (while (and (> n 0)
9243                 (gnus-summary-mark-article nil mark no-expire)
9244                 (zerop (gnus-summary-next-subject
9245                         (if backward -1 1)
9246                         (and gnus-summary-goto-unread
9247                              (not (eq gnus-summary-goto-unread 'never)))
9248                         t)))
9249       (setq n (1- n)))
9250     (when (/= 0 n)
9251       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9252     (gnus-summary-recenter)
9253     (gnus-summary-position-point)
9254     (gnus-set-mode-line 'summary)
9255     n))
9256
9257 (defun gnus-summary-mark-article-as-read (mark)
9258   "Mark the current article quickly as read with MARK."
9259   (let ((article (gnus-summary-article-number)))
9260     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9261     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9262     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9263     (push (cons article mark) gnus-newsgroup-reads)
9264     ;; Possibly remove from cache, if that is used.
9265     (when gnus-use-cache
9266       (gnus-cache-enter-remove-article article))
9267     ;; Allow the backend to change the mark.
9268     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9269     ;; Check for auto-expiry.
9270     (when (and gnus-newsgroup-auto-expire
9271                (memq mark gnus-auto-expirable-marks))
9272       (setq mark gnus-expirable-mark)
9273       ;; Let the backend know about the mark change.
9274       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9275       (push article gnus-newsgroup-expirable))
9276     ;; Set the mark in the buffer.
9277     (gnus-summary-update-mark mark 'unread)
9278     t))
9279
9280 (defun gnus-summary-mark-article-as-unread (mark)
9281   "Mark the current article quickly as unread with MARK."
9282   (let* ((article (gnus-summary-article-number))
9283          (old-mark (gnus-summary-article-mark article)))
9284     ;; Allow the backend to change the mark.
9285     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9286     (if (eq mark old-mark)
9287         t
9288       (if (<= article 0)
9289           (progn
9290             (gnus-error 1 "Can't mark negative article numbers")
9291             nil)
9292         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9293         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9294         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9295         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9296         (cond ((= mark gnus-ticked-mark)
9297                (setq gnus-newsgroup-marked
9298                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9299                                               article)))
9300               ((= mark gnus-dormant-mark)
9301                (setq gnus-newsgroup-dormant
9302                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9303                                               article)))
9304               (t
9305                (setq gnus-newsgroup-unreads
9306                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9307                                               article))))
9308         (gnus-pull article gnus-newsgroup-reads)
9309
9310         ;; See whether the article is to be put in the cache.
9311         (and gnus-use-cache
9312              (vectorp (gnus-summary-article-header article))
9313              (save-excursion
9314                (gnus-cache-possibly-enter-article
9315                 gnus-newsgroup-name article
9316                 (= mark gnus-ticked-mark)
9317                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9318
9319         ;; Fix the mark.
9320         (gnus-summary-update-mark mark 'unread)
9321         t))))
9322
9323 (defun gnus-summary-mark-article (&optional article mark no-expire)
9324   "Mark ARTICLE with MARK.  MARK can be any character.
9325 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9326 `??' (dormant) and `?E' (expirable).
9327 If MARK is nil, then the default character `?r' is used.
9328 If ARTICLE is nil, then the article on the current line will be
9329 marked.
9330 Iff NO-EXPIRE, auto-expiry will be inhibited."
9331   ;; The mark might be a string.
9332   (when (stringp mark)
9333     (setq mark (aref mark 0)))
9334   ;; If no mark is given, then we check auto-expiring.
9335   (when (null mark)
9336     (setq mark gnus-del-mark))
9337   (when (and (not no-expire)
9338              gnus-newsgroup-auto-expire
9339              (memq mark gnus-auto-expirable-marks))
9340     (setq mark gnus-expirable-mark))
9341   (let ((article (or article (gnus-summary-article-number)))
9342         (old-mark (gnus-summary-article-mark article)))
9343     ;; Allow the backend to change the mark.
9344     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9345     (if (eq mark old-mark)
9346         t
9347       (unless article
9348         (error "No article on current line"))
9349       (if (not (if (or (= mark gnus-unread-mark)
9350                        (= mark gnus-ticked-mark)
9351                        (= mark gnus-dormant-mark))
9352                    (gnus-mark-article-as-unread article mark)
9353                  (gnus-mark-article-as-read article mark)))
9354           t
9355         ;; See whether the article is to be put in the cache.
9356         (and gnus-use-cache
9357              (not (= mark gnus-canceled-mark))
9358              (vectorp (gnus-summary-article-header article))
9359              (save-excursion
9360                (gnus-cache-possibly-enter-article
9361                 gnus-newsgroup-name article
9362                 (= mark gnus-ticked-mark)
9363                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9364
9365         (when (gnus-summary-goto-subject article nil t)
9366           (let ((buffer-read-only nil))
9367             (gnus-summary-show-thread)
9368             ;; Fix the mark.
9369             (gnus-summary-update-mark mark 'unread)
9370             t))))))
9371
9372 (defun gnus-summary-update-secondary-mark (article)
9373   "Update the secondary (read, process, cache) mark."
9374   (gnus-summary-update-mark
9375    (cond ((memq article gnus-newsgroup-processable)
9376           gnus-process-mark)
9377          ((memq article gnus-newsgroup-cached)
9378           gnus-cached-mark)
9379          ((memq article gnus-newsgroup-replied)
9380           gnus-replied-mark)
9381          ((memq article gnus-newsgroup-forwarded)
9382           gnus-forwarded-mark)
9383          ((memq article gnus-newsgroup-saved)
9384           gnus-saved-mark)
9385          ((memq article gnus-newsgroup-recent)
9386           gnus-recent-mark)
9387          ((memq article gnus-newsgroup-unseen)
9388           gnus-unseen-mark)
9389          (t gnus-no-mark))
9390    'replied)
9391   (when (gnus-visual-p 'summary-highlight 'highlight)
9392     (gnus-run-hooks 'gnus-summary-update-hook))
9393   t)
9394
9395 (defun gnus-summary-update-mark (mark type)
9396   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9397         (buffer-read-only nil))
9398     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9399     (when forward
9400       (when (looking-at "\r")
9401         (incf forward))
9402       (when (<= (+ forward (point)) (point-max))
9403         ;; Go to the right position on the line.
9404         (goto-char (+ forward (point)))
9405         ;; Replace the old mark with the new mark.
9406         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9407         ;; Optionally update the marks by some user rule.
9408         (when (eq type 'unread)
9409           (gnus-data-set-mark
9410            (gnus-data-find (gnus-summary-article-number)) mark)
9411           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9412
9413 (defun gnus-mark-article-as-read (article &optional mark)
9414   "Enter ARTICLE in the pertinent lists and remove it from others."
9415   ;; Make the article expirable.
9416   (let ((mark (or mark gnus-del-mark)))
9417     (setq gnus-newsgroup-expirable
9418           (if (= mark gnus-expirable-mark)
9419               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9420             (delq article gnus-newsgroup-expirable)))
9421     ;; Remove from unread and marked lists.
9422     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9423     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9424     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9425     (push (cons article mark) gnus-newsgroup-reads)
9426     ;; Possibly remove from cache, if that is used.
9427     (when gnus-use-cache
9428       (gnus-cache-enter-remove-article article))
9429     t))
9430
9431 (defun gnus-mark-article-as-unread (article &optional mark)
9432   "Enter ARTICLE in the pertinent lists and remove it from others."
9433   (let ((mark (or mark gnus-ticked-mark)))
9434     (if (<= article 0)
9435         (progn
9436           (gnus-error 1 "Can't mark negative article numbers")
9437           nil)
9438       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9439             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9440             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9441             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9442
9443       ;; Unsuppress duplicates?
9444       (when gnus-suppress-duplicates
9445         (gnus-dup-unsuppress-article article))
9446
9447       (cond ((= mark gnus-ticked-mark)
9448              (setq gnus-newsgroup-marked
9449                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9450             ((= mark gnus-dormant-mark)
9451              (setq gnus-newsgroup-dormant
9452                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9453             (t
9454              (setq gnus-newsgroup-unreads
9455                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9456       (gnus-pull article gnus-newsgroup-reads)
9457       t)))
9458
9459 (defalias 'gnus-summary-mark-as-unread-forward
9460   'gnus-summary-tick-article-forward)
9461 (make-obsolete 'gnus-summary-mark-as-unread-forward
9462                'gnus-summary-tick-article-forward)
9463 (defun gnus-summary-tick-article-forward (n)
9464   "Tick N articles forwards.
9465 If N is negative, tick backwards instead.
9466 The difference between N and the number of articles ticked is returned."
9467   (interactive "p")
9468   (gnus-summary-mark-forward n gnus-ticked-mark))
9469
9470 (defalias 'gnus-summary-mark-as-unread-backward
9471   'gnus-summary-tick-article-backward)
9472 (make-obsolete 'gnus-summary-mark-as-unread-backward
9473                'gnus-summary-tick-article-backward)
9474 (defun gnus-summary-tick-article-backward (n)
9475   "Tick N articles backwards.
9476 The difference between N and the number of articles ticked is returned."
9477   (interactive "p")
9478   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9479
9480 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9481 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9482 (defun gnus-summary-tick-article (&optional article clear-mark)
9483   "Mark current article as unread.
9484 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9485 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9486   (interactive)
9487   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9488                                        gnus-ticked-mark)))
9489
9490 (defun gnus-summary-mark-as-read-forward (n)
9491   "Mark N articles as read forwards.
9492 If N is negative, mark backwards instead.
9493 The difference between N and the actual number of articles marked is
9494 returned."
9495   (interactive "p")
9496   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9497
9498 (defun gnus-summary-mark-as-read-backward (n)
9499   "Mark the N articles as read backwards.
9500 The difference between N and the actual number of articles marked is
9501 returned."
9502   (interactive "p")
9503   (gnus-summary-mark-forward
9504    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9505
9506 (defun gnus-summary-mark-as-read (&optional article mark)
9507   "Mark current article as read.
9508 ARTICLE specifies the article to be marked as read.
9509 MARK specifies a string to be inserted at the beginning of the line."
9510   (gnus-summary-mark-article article mark))
9511
9512 (defun gnus-summary-clear-mark-forward (n)
9513   "Clear marks from N articles forward.
9514 If N is negative, clear backward instead.
9515 The difference between N and the number of marks cleared is returned."
9516   (interactive "p")
9517   (gnus-summary-mark-forward n gnus-unread-mark))
9518
9519 (defun gnus-summary-clear-mark-backward (n)
9520   "Clear marks from N articles backward.
9521 The difference between N and the number of marks cleared is returned."
9522   (interactive "p")
9523   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9524
9525 (defun gnus-summary-mark-unread-as-read ()
9526   "Intended to be used by `gnus-summary-mark-article-hook'."
9527   (when (memq gnus-current-article gnus-newsgroup-unreads)
9528     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9529
9530 (defun gnus-summary-mark-read-and-unread-as-read ()
9531   "Intended to be used by `gnus-summary-mark-article-hook'."
9532   (let ((mark (gnus-summary-article-mark)))
9533     (when (or (gnus-unread-mark-p mark)
9534               (gnus-read-mark-p mark))
9535       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9536
9537 (defun gnus-summary-mark-unread-as-ticked ()
9538   "Intended to be used by `gnus-summary-mark-article-hook'."
9539   (when (memq gnus-current-article gnus-newsgroup-unreads)
9540     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9541
9542 (defun gnus-summary-mark-region-as-read (point mark all)
9543   "Mark all unread articles between point and mark as read.
9544 If given a prefix, mark all articles between point and mark as read,
9545 even ticked and dormant ones."
9546   (interactive "r\nP")
9547   (save-excursion
9548     (let (article)
9549       (goto-char point)
9550       (beginning-of-line)
9551       (while (and
9552               (< (point) mark)
9553               (progn
9554                 (when (or all
9555                           (memq (setq article (gnus-summary-article-number))
9556                                 gnus-newsgroup-unreads))
9557                   (gnus-summary-mark-article article gnus-del-mark))
9558                 t)
9559               (gnus-summary-find-next))))))
9560
9561 (defun gnus-summary-mark-below (score mark)
9562   "Mark articles with score less than SCORE with MARK."
9563   (interactive "P\ncMark: ")
9564   (setq score (if score
9565                   (prefix-numeric-value score)
9566                 (or gnus-summary-default-score 0)))
9567   (save-excursion
9568     (set-buffer gnus-summary-buffer)
9569     (goto-char (point-min))
9570     (while
9571         (progn
9572           (and (< (gnus-summary-article-score) score)
9573                (gnus-summary-mark-article nil mark))
9574           (gnus-summary-find-next)))))
9575
9576 (defun gnus-summary-kill-below (&optional score)
9577   "Mark articles with score below SCORE as read."
9578   (interactive "P")
9579   (gnus-summary-mark-below score gnus-killed-mark))
9580
9581 (defun gnus-summary-clear-above (&optional score)
9582   "Clear all marks from articles with score above SCORE."
9583   (interactive "P")
9584   (gnus-summary-mark-above score gnus-unread-mark))
9585
9586 (defun gnus-summary-tick-above (&optional score)
9587   "Tick all articles with score above SCORE."
9588   (interactive "P")
9589   (gnus-summary-mark-above score gnus-ticked-mark))
9590
9591 (defun gnus-summary-mark-above (score mark)
9592   "Mark articles with score over SCORE with MARK."
9593   (interactive "P\ncMark: ")
9594   (setq score (if score
9595                   (prefix-numeric-value score)
9596                 (or gnus-summary-default-score 0)))
9597   (save-excursion
9598     (set-buffer gnus-summary-buffer)
9599     (goto-char (point-min))
9600     (while (and (progn
9601                   (when (> (gnus-summary-article-score) score)
9602                     (gnus-summary-mark-article nil mark))
9603                   t)
9604                 (gnus-summary-find-next)))))
9605
9606 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9607 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9608 (defun gnus-summary-limit-include-expunged (&optional no-error)
9609   "Display all the hidden articles that were expunged for low scores."
9610   (interactive)
9611   (let ((buffer-read-only nil))
9612     (let ((scored gnus-newsgroup-scored)
9613           headers h)
9614       (while scored
9615         (unless (gnus-summary-article-header (caar scored))
9616           (and (setq h (gnus-number-to-header (caar scored)))
9617                (< (cdar scored) gnus-summary-expunge-below)
9618                (push h headers)))
9619         (setq scored (cdr scored)))
9620       (if (not headers)
9621           (when (not no-error)
9622             (error "No expunged articles hidden"))
9623         (goto-char (point-min))
9624         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9625         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9626         (mapcar (lambda (x) (push (mail-header-number x)
9627                                   gnus-newsgroup-limit))
9628                 headers)
9629         (gnus-summary-prepare-unthreaded (nreverse headers))
9630         (goto-char (point-min))
9631         (gnus-summary-position-point)
9632         t))))
9633
9634 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9635   "Mark all unread articles in this newsgroup as read.
9636 If prefix argument ALL is non-nil, ticked and dormant articles will
9637 also be marked as read.
9638 If QUIETLY is non-nil, no questions will be asked.
9639 If TO-HERE is non-nil, it should be a point in the buffer.  All
9640 articles before (after, if REVERSE is set) this point will be marked as read.
9641 Note that this function will only catch up the unread article
9642 in the current summary buffer limitation.
9643 The number of articles marked as read is returned."
9644   (interactive "P")
9645   (prog1
9646       (save-excursion
9647         (when (or quietly
9648                   (not gnus-interactive-catchup) ;Without confirmation?
9649                   gnus-expert-user
9650                   (gnus-y-or-n-p
9651                    (if all
9652                        "Mark absolutely all articles as read? "
9653                      "Mark all unread articles as read? ")))
9654           (if (and not-mark
9655                    (not gnus-newsgroup-adaptive)
9656                    (not gnus-newsgroup-auto-expire)
9657                    (not gnus-suppress-duplicates)
9658                    (or (not gnus-use-cache)
9659                        (eq gnus-use-cache 'passive)))
9660               (progn
9661                 (when all
9662                   (setq gnus-newsgroup-marked nil
9663                         gnus-newsgroup-dormant nil))
9664                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9665             ;; We actually mark all articles as canceled, which we
9666             ;; have to do when using auto-expiry or adaptive scoring.
9667             (gnus-summary-show-all-threads)
9668             (if (and to-here reverse)
9669                 (progn
9670                   (goto-char to-here)
9671                   (while (and
9672                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9673                           (gnus-summary-find-next (not all) nil nil t))))
9674               (when (gnus-summary-first-subject (not all) t)
9675                 (while (and
9676                         (if to-here (< (point) to-here) t)
9677                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9678                         (gnus-summary-find-next (not all) nil nil t)))))
9679             (gnus-set-mode-line 'summary))
9680           t))
9681     (gnus-summary-position-point)))
9682
9683 (defun gnus-summary-catchup-to-here (&optional all)
9684   "Mark all unticked articles before the current one as read.
9685 If ALL is non-nil, also mark ticked and dormant articles as read."
9686   (interactive "P")
9687   (save-excursion
9688     (gnus-save-hidden-threads
9689       (let ((beg (point)))
9690         ;; We check that there are unread articles.
9691         (when (or all (gnus-summary-find-prev))
9692           (gnus-summary-catchup all t beg)))))
9693   (gnus-summary-position-point))
9694
9695 (defun gnus-summary-catchup-from-here (&optional all)
9696   "Mark all unticked articles after the current one as read.
9697 If ALL is non-nil, also mark ticked and dormant articles as read."
9698   (interactive "P")
9699   (save-excursion
9700     (gnus-save-hidden-threads
9701       (let ((beg (point)))
9702         ;; We check that there are unread articles.
9703         (when (or all (gnus-summary-find-next))
9704           (gnus-summary-catchup all t beg nil t)))))
9705
9706   (gnus-summary-position-point))
9707 (defun gnus-summary-catchup-all (&optional quietly)
9708   "Mark all articles in this newsgroup as read."
9709   (interactive "P")
9710   (gnus-summary-catchup t quietly))
9711
9712 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9713   "Mark all unread articles in this group as read, then exit.
9714 If prefix argument ALL is non-nil, all articles are marked as read.
9715 If QUIETLY is non-nil, no questions will be asked."
9716   (interactive "P")
9717   (when (gnus-summary-catchup all quietly nil 'fast)
9718     ;; Select next newsgroup or exit.
9719     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9720              (eq gnus-auto-select-next 'quietly))
9721         (gnus-summary-next-group nil)
9722       (gnus-summary-exit))))
9723
9724 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9725   "Mark all articles in this newsgroup as read, and then exit."
9726   (interactive "P")
9727   (gnus-summary-catchup-and-exit t quietly))
9728
9729 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9730   "Mark all articles in this group as read and select the next group.
9731 If given a prefix, mark all articles, unread as well as ticked, as
9732 read."
9733   (interactive "P")
9734   (save-excursion
9735     (gnus-summary-catchup all))
9736   (gnus-summary-next-group))
9737
9738 ;;;
9739 ;;; with article
9740 ;;;
9741
9742 (defmacro gnus-with-article (article &rest forms)
9743   "Select ARTICLE and perform FORMS in the original article buffer.
9744 Then replace the article with the result."
9745   `(progn
9746      ;; We don't want the article to be marked as read.
9747      (let (gnus-mark-article-hook)
9748        (gnus-summary-select-article t t nil ,article))
9749      (set-buffer gnus-original-article-buffer)
9750      ,@forms
9751      (if (not (gnus-check-backend-function
9752                'request-replace-article (car gnus-article-current)))
9753          (gnus-message 5 "Read-only group; not replacing")
9754        (unless (gnus-request-replace-article
9755                 ,article (car gnus-article-current)
9756                 (current-buffer) t)
9757          (error "Couldn't replace article")))
9758      ;; The cache and backlog have to be flushed somewhat.
9759      (when gnus-keep-backlog
9760        (gnus-backlog-remove-article
9761         (car gnus-article-current) (cdr gnus-article-current)))
9762      (when gnus-use-cache
9763        (gnus-cache-update-article
9764         (car gnus-article-current) (cdr gnus-article-current)))))
9765
9766 (put 'gnus-with-article 'lisp-indent-function 1)
9767 (put 'gnus-with-article 'edebug-form-spec '(form body))
9768
9769 ;; Thread-based commands.
9770
9771 (defun gnus-summary-articles-in-thread (&optional article)
9772   "Return a list of all articles in the current thread.
9773 If ARTICLE is non-nil, return all articles in the thread that starts
9774 with that article."
9775   (let* ((article (or article (gnus-summary-article-number)))
9776          (data (gnus-data-find-list article))
9777          (top-level (gnus-data-level (car data)))
9778          (top-subject
9779           (cond ((null gnus-thread-operation-ignore-subject)
9780                  (gnus-simplify-subject-re
9781                   (mail-header-subject (gnus-data-header (car data)))))
9782                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9783                  (gnus-simplify-subject-fuzzy
9784                   (mail-header-subject (gnus-data-header (car data)))))
9785                 (t nil)))
9786          (end-point (save-excursion
9787                       (if (gnus-summary-go-to-next-thread)
9788                           (point) (point-max))))
9789          articles)
9790     (while (and data
9791                 (< (gnus-data-pos (car data)) end-point))
9792       (when (or (not top-subject)
9793                 (string= top-subject
9794                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9795                              (gnus-simplify-subject-fuzzy
9796                               (mail-header-subject
9797                                (gnus-data-header (car data))))
9798                            (gnus-simplify-subject-re
9799                             (mail-header-subject
9800                              (gnus-data-header (car data)))))))
9801         (push (gnus-data-number (car data)) articles))
9802       (unless (and (setq data (cdr data))
9803                    (> (gnus-data-level (car data)) top-level))
9804         (setq data nil)))
9805     ;; Return the list of articles.
9806     (nreverse articles)))
9807
9808 (defun gnus-summary-rethread-current ()
9809   "Rethread the thread the current article is part of."
9810   (interactive)
9811   (let* ((gnus-show-threads t)
9812          (article (gnus-summary-article-number))
9813          (id (mail-header-id (gnus-summary-article-header)))
9814          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9815     (unless id
9816       (error "No article on the current line"))
9817     (gnus-rebuild-thread id)
9818     (gnus-summary-goto-subject article)))
9819
9820 (defun gnus-summary-reparent-thread ()
9821   "Make the current article child of the marked (or previous) article.
9822
9823 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9824 is non-nil or the Subject: of both articles are the same."
9825   (interactive)
9826   (unless (not (gnus-group-read-only-p))
9827     (error "The current newsgroup does not support article editing"))
9828   (unless (<= (length gnus-newsgroup-processable) 1)
9829     (error "No more than one article may be marked"))
9830   (save-window-excursion
9831     (let ((gnus-article-buffer " *reparent*")
9832           (current-article (gnus-summary-article-number))
9833           ;; First grab the marked article, otherwise one line up.
9834           (parent-article (if (not (null gnus-newsgroup-processable))
9835                               (car gnus-newsgroup-processable)
9836                             (save-excursion
9837                               (if (eq (forward-line -1) 0)
9838                                   (gnus-summary-article-number)
9839                                 (error "Beginning of summary buffer"))))))
9840       (unless (not (eq current-article parent-article))
9841         (error "An article may not be self-referential"))
9842       (let ((message-id (mail-header-id
9843                          (gnus-summary-article-header parent-article))))
9844         (unless (and message-id (not (equal message-id "")))
9845           (error "No message-id in desired parent"))
9846         (gnus-with-article current-article
9847           (save-restriction
9848             (goto-char (point-min))
9849             (message-narrow-to-head)
9850             (if (re-search-forward "^References: " nil t)
9851                 (progn
9852                   (re-search-forward "^[^ \t]" nil t)
9853                   (forward-line -1)
9854                   (end-of-line)
9855                   (insert " " message-id))
9856               (insert "References: " message-id "\n"))))
9857         (set-buffer gnus-summary-buffer)
9858         (gnus-summary-unmark-all-processable)
9859         (gnus-summary-update-article current-article)
9860         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9861             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9862         (gnus-summary-rethread-current)
9863         (gnus-message 3 "Article %d is now the child of article %d"
9864                       current-article parent-article)))))
9865
9866 (defun gnus-summary-toggle-threads (&optional arg)
9867   "Toggle showing conversation threads.
9868 If ARG is positive number, turn showing conversation threads on."
9869   (interactive "P")
9870   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9871     (setq gnus-show-threads
9872           (if (null arg) (not gnus-show-threads)
9873             (> (prefix-numeric-value arg) 0)))
9874     (gnus-summary-prepare)
9875     (gnus-summary-goto-subject current)
9876     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9877     (gnus-summary-position-point)))
9878
9879 (defun gnus-summary-show-all-threads ()
9880   "Show all threads."
9881   (interactive)
9882   (save-excursion
9883     (let ((buffer-read-only nil))
9884       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9885   (gnus-summary-position-point))
9886
9887 (defun gnus-summary-show-thread ()
9888   "Show thread subtrees.
9889 Returns nil if no thread was there to be shown."
9890   (interactive)
9891   (let ((buffer-read-only nil)
9892         (orig (point))
9893         ;; first goto end then to beg, to have point at beg after let
9894         (end (progn (end-of-line) (point)))
9895         (beg (progn (beginning-of-line) (point))))
9896     (prog1
9897         ;; Any hidden lines here?
9898         (search-forward "\r" end t)
9899       (subst-char-in-region beg end ?\^M ?\n t)
9900       (goto-char orig)
9901       (gnus-summary-position-point))))
9902
9903 (defun gnus-summary-maybe-hide-threads ()
9904   "If requested, hide the threads that should be hidden."
9905   (when (and gnus-show-threads
9906              gnus-thread-hide-subtree)
9907     (gnus-summary-hide-all-threads
9908      (if (or (consp gnus-thread-hide-subtree)
9909              (gnus-functionp gnus-thread-hide-subtree))
9910          (gnus-make-predicate gnus-thread-hide-subtree)
9911        nil))))
9912
9913 ;;; Hiding predicates.
9914
9915 (defun gnus-article-unread-p (header)
9916   (memq (mail-header-number header) gnus-newsgroup-unreads))
9917
9918 (defun gnus-article-unseen-p (header)
9919   (memq (mail-header-number header) gnus-newsgroup-unseen))
9920
9921 (defun gnus-map-articles (predicate articles)
9922   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9923   (apply 'gnus-or (mapcar predicate
9924                           (mapcar 'gnus-summary-article-header articles))))
9925
9926 (defun gnus-summary-hide-all-threads (&optional predicate)
9927   "Hide all thread subtrees.
9928 If PREDICATE is supplied, threads that satisfy this predicate
9929 will not be hidden."
9930   (interactive)
9931   (save-excursion
9932     (goto-char (point-min))
9933     (let ((end nil))
9934       (while (not end)
9935         (when (or (not predicate)
9936                   (gnus-map-articles
9937                    predicate (gnus-summary-article-children)))
9938             (gnus-summary-hide-thread))
9939         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9940   (gnus-summary-position-point))
9941
9942 (defun gnus-summary-hide-thread ()
9943   "Hide thread subtrees.
9944 If PREDICATE is supplied, threads that satisfy this predicate
9945 will not be hidden.
9946 Returns nil if no threads were there to be hidden."
9947   (interactive)
9948   (let ((buffer-read-only nil)
9949         (start (point))
9950         (article (gnus-summary-article-number)))
9951     (goto-char start)
9952     ;; Go forward until either the buffer ends or the subthread
9953     ;; ends.
9954     (when (and (not (eobp))
9955                (or (zerop (gnus-summary-next-thread 1 t))
9956                    (goto-char (point-max))))
9957       (prog1
9958           (if (and (> (point) start)
9959                    (search-backward "\n" start t))
9960               (progn
9961                 (subst-char-in-region start (point) ?\n ?\^M)
9962                 (gnus-summary-goto-subject article))
9963             (goto-char start)
9964             nil)))))
9965
9966 (defun gnus-summary-go-to-next-thread (&optional previous)
9967   "Go to the same level (or less) next thread.
9968 If PREVIOUS is non-nil, go to previous thread instead.
9969 Return the article number moved to, or nil if moving was impossible."
9970   (let ((level (gnus-summary-thread-level))
9971         (way (if previous -1 1))
9972         (beg (point)))
9973     (forward-line way)
9974     (while (and (not (eobp))
9975                 (< level (gnus-summary-thread-level)))
9976       (forward-line way))
9977     (if (eobp)
9978         (progn
9979           (goto-char beg)
9980           nil)
9981       (setq beg (point))
9982       (prog1
9983           (gnus-summary-article-number)
9984         (goto-char beg)))))
9985
9986 (defun gnus-summary-next-thread (n &optional silent)
9987   "Go to the same level next N'th thread.
9988 If N is negative, search backward instead.
9989 Returns the difference between N and the number of skips actually
9990 done.
9991
9992 If SILENT, don't output messages."
9993   (interactive "p")
9994   (let ((backward (< n 0))
9995         (n (abs n)))
9996     (while (and (> n 0)
9997                 (gnus-summary-go-to-next-thread backward))
9998       (decf n))
9999     (unless silent
10000       (gnus-summary-position-point))
10001     (when (and (not silent) (/= 0 n))
10002       (gnus-message 7 "No more threads"))
10003     n))
10004
10005 (defun gnus-summary-prev-thread (n)
10006   "Go to the same level previous N'th thread.
10007 Returns the difference between N and the number of skips actually
10008 done."
10009   (interactive "p")
10010   (gnus-summary-next-thread (- n)))
10011
10012 (defun gnus-summary-go-down-thread ()
10013   "Go down one level in the current thread."
10014   (let ((children (gnus-summary-article-children)))
10015     (when children
10016       (gnus-summary-goto-subject (car children)))))
10017
10018 (defun gnus-summary-go-up-thread ()
10019   "Go up one level in the current thread."
10020   (let ((parent (gnus-summary-article-parent)))
10021     (when parent
10022       (gnus-summary-goto-subject parent))))
10023
10024 (defun gnus-summary-down-thread (n)
10025   "Go down thread N steps.
10026 If N is negative, go up instead.
10027 Returns the difference between N and how many steps down that were
10028 taken."
10029   (interactive "p")
10030   (let ((up (< n 0))
10031         (n (abs n)))
10032     (while (and (> n 0)
10033                 (if up (gnus-summary-go-up-thread)
10034                   (gnus-summary-go-down-thread)))
10035       (setq n (1- n)))
10036     (gnus-summary-position-point)
10037     (when (/= 0 n)
10038       (gnus-message 7 "Can't go further"))
10039     n))
10040
10041 (defun gnus-summary-up-thread (n)
10042   "Go up thread N steps.
10043 If N is negative, go down instead.
10044 Returns the difference between N and how many steps down that were
10045 taken."
10046   (interactive "p")
10047   (gnus-summary-down-thread (- n)))
10048
10049 (defun gnus-summary-top-thread ()
10050   "Go to the top of the thread."
10051   (interactive)
10052   (while (gnus-summary-go-up-thread))
10053   (gnus-summary-article-number))
10054
10055 (defun gnus-summary-kill-thread (&optional unmark)
10056   "Mark articles under current thread as read.
10057 If the prefix argument is positive, remove any kinds of marks.
10058 If the prefix argument is negative, tick articles instead."
10059   (interactive "P")
10060   (when unmark
10061     (setq unmark (prefix-numeric-value unmark)))
10062   (let ((articles (gnus-summary-articles-in-thread)))
10063     (save-excursion
10064       ;; Expand the thread.
10065       (gnus-summary-show-thread)
10066       ;; Mark all the articles.
10067       (while articles
10068         (gnus-summary-goto-subject (car articles))
10069         (cond ((null unmark)
10070                (gnus-summary-mark-article-as-read gnus-killed-mark))
10071               ((> unmark 0)
10072                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10073               (t
10074                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10075         (setq articles (cdr articles))))
10076     ;; Hide killed subtrees.
10077     (and (null unmark)
10078          gnus-thread-hide-killed
10079          (gnus-summary-hide-thread))
10080     ;; If marked as read, go to next unread subject.
10081     (when (null unmark)
10082       ;; Go to next unread subject.
10083       (gnus-summary-next-subject 1 t)))
10084   (gnus-set-mode-line 'summary))
10085
10086 ;; Summary sorting commands
10087
10088 (defun gnus-summary-sort-by-number (&optional reverse)
10089   "Sort the summary buffer by article number.
10090 Argument REVERSE means reverse order."
10091   (interactive "P")
10092   (gnus-summary-sort 'number reverse))
10093
10094 (defun gnus-summary-sort-by-author (&optional reverse)
10095   "Sort the summary buffer by author name alphabetically.
10096 If `case-fold-search' is non-nil, case of letters is ignored.
10097 Argument REVERSE means reverse order."
10098   (interactive "P")
10099   (gnus-summary-sort 'author reverse))
10100
10101 (defun gnus-summary-sort-by-subject (&optional reverse)
10102   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10103 If `case-fold-search' is non-nil, case of letters is ignored.
10104 Argument REVERSE means reverse order."
10105   (interactive "P")
10106   (gnus-summary-sort 'subject reverse))
10107
10108 (defun gnus-summary-sort-by-date (&optional reverse)
10109   "Sort the summary buffer by date.
10110 Argument REVERSE means reverse order."
10111   (interactive "P")
10112   (gnus-summary-sort 'date reverse))
10113
10114 (defun gnus-summary-sort-by-score (&optional reverse)
10115   "Sort the summary buffer by score.
10116 Argument REVERSE means reverse order."
10117   (interactive "P")
10118   (gnus-summary-sort 'score reverse))
10119
10120 (defun gnus-summary-sort-by-lines (&optional reverse)
10121   "Sort the summary buffer by the number of lines.
10122 Argument REVERSE means reverse order."
10123   (interactive "P")
10124   (gnus-summary-sort 'lines reverse))
10125
10126 (defun gnus-summary-sort-by-chars (&optional reverse)
10127   "Sort the summary buffer by article length.
10128 Argument REVERSE means reverse order."
10129   (interactive "P")
10130   (gnus-summary-sort 'chars reverse))
10131
10132 (defun gnus-summary-sort-by-original (&optional reverse)
10133   "Sort the summary buffer using the default sorting method.
10134 Argument REVERSE means reverse order."
10135   (interactive "P")
10136   (let* ((buffer-read-only)
10137          (gnus-summary-prepare-hook nil))
10138     ;; We do the sorting by regenerating the threads.
10139     (gnus-summary-prepare)
10140     ;; Hide subthreads if needed.
10141     (gnus-summary-maybe-hide-threads)))
10142
10143 (defun gnus-summary-sort (predicate reverse)
10144   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10145   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10146          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10147          (gnus-thread-sort-functions
10148           (if (not reverse)
10149               thread
10150             `(lambda (t1 t2)
10151                (,thread t2 t1))))
10152          (gnus-sort-gathered-threads-function
10153           gnus-thread-sort-functions)
10154          (gnus-article-sort-functions
10155           (if (not reverse)
10156               article
10157             `(lambda (t1 t2)
10158                (,article t2 t1))))
10159          (buffer-read-only)
10160          (gnus-summary-prepare-hook nil))
10161     ;; We do the sorting by regenerating the threads.
10162     (gnus-summary-prepare)
10163     ;; Hide subthreads if needed.
10164     (gnus-summary-maybe-hide-threads)))
10165
10166 ;; Summary saving commands.
10167
10168 (defun gnus-summary-save-article (&optional n not-saved)
10169   "Save the current article using the default saver function.
10170 If N is a positive number, save the N next articles.
10171 If N is a negative number, save the N previous articles.
10172 If N is nil and any articles have been marked with the process mark,
10173 save those articles instead.
10174 The variable `gnus-default-article-saver' specifies the saver function."
10175   (interactive "P")
10176   (let* ((articles (gnus-summary-work-articles n))
10177          (save-buffer (save-excursion
10178                         (nnheader-set-temp-buffer " *Gnus Save*")))
10179          (num (length articles))
10180          header file)
10181     (dolist (article articles)
10182       (setq header (gnus-summary-article-header article))
10183       (if (not (vectorp header))
10184           ;; This is a pseudo-article.
10185           (if (assq 'name header)
10186               (gnus-copy-file (cdr (assq 'name header)))
10187             (gnus-message 1 "Article %d is unsaveable" article))
10188         ;; This is a real article.
10189         (save-window-excursion
10190           (let ((gnus-display-mime-function nil)
10191                 (gnus-article-prepare-hook nil))
10192             (gnus-summary-select-article t nil nil article)))
10193         (save-excursion
10194           (set-buffer save-buffer)
10195           (erase-buffer)
10196           (insert-buffer-substring gnus-original-article-buffer))
10197         (setq file (gnus-article-save save-buffer file num))
10198         (gnus-summary-remove-process-mark article)
10199         (unless not-saved
10200           (gnus-summary-set-saved-mark article))))
10201     (gnus-kill-buffer save-buffer)
10202     (gnus-summary-position-point)
10203     (gnus-set-mode-line 'summary)
10204     n))
10205
10206 (defun gnus-summary-pipe-output (&optional arg)
10207   "Pipe the current article to a subprocess.
10208 If N is a positive number, pipe the N next articles.
10209 If N is a negative number, pipe the N previous articles.
10210 If N is nil and any articles have been marked with the process mark,
10211 pipe those articles instead."
10212   (interactive "P")
10213   (require 'gnus-art)
10214   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10215     (gnus-summary-save-article arg t))
10216   (let ((buffer (get-buffer "*Shell Command Output*")))
10217     (if (and buffer
10218              (with-current-buffer buffer (> (point-max) (point-min))))
10219         (gnus-configure-windows 'pipe))))
10220
10221 (defun gnus-summary-save-article-mail (&optional arg)
10222   "Append the current article to an mail file.
10223 If N is a positive number, save the N next articles.
10224 If N is a negative number, save the N previous articles.
10225 If N is nil and any articles have been marked with the process mark,
10226 save those articles instead."
10227   (interactive "P")
10228   (require 'gnus-art)
10229   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10230     (gnus-summary-save-article arg)))
10231
10232 (defun gnus-summary-save-article-rmail (&optional arg)
10233   "Append the current article to an rmail file.
10234 If N is a positive number, save the N next articles.
10235 If N is a negative number, save the N previous articles.
10236 If N is nil and any articles have been marked with the process mark,
10237 save those articles instead."
10238   (interactive "P")
10239   (require 'gnus-art)
10240   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10241     (gnus-summary-save-article arg)))
10242
10243 (defun gnus-summary-save-article-file (&optional arg)
10244   "Append the current article to a file.
10245 If N is a positive number, save the N next articles.
10246 If N is a negative number, save the N previous articles.
10247 If N is nil and any articles have been marked with the process mark,
10248 save those articles instead."
10249   (interactive "P")
10250   (require 'gnus-art)
10251   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10252     (gnus-summary-save-article arg)))
10253
10254 (defun gnus-summary-write-article-file (&optional arg)
10255   "Write the current article to a file, deleting the previous file.
10256 If N is a positive number, save the N next articles.
10257 If N is a negative number, save the N previous articles.
10258 If N is nil and any articles have been marked with the process mark,
10259 save those articles instead."
10260   (interactive "P")
10261   (require 'gnus-art)
10262   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10263     (gnus-summary-save-article arg)))
10264
10265 (defun gnus-summary-save-article-body-file (&optional arg)
10266   "Append the current article body to a file.
10267 If N is a positive number, save the N next articles.
10268 If N is a negative number, save the N previous articles.
10269 If N is nil and any articles have been marked with the process mark,
10270 save those articles instead."
10271   (interactive "P")
10272   (require 'gnus-art)
10273   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10274     (gnus-summary-save-article arg)))
10275
10276 (defun gnus-summary-muttprint (&optional arg)
10277   "Print the current article using Muttprint.
10278 If N is a positive number, save the N next articles.
10279 If N is a negative number, save the N previous articles.
10280 If N is nil and any articles have been marked with the process mark,
10281 save those articles instead."
10282   (interactive "P")
10283   (require 'gnus-art)
10284   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10285     (gnus-summary-save-article arg t)))
10286
10287 (defun gnus-summary-pipe-message (program)
10288   "Pipe the current article through PROGRAM."
10289   (interactive "sProgram: ")
10290   (gnus-summary-select-article)
10291   (let ((mail-header-separator ""))
10292     (gnus-eval-in-buffer-window gnus-article-buffer
10293       (save-restriction
10294         (widen)
10295         (let ((start (window-start))
10296               buffer-read-only)
10297           (message-pipe-buffer-body program)
10298           (set-window-start (get-buffer-window (current-buffer)) start))))))
10299
10300 (defun gnus-get-split-value (methods)
10301   "Return a value based on the split METHODS."
10302   (let (split-name method result match)
10303     (when methods
10304       (save-excursion
10305         (set-buffer gnus-original-article-buffer)
10306         (save-restriction
10307           (nnheader-narrow-to-headers)
10308           (while (and methods (not split-name))
10309             (goto-char (point-min))
10310             (setq method (pop methods))
10311             (setq match (car method))
10312             (when (cond
10313                    ((stringp match)
10314                     ;; Regular expression.
10315                     (ignore-errors
10316                       (re-search-forward match nil t)))
10317                    ((gnus-functionp match)
10318                     ;; Function.
10319                     (save-restriction
10320                       (widen)
10321                       (setq result (funcall match gnus-newsgroup-name))))
10322                    ((consp match)
10323                     ;; Form.
10324                     (save-restriction
10325                       (widen)
10326                       (setq result (eval match)))))
10327               (setq split-name (cdr method))
10328               (cond ((stringp result)
10329                      (push (expand-file-name
10330                             result gnus-article-save-directory)
10331                            split-name))
10332                     ((consp result)
10333                      (setq split-name (append result split-name)))))))))
10334     (nreverse split-name)))
10335
10336 (defun gnus-valid-move-group-p (group)
10337   (and (boundp group)
10338        (symbol-name group)
10339        (symbol-value group)
10340        (gnus-get-function (gnus-find-method-for-group
10341                            (symbol-name group)) 'request-accept-article t)))
10342
10343 (defun gnus-read-move-group-name (prompt default articles prefix)
10344   "Read a group name."
10345   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10346          (minibuffer-confirm-incomplete nil) ; XEmacs
10347          (prom
10348           (format "%s %s to:"
10349                   prompt
10350                   (if (> (length articles) 1)
10351                       (format "these %d articles" (length articles))
10352                     "this article")))
10353          (to-newsgroup
10354           (cond
10355            ((null split-name)
10356             (gnus-completing-read-with-default
10357              default prom
10358              gnus-active-hashtb
10359              'gnus-valid-move-group-p
10360              nil prefix
10361              'gnus-group-history))
10362            ((= 1 (length split-name))
10363             (gnus-completing-read-with-default
10364              (car split-name) prom
10365              gnus-active-hashtb
10366              'gnus-valid-move-group-p
10367              nil nil
10368              'gnus-group-history))
10369            (t
10370             (gnus-completing-read-with-default
10371              nil prom
10372              (mapcar (lambda (el) (list el))
10373                      (nreverse split-name))
10374              nil nil nil
10375              'gnus-group-history))))
10376          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10377     (when to-newsgroup
10378       (if (or (string= to-newsgroup "")
10379               (string= to-newsgroup prefix))
10380           (setq to-newsgroup default))
10381       (unless to-newsgroup
10382         (error "No group name entered"))
10383       (or (gnus-active to-newsgroup)
10384           (gnus-activate-group to-newsgroup nil nil to-method)
10385           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10386                                      to-newsgroup))
10387               (or (and (gnus-request-create-group to-newsgroup to-method)
10388                        (gnus-activate-group
10389                         to-newsgroup nil nil to-method)
10390                        (gnus-subscribe-group to-newsgroup))
10391                   (error "Couldn't create group %s" to-newsgroup)))
10392           (error "No such group: %s" to-newsgroup)))
10393     to-newsgroup))
10394
10395 (defun gnus-summary-save-parts (type dir n &optional reverse)
10396   "Save parts matching TYPE to DIR.
10397 If REVERSE, save parts that do not match TYPE."
10398   (interactive
10399    (list (read-string "Save parts of type: "
10400                       (or (car gnus-summary-save-parts-type-history)
10401                           gnus-summary-save-parts-default-mime)
10402                       'gnus-summary-save-parts-type-history)
10403          (setq gnus-summary-save-parts-last-directory
10404                (read-file-name "Save to directory: "
10405                                gnus-summary-save-parts-last-directory
10406                                nil t))
10407          current-prefix-arg))
10408   (gnus-summary-iterate n
10409     (let ((gnus-display-mime-function nil)
10410           (gnus-inhibit-treatment t))
10411       (gnus-summary-select-article))
10412     (save-excursion
10413       (set-buffer gnus-article-buffer)
10414       (let ((handles (or gnus-article-mime-handles
10415                          (mm-dissect-buffer nil gnus-article-loose-mime)
10416                          (mm-uu-dissect))))
10417         (when handles
10418           (gnus-summary-save-parts-1 type dir handles reverse)
10419           (unless gnus-article-mime-handles ;; Don't destroy this case.
10420             (mm-destroy-parts handles)))))))
10421
10422 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10423   (if (stringp (car handle))
10424       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10425               (cdr handle))
10426     (when (if reverse
10427               (not (string-match type (mm-handle-media-type handle)))
10428             (string-match type (mm-handle-media-type handle)))
10429       (let ((file (expand-file-name
10430                    (file-name-nondirectory
10431                     (or
10432                      (mail-content-type-get
10433                       (mm-handle-disposition handle) 'filename)
10434                      (concat gnus-newsgroup-name
10435                              "." (number-to-string
10436                                   (cdr gnus-article-current)))))
10437                    dir)))
10438         (unless (file-exists-p file)
10439           (mm-save-part-to-file handle file))))))
10440
10441 ;; Summary extract commands
10442
10443 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10444   (let ((buffer-read-only nil)
10445         (article (gnus-summary-article-number))
10446         after-article b e)
10447     (unless (gnus-summary-goto-subject article)
10448       (error "No such article: %d" article))
10449     (gnus-summary-position-point)
10450     ;; If all commands are to be bunched up on one line, we collect
10451     ;; them here.
10452     (unless gnus-view-pseudos-separately
10453       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10454             files action)
10455         (while ps
10456           (setq action (cdr (assq 'action (car ps))))
10457           (setq files (list (cdr (assq 'name (car ps)))))
10458           (while (and ps (cdr ps)
10459                       (string= (or action "1")
10460                                (or (cdr (assq 'action (cadr ps))) "2")))
10461             (push (cdr (assq 'name (cadr ps))) files)
10462             (setcdr ps (cddr ps)))
10463           (when files
10464             (when (not (string-match "%s" action))
10465               (push " " files))
10466             (push " " files)
10467             (when (assq 'execute (car ps))
10468               (setcdr (assq 'execute (car ps))
10469                       (funcall (if (string-match "%s" action)
10470                                    'format 'concat)
10471                                action
10472                                (mapconcat
10473                                 (lambda (f)
10474                                   (if (equal f " ")
10475                                       f
10476                                     (mm-quote-arg f)))
10477                                 files " ")))))
10478           (setq ps (cdr ps)))))
10479     (if (and gnus-view-pseudos (not not-view))
10480         (while pslist
10481           (when (assq 'execute (car pslist))
10482             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10483                                   (eq gnus-view-pseudos 'not-confirm)))
10484           (setq pslist (cdr pslist)))
10485       (save-excursion
10486         (while pslist
10487           (setq after-article (or (cdr (assq 'article (car pslist)))
10488                                   (gnus-summary-article-number)))
10489           (gnus-summary-goto-subject after-article)
10490           (forward-line 1)
10491           (setq b (point))
10492           (insert "    " (file-name-nondirectory
10493                           (cdr (assq 'name (car pslist))))
10494                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10495           (setq e (point))
10496           (forward-line -1)             ; back to `b'
10497           (gnus-add-text-properties
10498            b (1- e) (list 'gnus-number gnus-reffed-article-number
10499                           gnus-mouse-face-prop gnus-mouse-face))
10500           (gnus-data-enter
10501            after-article gnus-reffed-article-number
10502            gnus-unread-mark b (car pslist) 0 (- e b))
10503           (setq gnus-newsgroup-unreads
10504                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10505                                          gnus-reffed-article-number))
10506           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10507           (setq pslist (cdr pslist)))))))
10508
10509 (defun gnus-pseudos< (p1 p2)
10510   (let ((c1 (cdr (assq 'action p1)))
10511         (c2 (cdr (assq 'action p2))))
10512     (and c1 c2 (string< c1 c2))))
10513
10514 (defun gnus-request-pseudo-article (props)
10515   (cond ((assq 'execute props)
10516          (gnus-execute-command (cdr (assq 'execute props)))))
10517   (let ((gnus-current-article (gnus-summary-article-number)))
10518     (gnus-run-hooks 'gnus-mark-article-hook)))
10519
10520 (defun gnus-execute-command (command &optional automatic)
10521   (save-excursion
10522     (gnus-article-setup-buffer)
10523     (set-buffer gnus-article-buffer)
10524     (setq buffer-read-only nil)
10525     (let ((command (if automatic command
10526                      (read-string "Command: " (cons command 0)))))
10527       (erase-buffer)
10528       (insert "$ " command "\n\n")
10529       (if gnus-view-pseudo-asynchronously
10530           (start-process "gnus-execute" (current-buffer) shell-file-name
10531                          shell-command-switch command)
10532         (call-process shell-file-name nil t nil
10533                       shell-command-switch command)))))
10534
10535 ;; Summary kill commands.
10536
10537 (defun gnus-summary-edit-global-kill (article)
10538   "Edit the \"global\" kill file."
10539   (interactive (list (gnus-summary-article-number)))
10540   (gnus-group-edit-global-kill article))
10541
10542 (defun gnus-summary-edit-local-kill ()
10543   "Edit a local kill file applied to the current newsgroup."
10544   (interactive)
10545   (setq gnus-current-headers (gnus-summary-article-header))
10546   (gnus-group-edit-local-kill
10547    (gnus-summary-article-number) gnus-newsgroup-name))
10548
10549 ;;; Header reading.
10550
10551 (defun gnus-read-header (id &optional header)
10552   "Read the headers of article ID and enter them into the Gnus system."
10553   (let ((group gnus-newsgroup-name)
10554         (gnus-override-method
10555          (or
10556           gnus-override-method
10557           (and (gnus-news-group-p gnus-newsgroup-name)
10558                (car (gnus-refer-article-methods)))))
10559         where)
10560     ;; First we check to see whether the header in question is already
10561     ;; fetched.
10562     (if (stringp id)
10563         ;; This is a Message-ID.
10564         (setq header (or header (gnus-id-to-header id)))
10565       ;; This is an article number.
10566       (setq header (or header (gnus-summary-article-header id))))
10567     (if (and header
10568              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10569         ;; We have found the header.
10570         header
10571       ;; If this is a sparse article, we have to nix out its
10572       ;; previous entry in the thread hashtb.
10573       (when (and header
10574                  (gnus-summary-article-sparse-p (mail-header-number header)))
10575         (let* ((parent (gnus-parent-id (mail-header-references header)))
10576                (thread (and parent (gnus-id-to-thread parent))))
10577           (when thread
10578             (delq (assq header thread) thread))))
10579       ;; We have to really fetch the header to this article.
10580       (save-excursion
10581         (set-buffer nntp-server-buffer)
10582         (when (setq where (gnus-request-head id group))
10583           (nnheader-fold-continuation-lines)
10584           (goto-char (point-max))
10585           (insert ".\n")
10586           (goto-char (point-min))
10587           (insert "211 ")
10588           (princ (cond
10589                   ((numberp id) id)
10590                   ((cdr where) (cdr where))
10591                   (header (mail-header-number header))
10592                   (t gnus-reffed-article-number))
10593                  (current-buffer))
10594           (insert " Article retrieved.\n"))
10595         (if (or (not where)
10596                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10597             ()                          ; Malformed head.
10598           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10599             (when (and (stringp id)
10600                        (not (string= (gnus-group-real-name group)
10601                                      (car where))))
10602               ;; If we fetched by Message-ID and the article came
10603               ;; from a different group, we fudge some bogus article
10604               ;; numbers for this article.
10605               (mail-header-set-number header gnus-reffed-article-number))
10606             (save-excursion
10607               (set-buffer gnus-summary-buffer)
10608               (decf gnus-reffed-article-number)
10609               (gnus-remove-header (mail-header-number header))
10610               (push header gnus-newsgroup-headers)
10611               (setq gnus-current-headers header)
10612               (push (mail-header-number header) gnus-newsgroup-limit)))
10613           header)))))
10614
10615 (defun gnus-remove-header (number)
10616   "Remove header NUMBER from `gnus-newsgroup-headers'."
10617   (if (and gnus-newsgroup-headers
10618            (= number (mail-header-number (car gnus-newsgroup-headers))))
10619       (pop gnus-newsgroup-headers)
10620     (let ((headers gnus-newsgroup-headers))
10621       (while (and (cdr headers)
10622                   (not (= number (mail-header-number (cadr headers)))))
10623         (pop headers))
10624       (when (cdr headers)
10625         (setcdr headers (cddr headers))))))
10626
10627 ;;;
10628 ;;; summary highlights
10629 ;;;
10630
10631 (defun gnus-highlight-selected-summary ()
10632   "Highlight selected article in summary buffer."
10633   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10634   (when gnus-summary-selected-face
10635     (save-excursion
10636       (let* ((beg (progn (beginning-of-line) (point)))
10637              (end (progn (end-of-line) (point)))
10638              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10639              (from (if (get-text-property beg gnus-mouse-face-prop)
10640                        beg
10641                      (or (next-single-property-change
10642                           beg gnus-mouse-face-prop nil end)
10643                          beg)))
10644              (to
10645               (if (= from end)
10646                   (- from 2)
10647                 (or (next-single-property-change
10648                      from gnus-mouse-face-prop nil end)
10649                     end))))
10650         ;; If no mouse-face prop on line we will have to = from = end,
10651         ;; so we highlight the entire line instead.
10652         (when (= (+ to 2) from)
10653           (setq from beg)
10654           (setq to end))
10655         (if gnus-newsgroup-selected-overlay
10656             ;; Move old overlay.
10657             (gnus-move-overlay
10658              gnus-newsgroup-selected-overlay from to (current-buffer))
10659           ;; Create new overlay.
10660           (gnus-overlay-put
10661            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10662            'face gnus-summary-selected-face))))))
10663
10664 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10665 (defun gnus-summary-highlight-line ()
10666   "Highlight current line according to `gnus-summary-highlight'."
10667   (let* ((list gnus-summary-highlight)
10668          (beg (gnus-point-at-bol))
10669          (article (gnus-summary-article-number))
10670          (score (or (cdr (assq (or article gnus-current-article)
10671                                gnus-newsgroup-scored))
10672                     gnus-summary-default-score 0))
10673          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10674          (inhibit-read-only t)
10675          (default gnus-summary-default-score)
10676          (default-high gnus-summary-default-high-score)
10677          (default-low gnus-summary-default-low-score))
10678     ;; Eval the cars of the lists until we find a match.
10679     (while (and list
10680                 (not (eval (caar list))))
10681       (setq list (cdr list)))
10682     (let ((face (cdar list)))
10683       (unless (eq face (get-text-property beg 'face))
10684         (gnus-put-text-property-excluding-characters-with-faces
10685          beg (gnus-point-at-eol) 'face
10686          (setq face (if (boundp face) (symbol-value face) face)))
10687         (when gnus-summary-highlight-line-function
10688           (funcall gnus-summary-highlight-line-function article face))))))
10689
10690 (defun gnus-update-read-articles (group unread &optional compute)
10691   "Update the list of read articles in GROUP.
10692 UNREAD is a sorted list."
10693   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10694          (entry (gnus-gethash group gnus-newsrc-hashtb))
10695          (info (nth 2 entry))
10696          (prev 1)
10697          read)
10698     (if (or (not info) (not active))
10699         ;; There is no info on this group if it was, in fact,
10700         ;; killed.  Gnus stores no information on killed groups, so
10701         ;; there's nothing to be done.
10702         ;; One could store the information somewhere temporarily,
10703         ;; perhaps...  Hmmm...
10704         ()
10705       ;; Remove any negative articles numbers.
10706       (while (and unread (< (car unread) 0))
10707         (setq unread (cdr unread)))
10708       ;; Remove any expired article numbers
10709       (while (and unread (< (car unread) (car active)))
10710         (setq unread (cdr unread)))
10711       ;; Compute the ranges of read articles by looking at the list of
10712       ;; unread articles.
10713       (while unread
10714         (when (/= (car unread) prev)
10715           (push (if (= prev (1- (car unread))) prev
10716                   (cons prev (1- (car unread))))
10717                 read))
10718         (setq prev (1+ (car unread)))
10719         (setq unread (cdr unread)))
10720       (when (<= prev (cdr active))
10721         (push (cons prev (cdr active)) read))
10722       (setq read (if (> (length read) 1) (nreverse read) read))
10723       (if compute
10724           read
10725         (save-excursion
10726           (let (setmarkundo)
10727             ;; Propagate the read marks to the backend.
10728             (when (gnus-check-backend-function 'request-set-mark group)
10729               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10730                     (add (gnus-remove-from-range read (gnus-info-read info))))
10731                 (when (or add del)
10732                   (unless (gnus-check-group group)
10733                     (error "Can't open server for %s" group))
10734                   (gnus-request-set-mark
10735                    group (delq nil (list (if add (list add 'add '(read)))
10736                                          (if del (list del 'del '(read))))))
10737                   (setq setmarkundo
10738                         `(gnus-request-set-mark
10739                           ,group
10740                           ',(delq nil (list
10741                                        (if del (list del 'add '(read)))
10742                                        (if add (list add 'del '(read))))))))))
10743             (set-buffer gnus-group-buffer)
10744             (gnus-undo-register
10745               `(progn
10746                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10747                  (gnus-info-set-read ',info ',(gnus-info-read info))
10748                  (gnus-get-unread-articles-in-group ',info
10749                                                     (gnus-active ,group))
10750                  (gnus-group-update-group ,group t)
10751                  ,setmarkundo))))
10752         ;; Enter this list into the group info.
10753         (gnus-info-set-read info read)
10754         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10755         (gnus-get-unread-articles-in-group info (gnus-active group))
10756         t))))
10757
10758 (defun gnus-offer-save-summaries ()
10759   "Offer to save all active summary buffers."
10760   (let (buffers)
10761     ;; Go through all buffers and find all summaries.
10762     (dolist (buffer (buffer-list))
10763       (when (and (setq buffer (buffer-name buffer))
10764                  (string-match "Summary" buffer)
10765                  (save-excursion
10766                    (set-buffer buffer)
10767                    ;; We check that this is, indeed, a summary buffer.
10768                    (and (eq major-mode 'gnus-summary-mode)
10769                         ;; Also make sure this isn't bogus.
10770                         gnus-newsgroup-prepared
10771                         ;; Also make sure that this isn't a
10772                         ;; dead summary buffer.
10773                         (not gnus-dead-summary-mode))))
10774         (push buffer buffers)))
10775     ;; Go through all these summary buffers and offer to save them.
10776     (when buffers
10777       (save-excursion
10778         (map-y-or-n-p
10779          "Update summary buffer %s? "
10780          (lambda (buf)
10781            (switch-to-buffer buf)
10782            (gnus-summary-exit))
10783          buffers)))))
10784
10785 (defun gnus-summary-setup-default-charset ()
10786   "Setup newsgroup default charset."
10787   (if (equal gnus-newsgroup-name "nndraft:drafts")
10788       (setq gnus-newsgroup-charset nil)
10789     (let* ((ignored-charsets
10790             (or gnus-newsgroup-ephemeral-ignored-charsets
10791                 (append
10792                  (and gnus-newsgroup-name
10793                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10794                  gnus-newsgroup-ignored-charsets))))
10795       (setq gnus-newsgroup-charset
10796             (or gnus-newsgroup-ephemeral-charset
10797                 (and gnus-newsgroup-name
10798                      (gnus-parameter-charset gnus-newsgroup-name))
10799                 gnus-default-charset))
10800       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10801            ignored-charsets))))
10802
10803 ;;;
10804 ;;; Mime Commands
10805 ;;;
10806
10807 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10808   "Display the current article buffer fully MIME-buttonized.
10809 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10810 treated as multipart/mixed."
10811   (interactive "P")
10812   (require 'gnus-art)
10813   (let ((gnus-unbuttonized-mime-types nil)
10814         (gnus-mime-display-multipart-as-mixed show-all-parts))
10815     (gnus-summary-show-article)))
10816
10817 (defun gnus-summary-repair-multipart (article)
10818   "Add a Content-Type header to a multipart article without one."
10819   (interactive (list (gnus-summary-article-number)))
10820   (gnus-with-article article
10821     (message-narrow-to-head)
10822     (message-remove-header "Mime-Version")
10823     (goto-char (point-max))
10824     (insert "Mime-Version: 1.0\n")
10825     (widen)
10826     (when (search-forward "\n--" nil t)
10827       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10828         (message-narrow-to-head)
10829         (message-remove-header "Content-Type")
10830         (goto-char (point-max))
10831         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10832                         separator))
10833         (widen))))
10834   (let (gnus-mark-article-hook)
10835     (gnus-summary-select-article t t nil article)))
10836
10837 (defun gnus-summary-toggle-display-buttonized ()
10838   "Toggle the buttonizing of the article buffer."
10839   (interactive)
10840   (require 'gnus-art)
10841   (if (setq gnus-inhibit-mime-unbuttonizing
10842             (not gnus-inhibit-mime-unbuttonizing))
10843       (let ((gnus-unbuttonized-mime-types nil))
10844         (gnus-summary-show-article))
10845     (gnus-summary-show-article)))
10846
10847 ;;;
10848 ;;; Generic summary marking commands
10849 ;;;
10850
10851 (defvar gnus-summary-marking-alist
10852   '((read gnus-del-mark "d")
10853     (unread gnus-unread-mark "u")
10854     (ticked gnus-ticked-mark "!")
10855     (dormant gnus-dormant-mark "?")
10856     (expirable gnus-expirable-mark "e"))
10857   "An alist of names/marks/keystrokes.")
10858
10859 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10860 (defvar gnus-summary-mark-map)
10861
10862 (defun gnus-summary-make-all-marking-commands ()
10863   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10864   (dolist (elem gnus-summary-marking-alist)
10865     (apply 'gnus-summary-make-marking-command elem)))
10866
10867 (defun gnus-summary-make-marking-command (name mark keystroke)
10868   (let ((map (make-sparse-keymap)))
10869     (define-key gnus-summary-generic-mark-map keystroke map)
10870     (dolist (lway `((next "next" next nil "n")
10871                     (next-unread "next unread" next t "N")
10872                     (prev "previous" prev nil "p")
10873                     (prev-unread "previous unread" prev t "P")
10874                     (nomove "" nil nil ,keystroke)))
10875       (let ((func (gnus-summary-make-marking-command-1
10876                    mark (car lway) lway name)))
10877         (setq func (eval func))
10878         (define-key map (nth 4 lway) func)))))
10879
10880 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10881   `(defun ,(intern
10882             (format "gnus-summary-put-mark-as-%s%s"
10883                     name (if (eq way 'nomove)
10884                              ""
10885                            (concat "-" (symbol-name way)))))
10886      (n)
10887      ,(format
10888        "Mark the current article as %s%s.
10889 If N, the prefix, then repeat N times.
10890 If N is negative, move in reverse order.
10891 The difference between N and the actual number of articles marked is
10892 returned."
10893        name (cadr lway))
10894      (interactive "p")
10895      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10896
10897 (defun gnus-summary-generic-mark (n mark move unread)
10898   "Mark N articles with MARK."
10899   (unless (eq major-mode 'gnus-summary-mode)
10900     (error "This command can only be used in the summary buffer"))
10901   (gnus-summary-show-thread)
10902   (let ((nummove
10903          (cond
10904           ((eq move 'next) 1)
10905           ((eq move 'prev) -1)
10906           (t 0))))
10907     (if (zerop nummove)
10908         (setq n 1)
10909       (when (< n 0)
10910         (setq n (abs n)
10911               nummove (* -1 nummove))))
10912     (while (and (> n 0)
10913                 (gnus-summary-mark-article nil mark)
10914                 (zerop (gnus-summary-next-subject nummove unread t)))
10915       (setq n (1- n)))
10916     (when (/= 0 n)
10917       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10918     (gnus-summary-recenter)
10919     (gnus-summary-position-point)
10920     (gnus-set-mode-line 'summary)
10921     n))
10922
10923 (defun gnus-summary-insert-articles (articles)
10924   (when (setq articles
10925               (gnus-sorted-difference articles
10926                                       (mapcar (lambda (h)
10927                                                 (mail-header-number h))
10928                                               gnus-newsgroup-headers)))
10929     (setq gnus-newsgroup-headers
10930           (merge 'list
10931                  gnus-newsgroup-headers
10932                  (gnus-fetch-headers articles)
10933                  'gnus-article-sort-by-number))
10934     ;; Suppress duplicates?
10935     (when gnus-suppress-duplicates
10936       (gnus-dup-suppress-articles))
10937
10938     ;; We might want to build some more threads first.
10939     (when (and gnus-fetch-old-headers
10940                (eq gnus-headers-retrieved-by 'nov))
10941       (if (eq gnus-fetch-old-headers 'invisible)
10942           (gnus-build-all-threads)
10943         (gnus-build-old-threads)))
10944     ;; Let the Gnus agent mark articles as read.
10945     (when gnus-agent
10946       (gnus-agent-get-undownloaded-list))
10947     ;; Remove list identifiers from subject
10948     (when gnus-list-identifiers
10949       (gnus-summary-remove-list-identifiers))
10950     ;; First and last article in this newsgroup.
10951     (when gnus-newsgroup-headers
10952       (setq gnus-newsgroup-begin
10953             (mail-header-number (car gnus-newsgroup-headers))
10954             gnus-newsgroup-end
10955             (mail-header-number
10956              (gnus-last-element gnus-newsgroup-headers))))
10957     (when gnus-use-scoring
10958       (gnus-possibly-score-headers))))
10959
10960 (defun gnus-summary-insert-old-articles (&optional all)
10961   "Insert all old articles in this group.
10962 If ALL is non-nil, already read articles become readable.
10963 If ALL is a number, fetch this number of articles."
10964   (interactive "P")
10965   (prog1
10966       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
10967             older len)
10968         (setq older
10969               (gnus-sorted-difference
10970                (gnus-uncompress-range (list gnus-newsgroup-active))
10971                old))
10972         (setq len (length older))
10973         (cond
10974          ((null older) nil)
10975          ((numberp all)
10976           (if (< all len)
10977               (setq older (last older all))))
10978          (all nil)
10979          (t
10980           (if (and (numberp gnus-large-newsgroup)
10981                    (> len gnus-large-newsgroup))
10982               (let* ((cursor-in-echo-area nil)
10983                      (initial (gnus-parameter-large-newsgroup-initial 
10984                                gnus-newsgroup-name))
10985                      (input
10986                       (read-string
10987                        (format
10988                         "How many articles from %s (%s %d): "
10989                         (gnus-limit-string
10990                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
10991                         (if initial "max" "default")
10992                         len)
10993                        (if initial
10994                            (cons (number-to-string initial)
10995                                  0)))))
10996                 (unless (string-match "^[ \t]*$" input)
10997                   (setq all (string-to-number input))
10998                   (if (< all len)
10999                       (setq older (last older all))))))))
11000         (if (not older)
11001             (message "No old news.")
11002           (gnus-summary-insert-articles older)
11003           (gnus-summary-limit (gnus-sorted-nunion old older))))
11004     (gnus-summary-position-point)))
11005
11006 (defun gnus-summary-insert-new-articles ()
11007   "Insert all new articles in this group."
11008   (interactive)
11009   (prog1
11010       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11011             (old-active gnus-newsgroup-active)
11012             (nnmail-fetched-sources (list t))
11013             i new)
11014         (setq gnus-newsgroup-active
11015               (gnus-activate-group gnus-newsgroup-name 'scan))
11016         (setq i (1+ (cdr old-active)))
11017         (while (<= i (cdr gnus-newsgroup-active))
11018           (push i new)
11019           (incf i))
11020         (if (not new)
11021             (message "No gnus is bad news.")
11022           (setq new (nreverse new))
11023           (gnus-summary-insert-articles new)
11024           (setq gnus-newsgroup-unreads
11025                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11026           (gnus-summary-limit (gnus-sorted-nunion old new))))
11027     (gnus-summary-position-point)))
11028
11029 (gnus-summary-make-all-marking-commands)
11030
11031 (gnus-ems-redefine)
11032
11033 (provide 'gnus-sum)
11034
11035 (run-hooks 'gnus-sum-load-hook)
11036
11037 ;;; gnus-sum.el ends here