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