27d3f6e2c8182ac9f7439edcf7c82324bb16303c
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98,99 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 ? ;Whitespace
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 ? ;Whitespace
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-threading
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-group-charset-alist
801   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
802     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
803     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
804     ("^relcom\\>" koi8-r)
805     ("^\\(cz\\|hun\\|pl\\|sk\\)\\>" iso-8859-2)
806     ("^israel\\>" iso-8859-1)
807     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
808     (".*" iso-8859-1))
809   "Alist of regexps (to match group names) and default charsets to be used when reading."
810   :type '(repeat (list (regexp :tag "Group")
811                        (symbol :tag "Charset")))
812   :group 'gnus-charset)
813
814 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
815   "List of charsets that should be ignored.
816 When these charsets are used in the \"charset\" parameter, the
817 default charset will be used instead."
818   :type '(repeat symbol)
819   :group 'gnus-charset)
820
821 ;;; Internal variables
822
823 (defvar gnus-article-mime-handles nil)
824 (defvar gnus-article-decoded-p nil)
825 (defvar gnus-scores-exclude-files nil)
826 (defvar gnus-page-broken nil)
827 (defvar gnus-inhibit-mime-unbuttonizing nil)
828
829 (defvar gnus-original-article nil)
830 (defvar gnus-article-internal-prepare-hook nil)
831 (defvar gnus-newsgroup-process-stack nil)
832
833 (defvar gnus-thread-indent-array nil)
834 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
835
836 ;; Avoid highlighting in kill files.
837 (defvar gnus-summary-inhibit-highlight nil)
838 (defvar gnus-newsgroup-selected-overlay nil)
839 (defvar gnus-inhibit-limiting nil)
840 (defvar gnus-newsgroup-adaptive-score-file nil)
841 (defvar gnus-current-score-file nil)
842 (defvar gnus-current-move-group nil)
843 (defvar gnus-current-copy-group nil)
844 (defvar gnus-current-crosspost-group nil)
845
846 (defvar gnus-newsgroup-dependencies nil)
847 (defvar gnus-newsgroup-adaptive nil)
848 (defvar gnus-summary-display-article-function nil)
849 (defvar gnus-summary-highlight-line-function nil
850   "Function called after highlighting a summary line.")
851
852 (defvar gnus-summary-line-format-alist
853   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
854     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
855     (?s gnus-tmp-subject-or-nil ?s)
856     (?n gnus-tmp-name ?s)
857     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
858         ?s)
859     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
860             gnus-tmp-from) ?s)
861     (?F gnus-tmp-from ?s)
862     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
863     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
864     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
865     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
866     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
867     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
868     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
869     (?L gnus-tmp-lines ?d)
870     (?I gnus-tmp-indentation ?s)
871     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
872     (?R gnus-tmp-replied ?c)
873     (?\[ gnus-tmp-opening-bracket ?c)
874     (?\] gnus-tmp-closing-bracket ?c)
875     (?\> (make-string gnus-tmp-level ? ) ?s)
876     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
877     (?i gnus-tmp-score ?d)
878     (?z gnus-tmp-score-char ?c)
879     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
880     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
881     (?U gnus-tmp-unread ?c)
882     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
883     (?t (gnus-summary-number-of-articles-in-thread
884          (and (boundp 'thread) (car thread)) gnus-tmp-level)
885         ?d)
886     (?e (gnus-summary-number-of-articles-in-thread
887          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
888         ?c)
889     (?u gnus-tmp-user-defined ?s)
890     (?P (gnus-pick-line-number) ?d))
891   "An alist of format specifications that can appear in summary lines,
892 and what variables they correspond with, along with the type of the
893 variable (string, integer, character, etc).")
894
895 (defvar gnus-summary-dummy-line-format-alist
896   `((?S gnus-tmp-subject ?s)
897     (?N gnus-tmp-number ?d)
898     (?u gnus-tmp-user-defined ?s)))
899
900 (defvar gnus-summary-mode-line-format-alist
901   `((?G gnus-tmp-group-name ?s)
902     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
903     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
904     (?A gnus-tmp-article-number ?d)
905     (?Z gnus-tmp-unread-and-unselected ?s)
906     (?V gnus-version ?s)
907     (?U gnus-tmp-unread-and-unticked ?d)
908     (?S gnus-tmp-subject ?s)
909     (?e gnus-tmp-unselected ?d)
910     (?u gnus-tmp-user-defined ?s)
911     (?d (length gnus-newsgroup-dormant) ?d)
912     (?t (length gnus-newsgroup-marked) ?d)
913     (?r (length gnus-newsgroup-reads) ?d)
914     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
915     (?E gnus-newsgroup-expunged-tally ?d)
916     (?s (gnus-current-score-file-nondirectory) ?s)))
917
918 (defvar gnus-last-search-regexp nil
919   "Default regexp for article search command.")
920
921 (defvar gnus-last-shell-command nil
922   "Default shell command on article.")
923
924 (defvar gnus-newsgroup-begin nil)
925 (defvar gnus-newsgroup-end nil)
926 (defvar gnus-newsgroup-last-rmail nil)
927 (defvar gnus-newsgroup-last-mail nil)
928 (defvar gnus-newsgroup-last-folder nil)
929 (defvar gnus-newsgroup-last-file nil)
930 (defvar gnus-newsgroup-auto-expire nil)
931 (defvar gnus-newsgroup-active nil)
932
933 (defvar gnus-newsgroup-data nil)
934 (defvar gnus-newsgroup-data-reverse nil)
935 (defvar gnus-newsgroup-limit nil)
936 (defvar gnus-newsgroup-limits nil)
937
938 (defvar gnus-newsgroup-unreads nil
939   "List of unread articles in the current newsgroup.")
940
941 (defvar gnus-newsgroup-unselected nil
942   "List of unselected unread articles in the current newsgroup.")
943
944 (defvar gnus-newsgroup-reads nil
945   "Alist of read articles and article marks in the current newsgroup.")
946
947 (defvar gnus-newsgroup-expunged-tally nil)
948
949 (defvar gnus-newsgroup-marked nil
950   "List of ticked articles in the current newsgroup (a subset of unread art).")
951
952 (defvar gnus-newsgroup-killed nil
953   "List of ranges of articles that have been through the scoring process.")
954
955 (defvar gnus-newsgroup-cached nil
956   "List of articles that come from the article cache.")
957
958 (defvar gnus-newsgroup-saved nil
959   "List of articles that have been saved.")
960
961 (defvar gnus-newsgroup-kill-headers nil)
962
963 (defvar gnus-newsgroup-replied nil
964   "List of articles that have been replied to in the current newsgroup.")
965
966 (defvar gnus-newsgroup-expirable nil
967   "List of articles in the current newsgroup that can be expired.")
968
969 (defvar gnus-newsgroup-processable nil
970   "List of articles in the current newsgroup that can be processed.")
971
972 (defvar gnus-newsgroup-downloadable nil
973   "List of articles in the current newsgroup that can be processed.")
974
975 (defvar gnus-newsgroup-undownloaded nil
976   "List of articles in the current newsgroup that haven't been downloaded..")
977
978 (defvar gnus-newsgroup-unsendable nil
979   "List of articles in the current newsgroup that won't be sent.")
980
981 (defvar gnus-newsgroup-bookmarks nil
982   "List of articles in the current newsgroup that have bookmarks.")
983
984 (defvar gnus-newsgroup-dormant nil
985   "List of dormant articles in the current newsgroup.")
986
987 (defvar gnus-newsgroup-scored nil
988   "List of scored articles in the current newsgroup.")
989
990 (defvar gnus-newsgroup-headers nil
991   "List of article headers in the current newsgroup.")
992
993 (defvar gnus-newsgroup-threads nil)
994
995 (defvar gnus-newsgroup-prepared nil
996   "Whether the current group has been prepared properly.")
997
998 (defvar gnus-newsgroup-ancient nil
999   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1000
1001 (defvar gnus-newsgroup-sparse nil)
1002
1003 (defvar gnus-current-article nil)
1004 (defvar gnus-article-current nil)
1005 (defvar gnus-current-headers nil)
1006 (defvar gnus-have-all-headers nil)
1007 (defvar gnus-last-article nil)
1008 (defvar gnus-newsgroup-history nil)
1009 (defvar gnus-newsgroup-charset nil)
1010
1011 (defconst gnus-summary-local-variables
1012   '(gnus-newsgroup-name
1013     gnus-newsgroup-begin gnus-newsgroup-end
1014     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1015     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1016     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1017     gnus-newsgroup-unselected gnus-newsgroup-marked
1018     gnus-newsgroup-reads gnus-newsgroup-saved
1019     gnus-newsgroup-replied gnus-newsgroup-expirable
1020     gnus-newsgroup-processable gnus-newsgroup-killed
1021     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1022     gnus-newsgroup-unsendable
1023     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1024     gnus-newsgroup-headers gnus-newsgroup-threads
1025     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1026     gnus-current-article gnus-current-headers gnus-have-all-headers
1027     gnus-last-article gnus-article-internal-prepare-hook
1028     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1029     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1030     gnus-thread-expunge-below
1031     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1032     (gnus-summary-mark-below . global)
1033     gnus-newsgroup-active gnus-scores-exclude-files
1034     gnus-newsgroup-history gnus-newsgroup-ancient
1035     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1036     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1037     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1038     (gnus-newsgroup-expunged-tally . 0)
1039     gnus-cache-removable-articles gnus-newsgroup-cached
1040     gnus-newsgroup-data gnus-newsgroup-data-reverse
1041     gnus-newsgroup-limit gnus-newsgroup-limits
1042     gnus-newsgroup-charset)
1043   "Variables that are buffer-local to the summary buffers.")
1044
1045 ;; Byte-compiler warning.
1046 (defvar gnus-article-mode-map)
1047
1048 ;; MIME stuff.
1049
1050 (defvar gnus-decode-encoded-word-methods
1051   '(mail-decode-encoded-word-string)
1052   "List of methods used to decode encoded words.
1053
1054 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1055 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1056 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1057 whose names match REGEXP.
1058
1059 For example:
1060 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1061  mail-decode-encoded-word-string
1062  (\"chinese\" . rfc1843-decode-string))
1063 ")
1064
1065 (defvar gnus-decode-encoded-word-methods-cache nil)
1066
1067 (defun gnus-multi-decode-encoded-word-string (string)
1068   "Apply the functions from `gnus-encoded-word-methods' that match."
1069   (unless (and gnus-decode-encoded-word-methods-cache
1070                (eq gnus-newsgroup-name
1071                    (car gnus-decode-encoded-word-methods-cache)))
1072     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1073     (mapc '(lambda (x)
1074              (if (symbolp x)
1075                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1076                (if (and gnus-newsgroup-name
1077                         (string-match (car x) gnus-newsgroup-name))
1078                    (nconc gnus-decode-encoded-word-methods-cache
1079                           (list (cdr x))))))
1080           gnus-decode-encoded-word-methods))
1081   (let ((xlist gnus-decode-encoded-word-methods-cache))
1082     (pop xlist)
1083     (while xlist
1084       (setq string (funcall (pop xlist) string))))
1085   string)
1086
1087 ;; Subject simplification.
1088
1089 (defun gnus-simplify-whitespace (str)
1090   "Remove excessive whitespace."
1091   (let ((mystr str))
1092     ;; Multiple spaces.
1093     (while (string-match "[ \t][ \t]+" mystr)
1094       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1095                           " "
1096                           (substring mystr (match-end 0)))))
1097     ;; Leading spaces.
1098     (when (string-match "^[ \t]+" mystr)
1099       (setq mystr (substring mystr (match-end 0))))
1100     ;; Trailing spaces.
1101     (when (string-match "[ \t]+$" mystr)
1102       (setq mystr (substring mystr 0 (match-beginning 0))))
1103     mystr))
1104
1105 (defsubst gnus-simplify-subject-re (subject)
1106   "Remove \"Re:\" from subject lines."
1107   (if (string-match "^[Rr][Ee]: *" subject)
1108       (substring subject (match-end 0))
1109     subject))
1110
1111 (defun gnus-simplify-subject (subject &optional re-only)
1112   "Remove `Re:' and words in parentheses.
1113 If RE-ONLY is non-nil, strip leading `Re:'s only."
1114   (let ((case-fold-search t))           ;Ignore case.
1115     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1116     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1117       (setq subject (substring subject (match-end 0))))
1118     ;; Remove uninteresting prefixes.
1119     (when (and (not re-only)
1120                gnus-simplify-ignored-prefixes
1121                (string-match gnus-simplify-ignored-prefixes subject))
1122       (setq subject (substring subject (match-end 0))))
1123     ;; Remove words in parentheses from end.
1124     (unless re-only
1125       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1126         (setq subject (substring subject 0 (match-beginning 0)))))
1127     ;; Return subject string.
1128     subject))
1129
1130 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1131 ;; all whitespace.
1132 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1133   (goto-char (point-min))
1134   (while (re-search-forward regexp nil t)
1135       (replace-match (or newtext ""))))
1136
1137 (defun gnus-simplify-buffer-fuzzy ()
1138   "Simplify string in the buffer fuzzily.
1139 The string in the accessible portion of the current buffer is simplified.
1140 It is assumed to be a single-line subject.
1141 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1142 matter is removed.  Additional things can be deleted by setting
1143 gnus-simplify-subject-fuzzy-regexp."
1144   (let ((case-fold-search t)
1145         (modified-tick))
1146     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1147
1148     (while (not (eq modified-tick (buffer-modified-tick)))
1149       (setq modified-tick (buffer-modified-tick))
1150       (cond
1151        ((listp gnus-simplify-subject-fuzzy-regexp)
1152         (mapcar 'gnus-simplify-buffer-fuzzy-step
1153                 gnus-simplify-subject-fuzzy-regexp))
1154        (gnus-simplify-subject-fuzzy-regexp
1155         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1156       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1157       (gnus-simplify-buffer-fuzzy-step
1158        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1159       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1160
1161     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1162     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1163     (gnus-simplify-buffer-fuzzy-step " $")
1164     (gnus-simplify-buffer-fuzzy-step "^ +")))
1165
1166 (defun gnus-simplify-subject-fuzzy (subject)
1167   "Simplify a subject string fuzzily.
1168 See `gnus-simplify-buffer-fuzzy' for details."
1169   (save-excursion
1170     (gnus-set-work-buffer)
1171     (let ((case-fold-search t))
1172       ;; Remove uninteresting prefixes.
1173       (when (and gnus-simplify-ignored-prefixes
1174                  (string-match gnus-simplify-ignored-prefixes subject))
1175         (setq subject (substring subject (match-end 0))))
1176       (insert subject)
1177       (inline (gnus-simplify-buffer-fuzzy))
1178       (buffer-string))))
1179
1180 (defsubst gnus-simplify-subject-fully (subject)
1181   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1182   (cond
1183    (gnus-simplify-subject-functions
1184     (gnus-map-function gnus-simplify-subject-functions subject))
1185    ((null gnus-summary-gather-subject-limit)
1186     (gnus-simplify-subject-re subject))
1187    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1188     (gnus-simplify-subject-fuzzy subject))
1189    ((numberp gnus-summary-gather-subject-limit)
1190     (gnus-limit-string (gnus-simplify-subject-re subject)
1191                        gnus-summary-gather-subject-limit))
1192    (t
1193     subject)))
1194
1195 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1196   "Check whether two subjects are equal.
1197 If optional argument simple-first is t, first argument is already
1198 simplified."
1199   (cond
1200    ((null simple-first)
1201     (equal (gnus-simplify-subject-fully s1)
1202            (gnus-simplify-subject-fully s2)))
1203    (t
1204     (equal s1
1205            (gnus-simplify-subject-fully s2)))))
1206
1207 (defun gnus-summary-bubble-group ()
1208   "Increase the score of the current group.
1209 This is a handy function to add to `gnus-summary-exit-hook' to
1210 increase the score of each group you read."
1211   (gnus-group-add-score gnus-newsgroup-name))
1212
1213 \f
1214 ;;;
1215 ;;; Gnus summary mode
1216 ;;;
1217
1218 (put 'gnus-summary-mode 'mode-class 'special)
1219
1220 (when t
1221   ;; Non-orthogonal keys
1222
1223   (gnus-define-keys gnus-summary-mode-map
1224     " " gnus-summary-next-page
1225     "\177" gnus-summary-prev-page
1226     [delete] gnus-summary-prev-page
1227     [backspace] gnus-summary-prev-page
1228     "\r" gnus-summary-scroll-up
1229     "\M-\r" gnus-summary-scroll-down
1230     "n" gnus-summary-next-unread-article
1231     "p" gnus-summary-prev-unread-article
1232     "N" gnus-summary-next-article
1233     "P" gnus-summary-prev-article
1234     "\M-\C-n" gnus-summary-next-same-subject
1235     "\M-\C-p" gnus-summary-prev-same-subject
1236     "\M-n" gnus-summary-next-unread-subject
1237     "\M-p" gnus-summary-prev-unread-subject
1238     "." gnus-summary-first-unread-article
1239     "," gnus-summary-best-unread-article
1240     "\M-s" gnus-summary-search-article-forward
1241     "\M-r" gnus-summary-search-article-backward
1242     "<" gnus-summary-beginning-of-article
1243     ">" gnus-summary-end-of-article
1244     "j" gnus-summary-goto-article
1245     "^" gnus-summary-refer-parent-article
1246     "\M-^" gnus-summary-refer-article
1247     "u" gnus-summary-tick-article-forward
1248     "!" gnus-summary-tick-article-forward
1249     "U" gnus-summary-tick-article-backward
1250     "d" gnus-summary-mark-as-read-forward
1251     "D" gnus-summary-mark-as-read-backward
1252     "E" gnus-summary-mark-as-expirable
1253     "\M-u" gnus-summary-clear-mark-forward
1254     "\M-U" gnus-summary-clear-mark-backward
1255     "k" gnus-summary-kill-same-subject-and-select
1256     "\C-k" gnus-summary-kill-same-subject
1257     "\M-\C-k" gnus-summary-kill-thread
1258     "\M-\C-l" gnus-summary-lower-thread
1259     "e" gnus-summary-edit-article
1260     "#" gnus-summary-mark-as-processable
1261     "\M-#" gnus-summary-unmark-as-processable
1262     "\M-\C-t" gnus-summary-toggle-threads
1263     "\M-\C-s" gnus-summary-show-thread
1264     "\M-\C-h" gnus-summary-hide-thread
1265     "\M-\C-f" gnus-summary-next-thread
1266     "\M-\C-b" gnus-summary-prev-thread
1267     "\M-\C-u" gnus-summary-up-thread
1268     "\M-\C-d" gnus-summary-down-thread
1269     "&" gnus-summary-execute-command
1270     "c" gnus-summary-catchup-and-exit
1271     "\C-w" gnus-summary-mark-region-as-read
1272     "\C-t" gnus-summary-toggle-truncation
1273     "?" gnus-summary-mark-as-dormant
1274     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1275     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1276     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1277     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1278     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1279     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1280     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1281     "=" gnus-summary-expand-window
1282     "\C-x\C-s" gnus-summary-reselect-current-group
1283     "\M-g" gnus-summary-rescan-group
1284     "w" gnus-summary-stop-page-breaking
1285     "\C-c\C-r" gnus-summary-caesar-message
1286     "f" gnus-summary-followup
1287     "F" gnus-summary-followup-with-original
1288     "C" gnus-summary-cancel-article
1289     "r" gnus-summary-reply
1290     "R" gnus-summary-reply-with-original
1291     "\C-c\C-f" gnus-summary-mail-forward
1292     "o" gnus-summary-save-article
1293     "\C-o" gnus-summary-save-article-mail
1294     "|" gnus-summary-pipe-output
1295     "\M-k" gnus-summary-edit-local-kill
1296     "\M-K" gnus-summary-edit-global-kill
1297     ;; "V" gnus-version
1298     "\C-c\C-d" gnus-summary-describe-group
1299     "q" gnus-summary-exit
1300     "Q" gnus-summary-exit-no-update
1301     "\C-c\C-i" gnus-info-find-node
1302     gnus-mouse-2 gnus-mouse-pick-article
1303     "m" gnus-summary-mail-other-window
1304     "a" gnus-summary-post-news
1305     "x" gnus-summary-limit-to-unread
1306     "s" gnus-summary-isearch-article
1307     "t" gnus-summary-toggle-header
1308     "g" gnus-summary-show-article
1309     "l" gnus-summary-goto-last-article
1310     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1311     "\C-d" gnus-summary-enter-digest-group
1312     "\M-\C-d" gnus-summary-read-document
1313     "\M-\C-e" gnus-summary-edit-parameters
1314     "\M-\C-g" gnus-summary-customize-parameters
1315     "\C-c\C-b" gnus-bug
1316     "*" gnus-cache-enter-article
1317     "\M-*" gnus-cache-remove-article
1318     "\M-&" gnus-summary-universal-argument
1319     "\C-l" gnus-recenter
1320     "I" gnus-summary-increase-score
1321     "L" gnus-summary-lower-score
1322     "\M-i" gnus-symbolic-argument
1323     "h" gnus-summary-select-article-buffer
1324
1325     "b" gnus-article-view-part
1326     "\M-t" gnus-summary-toggle-display-buttonized
1327
1328     "V" gnus-summary-score-map
1329     "X" gnus-uu-extract-map
1330     "S" gnus-summary-send-map)
1331
1332   ;; Sort of orthogonal keymap
1333   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1334     "t" gnus-summary-tick-article-forward
1335     "!" gnus-summary-tick-article-forward
1336     "d" gnus-summary-mark-as-read-forward
1337     "r" gnus-summary-mark-as-read-forward
1338     "c" gnus-summary-clear-mark-forward
1339     " " gnus-summary-clear-mark-forward
1340     "e" gnus-summary-mark-as-expirable
1341     "x" gnus-summary-mark-as-expirable
1342     "?" gnus-summary-mark-as-dormant
1343     "b" gnus-summary-set-bookmark
1344     "B" gnus-summary-remove-bookmark
1345     "#" gnus-summary-mark-as-processable
1346     "\M-#" gnus-summary-unmark-as-processable
1347     "S" gnus-summary-limit-include-expunged
1348     "C" gnus-summary-catchup
1349     "H" gnus-summary-catchup-to-here
1350     "\C-c" gnus-summary-catchup-all
1351     "k" gnus-summary-kill-same-subject-and-select
1352     "K" gnus-summary-kill-same-subject
1353     "P" gnus-uu-mark-map)
1354
1355   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1356     "c" gnus-summary-clear-above
1357     "u" gnus-summary-tick-above
1358     "m" gnus-summary-mark-above
1359     "k" gnus-summary-kill-below)
1360
1361   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1362     "/" gnus-summary-limit-to-subject
1363     "n" gnus-summary-limit-to-articles
1364     "w" gnus-summary-pop-limit
1365     "s" gnus-summary-limit-to-subject
1366     "a" gnus-summary-limit-to-author
1367     "u" gnus-summary-limit-to-unread
1368     "m" gnus-summary-limit-to-marks
1369     "M" gnus-summary-limit-exclude-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     "Q" gnus-article-fill-long-lines
1459     "C" gnus-article-capitalize-sentences
1460     "c" gnus-article-remove-cr
1461     "q" gnus-article-de-quoted-unreadable
1462     "f" gnus-article-display-x-face
1463     "l" gnus-summary-stop-page-breaking
1464     "r" gnus-summary-caesar-message
1465     "t" gnus-article-hide-headers
1466     "v" gnus-summary-verbose-headers
1467     "h" gnus-article-treat-html
1468     "d" gnus-article-treat-dumbquotes)
1469
1470   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1471     "a" gnus-article-hide
1472     "h" gnus-article-hide-headers
1473     "b" gnus-article-hide-boring-headers
1474     "s" gnus-article-hide-signature
1475     "c" gnus-article-hide-citation
1476     "C" gnus-article-hide-citation-in-followups
1477     "p" gnus-article-hide-pgp
1478     "B" gnus-article-strip-banner
1479     "P" gnus-article-hide-pem
1480     "\C-c" gnus-article-hide-citation-maybe)
1481
1482   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1483     "a" gnus-article-highlight
1484     "h" gnus-article-highlight-headers
1485     "c" gnus-article-highlight-citation
1486     "s" gnus-article-highlight-signature)
1487
1488   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1489     "w" gnus-article-decode-mime-words
1490     "c" gnus-article-decode-charset
1491     "v" gnus-mime-view-all-parts
1492     "b" gnus-article-view-part)
1493
1494   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1495     "z" gnus-article-date-ut
1496     "u" gnus-article-date-ut
1497     "l" gnus-article-date-local
1498     "e" gnus-article-date-lapsed
1499     "o" gnus-article-date-original
1500     "i" gnus-article-date-iso8601
1501     "s" gnus-article-date-user)
1502
1503   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1504     "t" gnus-article-remove-trailing-blank-lines
1505     "l" gnus-article-strip-leading-blank-lines
1506     "m" gnus-article-strip-multiple-blank-lines
1507     "a" gnus-article-strip-blank-lines
1508     "A" gnus-article-strip-all-blank-lines
1509     "s" gnus-article-strip-leading-space
1510     "e" gnus-article-strip-trailing-space)
1511
1512   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1513     "v" gnus-version
1514     "f" gnus-summary-fetch-faq
1515     "d" gnus-summary-describe-group
1516     "h" gnus-summary-describe-briefly
1517     "i" gnus-info-find-node)
1518
1519   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1520     "e" gnus-summary-expire-articles
1521     "\M-\C-e" gnus-summary-expire-articles-now
1522     "\177" gnus-summary-delete-article
1523     [delete] gnus-summary-delete-article
1524     "m" gnus-summary-move-article
1525     "r" gnus-summary-respool-article
1526     "w" gnus-summary-edit-article
1527     "c" gnus-summary-copy-article
1528     "B" gnus-summary-crosspost-article
1529     "q" gnus-summary-respool-query
1530     "t" gnus-summary-respool-trace
1531     "i" gnus-summary-import-article
1532     "p" gnus-summary-article-posted-p)
1533
1534   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1535     "o" gnus-summary-save-article
1536     "m" gnus-summary-save-article-mail
1537     "F" gnus-summary-write-article-file
1538     "r" gnus-summary-save-article-rmail
1539     "f" gnus-summary-save-article-file
1540     "b" gnus-summary-save-article-body-file
1541     "h" gnus-summary-save-article-folder
1542     "v" gnus-summary-save-article-vm
1543     "p" gnus-summary-pipe-output
1544     "s" gnus-soup-add-article)
1545
1546   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1547     "b" gnus-summary-display-buttonized
1548     "m" gnus-summary-repair-multipart
1549     "v" gnus-article-view-part
1550     "o" gnus-article-save-part
1551     "c" gnus-article-copy-part
1552     "e" gnus-article-externalize-part
1553     "i" gnus-article-inline-part
1554     "|" gnus-article-pipe-part)
1555   )
1556
1557 (defun gnus-summary-make-menu-bar ()
1558   (gnus-turn-off-edit-menu 'summary)
1559
1560   (unless (boundp 'gnus-summary-misc-menu)
1561
1562     (easy-menu-define
1563      gnus-summary-kill-menu gnus-summary-mode-map ""
1564      (cons
1565       "Score"
1566       (nconc
1567        (list
1568         ["Enter score..." gnus-summary-score-entry t]
1569         ["Customize" gnus-score-customize t])
1570        (gnus-make-score-map 'increase)
1571        (gnus-make-score-map 'lower)
1572        '(("Mark"
1573           ["Kill below" gnus-summary-kill-below t]
1574           ["Mark above" gnus-summary-mark-above t]
1575           ["Tick above" gnus-summary-tick-above t]
1576           ["Clear above" gnus-summary-clear-above t])
1577          ["Current score" gnus-summary-current-score t]
1578          ["Set score" gnus-summary-set-score t]
1579          ["Switch current score file..." gnus-score-change-score-file t]
1580          ["Set mark below..." gnus-score-set-mark-below t]
1581          ["Set expunge below..." gnus-score-set-expunge-below t]
1582          ["Edit current score file" gnus-score-edit-current-scores t]
1583          ["Edit score file" gnus-score-edit-file t]
1584          ["Trace score" gnus-score-find-trace t]
1585          ["Find words" gnus-score-find-favourite-words t]
1586          ["Rescore buffer" gnus-summary-rescore t]
1587          ["Increase score..." gnus-summary-increase-score t]
1588          ["Lower score..." gnus-summary-lower-score t]))))
1589
1590     ;; Define both the Article menu in the summary buffer and the equivalent
1591     ;; Commands menu in the article buffer here for consistency.
1592     (let ((innards
1593            '(("Hide"
1594               ["All" gnus-article-hide t]
1595               ["Headers" gnus-article-hide-headers t]
1596               ["Signature" gnus-article-hide-signature t]
1597               ["Citation" gnus-article-hide-citation t]
1598               ["PGP" gnus-article-hide-pgp t]
1599               ["Banner" gnus-article-strip-banner t]
1600               ["Boring headers" gnus-article-hide-boring-headers t])
1601              ("Highlight"
1602               ["All" gnus-article-highlight t]
1603               ["Headers" gnus-article-highlight-headers t]
1604               ["Signature" gnus-article-highlight-signature t]
1605               ["Citation" gnus-article-highlight-citation t])
1606              ("MIME"
1607               ["Words" gnus-article-decode-mime-words t]
1608               ["Charset" gnus-article-decode-charset t]
1609               ["QP" gnus-article-de-quoted-unreadable t]
1610               ["View all" gnus-mime-view-all-parts t])
1611              ("Date"
1612               ["Local" gnus-article-date-local t]
1613               ["ISO8601" gnus-article-date-iso8601 t]
1614               ["UT" gnus-article-date-ut t]
1615               ["Original" gnus-article-date-original t]
1616               ["Lapsed" gnus-article-date-lapsed t]
1617               ["User-defined" gnus-article-date-user t])
1618              ("Washing"
1619               ("Remove Blanks"
1620                ["Leading" gnus-article-strip-leading-blank-lines t]
1621                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1622                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1623                ["All of the above" gnus-article-strip-blank-lines t]
1624                ["All" gnus-article-strip-all-blank-lines t]
1625                ["Leading space" gnus-article-strip-leading-space t]
1626                ["Trailing space" gnus-article-strip-trailing-space t])
1627               ["Overstrike" gnus-article-treat-overstrike t]
1628               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1629               ["Emphasis" gnus-article-emphasize t]
1630               ["Word wrap" gnus-article-fill-cited-article t]
1631               ["Fill long lines" gnus-article-fill-long-lines t]
1632               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1633               ["CR" gnus-article-remove-cr t]
1634               ["Show X-Face" gnus-article-display-x-face t]
1635               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1636               ["UnHTMLize" gnus-article-treat-html t]
1637               ["Rot 13" gnus-summary-caesar-message t]
1638               ["Unix pipe" gnus-summary-pipe-message t]
1639               ["Add buttons" gnus-article-add-buttons t]
1640               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1641               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1642               ["Verbose header" gnus-summary-verbose-headers t]
1643               ["Toggle header" gnus-summary-toggle-header t])
1644              ("Output"
1645               ["Save in default format" gnus-summary-save-article t]
1646               ["Save in file" gnus-summary-save-article-file t]
1647               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1648               ["Save in MH folder" gnus-summary-save-article-folder t]
1649               ["Save in VM folder" gnus-summary-save-article-vm t]
1650               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1651               ["Save body in file" gnus-summary-save-article-body-file t]
1652               ["Pipe through a filter" gnus-summary-pipe-output t]
1653               ["Add to SOUP packet" gnus-soup-add-article t]
1654               ["Print" gnus-summary-print-article t])
1655              ("Backend"
1656               ["Respool article..." gnus-summary-respool-article t]
1657               ["Move article..." gnus-summary-move-article
1658                (gnus-check-backend-function
1659                 'request-move-article gnus-newsgroup-name)]
1660               ["Copy article..." gnus-summary-copy-article t]
1661               ["Crosspost article..." gnus-summary-crosspost-article
1662                (gnus-check-backend-function
1663                 'request-replace-article gnus-newsgroup-name)]
1664               ["Import file..." gnus-summary-import-article t]
1665               ["Check if posted" gnus-summary-article-posted-p t]
1666               ["Edit article" gnus-summary-edit-article
1667                (not (gnus-group-read-only-p))]
1668               ["Delete article" gnus-summary-delete-article
1669                (gnus-check-backend-function
1670                 'request-expire-articles gnus-newsgroup-name)]
1671               ["Query respool" gnus-summary-respool-query t]
1672               ["Trace respool" gnus-summary-respool-trace t]
1673               ["Delete expirable articles" gnus-summary-expire-articles-now
1674                (gnus-check-backend-function
1675                 'request-expire-articles gnus-newsgroup-name)])
1676              ("Extract"
1677               ["Uudecode" gnus-uu-decode-uu t]
1678               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1679               ["Unshar" gnus-uu-decode-unshar t]
1680               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1681               ["Save" gnus-uu-decode-save t]
1682               ["Binhex" gnus-uu-decode-binhex t]
1683               ["Postscript" gnus-uu-decode-postscript t])
1684              ("Cache"
1685               ["Enter article" gnus-cache-enter-article t]
1686               ["Remove article" gnus-cache-remove-article t])
1687              ["Select article buffer" gnus-summary-select-article-buffer t]
1688              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1689              ["Isearch article..." gnus-summary-isearch-article t]
1690              ["Beginning of the article" gnus-summary-beginning-of-article t]
1691              ["End of the article" gnus-summary-end-of-article t]
1692              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1693              ["Fetch referenced articles" gnus-summary-refer-references t]
1694              ["Fetch current thread" gnus-summary-refer-thread t]
1695              ["Fetch article with id..." gnus-summary-refer-article t]
1696              ["Redisplay" gnus-summary-show-article t])))
1697       (easy-menu-define
1698        gnus-summary-article-menu gnus-summary-mode-map ""
1699        (cons "Article" innards))
1700
1701       (easy-menu-define
1702        gnus-article-commands-menu gnus-article-mode-map ""
1703        (cons "Commands" innards)))
1704
1705     (easy-menu-define
1706      gnus-summary-thread-menu gnus-summary-mode-map ""
1707      '("Threads"
1708        ["Toggle threading" gnus-summary-toggle-threads t]
1709        ["Hide threads" gnus-summary-hide-all-threads t]
1710        ["Show threads" gnus-summary-show-all-threads t]
1711        ["Hide thread" gnus-summary-hide-thread t]
1712        ["Show thread" gnus-summary-show-thread t]
1713        ["Go to next thread" gnus-summary-next-thread t]
1714        ["Go to previous thread" gnus-summary-prev-thread t]
1715        ["Go down thread" gnus-summary-down-thread t]
1716        ["Go up thread" gnus-summary-up-thread t]
1717        ["Top of thread" gnus-summary-top-thread t]
1718        ["Mark thread as read" gnus-summary-kill-thread t]
1719        ["Lower thread score" gnus-summary-lower-thread t]
1720        ["Raise thread score" gnus-summary-raise-thread t]
1721        ["Rethread current" gnus-summary-rethread-current t]
1722        ))
1723
1724     (easy-menu-define
1725      gnus-summary-post-menu gnus-summary-mode-map ""
1726      '("Post"
1727        ["Post an article" gnus-summary-post-news t]
1728        ["Followup" gnus-summary-followup t]
1729        ["Followup and yank" gnus-summary-followup-with-original t]
1730        ["Supersede article" gnus-summary-supersede-article t]
1731        ["Cancel article" gnus-summary-cancel-article t]
1732        ["Reply" gnus-summary-reply t]
1733        ["Reply and yank" gnus-summary-reply-with-original t]
1734        ["Wide reply" gnus-summary-wide-reply t]
1735        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1736        ["Mail forward" gnus-summary-mail-forward t]
1737        ["Post forward" gnus-summary-post-forward t]
1738        ["Digest and mail" gnus-uu-digest-mail-forward t]
1739        ["Digest and post" gnus-uu-digest-post-forward t]
1740        ["Resend message" gnus-summary-resend-message t]
1741        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1742        ["Send a mail" gnus-summary-mail-other-window t]
1743        ["Uuencode and post" gnus-uu-post-news t]
1744        ["Followup via news" gnus-summary-followup-to-mail t]
1745        ["Followup via news and yank"
1746         gnus-summary-followup-to-mail-with-original t]
1747        ;;("Draft"
1748        ;;["Send" gnus-summary-send-draft t]
1749        ;;["Send bounced" gnus-resend-bounced-mail t])
1750        ))
1751
1752     (easy-menu-define
1753      gnus-summary-misc-menu gnus-summary-mode-map ""
1754      '("Misc"
1755        ("Mark Read"
1756         ["Mark as read" gnus-summary-mark-as-read-forward t]
1757         ["Mark same subject and select"
1758          gnus-summary-kill-same-subject-and-select t]
1759         ["Mark same subject" gnus-summary-kill-same-subject t]
1760         ["Catchup" gnus-summary-catchup t]
1761         ["Catchup all" gnus-summary-catchup-all t]
1762         ["Catchup to here" gnus-summary-catchup-to-here t]
1763         ["Catchup region" gnus-summary-mark-region-as-read t]
1764         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1765        ("Mark Various"
1766         ["Tick" gnus-summary-tick-article-forward t]
1767         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1768         ["Remove marks" gnus-summary-clear-mark-forward t]
1769         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1770         ["Set bookmark" gnus-summary-set-bookmark t]
1771         ["Remove bookmark" gnus-summary-remove-bookmark t])
1772        ("Mark Limit"
1773         ["Marks..." gnus-summary-limit-to-marks t]
1774         ["Subject..." gnus-summary-limit-to-subject t]
1775         ["Author..." gnus-summary-limit-to-author t]
1776         ["Age..." gnus-summary-limit-to-age t]
1777         ["Score" gnus-summary-limit-to-score t]
1778         ["Unread" gnus-summary-limit-to-unread t]
1779         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1780         ["Articles" gnus-summary-limit-to-articles t]
1781         ["Pop limit" gnus-summary-pop-limit t]
1782         ["Show dormant" gnus-summary-limit-include-dormant t]
1783         ["Hide childless dormant"
1784          gnus-summary-limit-exclude-childless-dormant t]
1785         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1786         ["Hide marked" gnus-summary-limit-exclude-marks t]
1787         ["Show expunged" gnus-summary-show-all-expunged t])
1788        ("Process Mark"
1789         ["Set mark" gnus-summary-mark-as-processable t]
1790         ["Remove mark" gnus-summary-unmark-as-processable t]
1791         ["Remove all marks" gnus-summary-unmark-all-processable t]
1792         ["Mark above" gnus-uu-mark-over t]
1793         ["Mark series" gnus-uu-mark-series t]
1794         ["Mark region" gnus-uu-mark-region t]
1795         ["Unmark region" gnus-uu-unmark-region t]
1796         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1797         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1798         ["Mark all" gnus-uu-mark-all t]
1799         ["Mark buffer" gnus-uu-mark-buffer t]
1800         ["Mark sparse" gnus-uu-mark-sparse t]
1801         ["Mark thread" gnus-uu-mark-thread t]
1802         ["Unmark thread" gnus-uu-unmark-thread t]
1803         ("Process Mark Sets"
1804          ["Kill" gnus-summary-kill-process-mark t]
1805          ["Yank" gnus-summary-yank-process-mark
1806           gnus-newsgroup-process-stack]
1807          ["Save" gnus-summary-save-process-mark t]))
1808        ("Scroll article"
1809         ["Page forward" gnus-summary-next-page t]
1810         ["Page backward" gnus-summary-prev-page t]
1811         ["Line forward" gnus-summary-scroll-up t])
1812        ("Move"
1813         ["Next unread article" gnus-summary-next-unread-article t]
1814         ["Previous unread article" gnus-summary-prev-unread-article t]
1815         ["Next article" gnus-summary-next-article t]
1816         ["Previous article" gnus-summary-prev-article t]
1817         ["Next unread subject" gnus-summary-next-unread-subject t]
1818         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1819         ["Next article same subject" gnus-summary-next-same-subject t]
1820         ["Previous article same subject" gnus-summary-prev-same-subject t]
1821         ["First unread article" gnus-summary-first-unread-article t]
1822         ["Best unread article" gnus-summary-best-unread-article t]
1823         ["Go to subject number..." gnus-summary-goto-subject t]
1824         ["Go to article number..." gnus-summary-goto-article t]
1825         ["Go to the last article" gnus-summary-goto-last-article t]
1826         ["Pop article off history" gnus-summary-pop-article t])
1827        ("Sort"
1828         ["Sort by number" gnus-summary-sort-by-number t]
1829         ["Sort by author" gnus-summary-sort-by-author t]
1830         ["Sort by subject" gnus-summary-sort-by-subject t]
1831         ["Sort by date" gnus-summary-sort-by-date t]
1832         ["Sort by score" gnus-summary-sort-by-score t]
1833         ["Sort by lines" gnus-summary-sort-by-lines t])
1834        ("Help"
1835         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1836         ["Describe group" gnus-summary-describe-group t]
1837         ["Read manual" gnus-info-find-node t])
1838        ("Modes"
1839         ["Pick and read" gnus-pick-mode t]
1840         ["Binary" gnus-binary-mode t])
1841        ("Regeneration"
1842         ["Regenerate" gnus-summary-prepare t]
1843         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1844         ["Toggle threading" gnus-summary-toggle-threads t])
1845        ["Filter articles..." gnus-summary-execute-command t]
1846        ["Run command on subjects..." gnus-summary-universal-argument t]
1847        ["Search articles forward..." gnus-summary-search-article-forward t]
1848        ["Search articles backward..." gnus-summary-search-article-backward t]
1849        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1850        ["Expand window" gnus-summary-expand-window t]
1851        ["Expire expirable articles" gnus-summary-expire-articles
1852         (gnus-check-backend-function
1853          'request-expire-articles gnus-newsgroup-name)]
1854        ["Edit local kill file" gnus-summary-edit-local-kill t]
1855        ["Edit main kill file" gnus-summary-edit-global-kill t]
1856        ["Edit group parameters" gnus-summary-edit-parameters t]
1857        ["Customize group parameters" gnus-summary-customize-parameters t]
1858        ["Send a bug report" gnus-bug t]
1859        ("Exit"
1860         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1861         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1862         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1863         ["Exit group" gnus-summary-exit t]
1864         ["Exit group without updating" gnus-summary-exit-no-update t]
1865         ["Exit and goto next group" gnus-summary-next-group t]
1866         ["Exit and goto prev group" gnus-summary-prev-group t]
1867         ["Reselect group" gnus-summary-reselect-current-group t]
1868         ["Rescan group" gnus-summary-rescan-group t]
1869         ["Update dribble" gnus-summary-save-newsrc t])))
1870
1871     (gnus-run-hooks 'gnus-summary-menu-hook)))
1872
1873 (defun gnus-score-set-default (var value)
1874   "A version of set that updates the GNU Emacs menu-bar."
1875   (set var value)
1876   ;; It is the message that forces the active status to be updated.
1877   (message ""))
1878
1879 (defun gnus-make-score-map (type)
1880   "Make a summary score map of type TYPE."
1881   (if t
1882       nil
1883     (let ((headers '(("author" "from" string)
1884                      ("subject" "subject" string)
1885                      ("article body" "body" string)
1886                      ("article head" "head" string)
1887                      ("xref" "xref" string)
1888                      ("extra header" "extra" string)
1889                      ("lines" "lines" number)
1890                      ("followups to author" "followup" string)))
1891           (types '((number ("less than" <)
1892                            ("greater than" >)
1893                            ("equal" =))
1894                    (string ("substring" s)
1895                            ("exact string" e)
1896                            ("fuzzy string" f)
1897                            ("regexp" r))))
1898           (perms '(("temporary" (current-time-string))
1899                    ("permanent" nil)
1900                    ("immediate" now)))
1901           header)
1902       (list
1903        (apply
1904         'nconc
1905         (list
1906          (if (eq type 'lower)
1907              "Lower score"
1908            "Increase score"))
1909         (let (outh)
1910           (while headers
1911             (setq header (car headers))
1912             (setq outh
1913                   (cons
1914                    (apply
1915                     'nconc
1916                     (list (car header))
1917                     (let ((ts (cdr (assoc (nth 2 header) types)))
1918                           outt)
1919                       (while ts
1920                         (setq outt
1921                               (cons
1922                                (apply
1923                                 'nconc
1924                                 (list (caar ts))
1925                                 (let ((ps perms)
1926                                       outp)
1927                                   (while ps
1928                                     (setq outp
1929                                           (cons
1930                                            (vector
1931                                             (caar ps)
1932                                             (list
1933                                              'gnus-summary-score-entry
1934                                              (nth 1 header)
1935                                              (if (or (string= (nth 1 header)
1936                                                               "head")
1937                                                      (string= (nth 1 header)
1938                                                               "body"))
1939                                                  ""
1940                                                (list 'gnus-summary-header
1941                                                      (nth 1 header)))
1942                                              (list 'quote (nth 1 (car ts)))
1943                                              (list 'gnus-score-default nil)
1944                                              (nth 1 (car ps))
1945                                              t)
1946                                             t)
1947                                            outp))
1948                                     (setq ps (cdr ps)))
1949                                   (list (nreverse outp))))
1950                                outt))
1951                         (setq ts (cdr ts)))
1952                       (list (nreverse outt))))
1953                    outh))
1954             (setq headers (cdr headers)))
1955           (list (nreverse outh))))))))
1956
1957 \f
1958
1959 (defun gnus-summary-mode (&optional group)
1960   "Major mode for reading articles.
1961
1962 All normal editing commands are switched off.
1963 \\<gnus-summary-mode-map>
1964 Each line in this buffer represents one article.  To read an
1965 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1966 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1967 respectively.
1968
1969 You can also post articles and send mail from this buffer.  To
1970 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1971 of an article, type `\\[gnus-summary-reply]'.
1972
1973 There are approx. one gazillion commands you can execute in this
1974 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1975
1976 The following commands are available:
1977
1978 \\{gnus-summary-mode-map}"
1979   (interactive)
1980   (when (gnus-visual-p 'summary-menu 'menu)
1981     (gnus-summary-make-menu-bar))
1982   (kill-all-local-variables)
1983   (gnus-summary-make-local-variables)
1984   (gnus-make-thread-indent-array)
1985   (gnus-simplify-mode-line)
1986   (setq major-mode 'gnus-summary-mode)
1987   (setq mode-name "Summary")
1988   (make-local-variable 'minor-mode-alist)
1989   (use-local-map gnus-summary-mode-map)
1990   (buffer-disable-undo)
1991   (setq buffer-read-only t)             ;Disable modification
1992   (setq truncate-lines t)
1993   (setq selective-display t)
1994   (setq selective-display-ellipses t)   ;Display `...'
1995   (gnus-summary-set-display-table)
1996   (gnus-set-default-directory)
1997   (setq gnus-newsgroup-name group)
1998   (make-local-variable 'gnus-summary-line-format)
1999   (make-local-variable 'gnus-summary-line-format-spec)
2000   (make-local-variable 'gnus-summary-dummy-line-format)
2001   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2002   (make-local-variable 'gnus-summary-mark-positions)
2003   (make-local-hook 'pre-command-hook)
2004   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2005   (gnus-run-hooks 'gnus-summary-mode-hook)
2006   (mm-enable-multibyte)
2007   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2008   (gnus-update-summary-mark-positions))
2009
2010 (defun gnus-summary-make-local-variables ()
2011   "Make all the local summary buffer variables."
2012   (let (global)
2013     (dolist (local gnus-summary-local-variables)
2014       (if (consp local)
2015           (progn
2016             (if (eq (cdr local) 'global)
2017                 ;; Copy the global value of the variable.
2018                 (setq global (symbol-value (car local)))
2019               ;; Use the value from the list.
2020               (setq global (eval (cdr local))))
2021             (set (make-local-variable (car local)) global))
2022         ;; Simple nil-valued local variable.
2023         (set (make-local-variable local) nil)))))
2024
2025 (defun gnus-summary-clear-local-variables ()
2026   (let ((locals gnus-summary-local-variables))
2027     (while locals
2028       (if (consp (car locals))
2029           (and (vectorp (caar locals))
2030                (set (caar locals) nil))
2031         (and (vectorp (car locals))
2032              (set (car locals) nil)))
2033       (setq locals (cdr locals)))))
2034
2035 ;; Summary data functions.
2036
2037 (defmacro gnus-data-number (data)
2038   `(car ,data))
2039
2040 (defmacro gnus-data-set-number (data number)
2041   `(setcar ,data ,number))
2042
2043 (defmacro gnus-data-mark (data)
2044   `(nth 1 ,data))
2045
2046 (defmacro gnus-data-set-mark (data mark)
2047   `(setcar (nthcdr 1 ,data) ,mark))
2048
2049 (defmacro gnus-data-pos (data)
2050   `(nth 2 ,data))
2051
2052 (defmacro gnus-data-set-pos (data pos)
2053   `(setcar (nthcdr 2 ,data) ,pos))
2054
2055 (defmacro gnus-data-header (data)
2056   `(nth 3 ,data))
2057
2058 (defmacro gnus-data-set-header (data header)
2059   `(setf (nth 3 ,data) ,header))
2060
2061 (defmacro gnus-data-level (data)
2062   `(nth 4 ,data))
2063
2064 (defmacro gnus-data-unread-p (data)
2065   `(= (nth 1 ,data) gnus-unread-mark))
2066
2067 (defmacro gnus-data-read-p (data)
2068   `(/= (nth 1 ,data) gnus-unread-mark))
2069
2070 (defmacro gnus-data-pseudo-p (data)
2071   `(consp (nth 3 ,data)))
2072
2073 (defmacro gnus-data-find (number)
2074   `(assq ,number gnus-newsgroup-data))
2075
2076 (defmacro gnus-data-find-list (number &optional data)
2077   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2078      (memq (assq ,number bdata)
2079            bdata)))
2080
2081 (defmacro gnus-data-make (number mark pos header level)
2082   `(list ,number ,mark ,pos ,header ,level))
2083
2084 (defun gnus-data-enter (after-article number mark pos header level offset)
2085   (let ((data (gnus-data-find-list after-article)))
2086     (unless data
2087       (error "No such article: %d" after-article))
2088     (setcdr data (cons (gnus-data-make number mark pos header level)
2089                        (cdr data)))
2090     (setq gnus-newsgroup-data-reverse nil)
2091     (gnus-data-update-list (cddr data) offset)))
2092
2093 (defun gnus-data-enter-list (after-article list &optional offset)
2094   (when list
2095     (let ((data (and after-article (gnus-data-find-list after-article)))
2096           (ilist list))
2097       (if (not (or data
2098                    after-article))
2099           (let ((odata gnus-newsgroup-data))
2100             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2101             (when offset
2102               (gnus-data-update-list odata offset)))
2103         ;; Find the last element in the list to be spliced into the main
2104         ;; list.
2105         (while (cdr list)
2106           (setq list (cdr list)))
2107         (if (not data)
2108             (progn
2109               (setcdr list gnus-newsgroup-data)
2110               (setq gnus-newsgroup-data ilist)
2111               (when offset
2112                 (gnus-data-update-list (cdr list) offset)))
2113           (setcdr list (cdr data))
2114           (setcdr data ilist)
2115           (when offset
2116             (gnus-data-update-list (cdr list) offset))))
2117       (setq gnus-newsgroup-data-reverse nil))))
2118
2119 (defun gnus-data-remove (article &optional offset)
2120   (let ((data gnus-newsgroup-data))
2121     (if (= (gnus-data-number (car data)) article)
2122         (progn
2123           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2124                 gnus-newsgroup-data-reverse nil)
2125           (when offset
2126             (gnus-data-update-list gnus-newsgroup-data offset)))
2127       (while (cdr data)
2128         (when (= (gnus-data-number (cadr data)) article)
2129           (setcdr data (cddr data))
2130           (when offset
2131             (gnus-data-update-list (cdr data) offset))
2132           (setq data nil
2133                 gnus-newsgroup-data-reverse nil))
2134         (setq data (cdr data))))))
2135
2136 (defmacro gnus-data-list (backward)
2137   `(if ,backward
2138        (or gnus-newsgroup-data-reverse
2139            (setq gnus-newsgroup-data-reverse
2140                  (reverse gnus-newsgroup-data)))
2141      gnus-newsgroup-data))
2142
2143 (defun gnus-data-update-list (data offset)
2144   "Add OFFSET to the POS of all data entries in DATA."
2145   (setq gnus-newsgroup-data-reverse nil)
2146   (while data
2147     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2148     (setq data (cdr data))))
2149
2150 (defun gnus-summary-article-pseudo-p (article)
2151   "Say whether this article is a pseudo article or not."
2152   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2153
2154 (defmacro gnus-summary-article-sparse-p (article)
2155   "Say whether this article is a sparse article or not."
2156   `(memq ,article gnus-newsgroup-sparse))
2157
2158 (defmacro gnus-summary-article-ancient-p (article)
2159   "Say whether this article is a sparse article or not."
2160   `(memq ,article gnus-newsgroup-ancient))
2161
2162 (defun gnus-article-parent-p (number)
2163   "Say whether this article is a parent or not."
2164   (let ((data (gnus-data-find-list number)))
2165     (and (cdr data)                     ; There has to be an article after...
2166          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2167             (gnus-data-level (nth 1 data))))))
2168
2169 (defun gnus-article-children (number)
2170   "Return a list of all children to NUMBER."
2171   (let* ((data (gnus-data-find-list number))
2172          (level (gnus-data-level (car data)))
2173          children)
2174     (setq data (cdr data))
2175     (while (and data
2176                 (= (gnus-data-level (car data)) (1+ level)))
2177       (push (gnus-data-number (car data)) children)
2178       (setq data (cdr data)))
2179     children))
2180
2181 (defmacro gnus-summary-skip-intangible ()
2182   "If the current article is intangible, then jump to a different article."
2183   '(let ((to (get-text-property (point) 'gnus-intangible)))
2184      (and to (gnus-summary-goto-subject to))))
2185
2186 (defmacro gnus-summary-article-intangible-p ()
2187   "Say whether this article is intangible or not."
2188   '(get-text-property (point) 'gnus-intangible))
2189
2190 (defun gnus-article-read-p (article)
2191   "Say whether ARTICLE is read or not."
2192   (not (or (memq article gnus-newsgroup-marked)
2193            (memq article gnus-newsgroup-unreads)
2194            (memq article gnus-newsgroup-unselected)
2195            (memq article gnus-newsgroup-dormant))))
2196
2197 ;; Some summary mode macros.
2198
2199 (defmacro gnus-summary-article-number ()
2200   "The article number of the article on the current line.
2201 If there isn's an article number here, then we return the current
2202 article number."
2203   '(progn
2204      (gnus-summary-skip-intangible)
2205      (or (get-text-property (point) 'gnus-number)
2206          (gnus-summary-last-subject))))
2207
2208 (defmacro gnus-summary-article-header (&optional number)
2209   "Return the header of article NUMBER."
2210   `(gnus-data-header (gnus-data-find
2211                       ,(or number '(gnus-summary-article-number)))))
2212
2213 (defmacro gnus-summary-thread-level (&optional number)
2214   "Return the level of thread that starts with article NUMBER."
2215   `(if (and (eq gnus-summary-make-false-root 'dummy)
2216             (get-text-property (point) 'gnus-intangible))
2217        0
2218      (gnus-data-level (gnus-data-find
2219                        ,(or number '(gnus-summary-article-number))))))
2220
2221 (defmacro gnus-summary-article-mark (&optional number)
2222   "Return the mark of article NUMBER."
2223   `(gnus-data-mark (gnus-data-find
2224                     ,(or number '(gnus-summary-article-number)))))
2225
2226 (defmacro gnus-summary-article-pos (&optional number)
2227   "Return the position of the line of article NUMBER."
2228   `(gnus-data-pos (gnus-data-find
2229                    ,(or number '(gnus-summary-article-number)))))
2230
2231 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2232 (defmacro gnus-summary-article-subject (&optional number)
2233   "Return current subject string or nil if nothing."
2234   `(let ((headers
2235           ,(if number
2236                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2237              '(gnus-data-header (assq (gnus-summary-article-number)
2238                                       gnus-newsgroup-data)))))
2239      (and headers
2240           (vectorp headers)
2241           (mail-header-subject headers))))
2242
2243 (defmacro gnus-summary-article-score (&optional number)
2244   "Return current article score."
2245   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2246                   gnus-newsgroup-scored))
2247        gnus-summary-default-score 0))
2248
2249 (defun gnus-summary-article-children (&optional number)
2250   "Return a list of article numbers that are children of article NUMBER."
2251   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2252          (level (gnus-data-level (car data)))
2253          l children)
2254     (while (and (setq data (cdr data))
2255                 (> (setq l (gnus-data-level (car data))) level))
2256       (and (= (1+ level) l)
2257            (push (gnus-data-number (car data))
2258                  children)))
2259     (nreverse children)))
2260
2261 (defun gnus-summary-article-parent (&optional number)
2262   "Return the article number of the parent of article NUMBER."
2263   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2264                                     (gnus-data-list t)))
2265          (level (gnus-data-level (car data))))
2266     (if (zerop level)
2267         ()                              ; This is a root.
2268       ;; We search until we find an article with a level less than
2269       ;; this one.  That function has to be the parent.
2270       (while (and (setq data (cdr data))
2271                   (not (< (gnus-data-level (car data)) level))))
2272       (and data (gnus-data-number (car data))))))
2273
2274 (defun gnus-unread-mark-p (mark)
2275   "Say whether MARK is the unread mark."
2276   (= mark gnus-unread-mark))
2277
2278 (defun gnus-read-mark-p (mark)
2279   "Say whether MARK is one of the marks that mark as read.
2280 This is all marks except unread, ticked, dormant, and expirable."
2281   (not (or (= mark gnus-unread-mark)
2282            (= mark gnus-ticked-mark)
2283            (= mark gnus-dormant-mark)
2284            (= mark gnus-expirable-mark))))
2285
2286 (defmacro gnus-article-mark (number)
2287   "Return the MARK of article NUMBER.
2288 This macro should only be used when computing the mark the \"first\"
2289 time; i.e., when generating the summary lines.  After that,
2290 `gnus-summary-article-mark' should be used to examine the
2291 marks of articles."
2292   `(cond
2293     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2294     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2295     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2296     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2297     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2298     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2299     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2300     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2301            gnus-ancient-mark))))
2302
2303 ;; Saving hidden threads.
2304
2305 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2306 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2307
2308 (defmacro gnus-save-hidden-threads (&rest forms)
2309   "Save hidden threads, eval FORMS, and restore the hidden threads."
2310   (let ((config (make-symbol "config")))
2311     `(let ((,config (gnus-hidden-threads-configuration)))
2312        (unwind-protect
2313            (save-excursion
2314              ,@forms)
2315          (gnus-restore-hidden-threads-configuration ,config)))))
2316
2317 (defun gnus-data-compute-positions ()
2318   "Compute the positions of all articles."
2319   (setq gnus-newsgroup-data-reverse nil)
2320   (let ((data gnus-newsgroup-data))
2321     (save-excursion
2322       (gnus-save-hidden-threads
2323         (gnus-summary-show-all-threads)
2324         (goto-char (point-min))
2325         (while data
2326           (while (get-text-property (point) 'gnus-intangible)
2327             (forward-line 1))
2328           (gnus-data-set-pos (car data) (+ (point) 3))
2329           (setq data (cdr data))
2330           (forward-line 1))))))
2331
2332 (defun gnus-hidden-threads-configuration ()
2333   "Return the current hidden threads configuration."
2334   (save-excursion
2335     (let (config)
2336       (goto-char (point-min))
2337       (while (search-forward "\r" nil t)
2338         (push (1- (point)) config))
2339       config)))
2340
2341 (defun gnus-restore-hidden-threads-configuration (config)
2342   "Restore hidden threads configuration from CONFIG."
2343   (let (point buffer-read-only)
2344     (while (setq point (pop config))
2345       (when (and (< point (point-max))
2346                  (goto-char point)
2347                  (eq (char-after) ?\n))
2348         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2349
2350 ;; Various summary mode internalish functions.
2351
2352 (defun gnus-mouse-pick-article (e)
2353   (interactive "e")
2354   (mouse-set-point e)
2355   (gnus-summary-next-page nil t))
2356
2357 (defun gnus-summary-set-display-table ()
2358   ;; Change the display table.  Odd characters have a tendency to mess
2359   ;; up nicely formatted displays - we make all possible glyphs
2360   ;; display only a single character.
2361
2362   ;; We start from the standard display table, if any.
2363   (let ((table (or (copy-sequence standard-display-table)
2364                    (make-display-table)))
2365         (i 32))
2366     ;; Nix out all the control chars...
2367     (while (>= (setq i (1- i)) 0)
2368       (aset table i [??]))
2369     ;; ... but not newline and cr, of course.  (cr is necessary for the
2370     ;; selective display).
2371     (aset table ?\n nil)
2372     (aset table ?\r nil)
2373     ;; We keep TAB as well.
2374     (aset table ?\t nil)
2375     ;; We nix out any glyphs over 126 that are not set already.
2376     (let ((i 256))
2377       (while (>= (setq i (1- i)) 127)
2378         ;; Only modify if the entry is nil.
2379         (unless (aref table i)
2380           (aset table i [??]))))
2381     (setq buffer-display-table table)))
2382
2383 (defun gnus-summary-setup-buffer (group)
2384   "Initialize summary buffer."
2385   (let ((buffer (concat "*Summary " group "*")))
2386     (if (get-buffer buffer)
2387         (progn
2388           (set-buffer buffer)
2389           (setq gnus-summary-buffer (current-buffer))
2390           (not gnus-newsgroup-prepared))
2391       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2392       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2393       (gnus-summary-mode group)
2394       (when gnus-carpal
2395         (gnus-carpal-setup-buffer 'summary))
2396       (unless gnus-single-article-buffer
2397         (make-local-variable 'gnus-article-buffer)
2398         (make-local-variable 'gnus-article-current)
2399         (make-local-variable 'gnus-original-article-buffer))
2400       (setq gnus-newsgroup-name group)
2401       t)))
2402
2403 (defun gnus-set-global-variables ()
2404   ;; Set the global equivalents of the summary buffer-local variables
2405   ;; to the latest values they had.  These reflect the summary buffer
2406   ;; that was in action when the last article was fetched.
2407   (when (eq major-mode 'gnus-summary-mode)
2408     (setq gnus-summary-buffer (current-buffer))
2409     (let ((name gnus-newsgroup-name)
2410           (marked gnus-newsgroup-marked)
2411           (unread gnus-newsgroup-unreads)
2412           (headers gnus-current-headers)
2413           (data gnus-newsgroup-data)
2414           (summary gnus-summary-buffer)
2415           (article-buffer gnus-article-buffer)
2416           (original gnus-original-article-buffer)
2417           (gac gnus-article-current)
2418           (reffed gnus-reffed-article-number)
2419           (score-file gnus-current-score-file)
2420           (default-charset gnus-newsgroup-charset))
2421       (save-excursion
2422         (set-buffer gnus-group-buffer)
2423         (setq gnus-newsgroup-name name
2424               gnus-newsgroup-marked marked
2425               gnus-newsgroup-unreads unread
2426               gnus-current-headers headers
2427               gnus-newsgroup-data data
2428               gnus-article-current gac
2429               gnus-summary-buffer summary
2430               gnus-article-buffer article-buffer
2431               gnus-original-article-buffer original
2432               gnus-reffed-article-number reffed
2433               gnus-current-score-file score-file
2434               gnus-newsgroup-charset default-charset)
2435         ;; The article buffer also has local variables.
2436         (when (gnus-buffer-live-p gnus-article-buffer)
2437           (set-buffer gnus-article-buffer)
2438           (setq gnus-summary-buffer summary))))))
2439
2440 (defun gnus-summary-article-unread-p (article)
2441   "Say whether ARTICLE is unread or not."
2442   (memq article gnus-newsgroup-unreads))
2443
2444 (defun gnus-summary-first-article-p (&optional article)
2445   "Return whether ARTICLE is the first article in the buffer."
2446   (if (not (setq article (or article (gnus-summary-article-number))))
2447       nil
2448     (eq article (caar gnus-newsgroup-data))))
2449
2450 (defun gnus-summary-last-article-p (&optional article)
2451   "Return whether ARTICLE is the last article in the buffer."
2452   (if (not (setq article (or article (gnus-summary-article-number))))
2453       ;; All non-existent numbers are the last article.  :-)
2454       t
2455     (not (cdr (gnus-data-find-list article)))))
2456
2457 (defun gnus-make-thread-indent-array ()
2458   (let ((n 200))
2459     (unless (and gnus-thread-indent-array
2460                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2461       (setq gnus-thread-indent-array (make-vector 201 "")
2462             gnus-thread-indent-array-level gnus-thread-indent-level)
2463       (while (>= n 0)
2464         (aset gnus-thread-indent-array n
2465               (make-string (* n gnus-thread-indent-level) ? ))
2466         (setq n (1- n))))))
2467
2468 (defun gnus-update-summary-mark-positions ()
2469   "Compute where the summary marks are to go."
2470   (save-excursion
2471     (when (gnus-buffer-exists-p gnus-summary-buffer)
2472       (set-buffer gnus-summary-buffer))
2473     (let ((gnus-replied-mark 129)
2474           (gnus-score-below-mark 130)
2475           (gnus-score-over-mark 130)
2476           (gnus-download-mark 131)
2477           (spec gnus-summary-line-format-spec)
2478           gnus-visual pos)
2479       (save-excursion
2480         (gnus-set-work-buffer)
2481         (let ((gnus-summary-line-format-spec spec)
2482               (gnus-newsgroup-downloadable '((0 . t))))
2483           (gnus-summary-insert-line
2484            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2485           (goto-char (point-min))
2486           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2487                                              (- (point) 2)))))
2488           (goto-char (point-min))
2489           (push (cons 'replied (and (search-forward "\201" nil t)
2490                                     (- (point) 2)))
2491                 pos)
2492           (goto-char (point-min))
2493           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2494                 pos)
2495           (goto-char (point-min))
2496           (push (cons 'download
2497                       (and (search-forward "\203" nil t) (- (point) 2)))
2498                 pos)))
2499       (setq gnus-summary-mark-positions pos))))
2500
2501 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2502   "Insert a dummy root in the summary buffer."
2503   (beginning-of-line)
2504   (gnus-add-text-properties
2505    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2506    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2507
2508 (defun gnus-summary-from-or-to-or-newsgroups (header)
2509   (let ((to (cdr (assq 'To (mail-header-extra header))))
2510         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2511         (mail-parse-charset gnus-newsgroup-charset))
2512     (cond
2513      ((and to
2514            gnus-ignored-from-addresses
2515            (string-match gnus-ignored-from-addresses
2516                          (mail-header-from header)))
2517       (concat "-> "
2518               (or (car (funcall gnus-extract-address-components
2519                                 (funcall
2520                                  gnus-decode-encoded-word-function to)))
2521                   (funcall gnus-decode-encoded-word-function to))))
2522      ((and newsgroups
2523            gnus-ignored-from-addresses
2524            (string-match gnus-ignored-from-addresses
2525                          (mail-header-from header)))
2526       (concat "=> " newsgroups))
2527      (t
2528       (or (car (funcall gnus-extract-address-components
2529                         (mail-header-from header)))
2530           (mail-header-from header))))))
2531
2532 (defun gnus-summary-insert-line (gnus-tmp-header
2533                                  gnus-tmp-level gnus-tmp-current
2534                                  gnus-tmp-unread gnus-tmp-replied
2535                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2536                                  &optional gnus-tmp-dummy gnus-tmp-score
2537                                  gnus-tmp-process)
2538   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2539          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2540          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2541          (gnus-tmp-score-char
2542           (if (or (null gnus-summary-default-score)
2543                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2544                       gnus-summary-zcore-fuzz))
2545               ? ;Whitespace
2546             (if (< gnus-tmp-score gnus-summary-default-score)
2547                 gnus-score-below-mark gnus-score-over-mark)))
2548          (gnus-tmp-replied
2549           (cond (gnus-tmp-process gnus-process-mark)
2550                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2551                  gnus-cached-mark)
2552                 (gnus-tmp-replied gnus-replied-mark)
2553                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2554                  gnus-saved-mark)
2555                 (t gnus-unread-mark)))
2556          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2557          (gnus-tmp-name
2558           (cond
2559            ((string-match "<[^>]+> *$" gnus-tmp-from)
2560             (let ((beg (match-beginning 0)))
2561               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2562                        (substring gnus-tmp-from (1+ (match-beginning 0))
2563                                   (1- (match-end 0))))
2564                   (substring gnus-tmp-from 0 beg))))
2565            ((string-match "(.+)" gnus-tmp-from)
2566             (substring gnus-tmp-from
2567                        (1+ (match-beginning 0)) (1- (match-end 0))))
2568            (t gnus-tmp-from)))
2569          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2570          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2571          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2572          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2573          (buffer-read-only nil))
2574     (when (string= gnus-tmp-name "")
2575       (setq gnus-tmp-name gnus-tmp-from))
2576     (unless (numberp gnus-tmp-lines)
2577       (setq gnus-tmp-lines 0))
2578     (gnus-put-text-property
2579      (point)
2580      (progn (eval gnus-summary-line-format-spec) (point))
2581      'gnus-number gnus-tmp-number)
2582     (when (gnus-visual-p 'summary-highlight 'highlight)
2583       (forward-line -1)
2584       (gnus-run-hooks 'gnus-summary-update-hook)
2585       (forward-line 1))))
2586
2587 (defun gnus-summary-update-line (&optional dont-update)
2588   ;; Update summary line after change.
2589   (when (and gnus-summary-default-score
2590              (not gnus-summary-inhibit-highlight))
2591     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2592            (article (gnus-summary-article-number))
2593            (score (gnus-summary-article-score article)))
2594       (unless dont-update
2595         (if (and gnus-summary-mark-below
2596                  (< (gnus-summary-article-score)
2597                     gnus-summary-mark-below))
2598             ;; This article has a low score, so we mark it as read.
2599             (when (memq article gnus-newsgroup-unreads)
2600               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2601           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2602             ;; This article was previously marked as read on account
2603             ;; of a low score, but now it has risen, so we mark it as
2604             ;; unread.
2605             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2606         (gnus-summary-update-mark
2607          (if (or (null gnus-summary-default-score)
2608                  (<= (abs (- score gnus-summary-default-score))
2609                      gnus-summary-zcore-fuzz))
2610              ? ;Whitespace
2611            (if (< score gnus-summary-default-score)
2612                gnus-score-below-mark gnus-score-over-mark))
2613          'score))
2614       ;; Do visual highlighting.
2615       (when (gnus-visual-p 'summary-highlight 'highlight)
2616         (gnus-run-hooks 'gnus-summary-update-hook)))))
2617
2618 (defvar gnus-tmp-new-adopts nil)
2619
2620 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2621   "Return the number of articles in THREAD.
2622 This may be 0 in some cases -- if none of the articles in
2623 the thread are to be displayed."
2624   (let* ((number
2625           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2626           (cond
2627            ((not (listp thread))
2628             1)
2629            ((and (consp thread) (cdr thread))
2630             (apply
2631              '+ 1 (mapcar
2632                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2633            ((null thread)
2634             1)
2635            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2636             1)
2637            (t 0))))
2638     (when (and level (zerop level) gnus-tmp-new-adopts)
2639       (incf number
2640             (apply '+ (mapcar
2641                        'gnus-summary-number-of-articles-in-thread
2642                        gnus-tmp-new-adopts))))
2643     (if char
2644         (if (> number 1) gnus-not-empty-thread-mark
2645           gnus-empty-thread-mark)
2646       number)))
2647
2648 (defun gnus-summary-set-local-parameters (group)
2649   "Go through the local params of GROUP and set all variable specs in that list."
2650   (let ((params (gnus-group-find-parameter group))
2651         elem)
2652     (while params
2653       (setq elem (car params)
2654             params (cdr params))
2655       (and (consp elem)                 ; Has to be a cons.
2656            (consp (cdr elem))           ; The cdr has to be a list.
2657            (symbolp (car elem))         ; Has to be a symbol in there.
2658            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2659            (ignore-errors               ; So we set it.
2660              (make-local-variable (car elem))
2661              (set (car elem) (eval (nth 1 elem))))))))
2662
2663 (defun gnus-summary-read-group (group &optional show-all no-article
2664                                       kill-buffer no-display backward
2665                                       select-articles)
2666   "Start reading news in newsgroup GROUP.
2667 If SHOW-ALL is non-nil, already read articles are also listed.
2668 If NO-ARTICLE is non-nil, no article is selected initially.
2669 If NO-DISPLAY, don't generate a summary buffer."
2670   (let (result)
2671     (while (and group
2672                 (null (setq result
2673                             (let ((gnus-auto-select-next nil))
2674                               (or (gnus-summary-read-group-1
2675                                    group show-all no-article
2676                                    kill-buffer no-display
2677                                    select-articles)
2678                                   (setq show-all nil
2679                                         select-articles nil)))))
2680                 (eq gnus-auto-select-next 'quietly))
2681       (set-buffer gnus-group-buffer)
2682       ;; The entry function called above goes to the next
2683       ;; group automatically, so we go two groups back
2684       ;; if we are searching for the previous group.
2685       (when backward
2686         (gnus-group-prev-unread-group 2))
2687       (if (not (equal group (gnus-group-group-name)))
2688           (setq group (gnus-group-group-name))
2689         (setq group nil)))
2690     result))
2691
2692 (defun gnus-summary-read-group-1 (group show-all no-article
2693                                         kill-buffer no-display
2694                                         &optional select-articles)
2695   ;; Killed foreign groups can't be entered.
2696   (when (and (not (gnus-group-native-p group))
2697              (not (gnus-gethash group gnus-newsrc-hashtb)))
2698     (error "Dead non-native groups can't be entered"))
2699   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2700   (let* ((new-group (gnus-summary-setup-buffer group))
2701          (quit-config (gnus-group-quit-config group))
2702          (did-select (and new-group (gnus-select-newsgroup
2703                                      group show-all select-articles))))
2704     (cond
2705      ;; This summary buffer exists already, so we just select it.
2706      ((not new-group)
2707       (gnus-set-global-variables)
2708       (when kill-buffer
2709         (gnus-kill-or-deaden-summary kill-buffer))
2710       (gnus-configure-windows 'summary 'force)
2711       (gnus-set-mode-line 'summary)
2712       (gnus-summary-position-point)
2713       (message "")
2714       t)
2715      ;; We couldn't select this group.
2716      ((null did-select)
2717       (when (and (eq major-mode 'gnus-summary-mode)
2718                  (not (equal (current-buffer) kill-buffer)))
2719         (kill-buffer (current-buffer))
2720         (if (not quit-config)
2721             (progn
2722               ;; Update the info -- marks might need to be removed,
2723               ;; for instance.
2724               (gnus-summary-update-info)
2725               (set-buffer gnus-group-buffer)
2726               (gnus-group-jump-to-group group)
2727               (gnus-group-next-unread-group 1))
2728           (gnus-handle-ephemeral-exit quit-config)))
2729       (gnus-message 3 "Can't select group")
2730       nil)
2731      ;; The user did a `C-g' while prompting for number of articles,
2732      ;; so we exit this group.
2733      ((eq did-select 'quit)
2734       (and (eq major-mode 'gnus-summary-mode)
2735            (not (equal (current-buffer) kill-buffer))
2736            (kill-buffer (current-buffer)))
2737       (when kill-buffer
2738         (gnus-kill-or-deaden-summary kill-buffer))
2739       (if (not quit-config)
2740           (progn
2741             (set-buffer gnus-group-buffer)
2742             (gnus-group-jump-to-group group)
2743             (gnus-group-next-unread-group 1)
2744             (gnus-configure-windows 'group 'force))
2745         (gnus-handle-ephemeral-exit quit-config))
2746       ;; Finally signal the quit.
2747       (signal 'quit nil))
2748      ;; The group was successfully selected.
2749      (t
2750       (gnus-set-global-variables)
2751       ;; Save the active value in effect when the group was entered.
2752       (setq gnus-newsgroup-active
2753             (gnus-copy-sequence
2754              (gnus-active gnus-newsgroup-name)))
2755       ;; You can change the summary buffer in some way with this hook.
2756       (gnus-run-hooks 'gnus-select-group-hook)
2757       ;; Set any local variables in the group parameters.
2758       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2759       (gnus-update-format-specifications
2760        nil 'summary 'summary-mode 'summary-dummy)
2761       ;; Do score processing.
2762       (when gnus-use-scoring
2763         (gnus-possibly-score-headers))
2764       ;; Check whether to fill in the gaps in the threads.
2765       (when gnus-build-sparse-threads
2766         (gnus-build-sparse-threads))
2767       ;; Find the initial limit.
2768       (if gnus-show-threads
2769           (if show-all
2770               (let ((gnus-newsgroup-dormant nil))
2771                 (gnus-summary-initial-limit show-all))
2772             (gnus-summary-initial-limit show-all))
2773         (setq gnus-newsgroup-limit
2774               (mapcar
2775                (lambda (header) (mail-header-number header))
2776                gnus-newsgroup-headers)))
2777       ;; Generate the summary buffer.
2778       (unless no-display
2779         (gnus-summary-prepare))
2780       (when gnus-use-trees
2781         (gnus-tree-open group)
2782         (setq gnus-summary-highlight-line-function
2783               'gnus-tree-highlight-article))
2784       ;; If the summary buffer is empty, but there are some low-scored
2785       ;; articles or some excluded dormants, we include these in the
2786       ;; buffer.
2787       (when (and (zerop (buffer-size))
2788                  (not no-display))
2789         (cond (gnus-newsgroup-dormant
2790                (gnus-summary-limit-include-dormant))
2791               ((and gnus-newsgroup-scored show-all)
2792                (gnus-summary-limit-include-expunged t))))
2793       ;; Function `gnus-apply-kill-file' must be called in this hook.
2794       (gnus-run-hooks 'gnus-apply-kill-hook)
2795       (if (and (zerop (buffer-size))
2796                (not no-display))
2797           (progn
2798             ;; This newsgroup is empty.
2799             (gnus-summary-catchup-and-exit nil t)
2800             (gnus-message 6 "No unread news")
2801             (when kill-buffer
2802               (gnus-kill-or-deaden-summary kill-buffer))
2803             ;; Return nil from this function.
2804             nil)
2805         ;; Hide conversation thread subtrees.  We cannot do this in
2806         ;; gnus-summary-prepare-hook since kill processing may not
2807         ;; work with hidden articles.
2808         (and gnus-show-threads
2809              gnus-thread-hide-subtree
2810              (gnus-summary-hide-all-threads))
2811         (when kill-buffer
2812           (gnus-kill-or-deaden-summary kill-buffer))
2813         ;; Show first unread article if requested.
2814         (if (and (not no-article)
2815                  (not no-display)
2816                  gnus-newsgroup-unreads
2817                  gnus-auto-select-first)
2818             (progn
2819               (gnus-configure-windows 'summary)
2820               (cond
2821                ((eq gnus-auto-select-first 'best)
2822                 (gnus-summary-best-unread-article))
2823                ((eq gnus-auto-select-first t)
2824                 (gnus-summary-first-unread-article))
2825                ((gnus-functionp gnus-auto-select-first)
2826                 (funcall gnus-auto-select-first))))
2827           ;; Don't select any articles, just move point to the first
2828           ;; article in the group.
2829           (goto-char (point-min))
2830           (gnus-summary-position-point)
2831           (gnus-configure-windows 'summary 'force)
2832           (gnus-set-mode-line 'summary))
2833         (when (get-buffer-window gnus-group-buffer t)
2834           ;; Gotta use windows, because recenter does weird stuff if
2835           ;; the current buffer ain't the displayed window.
2836           (let ((owin (selected-window)))
2837             (select-window (get-buffer-window gnus-group-buffer t))
2838             (when (gnus-group-goto-group group)
2839               (recenter))
2840             (select-window owin)))
2841         ;; Mark this buffer as "prepared".
2842         (setq gnus-newsgroup-prepared t)
2843         (gnus-run-hooks 'gnus-summary-prepared-hook)
2844         t)))))
2845
2846 (defun gnus-summary-prepare ()
2847   "Generate the summary buffer."
2848   (interactive)
2849   (let ((buffer-read-only nil))
2850     (erase-buffer)
2851     (setq gnus-newsgroup-data nil
2852           gnus-newsgroup-data-reverse nil)
2853     (gnus-run-hooks 'gnus-summary-generate-hook)
2854     ;; Generate the buffer, either with threads or without.
2855     (when gnus-newsgroup-headers
2856       (gnus-summary-prepare-threads
2857        (if gnus-show-threads
2858            (gnus-sort-gathered-threads
2859             (funcall gnus-summary-thread-gathering-function
2860                      (gnus-sort-threads
2861                       (gnus-cut-threads (gnus-make-threads)))))
2862          ;; Unthreaded display.
2863          (gnus-sort-articles gnus-newsgroup-headers))))
2864     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2865     ;; Call hooks for modifying summary buffer.
2866     (goto-char (point-min))
2867     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2868
2869 (defsubst gnus-general-simplify-subject (subject)
2870   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2871   (setq subject
2872         (cond
2873          ;; Truncate the subject.
2874          (gnus-simplify-subject-functions
2875           (gnus-map-function gnus-simplify-subject-functions subject))
2876          ((numberp gnus-summary-gather-subject-limit)
2877           (setq subject (gnus-simplify-subject-re subject))
2878           (if (> (length subject) gnus-summary-gather-subject-limit)
2879               (substring subject 0 gnus-summary-gather-subject-limit)
2880             subject))
2881          ;; Fuzzily simplify it.
2882          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2883           (gnus-simplify-subject-fuzzy subject))
2884          ;; Just remove the leading "Re:".
2885          (t
2886           (gnus-simplify-subject-re subject))))
2887
2888   (if (and gnus-summary-gather-exclude-subject
2889            (string-match gnus-summary-gather-exclude-subject subject))
2890       nil                               ; This article shouldn't be gathered
2891     subject))
2892
2893 (defun gnus-summary-simplify-subject-query ()
2894   "Query where the respool algorithm would put this article."
2895   (interactive)
2896   (gnus-summary-select-article)
2897   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2898
2899 (defun gnus-gather-threads-by-subject (threads)
2900   "Gather threads by looking at Subject headers."
2901   (if (not gnus-summary-make-false-root)
2902       threads
2903     (let ((hashtb (gnus-make-hashtable 1024))
2904           (prev threads)
2905           (result threads)
2906           subject hthread whole-subject)
2907       (while threads
2908         (setq subject (gnus-general-simplify-subject
2909                        (setq whole-subject (mail-header-subject
2910                                             (caar threads)))))
2911         (when subject
2912           (if (setq hthread (gnus-gethash subject hashtb))
2913               (progn
2914                 ;; We enter a dummy root into the thread, if we
2915                 ;; haven't done that already.
2916                 (unless (stringp (caar hthread))
2917                   (setcar hthread (list whole-subject (car hthread))))
2918                 ;; We add this new gathered thread to this gathered
2919                 ;; thread.
2920                 (setcdr (car hthread)
2921                         (nconc (cdar hthread) (list (car threads))))
2922                 ;; Remove it from the list of threads.
2923                 (setcdr prev (cdr threads))
2924                 (setq threads prev))
2925             ;; Enter this thread into the hash table.
2926             (gnus-sethash subject threads hashtb)))
2927         (setq prev threads)
2928         (setq threads (cdr threads)))
2929       result)))
2930
2931 (defun gnus-gather-threads-by-references (threads)
2932   "Gather threads by looking at References headers."
2933   (let ((idhashtb (gnus-make-hashtable 1024))
2934         (thhashtb (gnus-make-hashtable 1024))
2935         (prev threads)
2936         (result threads)
2937         ids references id gthread gid entered ref)
2938     (while threads
2939       (when (setq references (mail-header-references (caar threads)))
2940         (setq id (mail-header-id (caar threads))
2941               ids (gnus-split-references references)
2942               entered nil)
2943         (while (setq ref (pop ids))
2944           (setq ids (delete ref ids))
2945           (if (not (setq gid (gnus-gethash ref idhashtb)))
2946               (progn
2947                 (gnus-sethash ref id idhashtb)
2948                 (gnus-sethash id threads thhashtb))
2949             (setq gthread (gnus-gethash gid thhashtb))
2950             (unless entered
2951               ;; We enter a dummy root into the thread, if we
2952               ;; haven't done that already.
2953               (unless (stringp (caar gthread))
2954                 (setcar gthread (list (mail-header-subject (caar gthread))
2955                                       (car gthread))))
2956               ;; We add this new gathered thread to this gathered
2957               ;; thread.
2958               (setcdr (car gthread)
2959                       (nconc (cdar gthread) (list (car threads)))))
2960             ;; Add it into the thread hash table.
2961             (gnus-sethash id gthread thhashtb)
2962             (setq entered t)
2963             ;; Remove it from the list of threads.
2964             (setcdr prev (cdr threads))
2965             (setq threads prev))))
2966       (setq prev threads)
2967       (setq threads (cdr threads)))
2968     result))
2969
2970 (defun gnus-sort-gathered-threads (threads)
2971   "Sort subtreads inside each gathered thread by article number."
2972   (let ((result threads))
2973     (while threads
2974       (when (stringp (caar threads))
2975         (setcdr (car threads)
2976                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2977       (setq threads (cdr threads)))
2978     result))
2979
2980 (defun gnus-thread-loop-p (root thread)
2981   "Say whether ROOT is in THREAD."
2982   (let ((stack (list thread))
2983         (infloop 0)
2984         th)
2985     (while (setq thread (pop stack))
2986       (setq th (cdr thread))
2987       (while (and th
2988                   (not (eq (caar th) root)))
2989         (pop th))
2990       (if th
2991           ;; We have found a loop.
2992           (let (ref-dep)
2993             (setcdr thread (delq (car th) (cdr thread)))
2994             (if (boundp (setq ref-dep (intern "none"
2995                                               gnus-newsgroup-dependencies)))
2996                 (setcdr (symbol-value ref-dep)
2997                         (nconc (cdr (symbol-value ref-dep))
2998                                (list (car th))))
2999               (set ref-dep (list nil (car th))))
3000             (setq infloop 1
3001                   stack nil))
3002         ;; Push all the subthreads onto the stack.
3003         (push (cdr thread) stack)))
3004     infloop))
3005
3006 (defun gnus-make-threads ()
3007   "Go through the dependency hashtb and find the roots.  Return all threads."
3008   (let (threads)
3009     (while (catch 'infloop
3010              (mapatoms
3011               (lambda (refs)
3012                 ;; Deal with self-referencing References loops.
3013                 (when (and (car (symbol-value refs))
3014                            (not (zerop
3015                                  (apply
3016                                   '+
3017                                   (mapcar
3018                                    (lambda (thread)
3019                                      (gnus-thread-loop-p
3020                                       (car (symbol-value refs)) thread))
3021                                    (cdr (symbol-value refs)))))))
3022                   (setq threads nil)
3023                   (throw 'infloop t))
3024                 (unless (car (symbol-value refs))
3025                   ;; These threads do not refer back to any other articles,
3026                   ;; so they're roots.
3027                   (setq threads (append (cdr (symbol-value refs)) threads))))
3028               gnus-newsgroup-dependencies)))
3029     threads))
3030
3031 ;; Build the thread tree.
3032 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3033   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3034
3035 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3036 if it was already present.
3037
3038 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3039 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3040 Message-IDs will be renamed be renamed to a unique Message-ID before
3041 being entered.
3042
3043 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3044   (let* ((id (mail-header-id header))
3045          (id-dep (and id (intern id dependencies)))
3046          ref ref-dep ref-header)
3047     ;; Enter this `header' in the `dependencies' table.
3048     (cond
3049      ((not id-dep)
3050       (setq header nil))
3051      ;; The first two cases do the normal part: enter a new `header'
3052      ;; in the `dependencies' table.
3053      ((not (boundp id-dep))
3054       (set id-dep (list header)))
3055      ((null (car (symbol-value id-dep)))
3056       (setcar (symbol-value id-dep) header))
3057
3058      ;; From here the `header' was already present in the
3059      ;; `dependencies' table.
3060      (force-new
3061       ;; Overrides an existing entry;
3062       ;; just set the header part of the entry.
3063       (setcar (symbol-value id-dep) header))
3064
3065      ;; Renames the existing `header' to a unique Message-ID.
3066      ((not gnus-summary-ignore-duplicates)
3067       ;; An article with this Message-ID has already been seen.
3068       ;; We rename the Message-ID.
3069       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3070            (list header))
3071       (mail-header-set-id header id))
3072
3073      ;; The last case ignores an existing entry, except it adds any
3074      ;; additional Xrefs (in case the two articles came from different
3075      ;; servers.
3076      ;; Also sets `header' to `nil' meaning that the `dependencies'
3077      ;; table was *not* modified.
3078      (t
3079       (mail-header-set-xref
3080        (car (symbol-value id-dep))
3081        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3082                    "")
3083                (or (mail-header-xref header) "")))
3084       (setq header nil)))
3085
3086     (when header
3087       ;; First check if that we are not creating a References loop.
3088       (setq ref (gnus-parent-id (mail-header-references header)))
3089       (while (and ref
3090                   (setq ref-dep (intern-soft ref dependencies))
3091                   (boundp ref-dep)
3092                   (setq ref-header (car (symbol-value ref-dep))))
3093         (if (string= id ref)
3094             ;; Yuk!  This is a reference loop.  Make the article be a
3095             ;; root article.
3096             (progn
3097               (mail-header-set-references (car (symbol-value id-dep)) "none")
3098               (setq ref nil))
3099           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3100       (setq ref (gnus-parent-id (mail-header-references header)))
3101       (setq ref-dep (intern (or ref "none") dependencies))
3102       (if (boundp ref-dep)
3103           (setcdr (symbol-value ref-dep)
3104                   (nconc (cdr (symbol-value ref-dep))
3105                          (list (symbol-value id-dep))))
3106         (set ref-dep (list nil (symbol-value id-dep)))))
3107     header))
3108
3109 (defun gnus-build-sparse-threads ()
3110   (let ((headers gnus-newsgroup-headers)
3111         (gnus-summary-ignore-duplicates t)
3112         header references generation relations
3113         subject child end new-child date)
3114     ;; First we create an alist of generations/relations, where
3115     ;; generations is how much we trust the relation, and the relation
3116     ;; is parent/child.
3117     (gnus-message 7 "Making sparse threads...")
3118     (save-excursion
3119       (nnheader-set-temp-buffer " *gnus sparse threads*")
3120       (while (setq header (pop headers))
3121         (when (and (setq references (mail-header-references header))
3122                    (not (string= references "")))
3123           (insert references)
3124           (setq child (mail-header-id header)
3125                 subject (mail-header-subject header)
3126                 date (mail-header-date header)
3127                 generation 0)
3128           (while (search-backward ">" nil t)
3129             (setq end (1+ (point)))
3130             (when (search-backward "<" nil t)
3131               (setq new-child (buffer-substring (point) end))
3132               (push (list (incf generation)
3133                           child (setq child new-child)
3134                           subject date)
3135                     relations)))
3136           (when child
3137             (push (list (1+ generation) child nil subject) relations))
3138           (erase-buffer)))
3139       (kill-buffer (current-buffer)))
3140     ;; Sort over trustworthiness.
3141     (mapcar
3142      (lambda (relation)
3143        (when (gnus-dependencies-add-header
3144               (make-full-mail-header
3145                gnus-reffed-article-number
3146                (nth 3 relation) "" (or (nth 4 relation) "")
3147                (nth 1 relation)
3148                (or (nth 2 relation) "") 0 0 "")
3149               gnus-newsgroup-dependencies nil)
3150          (push gnus-reffed-article-number gnus-newsgroup-limit)
3151          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3152          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3153                gnus-newsgroup-reads)
3154          (decf gnus-reffed-article-number)))
3155      (sort relations 'car-less-than-car))
3156     (gnus-message 7 "Making sparse threads...done")))
3157
3158 (defun gnus-build-old-threads ()
3159   ;; Look at all the articles that refer back to old articles, and
3160   ;; fetch the headers for the articles that aren't there.  This will
3161   ;; build complete threads - if the roots haven't been expired by the
3162   ;; server, that is.
3163   (let (id heads)
3164     (mapatoms
3165      (lambda (refs)
3166        (when (not (car (symbol-value refs)))
3167          (setq heads (cdr (symbol-value refs)))
3168          (while heads
3169            (if (memq (mail-header-number (caar heads))
3170                      gnus-newsgroup-dormant)
3171                (setq heads (cdr heads))
3172              (setq id (symbol-name refs))
3173              (while (and (setq id (gnus-build-get-header id))
3174                          (not (car (gnus-id-to-thread id)))))
3175              (setq heads nil)))))
3176      gnus-newsgroup-dependencies)))
3177
3178 ;; This function has to be called with point after the article number
3179 ;; on the beginning of the line.
3180 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3181   (let ((eol (gnus-point-at-eol))
3182         (buffer (current-buffer))
3183         header)
3184
3185     ;; overview: [num subject from date id refs chars lines misc]
3186     (unwind-protect
3187         (progn
3188           (narrow-to-region (point) eol)
3189           (unless (eobp)
3190             (forward-char))
3191
3192           (setq header
3193                 (make-full-mail-header
3194                  number                         ; number
3195                  (funcall gnus-decode-encoded-word-function
3196                           (nnheader-nov-field)) ; subject
3197                  (funcall gnus-decode-encoded-word-function
3198                           (nnheader-nov-field)) ; from
3199                  (nnheader-nov-field)           ; date
3200                  (nnheader-nov-read-message-id) ; id
3201                  (nnheader-nov-field)           ; refs
3202                  (nnheader-nov-read-integer)    ; chars
3203                  (nnheader-nov-read-integer)    ; lines
3204                  (unless (eobp)
3205                    (nnheader-nov-field))                ; misc
3206                  (nnheader-nov-parse-extra))))  ; extra
3207
3208       (widen))
3209
3210     (when gnus-alter-header-function
3211       (funcall gnus-alter-header-function header))
3212     (gnus-dependencies-add-header header dependencies force-new)))
3213
3214 (defun gnus-build-get-header (id)
3215   ;; Look through the buffer of NOV lines and find the header to
3216   ;; ID.  Enter this line into the dependencies hash table, and return
3217   ;; the id of the parent article (if any).
3218   (let ((deps gnus-newsgroup-dependencies)
3219         found header)
3220     (prog1
3221         (save-excursion
3222           (set-buffer nntp-server-buffer)
3223           (let ((case-fold-search nil))
3224             (goto-char (point-min))
3225             (while (and (not found)
3226                         (search-forward id nil t))
3227               (beginning-of-line)
3228               (setq found (looking-at
3229                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3230                                    (regexp-quote id))))
3231               (or found (beginning-of-line 2)))
3232             (when found
3233               (beginning-of-line)
3234               (and
3235                (setq header (gnus-nov-parse-line
3236                              (read (current-buffer)) deps))
3237                (gnus-parent-id (mail-header-references header))))))
3238       (when header
3239         (let ((number (mail-header-number header)))
3240           (push number gnus-newsgroup-limit)
3241           (push header gnus-newsgroup-headers)
3242           (if (memq number gnus-newsgroup-unselected)
3243               (progn
3244                 (push number gnus-newsgroup-unreads)
3245                 (setq gnus-newsgroup-unselected
3246                       (delq number gnus-newsgroup-unselected)))
3247             (push number gnus-newsgroup-ancient)))))))
3248
3249 (defun gnus-build-all-threads ()
3250   "Read all the headers."
3251   (let ((gnus-summary-ignore-duplicates t)
3252         (dependencies gnus-newsgroup-dependencies)
3253         header article)
3254     (save-excursion
3255       (set-buffer nntp-server-buffer)
3256       (let ((case-fold-search nil))
3257         (goto-char (point-min))
3258         (while (not (eobp))
3259           (ignore-errors
3260             (setq article (read (current-buffer))
3261                   header (gnus-nov-parse-line
3262                           article dependencies)))
3263           (when header
3264             (save-excursion
3265               (set-buffer gnus-summary-buffer)
3266               (push header gnus-newsgroup-headers)
3267               (if (memq (setq article (mail-header-number header))
3268                         gnus-newsgroup-unselected)
3269                   (progn
3270                     (push article gnus-newsgroup-unreads)
3271                     (setq gnus-newsgroup-unselected
3272                           (delq article gnus-newsgroup-unselected)))
3273                 (push article gnus-newsgroup-ancient)))
3274             (forward-line 1)))))))
3275
3276 (defun gnus-summary-update-article-line (article header)
3277   "Update the line for ARTICLE using HEADERS."
3278   (let* ((id (mail-header-id header))
3279          (thread (gnus-id-to-thread id)))
3280     (unless thread
3281       (error "Article in no thread"))
3282     ;; Update the thread.
3283     (setcar thread header)
3284     (gnus-summary-goto-subject article)
3285     (let* ((datal (gnus-data-find-list article))
3286            (data (car datal))
3287            (length (when (cdr datal)
3288                      (- (gnus-data-pos data)
3289                         (gnus-data-pos (cadr datal)))))
3290            (buffer-read-only nil)
3291            (level (gnus-summary-thread-level)))
3292       (gnus-delete-line)
3293       (gnus-summary-insert-line
3294        header level nil (gnus-article-mark article)
3295        (memq article gnus-newsgroup-replied)
3296        (memq article gnus-newsgroup-expirable)
3297        ;; Only insert the Subject string when it's different
3298        ;; from the previous Subject string.
3299        (if (gnus-subject-equal
3300             (condition-case ()
3301                 (mail-header-subject
3302                  (gnus-data-header
3303                   (cadr
3304                    (gnus-data-find-list
3305                     article
3306                     (gnus-data-list t)))))
3307               ;; Error on the side of excessive subjects.
3308               (error ""))
3309             (mail-header-subject header))
3310            ""
3311          (mail-header-subject header))
3312        nil (cdr (assq article gnus-newsgroup-scored))
3313        (memq article gnus-newsgroup-processable))
3314       (when length
3315         (gnus-data-update-list
3316          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3317
3318 (defun gnus-summary-update-article (article &optional iheader)
3319   "Update ARTICLE in the summary buffer."
3320   (set-buffer gnus-summary-buffer)
3321   (let* ((header (gnus-summary-article-header article))
3322          (id (mail-header-id header))
3323          (data (gnus-data-find article))
3324          (thread (gnus-id-to-thread id))
3325          (references (mail-header-references header))
3326          (parent
3327           (gnus-id-to-thread
3328            (or (gnus-parent-id
3329                 (when (and references
3330                            (not (equal "" references)))
3331                   references))
3332                "none")))
3333          (buffer-read-only nil)
3334          (old (car thread)))
3335     (when thread
3336       (unless iheader
3337         (setcar thread nil)
3338         (when parent
3339           (delq thread parent)))
3340       (if (gnus-summary-insert-subject id header)
3341           ;; Set the (possibly) new article number in the data structure.
3342           (gnus-data-set-number data (gnus-id-to-article id))
3343         (setcar thread old)
3344         nil))))
3345
3346 (defun gnus-rebuild-thread (id &optional line)
3347   "Rebuild the thread containing ID.
3348 If LINE, insert the rebuilt thread starting on line LINE."
3349   (let ((buffer-read-only nil)
3350         old-pos current thread data)
3351     (if (not gnus-show-threads)
3352         (setq thread (list (car (gnus-id-to-thread id))))
3353       ;; Get the thread this article is part of.
3354       (setq thread (gnus-remove-thread id)))
3355     (setq old-pos (gnus-point-at-bol))
3356     (setq current (save-excursion
3357                     (and (zerop (forward-line -1))
3358                          (gnus-summary-article-number))))
3359     ;; If this is a gathered thread, we have to go some re-gathering.
3360     (when (stringp (car thread))
3361       (let ((subject (car thread))
3362             roots thr)
3363         (setq thread (cdr thread))
3364         (while thread
3365           (unless (memq (setq thr (gnus-id-to-thread
3366                                    (gnus-root-id
3367                                     (mail-header-id (caar thread)))))
3368                         roots)
3369             (push thr roots))
3370           (setq thread (cdr thread)))
3371         ;; We now have all (unique) roots.
3372         (if (= (length roots) 1)
3373             ;; All the loose roots are now one solid root.
3374             (setq thread (car roots))
3375           (setq thread (cons subject (gnus-sort-threads roots))))))
3376     (let (threads)
3377       ;; We then insert this thread into the summary buffer.
3378       (when line
3379         (goto-char (point-min))
3380         (forward-line (1- line)))
3381       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3382         (if gnus-show-threads
3383             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3384           (gnus-summary-prepare-unthreaded thread))
3385         (setq data (nreverse gnus-newsgroup-data))
3386         (setq threads gnus-newsgroup-threads))
3387       ;; We splice the new data into the data structure.
3388       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3389       ;;!!! then we want to insert at the beginning of the buffer.
3390       ;;!!! That happens to be true with Gnus now, but that may
3391       ;;!!! change in the future.  Perhaps.
3392       (gnus-data-enter-list
3393        (if line nil current) data (- (point) old-pos))
3394       (setq gnus-newsgroup-threads
3395             (nconc threads gnus-newsgroup-threads))
3396       (gnus-data-compute-positions))))
3397
3398 (defun gnus-number-to-header (number)
3399   "Return the header for article NUMBER."
3400   (let ((headers gnus-newsgroup-headers))
3401     (while (and headers
3402                 (not (= number (mail-header-number (car headers)))))
3403       (pop headers))
3404     (when headers
3405       (car headers))))
3406
3407 (defun gnus-parent-headers (in-headers &optional generation)
3408   "Return the headers of the GENERATIONeth parent of HEADERS."
3409   (unless generation
3410     (setq generation 1))
3411   (let ((parent t)
3412         (headers in-headers)
3413         references)
3414     (while (and parent
3415                 (not (zerop generation))
3416                 (setq references (mail-header-references headers)))
3417       (setq headers (if (and references
3418                              (setq parent (gnus-parent-id references)))
3419                         (car (gnus-id-to-thread parent))
3420                       nil))
3421       (decf generation))
3422     (and (not (eq headers in-headers))
3423          headers)))
3424
3425 (defun gnus-id-to-thread (id)
3426   "Return the (sub-)thread where ID appears."
3427   (gnus-gethash id gnus-newsgroup-dependencies))
3428
3429 (defun gnus-id-to-article (id)
3430   "Return the article number of ID."
3431   (let ((thread (gnus-id-to-thread id)))
3432     (when (and thread
3433                (car thread))
3434       (mail-header-number (car thread)))))
3435
3436 (defun gnus-id-to-header (id)
3437   "Return the article headers of ID."
3438   (car (gnus-id-to-thread id)))
3439
3440 (defun gnus-article-displayed-root-p (article)
3441   "Say whether ARTICLE is a root(ish) article."
3442   (let ((level (gnus-summary-thread-level article))
3443         (refs (mail-header-references  (gnus-summary-article-header article)))
3444         particle)
3445     (cond
3446      ((null level) nil)
3447      ((zerop level) t)
3448      ((null refs) t)
3449      ((null (gnus-parent-id refs)) t)
3450      ((and (= 1 level)
3451            (null (setq particle (gnus-id-to-article
3452                                  (gnus-parent-id refs))))
3453            (null (gnus-summary-thread-level particle)))))))
3454
3455 (defun gnus-root-id (id)
3456   "Return the id of the root of the thread where ID appears."
3457   (let (last-id prev)
3458     (while (and id (setq prev (car (gnus-id-to-thread id))))
3459       (setq last-id id
3460             id (gnus-parent-id (mail-header-references prev))))
3461     last-id))
3462
3463 (defun gnus-articles-in-thread (thread)
3464   "Return the list of articles in THREAD."
3465   (cons (mail-header-number (car thread))
3466         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3467
3468 (defun gnus-remove-thread (id &optional dont-remove)
3469   "Remove the thread that has ID in it."
3470   (let (headers thread last-id)
3471     ;; First go up in this thread until we find the root.
3472     (setq last-id (gnus-root-id id)
3473           headers (message-flatten-list (gnus-id-to-thread last-id)))
3474     ;; We have now found the real root of this thread.  It might have
3475     ;; been gathered into some loose thread, so we have to search
3476     ;; through the threads to find the thread we wanted.
3477     (let ((threads gnus-newsgroup-threads)
3478           sub)
3479       (while threads
3480         (setq sub (car threads))
3481         (if (stringp (car sub))
3482             ;; This is a gathered thread, so we look at the roots
3483             ;; below it to find whether this article is in this
3484             ;; gathered root.
3485             (progn
3486               (setq sub (cdr sub))
3487               (while sub
3488                 (when (member (caar sub) headers)
3489                   (setq thread (car threads)
3490                         threads nil
3491                         sub nil))
3492                 (setq sub (cdr sub))))
3493           ;; It's an ordinary thread, so we check it.
3494           (when (eq (car sub) (car headers))
3495             (setq thread sub
3496                   threads nil)))
3497         (setq threads (cdr threads)))
3498       ;; If this article is in no thread, then it's a root.
3499       (if thread
3500           (unless dont-remove
3501             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3502         (setq thread (gnus-id-to-thread last-id)))
3503       (when thread
3504         (prog1
3505             thread                      ; We return this thread.
3506           (unless dont-remove
3507             (if (stringp (car thread))
3508                 (progn
3509                   ;; If we use dummy roots, then we have to remove the
3510                   ;; dummy root as well.
3511                   (when (eq gnus-summary-make-false-root 'dummy)
3512                     ;; We go to the dummy root by going to
3513                     ;; the first sub-"thread", and then one line up.
3514                     (gnus-summary-goto-article
3515                      (mail-header-number (caadr thread)))
3516                     (forward-line -1)
3517                     (gnus-delete-line)
3518                     (gnus-data-compute-positions))
3519                   (setq thread (cdr thread))
3520                   (while thread
3521                     (gnus-remove-thread-1 (car thread))
3522                     (setq thread (cdr thread))))
3523               (gnus-summary-show-all-threads)
3524               (gnus-remove-thread-1 thread))))))))
3525
3526 (defun gnus-remove-thread-1 (thread)
3527   "Remove the thread THREAD recursively."
3528   (let ((number (mail-header-number (pop thread)))
3529         d)
3530     (setq thread (reverse thread))
3531     (while thread
3532       (gnus-remove-thread-1 (pop thread)))
3533     (when (setq d (gnus-data-find number))
3534       (goto-char (gnus-data-pos d))
3535       (gnus-data-remove
3536        number
3537        (- (gnus-point-at-bol)
3538           (prog1
3539               (1+ (gnus-point-at-eol))
3540             (gnus-delete-line)))))))
3541
3542 (defun gnus-sort-threads (threads)
3543   "Sort THREADS."
3544   (if (not gnus-thread-sort-functions)
3545       threads
3546     (gnus-message 8 "Sorting threads...")
3547     (prog1
3548         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3549       (gnus-message 8 "Sorting threads...done"))))
3550
3551 (defun gnus-sort-articles (articles)
3552   "Sort ARTICLES."
3553   (when gnus-article-sort-functions
3554     (gnus-message 7 "Sorting articles...")
3555     (prog1
3556         (setq gnus-newsgroup-headers
3557               (sort articles (gnus-make-sort-function
3558                               gnus-article-sort-functions)))
3559       (gnus-message 7 "Sorting articles...done"))))
3560
3561 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3562 (defmacro gnus-thread-header (thread)
3563   ;; Return header of first article in THREAD.
3564   ;; Note that THREAD must never, ever be anything else than a variable -
3565   ;; using some other form will lead to serious barfage.
3566   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3567   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3568   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3569         (vector thread) 2))
3570
3571 (defsubst gnus-article-sort-by-number (h1 h2)
3572   "Sort articles by article number."
3573   (< (mail-header-number h1)
3574      (mail-header-number h2)))
3575
3576 (defun gnus-thread-sort-by-number (h1 h2)
3577   "Sort threads by root article number."
3578   (gnus-article-sort-by-number
3579    (gnus-thread-header h1) (gnus-thread-header h2)))
3580
3581 (defsubst gnus-article-sort-by-lines (h1 h2)
3582   "Sort articles by article Lines header."
3583   (< (mail-header-lines h1)
3584      (mail-header-lines h2)))
3585
3586 (defun gnus-thread-sort-by-lines (h1 h2)
3587   "Sort threads by root article Lines header."
3588   (gnus-article-sort-by-lines
3589    (gnus-thread-header h1) (gnus-thread-header h2)))
3590
3591 (defsubst gnus-article-sort-by-author (h1 h2)
3592   "Sort articles by root author."
3593   (string-lessp
3594    (let ((extract (funcall
3595                    gnus-extract-address-components
3596                    (mail-header-from h1))))
3597      (or (car extract) (cadr extract) ""))
3598    (let ((extract (funcall
3599                    gnus-extract-address-components
3600                    (mail-header-from h2))))
3601      (or (car extract) (cadr extract) ""))))
3602
3603 (defun gnus-thread-sort-by-author (h1 h2)
3604   "Sort threads by root author."
3605   (gnus-article-sort-by-author
3606    (gnus-thread-header h1)  (gnus-thread-header h2)))
3607
3608 (defsubst gnus-article-sort-by-subject (h1 h2)
3609   "Sort articles by root subject."
3610   (string-lessp
3611    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3612    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3613
3614 (defun gnus-thread-sort-by-subject (h1 h2)
3615   "Sort threads by root subject."
3616   (gnus-article-sort-by-subject
3617    (gnus-thread-header h1) (gnus-thread-header h2)))
3618
3619 (defsubst gnus-article-sort-by-date (h1 h2)
3620   "Sort articles by root article date."
3621   (time-less-p
3622    (gnus-date-get-time (mail-header-date h1))
3623    (gnus-date-get-time (mail-header-date h2))))
3624
3625 (defun gnus-thread-sort-by-date (h1 h2)
3626   "Sort threads by root article date."
3627   (gnus-article-sort-by-date
3628    (gnus-thread-header h1) (gnus-thread-header h2)))
3629
3630 (defsubst gnus-article-sort-by-score (h1 h2)
3631   "Sort articles by root article score.
3632 Unscored articles will be counted as having a score of zero."
3633   (> (or (cdr (assq (mail-header-number h1)
3634                     gnus-newsgroup-scored))
3635          gnus-summary-default-score 0)
3636      (or (cdr (assq (mail-header-number h2)
3637                     gnus-newsgroup-scored))
3638          gnus-summary-default-score 0)))
3639
3640 (defun gnus-thread-sort-by-score (h1 h2)
3641   "Sort threads by root article score."
3642   (gnus-article-sort-by-score
3643    (gnus-thread-header h1) (gnus-thread-header h2)))
3644
3645 (defun gnus-thread-sort-by-total-score (h1 h2)
3646   "Sort threads by the sum of all scores in the thread.
3647 Unscored articles will be counted as having a score of zero."
3648   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3649
3650 (defun gnus-thread-total-score (thread)
3651   ;; This function find the total score of THREAD.
3652   (cond ((null thread)
3653          0)
3654         ((consp thread)
3655          (if (stringp (car thread))
3656              (apply gnus-thread-score-function 0
3657                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3658            (gnus-thread-total-score-1 thread)))
3659         (t
3660          (gnus-thread-total-score-1 (list thread)))))
3661
3662 (defun gnus-thread-total-score-1 (root)
3663   ;; This function find the total score of the thread below ROOT.
3664   (setq root (car root))
3665   (apply gnus-thread-score-function
3666          (or (append
3667               (mapcar 'gnus-thread-total-score
3668                       (cdr (gnus-id-to-thread (mail-header-id root))))
3669               (when (> (mail-header-number root) 0)
3670                 (list (or (cdr (assq (mail-header-number root)
3671                                      gnus-newsgroup-scored))
3672                           gnus-summary-default-score 0))))
3673              (list gnus-summary-default-score)
3674              '(0))))
3675
3676 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3677 (defvar gnus-tmp-prev-subject nil)
3678 (defvar gnus-tmp-false-parent nil)
3679 (defvar gnus-tmp-root-expunged nil)
3680 (defvar gnus-tmp-dummy-line nil)
3681
3682 (defvar gnus-tmp-header)
3683 (defun gnus-extra-header (type &optional header)
3684   "Return the extra header of TYPE."
3685   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3686       ""))
3687
3688 (defun gnus-summary-prepare-threads (threads)
3689   "Prepare summary buffer from THREADS and indentation LEVEL.
3690 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3691 or a straight list of headers."
3692   (gnus-message 7 "Generating summary...")
3693
3694   (setq gnus-newsgroup-threads threads)
3695   (beginning-of-line)
3696
3697   (let ((gnus-tmp-level 0)
3698         (default-score (or gnus-summary-default-score 0))
3699         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3700         thread number subject stack state gnus-tmp-gathered beg-match
3701         new-roots gnus-tmp-new-adopts thread-end
3702         gnus-tmp-header gnus-tmp-unread
3703         gnus-tmp-replied gnus-tmp-subject-or-nil
3704         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3705         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3706         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3707
3708     (setq gnus-tmp-prev-subject nil)
3709
3710     (if (vectorp (car threads))
3711         ;; If this is a straight (sic) list of headers, then a
3712         ;; threaded summary display isn't required, so we just create
3713         ;; an unthreaded one.
3714         (gnus-summary-prepare-unthreaded threads)
3715
3716       ;; Do the threaded display.
3717
3718       (while (or threads stack gnus-tmp-new-adopts new-roots)
3719
3720         (if (and (= gnus-tmp-level 0)
3721                  (or (not stack)
3722                      (= (caar stack) 0))
3723                  (not gnus-tmp-false-parent)
3724                  (or gnus-tmp-new-adopts new-roots))
3725             (if gnus-tmp-new-adopts
3726                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3727                       thread (list (car gnus-tmp-new-adopts))
3728                       gnus-tmp-header (caar thread)
3729                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3730               (when new-roots
3731                 (setq thread (list (car new-roots))
3732                       gnus-tmp-header (caar thread)
3733                       new-roots (cdr new-roots))))
3734
3735           (if threads
3736               ;; If there are some threads, we do them before the
3737               ;; threads on the stack.
3738               (setq thread threads
3739                     gnus-tmp-header (caar thread))
3740             ;; There were no current threads, so we pop something off
3741             ;; the stack.
3742             (setq state (car stack)
3743                   gnus-tmp-level (car state)
3744                   thread (cdr state)
3745                   stack (cdr stack)
3746                   gnus-tmp-header (caar thread))))
3747
3748         (setq gnus-tmp-false-parent nil)
3749         (setq gnus-tmp-root-expunged nil)
3750         (setq thread-end nil)
3751
3752         (if (stringp gnus-tmp-header)
3753             ;; The header is a dummy root.
3754             (cond
3755              ((eq gnus-summary-make-false-root 'adopt)
3756               ;; We let the first article adopt the rest.
3757               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3758                                                (cddar thread)))
3759               (setq gnus-tmp-gathered
3760                     (nconc (mapcar
3761                             (lambda (h) (mail-header-number (car h)))
3762                             (cddar thread))
3763                            gnus-tmp-gathered))
3764               (setq thread (cons (list (caar thread)
3765                                        (cadar thread))
3766                                  (cdr thread)))
3767               (setq gnus-tmp-level -1
3768                     gnus-tmp-false-parent t))
3769              ((eq gnus-summary-make-false-root 'empty)
3770               ;; We print adopted articles with empty subject fields.
3771               (setq gnus-tmp-gathered
3772                     (nconc (mapcar
3773                             (lambda (h) (mail-header-number (car h)))
3774                             (cddar thread))
3775                            gnus-tmp-gathered))
3776               (setq gnus-tmp-level -1))
3777              ((eq gnus-summary-make-false-root 'dummy)
3778               ;; We remember that we probably want to output a dummy
3779               ;; root.
3780               (setq gnus-tmp-dummy-line gnus-tmp-header)
3781               (setq gnus-tmp-prev-subject gnus-tmp-header))
3782              (t
3783               ;; We do not make a root for the gathered
3784               ;; sub-threads at all.
3785               (setq gnus-tmp-level -1)))
3786
3787           (setq number (mail-header-number gnus-tmp-header)
3788                 subject (mail-header-subject gnus-tmp-header))
3789
3790           (cond
3791            ;; If the thread has changed subject, we might want to make
3792            ;; this subthread into a root.
3793            ((and (null gnus-thread-ignore-subject)
3794                  (not (zerop gnus-tmp-level))
3795                  gnus-tmp-prev-subject
3796                  (not (inline
3797                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3798             (setq new-roots (nconc new-roots (list (car thread)))
3799                   thread-end t
3800                   gnus-tmp-header nil))
3801            ;; If the article lies outside the current limit,
3802            ;; then we do not display it.
3803            ((not (memq number gnus-newsgroup-limit))
3804             (setq gnus-tmp-gathered
3805                   (nconc (mapcar
3806                           (lambda (h) (mail-header-number (car h)))
3807                           (cdar thread))
3808                          gnus-tmp-gathered))
3809             (setq gnus-tmp-new-adopts (if (cdar thread)
3810                                           (append gnus-tmp-new-adopts
3811                                                   (cdar thread))
3812                                         gnus-tmp-new-adopts)
3813                   thread-end t
3814                   gnus-tmp-header nil)
3815             (when (zerop gnus-tmp-level)
3816               (setq gnus-tmp-root-expunged t)))
3817            ;; Perhaps this article is to be marked as read?
3818            ((and gnus-summary-mark-below
3819                  (< (or (cdr (assq number gnus-newsgroup-scored))
3820                         default-score)
3821                     gnus-summary-mark-below)
3822                  ;; Don't touch sparse articles.
3823                  (not (gnus-summary-article-sparse-p number))
3824                  (not (gnus-summary-article-ancient-p number)))
3825             (setq gnus-newsgroup-unreads
3826                   (delq number gnus-newsgroup-unreads))
3827             (if gnus-newsgroup-auto-expire
3828                 (push number gnus-newsgroup-expirable)
3829               (push (cons number gnus-low-score-mark)
3830                     gnus-newsgroup-reads))))
3831
3832           (when gnus-tmp-header
3833             ;; We may have an old dummy line to output before this
3834             ;; article.
3835             (when (and gnus-tmp-dummy-line
3836                        (gnus-subject-equal
3837                         gnus-tmp-dummy-line
3838                         (mail-header-subject gnus-tmp-header)))
3839               (gnus-summary-insert-dummy-line
3840                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3841               (setq gnus-tmp-dummy-line nil))
3842
3843             ;; Compute the mark.
3844             (setq gnus-tmp-unread (gnus-article-mark number))
3845
3846             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3847                                   gnus-tmp-header gnus-tmp-level)
3848                   gnus-newsgroup-data)
3849
3850             ;; Actually insert the line.
3851             (setq
3852              gnus-tmp-subject-or-nil
3853              (cond
3854               ((and gnus-thread-ignore-subject
3855                     gnus-tmp-prev-subject
3856                     (not (inline (gnus-subject-equal
3857                                   gnus-tmp-prev-subject subject))))
3858                subject)
3859               ((zerop gnus-tmp-level)
3860                (if (and (eq gnus-summary-make-false-root 'empty)
3861                         (memq number gnus-tmp-gathered)
3862                         gnus-tmp-prev-subject
3863                         (inline (gnus-subject-equal
3864                                  gnus-tmp-prev-subject subject)))
3865                    gnus-summary-same-subject
3866                  subject))
3867               (t gnus-summary-same-subject)))
3868             (if (and (eq gnus-summary-make-false-root 'adopt)
3869                      (= gnus-tmp-level 1)
3870                      (memq number gnus-tmp-gathered))
3871                 (setq gnus-tmp-opening-bracket ?\<
3872                       gnus-tmp-closing-bracket ?\>)
3873               (setq gnus-tmp-opening-bracket ?\[
3874                     gnus-tmp-closing-bracket ?\]))
3875             (setq
3876              gnus-tmp-indentation
3877              (aref gnus-thread-indent-array gnus-tmp-level)
3878              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3879              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3880                                 gnus-summary-default-score 0)
3881              gnus-tmp-score-char
3882              (if (or (null gnus-summary-default-score)
3883                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3884                          gnus-summary-zcore-fuzz))
3885                  ? ;Whitespace
3886                (if (< gnus-tmp-score gnus-summary-default-score)
3887                    gnus-score-below-mark gnus-score-over-mark))
3888              gnus-tmp-replied
3889              (cond ((memq number gnus-newsgroup-processable)
3890                     gnus-process-mark)
3891                    ((memq number gnus-newsgroup-cached)
3892                     gnus-cached-mark)
3893                    ((memq number gnus-newsgroup-replied)
3894                     gnus-replied-mark)
3895                    ((memq number gnus-newsgroup-saved)
3896                     gnus-saved-mark)
3897                    (t gnus-unread-mark))
3898              gnus-tmp-from (mail-header-from gnus-tmp-header)
3899              gnus-tmp-name
3900              (cond
3901               ((string-match "<[^>]+> *$" gnus-tmp-from)
3902                (setq beg-match (match-beginning 0))
3903                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3904                         (substring gnus-tmp-from (1+ (match-beginning 0))
3905                                    (1- (match-end 0))))
3906                    (substring gnus-tmp-from 0 beg-match)))
3907               ((string-match "(.+)" gnus-tmp-from)
3908                (substring gnus-tmp-from
3909                           (1+ (match-beginning 0)) (1- (match-end 0))))
3910               (t gnus-tmp-from)))
3911             (when (string= gnus-tmp-name "")
3912               (setq gnus-tmp-name gnus-tmp-from))
3913             (unless (numberp gnus-tmp-lines)
3914               (setq gnus-tmp-lines 0))
3915             (gnus-put-text-property
3916              (point)
3917              (progn (eval gnus-summary-line-format-spec) (point))
3918              'gnus-number number)
3919             (when gnus-visual-p
3920               (forward-line -1)
3921               (gnus-run-hooks 'gnus-summary-update-hook)
3922               (forward-line 1))
3923
3924             (setq gnus-tmp-prev-subject subject)))
3925
3926         (when (nth 1 thread)
3927           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3928         (incf gnus-tmp-level)
3929         (setq threads (if thread-end nil (cdar thread)))
3930         (unless threads
3931           (setq gnus-tmp-level 0)))))
3932   (gnus-message 7 "Generating summary...done"))
3933
3934 (defun gnus-summary-prepare-unthreaded (headers)
3935   "Generate an unthreaded summary buffer based on HEADERS."
3936   (let (header number mark)
3937
3938     (beginning-of-line)
3939
3940     (while headers
3941       ;; We may have to root out some bad articles...
3942       (when (memq (setq number (mail-header-number
3943                                 (setq header (pop headers))))
3944                   gnus-newsgroup-limit)
3945         ;; Mark article as read when it has a low score.
3946         (when (and gnus-summary-mark-below
3947                    (< (or (cdr (assq number gnus-newsgroup-scored))
3948                           gnus-summary-default-score 0)
3949                       gnus-summary-mark-below)
3950                    (not (gnus-summary-article-ancient-p number)))
3951           (setq gnus-newsgroup-unreads
3952                 (delq number gnus-newsgroup-unreads))
3953           (if gnus-newsgroup-auto-expire
3954               (push number gnus-newsgroup-expirable)
3955             (push (cons number gnus-low-score-mark)
3956                   gnus-newsgroup-reads)))
3957
3958         (setq mark (gnus-article-mark number))
3959         (push (gnus-data-make number mark (1+ (point)) header 0)
3960               gnus-newsgroup-data)
3961         (gnus-summary-insert-line
3962          header 0 number
3963          mark (memq number gnus-newsgroup-replied)
3964          (memq number gnus-newsgroup-expirable)
3965          (mail-header-subject header) nil
3966          (cdr (assq number gnus-newsgroup-scored))
3967          (memq number gnus-newsgroup-processable))))))
3968
3969 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3970   "Select newsgroup GROUP.
3971 If READ-ALL is non-nil, all articles in the group are selected.
3972 If SELECT-ARTICLES, only select those articles from GROUP."
3973   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3974          ;;!!! Dirty hack; should be removed.
3975          (gnus-summary-ignore-duplicates
3976           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3977               t
3978             gnus-summary-ignore-duplicates))
3979          (info (nth 2 entry))
3980          articles fetched-articles cached)
3981
3982     (unless (gnus-check-server
3983              (setq gnus-current-select-method
3984                    (gnus-find-method-for-group group)))
3985       (error "Couldn't open server"))
3986
3987     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3988         (gnus-activate-group group)     ; Or we can activate it...
3989         (progn                          ; Or we bug out.
3990           (when (equal major-mode 'gnus-summary-mode)
3991             (kill-buffer (current-buffer)))
3992           (error "Couldn't request group %s: %s"
3993                  group (gnus-status-message group))))
3994
3995     (unless (gnus-request-group group t)
3996       (when (equal major-mode 'gnus-summary-mode)
3997         (kill-buffer (current-buffer)))
3998       (error "Couldn't request group %s: %s"
3999              group (gnus-status-message group)))
4000
4001     (setq gnus-newsgroup-name group)
4002     (setq gnus-newsgroup-unselected nil)
4003     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4004     (gnus-summary-setup-default-charset)
4005
4006     ;; Adjust and set lists of article marks.
4007     (when info
4008       (gnus-adjust-marked-articles info))
4009
4010     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4011     (when (gnus-virtual-group-p group)
4012       (setq cached gnus-newsgroup-cached))
4013
4014     (setq gnus-newsgroup-unreads
4015           (gnus-set-difference
4016            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4017            gnus-newsgroup-dormant))
4018
4019     (setq gnus-newsgroup-processable nil)
4020
4021     (gnus-update-read-articles group gnus-newsgroup-unreads)
4022
4023     (if (setq articles select-articles)
4024         (setq gnus-newsgroup-unselected
4025               (gnus-sorted-intersection
4026                gnus-newsgroup-unreads
4027                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4028       (setq articles (gnus-articles-to-read group read-all)))
4029
4030     (cond
4031      ((null articles)
4032       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4033       'quit)
4034      ((eq articles 0) nil)
4035      (t
4036       ;; Init the dependencies hash table.
4037       (setq gnus-newsgroup-dependencies
4038             (gnus-make-hashtable (length articles)))
4039       (gnus-set-global-variables)
4040       ;; Retrieve the headers and read them in.
4041       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4042       (setq gnus-newsgroup-headers
4043             (if (eq 'nov
4044                     (setq gnus-headers-retrieved-by
4045                           (gnus-retrieve-headers
4046                            articles gnus-newsgroup-name
4047                            ;; We might want to fetch old headers, but
4048                            ;; not if there is only 1 article.
4049                            (and (or (and
4050                                      (not (eq gnus-fetch-old-headers 'some))
4051                                      (not (numberp gnus-fetch-old-headers)))
4052                                     (> (length articles) 1))
4053                                 gnus-fetch-old-headers))))
4054                 (gnus-get-newsgroup-headers-xover
4055                  articles nil nil gnus-newsgroup-name t)
4056               (gnus-get-newsgroup-headers)))
4057       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4058
4059       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4060       (when cached
4061         (setq gnus-newsgroup-cached cached))
4062
4063       ;; Suppress duplicates?
4064       (when gnus-suppress-duplicates
4065         (gnus-dup-suppress-articles))
4066
4067       ;; Set the initial limit.
4068       (setq gnus-newsgroup-limit (copy-sequence articles))
4069       ;; Remove canceled articles from the list of unread articles.
4070       (setq gnus-newsgroup-unreads
4071             (gnus-set-sorted-intersection
4072              gnus-newsgroup-unreads
4073              (setq fetched-articles
4074                    (mapcar (lambda (headers) (mail-header-number headers))
4075                            gnus-newsgroup-headers))))
4076       ;; Removed marked articles that do not exist.
4077       (gnus-update-missing-marks
4078        (gnus-sorted-complement fetched-articles articles))
4079       ;; We might want to build some more threads first.
4080       (when (and gnus-fetch-old-headers
4081                  (eq gnus-headers-retrieved-by 'nov))
4082         (if (eq gnus-fetch-old-headers 'invisible)
4083             (gnus-build-all-threads)
4084           (gnus-build-old-threads)))
4085       ;; Let the Gnus agent mark articles as read.
4086       (when gnus-agent
4087         (gnus-agent-get-undownloaded-list))
4088       ;; Check whether auto-expire is to be done in this group.
4089       (setq gnus-newsgroup-auto-expire
4090             (gnus-group-auto-expirable-p group))
4091       ;; Set up the article buffer now, if necessary.
4092       (unless gnus-single-article-buffer
4093         (gnus-article-setup-buffer))
4094       ;; First and last article in this newsgroup.
4095       (when gnus-newsgroup-headers
4096         (setq gnus-newsgroup-begin
4097               (mail-header-number (car gnus-newsgroup-headers))
4098               gnus-newsgroup-end
4099               (mail-header-number
4100                (gnus-last-element gnus-newsgroup-headers))))
4101       ;; GROUP is successfully selected.
4102       (or gnus-newsgroup-headers t)))))
4103
4104 (defun gnus-articles-to-read (group &optional read-all)
4105   ;; Find out what articles the user wants to read.
4106   (let* ((articles
4107           ;; Select all articles if `read-all' is non-nil, or if there
4108           ;; are no unread articles.
4109           (if (or read-all
4110                   (and (zerop (length gnus-newsgroup-marked))
4111                        (zerop (length gnus-newsgroup-unreads)))
4112                   (eq (gnus-group-find-parameter group 'display)
4113                       'all))
4114               (gnus-uncompress-range (gnus-active group))
4115             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4116                           (copy-sequence gnus-newsgroup-unreads))
4117                   '<)))
4118          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4119          (scored (length scored-list))
4120          (number (length articles))
4121          (marked (+ (length gnus-newsgroup-marked)
4122                     (length gnus-newsgroup-dormant)))
4123          (select
4124           (cond
4125            ((numberp read-all)
4126             read-all)
4127            (t
4128             (condition-case ()
4129                 (cond
4130                  ((and (or (<= scored marked) (= scored number))
4131                        (numberp gnus-large-newsgroup)
4132                        (> number gnus-large-newsgroup))
4133                   (let ((input
4134                          (read-string
4135                           (format
4136                            "How many articles from %s (default %d): "
4137                            (gnus-limit-string gnus-newsgroup-name 35)
4138                            number))))
4139                     (if (string-match "^[ \t]*$" input) number input)))
4140                  ((and (> scored marked) (< scored number)
4141                        (> (- scored number) 20))
4142                   (let ((input
4143                          (read-string
4144                           (format "%s %s (%d scored, %d total): "
4145                                   "How many articles from"
4146                                   group scored number))))
4147                     (if (string-match "^[ \t]*$" input)
4148                         number input)))
4149                  (t number))
4150               (quit nil))))))
4151     (setq select (if (stringp select) (string-to-number select) select))
4152     (if (or (null select) (zerop select))
4153         select
4154       (if (and (not (zerop scored)) (<= (abs select) scored))
4155           (progn
4156             (setq articles (sort scored-list '<))
4157             (setq number (length articles)))
4158         (setq articles (copy-sequence articles)))
4159
4160       (when (< (abs select) number)
4161         (if (< select 0)
4162             ;; Select the N oldest articles.
4163             (setcdr (nthcdr (1- (abs select)) articles) nil)
4164           ;; Select the N most recent articles.
4165           (setq articles (nthcdr (- number select) articles))))
4166       (setq gnus-newsgroup-unselected
4167             (gnus-sorted-intersection
4168              gnus-newsgroup-unreads
4169              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4170       articles)))
4171
4172 (defun gnus-killed-articles (killed articles)
4173   (let (out)
4174     (while articles
4175       (when (inline (gnus-member-of-range (car articles) killed))
4176         (push (car articles) out))
4177       (setq articles (cdr articles)))
4178     out))
4179
4180 (defun gnus-uncompress-marks (marks)
4181   "Uncompress the mark ranges in MARKS."
4182   (let ((uncompressed '(score bookmark))
4183         out)
4184     (while marks
4185       (if (memq (caar marks) uncompressed)
4186           (push (car marks) out)
4187         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4188       (setq marks (cdr marks)))
4189     out))
4190
4191 (defun gnus-adjust-marked-articles (info)
4192   "Set all article lists and remove all marks that are no longer valid."
4193   (let* ((marked-lists (gnus-info-marks info))
4194          (active (gnus-active (gnus-info-group info)))
4195          (min (car active))
4196          (max (cdr active))
4197          (types gnus-article-mark-lists)
4198          (uncompressed '(score bookmark killed))
4199          marks var articles article mark)
4200
4201     (while marked-lists
4202       (setq marks (pop marked-lists))
4203       (set (setq var (intern (format "gnus-newsgroup-%s"
4204                                      (car (rassq (setq mark (car marks))
4205                                                  types)))))
4206            (if (memq (car marks) uncompressed) (cdr marks)
4207              (gnus-uncompress-range (cdr marks))))
4208
4209       (setq articles (symbol-value var))
4210
4211       ;; All articles have to be subsets of the active articles.
4212       (cond
4213        ;; Adjust "simple" lists.
4214        ((memq mark '(tick dormant expire reply save))
4215         (while articles
4216           (when (or (< (setq article (pop articles)) min) (> article max))
4217             (set var (delq article (symbol-value var))))))
4218        ;; Adjust assocs.
4219        ((memq mark uncompressed)
4220         (when (not (listp (cdr (symbol-value var))))
4221           (set var (list (symbol-value var))))
4222         (when (not (listp (cdr articles)))
4223           (setq articles (list articles)))
4224         (while articles
4225           (when (or (not (consp (setq article (pop articles))))
4226                     (< (car article) min)
4227                     (> (car article) max))
4228             (set var (delq article (symbol-value var))))))))))
4229
4230 (defun gnus-update-missing-marks (missing)
4231   "Go through the list of MISSING articles and remove them from the mark lists."
4232   (when missing
4233     (let ((types gnus-article-mark-lists)
4234           var m)
4235       ;; Go through all types.
4236       (while types
4237         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4238         (when (symbol-value var)
4239           ;; This list has articles.  So we delete all missing articles
4240           ;; from it.
4241           (setq m missing)
4242           (while m
4243             (set var (delq (pop m) (symbol-value var)))))))))
4244
4245 (defun gnus-update-marks ()
4246   "Enter the various lists of marked articles into the newsgroup info list."
4247   (let ((types gnus-article-mark-lists)
4248         (info (gnus-get-info gnus-newsgroup-name))
4249         (uncompressed '(score bookmark killed))
4250         type list newmarked symbol delta-marks)
4251     (when info
4252       ;; Add all marks lists that are non-nil to the list of marks lists.
4253       (while (setq type (pop types))
4254         (when (setq list (symbol-value
4255                           (setq symbol
4256                                 (intern (format "gnus-newsgroup-%s"
4257                                                 (car type))))))
4258
4259           ;; Get rid of the entries of the articles that have the
4260           ;; default score.
4261           (when (and (eq (cdr type) 'score)
4262                      gnus-save-score
4263                      list)
4264             (let* ((arts list)
4265                    (prev (cons nil list))
4266                    (all prev))
4267               (while arts
4268                 (if (or (not (consp (car arts)))
4269                         (= (cdar arts) gnus-summary-default-score))
4270                     (setcdr prev (cdr arts))
4271                   (setq prev arts))
4272                 (setq arts (cdr arts)))
4273               (setq list (cdr all))))
4274
4275          (when (gnus-check-backend-function 'request-set-mark
4276                                             gnus-newsgroup-name)
4277            ;; score & bookmark are not proper flags (they are cons cells)
4278            ;; cache is a internal gnus flag
4279            (unless (memq (cdr type) '(cache score bookmark))
4280              (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4281                     (del (gnus-remove-from-range old list))
4282                     (add (gnus-remove-from-range list old)))
4283                (if add
4284                    (push (list add 'add (list (cdr type))) delta-marks))
4285                (if del
4286                    (push (list del 'del (list (cdr type))) delta-marks)))))
4287
4288           (push (cons (cdr type)
4289                       (if (memq (cdr type) uncompressed) list
4290                         (gnus-compress-sequence
4291                          (set symbol (sort list '<)) t)))
4292                 newmarked)))
4293
4294       (if delta-marks
4295          (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4296
4297       ;; Enter these new marks into the info of the group.
4298       (if (nthcdr 3 info)
4299           (setcar (nthcdr 3 info) newmarked)
4300         ;; Add the marks lists to the end of the info.
4301         (when newmarked
4302           (setcdr (nthcdr 2 info) (list newmarked))))
4303
4304       ;; Cut off the end of the info if there's nothing else there.
4305       (let ((i 5))
4306         (while (and (> i 2)
4307                     (not (nth i info)))
4308           (when (nthcdr (decf i) info)
4309             (setcdr (nthcdr i info) nil)))))))
4310
4311 (defun gnus-set-mode-line (where)
4312   "This function sets the mode line of the article or summary buffers.
4313 If WHERE is `summary', the summary mode line format will be used."
4314   ;; Is this mode line one we keep updated?
4315   (when (and (memq where gnus-updated-mode-lines)
4316              (symbol-value
4317               (intern (format "gnus-%s-mode-line-format-spec" where))))
4318     (let (mode-string)
4319       (save-excursion
4320         ;; We evaluate this in the summary buffer since these
4321         ;; variables are buffer-local to that buffer.
4322         (set-buffer gnus-summary-buffer)
4323         ;; We bind all these variables that are used in the `eval' form
4324         ;; below.
4325         (let* ((mformat (symbol-value
4326                          (intern
4327                           (format "gnus-%s-mode-line-format-spec" where))))
4328                (gnus-tmp-group-name gnus-newsgroup-name)
4329                (gnus-tmp-article-number (or gnus-current-article 0))
4330                (gnus-tmp-unread gnus-newsgroup-unreads)
4331                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4332                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4333                (gnus-tmp-unread-and-unselected
4334                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4335                             (zerop gnus-tmp-unselected))
4336                        "")
4337                       ((zerop gnus-tmp-unselected)
4338                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4339                       (t (format "{%d(+%d) more}"
4340                                  gnus-tmp-unread-and-unticked
4341                                  gnus-tmp-unselected))))
4342                (gnus-tmp-subject
4343                 (if (and gnus-current-headers
4344                          (vectorp gnus-current-headers))
4345                     (gnus-mode-string-quote
4346                      (mail-header-subject gnus-current-headers))
4347                   ""))
4348                bufname-length max-len
4349                gnus-tmp-header);; passed as argument to any user-format-funcs
4350           (setq mode-string (eval mformat))
4351           (setq bufname-length (if (string-match "%b" mode-string)
4352                                    (- (length
4353                                        (buffer-name
4354                                         (if (eq where 'summary)
4355                                             nil
4356                                           (get-buffer gnus-article-buffer))))
4357                                       2)
4358                                  0))
4359           (setq max-len (max 4 (if gnus-mode-non-string-length
4360                                    (- (window-width)
4361                                       gnus-mode-non-string-length
4362                                       bufname-length)
4363                                  (length mode-string))))
4364           ;; We might have to chop a bit of the string off...
4365           (when (> (length mode-string) max-len)
4366             (setq mode-string
4367                   (concat (truncate-string-to-width mode-string (- max-len 3))
4368                           "...")))
4369           ;; Pad the mode string a bit.
4370           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4371       ;; Update the mode line.
4372       (setq mode-line-buffer-identification
4373             (gnus-mode-line-buffer-identification (list mode-string)))
4374       (set-buffer-modified-p t))))
4375
4376 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4377   "Go through the HEADERS list and add all Xrefs to a hash table.
4378 The resulting hash table is returned, or nil if no Xrefs were found."
4379   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4380          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4381          (xref-hashtb (gnus-make-hashtable))
4382          start group entry number xrefs header)
4383     (while headers
4384       (setq header (pop headers))
4385       (when (and (setq xrefs (mail-header-xref header))
4386                  (not (memq (setq number (mail-header-number header))
4387                             unreads)))
4388         (setq start 0)
4389         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4390           (setq start (match-end 0))
4391           (setq group (if prefix
4392                           (concat prefix (substring xrefs (match-beginning 1)
4393                                                     (match-end 1)))
4394                         (substring xrefs (match-beginning 1) (match-end 1))))
4395           (setq number
4396                 (string-to-int (substring xrefs (match-beginning 2)
4397                                           (match-end 2))))
4398           (if (setq entry (gnus-gethash group xref-hashtb))
4399               (setcdr entry (cons number (cdr entry)))
4400             (gnus-sethash group (cons number nil) xref-hashtb)))))
4401     (and start xref-hashtb)))
4402
4403 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4404   "Look through all the headers and mark the Xrefs as read."
4405   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4406         name entry info xref-hashtb idlist method nth4)
4407     (save-excursion
4408       (set-buffer gnus-group-buffer)
4409       (when (setq xref-hashtb
4410                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4411         (mapatoms
4412          (lambda (group)
4413            (unless (string= from-newsgroup (setq name (symbol-name group)))
4414              (setq idlist (symbol-value group))
4415              ;; Dead groups are not updated.
4416              (and (prog1
4417                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4418                             info (nth 2 entry))
4419                     (when (stringp (setq nth4 (gnus-info-method info)))
4420                       (setq nth4 (gnus-server-to-method nth4))))
4421                   ;; Only do the xrefs if the group has the same
4422                   ;; select method as the group we have just read.
4423                   (or (gnus-methods-equal-p
4424                        nth4 (gnus-find-method-for-group from-newsgroup))
4425                       virtual
4426                       (equal nth4 (setq method (gnus-find-method-for-group
4427                                                 from-newsgroup)))
4428                       (and (equal (car nth4) (car method))
4429                            (equal (nth 1 nth4) (nth 1 method))))
4430                   gnus-use-cross-reference
4431                   (or (not (eq gnus-use-cross-reference t))
4432                       virtual
4433                       ;; Only do cross-references on subscribed
4434                       ;; groups, if that is what is wanted.
4435                       (<= (gnus-info-level info) gnus-level-subscribed))
4436                   (gnus-group-make-articles-read name idlist))))
4437          xref-hashtb)))))
4438
4439 (defun gnus-compute-read-articles (group articles)
4440   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4441          (info (nth 2 entry))
4442          (active (gnus-active group))
4443          ninfo)
4444     (when entry
4445       ;; First peel off all invalid article numbers.
4446       (when active
4447         (let ((ids articles)
4448               id first)
4449           (while (setq id (pop ids))
4450             (when (and first (> id (cdr active)))
4451               ;; We'll end up in this situation in one particular
4452               ;; obscure situation.  If you re-scan a group and get
4453               ;; a new article that is cross-posted to a different
4454               ;; group that has not been re-scanned, you might get
4455               ;; crossposted article that has a higher number than
4456               ;; Gnus believes possible.  So we re-activate this
4457               ;; group as well.  This might mean doing the
4458               ;; crossposting thingy will *increase* the number
4459               ;; of articles in some groups.  Tsk, tsk.
4460               (setq active (or (gnus-activate-group group) active)))
4461             (when (or (> id (cdr active))
4462                       (< id (car active)))
4463               (setq articles (delq id articles))))))
4464       ;; If the read list is nil, we init it.
4465       (if (and active
4466                (null (gnus-info-read info))
4467                (> (car active) 1))
4468           (setq ninfo (cons 1 (1- (car active))))
4469         (setq ninfo (gnus-info-read info)))
4470       ;; Then we add the read articles to the range.
4471       (gnus-add-to-range
4472        ninfo (setq articles (sort articles '<))))))
4473
4474 (defun gnus-group-make-articles-read (group articles)
4475   "Update the info of GROUP to say that ARTICLES are read."
4476   (let* ((num 0)
4477          (entry (gnus-gethash group gnus-newsrc-hashtb))
4478          (info (nth 2 entry))
4479          (active (gnus-active group))
4480          range)
4481     (when entry
4482       (setq range (gnus-compute-read-articles group articles))
4483       (save-excursion
4484         (set-buffer gnus-group-buffer)
4485         (gnus-undo-register
4486           `(progn
4487              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4488              (gnus-info-set-read ',info ',(gnus-info-read info))
4489              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4490              (gnus-group-update-group ,group t))))
4491       ;; Add the read articles to the range.
4492       (gnus-info-set-read info range)
4493       ;; Then we have to re-compute how many unread
4494       ;; articles there are in this group.
4495       (when active
4496         (cond
4497          ((not range)
4498           (setq num (- (1+ (cdr active)) (car active))))
4499          ((not (listp (cdr range)))
4500           (setq num (- (cdr active) (- (1+ (cdr range))
4501                                        (car range)))))
4502          (t
4503           (while range
4504             (if (numberp (car range))
4505                 (setq num (1+ num))
4506               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4507             (setq range (cdr range)))
4508           (setq num (- (cdr active) num))))
4509         ;; Update the number of unread articles.
4510         (setcar entry num)
4511         ;; Update the group buffer.
4512         (gnus-group-update-group group t)))))
4513
4514 (defun gnus-methods-equal-p (m1 m2)
4515   (let ((m1 (or m1 gnus-select-method))
4516         (m2 (or m2 gnus-select-method)))
4517     (or (equal m1 m2)
4518         (and (eq (car m1) (car m2))
4519              (or (not (memq 'address (assoc (symbol-name (car m1))
4520                                             gnus-valid-select-methods)))
4521                  (equal (nth 1 m1) (nth 1 m2)))))))
4522
4523 (defvar gnus-newsgroup-none-id 0)
4524
4525 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4526   (let ((cur nntp-server-buffer)
4527         (dependencies
4528          (or dependencies
4529              (save-excursion (set-buffer gnus-summary-buffer)
4530                              gnus-newsgroup-dependencies)))
4531         headers id end ref
4532         (mail-parse-charset gnus-newsgroup-charset))
4533     (save-excursion
4534       (set-buffer nntp-server-buffer)
4535       ;; Translate all TAB characters into SPACE characters.
4536       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4537       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4538       (gnus-run-hooks 'gnus-parse-headers-hook)
4539       (let ((case-fold-search t)
4540             in-reply-to header p lines chars)
4541         (goto-char (point-min))
4542         ;; Search to the beginning of the next header.  Error messages
4543         ;; do not begin with 2 or 3.
4544         (while (re-search-forward "^[23][0-9]+ " nil t)
4545           (setq id nil
4546                 ref nil)
4547           ;; This implementation of this function, with nine
4548           ;; search-forwards instead of the one re-search-forward and
4549           ;; a case (which basically was the old function) is actually
4550           ;; about twice as fast, even though it looks messier.  You
4551           ;; can't have everything, I guess.  Speed and elegance
4552           ;; doesn't always go hand in hand.
4553           (setq
4554            header
4555            (vector
4556             ;; Number.
4557             (prog1
4558                 (read cur)
4559               (end-of-line)
4560               (setq p (point))
4561               (narrow-to-region (point)
4562                                 (or (and (search-forward "\n.\n" nil t)
4563                                          (- (point) 2))
4564                                     (point))))
4565             ;; Subject.
4566             (progn
4567               (goto-char p)
4568               (if (search-forward "\nsubject: " nil t)
4569                   (funcall gnus-decode-encoded-word-function
4570                            (nnheader-header-value))
4571                 "(none)"))
4572             ;; From.
4573             (progn
4574               (goto-char p)
4575               (if (search-forward "\nfrom: " nil t)
4576                   (funcall gnus-decode-encoded-word-function
4577                            (nnheader-header-value))
4578                 "(nobody)"))
4579             ;; Date.
4580             (progn
4581               (goto-char p)
4582               (if (search-forward "\ndate: " nil t)
4583                   (nnheader-header-value) ""))
4584             ;; Message-ID.
4585             (progn
4586               (goto-char p)
4587               (setq id (if (re-search-forward
4588                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4589                            ;; We do it this way to make sure the Message-ID
4590                            ;; is (somewhat) syntactically valid.
4591                            (buffer-substring (match-beginning 1)
4592                                              (match-end 1))
4593                          ;; If there was no message-id, we just fake one
4594                          ;; to make subsequent routines simpler.
4595                          (nnheader-generate-fake-message-id))))
4596             ;; References.
4597             (progn
4598               (goto-char p)
4599               (if (search-forward "\nreferences: " nil t)
4600                   (progn
4601                     (setq end (point))
4602                     (prog1
4603                         (nnheader-header-value)
4604                       (setq ref
4605                             (buffer-substring
4606                              (progn
4607                                (end-of-line)
4608                                (search-backward ">" end t)
4609                                (1+ (point)))
4610                              (progn
4611                                (search-backward "<" end t)
4612                                (point))))))
4613                 ;; Get the references from the in-reply-to header if there
4614                 ;; were no references and the in-reply-to header looks
4615                 ;; promising.
4616                 (if (and (search-forward "\nin-reply-to: " nil t)
4617                          (setq in-reply-to (nnheader-header-value))
4618                          (string-match "<[^>]+>" in-reply-to))
4619                     (let (ref2)
4620                       (setq ref (substring in-reply-to (match-beginning 0)
4621                                            (match-end 0)))
4622                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4623                         (setq ref2 (substring in-reply-to (match-beginning 0)
4624                                               (match-end 0)))
4625                         (when (> (length ref2) (length ref))
4626                           (setq ref ref2)))
4627                       ref)
4628                   (setq ref nil))))
4629             ;; Chars.
4630             (progn
4631               (goto-char p)
4632               (if (search-forward "\nchars: " nil t)
4633                   (if (numberp (setq chars (ignore-errors (read cur))))
4634                       chars 0)
4635                 0))
4636             ;; Lines.
4637             (progn
4638               (goto-char p)
4639               (if (search-forward "\nlines: " nil t)
4640                   (if (numberp (setq lines (ignore-errors (read cur))))
4641                       lines 0)
4642                 0))
4643             ;; Xref.
4644             (progn
4645               (goto-char p)
4646               (and (search-forward "\nxref: " nil t)
4647                    (nnheader-header-value)))
4648             ;; Extra.
4649             (when gnus-extra-headers
4650               (let ((extra gnus-extra-headers)
4651                     out)
4652                 (while extra
4653                   (goto-char p)
4654                   (when (search-forward
4655                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4656                     (push (cons (car extra) (nnheader-header-value))
4657                           out))
4658                   (pop extra))
4659                 out))))
4660           (when (equal id ref)
4661             (setq ref nil))
4662
4663           (when gnus-alter-header-function
4664             (funcall gnus-alter-header-function header)
4665             (setq id (mail-header-id header)
4666                   ref (gnus-parent-id (mail-header-references header))))
4667
4668           (when (setq header
4669                       (gnus-dependencies-add-header
4670                        header dependencies force-new))
4671             (push header headers))
4672           (goto-char (point-max))
4673           (widen))
4674         (nreverse headers)))))
4675
4676 ;; Goes through the xover lines and returns a list of vectors
4677 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4678                                                   force-new dependencies
4679                                                   group also-fetch-heads)
4680   "Parse the news overview data in the server buffer, and return a
4681 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4682   ;; Get the Xref when the users reads the articles since most/some
4683   ;; NNTP servers do not include Xrefs when using XOVER.
4684   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4685   (let ((mail-parse-charset gnus-newsgroup-charset)
4686         (cur nntp-server-buffer)
4687         (dependencies (or dependencies gnus-newsgroup-dependencies))
4688         number headers header)
4689     (save-excursion
4690       (set-buffer nntp-server-buffer)
4691       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4692       ;; Allow the user to mangle the headers before parsing them.
4693       (gnus-run-hooks 'gnus-parse-headers-hook)
4694       (goto-char (point-min))
4695       (while (not (eobp))
4696         (condition-case ()
4697             (while (and sequence (not (eobp)))
4698               (setq number (read cur))
4699               (while (and sequence
4700                           (< (car sequence) number))
4701                 (setq sequence (cdr sequence)))
4702               (and sequence
4703                    (eq number (car sequence))
4704                    (progn
4705                      (setq sequence (cdr sequence))
4706                      (setq header (inline
4707                                     (gnus-nov-parse-line
4708                                      number dependencies force-new))))
4709                    (push header headers))
4710               (forward-line 1))
4711           (error
4712            (gnus-error 4 "Strange nov line (%d)"
4713                        (count-lines (point-min) (point)))))
4714         (forward-line 1))
4715       ;; A common bug in inn is that if you have posted an article and
4716       ;; then retrieves the active file, it will answer correctly --
4717       ;; the new article is included.  However, a NOV entry for the
4718       ;; article may not have been generated yet, so this may fail.
4719       ;; We work around this problem by retrieving the last few
4720       ;; headers using HEAD.
4721       (if (or (not also-fetch-heads)
4722               (not sequence))
4723           ;; We (probably) got all the headers.
4724           (nreverse headers)
4725         (let ((gnus-nov-is-evil t))
4726           (nconc
4727            (nreverse headers)
4728            (when (gnus-retrieve-headers sequence group)
4729              (gnus-get-newsgroup-headers))))))))
4730
4731 (defun gnus-article-get-xrefs ()
4732   "Fill in the Xref value in `gnus-current-headers', if necessary.
4733 This is meant to be called in `gnus-article-internal-prepare-hook'."
4734   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4735                                  gnus-current-headers)))
4736     (or (not gnus-use-cross-reference)
4737         (not headers)
4738         (and (mail-header-xref headers)
4739              (not (string= (mail-header-xref headers) "")))
4740         (let ((case-fold-search t)
4741               xref)
4742           (save-restriction
4743             (nnheader-narrow-to-headers)
4744             (goto-char (point-min))
4745             (when (or (and (eq (downcase (char-after)) ?x)
4746                            (looking-at "Xref:"))
4747                       (search-forward "\nXref:" nil t))
4748               (goto-char (1+ (match-end 0)))
4749               (setq xref (buffer-substring (point)
4750                                            (progn (end-of-line) (point))))
4751               (mail-header-set-xref headers xref)))))))
4752
4753 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4754   "Find article ID and insert the summary line for that article.
4755 OLD-HEADER can either be a header or a line number to insert
4756 the subject line on."
4757   (let* ((line (and (numberp old-header) old-header))
4758          (old-header (and (vectorp old-header) old-header))
4759          (header (cond ((and old-header use-old-header)
4760                         old-header)
4761                        ((and (numberp id)
4762                              (gnus-number-to-header id))
4763                         (gnus-number-to-header id))
4764                        (t
4765                         (gnus-read-header id))))
4766          (number (and (numberp id) id))
4767          d)
4768     (when header
4769       ;; Rebuild the thread that this article is part of and go to the
4770       ;; article we have fetched.
4771       (when (and (not gnus-show-threads)
4772                  old-header)
4773         (when (and number
4774                    (setq d (gnus-data-find (mail-header-number old-header))))
4775           (goto-char (gnus-data-pos d))
4776           (gnus-data-remove
4777            number
4778            (- (gnus-point-at-bol)
4779               (prog1
4780                   (1+ (gnus-point-at-eol))
4781                 (gnus-delete-line))))))
4782       (when old-header
4783         (mail-header-set-number header (mail-header-number old-header)))
4784       (setq gnus-newsgroup-sparse
4785             (delq (setq number (mail-header-number header))
4786                   gnus-newsgroup-sparse))
4787       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4788       (push number gnus-newsgroup-limit)
4789       (gnus-rebuild-thread (mail-header-id header) line)
4790       (gnus-summary-goto-subject number nil t))
4791     (when (and (numberp number)
4792                (> number 0))
4793       ;; We have to update the boundaries even if we can't fetch the
4794       ;; article if ID is a number -- so that the next `P' or `N'
4795       ;; command will fetch the previous (or next) article even
4796       ;; if the one we tried to fetch this time has been canceled.
4797       (when (> number gnus-newsgroup-end)
4798         (setq gnus-newsgroup-end number))
4799       (when (< number gnus-newsgroup-begin)
4800         (setq gnus-newsgroup-begin number))
4801       (setq gnus-newsgroup-unselected
4802             (delq number gnus-newsgroup-unselected)))
4803     ;; Report back a success?
4804     (and header (mail-header-number header))))
4805
4806 ;;; Process/prefix in the summary buffer
4807
4808 (defun gnus-summary-work-articles (n)
4809   "Return a list of articles to be worked upon.
4810 The prefix argument, the list of process marked articles, and the
4811 current article will be taken into consideration."
4812   (save-excursion
4813     (set-buffer gnus-summary-buffer)
4814     (cond
4815      (n
4816       ;; A numerical prefix has been given.
4817       (setq n (prefix-numeric-value n))
4818       (let ((backward (< n 0))
4819             (n (abs (prefix-numeric-value n)))
4820             articles article)
4821         (save-excursion
4822           (while
4823               (and (> n 0)
4824                    (push (setq article (gnus-summary-article-number))
4825                          articles)
4826                    (if backward
4827                        (gnus-summary-find-prev nil article)
4828                      (gnus-summary-find-next nil article)))
4829             (decf n)))
4830         (nreverse articles)))
4831      ((and (gnus-region-active-p) (mark))
4832       (message "region active")
4833       ;; Work on the region between point and mark.
4834       (let ((max (max (point) (mark)))
4835             articles article)
4836         (save-excursion
4837           (goto-char (min (min (point) (mark))))
4838           (while
4839               (and
4840                (push (setq article (gnus-summary-article-number)) articles)
4841                (gnus-summary-find-next nil article)
4842                (< (point) max)))
4843           (nreverse articles))))
4844      (gnus-newsgroup-processable
4845       ;; There are process-marked articles present.
4846       ;; Save current state.
4847       (gnus-summary-save-process-mark)
4848       ;; Return the list.
4849       (reverse gnus-newsgroup-processable))
4850      (t
4851       ;; Just return the current article.
4852       (list (gnus-summary-article-number))))))
4853
4854 (defmacro gnus-summary-iterate (arg &rest forms)
4855   "Iterate over the process/prefixed articles and do FORMS.
4856 ARG is the interactive prefix given to the command.  FORMS will be
4857 executed with point over the summary line of the articles."
4858   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4859     `(let ((,articles (gnus-summary-work-articles ,arg)))
4860        (while ,articles
4861          (gnus-summary-goto-subject (car ,articles))
4862          ,@forms))))
4863
4864 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4865 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4866
4867 (defun gnus-summary-save-process-mark ()
4868   "Push the current set of process marked articles on the stack."
4869   (interactive)
4870   (push (copy-sequence gnus-newsgroup-processable)
4871         gnus-newsgroup-process-stack))
4872
4873 (defun gnus-summary-kill-process-mark ()
4874   "Push the current set of process marked articles on the stack and unmark."
4875   (interactive)
4876   (gnus-summary-save-process-mark)
4877   (gnus-summary-unmark-all-processable))
4878
4879 (defun gnus-summary-yank-process-mark ()
4880   "Pop the last process mark state off the stack and restore it."
4881   (interactive)
4882   (unless gnus-newsgroup-process-stack
4883     (error "Empty mark stack"))
4884   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4885
4886 (defun gnus-summary-process-mark-set (set)
4887   "Make SET into the current process marked articles."
4888   (gnus-summary-unmark-all-processable)
4889   (while set
4890     (gnus-summary-set-process-mark (pop set))))
4891
4892 ;;; Searching and stuff
4893
4894 (defun gnus-summary-search-group (&optional backward use-level)
4895   "Search for next unread newsgroup.
4896 If optional argument BACKWARD is non-nil, search backward instead."
4897   (save-excursion
4898     (set-buffer gnus-group-buffer)
4899     (when (gnus-group-search-forward
4900            backward nil (if use-level (gnus-group-group-level) nil))
4901       (gnus-group-group-name))))
4902
4903 (defun gnus-summary-best-group (&optional exclude-group)
4904   "Find the name of the best unread group.
4905 If EXCLUDE-GROUP, do not go to this group."
4906   (save-excursion
4907     (set-buffer gnus-group-buffer)
4908     (save-excursion
4909       (gnus-group-best-unread-group exclude-group))))
4910
4911 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4912   (if backward (gnus-summary-find-prev)
4913     (let* ((dummy (gnus-summary-article-intangible-p))
4914            (article (or article (gnus-summary-article-number)))
4915            (arts (gnus-data-find-list article))
4916            result)
4917       (when (and (not dummy)
4918                  (or (not gnus-summary-check-current)
4919                      (not unread)
4920                      (not (gnus-data-unread-p (car arts)))))
4921         (setq arts (cdr arts)))
4922       (when (setq result
4923                   (if unread
4924                       (progn
4925                         (while arts
4926                           (when (or (and undownloaded
4927                                          (eq gnus-undownloaded-mark
4928                                              (gnus-data-mark (car arts))))
4929                                     (gnus-data-unread-p (car arts)))
4930                             (setq result (car arts)
4931                                   arts nil))
4932                           (setq arts (cdr arts)))
4933                         result)
4934                     (car arts)))
4935         (goto-char (gnus-data-pos result))
4936         (gnus-data-number result)))))
4937
4938 (defun gnus-summary-find-prev (&optional unread article)
4939   (let* ((eobp (eobp))
4940          (article (or article (gnus-summary-article-number)))
4941          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4942          result)
4943     (when (and (not eobp)
4944                (or (not gnus-summary-check-current)
4945                    (not unread)
4946                    (not (gnus-data-unread-p (car arts)))))
4947       (setq arts (cdr arts)))
4948     (when (setq result
4949                 (if unread
4950                     (progn
4951                       (while arts
4952                         (when (gnus-data-unread-p (car arts))
4953                           (setq result (car arts)
4954                                 arts nil))
4955                         (setq arts (cdr arts)))
4956                       result)
4957                   (car arts)))
4958       (goto-char (gnus-data-pos result))
4959       (gnus-data-number result))))
4960
4961 (defun gnus-summary-find-subject (subject &optional unread backward article)
4962   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4963          (article (or article (gnus-summary-article-number)))
4964          (articles (gnus-data-list backward))
4965          (arts (gnus-data-find-list article articles))
4966          result)
4967     (when (or (not gnus-summary-check-current)
4968               (not unread)
4969               (not (gnus-data-unread-p (car arts))))
4970       (setq arts (cdr arts)))
4971     (while arts
4972       (and (or (not unread)
4973                (gnus-data-unread-p (car arts)))
4974            (vectorp (gnus-data-header (car arts)))
4975            (gnus-subject-equal
4976             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4977            (setq result (car arts)
4978                  arts nil))
4979       (setq arts (cdr arts)))
4980     (and result
4981          (goto-char (gnus-data-pos result))
4982          (gnus-data-number result))))
4983
4984 (defun gnus-summary-search-forward (&optional unread subject backward)
4985   "Search forward for an article.
4986 If UNREAD, look for unread articles.  If SUBJECT, look for
4987 articles with that subject.  If BACKWARD, search backward instead."
4988   (cond (subject (gnus-summary-find-subject subject unread backward))
4989         (backward (gnus-summary-find-prev unread))
4990         (t (gnus-summary-find-next unread))))
4991
4992 (defun gnus-recenter (&optional n)
4993   "Center point in window and redisplay frame.
4994 Also do horizontal recentering."
4995   (interactive "P")
4996   (when (and gnus-auto-center-summary
4997              (not (eq gnus-auto-center-summary 'vertical)))
4998     (gnus-horizontal-recenter))
4999   (recenter n))
5000
5001 (defun gnus-summary-recenter ()
5002   "Center point in the summary window.
5003 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5004 displayed, no centering will be performed."
5005   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5006   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5007   (let* ((top (cond ((< (window-height) 4) 0)
5008                     ((< (window-height) 7) 1)
5009                     (t (if (numberp gnus-auto-center-summary)
5010                            gnus-auto-center-summary
5011                          2))))
5012          (height (1- (window-height)))
5013          (bottom (save-excursion (goto-char (point-max))
5014                                  (forward-line (- height))
5015                                  (point)))
5016          (window (get-buffer-window (current-buffer))))
5017     ;; The user has to want it.
5018     (when gnus-auto-center-summary
5019       (when (get-buffer-window gnus-article-buffer)
5020         ;; Only do recentering when the article buffer is displayed,
5021         ;; Set the window start to either `bottom', which is the biggest
5022         ;; possible valid number, or the second line from the top,
5023         ;; whichever is the least.
5024         (set-window-start
5025          window (min bottom (save-excursion
5026                               (forward-line (- top)) (point)))))
5027       ;; Do horizontal recentering while we're at it.
5028       (when (and (get-buffer-window (current-buffer) t)
5029                  (not (eq gnus-auto-center-summary 'vertical)))
5030         (let ((selected (selected-window)))
5031           (select-window (get-buffer-window (current-buffer) t))
5032           (gnus-summary-position-point)
5033           (gnus-horizontal-recenter)
5034           (select-window selected))))))
5035
5036 (defun gnus-summary-jump-to-group (newsgroup)
5037   "Move point to NEWSGROUP in group mode buffer."
5038   ;; Keep update point of group mode buffer if visible.
5039   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5040       (save-window-excursion
5041         ;; Take care of tree window mode.
5042         (when (get-buffer-window gnus-group-buffer)
5043           (pop-to-buffer gnus-group-buffer))
5044         (gnus-group-jump-to-group newsgroup))
5045     (save-excursion
5046       ;; Take care of tree window mode.
5047       (if (get-buffer-window gnus-group-buffer)
5048           (pop-to-buffer gnus-group-buffer)
5049         (set-buffer gnus-group-buffer))
5050       (gnus-group-jump-to-group newsgroup))))
5051
5052 ;; This function returns a list of article numbers based on the
5053 ;; difference between the ranges of read articles in this group and
5054 ;; the range of active articles.
5055 (defun gnus-list-of-unread-articles (group)
5056   (let* ((read (gnus-info-read (gnus-get-info group)))
5057          (active (or (gnus-active group) (gnus-activate-group group)))
5058          (last (cdr active))
5059          first nlast unread)
5060     ;; If none are read, then all are unread.
5061     (if (not read)
5062         (setq first (car active))
5063       ;; If the range of read articles is a single range, then the
5064       ;; first unread article is the article after the last read
5065       ;; article.  Sounds logical, doesn't it?
5066       (if (not (listp (cdr read)))
5067           (setq first (max (car active) (1+ (cdr read))))
5068         ;; `read' is a list of ranges.
5069         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5070                                   (caar read)))
5071                   1)
5072           (setq first (car active)))
5073         (while read
5074           (when first
5075             (while (< first nlast)
5076               (push first unread)
5077               (setq first (1+ first))))
5078           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5079           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5080           (setq read (cdr read)))))
5081     ;; And add the last unread articles.
5082     (while (<= first last)
5083       (push first unread)
5084       (setq first (1+ first)))
5085     ;; Return the list of unread articles.
5086     (delq 0 (nreverse unread))))
5087
5088 (defun gnus-list-of-read-articles (group)
5089   "Return a list of unread, unticked and non-dormant articles."
5090   (let* ((info (gnus-get-info group))
5091          (marked (gnus-info-marks info))
5092          (active (gnus-active group)))
5093     (and info active
5094          (gnus-set-difference
5095           (gnus-sorted-complement
5096            (gnus-uncompress-range active)
5097            (gnus-list-of-unread-articles group))
5098           (append
5099            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5100            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5101
5102 ;; Various summary commands
5103
5104 (defun gnus-summary-select-article-buffer ()
5105   "Reconfigure windows to show article buffer."
5106   (interactive)
5107   (if (not (gnus-buffer-live-p gnus-article-buffer))
5108       (error "There is no article buffer for this summary buffer")
5109     (gnus-configure-windows 'article)
5110     (select-window (get-buffer-window gnus-article-buffer))))
5111
5112 (defun gnus-summary-universal-argument (arg)
5113   "Perform any operation on all articles that are process/prefixed."
5114   (interactive "P")
5115   (let ((articles (gnus-summary-work-articles arg))
5116         func article)
5117     (if (eq
5118          (setq
5119           func
5120           (key-binding
5121            (read-key-sequence
5122             (substitute-command-keys
5123              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5124              ))))
5125          'undefined)
5126         (gnus-error 1 "Undefined key")
5127       (save-excursion
5128         (while articles
5129           (gnus-summary-goto-subject (setq article (pop articles)))
5130           (let (gnus-newsgroup-processable)
5131             (command-execute func))
5132           (gnus-summary-remove-process-mark article)))))
5133   (gnus-summary-position-point))
5134
5135 (defun gnus-summary-toggle-truncation (&optional arg)
5136   "Toggle truncation of summary lines.
5137 With arg, turn line truncation on iff arg is positive."
5138   (interactive "P")
5139   (setq truncate-lines
5140         (if (null arg) (not truncate-lines)
5141           (> (prefix-numeric-value arg) 0)))
5142   (redraw-display))
5143
5144 (defun gnus-summary-reselect-current-group (&optional all rescan)
5145   "Exit and then reselect the current newsgroup.
5146 The prefix argument ALL means to select all articles."
5147   (interactive "P")
5148   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5149     (error "Ephemeral groups can't be reselected"))
5150   (let ((current-subject (gnus-summary-article-number))
5151         (group gnus-newsgroup-name))
5152     (setq gnus-newsgroup-begin nil)
5153     (gnus-summary-exit)
5154     ;; We have to adjust the point of group mode buffer because
5155     ;; point was moved to the next unread newsgroup by exiting.
5156     (gnus-summary-jump-to-group group)
5157     (when rescan
5158       (save-excursion
5159         (gnus-group-get-new-news-this-group 1)))
5160     (gnus-group-read-group all t)
5161     (gnus-summary-goto-subject current-subject nil t)))
5162
5163 (defun gnus-summary-rescan-group (&optional all)
5164   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5165   (interactive "P")
5166   (gnus-summary-reselect-current-group all t))
5167
5168 (defun gnus-summary-update-info (&optional non-destructive)
5169   (save-excursion
5170     (let ((group gnus-newsgroup-name))
5171       (when group
5172         (when gnus-newsgroup-kill-headers
5173           (setq gnus-newsgroup-killed
5174                 (gnus-compress-sequence
5175                  (nconc
5176                   (gnus-set-sorted-intersection
5177                    (gnus-uncompress-range gnus-newsgroup-killed)
5178                    (setq gnus-newsgroup-unselected
5179                          (sort gnus-newsgroup-unselected '<)))
5180                   (setq gnus-newsgroup-unreads
5181                         (sort gnus-newsgroup-unreads '<)))
5182                  t)))
5183         (unless (listp (cdr gnus-newsgroup-killed))
5184           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5185         (let ((headers gnus-newsgroup-headers))
5186           ;; Set the new ranges of read articles.
5187           (save-excursion
5188             (set-buffer gnus-group-buffer)
5189             (gnus-undo-force-boundary))
5190           (gnus-update-read-articles
5191            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5192           ;; Set the current article marks.
5193           (let ((gnus-newsgroup-scored
5194                  (if (and (not gnus-save-score)
5195                           (not non-destructive))
5196                      nil
5197                    gnus-newsgroup-scored)))
5198             (save-excursion
5199               (gnus-update-marks)))
5200           ;; Do the cross-ref thing.
5201           (when gnus-use-cross-reference
5202             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5203           ;; Do not switch windows but change the buffer to work.
5204           (set-buffer gnus-group-buffer)
5205           (unless (gnus-ephemeral-group-p group)
5206             (gnus-group-update-group group)))))))
5207
5208 (defun gnus-summary-save-newsrc (&optional force)
5209   "Save the current number of read/marked articles in the dribble buffer.
5210 The dribble buffer will then be saved.
5211 If FORCE (the prefix), also save the .newsrc file(s)."
5212   (interactive "P")
5213   (gnus-summary-update-info t)
5214   (if force
5215       (gnus-save-newsrc-file)
5216     (gnus-dribble-save)))
5217
5218 (defun gnus-summary-exit (&optional temporary)
5219   "Exit reading current newsgroup, and then return to group selection mode.
5220 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5221   (interactive)
5222   (gnus-set-global-variables)
5223   (when (gnus-buffer-live-p gnus-article-buffer)
5224     (save-excursion
5225       (set-buffer gnus-article-buffer)
5226       (mm-destroy-parts gnus-article-mime-handles)))
5227   (gnus-kill-save-kill-buffer)
5228   (gnus-async-halt-prefetch)
5229   (let* ((group gnus-newsgroup-name)
5230          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5231          (mode major-mode)
5232          (group-point nil)
5233          (buf (current-buffer)))
5234     (unless quit-config
5235       ;; Do adaptive scoring, and possibly save score files.
5236       (when gnus-newsgroup-adaptive
5237         (gnus-score-adaptive))
5238       (when gnus-use-scoring
5239         (gnus-score-save)))
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     (gnus-close-group group)
5260     ;; Make sure where we were, and go to next newsgroup.
5261     (set-buffer gnus-group-buffer)
5262     (unless quit-config
5263       (gnus-group-jump-to-group group))
5264     (gnus-run-hooks 'gnus-summary-exit-hook)
5265     (unless (or quit-config
5266                 ;; If this group has disappeared from the summary
5267                 ;; buffer, don't skip forwards.
5268                 (not (string= group (gnus-group-group-name))))
5269       (gnus-group-next-unread-group 1))
5270     (setq group-point (point))
5271     (if temporary
5272         nil                             ;Nothing to do.
5273       ;; If we have several article buffers, we kill them at exit.
5274       (unless gnus-single-article-buffer
5275         (gnus-kill-buffer gnus-article-buffer)
5276         (gnus-kill-buffer gnus-original-article-buffer)
5277         (setq gnus-article-current nil))
5278       (set-buffer buf)
5279       (if (not gnus-kill-summary-on-exit)
5280           (gnus-deaden-summary)
5281         ;; We set all buffer-local variables to nil.  It is unclear why
5282         ;; this is needed, but if we don't, buffer-local variables are
5283         ;; not garbage-collected, it seems.  This would the lead to en
5284         ;; ever-growing Emacs.
5285         (gnus-summary-clear-local-variables)
5286         (when (get-buffer gnus-article-buffer)
5287           (bury-buffer gnus-article-buffer))
5288         ;; We clear the global counterparts of the buffer-local
5289         ;; variables as well, just to be on the safe side.
5290         (set-buffer gnus-group-buffer)
5291         (gnus-summary-clear-local-variables)
5292         ;; Return to group mode buffer.
5293         (when (eq mode 'gnus-summary-mode)
5294           (gnus-kill-buffer buf)))
5295       (setq gnus-current-select-method gnus-select-method)
5296       (pop-to-buffer gnus-group-buffer)
5297       (if (not quit-config)
5298           (progn
5299             (goto-char group-point)
5300             (gnus-configure-windows 'group 'force))
5301         (gnus-handle-ephemeral-exit quit-config))
5302       ;; Clear the current group name.
5303       (unless quit-config
5304         (setq gnus-newsgroup-name nil)))))
5305
5306 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5307 (defun gnus-summary-exit-no-update (&optional no-questions)
5308   "Quit reading current newsgroup without updating read article info."
5309   (interactive)
5310   (let* ((group gnus-newsgroup-name)
5311          (quit-config (gnus-group-quit-config group)))
5312     (when (or no-questions
5313               gnus-expert-user
5314               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5315       (gnus-async-halt-prefetch)
5316       (mapcar 'funcall
5317               (delq 'gnus-summary-expire-articles
5318                     (copy-list gnus-summary-prepare-exit-hook)))
5319       (when (gnus-buffer-live-p gnus-article-buffer)
5320         (save-excursion
5321           (set-buffer gnus-article-buffer)
5322           (mm-destroy-parts gnus-article-mime-handles)))
5323       ;; If we have several article buffers, we kill them at exit.
5324       (unless gnus-single-article-buffer
5325         (gnus-kill-buffer gnus-article-buffer)
5326         (gnus-kill-buffer gnus-original-article-buffer)
5327         (setq gnus-article-current nil))
5328       (if (not gnus-kill-summary-on-exit)
5329           (gnus-deaden-summary)
5330         (gnus-close-group group)
5331         (gnus-summary-clear-local-variables)
5332         (set-buffer gnus-group-buffer)
5333         (gnus-summary-clear-local-variables)
5334         (when (get-buffer gnus-summary-buffer)
5335           (kill-buffer gnus-summary-buffer)))
5336       (unless gnus-single-article-buffer
5337         (setq gnus-article-current nil))
5338       (when gnus-use-trees
5339         (gnus-tree-close group))
5340       (gnus-async-prefetch-remove-group group)
5341       (when (get-buffer gnus-article-buffer)
5342         (bury-buffer gnus-article-buffer))
5343       ;; Return to the group buffer.
5344       (gnus-configure-windows 'group 'force)
5345       ;; Clear the current group name.
5346       (setq gnus-newsgroup-name nil)
5347       (when (equal (gnus-group-group-name) group)
5348         (gnus-group-next-unread-group 1))
5349       (when quit-config
5350         (gnus-handle-ephemeral-exit quit-config)))))
5351
5352 (defun gnus-handle-ephemeral-exit (quit-config)
5353   "Handle movement when leaving an ephemeral group.
5354 The state which existed when entering the ephemeral is reset."
5355   (if (not (buffer-name (car quit-config)))
5356       (gnus-configure-windows 'group 'force)
5357     (set-buffer (car quit-config))
5358     (cond ((eq major-mode 'gnus-summary-mode)
5359            (gnus-set-global-variables))
5360           ((eq major-mode 'gnus-article-mode)
5361            (save-excursion
5362              ;; The `gnus-summary-buffer' variable may point
5363              ;; to the old summary buffer when using a single
5364              ;; article buffer.
5365              (unless (gnus-buffer-live-p gnus-summary-buffer)
5366                (set-buffer gnus-group-buffer))
5367              (set-buffer gnus-summary-buffer)
5368              (gnus-set-global-variables))))
5369     (if (or (eq (cdr quit-config) 'article)
5370             (eq (cdr quit-config) 'pick))
5371         (progn
5372           ;; The current article may be from the ephemeral group
5373           ;; thus it is best that we reload this article
5374           (gnus-summary-show-article)
5375           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5376               (gnus-configure-windows 'pick 'force)
5377             (gnus-configure-windows (cdr quit-config) 'force)))
5378       (gnus-configure-windows (cdr quit-config) 'force))
5379     (when (eq major-mode 'gnus-summary-mode)
5380       (gnus-summary-next-subject 1 nil t)
5381       (gnus-summary-recenter)
5382       (gnus-summary-position-point))))
5383
5384 ;;; Dead summaries.
5385
5386 (defvar gnus-dead-summary-mode-map nil)
5387
5388 (unless gnus-dead-summary-mode-map
5389   (setq gnus-dead-summary-mode-map (make-keymap))
5390   (suppress-keymap gnus-dead-summary-mode-map)
5391   (substitute-key-definition
5392    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5393   (let ((keys '("\C-d" "\r" "\177" [delete])))
5394     (while keys
5395       (define-key gnus-dead-summary-mode-map
5396         (pop keys) 'gnus-summary-wake-up-the-dead))))
5397
5398 (defvar gnus-dead-summary-mode nil
5399   "Minor mode for Gnus summary buffers.")
5400
5401 (defun gnus-dead-summary-mode (&optional arg)
5402   "Minor mode for Gnus summary buffers."
5403   (interactive "P")
5404   (when (eq major-mode 'gnus-summary-mode)
5405     (make-local-variable 'gnus-dead-summary-mode)
5406     (setq gnus-dead-summary-mode
5407           (if (null arg) (not gnus-dead-summary-mode)
5408             (> (prefix-numeric-value arg) 0)))
5409     (when gnus-dead-summary-mode
5410       (gnus-add-minor-mode
5411        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5412
5413 (defun gnus-deaden-summary ()
5414   "Make the current summary buffer into a dead summary buffer."
5415   ;; Kill any previous dead summary buffer.
5416   (when (and gnus-dead-summary
5417              (buffer-name gnus-dead-summary))
5418     (save-excursion
5419       (set-buffer gnus-dead-summary)
5420       (when gnus-dead-summary-mode
5421         (kill-buffer (current-buffer)))))
5422   ;; Make this the current dead summary.
5423   (setq gnus-dead-summary (current-buffer))
5424   (gnus-dead-summary-mode 1)
5425   (let ((name (buffer-name)))
5426     (when (string-match "Summary" name)
5427       (rename-buffer
5428        (concat (substring name 0 (match-beginning 0)) "Dead "
5429                (substring name (match-beginning 0)))
5430        t))))
5431
5432 (defun gnus-kill-or-deaden-summary (buffer)
5433   "Kill or deaden the summary BUFFER."
5434   (save-excursion
5435     (when (and (buffer-name buffer)
5436                (not gnus-single-article-buffer))
5437       (save-excursion
5438         (set-buffer buffer)
5439         (gnus-kill-buffer gnus-article-buffer)
5440         (gnus-kill-buffer gnus-original-article-buffer)))
5441     (cond (gnus-kill-summary-on-exit
5442            (when (and gnus-use-trees
5443                       (gnus-buffer-exists-p buffer))
5444              (save-excursion
5445                (set-buffer buffer)
5446                (gnus-tree-close gnus-newsgroup-name)))
5447            (gnus-kill-buffer buffer))
5448           ((gnus-buffer-exists-p buffer)
5449            (save-excursion
5450              (set-buffer buffer)
5451              (gnus-deaden-summary))))))
5452
5453 (defun gnus-summary-wake-up-the-dead (&rest args)
5454   "Wake up the dead summary buffer."
5455   (interactive)
5456   (gnus-dead-summary-mode -1)
5457   (let ((name (buffer-name)))
5458     (when (string-match "Dead " name)
5459       (rename-buffer
5460        (concat (substring name 0 (match-beginning 0))
5461                (substring name (match-end 0)))
5462        t)))
5463   (gnus-message 3 "This dead summary is now alive again"))
5464
5465 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5466 (defun gnus-summary-fetch-faq (&optional faq-dir)
5467   "Fetch the FAQ for the current group.
5468 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5469 in."
5470   (interactive
5471    (list
5472     (when current-prefix-arg
5473       (completing-read
5474        "Faq dir: " (and (listp gnus-group-faq-directory)
5475                         (mapcar (lambda (file) (list file))
5476                                 gnus-group-faq-directory))))))
5477   (let (gnus-faq-buffer)
5478     (when (setq gnus-faq-buffer
5479                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5480       (gnus-configure-windows 'summary-faq))))
5481
5482 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5483 (defun gnus-summary-describe-group (&optional force)
5484   "Describe the current newsgroup."
5485   (interactive "P")
5486   (gnus-group-describe-group force gnus-newsgroup-name))
5487
5488 (defun gnus-summary-describe-briefly ()
5489   "Describe summary mode commands briefly."
5490   (interactive)
5491   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5492
5493 ;; Walking around group mode buffer from summary mode.
5494
5495 (defun gnus-summary-next-group (&optional no-article target-group backward)
5496   "Exit current newsgroup and then select next unread newsgroup.
5497 If prefix argument NO-ARTICLE is non-nil, no article is selected
5498 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5499 previous group instead."
5500   (interactive "P")
5501   ;; Stop pre-fetching.
5502   (gnus-async-halt-prefetch)
5503   (let ((current-group gnus-newsgroup-name)
5504         (current-buffer (current-buffer))
5505         entered)
5506     ;; First we semi-exit this group to update Xrefs and all variables.
5507     ;; We can't do a real exit, because the window conf must remain
5508     ;; the same in case the user is prompted for info, and we don't
5509     ;; want the window conf to change before that...
5510     (gnus-summary-exit t)
5511     (while (not entered)
5512       ;; Then we find what group we are supposed to enter.
5513       (set-buffer gnus-group-buffer)
5514       (gnus-group-jump-to-group current-group)
5515       (setq target-group
5516             (or target-group
5517                 (if (eq gnus-keep-same-level 'best)
5518                     (gnus-summary-best-group gnus-newsgroup-name)
5519                   (gnus-summary-search-group backward gnus-keep-same-level))))
5520       (if (not target-group)
5521           ;; There are no further groups, so we return to the group
5522           ;; buffer.
5523           (progn
5524             (gnus-message 5 "Returning to the group buffer")
5525             (setq entered t)
5526             (when (gnus-buffer-live-p current-buffer)
5527               (set-buffer current-buffer)
5528               (gnus-summary-exit))
5529             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5530         ;; We try to enter the target group.
5531         (gnus-group-jump-to-group target-group)
5532         (let ((unreads (gnus-group-group-unread)))
5533           (if (and (or (eq t unreads)
5534                        (and unreads (not (zerop unreads))))
5535                    (gnus-summary-read-group
5536                     target-group nil no-article
5537                     (and (buffer-name current-buffer) current-buffer)
5538                     nil backward))
5539               (setq entered t)
5540             (setq current-group target-group
5541                   target-group nil)))))))
5542
5543 (defun gnus-summary-prev-group (&optional no-article)
5544   "Exit current newsgroup and then select previous unread newsgroup.
5545 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5546   (interactive "P")
5547   (gnus-summary-next-group no-article nil t))
5548
5549 ;; Walking around summary lines.
5550
5551 (defun gnus-summary-first-subject (&optional unread undownloaded)
5552   "Go to the first unread subject.
5553 If UNREAD is non-nil, go to the first unread article.
5554 Returns the article selected or nil if there are no unread articles."
5555   (interactive "P")
5556   (prog1
5557       (cond
5558        ;; Empty summary.
5559        ((null gnus-newsgroup-data)
5560         (gnus-message 3 "No articles in the group")
5561         nil)
5562        ;; Pick the first article.
5563        ((not unread)
5564         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5565         (gnus-data-number (car gnus-newsgroup-data)))
5566        ;; No unread articles.
5567        ((null gnus-newsgroup-unreads)
5568         (gnus-message 3 "No more unread articles")
5569         nil)
5570        ;; Find the first unread article.
5571        (t
5572         (let ((data gnus-newsgroup-data))
5573           (while (and data
5574                       (and (not (and undownloaded
5575                                      (eq gnus-undownloaded-mark
5576                                          (gnus-data-mark (car data)))))
5577                            (not (gnus-data-unread-p (car data)))))
5578             (setq data (cdr data)))
5579           (when data
5580             (goto-char (gnus-data-pos (car data)))
5581             (gnus-data-number (car data))))))
5582     (gnus-summary-position-point)))
5583
5584 (defun gnus-summary-next-subject (n &optional unread dont-display)
5585   "Go to next N'th summary line.
5586 If N is negative, go to the previous N'th subject line.
5587 If UNREAD is non-nil, only unread articles are selected.
5588 The difference between N and the actual number of steps taken is
5589 returned."
5590   (interactive "p")
5591   (let ((backward (< n 0))
5592         (n (abs n)))
5593     (while (and (> n 0)
5594                 (if backward
5595                     (gnus-summary-find-prev unread)
5596                   (gnus-summary-find-next unread)))
5597       (gnus-summary-show-thread)
5598       (setq n (1- n)))
5599     (when (/= 0 n)
5600       (gnus-message 7 "No more%s articles"
5601                     (if unread " unread" "")))
5602     (unless dont-display
5603       (gnus-summary-recenter)
5604       (gnus-summary-position-point))
5605     n))
5606
5607 (defun gnus-summary-next-unread-subject (n)
5608   "Go to next N'th unread summary line."
5609   (interactive "p")
5610   (gnus-summary-next-subject n t))
5611
5612 (defun gnus-summary-prev-subject (n &optional unread)
5613   "Go to previous N'th summary line.
5614 If optional argument UNREAD is non-nil, only unread article is selected."
5615   (interactive "p")
5616   (gnus-summary-next-subject (- n) unread))
5617
5618 (defun gnus-summary-prev-unread-subject (n)
5619   "Go to previous N'th unread summary line."
5620   (interactive "p")
5621   (gnus-summary-next-subject (- n) t))
5622
5623 (defun gnus-summary-goto-subject (article &optional force silent)
5624   "Go the subject line of ARTICLE.
5625 If FORCE, also allow jumping to articles not currently shown."
5626   (interactive "nArticle number: ")
5627   (let ((b (point))
5628         (data (gnus-data-find article)))
5629     ;; We read in the article if we have to.
5630     (and (not data)
5631          force
5632          (gnus-summary-insert-subject
5633           article
5634           (if (or (numberp force) (vectorp force)) force)
5635           t)
5636          (setq data (gnus-data-find article)))
5637     (goto-char b)
5638     (if (not data)
5639         (progn
5640           (unless silent
5641             (gnus-message 3 "Can't find article %d" article))
5642           nil)
5643       (goto-char (gnus-data-pos data))
5644       (gnus-summary-position-point)
5645       article)))
5646
5647 ;; Walking around summary lines with displaying articles.
5648
5649 (defun gnus-summary-expand-window (&optional arg)
5650   "Make the summary buffer take up the entire Emacs frame.
5651 Given a prefix, will force an `article' buffer configuration."
5652   (interactive "P")
5653   (if arg
5654       (gnus-configure-windows 'article 'force)
5655     (gnus-configure-windows 'summary 'force)))
5656
5657 (defun gnus-summary-display-article (article &optional all-header)
5658   "Display ARTICLE in article buffer."
5659   (gnus-set-global-variables)
5660   (if (null article)
5661       nil
5662     (prog1
5663         (if gnus-summary-display-article-function
5664             (funcall gnus-summary-display-article-function article all-header)
5665           (gnus-article-prepare article all-header))
5666       (gnus-run-hooks 'gnus-select-article-hook)
5667       (when (and gnus-current-article
5668                  (not (zerop gnus-current-article)))
5669         (gnus-summary-goto-subject gnus-current-article))
5670       (gnus-summary-recenter)
5671       (when (and gnus-use-trees gnus-show-threads)
5672         (gnus-possibly-generate-tree article)
5673         (gnus-highlight-selected-tree article))
5674       ;; Successfully display article.
5675       (gnus-article-set-window-start
5676        (cdr (assq article gnus-newsgroup-bookmarks))))))
5677
5678 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5679   "Select the current article.
5680 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5681 non-nil, the article will be re-fetched even if it already present in
5682 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5683 be displayed."
5684   ;; Make sure we are in the summary buffer to work around bbdb bug.
5685   (unless (eq major-mode 'gnus-summary-mode)
5686     (set-buffer gnus-summary-buffer))
5687   (let ((article (or article (gnus-summary-article-number)))
5688         (all-headers (not (not all-headers))) ;Must be T or NIL.
5689         gnus-summary-display-article-function
5690         did)
5691     (and (not pseudo)
5692          (gnus-summary-article-pseudo-p article)
5693          (error "This is a pseudo-article"))
5694     (prog1
5695         (save-excursion
5696           (set-buffer gnus-summary-buffer)
5697           (if (or (and gnus-single-article-buffer
5698                        (or (null gnus-current-article)
5699                            (null gnus-article-current)
5700                            (null (get-buffer gnus-article-buffer))
5701                            (not (eq article (cdr gnus-article-current)))
5702                            (not (equal (car gnus-article-current)
5703                                        gnus-newsgroup-name))))
5704                   (and (not gnus-single-article-buffer)
5705                        (or (null gnus-current-article)
5706                            (not (eq gnus-current-article article))))
5707                   force)
5708               ;; The requested article is different from the current article.
5709               (prog1
5710                   (gnus-summary-display-article article all-headers)
5711                 (setq did article)
5712                 (when (or all-headers gnus-show-all-headers)
5713                   (gnus-article-show-all-headers)))
5714             (when (or all-headers gnus-show-all-headers)
5715               (gnus-article-show-all-headers))
5716             'old))
5717       (when did
5718         (gnus-article-set-window-start
5719          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5720
5721 (defun gnus-summary-set-current-mark (&optional current-mark)
5722   "Obsolete function."
5723   nil)
5724
5725 (defun gnus-summary-next-article (&optional unread subject backward push)
5726   "Select the next article.
5727 If UNREAD, only unread articles are selected.
5728 If SUBJECT, only articles with SUBJECT are selected.
5729 If BACKWARD, the previous article is selected instead of the next."
5730   (interactive "P")
5731   (cond
5732    ;; Is there such an article?
5733    ((and (gnus-summary-search-forward unread subject backward)
5734          (or (gnus-summary-display-article (gnus-summary-article-number))
5735              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5736     (gnus-summary-position-point))
5737    ;; If not, we try the first unread, if that is wanted.
5738    ((and subject
5739          gnus-auto-select-same
5740          (gnus-summary-first-unread-article))
5741     (gnus-summary-position-point)
5742     (gnus-message 6 "Wrapped"))
5743    ;; Try to get next/previous article not displayed in this group.
5744    ((and gnus-auto-extend-newsgroup
5745          (not unread) (not subject))
5746     (gnus-summary-goto-article
5747      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5748      nil (count-lines (point-min) (point))))
5749    ;; Go to next/previous group.
5750    (t
5751     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5752       (gnus-summary-jump-to-group gnus-newsgroup-name))
5753     (let ((cmd last-command-char)
5754           (point
5755            (save-excursion
5756              (set-buffer gnus-group-buffer)
5757              (point)))
5758           (group
5759            (if (eq gnus-keep-same-level 'best)
5760                (gnus-summary-best-group gnus-newsgroup-name)
5761              (gnus-summary-search-group backward gnus-keep-same-level))))
5762       ;; For some reason, the group window gets selected.  We change
5763       ;; it back.
5764       (select-window (get-buffer-window (current-buffer)))
5765       ;; Select next unread newsgroup automagically.
5766       (cond
5767        ((or (not gnus-auto-select-next)
5768             (not cmd))
5769         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5770        ((or (eq gnus-auto-select-next 'quietly)
5771             (and (eq gnus-auto-select-next 'slightly-quietly)
5772                  push)
5773             (and (eq gnus-auto-select-next 'almost-quietly)
5774                  (gnus-summary-last-article-p)))
5775         ;; Select quietly.
5776         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5777             (gnus-summary-exit)
5778           (gnus-message 7 "No more%s articles (%s)..."
5779                         (if unread " unread" "")
5780                         (if group (concat "selecting " group)
5781                           "exiting"))
5782           (gnus-summary-next-group nil group backward)))
5783        (t
5784         (when (gnus-key-press-event-p last-input-event)
5785           (gnus-summary-walk-group-buffer
5786            gnus-newsgroup-name cmd unread backward point))))))))
5787
5788 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5789   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5790                       (?\C-p (gnus-group-prev-unread-group 1))))
5791         (cursor-in-echo-area t)
5792         keve key group ended)
5793     (save-excursion
5794       (set-buffer gnus-group-buffer)
5795       (goto-char start)
5796       (setq group
5797             (if (eq gnus-keep-same-level 'best)
5798                 (gnus-summary-best-group gnus-newsgroup-name)
5799               (gnus-summary-search-group backward gnus-keep-same-level))))
5800     (while (not ended)
5801       (gnus-message
5802        5 "No more%s articles%s" (if unread " unread" "")
5803        (if (and group
5804                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5805            (format " (Type %s for %s [%s])"
5806                    (single-key-description cmd) group
5807                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5808          (format " (Type %s to exit %s)"
5809                  (single-key-description cmd)
5810                  gnus-newsgroup-name)))
5811       ;; Confirm auto selection.
5812       (setq key (car (setq keve (gnus-read-event-char))))
5813       (setq ended t)
5814       (cond
5815        ((assq key keystrokes)
5816         (let ((obuf (current-buffer)))
5817           (switch-to-buffer gnus-group-buffer)
5818           (when group
5819             (gnus-group-jump-to-group group))
5820           (eval (cadr (assq key keystrokes)))
5821           (setq group (gnus-group-group-name))
5822           (switch-to-buffer obuf))
5823         (setq ended nil))
5824        ((equal key cmd)
5825         (if (or (not group)
5826                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5827             (gnus-summary-exit)
5828           (gnus-summary-next-group nil group backward)))
5829        (t
5830         (push (cdr keve) unread-command-events))))))
5831
5832 (defun gnus-summary-next-unread-article ()
5833   "Select unread article after current one."
5834   (interactive)
5835   (gnus-summary-next-article
5836    (or (not (eq gnus-summary-goto-unread 'never))
5837        (gnus-summary-last-article-p (gnus-summary-article-number)))
5838    (and gnus-auto-select-same
5839         (gnus-summary-article-subject))))
5840
5841 (defun gnus-summary-prev-article (&optional unread subject)
5842   "Select the article after the current one.
5843 If UNREAD is non-nil, only unread articles are selected."
5844   (interactive "P")
5845   (gnus-summary-next-article unread subject t))
5846
5847 (defun gnus-summary-prev-unread-article ()
5848   "Select unread article before current one."
5849   (interactive)
5850   (gnus-summary-prev-article
5851    (or (not (eq gnus-summary-goto-unread 'never))
5852        (gnus-summary-first-article-p (gnus-summary-article-number)))
5853    (and gnus-auto-select-same
5854         (gnus-summary-article-subject))))
5855
5856 (defun gnus-summary-next-page (&optional lines circular)
5857   "Show next page of the selected article.
5858 If at the end of the current article, select the next article.
5859 LINES says how many lines should be scrolled up.
5860
5861 If CIRCULAR is non-nil, go to the start of the article instead of
5862 selecting the next article when reaching the end of the current
5863 article."
5864   (interactive "P")
5865   (setq gnus-summary-buffer (current-buffer))
5866   (gnus-set-global-variables)
5867   (let ((article (gnus-summary-article-number))
5868         (article-window (get-buffer-window gnus-article-buffer t))
5869         endp)
5870     ;; If the buffer is empty, we have no article.
5871     (unless article
5872       (error "No article to select"))
5873     (gnus-configure-windows 'article)
5874     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5875         (if (and (eq gnus-summary-goto-unread 'never)
5876                  (not (gnus-summary-last-article-p article)))
5877             (gnus-summary-next-article)
5878           (gnus-summary-next-unread-article))
5879       (if (or (null gnus-current-article)
5880               (null gnus-article-current)
5881               (/= article (cdr gnus-article-current))
5882               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5883           ;; Selected subject is different from current article's.
5884           (gnus-summary-display-article article)
5885         (when article-window
5886           (gnus-eval-in-buffer-window gnus-article-buffer
5887             (setq endp (gnus-article-next-page lines)))
5888           (when endp
5889             (cond (circular
5890                    (gnus-summary-beginning-of-article))
5891                   (lines
5892                    (gnus-message 3 "End of message"))
5893                   ((null lines)
5894                    (if (and (eq gnus-summary-goto-unread 'never)
5895                             (not (gnus-summary-last-article-p article)))
5896                        (gnus-summary-next-article)
5897                      (gnus-summary-next-unread-article))))))))
5898     (gnus-summary-recenter)
5899     (gnus-summary-position-point)))
5900
5901 (defun gnus-summary-prev-page (&optional lines move)
5902   "Show previous page of selected article.
5903 Argument LINES specifies lines to be scrolled down.
5904 If MOVE, move to the previous unread article if point is at
5905 the beginning of the buffer."
5906   (interactive "P")
5907   (let ((article (gnus-summary-article-number))
5908         (article-window (get-buffer-window gnus-article-buffer t))
5909         endp)
5910     (gnus-configure-windows 'article)
5911     (if (or (null gnus-current-article)
5912             (null gnus-article-current)
5913             (/= article (cdr gnus-article-current))
5914             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5915         ;; Selected subject is different from current article's.
5916         (gnus-summary-display-article article)
5917       (gnus-summary-recenter)
5918       (when article-window
5919         (gnus-eval-in-buffer-window gnus-article-buffer
5920           (setq endp (gnus-article-prev-page lines)))
5921         (when (and move endp)
5922           (cond (lines
5923                  (gnus-message 3 "Beginning of message"))
5924                 ((null lines)
5925                  (if (and (eq gnus-summary-goto-unread 'never)
5926                           (not (gnus-summary-first-article-p article)))
5927                      (gnus-summary-prev-article)
5928                    (gnus-summary-prev-unread-article))))))))
5929   (gnus-summary-position-point))
5930
5931 (defun gnus-summary-prev-page-or-article (&optional lines)
5932   "Show previous page of selected article.
5933 Argument LINES specifies lines to be scrolled down.
5934 If at the beginning of the article, go to the next article."
5935   (interactive "P")
5936   (gnus-summary-prev-page lines t))
5937
5938 (defun gnus-summary-scroll-up (lines)
5939   "Scroll up (or down) one line current article.
5940 Argument LINES specifies lines to be scrolled up (or down if negative)."
5941   (interactive "p")
5942   (gnus-configure-windows 'article)
5943   (gnus-summary-show-thread)
5944   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5945     (gnus-eval-in-buffer-window gnus-article-buffer
5946       (cond ((> lines 0)
5947              (when (gnus-article-next-page lines)
5948                (gnus-message 3 "End of message")))
5949             ((< lines 0)
5950              (gnus-article-prev-page (- lines))))))
5951   (gnus-summary-recenter)
5952   (gnus-summary-position-point))
5953
5954 (defun gnus-summary-scroll-down (lines)
5955   "Scroll down (or up) one line current article.
5956 Argument LINES specifies lines to be scrolled down (or up if negative)."
5957   (interactive "p")
5958   (gnus-summary-scroll-up (- lines)))
5959
5960 (defun gnus-summary-next-same-subject ()
5961   "Select next article which has the same subject as current one."
5962   (interactive)
5963   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5964
5965 (defun gnus-summary-prev-same-subject ()
5966   "Select previous article which has the same subject as current one."
5967   (interactive)
5968   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5969
5970 (defun gnus-summary-next-unread-same-subject ()
5971   "Select next unread article which has the same subject as current one."
5972   (interactive)
5973   (gnus-summary-next-article t (gnus-summary-article-subject)))
5974
5975 (defun gnus-summary-prev-unread-same-subject ()
5976   "Select previous unread article which has the same subject as current one."
5977   (interactive)
5978   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5979
5980 (defun gnus-summary-first-unread-article ()
5981   "Select the first unread article.
5982 Return nil if there are no unread articles."
5983   (interactive)
5984   (prog1
5985       (when (gnus-summary-first-subject t)
5986         (gnus-summary-show-thread)
5987         (gnus-summary-first-subject t)
5988         (gnus-summary-display-article (gnus-summary-article-number)))
5989     (gnus-summary-position-point)))
5990
5991 (defun gnus-summary-first-unread-subject ()
5992   "Place the point on the subject line of the first unread article.
5993 Return nil if there are no unread articles."
5994   (interactive)
5995   (prog1
5996       (when (gnus-summary-first-subject t)
5997         (gnus-summary-show-thread)
5998         (gnus-summary-first-subject t))
5999     (gnus-summary-position-point)))
6000
6001 (defun gnus-summary-first-article ()
6002   "Select the first article.
6003 Return nil if there are no articles."
6004   (interactive)
6005   (prog1
6006       (when (gnus-summary-first-subject)
6007         (gnus-summary-show-thread)
6008         (gnus-summary-first-subject)
6009         (gnus-summary-display-article (gnus-summary-article-number)))
6010     (gnus-summary-position-point)))
6011
6012 (defun gnus-summary-best-unread-article ()
6013   "Select the unread article with the highest score."
6014   (interactive)
6015   (let ((best -1000000)
6016         (data gnus-newsgroup-data)
6017         article score)
6018     (while data
6019       (and (gnus-data-unread-p (car data))
6020            (> (setq score
6021                     (gnus-summary-article-score (gnus-data-number (car data))))
6022               best)
6023            (setq best score
6024                  article (gnus-data-number (car data))))
6025       (setq data (cdr data)))
6026     (prog1
6027         (if article
6028             (gnus-summary-goto-article article)
6029           (error "No unread articles"))
6030       (gnus-summary-position-point))))
6031
6032 (defun gnus-summary-last-subject ()
6033   "Go to the last displayed subject line in the group."
6034   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6035     (when article
6036       (gnus-summary-goto-subject article))))
6037
6038 (defun gnus-summary-goto-article (article &optional all-headers force)
6039   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6040 If ALL-HEADERS is non-nil, no header lines are hidden.
6041 If FORCE, go to the article even if it isn't displayed.  If FORCE
6042 is a number, it is the line the article is to be displayed on."
6043   (interactive
6044    (list
6045     (completing-read
6046      "Article number or Message-ID: "
6047      (mapcar (lambda (number) (list (int-to-string number)))
6048              gnus-newsgroup-limit))
6049     current-prefix-arg
6050     t))
6051   (prog1
6052       (if (and (stringp article)
6053                (string-match "@" article))
6054           (gnus-summary-refer-article article)
6055         (when (stringp article)
6056           (setq article (string-to-number article)))
6057         (if (gnus-summary-goto-subject article force)
6058             (gnus-summary-display-article article all-headers)
6059           (gnus-message 4 "Couldn't go to article %s" article) nil))
6060     (gnus-summary-position-point)))
6061
6062 (defun gnus-summary-goto-last-article ()
6063   "Go to the previously read article."
6064   (interactive)
6065   (prog1
6066       (when gnus-last-article
6067         (gnus-summary-goto-article gnus-last-article nil t))
6068     (gnus-summary-position-point)))
6069
6070 (defun gnus-summary-pop-article (number)
6071   "Pop one article off the history and go to the previous.
6072 NUMBER articles will be popped off."
6073   (interactive "p")
6074   (let (to)
6075     (setq gnus-newsgroup-history
6076           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6077     (if to
6078         (gnus-summary-goto-article (car to) nil t)
6079       (error "Article history empty")))
6080   (gnus-summary-position-point))
6081
6082 ;; Summary commands and functions for limiting the summary buffer.
6083
6084 (defun gnus-summary-limit-to-articles (n)
6085   "Limit the summary buffer to the next N articles.
6086 If not given a prefix, use the process marked articles instead."
6087   (interactive "P")
6088   (prog1
6089       (let ((articles (gnus-summary-work-articles n)))
6090         (setq gnus-newsgroup-processable nil)
6091         (gnus-summary-limit articles))
6092     (gnus-summary-position-point)))
6093
6094 (defun gnus-summary-pop-limit (&optional total)
6095   "Restore the previous limit.
6096 If given a prefix, remove all limits."
6097   (interactive "P")
6098   (when total
6099     (setq gnus-newsgroup-limits
6100           (list (mapcar (lambda (h) (mail-header-number h))
6101                         gnus-newsgroup-headers))))
6102   (unless gnus-newsgroup-limits
6103     (error "No limit to pop"))
6104   (prog1
6105       (gnus-summary-limit nil 'pop)
6106     (gnus-summary-position-point)))
6107
6108 (defun gnus-summary-limit-to-subject (subject &optional header)
6109   "Limit the summary buffer to articles that have subjects that match a regexp."
6110   (interactive "sLimit to subject (regexp): ")
6111   (unless header
6112     (setq header "subject"))
6113   (when (not (equal "" subject))
6114     (prog1
6115         (let ((articles (gnus-summary-find-matching
6116                          (or header "subject") subject 'all)))
6117           (unless articles
6118             (error "Found no matches for \"%s\"" subject))
6119           (gnus-summary-limit articles))
6120       (gnus-summary-position-point))))
6121
6122 (defun gnus-summary-limit-to-author (from)
6123   "Limit the summary buffer to articles that have authors that match a regexp."
6124   (interactive "sLimit to author (regexp): ")
6125   (gnus-summary-limit-to-subject from "from"))
6126
6127 (defun gnus-summary-limit-to-age (age &optional younger-p)
6128   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6129 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6130 articles that are younger than AGE days."
6131   (interactive "nTime in days: \nP")
6132   (prog1
6133       (let ((data gnus-newsgroup-data)
6134             (cutoff (days-to-time age))
6135             articles d date is-younger)
6136         (while (setq d (pop data))
6137           (when (and (vectorp (gnus-data-header d))
6138                      (setq date (mail-header-date (gnus-data-header d))))
6139             (setq is-younger (time-less-p
6140                               (time-since (date-to-time date))
6141                               cutoff))
6142             (when (if younger-p
6143                       is-younger
6144                     (not is-younger))
6145               (push (gnus-data-number d) articles))))
6146         (gnus-summary-limit (nreverse articles)))
6147     (gnus-summary-position-point)))
6148
6149 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6150 (make-obsolete
6151  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6152
6153 (defun gnus-summary-limit-to-unread (&optional all)
6154   "Limit the summary buffer to articles that are not marked as read.
6155 If ALL is non-nil, limit strictly to unread articles."
6156   (interactive "P")
6157   (if all
6158       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6159     (gnus-summary-limit-to-marks
6160      ;; Concat all the marks that say that an article is read and have
6161      ;; those removed.
6162      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6163            gnus-killed-mark gnus-kill-file-mark
6164            gnus-low-score-mark gnus-expirable-mark
6165            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6166            gnus-duplicate-mark gnus-souped-mark)
6167      'reverse)))
6168
6169 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6170 (make-obsolete 'gnus-summary-delete-marked-with
6171                'gnus-summary-limit-exlude-marks)
6172
6173 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6174   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6175 If REVERSE, limit the summary buffer to articles that are marked
6176 with MARKS.  MARKS can either be a string of marks or a list of marks.
6177 Returns how many articles were removed."
6178   (interactive "sMarks: ")
6179   (gnus-summary-limit-to-marks marks t))
6180
6181 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6182   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6183 If REVERSE (the prefix), limit the summary buffer to articles that are
6184 not marked with MARKS.  MARKS can either be a string of marks or a
6185 list of marks.
6186 Returns how many articles were removed."
6187   (interactive "sMarks: \nP")
6188   (prog1
6189       (let ((data gnus-newsgroup-data)
6190             (marks (if (listp marks) marks
6191                      (append marks nil))) ; Transform to list.
6192             articles)
6193         (while data
6194           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6195                   (memq (gnus-data-mark (car data)) marks))
6196             (push (gnus-data-number (car data)) articles))
6197           (setq data (cdr data)))
6198         (gnus-summary-limit articles))
6199     (gnus-summary-position-point)))
6200
6201 (defun gnus-summary-limit-to-score (&optional score)
6202   "Limit to articles with score at or above SCORE."
6203   (interactive "P")
6204   (setq score (if score
6205                   (prefix-numeric-value score)
6206                 (or gnus-summary-default-score 0)))
6207   (let ((data gnus-newsgroup-data)
6208         articles)
6209     (while data
6210       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6211                 score)
6212         (push (gnus-data-number (car data)) articles))
6213       (setq data (cdr data)))
6214     (prog1
6215         (gnus-summary-limit articles)
6216       (gnus-summary-position-point))))
6217
6218 (defun gnus-summary-limit-include-thread (id)
6219   "Display all the hidden articles that in the current thread."
6220   (interactive (list (mail-header-id (gnus-summary-article-header))))
6221   (let ((articles (gnus-articles-in-thread
6222                    (gnus-id-to-thread (gnus-root-id id)))))
6223     (prog1
6224         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6225       (gnus-summary-position-point))))
6226
6227 (defun gnus-summary-limit-include-dormant ()
6228   "Display all the hidden articles that are marked as dormant.
6229 Note that this command only works on a subset of the articles currently
6230 fetched for this group."
6231   (interactive)
6232   (unless gnus-newsgroup-dormant
6233     (error "There are no dormant articles in this group"))
6234   (prog1
6235       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6236     (gnus-summary-position-point)))
6237
6238 (defun gnus-summary-limit-exclude-dormant ()
6239   "Hide all dormant articles."
6240   (interactive)
6241   (prog1
6242       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6243     (gnus-summary-position-point)))
6244
6245 (defun gnus-summary-limit-exclude-childless-dormant ()
6246   "Hide all dormant articles that have no children."
6247   (interactive)
6248   (let ((data (gnus-data-list t))
6249         articles d children)
6250     ;; Find all articles that are either not dormant or have
6251     ;; children.
6252     (while (setq d (pop data))
6253       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6254                 (and (setq children
6255                            (gnus-article-children (gnus-data-number d)))
6256                      (let (found)
6257                        (while children
6258                          (when (memq (car children) articles)
6259                            (setq children nil
6260                                  found t))
6261                          (pop children))
6262                        found)))
6263         (push (gnus-data-number d) articles)))
6264     ;; Do the limiting.
6265     (prog1
6266         (gnus-summary-limit articles)
6267       (gnus-summary-position-point))))
6268
6269 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6270   "Mark all unread excluded articles as read.
6271 If ALL, mark even excluded ticked and dormants as read."
6272   (interactive "P")
6273   (let ((articles (gnus-sorted-complement
6274                    (sort
6275                     (mapcar (lambda (h) (mail-header-number h))
6276                             gnus-newsgroup-headers)
6277                     '<)
6278                    (sort gnus-newsgroup-limit '<)))
6279         article)
6280     (setq gnus-newsgroup-unreads
6281           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6282     (if all
6283         (setq gnus-newsgroup-dormant nil
6284               gnus-newsgroup-marked nil
6285               gnus-newsgroup-reads
6286               (nconc
6287                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6288                gnus-newsgroup-reads))
6289       (while (setq article (pop articles))
6290         (unless (or (memq article gnus-newsgroup-dormant)
6291                     (memq article gnus-newsgroup-marked))
6292           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6293
6294 (defun gnus-summary-limit (articles &optional pop)
6295   (if pop
6296       ;; We pop the previous limit off the stack and use that.
6297       (setq articles (car gnus-newsgroup-limits)
6298             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6299     ;; We use the new limit, so we push the old limit on the stack.
6300     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6301   ;; Set the limit.
6302   (setq gnus-newsgroup-limit articles)
6303   (let ((total (length gnus-newsgroup-data))
6304         (data (gnus-data-find-list (gnus-summary-article-number)))
6305         (gnus-summary-mark-below nil)   ; Inhibit this.
6306         found)
6307     ;; This will do all the work of generating the new summary buffer
6308     ;; according to the new limit.
6309     (gnus-summary-prepare)
6310     ;; Hide any threads, possibly.
6311     (and gnus-show-threads
6312          gnus-thread-hide-subtree
6313          (gnus-summary-hide-all-threads))
6314     ;; Try to return to the article you were at, or one in the
6315     ;; neighborhood.
6316     (when data
6317       ;; We try to find some article after the current one.
6318       (while data
6319         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6320           (setq data nil
6321                 found t))
6322         (setq data (cdr data))))
6323     (unless found
6324       ;; If there is no data, that means that we were after the last
6325       ;; article.  The same goes when we can't find any articles
6326       ;; after the current one.
6327       (goto-char (point-max))
6328       (gnus-summary-find-prev))
6329     (gnus-set-mode-line 'summary)
6330     ;; We return how many articles were removed from the summary
6331     ;; buffer as a result of the new limit.
6332     (- total (length gnus-newsgroup-data))))
6333
6334 (defsubst gnus-invisible-cut-children (threads)
6335   (let ((num 0))
6336     (while threads
6337       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6338         (incf num))
6339       (pop threads))
6340     (< num 2)))
6341
6342 (defsubst gnus-cut-thread (thread)
6343   "Go forwards in the thread until we find an article that we want to display."
6344   (when (or (eq gnus-fetch-old-headers 'some)
6345             (eq gnus-fetch-old-headers 'invisible)
6346             (eq gnus-build-sparse-threads 'some)
6347             (eq gnus-build-sparse-threads 'more))
6348     ;; Deal with old-fetched headers and sparse threads.
6349     (while (and
6350             thread
6351             (or
6352              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6353              (gnus-summary-article-ancient-p
6354               (mail-header-number (car thread))))
6355             (if (or (<= (length (cdr thread)) 1)
6356                     (eq gnus-fetch-old-headers 'invisible))
6357                 (setq gnus-newsgroup-limit
6358                       (delq (mail-header-number (car thread))
6359                             gnus-newsgroup-limit)
6360                       thread (cadr thread))
6361               (when (gnus-invisible-cut-children (cdr thread))
6362                 (let ((th (cdr thread)))
6363                   (while th
6364                     (if (memq (mail-header-number (caar th))
6365                               gnus-newsgroup-limit)
6366                         (setq thread (car th)
6367                               th nil)
6368                       (setq th (cdr th))))))))))
6369   thread)
6370
6371 (defun gnus-cut-threads (threads)
6372   "Cut off all uninteresting articles from the beginning of threads."
6373   (when (or (eq gnus-fetch-old-headers 'some)
6374             (eq gnus-fetch-old-headers 'invisible)
6375             (eq gnus-build-sparse-threads 'some)
6376             (eq gnus-build-sparse-threads 'more))
6377     (let ((th threads))
6378       (while th
6379         (setcar th (gnus-cut-thread (car th)))
6380         (setq th (cdr th)))))
6381   ;; Remove nixed out threads.
6382   (delq nil threads))
6383
6384 (defun gnus-summary-initial-limit (&optional show-if-empty)
6385   "Figure out what the initial limit is supposed to be on group entry.
6386 This entails weeding out unwanted dormants, low-scored articles,
6387 fetch-old-headers verbiage, and so on."
6388   ;; Most groups have nothing to remove.
6389   (if (or gnus-inhibit-limiting
6390           (and (null gnus-newsgroup-dormant)
6391                (not (eq gnus-fetch-old-headers 'some))
6392                (not (eq gnus-fetch-old-headers 'invisible))
6393                (null gnus-summary-expunge-below)
6394                (not (eq gnus-build-sparse-threads 'some))
6395                (not (eq gnus-build-sparse-threads 'more))
6396                (null gnus-thread-expunge-below)
6397                (not gnus-use-nocem)))
6398       ()                                ; Do nothing.
6399     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6400     (setq gnus-newsgroup-limit nil)
6401     (mapatoms
6402      (lambda (node)
6403        (unless (car (symbol-value node))
6404          ;; These threads have no parents -- they are roots.
6405          (let ((nodes (cdr (symbol-value node)))
6406                thread)
6407            (while nodes
6408              (if (and gnus-thread-expunge-below
6409                       (< (gnus-thread-total-score (car nodes))
6410                          gnus-thread-expunge-below))
6411                  (gnus-expunge-thread (pop nodes))
6412                (setq thread (pop nodes))
6413                (gnus-summary-limit-children thread))))))
6414      gnus-newsgroup-dependencies)
6415     ;; If this limitation resulted in an empty group, we might
6416     ;; pop the previous limit and use it instead.
6417     (when (and (not gnus-newsgroup-limit)
6418                show-if-empty)
6419       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6420     gnus-newsgroup-limit))
6421
6422 (defun gnus-summary-limit-children (thread)
6423   "Return 1 if this subthread is visible and 0 if it is not."
6424   ;; First we get the number of visible children to this thread.  This
6425   ;; is done by recursing down the thread using this function, so this
6426   ;; will really go down to a leaf article first, before slowly
6427   ;; working its way up towards the root.
6428   (when thread
6429     (let ((children
6430            (if (cdr thread)
6431                (apply '+ (mapcar 'gnus-summary-limit-children
6432                                  (cdr thread)))
6433              0))
6434           (number (mail-header-number (car thread)))
6435           score)
6436       (if (and
6437            (not (memq number gnus-newsgroup-marked))
6438            (or
6439             ;; If this article is dormant and has absolutely no visible
6440             ;; children, then this article isn't visible.
6441             (and (memq number gnus-newsgroup-dormant)
6442                  (zerop children))
6443             ;; If this is "fetch-old-headered" and there is no
6444             ;; visible children, then we don't want this article.
6445             (and (eq gnus-fetch-old-headers 'some)
6446                  (gnus-summary-article-ancient-p number)
6447                  (zerop children))
6448             ;; If this is "fetch-old-headered" and `invisible', then
6449             ;; we don't want this article.
6450             (and (eq gnus-fetch-old-headers 'invisible)
6451                  (gnus-summary-article-ancient-p number))
6452             ;; If this is a sparsely inserted article with no children,
6453             ;; we don't want it.
6454             (and (eq gnus-build-sparse-threads 'some)
6455                  (gnus-summary-article-sparse-p number)
6456                  (zerop children))
6457             ;; If we use expunging, and this article is really
6458             ;; low-scored, then we don't want this article.
6459             (when (and gnus-summary-expunge-below
6460                        (< (setq score
6461                                 (or (cdr (assq number gnus-newsgroup-scored))
6462                                     gnus-summary-default-score))
6463                           gnus-summary-expunge-below))
6464               ;; We increase the expunge-tally here, but that has
6465               ;; nothing to do with the limits, really.
6466               (incf gnus-newsgroup-expunged-tally)
6467               ;; We also mark as read here, if that's wanted.
6468               (when (and gnus-summary-mark-below
6469                          (< score gnus-summary-mark-below))
6470                 (setq gnus-newsgroup-unreads
6471                       (delq number gnus-newsgroup-unreads))
6472                 (if gnus-newsgroup-auto-expire
6473                     (push number gnus-newsgroup-expirable)
6474                   (push (cons number gnus-low-score-mark)
6475                         gnus-newsgroup-reads)))
6476               t)
6477             ;; Check NoCeM things.
6478             (if (and gnus-use-nocem
6479                      (gnus-nocem-unwanted-article-p
6480                       (mail-header-id (car thread))))
6481                 (progn
6482                   (setq gnus-newsgroup-unreads
6483                         (delq number gnus-newsgroup-unreads))
6484                   t))))
6485           ;; Nope, invisible article.
6486           0
6487         ;; Ok, this article is to be visible, so we add it to the limit
6488         ;; and return 1.
6489         (push number gnus-newsgroup-limit)
6490         1))))
6491
6492 (defun gnus-expunge-thread (thread)
6493   "Mark all articles in THREAD as read."
6494   (let* ((number (mail-header-number (car thread))))
6495     (incf gnus-newsgroup-expunged-tally)
6496     ;; We also mark as read here, if that's wanted.
6497     (setq gnus-newsgroup-unreads
6498           (delq number gnus-newsgroup-unreads))
6499     (if gnus-newsgroup-auto-expire
6500         (push number gnus-newsgroup-expirable)
6501       (push (cons number gnus-low-score-mark)
6502             gnus-newsgroup-reads)))
6503   ;; Go recursively through all subthreads.
6504   (mapcar 'gnus-expunge-thread (cdr thread)))
6505
6506 ;; Summary article oriented commands
6507
6508 (defun gnus-summary-refer-parent-article (n)
6509   "Refer parent article N times.
6510 If N is negative, go to ancestor -N instead.
6511 The difference between N and the number of articles fetched is returned."
6512   (interactive "p")
6513   (let ((skip 1)
6514         error header ref)
6515     (when (not (natnump n))
6516       (setq skip (abs n)
6517             n 1))
6518     (while (and (> n 0)
6519                 (not error))
6520       (setq header (gnus-summary-article-header))
6521       (if (and (eq (mail-header-number header)
6522                    (cdr gnus-article-current))
6523                (equal gnus-newsgroup-name
6524                       (car gnus-article-current)))
6525           ;; If we try to find the parent of the currently
6526           ;; displayed article, then we take a look at the actual
6527           ;; References header, since this is slightly more
6528           ;; reliable than the References field we got from the
6529           ;; server.
6530           (save-excursion
6531             (set-buffer gnus-original-article-buffer)
6532             (nnheader-narrow-to-headers)
6533             (unless (setq ref (message-fetch-field "references"))
6534               (setq ref (message-fetch-field "in-reply-to")))
6535             (widen))
6536         (setq ref
6537               ;; It's not the current article, so we take a bet on
6538               ;; the value we got from the server.
6539               (mail-header-references header)))
6540       (if (and ref
6541                (not (equal ref "")))
6542           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6543             (gnus-message 1 "Couldn't find parent"))
6544         (gnus-message 1 "No references in article %d"
6545                       (gnus-summary-article-number))
6546         (setq error t))
6547       (decf n))
6548     (gnus-summary-position-point)
6549     n))
6550
6551 (defun gnus-summary-refer-references ()
6552   "Fetch all articles mentioned in the References header.
6553 Return the number of articles fetched."
6554   (interactive)
6555   (let ((ref (mail-header-references (gnus-summary-article-header)))
6556         (current (gnus-summary-article-number))
6557         (n 0))
6558     (if (or (not ref)
6559             (equal ref ""))
6560         (error "No References in the current article")
6561       ;; For each Message-ID in the References header...
6562       (while (string-match "<[^>]*>" ref)
6563         (incf n)
6564         ;; ... fetch that article.
6565         (gnus-summary-refer-article
6566          (prog1 (match-string 0 ref)
6567            (setq ref (substring ref (match-end 0))))))
6568       (gnus-summary-goto-subject current)
6569       (gnus-summary-position-point)
6570       n)))
6571
6572 (defun gnus-summary-refer-thread (&optional limit)
6573   "Fetch all articles in the current thread.
6574 If LIMIT (the numerical prefix), fetch that many old headers instead
6575 of what's specified by the `gnus-refer-thread-limit' variable."
6576   (interactive "P")
6577   (let ((id (mail-header-id (gnus-summary-article-header)))
6578         (limit (if limit (prefix-numeric-value limit)
6579                  gnus-refer-thread-limit)))
6580     ;; We want to fetch LIMIT *old* headers, but we also have to
6581     ;; re-fetch all the headers in the current buffer, because many of
6582     ;; them may be undisplayed.  So we adjust LIMIT.
6583     (when (numberp limit)
6584       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6585     (unless (eq gnus-fetch-old-headers 'invisible)
6586       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6587       ;; Retrieve the headers and read them in.
6588       (if (eq (gnus-retrieve-headers
6589                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6590               'nov)
6591           (gnus-build-all-threads)
6592         (error "Can't fetch thread from backends that don't support NOV"))
6593       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6594     (gnus-summary-limit-include-thread id)))
6595
6596 (defun gnus-summary-refer-article (message-id &optional arg)
6597   "Fetch an article specified by MESSAGE-ID.
6598 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6599 or `gnus-select-method', no matter what backend the article comes from."
6600   (interactive "sMessage-ID: \nP")
6601   (when (and (stringp message-id)
6602              (not (zerop (length message-id))))
6603     ;; Construct the correct Message-ID if necessary.
6604     ;; Suggested by tale@pawl.rpi.edu.
6605     (unless (string-match "^<" message-id)
6606       (setq message-id (concat "<" message-id)))
6607     (unless (string-match ">$" message-id)
6608       (setq message-id (concat message-id ">")))
6609     (let* ((header (gnus-id-to-header message-id))
6610            (sparse (and header
6611                         (gnus-summary-article-sparse-p
6612                          (mail-header-number header))
6613                         (memq (mail-header-number header)
6614                               gnus-newsgroup-limit))))
6615       (cond
6616        ;; If the article is present in the buffer we just go to it.
6617        ((and header
6618              (or (not (gnus-summary-article-sparse-p
6619                        (mail-header-number header)))
6620                  sparse))
6621         (prog1
6622             (gnus-summary-goto-article
6623              (mail-header-number header) nil t)
6624           (when sparse
6625             (gnus-summary-update-article (mail-header-number header)))))
6626        (t
6627         ;; We fetch the article
6628         (let ((gnus-override-method
6629                (cond ((gnus-news-group-p gnus-newsgroup-name)
6630                       gnus-refer-article-method)
6631                      (arg
6632                       (or gnus-refer-article-method gnus-select-method))
6633                      (t nil)))
6634               number)
6635           ;; Start the special refer-article method, if necessary.
6636           (when (and gnus-refer-article-method
6637                      (gnus-news-group-p gnus-newsgroup-name))
6638             (gnus-check-server gnus-refer-article-method))
6639           ;; Fetch the header, and display the article.
6640           (if (setq number (gnus-summary-insert-subject message-id))
6641               (gnus-summary-select-article nil nil nil number)
6642             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6643
6644 (defun gnus-summary-edit-parameters ()
6645   "Edit the group parameters of the current group."
6646   (interactive)
6647   (gnus-group-edit-group gnus-newsgroup-name 'params))
6648
6649 (defun gnus-summary-customize-parameters ()
6650   "Customize the group parameters of the current group."
6651   (interactive)
6652   (gnus-group-customize gnus-newsgroup-name))
6653
6654 (defun gnus-summary-enter-digest-group (&optional force)
6655   "Enter an nndoc group based on the current article.
6656 If FORCE, force a digest interpretation.  If not, try
6657 to guess what the document format is."
6658   (interactive "P")
6659   (let ((conf gnus-current-window-configuration))
6660     (save-excursion
6661       (gnus-summary-select-article))
6662     (setq gnus-current-window-configuration conf)
6663     (let* ((name (format "%s-%d"
6664                          (gnus-group-prefixed-name
6665                           gnus-newsgroup-name (list 'nndoc ""))
6666                          (save-excursion
6667                            (set-buffer gnus-summary-buffer)
6668                            gnus-current-article)))
6669            (ogroup gnus-newsgroup-name)
6670            (params (append (gnus-info-params (gnus-get-info ogroup))
6671                            (list (cons 'to-group ogroup))
6672                            (list (cons 'save-article-group ogroup))))
6673            (case-fold-search t)
6674            (buf (current-buffer))
6675            dig)
6676       (save-excursion
6677         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6678         (insert-buffer-substring gnus-original-article-buffer)
6679         ;; Remove lines that may lead nndoc to misinterpret the
6680         ;; document type.
6681         (narrow-to-region
6682          (goto-char (point-min))
6683          (or (search-forward "\n\n" nil t) (point)))
6684         (goto-char (point-min))
6685         (delete-matching-lines "^\\(Path\\):\\|^From ")
6686         (widen))
6687       (unwind-protect
6688           (if (gnus-group-read-ephemeral-group
6689                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6690                             (nndoc-article-type
6691                              ,(if force 'digest 'guess))) t)
6692               ;; Make all postings to this group go to the parent group.
6693               (nconc (gnus-info-params (gnus-get-info name))
6694                      params)
6695             ;; Couldn't select this doc group.
6696             (switch-to-buffer buf)
6697             (gnus-set-global-variables)
6698             (gnus-configure-windows 'summary)
6699             (gnus-message 3 "Article couldn't be entered?"))
6700         (kill-buffer dig)))))
6701
6702 (defun gnus-summary-read-document (n)
6703   "Open a new group based on the current article(s).
6704 This will allow you to read digests and other similar
6705 documents as newsgroups.
6706 Obeys the standard process/prefix convention."
6707   (interactive "P")
6708   (let* ((articles (gnus-summary-work-articles n))
6709          (ogroup gnus-newsgroup-name)
6710          (params (append (gnus-info-params (gnus-get-info ogroup))
6711                          (list (cons 'to-group ogroup))))
6712          article group egroup groups vgroup)
6713     (while (setq article (pop articles))
6714       (setq group (format "%s-%d" gnus-newsgroup-name article))
6715       (gnus-summary-remove-process-mark article)
6716       (when (gnus-summary-display-article article)
6717         (save-excursion
6718           (with-temp-buffer
6719             (insert-buffer-substring gnus-original-article-buffer)
6720             ;; Remove some headers that may lead nndoc to make
6721             ;; the wrong guess.
6722             (message-narrow-to-head)
6723             (goto-char (point-min))
6724             (delete-matching-lines "^\\(Path\\):\\|^From ")
6725             (widen)
6726             (if (setq egroup
6727                       (gnus-group-read-ephemeral-group
6728                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6729                                      (nndoc-article-type guess))
6730                        t nil t))
6731                 (progn
6732                   ;; Make all postings to this group go to the parent group.
6733                   (nconc (gnus-info-params (gnus-get-info egroup))
6734                          params)
6735                   (push egroup groups))
6736               ;; Couldn't select this doc group.
6737               (gnus-error 3 "Article couldn't be entered"))))))
6738     ;; Now we have selected all the documents.
6739     (cond
6740      ((not groups)
6741       (error "None of the articles could be interpreted as documents"))
6742      ((gnus-group-read-ephemeral-group
6743        (setq vgroup (format
6744                      "nnvirtual:%s-%s" gnus-newsgroup-name
6745                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6746        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6747        t
6748        (cons (current-buffer) 'summary)))
6749      (t
6750       (error "Couldn't select virtual nndoc group")))))
6751
6752 (defun gnus-summary-isearch-article (&optional regexp-p)
6753   "Do incremental search forward on the current article.
6754 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6755   (interactive "P")
6756   (gnus-summary-select-article)
6757   (gnus-configure-windows 'article)
6758   (gnus-eval-in-buffer-window gnus-article-buffer
6759     (save-restriction
6760       (widen)
6761       (isearch-forward regexp-p))))
6762
6763 (defun gnus-summary-search-article-forward (regexp &optional backward)
6764   "Search for an article containing REGEXP forward.
6765 If BACKWARD, search backward instead."
6766   (interactive
6767    (list (read-string
6768           (format "Search article %s (regexp%s): "
6769                   (if current-prefix-arg "backward" "forward")
6770                   (if gnus-last-search-regexp
6771                       (concat ", default " gnus-last-search-regexp)
6772                     "")))
6773          current-prefix-arg))
6774   (if (string-equal regexp "")
6775       (setq regexp (or gnus-last-search-regexp ""))
6776     (setq gnus-last-search-regexp regexp))
6777   (if (gnus-summary-search-article regexp backward)
6778       (gnus-summary-show-thread)
6779     (error "Search failed: \"%s\"" regexp)))
6780
6781 (defun gnus-summary-search-article-backward (regexp)
6782   "Search for an article containing REGEXP backward."
6783   (interactive
6784    (list (read-string
6785           (format "Search article backward (regexp%s): "
6786                   (if gnus-last-search-regexp
6787                       (concat ", default " gnus-last-search-regexp)
6788                     "")))))
6789   (gnus-summary-search-article-forward regexp 'backward))
6790
6791 (defun gnus-summary-search-article (regexp &optional backward)
6792   "Search for an article containing REGEXP.
6793 Optional argument BACKWARD means do search for backward.
6794 `gnus-select-article-hook' is not called during the search."
6795   ;; We have to require this here to make sure that the following
6796   ;; dynamic binding isn't shadowed by autoloading.
6797   (require 'gnus-async)
6798   (require 'gnus-art)
6799   (let ((gnus-select-article-hook nil)  ;Disable hook.
6800         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6801         (gnus-use-article-prefetch nil)
6802         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6803         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6804         (sum (current-buffer))
6805         (gnus-display-mime-function nil)
6806         (found nil)
6807         point)
6808     (gnus-save-hidden-threads
6809       (gnus-summary-select-article)
6810       (set-buffer gnus-article-buffer)
6811       (when backward
6812         (forward-line -1))
6813       (while (not found)
6814         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6815         (if (if backward
6816                 (re-search-backward regexp nil t)
6817               (re-search-forward regexp nil t))
6818             ;; We found the regexp.
6819             (progn
6820               (setq found 'found)
6821               (beginning-of-line)
6822               (set-window-start
6823                (get-buffer-window (current-buffer))
6824                (point))
6825               (forward-line 1)
6826               (set-buffer sum)
6827               (setq point (point)))
6828           ;; We didn't find it, so we go to the next article.
6829           (set-buffer sum)
6830           (setq found 'not)
6831           (while (eq found 'not)
6832             (if (not (if backward (gnus-summary-find-prev)
6833                        (gnus-summary-find-next)))
6834                 ;; No more articles.
6835                 (setq found t)
6836               ;; Select the next article and adjust point.
6837               (unless (gnus-summary-article-sparse-p
6838                        (gnus-summary-article-number))
6839                 (setq found nil)
6840                 (gnus-summary-select-article)
6841                 (set-buffer gnus-article-buffer)
6842                 (widen)
6843                 (goto-char (if backward (point-max) (point-min))))))))
6844       (gnus-message 7 ""))
6845     ;; Return whether we found the regexp.
6846     (when (eq found 'found)
6847       (goto-char point)
6848       (gnus-summary-show-thread)
6849       (gnus-summary-goto-subject gnus-current-article)
6850       (gnus-summary-position-point)
6851       t)))
6852
6853 (defun gnus-summary-find-matching (header regexp &optional backward unread
6854                                           not-case-fold)
6855   "Return a list of all articles that match REGEXP on HEADER.
6856 The search stars on the current article and goes forwards unless
6857 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6858 If UNREAD is non-nil, only unread articles will
6859 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6860 in the comparisons."
6861   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6862                 (gnus-data-find-list
6863                  (gnus-summary-article-number) (gnus-data-list backward))))
6864         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6865         (case-fold-search (not not-case-fold))
6866         articles d)
6867     (unless (fboundp (intern (concat "mail-header-" header)))
6868       (error "%s is not a valid header" header))
6869     (while data
6870       (setq d (car data))
6871       (and (or (not unread)             ; We want all articles...
6872                (gnus-data-unread-p d))  ; Or just unreads.
6873            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6874            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6875            (push (gnus-data-number d) articles)) ; Success!
6876       (setq data (cdr data)))
6877     (nreverse articles)))
6878
6879 (defun gnus-summary-execute-command (header regexp command &optional backward)
6880   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6881 If HEADER is an empty string (or nil), the match is done on the entire
6882 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6883   (interactive
6884    (list (let ((completion-ignore-case t))
6885            (completing-read
6886             "Header name: "
6887             (mapcar (lambda (string) (list string))
6888                     '("Number" "Subject" "From" "Lines" "Date"
6889                       "Message-ID" "Xref" "References" "Body"))
6890             nil 'require-match))
6891          (read-string "Regexp: ")
6892          (read-key-sequence "Command: ")
6893          current-prefix-arg))
6894   (when (equal header "Body")
6895     (setq header ""))
6896   ;; Hidden thread subtrees must be searched as well.
6897   (gnus-summary-show-all-threads)
6898   ;; We don't want to change current point nor window configuration.
6899   (save-excursion
6900     (save-window-excursion
6901       (gnus-message 6 "Executing %s..." (key-description command))
6902       ;; We'd like to execute COMMAND interactively so as to give arguments.
6903       (gnus-execute header regexp
6904                     `(call-interactively ',(key-binding command))
6905                     backward)
6906       (gnus-message 6 "Executing %s...done" (key-description command)))))
6907
6908 (defun gnus-summary-beginning-of-article ()
6909   "Scroll the article back to the beginning."
6910   (interactive)
6911   (gnus-summary-select-article)
6912   (gnus-configure-windows 'article)
6913   (gnus-eval-in-buffer-window gnus-article-buffer
6914     (widen)
6915     (goto-char (point-min))
6916     (when gnus-page-broken
6917       (gnus-narrow-to-page))))
6918
6919 (defun gnus-summary-end-of-article ()
6920   "Scroll to the end of the article."
6921   (interactive)
6922   (gnus-summary-select-article)
6923   (gnus-configure-windows 'article)
6924   (gnus-eval-in-buffer-window gnus-article-buffer
6925     (widen)
6926     (goto-char (point-max))
6927     (recenter -3)
6928     (when gnus-page-broken
6929       (gnus-narrow-to-page))))
6930
6931 (defun gnus-summary-print-article (&optional filename n)
6932   "Generate and print a PostScript image of the N next (mail) articles.
6933
6934 If N is negative, print the N previous articles.  If N is nil and articles
6935 have been marked with the process mark, print these instead.
6936
6937 If the optional first argument FILENAME is nil, send the image to the
6938 printer.  If FILENAME is a string, save the PostScript image in a file with
6939 that name.  If FILENAME is a number, prompt the user for the name of the file
6940 to save in."
6941   (interactive (list (ps-print-preprint current-prefix-arg)
6942                      current-prefix-arg))
6943   (dolist (article (gnus-summary-work-articles n))
6944     (gnus-summary-select-article nil nil 'pseudo article)
6945     (gnus-eval-in-buffer-window gnus-article-buffer
6946       (let ((buffer (generate-new-buffer " *print*")))
6947         (unwind-protect
6948             (progn
6949               (copy-to-buffer buffer (point-min) (point-max))
6950               (set-buffer buffer)
6951               (gnus-article-delete-invisible-text)
6952               (let ((ps-left-header
6953                      (list
6954                       (concat "("
6955                               (mail-header-subject gnus-current-headers) ")")
6956                       (concat "("
6957                               (mail-header-from gnus-current-headers) ")")))
6958                     (ps-right-header
6959                      (list
6960                       "/pagenumberstring load"
6961                       (concat "("
6962                               (mail-header-date gnus-current-headers) ")"))))
6963                 (gnus-run-hooks 'gnus-ps-print-hook)
6964                 (save-excursion
6965                   (ps-print-buffer-with-faces filename))))
6966           (kill-buffer buffer))))))
6967
6968 (defun gnus-summary-show-article (&optional arg)
6969   "Force re-fetching of the current article.
6970 If ARG (the prefix) is non-nil, show the raw article without any
6971 article massaging functions being run."
6972   (interactive "P")
6973   (if (not arg)
6974       ;; Select the article the normal way.
6975       (gnus-summary-select-article nil 'force)
6976     ;; We have to require this here to make sure that the following
6977     ;; dynamic binding isn't shadowed by autoloading.
6978     (require 'gnus-async)
6979     (require 'gnus-art)
6980     ;; Bind the article treatment functions to nil.
6981     (let ((gnus-have-all-headers t)
6982           gnus-article-prepare-hook
6983           gnus-article-decode-hook
6984           gnus-display-mime-function
6985           gnus-break-pages
6986           gnus-visual)
6987       ;; Destroy any MIME parts.
6988       (when (gnus-buffer-live-p gnus-article-buffer)
6989         (save-excursion
6990           (set-buffer gnus-article-buffer)
6991           (mm-destroy-parts gnus-article-mime-handles)))
6992       (gnus-summary-select-article nil 'force)))
6993   (gnus-summary-goto-subject gnus-current-article)
6994   (gnus-summary-position-point))
6995
6996 (defun gnus-summary-verbose-headers (&optional arg)
6997   "Toggle permanent full header display.
6998 If ARG is a positive number, turn header display on.
6999 If ARG is a negative number, turn header display off."
7000   (interactive "P")
7001   (setq gnus-show-all-headers
7002         (cond ((or (not (numberp arg))
7003                    (zerop arg))
7004                (not gnus-show-all-headers))
7005               ((natnump arg)
7006                t)))
7007   (gnus-summary-show-article))
7008
7009 (defun gnus-summary-toggle-header (&optional arg)
7010   "Show the headers if they are hidden, or hide them if they are shown.
7011 If ARG is a positive number, show the entire header.
7012 If ARG is a negative number, hide the unwanted header lines."
7013   (interactive "P")
7014   (save-excursion
7015     (set-buffer gnus-article-buffer)
7016     (let* ((buffer-read-only nil)
7017            (inhibit-point-motion-hooks t)
7018            (hidden (gnus-article-hidden-text-p 'headers))
7019            e)
7020       (goto-char (point-min))
7021       (when (search-forward "\n\n" nil t)
7022         (delete-region (point-min) (1- (point))))
7023       (goto-char (point-min))
7024       (save-excursion
7025         (set-buffer gnus-original-article-buffer)
7026         (goto-char (point-min))
7027         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7028       (insert-buffer-substring gnus-original-article-buffer 1 e)
7029       (narrow-to-region (point-min) (point))
7030       (if (or hidden
7031               (and (numberp arg) (< arg 0)))
7032           (let ((gnus-treat-hide-headers nil)
7033                 (gnus-treat-hide-boring-headers nil))
7034             (gnus-treat-article 'head))
7035         (gnus-treat-article 'head)))))
7036
7037 (defun gnus-summary-show-all-headers ()
7038   "Make all header lines visible."
7039   (interactive)
7040   (gnus-article-show-all-headers))
7041
7042 (defun gnus-summary-caesar-message (&optional arg)
7043   "Caesar rotate the current article by 13.
7044 The numerical prefix specifies how many places to rotate each letter
7045 forward."
7046   (interactive "P")
7047   (gnus-summary-select-article)
7048   (let ((mail-header-separator ""))
7049     (gnus-eval-in-buffer-window gnus-article-buffer
7050       (save-restriction
7051         (widen)
7052         (let ((start (window-start))
7053               buffer-read-only)
7054           (message-caesar-buffer-body arg)
7055           (set-window-start (get-buffer-window (current-buffer)) start))))))
7056
7057 (defun gnus-summary-stop-page-breaking ()
7058   "Stop page breaking in the current article."
7059   (interactive)
7060   (gnus-summary-select-article)
7061   (gnus-eval-in-buffer-window gnus-article-buffer
7062     (widen)
7063     (when (gnus-visual-p 'page-marker)
7064       (let ((buffer-read-only nil))
7065         (gnus-remove-text-with-property 'gnus-prev)
7066         (gnus-remove-text-with-property 'gnus-next))
7067       (setq gnus-page-broken nil))))
7068
7069 (defun gnus-summary-move-article (&optional n to-newsgroup
7070                                             select-method action)
7071   "Move the current article to a different newsgroup.
7072 If N is a positive number, move the N next articles.
7073 If N is a negative number, move the N previous articles.
7074 If N is nil and any articles have been marked with the process mark,
7075 move those articles instead.
7076 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7077 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7078 re-spool using this method.
7079
7080 For this function to work, both the current newsgroup and the
7081 newsgroup that you want to move to have to support the `request-move'
7082 and `request-accept' functions."
7083   (interactive "P")
7084   (unless action
7085     (setq action 'move))
7086   ;; Disable marking as read.
7087   (let (gnus-mark-article-hook)
7088     (save-window-excursion
7089       (gnus-summary-select-article)))
7090   ;; Check whether the source group supports the required functions.
7091   (cond ((and (eq action 'move)
7092               (not (gnus-check-backend-function
7093                     'request-move-article gnus-newsgroup-name)))
7094          (error "The current group does not support article moving"))
7095         ((and (eq action 'crosspost)
7096               (not (gnus-check-backend-function
7097                     'request-replace-article gnus-newsgroup-name)))
7098          (error "The current group does not support article editing")))
7099   (let ((articles (gnus-summary-work-articles n))
7100         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7101         (names '((move "Move" "Moving")
7102                  (copy "Copy" "Copying")
7103                  (crosspost "Crosspost" "Crossposting")))
7104         (copy-buf (save-excursion
7105                     (nnheader-set-temp-buffer " *copy article*")))
7106         art-group to-method new-xref article to-groups)
7107     (unless (assq action names)
7108       (error "Unknown action %s" action))
7109     ;; Read the newsgroup name.
7110     (when (and (not to-newsgroup)
7111                (not select-method))
7112       (setq to-newsgroup
7113             (gnus-read-move-group-name
7114              (cadr (assq action names))
7115              (symbol-value (intern (format "gnus-current-%s-group" action)))
7116              articles prefix))
7117       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7118     (setq to-method (or select-method
7119                         (gnus-group-name-to-method to-newsgroup)))
7120     ;; Check the method we are to move this article to...
7121     (unless (gnus-check-backend-function
7122              'request-accept-article (car to-method))
7123       (error "%s does not support article copying" (car to-method)))
7124     (unless (gnus-check-server to-method)
7125       (error "Can't open server %s" (car to-method)))
7126     (gnus-message 6 "%s to %s: %s..."
7127                   (caddr (assq action names))
7128                   (or (car select-method) to-newsgroup) articles)
7129     (while articles
7130       (setq article (pop articles))
7131       (setq
7132        art-group
7133        (cond
7134         ;; Move the article.
7135         ((eq action 'move)
7136          ;; Remove this article from future suppression.
7137          (gnus-dup-unsuppress-article article)
7138          (gnus-request-move-article
7139           article                       ; Article to move
7140           gnus-newsgroup-name           ; From newsgroup
7141           (nth 1 (gnus-find-method-for-group
7142                   gnus-newsgroup-name)) ; Server
7143           (list 'gnus-request-accept-article
7144                 to-newsgroup (list 'quote select-method)
7145                 (not articles) t)               ; Accept form
7146           (not articles)))              ; Only save nov last time
7147         ;; Copy the article.
7148         ((eq action 'copy)
7149          (save-excursion
7150            (set-buffer copy-buf)
7151            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7152              (gnus-request-accept-article
7153               to-newsgroup select-method (not articles) t))))
7154         ;; Crosspost the article.
7155         ((eq action 'crosspost)
7156          (let ((xref (message-tokenize-header
7157                       (mail-header-xref (gnus-summary-article-header article))
7158                       " ")))
7159            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7160                                   ":" article))
7161            (unless xref
7162              (setq xref (list (system-name))))
7163            (setq new-xref
7164                  (concat
7165                   (mapconcat 'identity
7166                              (delete "Xref:" (delete new-xref xref))
7167                              " ")
7168                   " " new-xref))
7169            (save-excursion
7170              (set-buffer copy-buf)
7171              ;; First put the article in the destination group.
7172              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7173              (when (consp (setq art-group
7174                                 (gnus-request-accept-article
7175                                  to-newsgroup select-method (not articles))))
7176                (setq new-xref (concat new-xref " " (car art-group)
7177                                       ":" (cdr art-group)))
7178                ;; Now we have the new Xrefs header, so we insert
7179                ;; it and replace the new article.
7180                (nnheader-replace-header "Xref" new-xref)
7181                (gnus-request-replace-article
7182                 (cdr art-group) to-newsgroup (current-buffer))
7183                art-group))))))
7184       (cond
7185        ((not art-group)
7186         (gnus-message 1 "Couldn't %s article %s"
7187                       (cadr (assq action names)) article))
7188        ((and (eq art-group 'junk)
7189              (eq action 'move))
7190         (gnus-summary-mark-article article gnus-canceled-mark)
7191         (gnus-message 4 "Deleted article %s" article))
7192        (t
7193         (let* ((pto-group (gnus-group-prefixed-name
7194                            (car art-group) to-method))
7195                (entry
7196                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7197                (info (nth 2 entry))
7198                (to-group (gnus-info-group info)))
7199           ;; Update the group that has been moved to.
7200           (when (and info
7201                      (memq action '(move copy)))
7202             (unless (member to-group to-groups)
7203               (push to-group to-groups))
7204
7205             (unless (memq article gnus-newsgroup-unreads)
7206               (gnus-info-set-read
7207                info (gnus-add-to-range (gnus-info-read info)
7208                                        (list (cdr art-group)))))
7209
7210             ;; Copy any marks over to the new group.
7211             (let ((marks gnus-article-mark-lists)
7212                   (to-article (cdr art-group)))
7213
7214               ;; See whether the article is to be put in the cache.
7215               (when gnus-use-cache
7216                 (gnus-cache-possibly-enter-article
7217                  to-group to-article
7218                  (let ((header (copy-sequence
7219                                 (gnus-summary-article-header article))))
7220                    (mail-header-set-number header to-article)
7221                    header)
7222                  (memq article gnus-newsgroup-marked)
7223                  (memq article gnus-newsgroup-dormant)
7224                  (memq article gnus-newsgroup-unreads)))
7225
7226               (when (and (equal to-group gnus-newsgroup-name)
7227                          (not (memq article gnus-newsgroup-unreads)))
7228                 ;; Mark this article as read in this group.
7229                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7230                 (setcdr (gnus-active to-group) to-article)
7231                 (setcdr gnus-newsgroup-active to-article))
7232
7233               (while marks
7234                 (when (memq article (symbol-value
7235                                      (intern (format "gnus-newsgroup-%s"
7236                                                      (caar marks)))))
7237                   ;; If the other group is the same as this group,
7238                   ;; then we have to add the mark to the list.
7239                   (when (equal to-group gnus-newsgroup-name)
7240                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7241                          (cons to-article
7242                                (symbol-value
7243                                 (intern (format "gnus-newsgroup-%s"
7244                                                 (caar marks)))))))
7245                   ;; Copy the marks to other group.
7246                   (gnus-add-marked-articles
7247                    to-group (cdar marks) (list to-article) info))
7248                 (setq marks (cdr marks)))
7249
7250               (gnus-dribble-enter
7251                (concat "(gnus-group-set-info '"
7252                        (gnus-prin1-to-string (gnus-get-info to-group))
7253                        ")"))))
7254
7255           ;; Update the Xref header in this article to point to
7256           ;; the new crossposted article we have just created.
7257           (when (eq action 'crosspost)
7258             (save-excursion
7259               (set-buffer copy-buf)
7260               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7261               (nnheader-replace-header "Xref" new-xref)
7262               (gnus-request-replace-article
7263                article gnus-newsgroup-name (current-buffer)))))
7264
7265         ;;;!!!Why is this necessary?
7266         (set-buffer gnus-summary-buffer)
7267
7268         (gnus-summary-goto-subject article)
7269         (when (eq action 'move)
7270           (gnus-summary-mark-article article gnus-canceled-mark))))
7271       (gnus-summary-remove-process-mark article))
7272     ;; Re-activate all groups that have been moved to.
7273     (while to-groups
7274       (save-excursion
7275         (set-buffer gnus-group-buffer)
7276         (when (gnus-group-goto-group (car to-groups) t)
7277           (gnus-group-get-new-news-this-group 1 t))
7278         (pop to-groups)))
7279
7280     (gnus-kill-buffer copy-buf)
7281     (gnus-summary-position-point)
7282     (gnus-set-mode-line 'summary)))
7283
7284 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7285   "Move the current article to a different newsgroup.
7286 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7287 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7288 re-spool using this method."
7289   (interactive "P")
7290   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7291
7292 (defun gnus-summary-crosspost-article (&optional n)
7293   "Crosspost the current article to some other group."
7294   (interactive "P")
7295   (gnus-summary-move-article n nil nil 'crosspost))
7296
7297 (defcustom gnus-summary-respool-default-method nil
7298   "Default method for respooling an article.
7299 If nil, use to the current newsgroup method."
7300   :type '(choice (gnus-select-method :value (nnml ""))
7301                  (const nil))
7302   :group 'gnus-summary-mail)
7303
7304 (defun gnus-summary-respool-article (&optional n method)
7305   "Respool the current article.
7306 The article will be squeezed through the mail spooling process again,
7307 which means that it will be put in some mail newsgroup or other
7308 depending on `nnmail-split-methods'.
7309 If N is a positive number, respool the N next articles.
7310 If N is a negative number, respool the N previous articles.
7311 If N is nil and any articles have been marked with the process mark,
7312 respool those articles instead.
7313
7314 Respooling can be done both from mail groups and \"real\" newsgroups.
7315 In the former case, the articles in question will be moved from the
7316 current group into whatever groups they are destined to.  In the
7317 latter case, they will be copied into the relevant groups."
7318   (interactive
7319    (list current-prefix-arg
7320          (let* ((methods (gnus-methods-using 'respool))
7321                 (methname
7322                  (symbol-name (or gnus-summary-respool-default-method
7323                                   (car (gnus-find-method-for-group
7324                                         gnus-newsgroup-name)))))
7325                 (method
7326                  (gnus-completing-read
7327                   methname "What backend do you want to use when respooling?"
7328                   methods nil t nil 'gnus-mail-method-history))
7329                 ms)
7330            (cond
7331             ((zerop (length (setq ms (gnus-servers-using-backend
7332                                       (intern method)))))
7333              (list (intern method) ""))
7334             ((= 1 (length ms))
7335              (car ms))
7336             (t
7337              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7338                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7339                            ms-alist))))))))
7340   (unless method
7341     (error "No method given for respooling"))
7342   (if (assoc (symbol-name
7343               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7344              (gnus-methods-using 'respool))
7345       (gnus-summary-move-article n nil method)
7346     (gnus-summary-copy-article n nil method)))
7347
7348 (defun gnus-summary-import-article (file)
7349   "Import an arbitrary file into a mail newsgroup."
7350   (interactive "fImport file: ")
7351   (let ((group gnus-newsgroup-name)
7352         (now (current-time))
7353         atts lines)
7354     (unless (gnus-check-backend-function 'request-accept-article group)
7355       (error "%s does not support article importing" group))
7356     (or (file-readable-p file)
7357         (not (file-regular-p file))
7358         (error "Can't read %s" file))
7359     (save-excursion
7360       (set-buffer (gnus-get-buffer-create " *import file*"))
7361       (erase-buffer)
7362       (insert-file-contents file)
7363       (goto-char (point-min))
7364       (unless (nnheader-article-p)
7365         ;; This doesn't look like an article, so we fudge some headers.
7366         (setq atts (file-attributes file)
7367               lines (count-lines (point-min) (point-max)))
7368         (insert "From: " (read-string "From: ") "\n"
7369                 "Subject: " (read-string "Subject: ") "\n"
7370                 "Date: " (message-make-date (nth 5 atts))
7371                 "\n"
7372                 "Message-ID: " (message-make-message-id) "\n"
7373                 "Lines: " (int-to-string lines) "\n"
7374                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7375       (gnus-request-accept-article group nil t)
7376       (kill-buffer (current-buffer)))))
7377
7378 (defun gnus-summary-article-posted-p ()
7379   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7380 This will be the case if the article has both been mailed and posted."
7381   (interactive)
7382   (let ((id (mail-header-references (gnus-summary-article-header)))
7383         (gnus-override-method
7384          (or gnus-refer-article-method gnus-select-method)))
7385     (if (gnus-request-head id "")
7386         (gnus-message 2 "The current message was found on %s"
7387                       gnus-override-method)
7388       (gnus-message 2 "The current message couldn't be found on %s"
7389                     gnus-override-method)
7390       nil)))
7391
7392 (defun gnus-summary-expire-articles (&optional now)
7393   "Expire all articles that are marked as expirable in the current group."
7394   (interactive)
7395   (when (gnus-check-backend-function
7396          'request-expire-articles gnus-newsgroup-name)
7397     ;; This backend supports expiry.
7398     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7399            (expirable (if total
7400                           (progn
7401                             ;; We need to update the info for
7402                             ;; this group for `gnus-list-of-read-articles'
7403                             ;; to give us the right answer.
7404                             (gnus-run-hooks 'gnus-exit-group-hook)
7405                             (gnus-summary-update-info)
7406                             (gnus-list-of-read-articles gnus-newsgroup-name))
7407                         (setq gnus-newsgroup-expirable
7408                               (sort gnus-newsgroup-expirable '<))))
7409            (expiry-wait (if now 'immediate
7410                           (gnus-group-find-parameter
7411                            gnus-newsgroup-name 'expiry-wait)))
7412            es)
7413       (when expirable
7414         ;; There are expirable articles in this group, so we run them
7415         ;; through the expiry process.
7416         (gnus-message 6 "Expiring articles...")
7417         ;; The list of articles that weren't expired is returned.
7418         (save-excursion
7419           (if expiry-wait
7420               (let ((nnmail-expiry-wait-function nil)
7421                     (nnmail-expiry-wait expiry-wait))
7422                 (setq es (gnus-request-expire-articles
7423                           expirable gnus-newsgroup-name)))
7424             (setq es (gnus-request-expire-articles
7425                       expirable gnus-newsgroup-name))))
7426         (unless total
7427           (setq gnus-newsgroup-expirable es))
7428         ;; We go through the old list of expirable, and mark all
7429         ;; really expired articles as nonexistent.
7430         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7431           (let ((gnus-use-cache nil))
7432             (while expirable
7433               (unless (memq (car expirable) es)
7434                 (when (gnus-data-find (car expirable))
7435                   (gnus-summary-mark-article
7436                    (car expirable) gnus-canceled-mark)))
7437               (setq expirable (cdr expirable)))))
7438         (gnus-message 6 "Expiring articles...done")))))
7439
7440 (defun gnus-summary-expire-articles-now ()
7441   "Expunge all expirable articles in the current group.
7442 This means that *all* articles that are marked as expirable will be
7443 deleted forever, right now."
7444   (interactive)
7445   (or gnus-expert-user
7446       (gnus-yes-or-no-p
7447        "Are you really, really, really sure you want to delete all these messages? ")
7448       (error "Phew!"))
7449   (gnus-summary-expire-articles t))
7450
7451 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7452 (defun gnus-summary-delete-article (&optional n)
7453   "Delete the N next (mail) articles.
7454 This command actually deletes articles.  This is not a marking
7455 command.  The article will disappear forever from your life, never to
7456 return.
7457 If N is negative, delete backwards.
7458 If N is nil and articles have been marked with the process mark,
7459 delete these instead."
7460   (interactive "P")
7461   (unless (gnus-check-backend-function 'request-expire-articles
7462                                        gnus-newsgroup-name)
7463     (error "The current newsgroup does not support article deletion"))
7464   ;; Compute the list of articles to delete.
7465   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7466         not-deleted)
7467     (if (and gnus-novice-user
7468              (not (gnus-yes-or-no-p
7469                    (format "Do you really want to delete %s forever? "
7470                            (if (> (length articles) 1)
7471                                (format "these %s articles" (length articles))
7472                              "this article")))))
7473         ()
7474       ;; Delete the articles.
7475       (setq not-deleted (gnus-request-expire-articles
7476                          articles gnus-newsgroup-name 'force))
7477       (while articles
7478         (gnus-summary-remove-process-mark (car articles))
7479         ;; The backend might not have been able to delete the article
7480         ;; after all.
7481         (unless (memq (car articles) not-deleted)
7482           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7483         (setq articles (cdr articles)))
7484       (when not-deleted
7485         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7486     (gnus-summary-position-point)
7487     (gnus-set-mode-line 'summary)
7488     not-deleted))
7489
7490 (defun gnus-summary-edit-article (&optional force)
7491   "Edit the current article.
7492 This will have permanent effect only in mail groups.
7493 If FORCE is non-nil, allow editing of articles even in read-only
7494 groups."
7495   (interactive "P")
7496   (let ((mail-parse-charset gnus-newsgroup-charset))
7497     (save-excursion
7498       (set-buffer gnus-summary-buffer)
7499       (gnus-set-global-variables)
7500       (when (and (not force)
7501                  (gnus-group-read-only-p))
7502         (error "The current newsgroup does not support article editing"))
7503       (gnus-summary-show-article t)
7504       (gnus-article-edit-article
7505        'mime-to-mml
7506        `(lambda (no-highlight)
7507           (let ((mail-parse-charset ',gnus-newsgroup-charset))
7508             (mml-to-mime)
7509             (gnus-summary-edit-article-done
7510              ,(or (mail-header-references gnus-current-headers) "")
7511              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7512
7513 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7514
7515 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7516                                                  no-highlight)
7517   "Make edits to the current article permanent."
7518   (interactive)
7519   ;; Replace the article.
7520   (let ((buf (current-buffer)))
7521     (with-temp-buffer
7522       (insert-buffer-substring buf)
7523       (if (and (not read-only)
7524                (not (gnus-request-replace-article
7525                      (cdr gnus-article-current) (car gnus-article-current)
7526                      (current-buffer) t)))
7527           (error "Couldn't replace article")
7528         ;; Update the summary buffer.
7529         (if (and references
7530                  (equal (message-tokenize-header references " ")
7531                         (message-tokenize-header
7532                          (or (message-fetch-field "references") "") " ")))
7533             ;; We only have to update this line.
7534             (save-excursion
7535               (save-restriction
7536                 (message-narrow-to-head)
7537                 (let ((head (buffer-string))
7538                       header)
7539                   (with-temp-buffer
7540                     (insert (format "211 %d Article retrieved.\n"
7541                                     (cdr gnus-article-current)))
7542                     (insert head)
7543                     (insert ".\n")
7544                     (let ((nntp-server-buffer (current-buffer)))
7545                       (setq header (car (gnus-get-newsgroup-headers
7546                                          (save-excursion
7547                                            (set-buffer gnus-summary-buffer)
7548                                            gnus-newsgroup-dependencies)
7549                                          t))))
7550                     (save-excursion
7551                       (set-buffer gnus-summary-buffer)
7552                       (gnus-data-set-header
7553                        (gnus-data-find (cdr gnus-article-current))
7554                        header)
7555                       (gnus-summary-update-article-line
7556                        (cdr gnus-article-current) header))))))
7557           ;; Update threads.
7558           (set-buffer (or buffer gnus-summary-buffer))
7559           (gnus-summary-update-article (cdr gnus-article-current)))
7560         ;; Prettify the article buffer again.
7561         (unless no-highlight
7562           (save-excursion
7563             (set-buffer gnus-article-buffer)
7564             ;;;!!! Fix this -- article should be rehighlighted.
7565             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7566             (set-buffer gnus-original-article-buffer)
7567             (gnus-request-article
7568              (cdr gnus-article-current)
7569              (car gnus-article-current) (current-buffer))))
7570         ;; Prettify the summary buffer line.
7571         (when (gnus-visual-p 'summary-highlight 'highlight)
7572           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7573
7574 (defun gnus-summary-edit-wash (key)
7575   "Perform editing command KEY in the article buffer."
7576   (interactive
7577    (list
7578     (progn
7579       (message "%s" (concat (this-command-keys) "- "))
7580       (read-char))))
7581   (message "")
7582   (gnus-summary-edit-article)
7583   (execute-kbd-macro (concat (this-command-keys) key))
7584   (gnus-article-edit-done))
7585
7586 ;;; Respooling
7587
7588 (defun gnus-summary-respool-query (&optional silent trace)
7589   "Query where the respool algorithm would put this article."
7590   (interactive)
7591   (let (gnus-mark-article-hook)
7592     (gnus-summary-select-article)
7593     (save-excursion
7594       (set-buffer gnus-original-article-buffer)
7595       (save-restriction
7596         (message-narrow-to-head)
7597         (let ((groups (nnmail-article-group 'identity trace)))
7598           (unless silent
7599             (if groups
7600                 (message "This message would go to %s"
7601                          (mapconcat 'car groups ", "))
7602               (message "This message would go to no groups"))
7603             groups))))))
7604
7605 (defun gnus-summary-respool-trace ()
7606   "Trace where the respool algorithm would put this article.
7607 Display a buffer showing all fancy splitting patterns which matched."
7608   (interactive)
7609   (gnus-summary-respool-query nil t))
7610
7611 ;; Summary marking commands.
7612
7613 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7614   "Mark articles which has the same subject as read, and then select the next.
7615 If UNMARK is positive, remove any kind of mark.
7616 If UNMARK is negative, tick articles."
7617   (interactive "P")
7618   (when unmark
7619     (setq unmark (prefix-numeric-value unmark)))
7620   (let ((count
7621          (gnus-summary-mark-same-subject
7622           (gnus-summary-article-subject) unmark)))
7623     ;; Select next unread article.  If auto-select-same mode, should
7624     ;; select the first unread article.
7625     (gnus-summary-next-article t (and gnus-auto-select-same
7626                                       (gnus-summary-article-subject)))
7627     (gnus-message 7 "%d article%s marked as %s"
7628                   count (if (= count 1) " is" "s are")
7629                   (if unmark "unread" "read"))))
7630
7631 (defun gnus-summary-kill-same-subject (&optional unmark)
7632   "Mark articles which has the same subject as read.
7633 If UNMARK is positive, remove any kind of mark.
7634 If UNMARK is negative, tick articles."
7635   (interactive "P")
7636   (when unmark
7637     (setq unmark (prefix-numeric-value unmark)))
7638   (let ((count
7639          (gnus-summary-mark-same-subject
7640           (gnus-summary-article-subject) unmark)))
7641     ;; If marked as read, go to next unread subject.
7642     (when (null unmark)
7643       ;; Go to next unread subject.
7644       (gnus-summary-next-subject 1 t))
7645     (gnus-message 7 "%d articles are marked as %s"
7646                   count (if unmark "unread" "read"))))
7647
7648 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7649   "Mark articles with same SUBJECT as read, and return marked number.
7650 If optional argument UNMARK is positive, remove any kinds of marks.
7651 If optional argument UNMARK is negative, mark articles as unread instead."
7652   (let ((count 1))
7653     (save-excursion
7654       (cond
7655        ((null unmark)                   ; Mark as read.
7656         (while (and
7657                 (progn
7658                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7659                   (gnus-summary-show-thread) t)
7660                 (gnus-summary-find-subject subject))
7661           (setq count (1+ count))))
7662        ((> unmark 0)                    ; Tick.
7663         (while (and
7664                 (progn
7665                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7666                   (gnus-summary-show-thread) t)
7667                 (gnus-summary-find-subject subject))
7668           (setq count (1+ count))))
7669        (t                               ; Mark as unread.
7670         (while (and
7671                 (progn
7672                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7673                   (gnus-summary-show-thread) t)
7674                 (gnus-summary-find-subject subject))
7675           (setq count (1+ count)))))
7676       (gnus-set-mode-line 'summary)
7677       ;; Return the number of marked articles.
7678       count)))
7679
7680 (defun gnus-summary-mark-as-processable (n &optional unmark)
7681   "Set the process mark on the next N articles.
7682 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7683 the process mark instead.  The difference between N and the actual
7684 number of articles marked is returned."
7685   (interactive "p")
7686   (let ((backward (< n 0))
7687         (n (abs n)))
7688     (while (and
7689             (> n 0)
7690             (if unmark
7691                 (gnus-summary-remove-process-mark
7692                  (gnus-summary-article-number))
7693               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7694             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7695       (setq n (1- n)))
7696     (when (/= 0 n)
7697       (gnus-message 7 "No more articles"))
7698     (gnus-summary-recenter)
7699     (gnus-summary-position-point)
7700     n))
7701
7702 (defun gnus-summary-unmark-as-processable (n)
7703   "Remove the process mark from the next N articles.
7704 If N is negative, unmark backward instead.  The difference between N and
7705 the actual number of articles unmarked is returned."
7706   (interactive "p")
7707   (gnus-summary-mark-as-processable n t))
7708
7709 (defun gnus-summary-unmark-all-processable ()
7710   "Remove the process mark from all articles."
7711   (interactive)
7712   (save-excursion
7713     (while gnus-newsgroup-processable
7714       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7715   (gnus-summary-position-point))
7716
7717 (defun gnus-summary-mark-as-expirable (n)
7718   "Mark N articles forward as expirable.
7719 If N is negative, mark backward instead.  The difference between N and
7720 the actual number of articles marked is returned."
7721   (interactive "p")
7722   (gnus-summary-mark-forward n gnus-expirable-mark))
7723
7724 (defun gnus-summary-mark-article-as-replied (article)
7725   "Mark ARTICLE replied and update the summary line."
7726   (push article gnus-newsgroup-replied)
7727   (let ((buffer-read-only nil))
7728     (when (gnus-summary-goto-subject article nil t)
7729       (gnus-summary-update-secondary-mark article))))
7730
7731 (defun gnus-summary-set-bookmark (article)
7732   "Set a bookmark in current article."
7733   (interactive (list (gnus-summary-article-number)))
7734   (when (or (not (get-buffer gnus-article-buffer))
7735             (not gnus-current-article)
7736             (not gnus-article-current)
7737             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7738     (error "No current article selected"))
7739   ;; Remove old bookmark, if one exists.
7740   (let ((old (assq article gnus-newsgroup-bookmarks)))
7741     (when old
7742       (setq gnus-newsgroup-bookmarks
7743             (delq old gnus-newsgroup-bookmarks))))
7744   ;; Set the new bookmark, which is on the form
7745   ;; (article-number . line-number-in-body).
7746   (push
7747    (cons article
7748          (save-excursion
7749            (set-buffer gnus-article-buffer)
7750            (count-lines
7751             (min (point)
7752                  (save-excursion
7753                    (goto-char (point-min))
7754                    (search-forward "\n\n" nil t)
7755                    (point)))
7756             (point))))
7757    gnus-newsgroup-bookmarks)
7758   (gnus-message 6 "A bookmark has been added to the current article."))
7759
7760 (defun gnus-summary-remove-bookmark (article)
7761   "Remove the bookmark from the current article."
7762   (interactive (list (gnus-summary-article-number)))
7763   ;; Remove old bookmark, if one exists.
7764   (let ((old (assq article gnus-newsgroup-bookmarks)))
7765     (if old
7766         (progn
7767           (setq gnus-newsgroup-bookmarks
7768                 (delq old gnus-newsgroup-bookmarks))
7769           (gnus-message 6 "Removed bookmark."))
7770       (gnus-message 6 "No bookmark in current article."))))
7771
7772 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7773 (defun gnus-summary-mark-as-dormant (n)
7774   "Mark N articles forward as dormant.
7775 If N is negative, mark backward instead.  The difference between N and
7776 the actual number of articles marked is returned."
7777   (interactive "p")
7778   (gnus-summary-mark-forward n gnus-dormant-mark))
7779
7780 (defun gnus-summary-set-process-mark (article)
7781   "Set the process mark on ARTICLE and update the summary line."
7782   (setq gnus-newsgroup-processable
7783         (cons article
7784               (delq article gnus-newsgroup-processable)))
7785   (when (gnus-summary-goto-subject article)
7786     (gnus-summary-show-thread)
7787     (gnus-summary-goto-subject article)
7788     (gnus-summary-update-secondary-mark article)))
7789
7790 (defun gnus-summary-remove-process-mark (article)
7791   "Remove the process mark from ARTICLE and update the summary line."
7792   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7793   (when (gnus-summary-goto-subject article)
7794     (gnus-summary-show-thread)
7795     (gnus-summary-goto-subject article)
7796     (gnus-summary-update-secondary-mark article)))
7797
7798 (defun gnus-summary-set-saved-mark (article)
7799   "Set the process mark on ARTICLE and update the summary line."
7800   (push article gnus-newsgroup-saved)
7801   (when (gnus-summary-goto-subject article)
7802     (gnus-summary-update-secondary-mark article)))
7803
7804 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7805   "Mark N articles as read forwards.
7806 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7807 The difference between N and the actual number of articles marked is
7808 returned."
7809   (interactive "p")
7810   (gnus-summary-show-thread)
7811   (let ((backward (< n 0))
7812         (gnus-summary-goto-unread
7813          (and gnus-summary-goto-unread
7814               (not (eq gnus-summary-goto-unread 'never))
7815               (not (memq mark (list gnus-unread-mark
7816                                     gnus-ticked-mark gnus-dormant-mark)))))
7817         (n (abs n))
7818         (mark (or mark gnus-del-mark)))
7819     (while (and (> n 0)
7820                 (gnus-summary-mark-article nil mark no-expire)
7821                 (zerop (gnus-summary-next-subject
7822                         (if backward -1 1)
7823                         (and gnus-summary-goto-unread
7824                              (not (eq gnus-summary-goto-unread 'never)))
7825                         t)))
7826       (setq n (1- n)))
7827     (when (/= 0 n)
7828       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7829     (gnus-summary-recenter)
7830     (gnus-summary-position-point)
7831     (gnus-set-mode-line 'summary)
7832     n))
7833
7834 (defun gnus-summary-mark-article-as-read (mark)
7835   "Mark the current article quickly as read with MARK."
7836   (let ((article (gnus-summary-article-number)))
7837     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7838     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7839     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7840     (push (cons article mark) gnus-newsgroup-reads)
7841     ;; Possibly remove from cache, if that is used.
7842     (when gnus-use-cache
7843       (gnus-cache-enter-remove-article article))
7844     ;; Allow the backend to change the mark.
7845     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7846     ;; Check for auto-expiry.
7847     (when (and gnus-newsgroup-auto-expire
7848                (memq mark gnus-auto-expirable-marks))
7849       (setq mark gnus-expirable-mark)
7850       ;; Let the backend know about the mark change.
7851       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7852       (push article gnus-newsgroup-expirable))
7853     ;; Set the mark in the buffer.
7854     (gnus-summary-update-mark mark 'unread)
7855     t))
7856
7857 (defun gnus-summary-mark-article-as-unread (mark)
7858   "Mark the current article quickly as unread with MARK."
7859   (let* ((article (gnus-summary-article-number))
7860          (old-mark (gnus-summary-article-mark article)))
7861     ;; Allow the backend to change the mark.
7862     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7863     (if (eq mark old-mark)
7864         t
7865       (if (<= article 0)
7866           (progn
7867             (gnus-error 1 "Can't mark negative article numbers")
7868             nil)
7869         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7870         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7871         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7872         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7873         (cond ((= mark gnus-ticked-mark)
7874                (push article gnus-newsgroup-marked))
7875               ((= mark gnus-dormant-mark)
7876                (push article gnus-newsgroup-dormant))
7877               (t
7878                (push article gnus-newsgroup-unreads)))
7879         (gnus-pull article gnus-newsgroup-reads)
7880
7881         ;; See whether the article is to be put in the cache.
7882         (and gnus-use-cache
7883              (vectorp (gnus-summary-article-header article))
7884              (save-excursion
7885                (gnus-cache-possibly-enter-article
7886                 gnus-newsgroup-name article
7887                 (gnus-summary-article-header article)
7888                 (= mark gnus-ticked-mark)
7889                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7890
7891         ;; Fix the mark.
7892         (gnus-summary-update-mark mark 'unread)
7893         t))))
7894
7895 (defun gnus-summary-mark-article (&optional article mark no-expire)
7896   "Mark ARTICLE with MARK.  MARK can be any character.
7897 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7898 `??' (dormant) and `?E' (expirable).
7899 If MARK is nil, then the default character `?r' is used.
7900 If ARTICLE is nil, then the article on the current line will be
7901 marked."
7902   ;; The mark might be a string.
7903   (when (stringp mark)
7904     (setq mark (aref mark 0)))
7905   ;; If no mark is given, then we check auto-expiring.
7906   (when (null mark)
7907     (setq mark gnus-del-mark))
7908   (when (and (not no-expire)
7909              gnus-newsgroup-auto-expire
7910              (memq mark gnus-auto-expirable-marks))
7911     (setq mark gnus-expirable-mark))
7912   (let ((article (or article (gnus-summary-article-number)))
7913         (old-mark (gnus-summary-article-mark article)))
7914     ;; Allow the backend to change the mark.
7915     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7916     (if (eq mark old-mark)
7917         t
7918       (unless article
7919         (error "No article on current line"))
7920       (if (not (if (or (= mark gnus-unread-mark)
7921                        (= mark gnus-ticked-mark)
7922                        (= mark gnus-dormant-mark))
7923                    (gnus-mark-article-as-unread article mark)
7924                  (gnus-mark-article-as-read article mark)))
7925           t
7926         ;; See whether the article is to be put in the cache.
7927         (and gnus-use-cache
7928              (not (= mark gnus-canceled-mark))
7929              (vectorp (gnus-summary-article-header article))
7930              (save-excursion
7931                (gnus-cache-possibly-enter-article
7932                 gnus-newsgroup-name article
7933                 (gnus-summary-article-header article)
7934                 (= mark gnus-ticked-mark)
7935                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7936
7937         (when (gnus-summary-goto-subject article nil t)
7938           (let ((buffer-read-only nil))
7939             (gnus-summary-show-thread)
7940             ;; Fix the mark.
7941             (gnus-summary-update-mark mark 'unread)
7942             t))))))
7943
7944 (defun gnus-summary-update-secondary-mark (article)
7945   "Update the secondary (read, process, cache) mark."
7946   (gnus-summary-update-mark
7947    (cond ((memq article gnus-newsgroup-processable)
7948           gnus-process-mark)
7949          ((memq article gnus-newsgroup-cached)
7950           gnus-cached-mark)
7951          ((memq article gnus-newsgroup-replied)
7952           gnus-replied-mark)
7953          ((memq article gnus-newsgroup-saved)
7954           gnus-saved-mark)
7955          (t gnus-unread-mark))
7956    'replied)
7957   (when (gnus-visual-p 'summary-highlight 'highlight)
7958     (gnus-run-hooks 'gnus-summary-update-hook))
7959   t)
7960
7961 (defun gnus-summary-update-mark (mark type)
7962   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7963         (buffer-read-only nil))
7964     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7965     (when forward
7966       (when (looking-at "\r")
7967         (incf forward))
7968       (when (<= (+ forward (point)) (point-max))
7969         ;; Go to the right position on the line.
7970         (goto-char (+ forward (point)))
7971         ;; Replace the old mark with the new mark.
7972         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7973         ;; Optionally update the marks by some user rule.
7974         (when (eq type 'unread)
7975           (gnus-data-set-mark
7976            (gnus-data-find (gnus-summary-article-number)) mark)
7977           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7978
7979 (defun gnus-mark-article-as-read (article &optional mark)
7980   "Enter ARTICLE in the pertinent lists and remove it from others."
7981   ;; Make the article expirable.
7982   (let ((mark (or mark gnus-del-mark)))
7983     (if (= mark gnus-expirable-mark)
7984         (push article gnus-newsgroup-expirable)
7985       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7986     ;; Remove from unread and marked lists.
7987     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7988     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7989     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7990     (push (cons article mark) gnus-newsgroup-reads)
7991     ;; Possibly remove from cache, if that is used.
7992     (when gnus-use-cache
7993       (gnus-cache-enter-remove-article article))
7994     t))
7995
7996 (defun gnus-mark-article-as-unread (article &optional mark)
7997   "Enter ARTICLE in the pertinent lists and remove it from others."
7998   (let ((mark (or mark gnus-ticked-mark)))
7999     (if (<= article 0)
8000         (progn
8001           (gnus-error 1 "Can't mark negative article numbers")
8002           nil)
8003       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8004             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8005             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8006             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8007
8008       ;; Unsuppress duplicates?
8009       (when gnus-suppress-duplicates
8010         (gnus-dup-unsuppress-article article))
8011
8012       (cond ((= mark gnus-ticked-mark)
8013              (push article gnus-newsgroup-marked))
8014             ((= mark gnus-dormant-mark)
8015              (push article gnus-newsgroup-dormant))
8016             (t
8017              (push article gnus-newsgroup-unreads)))
8018       (gnus-pull article gnus-newsgroup-reads)
8019       t)))
8020
8021 (defalias 'gnus-summary-mark-as-unread-forward
8022   'gnus-summary-tick-article-forward)
8023 (make-obsolete 'gnus-summary-mark-as-unread-forward
8024                'gnus-summary-tick-article-forward)
8025 (defun gnus-summary-tick-article-forward (n)
8026   "Tick N articles forwards.
8027 If N is negative, tick backwards instead.
8028 The difference between N and the number of articles ticked is returned."
8029   (interactive "p")
8030   (gnus-summary-mark-forward n gnus-ticked-mark))
8031
8032 (defalias 'gnus-summary-mark-as-unread-backward
8033   'gnus-summary-tick-article-backward)
8034 (make-obsolete 'gnus-summary-mark-as-unread-backward
8035                'gnus-summary-tick-article-backward)
8036 (defun gnus-summary-tick-article-backward (n)
8037   "Tick N articles backwards.
8038 The difference between N and the number of articles ticked is returned."
8039   (interactive "p")
8040   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8041
8042 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8043 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8044 (defun gnus-summary-tick-article (&optional article clear-mark)
8045   "Mark current article as unread.
8046 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8047 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8048   (interactive)
8049   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8050                                        gnus-ticked-mark)))
8051
8052 (defun gnus-summary-mark-as-read-forward (n)
8053   "Mark N articles as read forwards.
8054 If N is negative, mark backwards instead.
8055 The difference between N and the actual number of articles marked is
8056 returned."
8057   (interactive "p")
8058   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8059
8060 (defun gnus-summary-mark-as-read-backward (n)
8061   "Mark the N articles as read backwards.
8062 The difference between N and the actual number of articles marked is
8063 returned."
8064   (interactive "p")
8065   (gnus-summary-mark-forward
8066    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8067
8068 (defun gnus-summary-mark-as-read (&optional article mark)
8069   "Mark current article as read.
8070 ARTICLE specifies the article to be marked as read.
8071 MARK specifies a string to be inserted at the beginning of the line."
8072   (gnus-summary-mark-article article mark))
8073
8074 (defun gnus-summary-clear-mark-forward (n)
8075   "Clear marks from N articles forward.
8076 If N is negative, clear backward instead.
8077 The difference between N and the number of marks cleared is returned."
8078   (interactive "p")
8079   (gnus-summary-mark-forward n gnus-unread-mark))
8080
8081 (defun gnus-summary-clear-mark-backward (n)
8082   "Clear marks from N articles backward.
8083 The difference between N and the number of marks cleared is returned."
8084   (interactive "p")
8085   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8086
8087 (defun gnus-summary-mark-unread-as-read ()
8088   "Intended to be used by `gnus-summary-mark-article-hook'."
8089   (when (memq gnus-current-article gnus-newsgroup-unreads)
8090     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8091
8092 (defun gnus-summary-mark-read-and-unread-as-read ()
8093   "Intended to be used by `gnus-summary-mark-article-hook'."
8094   (let ((mark (gnus-summary-article-mark)))
8095     (when (or (gnus-unread-mark-p mark)
8096               (gnus-read-mark-p mark))
8097       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8098
8099 (defun gnus-summary-mark-region-as-read (point mark all)
8100   "Mark all unread articles between point and mark as read.
8101 If given a prefix, mark all articles between point and mark as read,
8102 even ticked and dormant ones."
8103   (interactive "r\nP")
8104   (save-excursion
8105     (let (article)
8106       (goto-char point)
8107       (beginning-of-line)
8108       (while (and
8109               (< (point) mark)
8110               (progn
8111                 (when (or all
8112                           (memq (setq article (gnus-summary-article-number))
8113                                 gnus-newsgroup-unreads))
8114                   (gnus-summary-mark-article article gnus-del-mark))
8115                 t)
8116               (gnus-summary-find-next))))))
8117
8118 (defun gnus-summary-mark-below (score mark)
8119   "Mark articles with score less than SCORE with MARK."
8120   (interactive "P\ncMark: ")
8121   (setq score (if score
8122                   (prefix-numeric-value score)
8123                 (or gnus-summary-default-score 0)))
8124   (save-excursion
8125     (set-buffer gnus-summary-buffer)
8126     (goto-char (point-min))
8127     (while
8128         (progn
8129           (and (< (gnus-summary-article-score) score)
8130                (gnus-summary-mark-article nil mark))
8131           (gnus-summary-find-next)))))
8132
8133 (defun gnus-summary-kill-below (&optional score)
8134   "Mark articles with score below SCORE as read."
8135   (interactive "P")
8136   (gnus-summary-mark-below score gnus-killed-mark))
8137
8138 (defun gnus-summary-clear-above (&optional score)
8139   "Clear all marks from articles with score above SCORE."
8140   (interactive "P")
8141   (gnus-summary-mark-above score gnus-unread-mark))
8142
8143 (defun gnus-summary-tick-above (&optional score)
8144   "Tick all articles with score above SCORE."
8145   (interactive "P")
8146   (gnus-summary-mark-above score gnus-ticked-mark))
8147
8148 (defun gnus-summary-mark-above (score mark)
8149   "Mark articles with score over SCORE with MARK."
8150   (interactive "P\ncMark: ")
8151   (setq score (if score
8152                   (prefix-numeric-value score)
8153                 (or gnus-summary-default-score 0)))
8154   (save-excursion
8155     (set-buffer gnus-summary-buffer)
8156     (goto-char (point-min))
8157     (while (and (progn
8158                   (when (> (gnus-summary-article-score) score)
8159                     (gnus-summary-mark-article nil mark))
8160                   t)
8161                 (gnus-summary-find-next)))))
8162
8163 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8164 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8165 (defun gnus-summary-limit-include-expunged (&optional no-error)
8166   "Display all the hidden articles that were expunged for low scores."
8167   (interactive)
8168   (let ((buffer-read-only nil))
8169     (let ((scored gnus-newsgroup-scored)
8170           headers h)
8171       (while scored
8172         (unless (gnus-summary-goto-subject (caar scored))
8173           (and (setq h (gnus-summary-article-header (caar scored)))
8174                (< (cdar scored) gnus-summary-expunge-below)
8175                (push h headers)))
8176         (setq scored (cdr scored)))
8177       (if (not headers)
8178           (when (not no-error)
8179             (error "No expunged articles hidden"))
8180         (goto-char (point-min))
8181         (gnus-summary-prepare-unthreaded (nreverse headers))
8182         (goto-char (point-min))
8183         (gnus-summary-position-point)
8184         t))))
8185
8186 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8187   "Mark all unread articles in this newsgroup as read.
8188 If prefix argument ALL is non-nil, ticked and dormant articles will
8189 also be marked as read.
8190 If QUIETLY is non-nil, no questions will be asked.
8191 If TO-HERE is non-nil, it should be a point in the buffer.  All
8192 articles before this point will be marked as read.
8193 Note that this function will only catch up the unread article
8194 in the current summary buffer limitation.
8195 The number of articles marked as read is returned."
8196   (interactive "P")
8197   (prog1
8198       (save-excursion
8199         (when (or quietly
8200                   (not gnus-interactive-catchup) ;Without confirmation?
8201                   gnus-expert-user
8202                   (gnus-y-or-n-p
8203                    (if all
8204                        "Mark absolutely all articles as read? "
8205                      "Mark all unread articles as read? ")))
8206           (if (and not-mark
8207                    (not gnus-newsgroup-adaptive)
8208                    (not gnus-newsgroup-auto-expire)
8209                    (not gnus-suppress-duplicates)
8210                    (or (not gnus-use-cache)
8211                        (eq gnus-use-cache 'passive)))
8212               (progn
8213                 (when all
8214                   (setq gnus-newsgroup-marked nil
8215                         gnus-newsgroup-dormant nil))
8216                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8217             ;; We actually mark all articles as canceled, which we
8218             ;; have to do when using auto-expiry or adaptive scoring.
8219             (gnus-summary-show-all-threads)
8220             (when (gnus-summary-first-subject (not all) t)
8221               (while (and
8222                       (if to-here (< (point) to-here) t)
8223                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8224                       (gnus-summary-find-next (not all) nil nil t))))
8225             (gnus-set-mode-line 'summary))
8226           t))
8227     (gnus-summary-position-point)))
8228
8229 (defun gnus-summary-catchup-to-here (&optional all)
8230   "Mark all unticked articles before the current one as read.
8231 If ALL is non-nil, also mark ticked and dormant articles as read."
8232   (interactive "P")
8233   (save-excursion
8234     (gnus-save-hidden-threads
8235       (let ((beg (point)))
8236         ;; We check that there are unread articles.
8237         (when (or all (gnus-summary-find-prev))
8238           (gnus-summary-catchup all t beg)))))
8239   (gnus-summary-position-point))
8240
8241 (defun gnus-summary-catchup-all (&optional quietly)
8242   "Mark all articles in this newsgroup as read."
8243   (interactive "P")
8244   (gnus-summary-catchup t quietly))
8245
8246 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8247   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8248 If prefix argument ALL is non-nil, all articles are marked as read."
8249   (interactive "P")
8250   (when (gnus-summary-catchup all quietly nil 'fast)
8251     ;; Select next newsgroup or exit.
8252     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8253              (eq gnus-auto-select-next 'quietly))
8254         (gnus-summary-next-group nil)
8255       (gnus-summary-exit))))
8256
8257 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8258   "Mark all articles in this newsgroup as read, and then exit."
8259   (interactive "P")
8260   (gnus-summary-catchup-and-exit t quietly))
8261
8262 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8263 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8264   "Mark all articles in this group as read and select the next group.
8265 If given a prefix, mark all articles, unread as well as ticked, as
8266 read."
8267   (interactive "P")
8268   (save-excursion
8269     (gnus-summary-catchup all))
8270   (gnus-summary-next-article t nil nil t))
8271
8272 ;; Thread-based commands.
8273
8274 (defun gnus-summary-articles-in-thread (&optional article)
8275   "Return a list of all articles in the current thread.
8276 If ARTICLE is non-nil, return all articles in the thread that starts
8277 with that article."
8278   (let* ((article (or article (gnus-summary-article-number)))
8279          (data (gnus-data-find-list article))
8280          (top-level (gnus-data-level (car data)))
8281          (top-subject
8282           (cond ((null gnus-thread-operation-ignore-subject)
8283                  (gnus-simplify-subject-re
8284                   (mail-header-subject (gnus-data-header (car data)))))
8285                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8286                  (gnus-simplify-subject-fuzzy
8287                   (mail-header-subject (gnus-data-header (car data)))))
8288                 (t nil)))
8289          (end-point (save-excursion
8290                       (if (gnus-summary-go-to-next-thread)
8291                           (point) (point-max))))
8292          articles)
8293     (while (and data
8294                 (< (gnus-data-pos (car data)) end-point))
8295       (when (or (not top-subject)
8296                 (string= top-subject
8297                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8298                              (gnus-simplify-subject-fuzzy
8299                               (mail-header-subject
8300                                (gnus-data-header (car data))))
8301                            (gnus-simplify-subject-re
8302                             (mail-header-subject
8303                              (gnus-data-header (car data)))))))
8304         (push (gnus-data-number (car data)) articles))
8305       (unless (and (setq data (cdr data))
8306                    (> (gnus-data-level (car data)) top-level))
8307         (setq data nil)))
8308     ;; Return the list of articles.
8309     (nreverse articles)))
8310
8311 (defun gnus-summary-rethread-current ()
8312   "Rethread the thread the current article is part of."
8313   (interactive)
8314   (let* ((gnus-show-threads t)
8315          (article (gnus-summary-article-number))
8316          (id (mail-header-id (gnus-summary-article-header)))
8317          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8318     (unless id
8319       (error "No article on the current line"))
8320     (gnus-rebuild-thread id)
8321     (gnus-summary-goto-subject article)))
8322
8323 (defun gnus-summary-reparent-thread ()
8324   "Make the current article child of the marked (or previous) article.
8325
8326 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8327 is non-nil or the Subject: of both articles are the same."
8328   (interactive)
8329   (unless (not (gnus-group-read-only-p))
8330     (error "The current newsgroup does not support article editing"))
8331   (unless (<= (length gnus-newsgroup-processable) 1)
8332     (error "No more than one article may be marked"))
8333   (save-window-excursion
8334     (let ((gnus-article-buffer " *reparent*")
8335           (current-article (gnus-summary-article-number))
8336           ;; First grab the marked article, otherwise one line up.
8337           (parent-article (if (not (null gnus-newsgroup-processable))
8338                               (car gnus-newsgroup-processable)
8339                             (save-excursion
8340                               (if (eq (forward-line -1) 0)
8341                                   (gnus-summary-article-number)
8342                                 (error "Beginning of summary buffer"))))))
8343       (unless (not (eq current-article parent-article))
8344         (error "An article may not be self-referential"))
8345       (let ((message-id (mail-header-id
8346                          (gnus-summary-article-header parent-article))))
8347         (unless (and message-id (not (equal message-id "")))
8348           (error "No message-id in desired parent"))
8349         (gnus-with-article current-article
8350           (goto-char (point-min))
8351           (if (re-search-forward "^References: " nil t)
8352               (progn
8353                 (re-search-forward "^[^ \t]" nil t)
8354                 (forward-line -1)
8355                 (end-of-line)
8356                 (insert " " message-id))
8357             (insert "References: " message-id "\n")))
8358         (set-buffer gnus-summary-buffer)
8359         (gnus-summary-unmark-all-processable)
8360         (gnus-summary-update-article current-article)
8361         (gnus-summary-rethread-current)
8362         (gnus-message 3 "Article %d is now the child of article %d"
8363                       current-article parent-article)))))
8364
8365 (defun gnus-summary-toggle-threads (&optional arg)
8366   "Toggle showing conversation threads.
8367 If ARG is positive number, turn showing conversation threads on."
8368   (interactive "P")
8369   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8370     (setq gnus-show-threads
8371           (if (null arg) (not gnus-show-threads)
8372             (> (prefix-numeric-value arg) 0)))
8373     (gnus-summary-prepare)
8374     (gnus-summary-goto-subject current)
8375     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8376     (gnus-summary-position-point)))
8377
8378 (defun gnus-summary-show-all-threads ()
8379   "Show all threads."
8380   (interactive)
8381   (save-excursion
8382     (let ((buffer-read-only nil))
8383       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8384   (gnus-summary-position-point))
8385
8386 (defun gnus-summary-show-thread ()
8387   "Show thread subtrees.
8388 Returns nil if no thread was there to be shown."
8389   (interactive)
8390   (let ((buffer-read-only nil)
8391         (orig (point))
8392         ;; first goto end then to beg, to have point at beg after let
8393         (end (progn (end-of-line) (point)))
8394         (beg (progn (beginning-of-line) (point))))
8395     (prog1
8396         ;; Any hidden lines here?
8397         (search-forward "\r" end t)
8398       (subst-char-in-region beg end ?\^M ?\n t)
8399       (goto-char orig)
8400       (gnus-summary-position-point))))
8401
8402 (defun gnus-summary-hide-all-threads ()
8403   "Hide all thread subtrees."
8404   (interactive)
8405   (save-excursion
8406     (goto-char (point-min))
8407     (gnus-summary-hide-thread)
8408     (while (zerop (gnus-summary-next-thread 1 t))
8409       (gnus-summary-hide-thread)))
8410   (gnus-summary-position-point))
8411
8412 (defun gnus-summary-hide-thread ()
8413   "Hide thread subtrees.
8414 Returns nil if no threads were there to be hidden."
8415   (interactive)
8416   (let ((buffer-read-only nil)
8417         (start (point))
8418         (article (gnus-summary-article-number)))
8419     (goto-char start)
8420     ;; Go forward until either the buffer ends or the subthread
8421     ;; ends.
8422     (when (and (not (eobp))
8423                (or (zerop (gnus-summary-next-thread 1 t))
8424                    (goto-char (point-max))))
8425       (prog1
8426           (if (and (> (point) start)
8427                    (search-backward "\n" start t))
8428               (progn
8429                 (subst-char-in-region start (point) ?\n ?\^M)
8430                 (gnus-summary-goto-subject article))
8431             (goto-char start)
8432             nil)
8433         ;;(gnus-summary-position-point)
8434         ))))
8435
8436 (defun gnus-summary-go-to-next-thread (&optional previous)
8437   "Go to the same level (or less) next thread.
8438 If PREVIOUS is non-nil, go to previous thread instead.
8439 Return the article number moved to, or nil if moving was impossible."
8440   (let ((level (gnus-summary-thread-level))
8441         (way (if previous -1 1))
8442         (beg (point)))
8443     (forward-line way)
8444     (while (and (not (eobp))
8445                 (< level (gnus-summary-thread-level)))
8446       (forward-line way))
8447     (if (eobp)
8448         (progn
8449           (goto-char beg)
8450           nil)
8451       (setq beg (point))
8452       (prog1
8453           (gnus-summary-article-number)
8454         (goto-char beg)))))
8455
8456 (defun gnus-summary-next-thread (n &optional silent)
8457   "Go to the same level next N'th thread.
8458 If N is negative, search backward instead.
8459 Returns the difference between N and the number of skips actually
8460 done.
8461
8462 If SILENT, don't output messages."
8463   (interactive "p")
8464   (let ((backward (< n 0))
8465         (n (abs n)))
8466     (while (and (> n 0)
8467                 (gnus-summary-go-to-next-thread backward))
8468       (decf n))
8469     (unless silent
8470       (gnus-summary-position-point))
8471     (when (and (not silent) (/= 0 n))
8472       (gnus-message 7 "No more threads"))
8473     n))
8474
8475 (defun gnus-summary-prev-thread (n)
8476   "Go to the same level previous N'th thread.
8477 Returns the difference between N and the number of skips actually
8478 done."
8479   (interactive "p")
8480   (gnus-summary-next-thread (- n)))
8481
8482 (defun gnus-summary-go-down-thread ()
8483   "Go down one level in the current thread."
8484   (let ((children (gnus-summary-article-children)))
8485     (when children
8486       (gnus-summary-goto-subject (car children)))))
8487
8488 (defun gnus-summary-go-up-thread ()
8489   "Go up one level in the current thread."
8490   (let ((parent (gnus-summary-article-parent)))
8491     (when parent
8492       (gnus-summary-goto-subject parent))))
8493
8494 (defun gnus-summary-down-thread (n)
8495   "Go down thread N steps.
8496 If N is negative, go up instead.
8497 Returns the difference between N and how many steps down that were
8498 taken."
8499   (interactive "p")
8500   (let ((up (< n 0))
8501         (n (abs n)))
8502     (while (and (> n 0)
8503                 (if up (gnus-summary-go-up-thread)
8504                   (gnus-summary-go-down-thread)))
8505       (setq n (1- n)))
8506     (gnus-summary-position-point)
8507     (when (/= 0 n)
8508       (gnus-message 7 "Can't go further"))
8509     n))
8510
8511 (defun gnus-summary-up-thread (n)
8512   "Go up thread N steps.
8513 If N is negative, go up instead.
8514 Returns the difference between N and how many steps down that were
8515 taken."
8516   (interactive "p")
8517   (gnus-summary-down-thread (- n)))
8518
8519 (defun gnus-summary-top-thread ()
8520   "Go to the top of the thread."
8521   (interactive)
8522   (while (gnus-summary-go-up-thread))
8523   (gnus-summary-article-number))
8524
8525 (defun gnus-summary-kill-thread (&optional unmark)
8526   "Mark articles under current thread as read.
8527 If the prefix argument is positive, remove any kinds of marks.
8528 If the prefix argument is negative, tick articles instead."
8529   (interactive "P")
8530   (when unmark
8531     (setq unmark (prefix-numeric-value unmark)))
8532   (let ((articles (gnus-summary-articles-in-thread)))
8533     (save-excursion
8534       ;; Expand the thread.
8535       (gnus-summary-show-thread)
8536       ;; Mark all the articles.
8537       (while articles
8538         (gnus-summary-goto-subject (car articles))
8539         (cond ((null unmark)
8540                (gnus-summary-mark-article-as-read gnus-killed-mark))
8541               ((> unmark 0)
8542                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8543               (t
8544                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8545         (setq articles (cdr articles))))
8546     ;; Hide killed subtrees.
8547     (and (null unmark)
8548          gnus-thread-hide-killed
8549          (gnus-summary-hide-thread))
8550     ;; If marked as read, go to next unread subject.
8551     (when (null unmark)
8552       ;; Go to next unread subject.
8553       (gnus-summary-next-subject 1 t)))
8554   (gnus-set-mode-line 'summary))
8555
8556 ;; Summary sorting commands
8557
8558 (defun gnus-summary-sort-by-number (&optional reverse)
8559   "Sort the summary buffer by article number.
8560 Argument REVERSE means reverse order."
8561   (interactive "P")
8562   (gnus-summary-sort 'number reverse))
8563
8564 (defun gnus-summary-sort-by-author (&optional reverse)
8565   "Sort the summary buffer by author name alphabetically.
8566 If case-fold-search is non-nil, case of letters is ignored.
8567 Argument REVERSE means reverse order."
8568   (interactive "P")
8569   (gnus-summary-sort 'author reverse))
8570
8571 (defun gnus-summary-sort-by-subject (&optional reverse)
8572   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8573 If case-fold-search is non-nil, case of letters is ignored.
8574 Argument REVERSE means reverse order."
8575   (interactive "P")
8576   (gnus-summary-sort 'subject reverse))
8577
8578 (defun gnus-summary-sort-by-date (&optional reverse)
8579   "Sort the summary buffer by date.
8580 Argument REVERSE means reverse order."
8581   (interactive "P")
8582   (gnus-summary-sort 'date reverse))
8583
8584 (defun gnus-summary-sort-by-score (&optional reverse)
8585   "Sort the summary buffer by score.
8586 Argument REVERSE means reverse order."
8587   (interactive "P")
8588   (gnus-summary-sort 'score reverse))
8589
8590 (defun gnus-summary-sort-by-lines (&optional reverse)
8591   "Sort the summary buffer by article length.
8592 Argument REVERSE means reverse order."
8593   (interactive "P")
8594   (gnus-summary-sort 'lines reverse))
8595
8596 (defun gnus-summary-sort (predicate reverse)
8597   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8598   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8599          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8600          (gnus-thread-sort-functions
8601           (if (not reverse)
8602               thread
8603             `(lambda (t1 t2)
8604                (,thread t2 t1))))
8605          (gnus-article-sort-functions
8606           (if (not reverse)
8607               article
8608             `(lambda (t1 t2)
8609                (,article t2 t1))))
8610          (buffer-read-only)
8611          (gnus-summary-prepare-hook nil))
8612     ;; We do the sorting by regenerating the threads.
8613     (gnus-summary-prepare)
8614     ;; Hide subthreads if needed.
8615     (when (and gnus-show-threads gnus-thread-hide-subtree)
8616       (gnus-summary-hide-all-threads))))
8617
8618 ;; Summary saving commands.
8619
8620 (defun gnus-summary-save-article (&optional n not-saved)
8621   "Save the current article using the default saver function.
8622 If N is a positive number, save the N next articles.
8623 If N is a negative number, save the N previous articles.
8624 If N is nil and any articles have been marked with the process mark,
8625 save those articles instead.
8626 The variable `gnus-default-article-saver' specifies the saver function."
8627   (interactive "P")
8628   (let* ((articles (gnus-summary-work-articles n))
8629          (save-buffer (save-excursion
8630                         (nnheader-set-temp-buffer " *Gnus Save*")))
8631          (num (length articles))
8632          header file)
8633     (dolist (article articles)
8634       (setq header (gnus-summary-article-header article))
8635       (if (not (vectorp header))
8636           ;; This is a pseudo-article.
8637           (if (assq 'name header)
8638               (gnus-copy-file (cdr (assq 'name header)))
8639             (gnus-message 1 "Article %d is unsaveable" article))
8640         ;; This is a real article.
8641         (save-window-excursion
8642           (gnus-summary-select-article t nil nil article))
8643         (save-excursion
8644           (set-buffer save-buffer)
8645           (erase-buffer)
8646           (insert-buffer-substring gnus-original-article-buffer))
8647         (setq file (gnus-article-save save-buffer file num))
8648         (gnus-summary-remove-process-mark article)
8649         (unless not-saved
8650           (gnus-summary-set-saved-mark article))))
8651     (gnus-kill-buffer save-buffer)
8652     (gnus-summary-position-point)
8653     (gnus-set-mode-line 'summary)
8654     n))
8655
8656 (defun gnus-summary-pipe-output (&optional arg)
8657   "Pipe the current article to a subprocess.
8658 If N is a positive number, pipe the N next articles.
8659 If N is a negative number, pipe the N previous articles.
8660 If N is nil and any articles have been marked with the process mark,
8661 pipe those articles instead."
8662   (interactive "P")
8663   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8664     (gnus-summary-save-article arg t))
8665   (gnus-configure-windows 'pipe))
8666
8667 (defun gnus-summary-save-article-mail (&optional arg)
8668   "Append the current article to an mail file.
8669 If N is a positive number, save the N next articles.
8670 If N is a negative number, save the N previous articles.
8671 If N is nil and any articles have been marked with the process mark,
8672 save those articles instead."
8673   (interactive "P")
8674   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8675     (gnus-summary-save-article arg)))
8676
8677 (defun gnus-summary-save-article-rmail (&optional arg)
8678   "Append the current article to an rmail file.
8679 If N is a positive number, save the N next articles.
8680 If N is a negative number, save the N previous articles.
8681 If N is nil and any articles have been marked with the process mark,
8682 save those articles instead."
8683   (interactive "P")
8684   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8685     (gnus-summary-save-article arg)))
8686
8687 (defun gnus-summary-save-article-file (&optional arg)
8688   "Append the current article to a file.
8689 If N is a positive number, save the N next articles.
8690 If N is a negative number, save the N previous articles.
8691 If N is nil and any articles have been marked with the process mark,
8692 save those articles instead."
8693   (interactive "P")
8694   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8695     (gnus-summary-save-article arg)))
8696
8697 (defun gnus-summary-write-article-file (&optional arg)
8698   "Write the current article to a file, deleting the previous file.
8699 If N is a positive number, save the N next articles.
8700 If N is a negative number, save the N previous articles.
8701 If N is nil and any articles have been marked with the process mark,
8702 save those articles instead."
8703   (interactive "P")
8704   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8705     (gnus-summary-save-article arg)))
8706
8707 (defun gnus-summary-save-article-body-file (&optional arg)
8708   "Append the current article body to a file.
8709 If N is a positive number, save the N next articles.
8710 If N is a negative number, save the N previous articles.
8711 If N is nil and any articles have been marked with the process mark,
8712 save those articles instead."
8713   (interactive "P")
8714   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8715     (gnus-summary-save-article arg)))
8716
8717 (defun gnus-summary-pipe-message (program)
8718   "Pipe the current article through PROGRAM."
8719   (interactive "sProgram: ")
8720   (gnus-summary-select-article)
8721   (let ((mail-header-separator ""))
8722     (gnus-eval-in-buffer-window gnus-article-buffer
8723       (save-restriction
8724         (widen)
8725         (let ((start (window-start))
8726               buffer-read-only)
8727           (message-pipe-buffer-body program)
8728           (set-window-start (get-buffer-window (current-buffer)) start))))))
8729
8730 (defun gnus-get-split-value (methods)
8731   "Return a value based on the split METHODS."
8732   (let (split-name method result match)
8733     (when methods
8734       (save-excursion
8735         (set-buffer gnus-original-article-buffer)
8736         (save-restriction
8737           (nnheader-narrow-to-headers)
8738           (while methods
8739             (goto-char (point-min))
8740             (setq method (pop methods))
8741             (setq match (car method))
8742             (when (cond
8743                    ((stringp match)
8744                     ;; Regular expression.
8745                     (ignore-errors
8746                       (re-search-forward match nil t)))
8747                    ((gnus-functionp match)
8748                     ;; Function.
8749                     (save-restriction
8750                       (widen)
8751                       (setq result (funcall match gnus-newsgroup-name))))
8752                    ((consp match)
8753                     ;; Form.
8754                     (save-restriction
8755                       (widen)
8756                       (setq result (eval match)))))
8757               (setq split-name (append (cdr method) split-name))
8758               (cond ((stringp result)
8759                      (push (expand-file-name
8760                             result gnus-article-save-directory)
8761                            split-name))
8762                     ((consp result)
8763                      (setq split-name (append result split-name)))))))))
8764     split-name))
8765
8766 (defun gnus-valid-move-group-p (group)
8767   (and (boundp group)
8768        (symbol-name group)
8769        (symbol-value group)
8770        (memq 'respool
8771              (assoc (symbol-name
8772                      (car (gnus-find-method-for-group
8773                            (symbol-name group))))
8774                     gnus-valid-select-methods))))
8775
8776 (defun gnus-read-move-group-name (prompt default articles prefix)
8777   "Read a group name."
8778   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8779          (minibuffer-confirm-incomplete nil) ; XEmacs
8780          (prom
8781           (format "%s %s to:"
8782                   prompt
8783                   (if (> (length articles) 1)
8784                       (format "these %d articles" (length articles))
8785                     "this article")))
8786          (to-newsgroup
8787           (cond
8788            ((null split-name)
8789             (gnus-completing-read default prom
8790                                   gnus-active-hashtb
8791                                   'gnus-valid-move-group-p
8792                                   nil prefix
8793                                   'gnus-group-history))
8794            ((= 1 (length split-name))
8795             (gnus-completing-read (car split-name) prom
8796                                   gnus-active-hashtb
8797                                   'gnus-valid-move-group-p
8798                                   nil nil
8799                                   'gnus-group-history))
8800            (t
8801             (gnus-completing-read nil prom
8802                                   (mapcar (lambda (el) (list el))
8803                                           (nreverse split-name))
8804                                   nil nil nil
8805                                   'gnus-group-history)))))
8806     (when to-newsgroup
8807       (if (or (string= to-newsgroup "")
8808               (string= to-newsgroup prefix))
8809           (setq to-newsgroup default))
8810       (unless to-newsgroup
8811         (error "No group name entered"))
8812       (or (gnus-active to-newsgroup)
8813           (gnus-activate-group to-newsgroup)
8814           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8815                                      to-newsgroup))
8816               (or (and (gnus-request-create-group
8817                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8818                        (gnus-activate-group to-newsgroup nil nil
8819                                             (gnus-group-name-to-method
8820                                              to-newsgroup)))
8821                   (error "Couldn't create group %s" to-newsgroup)))
8822           (error "No such group: %s" to-newsgroup)))
8823     to-newsgroup))
8824
8825 ;; Summary extract commands
8826
8827 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8828   (let ((buffer-read-only nil)
8829         (article (gnus-summary-article-number))
8830         after-article b e)
8831     (unless (gnus-summary-goto-subject article)
8832       (error "No such article: %d" article))
8833     (gnus-summary-position-point)
8834     ;; If all commands are to be bunched up on one line, we collect
8835     ;; them here.
8836     (unless gnus-view-pseudos-separately
8837       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8838             files action)
8839         (while ps
8840           (setq action (cdr (assq 'action (car ps))))
8841           (setq files (list (cdr (assq 'name (car ps)))))
8842           (while (and ps (cdr ps)
8843                       (string= (or action "1")
8844                                (or (cdr (assq 'action (cadr ps))) "2")))
8845             (push (cdr (assq 'name (cadr ps))) files)
8846             (setcdr ps (cddr ps)))
8847           (when files
8848             (when (not (string-match "%s" action))
8849               (push " " files))
8850             (push " " files)
8851             (when (assq 'execute (car ps))
8852               (setcdr (assq 'execute (car ps))
8853                       (funcall (if (string-match "%s" action)
8854                                    'format 'concat)
8855                                action
8856                                (mapconcat
8857                                 (lambda (f)
8858                                   (if (equal f " ")
8859                                       f
8860                                     (mm-quote-arg f)))
8861                                 files " ")))))
8862           (setq ps (cdr ps)))))
8863     (if (and gnus-view-pseudos (not not-view))
8864         (while pslist
8865           (when (assq 'execute (car pslist))
8866             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8867                                   (eq gnus-view-pseudos 'not-confirm)))
8868           (setq pslist (cdr pslist)))
8869       (save-excursion
8870         (while pslist
8871           (setq after-article (or (cdr (assq 'article (car pslist)))
8872                                   (gnus-summary-article-number)))
8873           (gnus-summary-goto-subject after-article)
8874           (forward-line 1)
8875           (setq b (point))
8876           (insert "    " (file-name-nondirectory
8877                           (cdr (assq 'name (car pslist))))
8878                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8879           (setq e (point))
8880           (forward-line -1)             ; back to `b'
8881           (gnus-add-text-properties
8882            b (1- e) (list 'gnus-number gnus-reffed-article-number
8883                           gnus-mouse-face-prop gnus-mouse-face))
8884           (gnus-data-enter
8885            after-article gnus-reffed-article-number
8886            gnus-unread-mark b (car pslist) 0 (- e b))
8887           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8888           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8889           (setq pslist (cdr pslist)))))))
8890
8891 (defun gnus-pseudos< (p1 p2)
8892   (let ((c1 (cdr (assq 'action p1)))
8893         (c2 (cdr (assq 'action p2))))
8894     (and c1 c2 (string< c1 c2))))
8895
8896 (defun gnus-request-pseudo-article (props)
8897   (cond ((assq 'execute props)
8898          (gnus-execute-command (cdr (assq 'execute props)))))
8899   (let ((gnus-current-article (gnus-summary-article-number)))
8900     (gnus-run-hooks 'gnus-mark-article-hook)))
8901
8902 (defun gnus-execute-command (command &optional automatic)
8903   (save-excursion
8904     (gnus-article-setup-buffer)
8905     (set-buffer gnus-article-buffer)
8906     (setq buffer-read-only nil)
8907     (let ((command (if automatic command
8908                      (read-string "Command: " (cons command 0)))))
8909       (erase-buffer)
8910       (insert "$ " command "\n\n")
8911       (if gnus-view-pseudo-asynchronously
8912           (start-process "gnus-execute" (current-buffer) shell-file-name
8913                          shell-command-switch command)
8914         (call-process shell-file-name nil t nil
8915                       shell-command-switch command)))))
8916
8917 ;; Summary kill commands.
8918
8919 (defun gnus-summary-edit-global-kill (article)
8920   "Edit the \"global\" kill file."
8921   (interactive (list (gnus-summary-article-number)))
8922   (gnus-group-edit-global-kill article))
8923
8924 (defun gnus-summary-edit-local-kill ()
8925   "Edit a local kill file applied to the current newsgroup."
8926   (interactive)
8927   (setq gnus-current-headers (gnus-summary-article-header))
8928   (gnus-group-edit-local-kill
8929    (gnus-summary-article-number) gnus-newsgroup-name))
8930
8931 ;;; Header reading.
8932
8933 (defun gnus-read-header (id &optional header)
8934   "Read the headers of article ID and enter them into the Gnus system."
8935   (let ((group gnus-newsgroup-name)
8936         (gnus-override-method
8937          (and (gnus-news-group-p gnus-newsgroup-name)
8938               gnus-refer-article-method))
8939         where)
8940     ;; First we check to see whether the header in question is already
8941     ;; fetched.
8942     (if (stringp id)
8943         ;; This is a Message-ID.
8944         (setq header (or header (gnus-id-to-header id)))
8945       ;; This is an article number.
8946       (setq header (or header (gnus-summary-article-header id))))
8947     (if (and header
8948              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8949         ;; We have found the header.
8950         header
8951       ;; If this is a sparse article, we have to nix out its
8952       ;; previous entry in the thread hashtb.
8953       (when (and header
8954                  (gnus-summary-article-sparse-p (mail-header-number header)))
8955         (let* ((parent (gnus-parent-id (mail-header-references header)))
8956                (thread (and parent (gnus-id-to-thread parent))))
8957           (when thread
8958             (delq (assq header thread) thread))))
8959       ;; We have to really fetch the header to this article.
8960       (save-excursion
8961         (set-buffer nntp-server-buffer)
8962         (when (setq where (gnus-request-head id group))
8963           (nnheader-fold-continuation-lines)
8964           (goto-char (point-max))
8965           (insert ".\n")
8966           (goto-char (point-min))
8967           (insert "211 ")
8968           (princ (cond
8969                   ((numberp id) id)
8970                   ((cdr where) (cdr where))
8971                   (header (mail-header-number header))
8972                   (t gnus-reffed-article-number))
8973                  (current-buffer))
8974           (insert " Article retrieved.\n"))
8975         (if (or (not where)
8976                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8977             ()                          ; Malformed head.
8978           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8979             (when (and (stringp id)
8980                        (not (string= (gnus-group-real-name group)
8981                                      (car where))))
8982               ;; If we fetched by Message-ID and the article came
8983               ;; from a different group, we fudge some bogus article
8984               ;; numbers for this article.
8985               (mail-header-set-number header gnus-reffed-article-number))
8986             (save-excursion
8987               (set-buffer gnus-summary-buffer)
8988               (decf gnus-reffed-article-number)
8989               (gnus-remove-header (mail-header-number header))
8990               (push header gnus-newsgroup-headers)
8991               (setq gnus-current-headers header)
8992               (push (mail-header-number header) gnus-newsgroup-limit)))
8993           header)))))
8994
8995 (defun gnus-remove-header (number)
8996   "Remove header NUMBER from `gnus-newsgroup-headers'."
8997   (if (and gnus-newsgroup-headers
8998            (= number (mail-header-number (car gnus-newsgroup-headers))))
8999       (pop gnus-newsgroup-headers)
9000     (let ((headers gnus-newsgroup-headers))
9001       (while (and (cdr headers)
9002                   (not (= number (mail-header-number (cadr headers)))))
9003         (pop headers))
9004       (when (cdr headers)
9005         (setcdr headers (cddr headers))))))
9006
9007 ;;;
9008 ;;; summary highlights
9009 ;;;
9010
9011 (defun gnus-highlight-selected-summary ()
9012   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9013   ;; Highlight selected article in summary buffer
9014   (when gnus-summary-selected-face
9015     (save-excursion
9016       (let* ((beg (progn (beginning-of-line) (point)))
9017              (end (progn (end-of-line) (point)))
9018              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9019              (from (if (get-text-property beg gnus-mouse-face-prop)
9020                        beg
9021                      (or (next-single-property-change
9022                           beg gnus-mouse-face-prop nil end)
9023                          beg)))
9024              (to
9025               (if (= from end)
9026                   (- from 2)
9027                 (or (next-single-property-change
9028                      from gnus-mouse-face-prop nil end)
9029                     end))))
9030         ;; If no mouse-face prop on line we will have to = from = end,
9031         ;; so we highlight the entire line instead.
9032         (when (= (+ to 2) from)
9033           (setq from beg)
9034           (setq to end))
9035         (if gnus-newsgroup-selected-overlay
9036             ;; Move old overlay.
9037             (gnus-move-overlay
9038              gnus-newsgroup-selected-overlay from to (current-buffer))
9039           ;; Create new overlay.
9040           (gnus-overlay-put
9041            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9042            'face gnus-summary-selected-face))))))
9043
9044 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9045 (defun gnus-summary-highlight-line ()
9046   "Highlight current line according to `gnus-summary-highlight'."
9047   (let* ((list gnus-summary-highlight)
9048          (p (point))
9049          (end (progn (end-of-line) (point)))
9050          ;; now find out where the line starts and leave point there.
9051          (beg (progn (beginning-of-line) (point)))
9052          (article (gnus-summary-article-number))
9053          (score (or (cdr (assq (or article gnus-current-article)
9054                                gnus-newsgroup-scored))
9055                     gnus-summary-default-score 0))
9056          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9057          (inhibit-read-only t))
9058     ;; Eval the cars of the lists until we find a match.
9059     (let ((default gnus-summary-default-score))
9060       (while (and list
9061                   (not (eval (caar list))))
9062         (setq list (cdr list))))
9063     (let ((face (cdar list)))
9064       (unless (eq face (get-text-property beg 'face))
9065         (gnus-put-text-property-excluding-characters-with-faces
9066          beg end 'face
9067          (setq face (if (boundp face) (symbol-value face) face)))
9068         (when gnus-summary-highlight-line-function
9069           (funcall gnus-summary-highlight-line-function article face))))
9070     (goto-char p)))
9071
9072 (defun gnus-update-read-articles (group unread &optional compute)
9073   "Update the list of read articles in GROUP."
9074   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9075          (entry (gnus-gethash group gnus-newsrc-hashtb))
9076          (info (nth 2 entry))
9077          (prev 1)
9078          (unread (sort (copy-sequence unread) '<))
9079          read)
9080     (if (or (not info) (not active))
9081         ;; There is no info on this group if it was, in fact,
9082         ;; killed.  Gnus stores no information on killed groups, so
9083         ;; there's nothing to be done.
9084         ;; One could store the information somewhere temporarily,
9085         ;; perhaps...  Hmmm...
9086         ()
9087       ;; Remove any negative articles numbers.
9088       (while (and unread (< (car unread) 0))
9089         (setq unread (cdr unread)))
9090       ;; Remove any expired article numbers
9091       (while (and unread (< (car unread) (car active)))
9092         (setq unread (cdr unread)))
9093       ;; Compute the ranges of read articles by looking at the list of
9094       ;; unread articles.
9095       (while unread
9096         (when (/= (car unread) prev)
9097           (push (if (= prev (1- (car unread))) prev
9098                   (cons prev (1- (car unread))))
9099                 read))
9100         (setq prev (1+ (car unread)))
9101         (setq unread (cdr unread)))
9102       (when (<= prev (cdr active))
9103         (push (cons prev (cdr active)) read))
9104       (setq read (if (> (length read) 1) (nreverse read) read))
9105       (if compute
9106           read
9107         (save-excursion
9108           (set-buffer gnus-group-buffer)
9109           (gnus-undo-register
9110             `(progn
9111                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9112                (gnus-info-set-read ',info ',(gnus-info-read info))
9113                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9114                (gnus-group-update-group ,group t))))
9115        ;; Propagate the read marks to the backend.
9116        (if (gnus-check-backend-function 'request-set-mark group)
9117            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9118                  (add (gnus-remove-from-range read (gnus-info-read info))))
9119              (when (or add del)
9120                (gnus-request-set-mark
9121                 group (delq nil (list (if add (list add 'add '(read)))
9122                                       (if del (list del 'del '(read)))))))))
9123         ;; Enter this list into the group info.
9124         (gnus-info-set-read info read)
9125         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9126         (gnus-get-unread-articles-in-group info (gnus-active group))
9127         t))))
9128
9129 (defun gnus-offer-save-summaries ()
9130   "Offer to save all active summary buffers."
9131   (save-excursion
9132     (let ((buflist (buffer-list))
9133           buffers bufname)
9134       ;; Go through all buffers and find all summaries.
9135       (while buflist
9136         (and (setq bufname (buffer-name (car buflist)))
9137              (string-match "Summary" bufname)
9138              (save-excursion
9139                (set-buffer bufname)
9140                ;; We check that this is, indeed, a summary buffer.
9141                (and (eq major-mode 'gnus-summary-mode)
9142                     ;; Also make sure this isn't bogus.
9143                     gnus-newsgroup-prepared
9144                     ;; Also make sure that this isn't a dead summary buffer.
9145                     (not gnus-dead-summary-mode)))
9146              (push bufname buffers))
9147         (setq buflist (cdr buflist)))
9148       ;; Go through all these summary buffers and offer to save them.
9149       (when buffers
9150         (map-y-or-n-p
9151          "Update summary buffer %s? "
9152          (lambda (buf)
9153            (switch-to-buffer buf)
9154            (gnus-summary-exit))
9155          buffers)))))
9156
9157 (defun gnus-summary-setup-default-charset ()
9158   "Setup newsgroup default charset."
9159   (let ((name (and gnus-newsgroup-name
9160                    (gnus-group-real-name gnus-newsgroup-name))))
9161     (setq gnus-newsgroup-charset
9162           (or (and gnus-newsgroup-name
9163                    (or (gnus-group-find-parameter gnus-newsgroup-name
9164                                                   'charset)
9165                        (let ((alist gnus-group-charset-alist)
9166                              elem (charset nil))
9167                          (while (setq elem (pop alist))
9168                            (when (and name
9169                                       (string-match (car elem) name))
9170                              (setq alist nil
9171                                    charset (cadr elem))))
9172                          charset)))
9173               gnus-default-charset))))
9174
9175 ;;;
9176 ;;; Mime Commands
9177 ;;;
9178
9179 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9180   "Display the current article buffer fully MIME-buttonized.
9181 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9182 treated as multipart/mixed."
9183   (interactive "P")
9184   (require 'gnus-art)
9185   (let ((gnus-unbuttonized-mime-types nil)
9186         (gnus-mime-display-multipart-as-mixed show-all-parts))
9187     (gnus-summary-show-article)))
9188
9189 (defun gnus-summary-repair-multipart (article)
9190   "Add a Content-Type header to a multipart article without one."
9191   (interactive (list (gnus-summary-article-number)))
9192   (gnus-with-article article
9193     (message-narrow-to-head)
9194     (goto-char (point-max))
9195     (widen)
9196     (when (search-forward "\n--" nil t)
9197       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9198         (message-narrow-to-head)
9199         (message-remove-header "Mime-Version")
9200         (message-remove-header "Content-Type")
9201         (goto-char (point-max))
9202         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9203                         separator))
9204         (insert "Mime-Version: 1.0\n")
9205         (widen))))
9206   (let (gnus-mark-article-hook)
9207     (gnus-summary-select-article t t nil 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