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