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