aedd26d356ae13811e3273ea1cfc72eafdf7ad4b
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject', 
260 `gnus-summary-first-unread-article' and 
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? 
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? 
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-treading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-default-charset 'iso-8859-1
801   "Default charset assumed to be used when viewing non-ASCII characters.
802 This variable is used only in non-Mule Emacsen.")
803
804 (defcustom gnus-newsgroup-default-charset-alist 
805   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" . cn-big5)
806     ("^cn\\>\\|\\<chinese\\>" . cn-gb-2312)
807     ("^fj\\>\\|^japan\\>" . iso-2022-jp-2)
808     ("^relcom\\>" . koi8-r))
809   "Alist of Regexps (to match group names) and default charsets to be applied."
810   :type '(repeat (cons (regexp :tag "Group")
811                        (symbol :tag "Charset")))
812   :group 'gnus)
813
814 (defcustom gnus-newsgroup-iso-8859-1-forced-regexp 
815   "^tw\\>\\|^hk\\>\\|^cn\\>\\|\\<chinese\\>"
816   "Regexp of newsgroup in which ISO-8859-1 is forced to other charset."
817   :type 'regexp
818   :group 'gnus)
819
820 ;;; Internal variables
821
822 (defvar gnus-article-mime-handles nil)
823 (defvar gnus-article-decoded-p nil)
824 (defvar gnus-scores-exclude-files nil)
825 (defvar gnus-page-broken nil)
826 (defvar gnus-inhibit-mime-unbuttonizing nil)
827
828 (defvar gnus-original-article nil)
829 (defvar gnus-article-internal-prepare-hook nil)
830 (defvar gnus-newsgroup-process-stack nil)
831
832 (defvar gnus-thread-indent-array nil)
833 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
834
835 ;; Avoid highlighting in kill files.
836 (defvar gnus-summary-inhibit-highlight nil)
837 (defvar gnus-newsgroup-selected-overlay nil)
838 (defvar gnus-inhibit-limiting nil)
839 (defvar gnus-newsgroup-adaptive-score-file nil)
840 (defvar gnus-current-score-file nil)
841 (defvar gnus-current-move-group nil)
842 (defvar gnus-current-copy-group nil)
843 (defvar gnus-current-crosspost-group nil)
844
845 (defvar gnus-newsgroup-dependencies nil)
846 (defvar gnus-newsgroup-adaptive nil)
847 (defvar gnus-summary-display-article-function nil)
848 (defvar gnus-summary-highlight-line-function nil
849   "Function called after highlighting a summary line.")
850
851 (defvar gnus-summary-line-format-alist
852   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
853     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
854     (?s gnus-tmp-subject-or-nil ?s)
855     (?n gnus-tmp-name ?s)
856     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
857         ?s)
858     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
859             gnus-tmp-from) ?s)
860     (?F gnus-tmp-from ?s)
861     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
862     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
863     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
864     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
865     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
866     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
867     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
868     (?L gnus-tmp-lines ?d)
869     (?I gnus-tmp-indentation ?s)
870     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
871     (?R gnus-tmp-replied ?c)
872     (?\[ gnus-tmp-opening-bracket ?c)
873     (?\] gnus-tmp-closing-bracket ?c)
874     (?\> (make-string gnus-tmp-level ? ) ?s)
875     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
876     (?i gnus-tmp-score ?d)
877     (?z gnus-tmp-score-char ?c)
878     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
879     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
880     (?U gnus-tmp-unread ?c)
881     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
882     (?t (gnus-summary-number-of-articles-in-thread
883          (and (boundp 'thread) (car thread)) gnus-tmp-level)
884         ?d)
885     (?e (gnus-summary-number-of-articles-in-thread
886          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
887         ?c)
888     (?u gnus-tmp-user-defined ?s)
889     (?P (gnus-pick-line-number) ?d))
890   "An alist of format specifications that can appear in summary lines,
891 and what variables they correspond with, along with the type of the
892 variable (string, integer, character, etc).")
893
894 (defvar gnus-summary-dummy-line-format-alist
895   `((?S gnus-tmp-subject ?s)
896     (?N gnus-tmp-number ?d)
897     (?u gnus-tmp-user-defined ?s)))
898
899 (defvar gnus-summary-mode-line-format-alist
900   `((?G gnus-tmp-group-name ?s)
901     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
902     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
903     (?A gnus-tmp-article-number ?d)
904     (?Z gnus-tmp-unread-and-unselected ?s)
905     (?V gnus-version ?s)
906     (?U gnus-tmp-unread-and-unticked ?d)
907     (?S gnus-tmp-subject ?s)
908     (?e gnus-tmp-unselected ?d)
909     (?u gnus-tmp-user-defined ?s)
910     (?d (length gnus-newsgroup-dormant) ?d)
911     (?t (length gnus-newsgroup-marked) ?d)
912     (?r (length gnus-newsgroup-reads) ?d)
913     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
914     (?E gnus-newsgroup-expunged-tally ?d)
915     (?s (gnus-current-score-file-nondirectory) ?s)))
916
917 (defvar gnus-last-search-regexp nil
918   "Default regexp for article search command.")
919
920 (defvar gnus-last-shell-command nil
921   "Default shell command on article.")
922
923 (defvar gnus-newsgroup-begin nil)
924 (defvar gnus-newsgroup-end nil)
925 (defvar gnus-newsgroup-last-rmail nil)
926 (defvar gnus-newsgroup-last-mail nil)
927 (defvar gnus-newsgroup-last-folder nil)
928 (defvar gnus-newsgroup-last-file nil)
929 (defvar gnus-newsgroup-auto-expire nil)
930 (defvar gnus-newsgroup-active nil)
931
932 (defvar gnus-newsgroup-data nil)
933 (defvar gnus-newsgroup-data-reverse nil)
934 (defvar gnus-newsgroup-limit nil)
935 (defvar gnus-newsgroup-limits nil)
936
937 (defvar gnus-newsgroup-unreads nil
938   "List of unread articles in the current newsgroup.")
939
940 (defvar gnus-newsgroup-unselected nil
941   "List of unselected unread articles in the current newsgroup.")
942
943 (defvar gnus-newsgroup-reads nil
944   "Alist of read articles and article marks in the current newsgroup.")
945
946 (defvar gnus-newsgroup-expunged-tally nil)
947
948 (defvar gnus-newsgroup-marked nil
949   "List of ticked articles in the current newsgroup (a subset of unread art).")
950
951 (defvar gnus-newsgroup-killed nil
952   "List of ranges of articles that have been through the scoring process.")
953
954 (defvar gnus-newsgroup-cached nil
955   "List of articles that come from the article cache.")
956
957 (defvar gnus-newsgroup-saved nil
958   "List of articles that have been saved.")
959
960 (defvar gnus-newsgroup-kill-headers nil)
961
962 (defvar gnus-newsgroup-replied nil
963   "List of articles that have been replied to in the current newsgroup.")
964
965 (defvar gnus-newsgroup-expirable nil
966   "List of articles in the current newsgroup that can be expired.")
967
968 (defvar gnus-newsgroup-processable nil
969   "List of articles in the current newsgroup that can be processed.")
970
971 (defvar gnus-newsgroup-downloadable nil
972   "List of articles in the current newsgroup that can be processed.")
973
974 (defvar gnus-newsgroup-undownloaded nil
975   "List of articles in the current newsgroup that haven't been downloaded..")
976
977 (defvar gnus-newsgroup-unsendable nil
978   "List of articles in the current newsgroup that won't be sent.")
979
980 (defvar gnus-newsgroup-bookmarks nil
981   "List of articles in the current newsgroup that have bookmarks.")
982
983 (defvar gnus-newsgroup-dormant nil
984   "List of dormant articles in the current newsgroup.")
985
986 (defvar gnus-newsgroup-scored nil
987   "List of scored articles in the current newsgroup.")
988
989 (defvar gnus-newsgroup-headers nil
990   "List of article headers in the current newsgroup.")
991
992 (defvar gnus-newsgroup-threads nil)
993
994 (defvar gnus-newsgroup-prepared nil
995   "Whether the current group has been prepared properly.")
996
997 (defvar gnus-newsgroup-ancient nil
998   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
999
1000 (defvar gnus-newsgroup-sparse nil)
1001
1002 (defvar gnus-current-article nil)
1003 (defvar gnus-article-current nil)
1004 (defvar gnus-current-headers nil)
1005 (defvar gnus-have-all-headers nil)
1006 (defvar gnus-last-article nil)
1007 (defvar gnus-newsgroup-history nil)
1008
1009 (defvar gnus-newsgroup-default-charset nil)
1010 (defvar gnus-newsgroup-iso-8859-1-forced nil)
1011
1012 (defconst gnus-summary-local-variables
1013   '(gnus-newsgroup-name
1014     gnus-newsgroup-begin gnus-newsgroup-end
1015     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1016     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1017     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1018     gnus-newsgroup-unselected gnus-newsgroup-marked
1019     gnus-newsgroup-reads gnus-newsgroup-saved
1020     gnus-newsgroup-replied gnus-newsgroup-expirable
1021     gnus-newsgroup-processable gnus-newsgroup-killed
1022     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1023     gnus-newsgroup-unsendable
1024     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1025     gnus-newsgroup-headers gnus-newsgroup-threads
1026     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1027     gnus-current-article gnus-current-headers gnus-have-all-headers
1028     gnus-last-article gnus-article-internal-prepare-hook
1029     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1030     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1031     gnus-thread-expunge-below
1032     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1033     (gnus-summary-mark-below . global)
1034     gnus-newsgroup-active gnus-scores-exclude-files
1035     gnus-newsgroup-history gnus-newsgroup-ancient
1036     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1037     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1038     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1039     (gnus-newsgroup-expunged-tally . 0)
1040     gnus-cache-removable-articles gnus-newsgroup-cached
1041     gnus-newsgroup-data gnus-newsgroup-data-reverse
1042     gnus-newsgroup-limit gnus-newsgroup-limits
1043     gnus-newsgroup-default-charset gnus-newsgroup-iso-8859-1-forced)
1044   "Variables that are buffer-local to the summary buffers.")
1045
1046 ;; Byte-compiler warning.
1047 (defvar gnus-article-mode-map)
1048
1049 ;; MIME stuff.
1050
1051 (defvar gnus-decode-encoded-word-methods
1052   '(mail-decode-encoded-word-string)
1053   "List of methods used to decode encoded words.
1054
1055 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1056 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1057 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1058 whose names match REGEXP.
1059
1060 For example: 
1061 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1062  mail-decode-encoded-word-string 
1063  (\"chinese\" . rfc1843-decode-string))
1064 ")
1065
1066 (defvar gnus-decode-encoded-word-methods-cache nil)
1067
1068 (defun gnus-multi-decode-encoded-word-string (string)
1069   "Apply the functions from `gnus-encoded-word-methods' that match."
1070   (unless (and gnus-decode-encoded-word-methods-cache
1071                (eq gnus-newsgroup-name 
1072                    (car gnus-decode-encoded-word-methods-cache)))
1073     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1074     (mapc '(lambda (x) 
1075              (if (symbolp x)
1076                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1077                (if (and gnus-newsgroup-name 
1078                         (string-match (car x) gnus-newsgroup-name))
1079                    (nconc gnus-decode-encoded-word-methods-cache 
1080                           (list (cdr x))))))
1081           gnus-decode-encoded-word-methods))
1082   (let ((xlist gnus-decode-encoded-word-methods-cache))
1083     (pop xlist)
1084     (while xlist
1085       (setq string (funcall (pop xlist) string))))
1086   string)
1087
1088 ;; Subject simplification.
1089
1090 (defun gnus-simplify-whitespace (str)
1091   "Remove excessive whitespace."
1092   (let ((mystr str))
1093     ;; Multiple spaces.
1094     (while (string-match "[ \t][ \t]+" mystr)
1095       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1096                           " "
1097                           (substring mystr (match-end 0)))))
1098     ;; Leading spaces.
1099     (when (string-match "^[ \t]+" mystr)
1100       (setq mystr (substring mystr (match-end 0))))
1101     ;; Trailing spaces.
1102     (when (string-match "[ \t]+$" mystr)
1103       (setq mystr (substring mystr 0 (match-beginning 0))))
1104     mystr))
1105
1106 (defsubst gnus-simplify-subject-re (subject)
1107   "Remove \"Re:\" from subject lines."
1108   (if (string-match "^[Rr][Ee]: *" subject)
1109       (substring subject (match-end 0))
1110     subject))
1111
1112 (defun gnus-simplify-subject (subject &optional re-only)
1113   "Remove `Re:' and words in parentheses.
1114 If RE-ONLY is non-nil, strip leading `Re:'s only."
1115   (let ((case-fold-search t))           ;Ignore case.
1116     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1117     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1118       (setq subject (substring subject (match-end 0))))
1119     ;; Remove uninteresting prefixes.
1120     (when (and (not re-only)
1121                gnus-simplify-ignored-prefixes
1122                (string-match gnus-simplify-ignored-prefixes subject))
1123       (setq subject (substring subject (match-end 0))))
1124     ;; Remove words in parentheses from end.
1125     (unless re-only
1126       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1127         (setq subject (substring subject 0 (match-beginning 0)))))
1128     ;; Return subject string.
1129     subject))
1130
1131 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1132 ;; all whitespace.
1133 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1134   (goto-char (point-min))
1135   (while (re-search-forward regexp nil t)
1136       (replace-match (or newtext ""))))
1137
1138 (defun gnus-simplify-buffer-fuzzy ()
1139   "Simplify string in the buffer fuzzily.
1140 The string in the accessible portion of the current buffer is simplified.
1141 It is assumed to be a single-line subject.
1142 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1143 matter is removed.  Additional things can be deleted by setting
1144 gnus-simplify-subject-fuzzy-regexp."
1145   (let ((case-fold-search t)
1146         (modified-tick))
1147     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1148
1149     (while (not (eq modified-tick (buffer-modified-tick)))
1150       (setq modified-tick (buffer-modified-tick))
1151       (cond
1152        ((listp gnus-simplify-subject-fuzzy-regexp)
1153         (mapcar 'gnus-simplify-buffer-fuzzy-step
1154                 gnus-simplify-subject-fuzzy-regexp))
1155        (gnus-simplify-subject-fuzzy-regexp
1156         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1157       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1158       (gnus-simplify-buffer-fuzzy-step
1159        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1160       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1161
1162     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1163     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1164     (gnus-simplify-buffer-fuzzy-step " $")
1165     (gnus-simplify-buffer-fuzzy-step "^ +")))
1166
1167 (defun gnus-simplify-subject-fuzzy (subject)
1168   "Simplify a subject string fuzzily.
1169 See `gnus-simplify-buffer-fuzzy' for details."
1170   (save-excursion
1171     (gnus-set-work-buffer)
1172     (let ((case-fold-search t))
1173       ;; Remove uninteresting prefixes.
1174       (when (and gnus-simplify-ignored-prefixes
1175                  (string-match gnus-simplify-ignored-prefixes subject))
1176         (setq subject (substring subject (match-end 0))))
1177       (insert subject)
1178       (inline (gnus-simplify-buffer-fuzzy))
1179       (buffer-string))))
1180
1181 (defsubst gnus-simplify-subject-fully (subject)
1182   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1183   (cond
1184    (gnus-simplify-subject-functions
1185     (gnus-map-function gnus-simplify-subject-functions subject))
1186    ((null gnus-summary-gather-subject-limit)
1187     (gnus-simplify-subject-re subject))
1188    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1189     (gnus-simplify-subject-fuzzy subject))
1190    ((numberp gnus-summary-gather-subject-limit)
1191     (gnus-limit-string (gnus-simplify-subject-re subject)
1192                        gnus-summary-gather-subject-limit))
1193    (t
1194     subject)))
1195
1196 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1197   "Check whether two subjects are equal.
1198 If optional argument simple-first is t, first argument is already
1199 simplified."
1200   (cond
1201    ((null simple-first)
1202     (equal (gnus-simplify-subject-fully s1)
1203            (gnus-simplify-subject-fully s2)))
1204    (t
1205     (equal s1
1206            (gnus-simplify-subject-fully s2)))))
1207
1208 (defun gnus-summary-bubble-group ()
1209   "Increase the score of the current group.
1210 This is a handy function to add to `gnus-summary-exit-hook' to
1211 increase the score of each group you read."
1212   (gnus-group-add-score gnus-newsgroup-name))
1213
1214 \f
1215 ;;;
1216 ;;; Gnus summary mode
1217 ;;;
1218
1219 (put 'gnus-summary-mode 'mode-class 'special)
1220
1221 (when t
1222   ;; Non-orthogonal keys
1223
1224   (gnus-define-keys gnus-summary-mode-map
1225     " " gnus-summary-next-page
1226     "\177" gnus-summary-prev-page
1227     [delete] gnus-summary-prev-page
1228     [backspace] gnus-summary-prev-page
1229     "\r" gnus-summary-scroll-up
1230     "\M-\r" gnus-summary-scroll-down
1231     "n" gnus-summary-next-unread-article
1232     "p" gnus-summary-prev-unread-article
1233     "N" gnus-summary-next-article
1234     "P" gnus-summary-prev-article
1235     "\M-\C-n" gnus-summary-next-same-subject
1236     "\M-\C-p" gnus-summary-prev-same-subject
1237     "\M-n" gnus-summary-next-unread-subject
1238     "\M-p" gnus-summary-prev-unread-subject
1239     "." gnus-summary-first-unread-article
1240     "," gnus-summary-best-unread-article
1241     "\M-s" gnus-summary-search-article-forward
1242     "\M-r" gnus-summary-search-article-backward
1243     "<" gnus-summary-beginning-of-article
1244     ">" gnus-summary-end-of-article
1245     "j" gnus-summary-goto-article
1246     "^" gnus-summary-refer-parent-article
1247     "\M-^" gnus-summary-refer-article
1248     "u" gnus-summary-tick-article-forward
1249     "!" gnus-summary-tick-article-forward
1250     "U" gnus-summary-tick-article-backward
1251     "d" gnus-summary-mark-as-read-forward
1252     "D" gnus-summary-mark-as-read-backward
1253     "E" gnus-summary-mark-as-expirable
1254     "\M-u" gnus-summary-clear-mark-forward
1255     "\M-U" gnus-summary-clear-mark-backward
1256     "k" gnus-summary-kill-same-subject-and-select
1257     "\C-k" gnus-summary-kill-same-subject
1258     "\M-\C-k" gnus-summary-kill-thread
1259     "\M-\C-l" gnus-summary-lower-thread
1260     "e" gnus-summary-edit-article
1261     "#" gnus-summary-mark-as-processable
1262     "\M-#" gnus-summary-unmark-as-processable
1263     "\M-\C-t" gnus-summary-toggle-threads
1264     "\M-\C-s" gnus-summary-show-thread
1265     "\M-\C-h" gnus-summary-hide-thread
1266     "\M-\C-f" gnus-summary-next-thread
1267     "\M-\C-b" gnus-summary-prev-thread
1268     "\M-\C-u" gnus-summary-up-thread
1269     "\M-\C-d" gnus-summary-down-thread
1270     "&" gnus-summary-execute-command
1271     "c" gnus-summary-catchup-and-exit
1272     "\C-w" gnus-summary-mark-region-as-read
1273     "\C-t" gnus-summary-toggle-truncation
1274     "?" gnus-summary-mark-as-dormant
1275     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1276     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1277     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1278     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1279     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1280     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1281     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1282     "=" gnus-summary-expand-window
1283     "\C-x\C-s" gnus-summary-reselect-current-group
1284     "\M-g" gnus-summary-rescan-group
1285     "w" gnus-summary-stop-page-breaking
1286     "\C-c\C-r" gnus-summary-caesar-message
1287     "f" gnus-summary-followup
1288     "F" gnus-summary-followup-with-original
1289     "C" gnus-summary-cancel-article
1290     "r" gnus-summary-reply
1291     "R" gnus-summary-reply-with-original
1292     "\C-c\C-f" gnus-summary-mail-forward
1293     "o" gnus-summary-save-article
1294     "\C-o" gnus-summary-save-article-mail
1295     "|" gnus-summary-pipe-output
1296     "\M-k" gnus-summary-edit-local-kill
1297     "\M-K" gnus-summary-edit-global-kill
1298     ;; "V" gnus-version
1299     "\C-c\C-d" gnus-summary-describe-group
1300     "q" gnus-summary-exit
1301     "Q" gnus-summary-exit-no-update
1302     "\C-c\C-i" gnus-info-find-node
1303     gnus-mouse-2 gnus-mouse-pick-article
1304     "m" gnus-summary-mail-other-window
1305     "a" gnus-summary-post-news
1306     "x" gnus-summary-limit-to-unread
1307     "s" gnus-summary-isearch-article
1308     "t" gnus-article-hide-headers
1309     "g" gnus-summary-show-article
1310     "l" gnus-summary-goto-last-article
1311     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1312     "\C-d" gnus-summary-enter-digest-group
1313     "\M-\C-d" gnus-summary-read-document
1314     "\M-\C-e" gnus-summary-edit-parameters
1315     "\M-\C-g" gnus-summary-customize-parameters
1316     "\C-c\C-b" gnus-bug
1317     "*" gnus-cache-enter-article
1318     "\M-*" gnus-cache-remove-article
1319     "\M-&" gnus-summary-universal-argument
1320     "\C-l" gnus-recenter
1321     "I" gnus-summary-increase-score
1322     "L" gnus-summary-lower-score
1323     "\M-i" gnus-symbolic-argument
1324     "h" gnus-summary-select-article-buffer
1325
1326     "b" gnus-article-view-part
1327     "\M-t" gnus-summary-toggle-display-buttonized
1328     
1329     "V" gnus-summary-score-map
1330     "X" gnus-uu-extract-map
1331     "S" gnus-summary-send-map)
1332
1333   ;; Sort of orthogonal keymap
1334   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1335     "t" gnus-summary-tick-article-forward
1336     "!" gnus-summary-tick-article-forward
1337     "d" gnus-summary-mark-as-read-forward
1338     "r" gnus-summary-mark-as-read-forward
1339     "c" gnus-summary-clear-mark-forward
1340     " " gnus-summary-clear-mark-forward
1341     "e" gnus-summary-mark-as-expirable
1342     "x" gnus-summary-mark-as-expirable
1343     "?" gnus-summary-mark-as-dormant
1344     "b" gnus-summary-set-bookmark
1345     "B" gnus-summary-remove-bookmark
1346     "#" gnus-summary-mark-as-processable
1347     "\M-#" gnus-summary-unmark-as-processable
1348     "S" gnus-summary-limit-include-expunged
1349     "C" gnus-summary-catchup
1350     "H" gnus-summary-catchup-to-here
1351     "\C-c" gnus-summary-catchup-all
1352     "k" gnus-summary-kill-same-subject-and-select
1353     "K" gnus-summary-kill-same-subject
1354     "P" gnus-uu-mark-map)
1355
1356   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1357     "c" gnus-summary-clear-above
1358     "u" gnus-summary-tick-above
1359     "m" gnus-summary-mark-above
1360     "k" gnus-summary-kill-below)
1361
1362   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1363     "/" gnus-summary-limit-to-subject
1364     "n" gnus-summary-limit-to-articles
1365     "w" gnus-summary-pop-limit
1366     "s" gnus-summary-limit-to-subject
1367     "a" gnus-summary-limit-to-author
1368     "u" gnus-summary-limit-to-unread
1369     "m" gnus-summary-limit-to-marks
1370     "v" gnus-summary-limit-to-score
1371     "*" gnus-summary-limit-include-cached
1372     "D" gnus-summary-limit-include-dormant
1373     "T" gnus-summary-limit-include-thread
1374     "d" gnus-summary-limit-exclude-dormant
1375     "t" gnus-summary-limit-to-age
1376     "E" gnus-summary-limit-include-expunged
1377     "c" gnus-summary-limit-exclude-childless-dormant
1378     "C" gnus-summary-limit-mark-excluded-as-read)
1379
1380   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1381     "n" gnus-summary-next-unread-article
1382     "p" gnus-summary-prev-unread-article
1383     "N" gnus-summary-next-article
1384     "P" gnus-summary-prev-article
1385     "\C-n" gnus-summary-next-same-subject
1386     "\C-p" gnus-summary-prev-same-subject
1387     "\M-n" gnus-summary-next-unread-subject
1388     "\M-p" gnus-summary-prev-unread-subject
1389     "f" gnus-summary-first-unread-article
1390     "b" gnus-summary-best-unread-article
1391     "j" gnus-summary-goto-article
1392     "g" gnus-summary-goto-subject
1393     "l" gnus-summary-goto-last-article
1394     "o" gnus-summary-pop-article)
1395
1396   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1397     "k" gnus-summary-kill-thread
1398     "l" gnus-summary-lower-thread
1399     "i" gnus-summary-raise-thread
1400     "T" gnus-summary-toggle-threads
1401     "t" gnus-summary-rethread-current
1402     "^" gnus-summary-reparent-thread
1403     "s" gnus-summary-show-thread
1404     "S" gnus-summary-show-all-threads
1405     "h" gnus-summary-hide-thread
1406     "H" gnus-summary-hide-all-threads
1407     "n" gnus-summary-next-thread
1408     "p" gnus-summary-prev-thread
1409     "u" gnus-summary-up-thread
1410     "o" gnus-summary-top-thread
1411     "d" gnus-summary-down-thread
1412     "#" gnus-uu-mark-thread
1413     "\M-#" gnus-uu-unmark-thread)
1414
1415   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1416     "g" gnus-summary-prepare
1417     "c" gnus-summary-insert-cached-articles)
1418
1419   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1420     "c" gnus-summary-catchup-and-exit
1421     "C" gnus-summary-catchup-all-and-exit
1422     "E" gnus-summary-exit-no-update
1423     "Q" gnus-summary-exit
1424     "Z" gnus-summary-exit
1425     "n" gnus-summary-catchup-and-goto-next-group
1426     "R" gnus-summary-reselect-current-group
1427     "G" gnus-summary-rescan-group
1428     "N" gnus-summary-next-group
1429     "s" gnus-summary-save-newsrc
1430     "P" gnus-summary-prev-group)
1431
1432   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1433     " " gnus-summary-next-page
1434     "n" gnus-summary-next-page
1435     "\177" gnus-summary-prev-page
1436     [delete] gnus-summary-prev-page
1437     "p" gnus-summary-prev-page
1438     "\r" gnus-summary-scroll-up
1439     "\M-\r" gnus-summary-scroll-down
1440     "<" gnus-summary-beginning-of-article
1441     ">" gnus-summary-end-of-article
1442     "b" gnus-summary-beginning-of-article
1443     "e" gnus-summary-end-of-article
1444     "^" gnus-summary-refer-parent-article
1445     "r" gnus-summary-refer-parent-article
1446     "R" gnus-summary-refer-references
1447     "T" gnus-summary-refer-thread
1448     "g" gnus-summary-show-article
1449     "s" gnus-summary-isearch-article
1450     "P" gnus-summary-print-article)
1451
1452   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1453     "b" gnus-article-add-buttons
1454     "B" gnus-article-add-buttons-to-head
1455     "o" gnus-article-treat-overstrike
1456     "e" gnus-article-emphasize
1457     "w" gnus-article-fill-cited-article
1458     "c" gnus-article-remove-cr
1459     "q" gnus-article-de-quoted-unreadable
1460     "f" gnus-article-display-x-face
1461     "l" gnus-summary-stop-page-breaking
1462     "r" gnus-summary-caesar-message
1463     "t" gnus-article-hide-headers
1464     "v" gnus-summary-verbose-headers
1465     "h" gnus-article-treat-html
1466     "d" gnus-article-treat-dumbquotes)
1467
1468   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1469     "a" gnus-article-hide
1470     "h" gnus-article-hide-headers
1471     "b" gnus-article-hide-boring-headers
1472     "s" gnus-article-hide-signature
1473     "c" gnus-article-hide-citation
1474     "C" gnus-article-hide-citation-in-followups
1475     "p" gnus-article-hide-pgp
1476     "P" gnus-article-hide-pem
1477     "\C-c" gnus-article-hide-citation-maybe)
1478
1479   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1480     "a" gnus-article-highlight
1481     "h" gnus-article-highlight-headers
1482     "c" gnus-article-highlight-citation
1483     "s" gnus-article-highlight-signature)
1484
1485   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1486     "w" gnus-article-decode-mime-words
1487     "c" gnus-article-decode-charset
1488     "v" gnus-mime-view-all-parts
1489     "b" gnus-article-view-part)
1490
1491   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1492     "z" gnus-article-date-ut
1493     "u" gnus-article-date-ut
1494     "l" gnus-article-date-local
1495     "e" gnus-article-date-lapsed
1496     "o" gnus-article-date-original
1497     "i" gnus-article-date-iso8601
1498     "s" gnus-article-date-user)
1499
1500   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1501     "t" gnus-article-remove-trailing-blank-lines
1502     "l" gnus-article-strip-leading-blank-lines
1503     "m" gnus-article-strip-multiple-blank-lines
1504     "a" gnus-article-strip-blank-lines
1505     "A" gnus-article-strip-all-blank-lines
1506     "s" gnus-article-strip-leading-space
1507     "e" gnus-article-strip-trailing-space)
1508
1509   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1510     "v" gnus-version
1511     "f" gnus-summary-fetch-faq
1512     "d" gnus-summary-describe-group
1513     "h" gnus-summary-describe-briefly
1514     "i" gnus-info-find-node)
1515
1516   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1517     "e" gnus-summary-expire-articles
1518     "\M-\C-e" gnus-summary-expire-articles-now
1519     "\177" gnus-summary-delete-article
1520     [delete] gnus-summary-delete-article
1521     "m" gnus-summary-move-article
1522     "r" gnus-summary-respool-article
1523     "w" gnus-summary-edit-article
1524     "c" gnus-summary-copy-article
1525     "B" gnus-summary-crosspost-article
1526     "q" gnus-summary-respool-query
1527     "t" gnus-summary-respool-trace
1528     "i" gnus-summary-import-article
1529     "p" gnus-summary-article-posted-p)
1530
1531   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1532     "o" gnus-summary-save-article
1533     "m" gnus-summary-save-article-mail
1534     "F" gnus-summary-write-article-file
1535     "r" gnus-summary-save-article-rmail
1536     "f" gnus-summary-save-article-file
1537     "b" gnus-summary-save-article-body-file
1538     "h" gnus-summary-save-article-folder
1539     "v" gnus-summary-save-article-vm
1540     "p" gnus-summary-pipe-output
1541     "s" gnus-soup-add-article)
1542
1543   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1544     "b" gnus-summary-display-buttonized
1545     "v" gnus-article-view-part
1546     "o" gnus-article-save-part
1547     "c" gnus-article-copy-part
1548     "e" gnus-article-externalize-part
1549     "|" gnus-article-pipe-part)
1550   )
1551
1552 (defun gnus-summary-make-menu-bar ()
1553   (gnus-turn-off-edit-menu 'summary)
1554
1555   (unless (boundp 'gnus-summary-misc-menu)
1556
1557     (easy-menu-define
1558      gnus-summary-kill-menu gnus-summary-mode-map ""
1559      (cons
1560       "Score"
1561       (nconc
1562        (list
1563         ["Enter score..." gnus-summary-score-entry t]
1564         ["Customize" gnus-score-customize t])
1565        (gnus-make-score-map 'increase)
1566        (gnus-make-score-map 'lower)
1567        '(("Mark"
1568           ["Kill below" gnus-summary-kill-below t]
1569           ["Mark above" gnus-summary-mark-above t]
1570           ["Tick above" gnus-summary-tick-above t]
1571           ["Clear above" gnus-summary-clear-above t])
1572          ["Current score" gnus-summary-current-score t]
1573          ["Set score" gnus-summary-set-score t]
1574          ["Switch current score file..." gnus-score-change-score-file t]
1575          ["Set mark below..." gnus-score-set-mark-below t]
1576          ["Set expunge below..." gnus-score-set-expunge-below t]
1577          ["Edit current score file" gnus-score-edit-current-scores t]
1578          ["Edit score file" gnus-score-edit-file t]
1579          ["Trace score" gnus-score-find-trace t]
1580          ["Find words" gnus-score-find-favourite-words t]
1581          ["Rescore buffer" gnus-summary-rescore t]
1582          ["Increase score..." gnus-summary-increase-score t]
1583          ["Lower score..." gnus-summary-lower-score t]))))
1584
1585     ;; Define both the Article menu in the summary buffer and the equivalent
1586     ;; Commands menu in the article buffer here for consistency.
1587     (let ((innards
1588            '(("Hide"
1589               ["All" gnus-article-hide t]
1590               ["Headers" gnus-article-hide-headers t]
1591               ["Signature" gnus-article-hide-signature t]
1592               ["Citation" gnus-article-hide-citation t]
1593               ["PGP" gnus-article-hide-pgp t]
1594               ["Boring headers" gnus-article-hide-boring-headers t])
1595              ("Highlight"
1596               ["All" gnus-article-highlight t]
1597               ["Headers" gnus-article-highlight-headers t]
1598               ["Signature" gnus-article-highlight-signature t]
1599               ["Citation" gnus-article-highlight-citation t])
1600              ("MIME"
1601               ["Words" gnus-article-decode-mime-words t]
1602               ["Charset" gnus-article-decode-charset t]
1603               ["QP" gnus-article-de-quoted-unreadable t]
1604               ["View all" gnus-mime-view-all-parts t])
1605              ("Date"
1606               ["Local" gnus-article-date-local t]
1607               ["ISO8601" gnus-article-date-iso8601 t]
1608               ["UT" gnus-article-date-ut t]
1609               ["Original" gnus-article-date-original t]
1610               ["Lapsed" gnus-article-date-lapsed t]
1611               ["User-defined" gnus-article-date-user t])
1612              ("Washing"
1613               ("Remove Blanks"
1614                ["Leading" gnus-article-strip-leading-blank-lines t]
1615                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1616                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1617                ["All of the above" gnus-article-strip-blank-lines t]
1618                ["All" gnus-article-strip-all-blank-lines t]
1619                ["Leading space" gnus-article-strip-leading-space t]
1620                ["Trailing space" gnus-article-strip-trailing-space t])
1621               ["Overstrike" gnus-article-treat-overstrike t]
1622               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1623               ["Emphasis" gnus-article-emphasize t]
1624               ["Word wrap" gnus-article-fill-cited-article t]
1625               ["CR" gnus-article-remove-cr t]
1626               ["Show X-Face" gnus-article-display-x-face t]
1627               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1628               ["UnHTMLize" gnus-article-treat-html t]
1629               ["Rot 13" gnus-summary-caesar-message t]
1630               ["Unix pipe" gnus-summary-pipe-message t]
1631               ["Add buttons" gnus-article-add-buttons t]
1632               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1633               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1634               ["Verbose header" gnus-summary-verbose-headers t]
1635               ["Toggle header" gnus-summary-toggle-header t])
1636              ("Output"
1637               ["Save in default format" gnus-summary-save-article t]
1638               ["Save in file" gnus-summary-save-article-file t]
1639               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1640               ["Save in MH folder" gnus-summary-save-article-folder t]
1641               ["Save in VM folder" gnus-summary-save-article-vm t]
1642               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1643               ["Save body in file" gnus-summary-save-article-body-file t]
1644               ["Pipe through a filter" gnus-summary-pipe-output t]
1645               ["Add to SOUP packet" gnus-soup-add-article t]
1646               ["Print" gnus-summary-print-article t])
1647              ("Backend"
1648               ["Respool article..." gnus-summary-respool-article t]
1649               ["Move article..." gnus-summary-move-article
1650                (gnus-check-backend-function
1651                 'request-move-article gnus-newsgroup-name)]
1652               ["Copy article..." gnus-summary-copy-article t]
1653               ["Crosspost article..." gnus-summary-crosspost-article
1654                (gnus-check-backend-function
1655                 'request-replace-article gnus-newsgroup-name)]
1656               ["Import file..." gnus-summary-import-article t]
1657               ["Check if posted" gnus-summary-article-posted-p t]
1658               ["Edit article" gnus-summary-edit-article
1659                (not (gnus-group-read-only-p))]
1660               ["Delete article" gnus-summary-delete-article
1661                (gnus-check-backend-function
1662                 'request-expire-articles gnus-newsgroup-name)]
1663               ["Query respool" gnus-summary-respool-query t]
1664               ["Trace respool" gnus-summary-respool-trace t]
1665               ["Delete expirable articles" gnus-summary-expire-articles-now
1666                (gnus-check-backend-function
1667                 'request-expire-articles gnus-newsgroup-name)])
1668              ("Extract"
1669               ["Uudecode" gnus-uu-decode-uu t]
1670               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1671               ["Unshar" gnus-uu-decode-unshar t]
1672               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1673               ["Save" gnus-uu-decode-save t]
1674               ["Binhex" gnus-uu-decode-binhex t]
1675               ["Postscript" gnus-uu-decode-postscript t])
1676              ("Cache"
1677               ["Enter article" gnus-cache-enter-article t]
1678               ["Remove article" gnus-cache-remove-article t])
1679              ["Select article buffer" gnus-summary-select-article-buffer t]
1680              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1681              ["Isearch article..." gnus-summary-isearch-article t]
1682              ["Beginning of the article" gnus-summary-beginning-of-article t]
1683              ["End of the article" gnus-summary-end-of-article t]
1684              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1685              ["Fetch referenced articles" gnus-summary-refer-references t]
1686              ["Fetch current thread" gnus-summary-refer-thread t]
1687              ["Fetch article with id..." gnus-summary-refer-article t]
1688              ["Redisplay" gnus-summary-show-article t])))
1689       (easy-menu-define
1690        gnus-summary-article-menu gnus-summary-mode-map ""
1691        (cons "Article" innards))
1692
1693       (easy-menu-define
1694        gnus-article-commands-menu gnus-article-mode-map ""
1695        (cons "Commands" innards)))
1696
1697     (easy-menu-define
1698      gnus-summary-thread-menu gnus-summary-mode-map ""
1699      '("Threads"
1700        ["Toggle threading" gnus-summary-toggle-threads t]
1701        ["Hide threads" gnus-summary-hide-all-threads t]
1702        ["Show threads" gnus-summary-show-all-threads t]
1703        ["Hide thread" gnus-summary-hide-thread t]
1704        ["Show thread" gnus-summary-show-thread t]
1705        ["Go to next thread" gnus-summary-next-thread t]
1706        ["Go to previous thread" gnus-summary-prev-thread t]
1707        ["Go down thread" gnus-summary-down-thread t]
1708        ["Go up thread" gnus-summary-up-thread t]
1709        ["Top of thread" gnus-summary-top-thread t]
1710        ["Mark thread as read" gnus-summary-kill-thread t]
1711        ["Lower thread score" gnus-summary-lower-thread t]
1712        ["Raise thread score" gnus-summary-raise-thread t]
1713        ["Rethread current" gnus-summary-rethread-current t]
1714        ))
1715
1716     (easy-menu-define
1717      gnus-summary-post-menu gnus-summary-mode-map ""
1718      '("Post"
1719        ["Post an article" gnus-summary-post-news t]
1720        ["Followup" gnus-summary-followup t]
1721        ["Followup and yank" gnus-summary-followup-with-original t]
1722        ["Supersede article" gnus-summary-supersede-article t]
1723        ["Cancel article" gnus-summary-cancel-article t]
1724        ["Reply" gnus-summary-reply t]
1725        ["Reply and yank" gnus-summary-reply-with-original t]
1726        ["Wide reply" gnus-summary-wide-reply t]
1727        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1728        ["Mail forward" gnus-summary-mail-forward t]
1729        ["Post forward" gnus-summary-post-forward t]
1730        ["Digest and mail" gnus-uu-digest-mail-forward t]
1731        ["Digest and post" gnus-uu-digest-post-forward t]
1732        ["Resend message" gnus-summary-resend-message t]
1733        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1734        ["Send a mail" gnus-summary-mail-other-window t]
1735        ["Uuencode and post" gnus-uu-post-news t]
1736        ["Followup via news" gnus-summary-followup-to-mail t]
1737        ["Followup via news and yank"
1738         gnus-summary-followup-to-mail-with-original t]
1739        ;;("Draft"
1740        ;;["Send" gnus-summary-send-draft t]
1741        ;;["Send bounced" gnus-resend-bounced-mail t])
1742        ))
1743
1744     (easy-menu-define
1745      gnus-summary-misc-menu gnus-summary-mode-map ""
1746      '("Misc"
1747        ("Mark Read"
1748         ["Mark as read" gnus-summary-mark-as-read-forward t]
1749         ["Mark same subject and select"
1750          gnus-summary-kill-same-subject-and-select t]
1751         ["Mark same subject" gnus-summary-kill-same-subject t]
1752         ["Catchup" gnus-summary-catchup t]
1753         ["Catchup all" gnus-summary-catchup-all t]
1754         ["Catchup to here" gnus-summary-catchup-to-here t]
1755         ["Catchup region" gnus-summary-mark-region-as-read t]
1756         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1757        ("Mark Various"
1758         ["Tick" gnus-summary-tick-article-forward t]
1759         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1760         ["Remove marks" gnus-summary-clear-mark-forward t]
1761         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1762         ["Set bookmark" gnus-summary-set-bookmark t]
1763         ["Remove bookmark" gnus-summary-remove-bookmark t])
1764        ("Mark Limit"
1765         ["Marks..." gnus-summary-limit-to-marks t]
1766         ["Subject..." gnus-summary-limit-to-subject t]
1767         ["Author..." gnus-summary-limit-to-author t]
1768         ["Age..." gnus-summary-limit-to-age t]
1769         ["Score" gnus-summary-limit-to-score t]
1770         ["Unread" gnus-summary-limit-to-unread t]
1771         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1772         ["Articles" gnus-summary-limit-to-articles t]
1773         ["Pop limit" gnus-summary-pop-limit t]
1774         ["Show dormant" gnus-summary-limit-include-dormant t]
1775         ["Hide childless dormant"
1776          gnus-summary-limit-exclude-childless-dormant t]
1777         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1778         ["Show expunged" gnus-summary-show-all-expunged t])
1779        ("Process Mark"
1780         ["Set mark" gnus-summary-mark-as-processable t]
1781         ["Remove mark" gnus-summary-unmark-as-processable t]
1782         ["Remove all marks" gnus-summary-unmark-all-processable t]
1783         ["Mark above" gnus-uu-mark-over t]
1784         ["Mark series" gnus-uu-mark-series t]
1785         ["Mark region" gnus-uu-mark-region t]
1786         ["Unmark region" gnus-uu-unmark-region t]
1787         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1788         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1789         ["Mark all" gnus-uu-mark-all t]
1790         ["Mark buffer" gnus-uu-mark-buffer t]
1791         ["Mark sparse" gnus-uu-mark-sparse t]
1792         ["Mark thread" gnus-uu-mark-thread t]
1793         ["Unmark thread" gnus-uu-unmark-thread t]
1794         ("Process Mark Sets"
1795          ["Kill" gnus-summary-kill-process-mark t]
1796          ["Yank" gnus-summary-yank-process-mark
1797           gnus-newsgroup-process-stack]
1798          ["Save" gnus-summary-save-process-mark t]))
1799        ("Scroll article"
1800         ["Page forward" gnus-summary-next-page t]
1801         ["Page backward" gnus-summary-prev-page t]
1802         ["Line forward" gnus-summary-scroll-up t])
1803        ("Move"
1804         ["Next unread article" gnus-summary-next-unread-article t]
1805         ["Previous unread article" gnus-summary-prev-unread-article t]
1806         ["Next article" gnus-summary-next-article t]
1807         ["Previous article" gnus-summary-prev-article t]
1808         ["Next unread subject" gnus-summary-next-unread-subject t]
1809         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1810         ["Next article same subject" gnus-summary-next-same-subject t]
1811         ["Previous article same subject" gnus-summary-prev-same-subject t]
1812         ["First unread article" gnus-summary-first-unread-article t]
1813         ["Best unread article" gnus-summary-best-unread-article t]
1814         ["Go to subject number..." gnus-summary-goto-subject t]
1815         ["Go to article number..." gnus-summary-goto-article t]
1816         ["Go to the last article" gnus-summary-goto-last-article t]
1817         ["Pop article off history" gnus-summary-pop-article t])
1818        ("Sort"
1819         ["Sort by number" gnus-summary-sort-by-number t]
1820         ["Sort by author" gnus-summary-sort-by-author t]
1821         ["Sort by subject" gnus-summary-sort-by-subject t]
1822         ["Sort by date" gnus-summary-sort-by-date t]
1823         ["Sort by score" gnus-summary-sort-by-score t]
1824         ["Sort by lines" gnus-summary-sort-by-lines t])
1825        ("Help"
1826         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1827         ["Describe group" gnus-summary-describe-group t]
1828         ["Read manual" gnus-info-find-node t])
1829        ("Modes"
1830         ["Pick and read" gnus-pick-mode t]
1831         ["Binary" gnus-binary-mode t])
1832        ("Regeneration"
1833         ["Regenerate" gnus-summary-prepare t]
1834         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1835         ["Toggle threading" gnus-summary-toggle-threads t])
1836        ["Filter articles..." gnus-summary-execute-command t]
1837        ["Run command on subjects..." gnus-summary-universal-argument t]
1838        ["Search articles forward..." gnus-summary-search-article-forward t]
1839        ["Search articles backward..." gnus-summary-search-article-backward t]
1840        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1841        ["Expand window" gnus-summary-expand-window t]
1842        ["Expire expirable articles" gnus-summary-expire-articles
1843         (gnus-check-backend-function
1844          'request-expire-articles gnus-newsgroup-name)]
1845        ["Edit local kill file" gnus-summary-edit-local-kill t]
1846        ["Edit main kill file" gnus-summary-edit-global-kill t]
1847        ["Edit group parameters" gnus-summary-edit-parameters t]
1848        ["Customize group parameters" gnus-summary-customize-parameters t]
1849        ["Send a bug report" gnus-bug t]
1850        ("Exit"
1851         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1852         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1853         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1854         ["Exit group" gnus-summary-exit t]
1855         ["Exit group without updating" gnus-summary-exit-no-update t]
1856         ["Exit and goto next group" gnus-summary-next-group t]
1857         ["Exit and goto prev group" gnus-summary-prev-group t]
1858         ["Reselect group" gnus-summary-reselect-current-group t]
1859         ["Rescan group" gnus-summary-rescan-group t]
1860         ["Update dribble" gnus-summary-save-newsrc t])))
1861
1862     (gnus-run-hooks 'gnus-summary-menu-hook)))
1863
1864 (defun gnus-score-set-default (var value)
1865   "A version of set that updates the GNU Emacs menu-bar."
1866   (set var value)
1867   ;; It is the message that forces the active status to be updated.
1868   (message ""))
1869
1870 (defun gnus-make-score-map (type)
1871   "Make a summary score map of type TYPE."
1872   (if t
1873       nil
1874     (let ((headers '(("author" "from" string)
1875                      ("subject" "subject" string)
1876                      ("article body" "body" string)
1877                      ("article head" "head" string)
1878                      ("xref" "xref" string)
1879                      ("extra header" "extra" string)
1880                      ("lines" "lines" number)
1881                      ("followups to author" "followup" string)))
1882           (types '((number ("less than" <)
1883                            ("greater than" >)
1884                            ("equal" =))
1885                    (string ("substring" s)
1886                            ("exact string" e)
1887                            ("fuzzy string" f)
1888                            ("regexp" r))))
1889           (perms '(("temporary" (current-time-string))
1890                    ("permanent" nil)
1891                    ("immediate" now)))
1892           header)
1893       (list
1894        (apply
1895         'nconc
1896         (list
1897          (if (eq type 'lower)
1898              "Lower score"
1899            "Increase score"))
1900         (let (outh)
1901           (while headers
1902             (setq header (car headers))
1903             (setq outh
1904                   (cons
1905                    (apply
1906                     'nconc
1907                     (list (car header))
1908                     (let ((ts (cdr (assoc (nth 2 header) types)))
1909                           outt)
1910                       (while ts
1911                         (setq outt
1912                               (cons
1913                                (apply
1914                                 'nconc
1915                                 (list (caar ts))
1916                                 (let ((ps perms)
1917                                       outp)
1918                                   (while ps
1919                                     (setq outp
1920                                           (cons
1921                                            (vector
1922                                             (caar ps)
1923                                             (list
1924                                              'gnus-summary-score-entry
1925                                              (nth 1 header)
1926                                              (if (or (string= (nth 1 header)
1927                                                               "head")
1928                                                      (string= (nth 1 header)
1929                                                               "body"))
1930                                                  ""
1931                                                (list 'gnus-summary-header
1932                                                      (nth 1 header)))
1933                                              (list 'quote (nth 1 (car ts)))
1934                                              (list 'gnus-score-default nil)
1935                                              (nth 1 (car ps))
1936                                              t)
1937                                             t)
1938                                            outp))
1939                                     (setq ps (cdr ps)))
1940                                   (list (nreverse outp))))
1941                                outt))
1942                         (setq ts (cdr ts)))
1943                       (list (nreverse outt))))
1944                    outh))
1945             (setq headers (cdr headers)))
1946           (list (nreverse outh))))))))
1947
1948 \f
1949
1950 (defun gnus-summary-mode (&optional group)
1951   "Major mode for reading articles.
1952
1953 All normal editing commands are switched off.
1954 \\<gnus-summary-mode-map>
1955 Each line in this buffer represents one article.  To read an
1956 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1957 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1958 respectively.
1959
1960 You can also post articles and send mail from this buffer.  To
1961 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1962 of an article, type `\\[gnus-summary-reply]'.
1963
1964 There are approx. one gazillion commands you can execute in this
1965 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1966
1967 The following commands are available:
1968
1969 \\{gnus-summary-mode-map}"
1970   (interactive)
1971   (when (gnus-visual-p 'summary-menu 'menu)
1972     (gnus-summary-make-menu-bar))
1973   (kill-all-local-variables)
1974   (gnus-summary-make-local-variables)
1975   (gnus-make-thread-indent-array)
1976   (gnus-simplify-mode-line)
1977   (setq major-mode 'gnus-summary-mode)
1978   (setq mode-name "Summary")
1979   (make-local-variable 'minor-mode-alist)
1980   (use-local-map gnus-summary-mode-map)
1981   (buffer-disable-undo)
1982   (setq buffer-read-only t)             ;Disable modification
1983   (setq truncate-lines t)
1984   (setq selective-display t)
1985   (setq selective-display-ellipses t)   ;Display `...'
1986   (gnus-summary-set-display-table)
1987   (gnus-set-default-directory)
1988   (setq gnus-newsgroup-name group)
1989   (make-local-variable 'gnus-summary-line-format)
1990   (make-local-variable 'gnus-summary-line-format-spec)
1991   (make-local-variable 'gnus-summary-dummy-line-format)
1992   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1993   (make-local-variable 'gnus-summary-mark-positions)
1994   (make-local-hook 'post-command-hook)
1995   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1996   (make-local-hook 'pre-command-hook)
1997   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1998   (gnus-run-hooks 'gnus-summary-mode-hook)
1999   (mm-enable-multibyte)
2000   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2001   (gnus-update-summary-mark-positions))
2002
2003 (defun gnus-summary-make-local-variables ()
2004   "Make all the local summary buffer variables."
2005   (let ((locals gnus-summary-local-variables)
2006         global local)
2007     (while (setq local (pop locals))
2008       (if (consp local)
2009           (progn
2010             (if (eq (cdr local) 'global)
2011                 ;; Copy the global value of the variable.
2012                 (setq global (symbol-value (car local)))
2013               ;; Use the value from the list.
2014               (setq global (eval (cdr local))))
2015             (make-local-variable (car local))
2016             (set (car local) global))
2017         ;; Simple nil-valued local variable.
2018         (make-local-variable local)
2019         (set local nil)))))
2020
2021 (defun gnus-summary-clear-local-variables ()
2022   (let ((locals gnus-summary-local-variables))
2023     (while locals
2024       (if (consp (car locals))
2025           (and (vectorp (caar locals))
2026                (set (caar locals) nil))
2027         (and (vectorp (car locals))
2028              (set (car locals) nil)))
2029       (setq locals (cdr locals)))))
2030
2031 ;; Summary data functions.
2032
2033 (defmacro gnus-data-number (data)
2034   `(car ,data))
2035
2036 (defmacro gnus-data-set-number (data number)
2037   `(setcar ,data ,number))
2038
2039 (defmacro gnus-data-mark (data)
2040   `(nth 1 ,data))
2041
2042 (defmacro gnus-data-set-mark (data mark)
2043   `(setcar (nthcdr 1 ,data) ,mark))
2044
2045 (defmacro gnus-data-pos (data)
2046   `(nth 2 ,data))
2047
2048 (defmacro gnus-data-set-pos (data pos)
2049   `(setcar (nthcdr 2 ,data) ,pos))
2050
2051 (defmacro gnus-data-header (data)
2052   `(nth 3 ,data))
2053
2054 (defmacro gnus-data-set-header (data header)
2055   `(setf (nth 3 ,data) ,header))
2056
2057 (defmacro gnus-data-level (data)
2058   `(nth 4 ,data))
2059
2060 (defmacro gnus-data-unread-p (data)
2061   `(= (nth 1 ,data) gnus-unread-mark))
2062
2063 (defmacro gnus-data-read-p (data)
2064   `(/= (nth 1 ,data) gnus-unread-mark))
2065
2066 (defmacro gnus-data-pseudo-p (data)
2067   `(consp (nth 3 ,data)))
2068
2069 (defmacro gnus-data-find (number)
2070   `(assq ,number gnus-newsgroup-data))
2071
2072 (defmacro gnus-data-find-list (number &optional data)
2073   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2074      (memq (assq ,number bdata)
2075            bdata)))
2076
2077 (defmacro gnus-data-make (number mark pos header level)
2078   `(list ,number ,mark ,pos ,header ,level))
2079
2080 (defun gnus-data-enter (after-article number mark pos header level offset)
2081   (let ((data (gnus-data-find-list after-article)))
2082     (unless data
2083       (error "No such article: %d" after-article))
2084     (setcdr data (cons (gnus-data-make number mark pos header level)
2085                        (cdr data)))
2086     (setq gnus-newsgroup-data-reverse nil)
2087     (gnus-data-update-list (cddr data) offset)))
2088
2089 (defun gnus-data-enter-list (after-article list &optional offset)
2090   (when list
2091     (let ((data (and after-article (gnus-data-find-list after-article)))
2092           (ilist list))
2093       (if (not (or data
2094                    after-article))
2095           (let ((odata gnus-newsgroup-data))
2096             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2097             (when offset
2098               (gnus-data-update-list odata offset)))
2099         ;; Find the last element in the list to be spliced into the main
2100         ;; list.
2101         (while (cdr list)
2102           (setq list (cdr list)))
2103         (if (not data)
2104             (progn
2105               (setcdr list gnus-newsgroup-data)
2106               (setq gnus-newsgroup-data ilist)
2107               (when offset
2108                 (gnus-data-update-list (cdr list) offset)))
2109           (setcdr list (cdr data))
2110           (setcdr data ilist)
2111           (when offset
2112             (gnus-data-update-list (cdr list) offset))))
2113       (setq gnus-newsgroup-data-reverse nil))))
2114
2115 (defun gnus-data-remove (article &optional offset)
2116   (let ((data gnus-newsgroup-data))
2117     (if (= (gnus-data-number (car data)) article)
2118         (progn
2119           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2120                 gnus-newsgroup-data-reverse nil)
2121           (when offset
2122             (gnus-data-update-list gnus-newsgroup-data offset)))
2123       (while (cdr data)
2124         (when (= (gnus-data-number (cadr data)) article)
2125           (setcdr data (cddr data))
2126           (when offset
2127             (gnus-data-update-list (cdr data) offset))
2128           (setq data nil
2129                 gnus-newsgroup-data-reverse nil))
2130         (setq data (cdr data))))))
2131
2132 (defmacro gnus-data-list (backward)
2133   `(if ,backward
2134        (or gnus-newsgroup-data-reverse
2135            (setq gnus-newsgroup-data-reverse
2136                  (reverse gnus-newsgroup-data)))
2137      gnus-newsgroup-data))
2138
2139 (defun gnus-data-update-list (data offset)
2140   "Add OFFSET to the POS of all data entries in DATA."
2141   (setq gnus-newsgroup-data-reverse nil)
2142   (while data
2143     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2144     (setq data (cdr data))))
2145
2146 (defun gnus-summary-article-pseudo-p (article)
2147   "Say whether this article is a pseudo article or not."
2148   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2149
2150 (defmacro gnus-summary-article-sparse-p (article)
2151   "Say whether this article is a sparse article or not."
2152   `(memq ,article gnus-newsgroup-sparse))
2153
2154 (defmacro gnus-summary-article-ancient-p (article)
2155   "Say whether this article is a sparse article or not."
2156   `(memq ,article gnus-newsgroup-ancient))
2157
2158 (defun gnus-article-parent-p (number)
2159   "Say whether this article is a parent or not."
2160   (let ((data (gnus-data-find-list number)))
2161     (and (cdr data)                     ; There has to be an article after...
2162          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2163             (gnus-data-level (nth 1 data))))))
2164
2165 (defun gnus-article-children (number)
2166   "Return a list of all children to NUMBER."
2167   (let* ((data (gnus-data-find-list number))
2168          (level (gnus-data-level (car data)))
2169          children)
2170     (setq data (cdr data))
2171     (while (and data
2172                 (= (gnus-data-level (car data)) (1+ level)))
2173       (push (gnus-data-number (car data)) children)
2174       (setq data (cdr data)))
2175     children))
2176
2177 (defmacro gnus-summary-skip-intangible ()
2178   "If the current article is intangible, then jump to a different article."
2179   '(let ((to (get-text-property (point) 'gnus-intangible)))
2180      (and to (gnus-summary-goto-subject to))))
2181
2182 (defmacro gnus-summary-article-intangible-p ()
2183   "Say whether this article is intangible or not."
2184   '(get-text-property (point) 'gnus-intangible))
2185
2186 (defun gnus-article-read-p (article)
2187   "Say whether ARTICLE is read or not."
2188   (not (or (memq article gnus-newsgroup-marked)
2189            (memq article gnus-newsgroup-unreads)
2190            (memq article gnus-newsgroup-unselected)
2191            (memq article gnus-newsgroup-dormant))))
2192
2193 ;; Some summary mode macros.
2194
2195 (defmacro gnus-summary-article-number ()
2196   "The article number of the article on the current line.
2197 If there isn's an article number here, then we return the current
2198 article number."
2199   '(progn
2200      (gnus-summary-skip-intangible)
2201      (or (get-text-property (point) 'gnus-number)
2202          (gnus-summary-last-subject))))
2203
2204 (defmacro gnus-summary-article-header (&optional number)
2205   "Return the header of article NUMBER."
2206   `(gnus-data-header (gnus-data-find
2207                       ,(or number '(gnus-summary-article-number)))))
2208
2209 (defmacro gnus-summary-thread-level (&optional number)
2210   "Return the level of thread that starts with article NUMBER."
2211   `(if (and (eq gnus-summary-make-false-root 'dummy)
2212             (get-text-property (point) 'gnus-intangible))
2213        0
2214      (gnus-data-level (gnus-data-find
2215                        ,(or number '(gnus-summary-article-number))))))
2216
2217 (defmacro gnus-summary-article-mark (&optional number)
2218   "Return the mark of article NUMBER."
2219   `(gnus-data-mark (gnus-data-find
2220                     ,(or number '(gnus-summary-article-number)))))
2221
2222 (defmacro gnus-summary-article-pos (&optional number)
2223   "Return the position of the line of article NUMBER."
2224   `(gnus-data-pos (gnus-data-find
2225                    ,(or number '(gnus-summary-article-number)))))
2226
2227 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2228 (defmacro gnus-summary-article-subject (&optional number)
2229   "Return current subject string or nil if nothing."
2230   `(let ((headers
2231           ,(if number
2232                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2233              '(gnus-data-header (assq (gnus-summary-article-number)
2234                                       gnus-newsgroup-data)))))
2235      (and headers
2236           (vectorp headers)
2237           (mail-header-subject headers))))
2238
2239 (defmacro gnus-summary-article-score (&optional number)
2240   "Return current article score."
2241   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2242                   gnus-newsgroup-scored))
2243        gnus-summary-default-score 0))
2244
2245 (defun gnus-summary-article-children (&optional number)
2246   "Return a list of article numbers that are children of article NUMBER."
2247   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2248          (level (gnus-data-level (car data)))
2249          l children)
2250     (while (and (setq data (cdr data))
2251                 (> (setq l (gnus-data-level (car data))) level))
2252       (and (= (1+ level) l)
2253            (push (gnus-data-number (car data))
2254                  children)))
2255     (nreverse children)))
2256
2257 (defun gnus-summary-article-parent (&optional number)
2258   "Return the article number of the parent of article NUMBER."
2259   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2260                                     (gnus-data-list t)))
2261          (level (gnus-data-level (car data))))
2262     (if (zerop level)
2263         ()                              ; This is a root.
2264       ;; We search until we find an article with a level less than
2265       ;; this one.  That function has to be the parent.
2266       (while (and (setq data (cdr data))
2267                   (not (< (gnus-data-level (car data)) level))))
2268       (and data (gnus-data-number (car data))))))
2269
2270 (defun gnus-unread-mark-p (mark)
2271   "Say whether MARK is the unread mark."
2272   (= mark gnus-unread-mark))
2273
2274 (defun gnus-read-mark-p (mark)
2275   "Say whether MARK is one of the marks that mark as read.
2276 This is all marks except unread, ticked, dormant, and expirable."
2277   (not (or (= mark gnus-unread-mark)
2278            (= mark gnus-ticked-mark)
2279            (= mark gnus-dormant-mark)
2280            (= mark gnus-expirable-mark))))
2281
2282 (defmacro gnus-article-mark (number)
2283   "Return the MARK of article NUMBER.
2284 This macro should only be used when computing the mark the \"first\"
2285 time; i.e., when generating the summary lines.  After that,
2286 `gnus-summary-article-mark' should be used to examine the
2287 marks of articles."
2288   `(cond
2289     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2290     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2291     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2292     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2293     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2294     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2295     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2296     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2297            gnus-ancient-mark))))
2298
2299 ;; Saving hidden threads.
2300
2301 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2302 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2303
2304 (defmacro gnus-save-hidden-threads (&rest forms)
2305   "Save hidden threads, eval FORMS, and restore the hidden threads."
2306   (let ((config (make-symbol "config")))
2307     `(let ((,config (gnus-hidden-threads-configuration)))
2308        (unwind-protect
2309            (save-excursion
2310              ,@forms)
2311          (gnus-restore-hidden-threads-configuration ,config)))))
2312
2313 (defun gnus-data-compute-positions ()
2314   "Compute the positions of all articles."
2315   (setq gnus-newsgroup-data-reverse nil)
2316   (let ((data gnus-newsgroup-data))
2317     (save-excursion
2318       (gnus-save-hidden-threads
2319         (gnus-summary-show-all-threads)
2320         (goto-char (point-min))
2321         (while data
2322           (while (get-text-property (point) 'gnus-intangible)
2323             (forward-line 1))
2324           (gnus-data-set-pos (car data) (+ (point) 3))
2325           (setq data (cdr data))
2326           (forward-line 1))))))
2327
2328 (defun gnus-hidden-threads-configuration ()
2329   "Return the current hidden threads configuration."
2330   (save-excursion
2331     (let (config)
2332       (goto-char (point-min))
2333       (while (search-forward "\r" nil t)
2334         (push (1- (point)) config))
2335       config)))
2336
2337 (defun gnus-restore-hidden-threads-configuration (config)
2338   "Restore hidden threads configuration from CONFIG."
2339   (let (point buffer-read-only)
2340     (while (setq point (pop config))
2341       (when (and (< point (point-max))
2342                  (goto-char point)
2343                  (eq (char-after) ?\n))
2344         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2345
2346 ;; Various summary mode internalish functions.
2347
2348 (defun gnus-mouse-pick-article (e)
2349   (interactive "e")
2350   (mouse-set-point e)
2351   (gnus-summary-next-page nil t))
2352
2353 (defun gnus-summary-set-display-table ()
2354   ;; Change the display table.  Odd characters have a tendency to mess
2355   ;; up nicely formatted displays - we make all possible glyphs
2356   ;; display only a single character.
2357
2358   ;; We start from the standard display table, if any.
2359   (let ((table (or (copy-sequence standard-display-table)
2360                    (make-display-table)))
2361         (i 32))
2362     ;; Nix out all the control chars...
2363     (while (>= (setq i (1- i)) 0)
2364       (aset table i [??]))
2365     ;; ... but not newline and cr, of course.  (cr is necessary for the
2366     ;; selective display).
2367     (aset table ?\n nil)
2368     (aset table ?\r nil)
2369     ;; We keep TAB as well.
2370     (aset table ?\t nil)
2371     ;; We nix out any glyphs over 126 that are not set already.
2372     (let ((i 256))
2373       (while (>= (setq i (1- i)) 127)
2374         ;; Only modify if the entry is nil.
2375         (unless (aref table i)
2376           (aset table i [??]))))
2377     (setq buffer-display-table table)))
2378
2379 (defun gnus-summary-setup-buffer (group)
2380   "Initialize summary buffer."
2381   (let ((buffer (concat "*Summary " group "*")))
2382     (if (get-buffer buffer)
2383         (progn
2384           (set-buffer buffer)
2385           (setq gnus-summary-buffer (current-buffer))
2386           (not gnus-newsgroup-prepared))
2387       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2388       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2389       (gnus-summary-mode group)
2390       (when gnus-carpal
2391         (gnus-carpal-setup-buffer 'summary))
2392       (unless gnus-single-article-buffer
2393         (make-local-variable 'gnus-article-buffer)
2394         (make-local-variable 'gnus-article-current)
2395         (make-local-variable 'gnus-original-article-buffer))
2396       (setq gnus-newsgroup-name group)
2397       t)))
2398
2399 (defun gnus-set-global-variables ()
2400   ;; Set the global equivalents of the summary buffer-local variables
2401   ;; to the latest values they had.  These reflect the summary buffer
2402   ;; that was in action when the last article was fetched.
2403   (when (eq major-mode 'gnus-summary-mode)
2404     (setq gnus-summary-buffer (current-buffer))
2405     (let ((name gnus-newsgroup-name)
2406           (marked gnus-newsgroup-marked)
2407           (unread gnus-newsgroup-unreads)
2408           (headers gnus-current-headers)
2409           (data gnus-newsgroup-data)
2410           (summary gnus-summary-buffer)
2411           (article-buffer gnus-article-buffer)
2412           (original gnus-original-article-buffer)
2413           (gac gnus-article-current)
2414           (reffed gnus-reffed-article-number)
2415           (score-file gnus-current-score-file)
2416           (default-charset gnus-newsgroup-default-charset)
2417           (iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2418       (save-excursion
2419         (set-buffer gnus-group-buffer)
2420         (setq gnus-newsgroup-name name
2421               gnus-newsgroup-marked marked
2422               gnus-newsgroup-unreads unread
2423               gnus-current-headers headers
2424               gnus-newsgroup-data data
2425               gnus-article-current gac
2426               gnus-summary-buffer summary
2427               gnus-article-buffer article-buffer
2428               gnus-original-article-buffer original
2429               gnus-reffed-article-number reffed
2430               gnus-current-score-file score-file
2431               gnus-newsgroup-default-charset default-charset
2432               gnus-newsgroup-iso-8859-1-forced iso-8859-1-forced)
2433         ;; The article buffer also has local variables.
2434         (when (gnus-buffer-live-p gnus-article-buffer)
2435           (set-buffer gnus-article-buffer)
2436           (setq gnus-summary-buffer summary))))))
2437
2438 (defun gnus-summary-article-unread-p (article)
2439   "Say whether ARTICLE is unread or not."
2440   (memq article gnus-newsgroup-unreads))
2441
2442 (defun gnus-summary-first-article-p (&optional article)
2443   "Return whether ARTICLE is the first article in the buffer."
2444   (if (not (setq article (or article (gnus-summary-article-number))))
2445       nil
2446     (eq article (caar gnus-newsgroup-data))))
2447
2448 (defun gnus-summary-last-article-p (&optional article)
2449   "Return whether ARTICLE is the last article in the buffer."
2450   (if (not (setq article (or article (gnus-summary-article-number))))
2451       t                                 ; All non-existent numbers are the last article.  :-)
2452     (not (cdr (gnus-data-find-list article)))))
2453
2454 (defun gnus-make-thread-indent-array ()
2455   (let ((n 200))
2456     (unless (and gnus-thread-indent-array
2457                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2458       (setq gnus-thread-indent-array (make-vector 201 "")
2459             gnus-thread-indent-array-level gnus-thread-indent-level)
2460       (while (>= n 0)
2461         (aset gnus-thread-indent-array n
2462               (make-string (* n gnus-thread-indent-level) ? ))
2463         (setq n (1- n))))))
2464
2465 (defun gnus-update-summary-mark-positions ()
2466   "Compute where the summary marks are to go."
2467   (save-excursion
2468     (when (gnus-buffer-exists-p gnus-summary-buffer)
2469       (set-buffer gnus-summary-buffer))
2470     (let ((gnus-replied-mark 129)
2471           (gnus-score-below-mark 130)
2472           (gnus-score-over-mark 130)
2473           (gnus-download-mark 131)
2474           (spec gnus-summary-line-format-spec)
2475           gnus-visual pos)
2476       (save-excursion
2477         (gnus-set-work-buffer)
2478         (let ((gnus-summary-line-format-spec spec)
2479               (gnus-newsgroup-downloadable '((0 . t))))
2480           (gnus-summary-insert-line
2481            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2482           (goto-char (point-min))
2483           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2484                                              (- (point) 2)))))
2485           (goto-char (point-min))
2486           (push (cons 'replied (and (search-forward "\201" nil t)
2487                                     (- (point) 2)))
2488                 pos)
2489           (goto-char (point-min))
2490           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2491                 pos)
2492           (goto-char (point-min))
2493           (push (cons 'download
2494                       (and (search-forward "\203" nil t) (- (point) 2)))
2495                 pos)))
2496       (setq gnus-summary-mark-positions pos))))
2497
2498 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2499   "Insert a dummy root in the summary buffer."
2500   (beginning-of-line)
2501   (gnus-add-text-properties
2502    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2503    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2504
2505 (defun gnus-summary-from-or-to-or-newsgroups (header)
2506   (let ((to (cdr (assq 'To (mail-header-extra header))))
2507         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2508         (rfc2047-default-charset gnus-newsgroup-default-charset)
2509         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2510     (cond
2511      ((and to
2512            gnus-ignored-from-addresses
2513            (string-match gnus-ignored-from-addresses
2514                          (mail-header-from header)))
2515       (concat "-> "
2516               (or (car (funcall gnus-extract-address-components
2517                                 (funcall
2518                                  gnus-decode-encoded-word-function to)))
2519                   (funcall gnus-decode-encoded-word-function to))))
2520      ((and newsgroups
2521            gnus-ignored-from-addresses
2522            (string-match gnus-ignored-from-addresses
2523                          (mail-header-from header)))
2524       (concat "=> " newsgroups))
2525      (t
2526       (or (car (funcall gnus-extract-address-components
2527                         (mail-header-from header)))
2528           (mail-header-from header))))))
2529
2530 (defun gnus-summary-insert-line (gnus-tmp-header
2531                                  gnus-tmp-level gnus-tmp-current
2532                                  gnus-tmp-unread gnus-tmp-replied
2533                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2534                                  &optional gnus-tmp-dummy gnus-tmp-score
2535                                  gnus-tmp-process)
2536   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2537          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2538          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2539          (gnus-tmp-score-char
2540           (if (or (null gnus-summary-default-score)
2541                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2542                       gnus-summary-zcore-fuzz))
2543               ? 
2544             (if (< gnus-tmp-score gnus-summary-default-score)
2545                 gnus-score-below-mark gnus-score-over-mark)))
2546          (gnus-tmp-replied
2547           (cond (gnus-tmp-process gnus-process-mark)
2548                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2549                  gnus-cached-mark)
2550                 (gnus-tmp-replied gnus-replied-mark)
2551                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2552                  gnus-saved-mark)
2553                 (t gnus-unread-mark)))
2554          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2555          (gnus-tmp-name
2556           (cond
2557            ((string-match "<[^>]+> *$" gnus-tmp-from)
2558             (let ((beg (match-beginning 0)))
2559               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2560                        (substring gnus-tmp-from (1+ (match-beginning 0))
2561                                   (1- (match-end 0))))
2562                   (substring gnus-tmp-from 0 beg))))
2563            ((string-match "(.+)" gnus-tmp-from)
2564             (substring gnus-tmp-from
2565                        (1+ (match-beginning 0)) (1- (match-end 0))))
2566            (t gnus-tmp-from)))
2567          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2568          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2569          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2570          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2571          (buffer-read-only nil))
2572     (when (string= gnus-tmp-name "")
2573       (setq gnus-tmp-name gnus-tmp-from))
2574     (unless (numberp gnus-tmp-lines)
2575       (setq gnus-tmp-lines 0))
2576     (gnus-put-text-property
2577      (point)
2578      (progn (eval gnus-summary-line-format-spec) (point))
2579      'gnus-number gnus-tmp-number)
2580     (when (gnus-visual-p 'summary-highlight 'highlight)
2581       (forward-line -1)
2582       (gnus-run-hooks 'gnus-summary-update-hook)
2583       (forward-line 1))))
2584
2585 (defun gnus-summary-update-line (&optional dont-update)
2586   ;; Update summary line after change.
2587   (when (and gnus-summary-default-score
2588              (not gnus-summary-inhibit-highlight))
2589     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2590            (article (gnus-summary-article-number))
2591            (score (gnus-summary-article-score article)))
2592       (unless dont-update
2593         (if (and gnus-summary-mark-below
2594                  (< (gnus-summary-article-score)
2595                     gnus-summary-mark-below))
2596             ;; This article has a low score, so we mark it as read.
2597             (when (memq article gnus-newsgroup-unreads)
2598               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2599           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2600             ;; This article was previously marked as read on account
2601             ;; of a low score, but now it has risen, so we mark it as
2602             ;; unread.
2603             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2604         (gnus-summary-update-mark
2605          (if (or (null gnus-summary-default-score)
2606                  (<= (abs (- score gnus-summary-default-score))
2607                      gnus-summary-zcore-fuzz))
2608              ? 
2609            (if (< score gnus-summary-default-score)
2610                gnus-score-below-mark gnus-score-over-mark))
2611          'score))
2612       ;; Do visual highlighting.
2613       (when (gnus-visual-p 'summary-highlight 'highlight)
2614         (gnus-run-hooks 'gnus-summary-update-hook)))))
2615
2616 (defvar gnus-tmp-new-adopts nil)
2617
2618 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2619   "Return the number of articles in THREAD.
2620 This may be 0 in some cases -- if none of the articles in
2621 the thread are to be displayed."
2622   (let* ((number
2623           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2624           (cond
2625            ((not (listp thread))
2626             1)
2627            ((and (consp thread) (cdr thread))
2628             (apply
2629              '+ 1 (mapcar
2630                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2631            ((null thread)
2632             1)
2633            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2634             1)
2635            (t 0))))
2636     (when (and level (zerop level) gnus-tmp-new-adopts)
2637       (incf number
2638             (apply '+ (mapcar
2639                        'gnus-summary-number-of-articles-in-thread
2640                        gnus-tmp-new-adopts))))
2641     (if char
2642         (if (> number 1) gnus-not-empty-thread-mark
2643           gnus-empty-thread-mark)
2644       number)))
2645
2646 (defun gnus-summary-set-local-parameters (group)
2647   "Go through the local params of GROUP and set all variable specs in that list."
2648   (let ((params (gnus-group-find-parameter group))
2649         elem)
2650     (while params
2651       (setq elem (car params)
2652             params (cdr params))
2653       (and (consp elem)                 ; Has to be a cons.
2654            (consp (cdr elem))           ; The cdr has to be a list.
2655            (symbolp (car elem))         ; Has to be a symbol in there.
2656            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2657            (ignore-errors               ; So we set it.
2658              (make-local-variable (car elem))
2659              (set (car elem) (eval (nth 1 elem))))))))
2660
2661 (defun gnus-summary-read-group (group &optional show-all no-article
2662                                       kill-buffer no-display backward
2663                                       select-articles)
2664   "Start reading news in newsgroup GROUP.
2665 If SHOW-ALL is non-nil, already read articles are also listed.
2666 If NO-ARTICLE is non-nil, no article is selected initially.
2667 If NO-DISPLAY, don't generate a summary buffer."
2668   (let (result)
2669     (while (and group
2670                 (null (setq result
2671                             (let ((gnus-auto-select-next nil))
2672                               (or (gnus-summary-read-group-1
2673                                    group show-all no-article
2674                                    kill-buffer no-display
2675                                    select-articles)
2676                                   (setq show-all nil
2677                                         select-articles nil)))))
2678                 (eq gnus-auto-select-next 'quietly))
2679       (set-buffer gnus-group-buffer)
2680       ;; The entry function called above goes to the next
2681       ;; group automatically, so we go two groups back
2682       ;; if we are searching for the previous group.
2683       (when backward
2684         (gnus-group-prev-unread-group 2))
2685       (if (not (equal group (gnus-group-group-name)))
2686           (setq group (gnus-group-group-name))
2687         (setq group nil)))
2688     result))
2689
2690 (defun gnus-summary-read-group-1 (group show-all no-article
2691                                         kill-buffer no-display
2692                                         &optional select-articles)
2693   ;; Killed foreign groups can't be entered.
2694   (when (and (not (gnus-group-native-p group))
2695              (not (gnus-gethash group gnus-newsrc-hashtb)))
2696     (error "Dead non-native groups can't be entered"))
2697   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2698   (let* ((new-group (gnus-summary-setup-buffer group))
2699          (quit-config (gnus-group-quit-config group))
2700          (did-select (and new-group (gnus-select-newsgroup
2701                                      group show-all select-articles))))
2702     (cond
2703      ;; This summary buffer exists already, so we just select it.
2704      ((not new-group)
2705       (gnus-set-global-variables)
2706       (when kill-buffer
2707         (gnus-kill-or-deaden-summary kill-buffer))
2708       (gnus-configure-windows 'summary 'force)
2709       (gnus-set-mode-line 'summary)
2710       (gnus-summary-position-point)
2711       (message "")
2712       t)
2713      ;; We couldn't select this group.
2714      ((null did-select)
2715       (when (and (eq major-mode 'gnus-summary-mode)
2716                  (not (equal (current-buffer) kill-buffer)))
2717         (kill-buffer (current-buffer))
2718         (if (not quit-config)
2719             (progn
2720               ;; Update the info -- marks might need to be removed,
2721               ;; for instance.
2722               (gnus-summary-update-info)
2723               (set-buffer gnus-group-buffer)
2724               (gnus-group-jump-to-group group)
2725               (gnus-group-next-unread-group 1))
2726           (gnus-handle-ephemeral-exit quit-config)))
2727       (gnus-message 3 "Can't select group")
2728       nil)
2729      ;; The user did a `C-g' while prompting for number of articles,
2730      ;; so we exit this group.
2731      ((eq did-select 'quit)
2732       (and (eq major-mode 'gnus-summary-mode)
2733            (not (equal (current-buffer) kill-buffer))
2734            (kill-buffer (current-buffer)))
2735       (when kill-buffer
2736         (gnus-kill-or-deaden-summary kill-buffer))
2737       (if (not quit-config)
2738           (progn
2739             (set-buffer gnus-group-buffer)
2740             (gnus-group-jump-to-group group)
2741             (gnus-group-next-unread-group 1)
2742             (gnus-configure-windows 'group 'force))
2743         (gnus-handle-ephemeral-exit quit-config))
2744       ;; Finally signal the quit.
2745       (signal 'quit nil))
2746      ;; The group was successfully selected.
2747      (t
2748       (gnus-set-global-variables)
2749       ;; Save the active value in effect when the group was entered.
2750       (setq gnus-newsgroup-active
2751             (gnus-copy-sequence
2752              (gnus-active gnus-newsgroup-name)))
2753       ;; You can change the summary buffer in some way with this hook.
2754       (gnus-run-hooks 'gnus-select-group-hook)
2755       ;; Set any local variables in the group parameters.
2756       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2757       (gnus-update-format-specifications
2758        nil 'summary 'summary-mode 'summary-dummy)
2759       ;; Do score processing.
2760       (when gnus-use-scoring
2761         (gnus-possibly-score-headers))
2762       ;; Check whether to fill in the gaps in the threads.
2763       (when gnus-build-sparse-threads
2764         (gnus-build-sparse-threads))
2765       ;; Find the initial limit.
2766       (if gnus-show-threads
2767           (if show-all
2768               (let ((gnus-newsgroup-dormant nil))
2769                 (gnus-summary-initial-limit show-all))
2770             (gnus-summary-initial-limit show-all))
2771         (setq gnus-newsgroup-limit
2772               (mapcar
2773                (lambda (header) (mail-header-number header))
2774                gnus-newsgroup-headers)))
2775       ;; Generate the summary buffer.
2776       (unless no-display
2777         (gnus-summary-prepare))
2778       (when gnus-use-trees
2779         (gnus-tree-open group)
2780         (setq gnus-summary-highlight-line-function
2781               'gnus-tree-highlight-article))
2782       ;; If the summary buffer is empty, but there are some low-scored
2783       ;; articles or some excluded dormants, we include these in the
2784       ;; buffer.
2785       (when (and (zerop (buffer-size))
2786                  (not no-display))
2787         (cond (gnus-newsgroup-dormant
2788                (gnus-summary-limit-include-dormant))
2789               ((and gnus-newsgroup-scored show-all)
2790                (gnus-summary-limit-include-expunged t))))
2791       ;; Function `gnus-apply-kill-file' must be called in this hook.
2792       (gnus-run-hooks 'gnus-apply-kill-hook)
2793       (if (and (zerop (buffer-size))
2794                (not no-display))
2795           (progn
2796             ;; This newsgroup is empty.
2797             (gnus-summary-catchup-and-exit nil t)
2798             (gnus-message 6 "No unread news")
2799             (when kill-buffer
2800               (gnus-kill-or-deaden-summary kill-buffer))
2801             ;; Return nil from this function.
2802             nil)
2803         ;; Hide conversation thread subtrees.  We cannot do this in
2804         ;; gnus-summary-prepare-hook since kill processing may not
2805         ;; work with hidden articles.
2806         (and gnus-show-threads
2807              gnus-thread-hide-subtree
2808              (gnus-summary-hide-all-threads))
2809         (when kill-buffer
2810           (gnus-kill-or-deaden-summary kill-buffer))
2811         ;; Show first unread article if requested.
2812         (if (and (not no-article)
2813                  (not no-display)
2814                  gnus-newsgroup-unreads
2815                  gnus-auto-select-first)
2816             (progn
2817               (gnus-configure-windows 'summary)
2818               (cond
2819                ((eq gnus-auto-select-first 'best)
2820                 (gnus-summary-best-unread-article))
2821                ((eq gnus-auto-select-first t)
2822                 (gnus-summary-first-unread-article))
2823                ((gnus-functionp gnus-auto-select-first)
2824                 (funcall gnus-auto-select-first))))
2825           ;; Don't select any articles, just move point to the first
2826           ;; article in the group.
2827           (goto-char (point-min))
2828           (gnus-summary-position-point)
2829           (gnus-configure-windows 'summary 'force)
2830           (gnus-set-mode-line 'summary))
2831         (when (get-buffer-window gnus-group-buffer t)
2832           ;; Gotta use windows, because recenter does weird stuff if
2833           ;; the current buffer ain't the displayed window.
2834           (let ((owin (selected-window)))
2835             (select-window (get-buffer-window gnus-group-buffer t))
2836             (when (gnus-group-goto-group group)
2837               (recenter))
2838             (select-window owin)))
2839         ;; Mark this buffer as "prepared".
2840         (setq gnus-newsgroup-prepared t)
2841         (gnus-run-hooks 'gnus-summary-prepared-hook)
2842         t)))))
2843
2844 (defun gnus-summary-prepare ()
2845   "Generate the summary buffer."
2846   (interactive)
2847   (let ((buffer-read-only nil))
2848     (erase-buffer)
2849     (setq gnus-newsgroup-data nil
2850           gnus-newsgroup-data-reverse nil)
2851     (gnus-run-hooks 'gnus-summary-generate-hook)
2852     ;; Generate the buffer, either with threads or without.
2853     (when gnus-newsgroup-headers
2854       (gnus-summary-prepare-threads
2855        (if gnus-show-threads
2856            (gnus-sort-gathered-threads
2857             (funcall gnus-summary-thread-gathering-function
2858                      (gnus-sort-threads
2859                       (gnus-cut-threads (gnus-make-threads)))))
2860          ;; Unthreaded display.
2861          (gnus-sort-articles gnus-newsgroup-headers))))
2862     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2863     ;; Call hooks for modifying summary buffer.
2864     (goto-char (point-min))
2865     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2866
2867 (defsubst gnus-general-simplify-subject (subject)
2868   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2869   (setq subject
2870         (cond
2871          ;; Truncate the subject.
2872          (gnus-simplify-subject-functions
2873           (gnus-map-function gnus-simplify-subject-functions subject))
2874          ((numberp gnus-summary-gather-subject-limit)
2875           (setq subject (gnus-simplify-subject-re subject))
2876           (if (> (length subject) gnus-summary-gather-subject-limit)
2877               (substring subject 0 gnus-summary-gather-subject-limit)
2878             subject))
2879          ;; Fuzzily simplify it.
2880          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2881           (gnus-simplify-subject-fuzzy subject))
2882          ;; Just remove the leading "Re:".
2883          (t
2884           (gnus-simplify-subject-re subject))))
2885
2886   (if (and gnus-summary-gather-exclude-subject
2887            (string-match gnus-summary-gather-exclude-subject subject))
2888       nil                               ; This article shouldn't be gathered
2889     subject))
2890
2891 (defun gnus-summary-simplify-subject-query ()
2892   "Query where the respool algorithm would put this article."
2893   (interactive)
2894   (gnus-summary-select-article)
2895   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2896
2897 (defun gnus-gather-threads-by-subject (threads)
2898   "Gather threads by looking at Subject headers."
2899   (if (not gnus-summary-make-false-root)
2900       threads
2901     (let ((hashtb (gnus-make-hashtable 1024))
2902           (prev threads)
2903           (result threads)
2904           subject hthread whole-subject)
2905       (while threads
2906         (setq subject (gnus-general-simplify-subject
2907                        (setq whole-subject (mail-header-subject
2908                                             (caar threads)))))
2909         (when subject
2910           (if (setq hthread (gnus-gethash subject hashtb))
2911               (progn
2912                 ;; We enter a dummy root into the thread, if we
2913                 ;; haven't done that already.
2914                 (unless (stringp (caar hthread))
2915                   (setcar hthread (list whole-subject (car hthread))))
2916                 ;; We add this new gathered thread to this gathered
2917                 ;; thread.
2918                 (setcdr (car hthread)
2919                         (nconc (cdar hthread) (list (car threads))))
2920                 ;; Remove it from the list of threads.
2921                 (setcdr prev (cdr threads))
2922                 (setq threads prev))
2923             ;; Enter this thread into the hash table.
2924             (gnus-sethash subject threads hashtb)))
2925         (setq prev threads)
2926         (setq threads (cdr threads)))
2927       result)))
2928
2929 (defun gnus-gather-threads-by-references (threads)
2930   "Gather threads by looking at References headers."
2931   (let ((idhashtb (gnus-make-hashtable 1024))
2932         (thhashtb (gnus-make-hashtable 1024))
2933         (prev threads)
2934         (result threads)
2935         ids references id gthread gid entered ref)
2936     (while threads
2937       (when (setq references (mail-header-references (caar threads)))
2938         (setq id (mail-header-id (caar threads))
2939               ids (gnus-split-references references)
2940               entered nil)
2941         (while (setq ref (pop ids))
2942           (setq ids (delete ref ids))
2943           (if (not (setq gid (gnus-gethash ref idhashtb)))
2944               (progn
2945                 (gnus-sethash ref id idhashtb)
2946                 (gnus-sethash id threads thhashtb))
2947             (setq gthread (gnus-gethash gid thhashtb))
2948             (unless entered
2949               ;; We enter a dummy root into the thread, if we
2950               ;; haven't done that already.
2951               (unless (stringp (caar gthread))
2952                 (setcar gthread (list (mail-header-subject (caar gthread))
2953                                       (car gthread))))
2954               ;; We add this new gathered thread to this gathered
2955               ;; thread.
2956               (setcdr (car gthread)
2957                       (nconc (cdar gthread) (list (car threads)))))
2958             ;; Add it into the thread hash table.
2959             (gnus-sethash id gthread thhashtb)
2960             (setq entered t)
2961             ;; Remove it from the list of threads.
2962             (setcdr prev (cdr threads))
2963             (setq threads prev))))
2964       (setq prev threads)
2965       (setq threads (cdr threads)))
2966     result))
2967
2968 (defun gnus-sort-gathered-threads (threads)
2969   "Sort subtreads inside each gathered thread by article number."
2970   (let ((result threads))
2971     (while threads
2972       (when (stringp (caar threads))
2973         (setcdr (car threads)
2974                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2975       (setq threads (cdr threads)))
2976     result))
2977
2978 (defun gnus-thread-loop-p (root thread)
2979   "Say whether ROOT is in THREAD."
2980   (let ((stack (list thread))
2981         (infloop 0)
2982         th)
2983     (while (setq thread (pop stack))
2984       (setq th (cdr thread))
2985       (while (and th
2986                   (not (eq (caar th) root)))
2987         (pop th))
2988       (if th
2989           ;; We have found a loop.
2990           (let (ref-dep)
2991             (setcdr thread (delq (car th) (cdr thread)))
2992             (if (boundp (setq ref-dep (intern "none"
2993                                               gnus-newsgroup-dependencies)))
2994                 (setcdr (symbol-value ref-dep)
2995                         (nconc (cdr (symbol-value ref-dep))
2996                                (list (car th))))
2997               (set ref-dep (list nil (car th))))
2998             (setq infloop 1
2999                   stack nil))
3000         ;; Push all the subthreads onto the stack.
3001         (push (cdr thread) stack)))
3002     infloop))
3003
3004 (defun gnus-make-threads ()
3005   "Go through the dependency hashtb and find the roots.  Return all threads."
3006   (let (threads)
3007     (while (catch 'infloop
3008              (mapatoms
3009               (lambda (refs)
3010                 ;; Deal with self-referencing References loops.
3011                 (when (and (car (symbol-value refs))
3012                            (not (zerop
3013                                  (apply
3014                                   '+
3015                                   (mapcar
3016                                    (lambda (thread)
3017                                      (gnus-thread-loop-p
3018                                       (car (symbol-value refs)) thread))
3019                                    (cdr (symbol-value refs)))))))
3020                   (setq threads nil)
3021                   (throw 'infloop t))
3022                 (unless (car (symbol-value refs))
3023                   ;; These threads do not refer back to any other articles,
3024                   ;; so they're roots.
3025                   (setq threads (append (cdr (symbol-value refs)) threads))))
3026               gnus-newsgroup-dependencies)))
3027     threads))
3028
3029 ;; Build the thread tree.
3030 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3031   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3032
3033 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3034 if it was already present.
3035
3036 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3037 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3038 Message-IDs will be renamed be renamed to a unique Message-ID before
3039 being entered.
3040
3041 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3042   (let* ((id (mail-header-id header))
3043          (id-dep (and id (intern id dependencies)))
3044          ref ref-dep ref-header)
3045     ;; Enter this `header' in the `dependencies' table.
3046     (cond
3047      ((not id-dep)
3048       (setq header nil))
3049      ;; The first two cases do the normal part: enter a new `header'
3050      ;; in the `dependencies' table.
3051      ((not (boundp id-dep))
3052       (set id-dep (list header)))
3053      ((null (car (symbol-value id-dep)))
3054       (setcar (symbol-value id-dep) header))
3055
3056      ;; From here the `header' was already present in the
3057      ;; `dependencies' table.
3058      (force-new
3059       ;; Overrides an existing entry;
3060       ;; just set the header part of the entry.
3061       (setcar (symbol-value id-dep) header))
3062
3063      ;; Renames the existing `header' to a unique Message-ID.
3064      ((not gnus-summary-ignore-duplicates)
3065       ;; An article with this Message-ID has already been seen.
3066       ;; We rename the Message-ID.
3067       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3068            (list header))
3069       (mail-header-set-id header id))
3070
3071      ;; The last case ignores an existing entry, except it adds any
3072      ;; additional Xrefs (in case the two articles came from different
3073      ;; servers.
3074      ;; Also sets `header' to `nil' meaning that the `dependencies'
3075      ;; table was *not* modified.
3076      (t
3077       (mail-header-set-xref
3078        (car (symbol-value id-dep))
3079        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3080                    "")
3081                (or (mail-header-xref header) "")))
3082       (setq header nil)))
3083
3084     (when header
3085       ;; First check if that we are not creating a References loop.
3086       (setq ref (gnus-parent-id (mail-header-references header)))
3087       (while (and ref
3088                   (setq ref-dep (intern-soft ref dependencies))
3089                   (boundp ref-dep)
3090                   (setq ref-header (car (symbol-value ref-dep))))
3091         (if (string= id ref)
3092             ;; Yuk!  This is a reference loop.  Make the article be a
3093             ;; root article.
3094             (progn
3095               (mail-header-set-references (car (symbol-value id-dep)) "none")
3096               (setq ref nil))
3097           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3098       (setq ref (gnus-parent-id (mail-header-references header)))
3099       (setq ref-dep (intern (or ref "none") dependencies))
3100       (if (boundp ref-dep)
3101           (setcdr (symbol-value ref-dep)
3102                   (nconc (cdr (symbol-value ref-dep))
3103                          (list (symbol-value id-dep))))
3104         (set ref-dep (list nil (symbol-value id-dep)))))
3105     header))
3106
3107 (defun gnus-build-sparse-threads ()
3108   (let ((headers gnus-newsgroup-headers)
3109         (gnus-summary-ignore-duplicates t)
3110         header references generation relations
3111         subject child end new-child date)
3112     ;; First we create an alist of generations/relations, where
3113     ;; generations is how much we trust the relation, and the relation
3114     ;; is parent/child.
3115     (gnus-message 7 "Making sparse threads...")
3116     (save-excursion
3117       (nnheader-set-temp-buffer " *gnus sparse threads*")
3118       (while (setq header (pop headers))
3119         (when (and (setq references (mail-header-references header))
3120                    (not (string= references "")))
3121           (insert references)
3122           (setq child (mail-header-id header)
3123                 subject (mail-header-subject header)
3124                 date (mail-header-date header)
3125                 generation 0)
3126           (while (search-backward ">" nil t)
3127             (setq end (1+ (point)))
3128             (when (search-backward "<" nil t)
3129               (setq new-child (buffer-substring (point) end))
3130               (push (list (incf generation)
3131                           child (setq child new-child)
3132                           subject date)
3133                     relations)))
3134           (when child
3135             (push (list (1+ generation) child nil subject) relations))
3136           (erase-buffer)))
3137       (kill-buffer (current-buffer)))
3138     ;; Sort over trustworthiness.
3139     (mapcar
3140      (lambda (relation)
3141        (when (gnus-dependencies-add-header
3142               (make-full-mail-header
3143                gnus-reffed-article-number
3144                (nth 3 relation) "" (or (nth 4 relation) "")
3145                (nth 1 relation)
3146                (or (nth 2 relation) "") 0 0 "")
3147               gnus-newsgroup-dependencies nil)
3148          (push gnus-reffed-article-number gnus-newsgroup-limit)
3149          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3150          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3151                gnus-newsgroup-reads)
3152          (decf gnus-reffed-article-number)))
3153      (sort relations 'car-less-than-car))
3154     (gnus-message 7 "Making sparse threads...done")))
3155
3156 (defun gnus-build-old-threads ()
3157   ;; Look at all the articles that refer back to old articles, and
3158   ;; fetch the headers for the articles that aren't there.  This will
3159   ;; build complete threads - if the roots haven't been expired by the
3160   ;; server, that is.
3161   (let (id heads)
3162     (mapatoms
3163      (lambda (refs)
3164        (when (not (car (symbol-value refs)))
3165          (setq heads (cdr (symbol-value refs)))
3166          (while heads
3167            (if (memq (mail-header-number (caar heads))
3168                      gnus-newsgroup-dormant)
3169                (setq heads (cdr heads))
3170              (setq id (symbol-name refs))
3171              (while (and (setq id (gnus-build-get-header id))
3172                          (not (car (gnus-id-to-thread id)))))
3173              (setq heads nil)))))
3174      gnus-newsgroup-dependencies)))
3175
3176 (defmacro gnus-nov-read-integer ()
3177   '(prog1
3178        (if (eq (char-after) ?\t)
3179            0
3180          (let ((num (ignore-errors (read buffer))))
3181            (if (numberp num) num 0)))
3182      (unless (eobp)
3183        (search-forward "\t" eol 'move))))
3184
3185 (defmacro gnus-nov-skip-field ()
3186   '(search-forward "\t" eol 'move))
3187
3188 (defmacro gnus-nov-field ()
3189   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3190
3191 (defmacro gnus-nov-parse-extra ()
3192   '(let (out string)
3193      (while (not (memq (char-after) '(?\n nil)))
3194        (setq string (gnus-nov-field))
3195        (when (string-match "^\\([^ :]+\\): " string)
3196          (push (cons (intern (match-string 1 string))
3197                      (substring string (match-end 0)))
3198                out)))
3199      out))
3200
3201 ;; This function has to be called with point after the article number
3202 ;; on the beginning of the line.
3203 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3204   (let ((eol (gnus-point-at-eol))
3205         (buffer (current-buffer))
3206         header)
3207
3208     ;; overview: [num subject from date id refs chars lines misc]
3209     (unwind-protect
3210         (progn
3211           (narrow-to-region (point) eol)
3212           (unless (eobp)
3213             (forward-char))
3214
3215           (setq header
3216                 (make-full-mail-header
3217                  number                 ; number
3218                  (funcall gnus-decode-encoded-word-function
3219                           (gnus-nov-field)) ; subject
3220                  (funcall gnus-decode-encoded-word-function
3221                           (gnus-nov-field)) ; from
3222                  (gnus-nov-field)       ; date
3223                  (or (gnus-nov-field)
3224                      (nnheader-generate-fake-message-id)) ; id
3225                  (gnus-nov-field)       ; refs
3226                  (gnus-nov-read-integer) ; chars
3227                  (gnus-nov-read-integer) ; lines
3228                  (unless (eq (char-after) ?\n)
3229                    (gnus-nov-field))    ; misc
3230                  (gnus-nov-parse-extra)))) ; extra
3231
3232       (widen))
3233
3234     (when gnus-alter-header-function
3235       (funcall gnus-alter-header-function header))
3236     (gnus-dependencies-add-header header dependencies force-new)))
3237
3238 (defun gnus-build-get-header (id)
3239   ;; Look through the buffer of NOV lines and find the header to
3240   ;; ID.  Enter this line into the dependencies hash table, and return
3241   ;; the id of the parent article (if any).
3242   (let ((deps gnus-newsgroup-dependencies)
3243         found header)
3244     (prog1
3245         (save-excursion
3246           (set-buffer nntp-server-buffer)
3247           (let ((case-fold-search nil))
3248             (goto-char (point-min))
3249             (while (and (not found)
3250                         (search-forward id nil t))
3251               (beginning-of-line)
3252               (setq found (looking-at
3253                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3254                                    (regexp-quote id))))
3255               (or found (beginning-of-line 2)))
3256             (when found
3257               (beginning-of-line)
3258               (and
3259                (setq header (gnus-nov-parse-line
3260                              (read (current-buffer)) deps))
3261                (gnus-parent-id (mail-header-references header))))))
3262       (when header
3263         (let ((number (mail-header-number header)))
3264           (push number gnus-newsgroup-limit)
3265           (push header gnus-newsgroup-headers)
3266           (if (memq number gnus-newsgroup-unselected)
3267               (progn
3268                 (push number gnus-newsgroup-unreads)
3269                 (setq gnus-newsgroup-unselected
3270                       (delq number gnus-newsgroup-unselected)))
3271             (push number gnus-newsgroup-ancient)))))))
3272
3273 (defun gnus-build-all-threads ()
3274   "Read all the headers."
3275   (let ((gnus-summary-ignore-duplicates t)
3276         (dependencies gnus-newsgroup-dependencies)
3277         header article)
3278     (save-excursion
3279       (set-buffer nntp-server-buffer)
3280       (let ((case-fold-search nil))
3281         (goto-char (point-min))
3282         (while (not (eobp))
3283           (ignore-errors
3284             (setq article (read (current-buffer))
3285                   header (gnus-nov-parse-line
3286                           article dependencies)))
3287           (when header
3288             (save-excursion
3289               (set-buffer gnus-summary-buffer)
3290               (push header gnus-newsgroup-headers)
3291               (if (memq (setq article (mail-header-number header))
3292                         gnus-newsgroup-unselected)
3293                   (progn
3294                     (push article gnus-newsgroup-unreads)
3295                     (setq gnus-newsgroup-unselected
3296                           (delq article gnus-newsgroup-unselected)))
3297                 (push article gnus-newsgroup-ancient)))
3298             (forward-line 1)))))))
3299
3300 (defun gnus-summary-update-article-line (article header)
3301   "Update the line for ARTICLE using HEADERS."
3302   (let* ((id (mail-header-id header))
3303          (thread (gnus-id-to-thread id)))
3304     (unless thread
3305       (error "Article in no thread"))
3306     ;; Update the thread.
3307     (setcar thread header)
3308     (gnus-summary-goto-subject article)
3309     (let* ((datal (gnus-data-find-list article))
3310            (data (car datal))
3311            (length (when (cdr datal)
3312                      (- (gnus-data-pos data)
3313                         (gnus-data-pos (cadr datal)))))
3314            (buffer-read-only nil)
3315            (level (gnus-summary-thread-level)))
3316       (gnus-delete-line)
3317       (gnus-summary-insert-line
3318        header level nil (gnus-article-mark article)
3319        (memq article gnus-newsgroup-replied)
3320        (memq article gnus-newsgroup-expirable)
3321        ;; Only insert the Subject string when it's different
3322        ;; from the previous Subject string.
3323        (if (gnus-subject-equal
3324             (condition-case ()
3325                 (mail-header-subject
3326                  (gnus-data-header
3327                   (cadr
3328                    (gnus-data-find-list
3329                     article
3330                     (gnus-data-list t)))))
3331               ;; Error on the side of excessive subjects.
3332               (error ""))
3333             (mail-header-subject header))
3334            ""
3335          (mail-header-subject header))
3336        nil (cdr (assq article gnus-newsgroup-scored))
3337        (memq article gnus-newsgroup-processable))
3338       (when length
3339         (gnus-data-update-list
3340          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3341
3342 (defun gnus-summary-update-article (article &optional iheader)
3343   "Update ARTICLE in the summary buffer."
3344   (set-buffer gnus-summary-buffer)
3345   (let* ((header (gnus-summary-article-header article))
3346          (id (mail-header-id header))
3347          (data (gnus-data-find article))
3348          (thread (gnus-id-to-thread id))
3349          (references (mail-header-references header))
3350          (parent
3351           (gnus-id-to-thread
3352            (or (gnus-parent-id
3353                 (when (and references
3354                            (not (equal "" references)))
3355                   references))
3356                "none")))
3357          (buffer-read-only nil)
3358          (old (car thread)))
3359     (when thread
3360       (unless iheader
3361         (setcar thread nil)
3362         (when parent
3363           (delq thread parent)))
3364       (if (gnus-summary-insert-subject id header)
3365           ;; Set the (possibly) new article number in the data structure.
3366           (gnus-data-set-number data (gnus-id-to-article id))
3367         (setcar thread old)
3368         nil))))
3369
3370 (defun gnus-rebuild-thread (id &optional line)
3371   "Rebuild the thread containing ID.
3372 If LINE, insert the rebuilt thread starting on line LINE."
3373   (let ((buffer-read-only nil)
3374         old-pos current thread data)
3375     (if (not gnus-show-threads)
3376         (setq thread (list (car (gnus-id-to-thread id))))
3377       ;; Get the thread this article is part of.
3378       (setq thread (gnus-remove-thread id)))
3379     (setq old-pos (gnus-point-at-bol))
3380     (setq current (save-excursion
3381                     (and (zerop (forward-line -1))
3382                          (gnus-summary-article-number))))
3383     ;; If this is a gathered thread, we have to go some re-gathering.
3384     (when (stringp (car thread))
3385       (let ((subject (car thread))
3386             roots thr)
3387         (setq thread (cdr thread))
3388         (while thread
3389           (unless (memq (setq thr (gnus-id-to-thread
3390                                    (gnus-root-id
3391                                     (mail-header-id (caar thread)))))
3392                         roots)
3393             (push thr roots))
3394           (setq thread (cdr thread)))
3395         ;; We now have all (unique) roots.
3396         (if (= (length roots) 1)
3397             ;; All the loose roots are now one solid root.
3398             (setq thread (car roots))
3399           (setq thread (cons subject (gnus-sort-threads roots))))))
3400     (let (threads)
3401       ;; We then insert this thread into the summary buffer.
3402       (when line
3403         (goto-char (point-min))
3404         (forward-line (1- line)))
3405       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3406         (if gnus-show-threads
3407             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3408           (gnus-summary-prepare-unthreaded thread))
3409         (setq data (nreverse gnus-newsgroup-data))
3410         (setq threads gnus-newsgroup-threads))
3411       ;; We splice the new data into the data structure.
3412       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3413       ;;!!! then we want to insert at the beginning of the buffer.
3414       ;;!!! That happens to be true with Gnus now, but that may
3415       ;;!!! change in the future.  Perhaps.
3416       (gnus-data-enter-list
3417        (if line nil current) data (- (point) old-pos))
3418       (setq gnus-newsgroup-threads
3419             (nconc threads gnus-newsgroup-threads))
3420       (gnus-data-compute-positions))))
3421
3422 (defun gnus-number-to-header (number)
3423   "Return the header for article NUMBER."
3424   (let ((headers gnus-newsgroup-headers))
3425     (while (and headers
3426                 (not (= number (mail-header-number (car headers)))))
3427       (pop headers))
3428     (when headers
3429       (car headers))))
3430
3431 (defun gnus-parent-headers (in-headers &optional generation)
3432   "Return the headers of the GENERATIONeth parent of HEADERS."
3433   (unless generation
3434     (setq generation 1))
3435   (let ((parent t)
3436         (headers in-headers)
3437         references)
3438     (while (and parent
3439                 (not (zerop generation))
3440                 (setq references (mail-header-references headers)))
3441       (setq headers (if (and references
3442                              (setq parent (gnus-parent-id references)))
3443                         (car (gnus-id-to-thread parent))
3444                       nil))
3445       (decf generation))
3446     (and (not (eq headers in-headers))
3447          headers)))
3448
3449 (defun gnus-id-to-thread (id)
3450   "Return the (sub-)thread where ID appears."
3451   (gnus-gethash id gnus-newsgroup-dependencies))
3452
3453 (defun gnus-id-to-article (id)
3454   "Return the article number of ID."
3455   (let ((thread (gnus-id-to-thread id)))
3456     (when (and thread
3457                (car thread))
3458       (mail-header-number (car thread)))))
3459
3460 (defun gnus-id-to-header (id)
3461   "Return the article headers of ID."
3462   (car (gnus-id-to-thread id)))
3463
3464 (defun gnus-article-displayed-root-p (article)
3465   "Say whether ARTICLE is a root(ish) article."
3466   (let ((level (gnus-summary-thread-level article))
3467         (refs (mail-header-references  (gnus-summary-article-header article)))
3468         particle)
3469     (cond
3470      ((null level) nil)
3471      ((zerop level) t)
3472      ((null refs) t)
3473      ((null (gnus-parent-id refs)) t)
3474      ((and (= 1 level)
3475            (null (setq particle (gnus-id-to-article
3476                                  (gnus-parent-id refs))))
3477            (null (gnus-summary-thread-level particle)))))))
3478
3479 (defun gnus-root-id (id)
3480   "Return the id of the root of the thread where ID appears."
3481   (let (last-id prev)
3482     (while (and id (setq prev (car (gnus-id-to-thread id))))
3483       (setq last-id id
3484             id (gnus-parent-id (mail-header-references prev))))
3485     last-id))
3486
3487 (defun gnus-articles-in-thread (thread)
3488   "Return the list of articles in THREAD."
3489   (cons (mail-header-number (car thread))
3490         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3491
3492 (defun gnus-remove-thread (id &optional dont-remove)
3493   "Remove the thread that has ID in it."
3494   (let (headers thread last-id)
3495     ;; First go up in this thread until we find the root.
3496     (setq last-id (gnus-root-id id)
3497           headers (message-flatten-list (gnus-id-to-thread last-id)))
3498     ;; We have now found the real root of this thread.  It might have
3499     ;; been gathered into some loose thread, so we have to search
3500     ;; through the threads to find the thread we wanted.
3501     (let ((threads gnus-newsgroup-threads)
3502           sub)
3503       (while threads
3504         (setq sub (car threads))
3505         (if (stringp (car sub))
3506             ;; This is a gathered thread, so we look at the roots
3507             ;; below it to find whether this article is in this
3508             ;; gathered root.
3509             (progn
3510               (setq sub (cdr sub))
3511               (while sub
3512                 (when (member (caar sub) headers)
3513                   (setq thread (car threads)
3514                         threads nil
3515                         sub nil))
3516                 (setq sub (cdr sub))))
3517           ;; It's an ordinary thread, so we check it.
3518           (when (eq (car sub) (car headers))
3519             (setq thread sub
3520                   threads nil)))
3521         (setq threads (cdr threads)))
3522       ;; If this article is in no thread, then it's a root.
3523       (if thread
3524           (unless dont-remove
3525             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3526         (setq thread (gnus-id-to-thread last-id)))
3527       (when thread
3528         (prog1
3529             thread                      ; We return this thread.
3530           (unless dont-remove
3531             (if (stringp (car thread))
3532                 (progn
3533                   ;; If we use dummy roots, then we have to remove the
3534                   ;; dummy root as well.
3535                   (when (eq gnus-summary-make-false-root 'dummy)
3536                     ;; We go to the dummy root by going to
3537                     ;; the first sub-"thread", and then one line up.
3538                     (gnus-summary-goto-article
3539                      (mail-header-number (caadr thread)))
3540                     (forward-line -1)
3541                     (gnus-delete-line)
3542                     (gnus-data-compute-positions))
3543                   (setq thread (cdr thread))
3544                   (while thread
3545                     (gnus-remove-thread-1 (car thread))
3546                     (setq thread (cdr thread))))
3547               (gnus-summary-show-all-threads)
3548               (gnus-remove-thread-1 thread))))))))
3549
3550 (defun gnus-remove-thread-1 (thread)
3551   "Remove the thread THREAD recursively."
3552   (let ((number (mail-header-number (pop thread)))
3553         d)
3554     (setq thread (reverse thread))
3555     (while thread
3556       (gnus-remove-thread-1 (pop thread)))
3557     (when (setq d (gnus-data-find number))
3558       (goto-char (gnus-data-pos d))
3559       (gnus-data-remove
3560        number
3561        (- (gnus-point-at-bol)
3562           (prog1
3563               (1+ (gnus-point-at-eol))
3564             (gnus-delete-line)))))))
3565
3566 (defun gnus-sort-threads (threads)
3567   "Sort THREADS."
3568   (if (not gnus-thread-sort-functions)
3569       threads
3570     (gnus-message 8 "Sorting threads...")
3571     (prog1
3572         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3573       (gnus-message 8 "Sorting threads...done"))))
3574
3575 (defun gnus-sort-articles (articles)
3576   "Sort ARTICLES."
3577   (when gnus-article-sort-functions
3578     (gnus-message 7 "Sorting articles...")
3579     (prog1
3580         (setq gnus-newsgroup-headers
3581               (sort articles (gnus-make-sort-function
3582                               gnus-article-sort-functions)))
3583       (gnus-message 7 "Sorting articles...done"))))
3584
3585 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3586 (defmacro gnus-thread-header (thread)
3587   ;; Return header of first article in THREAD.
3588   ;; Note that THREAD must never, ever be anything else than a variable -
3589   ;; using some other form will lead to serious barfage.
3590   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3591   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3592   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3593         (vector thread) 2))
3594
3595 (defsubst gnus-article-sort-by-number (h1 h2)
3596   "Sort articles by article number."
3597   (< (mail-header-number h1)
3598      (mail-header-number h2)))
3599
3600 (defun gnus-thread-sort-by-number (h1 h2)
3601   "Sort threads by root article number."
3602   (gnus-article-sort-by-number
3603    (gnus-thread-header h1) (gnus-thread-header h2)))
3604
3605 (defsubst gnus-article-sort-by-lines (h1 h2)
3606   "Sort articles by article Lines header."
3607   (< (mail-header-lines h1)
3608      (mail-header-lines h2)))
3609
3610 (defun gnus-thread-sort-by-lines (h1 h2)
3611   "Sort threads by root article Lines header."
3612   (gnus-article-sort-by-lines
3613    (gnus-thread-header h1) (gnus-thread-header h2)))
3614
3615 (defsubst gnus-article-sort-by-author (h1 h2)
3616   "Sort articles by root author."
3617   (string-lessp
3618    (let ((extract (funcall
3619                    gnus-extract-address-components
3620                    (mail-header-from h1))))
3621      (or (car extract) (cadr extract) ""))
3622    (let ((extract (funcall
3623                    gnus-extract-address-components
3624                    (mail-header-from h2))))
3625      (or (car extract) (cadr extract) ""))))
3626
3627 (defun gnus-thread-sort-by-author (h1 h2)
3628   "Sort threads by root author."
3629   (gnus-article-sort-by-author
3630    (gnus-thread-header h1)  (gnus-thread-header h2)))
3631
3632 (defsubst gnus-article-sort-by-subject (h1 h2)
3633   "Sort articles by root subject."
3634   (string-lessp
3635    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3636    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3637
3638 (defun gnus-thread-sort-by-subject (h1 h2)
3639   "Sort threads by root subject."
3640   (gnus-article-sort-by-subject
3641    (gnus-thread-header h1) (gnus-thread-header h2)))
3642
3643 (defsubst gnus-article-sort-by-date (h1 h2)
3644   "Sort articles by root article date."
3645   (time-less-p
3646    (gnus-date-get-time (mail-header-date h1))
3647    (gnus-date-get-time (mail-header-date h2))))
3648
3649 (defun gnus-thread-sort-by-date (h1 h2)
3650   "Sort threads by root article date."
3651   (gnus-article-sort-by-date
3652    (gnus-thread-header h1) (gnus-thread-header h2)))
3653
3654 (defsubst gnus-article-sort-by-score (h1 h2)
3655   "Sort articles by root article score.
3656 Unscored articles will be counted as having a score of zero."
3657   (> (or (cdr (assq (mail-header-number h1)
3658                     gnus-newsgroup-scored))
3659          gnus-summary-default-score 0)
3660      (or (cdr (assq (mail-header-number h2)
3661                     gnus-newsgroup-scored))
3662          gnus-summary-default-score 0)))
3663
3664 (defun gnus-thread-sort-by-score (h1 h2)
3665   "Sort threads by root article score."
3666   (gnus-article-sort-by-score
3667    (gnus-thread-header h1) (gnus-thread-header h2)))
3668
3669 (defun gnus-thread-sort-by-total-score (h1 h2)
3670   "Sort threads by the sum of all scores in the thread.
3671 Unscored articles will be counted as having a score of zero."
3672   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3673
3674 (defun gnus-thread-total-score (thread)
3675   ;;  This function find the total score of THREAD.
3676   (cond ((null thread)
3677          0)
3678         ((consp thread)
3679          (if (stringp (car thread))
3680              (apply gnus-thread-score-function 0
3681                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3682            (gnus-thread-total-score-1 thread)))
3683         (t
3684          (gnus-thread-total-score-1 (list thread)))))
3685
3686 (defun gnus-thread-total-score-1 (root)
3687   ;; This function find the total score of the thread below ROOT.
3688   (setq root (car root))
3689   (apply gnus-thread-score-function
3690          (or (append
3691               (mapcar 'gnus-thread-total-score
3692                       (cdr (gnus-id-to-thread (mail-header-id root))))
3693               (when (> (mail-header-number root) 0)
3694                 (list (or (cdr (assq (mail-header-number root)
3695                                      gnus-newsgroup-scored))
3696                           gnus-summary-default-score 0))))
3697              (list gnus-summary-default-score)
3698              '(0))))
3699
3700 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3701 (defvar gnus-tmp-prev-subject nil)
3702 (defvar gnus-tmp-false-parent nil)
3703 (defvar gnus-tmp-root-expunged nil)
3704 (defvar gnus-tmp-dummy-line nil)
3705
3706 (defvar gnus-tmp-header)
3707 (defun gnus-extra-header (type &optional header)
3708   "Return the extra header of TYPE."
3709   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3710       ""))
3711
3712 (defun gnus-summary-prepare-threads (threads)
3713   "Prepare summary buffer from THREADS and indentation LEVEL.
3714 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3715 or a straight list of headers."
3716   (gnus-message 7 "Generating summary...")
3717
3718   (setq gnus-newsgroup-threads threads)
3719   (beginning-of-line)
3720
3721   (let ((gnus-tmp-level 0)
3722         (default-score (or gnus-summary-default-score 0))
3723         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3724         thread number subject stack state gnus-tmp-gathered beg-match
3725         new-roots gnus-tmp-new-adopts thread-end
3726         gnus-tmp-header gnus-tmp-unread
3727         gnus-tmp-replied gnus-tmp-subject-or-nil
3728         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3729         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3730         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3731
3732     (setq gnus-tmp-prev-subject nil)
3733
3734     (if (vectorp (car threads))
3735         ;; If this is a straight (sic) list of headers, then a
3736         ;; threaded summary display isn't required, so we just create
3737         ;; an unthreaded one.
3738         (gnus-summary-prepare-unthreaded threads)
3739
3740       ;; Do the threaded display.
3741
3742       (while (or threads stack gnus-tmp-new-adopts new-roots)
3743
3744         (if (and (= gnus-tmp-level 0)
3745                  (or (not stack)
3746                      (= (caar stack) 0))
3747                  (not gnus-tmp-false-parent)
3748                  (or gnus-tmp-new-adopts new-roots))
3749             (if gnus-tmp-new-adopts
3750                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3751                       thread (list (car gnus-tmp-new-adopts))
3752                       gnus-tmp-header (caar thread)
3753                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3754               (when new-roots
3755                 (setq thread (list (car new-roots))
3756                       gnus-tmp-header (caar thread)
3757                       new-roots (cdr new-roots))))
3758
3759           (if threads
3760               ;; If there are some threads, we do them before the
3761               ;; threads on the stack.
3762               (setq thread threads
3763                     gnus-tmp-header (caar thread))
3764             ;; There were no current threads, so we pop something off
3765             ;; the stack.
3766             (setq state (car stack)
3767                   gnus-tmp-level (car state)
3768                   thread (cdr state)
3769                   stack (cdr stack)
3770                   gnus-tmp-header (caar thread))))
3771
3772         (setq gnus-tmp-false-parent nil)
3773         (setq gnus-tmp-root-expunged nil)
3774         (setq thread-end nil)
3775
3776         (if (stringp gnus-tmp-header)
3777             ;; The header is a dummy root.
3778             (cond
3779              ((eq gnus-summary-make-false-root 'adopt)
3780               ;; We let the first article adopt the rest.
3781               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3782                                                (cddar thread)))
3783               (setq gnus-tmp-gathered
3784                     (nconc (mapcar
3785                             (lambda (h) (mail-header-number (car h)))
3786                             (cddar thread))
3787                            gnus-tmp-gathered))
3788               (setq thread (cons (list (caar thread)
3789                                        (cadar thread))
3790                                  (cdr thread)))
3791               (setq gnus-tmp-level -1
3792                     gnus-tmp-false-parent t))
3793              ((eq gnus-summary-make-false-root 'empty)
3794               ;; We print adopted articles with empty subject fields.
3795               (setq gnus-tmp-gathered
3796                     (nconc (mapcar
3797                             (lambda (h) (mail-header-number (car h)))
3798                             (cddar thread))
3799                            gnus-tmp-gathered))
3800               (setq gnus-tmp-level -1))
3801              ((eq gnus-summary-make-false-root 'dummy)
3802               ;; We remember that we probably want to output a dummy
3803               ;; root.
3804               (setq gnus-tmp-dummy-line gnus-tmp-header)
3805               (setq gnus-tmp-prev-subject gnus-tmp-header))
3806              (t
3807               ;; We do not make a root for the gathered
3808               ;; sub-threads at all.
3809               (setq gnus-tmp-level -1)))
3810
3811           (setq number (mail-header-number gnus-tmp-header)
3812                 subject (mail-header-subject gnus-tmp-header))
3813
3814           (cond
3815            ;; If the thread has changed subject, we might want to make
3816            ;; this subthread into a root.
3817            ((and (null gnus-thread-ignore-subject)
3818                  (not (zerop gnus-tmp-level))
3819                  gnus-tmp-prev-subject
3820                  (not (inline
3821                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3822             (setq new-roots (nconc new-roots (list (car thread)))
3823                   thread-end t
3824                   gnus-tmp-header nil))
3825            ;; If the article lies outside the current limit,
3826            ;; then we do not display it.
3827            ((not (memq number gnus-newsgroup-limit))
3828             (setq gnus-tmp-gathered
3829                   (nconc (mapcar
3830                           (lambda (h) (mail-header-number (car h)))
3831                           (cdar thread))
3832                          gnus-tmp-gathered))
3833             (setq gnus-tmp-new-adopts (if (cdar thread)
3834                                           (append gnus-tmp-new-adopts
3835                                                   (cdar thread))
3836                                         gnus-tmp-new-adopts)
3837                   thread-end t
3838                   gnus-tmp-header nil)
3839             (when (zerop gnus-tmp-level)
3840               (setq gnus-tmp-root-expunged t)))
3841            ;; Perhaps this article is to be marked as read?
3842            ((and gnus-summary-mark-below
3843                  (< (or (cdr (assq number gnus-newsgroup-scored))
3844                         default-score)
3845                     gnus-summary-mark-below)
3846                  ;; Don't touch sparse articles.
3847                  (not (gnus-summary-article-sparse-p number))
3848                  (not (gnus-summary-article-ancient-p number)))
3849             (setq gnus-newsgroup-unreads
3850                   (delq number gnus-newsgroup-unreads))
3851             (if gnus-newsgroup-auto-expire
3852                 (push number gnus-newsgroup-expirable)
3853               (push (cons number gnus-low-score-mark)
3854                     gnus-newsgroup-reads))))
3855
3856           (when gnus-tmp-header
3857             ;; We may have an old dummy line to output before this
3858             ;; article.
3859             (when (and gnus-tmp-dummy-line
3860                        (gnus-subject-equal
3861                         gnus-tmp-dummy-line
3862                         (mail-header-subject gnus-tmp-header)))
3863               (gnus-summary-insert-dummy-line
3864                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3865               (setq gnus-tmp-dummy-line nil))
3866
3867             ;; Compute the mark.
3868             (setq gnus-tmp-unread (gnus-article-mark number))
3869
3870             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3871                                   gnus-tmp-header gnus-tmp-level)
3872                   gnus-newsgroup-data)
3873
3874             ;; Actually insert the line.
3875             (setq
3876              gnus-tmp-subject-or-nil
3877              (cond
3878               ((and gnus-thread-ignore-subject
3879                     gnus-tmp-prev-subject
3880                     (not (inline (gnus-subject-equal
3881                                   gnus-tmp-prev-subject subject))))
3882                subject)
3883               ((zerop gnus-tmp-level)
3884                (if (and (eq gnus-summary-make-false-root 'empty)
3885                         (memq number gnus-tmp-gathered)
3886                         gnus-tmp-prev-subject
3887                         (inline (gnus-subject-equal
3888                                  gnus-tmp-prev-subject subject)))
3889                    gnus-summary-same-subject
3890                  subject))
3891               (t gnus-summary-same-subject)))
3892             (if (and (eq gnus-summary-make-false-root 'adopt)
3893                      (= gnus-tmp-level 1)
3894                      (memq number gnus-tmp-gathered))
3895                 (setq gnus-tmp-opening-bracket ?\<
3896                       gnus-tmp-closing-bracket ?\>)
3897               (setq gnus-tmp-opening-bracket ?\[
3898                     gnus-tmp-closing-bracket ?\]))
3899             (setq
3900              gnus-tmp-indentation
3901              (aref gnus-thread-indent-array gnus-tmp-level)
3902              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3903              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3904                                 gnus-summary-default-score 0)
3905              gnus-tmp-score-char
3906              (if (or (null gnus-summary-default-score)
3907                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3908                          gnus-summary-zcore-fuzz))
3909                  ? 
3910                (if (< gnus-tmp-score gnus-summary-default-score)
3911                    gnus-score-below-mark gnus-score-over-mark))
3912              gnus-tmp-replied
3913              (cond ((memq number gnus-newsgroup-processable)
3914                     gnus-process-mark)
3915                    ((memq number gnus-newsgroup-cached)
3916                     gnus-cached-mark)
3917                    ((memq number gnus-newsgroup-replied)
3918                     gnus-replied-mark)
3919                    ((memq number gnus-newsgroup-saved)
3920                     gnus-saved-mark)
3921                    (t gnus-unread-mark))
3922              gnus-tmp-from (mail-header-from gnus-tmp-header)
3923              gnus-tmp-name
3924              (cond
3925               ((string-match "<[^>]+> *$" gnus-tmp-from)
3926                (setq beg-match (match-beginning 0))
3927                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3928                         (substring gnus-tmp-from (1+ (match-beginning 0))
3929                                    (1- (match-end 0))))
3930                    (substring gnus-tmp-from 0 beg-match)))
3931               ((string-match "(.+)" gnus-tmp-from)
3932                (substring gnus-tmp-from
3933                           (1+ (match-beginning 0)) (1- (match-end 0))))
3934               (t gnus-tmp-from)))
3935             (when (string= gnus-tmp-name "")
3936               (setq gnus-tmp-name gnus-tmp-from))
3937             (unless (numberp gnus-tmp-lines)
3938               (setq gnus-tmp-lines 0))
3939             (gnus-put-text-property
3940              (point)
3941              (progn (eval gnus-summary-line-format-spec) (point))
3942              'gnus-number number)
3943             (when gnus-visual-p
3944               (forward-line -1)
3945               (gnus-run-hooks 'gnus-summary-update-hook)
3946               (forward-line 1))
3947
3948             (setq gnus-tmp-prev-subject subject)))
3949
3950         (when (nth 1 thread)
3951           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3952         (incf gnus-tmp-level)
3953         (setq threads (if thread-end nil (cdar thread)))
3954         (unless threads
3955           (setq gnus-tmp-level 0)))))
3956   (gnus-message 7 "Generating summary...done"))
3957
3958 (defun gnus-summary-prepare-unthreaded (headers)
3959   "Generate an unthreaded summary buffer based on HEADERS."
3960   (let (header number mark)
3961
3962     (beginning-of-line)
3963
3964     (while headers
3965       ;; We may have to root out some bad articles...
3966       (when (memq (setq number (mail-header-number
3967                                 (setq header (pop headers))))
3968                   gnus-newsgroup-limit)
3969         ;; Mark article as read when it has a low score.
3970         (when (and gnus-summary-mark-below
3971                    (< (or (cdr (assq number gnus-newsgroup-scored))
3972                           gnus-summary-default-score 0)
3973                       gnus-summary-mark-below)
3974                    (not (gnus-summary-article-ancient-p number)))
3975           (setq gnus-newsgroup-unreads
3976                 (delq number gnus-newsgroup-unreads))
3977           (if gnus-newsgroup-auto-expire
3978               (push number gnus-newsgroup-expirable)
3979             (push (cons number gnus-low-score-mark)
3980                   gnus-newsgroup-reads)))
3981
3982         (setq mark (gnus-article-mark number))
3983         (push (gnus-data-make number mark (1+ (point)) header 0)
3984               gnus-newsgroup-data)
3985         (gnus-summary-insert-line
3986          header 0 number
3987          mark (memq number gnus-newsgroup-replied)
3988          (memq number gnus-newsgroup-expirable)
3989          (mail-header-subject header) nil
3990          (cdr (assq number gnus-newsgroup-scored))
3991          (memq number gnus-newsgroup-processable))))))
3992
3993 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3994   "Select newsgroup GROUP.
3995 If READ-ALL is non-nil, all articles in the group are selected.
3996 If SELECT-ARTICLES, only select those articles from GROUP."
3997   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3998          ;;!!! Dirty hack; should be removed.
3999          (gnus-summary-ignore-duplicates
4000           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4001               t
4002             gnus-summary-ignore-duplicates))
4003          (info (nth 2 entry))
4004          articles fetched-articles cached)
4005
4006     (unless (gnus-check-server
4007              (setq gnus-current-select-method
4008                    (gnus-find-method-for-group group)))
4009       (error "Couldn't open server"))
4010
4011     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4012         (gnus-activate-group group)     ; Or we can activate it...
4013         (progn                          ; Or we bug out.
4014           (when (equal major-mode 'gnus-summary-mode)
4015             (kill-buffer (current-buffer)))
4016           (error "Couldn't request group %s: %s"
4017                  group (gnus-status-message group))))
4018
4019     (unless (gnus-request-group group t)
4020       (when (equal major-mode 'gnus-summary-mode)
4021         (kill-buffer (current-buffer)))
4022       (error "Couldn't request group %s: %s"
4023              group (gnus-status-message group)))
4024
4025     (setq gnus-newsgroup-name group)
4026     (setq gnus-newsgroup-unselected nil)
4027     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4028     (gnus-newsgroup-setup-default-charset)
4029
4030     ;; Adjust and set lists of article marks.
4031     (when info
4032       (gnus-adjust-marked-articles info))
4033
4034     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4035     (when (gnus-virtual-group-p group)
4036       (setq cached gnus-newsgroup-cached))
4037
4038     (setq gnus-newsgroup-unreads
4039           (gnus-set-difference
4040            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4041            gnus-newsgroup-dormant))
4042
4043     (setq gnus-newsgroup-processable nil)
4044
4045     (gnus-update-read-articles group gnus-newsgroup-unreads)
4046
4047     (if (setq articles select-articles)
4048         (setq gnus-newsgroup-unselected
4049               (gnus-sorted-intersection
4050                gnus-newsgroup-unreads
4051                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4052       (setq articles (gnus-articles-to-read group read-all)))
4053
4054     (cond
4055      ((null articles)
4056       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4057       'quit)
4058      ((eq articles 0) nil)
4059      (t
4060       ;; Init the dependencies hash table.
4061       (setq gnus-newsgroup-dependencies
4062             (gnus-make-hashtable (length articles)))
4063       (gnus-set-global-variables)
4064       ;; Retrieve the headers and read them in.
4065       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4066       (setq gnus-newsgroup-headers
4067             (if (eq 'nov
4068                     (setq gnus-headers-retrieved-by
4069                           (gnus-retrieve-headers
4070                            articles gnus-newsgroup-name
4071                            ;; We might want to fetch old headers, but
4072                            ;; not if there is only 1 article.
4073                            (and (or (and
4074                                      (not (eq gnus-fetch-old-headers 'some))
4075                                      (not (numberp gnus-fetch-old-headers)))
4076                                     (> (length articles) 1))
4077                                 gnus-fetch-old-headers))))
4078                 (gnus-get-newsgroup-headers-xover
4079                  articles nil nil gnus-newsgroup-name t)
4080               (gnus-get-newsgroup-headers)))
4081       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4082
4083       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4084       (when cached
4085         (setq gnus-newsgroup-cached cached))
4086
4087       ;; Suppress duplicates?
4088       (when gnus-suppress-duplicates
4089         (gnus-dup-suppress-articles))
4090
4091       ;; Set the initial limit.
4092       (setq gnus-newsgroup-limit (copy-sequence articles))
4093       ;; Remove canceled articles from the list of unread articles.
4094       (setq gnus-newsgroup-unreads
4095             (gnus-set-sorted-intersection
4096              gnus-newsgroup-unreads
4097              (setq fetched-articles
4098                    (mapcar (lambda (headers) (mail-header-number headers))
4099                            gnus-newsgroup-headers))))
4100       ;; Removed marked articles that do not exist.
4101       (gnus-update-missing-marks
4102        (gnus-sorted-complement fetched-articles articles))
4103       ;; We might want to build some more threads first.
4104       (when (and gnus-fetch-old-headers
4105                  (eq gnus-headers-retrieved-by 'nov))
4106         (if (eq gnus-fetch-old-headers 'invisible)
4107             (gnus-build-all-threads)
4108           (gnus-build-old-threads)))
4109       ;; Let the Gnus agent mark articles as read.
4110       (when gnus-agent
4111         (gnus-agent-get-undownloaded-list))
4112       ;; Check whether auto-expire is to be done in this group.
4113       (setq gnus-newsgroup-auto-expire
4114             (gnus-group-auto-expirable-p group))
4115       ;; Set up the article buffer now, if necessary.
4116       (unless gnus-single-article-buffer
4117         (gnus-article-setup-buffer))
4118       ;; First and last article in this newsgroup.
4119       (when gnus-newsgroup-headers
4120         (setq gnus-newsgroup-begin
4121               (mail-header-number (car gnus-newsgroup-headers))
4122               gnus-newsgroup-end
4123               (mail-header-number
4124                (gnus-last-element gnus-newsgroup-headers))))
4125       ;; GROUP is successfully selected.
4126       (or gnus-newsgroup-headers t)))))
4127
4128 (defun gnus-articles-to-read (group &optional read-all)
4129   ;; Find out what articles the user wants to read.
4130   (let* ((articles
4131           ;; Select all articles if `read-all' is non-nil, or if there
4132           ;; are no unread articles.
4133           (if (or read-all
4134                   (and (zerop (length gnus-newsgroup-marked))
4135                        (zerop (length gnus-newsgroup-unreads)))
4136                   (eq (gnus-group-find-parameter group 'display)
4137                       'all))
4138               (gnus-uncompress-range (gnus-active group))
4139             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4140                           (copy-sequence gnus-newsgroup-unreads))
4141                   '<)))
4142          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4143          (scored (length scored-list))
4144          (number (length articles))
4145          (marked (+ (length gnus-newsgroup-marked)
4146                     (length gnus-newsgroup-dormant)))
4147          (select
4148           (cond
4149            ((numberp read-all)
4150             read-all)
4151            (t
4152             (condition-case ()
4153                 (cond
4154                  ((and (or (<= scored marked) (= scored number))
4155                        (numberp gnus-large-newsgroup)
4156                        (> number gnus-large-newsgroup))
4157                   (let ((input
4158                          (read-string
4159                           (format
4160                            "How many articles from %s (default %d): "
4161                            (gnus-limit-string gnus-newsgroup-name 35)
4162                            number))))
4163                     (if (string-match "^[ \t]*$" input) number input)))
4164                  ((and (> scored marked) (< scored number)
4165                        (> (- scored number) 20))
4166                   (let ((input
4167                          (read-string
4168                           (format "%s %s (%d scored, %d total): "
4169                                   "How many articles from"
4170                                   group scored number))))
4171                     (if (string-match "^[ \t]*$" input)
4172                         number input)))
4173                  (t number))
4174               (quit nil))))))
4175     (setq select (if (stringp select) (string-to-number select) select))
4176     (if (or (null select) (zerop select))
4177         select
4178       (if (and (not (zerop scored)) (<= (abs select) scored))
4179           (progn
4180             (setq articles (sort scored-list '<))
4181             (setq number (length articles)))
4182         (setq articles (copy-sequence articles)))
4183
4184       (when (< (abs select) number)
4185         (if (< select 0)
4186             ;; Select the N oldest articles.
4187             (setcdr (nthcdr (1- (abs select)) articles) nil)
4188           ;; Select the N most recent articles.
4189           (setq articles (nthcdr (- number select) articles))))
4190       (setq gnus-newsgroup-unselected
4191             (gnus-sorted-intersection
4192              gnus-newsgroup-unreads
4193              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4194       articles)))
4195
4196 (defun gnus-killed-articles (killed articles)
4197   (let (out)
4198     (while articles
4199       (when (inline (gnus-member-of-range (car articles) killed))
4200         (push (car articles) out))
4201       (setq articles (cdr articles)))
4202     out))
4203
4204 (defun gnus-uncompress-marks (marks)
4205   "Uncompress the mark ranges in MARKS."
4206   (let ((uncompressed '(score bookmark))
4207         out)
4208     (while marks
4209       (if (memq (caar marks) uncompressed)
4210           (push (car marks) out)
4211         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4212       (setq marks (cdr marks)))
4213     out))
4214
4215 (defun gnus-adjust-marked-articles (info)
4216   "Set all article lists and remove all marks that are no longer legal."
4217   (let* ((marked-lists (gnus-info-marks info))
4218          (active (gnus-active (gnus-info-group info)))
4219          (min (car active))
4220          (max (cdr active))
4221          (types gnus-article-mark-lists)
4222          (uncompressed '(score bookmark killed))
4223          marks var articles article mark)
4224
4225     (while marked-lists
4226       (setq marks (pop marked-lists))
4227       (set (setq var (intern (format "gnus-newsgroup-%s"
4228                                      (car (rassq (setq mark (car marks))
4229                                                  types)))))
4230            (if (memq (car marks) uncompressed) (cdr marks)
4231              (gnus-uncompress-range (cdr marks))))
4232
4233       (setq articles (symbol-value var))
4234
4235       ;; All articles have to be subsets of the active articles.
4236       (cond
4237        ;; Adjust "simple" lists.
4238        ((memq mark '(tick dormant expire reply save))
4239         (while articles
4240           (when (or (< (setq article (pop articles)) min) (> article max))
4241             (set var (delq article (symbol-value var))))))
4242        ;; Adjust assocs.
4243        ((memq mark uncompressed)
4244         (when (not (listp (cdr (symbol-value var))))
4245           (set var (list (symbol-value var))))
4246         (when (not (listp (cdr articles)))
4247           (setq articles (list articles)))
4248         (while articles
4249           (when (or (not (consp (setq article (pop articles))))
4250                     (< (car article) min)
4251                     (> (car article) max))
4252             (set var (delq article (symbol-value var))))))))))
4253
4254 (defun gnus-update-missing-marks (missing)
4255   "Go through the list of MISSING articles and remove them from the mark lists."
4256   (when missing
4257     (let ((types gnus-article-mark-lists)
4258           var m)
4259       ;; Go through all types.
4260       (while types
4261         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4262         (when (symbol-value var)
4263           ;; This list has articles.  So we delete all missing articles
4264           ;; from it.
4265           (setq m missing)
4266           (while m
4267             (set var (delq (pop m) (symbol-value var)))))))))
4268
4269 (defun gnus-update-marks ()
4270   "Enter the various lists of marked articles into the newsgroup info list."
4271   (let ((types gnus-article-mark-lists)
4272         (info (gnus-get-info gnus-newsgroup-name))
4273         (uncompressed '(score bookmark killed))
4274         type list newmarked symbol delta-marks)
4275     (when info
4276       ;; Add all marks lists that are non-nil to the list of marks lists.
4277       (while (setq type (pop types))
4278         (when (setq list (symbol-value
4279                           (setq symbol
4280                                 (intern (format "gnus-newsgroup-%s"
4281                                                 (car type))))))
4282
4283           ;; Get rid of the entries of the articles that have the
4284           ;; default score.
4285           (when (and (eq (cdr type) 'score)
4286                      gnus-save-score
4287                      list)
4288             (let* ((arts list)
4289                    (prev (cons nil list))
4290                    (all prev))
4291               (while arts
4292                 (if (or (not (consp (car arts)))
4293                         (= (cdar arts) gnus-summary-default-score))
4294                     (setcdr prev (cdr arts))
4295                   (setq prev arts))
4296                 (setq arts (cdr arts)))
4297               (setq list (cdr all))))
4298
4299           (push (cons (cdr type)
4300                       (if (memq (cdr type) uncompressed) list
4301                         (gnus-compress-sequence
4302                          (set symbol (sort list '<)) t)))
4303                 newmarked)))
4304
4305       ;; Enter these new marks into the info of the group.
4306       (if (nthcdr 3 info)
4307           (setcar (nthcdr 3 info) newmarked)
4308         ;; Add the marks lists to the end of the info.
4309         (when newmarked
4310           (setcdr (nthcdr 2 info) (list newmarked))))
4311
4312       ;; Cut off the end of the info if there's nothing else there.
4313       (let ((i 5))
4314         (while (and (> i 2)
4315                     (not (nth i info)))
4316           (when (nthcdr (decf i) info)
4317             (setcdr (nthcdr i info) nil)))))))
4318
4319 (defun gnus-set-mode-line (where)
4320   "This function sets the mode line of the article or summary buffers.
4321 If WHERE is `summary', the summary mode line format will be used."
4322   ;; Is this mode line one we keep updated?
4323   (when (and (memq where gnus-updated-mode-lines)
4324              (symbol-value
4325               (intern (format "gnus-%s-mode-line-format-spec" where))))
4326     (let (mode-string)
4327       (save-excursion
4328         ;; We evaluate this in the summary buffer since these
4329         ;; variables are buffer-local to that buffer.
4330         (set-buffer gnus-summary-buffer)
4331         ;; We bind all these variables that are used in the `eval' form
4332         ;; below.
4333         (let* ((mformat (symbol-value
4334                          (intern
4335                           (format "gnus-%s-mode-line-format-spec" where))))
4336                (gnus-tmp-group-name gnus-newsgroup-name)
4337                (gnus-tmp-article-number (or gnus-current-article 0))
4338                (gnus-tmp-unread gnus-newsgroup-unreads)
4339                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4340                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4341                (gnus-tmp-unread-and-unselected
4342                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4343                             (zerop gnus-tmp-unselected))
4344                        "")
4345                       ((zerop gnus-tmp-unselected)
4346                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4347                       (t (format "{%d(+%d) more}"
4348                                  gnus-tmp-unread-and-unticked
4349                                  gnus-tmp-unselected))))
4350                (gnus-tmp-subject
4351                 (if (and gnus-current-headers
4352                          (vectorp gnus-current-headers))
4353                     (gnus-mode-string-quote
4354                      (mail-header-subject gnus-current-headers))
4355                   ""))
4356                bufname-length max-len
4357                gnus-tmp-header);; passed as argument to any user-format-funcs
4358           (setq mode-string (eval mformat))
4359           (setq bufname-length (if (string-match "%b" mode-string)
4360                                    (- (length
4361                                        (buffer-name
4362                                         (if (eq where 'summary)
4363                                             nil
4364                                           (get-buffer gnus-article-buffer))))
4365                                       2)
4366                                  0))
4367           (setq max-len (max 4 (if gnus-mode-non-string-length
4368                                    (- (window-width)
4369                                       gnus-mode-non-string-length
4370                                       bufname-length)
4371                                  (length mode-string))))
4372           ;; We might have to chop a bit of the string off...
4373           (when (> (length mode-string) max-len)
4374             (setq mode-string
4375                   (concat (truncate-string-to-width mode-string (- max-len 3))
4376                           "...")))
4377           ;; Pad the mode string a bit.
4378           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4379       ;; Update the mode line.
4380       (setq mode-line-buffer-identification
4381             (gnus-mode-line-buffer-identification (list mode-string)))
4382       (set-buffer-modified-p t))))
4383
4384 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4385   "Go through the HEADERS list and add all Xrefs to a hash table.
4386 The resulting hash table is returned, or nil if no Xrefs were found."
4387   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4388          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4389          (xref-hashtb (gnus-make-hashtable))
4390          start group entry number xrefs header)
4391     (while headers
4392       (setq header (pop headers))
4393       (when (and (setq xrefs (mail-header-xref header))
4394                  (not (memq (setq number (mail-header-number header))
4395                             unreads)))
4396         (setq start 0)
4397         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4398           (setq start (match-end 0))
4399           (setq group (if prefix
4400                           (concat prefix (substring xrefs (match-beginning 1)
4401                                                     (match-end 1)))
4402                         (substring xrefs (match-beginning 1) (match-end 1))))
4403           (setq number
4404                 (string-to-int (substring xrefs (match-beginning 2)
4405                                           (match-end 2))))
4406           (if (setq entry (gnus-gethash group xref-hashtb))
4407               (setcdr entry (cons number (cdr entry)))
4408             (gnus-sethash group (cons number nil) xref-hashtb)))))
4409     (and start xref-hashtb)))
4410
4411 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4412   "Look through all the headers and mark the Xrefs as read."
4413   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4414         name entry info xref-hashtb idlist method nth4)
4415     (save-excursion
4416       (set-buffer gnus-group-buffer)
4417       (when (setq xref-hashtb
4418                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4419         (mapatoms
4420          (lambda (group)
4421            (unless (string= from-newsgroup (setq name (symbol-name group)))
4422              (setq idlist (symbol-value group))
4423              ;; Dead groups are not updated.
4424              (and (prog1
4425                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4426                             info (nth 2 entry))
4427                     (when (stringp (setq nth4 (gnus-info-method info)))
4428                       (setq nth4 (gnus-server-to-method nth4))))
4429                   ;; Only do the xrefs if the group has the same
4430                   ;; select method as the group we have just read.
4431                   (or (gnus-methods-equal-p
4432                        nth4 (gnus-find-method-for-group from-newsgroup))
4433                       virtual
4434                       (equal nth4 (setq method (gnus-find-method-for-group
4435                                                 from-newsgroup)))
4436                       (and (equal (car nth4) (car method))
4437                            (equal (nth 1 nth4) (nth 1 method))))
4438                   gnus-use-cross-reference
4439                   (or (not (eq gnus-use-cross-reference t))
4440                       virtual
4441                       ;; Only do cross-references on subscribed
4442                       ;; groups, if that is what is wanted.
4443                       (<= (gnus-info-level info) gnus-level-subscribed))
4444                   (gnus-group-make-articles-read name idlist))))
4445          xref-hashtb)))))
4446
4447 (defun gnus-compute-read-articles (group articles)
4448   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4449          (info (nth 2 entry))
4450          (active (gnus-active group))
4451          ninfo)
4452     (when entry
4453       ;; First peel off all illegal article numbers.
4454       (when active
4455         (let ((ids articles)
4456               id first)
4457           (while (setq id (pop ids))
4458             (when (and first (> id (cdr active)))
4459               ;; We'll end up in this situation in one particular
4460               ;; obscure situation.  If you re-scan a group and get
4461               ;; a new article that is cross-posted to a different
4462               ;; group that has not been re-scanned, you might get
4463               ;; crossposted article that has a higher number than
4464               ;; Gnus believes possible.  So we re-activate this
4465               ;; group as well.  This might mean doing the
4466               ;; crossposting thingy will *increase* the number
4467               ;; of articles in some groups.  Tsk, tsk.
4468               (setq active (or (gnus-activate-group group) active)))
4469             (when (or (> id (cdr active))
4470                       (< id (car active)))
4471               (setq articles (delq id articles))))))
4472       ;; If the read list is nil, we init it.
4473       (if (and active
4474                (null (gnus-info-read info))
4475                (> (car active) 1))
4476           (setq ninfo (cons 1 (1- (car active))))
4477         (setq ninfo (gnus-info-read info)))
4478       ;; Then we add the read articles to the range.
4479       (gnus-add-to-range
4480        ninfo (setq articles (sort articles '<))))))
4481
4482 (defun gnus-group-make-articles-read (group articles)
4483   "Update the info of GROUP to say that ARTICLES are read."
4484   (let* ((num 0)
4485          (entry (gnus-gethash group gnus-newsrc-hashtb))
4486          (info (nth 2 entry))
4487          (active (gnus-active group))
4488          range)
4489     (when entry
4490       (setq range (gnus-compute-read-articles group articles))
4491       (save-excursion
4492         (set-buffer gnus-group-buffer)
4493         (gnus-undo-register
4494           `(progn
4495              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4496              (gnus-info-set-read ',info ',(gnus-info-read info))
4497              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4498              (gnus-group-update-group ,group t))))
4499       ;; Add the read articles to the range.
4500       (gnus-info-set-read info range)
4501       ;; Then we have to re-compute how many unread
4502       ;; articles there are in this group.
4503       (when active
4504         (cond
4505          ((not range)
4506           (setq num (- (1+ (cdr active)) (car active))))
4507          ((not (listp (cdr range)))
4508           (setq num (- (cdr active) (- (1+ (cdr range))
4509                                        (car range)))))
4510          (t
4511           (while range
4512             (if (numberp (car range))
4513                 (setq num (1+ num))
4514               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4515             (setq range (cdr range)))
4516           (setq num (- (cdr active) num))))
4517         ;; Update the number of unread articles.
4518         (setcar entry num)
4519         ;; Update the group buffer.
4520         (gnus-group-update-group group t)))))
4521
4522 (defun gnus-methods-equal-p (m1 m2)
4523   (let ((m1 (or m1 gnus-select-method))
4524         (m2 (or m2 gnus-select-method)))
4525     (or (equal m1 m2)
4526         (and (eq (car m1) (car m2))
4527              (or (not (memq 'address (assoc (symbol-name (car m1))
4528                                             gnus-valid-select-methods)))
4529                  (equal (nth 1 m1) (nth 1 m2)))))))
4530
4531 (defvar gnus-newsgroup-none-id 0)
4532
4533 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4534   (let ((cur nntp-server-buffer)
4535         (dependencies
4536          (or dependencies
4537              (save-excursion (set-buffer gnus-summary-buffer)
4538                              gnus-newsgroup-dependencies)))
4539         headers id end ref
4540         (rfc2047-default-charset gnus-newsgroup-default-charset)
4541         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
4542     (save-excursion
4543       (set-buffer nntp-server-buffer)
4544       ;; Translate all TAB characters into SPACE characters.
4545       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4546       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4547       (gnus-run-hooks 'gnus-parse-headers-hook)
4548       (let ((case-fold-search t)
4549             in-reply-to header p lines chars)
4550         (goto-char (point-min))
4551         ;; Search to the beginning of the next header.  Error messages
4552         ;; do not begin with 2 or 3.
4553         (while (re-search-forward "^[23][0-9]+ " nil t)
4554           (setq id nil
4555                 ref nil)
4556           ;; This implementation of this function, with nine
4557           ;; search-forwards instead of the one re-search-forward and
4558           ;; a case (which basically was the old function) is actually
4559           ;; about twice as fast, even though it looks messier.  You
4560           ;; can't have everything, I guess.  Speed and elegance
4561           ;; doesn't always go hand in hand.
4562           (setq
4563            header
4564            (vector
4565             ;; Number.
4566             (prog1
4567                 (read cur)
4568               (end-of-line)
4569               (setq p (point))
4570               (narrow-to-region (point)
4571                                 (or (and (search-forward "\n.\n" nil t)
4572                                          (- (point) 2))
4573                                     (point))))
4574             ;; Subject.
4575             (progn
4576               (goto-char p)
4577               (if (search-forward "\nsubject: " nil t)
4578                   (funcall gnus-decode-encoded-word-function
4579                            (nnheader-header-value))
4580                 "(none)"))
4581             ;; From.
4582             (progn
4583               (goto-char p)
4584               (if (search-forward "\nfrom: " nil t)
4585                   (funcall gnus-decode-encoded-word-function
4586                            (nnheader-header-value))
4587                 "(nobody)"))
4588             ;; Date.
4589             (progn
4590               (goto-char p)
4591               (if (search-forward "\ndate: " nil t)
4592                   (nnheader-header-value) ""))
4593             ;; Message-ID.
4594             (progn
4595               (goto-char p)
4596               (setq id (if (re-search-forward
4597                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4598                            ;; We do it this way to make sure the Message-ID
4599                            ;; is (somewhat) syntactically valid.
4600                            (buffer-substring (match-beginning 1)
4601                                              (match-end 1))
4602                          ;; If there was no message-id, we just fake one
4603                          ;; to make subsequent routines simpler.
4604                          (nnheader-generate-fake-message-id))))
4605             ;; References.
4606             (progn
4607               (goto-char p)
4608               (if (search-forward "\nreferences: " nil t)
4609                   (progn
4610                     (setq end (point))
4611                     (prog1
4612                         (nnheader-header-value)
4613                       (setq ref
4614                             (buffer-substring
4615                              (progn
4616                                (end-of-line)
4617                                (search-backward ">" end t)
4618                                (1+ (point)))
4619                              (progn
4620                                (search-backward "<" end t)
4621                                (point))))))
4622                 ;; Get the references from the in-reply-to header if there
4623                 ;; were no references and the in-reply-to header looks
4624                 ;; promising.
4625                 (if (and (search-forward "\nin-reply-to: " nil t)
4626                          (setq in-reply-to (nnheader-header-value))
4627                          (string-match "<[^>]+>" in-reply-to))
4628                     (let (ref2)
4629                       (setq ref (substring in-reply-to (match-beginning 0)
4630                                            (match-end 0)))
4631                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4632                         (setq ref2 (substring in-reply-to (match-beginning 0)
4633                                               (match-end 0)))
4634                         (when (> (length ref2) (length ref))
4635                           (setq ref ref2)))
4636                       ref)
4637                   (setq ref nil))))
4638             ;; Chars.
4639             (progn
4640               (goto-char p)
4641               (if (search-forward "\nchars: " nil t)
4642                   (if (numberp (setq chars (ignore-errors (read cur))))
4643                       chars 0)
4644                 0))
4645             ;; Lines.
4646             (progn
4647               (goto-char p)
4648               (if (search-forward "\nlines: " nil t)
4649                   (if (numberp (setq lines (ignore-errors (read cur))))
4650                       lines 0)
4651                 0))
4652             ;; Xref.
4653             (progn
4654               (goto-char p)
4655               (and (search-forward "\nxref: " nil t)
4656                    (nnheader-header-value)))
4657             ;; Extra.
4658             (when gnus-extra-headers
4659               (let ((extra gnus-extra-headers)
4660                     out)
4661                 (while extra
4662                   (goto-char p)
4663                   (when (search-forward
4664                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4665                     (push (cons (car extra) (nnheader-header-value))
4666                           out))
4667                   (pop extra))
4668                 out))))
4669           (when (equal id ref)
4670             (setq ref nil))
4671
4672           (when gnus-alter-header-function
4673             (funcall gnus-alter-header-function header)
4674             (setq id (mail-header-id header)
4675                   ref (gnus-parent-id (mail-header-references header))))
4676
4677           (when (setq header
4678                       (gnus-dependencies-add-header
4679                        header dependencies force-new))
4680             (push header headers))
4681           (goto-char (point-max))
4682           (widen))
4683         (nreverse headers)))))
4684
4685 ;; Goes through the xover lines and returns a list of vectors
4686 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4687                                                   force-new dependencies
4688                                                   group also-fetch-heads)
4689   "Parse the news overview data in the server buffer, and return a
4690 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4691   ;; Get the Xref when the users reads the articles since most/some
4692   ;; NNTP servers do not include Xrefs when using XOVER.
4693   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4694   (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
4695         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced)
4696         (cur nntp-server-buffer)
4697         (dependencies (or dependencies gnus-newsgroup-dependencies))
4698         number headers header)
4699     (save-excursion
4700       (set-buffer nntp-server-buffer)
4701       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4702       ;; Allow the user to mangle the headers before parsing them.
4703       (gnus-run-hooks 'gnus-parse-headers-hook)
4704       (goto-char (point-min))
4705       (while (not (eobp))
4706         (condition-case ()
4707             (while (and sequence (not (eobp)))
4708               (setq number (read cur))
4709               (while (and sequence
4710                           (< (car sequence) number))
4711                 (setq sequence (cdr sequence)))
4712               (and sequence
4713                    (eq number (car sequence))
4714                    (progn
4715                      (setq sequence (cdr sequence))
4716                      (setq header (inline
4717                                     (gnus-nov-parse-line
4718                                      number dependencies force-new))))
4719                    (push header headers))
4720               (forward-line 1))
4721           (error
4722            (gnus-error 4 "Strange nov line (%d)"
4723                        (count-lines (point-min) (point)))))
4724         (forward-line 1))
4725       ;; A common bug in inn is that if you have posted an article and
4726       ;; then retrieves the active file, it will answer correctly --
4727       ;; the new article is included.  However, a NOV entry for the
4728       ;; article may not have been generated yet, so this may fail.
4729       ;; We work around this problem by retrieving the last few
4730       ;; headers using HEAD.
4731       (if (or (not also-fetch-heads)
4732               (not sequence))
4733           ;; We (probably) got all the headers.
4734           (nreverse headers)
4735         (let ((gnus-nov-is-evil t))
4736           (nconc
4737            (nreverse headers)
4738            (when (gnus-retrieve-headers sequence group)
4739              (gnus-get-newsgroup-headers))))))))
4740
4741 (defun gnus-article-get-xrefs ()
4742   "Fill in the Xref value in `gnus-current-headers', if necessary.
4743 This is meant to be called in `gnus-article-internal-prepare-hook'."
4744   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4745                                  gnus-current-headers)))
4746     (or (not gnus-use-cross-reference)
4747         (not headers)
4748         (and (mail-header-xref headers)
4749              (not (string= (mail-header-xref headers) "")))
4750         (let ((case-fold-search t)
4751               xref)
4752           (save-restriction
4753             (nnheader-narrow-to-headers)
4754             (goto-char (point-min))
4755             (when (or (and (eq (downcase (char-after)) ?x)
4756                            (looking-at "Xref:"))
4757                       (search-forward "\nXref:" nil t))
4758               (goto-char (1+ (match-end 0)))
4759               (setq xref (buffer-substring (point)
4760                                            (progn (end-of-line) (point))))
4761               (mail-header-set-xref headers xref)))))))
4762
4763 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4764   "Find article ID and insert the summary line for that article.
4765 OLD-HEADER can either be a header or a line number to insert
4766 the subject line on."
4767   (let* ((line (and (numberp old-header) old-header))
4768          (old-header (and (vectorp old-header) old-header))
4769          (header (cond ((and old-header use-old-header)
4770                         old-header)
4771                        ((and (numberp id)
4772                              (gnus-number-to-header id))
4773                         (gnus-number-to-header id))
4774                        (t
4775                         (gnus-read-header id))))
4776          (number (and (numberp id) id))
4777          d)
4778     (when header
4779       ;; Rebuild the thread that this article is part of and go to the
4780       ;; article we have fetched.
4781       (when (and (not gnus-show-threads)
4782                  old-header)
4783         (when (and number
4784                    (setq d (gnus-data-find (mail-header-number old-header))))
4785           (goto-char (gnus-data-pos d))
4786           (gnus-data-remove
4787            number
4788            (- (gnus-point-at-bol)
4789               (prog1
4790                   (1+ (gnus-point-at-eol))
4791                 (gnus-delete-line))))))
4792       (when old-header
4793         (mail-header-set-number header (mail-header-number old-header)))
4794       (setq gnus-newsgroup-sparse
4795             (delq (setq number (mail-header-number header))
4796                   gnus-newsgroup-sparse))
4797       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4798       (push number gnus-newsgroup-limit)
4799       (gnus-rebuild-thread (mail-header-id header) line)
4800       (gnus-summary-goto-subject number nil t))
4801     (when (and (numberp number)
4802                (> number 0))
4803       ;; We have to update the boundaries even if we can't fetch the
4804       ;; article if ID is a number -- so that the next `P' or `N'
4805       ;; command will fetch the previous (or next) article even
4806       ;; if the one we tried to fetch this time has been canceled.
4807       (when (> number gnus-newsgroup-end)
4808         (setq gnus-newsgroup-end number))
4809       (when (< number gnus-newsgroup-begin)
4810         (setq gnus-newsgroup-begin number))
4811       (setq gnus-newsgroup-unselected
4812             (delq number gnus-newsgroup-unselected)))
4813     ;; Report back a success?
4814     (and header (mail-header-number header))))
4815
4816 ;;; Process/prefix in the summary buffer
4817
4818 (defun gnus-summary-work-articles (n)
4819   "Return a list of articles to be worked upon.
4820 The prefix argument, the list of process marked articles, and the
4821 current article will be taken into consideration."
4822   (save-excursion
4823     (set-buffer gnus-summary-buffer)
4824     (cond
4825      (n
4826       ;; A numerical prefix has been given.
4827       (setq n (prefix-numeric-value n))
4828       (let ((backward (< n 0))
4829             (n (abs (prefix-numeric-value n)))
4830             articles article)
4831         (save-excursion
4832           (while
4833               (and (> n 0)
4834                    (push (setq article (gnus-summary-article-number))
4835                          articles)
4836                    (if backward
4837                        (gnus-summary-find-prev nil article)
4838                      (gnus-summary-find-next nil article)))
4839             (decf n)))
4840         (nreverse articles)))
4841      ((and (gnus-region-active-p) (mark))
4842       (message "region active")
4843       ;; Work on the region between point and mark.
4844       (let ((max (max (point) (mark)))
4845             articles article)
4846         (save-excursion
4847           (goto-char (min (min (point) (mark))))
4848           (while
4849               (and
4850                (push (setq article (gnus-summary-article-number)) articles)
4851                (gnus-summary-find-next nil article)
4852                (< (point) max)))
4853           (nreverse articles))))
4854      (gnus-newsgroup-processable
4855       ;; There are process-marked articles present.
4856       ;; Save current state.
4857       (gnus-summary-save-process-mark)
4858       ;; Return the list.
4859       (reverse gnus-newsgroup-processable))
4860      (t
4861       ;; Just return the current article.
4862       (list (gnus-summary-article-number))))))
4863
4864 (defmacro gnus-summary-iterate (arg &rest forms)
4865   "Iterate over the process/prefixed articles and do FORMS.
4866 ARG is the interactive prefix given to the command.  FORMS will be
4867 executed with point over the summary line of the articles."
4868   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4869     `(let ((,articles (gnus-summary-work-articles ,arg)))
4870        (while ,articles
4871          (gnus-summary-goto-subject (car ,articles))
4872          ,@forms))))
4873
4874 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4875 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4876
4877 (defun gnus-summary-save-process-mark ()
4878   "Push the current set of process marked articles on the stack."
4879   (interactive)
4880   (push (copy-sequence gnus-newsgroup-processable)
4881         gnus-newsgroup-process-stack))
4882
4883 (defun gnus-summary-kill-process-mark ()
4884   "Push the current set of process marked articles on the stack and unmark."
4885   (interactive)
4886   (gnus-summary-save-process-mark)
4887   (gnus-summary-unmark-all-processable))
4888
4889 (defun gnus-summary-yank-process-mark ()
4890   "Pop the last process mark state off the stack and restore it."
4891   (interactive)
4892   (unless gnus-newsgroup-process-stack
4893     (error "Empty mark stack"))
4894   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4895
4896 (defun gnus-summary-process-mark-set (set)
4897   "Make SET into the current process marked articles."
4898   (gnus-summary-unmark-all-processable)
4899   (while set
4900     (gnus-summary-set-process-mark (pop set))))
4901
4902 ;;; Searching and stuff
4903
4904 (defun gnus-summary-search-group (&optional backward use-level)
4905   "Search for next unread newsgroup.
4906 If optional argument BACKWARD is non-nil, search backward instead."
4907   (save-excursion
4908     (set-buffer gnus-group-buffer)
4909     (when (gnus-group-search-forward
4910            backward nil (if use-level (gnus-group-group-level) nil))
4911       (gnus-group-group-name))))
4912
4913 (defun gnus-summary-best-group (&optional exclude-group)
4914   "Find the name of the best unread group.
4915 If EXCLUDE-GROUP, do not go to this group."
4916   (save-excursion
4917     (set-buffer gnus-group-buffer)
4918     (save-excursion
4919       (gnus-group-best-unread-group exclude-group))))
4920
4921 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4922   (if backward (gnus-summary-find-prev)
4923     (let* ((dummy (gnus-summary-article-intangible-p))
4924            (article (or article (gnus-summary-article-number)))
4925            (arts (gnus-data-find-list article))
4926            result)
4927       (when (and (not dummy)
4928                  (or (not gnus-summary-check-current)
4929                      (not unread)
4930                      (not (gnus-data-unread-p (car arts)))))
4931         (setq arts (cdr arts)))
4932       (when (setq result
4933                   (if unread
4934                       (progn
4935                         (while arts
4936                           (when (or (and undownloaded
4937                                          (eq gnus-undownloaded-mark
4938                                              (gnus-data-mark (car arts))))
4939                                     (gnus-data-unread-p (car arts)))
4940                             (setq result (car arts)
4941                                   arts nil))
4942                           (setq arts (cdr arts)))
4943                         result)
4944                     (car arts)))
4945         (goto-char (gnus-data-pos result))
4946         (gnus-data-number result)))))
4947
4948 (defun gnus-summary-find-prev (&optional unread article)
4949   (let* ((eobp (eobp))
4950          (article (or article (gnus-summary-article-number)))
4951          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4952          result)
4953     (when (and (not eobp)
4954                (or (not gnus-summary-check-current)
4955                    (not unread)
4956                    (not (gnus-data-unread-p (car arts)))))
4957       (setq arts (cdr arts)))
4958     (when (setq result
4959                 (if unread
4960                     (progn
4961                       (while arts
4962                         (when (gnus-data-unread-p (car arts))
4963                           (setq result (car arts)
4964                                 arts nil))
4965                         (setq arts (cdr arts)))
4966                       result)
4967                   (car arts)))
4968       (goto-char (gnus-data-pos result))
4969       (gnus-data-number result))))
4970
4971 (defun gnus-summary-find-subject (subject &optional unread backward article)
4972   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4973          (article (or article (gnus-summary-article-number)))
4974          (articles (gnus-data-list backward))
4975          (arts (gnus-data-find-list article articles))
4976          result)
4977     (when (or (not gnus-summary-check-current)
4978               (not unread)
4979               (not (gnus-data-unread-p (car arts))))
4980       (setq arts (cdr arts)))
4981     (while arts
4982       (and (or (not unread)
4983                (gnus-data-unread-p (car arts)))
4984            (vectorp (gnus-data-header (car arts)))
4985            (gnus-subject-equal
4986             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4987            (setq result (car arts)
4988                  arts nil))
4989       (setq arts (cdr arts)))
4990     (and result
4991          (goto-char (gnus-data-pos result))
4992          (gnus-data-number result))))
4993
4994 (defun gnus-summary-search-forward (&optional unread subject backward)
4995   "Search forward for an article.
4996 If UNREAD, look for unread articles.  If SUBJECT, look for
4997 articles with that subject.  If BACKWARD, search backward instead."
4998   (cond (subject (gnus-summary-find-subject subject unread backward))
4999         (backward (gnus-summary-find-prev unread))
5000         (t (gnus-summary-find-next unread))))
5001
5002 (defun gnus-recenter (&optional n)
5003   "Center point in window and redisplay frame.
5004 Also do horizontal recentering."
5005   (interactive "P")
5006   (when (and gnus-auto-center-summary
5007              (not (eq gnus-auto-center-summary 'vertical)))
5008     (gnus-horizontal-recenter))
5009   (recenter n))
5010
5011 (defun gnus-summary-recenter ()
5012   "Center point in the summary window.
5013 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5014 displayed, no centering will be performed."
5015   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5016   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5017   (let* ((top (cond ((< (window-height) 4) 0)
5018                     ((< (window-height) 7) 1)
5019                     (t (if (numberp gnus-auto-center-summary)
5020                            gnus-auto-center-summary
5021                          2))))
5022          (height (1- (window-height)))
5023          (bottom (save-excursion (goto-char (point-max))
5024                                  (forward-line (- height))
5025                                  (point)))
5026          (window (get-buffer-window (current-buffer))))
5027     ;; The user has to want it.
5028     (when gnus-auto-center-summary
5029       (when (get-buffer-window gnus-article-buffer)
5030         ;; Only do recentering when the article buffer is displayed,
5031         ;; Set the window start to either `bottom', which is the biggest
5032         ;; possible valid number, or the second line from the top,
5033         ;; whichever is the least.
5034         (set-window-start
5035          window (min bottom (save-excursion
5036                               (forward-line (- top)) (point)))))
5037       ;; Do horizontal recentering while we're at it.
5038       (when (and (get-buffer-window (current-buffer) t)
5039                  (not (eq gnus-auto-center-summary 'vertical)))
5040         (let ((selected (selected-window)))
5041           (select-window (get-buffer-window (current-buffer) t))
5042           (gnus-summary-position-point)
5043           (gnus-horizontal-recenter)
5044           (select-window selected))))))
5045
5046 (defun gnus-summary-jump-to-group (newsgroup)
5047   "Move point to NEWSGROUP in group mode buffer."
5048   ;; Keep update point of group mode buffer if visible.
5049   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5050       (save-window-excursion
5051         ;; Take care of tree window mode.
5052         (when (get-buffer-window gnus-group-buffer)
5053           (pop-to-buffer gnus-group-buffer))
5054         (gnus-group-jump-to-group newsgroup))
5055     (save-excursion
5056       ;; Take care of tree window mode.
5057       (if (get-buffer-window gnus-group-buffer)
5058           (pop-to-buffer gnus-group-buffer)
5059         (set-buffer gnus-group-buffer))
5060       (gnus-group-jump-to-group newsgroup))))
5061
5062 ;; This function returns a list of article numbers based on the
5063 ;; difference between the ranges of read articles in this group and
5064 ;; the range of active articles.
5065 (defun gnus-list-of-unread-articles (group)
5066   (let* ((read (gnus-info-read (gnus-get-info group)))
5067          (active (or (gnus-active group) (gnus-activate-group group)))
5068          (last (cdr active))
5069          first nlast unread)
5070     ;; If none are read, then all are unread.
5071     (if (not read)
5072         (setq first (car active))
5073       ;; If the range of read articles is a single range, then the
5074       ;; first unread article is the article after the last read
5075       ;; article.  Sounds logical, doesn't it?
5076       (if (not (listp (cdr read)))
5077           (setq first (max (car active) (1+ (cdr read))))
5078         ;; `read' is a list of ranges.
5079         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5080                                   (caar read)))
5081                   1)
5082           (setq first (car active)))
5083         (while read
5084           (when first
5085             (while (< first nlast)
5086               (push first unread)
5087               (setq first (1+ first))))
5088           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5089           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5090           (setq read (cdr read)))))
5091     ;; And add the last unread articles.
5092     (while (<= first last)
5093       (push first unread)
5094       (setq first (1+ first)))
5095     ;; Return the list of unread articles.
5096     (delq 0 (nreverse unread))))
5097
5098 (defun gnus-list-of-read-articles (group)
5099   "Return a list of unread, unticked and non-dormant articles."
5100   (let* ((info (gnus-get-info group))
5101          (marked (gnus-info-marks info))
5102          (active (gnus-active group)))
5103     (and info active
5104          (gnus-set-difference
5105           (gnus-sorted-complement
5106            (gnus-uncompress-range active)
5107            (gnus-list-of-unread-articles group))
5108           (append
5109            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5110            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5111
5112 ;; Various summary commands
5113
5114 (defun gnus-summary-select-article-buffer ()
5115   "Reconfigure windows to show article buffer."
5116   (interactive)
5117   (if (not (gnus-buffer-live-p gnus-article-buffer))
5118       (error "There is no article buffer for this summary buffer")
5119     (gnus-configure-windows 'article)
5120     (select-window (get-buffer-window gnus-article-buffer))))
5121
5122 (defun gnus-summary-universal-argument (arg)
5123   "Perform any operation on all articles that are process/prefixed."
5124   (interactive "P")
5125   (let ((articles (gnus-summary-work-articles arg))
5126         func article)
5127     (if (eq
5128          (setq
5129           func
5130           (key-binding
5131            (read-key-sequence
5132             (substitute-command-keys
5133              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5134              ))))
5135          'undefined)
5136         (gnus-error 1 "Undefined key")
5137       (save-excursion
5138         (while articles
5139           (gnus-summary-goto-subject (setq article (pop articles)))
5140           (let (gnus-newsgroup-processable)
5141             (command-execute func))
5142           (gnus-summary-remove-process-mark article)))))
5143   (gnus-summary-position-point))
5144
5145 (defun gnus-summary-toggle-truncation (&optional arg)
5146   "Toggle truncation of summary lines.
5147 With arg, turn line truncation on iff arg is positive."
5148   (interactive "P")
5149   (setq truncate-lines
5150         (if (null arg) (not truncate-lines)
5151           (> (prefix-numeric-value arg) 0)))
5152   (redraw-display))
5153
5154 (defun gnus-summary-reselect-current-group (&optional all rescan)
5155   "Exit and then reselect the current newsgroup.
5156 The prefix argument ALL means to select all articles."
5157   (interactive "P")
5158   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5159     (error "Ephemeral groups can't be reselected"))
5160   (let ((current-subject (gnus-summary-article-number))
5161         (group gnus-newsgroup-name))
5162     (setq gnus-newsgroup-begin nil)
5163     (gnus-summary-exit)
5164     ;; We have to adjust the point of group mode buffer because
5165     ;; point was moved to the next unread newsgroup by exiting.
5166     (gnus-summary-jump-to-group group)
5167     (when rescan
5168       (save-excursion
5169         (gnus-group-get-new-news-this-group 1)))
5170     (gnus-group-read-group all t)
5171     (gnus-summary-goto-subject current-subject nil t)))
5172
5173 (defun gnus-summary-rescan-group (&optional all)
5174   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5175   (interactive "P")
5176   (gnus-summary-reselect-current-group all t))
5177
5178 (defun gnus-summary-update-info (&optional non-destructive)
5179   (save-excursion
5180     (let ((group gnus-newsgroup-name))
5181       (when group
5182         (when gnus-newsgroup-kill-headers
5183           (setq gnus-newsgroup-killed
5184                 (gnus-compress-sequence
5185                  (nconc
5186                   (gnus-set-sorted-intersection
5187                    (gnus-uncompress-range gnus-newsgroup-killed)
5188                    (setq gnus-newsgroup-unselected
5189                          (sort gnus-newsgroup-unselected '<)))
5190                   (setq gnus-newsgroup-unreads
5191                         (sort gnus-newsgroup-unreads '<)))
5192                  t)))
5193         (unless (listp (cdr gnus-newsgroup-killed))
5194           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5195         (let ((headers gnus-newsgroup-headers))
5196           ;; Set the new ranges of read articles.
5197           (save-excursion
5198             (set-buffer gnus-group-buffer)
5199             (gnus-undo-force-boundary))
5200           (gnus-update-read-articles
5201            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5202           ;; Set the current article marks.
5203           (let ((gnus-newsgroup-scored
5204                  (if (and (not gnus-save-score)
5205                           (not non-destructive))
5206                      nil
5207                    gnus-newsgroup-scored)))
5208             (save-excursion
5209               (gnus-update-marks)))
5210           ;; Do the cross-ref thing.
5211           (when gnus-use-cross-reference
5212             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5213           ;; Do not switch windows but change the buffer to work.
5214           (set-buffer gnus-group-buffer)
5215           (unless (gnus-ephemeral-group-p group)
5216             (gnus-group-update-group group)))))))
5217
5218 (defun gnus-summary-save-newsrc (&optional force)
5219   "Save the current number of read/marked articles in the dribble buffer.
5220 The dribble buffer will then be saved.
5221 If FORCE (the prefix), also save the .newsrc file(s)."
5222   (interactive "P")
5223   (gnus-summary-update-info t)
5224   (if force
5225       (gnus-save-newsrc-file)
5226     (gnus-dribble-save)))
5227
5228 (defun gnus-summary-exit (&optional temporary)
5229   "Exit reading current newsgroup, and then return to group selection mode.
5230 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5231   (interactive)
5232   (gnus-set-global-variables)
5233   (gnus-kill-save-kill-buffer)
5234   (gnus-async-halt-prefetch)
5235   (let* ((group gnus-newsgroup-name)
5236          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5237          (mode major-mode)
5238          (group-point nil)
5239          (buf (current-buffer)))
5240     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5241     ;; If we have several article buffers, we kill them at exit.
5242     (unless gnus-single-article-buffer
5243       (gnus-kill-buffer gnus-original-article-buffer)
5244       (setq gnus-article-current nil))
5245     (when gnus-use-cache
5246       (gnus-cache-possibly-remove-articles)
5247       (gnus-cache-save-buffers))
5248     (gnus-async-prefetch-remove-group group)
5249     (when gnus-suppress-duplicates
5250       (gnus-dup-enter-articles))
5251     (when gnus-use-trees
5252       (gnus-tree-close group))
5253     ;; Remove entries for this group.
5254     (nnmail-purge-split-history (gnus-group-real-name group))
5255     ;; Make all changes in this group permanent.
5256     (unless quit-config
5257       (gnus-run-hooks 'gnus-exit-group-hook)
5258       (gnus-summary-update-info)
5259       ;; Do adaptive scoring, and possibly save score files.
5260       (when gnus-newsgroup-adaptive
5261         (gnus-score-adaptive))
5262       (when gnus-use-scoring
5263         (gnus-score-save)))
5264     (gnus-close-group group)
5265     ;; Make sure where we were, and go to next newsgroup.
5266     (set-buffer gnus-group-buffer)
5267     (unless quit-config
5268       (gnus-group-jump-to-group group))
5269     (gnus-run-hooks 'gnus-summary-exit-hook)
5270     (unless (or quit-config
5271                 ;; If this group has disappeared from the summary
5272                 ;; buffer, don't skip forwards.
5273                 (not (string= group (gnus-group-group-name))))
5274       (gnus-group-next-unread-group 1))
5275     (setq group-point (point))
5276     (if temporary
5277         nil                             ;Nothing to do.
5278       (when (gnus-buffer-live-p gnus-article-buffer)
5279         (save-excursion
5280           (set-buffer gnus-article-buffer)
5281           (mm-destroy-parts gnus-article-mime-handles)))
5282       ;; If we have several article buffers, we kill them at exit.
5283       (unless gnus-single-article-buffer
5284         (gnus-kill-buffer gnus-article-buffer)
5285         (gnus-kill-buffer gnus-original-article-buffer)
5286         (setq gnus-article-current nil))
5287       (set-buffer buf)
5288       (if (not gnus-kill-summary-on-exit)
5289           (gnus-deaden-summary)
5290         ;; We set all buffer-local variables to nil.  It is unclear why
5291         ;; this is needed, but if we don't, buffer-local variables are
5292         ;; not garbage-collected, it seems.  This would the lead to en
5293         ;; ever-growing Emacs.
5294         (gnus-summary-clear-local-variables)
5295         (when (get-buffer gnus-article-buffer)
5296           (bury-buffer gnus-article-buffer))
5297         ;; We clear the global counterparts of the buffer-local
5298         ;; variables as well, just to be on the safe side.
5299         (set-buffer gnus-group-buffer)
5300         (gnus-summary-clear-local-variables)
5301         ;; Return to group mode buffer.
5302         (when (eq mode 'gnus-summary-mode)
5303           (gnus-kill-buffer buf)))
5304       (setq gnus-current-select-method gnus-select-method)
5305       (pop-to-buffer gnus-group-buffer)
5306       (if (not quit-config)
5307           (progn
5308             (goto-char group-point)
5309             (gnus-configure-windows 'group 'force))
5310         (gnus-handle-ephemeral-exit quit-config))
5311       ;; Clear the current group name.
5312       (unless quit-config
5313         (setq gnus-newsgroup-name nil)))))
5314
5315 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5316 (defun gnus-summary-exit-no-update (&optional no-questions)
5317   "Quit reading current newsgroup without updating read article info."
5318   (interactive)
5319   (let* ((group gnus-newsgroup-name)
5320          (quit-config (gnus-group-quit-config group)))
5321     (when (or no-questions
5322               gnus-expert-user
5323               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5324       (gnus-async-halt-prefetch)
5325       (gnus-run-hooks (delq 'gnus-summary-expire-articles 
5326                             (copy-list gnus-summary-prepare-exit-hook)))
5327       (when (gnus-buffer-live-p gnus-article-buffer)
5328         (save-excursion
5329           (set-buffer gnus-article-buffer)
5330           (mm-destroy-parts gnus-article-mime-handles)))
5331       ;; If we have several article buffers, we kill them at exit.
5332       (unless gnus-single-article-buffer
5333         (gnus-kill-buffer gnus-article-buffer)
5334         (gnus-kill-buffer gnus-original-article-buffer)
5335         (setq gnus-article-current nil))
5336       (if (not gnus-kill-summary-on-exit)
5337           (gnus-deaden-summary)
5338         (gnus-close-group group)
5339         (gnus-summary-clear-local-variables)
5340         (set-buffer gnus-group-buffer)
5341         (gnus-summary-clear-local-variables)
5342         (when (get-buffer gnus-summary-buffer)
5343           (kill-buffer gnus-summary-buffer)))
5344       (unless gnus-single-article-buffer
5345         (setq gnus-article-current nil))
5346       (when gnus-use-trees
5347         (gnus-tree-close group))
5348       (gnus-async-prefetch-remove-group group)
5349       (when (get-buffer gnus-article-buffer)
5350         (bury-buffer gnus-article-buffer))
5351       ;; Return to the group buffer.
5352       (gnus-configure-windows 'group 'force)
5353       ;; Clear the current group name.
5354       (setq gnus-newsgroup-name nil)
5355       (when (equal (gnus-group-group-name) group)
5356         (gnus-group-next-unread-group 1))
5357       (when quit-config
5358         (gnus-handle-ephemeral-exit quit-config)))))
5359
5360 (defun gnus-handle-ephemeral-exit (quit-config)
5361   "Handle movement when leaving an ephemeral group.
5362 The state which existed when entering the ephemeral is reset."
5363   (if (not (buffer-name (car quit-config)))
5364       (gnus-configure-windows 'group 'force)
5365     (set-buffer (car quit-config))
5366     (cond ((eq major-mode 'gnus-summary-mode)
5367            (gnus-set-global-variables))
5368           ((eq major-mode 'gnus-article-mode)
5369            (save-excursion
5370              ;; The `gnus-summary-buffer' variable may point
5371              ;; to the old summary buffer when using a single
5372              ;; article buffer.
5373              (unless (gnus-buffer-live-p gnus-summary-buffer)
5374                (set-buffer gnus-group-buffer))
5375              (set-buffer gnus-summary-buffer)
5376              (gnus-set-global-variables))))
5377     (if (or (eq (cdr quit-config) 'article)
5378             (eq (cdr quit-config) 'pick))
5379         (progn
5380           ;; The current article may be from the ephemeral group
5381           ;; thus it is best that we reload this article
5382           (gnus-summary-show-article)
5383           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5384               (gnus-configure-windows 'pick 'force)
5385             (gnus-configure-windows (cdr quit-config) 'force)))
5386       (gnus-configure-windows (cdr quit-config) 'force))
5387     (when (eq major-mode 'gnus-summary-mode)
5388       (gnus-summary-next-subject 1 nil t)
5389       (gnus-summary-recenter)
5390       (gnus-summary-position-point))))
5391
5392 ;;; Dead summaries.
5393
5394 (defvar gnus-dead-summary-mode-map nil)
5395
5396 (unless gnus-dead-summary-mode-map
5397   (setq gnus-dead-summary-mode-map (make-keymap))
5398   (suppress-keymap gnus-dead-summary-mode-map)
5399   (substitute-key-definition
5400    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5401   (let ((keys '("\C-d" "\r" "\177" [delete])))
5402     (while keys
5403       (define-key gnus-dead-summary-mode-map
5404         (pop keys) 'gnus-summary-wake-up-the-dead))))
5405
5406 (defvar gnus-dead-summary-mode nil
5407   "Minor mode for Gnus summary buffers.")
5408
5409 (defun gnus-dead-summary-mode (&optional arg)
5410   "Minor mode for Gnus summary buffers."
5411   (interactive "P")
5412   (when (eq major-mode 'gnus-summary-mode)
5413     (make-local-variable 'gnus-dead-summary-mode)
5414     (setq gnus-dead-summary-mode
5415           (if (null arg) (not gnus-dead-summary-mode)
5416             (> (prefix-numeric-value arg) 0)))
5417     (when gnus-dead-summary-mode
5418       (gnus-add-minor-mode
5419        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5420
5421 (defun gnus-deaden-summary ()
5422   "Make the current summary buffer into a dead summary buffer."
5423   ;; Kill any previous dead summary buffer.
5424   (when (and gnus-dead-summary
5425              (buffer-name gnus-dead-summary))
5426     (save-excursion
5427       (set-buffer gnus-dead-summary)
5428       (when gnus-dead-summary-mode
5429         (kill-buffer (current-buffer)))))
5430   ;; Make this the current dead summary.
5431   (setq gnus-dead-summary (current-buffer))
5432   (gnus-dead-summary-mode 1)
5433   (let ((name (buffer-name)))
5434     (when (string-match "Summary" name)
5435       (rename-buffer
5436        (concat (substring name 0 (match-beginning 0)) "Dead "
5437                (substring name (match-beginning 0)))
5438        t))))
5439
5440 (defun gnus-kill-or-deaden-summary (buffer)
5441   "Kill or deaden the summary BUFFER."
5442   (save-excursion
5443     (when (and (buffer-name buffer)
5444                (not gnus-single-article-buffer))
5445       (save-excursion
5446         (set-buffer buffer)
5447         (gnus-kill-buffer gnus-article-buffer)
5448         (gnus-kill-buffer gnus-original-article-buffer)))
5449     (cond (gnus-kill-summary-on-exit
5450            (when (and gnus-use-trees
5451                       (gnus-buffer-exists-p buffer))
5452              (save-excursion
5453                (set-buffer buffer)
5454                (gnus-tree-close gnus-newsgroup-name)))
5455            (gnus-kill-buffer buffer))
5456           ((gnus-buffer-exists-p buffer)
5457            (save-excursion
5458              (set-buffer buffer)
5459              (gnus-deaden-summary))))))
5460
5461 (defun gnus-summary-wake-up-the-dead (&rest args)
5462   "Wake up the dead summary buffer."
5463   (interactive)
5464   (gnus-dead-summary-mode -1)
5465   (let ((name (buffer-name)))
5466     (when (string-match "Dead " name)
5467       (rename-buffer
5468        (concat (substring name 0 (match-beginning 0))
5469                (substring name (match-end 0)))
5470        t)))
5471   (gnus-message 3 "This dead summary is now alive again"))
5472
5473 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5474 (defun gnus-summary-fetch-faq (&optional faq-dir)
5475   "Fetch the FAQ for the current group.
5476 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5477 in."
5478   (interactive
5479    (list
5480     (when current-prefix-arg
5481       (completing-read
5482        "Faq dir: " (and (listp gnus-group-faq-directory)
5483                         (mapcar (lambda (file) (list file))
5484                                 gnus-group-faq-directory))))))
5485   (let (gnus-faq-buffer)
5486     (when (setq gnus-faq-buffer
5487                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5488       (gnus-configure-windows 'summary-faq))))
5489
5490 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5491 (defun gnus-summary-describe-group (&optional force)
5492   "Describe the current newsgroup."
5493   (interactive "P")
5494   (gnus-group-describe-group force gnus-newsgroup-name))
5495
5496 (defun gnus-summary-describe-briefly ()
5497   "Describe summary mode commands briefly."
5498   (interactive)
5499   (gnus-message 6
5500                 (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")))
5501
5502 ;; Walking around group mode buffer from summary mode.
5503
5504 (defun gnus-summary-next-group (&optional no-article target-group backward)
5505   "Exit current newsgroup and then select next unread newsgroup.
5506 If prefix argument NO-ARTICLE is non-nil, no article is selected
5507 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5508 previous group instead."
5509   (interactive "P")
5510   ;; Stop pre-fetching.
5511   (gnus-async-halt-prefetch)
5512   (let ((current-group gnus-newsgroup-name)
5513         (current-buffer (current-buffer))
5514         entered)
5515     ;; First we semi-exit this group to update Xrefs and all variables.
5516     ;; We can't do a real exit, because the window conf must remain
5517     ;; the same in case the user is prompted for info, and we don't
5518     ;; want the window conf to change before that...
5519     (gnus-summary-exit t)
5520     (while (not entered)
5521       ;; Then we find what group we are supposed to enter.
5522       (set-buffer gnus-group-buffer)
5523       (gnus-group-jump-to-group current-group)
5524       (setq target-group
5525             (or target-group
5526                 (if (eq gnus-keep-same-level 'best)
5527                     (gnus-summary-best-group gnus-newsgroup-name)
5528                   (gnus-summary-search-group backward gnus-keep-same-level))))
5529       (if (not target-group)
5530           ;; There are no further groups, so we return to the group
5531           ;; buffer.
5532           (progn
5533             (gnus-message 5 "Returning to the group buffer")
5534             (setq entered t)
5535             (when (gnus-buffer-live-p current-buffer)
5536               (set-buffer current-buffer)
5537               (gnus-summary-exit))
5538             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5539         ;; We try to enter the target group.
5540         (gnus-group-jump-to-group target-group)
5541         (let ((unreads (gnus-group-group-unread)))
5542           (if (and (or (eq t unreads)
5543                        (and unreads (not (zerop unreads))))
5544                    (gnus-summary-read-group
5545                     target-group nil no-article
5546                     (and (buffer-name current-buffer) current-buffer)
5547                     nil backward))
5548               (setq entered t)
5549             (setq current-group target-group
5550                   target-group nil)))))))
5551
5552 (defun gnus-summary-prev-group (&optional no-article)
5553   "Exit current newsgroup and then select previous unread newsgroup.
5554 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5555   (interactive "P")
5556   (gnus-summary-next-group no-article nil t))
5557
5558 ;; Walking around summary lines.
5559
5560 (defun gnus-summary-first-subject (&optional unread undownloaded)
5561   "Go to the first unread subject.
5562 If UNREAD is non-nil, go to the first unread article.
5563 Returns the article selected or nil if there are no unread articles."
5564   (interactive "P")
5565   (prog1
5566       (cond
5567        ;; Empty summary.
5568        ((null gnus-newsgroup-data)
5569         (gnus-message 3 "No articles in the group")
5570         nil)
5571        ;; Pick the first article.
5572        ((not unread)
5573         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5574         (gnus-data-number (car gnus-newsgroup-data)))
5575        ;; No unread articles.
5576        ((null gnus-newsgroup-unreads)
5577         (gnus-message 3 "No more unread articles")
5578         nil)
5579        ;; Find the first unread article.
5580        (t
5581         (let ((data gnus-newsgroup-data))
5582           (while (and data
5583                       (and (not (and undownloaded
5584                                      (eq gnus-undownloaded-mark
5585                                          (gnus-data-mark (car data)))))
5586                            (not (gnus-data-unread-p (car data)))))
5587             (setq data (cdr data)))
5588           (when data
5589             (goto-char (gnus-data-pos (car data)))
5590             (gnus-data-number (car data))))))
5591     (gnus-summary-position-point)))
5592
5593 (defun gnus-summary-next-subject (n &optional unread dont-display)
5594   "Go to next N'th summary line.
5595 If N is negative, go to the previous N'th subject line.
5596 If UNREAD is non-nil, only unread articles are selected.
5597 The difference between N and the actual number of steps taken is
5598 returned."
5599   (interactive "p")
5600   (let ((backward (< n 0))
5601         (n (abs n)))
5602     (while (and (> n 0)
5603                 (if backward
5604                     (gnus-summary-find-prev unread)
5605                   (gnus-summary-find-next unread)))
5606       (gnus-summary-show-thread)
5607       (setq n (1- n)))
5608     (when (/= 0 n)
5609       (gnus-message 7 "No more%s articles"
5610                     (if unread " unread" "")))
5611     (unless dont-display
5612       (gnus-summary-recenter)
5613       (gnus-summary-position-point))
5614     n))
5615
5616 (defun gnus-summary-next-unread-subject (n)
5617   "Go to next N'th unread summary line."
5618   (interactive "p")
5619   (gnus-summary-next-subject n t))
5620
5621 (defun gnus-summary-prev-subject (n &optional unread)
5622   "Go to previous N'th summary line.
5623 If optional argument UNREAD is non-nil, only unread article is selected."
5624   (interactive "p")
5625   (gnus-summary-next-subject (- n) unread))
5626
5627 (defun gnus-summary-prev-unread-subject (n)
5628   "Go to previous N'th unread summary line."
5629   (interactive "p")
5630   (gnus-summary-next-subject (- n) t))
5631
5632 (defun gnus-summary-goto-subject (article &optional force silent)
5633   "Go the subject line of ARTICLE.
5634 If FORCE, also allow jumping to articles not currently shown."
5635   (interactive "nArticle number: ")
5636   (let ((b (point))
5637         (data (gnus-data-find article)))
5638     ;; We read in the article if we have to.
5639     (and (not data)
5640          force
5641          (gnus-summary-insert-subject
5642           article
5643           (if (or (numberp force) (vectorp force)) force)
5644           t)
5645          (setq data (gnus-data-find article)))
5646     (goto-char b)
5647     (if (not data)
5648         (progn
5649           (unless silent
5650             (gnus-message 3 "Can't find article %d" article))
5651           nil)
5652       (goto-char (gnus-data-pos data))
5653       (gnus-summary-position-point)
5654       article)))
5655
5656 ;; Walking around summary lines with displaying articles.
5657
5658 (defun gnus-summary-expand-window (&optional arg)
5659   "Make the summary buffer take up the entire Emacs frame.
5660 Given a prefix, will force an `article' buffer configuration."
5661   (interactive "P")
5662   (if arg
5663       (gnus-configure-windows 'article 'force)
5664     (gnus-configure-windows 'summary 'force)))
5665
5666 (defun gnus-summary-display-article (article &optional all-header)
5667   "Display ARTICLE in article buffer."
5668   (gnus-set-global-variables)
5669   (if (null article)
5670       nil
5671     (prog1
5672         (if gnus-summary-display-article-function
5673             (funcall gnus-summary-display-article-function article all-header)
5674           (gnus-article-prepare article all-header))
5675       (gnus-run-hooks 'gnus-select-article-hook)
5676       (when (and gnus-current-article
5677                  (not (zerop gnus-current-article)))
5678         (gnus-summary-goto-subject gnus-current-article))
5679       (gnus-summary-recenter)
5680       (when (and gnus-use-trees gnus-show-threads)
5681         (gnus-possibly-generate-tree article)
5682         (gnus-highlight-selected-tree article))
5683       ;; Successfully display article.
5684       (gnus-article-set-window-start
5685        (cdr (assq article gnus-newsgroup-bookmarks))))))
5686
5687 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5688   "Select the current article.
5689 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5690 non-nil, the article will be re-fetched even if it already present in
5691 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5692 be displayed."
5693   ;; Make sure we are in the summary buffer to work around bbdb bug.
5694   (unless (eq major-mode 'gnus-summary-mode)
5695     (set-buffer gnus-summary-buffer))
5696   (let ((article (or article (gnus-summary-article-number)))
5697         (all-headers (not (not all-headers))) ;Must be T or NIL.
5698         gnus-summary-display-article-function
5699         did)
5700     (and (not pseudo)
5701          (gnus-summary-article-pseudo-p article)
5702          (error "This is a pseudo-article"))
5703     (prog1
5704         (save-excursion
5705           (set-buffer gnus-summary-buffer)
5706           (if (or (and gnus-single-article-buffer
5707                        (or (null gnus-current-article)
5708                            (null gnus-article-current)
5709                            (null (get-buffer gnus-article-buffer))
5710                            (not (eq article (cdr gnus-article-current)))
5711                            (not (equal (car gnus-article-current)
5712                                        gnus-newsgroup-name))))
5713                   (and (not gnus-single-article-buffer)
5714                        (or (null gnus-current-article)
5715                            (not (eq gnus-current-article article))))
5716                   force)
5717               ;; The requested article is different from the current article.
5718               (prog1
5719                   (gnus-summary-display-article article all-headers)
5720                 (setq did article))
5721             (when (or all-headers gnus-show-all-headers)
5722               (gnus-article-show-all-headers))
5723             'old))
5724       (when did
5725         (gnus-article-set-window-start
5726          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5727
5728 (defun gnus-summary-set-current-mark (&optional current-mark)
5729   "Obsolete function."
5730   nil)
5731
5732 (defun gnus-summary-next-article (&optional unread subject backward push)
5733   "Select the next article.
5734 If UNREAD, only unread articles are selected.
5735 If SUBJECT, only articles with SUBJECT are selected.
5736 If BACKWARD, the previous article is selected instead of the next."
5737   (interactive "P")
5738   (cond
5739    ;; Is there such an article?
5740    ((and (gnus-summary-search-forward unread subject backward)
5741          (or (gnus-summary-display-article (gnus-summary-article-number))
5742              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5743     (gnus-summary-position-point))
5744    ;; If not, we try the first unread, if that is wanted.
5745    ((and subject
5746          gnus-auto-select-same
5747          (gnus-summary-first-unread-article))
5748     (gnus-summary-position-point)
5749     (gnus-message 6 "Wrapped"))
5750    ;; Try to get next/previous article not displayed in this group.
5751    ((and gnus-auto-extend-newsgroup
5752          (not unread) (not subject))
5753     (gnus-summary-goto-article
5754      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5755      nil (count-lines (point-min) (point))))
5756    ;; Go to next/previous group.
5757    (t
5758     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5759       (gnus-summary-jump-to-group gnus-newsgroup-name))
5760     (let ((cmd last-command-char)
5761           (point
5762            (save-excursion
5763              (set-buffer gnus-group-buffer)
5764              (point)))
5765           (group
5766            (if (eq gnus-keep-same-level 'best)
5767                (gnus-summary-best-group gnus-newsgroup-name)
5768              (gnus-summary-search-group backward gnus-keep-same-level))))
5769       ;; For some reason, the group window gets selected.  We change
5770       ;; it back.
5771       (select-window (get-buffer-window (current-buffer)))
5772       ;; Select next unread newsgroup automagically.
5773       (cond
5774        ((or (not gnus-auto-select-next)
5775             (not cmd))
5776         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5777        ((or (eq gnus-auto-select-next 'quietly)
5778             (and (eq gnus-auto-select-next 'slightly-quietly)
5779                  push)
5780             (and (eq gnus-auto-select-next 'almost-quietly)
5781                  (gnus-summary-last-article-p)))
5782         ;; Select quietly.
5783         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5784             (gnus-summary-exit)
5785           (gnus-message 7 "No more%s articles (%s)..."
5786                         (if unread " unread" "")
5787                         (if group (concat "selecting " group)
5788                           "exiting"))
5789           (gnus-summary-next-group nil group backward)))
5790        (t
5791         (when (gnus-key-press-event-p last-input-event)
5792           (gnus-summary-walk-group-buffer
5793            gnus-newsgroup-name cmd unread backward point))))))))
5794
5795 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5796   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5797                       (?\C-p (gnus-group-prev-unread-group 1))))
5798         (cursor-in-echo-area t)
5799         keve key group ended)
5800     (save-excursion
5801       (set-buffer gnus-group-buffer)
5802       (goto-char start)
5803       (setq group
5804             (if (eq gnus-keep-same-level 'best)
5805                 (gnus-summary-best-group gnus-newsgroup-name)
5806               (gnus-summary-search-group backward gnus-keep-same-level))))
5807     (while (not ended)
5808       (gnus-message
5809        5 "No more%s articles%s" (if unread " unread" "")
5810        (if (and group
5811                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5812            (format " (Type %s for %s [%s])"
5813                    (single-key-description cmd) group
5814                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5815          (format " (Type %s to exit %s)"
5816                  (single-key-description cmd)
5817                  gnus-newsgroup-name)))
5818       ;; Confirm auto selection.
5819       (setq key (car (setq keve (gnus-read-event-char))))
5820       (setq ended t)
5821       (cond
5822        ((assq key keystrokes)
5823         (let ((obuf (current-buffer)))
5824           (switch-to-buffer gnus-group-buffer)
5825           (when group
5826             (gnus-group-jump-to-group group))
5827           (eval (cadr (assq key keystrokes)))
5828           (setq group (gnus-group-group-name))
5829           (switch-to-buffer obuf))
5830         (setq ended nil))
5831        ((equal key cmd)
5832         (if (or (not group)
5833                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5834             (gnus-summary-exit)
5835           (gnus-summary-next-group nil group backward)))
5836        (t
5837         (push (cdr keve) unread-command-events))))))
5838
5839 (defun gnus-summary-next-unread-article ()
5840   "Select unread article after current one."
5841   (interactive)
5842   (gnus-summary-next-article
5843    (or (not (eq gnus-summary-goto-unread 'never))
5844        (gnus-summary-last-article-p (gnus-summary-article-number)))
5845    (and gnus-auto-select-same
5846         (gnus-summary-article-subject))))
5847
5848 (defun gnus-summary-prev-article (&optional unread subject)
5849   "Select the article after the current one.
5850 If UNREAD is non-nil, only unread articles are selected."
5851   (interactive "P")
5852   (gnus-summary-next-article unread subject t))
5853
5854 (defun gnus-summary-prev-unread-article ()
5855   "Select unread article before current one."
5856   (interactive)
5857   (gnus-summary-prev-article
5858    (or (not (eq gnus-summary-goto-unread 'never))
5859        (gnus-summary-first-article-p (gnus-summary-article-number)))
5860    (and gnus-auto-select-same
5861         (gnus-summary-article-subject))))
5862
5863 (defun gnus-summary-next-page (&optional lines circular)
5864   "Show next page of the selected article.
5865 If at the end of the current article, select the next article.
5866 LINES says how many lines should be scrolled up.
5867
5868 If CIRCULAR is non-nil, go to the start of the article instead of
5869 selecting the next article when reaching the end of the current
5870 article."
5871   (interactive "P")
5872   (setq gnus-summary-buffer (current-buffer))
5873   (gnus-set-global-variables)
5874   (let ((article (gnus-summary-article-number))
5875         (article-window (get-buffer-window gnus-article-buffer t))
5876         endp)
5877     ;; If the buffer is empty, we have no article.
5878     (unless article
5879       (error "No article to select"))
5880     (gnus-configure-windows 'article)
5881     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5882         (if (and (eq gnus-summary-goto-unread 'never)
5883                  (not (gnus-summary-last-article-p article)))
5884             (gnus-summary-next-article)
5885           (gnus-summary-next-unread-article))
5886       (if (or (null gnus-current-article)
5887               (null gnus-article-current)
5888               (/= article (cdr gnus-article-current))
5889               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5890           ;; Selected subject is different from current article's.
5891           (gnus-summary-display-article article)
5892         (when article-window
5893           (gnus-eval-in-buffer-window gnus-article-buffer
5894             (setq endp (gnus-article-next-page lines)))
5895           (when endp
5896             (cond (circular
5897                    (gnus-summary-beginning-of-article))
5898                   (lines
5899                    (gnus-message 3 "End of message"))
5900                   ((null lines)
5901                    (if (and (eq gnus-summary-goto-unread 'never)
5902                             (not (gnus-summary-last-article-p article)))
5903                        (gnus-summary-next-article)
5904                      (gnus-summary-next-unread-article))))))))
5905     (gnus-summary-recenter)
5906     (gnus-summary-position-point)))
5907
5908 (defun gnus-summary-prev-page (&optional lines move)
5909   "Show previous page of selected article.
5910 Argument LINES specifies lines to be scrolled down.
5911 If MOVE, move to the previous unread article if point is at
5912 the beginning of the buffer."
5913   (interactive "P")
5914   (let ((article (gnus-summary-article-number))
5915         (article-window (get-buffer-window gnus-article-buffer t))
5916         endp)
5917     (gnus-configure-windows 'article)
5918     (if (or (null gnus-current-article)
5919             (null gnus-article-current)
5920             (/= article (cdr gnus-article-current))
5921             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5922         ;; Selected subject is different from current article's.
5923         (gnus-summary-display-article article)
5924       (gnus-summary-recenter)
5925       (when article-window
5926         (gnus-eval-in-buffer-window gnus-article-buffer
5927           (setq endp (gnus-article-prev-page lines)))
5928         (when (and move endp)
5929           (cond (lines
5930                  (gnus-message 3 "Beginning of message"))
5931                 ((null lines)
5932                  (if (and (eq gnus-summary-goto-unread 'never)
5933                           (not (gnus-summary-first-article-p article)))
5934                      (gnus-summary-prev-article)
5935                    (gnus-summary-prev-unread-article))))))))
5936   (gnus-summary-position-point))
5937
5938 (defun gnus-summary-prev-page-or-article (&optional lines)
5939   "Show previous page of selected article.
5940 Argument LINES specifies lines to be scrolled down.
5941 If at the beginning of the article, go to the next article."
5942   (interactive "P")
5943   (gnus-summary-prev-page lines t))
5944
5945 (defun gnus-summary-scroll-up (lines)
5946   "Scroll up (or down) one line current article.
5947 Argument LINES specifies lines to be scrolled up (or down if negative)."
5948   (interactive "p")
5949   (gnus-configure-windows 'article)
5950   (gnus-summary-show-thread)
5951   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5952     (gnus-eval-in-buffer-window gnus-article-buffer
5953       (cond ((> lines 0)
5954              (when (gnus-article-next-page lines)
5955                (gnus-message 3 "End of message")))
5956             ((< lines 0)
5957              (gnus-article-prev-page (- lines))))))
5958   (gnus-summary-recenter)
5959   (gnus-summary-position-point))
5960
5961 (defun gnus-summary-scroll-down (lines)
5962   "Scroll down (or up) one line current article.
5963 Argument LINES specifies lines to be scrolled down (or up if negative)."
5964   (interactive "p")
5965   (gnus-summary-scroll-up (- lines)))
5966
5967 (defun gnus-summary-next-same-subject ()
5968   "Select next article which has the same subject as current one."
5969   (interactive)
5970   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5971
5972 (defun gnus-summary-prev-same-subject ()
5973   "Select previous article which has the same subject as current one."
5974   (interactive)
5975   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5976
5977 (defun gnus-summary-next-unread-same-subject ()
5978   "Select next unread article which has the same subject as current one."
5979   (interactive)
5980   (gnus-summary-next-article t (gnus-summary-article-subject)))
5981
5982 (defun gnus-summary-prev-unread-same-subject ()
5983   "Select previous unread article which has the same subject as current one."
5984   (interactive)
5985   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5986
5987 (defun gnus-summary-first-unread-article ()
5988   "Select the first unread article.
5989 Return nil if there are no unread articles."
5990   (interactive)
5991   (prog1
5992       (when (gnus-summary-first-subject t)
5993         (gnus-summary-show-thread)
5994         (gnus-summary-first-subject t)
5995         (gnus-summary-display-article (gnus-summary-article-number)))
5996     (gnus-summary-position-point)))
5997
5998 (defun gnus-summary-first-unread-subject ()
5999   "Place the point on the subject line of the first unread article.
6000 Return nil if there are no unread articles."
6001   (interactive)
6002   (prog1
6003       (when (gnus-summary-first-subject t)
6004         (gnus-summary-show-thread)
6005         (gnus-summary-first-subject t))
6006     (gnus-summary-position-point)))
6007
6008 (defun gnus-summary-first-article ()
6009   "Select the first article.
6010 Return nil if there are no articles."
6011   (interactive)
6012   (prog1
6013       (when (gnus-summary-first-subject)
6014         (gnus-summary-show-thread)
6015         (gnus-summary-first-subject)
6016         (gnus-summary-display-article (gnus-summary-article-number)))
6017     (gnus-summary-position-point)))
6018
6019 (defun gnus-summary-best-unread-article ()
6020   "Select the unread article with the highest score."
6021   (interactive)
6022   (let ((best -1000000)
6023         (data gnus-newsgroup-data)
6024         article score)
6025     (while data
6026       (and (gnus-data-unread-p (car data))
6027            (> (setq score
6028                     (gnus-summary-article-score (gnus-data-number (car data))))
6029               best)
6030            (setq best score
6031                  article (gnus-data-number (car data))))
6032       (setq data (cdr data)))
6033     (prog1
6034         (if article
6035             (gnus-summary-goto-article article)
6036           (error "No unread articles"))
6037       (gnus-summary-position-point))))
6038
6039 (defun gnus-summary-last-subject ()
6040   "Go to the last displayed subject line in the group."
6041   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6042     (when article
6043       (gnus-summary-goto-subject article))))
6044
6045 (defun gnus-summary-goto-article (article &optional all-headers force)
6046   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6047 If ALL-HEADERS is non-nil, no header lines are hidden.
6048 If FORCE, go to the article even if it isn't displayed.  If FORCE
6049 is a number, it is the line the article is to be displayed on."
6050   (interactive
6051    (list
6052     (completing-read
6053      "Article number or Message-ID: "
6054      (mapcar (lambda (number) (list (int-to-string number)))
6055              gnus-newsgroup-limit))
6056     current-prefix-arg
6057     t))
6058   (prog1
6059       (if (and (stringp article)
6060                (string-match "@" article))
6061           (gnus-summary-refer-article article)
6062         (when (stringp article)
6063           (setq article (string-to-number article)))
6064         (if (gnus-summary-goto-subject article force)
6065             (gnus-summary-display-article article all-headers)
6066           (gnus-message 4 "Couldn't go to article %s" article) nil))
6067     (gnus-summary-position-point)))
6068
6069 (defun gnus-summary-goto-last-article ()
6070   "Go to the previously read article."
6071   (interactive)
6072   (prog1
6073       (when gnus-last-article
6074         (gnus-summary-goto-article gnus-last-article nil t))
6075     (gnus-summary-position-point)))
6076
6077 (defun gnus-summary-pop-article (number)
6078   "Pop one article off the history and go to the previous.
6079 NUMBER articles will be popped off."
6080   (interactive "p")
6081   (let (to)
6082     (setq gnus-newsgroup-history
6083           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6084     (if to
6085         (gnus-summary-goto-article (car to) nil t)
6086       (error "Article history empty")))
6087   (gnus-summary-position-point))
6088
6089 ;; Summary commands and functions for limiting the summary buffer.
6090
6091 (defun gnus-summary-limit-to-articles (n)
6092   "Limit the summary buffer to the next N articles.
6093 If not given a prefix, use the process marked articles instead."
6094   (interactive "P")
6095   (prog1
6096       (let ((articles (gnus-summary-work-articles n)))
6097         (setq gnus-newsgroup-processable nil)
6098         (gnus-summary-limit articles))
6099     (gnus-summary-position-point)))
6100
6101 (defun gnus-summary-pop-limit (&optional total)
6102   "Restore the previous limit.
6103 If given a prefix, remove all limits."
6104   (interactive "P")
6105   (when total
6106     (setq gnus-newsgroup-limits
6107           (list (mapcar (lambda (h) (mail-header-number h))
6108                         gnus-newsgroup-headers))))
6109   (unless gnus-newsgroup-limits
6110     (error "No limit to pop"))
6111   (prog1
6112       (gnus-summary-limit nil 'pop)
6113     (gnus-summary-position-point)))
6114
6115 (defun gnus-summary-limit-to-subject (subject &optional header)
6116   "Limit the summary buffer to articles that have subjects that match a regexp."
6117   (interactive "sLimit to subject (regexp): ")
6118   (unless header
6119     (setq header "subject"))
6120   (when (not (equal "" subject))
6121     (prog1
6122         (let ((articles (gnus-summary-find-matching
6123                          (or header "subject") subject 'all)))
6124           (unless articles
6125             (error "Found no matches for \"%s\"" subject))
6126           (gnus-summary-limit articles))
6127       (gnus-summary-position-point))))
6128
6129 (defun gnus-summary-limit-to-author (from)
6130   "Limit the summary buffer to articles that have authors that match a regexp."
6131   (interactive "sLimit to author (regexp): ")
6132   (gnus-summary-limit-to-subject from "from"))
6133
6134 (defun gnus-summary-limit-to-age (age &optional younger-p)
6135   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6136 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6137 articles that are younger than AGE days."
6138   (interactive "nTime in days: \nP")
6139   (prog1
6140       (let ((data gnus-newsgroup-data)
6141             (cutoff (days-to-time age))
6142             articles d date is-younger)
6143         (while (setq d (pop data))
6144           (when (and (vectorp (gnus-data-header d))
6145                      (setq date (mail-header-date (gnus-data-header d))))
6146             (setq is-younger (time-less-p
6147                               (time-since (date-to-time date))
6148                               cutoff))
6149             (when (if younger-p
6150                       is-younger
6151                     (not is-younger))
6152               (push (gnus-data-number d) articles))))
6153         (gnus-summary-limit (nreverse articles)))
6154     (gnus-summary-position-point)))
6155
6156 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6157 (make-obsolete
6158  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6159
6160 (defun gnus-summary-limit-to-unread (&optional all)
6161   "Limit the summary buffer to articles that are not marked as read.
6162 If ALL is non-nil, limit strictly to unread articles."
6163   (interactive "P")
6164   (if all
6165       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6166     (gnus-summary-limit-to-marks
6167      ;; Concat all the marks that say that an article is read and have
6168      ;; those removed.
6169      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6170            gnus-killed-mark gnus-kill-file-mark
6171            gnus-low-score-mark gnus-expirable-mark
6172            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6173            gnus-duplicate-mark gnus-souped-mark)
6174      'reverse)))
6175
6176 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6177 (make-obsolete 'gnus-summary-delete-marked-with
6178                'gnus-summary-limit-exlude-marks)
6179
6180 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6181   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6182 If REVERSE, limit the summary buffer to articles that are marked
6183 with MARKS.  MARKS can either be a string of marks or a list of marks.
6184 Returns how many articles were removed."
6185   (interactive "sMarks: ")
6186   (gnus-summary-limit-to-marks marks t))
6187
6188 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6189   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6190 If REVERSE (the prefix), limit the summary buffer to articles that are
6191 not marked with MARKS.  MARKS can either be a string of marks or a
6192 list of marks.
6193 Returns how many articles were removed."
6194   (interactive "sMarks: \nP")
6195   (prog1
6196       (let ((data gnus-newsgroup-data)
6197             (marks (if (listp marks) marks
6198                      (append marks nil))) ; Transform to list.
6199             articles)
6200         (while data
6201           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6202                   (memq (gnus-data-mark (car data)) marks))
6203             (push (gnus-data-number (car data)) articles))
6204           (setq data (cdr data)))
6205         (gnus-summary-limit articles))
6206     (gnus-summary-position-point)))
6207
6208 (defun gnus-summary-limit-to-score (&optional score)
6209   "Limit to articles with score at or above SCORE."
6210   (interactive "P")
6211   (setq score (if score
6212                   (prefix-numeric-value score)
6213                 (or gnus-summary-default-score 0)))
6214   (let ((data gnus-newsgroup-data)
6215         articles)
6216     (while data
6217       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6218                 score)
6219         (push (gnus-data-number (car data)) articles))
6220       (setq data (cdr data)))
6221     (prog1
6222         (gnus-summary-limit articles)
6223       (gnus-summary-position-point))))
6224
6225 (defun gnus-summary-limit-include-thread (id)
6226   "Display all the hidden articles that in the current thread."
6227   (interactive (list (mail-header-id (gnus-summary-article-header))))
6228   (let ((articles (gnus-articles-in-thread
6229                    (gnus-id-to-thread (gnus-root-id id)))))
6230     (prog1
6231         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6232       (gnus-summary-position-point))))
6233
6234 (defun gnus-summary-limit-include-dormant ()
6235   "Display all the hidden articles that are marked as dormant.
6236 Note that this command only works on a subset of the articles currently
6237 fetched for this group."
6238   (interactive)
6239   (unless gnus-newsgroup-dormant
6240     (error "There are no dormant articles in this group"))
6241   (prog1
6242       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6243     (gnus-summary-position-point)))
6244
6245 (defun gnus-summary-limit-exclude-dormant ()
6246   "Hide all dormant articles."
6247   (interactive)
6248   (prog1
6249       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6250     (gnus-summary-position-point)))
6251
6252 (defun gnus-summary-limit-exclude-childless-dormant ()
6253   "Hide all dormant articles that have no children."
6254   (interactive)
6255   (let ((data (gnus-data-list t))
6256         articles d children)
6257     ;; Find all articles that are either not dormant or have
6258     ;; children.
6259     (while (setq d (pop data))
6260       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6261                 (and (setq children
6262                            (gnus-article-children (gnus-data-number d)))
6263                      (let (found)
6264                        (while children
6265                          (when (memq (car children) articles)
6266                            (setq children nil
6267                                  found t))
6268                          (pop children))
6269                        found)))
6270         (push (gnus-data-number d) articles)))
6271     ;; Do the limiting.
6272     (prog1
6273         (gnus-summary-limit articles)
6274       (gnus-summary-position-point))))
6275
6276 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6277   "Mark all unread excluded articles as read.
6278 If ALL, mark even excluded ticked and dormants as read."
6279   (interactive "P")
6280   (let ((articles (gnus-sorted-complement
6281                    (sort
6282                     (mapcar (lambda (h) (mail-header-number h))
6283                             gnus-newsgroup-headers)
6284                     '<)
6285                    (sort gnus-newsgroup-limit '<)))
6286         article)
6287     (setq gnus-newsgroup-unreads
6288           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6289     (if all
6290         (setq gnus-newsgroup-dormant nil
6291               gnus-newsgroup-marked nil
6292               gnus-newsgroup-reads
6293               (nconc
6294                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6295                gnus-newsgroup-reads))
6296       (while (setq article (pop articles))
6297         (unless (or (memq article gnus-newsgroup-dormant)
6298                     (memq article gnus-newsgroup-marked))
6299           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6300
6301 (defun gnus-summary-limit (articles &optional pop)
6302   (if pop
6303       ;; We pop the previous limit off the stack and use that.
6304       (setq articles (car gnus-newsgroup-limits)
6305             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6306     ;; We use the new limit, so we push the old limit on the stack.
6307     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6308   ;; Set the limit.
6309   (setq gnus-newsgroup-limit articles)
6310   (let ((total (length gnus-newsgroup-data))
6311         (data (gnus-data-find-list (gnus-summary-article-number)))
6312         (gnus-summary-mark-below nil)   ; Inhibit this.
6313         found)
6314     ;; This will do all the work of generating the new summary buffer
6315     ;; according to the new limit.
6316     (gnus-summary-prepare)
6317     ;; Hide any threads, possibly.
6318     (and gnus-show-threads
6319          gnus-thread-hide-subtree
6320          (gnus-summary-hide-all-threads))
6321     ;; Try to return to the article you were at, or one in the
6322     ;; neighborhood.
6323     (when data
6324       ;; We try to find some article after the current one.
6325       (while data
6326         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6327           (setq data nil
6328                 found t))
6329         (setq data (cdr data))))
6330     (unless found
6331       ;; If there is no data, that means that we were after the last
6332       ;; article.  The same goes when we can't find any articles
6333       ;; after the current one.
6334       (goto-char (point-max))
6335       (gnus-summary-find-prev))
6336     (gnus-set-mode-line 'summary)
6337     ;; We return how many articles were removed from the summary
6338     ;; buffer as a result of the new limit.
6339     (- total (length gnus-newsgroup-data))))
6340
6341 (defsubst gnus-invisible-cut-children (threads)
6342   (let ((num 0))
6343     (while threads
6344       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6345         (incf num))
6346       (pop threads))
6347     (< num 2)))
6348
6349 (defsubst gnus-cut-thread (thread)
6350   "Go forwards in the thread until we find an article that we want to display."
6351   (when (or (eq gnus-fetch-old-headers 'some)
6352             (eq gnus-fetch-old-headers 'invisible)
6353             (eq gnus-build-sparse-threads 'some)
6354             (eq gnus-build-sparse-threads 'more))
6355     ;; Deal with old-fetched headers and sparse threads.
6356     (while (and
6357             thread
6358             (or
6359              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6360              (gnus-summary-article-ancient-p
6361               (mail-header-number (car thread))))
6362             (if (or (<= (length (cdr thread)) 1)
6363                     (eq gnus-fetch-old-headers 'invisible))
6364                 (setq gnus-newsgroup-limit
6365                       (delq (mail-header-number (car thread))
6366                             gnus-newsgroup-limit)
6367                       thread (cadr thread))
6368               (when (gnus-invisible-cut-children (cdr thread))
6369                 (let ((th (cdr thread)))
6370                   (while th
6371                     (if (memq (mail-header-number (caar th))
6372                               gnus-newsgroup-limit)
6373                         (setq thread (car th)
6374                               th nil)
6375                       (setq th (cdr th))))))))))
6376   thread)
6377
6378 (defun gnus-cut-threads (threads)
6379   "Cut off all uninteresting articles from the beginning of threads."
6380   (when (or (eq gnus-fetch-old-headers 'some)
6381             (eq gnus-fetch-old-headers 'invisible)
6382             (eq gnus-build-sparse-threads 'some)
6383             (eq gnus-build-sparse-threads 'more))
6384     (let ((th threads))
6385       (while th
6386         (setcar th (gnus-cut-thread (car th)))
6387         (setq th (cdr th)))))
6388   ;; Remove nixed out threads.
6389   (delq nil threads))
6390
6391 (defun gnus-summary-initial-limit (&optional show-if-empty)
6392   "Figure out what the initial limit is supposed to be on group entry.
6393 This entails weeding out unwanted dormants, low-scored articles,
6394 fetch-old-headers verbiage, and so on."
6395   ;; Most groups have nothing to remove.
6396   (if (or gnus-inhibit-limiting
6397           (and (null gnus-newsgroup-dormant)
6398                (not (eq gnus-fetch-old-headers 'some))
6399                (not (eq gnus-fetch-old-headers 'invisible))
6400                (null gnus-summary-expunge-below)
6401                (not (eq gnus-build-sparse-threads 'some))
6402                (not (eq gnus-build-sparse-threads 'more))
6403                (null gnus-thread-expunge-below)
6404                (not gnus-use-nocem)))
6405       ()                                ; Do nothing.
6406     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6407     (setq gnus-newsgroup-limit nil)
6408     (mapatoms
6409      (lambda (node)
6410        (unless (car (symbol-value node))
6411          ;; These threads have no parents -- they are roots.
6412          (let ((nodes (cdr (symbol-value node)))
6413                thread)
6414            (while nodes
6415              (if (and gnus-thread-expunge-below
6416                       (< (gnus-thread-total-score (car nodes))
6417                          gnus-thread-expunge-below))
6418                  (gnus-expunge-thread (pop nodes))
6419                (setq thread (pop nodes))
6420                (gnus-summary-limit-children thread))))))
6421      gnus-newsgroup-dependencies)
6422     ;; If this limitation resulted in an empty group, we might
6423     ;; pop the previous limit and use it instead.
6424     (when (and (not gnus-newsgroup-limit)
6425                show-if-empty)
6426       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6427     gnus-newsgroup-limit))
6428
6429 (defun gnus-summary-limit-children (thread)
6430   "Return 1 if this subthread is visible and 0 if it is not."
6431   ;; First we get the number of visible children to this thread.  This
6432   ;; is done by recursing down the thread using this function, so this
6433   ;; will really go down to a leaf article first, before slowly
6434   ;; working its way up towards the root.
6435   (when thread
6436     (let ((children
6437            (if (cdr thread)
6438                (apply '+ (mapcar 'gnus-summary-limit-children
6439                                  (cdr thread)))
6440              0))
6441           (number (mail-header-number (car thread)))
6442           score)
6443       (if (and
6444            (not (memq number gnus-newsgroup-marked))
6445            (or
6446             ;; If this article is dormant and has absolutely no visible
6447             ;; children, then this article isn't visible.
6448             (and (memq number gnus-newsgroup-dormant)
6449                  (zerop children))
6450             ;; If this is "fetch-old-headered" and there is no
6451             ;; visible children, then we don't want this article.
6452             (and (eq gnus-fetch-old-headers 'some)
6453                  (gnus-summary-article-ancient-p number)
6454                  (zerop children))
6455             ;; If this is "fetch-old-headered" and `invisible', then
6456             ;; we don't want this article.
6457             (and (eq gnus-fetch-old-headers 'invisible)
6458                  (gnus-summary-article-ancient-p number))
6459             ;; If this is a sparsely inserted article with no children,
6460             ;; we don't want it.
6461             (and (eq gnus-build-sparse-threads 'some)
6462                  (gnus-summary-article-sparse-p number)
6463                  (zerop children))
6464             ;; If we use expunging, and this article is really
6465             ;; low-scored, then we don't want this article.
6466             (when (and gnus-summary-expunge-below
6467                        (< (setq score
6468                                 (or (cdr (assq number gnus-newsgroup-scored))
6469                                     gnus-summary-default-score))
6470                           gnus-summary-expunge-below))
6471               ;; We increase the expunge-tally here, but that has
6472               ;; nothing to do with the limits, really.
6473               (incf gnus-newsgroup-expunged-tally)
6474               ;; We also mark as read here, if that's wanted.
6475               (when (and gnus-summary-mark-below
6476                          (< score gnus-summary-mark-below))
6477                 (setq gnus-newsgroup-unreads
6478                       (delq number gnus-newsgroup-unreads))
6479                 (if gnus-newsgroup-auto-expire
6480                     (push number gnus-newsgroup-expirable)
6481                   (push (cons number gnus-low-score-mark)
6482                         gnus-newsgroup-reads)))
6483               t)
6484             ;; Check NoCeM things.
6485             (if (and gnus-use-nocem
6486                      (gnus-nocem-unwanted-article-p
6487                       (mail-header-id (car thread))))
6488                 (progn
6489                   (setq gnus-newsgroup-unreads
6490                         (delq number gnus-newsgroup-unreads))
6491                   t))))
6492           ;; Nope, invisible article.
6493           0
6494         ;; Ok, this article is to be visible, so we add it to the limit
6495         ;; and return 1.
6496         (push number gnus-newsgroup-limit)
6497         1))))
6498
6499 (defun gnus-expunge-thread (thread)
6500   "Mark all articles in THREAD as read."
6501   (let* ((number (mail-header-number (car thread))))
6502     (incf gnus-newsgroup-expunged-tally)
6503     ;; We also mark as read here, if that's wanted.
6504     (setq gnus-newsgroup-unreads
6505           (delq number gnus-newsgroup-unreads))
6506     (if gnus-newsgroup-auto-expire
6507         (push number gnus-newsgroup-expirable)
6508       (push (cons number gnus-low-score-mark)
6509             gnus-newsgroup-reads)))
6510   ;; Go recursively through all subthreads.
6511   (mapcar 'gnus-expunge-thread (cdr thread)))
6512
6513 ;; Summary article oriented commands
6514
6515 (defun gnus-summary-refer-parent-article (n)
6516   "Refer parent article N times.
6517 If N is negative, go to ancestor -N instead.
6518 The difference between N and the number of articles fetched is returned."
6519   (interactive "p")
6520   (let ((skip 1)
6521         error header ref)
6522     (when (not (natnump n))
6523       (setq skip (abs n)
6524             n 1))
6525     (while (and (> n 0)
6526                 (not error))
6527       (setq header (gnus-summary-article-header))
6528       (if (and (eq (mail-header-number header)
6529                    (cdr gnus-article-current))
6530                (equal gnus-newsgroup-name
6531                       (car gnus-article-current)))
6532           ;; If we try to find the parent of the currently
6533           ;; displayed article, then we take a look at the actual
6534           ;; References header, since this is slightly more
6535           ;; reliable than the References field we got from the
6536           ;; server.
6537           (save-excursion
6538             (set-buffer gnus-original-article-buffer)
6539             (nnheader-narrow-to-headers)
6540             (unless (setq ref (message-fetch-field "references"))
6541               (setq ref (message-fetch-field "in-reply-to")))
6542             (widen))
6543         (setq ref
6544               ;; It's not the current article, so we take a bet on
6545               ;; the value we got from the server.
6546               (mail-header-references header)))
6547       (if (and ref
6548                (not (equal ref "")))
6549           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6550             (gnus-message 1 "Couldn't find parent"))
6551         (gnus-message 1 "No references in article %d"
6552                       (gnus-summary-article-number))
6553         (setq error t))
6554       (decf n))
6555     (gnus-summary-position-point)
6556     n))
6557
6558 (defun gnus-summary-refer-references ()
6559   "Fetch all articles mentioned in the References header.
6560 Return the number of articles fetched."
6561   (interactive)
6562   (let ((ref (mail-header-references (gnus-summary-article-header)))
6563         (current (gnus-summary-article-number))
6564         (n 0))
6565     (if (or (not ref)
6566             (equal ref ""))
6567         (error "No References in the current article")
6568       ;; For each Message-ID in the References header...
6569       (while (string-match "<[^>]*>" ref)
6570         (incf n)
6571         ;; ... fetch that article.
6572         (gnus-summary-refer-article
6573          (prog1 (match-string 0 ref)
6574            (setq ref (substring ref (match-end 0))))))
6575       (gnus-summary-goto-subject current)
6576       (gnus-summary-position-point)
6577       n)))
6578
6579 (defun gnus-summary-refer-thread (&optional limit)
6580   "Fetch all articles in the current thread.
6581 If LIMIT (the numerical prefix), fetch that many old headers instead
6582 of what's specified by the `gnus-refer-thread-limit' variable."
6583   (interactive "P")
6584   (let ((id (mail-header-id (gnus-summary-article-header)))
6585         (limit (if limit (prefix-numeric-value limit)
6586                  gnus-refer-thread-limit)))
6587     ;; We want to fetch LIMIT *old* headers, but we also have to
6588     ;; re-fetch all the headers in the current buffer, because many of
6589     ;; them may be undisplayed.  So we adjust LIMIT.
6590     (when (numberp limit)
6591       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6592     (unless (eq gnus-fetch-old-headers 'invisible)
6593       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6594       ;; Retrieve the headers and read them in.
6595       (if (eq (gnus-retrieve-headers
6596                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6597               'nov)
6598           (gnus-build-all-threads)
6599         (error "Can't fetch thread from backends that don't support NOV"))
6600       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6601     (gnus-summary-limit-include-thread id)))
6602
6603 (defun gnus-summary-refer-article (message-id &optional arg)
6604   "Fetch an article specified by MESSAGE-ID.
6605 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6606 or `gnus-select-method', no matter what backend the article comes from."
6607   (interactive "sMessage-ID: \nP")
6608   (when (and (stringp message-id)
6609              (not (zerop (length message-id))))
6610     ;; Construct the correct Message-ID if necessary.
6611     ;; Suggested by tale@pawl.rpi.edu.
6612     (unless (string-match "^<" message-id)
6613       (setq message-id (concat "<" message-id)))
6614     (unless (string-match ">$" message-id)
6615       (setq message-id (concat message-id ">")))
6616     (let* ((header (gnus-id-to-header message-id))
6617            (sparse (and header
6618                         (gnus-summary-article-sparse-p
6619                          (mail-header-number header))
6620                         (memq (mail-header-number header)
6621                               gnus-newsgroup-limit))))
6622       (cond
6623        ;; If the article is present in the buffer we just go to it.
6624        ((and header
6625              (or (not (gnus-summary-article-sparse-p
6626                        (mail-header-number header)))
6627                  sparse))
6628         (prog1
6629             (gnus-summary-goto-article
6630              (mail-header-number header) nil t)
6631           (when sparse
6632             (gnus-summary-update-article (mail-header-number header)))))
6633        (t
6634         ;; We fetch the article
6635         (let ((gnus-override-method
6636                (cond ((gnus-news-group-p gnus-newsgroup-name)
6637                       gnus-refer-article-method)
6638                      (arg
6639                       (or gnus-refer-article-method gnus-select-method))
6640                      (t nil)))
6641               number)
6642           ;; Start the special refer-article method, if necessary.
6643           (when (and gnus-refer-article-method
6644                      (gnus-news-group-p gnus-newsgroup-name))
6645             (gnus-check-server gnus-refer-article-method))
6646           ;; Fetch the header, and display the article.
6647           (if (setq number (gnus-summary-insert-subject message-id))
6648               (gnus-summary-select-article nil nil nil number)
6649             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6650
6651 (defun gnus-summary-edit-parameters ()
6652   "Edit the group parameters of the current group."
6653   (interactive)
6654   (gnus-group-edit-group gnus-newsgroup-name 'params))
6655
6656 (defun gnus-summary-customize-parameters ()
6657   "Customize the group parameters of the current group."
6658   (interactive)
6659   (gnus-group-customize gnus-newsgroup-name))
6660
6661 (defun gnus-summary-enter-digest-group (&optional force)
6662   "Enter an nndoc group based on the current article.
6663 If FORCE, force a digest interpretation.  If not, try
6664 to guess what the document format is."
6665   (interactive "P")
6666   (let ((conf gnus-current-window-configuration))
6667     (save-excursion
6668       (gnus-summary-select-article))
6669     (setq gnus-current-window-configuration conf)
6670     (let* ((name (format "%s-%d"
6671                          (gnus-group-prefixed-name
6672                           gnus-newsgroup-name (list 'nndoc ""))
6673                          (save-excursion
6674                            (set-buffer gnus-summary-buffer)
6675                            gnus-current-article)))
6676            (ogroup gnus-newsgroup-name)
6677            (params (append (gnus-info-params (gnus-get-info ogroup))
6678                            (list (cons 'to-group ogroup))
6679                            (list (cons 'save-article-group ogroup))))
6680            (case-fold-search t)
6681            (buf (current-buffer))
6682            dig)
6683       (save-excursion
6684         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6685         (insert-buffer-substring gnus-original-article-buffer)
6686         ;; Remove lines that may lead nndoc to misinterpret the
6687         ;; document type.
6688         (narrow-to-region
6689          (goto-char (point-min))
6690          (or (search-forward "\n\n" nil t) (point)))
6691         (goto-char (point-min))
6692         (delete-matching-lines "^\\(Path\\):\\|^From ")
6693         (widen))
6694       (unwind-protect
6695           (if (gnus-group-read-ephemeral-group
6696                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6697                             (nndoc-article-type
6698                              ,(if force 'digest 'guess))) t)
6699               ;; Make all postings to this group go to the parent group.
6700               (nconc (gnus-info-params (gnus-get-info name))
6701                      params)
6702             ;; Couldn't select this doc group.
6703             (switch-to-buffer buf)
6704             (gnus-set-global-variables)
6705             (gnus-configure-windows 'summary)
6706             (gnus-message 3 "Article couldn't be entered?"))
6707         (kill-buffer dig)))))
6708
6709 (defun gnus-summary-read-document (n)
6710   "Open a new group based on the current article(s).
6711 This will allow you to read digests and other similar
6712 documents as newsgroups.
6713 Obeys the standard process/prefix convention."
6714   (interactive "P")
6715   (let* ((articles (gnus-summary-work-articles n))
6716          (ogroup gnus-newsgroup-name)
6717          (params (append (gnus-info-params (gnus-get-info ogroup))
6718                          (list (cons 'to-group ogroup))))
6719          article group egroup groups vgroup)
6720     (while (setq article (pop articles))
6721       (setq group (format "%s-%d" gnus-newsgroup-name article))
6722       (gnus-summary-remove-process-mark article)
6723       (when (gnus-summary-display-article article)
6724         (save-excursion
6725           (with-temp-buffer
6726             (insert-buffer-substring gnus-original-article-buffer)
6727             ;; Remove some headers that may lead nndoc to make
6728             ;; the wrong guess.
6729             (message-narrow-to-head)
6730             (goto-char (point-min))
6731             (delete-matching-lines "^\\(Path\\):\\|^From ")
6732             (widen)
6733             (if (setq egroup
6734                       (gnus-group-read-ephemeral-group
6735                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6736                                      (nndoc-article-type guess))
6737                        t nil t))
6738                 (progn
6739                   ;; Make all postings to this group go to the parent group.
6740                   (nconc (gnus-info-params (gnus-get-info egroup))
6741                          params)
6742                   (push egroup groups))
6743               ;; Couldn't select this doc group.
6744               (gnus-error 3 "Article couldn't be entered"))))))
6745     ;; Now we have selected all the documents.
6746     (cond
6747      ((not groups)
6748       (error "None of the articles could be interpreted as documents"))
6749      ((gnus-group-read-ephemeral-group
6750        (setq vgroup (format
6751                      "nnvirtual:%s-%s" gnus-newsgroup-name
6752                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6753        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6754        t
6755        (cons (current-buffer) 'summary)))
6756      (t
6757       (error "Couldn't select virtual nndoc group")))))
6758
6759 (defun gnus-summary-isearch-article (&optional regexp-p)
6760   "Do incremental search forward on the current article.
6761 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6762   (interactive "P")
6763   (gnus-summary-select-article)
6764   (gnus-configure-windows 'article)
6765   (gnus-eval-in-buffer-window gnus-article-buffer
6766     (save-restriction
6767       (widen)
6768       (isearch-forward regexp-p))))
6769
6770 (defun gnus-summary-search-article-forward (regexp &optional backward)
6771   "Search for an article containing REGEXP forward.
6772 If BACKWARD, search backward instead."
6773   (interactive
6774    (list (read-string
6775           (format "Search article %s (regexp%s): "
6776                   (if current-prefix-arg "backward" "forward")
6777                   (if gnus-last-search-regexp
6778                       (concat ", default " gnus-last-search-regexp)
6779                     "")))
6780          current-prefix-arg))
6781   (if (string-equal regexp "")
6782       (setq regexp (or gnus-last-search-regexp ""))
6783     (setq gnus-last-search-regexp regexp))
6784   (if (gnus-summary-search-article regexp backward)
6785       (gnus-summary-show-thread)
6786     (error "Search failed: \"%s\"" regexp)))
6787
6788 (defun gnus-summary-search-article-backward (regexp)
6789   "Search for an article containing REGEXP backward."
6790   (interactive
6791    (list (read-string
6792           (format "Search article backward (regexp%s): "
6793                   (if gnus-last-search-regexp
6794                       (concat ", default " gnus-last-search-regexp)
6795                     "")))))
6796   (gnus-summary-search-article-forward regexp 'backward))
6797
6798 (defun gnus-summary-search-article (regexp &optional backward)
6799   "Search for an article containing REGEXP.
6800 Optional argument BACKWARD means do search for backward.
6801 `gnus-select-article-hook' is not called during the search."
6802   ;; We have to require this here to make sure that the following
6803   ;; dynamic binding isn't shadowed by autoloading.
6804   (require 'gnus-async)
6805   (require 'gnus-art)
6806   (let ((gnus-select-article-hook nil)  ;Disable hook.
6807         (gnus-article-display-hook nil)
6808         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6809         (gnus-use-article-prefetch nil)
6810         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6811         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6812         (sum (current-buffer))
6813         (gnus-display-mime-function nil)
6814         (found nil)
6815         point)
6816     (gnus-save-hidden-threads
6817       (gnus-summary-select-article)
6818       (set-buffer gnus-article-buffer)
6819       (when backward
6820         (forward-line -1))
6821       (while (not found)
6822         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6823         (if (if backward
6824                 (re-search-backward regexp nil t)
6825               (re-search-forward regexp nil t))
6826             ;; We found the regexp.
6827             (progn
6828               (setq found 'found)
6829               (beginning-of-line)
6830               (set-window-start
6831                (get-buffer-window (current-buffer))
6832                (point))
6833               (forward-line 1)
6834               (set-buffer sum)
6835               (setq point (point)))
6836           ;; We didn't find it, so we go to the next article.
6837           (set-buffer sum)
6838           (setq found 'not)
6839           (while (eq found 'not)
6840             (if (not (if backward (gnus-summary-find-prev)
6841                        (gnus-summary-find-next)))
6842                 ;; No more articles.
6843                 (setq found t)
6844               ;; Select the next article and adjust point.
6845               (unless (gnus-summary-article-sparse-p
6846                        (gnus-summary-article-number))
6847                 (setq found nil)
6848                 (gnus-summary-select-article)
6849                 (set-buffer gnus-article-buffer)
6850                 (widen)
6851                 (goto-char (if backward (point-max) (point-min))))))))
6852       (gnus-message 7 ""))
6853     ;; Return whether we found the regexp.
6854     (when (eq found 'found)
6855       (goto-char point)
6856       (gnus-summary-show-thread)
6857       (gnus-summary-goto-subject gnus-current-article)
6858       (gnus-summary-position-point)
6859       t)))
6860
6861 (defun gnus-summary-find-matching (header regexp &optional backward unread
6862                                           not-case-fold)
6863   "Return a list of all articles that match REGEXP on HEADER.
6864 The search stars on the current article and goes forwards unless
6865 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6866 If UNREAD is non-nil, only unread articles will
6867 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6868 in the comparisons."
6869   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6870                 (gnus-data-find-list
6871                  (gnus-summary-article-number) (gnus-data-list backward))))
6872         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6873         (case-fold-search (not not-case-fold))
6874         articles d)
6875     (unless (fboundp (intern (concat "mail-header-" header)))
6876       (error "%s is not a valid header" header))
6877     (while data
6878       (setq d (car data))
6879       (and (or (not unread)             ; We want all articles...
6880                (gnus-data-unread-p d))  ; Or just unreads.
6881            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6882            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6883            (push (gnus-data-number d) articles)) ; Success!
6884       (setq data (cdr data)))
6885     (nreverse articles)))
6886
6887 (defun gnus-summary-execute-command (header regexp command &optional backward)
6888   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6889 If HEADER is an empty string (or nil), the match is done on the entire
6890 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6891   (interactive
6892    (list (let ((completion-ignore-case t))
6893            (completing-read
6894             "Header name: "
6895             (mapcar (lambda (string) (list string))
6896                     '("Number" "Subject" "From" "Lines" "Date"
6897                       "Message-ID" "Xref" "References" "Body"))
6898             nil 'require-match))
6899          (read-string "Regexp: ")
6900          (read-key-sequence "Command: ")
6901          current-prefix-arg))
6902   (when (equal header "Body")
6903     (setq header ""))
6904   ;; Hidden thread subtrees must be searched as well.
6905   (gnus-summary-show-all-threads)
6906   ;; We don't want to change current point nor window configuration.
6907   (save-excursion
6908     (save-window-excursion
6909       (gnus-message 6 "Executing %s..." (key-description command))
6910       ;; We'd like to execute COMMAND interactively so as to give arguments.
6911       (gnus-execute header regexp
6912                     `(call-interactively ',(key-binding command))
6913                     backward)
6914       (gnus-message 6 "Executing %s...done" (key-description command)))))
6915
6916 (defun gnus-summary-beginning-of-article ()
6917   "Scroll the article back to the beginning."
6918   (interactive)
6919   (gnus-summary-select-article)
6920   (gnus-configure-windows 'article)
6921   (gnus-eval-in-buffer-window gnus-article-buffer
6922     (widen)
6923     (goto-char (point-min))
6924     (when gnus-page-broken
6925       (gnus-narrow-to-page))))
6926
6927 (defun gnus-summary-end-of-article ()
6928   "Scroll to the end of the article."
6929   (interactive)
6930   (gnus-summary-select-article)
6931   (gnus-configure-windows 'article)
6932   (gnus-eval-in-buffer-window gnus-article-buffer
6933     (widen)
6934     (goto-char (point-max))
6935     (recenter -3)
6936     (when gnus-page-broken
6937       (gnus-narrow-to-page))))
6938
6939 (defun gnus-summary-print-article (&optional filename n)
6940   "Generate and print a PostScript image of the N next (mail) articles.
6941
6942 If N is negative, print the N previous articles.  If N is nil and articles
6943 have been marked with the process mark, print these instead.
6944
6945 If the optional second argument FILENAME is nil, send the image to the
6946 printer.  If FILENAME is a string, save the PostScript image in a file with
6947 that name.  If FILENAME is a number, prompt the user for the name of the file
6948 to save in."
6949   (interactive (list (ps-print-preprint current-prefix-arg)
6950                      current-prefix-arg))
6951   (dolist (article (gnus-summary-work-articles n))
6952     (gnus-summary-select-article nil nil 'pseudo article)
6953     (gnus-eval-in-buffer-window gnus-article-buffer
6954       (let ((buffer (generate-new-buffer " *print*")))
6955         (unwind-protect
6956             (progn
6957               (copy-to-buffer buffer (point-min) (point-max))
6958               (set-buffer buffer)
6959               (gnus-article-delete-invisible-text)
6960               (let ((ps-left-header
6961                      (list
6962                       (concat "("
6963                               (mail-header-subject gnus-current-headers) ")")
6964                       (concat "("
6965                               (mail-header-from gnus-current-headers) ")")))
6966                     (ps-right-header
6967                      (list
6968                       "/pagenumberstring load"
6969                       (concat "("
6970                               (mail-header-date gnus-current-headers) ")"))))
6971                 (gnus-run-hooks 'gnus-ps-print-hook)
6972                 (save-excursion
6973                   (ps-print-buffer-with-faces filename))))
6974           (kill-buffer buffer))))))
6975
6976 (defun gnus-summary-show-article (&optional arg)
6977   "Force re-fetching of the current article.
6978 If ARG (the prefix) is non-nil, show the raw article without any
6979 article massaging functions being run."
6980   (interactive "P")
6981   (if (not arg)
6982       ;; Select the article the normal way.
6983       (gnus-summary-select-article nil 'force)
6984     ;; We have to require this here to make sure that the following
6985     ;; dynamic binding isn't shadowed by autoloading.
6986     (require 'gnus-async)
6987     (require 'gnus-art)
6988     ;; Bind the article treatment functions to nil.
6989     (let ((gnus-have-all-headers t)
6990           gnus-article-display-hook
6991           gnus-article-prepare-hook
6992           gnus-article-decode-hook
6993           gnus-display-mime-function
6994           gnus-break-pages
6995           gnus-visual)
6996       (gnus-summary-select-article nil 'force)))
6997   (gnus-summary-goto-subject gnus-current-article)
6998   (gnus-summary-position-point))
6999
7000 (defun gnus-summary-verbose-headers (&optional arg)
7001   "Toggle permanent full header display.
7002 If ARG is a positive number, turn header display on.
7003 If ARG is a negative number, turn header display off."
7004   (interactive "P")
7005   (setq gnus-show-all-headers
7006         (cond ((or (not (numberp arg))
7007                    (zerop arg))
7008                (not gnus-show-all-headers))
7009               ((natnump arg)
7010                t)))
7011   (gnus-summary-show-article))
7012
7013 (defun gnus-summary-toggle-header (&optional arg)
7014   "Show the headers if they are hidden, or hide them if they are shown.
7015 If ARG is a positive number, show the entire header.
7016 If ARG is a negative number, hide the unwanted header lines."
7017   (interactive "P")
7018   (save-excursion
7019     (set-buffer gnus-article-buffer)
7020     (let* ((buffer-read-only nil)
7021            (inhibit-point-motion-hooks t)
7022            (hidden (text-property-any
7023                     (goto-char (point-min)) (search-forward "\n\n")
7024                     'invisible t))
7025            e)
7026       (goto-char (point-min))
7027       (when (search-forward "\n\n" nil t)
7028         (delete-region (point-min) (1- (point))))
7029       (goto-char (point-min))
7030       (save-excursion
7031         (set-buffer gnus-original-article-buffer)
7032         (goto-char (point-min))
7033         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7034       (insert-buffer-substring gnus-original-article-buffer 1 e)
7035       (let ((article-inhibit-hiding t))
7036         (gnus-run-hooks 'gnus-article-display-hook))
7037       (when (or (not hidden) (and (numberp arg) (< arg 0)))
7038         (gnus-article-hide-headers)))))
7039
7040 (defun gnus-summary-show-all-headers ()
7041   "Make all header lines visible."
7042   (interactive)
7043   (gnus-article-show-all-headers))
7044
7045 (defun gnus-summary-caesar-message (&optional arg)
7046   "Caesar rotate the current article by 13.
7047 The numerical prefix specifies how many places to rotate each letter
7048 forward."
7049   (interactive "P")
7050   (gnus-summary-select-article)
7051   (let ((mail-header-separator ""))
7052     (gnus-eval-in-buffer-window gnus-article-buffer
7053       (save-restriction
7054         (widen)
7055         (let ((start (window-start))
7056               buffer-read-only)
7057           (message-caesar-buffer-body arg)
7058           (set-window-start (get-buffer-window (current-buffer)) start))))))
7059
7060 (defun gnus-summary-stop-page-breaking ()
7061   "Stop page breaking in the current article."
7062   (interactive)
7063   (gnus-summary-select-article)
7064   (gnus-eval-in-buffer-window gnus-article-buffer
7065     (widen)
7066     (when (gnus-visual-p 'page-marker)
7067       (let ((buffer-read-only nil))
7068         (gnus-remove-text-with-property 'gnus-prev)
7069         (gnus-remove-text-with-property 'gnus-next))
7070       (setq gnus-page-broken nil))))
7071
7072 (defun gnus-summary-move-article (&optional n to-newsgroup
7073                                             select-method action)
7074   "Move the current article to a different newsgroup.
7075 If N is a positive number, move the N next articles.
7076 If N is a negative number, move the N previous articles.
7077 If N is nil and any articles have been marked with the process mark,
7078 move those articles instead.
7079 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7080 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7081 re-spool using this method.
7082
7083 For this function to work, both the current newsgroup and the
7084 newsgroup that you want to move to have to support the `request-move'
7085 and `request-accept' functions."
7086   (interactive "P")
7087   (unless action
7088     (setq action 'move))
7089   ;; Disable marking as read.
7090   (let (gnus-mark-article-hook)
7091     (save-window-excursion
7092       (gnus-summary-select-article)))
7093   ;; Check whether the source group supports the required functions.
7094   (cond ((and (eq action 'move)
7095               (not (gnus-check-backend-function
7096                     'request-move-article gnus-newsgroup-name)))
7097          (error "The current group does not support article moving"))
7098         ((and (eq action 'crosspost)
7099               (not (gnus-check-backend-function
7100                     'request-replace-article gnus-newsgroup-name)))
7101          (error "The current group does not support article editing")))
7102   (let ((articles (gnus-summary-work-articles n))
7103         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7104         (names '((move "Move" "Moving")
7105                  (copy "Copy" "Copying")
7106                  (crosspost "Crosspost" "Crossposting")))
7107         (copy-buf (save-excursion
7108                     (nnheader-set-temp-buffer " *copy article*")))
7109         art-group to-method new-xref article to-groups)
7110     (unless (assq action names)
7111       (error "Unknown action %s" action))
7112     ;; Read the newsgroup name.
7113     (when (and (not to-newsgroup)
7114                (not select-method))
7115       (setq to-newsgroup
7116             (gnus-read-move-group-name
7117              (cadr (assq action names))
7118              (symbol-value (intern (format "gnus-current-%s-group" action)))
7119              articles prefix))
7120       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7121     (setq to-method (or select-method
7122                         (gnus-group-name-to-method to-newsgroup)))
7123     ;; Check the method we are to move this article to...
7124     (unless (gnus-check-backend-function
7125              'request-accept-article (car to-method))
7126       (error "%s does not support article copying" (car to-method)))
7127     (unless (gnus-check-server to-method)
7128       (error "Can't open server %s" (car to-method)))
7129     (gnus-message 6 "%s to %s: %s..."
7130                   (caddr (assq action names))
7131                   (or (car select-method) to-newsgroup) articles)
7132     (while articles
7133       (setq article (pop articles))
7134       (setq
7135        art-group
7136        (cond
7137         ;; Move the article.
7138         ((eq action 'move)
7139          ;; Remove this article from future suppression.
7140          (gnus-dup-unsuppress-article article)
7141          (gnus-request-move-article
7142           article                       ; Article to move
7143           gnus-newsgroup-name           ; From newsgroup
7144           (nth 1 (gnus-find-method-for-group
7145                   gnus-newsgroup-name)) ; Server
7146           (list 'gnus-request-accept-article
7147                 to-newsgroup (list 'quote select-method)
7148                 (not articles) t)               ; Accept form
7149           (not articles)))              ; Only save nov last time
7150         ;; Copy the article.
7151         ((eq action 'copy)
7152          (save-excursion
7153            (set-buffer copy-buf)
7154            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7155              (gnus-request-accept-article
7156               to-newsgroup select-method (not articles) t))))
7157         ;; Crosspost the article.
7158         ((eq action 'crosspost)
7159          (let ((xref (message-tokenize-header
7160                       (mail-header-xref (gnus-summary-article-header article))
7161                       " ")))
7162            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7163                                   ":" article))
7164            (unless xref
7165              (setq xref (list (system-name))))
7166            (setq new-xref
7167                  (concat
7168                   (mapconcat 'identity
7169                              (delete "Xref:" (delete new-xref xref))
7170                              " ")
7171                   " " new-xref))
7172            (save-excursion
7173              (set-buffer copy-buf)
7174              ;; First put the article in the destination group.
7175              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7176              (when (consp (setq art-group
7177                                 (gnus-request-accept-article
7178                                  to-newsgroup select-method (not articles))))
7179                (setq new-xref (concat new-xref " " (car art-group)
7180                                       ":" (cdr art-group)))
7181                ;; Now we have the new Xrefs header, so we insert
7182                ;; it and replace the new article.
7183                (nnheader-replace-header "Xref" new-xref)
7184                (gnus-request-replace-article
7185                 (cdr art-group) to-newsgroup (current-buffer))
7186                art-group))))))
7187       (cond
7188        ((not art-group)
7189         (gnus-message 1 "Couldn't %s article %s"
7190                       (cadr (assq action names)) article))
7191        ((and (eq art-group 'junk)
7192              (eq action 'move))
7193         (gnus-summary-mark-article article gnus-canceled-mark)
7194         (gnus-message 4 "Deleted article %s" article))
7195        (t
7196         (let* ((pto-group (gnus-group-prefixed-name
7197                            (car art-group) to-method))
7198                (entry
7199                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7200                (info (nth 2 entry))
7201                (to-group (gnus-info-group info)))
7202           ;; Update the group that has been moved to.
7203           (when (and info
7204                      (memq action '(move copy)))
7205             (unless (member to-group to-groups)
7206               (push to-group to-groups))
7207
7208             (unless (memq article gnus-newsgroup-unreads)
7209               (gnus-info-set-read
7210                info (gnus-add-to-range (gnus-info-read info)
7211                                        (list (cdr art-group)))))
7212
7213             ;; Copy any marks over to the new group.
7214             (let ((marks gnus-article-mark-lists)
7215                   (to-article (cdr art-group)))
7216
7217               ;; See whether the article is to be put in the cache.
7218               (when gnus-use-cache
7219                 (gnus-cache-possibly-enter-article
7220                  to-group to-article
7221                  (let ((header (copy-sequence
7222                                 (gnus-summary-article-header article))))
7223                    (mail-header-set-number header to-article)
7224                    header)
7225                  (memq article gnus-newsgroup-marked)
7226                  (memq article gnus-newsgroup-dormant)
7227                  (memq article gnus-newsgroup-unreads)))
7228
7229               (when (and (equal to-group gnus-newsgroup-name)
7230                          (not (memq article gnus-newsgroup-unreads)))
7231                 ;; Mark this article as read in this group.
7232                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7233                 (setcdr (gnus-active to-group) to-article)
7234                 (setcdr gnus-newsgroup-active to-article))
7235
7236               (while marks
7237                 (when (memq article (symbol-value
7238                                      (intern (format "gnus-newsgroup-%s"
7239                                                      (caar marks)))))
7240                   ;; If the other group is the same as this group,
7241                   ;; then we have to add the mark to the list.
7242                   (when (equal to-group gnus-newsgroup-name)
7243                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7244                          (cons to-article
7245                                (symbol-value
7246                                 (intern (format "gnus-newsgroup-%s"
7247                                                 (caar marks)))))))
7248                   ;; Copy the marks to other group.
7249                   (gnus-add-marked-articles
7250                    to-group (cdar marks) (list to-article) info))
7251                 (setq marks (cdr marks)))
7252
7253               (gnus-dribble-enter
7254                (concat "(gnus-group-set-info '"
7255                        (gnus-prin1-to-string (gnus-get-info to-group))
7256                        ")"))))
7257
7258           ;; Update the Xref header in this article to point to
7259           ;; the new crossposted article we have just created.
7260           (when (eq action 'crosspost)
7261             (save-excursion
7262               (set-buffer copy-buf)
7263               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7264               (nnheader-replace-header "Xref" new-xref)
7265               (gnus-request-replace-article
7266                article gnus-newsgroup-name (current-buffer)))))
7267
7268         ;;;!!!Why is this necessary?
7269         (set-buffer gnus-summary-buffer)
7270
7271         (gnus-summary-goto-subject article)
7272         (when (eq action 'move)
7273           (gnus-summary-mark-article article gnus-canceled-mark))))
7274       (gnus-summary-remove-process-mark article))
7275     ;; Re-activate all groups that have been moved to.
7276     (while to-groups
7277       (save-excursion
7278         (set-buffer gnus-group-buffer)
7279         (when (gnus-group-goto-group (car to-groups) t)
7280           (gnus-group-get-new-news-this-group 1 t))
7281         (pop to-groups)))
7282
7283     (gnus-kill-buffer copy-buf)
7284     (gnus-summary-position-point)
7285     (gnus-set-mode-line 'summary)))
7286
7287 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7288   "Move the current article to a different newsgroup.
7289 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7290 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7291 re-spool using this method."
7292   (interactive "P")
7293   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7294
7295 (defun gnus-summary-crosspost-article (&optional n)
7296   "Crosspost the current article to some other group."
7297   (interactive "P")
7298   (gnus-summary-move-article n nil nil 'crosspost))
7299
7300 (defcustom gnus-summary-respool-default-method nil
7301   "Default method for respooling an article.
7302 If nil, use to the current newsgroup method."
7303   :type '(choice (gnus-select-method :value (nnml ""))
7304                  (const nil))
7305   :group 'gnus-summary-mail)
7306
7307 (defun gnus-summary-respool-article (&optional n method)
7308   "Respool the current article.
7309 The article will be squeezed through the mail spooling process again,
7310 which means that it will be put in some mail newsgroup or other
7311 depending on `nnmail-split-methods'.
7312 If N is a positive number, respool the N next articles.
7313 If N is a negative number, respool the N previous articles.
7314 If N is nil and any articles have been marked with the process mark,
7315 respool those articles instead.
7316
7317 Respooling can be done both from mail groups and \"real\" newsgroups.
7318 In the former case, the articles in question will be moved from the
7319 current group into whatever groups they are destined to.  In the
7320 latter case, they will be copied into the relevant groups."
7321   (interactive
7322    (list current-prefix-arg
7323          (let* ((methods (gnus-methods-using 'respool))
7324                 (methname
7325                  (symbol-name (or gnus-summary-respool-default-method
7326                                   (car (gnus-find-method-for-group
7327                                         gnus-newsgroup-name)))))
7328                 (method
7329                  (gnus-completing-read
7330                   methname "What backend do you want to use when respooling?"
7331                   methods nil t nil 'gnus-mail-method-history))
7332                 ms)
7333            (cond
7334             ((zerop (length (setq ms (gnus-servers-using-backend
7335                                       (intern method)))))
7336              (list (intern method) ""))
7337             ((= 1 (length ms))
7338              (car ms))
7339             (t
7340              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7341                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7342                            ms-alist))))))))
7343   (unless method
7344     (error "No method given for respooling"))
7345   (if (assoc (symbol-name
7346               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7347              (gnus-methods-using 'respool))
7348       (gnus-summary-move-article n nil method)
7349     (gnus-summary-copy-article n nil method)))
7350
7351 (defun gnus-summary-import-article (file)
7352   "Import an arbitrary file into a mail newsgroup."
7353   (interactive "fImport file: ")
7354   (let ((group gnus-newsgroup-name)
7355         (now (current-time))
7356         atts lines)
7357     (unless (gnus-check-backend-function 'request-accept-article group)
7358       (error "%s does not support article importing" group))
7359     (or (file-readable-p file)
7360         (not (file-regular-p file))
7361         (error "Can't read %s" file))
7362     (save-excursion
7363       (set-buffer (gnus-get-buffer-create " *import file*"))
7364       (erase-buffer)
7365       (insert-file-contents file)
7366       (goto-char (point-min))
7367       (unless (nnheader-article-p)
7368         ;; This doesn't look like an article, so we fudge some headers.
7369         (setq atts (file-attributes file)
7370               lines (count-lines (point-min) (point-max)))
7371         (insert "From: " (read-string "From: ") "\n"
7372                 "Subject: " (read-string "Subject: ") "\n"
7373                 "Date: " (message-make-date (nth 5 atts))
7374                 "\n"
7375                 "Message-ID: " (message-make-message-id) "\n"
7376                 "Lines: " (int-to-string lines) "\n"
7377                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7378       (gnus-request-accept-article group nil t)
7379       (kill-buffer (current-buffer)))))
7380
7381 (defun gnus-summary-article-posted-p ()
7382   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7383 This will be the case if the article has both been mailed and posted."
7384   (interactive)
7385   (let ((id (mail-header-references (gnus-summary-article-header)))
7386         (gnus-override-method
7387          (or gnus-refer-article-method gnus-select-method)))
7388     (if (gnus-request-head id "")
7389         (gnus-message 2 "The current message was found on %s"
7390                       gnus-override-method)
7391       (gnus-message 2 "The current message couldn't be found on %s"
7392                     gnus-override-method)
7393       nil)))
7394
7395 (defun gnus-summary-expire-articles (&optional now)
7396   "Expire all articles that are marked as expirable in the current group."
7397   (interactive)
7398   (when (gnus-check-backend-function
7399          'request-expire-articles gnus-newsgroup-name)
7400     ;; This backend supports expiry.
7401     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7402            (expirable (if total
7403                           (progn
7404                             ;; We need to update the info for
7405                             ;; this group for `gnus-list-of-read-articles'
7406                             ;; to give us the right answer.
7407                             (gnus-run-hooks 'gnus-exit-group-hook)
7408                             (gnus-summary-update-info)
7409                             (gnus-list-of-read-articles gnus-newsgroup-name))
7410                         (setq gnus-newsgroup-expirable
7411                               (sort gnus-newsgroup-expirable '<))))
7412            (expiry-wait (if now 'immediate
7413                           (gnus-group-find-parameter
7414                            gnus-newsgroup-name 'expiry-wait)))
7415            es)
7416       (when expirable
7417         ;; There are expirable articles in this group, so we run them
7418         ;; through the expiry process.
7419         (gnus-message 6 "Expiring articles...")
7420         ;; The list of articles that weren't expired is returned.
7421         (save-excursion
7422           (if expiry-wait
7423               (let ((nnmail-expiry-wait-function nil)
7424                     (nnmail-expiry-wait expiry-wait))
7425                 (setq es (gnus-request-expire-articles
7426                           expirable gnus-newsgroup-name)))
7427             (setq es (gnus-request-expire-articles
7428                       expirable gnus-newsgroup-name))))
7429         (unless total
7430           (setq gnus-newsgroup-expirable es))
7431         ;; We go through the old list of expirable, and mark all
7432         ;; really expired articles as nonexistent.
7433         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7434           (let ((gnus-use-cache nil))
7435             (while expirable
7436               (unless (memq (car expirable) es)
7437                 (when (gnus-data-find (car expirable))
7438                   (gnus-summary-mark-article
7439                    (car expirable) gnus-canceled-mark)))
7440               (setq expirable (cdr expirable)))))
7441         (gnus-message 6 "Expiring articles...done")))))
7442
7443 (defun gnus-summary-expire-articles-now ()
7444   "Expunge all expirable articles in the current group.
7445 This means that *all* articles that are marked as expirable will be
7446 deleted forever, right now."
7447   (interactive)
7448   (or gnus-expert-user
7449       (gnus-yes-or-no-p
7450        "Are you really, really, really sure you want to delete all these messages? ")
7451       (error "Phew!"))
7452   (gnus-summary-expire-articles t))
7453
7454 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7455 (defun gnus-summary-delete-article (&optional n)
7456   "Delete the N next (mail) articles.
7457 This command actually deletes articles.  This is not a marking
7458 command.  The article will disappear forever from your life, never to
7459 return.
7460 If N is negative, delete backwards.
7461 If N is nil and articles have been marked with the process mark,
7462 delete these instead."
7463   (interactive "P")
7464   (unless (gnus-check-backend-function 'request-expire-articles
7465                                        gnus-newsgroup-name)
7466     (error "The current newsgroup does not support article deletion"))
7467   ;; Compute the list of articles to delete.
7468   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7469         not-deleted)
7470     (if (and gnus-novice-user
7471              (not (gnus-yes-or-no-p
7472                    (format "Do you really want to delete %s forever? "
7473                            (if (> (length articles) 1)
7474                                (format "these %s articles" (length articles))
7475                              "this article")))))
7476         ()
7477       ;; Delete the articles.
7478       (setq not-deleted (gnus-request-expire-articles
7479                          articles gnus-newsgroup-name 'force))
7480       (while articles
7481         (gnus-summary-remove-process-mark (car articles))
7482         ;; The backend might not have been able to delete the article
7483         ;; after all.
7484         (unless (memq (car articles) not-deleted)
7485           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7486         (setq articles (cdr articles)))
7487       (when not-deleted
7488         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7489     (gnus-summary-position-point)
7490     (gnus-set-mode-line 'summary)
7491     not-deleted))
7492
7493 (defun gnus-summary-edit-article (&optional force)
7494   "Edit the current article.
7495 This will have permanent effect only in mail groups.
7496 If FORCE is non-nil, allow editing of articles even in read-only
7497 groups."
7498   (interactive "P")
7499   (save-excursion
7500     (set-buffer gnus-summary-buffer)
7501     (gnus-set-global-variables)
7502     (when (and (not force)
7503                (gnus-group-read-only-p))
7504       (error "The current newsgroup does not support article editing"))
7505     ;; Select article if needed.
7506     (unless (eq (gnus-summary-article-number)
7507                 gnus-current-article)
7508       (gnus-summary-select-article t))
7509     (gnus-article-date-original)
7510     (gnus-article-edit-article
7511      `(lambda (no-highlight)
7512         (gnus-summary-edit-article-done
7513          ,(or (mail-header-references gnus-current-headers) "")
7514          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7515
7516 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7517
7518 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7519                                                  no-highlight)
7520   "Make edits to the current article permanent."
7521   (interactive)
7522   ;; Replace the article.
7523   (let ((buf (current-buffer)))
7524     (with-temp-buffer
7525       (insert-buffer buf)
7526       (if (and (not read-only)
7527                (not (gnus-request-replace-article
7528                      (cdr gnus-article-current) (car gnus-article-current)
7529                      (current-buffer)
7530                      (not gnus-article-decoded-p))))
7531           (error "Couldn't replace article")
7532         ;; Update the summary buffer.
7533         (if (and references
7534                  (equal (message-tokenize-header references " ")
7535                         (message-tokenize-header
7536                          (or (message-fetch-field "references") "") " ")))
7537             ;; We only have to update this line.
7538             (save-excursion
7539               (save-restriction
7540                 (message-narrow-to-head)
7541                 (let ((head (buffer-string))
7542                       header)
7543                   (with-temp-buffer
7544                     (insert (format "211 %d Article retrieved.\n"
7545                                     (cdr gnus-article-current)))
7546                     (insert head)
7547                     (insert ".\n")
7548                     (let ((nntp-server-buffer (current-buffer)))
7549                       (setq header (car (gnus-get-newsgroup-headers
7550                                          (save-excursion
7551                                            (set-buffer gnus-summary-buffer)
7552                                            gnus-newsgroup-dependencies)
7553                                          t))))
7554                     (save-excursion
7555                       (set-buffer gnus-summary-buffer)
7556                       (gnus-data-set-header
7557                        (gnus-data-find (cdr gnus-article-current))
7558                        header)
7559                       (gnus-summary-update-article-line
7560                        (cdr gnus-article-current) header))))))
7561           ;; Update threads.
7562           (set-buffer (or buffer gnus-summary-buffer))
7563           (gnus-summary-update-article (cdr gnus-article-current)))
7564         ;; Prettify the article buffer again.
7565         (unless no-highlight
7566           (save-excursion
7567             (set-buffer gnus-article-buffer)
7568             (gnus-run-hooks 'gnus-article-display-hook)
7569             (set-buffer gnus-original-article-buffer)
7570             (gnus-request-article
7571              (cdr gnus-article-current)
7572              (car gnus-article-current) (current-buffer))))
7573         ;; Prettify the summary buffer line.
7574         (when (gnus-visual-p 'summary-highlight 'highlight)
7575           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7576
7577 (defun gnus-summary-edit-wash (key)
7578   "Perform editing command KEY in the article buffer."
7579   (interactive
7580    (list
7581     (progn
7582       (message "%s" (concat (this-command-keys) "- "))
7583       (read-char))))
7584   (message "")
7585   (gnus-summary-edit-article)
7586   (execute-kbd-macro (concat (this-command-keys) key))
7587   (gnus-article-edit-done))
7588
7589 ;;; Respooling
7590
7591 (defun gnus-summary-respool-query (&optional silent trace)
7592   "Query where the respool algorithm would put this article."
7593   (interactive)
7594   (let (gnus-mark-article-hook)
7595     (gnus-summary-select-article)
7596     (save-excursion
7597       (set-buffer gnus-original-article-buffer)
7598       (save-restriction
7599         (message-narrow-to-head)
7600         (let ((groups (nnmail-article-group 'identity trace)))
7601           (unless silent
7602             (if groups
7603                 (message "This message would go to %s"
7604                          (mapconcat 'car groups ", "))
7605               (message "This message would go to no groups"))
7606             groups))))))
7607
7608 (defun gnus-summary-respool-trace ()
7609   "Trace where the respool algorithm would put this article.
7610 Display a buffer showing all fancy splitting patterns which matched."
7611   (interactive)
7612   (gnus-summary-respool-query nil t))
7613
7614 ;; Summary marking commands.
7615
7616 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7617   "Mark articles which has the same subject as read, and then select the next.
7618 If UNMARK is positive, remove any kind of mark.
7619 If UNMARK is negative, tick articles."
7620   (interactive "P")
7621   (when unmark
7622     (setq unmark (prefix-numeric-value unmark)))
7623   (let ((count
7624          (gnus-summary-mark-same-subject
7625           (gnus-summary-article-subject) unmark)))
7626     ;; Select next unread article.  If auto-select-same mode, should
7627     ;; select the first unread article.
7628     (gnus-summary-next-article t (and gnus-auto-select-same
7629                                       (gnus-summary-article-subject)))
7630     (gnus-message 7 "%d article%s marked as %s"
7631                   count (if (= count 1) " is" "s are")
7632                   (if unmark "unread" "read"))))
7633
7634 (defun gnus-summary-kill-same-subject (&optional unmark)
7635   "Mark articles which has the same subject as read.
7636 If UNMARK is positive, remove any kind of mark.
7637 If UNMARK is negative, tick articles."
7638   (interactive "P")
7639   (when unmark
7640     (setq unmark (prefix-numeric-value unmark)))
7641   (let ((count
7642          (gnus-summary-mark-same-subject
7643           (gnus-summary-article-subject) unmark)))
7644     ;; If marked as read, go to next unread subject.
7645     (when (null unmark)
7646       ;; Go to next unread subject.
7647       (gnus-summary-next-subject 1 t))
7648     (gnus-message 7 "%d articles are marked as %s"
7649                   count (if unmark "unread" "read"))))
7650
7651 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7652   "Mark articles with same SUBJECT as read, and return marked number.
7653 If optional argument UNMARK is positive, remove any kinds of marks.
7654 If optional argument UNMARK is negative, mark articles as unread instead."
7655   (let ((count 1))
7656     (save-excursion
7657       (cond
7658        ((null unmark)                   ; Mark as read.
7659         (while (and
7660                 (progn
7661                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7662                   (gnus-summary-show-thread) t)
7663                 (gnus-summary-find-subject subject))
7664           (setq count (1+ count))))
7665        ((> unmark 0)                    ; Tick.
7666         (while (and
7667                 (progn
7668                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7669                   (gnus-summary-show-thread) t)
7670                 (gnus-summary-find-subject subject))
7671           (setq count (1+ count))))
7672        (t                               ; Mark as unread.
7673         (while (and
7674                 (progn
7675                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7676                   (gnus-summary-show-thread) t)
7677                 (gnus-summary-find-subject subject))
7678           (setq count (1+ count)))))
7679       (gnus-set-mode-line 'summary)
7680       ;; Return the number of marked articles.
7681       count)))
7682
7683 (defun gnus-summary-mark-as-processable (n &optional unmark)
7684   "Set the process mark on the next N articles.
7685 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7686 the process mark instead.  The difference between N and the actual
7687 number of articles marked is returned."
7688   (interactive "p")
7689   (let ((backward (< n 0))
7690         (n (abs n)))
7691     (while (and
7692             (> n 0)
7693             (if unmark
7694                 (gnus-summary-remove-process-mark
7695                  (gnus-summary-article-number))
7696               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7697             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7698       (setq n (1- n)))
7699     (when (/= 0 n)
7700       (gnus-message 7 "No more articles"))
7701     (gnus-summary-recenter)
7702     (gnus-summary-position-point)
7703     n))
7704
7705 (defun gnus-summary-unmark-as-processable (n)
7706   "Remove the process mark from the next N articles.
7707 If N is negative, unmark backward instead.  The difference between N and
7708 the actual number of articles unmarked is returned."
7709   (interactive "p")
7710   (gnus-summary-mark-as-processable n t))
7711
7712 (defun gnus-summary-unmark-all-processable ()
7713   "Remove the process mark from all articles."
7714   (interactive)
7715   (save-excursion
7716     (while gnus-newsgroup-processable
7717       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7718   (gnus-summary-position-point))
7719
7720 (defun gnus-summary-mark-as-expirable (n)
7721   "Mark N articles forward as expirable.
7722 If N is negative, mark backward instead.  The difference between N and
7723 the actual number of articles marked is returned."
7724   (interactive "p")
7725   (gnus-summary-mark-forward n gnus-expirable-mark))
7726
7727 (defun gnus-summary-mark-article-as-replied (article)
7728   "Mark ARTICLE replied and update the summary line."
7729   (push article gnus-newsgroup-replied)
7730   (let ((buffer-read-only nil))
7731     (when (gnus-summary-goto-subject article nil t)
7732       (gnus-summary-update-secondary-mark article))))
7733
7734 (defun gnus-summary-set-bookmark (article)
7735   "Set a bookmark in current article."
7736   (interactive (list (gnus-summary-article-number)))
7737   (when (or (not (get-buffer gnus-article-buffer))
7738             (not gnus-current-article)
7739             (not gnus-article-current)
7740             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7741     (error "No current article selected"))
7742   ;; Remove old bookmark, if one exists.
7743   (let ((old (assq article gnus-newsgroup-bookmarks)))
7744     (when old
7745       (setq gnus-newsgroup-bookmarks
7746             (delq old gnus-newsgroup-bookmarks))))
7747   ;; Set the new bookmark, which is on the form
7748   ;; (article-number . line-number-in-body).
7749   (push
7750    (cons article
7751          (save-excursion
7752            (set-buffer gnus-article-buffer)
7753            (count-lines
7754             (min (point)
7755                  (save-excursion
7756                    (goto-char (point-min))
7757                    (search-forward "\n\n" nil t)
7758                    (point)))
7759             (point))))
7760    gnus-newsgroup-bookmarks)
7761   (gnus-message 6 "A bookmark has been added to the current article."))
7762
7763 (defun gnus-summary-remove-bookmark (article)
7764   "Remove the bookmark from the current article."
7765   (interactive (list (gnus-summary-article-number)))
7766   ;; Remove old bookmark, if one exists.
7767   (let ((old (assq article gnus-newsgroup-bookmarks)))
7768     (if old
7769         (progn
7770           (setq gnus-newsgroup-bookmarks
7771                 (delq old gnus-newsgroup-bookmarks))
7772           (gnus-message 6 "Removed bookmark."))
7773       (gnus-message 6 "No bookmark in current article."))))
7774
7775 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7776 (defun gnus-summary-mark-as-dormant (n)
7777   "Mark N articles forward as dormant.
7778 If N is negative, mark backward instead.  The difference between N and
7779 the actual number of articles marked is returned."
7780   (interactive "p")
7781   (gnus-summary-mark-forward n gnus-dormant-mark))
7782
7783 (defun gnus-summary-set-process-mark (article)
7784   "Set the process mark on ARTICLE and update the summary line."
7785   (setq gnus-newsgroup-processable
7786         (cons article
7787               (delq article gnus-newsgroup-processable)))
7788   (when (gnus-summary-goto-subject article)
7789     (gnus-summary-show-thread)
7790     (gnus-summary-goto-subject article)
7791     (gnus-summary-update-secondary-mark article)))
7792
7793 (defun gnus-summary-remove-process-mark (article)
7794   "Remove the process mark from ARTICLE and update the summary line."
7795   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7796   (when (gnus-summary-goto-subject article)
7797     (gnus-summary-show-thread)
7798     (gnus-summary-goto-subject article)
7799     (gnus-summary-update-secondary-mark article)))
7800
7801 (defun gnus-summary-set-saved-mark (article)
7802   "Set the process mark on ARTICLE and update the summary line."
7803   (push article gnus-newsgroup-saved)
7804   (when (gnus-summary-goto-subject article)
7805     (gnus-summary-update-secondary-mark article)))
7806
7807 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7808   "Mark N articles as read forwards.
7809 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7810 The difference between N and the actual number of articles marked is
7811 returned."
7812   (interactive "p")
7813   (gnus-summary-show-thread)
7814   (let ((backward (< n 0))
7815         (gnus-summary-goto-unread
7816          (and gnus-summary-goto-unread
7817               (not (eq gnus-summary-goto-unread 'never))
7818               (not (memq mark (list gnus-unread-mark
7819                                     gnus-ticked-mark gnus-dormant-mark)))))
7820         (n (abs n))
7821         (mark (or mark gnus-del-mark)))
7822     (while (and (> n 0)
7823                 (gnus-summary-mark-article nil mark no-expire)
7824                 (zerop (gnus-summary-next-subject
7825                         (if backward -1 1)
7826                         (and gnus-summary-goto-unread
7827                              (not (eq gnus-summary-goto-unread 'never)))
7828                         t)))
7829       (setq n (1- n)))
7830     (when (/= 0 n)
7831       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7832     (gnus-summary-recenter)
7833     (gnus-summary-position-point)
7834     (gnus-set-mode-line 'summary)
7835     n))
7836
7837 (defun gnus-summary-mark-article-as-read (mark)
7838   "Mark the current article quickly as read with MARK."
7839   (let ((article (gnus-summary-article-number)))
7840     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7841     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7842     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7843     (push (cons article mark) gnus-newsgroup-reads)
7844     ;; Possibly remove from cache, if that is used.
7845     (when gnus-use-cache
7846       (gnus-cache-enter-remove-article article))
7847     ;; Allow the backend to change the mark.
7848     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7849     ;; Check for auto-expiry.
7850     (when (and gnus-newsgroup-auto-expire
7851                (memq mark gnus-auto-expirable-marks))
7852       (setq mark gnus-expirable-mark)
7853       ;; Let the backend know about the mark change.
7854       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7855       (push article gnus-newsgroup-expirable))
7856     ;; Set the mark in the buffer.
7857     (gnus-summary-update-mark mark 'unread)
7858     t))
7859
7860 (defun gnus-summary-mark-article-as-unread (mark)
7861   "Mark the current article quickly as unread with MARK."
7862   (let* ((article (gnus-summary-article-number))
7863          (old-mark (gnus-summary-article-mark article)))
7864     ;; Allow the backend to change the mark.
7865     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7866     (if (eq mark old-mark)
7867         t
7868       (if (<= article 0)
7869           (progn
7870             (gnus-error 1 "Can't mark negative article numbers")
7871             nil)
7872         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7873         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7874         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7875         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7876         (cond ((= mark gnus-ticked-mark)
7877                (push article gnus-newsgroup-marked))
7878               ((= mark gnus-dormant-mark)
7879                (push article gnus-newsgroup-dormant))
7880               (t
7881                (push article gnus-newsgroup-unreads)))
7882         (gnus-pull article gnus-newsgroup-reads)
7883
7884         ;; See whether the article is to be put in the cache.
7885         (and gnus-use-cache
7886              (vectorp (gnus-summary-article-header article))
7887              (save-excursion
7888                (gnus-cache-possibly-enter-article
7889                 gnus-newsgroup-name article
7890                 (gnus-summary-article-header article)
7891                 (= mark gnus-ticked-mark)
7892                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7893
7894         ;; Fix the mark.
7895         (gnus-summary-update-mark mark 'unread)
7896         t))))
7897
7898 (defun gnus-summary-mark-article (&optional article mark no-expire)
7899   "Mark ARTICLE with MARK.  MARK can be any character.
7900 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7901 `??' (dormant) and `?E' (expirable).
7902 If MARK is nil, then the default character `?r' is used.
7903 If ARTICLE is nil, then the article on the current line will be
7904 marked."
7905   ;; The mark might be a string.
7906   (when (stringp mark)
7907     (setq mark (aref mark 0)))
7908   ;; If no mark is given, then we check auto-expiring.
7909   (when (null mark)
7910     (setq mark gnus-del-mark))
7911   (when (and (not no-expire)
7912              gnus-newsgroup-auto-expire
7913              (memq mark gnus-auto-expirable-marks))
7914     (setq mark gnus-expirable-mark))
7915   (let ((article (or article (gnus-summary-article-number)))
7916         (old-mark (gnus-summary-article-mark article)))
7917     ;; Allow the backend to change the mark.
7918     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7919     (if (eq mark old-mark)
7920         t
7921       (unless article
7922         (error "No article on current line"))
7923       (if (not (if (or (= mark gnus-unread-mark)
7924                        (= mark gnus-ticked-mark)
7925                        (= mark gnus-dormant-mark))
7926                    (gnus-mark-article-as-unread article mark)
7927                  (gnus-mark-article-as-read article mark)))
7928           t
7929         ;; See whether the article is to be put in the cache.
7930         (and gnus-use-cache
7931              (not (= mark gnus-canceled-mark))
7932              (vectorp (gnus-summary-article-header article))
7933              (save-excursion
7934                (gnus-cache-possibly-enter-article
7935                 gnus-newsgroup-name article
7936                 (gnus-summary-article-header article)
7937                 (= mark gnus-ticked-mark)
7938                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7939
7940         (when (gnus-summary-goto-subject article nil t)
7941           (let ((buffer-read-only nil))
7942             (gnus-summary-show-thread)
7943             ;; Fix the mark.
7944             (gnus-summary-update-mark mark 'unread)
7945             t))))))
7946
7947 (defun gnus-summary-update-secondary-mark (article)
7948   "Update the secondary (read, process, cache) mark."
7949   (gnus-summary-update-mark
7950    (cond ((memq article gnus-newsgroup-processable)
7951           gnus-process-mark)
7952          ((memq article gnus-newsgroup-cached)
7953           gnus-cached-mark)
7954          ((memq article gnus-newsgroup-replied)
7955           gnus-replied-mark)
7956          ((memq article gnus-newsgroup-saved)
7957           gnus-saved-mark)
7958          (t gnus-unread-mark))
7959    'replied)
7960   (when (gnus-visual-p 'summary-highlight 'highlight)
7961     (gnus-run-hooks 'gnus-summary-update-hook))
7962   t)
7963
7964 (defun gnus-summary-update-mark (mark type)
7965   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7966         (buffer-read-only nil))
7967     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7968     (when forward
7969       (when (looking-at "\r")
7970         (incf forward))
7971       (when (<= (+ forward (point)) (point-max))
7972         ;; Go to the right position on the line.
7973         (goto-char (+ forward (point)))
7974         ;; Replace the old mark with the new mark.
7975         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7976         ;; Optionally update the marks by some user rule.
7977         (when (eq type 'unread)
7978           (gnus-data-set-mark
7979            (gnus-data-find (gnus-summary-article-number)) mark)
7980           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7981
7982 (defun gnus-mark-article-as-read (article &optional mark)
7983   "Enter ARTICLE in the pertinent lists and remove it from others."
7984   ;; Make the article expirable.
7985   (let ((mark (or mark gnus-del-mark)))
7986     (if (= mark gnus-expirable-mark)
7987         (push article gnus-newsgroup-expirable)
7988       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7989     ;; Remove from unread and marked lists.
7990     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7991     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7992     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7993     (push (cons article mark) gnus-newsgroup-reads)
7994     ;; Possibly remove from cache, if that is used.
7995     (when gnus-use-cache
7996       (gnus-cache-enter-remove-article article))
7997     t))
7998
7999 (defun gnus-mark-article-as-unread (article &optional mark)
8000   "Enter ARTICLE in the pertinent lists and remove it from others."
8001   (let ((mark (or mark gnus-ticked-mark)))
8002     (if (<= article 0)
8003         (progn
8004           (gnus-error 1 "Can't mark negative article numbers")
8005           nil)
8006       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8007             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8008             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8009             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8010
8011       ;; Unsuppress duplicates?
8012       (when gnus-suppress-duplicates
8013         (gnus-dup-unsuppress-article article))
8014
8015       (cond ((= mark gnus-ticked-mark)
8016              (push article gnus-newsgroup-marked))
8017             ((= mark gnus-dormant-mark)
8018              (push article gnus-newsgroup-dormant))
8019             (t
8020              (push article gnus-newsgroup-unreads)))
8021       (gnus-pull article gnus-newsgroup-reads)
8022       t)))
8023
8024 (defalias 'gnus-summary-mark-as-unread-forward
8025   'gnus-summary-tick-article-forward)
8026 (make-obsolete 'gnus-summary-mark-as-unread-forward
8027                'gnus-summary-tick-article-forward)
8028 (defun gnus-summary-tick-article-forward (n)
8029   "Tick N articles forwards.
8030 If N is negative, tick backwards instead.
8031 The difference between N and the number of articles ticked is returned."
8032   (interactive "p")
8033   (gnus-summary-mark-forward n gnus-ticked-mark))
8034
8035 (defalias 'gnus-summary-mark-as-unread-backward
8036   'gnus-summary-tick-article-backward)
8037 (make-obsolete 'gnus-summary-mark-as-unread-backward
8038                'gnus-summary-tick-article-backward)
8039 (defun gnus-summary-tick-article-backward (n)
8040   "Tick N articles backwards.
8041 The difference between N and the number of articles ticked is returned."
8042   (interactive "p")
8043   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8044
8045 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8046 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8047 (defun gnus-summary-tick-article (&optional article clear-mark)
8048   "Mark current article as unread.
8049 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8050 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8051   (interactive)
8052   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8053                                        gnus-ticked-mark)))
8054
8055 (defun gnus-summary-mark-as-read-forward (n)
8056   "Mark N articles as read forwards.
8057 If N is negative, mark backwards instead.
8058 The difference between N and the actual number of articles marked is
8059 returned."
8060   (interactive "p")
8061   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8062
8063 (defun gnus-summary-mark-as-read-backward (n)
8064   "Mark the N articles as read backwards.
8065 The difference between N and the actual number of articles marked is
8066 returned."
8067   (interactive "p")
8068   (gnus-summary-mark-forward (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8069
8070 (defun gnus-summary-mark-as-read (&optional article mark)
8071   "Mark current article as read.
8072 ARTICLE specifies the article to be marked as read.
8073 MARK specifies a string to be inserted at the beginning of the line."
8074   (gnus-summary-mark-article article mark))
8075
8076 (defun gnus-summary-clear-mark-forward (n)
8077   "Clear marks from N articles forward.
8078 If N is negative, clear backward instead.
8079 The difference between N and the number of marks cleared is returned."
8080   (interactive "p")
8081   (gnus-summary-mark-forward n gnus-unread-mark))
8082
8083 (defun gnus-summary-clear-mark-backward (n)
8084   "Clear marks from N articles backward.
8085 The difference between N and the number of marks cleared is returned."
8086   (interactive "p")
8087   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8088
8089 (defun gnus-summary-mark-unread-as-read ()
8090   "Intended to be used by `gnus-summary-mark-article-hook'."
8091   (when (memq gnus-current-article gnus-newsgroup-unreads)
8092     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8093
8094 (defun gnus-summary-mark-read-and-unread-as-read ()
8095   "Intended to be used by `gnus-summary-mark-article-hook'."
8096   (let ((mark (gnus-summary-article-mark)))
8097     (when (or (gnus-unread-mark-p mark)
8098               (gnus-read-mark-p mark))
8099       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8100
8101 (defun gnus-summary-mark-region-as-read (point mark all)
8102   "Mark all unread articles between point and mark as read.
8103 If given a prefix, mark all articles between point and mark as read,
8104 even ticked and dormant ones."
8105   (interactive "r\nP")
8106   (save-excursion
8107     (let (article)
8108       (goto-char point)
8109       (beginning-of-line)
8110       (while (and
8111               (< (point) mark)
8112               (progn
8113                 (when (or all
8114                           (memq (setq article (gnus-summary-article-number))
8115                                 gnus-newsgroup-unreads))
8116                   (gnus-summary-mark-article article gnus-del-mark))
8117                 t)
8118               (gnus-summary-find-next))))))
8119
8120 (defun gnus-summary-mark-below (score mark)
8121   "Mark articles with score less than SCORE with MARK."
8122   (interactive "P\ncMark: ")
8123   (setq score (if score
8124                   (prefix-numeric-value score)
8125                 (or gnus-summary-default-score 0)))
8126   (save-excursion
8127     (set-buffer gnus-summary-buffer)
8128     (goto-char (point-min))
8129     (while
8130         (progn
8131           (and (< (gnus-summary-article-score) score)
8132                (gnus-summary-mark-article nil mark))
8133           (gnus-summary-find-next)))))
8134
8135 (defun gnus-summary-kill-below (&optional score)
8136   "Mark articles with score below SCORE as read."
8137   (interactive "P")
8138   (gnus-summary-mark-below score gnus-killed-mark))
8139
8140 (defun gnus-summary-clear-above (&optional score)
8141   "Clear all marks from articles with score above SCORE."
8142   (interactive "P")
8143   (gnus-summary-mark-above score gnus-unread-mark))
8144
8145 (defun gnus-summary-tick-above (&optional score)
8146   "Tick all articles with score above SCORE."
8147   (interactive "P")
8148   (gnus-summary-mark-above score gnus-ticked-mark))
8149
8150 (defun gnus-summary-mark-above (score mark)
8151   "Mark articles with score over SCORE with MARK."
8152   (interactive "P\ncMark: ")
8153   (setq score (if score
8154                   (prefix-numeric-value score)
8155                 (or gnus-summary-default-score 0)))
8156   (save-excursion
8157     (set-buffer gnus-summary-buffer)
8158     (goto-char (point-min))
8159     (while (and (progn
8160                   (when (> (gnus-summary-article-score) score)
8161                     (gnus-summary-mark-article nil mark))
8162                   t)
8163                 (gnus-summary-find-next)))))
8164
8165 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8166 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8167 (defun gnus-summary-limit-include-expunged (&optional no-error)
8168   "Display all the hidden articles that were expunged for low scores."
8169   (interactive)
8170   (let ((buffer-read-only nil))
8171     (let ((scored gnus-newsgroup-scored)
8172           headers h)
8173       (while scored
8174         (unless (gnus-summary-goto-subject (caar scored))
8175           (and (setq h (gnus-summary-article-header (caar scored)))
8176                (< (cdar scored) gnus-summary-expunge-below)
8177                (push h headers)))
8178         (setq scored (cdr scored)))
8179       (if (not headers)
8180           (when (not no-error)
8181             (error "No expunged articles hidden"))
8182         (goto-char (point-min))
8183         (gnus-summary-prepare-unthreaded (nreverse headers))
8184         (goto-char (point-min))
8185         (gnus-summary-position-point)
8186         t))))
8187
8188 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8189   "Mark all unread articles in this newsgroup as read.
8190 If prefix argument ALL is non-nil, ticked and dormant articles will
8191 also be marked as read.
8192 If QUIETLY is non-nil, no questions will be asked.
8193 If TO-HERE is non-nil, it should be a point in the buffer.  All
8194 articles before this point will be marked as read.
8195 Note that this function will only catch up the unread article
8196 in the current summary buffer limitation.
8197 The number of articles marked as read is returned."
8198   (interactive "P")
8199   (prog1
8200       (save-excursion
8201         (when (or quietly
8202                   (not gnus-interactive-catchup) ;Without confirmation?
8203                   gnus-expert-user
8204                   (gnus-y-or-n-p
8205                    (if all
8206                        "Mark absolutely all articles as read? "
8207                      "Mark all unread articles as read? ")))
8208           (if (and not-mark
8209                    (not gnus-newsgroup-adaptive)
8210                    (not gnus-newsgroup-auto-expire)
8211                    (not gnus-suppress-duplicates)
8212                    (or (not gnus-use-cache)
8213                        (eq gnus-use-cache 'passive)))
8214               (progn
8215                 (when all
8216                   (setq gnus-newsgroup-marked nil
8217                         gnus-newsgroup-dormant nil))
8218                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8219             ;; We actually mark all articles as canceled, which we
8220             ;; have to do when using auto-expiry or adaptive scoring.
8221             (gnus-summary-show-all-threads)
8222             (when (gnus-summary-first-subject (not all) t)
8223               (while (and
8224                       (if to-here (< (point) to-here) t)
8225                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8226                       (gnus-summary-find-next (not all) nil nil t))))
8227             (gnus-set-mode-line 'summary))
8228           t))
8229     (gnus-summary-position-point)))
8230
8231 (defun gnus-summary-catchup-to-here (&optional all)
8232   "Mark all unticked articles before the current one as read.
8233 If ALL is non-nil, also mark ticked and dormant articles as read."
8234   (interactive "P")
8235   (save-excursion
8236     (gnus-save-hidden-threads
8237       (let ((beg (point)))
8238         ;; We check that there are unread articles.
8239         (when (or all (gnus-summary-find-prev))
8240           (gnus-summary-catchup all t beg)))))
8241   (gnus-summary-position-point))
8242
8243 (defun gnus-summary-catchup-all (&optional quietly)
8244   "Mark all articles in this newsgroup as read."
8245   (interactive "P")
8246   (gnus-summary-catchup t quietly))
8247
8248 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8249   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8250 If prefix argument ALL is non-nil, all articles are marked as read."
8251   (interactive "P")
8252   (when (gnus-summary-catchup all quietly nil 'fast)
8253     ;; Select next newsgroup or exit.
8254     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8255              (eq gnus-auto-select-next 'quietly))
8256         (gnus-summary-next-group nil)
8257       (gnus-summary-exit))))
8258
8259 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8260   "Mark all articles in this newsgroup as read, and then exit."
8261   (interactive "P")
8262   (gnus-summary-catchup-and-exit t quietly))
8263
8264 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8265 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8266   "Mark all articles in this group as read and select the next group.
8267 If given a prefix, mark all articles, unread as well as ticked, as
8268 read."
8269   (interactive "P")
8270   (save-excursion
8271     (gnus-summary-catchup all))
8272   (gnus-summary-next-article t nil nil t))
8273
8274 ;; Thread-based commands.
8275
8276 (defun gnus-summary-articles-in-thread (&optional article)
8277   "Return a list of all articles in the current thread.
8278 If ARTICLE is non-nil, return all articles in the thread that starts
8279 with that article."
8280   (let* ((article (or article (gnus-summary-article-number)))
8281          (data (gnus-data-find-list article))
8282          (top-level (gnus-data-level (car data)))
8283          (top-subject
8284           (cond ((null gnus-thread-operation-ignore-subject)
8285                  (gnus-simplify-subject-re
8286                   (mail-header-subject (gnus-data-header (car data)))))
8287                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8288                  (gnus-simplify-subject-fuzzy
8289                   (mail-header-subject (gnus-data-header (car data)))))
8290                 (t nil)))
8291          (end-point (save-excursion
8292                       (if (gnus-summary-go-to-next-thread)
8293                           (point) (point-max))))
8294          articles)
8295     (while (and data
8296                 (< (gnus-data-pos (car data)) end-point))
8297       (when (or (not top-subject)
8298                 (string= top-subject
8299                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8300                              (gnus-simplify-subject-fuzzy
8301                               (mail-header-subject
8302                                (gnus-data-header (car data))))
8303                            (gnus-simplify-subject-re
8304                             (mail-header-subject
8305                              (gnus-data-header (car data)))))))
8306         (push (gnus-data-number (car data)) articles))
8307       (unless (and (setq data (cdr data))
8308                    (> (gnus-data-level (car data)) top-level))
8309         (setq data nil)))
8310     ;; Return the list of articles.
8311     (nreverse articles)))
8312
8313 (defun gnus-summary-rethread-current ()
8314   "Rethread the thread the current article is part of."
8315   (interactive)
8316   (let* ((gnus-show-threads t)
8317          (article (gnus-summary-article-number))
8318          (id (mail-header-id (gnus-summary-article-header)))
8319          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8320     (unless id
8321       (error "No article on the current line"))
8322     (gnus-rebuild-thread id)
8323     (gnus-summary-goto-subject article)))
8324
8325 (defun gnus-summary-reparent-thread ()
8326   "Make the current article child of the marked (or previous) article.
8327
8328 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8329 is non-nil or the Subject: of both articles are the same."
8330   (interactive)
8331   (unless (not (gnus-group-read-only-p))
8332     (error "The current newsgroup does not support article editing"))
8333   (unless (<= (length gnus-newsgroup-processable) 1)
8334     (error "No more than one article may be marked"))
8335   (save-window-excursion
8336     (let ((gnus-article-buffer " *reparent*")
8337           (current-article (gnus-summary-article-number))
8338           ;; First grab the marked article, otherwise one line up.
8339           (parent-article (if (not (null gnus-newsgroup-processable))
8340                               (car gnus-newsgroup-processable)
8341                             (save-excursion
8342                               (if (eq (forward-line -1) 0)
8343                                   (gnus-summary-article-number)
8344                                 (error "Beginning of summary buffer"))))))
8345       (unless (not (eq current-article parent-article))
8346         (error "An article may not be self-referential"))
8347       (let ((message-id (mail-header-id
8348                          (gnus-summary-article-header parent-article))))
8349         (unless (and message-id (not (equal message-id "")))
8350           (error "No message-id in desired parent"))
8351         ;; We don't want the article to be marked as read.
8352         (let (gnus-mark-article-hook)
8353           (gnus-summary-select-article t t nil current-article))
8354         (set-buffer gnus-original-article-buffer)
8355         (let ((buf (format "%s" (buffer-string))))
8356           (with-temp-buffer
8357             (insert buf)
8358             (goto-char (point-min))
8359             (if (re-search-forward "^References: " nil t)
8360                 (progn
8361                   (re-search-forward "^[^ \t]" nil t)
8362                   (forward-line -1)
8363                   (end-of-line)
8364                   (insert " " message-id))
8365               (insert "References: " message-id "\n"))
8366             (unless (gnus-request-replace-article
8367                      current-article (car gnus-article-current)
8368                      (current-buffer))
8369               (error "Couldn't replace article"))))
8370         (set-buffer gnus-summary-buffer)
8371         (gnus-summary-unmark-all-processable)
8372         (gnus-summary-update-article current-article)
8373         (gnus-summary-rethread-current)
8374         (gnus-message 3 "Article %d is now the child of article %d"
8375                       current-article parent-article)))))
8376
8377 (defun gnus-summary-toggle-threads (&optional arg)
8378   "Toggle showing conversation threads.
8379 If ARG is positive number, turn showing conversation threads on."
8380   (interactive "P")
8381   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8382     (setq gnus-show-threads
8383           (if (null arg) (not gnus-show-threads)
8384             (> (prefix-numeric-value arg) 0)))
8385     (gnus-summary-prepare)
8386     (gnus-summary-goto-subject current)
8387     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8388     (gnus-summary-position-point)))
8389
8390 (defun gnus-summary-show-all-threads ()
8391   "Show all threads."
8392   (interactive)
8393   (save-excursion
8394     (let ((buffer-read-only nil))
8395       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8396   (gnus-summary-position-point))
8397
8398 (defun gnus-summary-show-thread ()
8399   "Show thread subtrees.
8400 Returns nil if no thread was there to be shown."
8401   (interactive)
8402   (let ((buffer-read-only nil)
8403         (orig (point))
8404         ;; first goto end then to beg, to have point at beg after let
8405         (end (progn (end-of-line) (point)))
8406         (beg (progn (beginning-of-line) (point))))
8407     (prog1
8408         ;; Any hidden lines here?
8409         (search-forward "\r" end t)
8410       (subst-char-in-region beg end ?\^M ?\n t)
8411       (goto-char orig)
8412       (gnus-summary-position-point))))
8413
8414 (defun gnus-summary-hide-all-threads ()
8415   "Hide all thread subtrees."
8416   (interactive)
8417   (save-excursion
8418     (goto-char (point-min))
8419     (gnus-summary-hide-thread)
8420     (while (zerop (gnus-summary-next-thread 1 t))
8421       (gnus-summary-hide-thread)))
8422   (gnus-summary-position-point))
8423
8424 (defun gnus-summary-hide-thread ()
8425   "Hide thread subtrees.
8426 Returns nil if no threads were there to be hidden."
8427   (interactive)
8428   (let ((buffer-read-only nil)
8429         (start (point))
8430         (article (gnus-summary-article-number)))
8431     (goto-char start)
8432     ;; Go forward until either the buffer ends or the subthread
8433     ;; ends.
8434     (when (and (not (eobp))
8435                (or (zerop (gnus-summary-next-thread 1 t))
8436                    (goto-char (point-max))))
8437       (prog1
8438           (if (and (> (point) start)
8439                    (search-backward "\n" start t))
8440               (progn
8441                 (subst-char-in-region start (point) ?\n ?\^M)
8442                 (gnus-summary-goto-subject article))
8443             (goto-char start)
8444             nil)
8445         ;;(gnus-summary-position-point)
8446         ))))
8447
8448 (defun gnus-summary-go-to-next-thread (&optional previous)
8449   "Go to the same level (or less) next thread.
8450 If PREVIOUS is non-nil, go to previous thread instead.
8451 Return the article number moved to, or nil if moving was impossible."
8452   (let ((level (gnus-summary-thread-level))
8453         (way (if previous -1 1))
8454         (beg (point)))
8455     (forward-line way)
8456     (while (and (not (eobp))
8457                 (< level (gnus-summary-thread-level)))
8458       (forward-line way))
8459     (if (eobp)
8460         (progn
8461           (goto-char beg)
8462           nil)
8463       (setq beg (point))
8464       (prog1
8465           (gnus-summary-article-number)
8466         (goto-char beg)))))
8467
8468 (defun gnus-summary-next-thread (n &optional silent)
8469   "Go to the same level next N'th thread.
8470 If N is negative, search backward instead.
8471 Returns the difference between N and the number of skips actually
8472 done.
8473
8474 If SILENT, don't output messages."
8475   (interactive "p")
8476   (let ((backward (< n 0))
8477         (n (abs n)))
8478     (while (and (> n 0)
8479                 (gnus-summary-go-to-next-thread backward))
8480       (decf n))
8481     (unless silent
8482       (gnus-summary-position-point))
8483     (when (and (not silent) (/= 0 n))
8484       (gnus-message 7 "No more threads"))
8485     n))
8486
8487 (defun gnus-summary-prev-thread (n)
8488   "Go to the same level previous N'th thread.
8489 Returns the difference between N and the number of skips actually
8490 done."
8491   (interactive "p")
8492   (gnus-summary-next-thread (- n)))
8493
8494 (defun gnus-summary-go-down-thread ()
8495   "Go down one level in the current thread."
8496   (let ((children (gnus-summary-article-children)))
8497     (when children
8498       (gnus-summary-goto-subject (car children)))))
8499
8500 (defun gnus-summary-go-up-thread ()
8501   "Go up one level in the current thread."
8502   (let ((parent (gnus-summary-article-parent)))
8503     (when parent
8504       (gnus-summary-goto-subject parent))))
8505
8506 (defun gnus-summary-down-thread (n)
8507   "Go down thread N steps.
8508 If N is negative, go up instead.
8509 Returns the difference between N and how many steps down that were
8510 taken."
8511   (interactive "p")
8512   (let ((up (< n 0))
8513         (n (abs n)))
8514     (while (and (> n 0)
8515                 (if up (gnus-summary-go-up-thread)
8516                   (gnus-summary-go-down-thread)))
8517       (setq n (1- n)))
8518     (gnus-summary-position-point)
8519     (when (/= 0 n)
8520       (gnus-message 7 "Can't go further"))
8521     n))
8522
8523 (defun gnus-summary-up-thread (n)
8524   "Go up thread N steps.
8525 If N is negative, go up instead.
8526 Returns the difference between N and how many steps down that were
8527 taken."
8528   (interactive "p")
8529   (gnus-summary-down-thread (- n)))
8530
8531 (defun gnus-summary-top-thread ()
8532   "Go to the top of the thread."
8533   (interactive)
8534   (while (gnus-summary-go-up-thread))
8535   (gnus-summary-article-number))
8536
8537 (defun gnus-summary-kill-thread (&optional unmark)
8538   "Mark articles under current thread as read.
8539 If the prefix argument is positive, remove any kinds of marks.
8540 If the prefix argument is negative, tick articles instead."
8541   (interactive "P")
8542   (when unmark
8543     (setq unmark (prefix-numeric-value unmark)))
8544   (let ((articles (gnus-summary-articles-in-thread)))
8545     (save-excursion
8546       ;; Expand the thread.
8547       (gnus-summary-show-thread)
8548       ;; Mark all the articles.
8549       (while articles
8550         (gnus-summary-goto-subject (car articles))
8551         (cond ((null unmark)
8552                (gnus-summary-mark-article-as-read gnus-killed-mark))
8553               ((> unmark 0)
8554                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8555               (t
8556                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8557         (setq articles (cdr articles))))
8558     ;; Hide killed subtrees.
8559     (and (null unmark)
8560          gnus-thread-hide-killed
8561          (gnus-summary-hide-thread))
8562     ;; If marked as read, go to next unread subject.
8563     (when (null unmark)
8564       ;; Go to next unread subject.
8565       (gnus-summary-next-subject 1 t)))
8566   (gnus-set-mode-line 'summary))
8567
8568 ;; Summary sorting commands
8569
8570 (defun gnus-summary-sort-by-number (&optional reverse)
8571   "Sort the summary buffer by article number.
8572 Argument REVERSE means reverse order."
8573   (interactive "P")
8574   (gnus-summary-sort 'number reverse))
8575
8576 (defun gnus-summary-sort-by-author (&optional reverse)
8577   "Sort the summary buffer by author name alphabetically.
8578 If case-fold-search is non-nil, case of letters is ignored.
8579 Argument REVERSE means reverse order."
8580   (interactive "P")
8581   (gnus-summary-sort 'author reverse))
8582
8583 (defun gnus-summary-sort-by-subject (&optional reverse)
8584   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8585 If case-fold-search is non-nil, case of letters is ignored.
8586 Argument REVERSE means reverse order."
8587   (interactive "P")
8588   (gnus-summary-sort 'subject reverse))
8589
8590 (defun gnus-summary-sort-by-date (&optional reverse)
8591   "Sort the summary buffer by date.
8592 Argument REVERSE means reverse order."
8593   (interactive "P")
8594   (gnus-summary-sort 'date reverse))
8595
8596 (defun gnus-summary-sort-by-score (&optional reverse)
8597   "Sort the summary buffer by score.
8598 Argument REVERSE means reverse order."
8599   (interactive "P")
8600   (gnus-summary-sort 'score reverse))
8601
8602 (defun gnus-summary-sort-by-lines (&optional reverse)
8603   "Sort the summary buffer by article length.
8604 Argument REVERSE means reverse order."
8605   (interactive "P")
8606   (gnus-summary-sort 'lines reverse))
8607
8608 (defun gnus-summary-sort (predicate reverse)
8609   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8610   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8611          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8612          (gnus-thread-sort-functions
8613           (if (not reverse)
8614               thread
8615             `(lambda (t1 t2)
8616                (,thread t2 t1))))
8617          (gnus-article-sort-functions
8618           (if (not reverse)
8619               article
8620             `(lambda (t1 t2)
8621                (,article t2 t1))))
8622          (buffer-read-only)
8623          (gnus-summary-prepare-hook nil))
8624     ;; We do the sorting by regenerating the threads.
8625     (gnus-summary-prepare)
8626     ;; Hide subthreads if needed.
8627     (when (and gnus-show-threads gnus-thread-hide-subtree)
8628       (gnus-summary-hide-all-threads))))
8629
8630 ;; Summary saving commands.
8631
8632 (defun gnus-summary-save-article (&optional n not-saved)
8633   "Save the current article using the default saver function.
8634 If N is a positive number, save the N next articles.
8635 If N is a negative number, save the N previous articles.
8636 If N is nil and any articles have been marked with the process mark,
8637 save those articles instead.
8638 The variable `gnus-default-article-saver' specifies the saver function."
8639   (interactive "P")
8640   (let* ((articles (gnus-summary-work-articles n))
8641          (save-buffer (save-excursion
8642                         (nnheader-set-temp-buffer " *Gnus Save*")))
8643          (num (length articles))
8644          header article file)
8645     (while articles
8646       (setq header (gnus-summary-article-header
8647                     (setq article (pop articles))))
8648       (if (not (vectorp header))
8649           ;; This is a pseudo-article.
8650           (if (assq 'name header)
8651               (gnus-copy-file (cdr (assq 'name header)))
8652             (gnus-message 1 "Article %d is unsaveable" article))
8653         ;; This is a real article.
8654         (save-window-excursion
8655           (gnus-summary-select-article t nil nil article))
8656         (save-excursion
8657           (set-buffer save-buffer)
8658           (erase-buffer)
8659           (insert-buffer-substring gnus-original-article-buffer))
8660         (setq file (gnus-article-save save-buffer file num))
8661         (gnus-summary-remove-process-mark article)
8662         (unless not-saved
8663           (gnus-summary-set-saved-mark article))))
8664     (gnus-kill-buffer save-buffer)
8665     (gnus-summary-position-point)
8666     (gnus-set-mode-line 'summary)
8667     n))
8668
8669 (defun gnus-summary-pipe-output (&optional arg)
8670   "Pipe the current article to a subprocess.
8671 If N is a positive number, pipe the N next articles.
8672 If N is a negative number, pipe the N previous articles.
8673 If N is nil and any articles have been marked with the process mark,
8674 pipe those articles instead."
8675   (interactive "P")
8676   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8677     (gnus-summary-save-article arg t))
8678   (gnus-configure-windows 'pipe))
8679
8680 (defun gnus-summary-save-article-mail (&optional arg)
8681   "Append the current article to an mail file.
8682 If N is a positive number, save the N next articles.
8683 If N is a negative number, save the N previous articles.
8684 If N is nil and any articles have been marked with the process mark,
8685 save those articles instead."
8686   (interactive "P")
8687   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8688     (gnus-summary-save-article arg)))
8689
8690 (defun gnus-summary-save-article-rmail (&optional arg)
8691   "Append the current article to an rmail file.
8692 If N is a positive number, save the N next articles.
8693 If N is a negative number, save the N previous articles.
8694 If N is nil and any articles have been marked with the process mark,
8695 save those articles instead."
8696   (interactive "P")
8697   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8698     (gnus-summary-save-article arg)))
8699
8700 (defun gnus-summary-save-article-file (&optional arg)
8701   "Append the current article to a file.
8702 If N is a positive number, save the N next articles.
8703 If N is a negative number, save the N previous articles.
8704 If N is nil and any articles have been marked with the process mark,
8705 save those articles instead."
8706   (interactive "P")
8707   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8708     (gnus-summary-save-article arg)))
8709
8710 (defun gnus-summary-write-article-file (&optional arg)
8711   "Write the current article to a file, deleting the previous file.
8712 If N is a positive number, save the N next articles.
8713 If N is a negative number, save the N previous articles.
8714 If N is nil and any articles have been marked with the process mark,
8715 save those articles instead."
8716   (interactive "P")
8717   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8718     (gnus-summary-save-article arg)))
8719
8720 (defun gnus-summary-save-article-body-file (&optional arg)
8721   "Append the current article body to a file.
8722 If N is a positive number, save the N next articles.
8723 If N is a negative number, save the N previous articles.
8724 If N is nil and any articles have been marked with the process mark,
8725 save those articles instead."
8726   (interactive "P")
8727   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8728     (gnus-summary-save-article arg)))
8729
8730 (defun gnus-summary-pipe-message (program)
8731   "Pipe the current article through PROGRAM."
8732   (interactive "sProgram: ")
8733   (gnus-summary-select-article)
8734   (let ((mail-header-separator ""))
8735     (gnus-eval-in-buffer-window gnus-article-buffer
8736       (save-restriction
8737         (widen)
8738         (let ((start (window-start))
8739               buffer-read-only)
8740           (message-pipe-buffer-body program)
8741           (set-window-start (get-buffer-window (current-buffer)) start))))))
8742
8743 (defun gnus-get-split-value (methods)
8744   "Return a value based on the split METHODS."
8745   (let (split-name method result match)
8746     (when methods
8747       (save-excursion
8748         (set-buffer gnus-original-article-buffer)
8749         (save-restriction
8750           (nnheader-narrow-to-headers)
8751           (while methods
8752             (goto-char (point-min))
8753             (setq method (pop methods))
8754             (setq match (car method))
8755             (when (cond
8756                    ((stringp match)
8757                     ;; Regular expression.
8758                     (ignore-errors
8759                       (re-search-forward match nil t)))
8760                    ((gnus-functionp match)
8761                     ;; Function.
8762                     (save-restriction
8763                       (widen)
8764                       (setq result (funcall match gnus-newsgroup-name))))
8765                    ((consp match)
8766                     ;; Form.
8767                     (save-restriction
8768                       (widen)
8769                       (setq result (eval match)))))
8770               (setq split-name (append (cdr method) split-name))
8771               (cond ((stringp result)
8772                      (push (expand-file-name
8773                             result gnus-article-save-directory)
8774                            split-name))
8775                     ((consp result)
8776                      (setq split-name (append result split-name)))))))))
8777     split-name))
8778
8779 (defun gnus-valid-move-group-p (group)
8780   (and (boundp group)
8781        (symbol-name group)
8782        (symbol-value group)
8783        (memq 'respool
8784              (assoc (symbol-name
8785                      (car (gnus-find-method-for-group
8786                            (symbol-name group))))
8787                     gnus-valid-select-methods))))
8788
8789 (defun gnus-read-move-group-name (prompt default articles prefix)
8790   "Read a group name."
8791   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8792          (minibuffer-confirm-incomplete nil) ; XEmacs
8793          (prom
8794           (format "%s %s to:"
8795                   prompt
8796                   (if (> (length articles) 1)
8797                       (format "these %d articles" (length articles))
8798                     "this article")))
8799          (to-newsgroup
8800           (cond
8801            ((null split-name)
8802             (gnus-completing-read default prom
8803                                   gnus-active-hashtb
8804                                   'gnus-valid-move-group-p
8805                                   nil prefix
8806                                   'gnus-group-history))
8807            ((= 1 (length split-name))
8808             (gnus-completing-read (car split-name) prom
8809                                   gnus-active-hashtb
8810                                   'gnus-valid-move-group-p
8811                                   nil nil
8812                                   'gnus-group-history))
8813            (t
8814             (gnus-completing-read nil prom
8815                                   (mapcar (lambda (el) (list el))
8816                                           (nreverse split-name))
8817                                   nil nil nil
8818                                   'gnus-group-history)))))
8819     (when to-newsgroup
8820       (if (or (string= to-newsgroup "")
8821               (string= to-newsgroup prefix))
8822           (setq to-newsgroup default))
8823       (unless to-newsgroup
8824         (error "No group name entered"))
8825       (or (gnus-active to-newsgroup)
8826           (gnus-activate-group to-newsgroup)
8827           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8828                                      to-newsgroup))
8829               (or (and (gnus-request-create-group
8830                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8831                        (gnus-activate-group to-newsgroup nil nil
8832                                             (gnus-group-name-to-method
8833                                              to-newsgroup)))
8834                   (error "Couldn't create group %s" to-newsgroup)))
8835           (error "No such group: %s" to-newsgroup)))
8836     to-newsgroup))
8837
8838 ;; Summary extract commands
8839
8840 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8841   (let ((buffer-read-only nil)
8842         (article (gnus-summary-article-number))
8843         after-article b e)
8844     (unless (gnus-summary-goto-subject article)
8845       (error "No such article: %d" article))
8846     (gnus-summary-position-point)
8847     ;; If all commands are to be bunched up on one line, we collect
8848     ;; them here.
8849     (unless gnus-view-pseudos-separately
8850       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8851             files action)
8852         (while ps
8853           (setq action (cdr (assq 'action (car ps))))
8854           (setq files (list (cdr (assq 'name (car ps)))))
8855           (while (and ps (cdr ps)
8856                       (string= (or action "1")
8857                                (or (cdr (assq 'action (cadr ps))) "2")))
8858             (push (cdr (assq 'name (cadr ps))) files)
8859             (setcdr ps (cddr ps)))
8860           (when files
8861             (when (not (string-match "%s" action))
8862               (push " " files))
8863             (push " " files)
8864             (when (assq 'execute (car ps))
8865               (setcdr (assq 'execute (car ps))
8866                       (funcall (if (string-match "%s" action)
8867                                    'format 'concat)
8868                                action
8869                                (mapconcat
8870                                 (lambda (f)
8871                                   (if (equal f " ")
8872                                       f
8873                                     (mm-quote-arg f)))
8874                                 files " ")))))
8875           (setq ps (cdr ps)))))
8876     (if (and gnus-view-pseudos (not not-view))
8877         (while pslist
8878           (when (assq 'execute (car pslist))
8879             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8880                                   (eq gnus-view-pseudos 'not-confirm)))
8881           (setq pslist (cdr pslist)))
8882       (save-excursion
8883         (while pslist
8884           (setq after-article (or (cdr (assq 'article (car pslist)))
8885                                   (gnus-summary-article-number)))
8886           (gnus-summary-goto-subject after-article)
8887           (forward-line 1)
8888           (setq b (point))
8889           (insert "    " (file-name-nondirectory
8890                           (cdr (assq 'name (car pslist))))
8891                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8892           (setq e (point))
8893           (forward-line -1)             ; back to `b'
8894           (gnus-add-text-properties
8895            b (1- e) (list 'gnus-number gnus-reffed-article-number
8896                           gnus-mouse-face-prop gnus-mouse-face))
8897           (gnus-data-enter
8898            after-article gnus-reffed-article-number
8899            gnus-unread-mark b (car pslist) 0 (- e b))
8900           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8901           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8902           (setq pslist (cdr pslist)))))))
8903
8904 (defun gnus-pseudos< (p1 p2)
8905   (let ((c1 (cdr (assq 'action p1)))
8906         (c2 (cdr (assq 'action p2))))
8907     (and c1 c2 (string< c1 c2))))
8908
8909 (defun gnus-request-pseudo-article (props)
8910   (cond ((assq 'execute props)
8911          (gnus-execute-command (cdr (assq 'execute props)))))
8912   (let ((gnus-current-article (gnus-summary-article-number)))
8913     (gnus-run-hooks 'gnus-mark-article-hook)))
8914
8915 (defun gnus-execute-command (command &optional automatic)
8916   (save-excursion
8917     (gnus-article-setup-buffer)
8918     (set-buffer gnus-article-buffer)
8919     (setq buffer-read-only nil)
8920     (let ((command (if automatic command
8921                      (read-string "Command: " (cons command 0)))))
8922       (erase-buffer)
8923       (insert "$ " command "\n\n")
8924       (if gnus-view-pseudo-asynchronously
8925           (start-process "gnus-execute" (current-buffer) shell-file-name
8926                          shell-command-switch command)
8927         (call-process shell-file-name nil t nil
8928                       shell-command-switch command)))))
8929
8930 ;; Summary kill commands.
8931
8932 (defun gnus-summary-edit-global-kill (article)
8933   "Edit the \"global\" kill file."
8934   (interactive (list (gnus-summary-article-number)))
8935   (gnus-group-edit-global-kill article))
8936
8937 (defun gnus-summary-edit-local-kill ()
8938   "Edit a local kill file applied to the current newsgroup."
8939   (interactive)
8940   (setq gnus-current-headers (gnus-summary-article-header))
8941   (gnus-group-edit-local-kill
8942    (gnus-summary-article-number) gnus-newsgroup-name))
8943
8944 ;;; Header reading.
8945
8946 (defun gnus-read-header (id &optional header)
8947   "Read the headers of article ID and enter them into the Gnus system."
8948   (let ((group gnus-newsgroup-name)
8949         (gnus-override-method
8950          (and (gnus-news-group-p gnus-newsgroup-name)
8951               gnus-refer-article-method))
8952         where)
8953     ;; First we check to see whether the header in question is already
8954     ;; fetched.
8955     (if (stringp id)
8956         ;; This is a Message-ID.
8957         (setq header (or header (gnus-id-to-header id)))
8958       ;; This is an article number.
8959       (setq header (or header (gnus-summary-article-header id))))
8960     (if (and header
8961              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8962         ;; We have found the header.
8963         header
8964       ;; If this is a sparse article, we have to nix out its
8965       ;; previous entry in the thread hashtb.
8966       (when (and header
8967                  (gnus-summary-article-sparse-p (mail-header-number header)))
8968         (let* ((parent (gnus-parent-id (mail-header-references header)))
8969                (thread (and parent (gnus-id-to-thread parent))))
8970           (when thread
8971             (delq (assq header thread) thread))))
8972       ;; We have to really fetch the header to this article.
8973       (save-excursion
8974         (set-buffer nntp-server-buffer)
8975         (when (setq where (gnus-request-head id group))
8976           (nnheader-fold-continuation-lines)
8977           (goto-char (point-max))
8978           (insert ".\n")
8979           (goto-char (point-min))
8980           (insert "211 ")
8981           (princ (cond
8982                   ((numberp id) id)
8983                   ((cdr where) (cdr where))
8984                   (header (mail-header-number header))
8985                   (t gnus-reffed-article-number))
8986                  (current-buffer))
8987           (insert " Article retrieved.\n"))
8988         (if (or (not where)
8989                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8990             ()                          ; Malformed head.
8991           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8992             (when (and (stringp id)
8993                        (not (string= (gnus-group-real-name group)
8994                                      (car where))))
8995               ;; If we fetched by Message-ID and the article came
8996               ;; from a different group, we fudge some bogus article
8997               ;; numbers for this article.
8998               (mail-header-set-number header gnus-reffed-article-number))
8999             (save-excursion
9000               (set-buffer gnus-summary-buffer)
9001               (decf gnus-reffed-article-number)
9002               (gnus-remove-header (mail-header-number header))
9003               (push header gnus-newsgroup-headers)
9004               (setq gnus-current-headers header)
9005               (push (mail-header-number header) gnus-newsgroup-limit)))
9006           header)))))
9007
9008 (defun gnus-remove-header (number)
9009   "Remove header NUMBER from `gnus-newsgroup-headers'."
9010   (if (and gnus-newsgroup-headers
9011            (= number (mail-header-number (car gnus-newsgroup-headers))))
9012       (pop gnus-newsgroup-headers)
9013     (let ((headers gnus-newsgroup-headers))
9014       (while (and (cdr headers)
9015                   (not (= number (mail-header-number (cadr headers)))))
9016         (pop headers))
9017       (when (cdr headers)
9018         (setcdr headers (cddr headers))))))
9019
9020 ;;;
9021 ;;; summary highlights
9022 ;;;
9023
9024 (defun gnus-highlight-selected-summary ()
9025   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9026   ;; Highlight selected article in summary buffer
9027   (when gnus-summary-selected-face
9028     (save-excursion
9029       (let* ((beg (progn (beginning-of-line) (point)))
9030              (end (progn (end-of-line) (point)))
9031              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9032              (from (if (get-text-property beg gnus-mouse-face-prop)
9033                        beg
9034                      (or (next-single-property-change
9035                           beg gnus-mouse-face-prop nil end)
9036                          beg)))
9037              (to
9038               (if (= from end)
9039                   (- from 2)
9040                 (or (next-single-property-change
9041                      from gnus-mouse-face-prop nil end)
9042                     end))))
9043         ;; If no mouse-face prop on line we will have to = from = end,
9044         ;; so we highlight the entire line instead.
9045         (when (= (+ to 2) from)
9046           (setq from beg)
9047           (setq to end))
9048         (if gnus-newsgroup-selected-overlay
9049             ;; Move old overlay.
9050             (gnus-move-overlay
9051              gnus-newsgroup-selected-overlay from to (current-buffer))
9052           ;; Create new overlay.
9053           (gnus-overlay-put
9054            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9055            'face gnus-summary-selected-face))))))
9056
9057 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9058 (defun gnus-summary-highlight-line ()
9059   "Highlight current line according to `gnus-summary-highlight'."
9060   (let* ((list gnus-summary-highlight)
9061          (p (point))
9062          (end (progn (end-of-line) (point)))
9063          ;; now find out where the line starts and leave point there.
9064          (beg (progn (beginning-of-line) (point)))
9065          (article (gnus-summary-article-number))
9066          (score (or (cdr (assq (or article gnus-current-article)
9067                                gnus-newsgroup-scored))
9068                     gnus-summary-default-score 0))
9069          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9070          (inhibit-read-only t))
9071     ;; Eval the cars of the lists until we find a match.
9072     (let ((default gnus-summary-default-score))
9073       (while (and list
9074                   (not (eval (caar list))))
9075         (setq list (cdr list))))
9076     (let ((face (cdar list)))
9077       (unless (eq face (get-text-property beg 'face))
9078         (gnus-put-text-property-excluding-characters-with-faces
9079          beg end 'face
9080          (setq face (if (boundp face) (symbol-value face) face)))
9081         (when gnus-summary-highlight-line-function
9082           (funcall gnus-summary-highlight-line-function article face))))
9083     (goto-char p)))
9084
9085 (defun gnus-update-read-articles (group unread &optional compute)
9086   "Update the list of read articles in GROUP."
9087   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9088          (entry (gnus-gethash group gnus-newsrc-hashtb))
9089          (info (nth 2 entry))
9090          (prev 1)
9091          (unread (sort (copy-sequence unread) '<))
9092          read)
9093     (if (or (not info) (not active))
9094         ;; There is no info on this group if it was, in fact,
9095         ;; killed.  Gnus stores no information on killed groups, so
9096         ;; there's nothing to be done.
9097         ;; One could store the information somewhere temporarily,
9098         ;; perhaps...  Hmmm...
9099         ()
9100       ;; Remove any negative articles numbers.
9101       (while (and unread (< (car unread) 0))
9102         (setq unread (cdr unread)))
9103       ;; Remove any expired article numbers
9104       (while (and unread (< (car unread) (car active)))
9105         (setq unread (cdr unread)))
9106       ;; Compute the ranges of read articles by looking at the list of
9107       ;; unread articles.
9108       (while unread
9109         (when (/= (car unread) prev)
9110           (push (if (= prev (1- (car unread))) prev
9111                   (cons prev (1- (car unread))))
9112                 read))
9113         (setq prev (1+ (car unread)))
9114         (setq unread (cdr unread)))
9115       (when (<= prev (cdr active))
9116         (push (cons prev (cdr active)) read))
9117       (setq read (if (> (length read) 1) (nreverse read) read))
9118       (if compute
9119           read
9120         (save-excursion
9121           (set-buffer gnus-group-buffer)
9122           (gnus-undo-register
9123             `(progn
9124                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9125                (gnus-info-set-read ',info ',(gnus-info-read info))
9126                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9127                (gnus-group-update-group ,group t))))
9128         ;; Enter this list into the group info.
9129         (gnus-info-set-read info read)
9130         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9131         (gnus-get-unread-articles-in-group info (gnus-active group))
9132         t))))
9133
9134 (defun gnus-offer-save-summaries ()
9135   "Offer to save all active summary buffers."
9136   (save-excursion
9137     (let ((buflist (buffer-list))
9138           buffers bufname)
9139       ;; Go through all buffers and find all summaries.
9140       (while buflist
9141         (and (setq bufname (buffer-name (car buflist)))
9142              (string-match "Summary" bufname)
9143              (save-excursion
9144                (set-buffer bufname)
9145                ;; We check that this is, indeed, a summary buffer.
9146                (and (eq major-mode 'gnus-summary-mode)
9147                     ;; Also make sure this isn't bogus.
9148                     gnus-newsgroup-prepared
9149                     ;; Also make sure that this isn't a dead summary buffer.
9150                     (not gnus-dead-summary-mode)))
9151              (push bufname buffers))
9152         (setq buflist (cdr buflist)))
9153       ;; Go through all these summary buffers and offer to save them.
9154       (when buffers
9155         (map-y-or-n-p
9156          "Update summary buffer %s? "
9157          (lambda (buf)
9158            (switch-to-buffer buf)
9159            (gnus-summary-exit))
9160          buffers)))))
9161
9162 (defun gnus-newsgroup-setup-default-charset ()
9163   "Setup newsgroup default charset."
9164   (let ((name (and gnus-newsgroup-name 
9165                    (string-match "[^:]+$" gnus-newsgroup-name)
9166                    (match-string 0 gnus-newsgroup-name))))
9167     (setq gnus-newsgroup-default-charset
9168           (or (and gnus-newsgroup-name
9169                    (or (gnus-group-find-parameter 
9170                         gnus-newsgroup-name 'charset)
9171                        (let ((alist gnus-newsgroup-default-charset-alist) 
9172                              elem (charset nil))
9173                          (while alist
9174                            (if (and name
9175                                     (string-match 
9176                                      (car (setq elem (pop alist)))
9177                                      name))
9178                                (setq alist nil
9179                                      charset (cdr elem))))
9180                          charset)))
9181               gnus-default-charset))
9182     (setq gnus-newsgroup-iso-8859-1-forced 
9183           (and gnus-newsgroup-name
9184                (or (gnus-group-find-parameter
9185                     gnus-newsgroup-name 'iso-8859-1-forced)
9186                    (and name
9187                         (string-match  gnus-newsgroup-iso-8859-1-forced-regexp 
9188                                        name))))))
9189   (if (stringp gnus-newsgroup-default-charset)
9190       (setq gnus-newsgroup-default-charset
9191             (intern (downcase gnus-newsgroup-default-charset))))
9192   (setq gnus-newsgroup-iso-8859-1-forced
9193         (if (stringp gnus-newsgroup-iso-8859-1-forced)
9194             (intern (downcase gnus-newsgroup-iso-8859-1-forced))
9195           (and gnus-newsgroup-iso-8859-1-forced
9196                gnus-newsgroup-default-charset))))
9197   
9198 ;;;
9199 ;;; MIME Commands
9200 ;;;
9201
9202 (defun gnus-summary-display-buttonized ()
9203   "Display the current article buffer fully MIME-buttonized."
9204   (interactive)
9205   (require 'gnus-art)
9206   (let ((gnus-unbuttonized-mime-types nil))
9207     (gnus-summary-show-article)))
9208     
9209 (defun gnus-summary-toggle-display-buttonized ()
9210   "Toggle the buttonizing of the article buffer."
9211   (interactive)
9212   (require 'gnus-art)
9213   (if (setq gnus-inhibit-mime-unbuttonizing
9214             (not gnus-inhibit-mime-unbuttonizing))
9215       (let ((gnus-unbuttonized-mime-types nil))
9216         (gnus-summary-show-article))
9217     (gnus-summary-show-article)))
9218     
9219 (gnus-ems-redefine)
9220
9221 (provide 'gnus-sum)
9222
9223 (run-hooks 'gnus-sum-load-hook)
9224
9225 ;;; gnus-sum.el ends here