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