*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (require 'mm-decode)
38 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
39
40 (defcustom gnus-kill-summary-on-exit t
41   "*If non-nil, kill the summary buffer when you exit from it.
42 If nil, the summary will become a \"*Dead Summary*\" buffer, and
43 it will be killed sometime later."
44   :group 'gnus-summary-exit
45   :type 'boolean)
46
47 (defcustom gnus-fetch-old-headers nil
48   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
49 If an unread article in the group refers to an older, already read (or
50 just marked as read) article, the old article will not normally be
51 displayed in the Summary buffer.  If this variable is non-nil, Gnus
52 will attempt to grab the headers to the old articles, and thereby
53 build complete threads.  If it has the value `some', only enough
54 headers to connect otherwise loose threads will be displayed.  This
55 variable can also be a number.  In that case, no more than that number
56 of old headers will be fetched.  If it has the value `invisible', all
57 old headers will be fetched, but none will be displayed.
58
59 The server has to support NOV for any of this to work."
60   :group 'gnus-thread
61   :type '(choice (const :tag "off" nil)
62                  (const some)
63                  number
64                  (sexp :menu-tag "other" t)))
65
66 (defcustom gnus-refer-thread-limit 200
67   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
68 If t, fetch all the available old headers."
69   :group 'gnus-thread
70   :type '(choice number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-summary-make-false-root 'adopt
74   "*nil means that Gnus won't gather loose threads.
75 If the root of a thread has expired or been read in a previous
76 session, the information necessary to build a complete thread has been
77 lost.  Instead of having many small sub-threads from this original thread
78 scattered all over the summary buffer, Gnus can gather them.
79
80 If non-nil, Gnus will try to gather all loose sub-threads from an
81 original thread into one large thread.
82
83 If this variable is non-nil, it should be one of `none', `adopt',
84 `dummy' or `empty'.
85
86 If this variable is `none', Gnus will not make a false root, but just
87 present the sub-threads after another.
88 If this variable is `dummy', Gnus will create a dummy root that will
89 have all the sub-threads as children.
90 If this variable is `adopt', Gnus will make one of the \"children\"
91 the parent and mark all the step-children as such.
92 If this variable is `empty', the \"children\" are printed with empty
93 subject fields.  (Or rather, they will be printed with a string
94 given by the `gnus-summary-same-subject' variable.)"
95   :group 'gnus-thread
96   :type '(choice (const :tag "off" nil)
97                  (const none)
98                  (const dummy)
99                  (const adopt)
100                  (const empty)))
101
102 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
103   "*A regexp to match subjects to be excluded from loose thread gathering.
104 As loose thread gathering is done on subjects only, that means that
105 there can be many false gatherings performed.  By rooting out certain
106 common subjects, gathering might become saner."
107   :group 'gnus-thread
108   :type 'regexp)
109
110 (defcustom gnus-summary-gather-subject-limit nil
111   "*Maximum length of subject comparisons when gathering loose threads.
112 Use nil to compare full subjects.  Setting this variable to a low
113 number will help gather threads that have been corrupted by
114 newsreaders chopping off subject lines, but it might also mean that
115 unrelated articles that have subject that happen to begin with the
116 same few characters will be incorrectly gathered.
117
118 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
119 comparing subjects."
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const fuzzy)
123                  (sexp :menu-tag "on" t)))
124
125 (defcustom gnus-simplify-subject-functions nil
126   "List of functions taking a string argument that simplify subjects.
127 The functions are applied recursively.
128
129 Useful functions to put in this list include: `gnus-simplify-subject-re',
130 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
131   :group 'gnus-thread
132   :type '(repeat function))
133
134 (defcustom gnus-simplify-ignored-prefixes nil
135   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
136   :group 'gnus-thread
137   :type '(choice (const :tag "off" nil)
138                  regexp))
139
140 (defcustom gnus-build-sparse-threads nil
141   "*If non-nil, fill in the gaps in threads.
142 If `some', only fill in the gaps that are needed to tie loose threads
143 together.  If `more', fill in all leaf nodes that Gnus can find.  If
144 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const some)
148                  (const more)
149                  (sexp :menu-tag "all" t)))
150
151 (defcustom gnus-summary-thread-gathering-function
152   'gnus-gather-threads-by-subject
153   "*Function used for gathering loose threads.
154 There are two pre-defined functions: `gnus-gather-threads-by-subject',
155 which only takes Subjects into consideration; and
156 `gnus-gather-threads-by-references', which compared the References
157 headers of the articles to find matches."
158   :group 'gnus-thread
159   :type '(radio (function-item gnus-gather-threads-by-subject)
160                 (function-item gnus-gather-threads-by-references)
161                 (function :tag "other")))
162
163 (defcustom gnus-summary-same-subject ""
164   "*String indicating that the current article has the same subject as the previous.
165 This variable will only be used if the value of
166 `gnus-summary-make-false-root' is `empty'."
167   :group 'gnus-summary-format
168   :type 'string)
169
170 (defcustom gnus-summary-goto-unread t
171   "*If t, marking commands will go to the next unread article.
172 If `never', commands that usually go to the next unread article, will
173 go to the next article, whether it is read or not.
174 If nil, only the marking commands will go to the next (un)read article."
175   :group 'gnus-summary-marks
176   :link '(custom-manual "(gnus)Setting Marks")
177   :type '(choice (const :tag "off" nil)
178                  (const never)
179                  (sexp :menu-tag "on" t)))
180
181 (defcustom gnus-summary-default-score 0
182   "*Default article score level.
183 All scores generated by the score files will be added to this score.
184 If this variable is nil, scoring will be disabled."
185   :group 'gnus-score-default
186   :type '(choice (const :tag "disable")
187                  integer))
188
189 (defcustom gnus-summary-zcore-fuzz 0
190   "*Fuzziness factor for the zcore in the summary buffer.
191 Articles with scores closer than this to `gnus-summary-default-score'
192 will not be marked."
193   :group 'gnus-summary-format
194   :type 'integer)
195
196 (defcustom gnus-simplify-subject-fuzzy-regexp nil
197   "*Strings to be removed when doing fuzzy matches.
198 This can either be a regular expression or list of regular expressions
199 that will be removed from subject strings if fuzzy subject
200 simplification is selected."
201   :group 'gnus-thread
202   :type '(repeat regexp))
203
204 (defcustom gnus-show-threads t
205   "*If non-nil, display threads in summary mode."
206   :group 'gnus-thread
207   :type 'boolean)
208
209 (defcustom gnus-thread-hide-subtree nil
210   "*If non-nil, hide all threads initially.
211 If threads are hidden, you have to run the command
212 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
213 to expose hidden threads."
214   :group 'gnus-thread
215   :type 'boolean)
216
217 (defcustom gnus-thread-hide-killed t
218   "*If non-nil, hide killed threads automatically."
219   :group 'gnus-thread
220   :type 'boolean)
221
222 (defcustom gnus-thread-ignore-subject t
223   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
224 If nil, articles that have different subjects from their parents will
225 start separate threads."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-operation-ignore-subject t
230   "*If non-nil, subjects will be ignored when doing thread commands.
231 This affects commands like `gnus-summary-kill-thread' and
232 `gnus-summary-lower-thread'.
233
234 If this variable is nil, articles in the same thread with different
235 subjects will not be included in the operation in question.  If this
236 variable is `fuzzy', only articles that have subjects that are fuzzily
237 equal will be included."
238   :group 'gnus-thread
239   :type '(choice (const :tag "off" nil)
240                  (const fuzzy)
241                  (sexp :tag "on" t)))
242
243 (defcustom gnus-thread-indent-level 4
244   "*Number that says how much each sub-thread should be indented."
245   :group 'gnus-thread
246   :type 'integer)
247
248 (defcustom gnus-auto-extend-newsgroup t
249   "*If non-nil, extend newsgroup forward and backward when requested."
250   :group 'gnus-summary-choose
251   :type 'boolean)
252
253 (defcustom gnus-auto-select-first t
254   "*If nil, don't select the first unread article when entering a group.
255 If this variable is `best', select the highest-scored unread article
256 in the group.  If t, select the first unread article.
257
258 This variable can also be a function to place point on a likely
259 subject line.  Useful values include `gnus-summary-first-unread-subject', 
260 `gnus-summary-first-unread-article' and 
261 `gnus-summary-best-unread-article'.
262
263 If you want to prevent automatic selection of the first unread article
264 in some newsgroups, set the variable to nil in
265 `gnus-select-group-hook'."
266   :group 'gnus-group-select
267   :type '(choice (const :tag "none" nil)
268                  (const best)
269                  (sexp :menu-tag "first" t)
270                  (function-item gnus-summary-first-unread-subject)
271                  (function-item gnus-summary-first-unread-article)
272                  (function-item gnus-summary-best-unread-article)))
273
274 (defcustom gnus-auto-select-next t
275   "*If non-nil, offer to go to the next group from the end of the previous.
276 If the value is t and the next newsgroup is empty, Gnus will exit
277 summary mode and go back to group mode.  If the value is neither nil
278 nor t, Gnus will select the following unread newsgroup.  In
279 particular, if the value is the symbol `quietly', the next unread
280 newsgroup will be selected without any confirmation, and if it is
281 `almost-quietly', the next group will be selected without any
282 confirmation if you are located on the last article in the group.
283 Finally, if this variable is `slightly-quietly', the `Z n' command
284 will go to the next group without confirmation."
285   :group 'gnus-summary-maneuvering
286   :type '(choice (const :tag "off" nil)
287                  (const quietly)
288                  (const almost-quietly)
289                  (const slightly-quietly)
290                  (sexp :menu-tag "on" t)))
291
292 (defcustom gnus-auto-select-same nil
293   "*If non-nil, select the next article with the same subject.
294 If there are no more articles with the same subject, go to
295 the first unread article."
296   :group 'gnus-summary-maneuvering
297   :type 'boolean)
298
299 (defcustom gnus-summary-check-current nil
300   "*If non-nil, consider the current article when moving.
301 The \"unread\" movement commands will stay on the same line if the
302 current article is unread."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-auto-center-summary t
307   "*If non-nil, always center the current summary buffer.
308 In particular, if `vertical' do only vertical recentering.  If non-nil
309 and non-`vertical', do both horizontal and vertical recentering."
310   :group 'gnus-summary-maneuvering
311   :type '(choice (const :tag "none" nil)
312                  (const vertical)
313                  (integer :tag "height")
314                  (sexp :menu-tag "both" t)))
315
316 (defcustom gnus-show-all-headers nil
317   "*If non-nil, don't hide any headers."
318   :group 'gnus-article-hiding
319   :group 'gnus-article-headers
320   :type 'boolean)
321
322 (defcustom gnus-summary-ignore-duplicates nil
323   "*If non-nil, ignore articles with identical Message-ID headers."
324   :group 'gnus-summary
325   :type 'boolean)
326
327 (defcustom gnus-single-article-buffer t
328   "*If non-nil, display all articles in the same buffer.
329 If nil, each group will get its own article buffer."
330   :group 'gnus-article-various
331   :type 'boolean)
332
333 (defcustom gnus-break-pages t
334   "*If non-nil, do page breaking on articles.
335 The page delimiter is specified by the `gnus-page-delimiter'
336 variable."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-move-split-methods nil
341   "*Variable used to suggest where articles are to be moved to.
342 It uses the same syntax as the `gnus-split-methods' variable."
343   :group 'gnus-summary-mail
344   :type '(repeat (choice (list :value (fun) function)
345                          (cons :value ("" "") regexp (repeat string))
346                          (sexp :value nil))))
347
348 (defcustom gnus-unread-mark ? 
349   "*Mark used for unread articles."
350   :group 'gnus-summary-marks
351   :type 'character)
352
353 (defcustom gnus-ticked-mark ?!
354   "*Mark used for ticked articles."
355   :group 'gnus-summary-marks
356   :type 'character)
357
358 (defcustom gnus-dormant-mark ??
359   "*Mark used for dormant articles."
360   :group 'gnus-summary-marks
361   :type 'character)
362
363 (defcustom gnus-del-mark ?r
364   "*Mark used for del'd articles."
365   :group 'gnus-summary-marks
366   :type 'character)
367
368 (defcustom gnus-read-mark ?R
369   "*Mark used for read articles."
370   :group 'gnus-summary-marks
371   :type 'character)
372
373 (defcustom gnus-expirable-mark ?E
374   "*Mark used for expirable articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-killed-mark ?K
379   "*Mark used for killed articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-souped-mark ?F
384   "*Mark used for killed articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-kill-file-mark ?X
389   "*Mark used for articles killed by kill files."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-low-score-mark ?Y
394   "*Mark used for articles with a low score."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-catchup-mark ?C
399   "*Mark used for articles that are caught up."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-replied-mark ?A
404   "*Mark used for articles that have been replied to."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-cached-mark ?*
409   "*Mark used for articles that are in the cache."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-saved-mark ?S
414   "*Mark used for articles that have been saved to."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-ancient-mark ?O
419   "*Mark used for ancient articles."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-sparse-mark ?Q
424   "*Mark used for sparsely reffed articles."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-canceled-mark ?G
429   "*Mark used for canceled articles."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-duplicate-mark ?M
434   "*Mark used for duplicate articles."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-undownloaded-mark ?@
439   "*Mark used for articles that weren't downloaded."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-downloadable-mark ?%
444   "*Mark used for articles that are to be downloaded."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-unsendable-mark ?=
449   "*Mark used for articles that won't be sent."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-score-over-mark ?+
454   "*Score mark used for articles with high scores."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-score-below-mark ?-
459   "*Score mark used for articles with low scores."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-empty-thread-mark ? 
464   "*There is no thread under the article."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-not-empty-thread-mark ?=
469   "*There is a thread under the article."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-view-pseudo-asynchronously nil
474   "*If non-nil, Gnus will view pseudo-articles asynchronously."
475   :group 'gnus-extract-view
476   :type 'boolean)
477
478 (defcustom gnus-auto-expirable-marks
479   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
480         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
481         gnus-souped-mark gnus-duplicate-mark)
482   "*The list of marks converted into expiration if a group is auto-expirable."
483   :group 'gnus-summary
484   :type '(repeat character))
485
486 (defcustom gnus-inhibit-user-auto-expire t
487   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
488   :group 'gnus-summary
489   :type 'boolean)
490
491 (defcustom gnus-view-pseudos nil
492   "*If `automatic', pseudo-articles will be viewed automatically.
493 If `not-confirm', pseudos will be viewed automatically, and the user
494 will not be asked to confirm the command."
495   :group 'gnus-extract-view
496   :type '(choice (const :tag "off" nil)
497                  (const automatic)
498                  (const not-confirm)))
499
500 (defcustom gnus-view-pseudos-separately t
501   "*If non-nil, one pseudo-article will be created for each file to be viewed.
502 If nil, all files that use the same viewing command will be given as a
503 list of parameters to that command."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-insert-pseudo-articles t
508   "*If non-nil, insert pseudo-articles when decoding articles."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-summary-dummy-line-format
513   "  %(:                          :%) %S\n"
514   "*The format specification for the dummy roots in the summary buffer.
515 It works along the same lines as a normal formatting string,
516 with some simple extensions.
517
518 %S  The subject"
519   :group 'gnus-threading
520   :type 'string)
521
522 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
523   "*The format specification for the summary mode line.
524 It works along the same lines as a normal formatting string,
525 with some simple extensions:
526
527 %G  Group name
528 %p  Unprefixed group name
529 %A  Current article number
530 %z  Current article score
531 %V  Gnus version
532 %U  Number of unread articles in the group
533 %e  Number of unselected articles in the group
534 %Z  A string with unread/unselected article counts
535 %g  Shortish group name
536 %S  Subject of the current article
537 %u  User-defined spec
538 %s  Current score file name
539 %d  Number of dormant articles
540 %r  Number of articles that have been marked as read in this session
541 %E  Number of articles expunged by the score files"
542   :group 'gnus-summary-format
543   :type 'string)
544
545 (defcustom gnus-summary-mark-below 0
546   "*Mark all articles with a score below this variable as read.
547 This variable is local to each summary buffer and usually set by the
548 score file."
549   :group 'gnus-score-default
550   :type 'integer)
551
552 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
553   "*List of functions used for sorting articles in the summary buffer.
554 This variable is only used when not using a threaded display."
555   :group 'gnus-summary-sort
556   :type '(repeat (choice (function-item gnus-article-sort-by-number)
557                          (function-item gnus-article-sort-by-author)
558                          (function-item gnus-article-sort-by-subject)
559                          (function-item gnus-article-sort-by-date)
560                          (function-item gnus-article-sort-by-score)
561                          (function :tag "other"))))
562
563 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
564   "*List of functions used for sorting threads in the summary buffer.
565 By default, threads are sorted by article number.
566
567 Each function takes two threads and return non-nil if the first thread
568 should be sorted before the other.  If you use more than one function,
569 the primary sort function should be the last.  You should probably
570 always include `gnus-thread-sort-by-number' in the list of sorting
571 functions -- preferably first.
572
573 Ready-made functions include `gnus-thread-sort-by-number',
574 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
575 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
576 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
577   :group 'gnus-summary-sort
578   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
579                          (function-item gnus-thread-sort-by-author)
580                          (function-item gnus-thread-sort-by-subject)
581                          (function-item gnus-thread-sort-by-date)
582                          (function-item gnus-thread-sort-by-score)
583                          (function-item gnus-thread-sort-by-total-score)
584                          (function :tag "other"))))
585
586 (defcustom gnus-thread-score-function '+
587   "*Function used for calculating the total score of a thread.
588
589 The function is called with the scores of the article and each
590 subthread and should then return the score of the thread.
591
592 Some functions you can use are `+', `max', or `min'."
593   :group 'gnus-summary-sort
594   :type 'function)
595
596 (defcustom gnus-summary-expunge-below nil
597   "All articles that have a score less than this variable will be expunged.
598 This variable is local to the summary buffers."
599   :group 'gnus-score-default
600   :type '(choice (const :tag "off" nil)
601                  integer))
602
603 (defcustom gnus-thread-expunge-below nil
604   "All threads that have a total score less than this variable will be expunged.
605 See `gnus-thread-score-function' for en explanation of what a
606 \"thread score\" is.
607
608 This variable is local to the summary buffers."
609   :group 'gnus-treading
610   :group 'gnus-score-default
611   :type '(choice (const :tag "off" nil)
612                  integer))
613
614 (defcustom gnus-summary-mode-hook nil
615   "*A hook for Gnus summary mode.
616 This hook is run before any variables are set in the summary buffer."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-menu-hook nil
621   "*Hook run after the creation of the summary mode menu."
622   :group 'gnus-summary-visual
623   :type 'hook)
624
625 (defcustom gnus-summary-exit-hook nil
626   "*A hook called on exit from the summary buffer.
627 It will be called with point in the group buffer."
628   :group 'gnus-summary-exit
629   :type 'hook)
630
631 (defcustom gnus-summary-prepare-hook nil
632   "*A hook called after the summary buffer has been generated.
633 If you want to modify the summary buffer, you can use this hook."
634   :group 'gnus-summary-various
635   :type 'hook)
636
637 (defcustom gnus-summary-prepared-hook nil
638   "*A hook called as the last thing after the summary buffer has been generated."
639   :group 'gnus-summary-various
640   :type 'hook)
641
642 (defcustom gnus-summary-generate-hook nil
643   "*A hook run just before generating the summary buffer.
644 This hook is commonly used to customize threading variables and the
645 like."
646   :group 'gnus-summary-various
647   :type 'hook)
648
649 (defcustom gnus-select-group-hook nil
650   "*A hook called when a newsgroup is selected.
651
652 If you'd like to simplify subjects like the
653 `gnus-summary-next-same-subject' command does, you can use the
654 following hook:
655
656  (setq gnus-select-group-hook
657       (list
658         (lambda ()
659           (mapcar (lambda (header)
660                      (mail-header-set-subject
661                       header
662                       (gnus-simplify-subject
663                        (mail-header-subject header) 're-only)))
664                   gnus-newsgroup-headers))))"
665   :group 'gnus-group-select
666   :type 'hook)
667
668 (defcustom gnus-select-article-hook nil
669   "*A hook called when an article is selected."
670   :group 'gnus-summary-choose
671   :type 'hook)
672
673 (defcustom gnus-visual-mark-article-hook
674   (list 'gnus-highlight-selected-summary)
675   "*Hook run after selecting an article in the summary buffer.
676 It is meant to be used for highlighting the article in some way.  It
677 is not run if `gnus-visual' is nil."
678   :group 'gnus-summary-visual
679   :type 'hook)
680
681 (defcustom gnus-parse-headers-hook nil
682   "*A hook called before parsing the headers."
683   :group 'gnus-various
684   :type 'hook)
685
686 (defcustom gnus-exit-group-hook nil
687   "*A hook called when exiting (not quitting) summary mode."
688   :group 'gnus-various
689   :type 'hook)
690
691 (defcustom gnus-summary-update-hook
692   (list 'gnus-summary-highlight-line)
693   "*A hook called when a summary line is changed.
694 The hook will not be called if `gnus-visual' is nil.
695
696 The default function `gnus-summary-highlight-line' will
697 highlight the line according to the `gnus-summary-highlight'
698 variable."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
703   "*A hook called when an article is selected for the first time.
704 The hook is intended to mark an article as read (or unread)
705 automatically when it is selected."
706   :group 'gnus-summary-choose
707   :type 'hook)
708
709 (defcustom gnus-group-no-more-groups-hook nil
710   "*A hook run when returning to group mode having no more (unread) groups."
711   :group 'gnus-group-select
712   :type 'hook)
713
714 (defcustom gnus-ps-print-hook nil
715   "*A hook run before ps-printing something from Gnus."
716   :group 'gnus-summary
717   :type 'hook)
718
719 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
720   "Face used for highlighting the current article in the summary buffer."
721   :group 'gnus-summary-visual
722   :type 'face)
723
724 (defcustom gnus-summary-highlight
725   '(((= mark gnus-canceled-mark)
726      . gnus-summary-cancelled-face)
727     ((and (> score default)
728           (or (= mark gnus-dormant-mark)
729               (= mark gnus-ticked-mark)))
730      . gnus-summary-high-ticked-face)
731     ((and (< score default)
732           (or (= mark gnus-dormant-mark)
733               (= mark gnus-ticked-mark)))
734      . gnus-summary-low-ticked-face)
735     ((or (= mark gnus-dormant-mark)
736          (= mark gnus-ticked-mark))
737      . gnus-summary-normal-ticked-face)
738     ((and (> score default) (= mark gnus-ancient-mark))
739      . gnus-summary-high-ancient-face)
740     ((and (< score default) (= mark gnus-ancient-mark))
741      . gnus-summary-low-ancient-face)
742     ((= mark gnus-ancient-mark)
743      . gnus-summary-normal-ancient-face)
744     ((and (> score default) (= mark gnus-unread-mark))
745      . gnus-summary-high-unread-face)
746     ((and (< score default) (= mark gnus-unread-mark))
747      . gnus-summary-low-unread-face)
748     ((= mark gnus-unread-mark)
749      . gnus-summary-normal-unread-face)
750     ((and (> score default) (memq mark (list gnus-downloadable-mark
751                                              gnus-undownloaded-mark)))
752      . gnus-summary-high-unread-face)
753     ((and (< score default) (memq mark (list gnus-downloadable-mark
754                                              gnus-undownloaded-mark)))
755      . gnus-summary-low-unread-face)
756     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
757      . gnus-summary-normal-unread-face)
758     ((> score default)
759      . gnus-summary-high-read-face)
760     ((< score default)
761      . gnus-summary-low-read-face)
762     (t
763      . gnus-summary-normal-read-face))
764   "*Controls the highlighting of summary buffer lines.
765
766 A list of (FORM . FACE) pairs.  When deciding how a a particular
767 summary line should be displayed, each form is evaluated.  The content
768 of the face field after the first true form is used.  You can change
769 how those summary lines are displayed, by editing the face field.
770
771 You can use the following variables in the FORM field.
772
773 score:   The articles score
774 default: The default article score.
775 below:   The score below which articles are automatically marked as read.
776 mark:    The articles mark."
777   :group 'gnus-summary-visual
778   :type '(repeat (cons (sexp :tag "Form" nil)
779                        face)))
780
781 (defcustom gnus-alter-header-function nil
782   "Function called to allow alteration of article header structures.
783 The function is called with one parameter, the article header vector,
784 which it may alter in any way.")
785
786 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
787   "Variable that says which function should be used to decode a string with encoded words.")
788
789 (defcustom gnus-extra-headers nil
790   "*Extra headers to parse."
791   :group 'gnus-summary
792   :type '(repeat symbol))
793
794 (defcustom gnus-ignored-from-addresses
795   (and user-mail-address (regexp-quote user-mail-address))
796   "*Regexp of From headers that may be suppressed in favor of To headers."
797   :group 'gnus-summary
798   :type 'regexp)
799
800 (defcustom gnus-default-charset 'iso-8859-1
801   "Default charset assumed to be used when viewing non-ASCII characters.
802 This variable is used only in non-Mule Emacsen.")
803
804 (defcustom gnus-newsgroup-default-charset-alist 
805   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" . cn-big5)
806     ("^cn\\>\\|\\<chinese\\>" . cn-gb-2312)
807     ("^fj\\>\\|^japan\\>" . iso-2022-jp-2)
808     ("^relcom\\>" . koi8-r))
809   "Alist of Regexps (to match group names) and default charsets to be applied."
810   :type '(repeat (cons (regexp :tag "Group")
811                        (symbol :tag "Charset")))
812   :group 'gnus)
813
814 (defcustom gnus-newsgroup-iso-8859-1-forced-regexp 
815   "^tw\\>\\|^hk\\>\\|^cn\\>\\|\\<chinese\\>"
816   "Regexp of newsgroup in which ISO-8859-1 is forced to other charset."
817   :type 'regexp
818   :group 'gnus)
819
820 ;;; Internal variables
821
822 (defvar gnus-article-mime-handles nil)
823 (defvar gnus-article-decoded-p nil)
824 (defvar gnus-scores-exclude-files nil)
825 (defvar gnus-page-broken nil)
826 (defvar gnus-inhibit-mime-unbuttonizing nil)
827
828 (defvar gnus-original-article nil)
829 (defvar gnus-article-internal-prepare-hook nil)
830 (defvar gnus-newsgroup-process-stack nil)
831
832 (defvar gnus-thread-indent-array nil)
833 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
834
835 ;; Avoid highlighting in kill files.
836 (defvar gnus-summary-inhibit-highlight nil)
837 (defvar gnus-newsgroup-selected-overlay nil)
838 (defvar gnus-inhibit-limiting nil)
839 (defvar gnus-newsgroup-adaptive-score-file nil)
840 (defvar gnus-current-score-file nil)
841 (defvar gnus-current-move-group nil)
842 (defvar gnus-current-copy-group nil)
843 (defvar gnus-current-crosspost-group nil)
844
845 (defvar gnus-newsgroup-dependencies nil)
846 (defvar gnus-newsgroup-adaptive nil)
847 (defvar gnus-summary-display-article-function nil)
848 (defvar gnus-summary-highlight-line-function nil
849   "Function called after highlighting a summary line.")
850
851 (defvar gnus-summary-line-format-alist
852   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
853     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
854     (?s gnus-tmp-subject-or-nil ?s)
855     (?n gnus-tmp-name ?s)
856     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
857         ?s)
858     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
859             gnus-tmp-from) ?s)
860     (?F gnus-tmp-from ?s)
861     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
862     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
863     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
864     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
865     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
866     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
867     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
868     (?L gnus-tmp-lines ?d)
869     (?I gnus-tmp-indentation ?s)
870     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
871     (?R gnus-tmp-replied ?c)
872     (?\[ gnus-tmp-opening-bracket ?c)
873     (?\] gnus-tmp-closing-bracket ?c)
874     (?\> (make-string gnus-tmp-level ? ) ?s)
875     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
876     (?i gnus-tmp-score ?d)
877     (?z gnus-tmp-score-char ?c)
878     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
879     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
880     (?U gnus-tmp-unread ?c)
881     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
882     (?t (gnus-summary-number-of-articles-in-thread
883          (and (boundp 'thread) (car thread)) gnus-tmp-level)
884         ?d)
885     (?e (gnus-summary-number-of-articles-in-thread
886          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
887         ?c)
888     (?u gnus-tmp-user-defined ?s)
889     (?P (gnus-pick-line-number) ?d))
890   "An alist of format specifications that can appear in summary lines,
891 and what variables they correspond with, along with the type of the
892 variable (string, integer, character, etc).")
893
894 (defvar gnus-summary-dummy-line-format-alist
895   `((?S gnus-tmp-subject ?s)
896     (?N gnus-tmp-number ?d)
897     (?u gnus-tmp-user-defined ?s)))
898
899 (defvar gnus-summary-mode-line-format-alist
900   `((?G gnus-tmp-group-name ?s)
901     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
902     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
903     (?A gnus-tmp-article-number ?d)
904     (?Z gnus-tmp-unread-and-unselected ?s)
905     (?V gnus-version ?s)
906     (?U gnus-tmp-unread-and-unticked ?d)
907     (?S gnus-tmp-subject ?s)
908     (?e gnus-tmp-unselected ?d)
909     (?u gnus-tmp-user-defined ?s)
910     (?d (length gnus-newsgroup-dormant) ?d)
911     (?t (length gnus-newsgroup-marked) ?d)
912     (?r (length gnus-newsgroup-reads) ?d)
913     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
914     (?E gnus-newsgroup-expunged-tally ?d)
915     (?s (gnus-current-score-file-nondirectory) ?s)))
916
917 (defvar gnus-last-search-regexp nil
918   "Default regexp for article search command.")
919
920 (defvar gnus-last-shell-command nil
921   "Default shell command on article.")
922
923 (defvar gnus-newsgroup-begin nil)
924 (defvar gnus-newsgroup-end nil)
925 (defvar gnus-newsgroup-last-rmail nil)
926 (defvar gnus-newsgroup-last-mail nil)
927 (defvar gnus-newsgroup-last-folder nil)
928 (defvar gnus-newsgroup-last-file nil)
929 (defvar gnus-newsgroup-auto-expire nil)
930 (defvar gnus-newsgroup-active nil)
931
932 (defvar gnus-newsgroup-data nil)
933 (defvar gnus-newsgroup-data-reverse nil)
934 (defvar gnus-newsgroup-limit nil)
935 (defvar gnus-newsgroup-limits nil)
936
937 (defvar gnus-newsgroup-unreads nil
938   "List of unread articles in the current newsgroup.")
939
940 (defvar gnus-newsgroup-unselected nil
941   "List of unselected unread articles in the current newsgroup.")
942
943 (defvar gnus-newsgroup-reads nil
944   "Alist of read articles and article marks in the current newsgroup.")
945
946 (defvar gnus-newsgroup-expunged-tally nil)
947
948 (defvar gnus-newsgroup-marked nil
949   "List of ticked articles in the current newsgroup (a subset of unread art).")
950
951 (defvar gnus-newsgroup-killed nil
952   "List of ranges of articles that have been through the scoring process.")
953
954 (defvar gnus-newsgroup-cached nil
955   "List of articles that come from the article cache.")
956
957 (defvar gnus-newsgroup-saved nil
958   "List of articles that have been saved.")
959
960 (defvar gnus-newsgroup-kill-headers nil)
961
962 (defvar gnus-newsgroup-replied nil
963   "List of articles that have been replied to in the current newsgroup.")
964
965 (defvar gnus-newsgroup-expirable nil
966   "List of articles in the current newsgroup that can be expired.")
967
968 (defvar gnus-newsgroup-processable nil
969   "List of articles in the current newsgroup that can be processed.")
970
971 (defvar gnus-newsgroup-downloadable nil
972   "List of articles in the current newsgroup that can be processed.")
973
974 (defvar gnus-newsgroup-undownloaded nil
975   "List of articles in the current newsgroup that haven't been downloaded..")
976
977 (defvar gnus-newsgroup-unsendable nil
978   "List of articles in the current newsgroup that won't be sent.")
979
980 (defvar gnus-newsgroup-bookmarks nil
981   "List of articles in the current newsgroup that have bookmarks.")
982
983 (defvar gnus-newsgroup-dormant nil
984   "List of dormant articles in the current newsgroup.")
985
986 (defvar gnus-newsgroup-scored nil
987   "List of scored articles in the current newsgroup.")
988
989 (defvar gnus-newsgroup-headers nil
990   "List of article headers in the current newsgroup.")
991
992 (defvar gnus-newsgroup-threads nil)
993
994 (defvar gnus-newsgroup-prepared nil
995   "Whether the current group has been prepared properly.")
996
997 (defvar gnus-newsgroup-ancient nil
998   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
999
1000 (defvar gnus-newsgroup-sparse nil)
1001
1002 (defvar gnus-current-article nil)
1003 (defvar gnus-article-current nil)
1004 (defvar gnus-current-headers nil)
1005 (defvar gnus-have-all-headers nil)
1006 (defvar gnus-last-article nil)
1007 (defvar gnus-newsgroup-history nil)
1008
1009 (defvar gnus-newsgroup-default-charset nil)
1010 (defvar gnus-newsgroup-iso-8859-1-forced nil)
1011
1012 (defconst gnus-summary-local-variables
1013   '(gnus-newsgroup-name
1014     gnus-newsgroup-begin gnus-newsgroup-end
1015     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1016     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1017     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1018     gnus-newsgroup-unselected gnus-newsgroup-marked
1019     gnus-newsgroup-reads gnus-newsgroup-saved
1020     gnus-newsgroup-replied gnus-newsgroup-expirable
1021     gnus-newsgroup-processable gnus-newsgroup-killed
1022     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1023     gnus-newsgroup-unsendable
1024     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1025     gnus-newsgroup-headers gnus-newsgroup-threads
1026     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1027     gnus-current-article gnus-current-headers gnus-have-all-headers
1028     gnus-last-article gnus-article-internal-prepare-hook
1029     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1030     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1031     gnus-thread-expunge-below
1032     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1033     (gnus-summary-mark-below . global)
1034     gnus-newsgroup-active gnus-scores-exclude-files
1035     gnus-newsgroup-history gnus-newsgroup-ancient
1036     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1037     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1038     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1039     (gnus-newsgroup-expunged-tally . 0)
1040     gnus-cache-removable-articles gnus-newsgroup-cached
1041     gnus-newsgroup-data gnus-newsgroup-data-reverse
1042     gnus-newsgroup-limit gnus-newsgroup-limits
1043     gnus-newsgroup-default-charset gnus-newsgroup-iso-8859-1-forced)
1044   "Variables that are buffer-local to the summary buffers.")
1045
1046 ;; Byte-compiler warning.
1047 (defvar gnus-article-mode-map)
1048
1049 ;; MIME stuff.
1050
1051 (defvar gnus-decode-encoded-word-methods
1052   '(mail-decode-encoded-word-string)
1053   "List of methods used to decode encoded words.
1054
1055 This variable is a list of FUNCTION or (REGEXP . FUNCTION). If item is
1056 FUNCTION, FUNCTION will be apply to all newsgroups. If item is a
1057 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1058 whose names match REGEXP.
1059
1060 For example: 
1061 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1062  mail-decode-encoded-word-string 
1063  (\"chinese\" . rfc1843-decode-string))
1064 ")
1065
1066 (defvar gnus-decode-encoded-word-methods-cache nil)
1067
1068 (defun gnus-multi-decode-encoded-word-string (string)
1069   "Apply the functions from `gnus-encoded-word-methods' that match."
1070   (unless (and gnus-decode-encoded-word-methods-cache
1071                (eq gnus-newsgroup-name 
1072                    (car gnus-decode-encoded-word-methods-cache)))
1073     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1074     (mapc '(lambda (x) 
1075              (if (symbolp x)
1076                  (nconc gnus-decode-encoded-word-methods-cache (list x))
1077                (if (and gnus-newsgroup-name 
1078                         (string-match (car x) gnus-newsgroup-name))
1079                    (nconc gnus-decode-encoded-word-methods-cache 
1080                           (list (cdr x))))))
1081           gnus-decode-encoded-word-methods))
1082   (let ((xlist gnus-decode-encoded-word-methods-cache))
1083     (pop xlist)
1084     (while xlist
1085       (setq string (funcall (pop xlist) string))))
1086   string)
1087
1088 ;; Subject simplification.
1089
1090 (defun gnus-simplify-whitespace (str)
1091   "Remove excessive whitespace."
1092   (let ((mystr str))
1093     ;; Multiple spaces.
1094     (while (string-match "[ \t][ \t]+" mystr)
1095       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1096                           " "
1097                           (substring mystr (match-end 0)))))
1098     ;; Leading spaces.
1099     (when (string-match "^[ \t]+" mystr)
1100       (setq mystr (substring mystr (match-end 0))))
1101     ;; Trailing spaces.
1102     (when (string-match "[ \t]+$" mystr)
1103       (setq mystr (substring mystr 0 (match-beginning 0))))
1104     mystr))
1105
1106 (defsubst gnus-simplify-subject-re (subject)
1107   "Remove \"Re:\" from subject lines."
1108   (if (string-match "^[Rr][Ee]: *" subject)
1109       (substring subject (match-end 0))
1110     subject))
1111
1112 (defun gnus-simplify-subject (subject &optional re-only)
1113   "Remove `Re:' and words in parentheses.
1114 If RE-ONLY is non-nil, strip leading `Re:'s only."
1115   (let ((case-fold-search t))           ;Ignore case.
1116     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1117     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1118       (setq subject (substring subject (match-end 0))))
1119     ;; Remove uninteresting prefixes.
1120     (when (and (not re-only)
1121                gnus-simplify-ignored-prefixes
1122                (string-match gnus-simplify-ignored-prefixes subject))
1123       (setq subject (substring subject (match-end 0))))
1124     ;; Remove words in parentheses from end.
1125     (unless re-only
1126       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1127         (setq subject (substring subject 0 (match-beginning 0)))))
1128     ;; Return subject string.
1129     subject))
1130
1131 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1132 ;; all whitespace.
1133 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1134   (goto-char (point-min))
1135   (while (re-search-forward regexp nil t)
1136       (replace-match (or newtext ""))))
1137
1138 (defun gnus-simplify-buffer-fuzzy ()
1139   "Simplify string in the buffer fuzzily.
1140 The string in the accessible portion of the current buffer is simplified.
1141 It is assumed to be a single-line subject.
1142 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1143 matter is removed.  Additional things can be deleted by setting
1144 gnus-simplify-subject-fuzzy-regexp."
1145   (let ((case-fold-search t)
1146         (modified-tick))
1147     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1148
1149     (while (not (eq modified-tick (buffer-modified-tick)))
1150       (setq modified-tick (buffer-modified-tick))
1151       (cond
1152        ((listp gnus-simplify-subject-fuzzy-regexp)
1153         (mapcar 'gnus-simplify-buffer-fuzzy-step
1154                 gnus-simplify-subject-fuzzy-regexp))
1155        (gnus-simplify-subject-fuzzy-regexp
1156         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1157       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1158       (gnus-simplify-buffer-fuzzy-step
1159        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1160       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1161
1162     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1163     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1164     (gnus-simplify-buffer-fuzzy-step " $")
1165     (gnus-simplify-buffer-fuzzy-step "^ +")))
1166
1167 (defun gnus-simplify-subject-fuzzy (subject)
1168   "Simplify a subject string fuzzily.
1169 See `gnus-simplify-buffer-fuzzy' for details."
1170   (save-excursion
1171     (gnus-set-work-buffer)
1172     (let ((case-fold-search t))
1173       ;; Remove uninteresting prefixes.
1174       (when (and gnus-simplify-ignored-prefixes
1175                  (string-match gnus-simplify-ignored-prefixes subject))
1176         (setq subject (substring subject (match-end 0))))
1177       (insert subject)
1178       (inline (gnus-simplify-buffer-fuzzy))
1179       (buffer-string))))
1180
1181 (defsubst gnus-simplify-subject-fully (subject)
1182   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1183   (cond
1184    (gnus-simplify-subject-functions
1185     (gnus-map-function gnus-simplify-subject-functions subject))
1186    ((null gnus-summary-gather-subject-limit)
1187     (gnus-simplify-subject-re subject))
1188    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1189     (gnus-simplify-subject-fuzzy subject))
1190    ((numberp gnus-summary-gather-subject-limit)
1191     (gnus-limit-string (gnus-simplify-subject-re subject)
1192                        gnus-summary-gather-subject-limit))
1193    (t
1194     subject)))
1195
1196 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1197   "Check whether two subjects are equal.
1198 If optional argument simple-first is t, first argument is already
1199 simplified."
1200   (cond
1201    ((null simple-first)
1202     (equal (gnus-simplify-subject-fully s1)
1203            (gnus-simplify-subject-fully s2)))
1204    (t
1205     (equal s1
1206            (gnus-simplify-subject-fully s2)))))
1207
1208 (defun gnus-summary-bubble-group ()
1209   "Increase the score of the current group.
1210 This is a handy function to add to `gnus-summary-exit-hook' to
1211 increase the score of each group you read."
1212   (gnus-group-add-score gnus-newsgroup-name))
1213
1214 \f
1215 ;;;
1216 ;;; Gnus summary mode
1217 ;;;
1218
1219 (put 'gnus-summary-mode 'mode-class 'special)
1220
1221 (when t
1222   ;; Non-orthogonal keys
1223
1224   (gnus-define-keys gnus-summary-mode-map
1225     " " gnus-summary-next-page
1226     "\177" gnus-summary-prev-page
1227     [delete] gnus-summary-prev-page
1228     [backspace] gnus-summary-prev-page
1229     "\r" gnus-summary-scroll-up
1230     "\M-\r" gnus-summary-scroll-down
1231     "n" gnus-summary-next-unread-article
1232     "p" gnus-summary-prev-unread-article
1233     "N" gnus-summary-next-article
1234     "P" gnus-summary-prev-article
1235     "\M-\C-n" gnus-summary-next-same-subject
1236     "\M-\C-p" gnus-summary-prev-same-subject
1237     "\M-n" gnus-summary-next-unread-subject
1238     "\M-p" gnus-summary-prev-unread-subject
1239     "." gnus-summary-first-unread-article
1240     "," gnus-summary-best-unread-article
1241     "\M-s" gnus-summary-search-article-forward
1242     "\M-r" gnus-summary-search-article-backward
1243     "<" gnus-summary-beginning-of-article
1244     ">" gnus-summary-end-of-article
1245     "j" gnus-summary-goto-article
1246     "^" gnus-summary-refer-parent-article
1247     "\M-^" gnus-summary-refer-article
1248     "u" gnus-summary-tick-article-forward
1249     "!" gnus-summary-tick-article-forward
1250     "U" gnus-summary-tick-article-backward
1251     "d" gnus-summary-mark-as-read-forward
1252     "D" gnus-summary-mark-as-read-backward
1253     "E" gnus-summary-mark-as-expirable
1254     "\M-u" gnus-summary-clear-mark-forward
1255     "\M-U" gnus-summary-clear-mark-backward
1256     "k" gnus-summary-kill-same-subject-and-select
1257     "\C-k" gnus-summary-kill-same-subject
1258     "\M-\C-k" gnus-summary-kill-thread
1259     "\M-\C-l" gnus-summary-lower-thread
1260     "e" gnus-summary-edit-article
1261     "#" gnus-summary-mark-as-processable
1262     "\M-#" gnus-summary-unmark-as-processable
1263     "\M-\C-t" gnus-summary-toggle-threads
1264     "\M-\C-s" gnus-summary-show-thread
1265     "\M-\C-h" gnus-summary-hide-thread
1266     "\M-\C-f" gnus-summary-next-thread
1267     "\M-\C-b" gnus-summary-prev-thread
1268     "\M-\C-u" gnus-summary-up-thread
1269     "\M-\C-d" gnus-summary-down-thread
1270     "&" gnus-summary-execute-command
1271     "c" gnus-summary-catchup-and-exit
1272     "\C-w" gnus-summary-mark-region-as-read
1273     "\C-t" gnus-summary-toggle-truncation
1274     "?" gnus-summary-mark-as-dormant
1275     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1276     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1277     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1278     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1279     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1280     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1281     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1282     "=" gnus-summary-expand-window
1283     "\C-x\C-s" gnus-summary-reselect-current-group
1284     "\M-g" gnus-summary-rescan-group
1285     "w" gnus-summary-stop-page-breaking
1286     "\C-c\C-r" gnus-summary-caesar-message
1287     "f" gnus-summary-followup
1288     "F" gnus-summary-followup-with-original
1289     "C" gnus-summary-cancel-article
1290     "r" gnus-summary-reply
1291     "R" gnus-summary-reply-with-original
1292     "\C-c\C-f" gnus-summary-mail-forward
1293     "o" gnus-summary-save-article
1294     "\C-o" gnus-summary-save-article-mail
1295     "|" gnus-summary-pipe-output
1296     "\M-k" gnus-summary-edit-local-kill
1297     "\M-K" gnus-summary-edit-global-kill
1298     ;; "V" gnus-version
1299     "\C-c\C-d" gnus-summary-describe-group
1300     "q" gnus-summary-exit
1301     "Q" gnus-summary-exit-no-update
1302     "\C-c\C-i" gnus-info-find-node
1303     gnus-mouse-2 gnus-mouse-pick-article
1304     "m" gnus-summary-mail-other-window
1305     "a" gnus-summary-post-news
1306     "x" gnus-summary-limit-to-unread
1307     "s" gnus-summary-isearch-article
1308     "t" gnus-article-hide-headers
1309     "g" gnus-summary-show-article
1310     "l" gnus-summary-goto-last-article
1311     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1312     "\C-d" gnus-summary-enter-digest-group
1313     "\M-\C-d" gnus-summary-read-document
1314     "\M-\C-e" gnus-summary-edit-parameters
1315     "\M-\C-g" gnus-summary-customize-parameters
1316     "\C-c\C-b" gnus-bug
1317     "*" gnus-cache-enter-article
1318     "\M-*" gnus-cache-remove-article
1319     "\M-&" gnus-summary-universal-argument
1320     "\C-l" gnus-recenter
1321     "I" gnus-summary-increase-score
1322     "L" gnus-summary-lower-score
1323     "\M-i" gnus-symbolic-argument
1324     "h" gnus-summary-select-article-buffer
1325
1326     "b" gnus-article-view-part
1327     "\M-t" gnus-summary-toggle-display-buttonized
1328     
1329     "V" gnus-summary-score-map
1330     "X" gnus-uu-extract-map
1331     "S" gnus-summary-send-map)
1332
1333   ;; Sort of orthogonal keymap
1334   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1335     "t" gnus-summary-tick-article-forward
1336     "!" gnus-summary-tick-article-forward
1337     "d" gnus-summary-mark-as-read-forward
1338     "r" gnus-summary-mark-as-read-forward
1339     "c" gnus-summary-clear-mark-forward
1340     " " gnus-summary-clear-mark-forward
1341     "e" gnus-summary-mark-as-expirable
1342     "x" gnus-summary-mark-as-expirable
1343     "?" gnus-summary-mark-as-dormant
1344     "b" gnus-summary-set-bookmark
1345     "B" gnus-summary-remove-bookmark
1346     "#" gnus-summary-mark-as-processable
1347     "\M-#" gnus-summary-unmark-as-processable
1348     "S" gnus-summary-limit-include-expunged
1349     "C" gnus-summary-catchup
1350     "H" gnus-summary-catchup-to-here
1351     "\C-c" gnus-summary-catchup-all
1352     "k" gnus-summary-kill-same-subject-and-select
1353     "K" gnus-summary-kill-same-subject
1354     "P" gnus-uu-mark-map)
1355
1356   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1357     "c" gnus-summary-clear-above
1358     "u" gnus-summary-tick-above
1359     "m" gnus-summary-mark-above
1360     "k" gnus-summary-kill-below)
1361
1362   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1363     "/" gnus-summary-limit-to-subject
1364     "n" gnus-summary-limit-to-articles
1365     "w" gnus-summary-pop-limit
1366     "s" gnus-summary-limit-to-subject
1367     "a" gnus-summary-limit-to-author
1368     "u" gnus-summary-limit-to-unread
1369     "m" gnus-summary-limit-to-marks
1370     "v" gnus-summary-limit-to-score
1371     "*" gnus-summary-limit-include-cached
1372     "D" gnus-summary-limit-include-dormant
1373     "T" gnus-summary-limit-include-thread
1374     "d" gnus-summary-limit-exclude-dormant
1375     "t" gnus-summary-limit-to-age
1376     "E" gnus-summary-limit-include-expunged
1377     "c" gnus-summary-limit-exclude-childless-dormant
1378     "C" gnus-summary-limit-mark-excluded-as-read)
1379
1380   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1381     "n" gnus-summary-next-unread-article
1382     "p" gnus-summary-prev-unread-article
1383     "N" gnus-summary-next-article
1384     "P" gnus-summary-prev-article
1385     "\C-n" gnus-summary-next-same-subject
1386     "\C-p" gnus-summary-prev-same-subject
1387     "\M-n" gnus-summary-next-unread-subject
1388     "\M-p" gnus-summary-prev-unread-subject
1389     "f" gnus-summary-first-unread-article
1390     "b" gnus-summary-best-unread-article
1391     "j" gnus-summary-goto-article
1392     "g" gnus-summary-goto-subject
1393     "l" gnus-summary-goto-last-article
1394     "o" gnus-summary-pop-article)
1395
1396   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1397     "k" gnus-summary-kill-thread
1398     "l" gnus-summary-lower-thread
1399     "i" gnus-summary-raise-thread
1400     "T" gnus-summary-toggle-threads
1401     "t" gnus-summary-rethread-current
1402     "^" gnus-summary-reparent-thread
1403     "s" gnus-summary-show-thread
1404     "S" gnus-summary-show-all-threads
1405     "h" gnus-summary-hide-thread
1406     "H" gnus-summary-hide-all-threads
1407     "n" gnus-summary-next-thread
1408     "p" gnus-summary-prev-thread
1409     "u" gnus-summary-up-thread
1410     "o" gnus-summary-top-thread
1411     "d" gnus-summary-down-thread
1412     "#" gnus-uu-mark-thread
1413     "\M-#" gnus-uu-unmark-thread)
1414
1415   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1416     "g" gnus-summary-prepare
1417     "c" gnus-summary-insert-cached-articles)
1418
1419   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1420     "c" gnus-summary-catchup-and-exit
1421     "C" gnus-summary-catchup-all-and-exit
1422     "E" gnus-summary-exit-no-update
1423     "Q" gnus-summary-exit
1424     "Z" gnus-summary-exit
1425     "n" gnus-summary-catchup-and-goto-next-group
1426     "R" gnus-summary-reselect-current-group
1427     "G" gnus-summary-rescan-group
1428     "N" gnus-summary-next-group
1429     "s" gnus-summary-save-newsrc
1430     "P" gnus-summary-prev-group)
1431
1432   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1433     " " gnus-summary-next-page
1434     "n" gnus-summary-next-page
1435     "\177" gnus-summary-prev-page
1436     [delete] gnus-summary-prev-page
1437     "p" gnus-summary-prev-page
1438     "\r" gnus-summary-scroll-up
1439     "\M-\r" gnus-summary-scroll-down
1440     "<" gnus-summary-beginning-of-article
1441     ">" gnus-summary-end-of-article
1442     "b" gnus-summary-beginning-of-article
1443     "e" gnus-summary-end-of-article
1444     "^" gnus-summary-refer-parent-article
1445     "r" gnus-summary-refer-parent-article
1446     "R" gnus-summary-refer-references
1447     "T" gnus-summary-refer-thread
1448     "g" gnus-summary-show-article
1449     "s" gnus-summary-isearch-article
1450     "P" gnus-summary-print-article)
1451
1452   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1453     "b" gnus-article-add-buttons
1454     "B" gnus-article-add-buttons-to-head
1455     "o" gnus-article-treat-overstrike
1456     "e" gnus-article-emphasize
1457     "w" gnus-article-fill-cited-article
1458     "c" gnus-article-remove-cr
1459     "q" gnus-article-de-quoted-unreadable
1460     "f" gnus-article-display-x-face
1461     "l" gnus-summary-stop-page-breaking
1462     "r" gnus-summary-caesar-message
1463     "t" gnus-article-hide-headers
1464     "v" gnus-summary-verbose-headers
1465     "h" gnus-article-treat-html
1466     "d" gnus-article-treat-dumbquotes)
1467
1468   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1469     "a" gnus-article-hide
1470     "h" gnus-article-hide-headers
1471     "b" gnus-article-hide-boring-headers
1472     "s" gnus-article-hide-signature
1473     "c" gnus-article-hide-citation
1474     "C" gnus-article-hide-citation-in-followups
1475     "p" gnus-article-hide-pgp
1476     "P" gnus-article-hide-pem
1477     "\C-c" gnus-article-hide-citation-maybe)
1478
1479   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1480     "a" gnus-article-highlight
1481     "h" gnus-article-highlight-headers
1482     "c" gnus-article-highlight-citation
1483     "s" gnus-article-highlight-signature)
1484
1485   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1486     "w" gnus-article-decode-mime-words
1487     "c" gnus-article-decode-charset
1488     "v" gnus-mime-view-all-parts
1489     "b" gnus-article-view-part)
1490
1491   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1492     "z" gnus-article-date-ut
1493     "u" gnus-article-date-ut
1494     "l" gnus-article-date-local
1495     "e" gnus-article-date-lapsed
1496     "o" gnus-article-date-original
1497     "i" gnus-article-date-iso8601
1498     "s" gnus-article-date-user)
1499
1500   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1501     "t" gnus-article-remove-trailing-blank-lines
1502     "l" gnus-article-strip-leading-blank-lines
1503     "m" gnus-article-strip-multiple-blank-lines
1504     "a" gnus-article-strip-blank-lines
1505     "A" gnus-article-strip-all-blank-lines
1506     "s" gnus-article-strip-leading-space
1507     "e" gnus-article-strip-trailing-space)
1508
1509   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1510     "v" gnus-version
1511     "f" gnus-summary-fetch-faq
1512     "d" gnus-summary-describe-group
1513     "h" gnus-summary-describe-briefly
1514     "i" gnus-info-find-node)
1515
1516   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1517     "e" gnus-summary-expire-articles
1518     "\M-\C-e" gnus-summary-expire-articles-now
1519     "\177" gnus-summary-delete-article
1520     [delete] gnus-summary-delete-article
1521     "m" gnus-summary-move-article
1522     "r" gnus-summary-respool-article
1523     "w" gnus-summary-edit-article
1524     "c" gnus-summary-copy-article
1525     "B" gnus-summary-crosspost-article
1526     "q" gnus-summary-respool-query
1527     "t" gnus-summary-respool-trace
1528     "i" gnus-summary-import-article
1529     "p" gnus-summary-article-posted-p)
1530
1531   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1532     "o" gnus-summary-save-article
1533     "m" gnus-summary-save-article-mail
1534     "F" gnus-summary-write-article-file
1535     "r" gnus-summary-save-article-rmail
1536     "f" gnus-summary-save-article-file
1537     "b" gnus-summary-save-article-body-file
1538     "h" gnus-summary-save-article-folder
1539     "v" gnus-summary-save-article-vm
1540     "p" gnus-summary-pipe-output
1541     "s" gnus-soup-add-article)
1542
1543   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1544     "b" gnus-summary-display-buttonized
1545     "m" gnus-summary-repair-multipart
1546     "v" gnus-article-view-part
1547     "o" gnus-article-save-part
1548     "c" gnus-article-copy-part
1549     "e" gnus-article-externalize-part
1550     "|" gnus-article-pipe-part)
1551   )
1552
1553 (defun gnus-summary-make-menu-bar ()
1554   (gnus-turn-off-edit-menu 'summary)
1555
1556   (unless (boundp 'gnus-summary-misc-menu)
1557
1558     (easy-menu-define
1559      gnus-summary-kill-menu gnus-summary-mode-map ""
1560      (cons
1561       "Score"
1562       (nconc
1563        (list
1564         ["Enter score..." gnus-summary-score-entry t]
1565         ["Customize" gnus-score-customize t])
1566        (gnus-make-score-map 'increase)
1567        (gnus-make-score-map 'lower)
1568        '(("Mark"
1569           ["Kill below" gnus-summary-kill-below t]
1570           ["Mark above" gnus-summary-mark-above t]
1571           ["Tick above" gnus-summary-tick-above t]
1572           ["Clear above" gnus-summary-clear-above t])
1573          ["Current score" gnus-summary-current-score t]
1574          ["Set score" gnus-summary-set-score t]
1575          ["Switch current score file..." gnus-score-change-score-file t]
1576          ["Set mark below..." gnus-score-set-mark-below t]
1577          ["Set expunge below..." gnus-score-set-expunge-below t]
1578          ["Edit current score file" gnus-score-edit-current-scores t]
1579          ["Edit score file" gnus-score-edit-file t]
1580          ["Trace score" gnus-score-find-trace t]
1581          ["Find words" gnus-score-find-favourite-words t]
1582          ["Rescore buffer" gnus-summary-rescore t]
1583          ["Increase score..." gnus-summary-increase-score t]
1584          ["Lower score..." gnus-summary-lower-score t]))))
1585
1586     ;; Define both the Article menu in the summary buffer and the equivalent
1587     ;; Commands menu in the article buffer here for consistency.
1588     (let ((innards
1589            '(("Hide"
1590               ["All" gnus-article-hide t]
1591               ["Headers" gnus-article-hide-headers t]
1592               ["Signature" gnus-article-hide-signature t]
1593               ["Citation" gnus-article-hide-citation t]
1594               ["PGP" gnus-article-hide-pgp t]
1595               ["Boring headers" gnus-article-hide-boring-headers t])
1596              ("Highlight"
1597               ["All" gnus-article-highlight t]
1598               ["Headers" gnus-article-highlight-headers t]
1599               ["Signature" gnus-article-highlight-signature t]
1600               ["Citation" gnus-article-highlight-citation t])
1601              ("MIME"
1602               ["Words" gnus-article-decode-mime-words t]
1603               ["Charset" gnus-article-decode-charset t]
1604               ["QP" gnus-article-de-quoted-unreadable t]
1605               ["View all" gnus-mime-view-all-parts t])
1606              ("Date"
1607               ["Local" gnus-article-date-local t]
1608               ["ISO8601" gnus-article-date-iso8601 t]
1609               ["UT" gnus-article-date-ut t]
1610               ["Original" gnus-article-date-original t]
1611               ["Lapsed" gnus-article-date-lapsed t]
1612               ["User-defined" gnus-article-date-user t])
1613              ("Washing"
1614               ("Remove Blanks"
1615                ["Leading" gnus-article-strip-leading-blank-lines t]
1616                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1617                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1618                ["All of the above" gnus-article-strip-blank-lines t]
1619                ["All" gnus-article-strip-all-blank-lines t]
1620                ["Leading space" gnus-article-strip-leading-space t]
1621                ["Trailing space" gnus-article-strip-trailing-space t])
1622               ["Overstrike" gnus-article-treat-overstrike t]
1623               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1624               ["Emphasis" gnus-article-emphasize t]
1625               ["Word wrap" gnus-article-fill-cited-article t]
1626               ["CR" gnus-article-remove-cr t]
1627               ["Show X-Face" gnus-article-display-x-face t]
1628               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1629               ["UnHTMLize" gnus-article-treat-html t]
1630               ["Rot 13" gnus-summary-caesar-message t]
1631               ["Unix pipe" gnus-summary-pipe-message t]
1632               ["Add buttons" gnus-article-add-buttons t]
1633               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1634               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1635               ["Verbose header" gnus-summary-verbose-headers t]
1636               ["Toggle header" gnus-summary-toggle-header t])
1637              ("Output"
1638               ["Save in default format" gnus-summary-save-article t]
1639               ["Save in file" gnus-summary-save-article-file t]
1640               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1641               ["Save in MH folder" gnus-summary-save-article-folder t]
1642               ["Save in VM folder" gnus-summary-save-article-vm t]
1643               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1644               ["Save body in file" gnus-summary-save-article-body-file t]
1645               ["Pipe through a filter" gnus-summary-pipe-output t]
1646               ["Add to SOUP packet" gnus-soup-add-article t]
1647               ["Print" gnus-summary-print-article t])
1648              ("Backend"
1649               ["Respool article..." gnus-summary-respool-article t]
1650               ["Move article..." gnus-summary-move-article
1651                (gnus-check-backend-function
1652                 'request-move-article gnus-newsgroup-name)]
1653               ["Copy article..." gnus-summary-copy-article t]
1654               ["Crosspost article..." gnus-summary-crosspost-article
1655                (gnus-check-backend-function
1656                 'request-replace-article gnus-newsgroup-name)]
1657               ["Import file..." gnus-summary-import-article t]
1658               ["Check if posted" gnus-summary-article-posted-p t]
1659               ["Edit article" gnus-summary-edit-article
1660                (not (gnus-group-read-only-p))]
1661               ["Delete article" gnus-summary-delete-article
1662                (gnus-check-backend-function
1663                 'request-expire-articles gnus-newsgroup-name)]
1664               ["Query respool" gnus-summary-respool-query t]
1665               ["Trace respool" gnus-summary-respool-trace t]
1666               ["Delete expirable articles" gnus-summary-expire-articles-now
1667                (gnus-check-backend-function
1668                 'request-expire-articles gnus-newsgroup-name)])
1669              ("Extract"
1670               ["Uudecode" gnus-uu-decode-uu t]
1671               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1672               ["Unshar" gnus-uu-decode-unshar t]
1673               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1674               ["Save" gnus-uu-decode-save t]
1675               ["Binhex" gnus-uu-decode-binhex t]
1676               ["Postscript" gnus-uu-decode-postscript t])
1677              ("Cache"
1678               ["Enter article" gnus-cache-enter-article t]
1679               ["Remove article" gnus-cache-remove-article t])
1680              ["Select article buffer" gnus-summary-select-article-buffer t]
1681              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1682              ["Isearch article..." gnus-summary-isearch-article t]
1683              ["Beginning of the article" gnus-summary-beginning-of-article t]
1684              ["End of the article" gnus-summary-end-of-article t]
1685              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1686              ["Fetch referenced articles" gnus-summary-refer-references t]
1687              ["Fetch current thread" gnus-summary-refer-thread t]
1688              ["Fetch article with id..." gnus-summary-refer-article t]
1689              ["Redisplay" gnus-summary-show-article t])))
1690       (easy-menu-define
1691        gnus-summary-article-menu gnus-summary-mode-map ""
1692        (cons "Article" innards))
1693
1694       (easy-menu-define
1695        gnus-article-commands-menu gnus-article-mode-map ""
1696        (cons "Commands" innards)))
1697
1698     (easy-menu-define
1699      gnus-summary-thread-menu gnus-summary-mode-map ""
1700      '("Threads"
1701        ["Toggle threading" gnus-summary-toggle-threads t]
1702        ["Hide threads" gnus-summary-hide-all-threads t]
1703        ["Show threads" gnus-summary-show-all-threads t]
1704        ["Hide thread" gnus-summary-hide-thread t]
1705        ["Show thread" gnus-summary-show-thread t]
1706        ["Go to next thread" gnus-summary-next-thread t]
1707        ["Go to previous thread" gnus-summary-prev-thread t]
1708        ["Go down thread" gnus-summary-down-thread t]
1709        ["Go up thread" gnus-summary-up-thread t]
1710        ["Top of thread" gnus-summary-top-thread t]
1711        ["Mark thread as read" gnus-summary-kill-thread t]
1712        ["Lower thread score" gnus-summary-lower-thread t]
1713        ["Raise thread score" gnus-summary-raise-thread t]
1714        ["Rethread current" gnus-summary-rethread-current t]
1715        ))
1716
1717     (easy-menu-define
1718      gnus-summary-post-menu gnus-summary-mode-map ""
1719      '("Post"
1720        ["Post an article" gnus-summary-post-news t]
1721        ["Followup" gnus-summary-followup t]
1722        ["Followup and yank" gnus-summary-followup-with-original t]
1723        ["Supersede article" gnus-summary-supersede-article t]
1724        ["Cancel article" gnus-summary-cancel-article t]
1725        ["Reply" gnus-summary-reply t]
1726        ["Reply and yank" gnus-summary-reply-with-original t]
1727        ["Wide reply" gnus-summary-wide-reply t]
1728        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1729        ["Mail forward" gnus-summary-mail-forward t]
1730        ["Post forward" gnus-summary-post-forward t]
1731        ["Digest and mail" gnus-uu-digest-mail-forward t]
1732        ["Digest and post" gnus-uu-digest-post-forward t]
1733        ["Resend message" gnus-summary-resend-message t]
1734        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1735        ["Send a mail" gnus-summary-mail-other-window t]
1736        ["Uuencode and post" gnus-uu-post-news t]
1737        ["Followup via news" gnus-summary-followup-to-mail t]
1738        ["Followup via news and yank"
1739         gnus-summary-followup-to-mail-with-original t]
1740        ;;("Draft"
1741        ;;["Send" gnus-summary-send-draft t]
1742        ;;["Send bounced" gnus-resend-bounced-mail t])
1743        ))
1744
1745     (easy-menu-define
1746      gnus-summary-misc-menu gnus-summary-mode-map ""
1747      '("Misc"
1748        ("Mark Read"
1749         ["Mark as read" gnus-summary-mark-as-read-forward t]
1750         ["Mark same subject and select"
1751          gnus-summary-kill-same-subject-and-select t]
1752         ["Mark same subject" gnus-summary-kill-same-subject t]
1753         ["Catchup" gnus-summary-catchup t]
1754         ["Catchup all" gnus-summary-catchup-all t]
1755         ["Catchup to here" gnus-summary-catchup-to-here t]
1756         ["Catchup region" gnus-summary-mark-region-as-read t]
1757         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1758        ("Mark Various"
1759         ["Tick" gnus-summary-tick-article-forward t]
1760         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1761         ["Remove marks" gnus-summary-clear-mark-forward t]
1762         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1763         ["Set bookmark" gnus-summary-set-bookmark t]
1764         ["Remove bookmark" gnus-summary-remove-bookmark t])
1765        ("Mark Limit"
1766         ["Marks..." gnus-summary-limit-to-marks t]
1767         ["Subject..." gnus-summary-limit-to-subject t]
1768         ["Author..." gnus-summary-limit-to-author t]
1769         ["Age..." gnus-summary-limit-to-age t]
1770         ["Score" gnus-summary-limit-to-score t]
1771         ["Unread" gnus-summary-limit-to-unread t]
1772         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1773         ["Articles" gnus-summary-limit-to-articles t]
1774         ["Pop limit" gnus-summary-pop-limit t]
1775         ["Show dormant" gnus-summary-limit-include-dormant t]
1776         ["Hide childless dormant"
1777          gnus-summary-limit-exclude-childless-dormant t]
1778         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1779         ["Show expunged" gnus-summary-show-all-expunged t])
1780        ("Process Mark"
1781         ["Set mark" gnus-summary-mark-as-processable t]
1782         ["Remove mark" gnus-summary-unmark-as-processable t]
1783         ["Remove all marks" gnus-summary-unmark-all-processable t]
1784         ["Mark above" gnus-uu-mark-over t]
1785         ["Mark series" gnus-uu-mark-series t]
1786         ["Mark region" gnus-uu-mark-region t]
1787         ["Unmark region" gnus-uu-unmark-region t]
1788         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1789         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1790         ["Mark all" gnus-uu-mark-all t]
1791         ["Mark buffer" gnus-uu-mark-buffer t]
1792         ["Mark sparse" gnus-uu-mark-sparse t]
1793         ["Mark thread" gnus-uu-mark-thread t]
1794         ["Unmark thread" gnus-uu-unmark-thread t]
1795         ("Process Mark Sets"
1796          ["Kill" gnus-summary-kill-process-mark t]
1797          ["Yank" gnus-summary-yank-process-mark
1798           gnus-newsgroup-process-stack]
1799          ["Save" gnus-summary-save-process-mark t]))
1800        ("Scroll article"
1801         ["Page forward" gnus-summary-next-page t]
1802         ["Page backward" gnus-summary-prev-page t]
1803         ["Line forward" gnus-summary-scroll-up t])
1804        ("Move"
1805         ["Next unread article" gnus-summary-next-unread-article t]
1806         ["Previous unread article" gnus-summary-prev-unread-article t]
1807         ["Next article" gnus-summary-next-article t]
1808         ["Previous article" gnus-summary-prev-article t]
1809         ["Next unread subject" gnus-summary-next-unread-subject t]
1810         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1811         ["Next article same subject" gnus-summary-next-same-subject t]
1812         ["Previous article same subject" gnus-summary-prev-same-subject t]
1813         ["First unread article" gnus-summary-first-unread-article t]
1814         ["Best unread article" gnus-summary-best-unread-article t]
1815         ["Go to subject number..." gnus-summary-goto-subject t]
1816         ["Go to article number..." gnus-summary-goto-article t]
1817         ["Go to the last article" gnus-summary-goto-last-article t]
1818         ["Pop article off history" gnus-summary-pop-article t])
1819        ("Sort"
1820         ["Sort by number" gnus-summary-sort-by-number t]
1821         ["Sort by author" gnus-summary-sort-by-author t]
1822         ["Sort by subject" gnus-summary-sort-by-subject t]
1823         ["Sort by date" gnus-summary-sort-by-date t]
1824         ["Sort by score" gnus-summary-sort-by-score t]
1825         ["Sort by lines" gnus-summary-sort-by-lines t])
1826        ("Help"
1827         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1828         ["Describe group" gnus-summary-describe-group t]
1829         ["Read manual" gnus-info-find-node t])
1830        ("Modes"
1831         ["Pick and read" gnus-pick-mode t]
1832         ["Binary" gnus-binary-mode t])
1833        ("Regeneration"
1834         ["Regenerate" gnus-summary-prepare t]
1835         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1836         ["Toggle threading" gnus-summary-toggle-threads t])
1837        ["Filter articles..." gnus-summary-execute-command t]
1838        ["Run command on subjects..." gnus-summary-universal-argument t]
1839        ["Search articles forward..." gnus-summary-search-article-forward t]
1840        ["Search articles backward..." gnus-summary-search-article-backward t]
1841        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1842        ["Expand window" gnus-summary-expand-window t]
1843        ["Expire expirable articles" gnus-summary-expire-articles
1844         (gnus-check-backend-function
1845          'request-expire-articles gnus-newsgroup-name)]
1846        ["Edit local kill file" gnus-summary-edit-local-kill t]
1847        ["Edit main kill file" gnus-summary-edit-global-kill t]
1848        ["Edit group parameters" gnus-summary-edit-parameters t]
1849        ["Customize group parameters" gnus-summary-customize-parameters t]
1850        ["Send a bug report" gnus-bug t]
1851        ("Exit"
1852         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1853         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1854         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1855         ["Exit group" gnus-summary-exit t]
1856         ["Exit group without updating" gnus-summary-exit-no-update t]
1857         ["Exit and goto next group" gnus-summary-next-group t]
1858         ["Exit and goto prev group" gnus-summary-prev-group t]
1859         ["Reselect group" gnus-summary-reselect-current-group t]
1860         ["Rescan group" gnus-summary-rescan-group t]
1861         ["Update dribble" gnus-summary-save-newsrc t])))
1862
1863     (gnus-run-hooks 'gnus-summary-menu-hook)))
1864
1865 (defun gnus-score-set-default (var value)
1866   "A version of set that updates the GNU Emacs menu-bar."
1867   (set var value)
1868   ;; It is the message that forces the active status to be updated.
1869   (message ""))
1870
1871 (defun gnus-make-score-map (type)
1872   "Make a summary score map of type TYPE."
1873   (if t
1874       nil
1875     (let ((headers '(("author" "from" string)
1876                      ("subject" "subject" string)
1877                      ("article body" "body" string)
1878                      ("article head" "head" string)
1879                      ("xref" "xref" string)
1880                      ("extra header" "extra" string)
1881                      ("lines" "lines" number)
1882                      ("followups to author" "followup" string)))
1883           (types '((number ("less than" <)
1884                            ("greater than" >)
1885                            ("equal" =))
1886                    (string ("substring" s)
1887                            ("exact string" e)
1888                            ("fuzzy string" f)
1889                            ("regexp" r))))
1890           (perms '(("temporary" (current-time-string))
1891                    ("permanent" nil)
1892                    ("immediate" now)))
1893           header)
1894       (list
1895        (apply
1896         'nconc
1897         (list
1898          (if (eq type 'lower)
1899              "Lower score"
1900            "Increase score"))
1901         (let (outh)
1902           (while headers
1903             (setq header (car headers))
1904             (setq outh
1905                   (cons
1906                    (apply
1907                     'nconc
1908                     (list (car header))
1909                     (let ((ts (cdr (assoc (nth 2 header) types)))
1910                           outt)
1911                       (while ts
1912                         (setq outt
1913                               (cons
1914                                (apply
1915                                 'nconc
1916                                 (list (caar ts))
1917                                 (let ((ps perms)
1918                                       outp)
1919                                   (while ps
1920                                     (setq outp
1921                                           (cons
1922                                            (vector
1923                                             (caar ps)
1924                                             (list
1925                                              'gnus-summary-score-entry
1926                                              (nth 1 header)
1927                                              (if (or (string= (nth 1 header)
1928                                                               "head")
1929                                                      (string= (nth 1 header)
1930                                                               "body"))
1931                                                  ""
1932                                                (list 'gnus-summary-header
1933                                                      (nth 1 header)))
1934                                              (list 'quote (nth 1 (car ts)))
1935                                              (list 'gnus-score-default nil)
1936                                              (nth 1 (car ps))
1937                                              t)
1938                                             t)
1939                                            outp))
1940                                     (setq ps (cdr ps)))
1941                                   (list (nreverse outp))))
1942                                outt))
1943                         (setq ts (cdr ts)))
1944                       (list (nreverse outt))))
1945                    outh))
1946             (setq headers (cdr headers)))
1947           (list (nreverse outh))))))))
1948
1949 \f
1950
1951 (defun gnus-summary-mode (&optional group)
1952   "Major mode for reading articles.
1953
1954 All normal editing commands are switched off.
1955 \\<gnus-summary-mode-map>
1956 Each line in this buffer represents one article.  To read an
1957 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1958 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1959 respectively.
1960
1961 You can also post articles and send mail from this buffer.  To
1962 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1963 of an article, type `\\[gnus-summary-reply]'.
1964
1965 There are approx. one gazillion commands you can execute in this
1966 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1967
1968 The following commands are available:
1969
1970 \\{gnus-summary-mode-map}"
1971   (interactive)
1972   (when (gnus-visual-p 'summary-menu 'menu)
1973     (gnus-summary-make-menu-bar))
1974   (kill-all-local-variables)
1975   (gnus-summary-make-local-variables)
1976   (gnus-make-thread-indent-array)
1977   (gnus-simplify-mode-line)
1978   (setq major-mode 'gnus-summary-mode)
1979   (setq mode-name "Summary")
1980   (make-local-variable 'minor-mode-alist)
1981   (use-local-map gnus-summary-mode-map)
1982   (buffer-disable-undo)
1983   (setq buffer-read-only t)             ;Disable modification
1984   (setq truncate-lines t)
1985   (setq selective-display t)
1986   (setq selective-display-ellipses t)   ;Display `...'
1987   (gnus-summary-set-display-table)
1988   (gnus-set-default-directory)
1989   (setq gnus-newsgroup-name group)
1990   (make-local-variable 'gnus-summary-line-format)
1991   (make-local-variable 'gnus-summary-line-format-spec)
1992   (make-local-variable 'gnus-summary-dummy-line-format)
1993   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1994   (make-local-variable 'gnus-summary-mark-positions)
1995   (make-local-hook 'post-command-hook)
1996   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1997   (make-local-hook 'pre-command-hook)
1998   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1999   (gnus-run-hooks 'gnus-summary-mode-hook)
2000   (mm-enable-multibyte)
2001   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2002   (gnus-update-summary-mark-positions))
2003
2004 (defun gnus-summary-make-local-variables ()
2005   "Make all the local summary buffer variables."
2006   (let ((locals gnus-summary-local-variables)
2007         global local)
2008     (while (setq local (pop locals))
2009       (if (consp local)
2010           (progn
2011             (if (eq (cdr local) 'global)
2012                 ;; Copy the global value of the variable.
2013                 (setq global (symbol-value (car local)))
2014               ;; Use the value from the list.
2015               (setq global (eval (cdr local))))
2016             (make-local-variable (car local))
2017             (set (car local) global))
2018         ;; Simple nil-valued local variable.
2019         (make-local-variable local)
2020         (set local nil)))))
2021
2022 (defun gnus-summary-clear-local-variables ()
2023   (let ((locals gnus-summary-local-variables))
2024     (while locals
2025       (if (consp (car locals))
2026           (and (vectorp (caar locals))
2027                (set (caar locals) nil))
2028         (and (vectorp (car locals))
2029              (set (car locals) nil)))
2030       (setq locals (cdr locals)))))
2031
2032 ;; Summary data functions.
2033
2034 (defmacro gnus-data-number (data)
2035   `(car ,data))
2036
2037 (defmacro gnus-data-set-number (data number)
2038   `(setcar ,data ,number))
2039
2040 (defmacro gnus-data-mark (data)
2041   `(nth 1 ,data))
2042
2043 (defmacro gnus-data-set-mark (data mark)
2044   `(setcar (nthcdr 1 ,data) ,mark))
2045
2046 (defmacro gnus-data-pos (data)
2047   `(nth 2 ,data))
2048
2049 (defmacro gnus-data-set-pos (data pos)
2050   `(setcar (nthcdr 2 ,data) ,pos))
2051
2052 (defmacro gnus-data-header (data)
2053   `(nth 3 ,data))
2054
2055 (defmacro gnus-data-set-header (data header)
2056   `(setf (nth 3 ,data) ,header))
2057
2058 (defmacro gnus-data-level (data)
2059   `(nth 4 ,data))
2060
2061 (defmacro gnus-data-unread-p (data)
2062   `(= (nth 1 ,data) gnus-unread-mark))
2063
2064 (defmacro gnus-data-read-p (data)
2065   `(/= (nth 1 ,data) gnus-unread-mark))
2066
2067 (defmacro gnus-data-pseudo-p (data)
2068   `(consp (nth 3 ,data)))
2069
2070 (defmacro gnus-data-find (number)
2071   `(assq ,number gnus-newsgroup-data))
2072
2073 (defmacro gnus-data-find-list (number &optional data)
2074   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2075      (memq (assq ,number bdata)
2076            bdata)))
2077
2078 (defmacro gnus-data-make (number mark pos header level)
2079   `(list ,number ,mark ,pos ,header ,level))
2080
2081 (defun gnus-data-enter (after-article number mark pos header level offset)
2082   (let ((data (gnus-data-find-list after-article)))
2083     (unless data
2084       (error "No such article: %d" after-article))
2085     (setcdr data (cons (gnus-data-make number mark pos header level)
2086                        (cdr data)))
2087     (setq gnus-newsgroup-data-reverse nil)
2088     (gnus-data-update-list (cddr data) offset)))
2089
2090 (defun gnus-data-enter-list (after-article list &optional offset)
2091   (when list
2092     (let ((data (and after-article (gnus-data-find-list after-article)))
2093           (ilist list))
2094       (if (not (or data
2095                    after-article))
2096           (let ((odata gnus-newsgroup-data))
2097             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2098             (when offset
2099               (gnus-data-update-list odata offset)))
2100         ;; Find the last element in the list to be spliced into the main
2101         ;; list.
2102         (while (cdr list)
2103           (setq list (cdr list)))
2104         (if (not data)
2105             (progn
2106               (setcdr list gnus-newsgroup-data)
2107               (setq gnus-newsgroup-data ilist)
2108               (when offset
2109                 (gnus-data-update-list (cdr list) offset)))
2110           (setcdr list (cdr data))
2111           (setcdr data ilist)
2112           (when offset
2113             (gnus-data-update-list (cdr list) offset))))
2114       (setq gnus-newsgroup-data-reverse nil))))
2115
2116 (defun gnus-data-remove (article &optional offset)
2117   (let ((data gnus-newsgroup-data))
2118     (if (= (gnus-data-number (car data)) article)
2119         (progn
2120           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2121                 gnus-newsgroup-data-reverse nil)
2122           (when offset
2123             (gnus-data-update-list gnus-newsgroup-data offset)))
2124       (while (cdr data)
2125         (when (= (gnus-data-number (cadr data)) article)
2126           (setcdr data (cddr data))
2127           (when offset
2128             (gnus-data-update-list (cdr data) offset))
2129           (setq data nil
2130                 gnus-newsgroup-data-reverse nil))
2131         (setq data (cdr data))))))
2132
2133 (defmacro gnus-data-list (backward)
2134   `(if ,backward
2135        (or gnus-newsgroup-data-reverse
2136            (setq gnus-newsgroup-data-reverse
2137                  (reverse gnus-newsgroup-data)))
2138      gnus-newsgroup-data))
2139
2140 (defun gnus-data-update-list (data offset)
2141   "Add OFFSET to the POS of all data entries in DATA."
2142   (setq gnus-newsgroup-data-reverse nil)
2143   (while data
2144     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2145     (setq data (cdr data))))
2146
2147 (defun gnus-summary-article-pseudo-p (article)
2148   "Say whether this article is a pseudo article or not."
2149   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2150
2151 (defmacro gnus-summary-article-sparse-p (article)
2152   "Say whether this article is a sparse article or not."
2153   `(memq ,article gnus-newsgroup-sparse))
2154
2155 (defmacro gnus-summary-article-ancient-p (article)
2156   "Say whether this article is a sparse article or not."
2157   `(memq ,article gnus-newsgroup-ancient))
2158
2159 (defun gnus-article-parent-p (number)
2160   "Say whether this article is a parent or not."
2161   (let ((data (gnus-data-find-list number)))
2162     (and (cdr data)                     ; There has to be an article after...
2163          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2164             (gnus-data-level (nth 1 data))))))
2165
2166 (defun gnus-article-children (number)
2167   "Return a list of all children to NUMBER."
2168   (let* ((data (gnus-data-find-list number))
2169          (level (gnus-data-level (car data)))
2170          children)
2171     (setq data (cdr data))
2172     (while (and data
2173                 (= (gnus-data-level (car data)) (1+ level)))
2174       (push (gnus-data-number (car data)) children)
2175       (setq data (cdr data)))
2176     children))
2177
2178 (defmacro gnus-summary-skip-intangible ()
2179   "If the current article is intangible, then jump to a different article."
2180   '(let ((to (get-text-property (point) 'gnus-intangible)))
2181      (and to (gnus-summary-goto-subject to))))
2182
2183 (defmacro gnus-summary-article-intangible-p ()
2184   "Say whether this article is intangible or not."
2185   '(get-text-property (point) 'gnus-intangible))
2186
2187 (defun gnus-article-read-p (article)
2188   "Say whether ARTICLE is read or not."
2189   (not (or (memq article gnus-newsgroup-marked)
2190            (memq article gnus-newsgroup-unreads)
2191            (memq article gnus-newsgroup-unselected)
2192            (memq article gnus-newsgroup-dormant))))
2193
2194 ;; Some summary mode macros.
2195
2196 (defmacro gnus-summary-article-number ()
2197   "The article number of the article on the current line.
2198 If there isn's an article number here, then we return the current
2199 article number."
2200   '(progn
2201      (gnus-summary-skip-intangible)
2202      (or (get-text-property (point) 'gnus-number)
2203          (gnus-summary-last-subject))))
2204
2205 (defmacro gnus-summary-article-header (&optional number)
2206   "Return the header of article NUMBER."
2207   `(gnus-data-header (gnus-data-find
2208                       ,(or number '(gnus-summary-article-number)))))
2209
2210 (defmacro gnus-summary-thread-level (&optional number)
2211   "Return the level of thread that starts with article NUMBER."
2212   `(if (and (eq gnus-summary-make-false-root 'dummy)
2213             (get-text-property (point) 'gnus-intangible))
2214        0
2215      (gnus-data-level (gnus-data-find
2216                        ,(or number '(gnus-summary-article-number))))))
2217
2218 (defmacro gnus-summary-article-mark (&optional number)
2219   "Return the mark of article NUMBER."
2220   `(gnus-data-mark (gnus-data-find
2221                     ,(or number '(gnus-summary-article-number)))))
2222
2223 (defmacro gnus-summary-article-pos (&optional number)
2224   "Return the position of the line of article NUMBER."
2225   `(gnus-data-pos (gnus-data-find
2226                    ,(or number '(gnus-summary-article-number)))))
2227
2228 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2229 (defmacro gnus-summary-article-subject (&optional number)
2230   "Return current subject string or nil if nothing."
2231   `(let ((headers
2232           ,(if number
2233                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2234              '(gnus-data-header (assq (gnus-summary-article-number)
2235                                       gnus-newsgroup-data)))))
2236      (and headers
2237           (vectorp headers)
2238           (mail-header-subject headers))))
2239
2240 (defmacro gnus-summary-article-score (&optional number)
2241   "Return current article score."
2242   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2243                   gnus-newsgroup-scored))
2244        gnus-summary-default-score 0))
2245
2246 (defun gnus-summary-article-children (&optional number)
2247   "Return a list of article numbers that are children of article NUMBER."
2248   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2249          (level (gnus-data-level (car data)))
2250          l children)
2251     (while (and (setq data (cdr data))
2252                 (> (setq l (gnus-data-level (car data))) level))
2253       (and (= (1+ level) l)
2254            (push (gnus-data-number (car data))
2255                  children)))
2256     (nreverse children)))
2257
2258 (defun gnus-summary-article-parent (&optional number)
2259   "Return the article number of the parent of article NUMBER."
2260   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2261                                     (gnus-data-list t)))
2262          (level (gnus-data-level (car data))))
2263     (if (zerop level)
2264         ()                              ; This is a root.
2265       ;; We search until we find an article with a level less than
2266       ;; this one.  That function has to be the parent.
2267       (while (and (setq data (cdr data))
2268                   (not (< (gnus-data-level (car data)) level))))
2269       (and data (gnus-data-number (car data))))))
2270
2271 (defun gnus-unread-mark-p (mark)
2272   "Say whether MARK is the unread mark."
2273   (= mark gnus-unread-mark))
2274
2275 (defun gnus-read-mark-p (mark)
2276   "Say whether MARK is one of the marks that mark as read.
2277 This is all marks except unread, ticked, dormant, and expirable."
2278   (not (or (= mark gnus-unread-mark)
2279            (= mark gnus-ticked-mark)
2280            (= mark gnus-dormant-mark)
2281            (= mark gnus-expirable-mark))))
2282
2283 (defmacro gnus-article-mark (number)
2284   "Return the MARK of article NUMBER.
2285 This macro should only be used when computing the mark the \"first\"
2286 time; i.e., when generating the summary lines.  After that,
2287 `gnus-summary-article-mark' should be used to examine the
2288 marks of articles."
2289   `(cond
2290     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2291     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2292     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2293     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2294     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2295     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2296     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2297     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2298            gnus-ancient-mark))))
2299
2300 ;; Saving hidden threads.
2301
2302 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2303 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2304
2305 (defmacro gnus-save-hidden-threads (&rest forms)
2306   "Save hidden threads, eval FORMS, and restore the hidden threads."
2307   (let ((config (make-symbol "config")))
2308     `(let ((,config (gnus-hidden-threads-configuration)))
2309        (unwind-protect
2310            (save-excursion
2311              ,@forms)
2312          (gnus-restore-hidden-threads-configuration ,config)))))
2313
2314 (defun gnus-data-compute-positions ()
2315   "Compute the positions of all articles."
2316   (setq gnus-newsgroup-data-reverse nil)
2317   (let ((data gnus-newsgroup-data))
2318     (save-excursion
2319       (gnus-save-hidden-threads
2320         (gnus-summary-show-all-threads)
2321         (goto-char (point-min))
2322         (while data
2323           (while (get-text-property (point) 'gnus-intangible)
2324             (forward-line 1))
2325           (gnus-data-set-pos (car data) (+ (point) 3))
2326           (setq data (cdr data))
2327           (forward-line 1))))))
2328
2329 (defun gnus-hidden-threads-configuration ()
2330   "Return the current hidden threads configuration."
2331   (save-excursion
2332     (let (config)
2333       (goto-char (point-min))
2334       (while (search-forward "\r" nil t)
2335         (push (1- (point)) config))
2336       config)))
2337
2338 (defun gnus-restore-hidden-threads-configuration (config)
2339   "Restore hidden threads configuration from CONFIG."
2340   (let (point buffer-read-only)
2341     (while (setq point (pop config))
2342       (when (and (< point (point-max))
2343                  (goto-char point)
2344                  (eq (char-after) ?\n))
2345         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2346
2347 ;; Various summary mode internalish functions.
2348
2349 (defun gnus-mouse-pick-article (e)
2350   (interactive "e")
2351   (mouse-set-point e)
2352   (gnus-summary-next-page nil t))
2353
2354 (defun gnus-summary-set-display-table ()
2355   ;; Change the display table.  Odd characters have a tendency to mess
2356   ;; up nicely formatted displays - we make all possible glyphs
2357   ;; display only a single character.
2358
2359   ;; We start from the standard display table, if any.
2360   (let ((table (or (copy-sequence standard-display-table)
2361                    (make-display-table)))
2362         (i 32))
2363     ;; Nix out all the control chars...
2364     (while (>= (setq i (1- i)) 0)
2365       (aset table i [??]))
2366     ;; ... but not newline and cr, of course.  (cr is necessary for the
2367     ;; selective display).
2368     (aset table ?\n nil)
2369     (aset table ?\r nil)
2370     ;; We keep TAB as well.
2371     (aset table ?\t nil)
2372     ;; We nix out any glyphs over 126 that are not set already.
2373     (let ((i 256))
2374       (while (>= (setq i (1- i)) 127)
2375         ;; Only modify if the entry is nil.
2376         (unless (aref table i)
2377           (aset table i [??]))))
2378     (setq buffer-display-table table)))
2379
2380 (defun gnus-summary-setup-buffer (group)
2381   "Initialize summary buffer."
2382   (let ((buffer (concat "*Summary " group "*")))
2383     (if (get-buffer buffer)
2384         (progn
2385           (set-buffer buffer)
2386           (setq gnus-summary-buffer (current-buffer))
2387           (not gnus-newsgroup-prepared))
2388       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2389       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2390       (gnus-summary-mode group)
2391       (when gnus-carpal
2392         (gnus-carpal-setup-buffer 'summary))
2393       (unless gnus-single-article-buffer
2394         (make-local-variable 'gnus-article-buffer)
2395         (make-local-variable 'gnus-article-current)
2396         (make-local-variable 'gnus-original-article-buffer))
2397       (setq gnus-newsgroup-name group)
2398       t)))
2399
2400 (defun gnus-set-global-variables ()
2401   ;; Set the global equivalents of the summary buffer-local variables
2402   ;; to the latest values they had.  These reflect the summary buffer
2403   ;; that was in action when the last article was fetched.
2404   (when (eq major-mode 'gnus-summary-mode)
2405     (setq gnus-summary-buffer (current-buffer))
2406     (let ((name gnus-newsgroup-name)
2407           (marked gnus-newsgroup-marked)
2408           (unread gnus-newsgroup-unreads)
2409           (headers gnus-current-headers)
2410           (data gnus-newsgroup-data)
2411           (summary gnus-summary-buffer)
2412           (article-buffer gnus-article-buffer)
2413           (original gnus-original-article-buffer)
2414           (gac gnus-article-current)
2415           (reffed gnus-reffed-article-number)
2416           (score-file gnus-current-score-file)
2417           (default-charset gnus-newsgroup-default-charset)
2418           (iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
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-default-charset default-charset
2433               gnus-newsgroup-iso-8859-1-forced iso-8859-1-forced)
2434         ;; The article buffer also has local variables.
2435         (when (gnus-buffer-live-p gnus-article-buffer)
2436           (set-buffer gnus-article-buffer)
2437           (setq gnus-summary-buffer summary))))))
2438
2439 (defun gnus-summary-article-unread-p (article)
2440   "Say whether ARTICLE is unread or not."
2441   (memq article gnus-newsgroup-unreads))
2442
2443 (defun gnus-summary-first-article-p (&optional article)
2444   "Return whether ARTICLE is the first article in the buffer."
2445   (if (not (setq article (or article (gnus-summary-article-number))))
2446       nil
2447     (eq article (caar gnus-newsgroup-data))))
2448
2449 (defun gnus-summary-last-article-p (&optional article)
2450   "Return whether ARTICLE is the last article in the buffer."
2451   (if (not (setq article (or article (gnus-summary-article-number))))
2452       t                                 ; All non-existent numbers are the last article.  :-)
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         (rfc2047-default-charset gnus-newsgroup-default-charset)
2510         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
2511     (cond
2512      ((and to
2513            gnus-ignored-from-addresses
2514            (string-match gnus-ignored-from-addresses
2515                          (mail-header-from header)))
2516       (concat "-> "
2517               (or (car (funcall gnus-extract-address-components
2518                                 (funcall
2519                                  gnus-decode-encoded-word-function to)))
2520                   (funcall gnus-decode-encoded-word-function to))))
2521      ((and newsgroups
2522            gnus-ignored-from-addresses
2523            (string-match gnus-ignored-from-addresses
2524                          (mail-header-from header)))
2525       (concat "=> " newsgroups))
2526      (t
2527       (or (car (funcall gnus-extract-address-components
2528                         (mail-header-from header)))
2529           (mail-header-from header))))))
2530
2531 (defun gnus-summary-insert-line (gnus-tmp-header
2532                                  gnus-tmp-level gnus-tmp-current
2533                                  gnus-tmp-unread gnus-tmp-replied
2534                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2535                                  &optional gnus-tmp-dummy gnus-tmp-score
2536                                  gnus-tmp-process)
2537   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2538          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2539          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2540          (gnus-tmp-score-char
2541           (if (or (null gnus-summary-default-score)
2542                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2543                       gnus-summary-zcore-fuzz))
2544               ? 
2545             (if (< gnus-tmp-score gnus-summary-default-score)
2546                 gnus-score-below-mark gnus-score-over-mark)))
2547          (gnus-tmp-replied
2548           (cond (gnus-tmp-process gnus-process-mark)
2549                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2550                  gnus-cached-mark)
2551                 (gnus-tmp-replied gnus-replied-mark)
2552                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2553                  gnus-saved-mark)
2554                 (t gnus-unread-mark)))
2555          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2556          (gnus-tmp-name
2557           (cond
2558            ((string-match "<[^>]+> *$" gnus-tmp-from)
2559             (let ((beg (match-beginning 0)))
2560               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2561                        (substring gnus-tmp-from (1+ (match-beginning 0))
2562                                   (1- (match-end 0))))
2563                   (substring gnus-tmp-from 0 beg))))
2564            ((string-match "(.+)" gnus-tmp-from)
2565             (substring gnus-tmp-from
2566                        (1+ (match-beginning 0)) (1- (match-end 0))))
2567            (t gnus-tmp-from)))
2568          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2569          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2570          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2571          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2572          (buffer-read-only nil))
2573     (when (string= gnus-tmp-name "")
2574       (setq gnus-tmp-name gnus-tmp-from))
2575     (unless (numberp gnus-tmp-lines)
2576       (setq gnus-tmp-lines 0))
2577     (gnus-put-text-property
2578      (point)
2579      (progn (eval gnus-summary-line-format-spec) (point))
2580      'gnus-number gnus-tmp-number)
2581     (when (gnus-visual-p 'summary-highlight 'highlight)
2582       (forward-line -1)
2583       (gnus-run-hooks 'gnus-summary-update-hook)
2584       (forward-line 1))))
2585
2586 (defun gnus-summary-update-line (&optional dont-update)
2587   ;; Update summary line after change.
2588   (when (and gnus-summary-default-score
2589              (not gnus-summary-inhibit-highlight))
2590     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2591            (article (gnus-summary-article-number))
2592            (score (gnus-summary-article-score article)))
2593       (unless dont-update
2594         (if (and gnus-summary-mark-below
2595                  (< (gnus-summary-article-score)
2596                     gnus-summary-mark-below))
2597             ;; This article has a low score, so we mark it as read.
2598             (when (memq article gnus-newsgroup-unreads)
2599               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2600           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2601             ;; This article was previously marked as read on account
2602             ;; of a low score, but now it has risen, so we mark it as
2603             ;; unread.
2604             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2605         (gnus-summary-update-mark
2606          (if (or (null gnus-summary-default-score)
2607                  (<= (abs (- score gnus-summary-default-score))
2608                      gnus-summary-zcore-fuzz))
2609              ? 
2610            (if (< score gnus-summary-default-score)
2611                gnus-score-below-mark gnus-score-over-mark))
2612          'score))
2613       ;; Do visual highlighting.
2614       (when (gnus-visual-p 'summary-highlight 'highlight)
2615         (gnus-run-hooks 'gnus-summary-update-hook)))))
2616
2617 (defvar gnus-tmp-new-adopts nil)
2618
2619 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2620   "Return the number of articles in THREAD.
2621 This may be 0 in some cases -- if none of the articles in
2622 the thread are to be displayed."
2623   (let* ((number
2624           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2625           (cond
2626            ((not (listp thread))
2627             1)
2628            ((and (consp thread) (cdr thread))
2629             (apply
2630              '+ 1 (mapcar
2631                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2632            ((null thread)
2633             1)
2634            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2635             1)
2636            (t 0))))
2637     (when (and level (zerop level) gnus-tmp-new-adopts)
2638       (incf number
2639             (apply '+ (mapcar
2640                        'gnus-summary-number-of-articles-in-thread
2641                        gnus-tmp-new-adopts))))
2642     (if char
2643         (if (> number 1) gnus-not-empty-thread-mark
2644           gnus-empty-thread-mark)
2645       number)))
2646
2647 (defun gnus-summary-set-local-parameters (group)
2648   "Go through the local params of GROUP and set all variable specs in that list."
2649   (let ((params (gnus-group-find-parameter group))
2650         elem)
2651     (while params
2652       (setq elem (car params)
2653             params (cdr params))
2654       (and (consp elem)                 ; Has to be a cons.
2655            (consp (cdr elem))           ; The cdr has to be a list.
2656            (symbolp (car elem))         ; Has to be a symbol in there.
2657            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2658            (ignore-errors               ; So we set it.
2659              (make-local-variable (car elem))
2660              (set (car elem) (eval (nth 1 elem))))))))
2661
2662 (defun gnus-summary-read-group (group &optional show-all no-article
2663                                       kill-buffer no-display backward
2664                                       select-articles)
2665   "Start reading news in newsgroup GROUP.
2666 If SHOW-ALL is non-nil, already read articles are also listed.
2667 If NO-ARTICLE is non-nil, no article is selected initially.
2668 If NO-DISPLAY, don't generate a summary buffer."
2669   (let (result)
2670     (while (and group
2671                 (null (setq result
2672                             (let ((gnus-auto-select-next nil))
2673                               (or (gnus-summary-read-group-1
2674                                    group show-all no-article
2675                                    kill-buffer no-display
2676                                    select-articles)
2677                                   (setq show-all nil
2678                                         select-articles nil)))))
2679                 (eq gnus-auto-select-next 'quietly))
2680       (set-buffer gnus-group-buffer)
2681       ;; The entry function called above goes to the next
2682       ;; group automatically, so we go two groups back
2683       ;; if we are searching for the previous group.
2684       (when backward
2685         (gnus-group-prev-unread-group 2))
2686       (if (not (equal group (gnus-group-group-name)))
2687           (setq group (gnus-group-group-name))
2688         (setq group nil)))
2689     result))
2690
2691 (defun gnus-summary-read-group-1 (group show-all no-article
2692                                         kill-buffer no-display
2693                                         &optional select-articles)
2694   ;; Killed foreign groups can't be entered.
2695   (when (and (not (gnus-group-native-p group))
2696              (not (gnus-gethash group gnus-newsrc-hashtb)))
2697     (error "Dead non-native groups can't be entered"))
2698   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2699   (let* ((new-group (gnus-summary-setup-buffer group))
2700          (quit-config (gnus-group-quit-config group))
2701          (did-select (and new-group (gnus-select-newsgroup
2702                                      group show-all select-articles))))
2703     (cond
2704      ;; This summary buffer exists already, so we just select it.
2705      ((not new-group)
2706       (gnus-set-global-variables)
2707       (when kill-buffer
2708         (gnus-kill-or-deaden-summary kill-buffer))
2709       (gnus-configure-windows 'summary 'force)
2710       (gnus-set-mode-line 'summary)
2711       (gnus-summary-position-point)
2712       (message "")
2713       t)
2714      ;; We couldn't select this group.
2715      ((null did-select)
2716       (when (and (eq major-mode 'gnus-summary-mode)
2717                  (not (equal (current-buffer) kill-buffer)))
2718         (kill-buffer (current-buffer))
2719         (if (not quit-config)
2720             (progn
2721               ;; Update the info -- marks might need to be removed,
2722               ;; for instance.
2723               (gnus-summary-update-info)
2724               (set-buffer gnus-group-buffer)
2725               (gnus-group-jump-to-group group)
2726               (gnus-group-next-unread-group 1))
2727           (gnus-handle-ephemeral-exit quit-config)))
2728       (gnus-message 3 "Can't select group")
2729       nil)
2730      ;; The user did a `C-g' while prompting for number of articles,
2731      ;; so we exit this group.
2732      ((eq did-select 'quit)
2733       (and (eq major-mode 'gnus-summary-mode)
2734            (not (equal (current-buffer) kill-buffer))
2735            (kill-buffer (current-buffer)))
2736       (when kill-buffer
2737         (gnus-kill-or-deaden-summary kill-buffer))
2738       (if (not quit-config)
2739           (progn
2740             (set-buffer gnus-group-buffer)
2741             (gnus-group-jump-to-group group)
2742             (gnus-group-next-unread-group 1)
2743             (gnus-configure-windows 'group 'force))
2744         (gnus-handle-ephemeral-exit quit-config))
2745       ;; Finally signal the quit.
2746       (signal 'quit nil))
2747      ;; The group was successfully selected.
2748      (t
2749       (gnus-set-global-variables)
2750       ;; Save the active value in effect when the group was entered.
2751       (setq gnus-newsgroup-active
2752             (gnus-copy-sequence
2753              (gnus-active gnus-newsgroup-name)))
2754       ;; You can change the summary buffer in some way with this hook.
2755       (gnus-run-hooks 'gnus-select-group-hook)
2756       ;; Set any local variables in the group parameters.
2757       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2758       (gnus-update-format-specifications
2759        nil 'summary 'summary-mode 'summary-dummy)
2760       ;; Do score processing.
2761       (when gnus-use-scoring
2762         (gnus-possibly-score-headers))
2763       ;; Check whether to fill in the gaps in the threads.
2764       (when gnus-build-sparse-threads
2765         (gnus-build-sparse-threads))
2766       ;; Find the initial limit.
2767       (if gnus-show-threads
2768           (if show-all
2769               (let ((gnus-newsgroup-dormant nil))
2770                 (gnus-summary-initial-limit show-all))
2771             (gnus-summary-initial-limit show-all))
2772         (setq gnus-newsgroup-limit
2773               (mapcar
2774                (lambda (header) (mail-header-number header))
2775                gnus-newsgroup-headers)))
2776       ;; Generate the summary buffer.
2777       (unless no-display
2778         (gnus-summary-prepare))
2779       (when gnus-use-trees
2780         (gnus-tree-open group)
2781         (setq gnus-summary-highlight-line-function
2782               'gnus-tree-highlight-article))
2783       ;; If the summary buffer is empty, but there are some low-scored
2784       ;; articles or some excluded dormants, we include these in the
2785       ;; buffer.
2786       (when (and (zerop (buffer-size))
2787                  (not no-display))
2788         (cond (gnus-newsgroup-dormant
2789                (gnus-summary-limit-include-dormant))
2790               ((and gnus-newsgroup-scored show-all)
2791                (gnus-summary-limit-include-expunged t))))
2792       ;; Function `gnus-apply-kill-file' must be called in this hook.
2793       (gnus-run-hooks 'gnus-apply-kill-hook)
2794       (if (and (zerop (buffer-size))
2795                (not no-display))
2796           (progn
2797             ;; This newsgroup is empty.
2798             (gnus-summary-catchup-and-exit nil t)
2799             (gnus-message 6 "No unread news")
2800             (when kill-buffer
2801               (gnus-kill-or-deaden-summary kill-buffer))
2802             ;; Return nil from this function.
2803             nil)
2804         ;; Hide conversation thread subtrees.  We cannot do this in
2805         ;; gnus-summary-prepare-hook since kill processing may not
2806         ;; work with hidden articles.
2807         (and gnus-show-threads
2808              gnus-thread-hide-subtree
2809              (gnus-summary-hide-all-threads))
2810         (when kill-buffer
2811           (gnus-kill-or-deaden-summary kill-buffer))
2812         ;; Show first unread article if requested.
2813         (if (and (not no-article)
2814                  (not no-display)
2815                  gnus-newsgroup-unreads
2816                  gnus-auto-select-first)
2817             (progn
2818               (gnus-configure-windows 'summary)
2819               (cond
2820                ((eq gnus-auto-select-first 'best)
2821                 (gnus-summary-best-unread-article))
2822                ((eq gnus-auto-select-first t)
2823                 (gnus-summary-first-unread-article))
2824                ((gnus-functionp gnus-auto-select-first)
2825                 (funcall gnus-auto-select-first))))
2826           ;; Don't select any articles, just move point to the first
2827           ;; article in the group.
2828           (goto-char (point-min))
2829           (gnus-summary-position-point)
2830           (gnus-configure-windows 'summary 'force)
2831           (gnus-set-mode-line 'summary))
2832         (when (get-buffer-window gnus-group-buffer t)
2833           ;; Gotta use windows, because recenter does weird stuff if
2834           ;; the current buffer ain't the displayed window.
2835           (let ((owin (selected-window)))
2836             (select-window (get-buffer-window gnus-group-buffer t))
2837             (when (gnus-group-goto-group group)
2838               (recenter))
2839             (select-window owin)))
2840         ;; Mark this buffer as "prepared".
2841         (setq gnus-newsgroup-prepared t)
2842         (gnus-run-hooks 'gnus-summary-prepared-hook)
2843         t)))))
2844
2845 (defun gnus-summary-prepare ()
2846   "Generate the summary buffer."
2847   (interactive)
2848   (let ((buffer-read-only nil))
2849     (erase-buffer)
2850     (setq gnus-newsgroup-data nil
2851           gnus-newsgroup-data-reverse nil)
2852     (gnus-run-hooks 'gnus-summary-generate-hook)
2853     ;; Generate the buffer, either with threads or without.
2854     (when gnus-newsgroup-headers
2855       (gnus-summary-prepare-threads
2856        (if gnus-show-threads
2857            (gnus-sort-gathered-threads
2858             (funcall gnus-summary-thread-gathering-function
2859                      (gnus-sort-threads
2860                       (gnus-cut-threads (gnus-make-threads)))))
2861          ;; Unthreaded display.
2862          (gnus-sort-articles gnus-newsgroup-headers))))
2863     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2864     ;; Call hooks for modifying summary buffer.
2865     (goto-char (point-min))
2866     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2867
2868 (defsubst gnus-general-simplify-subject (subject)
2869   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2870   (setq subject
2871         (cond
2872          ;; Truncate the subject.
2873          (gnus-simplify-subject-functions
2874           (gnus-map-function gnus-simplify-subject-functions subject))
2875          ((numberp gnus-summary-gather-subject-limit)
2876           (setq subject (gnus-simplify-subject-re subject))
2877           (if (> (length subject) gnus-summary-gather-subject-limit)
2878               (substring subject 0 gnus-summary-gather-subject-limit)
2879             subject))
2880          ;; Fuzzily simplify it.
2881          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2882           (gnus-simplify-subject-fuzzy subject))
2883          ;; Just remove the leading "Re:".
2884          (t
2885           (gnus-simplify-subject-re subject))))
2886
2887   (if (and gnus-summary-gather-exclude-subject
2888            (string-match gnus-summary-gather-exclude-subject subject))
2889       nil                               ; This article shouldn't be gathered
2890     subject))
2891
2892 (defun gnus-summary-simplify-subject-query ()
2893   "Query where the respool algorithm would put this article."
2894   (interactive)
2895   (gnus-summary-select-article)
2896   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2897
2898 (defun gnus-gather-threads-by-subject (threads)
2899   "Gather threads by looking at Subject headers."
2900   (if (not gnus-summary-make-false-root)
2901       threads
2902     (let ((hashtb (gnus-make-hashtable 1024))
2903           (prev threads)
2904           (result threads)
2905           subject hthread whole-subject)
2906       (while threads
2907         (setq subject (gnus-general-simplify-subject
2908                        (setq whole-subject (mail-header-subject
2909                                             (caar threads)))))
2910         (when subject
2911           (if (setq hthread (gnus-gethash subject hashtb))
2912               (progn
2913                 ;; We enter a dummy root into the thread, if we
2914                 ;; haven't done that already.
2915                 (unless (stringp (caar hthread))
2916                   (setcar hthread (list whole-subject (car hthread))))
2917                 ;; We add this new gathered thread to this gathered
2918                 ;; thread.
2919                 (setcdr (car hthread)
2920                         (nconc (cdar hthread) (list (car threads))))
2921                 ;; Remove it from the list of threads.
2922                 (setcdr prev (cdr threads))
2923                 (setq threads prev))
2924             ;; Enter this thread into the hash table.
2925             (gnus-sethash subject threads hashtb)))
2926         (setq prev threads)
2927         (setq threads (cdr threads)))
2928       result)))
2929
2930 (defun gnus-gather-threads-by-references (threads)
2931   "Gather threads by looking at References headers."
2932   (let ((idhashtb (gnus-make-hashtable 1024))
2933         (thhashtb (gnus-make-hashtable 1024))
2934         (prev threads)
2935         (result threads)
2936         ids references id gthread gid entered ref)
2937     (while threads
2938       (when (setq references (mail-header-references (caar threads)))
2939         (setq id (mail-header-id (caar threads))
2940               ids (gnus-split-references references)
2941               entered nil)
2942         (while (setq ref (pop ids))
2943           (setq ids (delete ref ids))
2944           (if (not (setq gid (gnus-gethash ref idhashtb)))
2945               (progn
2946                 (gnus-sethash ref id idhashtb)
2947                 (gnus-sethash id threads thhashtb))
2948             (setq gthread (gnus-gethash gid thhashtb))
2949             (unless entered
2950               ;; We enter a dummy root into the thread, if we
2951               ;; haven't done that already.
2952               (unless (stringp (caar gthread))
2953                 (setcar gthread (list (mail-header-subject (caar gthread))
2954                                       (car gthread))))
2955               ;; We add this new gathered thread to this gathered
2956               ;; thread.
2957               (setcdr (car gthread)
2958                       (nconc (cdar gthread) (list (car threads)))))
2959             ;; Add it into the thread hash table.
2960             (gnus-sethash id gthread thhashtb)
2961             (setq entered t)
2962             ;; Remove it from the list of threads.
2963             (setcdr prev (cdr threads))
2964             (setq threads prev))))
2965       (setq prev threads)
2966       (setq threads (cdr threads)))
2967     result))
2968
2969 (defun gnus-sort-gathered-threads (threads)
2970   "Sort subtreads inside each gathered thread by article number."
2971   (let ((result threads))
2972     (while threads
2973       (when (stringp (caar threads))
2974         (setcdr (car threads)
2975                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2976       (setq threads (cdr threads)))
2977     result))
2978
2979 (defun gnus-thread-loop-p (root thread)
2980   "Say whether ROOT is in THREAD."
2981   (let ((stack (list thread))
2982         (infloop 0)
2983         th)
2984     (while (setq thread (pop stack))
2985       (setq th (cdr thread))
2986       (while (and th
2987                   (not (eq (caar th) root)))
2988         (pop th))
2989       (if th
2990           ;; We have found a loop.
2991           (let (ref-dep)
2992             (setcdr thread (delq (car th) (cdr thread)))
2993             (if (boundp (setq ref-dep (intern "none"
2994                                               gnus-newsgroup-dependencies)))
2995                 (setcdr (symbol-value ref-dep)
2996                         (nconc (cdr (symbol-value ref-dep))
2997                                (list (car th))))
2998               (set ref-dep (list nil (car th))))
2999             (setq infloop 1
3000                   stack nil))
3001         ;; Push all the subthreads onto the stack.
3002         (push (cdr thread) stack)))
3003     infloop))
3004
3005 (defun gnus-make-threads ()
3006   "Go through the dependency hashtb and find the roots.  Return all threads."
3007   (let (threads)
3008     (while (catch 'infloop
3009              (mapatoms
3010               (lambda (refs)
3011                 ;; Deal with self-referencing References loops.
3012                 (when (and (car (symbol-value refs))
3013                            (not (zerop
3014                                  (apply
3015                                   '+
3016                                   (mapcar
3017                                    (lambda (thread)
3018                                      (gnus-thread-loop-p
3019                                       (car (symbol-value refs)) thread))
3020                                    (cdr (symbol-value refs)))))))
3021                   (setq threads nil)
3022                   (throw 'infloop t))
3023                 (unless (car (symbol-value refs))
3024                   ;; These threads do not refer back to any other articles,
3025                   ;; so they're roots.
3026                   (setq threads (append (cdr (symbol-value refs)) threads))))
3027               gnus-newsgroup-dependencies)))
3028     threads))
3029
3030 ;; Build the thread tree.
3031 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3032   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3033
3034 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3035 if it was already present.
3036
3037 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3038 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3039 Message-IDs will be renamed be renamed to a unique Message-ID before
3040 being entered.
3041
3042 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3043   (let* ((id (mail-header-id header))
3044          (id-dep (and id (intern id dependencies)))
3045          ref ref-dep ref-header)
3046     ;; Enter this `header' in the `dependencies' table.
3047     (cond
3048      ((not id-dep)
3049       (setq header nil))
3050      ;; The first two cases do the normal part: enter a new `header'
3051      ;; in the `dependencies' table.
3052      ((not (boundp id-dep))
3053       (set id-dep (list header)))
3054      ((null (car (symbol-value id-dep)))
3055       (setcar (symbol-value id-dep) header))
3056
3057      ;; From here the `header' was already present in the
3058      ;; `dependencies' table.
3059      (force-new
3060       ;; Overrides an existing entry;
3061       ;; just set the header part of the entry.
3062       (setcar (symbol-value id-dep) header))
3063
3064      ;; Renames the existing `header' to a unique Message-ID.
3065      ((not gnus-summary-ignore-duplicates)
3066       ;; An article with this Message-ID has already been seen.
3067       ;; We rename the Message-ID.
3068       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3069            (list header))
3070       (mail-header-set-id header id))
3071
3072      ;; The last case ignores an existing entry, except it adds any
3073      ;; additional Xrefs (in case the two articles came from different
3074      ;; servers.
3075      ;; Also sets `header' to `nil' meaning that the `dependencies'
3076      ;; table was *not* modified.
3077      (t
3078       (mail-header-set-xref
3079        (car (symbol-value id-dep))
3080        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3081                    "")
3082                (or (mail-header-xref header) "")))
3083       (setq header nil)))
3084
3085     (when header
3086       ;; First check if that we are not creating a References loop.
3087       (setq ref (gnus-parent-id (mail-header-references header)))
3088       (while (and ref
3089                   (setq ref-dep (intern-soft ref dependencies))
3090                   (boundp ref-dep)
3091                   (setq ref-header (car (symbol-value ref-dep))))
3092         (if (string= id ref)
3093             ;; Yuk!  This is a reference loop.  Make the article be a
3094             ;; root article.
3095             (progn
3096               (mail-header-set-references (car (symbol-value id-dep)) "none")
3097               (setq ref nil))
3098           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3099       (setq ref (gnus-parent-id (mail-header-references header)))
3100       (setq ref-dep (intern (or ref "none") dependencies))
3101       (if (boundp ref-dep)
3102           (setcdr (symbol-value ref-dep)
3103                   (nconc (cdr (symbol-value ref-dep))
3104                          (list (symbol-value id-dep))))
3105         (set ref-dep (list nil (symbol-value id-dep)))))
3106     header))
3107
3108 (defun gnus-build-sparse-threads ()
3109   (let ((headers gnus-newsgroup-headers)
3110         (gnus-summary-ignore-duplicates t)
3111         header references generation relations
3112         subject child end new-child date)
3113     ;; First we create an alist of generations/relations, where
3114     ;; generations is how much we trust the relation, and the relation
3115     ;; is parent/child.
3116     (gnus-message 7 "Making sparse threads...")
3117     (save-excursion
3118       (nnheader-set-temp-buffer " *gnus sparse threads*")
3119       (while (setq header (pop headers))
3120         (when (and (setq references (mail-header-references header))
3121                    (not (string= references "")))
3122           (insert references)
3123           (setq child (mail-header-id header)
3124                 subject (mail-header-subject header)
3125                 date (mail-header-date header)
3126                 generation 0)
3127           (while (search-backward ">" nil t)
3128             (setq end (1+ (point)))
3129             (when (search-backward "<" nil t)
3130               (setq new-child (buffer-substring (point) end))
3131               (push (list (incf generation)
3132                           child (setq child new-child)
3133                           subject date)
3134                     relations)))
3135           (when child
3136             (push (list (1+ generation) child nil subject) relations))
3137           (erase-buffer)))
3138       (kill-buffer (current-buffer)))
3139     ;; Sort over trustworthiness.
3140     (mapcar
3141      (lambda (relation)
3142        (when (gnus-dependencies-add-header
3143               (make-full-mail-header
3144                gnus-reffed-article-number
3145                (nth 3 relation) "" (or (nth 4 relation) "")
3146                (nth 1 relation)
3147                (or (nth 2 relation) "") 0 0 "")
3148               gnus-newsgroup-dependencies nil)
3149          (push gnus-reffed-article-number gnus-newsgroup-limit)
3150          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3151          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3152                gnus-newsgroup-reads)
3153          (decf gnus-reffed-article-number)))
3154      (sort relations 'car-less-than-car))
3155     (gnus-message 7 "Making sparse threads...done")))
3156
3157 (defun gnus-build-old-threads ()
3158   ;; Look at all the articles that refer back to old articles, and
3159   ;; fetch the headers for the articles that aren't there.  This will
3160   ;; build complete threads - if the roots haven't been expired by the
3161   ;; server, that is.
3162   (let (id heads)
3163     (mapatoms
3164      (lambda (refs)
3165        (when (not (car (symbol-value refs)))
3166          (setq heads (cdr (symbol-value refs)))
3167          (while heads
3168            (if (memq (mail-header-number (caar heads))
3169                      gnus-newsgroup-dormant)
3170                (setq heads (cdr heads))
3171              (setq id (symbol-name refs))
3172              (while (and (setq id (gnus-build-get-header id))
3173                          (not (car (gnus-id-to-thread id)))))
3174              (setq heads nil)))))
3175      gnus-newsgroup-dependencies)))
3176
3177 (defmacro gnus-nov-read-integer ()
3178   '(prog1
3179        (if (eq (char-after) ?\t)
3180            0
3181          (let ((num (ignore-errors (read buffer))))
3182            (if (numberp num) num 0)))
3183      (unless (eobp)
3184        (search-forward "\t" eol 'move))))
3185
3186 (defmacro gnus-nov-skip-field ()
3187   '(search-forward "\t" eol 'move))
3188
3189 (defmacro gnus-nov-field ()
3190   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3191
3192 (defmacro gnus-nov-parse-extra ()
3193   '(let (out string)
3194      (while (not (memq (char-after) '(?\n nil)))
3195        (setq string (gnus-nov-field))
3196        (when (string-match "^\\([^ :]+\\): " string)
3197          (push (cons (intern (match-string 1 string))
3198                      (substring string (match-end 0)))
3199                out)))
3200      out))
3201
3202 ;; This function has to be called with point after the article number
3203 ;; on the beginning of the line.
3204 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3205   (let ((eol (gnus-point-at-eol))
3206         (buffer (current-buffer))
3207         header)
3208
3209     ;; overview: [num subject from date id refs chars lines misc]
3210     (unwind-protect
3211         (progn
3212           (narrow-to-region (point) eol)
3213           (unless (eobp)
3214             (forward-char))
3215
3216           (setq header
3217                 (make-full-mail-header
3218                  number                 ; number
3219                  (funcall gnus-decode-encoded-word-function
3220                           (gnus-nov-field)) ; subject
3221                  (funcall gnus-decode-encoded-word-function
3222                           (gnus-nov-field)) ; from
3223                  (gnus-nov-field)       ; date
3224                  (or (gnus-nov-field)
3225                      (nnheader-generate-fake-message-id)) ; id
3226                  (gnus-nov-field)       ; refs
3227                  (gnus-nov-read-integer) ; chars
3228                  (gnus-nov-read-integer) ; lines
3229                  (unless (eq (char-after) ?\n)
3230                    (gnus-nov-field))    ; misc
3231                  (gnus-nov-parse-extra)))) ; extra
3232
3233       (widen))
3234
3235     (when gnus-alter-header-function
3236       (funcall gnus-alter-header-function header))
3237     (gnus-dependencies-add-header header dependencies force-new)))
3238
3239 (defun gnus-build-get-header (id)
3240   ;; Look through the buffer of NOV lines and find the header to
3241   ;; ID.  Enter this line into the dependencies hash table, and return
3242   ;; the id of the parent article (if any).
3243   (let ((deps gnus-newsgroup-dependencies)
3244         found header)
3245     (prog1
3246         (save-excursion
3247           (set-buffer nntp-server-buffer)
3248           (let ((case-fold-search nil))
3249             (goto-char (point-min))
3250             (while (and (not found)
3251                         (search-forward id nil t))
3252               (beginning-of-line)
3253               (setq found (looking-at
3254                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3255                                    (regexp-quote id))))
3256               (or found (beginning-of-line 2)))
3257             (when found
3258               (beginning-of-line)
3259               (and
3260                (setq header (gnus-nov-parse-line
3261                              (read (current-buffer)) deps))
3262                (gnus-parent-id (mail-header-references header))))))
3263       (when header
3264         (let ((number (mail-header-number header)))
3265           (push number gnus-newsgroup-limit)
3266           (push header gnus-newsgroup-headers)
3267           (if (memq number gnus-newsgroup-unselected)
3268               (progn
3269                 (push number gnus-newsgroup-unreads)
3270                 (setq gnus-newsgroup-unselected
3271                       (delq number gnus-newsgroup-unselected)))
3272             (push number gnus-newsgroup-ancient)))))))
3273
3274 (defun gnus-build-all-threads ()
3275   "Read all the headers."
3276   (let ((gnus-summary-ignore-duplicates t)
3277         (dependencies gnus-newsgroup-dependencies)
3278         header article)
3279     (save-excursion
3280       (set-buffer nntp-server-buffer)
3281       (let ((case-fold-search nil))
3282         (goto-char (point-min))
3283         (while (not (eobp))
3284           (ignore-errors
3285             (setq article (read (current-buffer))
3286                   header (gnus-nov-parse-line
3287                           article dependencies)))
3288           (when header
3289             (save-excursion
3290               (set-buffer gnus-summary-buffer)
3291               (push header gnus-newsgroup-headers)
3292               (if (memq (setq article (mail-header-number header))
3293                         gnus-newsgroup-unselected)
3294                   (progn
3295                     (push article gnus-newsgroup-unreads)
3296                     (setq gnus-newsgroup-unselected
3297                           (delq article gnus-newsgroup-unselected)))
3298                 (push article gnus-newsgroup-ancient)))
3299             (forward-line 1)))))))
3300
3301 (defun gnus-summary-update-article-line (article header)
3302   "Update the line for ARTICLE using HEADERS."
3303   (let* ((id (mail-header-id header))
3304          (thread (gnus-id-to-thread id)))
3305     (unless thread
3306       (error "Article in no thread"))
3307     ;; Update the thread.
3308     (setcar thread header)
3309     (gnus-summary-goto-subject article)
3310     (let* ((datal (gnus-data-find-list article))
3311            (data (car datal))
3312            (length (when (cdr datal)
3313                      (- (gnus-data-pos data)
3314                         (gnus-data-pos (cadr datal)))))
3315            (buffer-read-only nil)
3316            (level (gnus-summary-thread-level)))
3317       (gnus-delete-line)
3318       (gnus-summary-insert-line
3319        header level nil (gnus-article-mark article)
3320        (memq article gnus-newsgroup-replied)
3321        (memq article gnus-newsgroup-expirable)
3322        ;; Only insert the Subject string when it's different
3323        ;; from the previous Subject string.
3324        (if (gnus-subject-equal
3325             (condition-case ()
3326                 (mail-header-subject
3327                  (gnus-data-header
3328                   (cadr
3329                    (gnus-data-find-list
3330                     article
3331                     (gnus-data-list t)))))
3332               ;; Error on the side of excessive subjects.
3333               (error ""))
3334             (mail-header-subject header))
3335            ""
3336          (mail-header-subject header))
3337        nil (cdr (assq article gnus-newsgroup-scored))
3338        (memq article gnus-newsgroup-processable))
3339       (when length
3340         (gnus-data-update-list
3341          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3342
3343 (defun gnus-summary-update-article (article &optional iheader)
3344   "Update ARTICLE in the summary buffer."
3345   (set-buffer gnus-summary-buffer)
3346   (let* ((header (gnus-summary-article-header article))
3347          (id (mail-header-id header))
3348          (data (gnus-data-find article))
3349          (thread (gnus-id-to-thread id))
3350          (references (mail-header-references header))
3351          (parent
3352           (gnus-id-to-thread
3353            (or (gnus-parent-id
3354                 (when (and references
3355                            (not (equal "" references)))
3356                   references))
3357                "none")))
3358          (buffer-read-only nil)
3359          (old (car thread)))
3360     (when thread
3361       (unless iheader
3362         (setcar thread nil)
3363         (when parent
3364           (delq thread parent)))
3365       (if (gnus-summary-insert-subject id header)
3366           ;; Set the (possibly) new article number in the data structure.
3367           (gnus-data-set-number data (gnus-id-to-article id))
3368         (setcar thread old)
3369         nil))))
3370
3371 (defun gnus-rebuild-thread (id &optional line)
3372   "Rebuild the thread containing ID.
3373 If LINE, insert the rebuilt thread starting on line LINE."
3374   (let ((buffer-read-only nil)
3375         old-pos current thread data)
3376     (if (not gnus-show-threads)
3377         (setq thread (list (car (gnus-id-to-thread id))))
3378       ;; Get the thread this article is part of.
3379       (setq thread (gnus-remove-thread id)))
3380     (setq old-pos (gnus-point-at-bol))
3381     (setq current (save-excursion
3382                     (and (zerop (forward-line -1))
3383                          (gnus-summary-article-number))))
3384     ;; If this is a gathered thread, we have to go some re-gathering.
3385     (when (stringp (car thread))
3386       (let ((subject (car thread))
3387             roots thr)
3388         (setq thread (cdr thread))
3389         (while thread
3390           (unless (memq (setq thr (gnus-id-to-thread
3391                                    (gnus-root-id
3392                                     (mail-header-id (caar thread)))))
3393                         roots)
3394             (push thr roots))
3395           (setq thread (cdr thread)))
3396         ;; We now have all (unique) roots.
3397         (if (= (length roots) 1)
3398             ;; All the loose roots are now one solid root.
3399             (setq thread (car roots))
3400           (setq thread (cons subject (gnus-sort-threads roots))))))
3401     (let (threads)
3402       ;; We then insert this thread into the summary buffer.
3403       (when line
3404         (goto-char (point-min))
3405         (forward-line (1- line)))
3406       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3407         (if gnus-show-threads
3408             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3409           (gnus-summary-prepare-unthreaded thread))
3410         (setq data (nreverse gnus-newsgroup-data))
3411         (setq threads gnus-newsgroup-threads))
3412       ;; We splice the new data into the data structure.
3413       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3414       ;;!!! then we want to insert at the beginning of the buffer.
3415       ;;!!! That happens to be true with Gnus now, but that may
3416       ;;!!! change in the future.  Perhaps.
3417       (gnus-data-enter-list
3418        (if line nil current) data (- (point) old-pos))
3419       (setq gnus-newsgroup-threads
3420             (nconc threads gnus-newsgroup-threads))
3421       (gnus-data-compute-positions))))
3422
3423 (defun gnus-number-to-header (number)
3424   "Return the header for article NUMBER."
3425   (let ((headers gnus-newsgroup-headers))
3426     (while (and headers
3427                 (not (= number (mail-header-number (car headers)))))
3428       (pop headers))
3429     (when headers
3430       (car headers))))
3431
3432 (defun gnus-parent-headers (in-headers &optional generation)
3433   "Return the headers of the GENERATIONeth parent of HEADERS."
3434   (unless generation
3435     (setq generation 1))
3436   (let ((parent t)
3437         (headers in-headers)
3438         references)
3439     (while (and parent
3440                 (not (zerop generation))
3441                 (setq references (mail-header-references headers)))
3442       (setq headers (if (and references
3443                              (setq parent (gnus-parent-id references)))
3444                         (car (gnus-id-to-thread parent))
3445                       nil))
3446       (decf generation))
3447     (and (not (eq headers in-headers))
3448          headers)))
3449
3450 (defun gnus-id-to-thread (id)
3451   "Return the (sub-)thread where ID appears."
3452   (gnus-gethash id gnus-newsgroup-dependencies))
3453
3454 (defun gnus-id-to-article (id)
3455   "Return the article number of ID."
3456   (let ((thread (gnus-id-to-thread id)))
3457     (when (and thread
3458                (car thread))
3459       (mail-header-number (car thread)))))
3460
3461 (defun gnus-id-to-header (id)
3462   "Return the article headers of ID."
3463   (car (gnus-id-to-thread id)))
3464
3465 (defun gnus-article-displayed-root-p (article)
3466   "Say whether ARTICLE is a root(ish) article."
3467   (let ((level (gnus-summary-thread-level article))
3468         (refs (mail-header-references  (gnus-summary-article-header article)))
3469         particle)
3470     (cond
3471      ((null level) nil)
3472      ((zerop level) t)
3473      ((null refs) t)
3474      ((null (gnus-parent-id refs)) t)
3475      ((and (= 1 level)
3476            (null (setq particle (gnus-id-to-article
3477                                  (gnus-parent-id refs))))
3478            (null (gnus-summary-thread-level particle)))))))
3479
3480 (defun gnus-root-id (id)
3481   "Return the id of the root of the thread where ID appears."
3482   (let (last-id prev)
3483     (while (and id (setq prev (car (gnus-id-to-thread id))))
3484       (setq last-id id
3485             id (gnus-parent-id (mail-header-references prev))))
3486     last-id))
3487
3488 (defun gnus-articles-in-thread (thread)
3489   "Return the list of articles in THREAD."
3490   (cons (mail-header-number (car thread))
3491         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3492
3493 (defun gnus-remove-thread (id &optional dont-remove)
3494   "Remove the thread that has ID in it."
3495   (let (headers thread last-id)
3496     ;; First go up in this thread until we find the root.
3497     (setq last-id (gnus-root-id id)
3498           headers (message-flatten-list (gnus-id-to-thread last-id)))
3499     ;; We have now found the real root of this thread.  It might have
3500     ;; been gathered into some loose thread, so we have to search
3501     ;; through the threads to find the thread we wanted.
3502     (let ((threads gnus-newsgroup-threads)
3503           sub)
3504       (while threads
3505         (setq sub (car threads))
3506         (if (stringp (car sub))
3507             ;; This is a gathered thread, so we look at the roots
3508             ;; below it to find whether this article is in this
3509             ;; gathered root.
3510             (progn
3511               (setq sub (cdr sub))
3512               (while sub
3513                 (when (member (caar sub) headers)
3514                   (setq thread (car threads)
3515                         threads nil
3516                         sub nil))
3517                 (setq sub (cdr sub))))
3518           ;; It's an ordinary thread, so we check it.
3519           (when (eq (car sub) (car headers))
3520             (setq thread sub
3521                   threads nil)))
3522         (setq threads (cdr threads)))
3523       ;; If this article is in no thread, then it's a root.
3524       (if thread
3525           (unless dont-remove
3526             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3527         (setq thread (gnus-id-to-thread last-id)))
3528       (when thread
3529         (prog1
3530             thread                      ; We return this thread.
3531           (unless dont-remove
3532             (if (stringp (car thread))
3533                 (progn
3534                   ;; If we use dummy roots, then we have to remove the
3535                   ;; dummy root as well.
3536                   (when (eq gnus-summary-make-false-root 'dummy)
3537                     ;; We go to the dummy root by going to
3538                     ;; the first sub-"thread", and then one line up.
3539                     (gnus-summary-goto-article
3540                      (mail-header-number (caadr thread)))
3541                     (forward-line -1)
3542                     (gnus-delete-line)
3543                     (gnus-data-compute-positions))
3544                   (setq thread (cdr thread))
3545                   (while thread
3546                     (gnus-remove-thread-1 (car thread))
3547                     (setq thread (cdr thread))))
3548               (gnus-summary-show-all-threads)
3549               (gnus-remove-thread-1 thread))))))))
3550
3551 (defun gnus-remove-thread-1 (thread)
3552   "Remove the thread THREAD recursively."
3553   (let ((number (mail-header-number (pop thread)))
3554         d)
3555     (setq thread (reverse thread))
3556     (while thread
3557       (gnus-remove-thread-1 (pop thread)))
3558     (when (setq d (gnus-data-find number))
3559       (goto-char (gnus-data-pos d))
3560       (gnus-data-remove
3561        number
3562        (- (gnus-point-at-bol)
3563           (prog1
3564               (1+ (gnus-point-at-eol))
3565             (gnus-delete-line)))))))
3566
3567 (defun gnus-sort-threads (threads)
3568   "Sort THREADS."
3569   (if (not gnus-thread-sort-functions)
3570       threads
3571     (gnus-message 8 "Sorting threads...")
3572     (prog1
3573         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3574       (gnus-message 8 "Sorting threads...done"))))
3575
3576 (defun gnus-sort-articles (articles)
3577   "Sort ARTICLES."
3578   (when gnus-article-sort-functions
3579     (gnus-message 7 "Sorting articles...")
3580     (prog1
3581         (setq gnus-newsgroup-headers
3582               (sort articles (gnus-make-sort-function
3583                               gnus-article-sort-functions)))
3584       (gnus-message 7 "Sorting articles...done"))))
3585
3586 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3587 (defmacro gnus-thread-header (thread)
3588   ;; Return header of first article in THREAD.
3589   ;; Note that THREAD must never, ever be anything else than a variable -
3590   ;; using some other form will lead to serious barfage.
3591   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3592   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3593   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3594         (vector thread) 2))
3595
3596 (defsubst gnus-article-sort-by-number (h1 h2)
3597   "Sort articles by article number."
3598   (< (mail-header-number h1)
3599      (mail-header-number h2)))
3600
3601 (defun gnus-thread-sort-by-number (h1 h2)
3602   "Sort threads by root article number."
3603   (gnus-article-sort-by-number
3604    (gnus-thread-header h1) (gnus-thread-header h2)))
3605
3606 (defsubst gnus-article-sort-by-lines (h1 h2)
3607   "Sort articles by article Lines header."
3608   (< (mail-header-lines h1)
3609      (mail-header-lines h2)))
3610
3611 (defun gnus-thread-sort-by-lines (h1 h2)
3612   "Sort threads by root article Lines header."
3613   (gnus-article-sort-by-lines
3614    (gnus-thread-header h1) (gnus-thread-header h2)))
3615
3616 (defsubst gnus-article-sort-by-author (h1 h2)
3617   "Sort articles by root author."
3618   (string-lessp
3619    (let ((extract (funcall
3620                    gnus-extract-address-components
3621                    (mail-header-from h1))))
3622      (or (car extract) (cadr extract) ""))
3623    (let ((extract (funcall
3624                    gnus-extract-address-components
3625                    (mail-header-from h2))))
3626      (or (car extract) (cadr extract) ""))))
3627
3628 (defun gnus-thread-sort-by-author (h1 h2)
3629   "Sort threads by root author."
3630   (gnus-article-sort-by-author
3631    (gnus-thread-header h1)  (gnus-thread-header h2)))
3632
3633 (defsubst gnus-article-sort-by-subject (h1 h2)
3634   "Sort articles by root subject."
3635   (string-lessp
3636    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3637    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3638
3639 (defun gnus-thread-sort-by-subject (h1 h2)
3640   "Sort threads by root subject."
3641   (gnus-article-sort-by-subject
3642    (gnus-thread-header h1) (gnus-thread-header h2)))
3643
3644 (defsubst gnus-article-sort-by-date (h1 h2)
3645   "Sort articles by root article date."
3646   (time-less-p
3647    (gnus-date-get-time (mail-header-date h1))
3648    (gnus-date-get-time (mail-header-date h2))))
3649
3650 (defun gnus-thread-sort-by-date (h1 h2)
3651   "Sort threads by root article date."
3652   (gnus-article-sort-by-date
3653    (gnus-thread-header h1) (gnus-thread-header h2)))
3654
3655 (defsubst gnus-article-sort-by-score (h1 h2)
3656   "Sort articles by root article score.
3657 Unscored articles will be counted as having a score of zero."
3658   (> (or (cdr (assq (mail-header-number h1)
3659                     gnus-newsgroup-scored))
3660          gnus-summary-default-score 0)
3661      (or (cdr (assq (mail-header-number h2)
3662                     gnus-newsgroup-scored))
3663          gnus-summary-default-score 0)))
3664
3665 (defun gnus-thread-sort-by-score (h1 h2)
3666   "Sort threads by root article score."
3667   (gnus-article-sort-by-score
3668    (gnus-thread-header h1) (gnus-thread-header h2)))
3669
3670 (defun gnus-thread-sort-by-total-score (h1 h2)
3671   "Sort threads by the sum of all scores in the thread.
3672 Unscored articles will be counted as having a score of zero."
3673   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3674
3675 (defun gnus-thread-total-score (thread)
3676   ;;  This function find the total score of THREAD.
3677   (cond ((null thread)
3678          0)
3679         ((consp thread)
3680          (if (stringp (car thread))
3681              (apply gnus-thread-score-function 0
3682                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3683            (gnus-thread-total-score-1 thread)))
3684         (t
3685          (gnus-thread-total-score-1 (list thread)))))
3686
3687 (defun gnus-thread-total-score-1 (root)
3688   ;; This function find the total score of the thread below ROOT.
3689   (setq root (car root))
3690   (apply gnus-thread-score-function
3691          (or (append
3692               (mapcar 'gnus-thread-total-score
3693                       (cdr (gnus-id-to-thread (mail-header-id root))))
3694               (when (> (mail-header-number root) 0)
3695                 (list (or (cdr (assq (mail-header-number root)
3696                                      gnus-newsgroup-scored))
3697                           gnus-summary-default-score 0))))
3698              (list gnus-summary-default-score)
3699              '(0))))
3700
3701 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3702 (defvar gnus-tmp-prev-subject nil)
3703 (defvar gnus-tmp-false-parent nil)
3704 (defvar gnus-tmp-root-expunged nil)
3705 (defvar gnus-tmp-dummy-line nil)
3706
3707 (defvar gnus-tmp-header)
3708 (defun gnus-extra-header (type &optional header)
3709   "Return the extra header of TYPE."
3710   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3711       ""))
3712
3713 (defun gnus-summary-prepare-threads (threads)
3714   "Prepare summary buffer from THREADS and indentation LEVEL.
3715 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3716 or a straight list of headers."
3717   (gnus-message 7 "Generating summary...")
3718
3719   (setq gnus-newsgroup-threads threads)
3720   (beginning-of-line)
3721
3722   (let ((gnus-tmp-level 0)
3723         (default-score (or gnus-summary-default-score 0))
3724         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3725         thread number subject stack state gnus-tmp-gathered beg-match
3726         new-roots gnus-tmp-new-adopts thread-end
3727         gnus-tmp-header gnus-tmp-unread
3728         gnus-tmp-replied gnus-tmp-subject-or-nil
3729         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3730         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3731         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3732
3733     (setq gnus-tmp-prev-subject nil)
3734
3735     (if (vectorp (car threads))
3736         ;; If this is a straight (sic) list of headers, then a
3737         ;; threaded summary display isn't required, so we just create
3738         ;; an unthreaded one.
3739         (gnus-summary-prepare-unthreaded threads)
3740
3741       ;; Do the threaded display.
3742
3743       (while (or threads stack gnus-tmp-new-adopts new-roots)
3744
3745         (if (and (= gnus-tmp-level 0)
3746                  (or (not stack)
3747                      (= (caar stack) 0))
3748                  (not gnus-tmp-false-parent)
3749                  (or gnus-tmp-new-adopts new-roots))
3750             (if gnus-tmp-new-adopts
3751                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3752                       thread (list (car gnus-tmp-new-adopts))
3753                       gnus-tmp-header (caar thread)
3754                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3755               (when new-roots
3756                 (setq thread (list (car new-roots))
3757                       gnus-tmp-header (caar thread)
3758                       new-roots (cdr new-roots))))
3759
3760           (if threads
3761               ;; If there are some threads, we do them before the
3762               ;; threads on the stack.
3763               (setq thread threads
3764                     gnus-tmp-header (caar thread))
3765             ;; There were no current threads, so we pop something off
3766             ;; the stack.
3767             (setq state (car stack)
3768                   gnus-tmp-level (car state)
3769                   thread (cdr state)
3770                   stack (cdr stack)
3771                   gnus-tmp-header (caar thread))))
3772
3773         (setq gnus-tmp-false-parent nil)
3774         (setq gnus-tmp-root-expunged nil)
3775         (setq thread-end nil)
3776
3777         (if (stringp gnus-tmp-header)
3778             ;; The header is a dummy root.
3779             (cond
3780              ((eq gnus-summary-make-false-root 'adopt)
3781               ;; We let the first article adopt the rest.
3782               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3783                                                (cddar thread)))
3784               (setq gnus-tmp-gathered
3785                     (nconc (mapcar
3786                             (lambda (h) (mail-header-number (car h)))
3787                             (cddar thread))
3788                            gnus-tmp-gathered))
3789               (setq thread (cons (list (caar thread)
3790                                        (cadar thread))
3791                                  (cdr thread)))
3792               (setq gnus-tmp-level -1
3793                     gnus-tmp-false-parent t))
3794              ((eq gnus-summary-make-false-root 'empty)
3795               ;; We print adopted articles with empty subject fields.
3796               (setq gnus-tmp-gathered
3797                     (nconc (mapcar
3798                             (lambda (h) (mail-header-number (car h)))
3799                             (cddar thread))
3800                            gnus-tmp-gathered))
3801               (setq gnus-tmp-level -1))
3802              ((eq gnus-summary-make-false-root 'dummy)
3803               ;; We remember that we probably want to output a dummy
3804               ;; root.
3805               (setq gnus-tmp-dummy-line gnus-tmp-header)
3806               (setq gnus-tmp-prev-subject gnus-tmp-header))
3807              (t
3808               ;; We do not make a root for the gathered
3809               ;; sub-threads at all.
3810               (setq gnus-tmp-level -1)))
3811
3812           (setq number (mail-header-number gnus-tmp-header)
3813                 subject (mail-header-subject gnus-tmp-header))
3814
3815           (cond
3816            ;; If the thread has changed subject, we might want to make
3817            ;; this subthread into a root.
3818            ((and (null gnus-thread-ignore-subject)
3819                  (not (zerop gnus-tmp-level))
3820                  gnus-tmp-prev-subject
3821                  (not (inline
3822                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3823             (setq new-roots (nconc new-roots (list (car thread)))
3824                   thread-end t
3825                   gnus-tmp-header nil))
3826            ;; If the article lies outside the current limit,
3827            ;; then we do not display it.
3828            ((not (memq number gnus-newsgroup-limit))
3829             (setq gnus-tmp-gathered
3830                   (nconc (mapcar
3831                           (lambda (h) (mail-header-number (car h)))
3832                           (cdar thread))
3833                          gnus-tmp-gathered))
3834             (setq gnus-tmp-new-adopts (if (cdar thread)
3835                                           (append gnus-tmp-new-adopts
3836                                                   (cdar thread))
3837                                         gnus-tmp-new-adopts)
3838                   thread-end t
3839                   gnus-tmp-header nil)
3840             (when (zerop gnus-tmp-level)
3841               (setq gnus-tmp-root-expunged t)))
3842            ;; Perhaps this article is to be marked as read?
3843            ((and gnus-summary-mark-below
3844                  (< (or (cdr (assq number gnus-newsgroup-scored))
3845                         default-score)
3846                     gnus-summary-mark-below)
3847                  ;; Don't touch sparse articles.
3848                  (not (gnus-summary-article-sparse-p number))
3849                  (not (gnus-summary-article-ancient-p number)))
3850             (setq gnus-newsgroup-unreads
3851                   (delq number gnus-newsgroup-unreads))
3852             (if gnus-newsgroup-auto-expire
3853                 (push number gnus-newsgroup-expirable)
3854               (push (cons number gnus-low-score-mark)
3855                     gnus-newsgroup-reads))))
3856
3857           (when gnus-tmp-header
3858             ;; We may have an old dummy line to output before this
3859             ;; article.
3860             (when (and gnus-tmp-dummy-line
3861                        (gnus-subject-equal
3862                         gnus-tmp-dummy-line
3863                         (mail-header-subject gnus-tmp-header)))
3864               (gnus-summary-insert-dummy-line
3865                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3866               (setq gnus-tmp-dummy-line nil))
3867
3868             ;; Compute the mark.
3869             (setq gnus-tmp-unread (gnus-article-mark number))
3870
3871             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3872                                   gnus-tmp-header gnus-tmp-level)
3873                   gnus-newsgroup-data)
3874
3875             ;; Actually insert the line.
3876             (setq
3877              gnus-tmp-subject-or-nil
3878              (cond
3879               ((and gnus-thread-ignore-subject
3880                     gnus-tmp-prev-subject
3881                     (not (inline (gnus-subject-equal
3882                                   gnus-tmp-prev-subject subject))))
3883                subject)
3884               ((zerop gnus-tmp-level)
3885                (if (and (eq gnus-summary-make-false-root 'empty)
3886                         (memq number gnus-tmp-gathered)
3887                         gnus-tmp-prev-subject
3888                         (inline (gnus-subject-equal
3889                                  gnus-tmp-prev-subject subject)))
3890                    gnus-summary-same-subject
3891                  subject))
3892               (t gnus-summary-same-subject)))
3893             (if (and (eq gnus-summary-make-false-root 'adopt)
3894                      (= gnus-tmp-level 1)
3895                      (memq number gnus-tmp-gathered))
3896                 (setq gnus-tmp-opening-bracket ?\<
3897                       gnus-tmp-closing-bracket ?\>)
3898               (setq gnus-tmp-opening-bracket ?\[
3899                     gnus-tmp-closing-bracket ?\]))
3900             (setq
3901              gnus-tmp-indentation
3902              (aref gnus-thread-indent-array gnus-tmp-level)
3903              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3904              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3905                                 gnus-summary-default-score 0)
3906              gnus-tmp-score-char
3907              (if (or (null gnus-summary-default-score)
3908                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3909                          gnus-summary-zcore-fuzz))
3910                  ? 
3911                (if (< gnus-tmp-score gnus-summary-default-score)
3912                    gnus-score-below-mark gnus-score-over-mark))
3913              gnus-tmp-replied
3914              (cond ((memq number gnus-newsgroup-processable)
3915                     gnus-process-mark)
3916                    ((memq number gnus-newsgroup-cached)
3917                     gnus-cached-mark)
3918                    ((memq number gnus-newsgroup-replied)
3919                     gnus-replied-mark)
3920                    ((memq number gnus-newsgroup-saved)
3921                     gnus-saved-mark)
3922                    (t gnus-unread-mark))
3923              gnus-tmp-from (mail-header-from gnus-tmp-header)
3924              gnus-tmp-name
3925              (cond
3926               ((string-match "<[^>]+> *$" gnus-tmp-from)
3927                (setq beg-match (match-beginning 0))
3928                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3929                         (substring gnus-tmp-from (1+ (match-beginning 0))
3930                                    (1- (match-end 0))))
3931                    (substring gnus-tmp-from 0 beg-match)))
3932               ((string-match "(.+)" gnus-tmp-from)
3933                (substring gnus-tmp-from
3934                           (1+ (match-beginning 0)) (1- (match-end 0))))
3935               (t gnus-tmp-from)))
3936             (when (string= gnus-tmp-name "")
3937               (setq gnus-tmp-name gnus-tmp-from))
3938             (unless (numberp gnus-tmp-lines)
3939               (setq gnus-tmp-lines 0))
3940             (gnus-put-text-property
3941              (point)
3942              (progn (eval gnus-summary-line-format-spec) (point))
3943              'gnus-number number)
3944             (when gnus-visual-p
3945               (forward-line -1)
3946               (gnus-run-hooks 'gnus-summary-update-hook)
3947               (forward-line 1))
3948
3949             (setq gnus-tmp-prev-subject subject)))
3950
3951         (when (nth 1 thread)
3952           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3953         (incf gnus-tmp-level)
3954         (setq threads (if thread-end nil (cdar thread)))
3955         (unless threads
3956           (setq gnus-tmp-level 0)))))
3957   (gnus-message 7 "Generating summary...done"))
3958
3959 (defun gnus-summary-prepare-unthreaded (headers)
3960   "Generate an unthreaded summary buffer based on HEADERS."
3961   (let (header number mark)
3962
3963     (beginning-of-line)
3964
3965     (while headers
3966       ;; We may have to root out some bad articles...
3967       (when (memq (setq number (mail-header-number
3968                                 (setq header (pop headers))))
3969                   gnus-newsgroup-limit)
3970         ;; Mark article as read when it has a low score.
3971         (when (and gnus-summary-mark-below
3972                    (< (or (cdr (assq number gnus-newsgroup-scored))
3973                           gnus-summary-default-score 0)
3974                       gnus-summary-mark-below)
3975                    (not (gnus-summary-article-ancient-p number)))
3976           (setq gnus-newsgroup-unreads
3977                 (delq number gnus-newsgroup-unreads))
3978           (if gnus-newsgroup-auto-expire
3979               (push number gnus-newsgroup-expirable)
3980             (push (cons number gnus-low-score-mark)
3981                   gnus-newsgroup-reads)))
3982
3983         (setq mark (gnus-article-mark number))
3984         (push (gnus-data-make number mark (1+ (point)) header 0)
3985               gnus-newsgroup-data)
3986         (gnus-summary-insert-line
3987          header 0 number
3988          mark (memq number gnus-newsgroup-replied)
3989          (memq number gnus-newsgroup-expirable)
3990          (mail-header-subject header) nil
3991          (cdr (assq number gnus-newsgroup-scored))
3992          (memq number gnus-newsgroup-processable))))))
3993
3994 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3995   "Select newsgroup GROUP.
3996 If READ-ALL is non-nil, all articles in the group are selected.
3997 If SELECT-ARTICLES, only select those articles from GROUP."
3998   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3999          ;;!!! Dirty hack; should be removed.
4000          (gnus-summary-ignore-duplicates
4001           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4002               t
4003             gnus-summary-ignore-duplicates))
4004          (info (nth 2 entry))
4005          articles fetched-articles cached)
4006
4007     (unless (gnus-check-server
4008              (setq gnus-current-select-method
4009                    (gnus-find-method-for-group group)))
4010       (error "Couldn't open server"))
4011
4012     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4013         (gnus-activate-group group)     ; Or we can activate it...
4014         (progn                          ; Or we bug out.
4015           (when (equal major-mode 'gnus-summary-mode)
4016             (kill-buffer (current-buffer)))
4017           (error "Couldn't request group %s: %s"
4018                  group (gnus-status-message group))))
4019
4020     (unless (gnus-request-group group t)
4021       (when (equal major-mode 'gnus-summary-mode)
4022         (kill-buffer (current-buffer)))
4023       (error "Couldn't request group %s: %s"
4024              group (gnus-status-message group)))
4025
4026     (setq gnus-newsgroup-name group)
4027     (setq gnus-newsgroup-unselected nil)
4028     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4029     (gnus-newsgroup-setup-default-charset)
4030
4031     ;; Adjust and set lists of article marks.
4032     (when info
4033       (gnus-adjust-marked-articles info))
4034
4035     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4036     (when (gnus-virtual-group-p group)
4037       (setq cached gnus-newsgroup-cached))
4038
4039     (setq gnus-newsgroup-unreads
4040           (gnus-set-difference
4041            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4042            gnus-newsgroup-dormant))
4043
4044     (setq gnus-newsgroup-processable nil)
4045
4046     (gnus-update-read-articles group gnus-newsgroup-unreads)
4047
4048     (if (setq articles select-articles)
4049         (setq gnus-newsgroup-unselected
4050               (gnus-sorted-intersection
4051                gnus-newsgroup-unreads
4052                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4053       (setq articles (gnus-articles-to-read group read-all)))
4054
4055     (cond
4056      ((null articles)
4057       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4058       'quit)
4059      ((eq articles 0) nil)
4060      (t
4061       ;; Init the dependencies hash table.
4062       (setq gnus-newsgroup-dependencies
4063             (gnus-make-hashtable (length articles)))
4064       (gnus-set-global-variables)
4065       ;; Retrieve the headers and read them in.
4066       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4067       (setq gnus-newsgroup-headers
4068             (if (eq 'nov
4069                     (setq gnus-headers-retrieved-by
4070                           (gnus-retrieve-headers
4071                            articles gnus-newsgroup-name
4072                            ;; We might want to fetch old headers, but
4073                            ;; not if there is only 1 article.
4074                            (and (or (and
4075                                      (not (eq gnus-fetch-old-headers 'some))
4076                                      (not (numberp gnus-fetch-old-headers)))
4077                                     (> (length articles) 1))
4078                                 gnus-fetch-old-headers))))
4079                 (gnus-get-newsgroup-headers-xover
4080                  articles nil nil gnus-newsgroup-name t)
4081               (gnus-get-newsgroup-headers)))
4082       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4083
4084       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4085       (when cached
4086         (setq gnus-newsgroup-cached cached))
4087
4088       ;; Suppress duplicates?
4089       (when gnus-suppress-duplicates
4090         (gnus-dup-suppress-articles))
4091
4092       ;; Set the initial limit.
4093       (setq gnus-newsgroup-limit (copy-sequence articles))
4094       ;; Remove canceled articles from the list of unread articles.
4095       (setq gnus-newsgroup-unreads
4096             (gnus-set-sorted-intersection
4097              gnus-newsgroup-unreads
4098              (setq fetched-articles
4099                    (mapcar (lambda (headers) (mail-header-number headers))
4100                            gnus-newsgroup-headers))))
4101       ;; Removed marked articles that do not exist.
4102       (gnus-update-missing-marks
4103        (gnus-sorted-complement fetched-articles articles))
4104       ;; We might want to build some more threads first.
4105       (when (and gnus-fetch-old-headers
4106                  (eq gnus-headers-retrieved-by 'nov))
4107         (if (eq gnus-fetch-old-headers 'invisible)
4108             (gnus-build-all-threads)
4109           (gnus-build-old-threads)))
4110       ;; Let the Gnus agent mark articles as read.
4111       (when gnus-agent
4112         (gnus-agent-get-undownloaded-list))
4113       ;; Check whether auto-expire is to be done in this group.
4114       (setq gnus-newsgroup-auto-expire
4115             (gnus-group-auto-expirable-p group))
4116       ;; Set up the article buffer now, if necessary.
4117       (unless gnus-single-article-buffer
4118         (gnus-article-setup-buffer))
4119       ;; First and last article in this newsgroup.
4120       (when gnus-newsgroup-headers
4121         (setq gnus-newsgroup-begin
4122               (mail-header-number (car gnus-newsgroup-headers))
4123               gnus-newsgroup-end
4124               (mail-header-number
4125                (gnus-last-element gnus-newsgroup-headers))))
4126       ;; GROUP is successfully selected.
4127       (or gnus-newsgroup-headers t)))))
4128
4129 (defun gnus-articles-to-read (group &optional read-all)
4130   ;; Find out what articles the user wants to read.
4131   (let* ((articles
4132           ;; Select all articles if `read-all' is non-nil, or if there
4133           ;; are no unread articles.
4134           (if (or read-all
4135                   (and (zerop (length gnus-newsgroup-marked))
4136                        (zerop (length gnus-newsgroup-unreads)))
4137                   (eq (gnus-group-find-parameter group 'display)
4138                       'all))
4139               (gnus-uncompress-range (gnus-active group))
4140             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4141                           (copy-sequence gnus-newsgroup-unreads))
4142                   '<)))
4143          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4144          (scored (length scored-list))
4145          (number (length articles))
4146          (marked (+ (length gnus-newsgroup-marked)
4147                     (length gnus-newsgroup-dormant)))
4148          (select
4149           (cond
4150            ((numberp read-all)
4151             read-all)
4152            (t
4153             (condition-case ()
4154                 (cond
4155                  ((and (or (<= scored marked) (= scored number))
4156                        (numberp gnus-large-newsgroup)
4157                        (> number gnus-large-newsgroup))
4158                   (let ((input
4159                          (read-string
4160                           (format
4161                            "How many articles from %s (default %d): "
4162                            (gnus-limit-string gnus-newsgroup-name 35)
4163                            number))))
4164                     (if (string-match "^[ \t]*$" input) number input)))
4165                  ((and (> scored marked) (< scored number)
4166                        (> (- scored number) 20))
4167                   (let ((input
4168                          (read-string
4169                           (format "%s %s (%d scored, %d total): "
4170                                   "How many articles from"
4171                                   group scored number))))
4172                     (if (string-match "^[ \t]*$" input)
4173                         number input)))
4174                  (t number))
4175               (quit nil))))))
4176     (setq select (if (stringp select) (string-to-number select) select))
4177     (if (or (null select) (zerop select))
4178         select
4179       (if (and (not (zerop scored)) (<= (abs select) scored))
4180           (progn
4181             (setq articles (sort scored-list '<))
4182             (setq number (length articles)))
4183         (setq articles (copy-sequence articles)))
4184
4185       (when (< (abs select) number)
4186         (if (< select 0)
4187             ;; Select the N oldest articles.
4188             (setcdr (nthcdr (1- (abs select)) articles) nil)
4189           ;; Select the N most recent articles.
4190           (setq articles (nthcdr (- number select) articles))))
4191       (setq gnus-newsgroup-unselected
4192             (gnus-sorted-intersection
4193              gnus-newsgroup-unreads
4194              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4195       articles)))
4196
4197 (defun gnus-killed-articles (killed articles)
4198   (let (out)
4199     (while articles
4200       (when (inline (gnus-member-of-range (car articles) killed))
4201         (push (car articles) out))
4202       (setq articles (cdr articles)))
4203     out))
4204
4205 (defun gnus-uncompress-marks (marks)
4206   "Uncompress the mark ranges in MARKS."
4207   (let ((uncompressed '(score bookmark))
4208         out)
4209     (while marks
4210       (if (memq (caar marks) uncompressed)
4211           (push (car marks) out)
4212         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4213       (setq marks (cdr marks)))
4214     out))
4215
4216 (defun gnus-adjust-marked-articles (info)
4217   "Set all article lists and remove all marks that are no longer legal."
4218   (let* ((marked-lists (gnus-info-marks info))
4219          (active (gnus-active (gnus-info-group info)))
4220          (min (car active))
4221          (max (cdr active))
4222          (types gnus-article-mark-lists)
4223          (uncompressed '(score bookmark killed))
4224          marks var articles article mark)
4225
4226     (while marked-lists
4227       (setq marks (pop marked-lists))
4228       (set (setq var (intern (format "gnus-newsgroup-%s"
4229                                      (car (rassq (setq mark (car marks))
4230                                                  types)))))
4231            (if (memq (car marks) uncompressed) (cdr marks)
4232              (gnus-uncompress-range (cdr marks))))
4233
4234       (setq articles (symbol-value var))
4235
4236       ;; All articles have to be subsets of the active articles.
4237       (cond
4238        ;; Adjust "simple" lists.
4239        ((memq mark '(tick dormant expire reply save))
4240         (while articles
4241           (when (or (< (setq article (pop articles)) min) (> article max))
4242             (set var (delq article (symbol-value var))))))
4243        ;; Adjust assocs.
4244        ((memq mark uncompressed)
4245         (when (not (listp (cdr (symbol-value var))))
4246           (set var (list (symbol-value var))))
4247         (when (not (listp (cdr articles)))
4248           (setq articles (list articles)))
4249         (while articles
4250           (when (or (not (consp (setq article (pop articles))))
4251                     (< (car article) min)
4252                     (> (car article) max))
4253             (set var (delq article (symbol-value var))))))))))
4254
4255 (defun gnus-update-missing-marks (missing)
4256   "Go through the list of MISSING articles and remove them from the mark lists."
4257   (when missing
4258     (let ((types gnus-article-mark-lists)
4259           var m)
4260       ;; Go through all types.
4261       (while types
4262         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4263         (when (symbol-value var)
4264           ;; This list has articles.  So we delete all missing articles
4265           ;; from it.
4266           (setq m missing)
4267           (while m
4268             (set var (delq (pop m) (symbol-value var)))))))))
4269
4270 (defun gnus-update-marks ()
4271   "Enter the various lists of marked articles into the newsgroup info list."
4272   (let ((types gnus-article-mark-lists)
4273         (info (gnus-get-info gnus-newsgroup-name))
4274         (uncompressed '(score bookmark killed))
4275         type list newmarked symbol delta-marks)
4276     (when info
4277       ;; Add all marks lists that are non-nil to the list of marks lists.
4278       (while (setq type (pop types))
4279         (when (setq list (symbol-value
4280                           (setq symbol
4281                                 (intern (format "gnus-newsgroup-%s"
4282                                                 (car type))))))
4283
4284           ;; Get rid of the entries of the articles that have the
4285           ;; default score.
4286           (when (and (eq (cdr type) 'score)
4287                      gnus-save-score
4288                      list)
4289             (let* ((arts list)
4290                    (prev (cons nil list))
4291                    (all prev))
4292               (while arts
4293                 (if (or (not (consp (car arts)))
4294                         (= (cdar arts) gnus-summary-default-score))
4295                     (setcdr prev (cdr arts))
4296                   (setq prev arts))
4297                 (setq arts (cdr arts)))
4298               (setq list (cdr all))))
4299
4300           (push (cons (cdr type)
4301                       (if (memq (cdr type) uncompressed) list
4302                         (gnus-compress-sequence
4303                          (set symbol (sort list '<)) t)))
4304                 newmarked)))
4305
4306       ;; Enter these new marks into the info of the group.
4307       (if (nthcdr 3 info)
4308           (setcar (nthcdr 3 info) newmarked)
4309         ;; Add the marks lists to the end of the info.
4310         (when newmarked
4311           (setcdr (nthcdr 2 info) (list newmarked))))
4312
4313       ;; Cut off the end of the info if there's nothing else there.
4314       (let ((i 5))
4315         (while (and (> i 2)
4316                     (not (nth i info)))
4317           (when (nthcdr (decf i) info)
4318             (setcdr (nthcdr i info) nil)))))))
4319
4320 (defun gnus-set-mode-line (where)
4321   "This function sets the mode line of the article or summary buffers.
4322 If WHERE is `summary', the summary mode line format will be used."
4323   ;; Is this mode line one we keep updated?
4324   (when (and (memq where gnus-updated-mode-lines)
4325              (symbol-value
4326               (intern (format "gnus-%s-mode-line-format-spec" where))))
4327     (let (mode-string)
4328       (save-excursion
4329         ;; We evaluate this in the summary buffer since these
4330         ;; variables are buffer-local to that buffer.
4331         (set-buffer gnus-summary-buffer)
4332         ;; We bind all these variables that are used in the `eval' form
4333         ;; below.
4334         (let* ((mformat (symbol-value
4335                          (intern
4336                           (format "gnus-%s-mode-line-format-spec" where))))
4337                (gnus-tmp-group-name gnus-newsgroup-name)
4338                (gnus-tmp-article-number (or gnus-current-article 0))
4339                (gnus-tmp-unread gnus-newsgroup-unreads)
4340                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4341                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4342                (gnus-tmp-unread-and-unselected
4343                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4344                             (zerop gnus-tmp-unselected))
4345                        "")
4346                       ((zerop gnus-tmp-unselected)
4347                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4348                       (t (format "{%d(+%d) more}"
4349                                  gnus-tmp-unread-and-unticked
4350                                  gnus-tmp-unselected))))
4351                (gnus-tmp-subject
4352                 (if (and gnus-current-headers
4353                          (vectorp gnus-current-headers))
4354                     (gnus-mode-string-quote
4355                      (mail-header-subject gnus-current-headers))
4356                   ""))
4357                bufname-length max-len
4358                gnus-tmp-header);; passed as argument to any user-format-funcs
4359           (setq mode-string (eval mformat))
4360           (setq bufname-length (if (string-match "%b" mode-string)
4361                                    (- (length
4362                                        (buffer-name
4363                                         (if (eq where 'summary)
4364                                             nil
4365                                           (get-buffer gnus-article-buffer))))
4366                                       2)
4367                                  0))
4368           (setq max-len (max 4 (if gnus-mode-non-string-length
4369                                    (- (window-width)
4370                                       gnus-mode-non-string-length
4371                                       bufname-length)
4372                                  (length mode-string))))
4373           ;; We might have to chop a bit of the string off...
4374           (when (> (length mode-string) max-len)
4375             (setq mode-string
4376                   (concat (truncate-string-to-width mode-string (- max-len 3))
4377                           "...")))
4378           ;; Pad the mode string a bit.
4379           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4380       ;; Update the mode line.
4381       (setq mode-line-buffer-identification
4382             (gnus-mode-line-buffer-identification (list mode-string)))
4383       (set-buffer-modified-p t))))
4384
4385 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4386   "Go through the HEADERS list and add all Xrefs to a hash table.
4387 The resulting hash table is returned, or nil if no Xrefs were found."
4388   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4389          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4390          (xref-hashtb (gnus-make-hashtable))
4391          start group entry number xrefs header)
4392     (while headers
4393       (setq header (pop headers))
4394       (when (and (setq xrefs (mail-header-xref header))
4395                  (not (memq (setq number (mail-header-number header))
4396                             unreads)))
4397         (setq start 0)
4398         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4399           (setq start (match-end 0))
4400           (setq group (if prefix
4401                           (concat prefix (substring xrefs (match-beginning 1)
4402                                                     (match-end 1)))
4403                         (substring xrefs (match-beginning 1) (match-end 1))))
4404           (setq number
4405                 (string-to-int (substring xrefs (match-beginning 2)
4406                                           (match-end 2))))
4407           (if (setq entry (gnus-gethash group xref-hashtb))
4408               (setcdr entry (cons number (cdr entry)))
4409             (gnus-sethash group (cons number nil) xref-hashtb)))))
4410     (and start xref-hashtb)))
4411
4412 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4413   "Look through all the headers and mark the Xrefs as read."
4414   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4415         name entry info xref-hashtb idlist method nth4)
4416     (save-excursion
4417       (set-buffer gnus-group-buffer)
4418       (when (setq xref-hashtb
4419                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4420         (mapatoms
4421          (lambda (group)
4422            (unless (string= from-newsgroup (setq name (symbol-name group)))
4423              (setq idlist (symbol-value group))
4424              ;; Dead groups are not updated.
4425              (and (prog1
4426                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4427                             info (nth 2 entry))
4428                     (when (stringp (setq nth4 (gnus-info-method info)))
4429                       (setq nth4 (gnus-server-to-method nth4))))
4430                   ;; Only do the xrefs if the group has the same
4431                   ;; select method as the group we have just read.
4432                   (or (gnus-methods-equal-p
4433                        nth4 (gnus-find-method-for-group from-newsgroup))
4434                       virtual
4435                       (equal nth4 (setq method (gnus-find-method-for-group
4436                                                 from-newsgroup)))
4437                       (and (equal (car nth4) (car method))
4438                            (equal (nth 1 nth4) (nth 1 method))))
4439                   gnus-use-cross-reference
4440                   (or (not (eq gnus-use-cross-reference t))
4441                       virtual
4442                       ;; Only do cross-references on subscribed
4443                       ;; groups, if that is what is wanted.
4444                       (<= (gnus-info-level info) gnus-level-subscribed))
4445                   (gnus-group-make-articles-read name idlist))))
4446          xref-hashtb)))))
4447
4448 (defun gnus-compute-read-articles (group articles)
4449   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4450          (info (nth 2 entry))
4451          (active (gnus-active group))
4452          ninfo)
4453     (when entry
4454       ;; First peel off all illegal article numbers.
4455       (when active
4456         (let ((ids articles)
4457               id first)
4458           (while (setq id (pop ids))
4459             (when (and first (> id (cdr active)))
4460               ;; We'll end up in this situation in one particular
4461               ;; obscure situation.  If you re-scan a group and get
4462               ;; a new article that is cross-posted to a different
4463               ;; group that has not been re-scanned, you might get
4464               ;; crossposted article that has a higher number than
4465               ;; Gnus believes possible.  So we re-activate this
4466               ;; group as well.  This might mean doing the
4467               ;; crossposting thingy will *increase* the number
4468               ;; of articles in some groups.  Tsk, tsk.
4469               (setq active (or (gnus-activate-group group) active)))
4470             (when (or (> id (cdr active))
4471                       (< id (car active)))
4472               (setq articles (delq id articles))))))
4473       ;; If the read list is nil, we init it.
4474       (if (and active
4475                (null (gnus-info-read info))
4476                (> (car active) 1))
4477           (setq ninfo (cons 1 (1- (car active))))
4478         (setq ninfo (gnus-info-read info)))
4479       ;; Then we add the read articles to the range.
4480       (gnus-add-to-range
4481        ninfo (setq articles (sort articles '<))))))
4482
4483 (defun gnus-group-make-articles-read (group articles)
4484   "Update the info of GROUP to say that ARTICLES are read."
4485   (let* ((num 0)
4486          (entry (gnus-gethash group gnus-newsrc-hashtb))
4487          (info (nth 2 entry))
4488          (active (gnus-active group))
4489          range)
4490     (when entry
4491       (setq range (gnus-compute-read-articles group articles))
4492       (save-excursion
4493         (set-buffer gnus-group-buffer)
4494         (gnus-undo-register
4495           `(progn
4496              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4497              (gnus-info-set-read ',info ',(gnus-info-read info))
4498              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4499              (gnus-group-update-group ,group t))))
4500       ;; Add the read articles to the range.
4501       (gnus-info-set-read info range)
4502       ;; Then we have to re-compute how many unread
4503       ;; articles there are in this group.
4504       (when active
4505         (cond
4506          ((not range)
4507           (setq num (- (1+ (cdr active)) (car active))))
4508          ((not (listp (cdr range)))
4509           (setq num (- (cdr active) (- (1+ (cdr range))
4510                                        (car range)))))
4511          (t
4512           (while range
4513             (if (numberp (car range))
4514                 (setq num (1+ num))
4515               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4516             (setq range (cdr range)))
4517           (setq num (- (cdr active) num))))
4518         ;; Update the number of unread articles.
4519         (setcar entry num)
4520         ;; Update the group buffer.
4521         (gnus-group-update-group group t)))))
4522
4523 (defun gnus-methods-equal-p (m1 m2)
4524   (let ((m1 (or m1 gnus-select-method))
4525         (m2 (or m2 gnus-select-method)))
4526     (or (equal m1 m2)
4527         (and (eq (car m1) (car m2))
4528              (or (not (memq 'address (assoc (symbol-name (car m1))
4529                                             gnus-valid-select-methods)))
4530                  (equal (nth 1 m1) (nth 1 m2)))))))
4531
4532 (defvar gnus-newsgroup-none-id 0)
4533
4534 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4535   (let ((cur nntp-server-buffer)
4536         (dependencies
4537          (or dependencies
4538              (save-excursion (set-buffer gnus-summary-buffer)
4539                              gnus-newsgroup-dependencies)))
4540         headers id end ref
4541         (rfc2047-default-charset gnus-newsgroup-default-charset)
4542         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced))
4543     (save-excursion
4544       (set-buffer nntp-server-buffer)
4545       ;; Translate all TAB characters into SPACE characters.
4546       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4547       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4548       (gnus-run-hooks 'gnus-parse-headers-hook)
4549       (let ((case-fold-search t)
4550             in-reply-to header p lines chars)
4551         (goto-char (point-min))
4552         ;; Search to the beginning of the next header.  Error messages
4553         ;; do not begin with 2 or 3.
4554         (while (re-search-forward "^[23][0-9]+ " nil t)
4555           (setq id nil
4556                 ref nil)
4557           ;; This implementation of this function, with nine
4558           ;; search-forwards instead of the one re-search-forward and
4559           ;; a case (which basically was the old function) is actually
4560           ;; about twice as fast, even though it looks messier.  You
4561           ;; can't have everything, I guess.  Speed and elegance
4562           ;; doesn't always go hand in hand.
4563           (setq
4564            header
4565            (vector
4566             ;; Number.
4567             (prog1
4568                 (read cur)
4569               (end-of-line)
4570               (setq p (point))
4571               (narrow-to-region (point)
4572                                 (or (and (search-forward "\n.\n" nil t)
4573                                          (- (point) 2))
4574                                     (point))))
4575             ;; Subject.
4576             (progn
4577               (goto-char p)
4578               (if (search-forward "\nsubject: " nil t)
4579                   (funcall gnus-decode-encoded-word-function
4580                            (nnheader-header-value))
4581                 "(none)"))
4582             ;; From.
4583             (progn
4584               (goto-char p)
4585               (if (search-forward "\nfrom: " nil t)
4586                   (funcall gnus-decode-encoded-word-function
4587                            (nnheader-header-value))
4588                 "(nobody)"))
4589             ;; Date.
4590             (progn
4591               (goto-char p)
4592               (if (search-forward "\ndate: " nil t)
4593                   (nnheader-header-value) ""))
4594             ;; Message-ID.
4595             (progn
4596               (goto-char p)
4597               (setq id (if (re-search-forward
4598                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4599                            ;; We do it this way to make sure the Message-ID
4600                            ;; is (somewhat) syntactically valid.
4601                            (buffer-substring (match-beginning 1)
4602                                              (match-end 1))
4603                          ;; If there was no message-id, we just fake one
4604                          ;; to make subsequent routines simpler.
4605                          (nnheader-generate-fake-message-id))))
4606             ;; References.
4607             (progn
4608               (goto-char p)
4609               (if (search-forward "\nreferences: " nil t)
4610                   (progn
4611                     (setq end (point))
4612                     (prog1
4613                         (nnheader-header-value)
4614                       (setq ref
4615                             (buffer-substring
4616                              (progn
4617                                (end-of-line)
4618                                (search-backward ">" end t)
4619                                (1+ (point)))
4620                              (progn
4621                                (search-backward "<" end t)
4622                                (point))))))
4623                 ;; Get the references from the in-reply-to header if there
4624                 ;; were no references and the in-reply-to header looks
4625                 ;; promising.
4626                 (if (and (search-forward "\nin-reply-to: " nil t)
4627                          (setq in-reply-to (nnheader-header-value))
4628                          (string-match "<[^>]+>" in-reply-to))
4629                     (let (ref2)
4630                       (setq ref (substring in-reply-to (match-beginning 0)
4631                                            (match-end 0)))
4632                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4633                         (setq ref2 (substring in-reply-to (match-beginning 0)
4634                                               (match-end 0)))
4635                         (when (> (length ref2) (length ref))
4636                           (setq ref ref2)))
4637                       ref)
4638                   (setq ref nil))))
4639             ;; Chars.
4640             (progn
4641               (goto-char p)
4642               (if (search-forward "\nchars: " nil t)
4643                   (if (numberp (setq chars (ignore-errors (read cur))))
4644                       chars 0)
4645                 0))
4646             ;; Lines.
4647             (progn
4648               (goto-char p)
4649               (if (search-forward "\nlines: " nil t)
4650                   (if (numberp (setq lines (ignore-errors (read cur))))
4651                       lines 0)
4652                 0))
4653             ;; Xref.
4654             (progn
4655               (goto-char p)
4656               (and (search-forward "\nxref: " nil t)
4657                    (nnheader-header-value)))
4658             ;; Extra.
4659             (when gnus-extra-headers
4660               (let ((extra gnus-extra-headers)
4661                     out)
4662                 (while extra
4663                   (goto-char p)
4664                   (when (search-forward
4665                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4666                     (push (cons (car extra) (nnheader-header-value))
4667                           out))
4668                   (pop extra))
4669                 out))))
4670           (when (equal id ref)
4671             (setq ref nil))
4672
4673           (when gnus-alter-header-function
4674             (funcall gnus-alter-header-function header)
4675             (setq id (mail-header-id header)
4676                   ref (gnus-parent-id (mail-header-references header))))
4677
4678           (when (setq header
4679                       (gnus-dependencies-add-header
4680                        header dependencies force-new))
4681             (push header headers))
4682           (goto-char (point-max))
4683           (widen))
4684         (nreverse headers)))))
4685
4686 ;; Goes through the xover lines and returns a list of vectors
4687 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4688                                                   force-new dependencies
4689                                                   group also-fetch-heads)
4690   "Parse the news overview data in the server buffer, and return a
4691 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4692   ;; Get the Xref when the users reads the articles since most/some
4693   ;; NNTP servers do not include Xrefs when using XOVER.
4694   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4695   (let ((rfc2047-default-charset gnus-newsgroup-default-charset)
4696         (mm-charset-iso-8859-1-forced gnus-newsgroup-iso-8859-1-forced)
4697         (cur nntp-server-buffer)
4698         (dependencies (or dependencies gnus-newsgroup-dependencies))
4699         number headers header)
4700     (save-excursion
4701       (set-buffer nntp-server-buffer)
4702       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4703       ;; Allow the user to mangle the headers before parsing them.
4704       (gnus-run-hooks 'gnus-parse-headers-hook)
4705       (goto-char (point-min))
4706       (while (not (eobp))
4707         (condition-case ()
4708             (while (and sequence (not (eobp)))
4709               (setq number (read cur))
4710               (while (and sequence
4711                           (< (car sequence) number))
4712                 (setq sequence (cdr sequence)))
4713               (and sequence
4714                    (eq number (car sequence))
4715                    (progn
4716                      (setq sequence (cdr sequence))
4717                      (setq header (inline
4718                                     (gnus-nov-parse-line
4719                                      number dependencies force-new))))
4720                    (push header headers))
4721               (forward-line 1))
4722           (error
4723            (gnus-error 4 "Strange nov line (%d)"
4724                        (count-lines (point-min) (point)))))
4725         (forward-line 1))
4726       ;; A common bug in inn is that if you have posted an article and
4727       ;; then retrieves the active file, it will answer correctly --
4728       ;; the new article is included.  However, a NOV entry for the
4729       ;; article may not have been generated yet, so this may fail.
4730       ;; We work around this problem by retrieving the last few
4731       ;; headers using HEAD.
4732       (if (or (not also-fetch-heads)
4733               (not sequence))
4734           ;; We (probably) got all the headers.
4735           (nreverse headers)
4736         (let ((gnus-nov-is-evil t))
4737           (nconc
4738            (nreverse headers)
4739            (when (gnus-retrieve-headers sequence group)
4740              (gnus-get-newsgroup-headers))))))))
4741
4742 (defun gnus-article-get-xrefs ()
4743   "Fill in the Xref value in `gnus-current-headers', if necessary.
4744 This is meant to be called in `gnus-article-internal-prepare-hook'."
4745   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4746                                  gnus-current-headers)))
4747     (or (not gnus-use-cross-reference)
4748         (not headers)
4749         (and (mail-header-xref headers)
4750              (not (string= (mail-header-xref headers) "")))
4751         (let ((case-fold-search t)
4752               xref)
4753           (save-restriction
4754             (nnheader-narrow-to-headers)
4755             (goto-char (point-min))
4756             (when (or (and (eq (downcase (char-after)) ?x)
4757                            (looking-at "Xref:"))
4758                       (search-forward "\nXref:" nil t))
4759               (goto-char (1+ (match-end 0)))
4760               (setq xref (buffer-substring (point)
4761                                            (progn (end-of-line) (point))))
4762               (mail-header-set-xref headers xref)))))))
4763
4764 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4765   "Find article ID and insert the summary line for that article.
4766 OLD-HEADER can either be a header or a line number to insert
4767 the subject line on."
4768   (let* ((line (and (numberp old-header) old-header))
4769          (old-header (and (vectorp old-header) old-header))
4770          (header (cond ((and old-header use-old-header)
4771                         old-header)
4772                        ((and (numberp id)
4773                              (gnus-number-to-header id))
4774                         (gnus-number-to-header id))
4775                        (t
4776                         (gnus-read-header id))))
4777          (number (and (numberp id) id))
4778          d)
4779     (when header
4780       ;; Rebuild the thread that this article is part of and go to the
4781       ;; article we have fetched.
4782       (when (and (not gnus-show-threads)
4783                  old-header)
4784         (when (and number
4785                    (setq d (gnus-data-find (mail-header-number old-header))))
4786           (goto-char (gnus-data-pos d))
4787           (gnus-data-remove
4788            number
4789            (- (gnus-point-at-bol)
4790               (prog1
4791                   (1+ (gnus-point-at-eol))
4792                 (gnus-delete-line))))))
4793       (when old-header
4794         (mail-header-set-number header (mail-header-number old-header)))
4795       (setq gnus-newsgroup-sparse
4796             (delq (setq number (mail-header-number header))
4797                   gnus-newsgroup-sparse))
4798       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4799       (push number gnus-newsgroup-limit)
4800       (gnus-rebuild-thread (mail-header-id header) line)
4801       (gnus-summary-goto-subject number nil t))
4802     (when (and (numberp number)
4803                (> number 0))
4804       ;; We have to update the boundaries even if we can't fetch the
4805       ;; article if ID is a number -- so that the next `P' or `N'
4806       ;; command will fetch the previous (or next) article even
4807       ;; if the one we tried to fetch this time has been canceled.
4808       (when (> number gnus-newsgroup-end)
4809         (setq gnus-newsgroup-end number))
4810       (when (< number gnus-newsgroup-begin)
4811         (setq gnus-newsgroup-begin number))
4812       (setq gnus-newsgroup-unselected
4813             (delq number gnus-newsgroup-unselected)))
4814     ;; Report back a success?
4815     (and header (mail-header-number header))))
4816
4817 ;;; Process/prefix in the summary buffer
4818
4819 (defun gnus-summary-work-articles (n)
4820   "Return a list of articles to be worked upon.
4821 The prefix argument, the list of process marked articles, and the
4822 current article will be taken into consideration."
4823   (save-excursion
4824     (set-buffer gnus-summary-buffer)
4825     (cond
4826      (n
4827       ;; A numerical prefix has been given.
4828       (setq n (prefix-numeric-value n))
4829       (let ((backward (< n 0))
4830             (n (abs (prefix-numeric-value n)))
4831             articles article)
4832         (save-excursion
4833           (while
4834               (and (> n 0)
4835                    (push (setq article (gnus-summary-article-number))
4836                          articles)
4837                    (if backward
4838                        (gnus-summary-find-prev nil article)
4839                      (gnus-summary-find-next nil article)))
4840             (decf n)))
4841         (nreverse articles)))
4842      ((and (gnus-region-active-p) (mark))
4843       (message "region active")
4844       ;; Work on the region between point and mark.
4845       (let ((max (max (point) (mark)))
4846             articles article)
4847         (save-excursion
4848           (goto-char (min (min (point) (mark))))
4849           (while
4850               (and
4851                (push (setq article (gnus-summary-article-number)) articles)
4852                (gnus-summary-find-next nil article)
4853                (< (point) max)))
4854           (nreverse articles))))
4855      (gnus-newsgroup-processable
4856       ;; There are process-marked articles present.
4857       ;; Save current state.
4858       (gnus-summary-save-process-mark)
4859       ;; Return the list.
4860       (reverse gnus-newsgroup-processable))
4861      (t
4862       ;; Just return the current article.
4863       (list (gnus-summary-article-number))))))
4864
4865 (defmacro gnus-summary-iterate (arg &rest forms)
4866   "Iterate over the process/prefixed articles and do FORMS.
4867 ARG is the interactive prefix given to the command.  FORMS will be
4868 executed with point over the summary line of the articles."
4869   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4870     `(let ((,articles (gnus-summary-work-articles ,arg)))
4871        (while ,articles
4872          (gnus-summary-goto-subject (car ,articles))
4873          ,@forms))))
4874
4875 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4876 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4877
4878 (defun gnus-summary-save-process-mark ()
4879   "Push the current set of process marked articles on the stack."
4880   (interactive)
4881   (push (copy-sequence gnus-newsgroup-processable)
4882         gnus-newsgroup-process-stack))
4883
4884 (defun gnus-summary-kill-process-mark ()
4885   "Push the current set of process marked articles on the stack and unmark."
4886   (interactive)
4887   (gnus-summary-save-process-mark)
4888   (gnus-summary-unmark-all-processable))
4889
4890 (defun gnus-summary-yank-process-mark ()
4891   "Pop the last process mark state off the stack and restore it."
4892   (interactive)
4893   (unless gnus-newsgroup-process-stack
4894     (error "Empty mark stack"))
4895   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4896
4897 (defun gnus-summary-process-mark-set (set)
4898   "Make SET into the current process marked articles."
4899   (gnus-summary-unmark-all-processable)
4900   (while set
4901     (gnus-summary-set-process-mark (pop set))))
4902
4903 ;;; Searching and stuff
4904
4905 (defun gnus-summary-search-group (&optional backward use-level)
4906   "Search for next unread newsgroup.
4907 If optional argument BACKWARD is non-nil, search backward instead."
4908   (save-excursion
4909     (set-buffer gnus-group-buffer)
4910     (when (gnus-group-search-forward
4911            backward nil (if use-level (gnus-group-group-level) nil))
4912       (gnus-group-group-name))))
4913
4914 (defun gnus-summary-best-group (&optional exclude-group)
4915   "Find the name of the best unread group.
4916 If EXCLUDE-GROUP, do not go to this group."
4917   (save-excursion
4918     (set-buffer gnus-group-buffer)
4919     (save-excursion
4920       (gnus-group-best-unread-group exclude-group))))
4921
4922 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4923   (if backward (gnus-summary-find-prev)
4924     (let* ((dummy (gnus-summary-article-intangible-p))
4925            (article (or article (gnus-summary-article-number)))
4926            (arts (gnus-data-find-list article))
4927            result)
4928       (when (and (not dummy)
4929                  (or (not gnus-summary-check-current)
4930                      (not unread)
4931                      (not (gnus-data-unread-p (car arts)))))
4932         (setq arts (cdr arts)))
4933       (when (setq result
4934                   (if unread
4935                       (progn
4936                         (while arts
4937                           (when (or (and undownloaded
4938                                          (eq gnus-undownloaded-mark
4939                                              (gnus-data-mark (car arts))))
4940                                     (gnus-data-unread-p (car arts)))
4941                             (setq result (car arts)
4942                                   arts nil))
4943                           (setq arts (cdr arts)))
4944                         result)
4945                     (car arts)))
4946         (goto-char (gnus-data-pos result))
4947         (gnus-data-number result)))))
4948
4949 (defun gnus-summary-find-prev (&optional unread article)
4950   (let* ((eobp (eobp))
4951          (article (or article (gnus-summary-article-number)))
4952          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4953          result)
4954     (when (and (not eobp)
4955                (or (not gnus-summary-check-current)
4956                    (not unread)
4957                    (not (gnus-data-unread-p (car arts)))))
4958       (setq arts (cdr arts)))
4959     (when (setq result
4960                 (if unread
4961                     (progn
4962                       (while arts
4963                         (when (gnus-data-unread-p (car arts))
4964                           (setq result (car arts)
4965                                 arts nil))
4966                         (setq arts (cdr arts)))
4967                       result)
4968                   (car arts)))
4969       (goto-char (gnus-data-pos result))
4970       (gnus-data-number result))))
4971
4972 (defun gnus-summary-find-subject (subject &optional unread backward article)
4973   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4974          (article (or article (gnus-summary-article-number)))
4975          (articles (gnus-data-list backward))
4976          (arts (gnus-data-find-list article articles))
4977          result)
4978     (when (or (not gnus-summary-check-current)
4979               (not unread)
4980               (not (gnus-data-unread-p (car arts))))
4981       (setq arts (cdr arts)))
4982     (while arts
4983       (and (or (not unread)
4984                (gnus-data-unread-p (car arts)))
4985            (vectorp (gnus-data-header (car arts)))
4986            (gnus-subject-equal
4987             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4988            (setq result (car arts)
4989                  arts nil))
4990       (setq arts (cdr arts)))
4991     (and result
4992          (goto-char (gnus-data-pos result))
4993          (gnus-data-number result))))
4994
4995 (defun gnus-summary-search-forward (&optional unread subject backward)
4996   "Search forward for an article.
4997 If UNREAD, look for unread articles.  If SUBJECT, look for
4998 articles with that subject.  If BACKWARD, search backward instead."
4999   (cond (subject (gnus-summary-find-subject subject unread backward))
5000         (backward (gnus-summary-find-prev unread))
5001         (t (gnus-summary-find-next unread))))
5002
5003 (defun gnus-recenter (&optional n)
5004   "Center point in window and redisplay frame.
5005 Also do horizontal recentering."
5006   (interactive "P")
5007   (when (and gnus-auto-center-summary
5008              (not (eq gnus-auto-center-summary 'vertical)))
5009     (gnus-horizontal-recenter))
5010   (recenter n))
5011
5012 (defun gnus-summary-recenter ()
5013   "Center point in the summary window.
5014 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5015 displayed, no centering will be performed."
5016   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5017   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5018   (let* ((top (cond ((< (window-height) 4) 0)
5019                     ((< (window-height) 7) 1)
5020                     (t (if (numberp gnus-auto-center-summary)
5021                            gnus-auto-center-summary
5022                          2))))
5023          (height (1- (window-height)))
5024          (bottom (save-excursion (goto-char (point-max))
5025                                  (forward-line (- height))
5026                                  (point)))
5027          (window (get-buffer-window (current-buffer))))
5028     ;; The user has to want it.
5029     (when gnus-auto-center-summary
5030       (when (get-buffer-window gnus-article-buffer)
5031         ;; Only do recentering when the article buffer is displayed,
5032         ;; Set the window start to either `bottom', which is the biggest
5033         ;; possible valid number, or the second line from the top,
5034         ;; whichever is the least.
5035         (set-window-start
5036          window (min bottom (save-excursion
5037                               (forward-line (- top)) (point)))))
5038       ;; Do horizontal recentering while we're at it.
5039       (when (and (get-buffer-window (current-buffer) t)
5040                  (not (eq gnus-auto-center-summary 'vertical)))
5041         (let ((selected (selected-window)))
5042           (select-window (get-buffer-window (current-buffer) t))
5043           (gnus-summary-position-point)
5044           (gnus-horizontal-recenter)
5045           (select-window selected))))))
5046
5047 (defun gnus-summary-jump-to-group (newsgroup)
5048   "Move point to NEWSGROUP in group mode buffer."
5049   ;; Keep update point of group mode buffer if visible.
5050   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5051       (save-window-excursion
5052         ;; Take care of tree window mode.
5053         (when (get-buffer-window gnus-group-buffer)
5054           (pop-to-buffer gnus-group-buffer))
5055         (gnus-group-jump-to-group newsgroup))
5056     (save-excursion
5057       ;; Take care of tree window mode.
5058       (if (get-buffer-window gnus-group-buffer)
5059           (pop-to-buffer gnus-group-buffer)
5060         (set-buffer gnus-group-buffer))
5061       (gnus-group-jump-to-group newsgroup))))
5062
5063 ;; This function returns a list of article numbers based on the
5064 ;; difference between the ranges of read articles in this group and
5065 ;; the range of active articles.
5066 (defun gnus-list-of-unread-articles (group)
5067   (let* ((read (gnus-info-read (gnus-get-info group)))
5068          (active (or (gnus-active group) (gnus-activate-group group)))
5069          (last (cdr active))
5070          first nlast unread)
5071     ;; If none are read, then all are unread.
5072     (if (not read)
5073         (setq first (car active))
5074       ;; If the range of read articles is a single range, then the
5075       ;; first unread article is the article after the last read
5076       ;; article.  Sounds logical, doesn't it?
5077       (if (not (listp (cdr read)))
5078           (setq first (max (car active) (1+ (cdr read))))
5079         ;; `read' is a list of ranges.
5080         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5081                                   (caar read)))
5082                   1)
5083           (setq first (car active)))
5084         (while read
5085           (when first
5086             (while (< first nlast)
5087               (push first unread)
5088               (setq first (1+ first))))
5089           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5090           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5091           (setq read (cdr read)))))
5092     ;; And add the last unread articles.
5093     (while (<= first last)
5094       (push first unread)
5095       (setq first (1+ first)))
5096     ;; Return the list of unread articles.
5097     (delq 0 (nreverse unread))))
5098
5099 (defun gnus-list-of-read-articles (group)
5100   "Return a list of unread, unticked and non-dormant articles."
5101   (let* ((info (gnus-get-info group))
5102          (marked (gnus-info-marks info))
5103          (active (gnus-active group)))
5104     (and info active
5105          (gnus-set-difference
5106           (gnus-sorted-complement
5107            (gnus-uncompress-range active)
5108            (gnus-list-of-unread-articles group))
5109           (append
5110            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5111            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5112
5113 ;; Various summary commands
5114
5115 (defun gnus-summary-select-article-buffer ()
5116   "Reconfigure windows to show article buffer."
5117   (interactive)
5118   (if (not (gnus-buffer-live-p gnus-article-buffer))
5119       (error "There is no article buffer for this summary buffer")
5120     (gnus-configure-windows 'article)
5121     (select-window (get-buffer-window gnus-article-buffer))))
5122
5123 (defun gnus-summary-universal-argument (arg)
5124   "Perform any operation on all articles that are process/prefixed."
5125   (interactive "P")
5126   (let ((articles (gnus-summary-work-articles arg))
5127         func article)
5128     (if (eq
5129          (setq
5130           func
5131           (key-binding
5132            (read-key-sequence
5133             (substitute-command-keys
5134              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5135              ))))
5136          'undefined)
5137         (gnus-error 1 "Undefined key")
5138       (save-excursion
5139         (while articles
5140           (gnus-summary-goto-subject (setq article (pop articles)))
5141           (let (gnus-newsgroup-processable)
5142             (command-execute func))
5143           (gnus-summary-remove-process-mark article)))))
5144   (gnus-summary-position-point))
5145
5146 (defun gnus-summary-toggle-truncation (&optional arg)
5147   "Toggle truncation of summary lines.
5148 With arg, turn line truncation on iff arg is positive."
5149   (interactive "P")
5150   (setq truncate-lines
5151         (if (null arg) (not truncate-lines)
5152           (> (prefix-numeric-value arg) 0)))
5153   (redraw-display))
5154
5155 (defun gnus-summary-reselect-current-group (&optional all rescan)
5156   "Exit and then reselect the current newsgroup.
5157 The prefix argument ALL means to select all articles."
5158   (interactive "P")
5159   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5160     (error "Ephemeral groups can't be reselected"))
5161   (let ((current-subject (gnus-summary-article-number))
5162         (group gnus-newsgroup-name))
5163     (setq gnus-newsgroup-begin nil)
5164     (gnus-summary-exit)
5165     ;; We have to adjust the point of group mode buffer because
5166     ;; point was moved to the next unread newsgroup by exiting.
5167     (gnus-summary-jump-to-group group)
5168     (when rescan
5169       (save-excursion
5170         (gnus-group-get-new-news-this-group 1)))
5171     (gnus-group-read-group all t)
5172     (gnus-summary-goto-subject current-subject nil t)))
5173
5174 (defun gnus-summary-rescan-group (&optional all)
5175   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5176   (interactive "P")
5177   (gnus-summary-reselect-current-group all t))
5178
5179 (defun gnus-summary-update-info (&optional non-destructive)
5180   (save-excursion
5181     (let ((group gnus-newsgroup-name))
5182       (when group
5183         (when gnus-newsgroup-kill-headers
5184           (setq gnus-newsgroup-killed
5185                 (gnus-compress-sequence
5186                  (nconc
5187                   (gnus-set-sorted-intersection
5188                    (gnus-uncompress-range gnus-newsgroup-killed)
5189                    (setq gnus-newsgroup-unselected
5190                          (sort gnus-newsgroup-unselected '<)))
5191                   (setq gnus-newsgroup-unreads
5192                         (sort gnus-newsgroup-unreads '<)))
5193                  t)))
5194         (unless (listp (cdr gnus-newsgroup-killed))
5195           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5196         (let ((headers gnus-newsgroup-headers))
5197           ;; Set the new ranges of read articles.
5198           (save-excursion
5199             (set-buffer gnus-group-buffer)
5200             (gnus-undo-force-boundary))
5201           (gnus-update-read-articles
5202            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5203           ;; Set the current article marks.
5204           (let ((gnus-newsgroup-scored
5205                  (if (and (not gnus-save-score)
5206                           (not non-destructive))
5207                      nil
5208                    gnus-newsgroup-scored)))
5209             (save-excursion
5210               (gnus-update-marks)))
5211           ;; Do the cross-ref thing.
5212           (when gnus-use-cross-reference
5213             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5214           ;; Do not switch windows but change the buffer to work.
5215           (set-buffer gnus-group-buffer)
5216           (unless (gnus-ephemeral-group-p group)
5217             (gnus-group-update-group group)))))))
5218
5219 (defun gnus-summary-save-newsrc (&optional force)
5220   "Save the current number of read/marked articles in the dribble buffer.
5221 The dribble buffer will then be saved.
5222 If FORCE (the prefix), also save the .newsrc file(s)."
5223   (interactive "P")
5224   (gnus-summary-update-info t)
5225   (if force
5226       (gnus-save-newsrc-file)
5227     (gnus-dribble-save)))
5228
5229 (defun gnus-summary-exit (&optional temporary)
5230   "Exit reading current newsgroup, and then return to group selection mode.
5231 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5232   (interactive)
5233   (gnus-set-global-variables)
5234   (gnus-kill-save-kill-buffer)
5235   (gnus-async-halt-prefetch)
5236   (let* ((group gnus-newsgroup-name)
5237          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5238          (mode major-mode)
5239          (group-point nil)
5240          (buf (current-buffer)))
5241     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5242     ;; If we have several article buffers, we kill them at exit.
5243     (unless gnus-single-article-buffer
5244       (gnus-kill-buffer gnus-original-article-buffer)
5245       (setq gnus-article-current nil))
5246     (when gnus-use-cache
5247       (gnus-cache-possibly-remove-articles)
5248       (gnus-cache-save-buffers))
5249     (gnus-async-prefetch-remove-group group)
5250     (when gnus-suppress-duplicates
5251       (gnus-dup-enter-articles))
5252     (when gnus-use-trees
5253       (gnus-tree-close group))
5254     ;; Remove entries for this group.
5255     (nnmail-purge-split-history (gnus-group-real-name group))
5256     ;; Make all changes in this group permanent.
5257     (unless quit-config
5258       (gnus-run-hooks 'gnus-exit-group-hook)
5259       (gnus-summary-update-info)
5260       ;; Do adaptive scoring, and possibly save score files.
5261       (when gnus-newsgroup-adaptive
5262         (gnus-score-adaptive))
5263       (when gnus-use-scoring
5264         (gnus-score-save)))
5265     (gnus-close-group group)
5266     ;; Make sure where we were, and go to next newsgroup.
5267     (set-buffer gnus-group-buffer)
5268     (unless quit-config
5269       (gnus-group-jump-to-group group))
5270     (gnus-run-hooks 'gnus-summary-exit-hook)
5271     (unless (or quit-config
5272                 ;; If this group has disappeared from the summary
5273                 ;; buffer, don't skip forwards.
5274                 (not (string= group (gnus-group-group-name))))
5275       (gnus-group-next-unread-group 1))
5276     (setq group-point (point))
5277     (if temporary
5278         nil                             ;Nothing to do.
5279       (when (gnus-buffer-live-p gnus-article-buffer)
5280         (save-excursion
5281           (set-buffer gnus-article-buffer)
5282           (mm-destroy-parts gnus-article-mime-handles)))
5283       ;; If we have several article buffers, we kill them at exit.
5284       (unless gnus-single-article-buffer
5285         (gnus-kill-buffer gnus-article-buffer)
5286         (gnus-kill-buffer gnus-original-article-buffer)
5287         (setq gnus-article-current nil))
5288       (set-buffer buf)
5289       (if (not gnus-kill-summary-on-exit)
5290           (gnus-deaden-summary)
5291         ;; We set all buffer-local variables to nil.  It is unclear why
5292         ;; this is needed, but if we don't, buffer-local variables are
5293         ;; not garbage-collected, it seems.  This would the lead to en
5294         ;; ever-growing Emacs.
5295         (gnus-summary-clear-local-variables)
5296         (when (get-buffer gnus-article-buffer)
5297           (bury-buffer gnus-article-buffer))
5298         ;; We clear the global counterparts of the buffer-local
5299         ;; variables as well, just to be on the safe side.
5300         (set-buffer gnus-group-buffer)
5301         (gnus-summary-clear-local-variables)
5302         ;; Return to group mode buffer.
5303         (when (eq mode 'gnus-summary-mode)
5304           (gnus-kill-buffer buf)))
5305       (setq gnus-current-select-method gnus-select-method)
5306       (pop-to-buffer gnus-group-buffer)
5307       (if (not quit-config)
5308           (progn
5309             (goto-char group-point)
5310             (gnus-configure-windows 'group 'force))
5311         (gnus-handle-ephemeral-exit quit-config))
5312       ;; Clear the current group name.
5313       (unless quit-config
5314         (setq gnus-newsgroup-name nil)))))
5315
5316 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5317 (defun gnus-summary-exit-no-update (&optional no-questions)
5318   "Quit reading current newsgroup without updating read article info."
5319   (interactive)
5320   (let* ((group gnus-newsgroup-name)
5321          (quit-config (gnus-group-quit-config group)))
5322     (when (or no-questions
5323               gnus-expert-user
5324               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5325       (gnus-async-halt-prefetch)
5326       (gnus-run-hooks (delq 'gnus-summary-expire-articles 
5327                             (copy-list gnus-summary-prepare-exit-hook)))
5328       (when (gnus-buffer-live-p gnus-article-buffer)
5329         (save-excursion
5330           (set-buffer gnus-article-buffer)
5331           (mm-destroy-parts gnus-article-mime-handles)))
5332       ;; If we have several article buffers, we kill them at exit.
5333       (unless gnus-single-article-buffer
5334         (gnus-kill-buffer gnus-article-buffer)
5335         (gnus-kill-buffer gnus-original-article-buffer)
5336         (setq gnus-article-current nil))
5337       (if (not gnus-kill-summary-on-exit)
5338           (gnus-deaden-summary)
5339         (gnus-close-group group)
5340         (gnus-summary-clear-local-variables)
5341         (set-buffer gnus-group-buffer)
5342         (gnus-summary-clear-local-variables)
5343         (when (get-buffer gnus-summary-buffer)
5344           (kill-buffer gnus-summary-buffer)))
5345       (unless gnus-single-article-buffer
5346         (setq gnus-article-current nil))
5347       (when gnus-use-trees
5348         (gnus-tree-close group))
5349       (gnus-async-prefetch-remove-group group)
5350       (when (get-buffer gnus-article-buffer)
5351         (bury-buffer gnus-article-buffer))
5352       ;; Return to the group buffer.
5353       (gnus-configure-windows 'group 'force)
5354       ;; Clear the current group name.
5355       (setq gnus-newsgroup-name nil)
5356       (when (equal (gnus-group-group-name) group)
5357         (gnus-group-next-unread-group 1))
5358       (when quit-config
5359         (gnus-handle-ephemeral-exit quit-config)))))
5360
5361 (defun gnus-handle-ephemeral-exit (quit-config)
5362   "Handle movement when leaving an ephemeral group.
5363 The state which existed when entering the ephemeral is reset."
5364   (if (not (buffer-name (car quit-config)))
5365       (gnus-configure-windows 'group 'force)
5366     (set-buffer (car quit-config))
5367     (cond ((eq major-mode 'gnus-summary-mode)
5368            (gnus-set-global-variables))
5369           ((eq major-mode 'gnus-article-mode)
5370            (save-excursion
5371              ;; The `gnus-summary-buffer' variable may point
5372              ;; to the old summary buffer when using a single
5373              ;; article buffer.
5374              (unless (gnus-buffer-live-p gnus-summary-buffer)
5375                (set-buffer gnus-group-buffer))
5376              (set-buffer gnus-summary-buffer)
5377              (gnus-set-global-variables))))
5378     (if (or (eq (cdr quit-config) 'article)
5379             (eq (cdr quit-config) 'pick))
5380         (progn
5381           ;; The current article may be from the ephemeral group
5382           ;; thus it is best that we reload this article
5383           (gnus-summary-show-article)
5384           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5385               (gnus-configure-windows 'pick 'force)
5386             (gnus-configure-windows (cdr quit-config) 'force)))
5387       (gnus-configure-windows (cdr quit-config) 'force))
5388     (when (eq major-mode 'gnus-summary-mode)
5389       (gnus-summary-next-subject 1 nil t)
5390       (gnus-summary-recenter)
5391       (gnus-summary-position-point))))
5392
5393 ;;; Dead summaries.
5394
5395 (defvar gnus-dead-summary-mode-map nil)
5396
5397 (unless gnus-dead-summary-mode-map
5398   (setq gnus-dead-summary-mode-map (make-keymap))
5399   (suppress-keymap gnus-dead-summary-mode-map)
5400   (substitute-key-definition
5401    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5402   (let ((keys '("\C-d" "\r" "\177" [delete])))
5403     (while keys
5404       (define-key gnus-dead-summary-mode-map
5405         (pop keys) 'gnus-summary-wake-up-the-dead))))
5406
5407 (defvar gnus-dead-summary-mode nil
5408   "Minor mode for Gnus summary buffers.")
5409
5410 (defun gnus-dead-summary-mode (&optional arg)
5411   "Minor mode for Gnus summary buffers."
5412   (interactive "P")
5413   (when (eq major-mode 'gnus-summary-mode)
5414     (make-local-variable 'gnus-dead-summary-mode)
5415     (setq gnus-dead-summary-mode
5416           (if (null arg) (not gnus-dead-summary-mode)
5417             (> (prefix-numeric-value arg) 0)))
5418     (when gnus-dead-summary-mode
5419       (gnus-add-minor-mode
5420        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5421
5422 (defun gnus-deaden-summary ()
5423   "Make the current summary buffer into a dead summary buffer."
5424   ;; Kill any previous dead summary buffer.
5425   (when (and gnus-dead-summary
5426              (buffer-name gnus-dead-summary))
5427     (save-excursion
5428       (set-buffer gnus-dead-summary)
5429       (when gnus-dead-summary-mode
5430         (kill-buffer (current-buffer)))))
5431   ;; Make this the current dead summary.
5432   (setq gnus-dead-summary (current-buffer))
5433   (gnus-dead-summary-mode 1)
5434   (let ((name (buffer-name)))
5435     (when (string-match "Summary" name)
5436       (rename-buffer
5437        (concat (substring name 0 (match-beginning 0)) "Dead "
5438                (substring name (match-beginning 0)))
5439        t))))
5440
5441 (defun gnus-kill-or-deaden-summary (buffer)
5442   "Kill or deaden the summary BUFFER."
5443   (save-excursion
5444     (when (and (buffer-name buffer)
5445                (not gnus-single-article-buffer))
5446       (save-excursion
5447         (set-buffer buffer)
5448         (gnus-kill-buffer gnus-article-buffer)
5449         (gnus-kill-buffer gnus-original-article-buffer)))
5450     (cond (gnus-kill-summary-on-exit
5451            (when (and gnus-use-trees
5452                       (gnus-buffer-exists-p buffer))
5453              (save-excursion
5454                (set-buffer buffer)
5455                (gnus-tree-close gnus-newsgroup-name)))
5456            (gnus-kill-buffer buffer))
5457           ((gnus-buffer-exists-p buffer)
5458            (save-excursion
5459              (set-buffer buffer)
5460              (gnus-deaden-summary))))))
5461
5462 (defun gnus-summary-wake-up-the-dead (&rest args)
5463   "Wake up the dead summary buffer."
5464   (interactive)
5465   (gnus-dead-summary-mode -1)
5466   (let ((name (buffer-name)))
5467     (when (string-match "Dead " name)
5468       (rename-buffer
5469        (concat (substring name 0 (match-beginning 0))
5470                (substring name (match-end 0)))
5471        t)))
5472   (gnus-message 3 "This dead summary is now alive again"))
5473
5474 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5475 (defun gnus-summary-fetch-faq (&optional faq-dir)
5476   "Fetch the FAQ for the current group.
5477 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5478 in."
5479   (interactive
5480    (list
5481     (when current-prefix-arg
5482       (completing-read
5483        "Faq dir: " (and (listp gnus-group-faq-directory)
5484                         (mapcar (lambda (file) (list file))
5485                                 gnus-group-faq-directory))))))
5486   (let (gnus-faq-buffer)
5487     (when (setq gnus-faq-buffer
5488                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5489       (gnus-configure-windows 'summary-faq))))
5490
5491 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5492 (defun gnus-summary-describe-group (&optional force)
5493   "Describe the current newsgroup."
5494   (interactive "P")
5495   (gnus-group-describe-group force gnus-newsgroup-name))
5496
5497 (defun gnus-summary-describe-briefly ()
5498   "Describe summary mode commands briefly."
5499   (interactive)
5500   (gnus-message 6
5501                 (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")))
5502
5503 ;; Walking around group mode buffer from summary mode.
5504
5505 (defun gnus-summary-next-group (&optional no-article target-group backward)
5506   "Exit current newsgroup and then select next unread newsgroup.
5507 If prefix argument NO-ARTICLE is non-nil, no article is selected
5508 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5509 previous group instead."
5510   (interactive "P")
5511   ;; Stop pre-fetching.
5512   (gnus-async-halt-prefetch)
5513   (let ((current-group gnus-newsgroup-name)
5514         (current-buffer (current-buffer))
5515         entered)
5516     ;; First we semi-exit this group to update Xrefs and all variables.
5517     ;; We can't do a real exit, because the window conf must remain
5518     ;; the same in case the user is prompted for info, and we don't
5519     ;; want the window conf to change before that...
5520     (gnus-summary-exit t)
5521     (while (not entered)
5522       ;; Then we find what group we are supposed to enter.
5523       (set-buffer gnus-group-buffer)
5524       (gnus-group-jump-to-group current-group)
5525       (setq target-group
5526             (or target-group
5527                 (if (eq gnus-keep-same-level 'best)
5528                     (gnus-summary-best-group gnus-newsgroup-name)
5529                   (gnus-summary-search-group backward gnus-keep-same-level))))
5530       (if (not target-group)
5531           ;; There are no further groups, so we return to the group
5532           ;; buffer.
5533           (progn
5534             (gnus-message 5 "Returning to the group buffer")
5535             (setq entered t)
5536             (when (gnus-buffer-live-p current-buffer)
5537               (set-buffer current-buffer)
5538               (gnus-summary-exit))
5539             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5540         ;; We try to enter the target group.
5541         (gnus-group-jump-to-group target-group)
5542         (let ((unreads (gnus-group-group-unread)))
5543           (if (and (or (eq t unreads)
5544                        (and unreads (not (zerop unreads))))
5545                    (gnus-summary-read-group
5546                     target-group nil no-article
5547                     (and (buffer-name current-buffer) current-buffer)
5548                     nil backward))
5549               (setq entered t)
5550             (setq current-group target-group
5551                   target-group nil)))))))
5552
5553 (defun gnus-summary-prev-group (&optional no-article)
5554   "Exit current newsgroup and then select previous unread newsgroup.
5555 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5556   (interactive "P")
5557   (gnus-summary-next-group no-article nil t))
5558
5559 ;; Walking around summary lines.
5560
5561 (defun gnus-summary-first-subject (&optional unread undownloaded)
5562   "Go to the first unread subject.
5563 If UNREAD is non-nil, go to the first unread article.
5564 Returns the article selected or nil if there are no unread articles."
5565   (interactive "P")
5566   (prog1
5567       (cond
5568        ;; Empty summary.
5569        ((null gnus-newsgroup-data)
5570         (gnus-message 3 "No articles in the group")
5571         nil)
5572        ;; Pick the first article.
5573        ((not unread)
5574         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5575         (gnus-data-number (car gnus-newsgroup-data)))
5576        ;; No unread articles.
5577        ((null gnus-newsgroup-unreads)
5578         (gnus-message 3 "No more unread articles")
5579         nil)
5580        ;; Find the first unread article.
5581        (t
5582         (let ((data gnus-newsgroup-data))
5583           (while (and data
5584                       (and (not (and undownloaded
5585                                      (eq gnus-undownloaded-mark
5586                                          (gnus-data-mark (car data)))))
5587                            (not (gnus-data-unread-p (car data)))))
5588             (setq data (cdr data)))
5589           (when data
5590             (goto-char (gnus-data-pos (car data)))
5591             (gnus-data-number (car data))))))
5592     (gnus-summary-position-point)))
5593
5594 (defun gnus-summary-next-subject (n &optional unread dont-display)
5595   "Go to next N'th summary line.
5596 If N is negative, go to the previous N'th subject line.
5597 If UNREAD is non-nil, only unread articles are selected.
5598 The difference between N and the actual number of steps taken is
5599 returned."
5600   (interactive "p")
5601   (let ((backward (< n 0))
5602         (n (abs n)))
5603     (while (and (> n 0)
5604                 (if backward
5605                     (gnus-summary-find-prev unread)
5606                   (gnus-summary-find-next unread)))
5607       (gnus-summary-show-thread)
5608       (setq n (1- n)))
5609     (when (/= 0 n)
5610       (gnus-message 7 "No more%s articles"
5611                     (if unread " unread" "")))
5612     (unless dont-display
5613       (gnus-summary-recenter)
5614       (gnus-summary-position-point))
5615     n))
5616
5617 (defun gnus-summary-next-unread-subject (n)
5618   "Go to next N'th unread summary line."
5619   (interactive "p")
5620   (gnus-summary-next-subject n t))
5621
5622 (defun gnus-summary-prev-subject (n &optional unread)
5623   "Go to previous N'th summary line.
5624 If optional argument UNREAD is non-nil, only unread article is selected."
5625   (interactive "p")
5626   (gnus-summary-next-subject (- n) unread))
5627
5628 (defun gnus-summary-prev-unread-subject (n)
5629   "Go to previous N'th unread summary line."
5630   (interactive "p")
5631   (gnus-summary-next-subject (- n) t))
5632
5633 (defun gnus-summary-goto-subject (article &optional force silent)
5634   "Go the subject line of ARTICLE.
5635 If FORCE, also allow jumping to articles not currently shown."
5636   (interactive "nArticle number: ")
5637   (let ((b (point))
5638         (data (gnus-data-find article)))
5639     ;; We read in the article if we have to.
5640     (and (not data)
5641          force
5642          (gnus-summary-insert-subject
5643           article
5644           (if (or (numberp force) (vectorp force)) force)
5645           t)
5646          (setq data (gnus-data-find article)))
5647     (goto-char b)
5648     (if (not data)
5649         (progn
5650           (unless silent
5651             (gnus-message 3 "Can't find article %d" article))
5652           nil)
5653       (goto-char (gnus-data-pos data))
5654       (gnus-summary-position-point)
5655       article)))
5656
5657 ;; Walking around summary lines with displaying articles.
5658
5659 (defun gnus-summary-expand-window (&optional arg)
5660   "Make the summary buffer take up the entire Emacs frame.
5661 Given a prefix, will force an `article' buffer configuration."
5662   (interactive "P")
5663   (if arg
5664       (gnus-configure-windows 'article 'force)
5665     (gnus-configure-windows 'summary 'force)))
5666
5667 (defun gnus-summary-display-article (article &optional all-header)
5668   "Display ARTICLE in article buffer."
5669   (gnus-set-global-variables)
5670   (if (null article)
5671       nil
5672     (prog1
5673         (if gnus-summary-display-article-function
5674             (funcall gnus-summary-display-article-function article all-header)
5675           (gnus-article-prepare article all-header))
5676       (gnus-run-hooks 'gnus-select-article-hook)
5677       (when (and gnus-current-article
5678                  (not (zerop gnus-current-article)))
5679         (gnus-summary-goto-subject gnus-current-article))
5680       (gnus-summary-recenter)
5681       (when (and gnus-use-trees gnus-show-threads)
5682         (gnus-possibly-generate-tree article)
5683         (gnus-highlight-selected-tree article))
5684       ;; Successfully display article.
5685       (gnus-article-set-window-start
5686        (cdr (assq article gnus-newsgroup-bookmarks))))))
5687
5688 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5689   "Select the current article.
5690 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5691 non-nil, the article will be re-fetched even if it already present in
5692 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5693 be displayed."
5694   ;; Make sure we are in the summary buffer to work around bbdb bug.
5695   (unless (eq major-mode 'gnus-summary-mode)
5696     (set-buffer gnus-summary-buffer))
5697   (let ((article (or article (gnus-summary-article-number)))
5698         (all-headers (not (not all-headers))) ;Must be T or NIL.
5699         gnus-summary-display-article-function
5700         did)
5701     (and (not pseudo)
5702          (gnus-summary-article-pseudo-p article)
5703          (error "This is a pseudo-article"))
5704     (prog1
5705         (save-excursion
5706           (set-buffer gnus-summary-buffer)
5707           (if (or (and gnus-single-article-buffer
5708                        (or (null gnus-current-article)
5709                            (null gnus-article-current)
5710                            (null (get-buffer gnus-article-buffer))
5711                            (not (eq article (cdr gnus-article-current)))
5712                            (not (equal (car gnus-article-current)
5713                                        gnus-newsgroup-name))))
5714                   (and (not gnus-single-article-buffer)
5715                        (or (null gnus-current-article)
5716                            (not (eq gnus-current-article article))))
5717                   force)
5718               ;; The requested article is different from the current article.
5719               (prog1
5720                   (gnus-summary-display-article article all-headers)
5721                 (setq did article))
5722             (when (or all-headers gnus-show-all-headers)
5723               (gnus-article-show-all-headers))
5724             'old))
5725       (when did
5726         (gnus-article-set-window-start
5727          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5728
5729 (defun gnus-summary-set-current-mark (&optional current-mark)
5730   "Obsolete function."
5731   nil)
5732
5733 (defun gnus-summary-next-article (&optional unread subject backward push)
5734   "Select the next article.
5735 If UNREAD, only unread articles are selected.
5736 If SUBJECT, only articles with SUBJECT are selected.
5737 If BACKWARD, the previous article is selected instead of the next."
5738   (interactive "P")
5739   (cond
5740    ;; Is there such an article?
5741    ((and (gnus-summary-search-forward unread subject backward)
5742          (or (gnus-summary-display-article (gnus-summary-article-number))
5743              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5744     (gnus-summary-position-point))
5745    ;; If not, we try the first unread, if that is wanted.
5746    ((and subject
5747          gnus-auto-select-same
5748          (gnus-summary-first-unread-article))
5749     (gnus-summary-position-point)
5750     (gnus-message 6 "Wrapped"))
5751    ;; Try to get next/previous article not displayed in this group.
5752    ((and gnus-auto-extend-newsgroup
5753          (not unread) (not subject))
5754     (gnus-summary-goto-article
5755      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5756      nil (count-lines (point-min) (point))))
5757    ;; Go to next/previous group.
5758    (t
5759     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5760       (gnus-summary-jump-to-group gnus-newsgroup-name))
5761     (let ((cmd last-command-char)
5762           (point
5763            (save-excursion
5764              (set-buffer gnus-group-buffer)
5765              (point)))
5766           (group
5767            (if (eq gnus-keep-same-level 'best)
5768                (gnus-summary-best-group gnus-newsgroup-name)
5769              (gnus-summary-search-group backward gnus-keep-same-level))))
5770       ;; For some reason, the group window gets selected.  We change
5771       ;; it back.
5772       (select-window (get-buffer-window (current-buffer)))
5773       ;; Select next unread newsgroup automagically.
5774       (cond
5775        ((or (not gnus-auto-select-next)
5776             (not cmd))
5777         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5778        ((or (eq gnus-auto-select-next 'quietly)
5779             (and (eq gnus-auto-select-next 'slightly-quietly)
5780                  push)
5781             (and (eq gnus-auto-select-next 'almost-quietly)
5782                  (gnus-summary-last-article-p)))
5783         ;; Select quietly.
5784         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5785             (gnus-summary-exit)
5786           (gnus-message 7 "No more%s articles (%s)..."
5787                         (if unread " unread" "")
5788                         (if group (concat "selecting " group)
5789                           "exiting"))
5790           (gnus-summary-next-group nil group backward)))
5791        (t
5792         (when (gnus-key-press-event-p last-input-event)
5793           (gnus-summary-walk-group-buffer
5794            gnus-newsgroup-name cmd unread backward point))))))))
5795
5796 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5797   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5798                       (?\C-p (gnus-group-prev-unread-group 1))))
5799         (cursor-in-echo-area t)
5800         keve key group ended)
5801     (save-excursion
5802       (set-buffer gnus-group-buffer)
5803       (goto-char start)
5804       (setq group
5805             (if (eq gnus-keep-same-level 'best)
5806                 (gnus-summary-best-group gnus-newsgroup-name)
5807               (gnus-summary-search-group backward gnus-keep-same-level))))
5808     (while (not ended)
5809       (gnus-message
5810        5 "No more%s articles%s" (if unread " unread" "")
5811        (if (and group
5812                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5813            (format " (Type %s for %s [%s])"
5814                    (single-key-description cmd) group
5815                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5816          (format " (Type %s to exit %s)"
5817                  (single-key-description cmd)
5818                  gnus-newsgroup-name)))
5819       ;; Confirm auto selection.
5820       (setq key (car (setq keve (gnus-read-event-char))))
5821       (setq ended t)
5822       (cond
5823        ((assq key keystrokes)
5824         (let ((obuf (current-buffer)))
5825           (switch-to-buffer gnus-group-buffer)
5826           (when group
5827             (gnus-group-jump-to-group group))
5828           (eval (cadr (assq key keystrokes)))
5829           (setq group (gnus-group-group-name))
5830           (switch-to-buffer obuf))
5831         (setq ended nil))
5832        ((equal key cmd)
5833         (if (or (not group)
5834                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5835             (gnus-summary-exit)
5836           (gnus-summary-next-group nil group backward)))
5837        (t
5838         (push (cdr keve) unread-command-events))))))
5839
5840 (defun gnus-summary-next-unread-article ()
5841   "Select unread article after current one."
5842   (interactive)
5843   (gnus-summary-next-article
5844    (or (not (eq gnus-summary-goto-unread 'never))
5845        (gnus-summary-last-article-p (gnus-summary-article-number)))
5846    (and gnus-auto-select-same
5847         (gnus-summary-article-subject))))
5848
5849 (defun gnus-summary-prev-article (&optional unread subject)
5850   "Select the article after the current one.
5851 If UNREAD is non-nil, only unread articles are selected."
5852   (interactive "P")
5853   (gnus-summary-next-article unread subject t))
5854
5855 (defun gnus-summary-prev-unread-article ()
5856   "Select unread article before current one."
5857   (interactive)
5858   (gnus-summary-prev-article
5859    (or (not (eq gnus-summary-goto-unread 'never))
5860        (gnus-summary-first-article-p (gnus-summary-article-number)))
5861    (and gnus-auto-select-same
5862         (gnus-summary-article-subject))))
5863
5864 (defun gnus-summary-next-page (&optional lines circular)
5865   "Show next page of the selected article.
5866 If at the end of the current article, select the next article.
5867 LINES says how many lines should be scrolled up.
5868
5869 If CIRCULAR is non-nil, go to the start of the article instead of
5870 selecting the next article when reaching the end of the current
5871 article."
5872   (interactive "P")
5873   (setq gnus-summary-buffer (current-buffer))
5874   (gnus-set-global-variables)
5875   (let ((article (gnus-summary-article-number))
5876         (article-window (get-buffer-window gnus-article-buffer t))
5877         endp)
5878     ;; If the buffer is empty, we have no article.
5879     (unless article
5880       (error "No article to select"))
5881     (gnus-configure-windows 'article)
5882     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5883         (if (and (eq gnus-summary-goto-unread 'never)
5884                  (not (gnus-summary-last-article-p article)))
5885             (gnus-summary-next-article)
5886           (gnus-summary-next-unread-article))
5887       (if (or (null gnus-current-article)
5888               (null gnus-article-current)
5889               (/= article (cdr gnus-article-current))
5890               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5891           ;; Selected subject is different from current article's.
5892           (gnus-summary-display-article article)
5893         (when article-window
5894           (gnus-eval-in-buffer-window gnus-article-buffer
5895             (setq endp (gnus-article-next-page lines)))
5896           (when endp
5897             (cond (circular
5898                    (gnus-summary-beginning-of-article))
5899                   (lines
5900                    (gnus-message 3 "End of message"))
5901                   ((null lines)
5902                    (if (and (eq gnus-summary-goto-unread 'never)
5903                             (not (gnus-summary-last-article-p article)))
5904                        (gnus-summary-next-article)
5905                      (gnus-summary-next-unread-article))))))))
5906     (gnus-summary-recenter)
5907     (gnus-summary-position-point)))
5908
5909 (defun gnus-summary-prev-page (&optional lines move)
5910   "Show previous page of selected article.
5911 Argument LINES specifies lines to be scrolled down.
5912 If MOVE, move to the previous unread article if point is at
5913 the beginning of the buffer."
5914   (interactive "P")
5915   (let ((article (gnus-summary-article-number))
5916         (article-window (get-buffer-window gnus-article-buffer t))
5917         endp)
5918     (gnus-configure-windows 'article)
5919     (if (or (null gnus-current-article)
5920             (null gnus-article-current)
5921             (/= article (cdr gnus-article-current))
5922             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5923         ;; Selected subject is different from current article's.
5924         (gnus-summary-display-article article)
5925       (gnus-summary-recenter)
5926       (when article-window
5927         (gnus-eval-in-buffer-window gnus-article-buffer
5928           (setq endp (gnus-article-prev-page lines)))
5929         (when (and move endp)
5930           (cond (lines
5931                  (gnus-message 3 "Beginning of message"))
5932                 ((null lines)
5933                  (if (and (eq gnus-summary-goto-unread 'never)
5934                           (not (gnus-summary-first-article-p article)))
5935                      (gnus-summary-prev-article)
5936                    (gnus-summary-prev-unread-article))))))))
5937   (gnus-summary-position-point))
5938
5939 (defun gnus-summary-prev-page-or-article (&optional lines)
5940   "Show previous page of selected article.
5941 Argument LINES specifies lines to be scrolled down.
5942 If at the beginning of the article, go to the next article."
5943   (interactive "P")
5944   (gnus-summary-prev-page lines t))
5945
5946 (defun gnus-summary-scroll-up (lines)
5947   "Scroll up (or down) one line current article.
5948 Argument LINES specifies lines to be scrolled up (or down if negative)."
5949   (interactive "p")
5950   (gnus-configure-windows 'article)
5951   (gnus-summary-show-thread)
5952   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5953     (gnus-eval-in-buffer-window gnus-article-buffer
5954       (cond ((> lines 0)
5955              (when (gnus-article-next-page lines)
5956                (gnus-message 3 "End of message")))
5957             ((< lines 0)
5958              (gnus-article-prev-page (- lines))))))
5959   (gnus-summary-recenter)
5960   (gnus-summary-position-point))
5961
5962 (defun gnus-summary-scroll-down (lines)
5963   "Scroll down (or up) one line current article.
5964 Argument LINES specifies lines to be scrolled down (or up if negative)."
5965   (interactive "p")
5966   (gnus-summary-scroll-up (- lines)))
5967
5968 (defun gnus-summary-next-same-subject ()
5969   "Select next article which has the same subject as current one."
5970   (interactive)
5971   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5972
5973 (defun gnus-summary-prev-same-subject ()
5974   "Select previous article which has the same subject as current one."
5975   (interactive)
5976   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5977
5978 (defun gnus-summary-next-unread-same-subject ()
5979   "Select next unread article which has the same subject as current one."
5980   (interactive)
5981   (gnus-summary-next-article t (gnus-summary-article-subject)))
5982
5983 (defun gnus-summary-prev-unread-same-subject ()
5984   "Select previous unread article which has the same subject as current one."
5985   (interactive)
5986   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5987
5988 (defun gnus-summary-first-unread-article ()
5989   "Select the first unread article.
5990 Return nil if there are no unread articles."
5991   (interactive)
5992   (prog1
5993       (when (gnus-summary-first-subject t)
5994         (gnus-summary-show-thread)
5995         (gnus-summary-first-subject t)
5996         (gnus-summary-display-article (gnus-summary-article-number)))
5997     (gnus-summary-position-point)))
5998
5999 (defun gnus-summary-first-unread-subject ()
6000   "Place the point on the subject line of the first unread article.
6001 Return nil if there are no unread articles."
6002   (interactive)
6003   (prog1
6004       (when (gnus-summary-first-subject t)
6005         (gnus-summary-show-thread)
6006         (gnus-summary-first-subject t))
6007     (gnus-summary-position-point)))
6008
6009 (defun gnus-summary-first-article ()
6010   "Select the first article.
6011 Return nil if there are no articles."
6012   (interactive)
6013   (prog1
6014       (when (gnus-summary-first-subject)
6015         (gnus-summary-show-thread)
6016         (gnus-summary-first-subject)
6017         (gnus-summary-display-article (gnus-summary-article-number)))
6018     (gnus-summary-position-point)))
6019
6020 (defun gnus-summary-best-unread-article ()
6021   "Select the unread article with the highest score."
6022   (interactive)
6023   (let ((best -1000000)
6024         (data gnus-newsgroup-data)
6025         article score)
6026     (while data
6027       (and (gnus-data-unread-p (car data))
6028            (> (setq score
6029                     (gnus-summary-article-score (gnus-data-number (car data))))
6030               best)
6031            (setq best score
6032                  article (gnus-data-number (car data))))
6033       (setq data (cdr data)))
6034     (prog1
6035         (if article
6036             (gnus-summary-goto-article article)
6037           (error "No unread articles"))
6038       (gnus-summary-position-point))))
6039
6040 (defun gnus-summary-last-subject ()
6041   "Go to the last displayed subject line in the group."
6042   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6043     (when article
6044       (gnus-summary-goto-subject article))))
6045
6046 (defun gnus-summary-goto-article (article &optional all-headers force)
6047   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6048 If ALL-HEADERS is non-nil, no header lines are hidden.
6049 If FORCE, go to the article even if it isn't displayed.  If FORCE
6050 is a number, it is the line the article is to be displayed on."
6051   (interactive
6052    (list
6053     (completing-read
6054      "Article number or Message-ID: "
6055      (mapcar (lambda (number) (list (int-to-string number)))
6056              gnus-newsgroup-limit))
6057     current-prefix-arg
6058     t))
6059   (prog1
6060       (if (and (stringp article)
6061                (string-match "@" article))
6062           (gnus-summary-refer-article article)
6063         (when (stringp article)
6064           (setq article (string-to-number article)))
6065         (if (gnus-summary-goto-subject article force)
6066             (gnus-summary-display-article article all-headers)
6067           (gnus-message 4 "Couldn't go to article %s" article) nil))
6068     (gnus-summary-position-point)))
6069
6070 (defun gnus-summary-goto-last-article ()
6071   "Go to the previously read article."
6072   (interactive)
6073   (prog1
6074       (when gnus-last-article
6075         (gnus-summary-goto-article gnus-last-article nil t))
6076     (gnus-summary-position-point)))
6077
6078 (defun gnus-summary-pop-article (number)
6079   "Pop one article off the history and go to the previous.
6080 NUMBER articles will be popped off."
6081   (interactive "p")
6082   (let (to)
6083     (setq gnus-newsgroup-history
6084           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6085     (if to
6086         (gnus-summary-goto-article (car to) nil t)
6087       (error "Article history empty")))
6088   (gnus-summary-position-point))
6089
6090 ;; Summary commands and functions for limiting the summary buffer.
6091
6092 (defun gnus-summary-limit-to-articles (n)
6093   "Limit the summary buffer to the next N articles.
6094 If not given a prefix, use the process marked articles instead."
6095   (interactive "P")
6096   (prog1
6097       (let ((articles (gnus-summary-work-articles n)))
6098         (setq gnus-newsgroup-processable nil)
6099         (gnus-summary-limit articles))
6100     (gnus-summary-position-point)))
6101
6102 (defun gnus-summary-pop-limit (&optional total)
6103   "Restore the previous limit.
6104 If given a prefix, remove all limits."
6105   (interactive "P")
6106   (when total
6107     (setq gnus-newsgroup-limits
6108           (list (mapcar (lambda (h) (mail-header-number h))
6109                         gnus-newsgroup-headers))))
6110   (unless gnus-newsgroup-limits
6111     (error "No limit to pop"))
6112   (prog1
6113       (gnus-summary-limit nil 'pop)
6114     (gnus-summary-position-point)))
6115
6116 (defun gnus-summary-limit-to-subject (subject &optional header)
6117   "Limit the summary buffer to articles that have subjects that match a regexp."
6118   (interactive "sLimit to subject (regexp): ")
6119   (unless header
6120     (setq header "subject"))
6121   (when (not (equal "" subject))
6122     (prog1
6123         (let ((articles (gnus-summary-find-matching
6124                          (or header "subject") subject 'all)))
6125           (unless articles
6126             (error "Found no matches for \"%s\"" subject))
6127           (gnus-summary-limit articles))
6128       (gnus-summary-position-point))))
6129
6130 (defun gnus-summary-limit-to-author (from)
6131   "Limit the summary buffer to articles that have authors that match a regexp."
6132   (interactive "sLimit to author (regexp): ")
6133   (gnus-summary-limit-to-subject from "from"))
6134
6135 (defun gnus-summary-limit-to-age (age &optional younger-p)
6136   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6137 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6138 articles that are younger than AGE days."
6139   (interactive "nTime in days: \nP")
6140   (prog1
6141       (let ((data gnus-newsgroup-data)
6142             (cutoff (days-to-time age))
6143             articles d date is-younger)
6144         (while (setq d (pop data))
6145           (when (and (vectorp (gnus-data-header d))
6146                      (setq date (mail-header-date (gnus-data-header d))))
6147             (setq is-younger (time-less-p
6148                               (time-since (date-to-time date))
6149                               cutoff))
6150             (when (if younger-p
6151                       is-younger
6152                     (not is-younger))
6153               (push (gnus-data-number d) articles))))
6154         (gnus-summary-limit (nreverse articles)))
6155     (gnus-summary-position-point)))
6156
6157 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6158 (make-obsolete
6159  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6160
6161 (defun gnus-summary-limit-to-unread (&optional all)
6162   "Limit the summary buffer to articles that are not marked as read.
6163 If ALL is non-nil, limit strictly to unread articles."
6164   (interactive "P")
6165   (if all
6166       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6167     (gnus-summary-limit-to-marks
6168      ;; Concat all the marks that say that an article is read and have
6169      ;; those removed.
6170      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6171            gnus-killed-mark gnus-kill-file-mark
6172            gnus-low-score-mark gnus-expirable-mark
6173            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6174            gnus-duplicate-mark gnus-souped-mark)
6175      'reverse)))
6176
6177 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6178 (make-obsolete 'gnus-summary-delete-marked-with
6179                'gnus-summary-limit-exlude-marks)
6180
6181 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6182   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6183 If REVERSE, limit the summary buffer to articles that are marked
6184 with MARKS.  MARKS can either be a string of marks or a list of marks.
6185 Returns how many articles were removed."
6186   (interactive "sMarks: ")
6187   (gnus-summary-limit-to-marks marks t))
6188
6189 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6190   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6191 If REVERSE (the prefix), limit the summary buffer to articles that are
6192 not marked with MARKS.  MARKS can either be a string of marks or a
6193 list of marks.
6194 Returns how many articles were removed."
6195   (interactive "sMarks: \nP")
6196   (prog1
6197       (let ((data gnus-newsgroup-data)
6198             (marks (if (listp marks) marks
6199                      (append marks nil))) ; Transform to list.
6200             articles)
6201         (while data
6202           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6203                   (memq (gnus-data-mark (car data)) marks))
6204             (push (gnus-data-number (car data)) articles))
6205           (setq data (cdr data)))
6206         (gnus-summary-limit articles))
6207     (gnus-summary-position-point)))
6208
6209 (defun gnus-summary-limit-to-score (&optional score)
6210   "Limit to articles with score at or above SCORE."
6211   (interactive "P")
6212   (setq score (if score
6213                   (prefix-numeric-value score)
6214                 (or gnus-summary-default-score 0)))
6215   (let ((data gnus-newsgroup-data)
6216         articles)
6217     (while data
6218       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6219                 score)
6220         (push (gnus-data-number (car data)) articles))
6221       (setq data (cdr data)))
6222     (prog1
6223         (gnus-summary-limit articles)
6224       (gnus-summary-position-point))))
6225
6226 (defun gnus-summary-limit-include-thread (id)
6227   "Display all the hidden articles that in the current thread."
6228   (interactive (list (mail-header-id (gnus-summary-article-header))))
6229   (let ((articles (gnus-articles-in-thread
6230                    (gnus-id-to-thread (gnus-root-id id)))))
6231     (prog1
6232         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6233       (gnus-summary-position-point))))
6234
6235 (defun gnus-summary-limit-include-dormant ()
6236   "Display all the hidden articles that are marked as dormant.
6237 Note that this command only works on a subset of the articles currently
6238 fetched for this group."
6239   (interactive)
6240   (unless gnus-newsgroup-dormant
6241     (error "There are no dormant articles in this group"))
6242   (prog1
6243       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6244     (gnus-summary-position-point)))
6245
6246 (defun gnus-summary-limit-exclude-dormant ()
6247   "Hide all dormant articles."
6248   (interactive)
6249   (prog1
6250       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6251     (gnus-summary-position-point)))
6252
6253 (defun gnus-summary-limit-exclude-childless-dormant ()
6254   "Hide all dormant articles that have no children."
6255   (interactive)
6256   (let ((data (gnus-data-list t))
6257         articles d children)
6258     ;; Find all articles that are either not dormant or have
6259     ;; children.
6260     (while (setq d (pop data))
6261       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6262                 (and (setq children
6263                            (gnus-article-children (gnus-data-number d)))
6264                      (let (found)
6265                        (while children
6266                          (when (memq (car children) articles)
6267                            (setq children nil
6268                                  found t))
6269                          (pop children))
6270                        found)))
6271         (push (gnus-data-number d) articles)))
6272     ;; Do the limiting.
6273     (prog1
6274         (gnus-summary-limit articles)
6275       (gnus-summary-position-point))))
6276
6277 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6278   "Mark all unread excluded articles as read.
6279 If ALL, mark even excluded ticked and dormants as read."
6280   (interactive "P")
6281   (let ((articles (gnus-sorted-complement
6282                    (sort
6283                     (mapcar (lambda (h) (mail-header-number h))
6284                             gnus-newsgroup-headers)
6285                     '<)
6286                    (sort gnus-newsgroup-limit '<)))
6287         article)
6288     (setq gnus-newsgroup-unreads
6289           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6290     (if all
6291         (setq gnus-newsgroup-dormant nil
6292               gnus-newsgroup-marked nil
6293               gnus-newsgroup-reads
6294               (nconc
6295                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6296                gnus-newsgroup-reads))
6297       (while (setq article (pop articles))
6298         (unless (or (memq article gnus-newsgroup-dormant)
6299                     (memq article gnus-newsgroup-marked))
6300           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6301
6302 (defun gnus-summary-limit (articles &optional pop)
6303   (if pop
6304       ;; We pop the previous limit off the stack and use that.
6305       (setq articles (car gnus-newsgroup-limits)
6306             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6307     ;; We use the new limit, so we push the old limit on the stack.
6308     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6309   ;; Set the limit.
6310   (setq gnus-newsgroup-limit articles)
6311   (let ((total (length gnus-newsgroup-data))
6312         (data (gnus-data-find-list (gnus-summary-article-number)))
6313         (gnus-summary-mark-below nil)   ; Inhibit this.
6314         found)
6315     ;; This will do all the work of generating the new summary buffer
6316     ;; according to the new limit.
6317     (gnus-summary-prepare)
6318     ;; Hide any threads, possibly.
6319     (and gnus-show-threads
6320          gnus-thread-hide-subtree
6321          (gnus-summary-hide-all-threads))
6322     ;; Try to return to the article you were at, or one in the
6323     ;; neighborhood.
6324     (when data
6325       ;; We try to find some article after the current one.
6326       (while data
6327         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6328           (setq data nil
6329                 found t))
6330         (setq data (cdr data))))
6331     (unless found
6332       ;; If there is no data, that means that we were after the last
6333       ;; article.  The same goes when we can't find any articles
6334       ;; after the current one.
6335       (goto-char (point-max))
6336       (gnus-summary-find-prev))
6337     (gnus-set-mode-line 'summary)
6338     ;; We return how many articles were removed from the summary
6339     ;; buffer as a result of the new limit.
6340     (- total (length gnus-newsgroup-data))))
6341
6342 (defsubst gnus-invisible-cut-children (threads)
6343   (let ((num 0))
6344     (while threads
6345       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6346         (incf num))
6347       (pop threads))
6348     (< num 2)))
6349
6350 (defsubst gnus-cut-thread (thread)
6351   "Go forwards in the thread until we find an article that we want to display."
6352   (when (or (eq gnus-fetch-old-headers 'some)
6353             (eq gnus-fetch-old-headers 'invisible)
6354             (eq gnus-build-sparse-threads 'some)
6355             (eq gnus-build-sparse-threads 'more))
6356     ;; Deal with old-fetched headers and sparse threads.
6357     (while (and
6358             thread
6359             (or
6360              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6361              (gnus-summary-article-ancient-p
6362               (mail-header-number (car thread))))
6363             (if (or (<= (length (cdr thread)) 1)
6364                     (eq gnus-fetch-old-headers 'invisible))
6365                 (setq gnus-newsgroup-limit
6366                       (delq (mail-header-number (car thread))
6367                             gnus-newsgroup-limit)
6368                       thread (cadr thread))
6369               (when (gnus-invisible-cut-children (cdr thread))
6370                 (let ((th (cdr thread)))
6371                   (while th
6372                     (if (memq (mail-header-number (caar th))
6373                               gnus-newsgroup-limit)
6374                         (setq thread (car th)
6375                               th nil)
6376                       (setq th (cdr th))))))))))
6377   thread)
6378
6379 (defun gnus-cut-threads (threads)
6380   "Cut off all uninteresting articles from the beginning of threads."
6381   (when (or (eq gnus-fetch-old-headers 'some)
6382             (eq gnus-fetch-old-headers 'invisible)
6383             (eq gnus-build-sparse-threads 'some)
6384             (eq gnus-build-sparse-threads 'more))
6385     (let ((th threads))
6386       (while th
6387         (setcar th (gnus-cut-thread (car th)))
6388         (setq th (cdr th)))))
6389   ;; Remove nixed out threads.
6390   (delq nil threads))
6391
6392 (defun gnus-summary-initial-limit (&optional show-if-empty)
6393   "Figure out what the initial limit is supposed to be on group entry.
6394 This entails weeding out unwanted dormants, low-scored articles,
6395 fetch-old-headers verbiage, and so on."
6396   ;; Most groups have nothing to remove.
6397   (if (or gnus-inhibit-limiting
6398           (and (null gnus-newsgroup-dormant)
6399                (not (eq gnus-fetch-old-headers 'some))
6400                (not (eq gnus-fetch-old-headers 'invisible))
6401                (null gnus-summary-expunge-below)
6402                (not (eq gnus-build-sparse-threads 'some))
6403                (not (eq gnus-build-sparse-threads 'more))
6404                (null gnus-thread-expunge-below)
6405                (not gnus-use-nocem)))
6406       ()                                ; Do nothing.
6407     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6408     (setq gnus-newsgroup-limit nil)
6409     (mapatoms
6410      (lambda (node)
6411        (unless (car (symbol-value node))
6412          ;; These threads have no parents -- they are roots.
6413          (let ((nodes (cdr (symbol-value node)))
6414                thread)
6415            (while nodes
6416              (if (and gnus-thread-expunge-below
6417                       (< (gnus-thread-total-score (car nodes))
6418                          gnus-thread-expunge-below))
6419                  (gnus-expunge-thread (pop nodes))
6420                (setq thread (pop nodes))
6421                (gnus-summary-limit-children thread))))))
6422      gnus-newsgroup-dependencies)
6423     ;; If this limitation resulted in an empty group, we might
6424     ;; pop the previous limit and use it instead.
6425     (when (and (not gnus-newsgroup-limit)
6426                show-if-empty)
6427       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6428     gnus-newsgroup-limit))
6429
6430 (defun gnus-summary-limit-children (thread)
6431   "Return 1 if this subthread is visible and 0 if it is not."
6432   ;; First we get the number of visible children to this thread.  This
6433   ;; is done by recursing down the thread using this function, so this
6434   ;; will really go down to a leaf article first, before slowly
6435   ;; working its way up towards the root.
6436   (when thread
6437     (let ((children
6438            (if (cdr thread)
6439                (apply '+ (mapcar 'gnus-summary-limit-children
6440                                  (cdr thread)))
6441              0))
6442           (number (mail-header-number (car thread)))
6443           score)
6444       (if (and
6445            (not (memq number gnus-newsgroup-marked))
6446            (or
6447             ;; If this article is dormant and has absolutely no visible
6448             ;; children, then this article isn't visible.
6449             (and (memq number gnus-newsgroup-dormant)
6450                  (zerop children))
6451             ;; If this is "fetch-old-headered" and there is no
6452             ;; visible children, then we don't want this article.
6453             (and (eq gnus-fetch-old-headers 'some)
6454                  (gnus-summary-article-ancient-p number)
6455                  (zerop children))
6456             ;; If this is "fetch-old-headered" and `invisible', then
6457             ;; we don't want this article.
6458             (and (eq gnus-fetch-old-headers 'invisible)
6459                  (gnus-summary-article-ancient-p number))
6460             ;; If this is a sparsely inserted article with no children,
6461             ;; we don't want it.
6462             (and (eq gnus-build-sparse-threads 'some)
6463                  (gnus-summary-article-sparse-p number)
6464                  (zerop children))
6465             ;; If we use expunging, and this article is really
6466             ;; low-scored, then we don't want this article.
6467             (when (and gnus-summary-expunge-below
6468                        (< (setq score
6469                                 (or (cdr (assq number gnus-newsgroup-scored))
6470                                     gnus-summary-default-score))
6471                           gnus-summary-expunge-below))
6472               ;; We increase the expunge-tally here, but that has
6473               ;; nothing to do with the limits, really.
6474               (incf gnus-newsgroup-expunged-tally)
6475               ;; We also mark as read here, if that's wanted.
6476               (when (and gnus-summary-mark-below
6477                          (< score gnus-summary-mark-below))
6478                 (setq gnus-newsgroup-unreads
6479                       (delq number gnus-newsgroup-unreads))
6480                 (if gnus-newsgroup-auto-expire
6481                     (push number gnus-newsgroup-expirable)
6482                   (push (cons number gnus-low-score-mark)
6483                         gnus-newsgroup-reads)))
6484               t)
6485             ;; Check NoCeM things.
6486             (if (and gnus-use-nocem
6487                      (gnus-nocem-unwanted-article-p
6488                       (mail-header-id (car thread))))
6489                 (progn
6490                   (setq gnus-newsgroup-unreads
6491                         (delq number gnus-newsgroup-unreads))
6492                   t))))
6493           ;; Nope, invisible article.
6494           0
6495         ;; Ok, this article is to be visible, so we add it to the limit
6496         ;; and return 1.
6497         (push number gnus-newsgroup-limit)
6498         1))))
6499
6500 (defun gnus-expunge-thread (thread)
6501   "Mark all articles in THREAD as read."
6502   (let* ((number (mail-header-number (car thread))))
6503     (incf gnus-newsgroup-expunged-tally)
6504     ;; We also mark as read here, if that's wanted.
6505     (setq gnus-newsgroup-unreads
6506           (delq number gnus-newsgroup-unreads))
6507     (if gnus-newsgroup-auto-expire
6508         (push number gnus-newsgroup-expirable)
6509       (push (cons number gnus-low-score-mark)
6510             gnus-newsgroup-reads)))
6511   ;; Go recursively through all subthreads.
6512   (mapcar 'gnus-expunge-thread (cdr thread)))
6513
6514 ;; Summary article oriented commands
6515
6516 (defun gnus-summary-refer-parent-article (n)
6517   "Refer parent article N times.
6518 If N is negative, go to ancestor -N instead.
6519 The difference between N and the number of articles fetched is returned."
6520   (interactive "p")
6521   (let ((skip 1)
6522         error header ref)
6523     (when (not (natnump n))
6524       (setq skip (abs n)
6525             n 1))
6526     (while (and (> n 0)
6527                 (not error))
6528       (setq header (gnus-summary-article-header))
6529       (if (and (eq (mail-header-number header)
6530                    (cdr gnus-article-current))
6531                (equal gnus-newsgroup-name
6532                       (car gnus-article-current)))
6533           ;; If we try to find the parent of the currently
6534           ;; displayed article, then we take a look at the actual
6535           ;; References header, since this is slightly more
6536           ;; reliable than the References field we got from the
6537           ;; server.
6538           (save-excursion
6539             (set-buffer gnus-original-article-buffer)
6540             (nnheader-narrow-to-headers)
6541             (unless (setq ref (message-fetch-field "references"))
6542               (setq ref (message-fetch-field "in-reply-to")))
6543             (widen))
6544         (setq ref
6545               ;; It's not the current article, so we take a bet on
6546               ;; the value we got from the server.
6547               (mail-header-references header)))
6548       (if (and ref
6549                (not (equal ref "")))
6550           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6551             (gnus-message 1 "Couldn't find parent"))
6552         (gnus-message 1 "No references in article %d"
6553                       (gnus-summary-article-number))
6554         (setq error t))
6555       (decf n))
6556     (gnus-summary-position-point)
6557     n))
6558
6559 (defun gnus-summary-refer-references ()
6560   "Fetch all articles mentioned in the References header.
6561 Return the number of articles fetched."
6562   (interactive)
6563   (let ((ref (mail-header-references (gnus-summary-article-header)))
6564         (current (gnus-summary-article-number))
6565         (n 0))
6566     (if (or (not ref)
6567             (equal ref ""))
6568         (error "No References in the current article")
6569       ;; For each Message-ID in the References header...
6570       (while (string-match "<[^>]*>" ref)
6571         (incf n)
6572         ;; ... fetch that article.
6573         (gnus-summary-refer-article
6574          (prog1 (match-string 0 ref)
6575            (setq ref (substring ref (match-end 0))))))
6576       (gnus-summary-goto-subject current)
6577       (gnus-summary-position-point)
6578       n)))
6579
6580 (defun gnus-summary-refer-thread (&optional limit)
6581   "Fetch all articles in the current thread.
6582 If LIMIT (the numerical prefix), fetch that many old headers instead
6583 of what's specified by the `gnus-refer-thread-limit' variable."
6584   (interactive "P")
6585   (let ((id (mail-header-id (gnus-summary-article-header)))
6586         (limit (if limit (prefix-numeric-value limit)
6587                  gnus-refer-thread-limit)))
6588     ;; We want to fetch LIMIT *old* headers, but we also have to
6589     ;; re-fetch all the headers in the current buffer, because many of
6590     ;; them may be undisplayed.  So we adjust LIMIT.
6591     (when (numberp limit)
6592       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6593     (unless (eq gnus-fetch-old-headers 'invisible)
6594       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6595       ;; Retrieve the headers and read them in.
6596       (if (eq (gnus-retrieve-headers
6597                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6598               'nov)
6599           (gnus-build-all-threads)
6600         (error "Can't fetch thread from backends that don't support NOV"))
6601       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6602     (gnus-summary-limit-include-thread id)))
6603
6604 (defun gnus-summary-refer-article (message-id &optional arg)
6605   "Fetch an article specified by MESSAGE-ID.
6606 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6607 or `gnus-select-method', no matter what backend the article comes from."
6608   (interactive "sMessage-ID: \nP")
6609   (when (and (stringp message-id)
6610              (not (zerop (length message-id))))
6611     ;; Construct the correct Message-ID if necessary.
6612     ;; Suggested by tale@pawl.rpi.edu.
6613     (unless (string-match "^<" message-id)
6614       (setq message-id (concat "<" message-id)))
6615     (unless (string-match ">$" message-id)
6616       (setq message-id (concat message-id ">")))
6617     (let* ((header (gnus-id-to-header message-id))
6618            (sparse (and header
6619                         (gnus-summary-article-sparse-p
6620                          (mail-header-number header))
6621                         (memq (mail-header-number header)
6622                               gnus-newsgroup-limit))))
6623       (cond
6624        ;; If the article is present in the buffer we just go to it.
6625        ((and header
6626              (or (not (gnus-summary-article-sparse-p
6627                        (mail-header-number header)))
6628                  sparse))
6629         (prog1
6630             (gnus-summary-goto-article
6631              (mail-header-number header) nil t)
6632           (when sparse
6633             (gnus-summary-update-article (mail-header-number header)))))
6634        (t
6635         ;; We fetch the article
6636         (let ((gnus-override-method
6637                (cond ((gnus-news-group-p gnus-newsgroup-name)
6638                       gnus-refer-article-method)
6639                      (arg
6640                       (or gnus-refer-article-method gnus-select-method))
6641                      (t nil)))
6642               number)
6643           ;; Start the special refer-article method, if necessary.
6644           (when (and gnus-refer-article-method
6645                      (gnus-news-group-p gnus-newsgroup-name))
6646             (gnus-check-server gnus-refer-article-method))
6647           ;; Fetch the header, and display the article.
6648           (if (setq number (gnus-summary-insert-subject message-id))
6649               (gnus-summary-select-article nil nil nil number)
6650             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6651
6652 (defun gnus-summary-edit-parameters ()
6653   "Edit the group parameters of the current group."
6654   (interactive)
6655   (gnus-group-edit-group gnus-newsgroup-name 'params))
6656
6657 (defun gnus-summary-customize-parameters ()
6658   "Customize the group parameters of the current group."
6659   (interactive)
6660   (gnus-group-customize gnus-newsgroup-name))
6661
6662 (defun gnus-summary-enter-digest-group (&optional force)
6663   "Enter an nndoc group based on the current article.
6664 If FORCE, force a digest interpretation.  If not, try
6665 to guess what the document format is."
6666   (interactive "P")
6667   (let ((conf gnus-current-window-configuration))
6668     (save-excursion
6669       (gnus-summary-select-article))
6670     (setq gnus-current-window-configuration conf)
6671     (let* ((name (format "%s-%d"
6672                          (gnus-group-prefixed-name
6673                           gnus-newsgroup-name (list 'nndoc ""))
6674                          (save-excursion
6675                            (set-buffer gnus-summary-buffer)
6676                            gnus-current-article)))
6677            (ogroup gnus-newsgroup-name)
6678            (params (append (gnus-info-params (gnus-get-info ogroup))
6679                            (list (cons 'to-group ogroup))
6680                            (list (cons 'save-article-group ogroup))))
6681            (case-fold-search t)
6682            (buf (current-buffer))
6683            dig)
6684       (save-excursion
6685         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6686         (insert-buffer-substring gnus-original-article-buffer)
6687         ;; Remove lines that may lead nndoc to misinterpret the
6688         ;; document type.
6689         (narrow-to-region
6690          (goto-char (point-min))
6691          (or (search-forward "\n\n" nil t) (point)))
6692         (goto-char (point-min))
6693         (delete-matching-lines "^\\(Path\\):\\|^From ")
6694         (widen))
6695       (unwind-protect
6696           (if (gnus-group-read-ephemeral-group
6697                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6698                             (nndoc-article-type
6699                              ,(if force 'digest 'guess))) t)
6700               ;; Make all postings to this group go to the parent group.
6701               (nconc (gnus-info-params (gnus-get-info name))
6702                      params)
6703             ;; Couldn't select this doc group.
6704             (switch-to-buffer buf)
6705             (gnus-set-global-variables)
6706             (gnus-configure-windows 'summary)
6707             (gnus-message 3 "Article couldn't be entered?"))
6708         (kill-buffer dig)))))
6709
6710 (defun gnus-summary-read-document (n)
6711   "Open a new group based on the current article(s).
6712 This will allow you to read digests and other similar
6713 documents as newsgroups.
6714 Obeys the standard process/prefix convention."
6715   (interactive "P")
6716   (let* ((articles (gnus-summary-work-articles n))
6717          (ogroup gnus-newsgroup-name)
6718          (params (append (gnus-info-params (gnus-get-info ogroup))
6719                          (list (cons 'to-group ogroup))))
6720          article group egroup groups vgroup)
6721     (while (setq article (pop articles))
6722       (setq group (format "%s-%d" gnus-newsgroup-name article))
6723       (gnus-summary-remove-process-mark article)
6724       (when (gnus-summary-display-article article)
6725         (save-excursion
6726           (with-temp-buffer
6727             (insert-buffer-substring gnus-original-article-buffer)
6728             ;; Remove some headers that may lead nndoc to make
6729             ;; the wrong guess.
6730             (message-narrow-to-head)
6731             (goto-char (point-min))
6732             (delete-matching-lines "^\\(Path\\):\\|^From ")
6733             (widen)
6734             (if (setq egroup
6735                       (gnus-group-read-ephemeral-group
6736                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6737                                      (nndoc-article-type guess))
6738                        t nil t))
6739                 (progn
6740                   ;; Make all postings to this group go to the parent group.
6741                   (nconc (gnus-info-params (gnus-get-info egroup))
6742                          params)
6743                   (push egroup groups))
6744               ;; Couldn't select this doc group.
6745               (gnus-error 3 "Article couldn't be entered"))))))
6746     ;; Now we have selected all the documents.
6747     (cond
6748      ((not groups)
6749       (error "None of the articles could be interpreted as documents"))
6750      ((gnus-group-read-ephemeral-group
6751        (setq vgroup (format
6752                      "nnvirtual:%s-%s" gnus-newsgroup-name
6753                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6754        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6755        t
6756        (cons (current-buffer) 'summary)))
6757      (t
6758       (error "Couldn't select virtual nndoc group")))))
6759
6760 (defun gnus-summary-isearch-article (&optional regexp-p)
6761   "Do incremental search forward on the current article.
6762 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6763   (interactive "P")
6764   (gnus-summary-select-article)
6765   (gnus-configure-windows 'article)
6766   (gnus-eval-in-buffer-window gnus-article-buffer
6767     (save-restriction
6768       (widen)
6769       (isearch-forward regexp-p))))
6770
6771 (defun gnus-summary-search-article-forward (regexp &optional backward)
6772   "Search for an article containing REGEXP forward.
6773 If BACKWARD, search backward instead."
6774   (interactive
6775    (list (read-string
6776           (format "Search article %s (regexp%s): "
6777                   (if current-prefix-arg "backward" "forward")
6778                   (if gnus-last-search-regexp
6779                       (concat ", default " gnus-last-search-regexp)
6780                     "")))
6781          current-prefix-arg))
6782   (if (string-equal regexp "")
6783       (setq regexp (or gnus-last-search-regexp ""))
6784     (setq gnus-last-search-regexp regexp))
6785   (if (gnus-summary-search-article regexp backward)
6786       (gnus-summary-show-thread)
6787     (error "Search failed: \"%s\"" regexp)))
6788
6789 (defun gnus-summary-search-article-backward (regexp)
6790   "Search for an article containing REGEXP backward."
6791   (interactive
6792    (list (read-string
6793           (format "Search article backward (regexp%s): "
6794                   (if gnus-last-search-regexp
6795                       (concat ", default " gnus-last-search-regexp)
6796                     "")))))
6797   (gnus-summary-search-article-forward regexp 'backward))
6798
6799 (defun gnus-summary-search-article (regexp &optional backward)
6800   "Search for an article containing REGEXP.
6801 Optional argument BACKWARD means do search for backward.
6802 `gnus-select-article-hook' is not called during the search."
6803   ;; We have to require this here to make sure that the following
6804   ;; dynamic binding isn't shadowed by autoloading.
6805   (require 'gnus-async)
6806   (require 'gnus-art)
6807   (let ((gnus-select-article-hook nil)  ;Disable hook.
6808         (gnus-article-display-hook nil)
6809         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6810         (gnus-use-article-prefetch nil)
6811         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6812         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6813         (sum (current-buffer))
6814         (gnus-display-mime-function nil)
6815         (found nil)
6816         point)
6817     (gnus-save-hidden-threads
6818       (gnus-summary-select-article)
6819       (set-buffer gnus-article-buffer)
6820       (when backward
6821         (forward-line -1))
6822       (while (not found)
6823         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6824         (if (if backward
6825                 (re-search-backward regexp nil t)
6826               (re-search-forward regexp nil t))
6827             ;; We found the regexp.
6828             (progn
6829               (setq found 'found)
6830               (beginning-of-line)
6831               (set-window-start
6832                (get-buffer-window (current-buffer))
6833                (point))
6834               (forward-line 1)
6835               (set-buffer sum)
6836               (setq point (point)))
6837           ;; We didn't find it, so we go to the next article.
6838           (set-buffer sum)
6839           (setq found 'not)
6840           (while (eq found 'not)
6841             (if (not (if backward (gnus-summary-find-prev)
6842                        (gnus-summary-find-next)))
6843                 ;; No more articles.
6844                 (setq found t)
6845               ;; Select the next article and adjust point.
6846               (unless (gnus-summary-article-sparse-p
6847                        (gnus-summary-article-number))
6848                 (setq found nil)
6849                 (gnus-summary-select-article)
6850                 (set-buffer gnus-article-buffer)
6851                 (widen)
6852                 (goto-char (if backward (point-max) (point-min))))))))
6853       (gnus-message 7 ""))
6854     ;; Return whether we found the regexp.
6855     (when (eq found 'found)
6856       (goto-char point)
6857       (gnus-summary-show-thread)
6858       (gnus-summary-goto-subject gnus-current-article)
6859       (gnus-summary-position-point)
6860       t)))
6861
6862 (defun gnus-summary-find-matching (header regexp &optional backward unread
6863                                           not-case-fold)
6864   "Return a list of all articles that match REGEXP on HEADER.
6865 The search stars on the current article and goes forwards unless
6866 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6867 If UNREAD is non-nil, only unread articles will
6868 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6869 in the comparisons."
6870   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6871                 (gnus-data-find-list
6872                  (gnus-summary-article-number) (gnus-data-list backward))))
6873         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6874         (case-fold-search (not not-case-fold))
6875         articles d)
6876     (unless (fboundp (intern (concat "mail-header-" header)))
6877       (error "%s is not a valid header" header))
6878     (while data
6879       (setq d (car data))
6880       (and (or (not unread)             ; We want all articles...
6881                (gnus-data-unread-p d))  ; Or just unreads.
6882            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6883            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6884            (push (gnus-data-number d) articles)) ; Success!
6885       (setq data (cdr data)))
6886     (nreverse articles)))
6887
6888 (defun gnus-summary-execute-command (header regexp command &optional backward)
6889   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6890 If HEADER is an empty string (or nil), the match is done on the entire
6891 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6892   (interactive
6893    (list (let ((completion-ignore-case t))
6894            (completing-read
6895             "Header name: "
6896             (mapcar (lambda (string) (list string))
6897                     '("Number" "Subject" "From" "Lines" "Date"
6898                       "Message-ID" "Xref" "References" "Body"))
6899             nil 'require-match))
6900          (read-string "Regexp: ")
6901          (read-key-sequence "Command: ")
6902          current-prefix-arg))
6903   (when (equal header "Body")
6904     (setq header ""))
6905   ;; Hidden thread subtrees must be searched as well.
6906   (gnus-summary-show-all-threads)
6907   ;; We don't want to change current point nor window configuration.
6908   (save-excursion
6909     (save-window-excursion
6910       (gnus-message 6 "Executing %s..." (key-description command))
6911       ;; We'd like to execute COMMAND interactively so as to give arguments.
6912       (gnus-execute header regexp
6913                     `(call-interactively ',(key-binding command))
6914                     backward)
6915       (gnus-message 6 "Executing %s...done" (key-description command)))))
6916
6917 (defun gnus-summary-beginning-of-article ()
6918   "Scroll the article back to the beginning."
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-min))
6925     (when gnus-page-broken
6926       (gnus-narrow-to-page))))
6927
6928 (defun gnus-summary-end-of-article ()
6929   "Scroll to the end of the article."
6930   (interactive)
6931   (gnus-summary-select-article)
6932   (gnus-configure-windows 'article)
6933   (gnus-eval-in-buffer-window gnus-article-buffer
6934     (widen)
6935     (goto-char (point-max))
6936     (recenter -3)
6937     (when gnus-page-broken
6938       (gnus-narrow-to-page))))
6939
6940 (defun gnus-summary-print-article (&optional filename n)
6941   "Generate and print a PostScript image of the N next (mail) articles.
6942
6943 If N is negative, print the N previous articles.  If N is nil and articles
6944 have been marked with the process mark, print these instead.
6945
6946 If the optional second argument FILENAME is nil, send the image to the
6947 printer.  If FILENAME is a string, save the PostScript image in a file with
6948 that name.  If FILENAME is a number, prompt the user for the name of the file
6949 to save in."
6950   (interactive (list (ps-print-preprint current-prefix-arg)
6951                      current-prefix-arg))
6952   (dolist (article (gnus-summary-work-articles n))
6953     (gnus-summary-select-article nil nil 'pseudo article)
6954     (gnus-eval-in-buffer-window gnus-article-buffer
6955       (let ((buffer (generate-new-buffer " *print*")))
6956         (unwind-protect
6957             (progn
6958               (copy-to-buffer buffer (point-min) (point-max))
6959               (set-buffer buffer)
6960               (gnus-article-delete-invisible-text)
6961               (let ((ps-left-header
6962                      (list
6963                       (concat "("
6964                               (mail-header-subject gnus-current-headers) ")")
6965                       (concat "("
6966                               (mail-header-from gnus-current-headers) ")")))
6967                     (ps-right-header
6968                      (list
6969                       "/pagenumberstring load"
6970                       (concat "("
6971                               (mail-header-date gnus-current-headers) ")"))))
6972                 (gnus-run-hooks 'gnus-ps-print-hook)
6973                 (save-excursion
6974                   (ps-print-buffer-with-faces filename))))
6975           (kill-buffer buffer))))))
6976
6977 (defun gnus-summary-show-article (&optional arg)
6978   "Force re-fetching of the current article.
6979 If ARG (the prefix) is non-nil, show the raw article without any
6980 article massaging functions being run."
6981   (interactive "P")
6982   (if (not arg)
6983       ;; Select the article the normal way.
6984       (gnus-summary-select-article nil 'force)
6985     ;; We have to require this here to make sure that the following
6986     ;; dynamic binding isn't shadowed by autoloading.
6987     (require 'gnus-async)
6988     (require 'gnus-art)
6989     ;; Bind the article treatment functions to nil.
6990     (let ((gnus-have-all-headers t)
6991           gnus-article-display-hook
6992           gnus-article-prepare-hook
6993           gnus-article-decode-hook
6994           gnus-display-mime-function
6995           gnus-break-pages
6996           gnus-visual)
6997       (gnus-summary-select-article nil 'force)))
6998   (gnus-summary-goto-subject gnus-current-article)
6999   (gnus-summary-position-point))
7000
7001 (defun gnus-summary-verbose-headers (&optional arg)
7002   "Toggle permanent full header display.
7003 If ARG is a positive number, turn header display on.
7004 If ARG is a negative number, turn header display off."
7005   (interactive "P")
7006   (setq gnus-show-all-headers
7007         (cond ((or (not (numberp arg))
7008                    (zerop arg))
7009                (not gnus-show-all-headers))
7010               ((natnump arg)
7011                t)))
7012   (gnus-summary-show-article))
7013
7014 (defun gnus-summary-toggle-header (&optional arg)
7015   "Show the headers if they are hidden, or hide them if they are shown.
7016 If ARG is a positive number, show the entire header.
7017 If ARG is a negative number, hide the unwanted header lines."
7018   (interactive "P")
7019   (save-excursion
7020     (set-buffer gnus-article-buffer)
7021     (let* ((buffer-read-only nil)
7022            (inhibit-point-motion-hooks t)
7023            (hidden (text-property-any
7024                     (goto-char (point-min)) (search-forward "\n\n")
7025                     'invisible t))
7026            e)
7027       (goto-char (point-min))
7028       (when (search-forward "\n\n" nil t)
7029         (delete-region (point-min) (1- (point))))
7030       (goto-char (point-min))
7031       (save-excursion
7032         (set-buffer gnus-original-article-buffer)
7033         (goto-char (point-min))
7034         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7035       (insert-buffer-substring gnus-original-article-buffer 1 e)
7036       (let ((article-inhibit-hiding t))
7037         (gnus-run-hooks 'gnus-article-display-hook))
7038       (when (or (not hidden) (and (numberp arg) (< arg 0)))
7039         (gnus-article-hide-headers)))))
7040
7041 (defun gnus-summary-show-all-headers ()
7042   "Make all header lines visible."
7043   (interactive)
7044   (gnus-article-show-all-headers))
7045
7046 (defun gnus-summary-caesar-message (&optional arg)
7047   "Caesar rotate the current article by 13.
7048 The numerical prefix specifies how many places to rotate each letter
7049 forward."
7050   (interactive "P")
7051   (gnus-summary-select-article)
7052   (let ((mail-header-separator ""))
7053     (gnus-eval-in-buffer-window gnus-article-buffer
7054       (save-restriction
7055         (widen)
7056         (let ((start (window-start))
7057               buffer-read-only)
7058           (message-caesar-buffer-body arg)
7059           (set-window-start (get-buffer-window (current-buffer)) start))))))
7060
7061 (defun gnus-summary-stop-page-breaking ()
7062   "Stop page breaking in the current article."
7063   (interactive)
7064   (gnus-summary-select-article)
7065   (gnus-eval-in-buffer-window gnus-article-buffer
7066     (widen)
7067     (when (gnus-visual-p 'page-marker)
7068       (let ((buffer-read-only nil))
7069         (gnus-remove-text-with-property 'gnus-prev)
7070         (gnus-remove-text-with-property 'gnus-next))
7071       (setq gnus-page-broken nil))))
7072
7073 (defun gnus-summary-move-article (&optional n to-newsgroup
7074                                             select-method action)
7075   "Move the current article to a different newsgroup.
7076 If N is a positive number, move the N next articles.
7077 If N is a negative number, move the N previous articles.
7078 If N is nil and any articles have been marked with the process mark,
7079 move those articles instead.
7080 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7081 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7082 re-spool using this method.
7083
7084 For this function to work, both the current newsgroup and the
7085 newsgroup that you want to move to have to support the `request-move'
7086 and `request-accept' functions."
7087   (interactive "P")
7088   (unless action
7089     (setq action 'move))
7090   ;; Disable marking as read.
7091   (let (gnus-mark-article-hook)
7092     (save-window-excursion
7093       (gnus-summary-select-article)))
7094   ;; Check whether the source group supports the required functions.
7095   (cond ((and (eq action 'move)
7096               (not (gnus-check-backend-function
7097                     'request-move-article gnus-newsgroup-name)))
7098          (error "The current group does not support article moving"))
7099         ((and (eq action 'crosspost)
7100               (not (gnus-check-backend-function
7101                     'request-replace-article gnus-newsgroup-name)))
7102          (error "The current group does not support article editing")))
7103   (let ((articles (gnus-summary-work-articles n))
7104         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7105         (names '((move "Move" "Moving")
7106                  (copy "Copy" "Copying")
7107                  (crosspost "Crosspost" "Crossposting")))
7108         (copy-buf (save-excursion
7109                     (nnheader-set-temp-buffer " *copy article*")))
7110         art-group to-method new-xref article to-groups)
7111     (unless (assq action names)
7112       (error "Unknown action %s" action))
7113     ;; Read the newsgroup name.
7114     (when (and (not to-newsgroup)
7115                (not select-method))
7116       (setq to-newsgroup
7117             (gnus-read-move-group-name
7118              (cadr (assq action names))
7119              (symbol-value (intern (format "gnus-current-%s-group" action)))
7120              articles prefix))
7121       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7122     (setq to-method (or select-method
7123                         (gnus-group-name-to-method to-newsgroup)))
7124     ;; Check the method we are to move this article to...
7125     (unless (gnus-check-backend-function
7126              'request-accept-article (car to-method))
7127       (error "%s does not support article copying" (car to-method)))
7128     (unless (gnus-check-server to-method)
7129       (error "Can't open server %s" (car to-method)))
7130     (gnus-message 6 "%s to %s: %s..."
7131                   (caddr (assq action names))
7132                   (or (car select-method) to-newsgroup) articles)
7133     (while articles
7134       (setq article (pop articles))
7135       (setq
7136        art-group
7137        (cond
7138         ;; Move the article.
7139         ((eq action 'move)
7140          ;; Remove this article from future suppression.
7141          (gnus-dup-unsuppress-article article)
7142          (gnus-request-move-article
7143           article                       ; Article to move
7144           gnus-newsgroup-name           ; From newsgroup
7145           (nth 1 (gnus-find-method-for-group
7146                   gnus-newsgroup-name)) ; Server
7147           (list 'gnus-request-accept-article
7148                 to-newsgroup (list 'quote select-method)
7149                 (not articles) t)               ; Accept form
7150           (not articles)))              ; Only save nov last time
7151         ;; Copy the article.
7152         ((eq action 'copy)
7153          (save-excursion
7154            (set-buffer copy-buf)
7155            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7156              (gnus-request-accept-article
7157               to-newsgroup select-method (not articles) t))))
7158         ;; Crosspost the article.
7159         ((eq action 'crosspost)
7160          (let ((xref (message-tokenize-header
7161                       (mail-header-xref (gnus-summary-article-header article))
7162                       " ")))
7163            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7164                                   ":" article))
7165            (unless xref
7166              (setq xref (list (system-name))))
7167            (setq new-xref
7168                  (concat
7169                   (mapconcat 'identity
7170                              (delete "Xref:" (delete new-xref xref))
7171                              " ")
7172                   " " new-xref))
7173            (save-excursion
7174              (set-buffer copy-buf)
7175              ;; First put the article in the destination group.
7176              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7177              (when (consp (setq art-group
7178                                 (gnus-request-accept-article
7179                                  to-newsgroup select-method (not articles))))
7180                (setq new-xref (concat new-xref " " (car art-group)
7181                                       ":" (cdr art-group)))
7182                ;; Now we have the new Xrefs header, so we insert
7183                ;; it and replace the new article.
7184                (nnheader-replace-header "Xref" new-xref)
7185                (gnus-request-replace-article
7186                 (cdr art-group) to-newsgroup (current-buffer))
7187                art-group))))))
7188       (cond
7189        ((not art-group)
7190         (gnus-message 1 "Couldn't %s article %s"
7191                       (cadr (assq action names)) article))
7192        ((and (eq art-group 'junk)
7193              (eq action 'move))
7194         (gnus-summary-mark-article article gnus-canceled-mark)
7195         (gnus-message 4 "Deleted article %s" article))
7196        (t
7197         (let* ((pto-group (gnus-group-prefixed-name
7198                            (car art-group) to-method))
7199                (entry
7200                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7201                (info (nth 2 entry))
7202                (to-group (gnus-info-group info)))
7203           ;; Update the group that has been moved to.
7204           (when (and info
7205                      (memq action '(move copy)))
7206             (unless (member to-group to-groups)
7207               (push to-group to-groups))
7208
7209             (unless (memq article gnus-newsgroup-unreads)
7210               (gnus-info-set-read
7211                info (gnus-add-to-range (gnus-info-read info)
7212                                        (list (cdr art-group)))))
7213
7214             ;; Copy any marks over to the new group.
7215             (let ((marks gnus-article-mark-lists)
7216                   (to-article (cdr art-group)))
7217
7218               ;; See whether the article is to be put in the cache.
7219               (when gnus-use-cache
7220                 (gnus-cache-possibly-enter-article
7221                  to-group to-article
7222                  (let ((header (copy-sequence
7223                                 (gnus-summary-article-header article))))
7224                    (mail-header-set-number header to-article)
7225                    header)
7226                  (memq article gnus-newsgroup-marked)
7227                  (memq article gnus-newsgroup-dormant)
7228                  (memq article gnus-newsgroup-unreads)))
7229
7230               (when (and (equal to-group gnus-newsgroup-name)
7231                          (not (memq article gnus-newsgroup-unreads)))
7232                 ;; Mark this article as read in this group.
7233                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7234                 (setcdr (gnus-active to-group) to-article)
7235                 (setcdr gnus-newsgroup-active to-article))
7236
7237               (while marks
7238                 (when (memq article (symbol-value
7239                                      (intern (format "gnus-newsgroup-%s"
7240                                                      (caar marks)))))
7241                   ;; If the other group is the same as this group,
7242                   ;; then we have to add the mark to the list.
7243                   (when (equal to-group gnus-newsgroup-name)
7244                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7245                          (cons to-article
7246                                (symbol-value
7247                                 (intern (format "gnus-newsgroup-%s"
7248                                                 (caar marks)))))))
7249                   ;; Copy the marks to other group.
7250                   (gnus-add-marked-articles
7251                    to-group (cdar marks) (list to-article) info))
7252                 (setq marks (cdr marks)))
7253
7254               (gnus-dribble-enter
7255                (concat "(gnus-group-set-info '"
7256                        (gnus-prin1-to-string (gnus-get-info to-group))
7257                        ")"))))
7258
7259           ;; Update the Xref header in this article to point to
7260           ;; the new crossposted article we have just created.
7261           (when (eq action 'crosspost)
7262             (save-excursion
7263               (set-buffer copy-buf)
7264               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7265               (nnheader-replace-header "Xref" new-xref)
7266               (gnus-request-replace-article
7267                article gnus-newsgroup-name (current-buffer)))))
7268
7269         ;;;!!!Why is this necessary?
7270         (set-buffer gnus-summary-buffer)
7271
7272         (gnus-summary-goto-subject article)
7273         (when (eq action 'move)
7274           (gnus-summary-mark-article article gnus-canceled-mark))))
7275       (gnus-summary-remove-process-mark article))
7276     ;; Re-activate all groups that have been moved to.
7277     (while to-groups
7278       (save-excursion
7279         (set-buffer gnus-group-buffer)
7280         (when (gnus-group-goto-group (car to-groups) t)
7281           (gnus-group-get-new-news-this-group 1 t))
7282         (pop to-groups)))
7283
7284     (gnus-kill-buffer copy-buf)
7285     (gnus-summary-position-point)
7286     (gnus-set-mode-line 'summary)))
7287
7288 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7289   "Move the current article to a different newsgroup.
7290 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7291 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7292 re-spool using this method."
7293   (interactive "P")
7294   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7295
7296 (defun gnus-summary-crosspost-article (&optional n)
7297   "Crosspost the current article to some other group."
7298   (interactive "P")
7299   (gnus-summary-move-article n nil nil 'crosspost))
7300
7301 (defcustom gnus-summary-respool-default-method nil
7302   "Default method for respooling an article.
7303 If nil, use to the current newsgroup method."
7304   :type '(choice (gnus-select-method :value (nnml ""))
7305                  (const nil))
7306   :group 'gnus-summary-mail)
7307
7308 (defun gnus-summary-respool-article (&optional n method)
7309   "Respool the current article.
7310 The article will be squeezed through the mail spooling process again,
7311 which means that it will be put in some mail newsgroup or other
7312 depending on `nnmail-split-methods'.
7313 If N is a positive number, respool the N next articles.
7314 If N is a negative number, respool the N previous articles.
7315 If N is nil and any articles have been marked with the process mark,
7316 respool those articles instead.
7317
7318 Respooling can be done both from mail groups and \"real\" newsgroups.
7319 In the former case, the articles in question will be moved from the
7320 current group into whatever groups they are destined to.  In the
7321 latter case, they will be copied into the relevant groups."
7322   (interactive
7323    (list current-prefix-arg
7324          (let* ((methods (gnus-methods-using 'respool))
7325                 (methname
7326                  (symbol-name (or gnus-summary-respool-default-method
7327                                   (car (gnus-find-method-for-group
7328                                         gnus-newsgroup-name)))))
7329                 (method
7330                  (gnus-completing-read
7331                   methname "What backend do you want to use when respooling?"
7332                   methods nil t nil 'gnus-mail-method-history))
7333                 ms)
7334            (cond
7335             ((zerop (length (setq ms (gnus-servers-using-backend
7336                                       (intern method)))))
7337              (list (intern method) ""))
7338             ((= 1 (length ms))
7339              (car ms))
7340             (t
7341              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7342                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7343                            ms-alist))))))))
7344   (unless method
7345     (error "No method given for respooling"))
7346   (if (assoc (symbol-name
7347               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7348              (gnus-methods-using 'respool))
7349       (gnus-summary-move-article n nil method)
7350     (gnus-summary-copy-article n nil method)))
7351
7352 (defun gnus-summary-import-article (file)
7353   "Import an arbitrary file into a mail newsgroup."
7354   (interactive "fImport file: ")
7355   (let ((group gnus-newsgroup-name)
7356         (now (current-time))
7357         atts lines)
7358     (unless (gnus-check-backend-function 'request-accept-article group)
7359       (error "%s does not support article importing" group))
7360     (or (file-readable-p file)
7361         (not (file-regular-p file))
7362         (error "Can't read %s" file))
7363     (save-excursion
7364       (set-buffer (gnus-get-buffer-create " *import file*"))
7365       (erase-buffer)
7366       (insert-file-contents file)
7367       (goto-char (point-min))
7368       (unless (nnheader-article-p)
7369         ;; This doesn't look like an article, so we fudge some headers.
7370         (setq atts (file-attributes file)
7371               lines (count-lines (point-min) (point-max)))
7372         (insert "From: " (read-string "From: ") "\n"
7373                 "Subject: " (read-string "Subject: ") "\n"
7374                 "Date: " (message-make-date (nth 5 atts))
7375                 "\n"
7376                 "Message-ID: " (message-make-message-id) "\n"
7377                 "Lines: " (int-to-string lines) "\n"
7378                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7379       (gnus-request-accept-article group nil t)
7380       (kill-buffer (current-buffer)))))
7381
7382 (defun gnus-summary-article-posted-p ()
7383   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7384 This will be the case if the article has both been mailed and posted."
7385   (interactive)
7386   (let ((id (mail-header-references (gnus-summary-article-header)))
7387         (gnus-override-method
7388          (or gnus-refer-article-method gnus-select-method)))
7389     (if (gnus-request-head id "")
7390         (gnus-message 2 "The current message was found on %s"
7391                       gnus-override-method)
7392       (gnus-message 2 "The current message couldn't be found on %s"
7393                     gnus-override-method)
7394       nil)))
7395
7396 (defun gnus-summary-expire-articles (&optional now)
7397   "Expire all articles that are marked as expirable in the current group."
7398   (interactive)
7399   (when (gnus-check-backend-function
7400          'request-expire-articles gnus-newsgroup-name)
7401     ;; This backend supports expiry.
7402     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7403            (expirable (if total
7404                           (progn
7405                             ;; We need to update the info for
7406                             ;; this group for `gnus-list-of-read-articles'
7407                             ;; to give us the right answer.
7408                             (gnus-run-hooks 'gnus-exit-group-hook)
7409                             (gnus-summary-update-info)
7410                             (gnus-list-of-read-articles gnus-newsgroup-name))
7411                         (setq gnus-newsgroup-expirable
7412                               (sort gnus-newsgroup-expirable '<))))
7413            (expiry-wait (if now 'immediate
7414                           (gnus-group-find-parameter
7415                            gnus-newsgroup-name 'expiry-wait)))
7416            es)
7417       (when expirable
7418         ;; There are expirable articles in this group, so we run them
7419         ;; through the expiry process.
7420         (gnus-message 6 "Expiring articles...")
7421         ;; The list of articles that weren't expired is returned.
7422         (save-excursion
7423           (if expiry-wait
7424               (let ((nnmail-expiry-wait-function nil)
7425                     (nnmail-expiry-wait expiry-wait))
7426                 (setq es (gnus-request-expire-articles
7427                           expirable gnus-newsgroup-name)))
7428             (setq es (gnus-request-expire-articles
7429                       expirable gnus-newsgroup-name))))
7430         (unless total
7431           (setq gnus-newsgroup-expirable es))
7432         ;; We go through the old list of expirable, and mark all
7433         ;; really expired articles as nonexistent.
7434         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7435           (let ((gnus-use-cache nil))
7436             (while expirable
7437               (unless (memq (car expirable) es)
7438                 (when (gnus-data-find (car expirable))
7439                   (gnus-summary-mark-article
7440                    (car expirable) gnus-canceled-mark)))
7441               (setq expirable (cdr expirable)))))
7442         (gnus-message 6 "Expiring articles...done")))))
7443
7444 (defun gnus-summary-expire-articles-now ()
7445   "Expunge all expirable articles in the current group.
7446 This means that *all* articles that are marked as expirable will be
7447 deleted forever, right now."
7448   (interactive)
7449   (or gnus-expert-user
7450       (gnus-yes-or-no-p
7451        "Are you really, really, really sure you want to delete all these messages? ")
7452       (error "Phew!"))
7453   (gnus-summary-expire-articles t))
7454
7455 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7456 (defun gnus-summary-delete-article (&optional n)
7457   "Delete the N next (mail) articles.
7458 This command actually deletes articles.  This is not a marking
7459 command.  The article will disappear forever from your life, never to
7460 return.
7461 If N is negative, delete backwards.
7462 If N is nil and articles have been marked with the process mark,
7463 delete these instead."
7464   (interactive "P")
7465   (unless (gnus-check-backend-function 'request-expire-articles
7466                                        gnus-newsgroup-name)
7467     (error "The current newsgroup does not support article deletion"))
7468   ;; Compute the list of articles to delete.
7469   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7470         not-deleted)
7471     (if (and gnus-novice-user
7472              (not (gnus-yes-or-no-p
7473                    (format "Do you really want to delete %s forever? "
7474                            (if (> (length articles) 1)
7475                                (format "these %s articles" (length articles))
7476                              "this article")))))
7477         ()
7478       ;; Delete the articles.
7479       (setq not-deleted (gnus-request-expire-articles
7480                          articles gnus-newsgroup-name 'force))
7481       (while articles
7482         (gnus-summary-remove-process-mark (car articles))
7483         ;; The backend might not have been able to delete the article
7484         ;; after all.
7485         (unless (memq (car articles) not-deleted)
7486           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7487         (setq articles (cdr articles)))
7488       (when not-deleted
7489         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7490     (gnus-summary-position-point)
7491     (gnus-set-mode-line 'summary)
7492     not-deleted))
7493
7494 (defun gnus-summary-edit-article (&optional force)
7495   "Edit the current article.
7496 This will have permanent effect only in mail groups.
7497 If FORCE is non-nil, allow editing of articles even in read-only
7498 groups."
7499   (interactive "P")
7500   (save-excursion
7501     (set-buffer gnus-summary-buffer)
7502     (gnus-set-global-variables)
7503     (when (and (not force)
7504                (gnus-group-read-only-p))
7505       (error "The current newsgroup does not support article editing"))
7506     ;; Select article if needed.
7507     (unless (eq (gnus-summary-article-number)
7508                 gnus-current-article)
7509       (gnus-summary-select-article t))
7510     (gnus-article-date-original)
7511     (gnus-article-edit-article
7512      `(lambda (no-highlight)
7513         (gnus-summary-edit-article-done
7514          ,(or (mail-header-references gnus-current-headers) "")
7515          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7516
7517 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7518
7519 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7520                                                  no-highlight)
7521   "Make edits to the current article permanent."
7522   (interactive)
7523   ;; Replace the article.
7524   (let ((buf (current-buffer)))
7525     (with-temp-buffer
7526       (insert-buffer buf)
7527       (if (and (not read-only)
7528                (not (gnus-request-replace-article
7529                      (cdr gnus-article-current) (car gnus-article-current)
7530                      (current-buffer)
7531                      (not gnus-article-decoded-p))))
7532           (error "Couldn't replace article")
7533         ;; Update the summary buffer.
7534         (if (and references
7535                  (equal (message-tokenize-header references " ")
7536                         (message-tokenize-header
7537                          (or (message-fetch-field "references") "") " ")))
7538             ;; We only have to update this line.
7539             (save-excursion
7540               (save-restriction
7541                 (message-narrow-to-head)
7542                 (let ((head (buffer-string))
7543                       header)
7544                   (with-temp-buffer
7545                     (insert (format "211 %d Article retrieved.\n"
7546                                     (cdr gnus-article-current)))
7547                     (insert head)
7548                     (insert ".\n")
7549                     (let ((nntp-server-buffer (current-buffer)))
7550                       (setq header (car (gnus-get-newsgroup-headers
7551                                          (save-excursion
7552                                            (set-buffer gnus-summary-buffer)
7553                                            gnus-newsgroup-dependencies)
7554                                          t))))
7555                     (save-excursion
7556                       (set-buffer gnus-summary-buffer)
7557                       (gnus-data-set-header
7558                        (gnus-data-find (cdr gnus-article-current))
7559                        header)
7560                       (gnus-summary-update-article-line
7561                        (cdr gnus-article-current) header))))))
7562           ;; Update threads.
7563           (set-buffer (or buffer gnus-summary-buffer))
7564           (gnus-summary-update-article (cdr gnus-article-current)))
7565         ;; Prettify the article buffer again.
7566         (unless no-highlight
7567           (save-excursion
7568             (set-buffer gnus-article-buffer)
7569             (gnus-run-hooks 'gnus-article-display-hook)
7570             (set-buffer gnus-original-article-buffer)
7571             (gnus-request-article
7572              (cdr gnus-article-current)
7573              (car gnus-article-current) (current-buffer))))
7574         ;; Prettify the summary buffer line.
7575         (when (gnus-visual-p 'summary-highlight 'highlight)
7576           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7577
7578 (defun gnus-summary-edit-wash (key)
7579   "Perform editing command KEY in the article buffer."
7580   (interactive
7581    (list
7582     (progn
7583       (message "%s" (concat (this-command-keys) "- "))
7584       (read-char))))
7585   (message "")
7586   (gnus-summary-edit-article)
7587   (execute-kbd-macro (concat (this-command-keys) key))
7588   (gnus-article-edit-done))
7589
7590 ;;; Respooling
7591
7592 (defun gnus-summary-respool-query (&optional silent trace)
7593   "Query where the respool algorithm would put this article."
7594   (interactive)
7595   (let (gnus-mark-article-hook)
7596     (gnus-summary-select-article)
7597     (save-excursion
7598       (set-buffer gnus-original-article-buffer)
7599       (save-restriction
7600         (message-narrow-to-head)
7601         (let ((groups (nnmail-article-group 'identity trace)))
7602           (unless silent
7603             (if groups
7604                 (message "This message would go to %s"
7605                          (mapconcat 'car groups ", "))
7606               (message "This message would go to no groups"))
7607             groups))))))
7608
7609 (defun gnus-summary-respool-trace ()
7610   "Trace where the respool algorithm would put this article.
7611 Display a buffer showing all fancy splitting patterns which matched."
7612   (interactive)
7613   (gnus-summary-respool-query nil t))
7614
7615 ;; Summary marking commands.
7616
7617 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7618   "Mark articles which has the same subject as read, and then select the next.
7619 If UNMARK is positive, remove any kind of mark.
7620 If UNMARK is negative, tick articles."
7621   (interactive "P")
7622   (when unmark
7623     (setq unmark (prefix-numeric-value unmark)))
7624   (let ((count
7625          (gnus-summary-mark-same-subject
7626           (gnus-summary-article-subject) unmark)))
7627     ;; Select next unread article.  If auto-select-same mode, should
7628     ;; select the first unread article.
7629     (gnus-summary-next-article t (and gnus-auto-select-same
7630                                       (gnus-summary-article-subject)))
7631     (gnus-message 7 "%d article%s marked as %s"
7632                   count (if (= count 1) " is" "s are")
7633                   (if unmark "unread" "read"))))
7634
7635 (defun gnus-summary-kill-same-subject (&optional unmark)
7636   "Mark articles which has the same subject as read.
7637 If UNMARK is positive, remove any kind of mark.
7638 If UNMARK is negative, tick articles."
7639   (interactive "P")
7640   (when unmark
7641     (setq unmark (prefix-numeric-value unmark)))
7642   (let ((count
7643          (gnus-summary-mark-same-subject
7644           (gnus-summary-article-subject) unmark)))
7645     ;; If marked as read, go to next unread subject.
7646     (when (null unmark)
7647       ;; Go to next unread subject.
7648       (gnus-summary-next-subject 1 t))
7649     (gnus-message 7 "%d articles are marked as %s"
7650                   count (if unmark "unread" "read"))))
7651
7652 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7653   "Mark articles with same SUBJECT as read, and return marked number.
7654 If optional argument UNMARK is positive, remove any kinds of marks.
7655 If optional argument UNMARK is negative, mark articles as unread instead."
7656   (let ((count 1))
7657     (save-excursion
7658       (cond
7659        ((null unmark)                   ; Mark as read.
7660         (while (and
7661                 (progn
7662                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7663                   (gnus-summary-show-thread) t)
7664                 (gnus-summary-find-subject subject))
7665           (setq count (1+ count))))
7666        ((> unmark 0)                    ; Tick.
7667         (while (and
7668                 (progn
7669                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7670                   (gnus-summary-show-thread) t)
7671                 (gnus-summary-find-subject subject))
7672           (setq count (1+ count))))
7673        (t                               ; Mark as unread.
7674         (while (and
7675                 (progn
7676                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7677                   (gnus-summary-show-thread) t)
7678                 (gnus-summary-find-subject subject))
7679           (setq count (1+ count)))))
7680       (gnus-set-mode-line 'summary)
7681       ;; Return the number of marked articles.
7682       count)))
7683
7684 (defun gnus-summary-mark-as-processable (n &optional unmark)
7685   "Set the process mark on the next N articles.
7686 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7687 the process mark instead.  The difference between N and the actual
7688 number of articles marked is returned."
7689   (interactive "p")
7690   (let ((backward (< n 0))
7691         (n (abs n)))
7692     (while (and
7693             (> n 0)
7694             (if unmark
7695                 (gnus-summary-remove-process-mark
7696                  (gnus-summary-article-number))
7697               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7698             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7699       (setq n (1- n)))
7700     (when (/= 0 n)
7701       (gnus-message 7 "No more articles"))
7702     (gnus-summary-recenter)
7703     (gnus-summary-position-point)
7704     n))
7705
7706 (defun gnus-summary-unmark-as-processable (n)
7707   "Remove the process mark from the next N articles.
7708 If N is negative, unmark backward instead.  The difference between N and
7709 the actual number of articles unmarked is returned."
7710   (interactive "p")
7711   (gnus-summary-mark-as-processable n t))
7712
7713 (defun gnus-summary-unmark-all-processable ()
7714   "Remove the process mark from all articles."
7715   (interactive)
7716   (save-excursion
7717     (while gnus-newsgroup-processable
7718       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7719   (gnus-summary-position-point))
7720
7721 (defun gnus-summary-mark-as-expirable (n)
7722   "Mark N articles forward as expirable.
7723 If N is negative, mark backward instead.  The difference between N and
7724 the actual number of articles marked is returned."
7725   (interactive "p")
7726   (gnus-summary-mark-forward n gnus-expirable-mark))
7727
7728 (defun gnus-summary-mark-article-as-replied (article)
7729   "Mark ARTICLE replied and update the summary line."
7730   (push article gnus-newsgroup-replied)
7731   (let ((buffer-read-only nil))
7732     (when (gnus-summary-goto-subject article nil t)
7733       (gnus-summary-update-secondary-mark article))))
7734
7735 (defun gnus-summary-set-bookmark (article)
7736   "Set a bookmark in current article."
7737   (interactive (list (gnus-summary-article-number)))
7738   (when (or (not (get-buffer gnus-article-buffer))
7739             (not gnus-current-article)
7740             (not gnus-article-current)
7741             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7742     (error "No current article selected"))
7743   ;; Remove old bookmark, if one exists.
7744   (let ((old (assq article gnus-newsgroup-bookmarks)))
7745     (when old
7746       (setq gnus-newsgroup-bookmarks
7747             (delq old gnus-newsgroup-bookmarks))))
7748   ;; Set the new bookmark, which is on the form
7749   ;; (article-number . line-number-in-body).
7750   (push
7751    (cons article
7752          (save-excursion
7753            (set-buffer gnus-article-buffer)
7754            (count-lines
7755             (min (point)
7756                  (save-excursion
7757                    (goto-char (point-min))
7758                    (search-forward "\n\n" nil t)
7759                    (point)))
7760             (point))))
7761    gnus-newsgroup-bookmarks)
7762   (gnus-message 6 "A bookmark has been added to the current article."))
7763
7764 (defun gnus-summary-remove-bookmark (article)
7765   "Remove the bookmark from the current article."
7766   (interactive (list (gnus-summary-article-number)))
7767   ;; Remove old bookmark, if one exists.
7768   (let ((old (assq article gnus-newsgroup-bookmarks)))
7769     (if old
7770         (progn
7771           (setq gnus-newsgroup-bookmarks
7772                 (delq old gnus-newsgroup-bookmarks))
7773           (gnus-message 6 "Removed bookmark."))
7774       (gnus-message 6 "No bookmark in current article."))))
7775
7776 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7777 (defun gnus-summary-mark-as-dormant (n)
7778   "Mark N articles forward as dormant.
7779 If N is negative, mark backward instead.  The difference between N and
7780 the actual number of articles marked is returned."
7781   (interactive "p")
7782   (gnus-summary-mark-forward n gnus-dormant-mark))
7783
7784 (defun gnus-summary-set-process-mark (article)
7785   "Set the process mark on ARTICLE and update the summary line."
7786   (setq gnus-newsgroup-processable
7787         (cons article
7788               (delq article gnus-newsgroup-processable)))
7789   (when (gnus-summary-goto-subject article)
7790     (gnus-summary-show-thread)
7791     (gnus-summary-goto-subject article)
7792     (gnus-summary-update-secondary-mark article)))
7793
7794 (defun gnus-summary-remove-process-mark (article)
7795   "Remove the process mark from ARTICLE and update the summary line."
7796   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7797   (when (gnus-summary-goto-subject article)
7798     (gnus-summary-show-thread)
7799     (gnus-summary-goto-subject article)
7800     (gnus-summary-update-secondary-mark article)))
7801
7802 (defun gnus-summary-set-saved-mark (article)
7803   "Set the process mark on ARTICLE and update the summary line."
7804   (push article gnus-newsgroup-saved)
7805   (when (gnus-summary-goto-subject article)
7806     (gnus-summary-update-secondary-mark article)))
7807
7808 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7809   "Mark N articles as read forwards.
7810 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7811 The difference between N and the actual number of articles marked is
7812 returned."
7813   (interactive "p")
7814   (gnus-summary-show-thread)
7815   (let ((backward (< n 0))
7816         (gnus-summary-goto-unread
7817          (and gnus-summary-goto-unread
7818               (not (eq gnus-summary-goto-unread 'never))
7819               (not (memq mark (list gnus-unread-mark
7820                                     gnus-ticked-mark gnus-dormant-mark)))))
7821         (n (abs n))
7822         (mark (or mark gnus-del-mark)))
7823     (while (and (> n 0)
7824                 (gnus-summary-mark-article nil mark no-expire)
7825                 (zerop (gnus-summary-next-subject
7826                         (if backward -1 1)
7827                         (and gnus-summary-goto-unread
7828                              (not (eq gnus-summary-goto-unread 'never)))
7829                         t)))
7830       (setq n (1- n)))
7831     (when (/= 0 n)
7832       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7833     (gnus-summary-recenter)
7834     (gnus-summary-position-point)
7835     (gnus-set-mode-line 'summary)
7836     n))
7837
7838 (defun gnus-summary-mark-article-as-read (mark)
7839   "Mark the current article quickly as read with MARK."
7840   (let ((article (gnus-summary-article-number)))
7841     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7842     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7843     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7844     (push (cons article mark) gnus-newsgroup-reads)
7845     ;; Possibly remove from cache, if that is used.
7846     (when gnus-use-cache
7847       (gnus-cache-enter-remove-article article))
7848     ;; Allow the backend to change the mark.
7849     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7850     ;; Check for auto-expiry.
7851     (when (and gnus-newsgroup-auto-expire
7852                (memq mark gnus-auto-expirable-marks))
7853       (setq mark gnus-expirable-mark)
7854       ;; Let the backend know about the mark change.
7855       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7856       (push article gnus-newsgroup-expirable))
7857     ;; Set the mark in the buffer.
7858     (gnus-summary-update-mark mark 'unread)
7859     t))
7860
7861 (defun gnus-summary-mark-article-as-unread (mark)
7862   "Mark the current article quickly as unread with MARK."
7863   (let* ((article (gnus-summary-article-number))
7864          (old-mark (gnus-summary-article-mark article)))
7865     ;; Allow the backend to change the mark.
7866     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7867     (if (eq mark old-mark)
7868         t
7869       (if (<= article 0)
7870           (progn
7871             (gnus-error 1 "Can't mark negative article numbers")
7872             nil)
7873         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7874         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7875         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7876         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7877         (cond ((= mark gnus-ticked-mark)
7878                (push article gnus-newsgroup-marked))
7879               ((= mark gnus-dormant-mark)
7880                (push article gnus-newsgroup-dormant))
7881               (t
7882                (push article gnus-newsgroup-unreads)))
7883         (gnus-pull article gnus-newsgroup-reads)
7884
7885         ;; See whether the article is to be put in the cache.
7886         (and gnus-use-cache
7887              (vectorp (gnus-summary-article-header article))
7888              (save-excursion
7889                (gnus-cache-possibly-enter-article
7890                 gnus-newsgroup-name article
7891                 (gnus-summary-article-header article)
7892                 (= mark gnus-ticked-mark)
7893                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7894
7895         ;; Fix the mark.
7896         (gnus-summary-update-mark mark 'unread)
7897         t))))
7898
7899 (defun gnus-summary-mark-article (&optional article mark no-expire)
7900   "Mark ARTICLE with MARK.  MARK can be any character.
7901 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7902 `??' (dormant) and `?E' (expirable).
7903 If MARK is nil, then the default character `?r' is used.
7904 If ARTICLE is nil, then the article on the current line will be
7905 marked."
7906   ;; The mark might be a string.
7907   (when (stringp mark)
7908     (setq mark (aref mark 0)))
7909   ;; If no mark is given, then we check auto-expiring.
7910   (when (null mark)
7911     (setq mark gnus-del-mark))
7912   (when (and (not no-expire)
7913              gnus-newsgroup-auto-expire
7914              (memq mark gnus-auto-expirable-marks))
7915     (setq mark gnus-expirable-mark))
7916   (let ((article (or article (gnus-summary-article-number)))
7917         (old-mark (gnus-summary-article-mark article)))
7918     ;; Allow the backend to change the mark.
7919     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7920     (if (eq mark old-mark)
7921         t
7922       (unless article
7923         (error "No article on current line"))
7924       (if (not (if (or (= mark gnus-unread-mark)
7925                        (= mark gnus-ticked-mark)
7926                        (= mark gnus-dormant-mark))
7927                    (gnus-mark-article-as-unread article mark)
7928                  (gnus-mark-article-as-read article mark)))
7929           t
7930         ;; See whether the article is to be put in the cache.
7931         (and gnus-use-cache
7932              (not (= mark gnus-canceled-mark))
7933              (vectorp (gnus-summary-article-header article))
7934              (save-excursion
7935                (gnus-cache-possibly-enter-article
7936                 gnus-newsgroup-name article
7937                 (gnus-summary-article-header article)
7938                 (= mark gnus-ticked-mark)
7939                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7940
7941         (when (gnus-summary-goto-subject article nil t)
7942           (let ((buffer-read-only nil))
7943             (gnus-summary-show-thread)
7944             ;; Fix the mark.
7945             (gnus-summary-update-mark mark 'unread)
7946             t))))))
7947
7948 (defun gnus-summary-update-secondary-mark (article)
7949   "Update the secondary (read, process, cache) mark."
7950   (gnus-summary-update-mark
7951    (cond ((memq article gnus-newsgroup-processable)
7952           gnus-process-mark)
7953          ((memq article gnus-newsgroup-cached)
7954           gnus-cached-mark)
7955          ((memq article gnus-newsgroup-replied)
7956           gnus-replied-mark)
7957          ((memq article gnus-newsgroup-saved)
7958           gnus-saved-mark)
7959          (t gnus-unread-mark))
7960    'replied)
7961   (when (gnus-visual-p 'summary-highlight 'highlight)
7962     (gnus-run-hooks 'gnus-summary-update-hook))
7963   t)
7964
7965 (defun gnus-summary-update-mark (mark type)
7966   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7967         (buffer-read-only nil))
7968     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7969     (when forward
7970       (when (looking-at "\r")
7971         (incf forward))
7972       (when (<= (+ forward (point)) (point-max))
7973         ;; Go to the right position on the line.
7974         (goto-char (+ forward (point)))
7975         ;; Replace the old mark with the new mark.
7976         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7977         ;; Optionally update the marks by some user rule.
7978         (when (eq type 'unread)
7979           (gnus-data-set-mark
7980            (gnus-data-find (gnus-summary-article-number)) mark)
7981           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7982
7983 (defun gnus-mark-article-as-read (article &optional mark)
7984   "Enter ARTICLE in the pertinent lists and remove it from others."
7985   ;; Make the article expirable.
7986   (let ((mark (or mark gnus-del-mark)))
7987     (if (= mark gnus-expirable-mark)
7988         (push article gnus-newsgroup-expirable)
7989       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7990     ;; Remove from unread and marked lists.
7991     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7992     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7993     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7994     (push (cons article mark) gnus-newsgroup-reads)
7995     ;; Possibly remove from cache, if that is used.
7996     (when gnus-use-cache
7997       (gnus-cache-enter-remove-article article))
7998     t))
7999
8000 (defun gnus-mark-article-as-unread (article &optional mark)
8001   "Enter ARTICLE in the pertinent lists and remove it from others."
8002   (let ((mark (or mark gnus-ticked-mark)))
8003     (if (<= article 0)
8004         (progn
8005           (gnus-error 1 "Can't mark negative article numbers")
8006           nil)
8007       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8008             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8009             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8010             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8011
8012       ;; Unsuppress duplicates?
8013       (when gnus-suppress-duplicates
8014         (gnus-dup-unsuppress-article article))
8015
8016       (cond ((= mark gnus-ticked-mark)
8017              (push article gnus-newsgroup-marked))
8018             ((= mark gnus-dormant-mark)
8019              (push article gnus-newsgroup-dormant))
8020             (t
8021              (push article gnus-newsgroup-unreads)))
8022       (gnus-pull article gnus-newsgroup-reads)
8023       t)))
8024
8025 (defalias 'gnus-summary-mark-as-unread-forward
8026   'gnus-summary-tick-article-forward)
8027 (make-obsolete 'gnus-summary-mark-as-unread-forward
8028                'gnus-summary-tick-article-forward)
8029 (defun gnus-summary-tick-article-forward (n)
8030   "Tick N articles forwards.
8031 If N is negative, tick backwards instead.
8032 The difference between N and the number of articles ticked is returned."
8033   (interactive "p")
8034   (gnus-summary-mark-forward n gnus-ticked-mark))
8035
8036 (defalias 'gnus-summary-mark-as-unread-backward
8037   'gnus-summary-tick-article-backward)
8038 (make-obsolete 'gnus-summary-mark-as-unread-backward
8039                'gnus-summary-tick-article-backward)
8040 (defun gnus-summary-tick-article-backward (n)
8041   "Tick N articles backwards.
8042 The difference between N and the number of articles ticked is returned."
8043   (interactive "p")
8044   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8045
8046 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8047 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8048 (defun gnus-summary-tick-article (&optional article clear-mark)
8049   "Mark current article as unread.
8050 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8051 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8052   (interactive)
8053   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8054                                        gnus-ticked-mark)))
8055
8056 (defun gnus-summary-mark-as-read-forward (n)
8057   "Mark N articles as read forwards.
8058 If N is negative, mark backwards instead.
8059 The difference between N and the actual number of articles marked is
8060 returned."
8061   (interactive "p")
8062   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8063
8064 (defun gnus-summary-mark-as-read-backward (n)
8065   "Mark the N articles as read backwards.
8066 The difference between N and the actual number of articles marked is
8067 returned."
8068   (interactive "p")
8069   (gnus-summary-mark-forward (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8070
8071 (defun gnus-summary-mark-as-read (&optional article mark)
8072   "Mark current article as read.
8073 ARTICLE specifies the article to be marked as read.
8074 MARK specifies a string to be inserted at the beginning of the line."
8075   (gnus-summary-mark-article article mark))
8076
8077 (defun gnus-summary-clear-mark-forward (n)
8078   "Clear marks from N articles forward.
8079 If N is negative, clear backward instead.
8080 The difference between N and the number of marks cleared is returned."
8081   (interactive "p")
8082   (gnus-summary-mark-forward n gnus-unread-mark))
8083
8084 (defun gnus-summary-clear-mark-backward (n)
8085   "Clear marks from N articles backward.
8086 The difference between N and the number of marks cleared is returned."
8087   (interactive "p")
8088   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8089
8090 (defun gnus-summary-mark-unread-as-read ()
8091   "Intended to be used by `gnus-summary-mark-article-hook'."
8092   (when (memq gnus-current-article gnus-newsgroup-unreads)
8093     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8094
8095 (defun gnus-summary-mark-read-and-unread-as-read ()
8096   "Intended to be used by `gnus-summary-mark-article-hook'."
8097   (let ((mark (gnus-summary-article-mark)))
8098     (when (or (gnus-unread-mark-p mark)
8099               (gnus-read-mark-p mark))
8100       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8101
8102 (defun gnus-summary-mark-region-as-read (point mark all)
8103   "Mark all unread articles between point and mark as read.
8104 If given a prefix, mark all articles between point and mark as read,
8105 even ticked and dormant ones."
8106   (interactive "r\nP")
8107   (save-excursion
8108     (let (article)
8109       (goto-char point)
8110       (beginning-of-line)
8111       (while (and
8112               (< (point) mark)
8113               (progn
8114                 (when (or all
8115                           (memq (setq article (gnus-summary-article-number))
8116                                 gnus-newsgroup-unreads))
8117                   (gnus-summary-mark-article article gnus-del-mark))
8118                 t)
8119               (gnus-summary-find-next))))))
8120
8121 (defun gnus-summary-mark-below (score mark)
8122   "Mark articles with score less than SCORE with MARK."
8123   (interactive "P\ncMark: ")
8124   (setq score (if score
8125                   (prefix-numeric-value score)
8126                 (or gnus-summary-default-score 0)))
8127   (save-excursion
8128     (set-buffer gnus-summary-buffer)
8129     (goto-char (point-min))
8130     (while
8131         (progn
8132           (and (< (gnus-summary-article-score) score)
8133                (gnus-summary-mark-article nil mark))
8134           (gnus-summary-find-next)))))
8135
8136 (defun gnus-summary-kill-below (&optional score)
8137   "Mark articles with score below SCORE as read."
8138   (interactive "P")
8139   (gnus-summary-mark-below score gnus-killed-mark))
8140
8141 (defun gnus-summary-clear-above (&optional score)
8142   "Clear all marks from articles with score above SCORE."
8143   (interactive "P")
8144   (gnus-summary-mark-above score gnus-unread-mark))
8145
8146 (defun gnus-summary-tick-above (&optional score)
8147   "Tick all articles with score above SCORE."
8148   (interactive "P")
8149   (gnus-summary-mark-above score gnus-ticked-mark))
8150
8151 (defun gnus-summary-mark-above (score mark)
8152   "Mark articles with score over SCORE with MARK."
8153   (interactive "P\ncMark: ")
8154   (setq score (if score
8155                   (prefix-numeric-value score)
8156                 (or gnus-summary-default-score 0)))
8157   (save-excursion
8158     (set-buffer gnus-summary-buffer)
8159     (goto-char (point-min))
8160     (while (and (progn
8161                   (when (> (gnus-summary-article-score) score)
8162                     (gnus-summary-mark-article nil mark))
8163                   t)
8164                 (gnus-summary-find-next)))))
8165
8166 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8167 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8168 (defun gnus-summary-limit-include-expunged (&optional no-error)
8169   "Display all the hidden articles that were expunged for low scores."
8170   (interactive)
8171   (let ((buffer-read-only nil))
8172     (let ((scored gnus-newsgroup-scored)
8173           headers h)
8174       (while scored
8175         (unless (gnus-summary-goto-subject (caar scored))
8176           (and (setq h (gnus-summary-article-header (caar scored)))
8177                (< (cdar scored) gnus-summary-expunge-below)
8178                (push h headers)))
8179         (setq scored (cdr scored)))
8180       (if (not headers)
8181           (when (not no-error)
8182             (error "No expunged articles hidden"))
8183         (goto-char (point-min))
8184         (gnus-summary-prepare-unthreaded (nreverse headers))
8185         (goto-char (point-min))
8186         (gnus-summary-position-point)
8187         t))))
8188
8189 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8190   "Mark all unread articles in this newsgroup as read.
8191 If prefix argument ALL is non-nil, ticked and dormant articles will
8192 also be marked as read.
8193 If QUIETLY is non-nil, no questions will be asked.
8194 If TO-HERE is non-nil, it should be a point in the buffer.  All
8195 articles before this point will be marked as read.
8196 Note that this function will only catch up the unread article
8197 in the current summary buffer limitation.
8198 The number of articles marked as read is returned."
8199   (interactive "P")
8200   (prog1
8201       (save-excursion
8202         (when (or quietly
8203                   (not gnus-interactive-catchup) ;Without confirmation?
8204                   gnus-expert-user
8205                   (gnus-y-or-n-p
8206                    (if all
8207                        "Mark absolutely all articles as read? "
8208                      "Mark all unread articles as read? ")))
8209           (if (and not-mark
8210                    (not gnus-newsgroup-adaptive)
8211                    (not gnus-newsgroup-auto-expire)
8212                    (not gnus-suppress-duplicates)
8213                    (or (not gnus-use-cache)
8214                        (eq gnus-use-cache 'passive)))
8215               (progn
8216                 (when all
8217                   (setq gnus-newsgroup-marked nil
8218                         gnus-newsgroup-dormant nil))
8219                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8220             ;; We actually mark all articles as canceled, which we
8221             ;; have to do when using auto-expiry or adaptive scoring.
8222             (gnus-summary-show-all-threads)
8223             (when (gnus-summary-first-subject (not all) t)
8224               (while (and
8225                       (if to-here (< (point) to-here) t)
8226                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8227                       (gnus-summary-find-next (not all) nil nil t))))
8228             (gnus-set-mode-line 'summary))
8229           t))
8230     (gnus-summary-position-point)))
8231
8232 (defun gnus-summary-catchup-to-here (&optional all)
8233   "Mark all unticked articles before the current one as read.
8234 If ALL is non-nil, also mark ticked and dormant articles as read."
8235   (interactive "P")
8236   (save-excursion
8237     (gnus-save-hidden-threads
8238       (let ((beg (point)))
8239         ;; We check that there are unread articles.
8240         (when (or all (gnus-summary-find-prev))
8241           (gnus-summary-catchup all t beg)))))
8242   (gnus-summary-position-point))
8243
8244 (defun gnus-summary-catchup-all (&optional quietly)
8245   "Mark all articles in this newsgroup as read."
8246   (interactive "P")
8247   (gnus-summary-catchup t quietly))
8248
8249 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8250   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8251 If prefix argument ALL is non-nil, all articles are marked as read."
8252   (interactive "P")
8253   (when (gnus-summary-catchup all quietly nil 'fast)
8254     ;; Select next newsgroup or exit.
8255     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8256              (eq gnus-auto-select-next 'quietly))
8257         (gnus-summary-next-group nil)
8258       (gnus-summary-exit))))
8259
8260 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8261   "Mark all articles in this newsgroup as read, and then exit."
8262   (interactive "P")
8263   (gnus-summary-catchup-and-exit t quietly))
8264
8265 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8266 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8267   "Mark all articles in this group as read and select the next group.
8268 If given a prefix, mark all articles, unread as well as ticked, as
8269 read."
8270   (interactive "P")
8271   (save-excursion
8272     (gnus-summary-catchup all))
8273   (gnus-summary-next-article t nil nil t))
8274
8275 ;; Thread-based commands.
8276
8277 (defun gnus-summary-articles-in-thread (&optional article)
8278   "Return a list of all articles in the current thread.
8279 If ARTICLE is non-nil, return all articles in the thread that starts
8280 with that article."
8281   (let* ((article (or article (gnus-summary-article-number)))
8282          (data (gnus-data-find-list article))
8283          (top-level (gnus-data-level (car data)))
8284          (top-subject
8285           (cond ((null gnus-thread-operation-ignore-subject)
8286                  (gnus-simplify-subject-re
8287                   (mail-header-subject (gnus-data-header (car data)))))
8288                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8289                  (gnus-simplify-subject-fuzzy
8290                   (mail-header-subject (gnus-data-header (car data)))))
8291                 (t nil)))
8292          (end-point (save-excursion
8293                       (if (gnus-summary-go-to-next-thread)
8294                           (point) (point-max))))
8295          articles)
8296     (while (and data
8297                 (< (gnus-data-pos (car data)) end-point))
8298       (when (or (not top-subject)
8299                 (string= top-subject
8300                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8301                              (gnus-simplify-subject-fuzzy
8302                               (mail-header-subject
8303                                (gnus-data-header (car data))))
8304                            (gnus-simplify-subject-re
8305                             (mail-header-subject
8306                              (gnus-data-header (car data)))))))
8307         (push (gnus-data-number (car data)) articles))
8308       (unless (and (setq data (cdr data))
8309                    (> (gnus-data-level (car data)) top-level))
8310         (setq data nil)))
8311     ;; Return the list of articles.
8312     (nreverse articles)))
8313
8314 (defun gnus-summary-rethread-current ()
8315   "Rethread the thread the current article is part of."
8316   (interactive)
8317   (let* ((gnus-show-threads t)
8318          (article (gnus-summary-article-number))
8319          (id (mail-header-id (gnus-summary-article-header)))
8320          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8321     (unless id
8322       (error "No article on the current line"))
8323     (gnus-rebuild-thread id)
8324     (gnus-summary-goto-subject article)))
8325
8326 (defun gnus-summary-reparent-thread ()
8327   "Make the current article child of the marked (or previous) article.
8328
8329 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8330 is non-nil or the Subject: of both articles are the same."
8331   (interactive)
8332   (unless (not (gnus-group-read-only-p))
8333     (error "The current newsgroup does not support article editing"))
8334   (unless (<= (length gnus-newsgroup-processable) 1)
8335     (error "No more than one article may be marked"))
8336   (save-window-excursion
8337     (let ((gnus-article-buffer " *reparent*")
8338           (current-article (gnus-summary-article-number))
8339           ;; First grab the marked article, otherwise one line up.
8340           (parent-article (if (not (null gnus-newsgroup-processable))
8341                               (car gnus-newsgroup-processable)
8342                             (save-excursion
8343                               (if (eq (forward-line -1) 0)
8344                                   (gnus-summary-article-number)
8345                                 (error "Beginning of summary buffer"))))))
8346       (unless (not (eq current-article parent-article))
8347         (error "An article may not be self-referential"))
8348       (let ((message-id (mail-header-id
8349                          (gnus-summary-article-header parent-article))))
8350         (unless (and message-id (not (equal message-id "")))
8351           (error "No message-id in desired parent"))
8352         (gnus-with-article current-article
8353           (goto-char (point-min))
8354           (if (re-search-forward "^References: " nil t)
8355               (progn
8356                 (re-search-forward "^[^ \t]" nil t)
8357                 (forward-line -1)
8358                 (end-of-line)
8359                 (insert " " message-id))
8360             (insert "References: " message-id "\n")))
8361         (set-buffer gnus-summary-buffer)
8362         (gnus-summary-unmark-all-processable)
8363         (gnus-summary-update-article current-article)
8364         (gnus-summary-rethread-current)
8365         (gnus-message 3 "Article %d is now the child of article %d"
8366                       current-article parent-article)))))
8367
8368 (defun gnus-summary-toggle-threads (&optional arg)
8369   "Toggle showing conversation threads.
8370 If ARG is positive number, turn showing conversation threads on."
8371   (interactive "P")
8372   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8373     (setq gnus-show-threads
8374           (if (null arg) (not gnus-show-threads)
8375             (> (prefix-numeric-value arg) 0)))
8376     (gnus-summary-prepare)
8377     (gnus-summary-goto-subject current)
8378     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8379     (gnus-summary-position-point)))
8380
8381 (defun gnus-summary-show-all-threads ()
8382   "Show all threads."
8383   (interactive)
8384   (save-excursion
8385     (let ((buffer-read-only nil))
8386       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8387   (gnus-summary-position-point))
8388
8389 (defun gnus-summary-show-thread ()
8390   "Show thread subtrees.
8391 Returns nil if no thread was there to be shown."
8392   (interactive)
8393   (let ((buffer-read-only nil)
8394         (orig (point))
8395         ;; first goto end then to beg, to have point at beg after let
8396         (end (progn (end-of-line) (point)))
8397         (beg (progn (beginning-of-line) (point))))
8398     (prog1
8399         ;; Any hidden lines here?
8400         (search-forward "\r" end t)
8401       (subst-char-in-region beg end ?\^M ?\n t)
8402       (goto-char orig)
8403       (gnus-summary-position-point))))
8404
8405 (defun gnus-summary-hide-all-threads ()
8406   "Hide all thread subtrees."
8407   (interactive)
8408   (save-excursion
8409     (goto-char (point-min))
8410     (gnus-summary-hide-thread)
8411     (while (zerop (gnus-summary-next-thread 1 t))
8412       (gnus-summary-hide-thread)))
8413   (gnus-summary-position-point))
8414
8415 (defun gnus-summary-hide-thread ()
8416   "Hide thread subtrees.
8417 Returns nil if no threads were there to be hidden."
8418   (interactive)
8419   (let ((buffer-read-only nil)
8420         (start (point))
8421         (article (gnus-summary-article-number)))
8422     (goto-char start)
8423     ;; Go forward until either the buffer ends or the subthread
8424     ;; ends.
8425     (when (and (not (eobp))
8426                (or (zerop (gnus-summary-next-thread 1 t))
8427                    (goto-char (point-max))))
8428       (prog1
8429           (if (and (> (point) start)
8430                    (search-backward "\n" start t))
8431               (progn
8432                 (subst-char-in-region start (point) ?\n ?\^M)
8433                 (gnus-summary-goto-subject article))
8434             (goto-char start)
8435             nil)
8436         ;;(gnus-summary-position-point)
8437         ))))
8438
8439 (defun gnus-summary-go-to-next-thread (&optional previous)
8440   "Go to the same level (or less) next thread.
8441 If PREVIOUS is non-nil, go to previous thread instead.
8442 Return the article number moved to, or nil if moving was impossible."
8443   (let ((level (gnus-summary-thread-level))
8444         (way (if previous -1 1))
8445         (beg (point)))
8446     (forward-line way)
8447     (while (and (not (eobp))
8448                 (< level (gnus-summary-thread-level)))
8449       (forward-line way))
8450     (if (eobp)
8451         (progn
8452           (goto-char beg)
8453           nil)
8454       (setq beg (point))
8455       (prog1
8456           (gnus-summary-article-number)
8457         (goto-char beg)))))
8458
8459 (defun gnus-summary-next-thread (n &optional silent)
8460   "Go to the same level next N'th thread.
8461 If N is negative, search backward instead.
8462 Returns the difference between N and the number of skips actually
8463 done.
8464
8465 If SILENT, don't output messages."
8466   (interactive "p")
8467   (let ((backward (< n 0))
8468         (n (abs n)))
8469     (while (and (> n 0)
8470                 (gnus-summary-go-to-next-thread backward))
8471       (decf n))
8472     (unless silent
8473       (gnus-summary-position-point))
8474     (when (and (not silent) (/= 0 n))
8475       (gnus-message 7 "No more threads"))
8476     n))
8477
8478 (defun gnus-summary-prev-thread (n)
8479   "Go to the same level previous N'th thread.
8480 Returns the difference between N and the number of skips actually
8481 done."
8482   (interactive "p")
8483   (gnus-summary-next-thread (- n)))
8484
8485 (defun gnus-summary-go-down-thread ()
8486   "Go down one level in the current thread."
8487   (let ((children (gnus-summary-article-children)))
8488     (when children
8489       (gnus-summary-goto-subject (car children)))))
8490
8491 (defun gnus-summary-go-up-thread ()
8492   "Go up one level in the current thread."
8493   (let ((parent (gnus-summary-article-parent)))
8494     (when parent
8495       (gnus-summary-goto-subject parent))))
8496
8497 (defun gnus-summary-down-thread (n)
8498   "Go down thread N steps.
8499 If N is negative, go up instead.
8500 Returns the difference between N and how many steps down that were
8501 taken."
8502   (interactive "p")
8503   (let ((up (< n 0))
8504         (n (abs n)))
8505     (while (and (> n 0)
8506                 (if up (gnus-summary-go-up-thread)
8507                   (gnus-summary-go-down-thread)))
8508       (setq n (1- n)))
8509     (gnus-summary-position-point)
8510     (when (/= 0 n)
8511       (gnus-message 7 "Can't go further"))
8512     n))
8513
8514 (defun gnus-summary-up-thread (n)
8515   "Go up thread N steps.
8516 If N is negative, go up instead.
8517 Returns the difference between N and how many steps down that were
8518 taken."
8519   (interactive "p")
8520   (gnus-summary-down-thread (- n)))
8521
8522 (defun gnus-summary-top-thread ()
8523   "Go to the top of the thread."
8524   (interactive)
8525   (while (gnus-summary-go-up-thread))
8526   (gnus-summary-article-number))
8527
8528 (defun gnus-summary-kill-thread (&optional unmark)
8529   "Mark articles under current thread as read.
8530 If the prefix argument is positive, remove any kinds of marks.
8531 If the prefix argument is negative, tick articles instead."
8532   (interactive "P")
8533   (when unmark
8534     (setq unmark (prefix-numeric-value unmark)))
8535   (let ((articles (gnus-summary-articles-in-thread)))
8536     (save-excursion
8537       ;; Expand the thread.
8538       (gnus-summary-show-thread)
8539       ;; Mark all the articles.
8540       (while articles
8541         (gnus-summary-goto-subject (car articles))
8542         (cond ((null unmark)
8543                (gnus-summary-mark-article-as-read gnus-killed-mark))
8544               ((> unmark 0)
8545                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8546               (t
8547                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8548         (setq articles (cdr articles))))
8549     ;; Hide killed subtrees.
8550     (and (null unmark)
8551          gnus-thread-hide-killed
8552          (gnus-summary-hide-thread))
8553     ;; If marked as read, go to next unread subject.
8554     (when (null unmark)
8555       ;; Go to next unread subject.
8556       (gnus-summary-next-subject 1 t)))
8557   (gnus-set-mode-line 'summary))
8558
8559 ;; Summary sorting commands
8560
8561 (defun gnus-summary-sort-by-number (&optional reverse)
8562   "Sort the summary buffer by article number.
8563 Argument REVERSE means reverse order."
8564   (interactive "P")
8565   (gnus-summary-sort 'number reverse))
8566
8567 (defun gnus-summary-sort-by-author (&optional reverse)
8568   "Sort the summary buffer by author name alphabetically.
8569 If case-fold-search is non-nil, case of letters is ignored.
8570 Argument REVERSE means reverse order."
8571   (interactive "P")
8572   (gnus-summary-sort 'author reverse))
8573
8574 (defun gnus-summary-sort-by-subject (&optional reverse)
8575   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8576 If case-fold-search is non-nil, case of letters is ignored.
8577 Argument REVERSE means reverse order."
8578   (interactive "P")
8579   (gnus-summary-sort 'subject reverse))
8580
8581 (defun gnus-summary-sort-by-date (&optional reverse)
8582   "Sort the summary buffer by date.
8583 Argument REVERSE means reverse order."
8584   (interactive "P")
8585   (gnus-summary-sort 'date reverse))
8586
8587 (defun gnus-summary-sort-by-score (&optional reverse)
8588   "Sort the summary buffer by score.
8589 Argument REVERSE means reverse order."
8590   (interactive "P")
8591   (gnus-summary-sort 'score reverse))
8592
8593 (defun gnus-summary-sort-by-lines (&optional reverse)
8594   "Sort the summary buffer by article length.
8595 Argument REVERSE means reverse order."
8596   (interactive "P")
8597   (gnus-summary-sort 'lines reverse))
8598
8599 (defun gnus-summary-sort (predicate reverse)
8600   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8601   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8602          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8603          (gnus-thread-sort-functions
8604           (if (not reverse)
8605               thread
8606             `(lambda (t1 t2)
8607                (,thread t2 t1))))
8608          (gnus-article-sort-functions
8609           (if (not reverse)
8610               article
8611             `(lambda (t1 t2)
8612                (,article t2 t1))))
8613          (buffer-read-only)
8614          (gnus-summary-prepare-hook nil))
8615     ;; We do the sorting by regenerating the threads.
8616     (gnus-summary-prepare)
8617     ;; Hide subthreads if needed.
8618     (when (and gnus-show-threads gnus-thread-hide-subtree)
8619       (gnus-summary-hide-all-threads))))
8620
8621 ;; Summary saving commands.
8622
8623 (defun gnus-summary-save-article (&optional n not-saved)
8624   "Save the current article using the default saver function.
8625 If N is a positive number, save the N next articles.
8626 If N is a negative number, save the N previous articles.
8627 If N is nil and any articles have been marked with the process mark,
8628 save those articles instead.
8629 The variable `gnus-default-article-saver' specifies the saver function."
8630   (interactive "P")
8631   (let* ((articles (gnus-summary-work-articles n))
8632          (save-buffer (save-excursion
8633                         (nnheader-set-temp-buffer " *Gnus Save*")))
8634          (num (length articles))
8635          header article file)
8636     (while articles
8637       (setq header (gnus-summary-article-header
8638                     (setq article (pop articles))))
8639       (if (not (vectorp header))
8640           ;; This is a pseudo-article.
8641           (if (assq 'name header)
8642               (gnus-copy-file (cdr (assq 'name header)))
8643             (gnus-message 1 "Article %d is unsaveable" article))
8644         ;; This is a real article.
8645         (save-window-excursion
8646           (gnus-summary-select-article t nil nil article))
8647         (save-excursion
8648           (set-buffer save-buffer)
8649           (erase-buffer)
8650           (insert-buffer-substring gnus-original-article-buffer))
8651         (setq file (gnus-article-save save-buffer file num))
8652         (gnus-summary-remove-process-mark article)
8653         (unless not-saved
8654           (gnus-summary-set-saved-mark article))))
8655     (gnus-kill-buffer save-buffer)
8656     (gnus-summary-position-point)
8657     (gnus-set-mode-line 'summary)
8658     n))
8659
8660 (defun gnus-summary-pipe-output (&optional arg)
8661   "Pipe the current article to a subprocess.
8662 If N is a positive number, pipe the N next articles.
8663 If N is a negative number, pipe the N previous articles.
8664 If N is nil and any articles have been marked with the process mark,
8665 pipe those articles instead."
8666   (interactive "P")
8667   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8668     (gnus-summary-save-article arg t))
8669   (gnus-configure-windows 'pipe))
8670
8671 (defun gnus-summary-save-article-mail (&optional arg)
8672   "Append the current article to an mail file.
8673 If N is a positive number, save the N next articles.
8674 If N is a negative number, save the N previous articles.
8675 If N is nil and any articles have been marked with the process mark,
8676 save those articles instead."
8677   (interactive "P")
8678   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8679     (gnus-summary-save-article arg)))
8680
8681 (defun gnus-summary-save-article-rmail (&optional arg)
8682   "Append the current article to an rmail file.
8683 If N is a positive number, save the N next articles.
8684 If N is a negative number, save the N previous articles.
8685 If N is nil and any articles have been marked with the process mark,
8686 save those articles instead."
8687   (interactive "P")
8688   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8689     (gnus-summary-save-article arg)))
8690
8691 (defun gnus-summary-save-article-file (&optional arg)
8692   "Append the current article to a file.
8693 If N is a positive number, save the N next articles.
8694 If N is a negative number, save the N previous articles.
8695 If N is nil and any articles have been marked with the process mark,
8696 save those articles instead."
8697   (interactive "P")
8698   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8699     (gnus-summary-save-article arg)))
8700
8701 (defun gnus-summary-write-article-file (&optional arg)
8702   "Write the current article to a file, deleting the previous file.
8703 If N is a positive number, save the N next articles.
8704 If N is a negative number, save the N previous articles.
8705 If N is nil and any articles have been marked with the process mark,
8706 save those articles instead."
8707   (interactive "P")
8708   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8709     (gnus-summary-save-article arg)))
8710
8711 (defun gnus-summary-save-article-body-file (&optional arg)
8712   "Append the current article body to a file.
8713 If N is a positive number, save the N next articles.
8714 If N is a negative number, save the N previous articles.
8715 If N is nil and any articles have been marked with the process mark,
8716 save those articles instead."
8717   (interactive "P")
8718   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8719     (gnus-summary-save-article arg)))
8720
8721 (defun gnus-summary-pipe-message (program)
8722   "Pipe the current article through PROGRAM."
8723   (interactive "sProgram: ")
8724   (gnus-summary-select-article)
8725   (let ((mail-header-separator ""))
8726     (gnus-eval-in-buffer-window gnus-article-buffer
8727       (save-restriction
8728         (widen)
8729         (let ((start (window-start))
8730               buffer-read-only)
8731           (message-pipe-buffer-body program)
8732           (set-window-start (get-buffer-window (current-buffer)) start))))))
8733
8734 (defun gnus-get-split-value (methods)
8735   "Return a value based on the split METHODS."
8736   (let (split-name method result match)
8737     (when methods
8738       (save-excursion
8739         (set-buffer gnus-original-article-buffer)
8740         (save-restriction
8741           (nnheader-narrow-to-headers)
8742           (while methods
8743             (goto-char (point-min))
8744             (setq method (pop methods))
8745             (setq match (car method))
8746             (when (cond
8747                    ((stringp match)
8748                     ;; Regular expression.
8749                     (ignore-errors
8750                       (re-search-forward match nil t)))
8751                    ((gnus-functionp match)
8752                     ;; Function.
8753                     (save-restriction
8754                       (widen)
8755                       (setq result (funcall match gnus-newsgroup-name))))
8756                    ((consp match)
8757                     ;; Form.
8758                     (save-restriction
8759                       (widen)
8760                       (setq result (eval match)))))
8761               (setq split-name (append (cdr method) split-name))
8762               (cond ((stringp result)
8763                      (push (expand-file-name
8764                             result gnus-article-save-directory)
8765                            split-name))
8766                     ((consp result)
8767                      (setq split-name (append result split-name)))))))))
8768     split-name))
8769
8770 (defun gnus-valid-move-group-p (group)
8771   (and (boundp group)
8772        (symbol-name group)
8773        (symbol-value group)
8774        (memq 'respool
8775              (assoc (symbol-name
8776                      (car (gnus-find-method-for-group
8777                            (symbol-name group))))
8778                     gnus-valid-select-methods))))
8779
8780 (defun gnus-read-move-group-name (prompt default articles prefix)
8781   "Read a group name."
8782   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8783          (minibuffer-confirm-incomplete nil) ; XEmacs
8784          (prom
8785           (format "%s %s to:"
8786                   prompt
8787                   (if (> (length articles) 1)
8788                       (format "these %d articles" (length articles))
8789                     "this article")))
8790          (to-newsgroup
8791           (cond
8792            ((null split-name)
8793             (gnus-completing-read default prom
8794                                   gnus-active-hashtb
8795                                   'gnus-valid-move-group-p
8796                                   nil prefix
8797                                   'gnus-group-history))
8798            ((= 1 (length split-name))
8799             (gnus-completing-read (car split-name) prom
8800                                   gnus-active-hashtb
8801                                   'gnus-valid-move-group-p
8802                                   nil nil
8803                                   'gnus-group-history))
8804            (t
8805             (gnus-completing-read nil prom
8806                                   (mapcar (lambda (el) (list el))
8807                                           (nreverse split-name))
8808                                   nil nil nil
8809                                   'gnus-group-history)))))
8810     (when to-newsgroup
8811       (if (or (string= to-newsgroup "")
8812               (string= to-newsgroup prefix))
8813           (setq to-newsgroup default))
8814       (unless to-newsgroup
8815         (error "No group name entered"))
8816       (or (gnus-active to-newsgroup)
8817           (gnus-activate-group to-newsgroup)
8818           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8819                                      to-newsgroup))
8820               (or (and (gnus-request-create-group
8821                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8822                        (gnus-activate-group to-newsgroup nil nil
8823                                             (gnus-group-name-to-method
8824                                              to-newsgroup)))
8825                   (error "Couldn't create group %s" to-newsgroup)))
8826           (error "No such group: %s" to-newsgroup)))
8827     to-newsgroup))
8828
8829 ;; Summary extract commands
8830
8831 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8832   (let ((buffer-read-only nil)
8833         (article (gnus-summary-article-number))
8834         after-article b e)
8835     (unless (gnus-summary-goto-subject article)
8836       (error "No such article: %d" article))
8837     (gnus-summary-position-point)
8838     ;; If all commands are to be bunched up on one line, we collect
8839     ;; them here.
8840     (unless gnus-view-pseudos-separately
8841       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8842             files action)
8843         (while ps
8844           (setq action (cdr (assq 'action (car ps))))
8845           (setq files (list (cdr (assq 'name (car ps)))))
8846           (while (and ps (cdr ps)
8847                       (string= (or action "1")
8848                                (or (cdr (assq 'action (cadr ps))) "2")))
8849             (push (cdr (assq 'name (cadr ps))) files)
8850             (setcdr ps (cddr ps)))
8851           (when files
8852             (when (not (string-match "%s" action))
8853               (push " " files))
8854             (push " " files)
8855             (when (assq 'execute (car ps))
8856               (setcdr (assq 'execute (car ps))
8857                       (funcall (if (string-match "%s" action)
8858                                    'format 'concat)
8859                                action
8860                                (mapconcat
8861                                 (lambda (f)
8862                                   (if (equal f " ")
8863                                       f
8864                                     (mm-quote-arg f)))
8865                                 files " ")))))
8866           (setq ps (cdr ps)))))
8867     (if (and gnus-view-pseudos (not not-view))
8868         (while pslist
8869           (when (assq 'execute (car pslist))
8870             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8871                                   (eq gnus-view-pseudos 'not-confirm)))
8872           (setq pslist (cdr pslist)))
8873       (save-excursion
8874         (while pslist
8875           (setq after-article (or (cdr (assq 'article (car pslist)))
8876                                   (gnus-summary-article-number)))
8877           (gnus-summary-goto-subject after-article)
8878           (forward-line 1)
8879           (setq b (point))
8880           (insert "    " (file-name-nondirectory
8881                           (cdr (assq 'name (car pslist))))
8882                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8883           (setq e (point))
8884           (forward-line -1)             ; back to `b'
8885           (gnus-add-text-properties
8886            b (1- e) (list 'gnus-number gnus-reffed-article-number
8887                           gnus-mouse-face-prop gnus-mouse-face))
8888           (gnus-data-enter
8889            after-article gnus-reffed-article-number
8890            gnus-unread-mark b (car pslist) 0 (- e b))
8891           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8892           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8893           (setq pslist (cdr pslist)))))))
8894
8895 (defun gnus-pseudos< (p1 p2)
8896   (let ((c1 (cdr (assq 'action p1)))
8897         (c2 (cdr (assq 'action p2))))
8898     (and c1 c2 (string< c1 c2))))
8899
8900 (defun gnus-request-pseudo-article (props)
8901   (cond ((assq 'execute props)
8902          (gnus-execute-command (cdr (assq 'execute props)))))
8903   (let ((gnus-current-article (gnus-summary-article-number)))
8904     (gnus-run-hooks 'gnus-mark-article-hook)))
8905
8906 (defun gnus-execute-command (command &optional automatic)
8907   (save-excursion
8908     (gnus-article-setup-buffer)
8909     (set-buffer gnus-article-buffer)
8910     (setq buffer-read-only nil)
8911     (let ((command (if automatic command
8912                      (read-string "Command: " (cons command 0)))))
8913       (erase-buffer)
8914       (insert "$ " command "\n\n")
8915       (if gnus-view-pseudo-asynchronously
8916           (start-process "gnus-execute" (current-buffer) shell-file-name
8917                          shell-command-switch command)
8918         (call-process shell-file-name nil t nil
8919                       shell-command-switch command)))))
8920
8921 ;; Summary kill commands.
8922
8923 (defun gnus-summary-edit-global-kill (article)
8924   "Edit the \"global\" kill file."
8925   (interactive (list (gnus-summary-article-number)))
8926   (gnus-group-edit-global-kill article))
8927
8928 (defun gnus-summary-edit-local-kill ()
8929   "Edit a local kill file applied to the current newsgroup."
8930   (interactive)
8931   (setq gnus-current-headers (gnus-summary-article-header))
8932   (gnus-group-edit-local-kill
8933    (gnus-summary-article-number) gnus-newsgroup-name))
8934
8935 ;;; Header reading.
8936
8937 (defun gnus-read-header (id &optional header)
8938   "Read the headers of article ID and enter them into the Gnus system."
8939   (let ((group gnus-newsgroup-name)
8940         (gnus-override-method
8941          (and (gnus-news-group-p gnus-newsgroup-name)
8942               gnus-refer-article-method))
8943         where)
8944     ;; First we check to see whether the header in question is already
8945     ;; fetched.
8946     (if (stringp id)
8947         ;; This is a Message-ID.
8948         (setq header (or header (gnus-id-to-header id)))
8949       ;; This is an article number.
8950       (setq header (or header (gnus-summary-article-header id))))
8951     (if (and header
8952              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8953         ;; We have found the header.
8954         header
8955       ;; If this is a sparse article, we have to nix out its
8956       ;; previous entry in the thread hashtb.
8957       (when (and header
8958                  (gnus-summary-article-sparse-p (mail-header-number header)))
8959         (let* ((parent (gnus-parent-id (mail-header-references header)))
8960                (thread (and parent (gnus-id-to-thread parent))))
8961           (when thread
8962             (delq (assq header thread) thread))))
8963       ;; We have to really fetch the header to this article.
8964       (save-excursion
8965         (set-buffer nntp-server-buffer)
8966         (when (setq where (gnus-request-head id group))
8967           (nnheader-fold-continuation-lines)
8968           (goto-char (point-max))
8969           (insert ".\n")
8970           (goto-char (point-min))
8971           (insert "211 ")
8972           (princ (cond
8973                   ((numberp id) id)
8974                   ((cdr where) (cdr where))
8975                   (header (mail-header-number header))
8976                   (t gnus-reffed-article-number))
8977                  (current-buffer))
8978           (insert " Article retrieved.\n"))
8979         (if (or (not where)
8980                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8981             ()                          ; Malformed head.
8982           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8983             (when (and (stringp id)
8984                        (not (string= (gnus-group-real-name group)
8985                                      (car where))))
8986               ;; If we fetched by Message-ID and the article came
8987               ;; from a different group, we fudge some bogus article
8988               ;; numbers for this article.
8989               (mail-header-set-number header gnus-reffed-article-number))
8990             (save-excursion
8991               (set-buffer gnus-summary-buffer)
8992               (decf gnus-reffed-article-number)
8993               (gnus-remove-header (mail-header-number header))
8994               (push header gnus-newsgroup-headers)
8995               (setq gnus-current-headers header)
8996               (push (mail-header-number header) gnus-newsgroup-limit)))
8997           header)))))
8998
8999 (defun gnus-remove-header (number)
9000   "Remove header NUMBER from `gnus-newsgroup-headers'."
9001   (if (and gnus-newsgroup-headers
9002            (= number (mail-header-number (car gnus-newsgroup-headers))))
9003       (pop gnus-newsgroup-headers)
9004     (let ((headers gnus-newsgroup-headers))
9005       (while (and (cdr headers)
9006                   (not (= number (mail-header-number (cadr headers)))))
9007         (pop headers))
9008       (when (cdr headers)
9009         (setcdr headers (cddr headers))))))
9010
9011 ;;;
9012 ;;; summary highlights
9013 ;;;
9014
9015 (defun gnus-highlight-selected-summary ()
9016   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9017   ;; Highlight selected article in summary buffer
9018   (when gnus-summary-selected-face
9019     (save-excursion
9020       (let* ((beg (progn (beginning-of-line) (point)))
9021              (end (progn (end-of-line) (point)))
9022              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9023              (from (if (get-text-property beg gnus-mouse-face-prop)
9024                        beg
9025                      (or (next-single-property-change
9026                           beg gnus-mouse-face-prop nil end)
9027                          beg)))
9028              (to
9029               (if (= from end)
9030                   (- from 2)
9031                 (or (next-single-property-change
9032                      from gnus-mouse-face-prop nil end)
9033                     end))))
9034         ;; If no mouse-face prop on line we will have to = from = end,
9035         ;; so we highlight the entire line instead.
9036         (when (= (+ to 2) from)
9037           (setq from beg)
9038           (setq to end))
9039         (if gnus-newsgroup-selected-overlay
9040             ;; Move old overlay.
9041             (gnus-move-overlay
9042              gnus-newsgroup-selected-overlay from to (current-buffer))
9043           ;; Create new overlay.
9044           (gnus-overlay-put
9045            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9046            'face gnus-summary-selected-face))))))
9047
9048 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9049 (defun gnus-summary-highlight-line ()
9050   "Highlight current line according to `gnus-summary-highlight'."
9051   (let* ((list gnus-summary-highlight)
9052          (p (point))
9053          (end (progn (end-of-line) (point)))
9054          ;; now find out where the line starts and leave point there.
9055          (beg (progn (beginning-of-line) (point)))
9056          (article (gnus-summary-article-number))
9057          (score (or (cdr (assq (or article gnus-current-article)
9058                                gnus-newsgroup-scored))
9059                     gnus-summary-default-score 0))
9060          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9061          (inhibit-read-only t))
9062     ;; Eval the cars of the lists until we find a match.
9063     (let ((default gnus-summary-default-score))
9064       (while (and list
9065                   (not (eval (caar list))))
9066         (setq list (cdr list))))
9067     (let ((face (cdar list)))
9068       (unless (eq face (get-text-property beg 'face))
9069         (gnus-put-text-property-excluding-characters-with-faces
9070          beg end 'face
9071          (setq face (if (boundp face) (symbol-value face) face)))
9072         (when gnus-summary-highlight-line-function
9073           (funcall gnus-summary-highlight-line-function article face))))
9074     (goto-char p)))
9075
9076 (defun gnus-update-read-articles (group unread &optional compute)
9077   "Update the list of read articles in GROUP."
9078   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9079          (entry (gnus-gethash group gnus-newsrc-hashtb))
9080          (info (nth 2 entry))
9081          (prev 1)
9082          (unread (sort (copy-sequence unread) '<))
9083          read)
9084     (if (or (not info) (not active))
9085         ;; There is no info on this group if it was, in fact,
9086         ;; killed.  Gnus stores no information on killed groups, so
9087         ;; there's nothing to be done.
9088         ;; One could store the information somewhere temporarily,
9089         ;; perhaps...  Hmmm...
9090         ()
9091       ;; Remove any negative articles numbers.
9092       (while (and unread (< (car unread) 0))
9093         (setq unread (cdr unread)))
9094       ;; Remove any expired article numbers
9095       (while (and unread (< (car unread) (car active)))
9096         (setq unread (cdr unread)))
9097       ;; Compute the ranges of read articles by looking at the list of
9098       ;; unread articles.
9099       (while unread
9100         (when (/= (car unread) prev)
9101           (push (if (= prev (1- (car unread))) prev
9102                   (cons prev (1- (car unread))))
9103                 read))
9104         (setq prev (1+ (car unread)))
9105         (setq unread (cdr unread)))
9106       (when (<= prev (cdr active))
9107         (push (cons prev (cdr active)) read))
9108       (setq read (if (> (length read) 1) (nreverse read) read))
9109       (if compute
9110           read
9111         (save-excursion
9112           (set-buffer gnus-group-buffer)
9113           (gnus-undo-register
9114             `(progn
9115                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9116                (gnus-info-set-read ',info ',(gnus-info-read info))
9117                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9118                (gnus-group-update-group ,group t))))
9119         ;; Enter this list into the group info.
9120         (gnus-info-set-read info read)
9121         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9122         (gnus-get-unread-articles-in-group info (gnus-active group))
9123         t))))
9124
9125 (defun gnus-offer-save-summaries ()
9126   "Offer to save all active summary buffers."
9127   (save-excursion
9128     (let ((buflist (buffer-list))
9129           buffers bufname)
9130       ;; Go through all buffers and find all summaries.
9131       (while buflist
9132         (and (setq bufname (buffer-name (car buflist)))
9133              (string-match "Summary" bufname)
9134              (save-excursion
9135                (set-buffer bufname)
9136                ;; We check that this is, indeed, a summary buffer.
9137                (and (eq major-mode 'gnus-summary-mode)
9138                     ;; Also make sure this isn't bogus.
9139                     gnus-newsgroup-prepared
9140                     ;; Also make sure that this isn't a dead summary buffer.
9141                     (not gnus-dead-summary-mode)))
9142              (push bufname buffers))
9143         (setq buflist (cdr buflist)))
9144       ;; Go through all these summary buffers and offer to save them.
9145       (when buffers
9146         (map-y-or-n-p
9147          "Update summary buffer %s? "
9148          (lambda (buf)
9149            (switch-to-buffer buf)
9150            (gnus-summary-exit))
9151          buffers)))))
9152
9153 (defun gnus-newsgroup-setup-default-charset ()
9154   "Setup newsgroup default charset."
9155   (let ((name (and gnus-newsgroup-name 
9156                    (string-match "[^:]+$" gnus-newsgroup-name)
9157                    (match-string 0 gnus-newsgroup-name))))
9158     (setq gnus-newsgroup-default-charset
9159           (or (and gnus-newsgroup-name
9160                    (or (gnus-group-find-parameter 
9161                         gnus-newsgroup-name 'charset)
9162                        (let ((alist gnus-newsgroup-default-charset-alist) 
9163                              elem (charset nil))
9164                          (while alist
9165                            (if (and name
9166                                     (string-match 
9167                                      (car (setq elem (pop alist)))
9168                                      name))
9169                                (setq alist nil
9170                                      charset (cdr elem))))
9171                          charset)))
9172               gnus-default-charset))
9173     (setq gnus-newsgroup-iso-8859-1-forced 
9174           (and gnus-newsgroup-name
9175                (or (gnus-group-find-parameter
9176                     gnus-newsgroup-name 'iso-8859-1-forced)
9177                    (and name
9178                         (string-match  gnus-newsgroup-iso-8859-1-forced-regexp 
9179                                        name))))))
9180   (if (stringp gnus-newsgroup-default-charset)
9181       (setq gnus-newsgroup-default-charset
9182             (intern (downcase gnus-newsgroup-default-charset))))
9183   (setq gnus-newsgroup-iso-8859-1-forced
9184         (if (stringp gnus-newsgroup-iso-8859-1-forced)
9185             (intern (downcase gnus-newsgroup-iso-8859-1-forced))
9186           (and gnus-newsgroup-iso-8859-1-forced
9187                gnus-newsgroup-default-charset))))
9188   
9189 ;;;
9190 ;;; MIME Commands
9191 ;;;
9192
9193 (defun gnus-summary-display-buttonized ()
9194   "Display the current article buffer fully MIME-buttonized."
9195   (interactive)
9196   (require 'gnus-art)
9197   (let ((gnus-unbuttonized-mime-types nil))
9198     (gnus-summary-show-article)))
9199
9200 (defun gnus-summary-repair-multipart (article)
9201   "Add a Content-Type header to a multipart article without one."
9202   (interactive (list (gnus-summary-article-number)))
9203   (gnus-with-article article
9204     (message-narrow-to-head)
9205     (goto-char (point-max))
9206     (widen)
9207     (when (search-forward "\n--" nil t)
9208       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9209         (message-narrow-to-head)
9210         (message-remove-header "Mime-Version")
9211         (message-remove-header "Content-Type")
9212         (goto-char (point-max))
9213         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9214                         separator))
9215         (insert "Mime-Version: 1.0\n")
9216         (widen))))
9217   (let (gnus-mark-article-hook)
9218     (gnus-summary-select-article t t nil article)))
9219
9220 (defun gnus-summary-toggle-display-buttonized ()
9221   "Toggle the buttonizing of the article buffer."
9222   (interactive)
9223   (require 'gnus-art)
9224   (if (setq gnus-inhibit-mime-unbuttonizing
9225             (not gnus-inhibit-mime-unbuttonizing))
9226       (let ((gnus-unbuttonized-mime-types nil))
9227         (gnus-summary-show-article))
9228     (gnus-summary-show-article)))
9229     
9230 (gnus-ems-redefine)
9231
9232 (provide 'gnus-sum)
9233
9234 (run-hooks 'gnus-sum-load-hook)
9235
9236 ;;; gnus-sum.el ends here