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