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