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