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