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