*** 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-summary-article-pseudo-p (article)
2034   "Say whether this article is a pseudo article or not."
2035   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2036
2037 (defmacro gnus-summary-article-sparse-p (article)
2038   "Say whether this article is a sparse article or not."
2039   `(memq ,article gnus-newsgroup-sparse))
2040
2041 (defmacro gnus-summary-article-ancient-p (article)
2042   "Say whether this article is a sparse article or not."
2043   `(memq ,article gnus-newsgroup-ancient))
2044
2045 (defun gnus-article-parent-p (number)
2046   "Say whether this article is a parent or not."
2047   (let ((data (gnus-data-find-list number)))
2048     (and (cdr data)                     ; There has to be an article after...
2049          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2050             (gnus-data-level (nth 1 data))))))
2051
2052 (defun gnus-article-children (number)
2053   "Return a list of all children to NUMBER."
2054   (let* ((data (gnus-data-find-list number))
2055          (level (gnus-data-level (car data)))
2056          children)
2057     (setq data (cdr data))
2058     (while (and data
2059                 (= (gnus-data-level (car data)) (1+ level)))
2060       (push (gnus-data-number (car data)) children)
2061       (setq data (cdr data)))
2062     children))
2063
2064 (defmacro gnus-summary-skip-intangible ()
2065   "If the current article is intangible, then jump to a different article."
2066   '(let ((to (get-text-property (point) 'gnus-intangible)))
2067      (and to (gnus-summary-goto-subject to))))
2068
2069 (defmacro gnus-summary-article-intangible-p ()
2070   "Say whether this article is intangible or not."
2071   '(get-text-property (point) 'gnus-intangible))
2072
2073 (defun gnus-article-read-p (article)
2074   "Say whether ARTICLE is read or not."
2075   (not (or (memq article gnus-newsgroup-marked)
2076            (memq article gnus-newsgroup-unreads)
2077            (memq article gnus-newsgroup-unselected)
2078            (memq article gnus-newsgroup-dormant))))
2079
2080 ;; Some summary mode macros.
2081
2082 (defmacro gnus-summary-article-number ()
2083   "The article number of the article on the current line.
2084 If there isn's an article number here, then we return the current
2085 article number."
2086   '(progn
2087      (gnus-summary-skip-intangible)
2088      (or (get-text-property (point) 'gnus-number)
2089          (gnus-summary-last-subject))))
2090
2091 (defmacro gnus-summary-article-header (&optional number)
2092   "Return the header of article NUMBER."
2093   `(gnus-data-header (gnus-data-find
2094                       ,(or number '(gnus-summary-article-number)))))
2095
2096 (defmacro gnus-summary-thread-level (&optional number)
2097   "Return the level of thread that starts with article NUMBER."
2098   `(if (and (eq gnus-summary-make-false-root 'dummy)
2099             (get-text-property (point) 'gnus-intangible))
2100        0
2101      (gnus-data-level (gnus-data-find
2102                        ,(or number '(gnus-summary-article-number))))))
2103
2104 (defmacro gnus-summary-article-mark (&optional number)
2105   "Return the mark of article NUMBER."
2106   `(gnus-data-mark (gnus-data-find
2107                     ,(or number '(gnus-summary-article-number)))))
2108
2109 (defmacro gnus-summary-article-pos (&optional number)
2110   "Return the position of the line of article NUMBER."
2111   `(gnus-data-pos (gnus-data-find
2112                    ,(or number '(gnus-summary-article-number)))))
2113
2114 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2115 (defmacro gnus-summary-article-subject (&optional number)
2116   "Return current subject string or nil if nothing."
2117   `(let ((headers
2118           ,(if number
2119                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2120              '(gnus-data-header (assq (gnus-summary-article-number)
2121                                       gnus-newsgroup-data)))))
2122      (and headers
2123           (vectorp headers)
2124           (mail-header-subject headers))))
2125
2126 (defmacro gnus-summary-article-score (&optional number)
2127   "Return current article score."
2128   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2129                   gnus-newsgroup-scored))
2130        gnus-summary-default-score 0))
2131
2132 (defun gnus-summary-article-children (&optional number)
2133   "Return a list of article numbers that are children of article NUMBER."
2134   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2135          (level (gnus-data-level (car data)))
2136          l children)
2137     (while (and (setq data (cdr data))
2138                 (> (setq l (gnus-data-level (car data))) level))
2139       (and (= (1+ level) l)
2140            (push (gnus-data-number (car data))
2141                  children)))
2142     (nreverse children)))
2143
2144 (defun gnus-summary-article-parent (&optional number)
2145   "Return the article number of the parent of article NUMBER."
2146   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2147                                     (gnus-data-list t)))
2148          (level (gnus-data-level (car data))))
2149     (if (zerop level)
2150         ()                              ; This is a root.
2151       ;; We search until we find an article with a level less than
2152       ;; this one.  That function has to be the parent.
2153       (while (and (setq data (cdr data))
2154                   (not (< (gnus-data-level (car data)) level))))
2155       (and data (gnus-data-number (car data))))))
2156
2157 (defun gnus-unread-mark-p (mark)
2158   "Say whether MARK is the unread mark."
2159   (= mark gnus-unread-mark))
2160
2161 (defun gnus-read-mark-p (mark)
2162   "Say whether MARK is one of the marks that mark as read.
2163 This is all marks except unread, ticked, dormant, and expirable."
2164   (not (or (= mark gnus-unread-mark)
2165            (= mark gnus-ticked-mark)
2166            (= mark gnus-dormant-mark)
2167            (= mark gnus-expirable-mark))))
2168
2169 (defmacro gnus-article-mark (number)
2170   "Return the MARK of article NUMBER.
2171 This macro should only be used when computing the mark the \"first\"
2172 time; i.e., when generating the summary lines.  After that,
2173 `gnus-summary-article-mark' should be used to examine the
2174 marks of articles."
2175   `(cond
2176     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2177     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2178     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2179     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2180     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2181     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2182     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2183     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2184            gnus-ancient-mark))))
2185
2186 ;; Saving hidden threads.
2187
2188 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2189 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2190
2191 (defmacro gnus-save-hidden-threads (&rest forms)
2192   "Save hidden threads, eval FORMS, and restore the hidden threads."
2193   (let ((config (make-symbol "config")))
2194     `(let ((,config (gnus-hidden-threads-configuration)))
2195        (unwind-protect
2196            (save-excursion
2197              ,@forms)
2198          (gnus-restore-hidden-threads-configuration ,config)))))
2199
2200 (defun gnus-data-compute-positions ()
2201   "Compute the positions of all articles."
2202   (setq gnus-newsgroup-data-reverse nil)
2203   (let ((data gnus-newsgroup-data))
2204     (save-excursion
2205       (gnus-save-hidden-threads
2206         (gnus-summary-show-all-threads)
2207         (goto-char (point-min))
2208         (while data
2209           (while (get-text-property (point) 'gnus-intangible)
2210             (forward-line 1))
2211           (gnus-data-set-pos (car data) (+ (point) 3))
2212           (setq data (cdr data))
2213           (forward-line 1))))))
2214
2215 (defun gnus-hidden-threads-configuration ()
2216   "Return the current hidden threads configuration."
2217   (save-excursion
2218     (let (config)
2219       (goto-char (point-min))
2220       (while (search-forward "\r" nil t)
2221         (push (1- (point)) config))
2222       config)))
2223
2224 (defun gnus-restore-hidden-threads-configuration (config)
2225   "Restore hidden threads configuration from CONFIG."
2226   (let (point buffer-read-only)
2227     (while (setq point (pop config))
2228       (when (and (< point (point-max))
2229                  (goto-char point)
2230                  (= (following-char) ?\n))
2231         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2232
2233 ;; Various summary mode internalish functions.
2234
2235 (defun gnus-mouse-pick-article (e)
2236   (interactive "e")
2237   (mouse-set-point e)
2238   (gnus-summary-next-page nil t))
2239
2240 (defun gnus-summary-set-display-table ()
2241   ;; Change the display table.  Odd characters have a tendency to mess
2242   ;; up nicely formatted displays - we make all possible glyphs
2243   ;; display only a single character.
2244
2245   ;; We start from the standard display table, if any.
2246   (let ((table (or (copy-sequence standard-display-table)
2247                    (make-display-table)))
2248         (i 32))
2249     ;; Nix out all the control chars...
2250     (while (>= (setq i (1- i)) 0)
2251       (aset table i [??]))
2252     ;; ... but not newline and cr, of course.  (cr is necessary for the
2253     ;; selective display).
2254     (aset table ?\n nil)
2255     (aset table ?\r nil)
2256     ;; We keep TAB as well.
2257     (aset table ?\t nil)
2258     ;; We nix out any glyphs over 126 that are not set already.
2259     (let ((i 256))
2260       (while (>= (setq i (1- i)) 127)
2261         ;; Only modify if the entry is nil.
2262         (unless (aref table i)
2263           (aset table i [??]))))
2264     (setq buffer-display-table table)))
2265
2266 (defun gnus-summary-setup-buffer (group)
2267   "Initialize summary buffer."
2268   (let ((buffer (concat "*Summary " group "*")))
2269     (if (get-buffer buffer)
2270         (progn
2271           (set-buffer buffer)
2272           (setq gnus-summary-buffer (current-buffer))
2273           (not gnus-newsgroup-prepared))
2274       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2275       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2276       (gnus-summary-mode group)
2277       (when gnus-carpal
2278         (gnus-carpal-setup-buffer 'summary))
2279       (unless gnus-single-article-buffer
2280         (make-local-variable 'gnus-article-buffer)
2281         (make-local-variable 'gnus-article-current)
2282         (make-local-variable 'gnus-original-article-buffer))
2283       (setq gnus-newsgroup-name group)
2284       t)))
2285
2286 (defun gnus-set-global-variables ()
2287   ;; Set the global equivalents of the summary buffer-local variables
2288   ;; to the latest values they had.  These reflect the summary buffer
2289   ;; that was in action when the last article was fetched.
2290   (when (eq major-mode 'gnus-summary-mode)
2291     (setq gnus-summary-buffer (current-buffer))
2292     (let ((name gnus-newsgroup-name)
2293           (marked gnus-newsgroup-marked)
2294           (unread gnus-newsgroup-unreads)
2295           (headers gnus-current-headers)
2296           (data gnus-newsgroup-data)
2297           (summary gnus-summary-buffer)
2298           (article-buffer gnus-article-buffer)
2299           (original gnus-original-article-buffer)
2300           (gac gnus-article-current)
2301           (reffed gnus-reffed-article-number)
2302           (score-file gnus-current-score-file))
2303       (save-excursion
2304         (set-buffer gnus-group-buffer)
2305         (setq gnus-newsgroup-name name
2306               gnus-newsgroup-marked marked
2307               gnus-newsgroup-unreads unread
2308               gnus-current-headers headers
2309               gnus-newsgroup-data data
2310               gnus-article-current gac
2311               gnus-summary-buffer summary
2312               gnus-article-buffer article-buffer
2313               gnus-original-article-buffer original
2314               gnus-reffed-article-number reffed
2315               gnus-current-score-file score-file)
2316         ;; The article buffer also has local variables.
2317         (when (gnus-buffer-live-p gnus-article-buffer)
2318           (set-buffer gnus-article-buffer)
2319           (setq gnus-summary-buffer summary))))))
2320
2321 (defun gnus-summary-article-unread-p (article)
2322   "Say whether ARTICLE is unread or not."
2323   (memq article gnus-newsgroup-unreads))
2324
2325 (defun gnus-summary-first-article-p (&optional article)
2326   "Return whether ARTICLE is the first article in the buffer."
2327   (if (not (setq article (or article (gnus-summary-article-number))))
2328       nil
2329     (eq article (caar gnus-newsgroup-data))))
2330
2331 (defun gnus-summary-last-article-p (&optional article)
2332   "Return whether ARTICLE is the last article in the buffer."
2333   (if (not (setq article (or article (gnus-summary-article-number))))
2334       t         ; All non-existent numbers are the last article.  :-)
2335     (not (cdr (gnus-data-find-list article)))))
2336
2337 (defun gnus-make-thread-indent-array ()
2338   (let ((n 200))
2339     (unless (and gnus-thread-indent-array
2340                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2341       (setq gnus-thread-indent-array (make-vector 201 "")
2342             gnus-thread-indent-array-level gnus-thread-indent-level)
2343       (while (>= n 0)
2344         (aset gnus-thread-indent-array n
2345               (make-string (* n gnus-thread-indent-level) ? ))
2346         (setq n (1- n))))))
2347
2348 (defun gnus-update-summary-mark-positions ()
2349   "Compute where the summary marks are to go."
2350   (save-excursion
2351     (when (gnus-buffer-exists-p gnus-summary-buffer)
2352       (set-buffer gnus-summary-buffer))
2353     (let ((gnus-replied-mark 129)
2354           (gnus-score-below-mark 130)
2355           (gnus-score-over-mark 130)
2356           (gnus-download-mark 131)
2357           (spec gnus-summary-line-format-spec)
2358           gnus-visual pos)
2359       (save-excursion
2360         (gnus-set-work-buffer)
2361         (let ((gnus-summary-line-format-spec spec)
2362               (gnus-newsgroup-downloadable '((0 . t))))
2363           (gnus-summary-insert-line
2364            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2365           (goto-char (point-min))
2366           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2367                                              (- (point) 2)))))
2368           (goto-char (point-min))
2369           (push (cons 'replied (and (search-forward "\201" nil t)
2370                                     (- (point) 2)))
2371                 pos)
2372           (goto-char (point-min))
2373           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2374                 pos)
2375           (goto-char (point-min))
2376           (push (cons 'download
2377                       (and (search-forward "\203" nil t) (- (point) 2)))
2378                 pos)))
2379       (setq gnus-summary-mark-positions pos))))
2380
2381 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2382   "Insert a dummy root in the summary buffer."
2383   (beginning-of-line)
2384   (gnus-add-text-properties
2385    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2386    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2387
2388 (defun gnus-summary-insert-line (gnus-tmp-header
2389                                  gnus-tmp-level gnus-tmp-current
2390                                  gnus-tmp-unread gnus-tmp-replied
2391                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2392                                  &optional gnus-tmp-dummy gnus-tmp-score
2393                                  gnus-tmp-process)
2394   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2395          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2396          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2397          (gnus-tmp-score-char
2398           (if (or (null gnus-summary-default-score)
2399                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2400                       gnus-summary-zcore-fuzz))
2401               ? 
2402             (if (< gnus-tmp-score gnus-summary-default-score)
2403                 gnus-score-below-mark gnus-score-over-mark)))
2404          (gnus-tmp-replied
2405           (cond (gnus-tmp-process gnus-process-mark)
2406                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2407                  gnus-cached-mark)
2408                 (gnus-tmp-replied gnus-replied-mark)
2409                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2410                  gnus-saved-mark)
2411                 (t gnus-unread-mark)))
2412          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2413          (gnus-tmp-name
2414           (cond
2415            ((string-match "<[^>]+> *$" gnus-tmp-from)
2416             (let ((beg (match-beginning 0)))
2417               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2418                        (substring gnus-tmp-from (1+ (match-beginning 0))
2419                                   (1- (match-end 0))))
2420                   (substring gnus-tmp-from 0 beg))))
2421            ((string-match "(.+)" gnus-tmp-from)
2422             (substring gnus-tmp-from
2423                        (1+ (match-beginning 0)) (1- (match-end 0))))
2424            (t gnus-tmp-from)))
2425          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2426          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2427          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2428          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2429          (buffer-read-only nil))
2430     (when (string= gnus-tmp-name "")
2431       (setq gnus-tmp-name gnus-tmp-from))
2432     (unless (numberp gnus-tmp-lines)
2433       (setq gnus-tmp-lines 0))
2434     (gnus-put-text-property-excluding-characters-with-faces
2435      (point)
2436      (progn (eval gnus-summary-line-format-spec) (point))
2437      'gnus-number gnus-tmp-number)
2438     (when (gnus-visual-p 'summary-highlight 'highlight)
2439       (forward-line -1)
2440       (gnus-run-hooks 'gnus-summary-update-hook)
2441       (forward-line 1))))
2442
2443 (defun gnus-summary-update-line (&optional dont-update)
2444   ;; Update summary line after change.
2445   (when (and gnus-summary-default-score
2446              (not gnus-summary-inhibit-highlight))
2447     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2448            (article (gnus-summary-article-number))
2449            (score (gnus-summary-article-score article)))
2450       (unless dont-update
2451         (if (and gnus-summary-mark-below
2452                  (< (gnus-summary-article-score)
2453                     gnus-summary-mark-below))
2454             ;; This article has a low score, so we mark it as read.
2455             (when (memq article gnus-newsgroup-unreads)
2456               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2457           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2458             ;; This article was previously marked as read on account
2459             ;; of a low score, but now it has risen, so we mark it as
2460             ;; unread.
2461             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2462         (gnus-summary-update-mark
2463          (if (or (null gnus-summary-default-score)
2464                  (<= (abs (- score gnus-summary-default-score))
2465                      gnus-summary-zcore-fuzz))
2466              ? 
2467            (if (< score gnus-summary-default-score)
2468                gnus-score-below-mark gnus-score-over-mark))
2469          'score))
2470       ;; Do visual highlighting.
2471       (when (gnus-visual-p 'summary-highlight 'highlight)
2472         (gnus-run-hooks 'gnus-summary-update-hook)))))
2473
2474 (defvar gnus-tmp-new-adopts nil)
2475
2476 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2477   "Return the number of articles in THREAD.
2478 This may be 0 in some cases -- if none of the articles in
2479 the thread are to be displayed."
2480   (let* ((number
2481           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2482           (cond
2483            ((not (listp thread))
2484             1)
2485            ((and (consp thread) (cdr thread))
2486             (apply
2487              '+ 1 (mapcar
2488                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2489            ((null thread)
2490             1)
2491            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2492             1)
2493            (t 0))))
2494     (when (and level (zerop level) gnus-tmp-new-adopts)
2495       (incf number
2496             (apply '+ (mapcar
2497                        'gnus-summary-number-of-articles-in-thread
2498                        gnus-tmp-new-adopts))))
2499     (if char
2500         (if (> number 1) gnus-not-empty-thread-mark
2501           gnus-empty-thread-mark)
2502       number)))
2503
2504 (defun gnus-summary-set-local-parameters (group)
2505   "Go through the local params of GROUP and set all variable specs in that list."
2506   (let ((params (gnus-group-find-parameter group))
2507         elem)
2508     (while params
2509       (setq elem (car params)
2510             params (cdr params))
2511       (and (consp elem)                 ; Has to be a cons.
2512            (consp (cdr elem))           ; The cdr has to be a list.
2513            (symbolp (car elem))         ; Has to be a symbol in there.
2514            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2515            (ignore-errors               ; So we set it.
2516              (make-local-variable (car elem))
2517              (set (car elem) (eval (nth 1 elem))))))))
2518
2519 (defun gnus-summary-read-group (group &optional show-all no-article
2520                                       kill-buffer no-display backward
2521                                       select-articles)
2522   "Start reading news in newsgroup GROUP.
2523 If SHOW-ALL is non-nil, already read articles are also listed.
2524 If NO-ARTICLE is non-nil, no article is selected initially.
2525 If NO-DISPLAY, don't generate a summary buffer."
2526   (let (result)
2527     (while (and group
2528                 (null (setq result
2529                             (let ((gnus-auto-select-next nil))
2530                               (or (gnus-summary-read-group-1
2531                                    group show-all no-article
2532                                    kill-buffer no-display
2533                                    select-articles)
2534                                   (setq show-all nil
2535                                    select-articles nil)))))
2536                 (eq gnus-auto-select-next 'quietly))
2537       (set-buffer gnus-group-buffer)
2538       ;; The entry function called above goes to the next
2539       ;; group automatically, so we go two groups back
2540       ;; if we are searching for the previous group.
2541       (when backward
2542         (gnus-group-prev-unread-group 2))
2543       (if (not (equal group (gnus-group-group-name)))
2544           (setq group (gnus-group-group-name))
2545         (setq group nil)))
2546     result))
2547
2548 (defun gnus-summary-read-group-1 (group show-all no-article
2549                                         kill-buffer no-display
2550                                         &optional select-articles)
2551   ;; Killed foreign groups can't be entered.
2552   (when (and (not (gnus-group-native-p group))
2553              (not (gnus-gethash group gnus-newsrc-hashtb)))
2554     (error "Dead non-native groups can't be entered"))
2555   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2556   (let* ((new-group (gnus-summary-setup-buffer group))
2557          (quit-config (gnus-group-quit-config group))
2558          (did-select (and new-group (gnus-select-newsgroup
2559                                      group show-all select-articles))))
2560     (cond
2561      ;; This summary buffer exists already, so we just select it.
2562      ((not new-group)
2563       (gnus-set-global-variables)
2564       (when kill-buffer
2565         (gnus-kill-or-deaden-summary kill-buffer))
2566       (gnus-configure-windows 'summary 'force)
2567       (gnus-set-mode-line 'summary)
2568       (gnus-summary-position-point)
2569       (message "")
2570       t)
2571      ;; We couldn't select this group.
2572      ((null did-select)
2573       (when (and (eq major-mode 'gnus-summary-mode)
2574                  (not (equal (current-buffer) kill-buffer)))
2575         (kill-buffer (current-buffer))
2576         (if (not quit-config)
2577             (progn
2578               ;; Update the info -- marks might need to be removed,
2579               ;; for instance.
2580               (gnus-summary-update-info)
2581               (set-buffer gnus-group-buffer)
2582               (gnus-group-jump-to-group group)
2583               (gnus-group-next-unread-group 1))
2584           (gnus-handle-ephemeral-exit quit-config)))
2585       (gnus-message 3 "Can't select group")
2586       nil)
2587      ;; The user did a `C-g' while prompting for number of articles,
2588      ;; so we exit this group.
2589      ((eq did-select 'quit)
2590       (and (eq major-mode 'gnus-summary-mode)
2591            (not (equal (current-buffer) kill-buffer))
2592            (kill-buffer (current-buffer)))
2593       (when kill-buffer
2594         (gnus-kill-or-deaden-summary kill-buffer))
2595       (if (not quit-config)
2596           (progn
2597             (set-buffer gnus-group-buffer)
2598             (gnus-group-jump-to-group group)
2599             (gnus-group-next-unread-group 1)
2600             (gnus-configure-windows 'group 'force))
2601         (gnus-handle-ephemeral-exit quit-config))
2602       ;; Finally signal the quit.
2603       (signal 'quit nil))
2604      ;; The group was successfully selected.
2605      (t
2606       (gnus-set-global-variables)
2607       ;; Save the active value in effect when the group was entered.
2608       (setq gnus-newsgroup-active
2609             (gnus-copy-sequence
2610              (gnus-active gnus-newsgroup-name)))
2611       ;; You can change the summary buffer in some way with this hook.
2612       (gnus-run-hooks 'gnus-select-group-hook)
2613       ;; Set any local variables in the group parameters.
2614       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2615       (gnus-update-format-specifications
2616        nil 'summary 'summary-mode 'summary-dummy)
2617       ;; Do score processing.
2618       (when gnus-use-scoring
2619         (gnus-possibly-score-headers))
2620       ;; Check whether to fill in the gaps in the threads.
2621       (when gnus-build-sparse-threads
2622         (gnus-build-sparse-threads))
2623       ;; Find the initial limit.
2624       (if gnus-show-threads
2625           (if show-all
2626               (let ((gnus-newsgroup-dormant nil))
2627                 (gnus-summary-initial-limit show-all))
2628             (gnus-summary-initial-limit show-all))
2629         (setq gnus-newsgroup-limit
2630               (mapcar
2631                (lambda (header) (mail-header-number header))
2632                gnus-newsgroup-headers)))
2633       ;; Generate the summary buffer.
2634       (unless no-display
2635         (gnus-summary-prepare))
2636       (when gnus-use-trees
2637         (gnus-tree-open group)
2638         (setq gnus-summary-highlight-line-function
2639               'gnus-tree-highlight-article))
2640       ;; If the summary buffer is empty, but there are some low-scored
2641       ;; articles or some excluded dormants, we include these in the
2642       ;; buffer.
2643       (when (and (zerop (buffer-size))
2644                  (not no-display))
2645         (cond (gnus-newsgroup-dormant
2646                (gnus-summary-limit-include-dormant))
2647               ((and gnus-newsgroup-scored show-all)
2648                (gnus-summary-limit-include-expunged t))))
2649       ;; Function `gnus-apply-kill-file' must be called in this hook.
2650       (gnus-run-hooks 'gnus-apply-kill-hook)
2651       (if (and (zerop (buffer-size))
2652                (not no-display))
2653           (progn
2654             ;; This newsgroup is empty.
2655             (gnus-summary-catchup-and-exit nil t)
2656             (gnus-message 6 "No unread news")
2657             (when kill-buffer
2658               (gnus-kill-or-deaden-summary kill-buffer))
2659             ;; Return nil from this function.
2660             nil)
2661         ;; Hide conversation thread subtrees.  We cannot do this in
2662         ;; gnus-summary-prepare-hook since kill processing may not
2663         ;; work with hidden articles.
2664         (and gnus-show-threads
2665              gnus-thread-hide-subtree
2666              (gnus-summary-hide-all-threads))
2667         (when kill-buffer
2668           (gnus-kill-or-deaden-summary kill-buffer))
2669         ;; Show first unread article if requested.
2670         (if (and (not no-article)
2671                  (not no-display)
2672                  gnus-newsgroup-unreads
2673                  gnus-auto-select-first)
2674             (unless (if (eq gnus-auto-select-first 'best)
2675                         (gnus-summary-best-unread-article)
2676                       (gnus-summary-first-unread-article))
2677               (gnus-configure-windows 'summary))
2678           ;; Don't select any articles, just move point to the first
2679           ;; article in the group.
2680           (goto-char (point-min))
2681           (gnus-summary-position-point)
2682           (gnus-configure-windows 'summary 'force)
2683           (gnus-set-mode-line 'summary))        
2684         (when (get-buffer-window gnus-group-buffer t)
2685           ;; Gotta use windows, because recenter does weird stuff if
2686           ;; the current buffer ain't the displayed window.
2687           (let ((owin (selected-window)))
2688             (select-window (get-buffer-window gnus-group-buffer t))
2689             (when (gnus-group-goto-group group)
2690               (recenter))
2691             (select-window owin)))
2692         ;; Mark this buffer as "prepared".
2693         (setq gnus-newsgroup-prepared t)
2694         (gnus-run-hooks 'gnus-summary-prepared-hook)
2695         t)))))
2696
2697 (defun gnus-summary-prepare ()
2698   "Generate the summary buffer."
2699   (interactive)
2700   (let ((buffer-read-only nil))
2701     (erase-buffer)
2702     (setq gnus-newsgroup-data nil
2703           gnus-newsgroup-data-reverse nil)
2704     (gnus-run-hooks 'gnus-summary-generate-hook)
2705     ;; Generate the buffer, either with threads or without.
2706     (when gnus-newsgroup-headers
2707       (gnus-summary-prepare-threads
2708        (if gnus-show-threads
2709            (gnus-sort-gathered-threads
2710             (funcall gnus-summary-thread-gathering-function
2711                      (gnus-sort-threads
2712                       (gnus-cut-threads (gnus-make-threads)))))
2713          ;; Unthreaded display.
2714          (gnus-sort-articles gnus-newsgroup-headers))))
2715     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2716     ;; Call hooks for modifying summary buffer.
2717     (goto-char (point-min))
2718     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2719
2720 (defsubst gnus-general-simplify-subject (subject)
2721   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2722   (setq subject
2723         (cond
2724          ;; Truncate the subject.
2725          (gnus-simplify-subject-functions
2726           (gnus-map-function gnus-simplify-subject-functions subject))
2727          ((numberp gnus-summary-gather-subject-limit)
2728           (setq subject (gnus-simplify-subject-re subject))
2729           (if (> (length subject) gnus-summary-gather-subject-limit)
2730               (substring subject 0 gnus-summary-gather-subject-limit)
2731             subject))
2732          ;; Fuzzily simplify it.
2733          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2734           (gnus-simplify-subject-fuzzy subject))
2735          ;; Just remove the leading "Re:".
2736          (t
2737           (gnus-simplify-subject-re subject))))
2738
2739   (if (and gnus-summary-gather-exclude-subject
2740            (string-match gnus-summary-gather-exclude-subject subject))
2741       nil                               ; This article shouldn't be gathered
2742     subject))
2743
2744 (defun gnus-summary-simplify-subject-query ()
2745   "Query where the respool algorithm would put this article."
2746   (interactive)
2747   (gnus-summary-select-article)
2748   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2749
2750 (defun gnus-gather-threads-by-subject (threads)
2751   "Gather threads by looking at Subject headers."
2752   (if (not gnus-summary-make-false-root)
2753       threads
2754     (let ((hashtb (gnus-make-hashtable 1024))
2755           (prev threads)
2756           (result threads)
2757           subject hthread whole-subject)
2758       (while threads
2759         (setq subject (gnus-general-simplify-subject
2760                        (setq whole-subject (mail-header-subject
2761                                             (caar threads)))))
2762         (when subject
2763           (if (setq hthread (gnus-gethash subject hashtb))
2764               (progn
2765                 ;; We enter a dummy root into the thread, if we
2766                 ;; haven't done that already.
2767                 (unless (stringp (caar hthread))
2768                   (setcar hthread (list whole-subject (car hthread))))
2769                 ;; We add this new gathered thread to this gathered
2770                 ;; thread.
2771                 (setcdr (car hthread)
2772                         (nconc (cdar hthread) (list (car threads))))
2773                 ;; Remove it from the list of threads.
2774                 (setcdr prev (cdr threads))
2775                 (setq threads prev))
2776             ;; Enter this thread into the hash table.
2777             (gnus-sethash subject threads hashtb)))
2778         (setq prev threads)
2779         (setq threads (cdr threads)))
2780       result)))
2781
2782 (defun gnus-gather-threads-by-references (threads)
2783   "Gather threads by looking at References headers."
2784   (let ((idhashtb (gnus-make-hashtable 1024))
2785         (thhashtb (gnus-make-hashtable 1024))
2786         (prev threads)
2787         (result threads)
2788         ids references id gthread gid entered ref)
2789     (while threads
2790       (when (setq references (mail-header-references (caar threads)))
2791         (setq id (mail-header-id (caar threads))
2792               ids (gnus-split-references references)
2793               entered nil)
2794         (while (setq ref (pop ids))
2795           (setq ids (delete ref ids))
2796           (if (not (setq gid (gnus-gethash ref idhashtb)))
2797               (progn
2798                 (gnus-sethash ref id idhashtb)
2799                 (gnus-sethash id threads thhashtb))
2800             (setq gthread (gnus-gethash gid thhashtb))
2801             (unless entered
2802               ;; We enter a dummy root into the thread, if we
2803               ;; haven't done that already.
2804               (unless (stringp (caar gthread))
2805                 (setcar gthread (list (mail-header-subject (caar gthread))
2806                                       (car gthread))))
2807               ;; We add this new gathered thread to this gathered
2808               ;; thread.
2809               (setcdr (car gthread)
2810                       (nconc (cdar gthread) (list (car threads)))))
2811             ;; Add it into the thread hash table.
2812             (gnus-sethash id gthread thhashtb)
2813             (setq entered t)
2814             ;; Remove it from the list of threads.
2815             (setcdr prev (cdr threads))
2816             (setq threads prev))))
2817       (setq prev threads)
2818       (setq threads (cdr threads)))
2819     result))
2820
2821 (defun gnus-sort-gathered-threads (threads)
2822   "Sort subtreads inside each gathered thread by article number."
2823   (let ((result threads))
2824     (while threads
2825       (when (stringp (caar threads))
2826         (setcdr (car threads)
2827                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2828       (setq threads (cdr threads)))
2829     result))
2830
2831 (defun gnus-thread-loop-p (root thread)
2832   "Say whether ROOT is in THREAD."
2833   (let ((stack (list thread))
2834         (infloop 0)
2835         th)
2836     (while (setq thread (pop stack))
2837       (setq th (cdr thread))
2838       (while (and th
2839                   (not (eq (caar th) root)))
2840         (pop th))
2841       (if th
2842           ;; We have found a loop.
2843           (let (ref-dep)
2844             (setcdr thread (delq (car th) (cdr thread)))
2845             (if (boundp (setq ref-dep (intern "none"
2846                                               gnus-newsgroup-dependencies)))
2847                 (setcdr (symbol-value ref-dep)
2848                         (nconc (cdr (symbol-value ref-dep))
2849                                (list (car th))))
2850               (set ref-dep (list nil (car th))))
2851             (setq infloop 1
2852                   stack nil))
2853         ;; Push all the subthreads onto the stack.
2854         (push (cdr thread) stack)))
2855     infloop))
2856
2857 (defun gnus-make-threads ()
2858   "Go through the dependency hashtb and find the roots.  Return all threads."
2859   (let (threads)
2860     (while (catch 'infloop
2861              (mapatoms
2862               (lambda (refs)
2863                 ;; Deal with self-referencing References loops.
2864                 (when (and (car (symbol-value refs))
2865                            (not (zerop
2866                                  (apply
2867                                   '+
2868                                   (mapcar
2869                                    (lambda (thread)
2870                                      (gnus-thread-loop-p
2871                                       (car (symbol-value refs)) thread))
2872                                    (cdr (symbol-value refs)))))))
2873                   (setq threads nil)
2874                   (throw 'infloop t))
2875                 (unless (car (symbol-value refs))
2876                   ;; These threads do not refer back to any other articles,
2877                   ;; so they're roots.
2878                   (setq threads (append (cdr (symbol-value refs)) threads))))
2879               gnus-newsgroup-dependencies)))
2880     threads))
2881
2882 ;; Build the thread tree.
2883 (defun gnus-dependencies-add-header (header dependencies force-new)
2884   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2885
2886 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2887 if it was already present.
2888
2889 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
2890 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
2891 Message-IDs will be renamed be renamed to a unique Message-ID before
2892 being entered.
2893
2894 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
2895   (let* ((id (mail-header-id header))
2896          (id-dep (and id (intern id dependencies)))
2897          ref ref-dep ref-header)
2898     ;; Enter this `header' in the `dependencies' table.
2899     (cond
2900      ((not id-dep)
2901       (setq header nil))
2902      ;; The first two cases do the normal part: enter a new `header'
2903      ;; in the `dependencies' table.
2904      ((not (boundp id-dep))
2905       (set id-dep (list header)))
2906      ((null (car (symbol-value id-dep)))
2907       (setcar (symbol-value id-dep) header))
2908
2909      ;; From here the `header' was already present in the
2910      ;; `dependencies' table.
2911      (force-new
2912       ;; Overrides an existing entry;
2913       ;; just set the header part of the entry.
2914       (setcar (symbol-value id-dep) header))
2915
2916      ;; Renames the existing `header' to a unique Message-ID.
2917      ((not gnus-summary-ignore-duplicates)
2918       ;; An article with this Message-ID has already been seen.
2919       ;; We rename the Message-ID.
2920       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
2921            (list header))
2922       (mail-header-set-id header id))
2923
2924      ;; The last case ignores an existing entry, except it adds any
2925      ;; additional Xrefs (in case the two articles came from different
2926      ;; servers.
2927      ;; Also sets `header' to `nil' meaning that the `dependencies'
2928      ;; table was *not* modified.
2929      (t
2930       (mail-header-set-xref
2931        (car (symbol-value id-dep))
2932        (concat (or (mail-header-xref (car (symbol-value id-dep)))
2933                    "")
2934                (or (mail-header-xref header) "")))
2935       (setq header nil)))
2936
2937     (when header
2938       ;; First check if that we are not creating a References loop.
2939       (setq ref (gnus-parent-id (mail-header-references header)))
2940       (while (and ref
2941                   (setq ref-dep (intern-soft ref dependencies))
2942                   (boundp ref-dep)
2943                   (setq ref-header (car (symbol-value ref-dep))))
2944         (if (string= id ref)
2945             ;; Yuk!  This is a reference loop.  Make the article be a
2946             ;; root article.
2947             (progn
2948               (mail-header-set-references (car (symbol-value id-dep)) "none")
2949               (setq ref nil))
2950           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
2951       (setq ref (gnus-parent-id (mail-header-references header)))
2952       (setq ref-dep (intern (or ref "none") dependencies))
2953       (if (boundp ref-dep)
2954           (setcdr (symbol-value ref-dep)
2955                   (nconc (cdr (symbol-value ref-dep))
2956                          (list (symbol-value id-dep))))
2957         (set ref-dep (list nil (symbol-value id-dep)))))
2958     header))
2959
2960 (defun gnus-build-sparse-threads ()
2961   (let ((headers gnus-newsgroup-headers)
2962         (gnus-summary-ignore-duplicates t)
2963         header references generation relations
2964         subject child end new-child date)
2965     ;; First we create an alist of generations/relations, where
2966     ;; generations is how much we trust the relation, and the relation
2967     ;; is parent/child.
2968     (gnus-message 7 "Making sparse threads...")
2969     (save-excursion
2970       (nnheader-set-temp-buffer " *gnus sparse threads*")
2971       (while (setq header (pop headers))
2972         (when (and (setq references (mail-header-references header))
2973                    (not (string= references "")))
2974           (insert references)
2975           (setq child (mail-header-id header)
2976                 subject (mail-header-subject header)
2977                 date (mail-header-date header)
2978                 generation 0)
2979           (while (search-backward ">" nil t)
2980             (setq end (1+ (point)))
2981             (when (search-backward "<" nil t)
2982               (setq new-child (buffer-substring (point) end))
2983               (push (list (incf generation)
2984                           child (setq child new-child)
2985                           subject date)
2986                     relations)))
2987           (when child
2988             (push (list (1+ generation) child nil subject) relations))
2989           (erase-buffer)))
2990       (kill-buffer (current-buffer)))
2991     ;; Sort over trustworthiness.
2992     (mapcar
2993      (lambda (relation)
2994        (when (gnus-dependencies-add-header
2995               (make-full-mail-header
2996                gnus-reffed-article-number
2997                (nth 3 relation) "" (or (nth 4 relation) "")
2998                (nth 1 relation)
2999                (or (nth 2 relation) "") 0 0 "")
3000               gnus-newsgroup-dependencies nil)
3001          (push gnus-reffed-article-number gnus-newsgroup-limit)
3002          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3003          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3004                gnus-newsgroup-reads)
3005          (decf gnus-reffed-article-number)))
3006      (sort relations 'car-less-than-car))
3007     (gnus-message 7 "Making sparse threads...done")))
3008
3009 (defun gnus-build-old-threads ()
3010   ;; Look at all the articles that refer back to old articles, and
3011   ;; fetch the headers for the articles that aren't there.  This will
3012   ;; build complete threads - if the roots haven't been expired by the
3013   ;; server, that is.
3014   (let (id heads)
3015     (mapatoms
3016      (lambda (refs)
3017        (when (not (car (symbol-value refs)))
3018          (setq heads (cdr (symbol-value refs)))
3019          (while heads
3020            (if (memq (mail-header-number (caar heads))
3021                      gnus-newsgroup-dormant)
3022                (setq heads (cdr heads))
3023              (setq id (symbol-name refs))
3024              (while (and (setq id (gnus-build-get-header id))
3025                          (not (car (gnus-id-to-thread id)))))
3026              (setq heads nil)))))
3027      gnus-newsgroup-dependencies)))
3028
3029 ;; The following macros and functions were written by Felix Lee
3030 ;; <flee@cse.psu.edu>.
3031
3032 (defmacro gnus-nov-read-integer ()
3033   '(prog1
3034        (if (= (following-char) ?\t)
3035            0
3036          (let ((num (ignore-errors (read buffer))))
3037            (if (numberp num) num 0)))
3038      (unless (eobp)
3039        (search-forward "\t" eol 'move))))
3040
3041 (defmacro gnus-nov-skip-field ()
3042   '(search-forward "\t" eol 'move))
3043
3044 (defmacro gnus-nov-field ()
3045   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3046
3047 ;; This function has to be called with point after the article number
3048 ;; on the beginning of the line.
3049 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3050   (let ((eol (gnus-point-at-eol))
3051         (buffer (current-buffer))
3052         header)
3053
3054     ;; overview: [num subject from date id refs chars lines misc]
3055     (unwind-protect
3056         (progn
3057           (narrow-to-region (point) eol)
3058           (unless (eobp)
3059             (forward-char))
3060
3061           (setq header
3062                 (make-full-mail-header
3063                  number                 ; number
3064                  (rfc2047-decode-string (gnus-nov-field)) ; subject
3065                  (rfc2047-decode-string (gnus-nov-field)) ; from
3066                  (gnus-nov-field)       ; date
3067                  (or (gnus-nov-field)
3068                      (nnheader-generate-fake-message-id)) ; id
3069                  (gnus-nov-field)       ; refs
3070                  (gnus-nov-read-integer) ; chars
3071                  (gnus-nov-read-integer) ; lines
3072                  (unless (= (following-char) ?\n)
3073                    (gnus-nov-field))))) ; misc
3074
3075       (widen))
3076
3077     (when gnus-alter-header-function
3078       (funcall gnus-alter-header-function header))
3079     (gnus-dependencies-add-header header dependencies force-new)))
3080
3081 (defun gnus-build-get-header (id)
3082   ;; Look through the buffer of NOV lines and find the header to
3083   ;; ID.  Enter this line into the dependencies hash table, and return
3084   ;; the id of the parent article (if any).
3085   (let ((deps gnus-newsgroup-dependencies)
3086         found header)
3087     (prog1
3088         (save-excursion
3089           (set-buffer nntp-server-buffer)
3090           (let ((case-fold-search nil))
3091             (goto-char (point-min))
3092             (while (and (not found)
3093                         (search-forward id nil t))
3094               (beginning-of-line)
3095               (setq found (looking-at
3096                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3097                                    (regexp-quote id))))
3098               (or found (beginning-of-line 2)))
3099             (when found
3100               (beginning-of-line)
3101               (and
3102                (setq header (gnus-nov-parse-line
3103                              (read (current-buffer)) deps))
3104                (gnus-parent-id (mail-header-references header))))))
3105       (when header
3106         (let ((number (mail-header-number header)))
3107           (push number gnus-newsgroup-limit)
3108           (push header gnus-newsgroup-headers)
3109           (if (memq number gnus-newsgroup-unselected)
3110               (progn
3111                 (push number gnus-newsgroup-unreads)
3112                 (setq gnus-newsgroup-unselected
3113                       (delq number gnus-newsgroup-unselected)))
3114             (push number gnus-newsgroup-ancient)))))))
3115
3116 (defun gnus-build-all-threads ()
3117   "Read all the headers."
3118   (let ((gnus-summary-ignore-duplicates t)
3119         (dependencies gnus-newsgroup-dependencies)
3120         header article)
3121     (save-excursion
3122       (set-buffer nntp-server-buffer)
3123       (let ((case-fold-search nil))
3124         (goto-char (point-min))
3125         (while (not (eobp))
3126           (ignore-errors
3127             (setq article (read (current-buffer))
3128                   header (gnus-nov-parse-line
3129                           article dependencies)))
3130           (when header
3131             (save-excursion
3132               (set-buffer gnus-summary-buffer)
3133               (push header gnus-newsgroup-headers)
3134               (if (memq (setq article (mail-header-number header))
3135                         gnus-newsgroup-unselected)
3136                   (progn
3137                     (push article gnus-newsgroup-unreads)
3138                     (setq gnus-newsgroup-unselected
3139                           (delq article gnus-newsgroup-unselected)))
3140                 (push article gnus-newsgroup-ancient)))
3141             (forward-line 1)))))))
3142
3143 (defun gnus-summary-update-article-line (article header)
3144   "Update the line for ARTICLE using HEADERS."
3145   (let* ((id (mail-header-id header))
3146          (thread (gnus-id-to-thread id)))
3147     (unless thread
3148       (error "Article in no thread"))
3149     ;; Update the thread.
3150     (setcar thread header)
3151     (gnus-summary-goto-subject article)
3152     (let* ((datal (gnus-data-find-list article))
3153            (data (car datal))
3154            (length (when (cdr datal)
3155                      (- (gnus-data-pos data)
3156                         (gnus-data-pos (cadr datal)))))
3157            (buffer-read-only nil)
3158            (level (gnus-summary-thread-level)))
3159       (gnus-delete-line)
3160       (gnus-summary-insert-line
3161        header level nil (gnus-article-mark article)
3162        (memq article gnus-newsgroup-replied)
3163        (memq article gnus-newsgroup-expirable)
3164        ;; Only insert the Subject string when it's different
3165        ;; from the previous Subject string.
3166        (if (gnus-subject-equal
3167             (condition-case ()
3168                 (mail-header-subject
3169                  (gnus-data-header
3170                   (cadr
3171                    (gnus-data-find-list
3172                     article
3173                     (gnus-data-list t)))))
3174               ;; Error on the side of excessive subjects.
3175               (error ""))
3176             (mail-header-subject header))
3177            ""
3178          (mail-header-subject header))
3179        nil (cdr (assq article gnus-newsgroup-scored))
3180        (memq article gnus-newsgroup-processable))
3181       (when length
3182         (gnus-data-update-list
3183          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3184
3185 (defun gnus-summary-update-article (article &optional iheader)
3186   "Update ARTICLE in the summary buffer."
3187   (set-buffer gnus-summary-buffer)
3188   (let* ((header (gnus-summary-article-header article))
3189          (id (mail-header-id header))
3190          (data (gnus-data-find article))
3191          (thread (gnus-id-to-thread id))
3192          (references (mail-header-references header))
3193          (parent
3194           (gnus-id-to-thread
3195            (or (gnus-parent-id
3196                 (when (and references
3197                            (not (equal "" references)))
3198                   references))
3199                "none")))
3200          (buffer-read-only nil)
3201          (old (car thread)))
3202     (when thread
3203       (unless iheader
3204         (setcar thread nil)
3205         (when parent
3206           (delq thread parent)))
3207       (if (gnus-summary-insert-subject id header)
3208           ;; Set the (possibly) new article number in the data structure.
3209           (gnus-data-set-number data (gnus-id-to-article id))
3210         (setcar thread old)
3211         nil))))
3212
3213 (defun gnus-rebuild-thread (id &optional line)
3214   "Rebuild the thread containing ID.
3215 If LINE, insert the rebuilt thread starting on line LINE."
3216   (let ((buffer-read-only nil)
3217         old-pos current thread data)
3218     (if (not gnus-show-threads)
3219         (setq thread (list (car (gnus-id-to-thread id))))
3220       ;; Get the thread this article is part of.
3221       (setq thread (gnus-remove-thread id)))
3222     (setq old-pos (gnus-point-at-bol))
3223     (setq current (save-excursion
3224                     (and (zerop (forward-line -1))
3225                          (gnus-summary-article-number))))
3226     ;; If this is a gathered thread, we have to go some re-gathering.
3227     (when (stringp (car thread))
3228       (let ((subject (car thread))
3229             roots thr)
3230         (setq thread (cdr thread))
3231         (while thread
3232           (unless (memq (setq thr (gnus-id-to-thread
3233                                    (gnus-root-id
3234                                     (mail-header-id (caar thread)))))
3235                         roots)
3236             (push thr roots))
3237           (setq thread (cdr thread)))
3238         ;; We now have all (unique) roots.
3239         (if (= (length roots) 1)
3240             ;; All the loose roots are now one solid root.
3241             (setq thread (car roots))
3242           (setq thread (cons subject (gnus-sort-threads roots))))))
3243     (let (threads)
3244       ;; We then insert this thread into the summary buffer.
3245       (when line
3246         (goto-char (point-min))
3247         (forward-line (1- line)))
3248       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3249         (if gnus-show-threads
3250             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3251           (gnus-summary-prepare-unthreaded thread))
3252         (setq data (nreverse gnus-newsgroup-data))
3253         (setq threads gnus-newsgroup-threads))
3254       ;; We splice the new data into the data structure.
3255       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3256       ;;!!! then we want to insert at the beginning of the buffer.
3257       ;;!!! That happens to be true with Gnus now, but that may
3258       ;;!!! change in the future.  Perhaps.
3259       (gnus-data-enter-list
3260        (if line nil current) data (- (point) old-pos))
3261       (setq gnus-newsgroup-threads
3262             (nconc threads gnus-newsgroup-threads))
3263       (gnus-data-compute-positions))))
3264
3265 (defun gnus-number-to-header (number)
3266   "Return the header for article NUMBER."
3267   (let ((headers gnus-newsgroup-headers))
3268     (while (and headers
3269                 (not (= number (mail-header-number (car headers)))))
3270       (pop headers))
3271     (when headers
3272       (car headers))))
3273
3274 (defun gnus-parent-headers (in-headers &optional generation)
3275   "Return the headers of the GENERATIONeth parent of HEADERS."
3276   (unless generation
3277     (setq generation 1))
3278   (let ((parent t)
3279         (headers in-headers)
3280         references)
3281     (while (and parent
3282                 (not (zerop generation))
3283                 (setq references (mail-header-references headers)))
3284       (setq headers (if (and references
3285                              (setq parent (gnus-parent-id references)))
3286                         (car (gnus-id-to-thread parent))
3287                       nil))
3288       (decf generation))
3289     (and (not (eq headers in-headers))
3290          headers)))
3291
3292 (defun gnus-id-to-thread (id)
3293   "Return the (sub-)thread where ID appears."
3294   (gnus-gethash id gnus-newsgroup-dependencies))
3295
3296 (defun gnus-id-to-article (id)
3297   "Return the article number of ID."
3298   (let ((thread (gnus-id-to-thread id)))
3299     (when (and thread
3300                (car thread))
3301       (mail-header-number (car thread)))))
3302
3303 (defun gnus-id-to-header (id)
3304   "Return the article headers of ID."
3305   (car (gnus-id-to-thread id)))
3306
3307 (defun gnus-article-displayed-root-p (article)
3308   "Say whether ARTICLE is a root(ish) article."
3309   (let ((level (gnus-summary-thread-level article))
3310         (refs (mail-header-references  (gnus-summary-article-header article)))
3311         particle)
3312     (cond
3313      ((null level) nil)
3314      ((zerop level) t)
3315      ((null refs) t)
3316      ((null (gnus-parent-id refs)) t)
3317      ((and (= 1 level)
3318            (null (setq particle (gnus-id-to-article
3319                                  (gnus-parent-id refs))))
3320            (null (gnus-summary-thread-level particle)))))))
3321
3322 (defun gnus-root-id (id)
3323   "Return the id of the root of the thread where ID appears."
3324   (let (last-id prev)
3325     (while (and id (setq prev (car (gnus-id-to-thread id))))
3326       (setq last-id id
3327             id (gnus-parent-id (mail-header-references prev))))
3328     last-id))
3329
3330 (defun gnus-articles-in-thread (thread)
3331   "Return the list of articles in THREAD."
3332   (cons (mail-header-number (car thread))
3333         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3334
3335 (defun gnus-remove-thread (id &optional dont-remove)
3336   "Remove the thread that has ID in it."
3337   (let (headers thread last-id)
3338     ;; First go up in this thread until we find the root.
3339     (setq last-id (gnus-root-id id)
3340           headers (message-flatten-list (gnus-id-to-thread last-id)))
3341     ;; We have now found the real root of this thread.  It might have
3342     ;; been gathered into some loose thread, so we have to search
3343     ;; through the threads to find the thread we wanted.
3344     (let ((threads gnus-newsgroup-threads)
3345           sub)
3346       (while threads
3347         (setq sub (car threads))
3348         (if (stringp (car sub))
3349             ;; This is a gathered thread, so we look at the roots
3350             ;; below it to find whether this article is in this
3351             ;; gathered root.
3352             (progn
3353               (setq sub (cdr sub))
3354               (while sub
3355                 (when (member (caar sub) headers)
3356                   (setq thread (car threads)
3357                         threads nil
3358                         sub nil))
3359                 (setq sub (cdr sub))))
3360           ;; It's an ordinary thread, so we check it.
3361           (when (eq (car sub) (car headers))
3362             (setq thread sub
3363                   threads nil)))
3364         (setq threads (cdr threads)))
3365       ;; If this article is in no thread, then it's a root.
3366       (if thread
3367           (unless dont-remove
3368             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3369         (setq thread (gnus-id-to-thread last-id)))
3370       (when thread
3371         (prog1
3372             thread                      ; We return this thread.
3373           (unless dont-remove
3374             (if (stringp (car thread))
3375                 (progn
3376                   ;; If we use dummy roots, then we have to remove the
3377                   ;; dummy root as well.
3378                   (when (eq gnus-summary-make-false-root 'dummy)
3379                     ;; We go to the dummy root by going to
3380                     ;; the first sub-"thread", and then one line up.
3381                     (gnus-summary-goto-article
3382                      (mail-header-number (caadr thread)))
3383                     (forward-line -1)
3384                     (gnus-delete-line)
3385                     (gnus-data-compute-positions))
3386                   (setq thread (cdr thread))
3387                   (while thread
3388                     (gnus-remove-thread-1 (car thread))
3389                     (setq thread (cdr thread))))
3390               (gnus-summary-show-all-threads)
3391               (gnus-remove-thread-1 thread))))))))
3392
3393 (defun gnus-remove-thread-1 (thread)
3394   "Remove the thread THREAD recursively."
3395   (let ((number (mail-header-number (pop thread)))
3396         d)
3397     (setq thread (reverse thread))
3398     (while thread
3399       (gnus-remove-thread-1 (pop thread)))
3400     (when (setq d (gnus-data-find number))
3401       (goto-char (gnus-data-pos d))
3402       (gnus-data-remove
3403        number
3404        (- (gnus-point-at-bol)
3405           (prog1
3406               (1+ (gnus-point-at-eol))
3407             (gnus-delete-line)))))))
3408
3409 (defun gnus-sort-threads (threads)
3410   "Sort THREADS."
3411   (if (not gnus-thread-sort-functions)
3412       threads
3413     (gnus-message 8 "Sorting threads...")
3414     (prog1
3415         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3416       (gnus-message 8 "Sorting threads...done"))))
3417
3418 (defun gnus-sort-articles (articles)
3419   "Sort ARTICLES."
3420   (when gnus-article-sort-functions
3421     (gnus-message 7 "Sorting articles...")
3422     (prog1
3423         (setq gnus-newsgroup-headers
3424               (sort articles (gnus-make-sort-function
3425                               gnus-article-sort-functions)))
3426       (gnus-message 7 "Sorting articles...done"))))
3427
3428 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3429 (defmacro gnus-thread-header (thread)
3430   ;; Return header of first article in THREAD.
3431   ;; Note that THREAD must never, ever be anything else than a variable -
3432   ;; using some other form will lead to serious barfage.
3433   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3434   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3435   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3436         (vector thread) 2))
3437
3438 (defsubst gnus-article-sort-by-number (h1 h2)
3439   "Sort articles by article number."
3440   (< (mail-header-number h1)
3441      (mail-header-number h2)))
3442
3443 (defun gnus-thread-sort-by-number (h1 h2)
3444   "Sort threads by root article number."
3445   (gnus-article-sort-by-number
3446    (gnus-thread-header h1) (gnus-thread-header h2)))
3447
3448 (defsubst gnus-article-sort-by-lines (h1 h2)
3449   "Sort articles by article Lines header."
3450   (< (mail-header-lines h1)
3451      (mail-header-lines h2)))
3452
3453 (defun gnus-thread-sort-by-lines (h1 h2)
3454   "Sort threads by root article Lines header."
3455   (gnus-article-sort-by-lines
3456    (gnus-thread-header h1) (gnus-thread-header h2)))
3457
3458 (defsubst gnus-article-sort-by-author (h1 h2)
3459   "Sort articles by root author."
3460   (string-lessp
3461    (let ((extract (funcall
3462                    gnus-extract-address-components
3463                    (mail-header-from h1))))
3464      (or (car extract) (cadr extract) ""))
3465    (let ((extract (funcall
3466                    gnus-extract-address-components
3467                    (mail-header-from h2))))
3468      (or (car extract) (cadr extract) ""))))
3469
3470 (defun gnus-thread-sort-by-author (h1 h2)
3471   "Sort threads by root author."
3472   (gnus-article-sort-by-author
3473    (gnus-thread-header h1)  (gnus-thread-header h2)))
3474
3475 (defsubst gnus-article-sort-by-subject (h1 h2)
3476   "Sort articles by root subject."
3477   (string-lessp
3478    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3479    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3480
3481 (defun gnus-thread-sort-by-subject (h1 h2)
3482   "Sort threads by root subject."
3483   (gnus-article-sort-by-subject
3484    (gnus-thread-header h1) (gnus-thread-header h2)))
3485
3486 (defsubst gnus-article-sort-by-date (h1 h2)
3487   "Sort articles by root article date."
3488   (time-less-p
3489    (gnus-date-get-time (mail-header-date h1))
3490    (gnus-date-get-time (mail-header-date h2))))
3491
3492 (defun gnus-thread-sort-by-date (h1 h2)
3493   "Sort threads by root article date."
3494   (gnus-article-sort-by-date
3495    (gnus-thread-header h1) (gnus-thread-header h2)))
3496
3497 (defsubst gnus-article-sort-by-score (h1 h2)
3498   "Sort articles by root article score.
3499 Unscored articles will be counted as having a score of zero."
3500   (> (or (cdr (assq (mail-header-number h1)
3501                     gnus-newsgroup-scored))
3502          gnus-summary-default-score 0)
3503      (or (cdr (assq (mail-header-number h2)
3504                     gnus-newsgroup-scored))
3505          gnus-summary-default-score 0)))
3506
3507 (defun gnus-thread-sort-by-score (h1 h2)
3508   "Sort threads by root article score."
3509   (gnus-article-sort-by-score
3510    (gnus-thread-header h1) (gnus-thread-header h2)))
3511
3512 (defun gnus-thread-sort-by-total-score (h1 h2)
3513   "Sort threads by the sum of all scores in the thread.
3514 Unscored articles will be counted as having a score of zero."
3515   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3516
3517 (defun gnus-thread-total-score (thread)
3518   ;;  This function find the total score of THREAD.
3519   (cond ((null thread)
3520          0)
3521         ((consp thread)
3522          (if (stringp (car thread))
3523              (apply gnus-thread-score-function 0
3524                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3525            (gnus-thread-total-score-1 thread)))
3526         (t
3527          (gnus-thread-total-score-1 (list thread)))))
3528
3529 (defun gnus-thread-total-score-1 (root)
3530   ;; This function find the total score of the thread below ROOT.
3531   (setq root (car root))
3532   (apply gnus-thread-score-function
3533          (or (append
3534               (mapcar 'gnus-thread-total-score
3535                       (cdr (gnus-id-to-thread (mail-header-id root))))
3536               (when (> (mail-header-number root) 0)
3537                 (list (or (cdr (assq (mail-header-number root)
3538                                      gnus-newsgroup-scored))
3539                           gnus-summary-default-score 0))))
3540              (list gnus-summary-default-score)
3541              '(0))))
3542
3543 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3544 (defvar gnus-tmp-prev-subject nil)
3545 (defvar gnus-tmp-false-parent nil)
3546 (defvar gnus-tmp-root-expunged nil)
3547 (defvar gnus-tmp-dummy-line nil)
3548
3549 (defun gnus-summary-prepare-threads (threads)
3550   "Prepare summary buffer from THREADS and indentation LEVEL.
3551 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3552 or a straight list of headers."
3553   (gnus-message 7 "Generating summary...")
3554
3555   (setq gnus-newsgroup-threads threads)
3556   (beginning-of-line)
3557
3558   (let ((gnus-tmp-level 0)
3559         (default-score (or gnus-summary-default-score 0))
3560         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3561         thread number subject stack state gnus-tmp-gathered beg-match
3562         new-roots gnus-tmp-new-adopts thread-end
3563         gnus-tmp-header gnus-tmp-unread
3564         gnus-tmp-replied gnus-tmp-subject-or-nil
3565         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3566         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3567         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3568
3569     (setq gnus-tmp-prev-subject nil)
3570
3571     (if (vectorp (car threads))
3572         ;; If this is a straight (sic) list of headers, then a
3573         ;; threaded summary display isn't required, so we just create
3574         ;; an unthreaded one.
3575         (gnus-summary-prepare-unthreaded threads)
3576
3577       ;; Do the threaded display.
3578
3579       (while (or threads stack gnus-tmp-new-adopts new-roots)
3580
3581         (if (and (= gnus-tmp-level 0)
3582                  (or (not stack)
3583                      (= (caar stack) 0))
3584                  (not gnus-tmp-false-parent)
3585                  (or gnus-tmp-new-adopts new-roots))
3586             (if gnus-tmp-new-adopts
3587                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3588                       thread (list (car gnus-tmp-new-adopts))
3589                       gnus-tmp-header (caar thread)
3590                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3591               (when new-roots
3592                 (setq thread (list (car new-roots))
3593                       gnus-tmp-header (caar thread)
3594                       new-roots (cdr new-roots))))
3595
3596           (if threads
3597               ;; If there are some threads, we do them before the
3598               ;; threads on the stack.
3599               (setq thread threads
3600                     gnus-tmp-header (caar thread))
3601             ;; There were no current threads, so we pop something off
3602             ;; the stack.
3603             (setq state (car stack)
3604                   gnus-tmp-level (car state)
3605                   thread (cdr state)
3606                   stack (cdr stack)
3607                   gnus-tmp-header (caar thread))))
3608
3609         (setq gnus-tmp-false-parent nil)
3610         (setq gnus-tmp-root-expunged nil)
3611         (setq thread-end nil)
3612
3613         (if (stringp gnus-tmp-header)
3614             ;; The header is a dummy root.
3615             (cond
3616              ((eq gnus-summary-make-false-root 'adopt)
3617               ;; We let the first article adopt the rest.
3618               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3619                                                (cddar thread)))
3620               (setq gnus-tmp-gathered
3621                     (nconc (mapcar
3622                             (lambda (h) (mail-header-number (car h)))
3623                             (cddar thread))
3624                            gnus-tmp-gathered))
3625               (setq thread (cons (list (caar thread)
3626                                        (cadar thread))
3627                                  (cdr thread)))
3628               (setq gnus-tmp-level -1
3629                     gnus-tmp-false-parent t))
3630              ((eq gnus-summary-make-false-root 'empty)
3631               ;; We print adopted articles with empty subject fields.
3632               (setq gnus-tmp-gathered
3633                     (nconc (mapcar
3634                             (lambda (h) (mail-header-number (car h)))
3635                             (cddar thread))
3636                            gnus-tmp-gathered))
3637               (setq gnus-tmp-level -1))
3638              ((eq gnus-summary-make-false-root 'dummy)
3639               ;; We remember that we probably want to output a dummy
3640               ;; root.
3641               (setq gnus-tmp-dummy-line gnus-tmp-header)
3642               (setq gnus-tmp-prev-subject gnus-tmp-header))
3643              (t
3644               ;; We do not make a root for the gathered
3645               ;; sub-threads at all.
3646               (setq gnus-tmp-level -1)))
3647
3648           (setq number (mail-header-number gnus-tmp-header)
3649                 subject (mail-header-subject gnus-tmp-header))
3650
3651           (cond
3652            ;; If the thread has changed subject, we might want to make
3653            ;; this subthread into a root.
3654            ((and (null gnus-thread-ignore-subject)
3655                  (not (zerop gnus-tmp-level))
3656                  gnus-tmp-prev-subject
3657                  (not (inline
3658                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3659             (setq new-roots (nconc new-roots (list (car thread)))
3660                   thread-end t
3661                   gnus-tmp-header nil))
3662            ;; If the article lies outside the current limit,
3663            ;; then we do not display it.
3664            ((not (memq number gnus-newsgroup-limit))
3665             (setq gnus-tmp-gathered
3666                   (nconc (mapcar
3667                           (lambda (h) (mail-header-number (car h)))
3668                           (cdar thread))
3669                          gnus-tmp-gathered))
3670             (setq gnus-tmp-new-adopts (if (cdar thread)
3671                                           (append gnus-tmp-new-adopts
3672                                                   (cdar thread))
3673                                         gnus-tmp-new-adopts)
3674                   thread-end t
3675                   gnus-tmp-header nil)
3676             (when (zerop gnus-tmp-level)
3677               (setq gnus-tmp-root-expunged t)))
3678            ;; Perhaps this article is to be marked as read?
3679            ((and gnus-summary-mark-below
3680                  (< (or (cdr (assq number gnus-newsgroup-scored))
3681                         default-score)
3682                     gnus-summary-mark-below)
3683                  ;; Don't touch sparse articles.
3684                  (not (gnus-summary-article-sparse-p number))
3685                  (not (gnus-summary-article-ancient-p number)))
3686             (setq gnus-newsgroup-unreads
3687                   (delq number gnus-newsgroup-unreads))
3688             (if gnus-newsgroup-auto-expire
3689                 (push number gnus-newsgroup-expirable)
3690               (push (cons number gnus-low-score-mark)
3691                     gnus-newsgroup-reads))))
3692
3693           (when gnus-tmp-header
3694             ;; We may have an old dummy line to output before this
3695             ;; article.
3696             (when (and gnus-tmp-dummy-line
3697                        (gnus-subject-equal
3698                         gnus-tmp-dummy-line
3699                         (mail-header-subject gnus-tmp-header)))
3700               (gnus-summary-insert-dummy-line
3701                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3702               (setq gnus-tmp-dummy-line nil))
3703
3704             ;; Compute the mark.
3705             (setq gnus-tmp-unread (gnus-article-mark number))
3706
3707             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3708                                   gnus-tmp-header gnus-tmp-level)
3709                   gnus-newsgroup-data)
3710
3711             ;; Actually insert the line.
3712             (setq
3713              gnus-tmp-subject-or-nil
3714              (cond
3715               ((and gnus-thread-ignore-subject
3716                     gnus-tmp-prev-subject
3717                     (not (inline (gnus-subject-equal
3718                                   gnus-tmp-prev-subject subject))))
3719                subject)
3720               ((zerop gnus-tmp-level)
3721                (if (and (eq gnus-summary-make-false-root 'empty)
3722                         (memq number gnus-tmp-gathered)
3723                         gnus-tmp-prev-subject
3724                         (inline (gnus-subject-equal
3725                                  gnus-tmp-prev-subject subject)))
3726                    gnus-summary-same-subject
3727                  subject))
3728               (t gnus-summary-same-subject)))
3729             (if (and (eq gnus-summary-make-false-root 'adopt)
3730                      (= gnus-tmp-level 1)
3731                      (memq number gnus-tmp-gathered))
3732                 (setq gnus-tmp-opening-bracket ?\<
3733                       gnus-tmp-closing-bracket ?\>)
3734               (setq gnus-tmp-opening-bracket ?\[
3735                     gnus-tmp-closing-bracket ?\]))
3736             (setq
3737              gnus-tmp-indentation
3738              (aref gnus-thread-indent-array gnus-tmp-level)
3739              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3740              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3741                                 gnus-summary-default-score 0)
3742              gnus-tmp-score-char
3743              (if (or (null gnus-summary-default-score)
3744                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3745                          gnus-summary-zcore-fuzz))
3746                  ? 
3747                (if (< gnus-tmp-score gnus-summary-default-score)
3748                    gnus-score-below-mark gnus-score-over-mark))
3749              gnus-tmp-replied
3750              (cond ((memq number gnus-newsgroup-processable)
3751                     gnus-process-mark)
3752                    ((memq number gnus-newsgroup-cached)
3753                     gnus-cached-mark)
3754                    ((memq number gnus-newsgroup-replied)
3755                     gnus-replied-mark)
3756                    ((memq number gnus-newsgroup-saved)
3757                     gnus-saved-mark)
3758                    (t gnus-unread-mark))
3759              gnus-tmp-from (mail-header-from gnus-tmp-header)
3760              gnus-tmp-name
3761              (cond
3762               ((string-match "<[^>]+> *$" gnus-tmp-from)
3763                (setq beg-match (match-beginning 0))
3764                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3765                         (substring gnus-tmp-from (1+ (match-beginning 0))
3766                                    (1- (match-end 0))))
3767                    (substring gnus-tmp-from 0 beg-match)))
3768               ((string-match "(.+)" gnus-tmp-from)
3769                (substring gnus-tmp-from
3770                           (1+ (match-beginning 0)) (1- (match-end 0))))
3771               (t gnus-tmp-from)))
3772             (when (string= gnus-tmp-name "")
3773               (setq gnus-tmp-name gnus-tmp-from))
3774             (unless (numberp gnus-tmp-lines)
3775               (setq gnus-tmp-lines 0))
3776             (gnus-put-text-property-excluding-characters-with-faces
3777              (point)
3778              (progn (eval gnus-summary-line-format-spec) (point))
3779              'gnus-number number)
3780             (when gnus-visual-p
3781               (forward-line -1)
3782               (gnus-run-hooks 'gnus-summary-update-hook)
3783               (forward-line 1))
3784
3785             (setq gnus-tmp-prev-subject subject)))
3786
3787         (when (nth 1 thread)
3788           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3789         (incf gnus-tmp-level)
3790         (setq threads (if thread-end nil (cdar thread)))
3791         (unless threads
3792           (setq gnus-tmp-level 0)))))
3793   (gnus-message 7 "Generating summary...done"))
3794
3795 (defun gnus-summary-prepare-unthreaded (headers)
3796   "Generate an unthreaded summary buffer based on HEADERS."
3797   (let (header number mark)
3798
3799     (beginning-of-line)
3800
3801     (while headers
3802       ;; We may have to root out some bad articles...
3803       (when (memq (setq number (mail-header-number
3804                                 (setq header (pop headers))))
3805                   gnus-newsgroup-limit)
3806         ;; Mark article as read when it has a low score.
3807         (when (and gnus-summary-mark-below
3808                    (< (or (cdr (assq number gnus-newsgroup-scored))
3809                           gnus-summary-default-score 0)
3810                       gnus-summary-mark-below)
3811                    (not (gnus-summary-article-ancient-p number)))
3812           (setq gnus-newsgroup-unreads
3813                 (delq number gnus-newsgroup-unreads))
3814           (if gnus-newsgroup-auto-expire
3815               (push number gnus-newsgroup-expirable)
3816             (push (cons number gnus-low-score-mark)
3817                   gnus-newsgroup-reads)))
3818
3819         (setq mark (gnus-article-mark number))
3820         (push (gnus-data-make number mark (1+ (point)) header 0)
3821               gnus-newsgroup-data)
3822         (gnus-summary-insert-line
3823          header 0 number
3824          mark (memq number gnus-newsgroup-replied)
3825          (memq number gnus-newsgroup-expirable)
3826          (mail-header-subject header) nil
3827          (cdr (assq number gnus-newsgroup-scored))
3828          (memq number gnus-newsgroup-processable))))))
3829
3830 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3831   "Select newsgroup GROUP.
3832 If READ-ALL is non-nil, all articles in the group are selected.
3833 If SELECT-ARTICLES, only select those articles from GROUP."
3834   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3835          ;;!!! Dirty hack; should be removed.
3836          (gnus-summary-ignore-duplicates
3837           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3838               t
3839             gnus-summary-ignore-duplicates))
3840          (info (nth 2 entry))
3841          articles fetched-articles cached)
3842
3843     (unless (gnus-check-server
3844              (setq gnus-current-select-method
3845                    (gnus-find-method-for-group group)))
3846       (error "Couldn't open server"))
3847
3848     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3849         (gnus-activate-group group)     ; Or we can activate it...
3850         (progn                          ; Or we bug out.
3851           (when (equal major-mode 'gnus-summary-mode)
3852             (kill-buffer (current-buffer)))
3853           (error "Couldn't request group %s: %s"
3854                  group (gnus-status-message group))))
3855
3856     (unless (gnus-request-group group t)
3857       (when (equal major-mode 'gnus-summary-mode)
3858         (kill-buffer (current-buffer)))
3859       (error "Couldn't request group %s: %s"
3860              group (gnus-status-message group)))
3861
3862     (setq gnus-newsgroup-name group)
3863     (setq gnus-newsgroup-unselected nil)
3864     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3865
3866     ;; Adjust and set lists of article marks.
3867     (when info
3868       (gnus-adjust-marked-articles info))
3869
3870     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3871     (when (gnus-virtual-group-p group)
3872       (setq cached gnus-newsgroup-cached))
3873
3874     (setq gnus-newsgroup-unreads
3875           (gnus-set-difference
3876            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3877            gnus-newsgroup-dormant))
3878
3879     (setq gnus-newsgroup-processable nil)
3880
3881     (gnus-update-read-articles group gnus-newsgroup-unreads)
3882
3883     (if (setq articles select-articles)
3884         (setq gnus-newsgroup-unselected
3885               (gnus-sorted-intersection
3886                gnus-newsgroup-unreads
3887                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3888       (setq articles (gnus-articles-to-read group read-all)))
3889
3890     (cond
3891      ((null articles)
3892       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3893       'quit)
3894      ((eq articles 0) nil)
3895      (t
3896       ;; Init the dependencies hash table.
3897       (setq gnus-newsgroup-dependencies
3898             (gnus-make-hashtable (length articles)))
3899       ;; Retrieve the headers and read them in.
3900       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3901       (setq gnus-newsgroup-headers
3902             (if (eq 'nov
3903                     (setq gnus-headers-retrieved-by
3904                           (gnus-retrieve-headers
3905                            articles gnus-newsgroup-name
3906                            ;; We might want to fetch old headers, but
3907                            ;; not if there is only 1 article.
3908                            (and (or (and
3909                                      (not (eq gnus-fetch-old-headers 'some))
3910                                      (not (numberp gnus-fetch-old-headers)))
3911                                     (> (length articles) 1))
3912                                 gnus-fetch-old-headers))))
3913                 (gnus-get-newsgroup-headers-xover
3914                  articles nil nil gnus-newsgroup-name t)
3915               (gnus-get-newsgroup-headers)))
3916       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3917
3918       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3919       (when cached
3920         (setq gnus-newsgroup-cached cached))
3921
3922       ;; Suppress duplicates?
3923       (when gnus-suppress-duplicates
3924         (gnus-dup-suppress-articles))
3925
3926       ;; Set the initial limit.
3927       (setq gnus-newsgroup-limit (copy-sequence articles))
3928       ;; Remove canceled articles from the list of unread articles.
3929       (setq gnus-newsgroup-unreads
3930             (gnus-set-sorted-intersection
3931              gnus-newsgroup-unreads
3932              (setq fetched-articles
3933                    (mapcar (lambda (headers) (mail-header-number headers))
3934                            gnus-newsgroup-headers))))
3935       ;; Removed marked articles that do not exist.
3936       (gnus-update-missing-marks
3937        (gnus-sorted-complement fetched-articles articles))
3938       ;; We might want to build some more threads first.
3939       (when (and gnus-fetch-old-headers
3940                  (eq gnus-headers-retrieved-by 'nov))
3941         (if (eq gnus-fetch-old-headers 'invisible)
3942             (gnus-build-all-threads)
3943           (gnus-build-old-threads)))
3944       ;; Let the Gnus agent mark articles as read.
3945       (when gnus-agent
3946         (gnus-agent-get-undownloaded-list))
3947       ;; Check whether auto-expire is to be done in this group.
3948       (setq gnus-newsgroup-auto-expire
3949             (gnus-group-auto-expirable-p group))
3950       ;; Set up the article buffer now, if necessary.
3951       (unless gnus-single-article-buffer
3952         (gnus-article-setup-buffer))
3953       ;; First and last article in this newsgroup.
3954       (when gnus-newsgroup-headers
3955         (setq gnus-newsgroup-begin
3956               (mail-header-number (car gnus-newsgroup-headers))
3957               gnus-newsgroup-end
3958               (mail-header-number
3959                (gnus-last-element gnus-newsgroup-headers))))
3960       ;; GROUP is successfully selected.
3961       (or gnus-newsgroup-headers t)))))
3962
3963 (defun gnus-articles-to-read (group &optional read-all)
3964   ;; Find out what articles the user wants to read.
3965   (let* ((articles
3966           ;; Select all articles if `read-all' is non-nil, or if there
3967           ;; are no unread articles.
3968           (if (or read-all
3969                   (and (zerop (length gnus-newsgroup-marked))
3970                        (zerop (length gnus-newsgroup-unreads)))
3971                   (eq (gnus-group-find-parameter group 'display)
3972                       'all))
3973               (gnus-uncompress-range (gnus-active group))
3974             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
3975                           (copy-sequence gnus-newsgroup-unreads))
3976                   '<)))
3977          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
3978          (scored (length scored-list))
3979          (number (length articles))
3980          (marked (+ (length gnus-newsgroup-marked)
3981                     (length gnus-newsgroup-dormant)))
3982          (select
3983           (cond
3984            ((numberp read-all)
3985             read-all)
3986            (t
3987             (condition-case ()
3988                 (cond
3989                  ((and (or (<= scored marked) (= scored number))
3990                        (numberp gnus-large-newsgroup)
3991                        (> number gnus-large-newsgroup))
3992                   (let ((input
3993                          (read-string
3994                           (format
3995                            "How many articles from %s (default %d): "
3996                            (gnus-limit-string gnus-newsgroup-name 35)
3997                            number))))
3998                     (if (string-match "^[ \t]*$" input) number input)))
3999                  ((and (> scored marked) (< scored number)
4000                        (> (- scored number) 20))
4001                   (let ((input
4002                          (read-string
4003                           (format "%s %s (%d scored, %d total): "
4004                                   "How many articles from"
4005                                   group scored number))))
4006                     (if (string-match "^[ \t]*$" input)
4007                         number input)))
4008                  (t number))
4009               (quit nil))))))
4010     (setq select (if (stringp select) (string-to-number select) select))
4011     (if (or (null select) (zerop select))
4012         select
4013       (if (and (not (zerop scored)) (<= (abs select) scored))
4014           (progn
4015             (setq articles (sort scored-list '<))
4016             (setq number (length articles)))
4017         (setq articles (copy-sequence articles)))
4018
4019       (when (< (abs select) number)
4020         (if (< select 0)
4021             ;; Select the N oldest articles.
4022             (setcdr (nthcdr (1- (abs select)) articles) nil)
4023           ;; Select the N most recent articles.
4024           (setq articles (nthcdr (- number select) articles))))
4025       (setq gnus-newsgroup-unselected
4026             (gnus-sorted-intersection
4027              gnus-newsgroup-unreads
4028              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4029       articles)))
4030
4031 (defun gnus-killed-articles (killed articles)
4032   (let (out)
4033     (while articles
4034       (when (inline (gnus-member-of-range (car articles) killed))
4035         (push (car articles) out))
4036       (setq articles (cdr articles)))
4037     out))
4038
4039 (defun gnus-uncompress-marks (marks)
4040   "Uncompress the mark ranges in MARKS."
4041   (let ((uncompressed '(score bookmark))
4042         out)
4043     (while marks
4044       (if (memq (caar marks) uncompressed)
4045           (push (car marks) out)
4046         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4047       (setq marks (cdr marks)))
4048     out))
4049
4050 (defun gnus-adjust-marked-articles (info)
4051   "Set all article lists and remove all marks that are no longer legal."
4052   (let* ((marked-lists (gnus-info-marks info))
4053          (active (gnus-active (gnus-info-group info)))
4054          (min (car active))
4055          (max (cdr active))
4056          (types gnus-article-mark-lists)
4057          (uncompressed '(score bookmark killed))
4058          marks var articles article mark)
4059
4060     (while marked-lists
4061       (setq marks (pop marked-lists))
4062       (set (setq var (intern (format "gnus-newsgroup-%s"
4063                                      (car (rassq (setq mark (car marks))
4064                                                  types)))))
4065            (if (memq (car marks) uncompressed) (cdr marks)
4066              (gnus-uncompress-range (cdr marks))))
4067
4068       (setq articles (symbol-value var))
4069
4070       ;; All articles have to be subsets of the active articles.
4071       (cond
4072        ;; Adjust "simple" lists.
4073        ((memq mark '(tick dormant expire reply save))
4074         (while articles
4075           (when (or (< (setq article (pop articles)) min) (> article max))
4076             (set var (delq article (symbol-value var))))))
4077        ;; Adjust assocs.
4078        ((memq mark uncompressed)
4079         (when (not (listp (cdr (symbol-value var))))
4080           (set var (list (symbol-value var))))
4081         (when (not (listp (cdr articles)))
4082           (setq articles (list articles)))
4083         (while articles
4084           (when (or (not (consp (setq article (pop articles))))
4085                     (< (car article) min)
4086                     (> (car article) max))
4087             (set var (delq article (symbol-value var))))))))))
4088
4089 (defun gnus-update-missing-marks (missing)
4090   "Go through the list of MISSING articles and remove them from the mark lists."
4091   (when missing
4092     (let ((types gnus-article-mark-lists)
4093           var m)
4094       ;; Go through all types.
4095       (while types
4096         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4097         (when (symbol-value var)
4098           ;; This list has articles.  So we delete all missing articles
4099           ;; from it.
4100           (setq m missing)
4101           (while m
4102             (set var (delq (pop m) (symbol-value var)))))))))
4103
4104 (defun gnus-update-marks ()
4105   "Enter the various lists of marked articles into the newsgroup info list."
4106   (let ((types gnus-article-mark-lists)
4107         (info (gnus-get-info gnus-newsgroup-name))
4108         (uncompressed '(score bookmark killed))
4109         type list newmarked symbol)
4110     (when info
4111       ;; Add all marks lists that are non-nil to the list of marks lists.
4112       (while (setq type (pop types))
4113         (when (setq list (symbol-value
4114                           (setq symbol
4115                                 (intern (format "gnus-newsgroup-%s"
4116                                                 (car type))))))
4117
4118           ;; Get rid of the entries of the articles that have the
4119           ;; default score.
4120           (when (and (eq (cdr type) 'score)
4121                      gnus-save-score
4122                      list)
4123             (let* ((arts list)
4124                    (prev (cons nil list))
4125                    (all prev))
4126               (while arts
4127                 (if (or (not (consp (car arts)))
4128                         (= (cdar arts) gnus-summary-default-score))
4129                     (setcdr prev (cdr arts))
4130                   (setq prev arts))
4131                 (setq arts (cdr arts)))
4132               (setq list (cdr all))))
4133
4134           (push (cons (cdr type)
4135                       (if (memq (cdr type) uncompressed) list
4136                         (gnus-compress-sequence
4137                          (set symbol (sort list '<)) t)))
4138                 newmarked)))
4139
4140       ;; Enter these new marks into the info of the group.
4141       (if (nthcdr 3 info)
4142           (setcar (nthcdr 3 info) newmarked)
4143         ;; Add the marks lists to the end of the info.
4144         (when newmarked
4145           (setcdr (nthcdr 2 info) (list newmarked))))
4146
4147       ;; Cut off the end of the info if there's nothing else there.
4148       (let ((i 5))
4149         (while (and (> i 2)
4150                     (not (nth i info)))
4151           (when (nthcdr (decf i) info)
4152             (setcdr (nthcdr i info) nil)))))))
4153
4154 (defun gnus-set-mode-line (where)
4155   "This function sets the mode line of the article or summary buffers.
4156 If WHERE is `summary', the summary mode line format will be used."
4157   ;; Is this mode line one we keep updated?
4158   (when (memq where gnus-updated-mode-lines)
4159     (let (mode-string)
4160       (save-excursion
4161         ;; We evaluate this in the summary buffer since these
4162         ;; variables are buffer-local to that buffer.
4163         (set-buffer gnus-summary-buffer)
4164         ;; We bind all these variables that are used in the `eval' form
4165         ;; below.
4166         (let* ((mformat (symbol-value
4167                          (intern
4168                           (format "gnus-%s-mode-line-format-spec" where))))
4169                (gnus-tmp-group-name gnus-newsgroup-name)
4170                (gnus-tmp-article-number (or gnus-current-article 0))
4171                (gnus-tmp-unread gnus-newsgroup-unreads)
4172                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4173                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4174                (gnus-tmp-unread-and-unselected
4175                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4176                             (zerop gnus-tmp-unselected))
4177                        "")
4178                       ((zerop gnus-tmp-unselected)
4179                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4180                       (t (format "{%d(+%d) more}"
4181                                  gnus-tmp-unread-and-unticked
4182                                  gnus-tmp-unselected))))
4183                (gnus-tmp-subject
4184                 (if (and gnus-current-headers
4185                          (vectorp gnus-current-headers))
4186                     (gnus-mode-string-quote
4187                      (mail-header-subject gnus-current-headers))
4188                   ""))
4189                bufname-length max-len
4190                gnus-tmp-header);; passed as argument to any user-format-funcs
4191           (setq mode-string (eval mformat))
4192           (setq bufname-length (if (string-match "%b" mode-string)
4193                                    (- (length
4194                                        (buffer-name
4195                                         (if (eq where 'summary)
4196                                             nil
4197                                           (get-buffer gnus-article-buffer))))
4198                                       2)
4199                                  0))
4200           (setq max-len (max 4 (if gnus-mode-non-string-length
4201                                    (- (window-width)
4202                                       gnus-mode-non-string-length
4203                                       bufname-length)
4204                                  (length mode-string))))
4205           ;; We might have to chop a bit of the string off...
4206           (when (> (length mode-string) max-len)
4207             (setq mode-string
4208                   (concat (truncate-string mode-string (- max-len 3))
4209                           "...")))
4210           ;; Pad the mode string a bit.
4211           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4212       ;; Update the mode line.
4213       (setq mode-line-buffer-identification
4214             (gnus-mode-line-buffer-identification (list mode-string)))
4215       (set-buffer-modified-p t))))
4216
4217 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4218   "Go through the HEADERS list and add all Xrefs to a hash table.
4219 The resulting hash table is returned, or nil if no Xrefs were found."
4220   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4221          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4222          (xref-hashtb (gnus-make-hashtable))
4223          start group entry number xrefs header)
4224     (while headers
4225       (setq header (pop headers))
4226       (when (and (setq xrefs (mail-header-xref header))
4227                  (not (memq (setq number (mail-header-number header))
4228                             unreads)))
4229         (setq start 0)
4230         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4231           (setq start (match-end 0))
4232           (setq group (if prefix
4233                           (concat prefix (substring xrefs (match-beginning 1)
4234                                                     (match-end 1)))
4235                         (substring xrefs (match-beginning 1) (match-end 1))))
4236           (setq number
4237                 (string-to-int (substring xrefs (match-beginning 2)
4238                                           (match-end 2))))
4239           (if (setq entry (gnus-gethash group xref-hashtb))
4240               (setcdr entry (cons number (cdr entry)))
4241             (gnus-sethash group (cons number nil) xref-hashtb)))))
4242     (and start xref-hashtb)))
4243
4244 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4245   "Look through all the headers and mark the Xrefs as read."
4246   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4247         name entry info xref-hashtb idlist method nth4)
4248     (save-excursion
4249       (set-buffer gnus-group-buffer)
4250       (when (setq xref-hashtb
4251                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4252         (mapatoms
4253          (lambda (group)
4254            (unless (string= from-newsgroup (setq name (symbol-name group)))
4255              (setq idlist (symbol-value group))
4256              ;; Dead groups are not updated.
4257              (and (prog1
4258                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4259                             info (nth 2 entry))
4260                     (when (stringp (setq nth4 (gnus-info-method info)))
4261                       (setq nth4 (gnus-server-to-method nth4))))
4262                   ;; Only do the xrefs if the group has the same
4263                   ;; select method as the group we have just read.
4264                   (or (gnus-methods-equal-p
4265                        nth4 (gnus-find-method-for-group from-newsgroup))
4266                       virtual
4267                       (equal nth4 (setq method (gnus-find-method-for-group
4268                                                 from-newsgroup)))
4269                       (and (equal (car nth4) (car method))
4270                            (equal (nth 1 nth4) (nth 1 method))))
4271                   gnus-use-cross-reference
4272                   (or (not (eq gnus-use-cross-reference t))
4273                       virtual
4274                       ;; Only do cross-references on subscribed
4275                       ;; groups, if that is what is wanted.
4276                       (<= (gnus-info-level info) gnus-level-subscribed))
4277                   (gnus-group-make-articles-read name idlist))))
4278          xref-hashtb)))))
4279
4280 (defun gnus-compute-read-articles (group articles)
4281   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4282          (info (nth 2 entry))
4283          (active (gnus-active group))
4284          ninfo)
4285     (when entry
4286       ;; First peel off all illegal article numbers.
4287       (when active
4288         (let ((ids articles)
4289               id first)
4290           (while (setq id (pop ids))
4291             (when (and first (> id (cdr active)))
4292               ;; We'll end up in this situation in one particular
4293               ;; obscure situation.  If you re-scan a group and get
4294               ;; a new article that is cross-posted to a different
4295               ;; group that has not been re-scanned, you might get
4296               ;; crossposted article that has a higher number than
4297               ;; Gnus believes possible.  So we re-activate this
4298               ;; group as well.  This might mean doing the
4299               ;; crossposting thingy will *increase* the number
4300               ;; of articles in some groups.  Tsk, tsk.
4301               (setq active (or (gnus-activate-group group) active)))
4302             (when (or (> id (cdr active))
4303                       (< id (car active)))
4304               (setq articles (delq id articles))))))
4305       ;; If the read list is nil, we init it.
4306       (if (and active
4307                (null (gnus-info-read info))
4308                (> (car active) 1))
4309           (setq ninfo (cons 1 (1- (car active))))
4310         (setq ninfo (gnus-info-read info)))
4311       ;; Then we add the read articles to the range.
4312       (gnus-add-to-range
4313        ninfo (setq articles (sort articles '<))))))
4314   
4315 (defun gnus-group-make-articles-read (group articles)
4316   "Update the info of GROUP to say that ARTICLES are read."
4317   (let* ((num 0)
4318          (entry (gnus-gethash group gnus-newsrc-hashtb))
4319          (info (nth 2 entry))
4320          (active (gnus-active group))
4321          range)
4322     (when entry
4323       (setq range (gnus-compute-read-articles group articles))
4324       (save-excursion
4325         (set-buffer gnus-group-buffer)
4326         (gnus-undo-register
4327           `(progn
4328              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4329              (gnus-info-set-read ',info ',(gnus-info-read info))
4330              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4331              (gnus-group-update-group ,group t))))
4332       ;; Add the read articles to the range.
4333       (gnus-info-set-read info range)
4334       ;; Then we have to re-compute how many unread
4335       ;; articles there are in this group.
4336       (when active
4337         (cond
4338          ((not range)
4339           (setq num (- (1+ (cdr active)) (car active))))
4340          ((not (listp (cdr range)))
4341           (setq num (- (cdr active) (- (1+ (cdr range))
4342                                        (car range)))))
4343          (t
4344           (while range
4345             (if (numberp (car range))
4346                 (setq num (1+ num))
4347               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4348             (setq range (cdr range)))
4349           (setq num (- (cdr active) num))))
4350         ;; Update the number of unread articles.
4351         (setcar entry num)
4352         ;; Update the group buffer.
4353         (gnus-group-update-group group t)))))
4354
4355 (defun gnus-methods-equal-p (m1 m2)
4356   (let ((m1 (or m1 gnus-select-method))
4357         (m2 (or m2 gnus-select-method)))
4358     (or (equal m1 m2)
4359         (and (eq (car m1) (car m2))
4360              (or (not (memq 'address (assoc (symbol-name (car m1))
4361                                             gnus-valid-select-methods)))
4362                  (equal (nth 1 m1) (nth 1 m2)))))))
4363
4364 (defvar gnus-newsgroup-none-id 0)
4365
4366 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4367   (let ((cur nntp-server-buffer)
4368         (dependencies
4369          (or dependencies
4370              (save-excursion (set-buffer gnus-summary-buffer)
4371                              gnus-newsgroup-dependencies)))
4372         headers id end ref)
4373     (save-excursion
4374       (set-buffer nntp-server-buffer)
4375       ;; Translate all TAB characters into SPACE characters.
4376       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4377       (gnus-run-hooks 'gnus-parse-headers-hook)
4378       (let ((case-fold-search t)
4379             in-reply-to header p lines chars)
4380         (goto-char (point-min))
4381         ;; Search to the beginning of the next header.  Error messages
4382         ;; do not begin with 2 or 3.
4383         (while (re-search-forward "^[23][0-9]+ " nil t)
4384           (setq id nil
4385                 ref nil)
4386           ;; This implementation of this function, with nine
4387           ;; search-forwards instead of the one re-search-forward and
4388           ;; a case (which basically was the old function) is actually
4389           ;; about twice as fast, even though it looks messier.  You
4390           ;; can't have everything, I guess.  Speed and elegance
4391           ;; doesn't always go hand in hand.
4392           (setq
4393            header
4394            (vector
4395             ;; Number.
4396             (prog1
4397                 (read cur)
4398               (end-of-line)
4399               (setq p (point))
4400               (narrow-to-region (point)
4401                                 (or (and (search-forward "\n.\n" nil t)
4402                                          (- (point) 2))
4403                                     (point))))
4404             ;; Subject.
4405             (progn
4406               (goto-char p)
4407               (if (search-forward "\nsubject: " nil t)
4408                   (rfc2047-decode-string (nnheader-header-value))
4409                 "(none)"))
4410             ;; From.
4411             (progn
4412               (goto-char p)
4413               (if (search-forward "\nfrom: " nil t)
4414                   (rfc2047-decode-string (nnheader-header-value))
4415                 "(nobody)"))
4416             ;; Date.
4417             (progn
4418               (goto-char p)
4419               (if (search-forward "\ndate: " nil t)
4420                   (nnheader-header-value) ""))
4421             ;; Message-ID.
4422             (progn
4423               (goto-char p)
4424               (setq id (if (re-search-forward
4425                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4426                            ;; We do it this way to make sure the Message-ID
4427                            ;; is (somewhat) syntactically valid.
4428                            (buffer-substring (match-beginning 1)
4429                                              (match-end 1))
4430                          ;; If there was no message-id, we just fake one
4431                          ;; to make subsequent routines simpler.
4432                          (nnheader-generate-fake-message-id))))
4433             ;; References.
4434             (progn
4435               (goto-char p)
4436               (if (search-forward "\nreferences: " nil t)
4437                   (progn
4438                     (setq end (point))
4439                     (prog1
4440                         (nnheader-header-value)
4441                       (setq ref
4442                             (buffer-substring
4443                              (progn
4444                                (end-of-line)
4445                                (search-backward ">" end t)
4446                                (1+ (point)))
4447                              (progn
4448                                (search-backward "<" end t)
4449                                (point))))))
4450                 ;; Get the references from the in-reply-to header if there
4451                 ;; were no references and the in-reply-to header looks
4452                 ;; promising.
4453                 (if (and (search-forward "\nin-reply-to: " nil t)
4454                          (setq in-reply-to (nnheader-header-value))
4455                          (string-match "<[^>]+>" in-reply-to))
4456                     (let (ref2)
4457                       (setq ref (substring in-reply-to (match-beginning 0)
4458                                            (match-end 0)))
4459                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4460                         (setq ref2 (substring in-reply-to (match-beginning 0)
4461                                               (match-end 0)))
4462                         (when (> (length ref2) (length ref))
4463                           (setq ref ref2)))
4464                       ref)
4465                   (setq ref nil))))
4466             ;; Chars.
4467             (progn
4468               (goto-char p)
4469               (if (search-forward "\nchars: " nil t)
4470                   (if (numberp (setq chars (ignore-errors (read cur))))
4471                       chars 0)
4472                 0))
4473             ;; Lines.
4474             (progn
4475               (goto-char p)
4476               (if (search-forward "\nlines: " nil t)
4477                   (if (numberp (setq lines (ignore-errors (read cur))))
4478                       lines 0)
4479                 0))
4480             ;; Xref.
4481             (progn
4482               (goto-char p)
4483               (and (search-forward "\nxref: " nil t)
4484                    (nnheader-header-value)))))
4485           (when (equal id ref)
4486             (setq ref nil))
4487
4488           (when gnus-alter-header-function
4489             (funcall gnus-alter-header-function header)
4490             (setq id (mail-header-id header)
4491                   ref (gnus-parent-id (mail-header-references header))))
4492
4493           (when (setq header
4494                       (gnus-dependencies-add-header
4495                        header dependencies force-new))
4496             (push header headers))
4497           (goto-char (point-max))
4498           (widen))
4499         (nreverse headers)))))
4500
4501 ;; Goes through the xover lines and returns a list of vectors
4502 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4503                                                   force-new dependencies
4504                                                   group also-fetch-heads)
4505   "Parse the news overview data in the server buffer, and return a
4506 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4507   ;; Get the Xref when the users reads the articles since most/some
4508   ;; NNTP servers do not include Xrefs when using XOVER.
4509   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4510   (let ((cur nntp-server-buffer)
4511         (dependencies (or dependencies gnus-newsgroup-dependencies))
4512         number headers header)
4513     (save-excursion
4514       (set-buffer nntp-server-buffer)
4515       ;; Allow the user to mangle the headers before parsing them.
4516       (gnus-run-hooks 'gnus-parse-headers-hook)
4517       (goto-char (point-min))
4518       (while (not (eobp))
4519         (condition-case ()
4520             (while (and sequence (not (eobp)))
4521               (setq number (read cur))
4522               (while (and sequence
4523                           (< (car sequence) number))
4524                 (setq sequence (cdr sequence)))
4525               (and sequence
4526                    (eq number (car sequence))
4527                    (progn
4528                      (setq sequence (cdr sequence))
4529                      (setq header (inline
4530                                     (gnus-nov-parse-line
4531                                      number dependencies force-new))))
4532                    (push header headers))
4533               (forward-line 1))
4534           ;(error
4535           ; (gnus-error 4 "Strange nov line (%d)"
4536         ;              (count-lines (point-min) (point))))
4537           )
4538         (forward-line 1))
4539       ;; A common bug in inn is that if you have posted an article and
4540       ;; then retrieves the active file, it will answer correctly --
4541       ;; the new article is included.  However, a NOV entry for the
4542       ;; article may not have been generated yet, so this may fail.
4543       ;; We work around this problem by retrieving the last few
4544       ;; headers using HEAD.
4545       (if (or (not also-fetch-heads)
4546               (not sequence))
4547           ;; We (probably) got all the headers.
4548           (nreverse headers)
4549         (let ((gnus-nov-is-evil t))
4550           (nconc
4551            (nreverse headers)
4552            (when (gnus-retrieve-headers sequence group)
4553              (gnus-get-newsgroup-headers))))))))
4554
4555 (defun gnus-article-get-xrefs ()
4556   "Fill in the Xref value in `gnus-current-headers', if necessary.
4557 This is meant to be called in `gnus-article-internal-prepare-hook'."
4558   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4559                                  gnus-current-headers)))
4560     (or (not gnus-use-cross-reference)
4561         (not headers)
4562         (and (mail-header-xref headers)
4563              (not (string= (mail-header-xref headers) "")))
4564         (let ((case-fold-search t)
4565               xref)
4566           (save-restriction
4567             (nnheader-narrow-to-headers)
4568             (goto-char (point-min))
4569             (when (or (and (eq (downcase (following-char)) ?x)
4570                            (looking-at "Xref:"))
4571                       (search-forward "\nXref:" nil t))
4572               (goto-char (1+ (match-end 0)))
4573               (setq xref (buffer-substring (point)
4574                                            (progn (end-of-line) (point))))
4575               (mail-header-set-xref headers xref)))))))
4576
4577 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4578   "Find article ID and insert the summary line for that article.
4579 OLD-HEADER can either be a header or a line number to insert
4580 the subject line on."
4581   (let* ((line (and (numberp old-header) old-header))
4582          (old-header (and (vectorp old-header) old-header))
4583          (header (cond ((and old-header use-old-header)
4584                        old-header)
4585                       ((and (numberp id)
4586                             (gnus-number-to-header id))
4587                        (gnus-number-to-header id))
4588                       (t
4589                        (gnus-read-header id))))
4590         (number (and (numberp id) id))
4591         d)
4592     (when header
4593       ;; Rebuild the thread that this article is part of and go to the
4594       ;; article we have fetched.
4595       (when (and (not gnus-show-threads)
4596                  old-header)
4597         (when (and number
4598                    (setq d (gnus-data-find (mail-header-number old-header))))
4599           (goto-char (gnus-data-pos d))
4600           (gnus-data-remove
4601            number
4602            (- (gnus-point-at-bol)
4603               (prog1
4604                   (1+ (gnus-point-at-eol))
4605                 (gnus-delete-line))))))
4606       (when old-header
4607         (mail-header-set-number header (mail-header-number old-header)))
4608       (setq gnus-newsgroup-sparse
4609             (delq (setq number (mail-header-number header))
4610                   gnus-newsgroup-sparse))
4611       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4612       (push number gnus-newsgroup-limit)
4613       (gnus-rebuild-thread (mail-header-id header) line)
4614       (gnus-summary-goto-subject number nil t))
4615     (when (and (numberp number)
4616                (> number 0))
4617       ;; We have to update the boundaries even if we can't fetch the
4618       ;; article if ID is a number -- so that the next `P' or `N'
4619       ;; command will fetch the previous (or next) article even
4620       ;; if the one we tried to fetch this time has been canceled.
4621       (when (> number gnus-newsgroup-end)
4622         (setq gnus-newsgroup-end number))
4623       (when (< number gnus-newsgroup-begin)
4624         (setq gnus-newsgroup-begin number))
4625       (setq gnus-newsgroup-unselected
4626             (delq number gnus-newsgroup-unselected)))
4627     ;; Report back a success?
4628     (and header (mail-header-number header))))
4629
4630 ;;; Process/prefix in the summary buffer
4631
4632 (defun gnus-summary-work-articles (n)
4633   "Return a list of articles to be worked upon.
4634 The prefix argument, the list of process marked articles, and the
4635 current article will be taken into consideration."
4636   (save-excursion
4637     (set-buffer gnus-summary-buffer)
4638     (cond
4639      (n
4640       ;; A numerical prefix has been given.
4641       (setq n (prefix-numeric-value n))
4642       (let ((backward (< n 0))
4643             (n (abs (prefix-numeric-value n)))
4644             articles article)
4645         (save-excursion
4646           (while
4647               (and (> n 0)
4648                    (push (setq article (gnus-summary-article-number))
4649                          articles)
4650                    (if backward
4651                        (gnus-summary-find-prev nil article)
4652                      (gnus-summary-find-next nil article)))
4653             (decf n)))
4654         (nreverse articles)))
4655      ((and (gnus-region-active-p) (mark))
4656       (message "region active")
4657       ;; Work on the region between point and mark.
4658       (let ((max (max (point) (mark)))
4659             articles article)
4660         (save-excursion
4661           (goto-char (min (min (point) (mark))))
4662           (while
4663               (and
4664                (push (setq article (gnus-summary-article-number)) articles)
4665                (gnus-summary-find-next nil article)
4666                (< (point) max)))
4667           (nreverse articles))))
4668      (gnus-newsgroup-processable
4669       ;; There are process-marked articles present.
4670       ;; Save current state.
4671       (gnus-summary-save-process-mark)
4672       ;; Return the list.
4673       (reverse gnus-newsgroup-processable))
4674      (t
4675       ;; Just return the current article.
4676       (list (gnus-summary-article-number))))))
4677
4678 (defmacro gnus-summary-iterate (arg &rest forms)
4679   "Iterate over the process/prefixed articles and do FORMS.
4680 ARG is the interactive prefix given to the command.  FORMS will be
4681 executed with point over the summary line of the articles."
4682   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4683     `(let ((,articles (gnus-summary-work-articles ,arg)))
4684        (while ,articles
4685          (gnus-summary-goto-subject (car ,articles))
4686          ,@forms))))
4687
4688 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4689 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4690
4691 (defun gnus-summary-save-process-mark ()
4692   "Push the current set of process marked articles on the stack."
4693   (interactive)
4694   (push (copy-sequence gnus-newsgroup-processable)
4695         gnus-newsgroup-process-stack))
4696
4697 (defun gnus-summary-kill-process-mark ()
4698   "Push the current set of process marked articles on the stack and unmark."
4699   (interactive)
4700   (gnus-summary-save-process-mark)
4701   (gnus-summary-unmark-all-processable))
4702
4703 (defun gnus-summary-yank-process-mark ()
4704   "Pop the last process mark state off the stack and restore it."
4705   (interactive)
4706   (unless gnus-newsgroup-process-stack
4707     (error "Empty mark stack"))
4708   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4709
4710 (defun gnus-summary-process-mark-set (set)
4711   "Make SET into the current process marked articles."
4712   (gnus-summary-unmark-all-processable)
4713   (while set
4714     (gnus-summary-set-process-mark (pop set))))
4715
4716 ;;; Searching and stuff
4717
4718 (defun gnus-summary-search-group (&optional backward use-level)
4719   "Search for next unread newsgroup.
4720 If optional argument BACKWARD is non-nil, search backward instead."
4721   (save-excursion
4722     (set-buffer gnus-group-buffer)
4723     (when (gnus-group-search-forward
4724            backward nil (if use-level (gnus-group-group-level) nil))
4725       (gnus-group-group-name))))
4726
4727 (defun gnus-summary-best-group (&optional exclude-group)
4728   "Find the name of the best unread group.
4729 If EXCLUDE-GROUP, do not go to this group."
4730   (save-excursion
4731     (set-buffer gnus-group-buffer)
4732     (save-excursion
4733       (gnus-group-best-unread-group exclude-group))))
4734
4735 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4736   (if backward (gnus-summary-find-prev)
4737     (let* ((dummy (gnus-summary-article-intangible-p))
4738            (article (or article (gnus-summary-article-number)))
4739            (arts (gnus-data-find-list article))
4740            result)
4741       (when (and (not dummy)
4742                  (or (not gnus-summary-check-current)
4743                      (not unread)
4744                      (not (gnus-data-unread-p (car arts)))))
4745         (setq arts (cdr arts)))
4746       (when (setq result
4747                   (if unread
4748                       (progn
4749                         (while arts
4750                           (when (or (and undownloaded
4751                                          (eq gnus-undownloaded-mark
4752                                              (gnus-data-mark (car arts))))
4753                                     (gnus-data-unread-p (car arts)))
4754                             (setq result (car arts)
4755                                   arts nil))
4756                           (setq arts (cdr arts)))
4757                         result)
4758                     (car arts)))
4759         (goto-char (gnus-data-pos result))
4760         (gnus-data-number result)))))
4761
4762 (defun gnus-summary-find-prev (&optional unread article)
4763   (let* ((eobp (eobp))
4764          (article (or article (gnus-summary-article-number)))
4765          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4766          result)
4767     (when (and (not eobp)
4768                (or (not gnus-summary-check-current)
4769                    (not unread)
4770                    (not (gnus-data-unread-p (car arts)))))
4771       (setq arts (cdr arts)))
4772     (when (setq result
4773                 (if unread
4774                     (progn
4775                       (while arts
4776                         (when (gnus-data-unread-p (car arts))
4777                           (setq result (car arts)
4778                                 arts nil))
4779                         (setq arts (cdr arts)))
4780                       result)
4781                   (car arts)))
4782       (goto-char (gnus-data-pos result))
4783       (gnus-data-number result))))
4784
4785 (defun gnus-summary-find-subject (subject &optional unread backward article)
4786   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4787          (article (or article (gnus-summary-article-number)))
4788          (articles (gnus-data-list backward))
4789          (arts (gnus-data-find-list article articles))
4790          result)
4791     (when (or (not gnus-summary-check-current)
4792               (not unread)
4793               (not (gnus-data-unread-p (car arts))))
4794       (setq arts (cdr arts)))
4795     (while arts
4796       (and (or (not unread)
4797                (gnus-data-unread-p (car arts)))
4798            (vectorp (gnus-data-header (car arts)))
4799            (gnus-subject-equal
4800             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4801            (setq result (car arts)
4802                  arts nil))
4803       (setq arts (cdr arts)))
4804     (and result
4805          (goto-char (gnus-data-pos result))
4806          (gnus-data-number result))))
4807
4808 (defun gnus-summary-search-forward (&optional unread subject backward)
4809   "Search forward for an article.
4810 If UNREAD, look for unread articles.  If SUBJECT, look for
4811 articles with that subject.  If BACKWARD, search backward instead."
4812   (cond (subject (gnus-summary-find-subject subject unread backward))
4813         (backward (gnus-summary-find-prev unread))
4814         (t (gnus-summary-find-next unread))))
4815
4816 (defun gnus-recenter (&optional n)
4817   "Center point in window and redisplay frame.
4818 Also do horizontal recentering."
4819   (interactive "P")
4820   (when (and gnus-auto-center-summary
4821              (not (eq gnus-auto-center-summary 'vertical)))
4822     (gnus-horizontal-recenter))
4823   (recenter n))
4824
4825 (defun gnus-summary-recenter ()
4826   "Center point in the summary window.
4827 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4828 displayed, no centering will be performed."
4829   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4830   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4831   (let* ((top (cond ((< (window-height) 4) 0)
4832                     ((< (window-height) 7) 1)
4833                     (t 2)))
4834          (height (1- (window-height)))
4835          (bottom (save-excursion (goto-char (point-max))
4836                                  (forward-line (- height))
4837                                  (point)))
4838          (window (get-buffer-window (current-buffer))))
4839     ;; The user has to want it.
4840     (when gnus-auto-center-summary
4841       (when (get-buffer-window gnus-article-buffer)
4842         ;; Only do recentering when the article buffer is displayed,
4843         ;; Set the window start to either `bottom', which is the biggest
4844         ;; possible valid number, or the second line from the top,
4845         ;; whichever is the least.
4846         (set-window-start
4847          window (min bottom (save-excursion
4848                               (forward-line (- top)) (point)))))
4849       ;; Do horizontal recentering while we're at it.
4850       (when (and (get-buffer-window (current-buffer) t)
4851                  (not (eq gnus-auto-center-summary 'vertical)))
4852         (let ((selected (selected-window)))
4853           (select-window (get-buffer-window (current-buffer) t))
4854           (gnus-summary-position-point)
4855           (gnus-horizontal-recenter)
4856           (select-window selected))))))
4857
4858 (defun gnus-summary-jump-to-group (newsgroup)
4859   "Move point to NEWSGROUP in group mode buffer."
4860   ;; Keep update point of group mode buffer if visible.
4861   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4862       (save-window-excursion
4863         ;; Take care of tree window mode.
4864         (when (get-buffer-window gnus-group-buffer)
4865           (pop-to-buffer gnus-group-buffer))
4866         (gnus-group-jump-to-group newsgroup))
4867     (save-excursion
4868       ;; Take care of tree window mode.
4869       (if (get-buffer-window gnus-group-buffer)
4870           (pop-to-buffer gnus-group-buffer)
4871         (set-buffer gnus-group-buffer))
4872       (gnus-group-jump-to-group newsgroup))))
4873
4874 ;; This function returns a list of article numbers based on the
4875 ;; difference between the ranges of read articles in this group and
4876 ;; the range of active articles.
4877 (defun gnus-list-of-unread-articles (group)
4878   (let* ((read (gnus-info-read (gnus-get-info group)))
4879          (active (or (gnus-active group) (gnus-activate-group group)))
4880          (last (cdr active))
4881          first nlast unread)
4882     ;; If none are read, then all are unread.
4883     (if (not read)
4884         (setq first (car active))
4885       ;; If the range of read articles is a single range, then the
4886       ;; first unread article is the article after the last read
4887       ;; article.  Sounds logical, doesn't it?
4888       (if (not (listp (cdr read)))
4889           (setq first (max (car active) (1+ (cdr read))))
4890         ;; `read' is a list of ranges.
4891         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
4892                                   (caar read)))
4893                   1)
4894           (setq first (car active)))
4895         (while read
4896           (when first
4897             (while (< first nlast)
4898               (push first unread)
4899               (setq first (1+ first))))
4900           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4901           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4902           (setq read (cdr read)))))
4903     ;; And add the last unread articles.
4904     (while (<= first last)
4905       (push first unread)
4906       (setq first (1+ first)))
4907     ;; Return the list of unread articles.
4908     (delq 0 (nreverse unread))))
4909
4910 (defun gnus-list-of-read-articles (group)
4911   "Return a list of unread, unticked and non-dormant articles."
4912   (let* ((info (gnus-get-info group))
4913          (marked (gnus-info-marks info))
4914          (active (gnus-active group)))
4915     (and info active
4916          (gnus-set-difference
4917           (gnus-sorted-complement
4918            (gnus-uncompress-range active)
4919            (gnus-list-of-unread-articles group))
4920           (append
4921            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4922            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4923
4924 ;; Various summary commands
4925
4926 (defun gnus-summary-select-article-buffer ()
4927   "Reconfigure windows to show article buffer."
4928   (interactive)
4929   (if (not (gnus-buffer-live-p gnus-article-buffer))
4930       (error "There is no article buffer for this summary buffer")
4931     (gnus-configure-windows 'article)
4932     (select-window (get-buffer-window gnus-article-buffer))))
4933
4934 (defun gnus-summary-universal-argument (arg)
4935   "Perform any operation on all articles that are process/prefixed."
4936   (interactive "P")
4937   (let ((articles (gnus-summary-work-articles arg))
4938         func article)
4939     (if (eq
4940          (setq
4941           func
4942           (key-binding
4943            (read-key-sequence
4944             (substitute-command-keys
4945              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4946              ))))
4947          'undefined)
4948         (gnus-error 1 "Undefined key")
4949       (save-excursion
4950         (while articles
4951           (gnus-summary-goto-subject (setq article (pop articles)))
4952           (let (gnus-newsgroup-processable)
4953             (command-execute func))
4954           (gnus-summary-remove-process-mark article)))))
4955   (gnus-summary-position-point))
4956
4957 (defun gnus-summary-toggle-truncation (&optional arg)
4958   "Toggle truncation of summary lines.
4959 With arg, turn line truncation on iff arg is positive."
4960   (interactive "P")
4961   (setq truncate-lines
4962         (if (null arg) (not truncate-lines)
4963           (> (prefix-numeric-value arg) 0)))
4964   (redraw-display))
4965
4966 (defun gnus-summary-reselect-current-group (&optional all rescan)
4967   "Exit and then reselect the current newsgroup.
4968 The prefix argument ALL means to select all articles."
4969   (interactive "P")
4970   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
4971     (error "Ephemeral groups can't be reselected"))
4972   (let ((current-subject (gnus-summary-article-number))
4973         (group gnus-newsgroup-name))
4974     (setq gnus-newsgroup-begin nil)
4975     (gnus-summary-exit)
4976     ;; We have to adjust the point of group mode buffer because
4977     ;; point was moved to the next unread newsgroup by exiting.
4978     (gnus-summary-jump-to-group group)
4979     (when rescan
4980       (save-excursion
4981         (gnus-group-get-new-news-this-group 1)))
4982     (gnus-group-read-group all t)
4983     (gnus-summary-goto-subject current-subject nil t)))
4984
4985 (defun gnus-summary-rescan-group (&optional all)
4986   "Exit the newsgroup, ask for new articles, and select the newsgroup."
4987   (interactive "P")
4988   (gnus-summary-reselect-current-group all t))
4989
4990 (defun gnus-summary-update-info (&optional non-destructive)
4991   (save-excursion
4992     (let ((group gnus-newsgroup-name))
4993       (when group
4994         (when gnus-newsgroup-kill-headers
4995           (setq gnus-newsgroup-killed
4996                 (gnus-compress-sequence
4997                  (nconc
4998                   (gnus-set-sorted-intersection
4999                    (gnus-uncompress-range gnus-newsgroup-killed)
5000                    (setq gnus-newsgroup-unselected
5001                          (sort gnus-newsgroup-unselected '<)))
5002                   (setq gnus-newsgroup-unreads
5003                         (sort gnus-newsgroup-unreads '<)))
5004                  t)))
5005         (unless (listp (cdr gnus-newsgroup-killed))
5006           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5007         (let ((headers gnus-newsgroup-headers))
5008           ;; Set the new ranges of read articles.
5009           (save-excursion
5010             (set-buffer gnus-group-buffer)
5011             (gnus-undo-force-boundary))
5012           (gnus-update-read-articles
5013            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5014           ;; Set the current article marks.
5015           (let ((gnus-newsgroup-scored 
5016                  (if (and (not gnus-save-score)
5017                           (not non-destructive))
5018                      nil
5019                    gnus-newsgroup-scored)))
5020             (save-excursion
5021               (gnus-update-marks)))
5022           ;; Do the cross-ref thing.
5023           (when gnus-use-cross-reference
5024             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5025           ;; Do not switch windows but change the buffer to work.
5026           (set-buffer gnus-group-buffer)
5027           (unless (gnus-ephemeral-group-p group)
5028             (gnus-group-update-group group)))))))
5029
5030 (defun gnus-summary-save-newsrc (&optional force)
5031   "Save the current number of read/marked articles in the dribble buffer.
5032 The dribble buffer will then be saved.
5033 If FORCE (the prefix), also save the .newsrc file(s)."
5034   (interactive "P")
5035   (gnus-summary-update-info t)
5036   (if force
5037       (gnus-save-newsrc-file)
5038     (gnus-dribble-save)))
5039
5040 (defun gnus-summary-exit (&optional temporary)
5041   "Exit reading current newsgroup, and then return to group selection mode.
5042 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5043   (interactive)
5044   (gnus-set-global-variables)
5045   (gnus-kill-save-kill-buffer)
5046   (gnus-async-halt-prefetch)
5047   (let* ((group gnus-newsgroup-name)
5048          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5049          (mode major-mode)
5050          (group-point nil)
5051          (buf (current-buffer)))
5052     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5053     ;; If we have several article buffers, we kill them at exit.
5054     (unless gnus-single-article-buffer
5055       (gnus-kill-buffer gnus-original-article-buffer)
5056       (setq gnus-article-current nil))
5057     (when gnus-use-cache
5058       (gnus-cache-possibly-remove-articles)
5059       (gnus-cache-save-buffers))
5060     (gnus-async-prefetch-remove-group group)
5061     (when gnus-suppress-duplicates
5062       (gnus-dup-enter-articles))
5063     (when gnus-use-trees
5064       (gnus-tree-close group))
5065     ;; Remove entries for this group.
5066     (nnmail-purge-split-history (gnus-group-real-name group))
5067     ;; Make all changes in this group permanent.
5068     (unless quit-config
5069       (gnus-run-hooks 'gnus-exit-group-hook)
5070       (gnus-summary-update-info)
5071       ;; Do adaptive scoring, and possibly save score files.
5072       (when gnus-newsgroup-adaptive
5073         (gnus-score-adaptive))
5074       (when gnus-use-scoring
5075         (gnus-score-save)))
5076     (gnus-close-group group)
5077     ;; Make sure where we were, and go to next newsgroup.
5078     (set-buffer gnus-group-buffer)
5079     (unless quit-config
5080       (gnus-group-jump-to-group group))
5081     (gnus-run-hooks 'gnus-summary-exit-hook)
5082     (unless (or quit-config
5083                 ;; If this group has disappeared from the summary
5084                 ;; buffer, don't skip forwards.
5085                 (not (string= group (gnus-group-group-name))))
5086       (gnus-group-next-unread-group 1))
5087     (setq group-point (point))
5088     (if temporary
5089         nil                             ;Nothing to do.
5090       ;; If we have several article buffers, we kill them at exit.
5091       (unless gnus-single-article-buffer
5092         (gnus-kill-buffer gnus-article-buffer)
5093         (gnus-kill-buffer gnus-original-article-buffer)
5094         (setq gnus-article-current nil))
5095       (set-buffer buf)
5096       (if (not gnus-kill-summary-on-exit)
5097           (gnus-deaden-summary)
5098         ;; We set all buffer-local variables to nil.  It is unclear why
5099         ;; this is needed, but if we don't, buffer-local variables are
5100         ;; not garbage-collected, it seems.  This would the lead to en
5101         ;; ever-growing Emacs.
5102         (gnus-summary-clear-local-variables)
5103         (when (get-buffer gnus-article-buffer)
5104           (bury-buffer gnus-article-buffer))
5105         ;; We clear the global counterparts of the buffer-local
5106         ;; variables as well, just to be on the safe side.
5107         (set-buffer gnus-group-buffer)
5108         (gnus-summary-clear-local-variables)
5109         ;; Return to group mode buffer.
5110         (when (eq mode 'gnus-summary-mode)
5111           (gnus-kill-buffer buf)))
5112       (setq gnus-current-select-method gnus-select-method)
5113       (pop-to-buffer gnus-group-buffer)
5114       (if (not quit-config)
5115           (progn
5116             (goto-char group-point)
5117             (gnus-configure-windows 'group 'force))
5118         (gnus-handle-ephemeral-exit quit-config))
5119       ;; Clear the current group name.
5120       (unless quit-config
5121         (setq gnus-newsgroup-name nil)))))
5122
5123 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5124 (defun gnus-summary-exit-no-update (&optional no-questions)
5125   "Quit reading current newsgroup without updating read article info."
5126   (interactive)
5127   (let* ((group gnus-newsgroup-name)
5128          (quit-config (gnus-group-quit-config group)))
5129     (when (or no-questions
5130               gnus-expert-user
5131               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5132       (gnus-async-halt-prefetch)
5133       (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5134       ;; If we have several article buffers, we kill them at exit.
5135       (unless gnus-single-article-buffer
5136         (gnus-kill-buffer gnus-article-buffer)
5137         (gnus-kill-buffer gnus-original-article-buffer)
5138         (setq gnus-article-current nil))
5139       (if (not gnus-kill-summary-on-exit)
5140           (gnus-deaden-summary)
5141         (gnus-close-group group)
5142         (gnus-summary-clear-local-variables)
5143         (set-buffer gnus-group-buffer)
5144         (gnus-summary-clear-local-variables)
5145         (when (get-buffer gnus-summary-buffer)
5146           (kill-buffer gnus-summary-buffer)))
5147       (unless gnus-single-article-buffer
5148         (setq gnus-article-current nil))
5149       (when gnus-use-trees
5150         (gnus-tree-close group))
5151       (gnus-async-prefetch-remove-group group)
5152       (when (get-buffer gnus-article-buffer)
5153         (bury-buffer gnus-article-buffer))
5154       ;; Return to the group buffer.
5155       (gnus-configure-windows 'group 'force)
5156       ;; Clear the current group name.
5157       (setq gnus-newsgroup-name nil)
5158       (when (equal (gnus-group-group-name) group)
5159         (gnus-group-next-unread-group 1))
5160       (when quit-config
5161         (gnus-handle-ephemeral-exit quit-config)))))
5162
5163 (defun gnus-handle-ephemeral-exit (quit-config)
5164   "Handle movement when leaving an ephemeral group.
5165 The state which existed when entering the ephemeral is reset."
5166   (if (not (buffer-name (car quit-config)))
5167       (gnus-configure-windows 'group 'force)
5168     (set-buffer (car quit-config))
5169     (cond ((eq major-mode 'gnus-summary-mode)
5170            (gnus-set-global-variables))
5171           ((eq major-mode 'gnus-article-mode)
5172            (save-excursion
5173              ;; The `gnus-summary-buffer' variable may point
5174              ;; to the old summary buffer when using a single
5175              ;; article buffer.
5176              (unless (gnus-buffer-live-p gnus-summary-buffer)
5177                (set-buffer gnus-group-buffer))
5178              (set-buffer gnus-summary-buffer)
5179              (gnus-set-global-variables))))
5180     (if (or (eq (cdr quit-config) 'article)
5181             (eq (cdr quit-config) 'pick))
5182         (progn
5183           ;; The current article may be from the ephemeral group
5184           ;; thus it is best that we reload this article
5185           (gnus-summary-show-article)
5186           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5187               (gnus-configure-windows 'pick 'force)
5188             (gnus-configure-windows (cdr quit-config) 'force)))
5189       (gnus-configure-windows (cdr quit-config) 'force))
5190     (when (eq major-mode 'gnus-summary-mode)
5191       (gnus-summary-next-subject 1 nil t)
5192       (gnus-summary-recenter)
5193       (gnus-summary-position-point))))
5194
5195 ;;; Dead summaries.
5196
5197 (defvar gnus-dead-summary-mode-map nil)
5198
5199 (unless gnus-dead-summary-mode-map
5200   (setq gnus-dead-summary-mode-map (make-keymap))
5201   (suppress-keymap gnus-dead-summary-mode-map)
5202   (substitute-key-definition
5203    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5204   (let ((keys '("\C-d" "\r" "\177" [delete])))
5205     (while keys
5206       (define-key gnus-dead-summary-mode-map
5207         (pop keys) 'gnus-summary-wake-up-the-dead))))
5208
5209 (defvar gnus-dead-summary-mode nil
5210   "Minor mode for Gnus summary buffers.")
5211
5212 (defun gnus-dead-summary-mode (&optional arg)
5213   "Minor mode for Gnus summary buffers."
5214   (interactive "P")
5215   (when (eq major-mode 'gnus-summary-mode)
5216     (make-local-variable 'gnus-dead-summary-mode)
5217     (setq gnus-dead-summary-mode
5218           (if (null arg) (not gnus-dead-summary-mode)
5219             (> (prefix-numeric-value arg) 0)))
5220     (when gnus-dead-summary-mode
5221       (gnus-add-minor-mode
5222        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5223
5224 (defun gnus-deaden-summary ()
5225   "Make the current summary buffer into a dead summary buffer."
5226   ;; Kill any previous dead summary buffer.
5227   (when (and gnus-dead-summary
5228              (buffer-name gnus-dead-summary))
5229     (save-excursion
5230       (set-buffer gnus-dead-summary)
5231       (when gnus-dead-summary-mode
5232         (kill-buffer (current-buffer)))))
5233   ;; Make this the current dead summary.
5234   (setq gnus-dead-summary (current-buffer))
5235   (gnus-dead-summary-mode 1)
5236   (let ((name (buffer-name)))
5237     (when (string-match "Summary" name)
5238       (rename-buffer
5239        (concat (substring name 0 (match-beginning 0)) "Dead "
5240                (substring name (match-beginning 0)))
5241        t))))
5242
5243 (defun gnus-kill-or-deaden-summary (buffer)
5244   "Kill or deaden the summary BUFFER."
5245   (save-excursion
5246     (when (and (buffer-name buffer)
5247                (not gnus-single-article-buffer))
5248       (save-excursion
5249         (set-buffer buffer)
5250         (gnus-kill-buffer gnus-article-buffer)
5251         (gnus-kill-buffer gnus-original-article-buffer)))
5252     (cond (gnus-kill-summary-on-exit
5253            (when (and gnus-use-trees
5254                       (gnus-buffer-exists-p buffer))
5255              (save-excursion
5256                (set-buffer buffer)
5257                (gnus-tree-close gnus-newsgroup-name)))
5258            (gnus-kill-buffer buffer))
5259           ((gnus-buffer-exists-p buffer)
5260            (save-excursion
5261              (set-buffer buffer)
5262              (gnus-deaden-summary))))))
5263
5264 (defun gnus-summary-wake-up-the-dead (&rest args)
5265   "Wake up the dead summary buffer."
5266   (interactive)
5267   (gnus-dead-summary-mode -1)
5268   (let ((name (buffer-name)))
5269     (when (string-match "Dead " name)
5270       (rename-buffer
5271        (concat (substring name 0 (match-beginning 0))
5272                (substring name (match-end 0)))
5273        t)))
5274   (gnus-message 3 "This dead summary is now alive again"))
5275
5276 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5277 (defun gnus-summary-fetch-faq (&optional faq-dir)
5278   "Fetch the FAQ for the current group.
5279 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5280 in."
5281   (interactive
5282    (list
5283     (when current-prefix-arg
5284       (completing-read
5285        "Faq dir: " (and (listp gnus-group-faq-directory)
5286                         (mapcar (lambda (file) (list file))
5287                                 gnus-group-faq-directory))))))
5288   (let (gnus-faq-buffer)
5289     (when (setq gnus-faq-buffer
5290                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5291       (gnus-configure-windows 'summary-faq))))
5292
5293 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5294 (defun gnus-summary-describe-group (&optional force)
5295   "Describe the current newsgroup."
5296   (interactive "P")
5297   (gnus-group-describe-group force gnus-newsgroup-name))
5298
5299 (defun gnus-summary-describe-briefly ()
5300   "Describe summary mode commands briefly."
5301   (interactive)
5302   (gnus-message 6
5303                 (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")))
5304
5305 ;; Walking around group mode buffer from summary mode.
5306
5307 (defun gnus-summary-next-group (&optional no-article target-group backward)
5308   "Exit current newsgroup and then select next unread newsgroup.
5309 If prefix argument NO-ARTICLE is non-nil, no article is selected
5310 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5311 previous group instead."
5312   (interactive "P")
5313   ;; Stop pre-fetching.
5314   (gnus-async-halt-prefetch)
5315   (let ((current-group gnus-newsgroup-name)
5316         (current-buffer (current-buffer))
5317         entered)
5318     ;; First we semi-exit this group to update Xrefs and all variables.
5319     ;; We can't do a real exit, because the window conf must remain
5320     ;; the same in case the user is prompted for info, and we don't
5321     ;; want the window conf to change before that...
5322     (gnus-summary-exit t)
5323     (while (not entered)
5324       ;; Then we find what group we are supposed to enter.
5325       (set-buffer gnus-group-buffer)
5326       (gnus-group-jump-to-group current-group)
5327       (setq target-group
5328             (or target-group
5329                 (if (eq gnus-keep-same-level 'best)
5330                     (gnus-summary-best-group gnus-newsgroup-name)
5331                   (gnus-summary-search-group backward gnus-keep-same-level))))
5332       (if (not target-group)
5333           ;; There are no further groups, so we return to the group
5334           ;; buffer.
5335           (progn
5336             (gnus-message 5 "Returning to the group buffer")
5337             (setq entered t)
5338             (when (gnus-buffer-live-p current-buffer)
5339               (set-buffer current-buffer)
5340               (gnus-summary-exit))
5341             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5342         ;; We try to enter the target group.
5343         (gnus-group-jump-to-group target-group)
5344         (let ((unreads (gnus-group-group-unread)))
5345           (if (and (or (eq t unreads)
5346                        (and unreads (not (zerop unreads))))
5347                    (gnus-summary-read-group
5348                     target-group nil no-article
5349                     (and (buffer-name current-buffer) current-buffer)
5350                     nil backward))
5351               (setq entered t)
5352             (setq current-group target-group
5353                   target-group nil)))))))
5354
5355 (defun gnus-summary-prev-group (&optional no-article)
5356   "Exit current newsgroup and then select previous unread newsgroup.
5357 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5358   (interactive "P")
5359   (gnus-summary-next-group no-article nil t))
5360
5361 ;; Walking around summary lines.
5362
5363 (defun gnus-summary-first-subject (&optional unread undownloaded)
5364   "Go to the first unread subject.
5365 If UNREAD is non-nil, go to the first unread article.
5366 Returns the article selected or nil if there are no unread articles."
5367   (interactive "P")
5368   (prog1
5369       (cond
5370        ;; Empty summary.
5371        ((null gnus-newsgroup-data)
5372         (gnus-message 3 "No articles in the group")
5373         nil)
5374        ;; Pick the first article.
5375        ((not unread)
5376         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5377         (gnus-data-number (car gnus-newsgroup-data)))
5378        ;; No unread articles.
5379        ((null gnus-newsgroup-unreads)
5380         (gnus-message 3 "No more unread articles")
5381         nil)
5382        ;; Find the first unread article.
5383        (t
5384         (let ((data gnus-newsgroup-data))
5385           (while (and data
5386                       (and (not (and undownloaded
5387                                      (eq gnus-undownloaded-mark
5388                                          (gnus-data-mark (car data)))))
5389                            (not (gnus-data-unread-p (car data)))))
5390             (setq data (cdr data)))
5391           (when data
5392             (goto-char (gnus-data-pos (car data)))
5393             (gnus-data-number (car data))))))
5394     (gnus-summary-position-point)))
5395
5396 (defun gnus-summary-next-subject (n &optional unread dont-display)
5397   "Go to next N'th summary line.
5398 If N is negative, go to the previous N'th subject line.
5399 If UNREAD is non-nil, only unread articles are selected.
5400 The difference between N and the actual number of steps taken is
5401 returned."
5402   (interactive "p")
5403   (let ((backward (< n 0))
5404         (n (abs n)))
5405     (while (and (> n 0)
5406                 (if backward
5407                     (gnus-summary-find-prev unread)
5408                   (gnus-summary-find-next unread)))
5409       (gnus-summary-show-thread)
5410       (setq n (1- n)))
5411     (when (/= 0 n)
5412       (gnus-message 7 "No more%s articles"
5413                     (if unread " unread" "")))
5414     (unless dont-display
5415       (gnus-summary-recenter)
5416       (gnus-summary-position-point))
5417     n))
5418
5419 (defun gnus-summary-next-unread-subject (n)
5420   "Go to next N'th unread summary line."
5421   (interactive "p")
5422   (gnus-summary-next-subject n t))
5423
5424 (defun gnus-summary-prev-subject (n &optional unread)
5425   "Go to previous N'th summary line.
5426 If optional argument UNREAD is non-nil, only unread article is selected."
5427   (interactive "p")
5428   (gnus-summary-next-subject (- n) unread))
5429
5430 (defun gnus-summary-prev-unread-subject (n)
5431   "Go to previous N'th unread summary line."
5432   (interactive "p")
5433   (gnus-summary-next-subject (- n) t))
5434
5435 (defun gnus-summary-goto-subject (article &optional force silent)
5436   "Go the subject line of ARTICLE.
5437 If FORCE, also allow jumping to articles not currently shown."
5438   (interactive "nArticle number: ")
5439   (let ((b (point))
5440         (data (gnus-data-find article)))
5441     ;; We read in the article if we have to.
5442     (and (not data)
5443          force
5444          (gnus-summary-insert-subject
5445           article
5446           (if (or (numberp force) (vectorp force)) force)
5447           t)
5448          (setq data (gnus-data-find article)))
5449     (goto-char b)
5450     (if (not data)
5451         (progn
5452           (unless silent
5453             (gnus-message 3 "Can't find article %d" article))
5454           nil)
5455       (goto-char (gnus-data-pos data))
5456       (gnus-summary-position-point)
5457       article)))
5458
5459 ;; Walking around summary lines with displaying articles.
5460
5461 (defun gnus-summary-expand-window (&optional arg)
5462   "Make the summary buffer take up the entire Emacs frame.
5463 Given a prefix, will force an `article' buffer configuration."
5464   (interactive "P")
5465   (if arg
5466       (gnus-configure-windows 'article 'force)
5467     (gnus-configure-windows 'summary 'force)))
5468
5469 (defun gnus-summary-display-article (article &optional all-header)
5470   "Display ARTICLE in article buffer."
5471   (gnus-set-global-variables)
5472   (if (null article)
5473       nil
5474     (prog1
5475         (if gnus-summary-display-article-function
5476             (funcall gnus-summary-display-article-function article all-header)
5477           (gnus-article-prepare article all-header))
5478       (gnus-run-hooks 'gnus-select-article-hook)
5479       (when (and gnus-current-article
5480                  (not (zerop gnus-current-article)))
5481         (gnus-summary-goto-subject gnus-current-article))
5482       (gnus-summary-recenter)
5483       (when (and gnus-use-trees gnus-show-threads)
5484         (gnus-possibly-generate-tree article)
5485         (gnus-highlight-selected-tree article))
5486       ;; Successfully display article.
5487       (gnus-article-set-window-start
5488        (cdr (assq article gnus-newsgroup-bookmarks))))))
5489
5490 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5491   "Select the current article.
5492 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5493 non-nil, the article will be re-fetched even if it already present in
5494 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5495 be displayed."
5496   ;; Make sure we are in the summary buffer to work around bbdb bug.
5497   (unless (eq major-mode 'gnus-summary-mode)
5498     (set-buffer gnus-summary-buffer))
5499   (let ((article (or article (gnus-summary-article-number)))
5500         (all-headers (not (not all-headers))) ;Must be T or NIL.
5501         gnus-summary-display-article-function
5502         did)
5503     (and (not pseudo)
5504          (gnus-summary-article-pseudo-p article)
5505          (error "This is a pseudo-article"))
5506     (prog1
5507         (save-excursion
5508           (set-buffer gnus-summary-buffer)
5509           (if (or (and gnus-single-article-buffer
5510                        (or (null gnus-current-article)
5511                            (null gnus-article-current)
5512                            (null (get-buffer gnus-article-buffer))
5513                            (not (eq article (cdr gnus-article-current)))
5514                            (not (equal (car gnus-article-current)
5515                                        gnus-newsgroup-name))))
5516                   (and (not gnus-single-article-buffer)
5517                        (or (null gnus-current-article)
5518                            (not (eq gnus-current-article article))))
5519                   force)
5520               ;; The requested article is different from the current article.
5521               (prog1
5522                   (gnus-summary-display-article article all-headers)
5523                 (setq did article))
5524             (when (or all-headers gnus-show-all-headers)
5525               (gnus-article-show-all-headers))
5526             'old))
5527       (when did
5528         (gnus-article-set-window-start
5529          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5530
5531 (defun gnus-summary-set-current-mark (&optional current-mark)
5532   "Obsolete function."
5533   nil)
5534
5535 (defun gnus-summary-next-article (&optional unread subject backward push)
5536   "Select the next article.
5537 If UNREAD, only unread articles are selected.
5538 If SUBJECT, only articles with SUBJECT are selected.
5539 If BACKWARD, the previous article is selected instead of the next."
5540   (interactive "P")
5541   (cond
5542    ;; Is there such an article?
5543    ((and (gnus-summary-search-forward unread subject backward)
5544          (or (gnus-summary-display-article (gnus-summary-article-number))
5545              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5546     (gnus-summary-position-point))
5547    ;; If not, we try the first unread, if that is wanted.
5548    ((and subject
5549          gnus-auto-select-same
5550          (gnus-summary-first-unread-article))
5551     (gnus-summary-position-point)
5552     (gnus-message 6 "Wrapped"))
5553    ;; Try to get next/previous article not displayed in this group.
5554    ((and gnus-auto-extend-newsgroup
5555          (not unread) (not subject))
5556     (gnus-summary-goto-article
5557      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5558      nil (count-lines (point-min) (point))))
5559    ;; Go to next/previous group.
5560    (t
5561     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5562       (gnus-summary-jump-to-group gnus-newsgroup-name))
5563     (let ((cmd last-command-char)
5564           (point
5565            (save-excursion
5566              (set-buffer gnus-group-buffer)
5567              (point)))
5568           (group
5569            (if (eq gnus-keep-same-level 'best)
5570                (gnus-summary-best-group gnus-newsgroup-name)
5571              (gnus-summary-search-group backward gnus-keep-same-level))))
5572       ;; For some reason, the group window gets selected.  We change
5573       ;; it back.
5574       (select-window (get-buffer-window (current-buffer)))
5575       ;; Select next unread newsgroup automagically.
5576       (cond
5577        ((or (not gnus-auto-select-next)
5578             (not cmd))
5579         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5580        ((or (eq gnus-auto-select-next 'quietly)
5581             (and (eq gnus-auto-select-next 'slightly-quietly)
5582                  push)
5583             (and (eq gnus-auto-select-next 'almost-quietly)
5584                  (gnus-summary-last-article-p)))
5585         ;; Select quietly.
5586         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5587             (gnus-summary-exit)
5588           (gnus-message 7 "No more%s articles (%s)..."
5589                         (if unread " unread" "")
5590                         (if group (concat "selecting " group)
5591                           "exiting"))
5592           (gnus-summary-next-group nil group backward)))
5593        (t
5594         (when (gnus-key-press-event-p last-input-event)
5595           (gnus-summary-walk-group-buffer
5596            gnus-newsgroup-name cmd unread backward point))))))))
5597
5598 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5599   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5600                       (?\C-p (gnus-group-prev-unread-group 1))))
5601         (cursor-in-echo-area t)
5602         keve key group ended)
5603     (save-excursion
5604       (set-buffer gnus-group-buffer)
5605       (goto-char start)
5606       (setq group
5607             (if (eq gnus-keep-same-level 'best)
5608                 (gnus-summary-best-group gnus-newsgroup-name)
5609               (gnus-summary-search-group backward gnus-keep-same-level))))
5610     (while (not ended)
5611       (gnus-message
5612        5 "No more%s articles%s" (if unread " unread" "")
5613        (if (and group
5614                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5615            (format " (Type %s for %s [%s])"
5616                    (single-key-description cmd) group
5617                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5618          (format " (Type %s to exit %s)"
5619                  (single-key-description cmd)
5620                  gnus-newsgroup-name)))
5621       ;; Confirm auto selection.
5622       (setq key (car (setq keve (gnus-read-event-char))))
5623       (setq ended t)
5624       (cond
5625        ((assq key keystrokes)
5626         (let ((obuf (current-buffer)))
5627           (switch-to-buffer gnus-group-buffer)
5628           (when group
5629             (gnus-group-jump-to-group group))
5630           (eval (cadr (assq key keystrokes)))
5631           (setq group (gnus-group-group-name))
5632           (switch-to-buffer obuf))
5633         (setq ended nil))
5634        ((equal key cmd)
5635         (if (or (not group)
5636                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5637             (gnus-summary-exit)
5638           (gnus-summary-next-group nil group backward)))
5639        (t
5640         (push (cdr keve) unread-command-events))))))
5641
5642 (defun gnus-summary-next-unread-article ()
5643   "Select unread article after current one."
5644   (interactive)
5645   (gnus-summary-next-article
5646    (or (not (eq gnus-summary-goto-unread 'never))
5647        (gnus-summary-last-article-p (gnus-summary-article-number)))
5648    (and gnus-auto-select-same
5649         (gnus-summary-article-subject))))
5650
5651 (defun gnus-summary-prev-article (&optional unread subject)
5652   "Select the article after the current one.
5653 If UNREAD is non-nil, only unread articles are selected."
5654   (interactive "P")
5655   (gnus-summary-next-article unread subject t))
5656
5657 (defun gnus-summary-prev-unread-article ()
5658   "Select unread article before current one."
5659   (interactive)
5660   (gnus-summary-prev-article
5661    (or (not (eq gnus-summary-goto-unread 'never))
5662        (gnus-summary-first-article-p (gnus-summary-article-number)))
5663    (and gnus-auto-select-same
5664         (gnus-summary-article-subject))))
5665
5666 (defun gnus-summary-next-page (&optional lines circular)
5667   "Show next page of the selected article.
5668 If at the end of the current article, select the next article.
5669 LINES says how many lines should be scrolled up.
5670
5671 If CIRCULAR is non-nil, go to the start of the article instead of
5672 selecting the next article when reaching the end of the current
5673 article."
5674   (interactive "P")
5675   (setq gnus-summary-buffer (current-buffer))
5676   (gnus-set-global-variables)
5677   (let ((article (gnus-summary-article-number))
5678         (article-window (get-buffer-window gnus-article-buffer t))
5679         endp)
5680     ;; If the buffer is empty, we have no article.
5681     (unless article
5682       (error "No article to select"))
5683     (gnus-configure-windows 'article)
5684     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5685         (if (and (eq gnus-summary-goto-unread 'never)
5686                  (not (gnus-summary-last-article-p article)))
5687             (gnus-summary-next-article)
5688           (gnus-summary-next-unread-article))
5689       (if (or (null gnus-current-article)
5690               (null gnus-article-current)
5691               (/= article (cdr gnus-article-current))
5692               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5693           ;; Selected subject is different from current article's.
5694           (gnus-summary-display-article article)
5695         (when article-window
5696           (gnus-eval-in-buffer-window gnus-article-buffer
5697             (setq endp (gnus-article-next-page lines)))
5698           (when endp
5699             (cond (circular
5700                    (gnus-summary-beginning-of-article))
5701                   (lines
5702                    (gnus-message 3 "End of message"))
5703                   ((null lines)
5704                    (if (and (eq gnus-summary-goto-unread 'never)
5705                             (not (gnus-summary-last-article-p article)))
5706                        (gnus-summary-next-article)
5707                      (gnus-summary-next-unread-article))))))))
5708     (gnus-summary-recenter)
5709     (gnus-summary-position-point)))
5710
5711 (defun gnus-summary-prev-page (&optional lines move)
5712   "Show previous page of selected article.
5713 Argument LINES specifies lines to be scrolled down.
5714 If MOVE, move to the previous unread article if point is at
5715 the beginning of the buffer."
5716   (interactive "P")
5717   (let ((article (gnus-summary-article-number))
5718         (article-window (get-buffer-window gnus-article-buffer t))
5719         endp)
5720     (gnus-configure-windows 'article)
5721     (if (or (null gnus-current-article)
5722             (null gnus-article-current)
5723             (/= article (cdr gnus-article-current))
5724             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5725         ;; Selected subject is different from current article's.
5726         (gnus-summary-display-article article)
5727       (gnus-summary-recenter)
5728       (when article-window
5729         (gnus-eval-in-buffer-window gnus-article-buffer
5730           (setq endp (gnus-article-prev-page lines)))
5731         (when (and move endp)
5732           (cond (lines
5733                  (gnus-message 3 "Beginning of message"))
5734                 ((null lines)
5735                  (if (and (eq gnus-summary-goto-unread 'never)
5736                           (not (gnus-summary-first-article-p article)))
5737                      (gnus-summary-prev-article)
5738                    (gnus-summary-prev-unread-article))))))))
5739   (gnus-summary-position-point))
5740
5741 (defun gnus-summary-prev-page-or-article (&optional lines)
5742   "Show previous page of selected article.
5743 Argument LINES specifies lines to be scrolled down.
5744 If at the beginning of the article, go to the next article."
5745   (interactive "P")
5746   (gnus-summary-prev-page lines t))
5747
5748 (defun gnus-summary-scroll-up (lines)
5749   "Scroll up (or down) one line current article.
5750 Argument LINES specifies lines to be scrolled up (or down if negative)."
5751   (interactive "p")
5752   (gnus-configure-windows 'article)
5753   (gnus-summary-show-thread)
5754   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5755     (gnus-eval-in-buffer-window gnus-article-buffer
5756       (cond ((> lines 0)
5757              (when (gnus-article-next-page lines)
5758                (gnus-message 3 "End of message")))
5759             ((< lines 0)
5760              (gnus-article-prev-page (- lines))))))
5761   (gnus-summary-recenter)
5762   (gnus-summary-position-point))
5763
5764 (defun gnus-summary-scroll-down (lines)
5765   "Scroll down (or up) one line current article.
5766 Argument LINES specifies lines to be scrolled down (or up if negative)."
5767   (interactive "p")
5768   (gnus-summary-scroll-up (- lines)))
5769
5770 (defun gnus-summary-next-same-subject ()
5771   "Select next article which has the same subject as current one."
5772   (interactive)
5773   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5774
5775 (defun gnus-summary-prev-same-subject ()
5776   "Select previous article which has the same subject as current one."
5777   (interactive)
5778   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5779
5780 (defun gnus-summary-next-unread-same-subject ()
5781   "Select next unread article which has the same subject as current one."
5782   (interactive)
5783   (gnus-summary-next-article t (gnus-summary-article-subject)))
5784
5785 (defun gnus-summary-prev-unread-same-subject ()
5786   "Select previous unread article which has the same subject as current one."
5787   (interactive)
5788   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5789
5790 (defun gnus-summary-first-unread-article ()
5791   "Select the first unread article.
5792 Return nil if there are no unread articles."
5793   (interactive)
5794   (prog1
5795       (when (gnus-summary-first-subject t)
5796         (gnus-summary-show-thread)
5797         (gnus-summary-first-subject t)
5798         (gnus-summary-display-article (gnus-summary-article-number)))
5799     (gnus-summary-position-point)))
5800
5801 (defun gnus-summary-first-article ()
5802   "Select the first article.
5803 Return nil if there are no articles."
5804   (interactive)
5805   (prog1
5806       (when (gnus-summary-first-subject)
5807       (gnus-summary-show-thread)
5808       (gnus-summary-first-subject)
5809       (gnus-summary-display-article (gnus-summary-article-number)))
5810     (gnus-summary-position-point)))
5811
5812 (defun gnus-summary-best-unread-article ()
5813   "Select the unread article with the highest score."
5814   (interactive)
5815   (let ((best -1000000)
5816         (data gnus-newsgroup-data)
5817         article score)
5818     (while data
5819       (and (gnus-data-unread-p (car data))
5820            (> (setq score
5821                     (gnus-summary-article-score (gnus-data-number (car data))))
5822               best)
5823            (setq best score
5824                  article (gnus-data-number (car data))))
5825       (setq data (cdr data)))
5826     (prog1
5827         (if article
5828             (gnus-summary-goto-article article)
5829           (error "No unread articles"))
5830       (gnus-summary-position-point))))
5831
5832 (defun gnus-summary-last-subject ()
5833   "Go to the last displayed subject line in the group."
5834   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5835     (when article
5836       (gnus-summary-goto-subject article))))
5837
5838 (defun gnus-summary-goto-article (article &optional all-headers force)
5839   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5840 If ALL-HEADERS is non-nil, no header lines are hidden.
5841 If FORCE, go to the article even if it isn't displayed.  If FORCE
5842 is a number, it is the line the article is to be displayed on."
5843   (interactive
5844    (list
5845     (completing-read
5846      "Article number or Message-ID: "
5847      (mapcar (lambda (number) (list (int-to-string number)))
5848              gnus-newsgroup-limit))
5849     current-prefix-arg
5850     t))
5851   (prog1
5852       (if (and (stringp article)
5853                (string-match "@" article))
5854           (gnus-summary-refer-article article)
5855         (when (stringp article)
5856           (setq article (string-to-number article)))
5857         (if (gnus-summary-goto-subject article force)
5858             (gnus-summary-display-article article all-headers)
5859           (gnus-message 4 "Couldn't go to article %s" article) nil))
5860     (gnus-summary-position-point)))
5861
5862 (defun gnus-summary-goto-last-article ()
5863   "Go to the previously read article."
5864   (interactive)
5865   (prog1
5866       (when gnus-last-article
5867         (gnus-summary-goto-article gnus-last-article nil t))
5868     (gnus-summary-position-point)))
5869
5870 (defun gnus-summary-pop-article (number)
5871   "Pop one article off the history and go to the previous.
5872 NUMBER articles will be popped off."
5873   (interactive "p")
5874   (let (to)
5875     (setq gnus-newsgroup-history
5876           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5877     (if to
5878         (gnus-summary-goto-article (car to) nil t)
5879       (error "Article history empty")))
5880   (gnus-summary-position-point))
5881
5882 ;; Summary commands and functions for limiting the summary buffer.
5883
5884 (defun gnus-summary-limit-to-articles (n)
5885   "Limit the summary buffer to the next N articles.
5886 If not given a prefix, use the process marked articles instead."
5887   (interactive "P")
5888   (prog1
5889       (let ((articles (gnus-summary-work-articles n)))
5890         (setq gnus-newsgroup-processable nil)
5891         (gnus-summary-limit articles))
5892     (gnus-summary-position-point)))
5893
5894 (defun gnus-summary-pop-limit (&optional total)
5895   "Restore the previous limit.
5896 If given a prefix, remove all limits."
5897   (interactive "P")
5898   (when total
5899     (setq gnus-newsgroup-limits
5900           (list (mapcar (lambda (h) (mail-header-number h))
5901                         gnus-newsgroup-headers))))
5902   (unless gnus-newsgroup-limits
5903     (error "No limit to pop"))
5904   (prog1
5905       (gnus-summary-limit nil 'pop)
5906     (gnus-summary-position-point)))
5907
5908 (defun gnus-summary-limit-to-subject (subject &optional header)
5909   "Limit the summary buffer to articles that have subjects that match a regexp."
5910   (interactive "sLimit to subject (regexp): ")
5911   (unless header
5912     (setq header "subject"))
5913   (when (not (equal "" subject))
5914     (prog1
5915         (let ((articles (gnus-summary-find-matching
5916                          (or header "subject") subject 'all)))
5917           (unless articles
5918             (error "Found no matches for \"%s\"" subject))
5919           (gnus-summary-limit articles))
5920       (gnus-summary-position-point))))
5921
5922 (defun gnus-summary-limit-to-author (from)
5923   "Limit the summary buffer to articles that have authors that match a regexp."
5924   (interactive "sLimit to author (regexp): ")
5925   (gnus-summary-limit-to-subject from "from"))
5926
5927 (defun gnus-summary-limit-to-age (age &optional younger-p)
5928   "Limit the summary buffer to articles that are older than (or equal) AGE days.
5929 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5930 articles that are younger than AGE days."
5931   (interactive "nTime in days: \nP")
5932   (prog1
5933       (let ((data gnus-newsgroup-data)
5934             (cutoff (days-to-time age))
5935             articles d date is-younger)
5936         (while (setq d (pop data))
5937           (when (and (vectorp (gnus-data-header d))
5938                      (setq date (mail-header-date (gnus-data-header d))))
5939             (setq is-younger (time-less-p
5940                               (time-since (date-to-time date))
5941                               cutoff))
5942             (when (if younger-p
5943                       is-younger
5944                     (not is-younger))
5945               (push (gnus-data-number d) articles))))
5946         (gnus-summary-limit (nreverse articles)))
5947     (gnus-summary-position-point)))
5948
5949 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5950 (make-obsolete
5951  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5952
5953 (defun gnus-summary-limit-to-unread (&optional all)
5954   "Limit the summary buffer to articles that are not marked as read.
5955 If ALL is non-nil, limit strictly to unread articles."
5956   (interactive "P")
5957   (if all
5958       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5959     (gnus-summary-limit-to-marks
5960      ;; Concat all the marks that say that an article is read and have
5961      ;; those removed.
5962      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5963            gnus-killed-mark gnus-kill-file-mark
5964            gnus-low-score-mark gnus-expirable-mark
5965            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5966            gnus-duplicate-mark gnus-souped-mark)
5967      'reverse)))
5968
5969 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
5970 (make-obsolete 'gnus-summary-delete-marked-with
5971                'gnus-summary-limit-exlude-marks)
5972
5973 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
5974   "Exclude articles that are marked with MARKS (e.g. \"DK\").
5975 If REVERSE, limit the summary buffer to articles that are marked
5976 with MARKS.  MARKS can either be a string of marks or a list of marks.
5977 Returns how many articles were removed."
5978   (interactive "sMarks: ")
5979   (gnus-summary-limit-to-marks marks t))
5980
5981 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5982   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5983 If REVERSE (the prefix), limit the summary buffer to articles that are
5984 not marked with MARKS.  MARKS can either be a string of marks or a
5985 list of marks.
5986 Returns how many articles were removed."
5987   (interactive "sMarks: \nP")
5988   (prog1
5989       (let ((data gnus-newsgroup-data)
5990             (marks (if (listp marks) marks
5991                      (append marks nil))) ; Transform to list.
5992             articles)
5993         (while data
5994           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
5995                   (memq (gnus-data-mark (car data)) marks))
5996             (push (gnus-data-number (car data)) articles))
5997           (setq data (cdr data)))
5998         (gnus-summary-limit articles))
5999     (gnus-summary-position-point)))
6000
6001 (defun gnus-summary-limit-to-score (&optional score)
6002   "Limit to articles with score at or above SCORE."
6003   (interactive "P")
6004   (setq score (if score
6005                   (prefix-numeric-value score)
6006                 (or gnus-summary-default-score 0)))
6007   (let ((data gnus-newsgroup-data)
6008         articles)
6009     (while data
6010       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6011                 score)
6012         (push (gnus-data-number (car data)) articles))
6013       (setq data (cdr data)))
6014     (prog1
6015         (gnus-summary-limit articles)
6016       (gnus-summary-position-point))))
6017
6018 (defun gnus-summary-limit-include-thread (id)
6019   "Display all the hidden articles that in the current thread."
6020   (interactive (list (mail-header-id (gnus-summary-article-header))))
6021   (let ((articles (gnus-articles-in-thread
6022                    (gnus-id-to-thread (gnus-root-id id)))))
6023     (prog1
6024         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6025       (gnus-summary-position-point))))
6026
6027 (defun gnus-summary-limit-include-dormant ()
6028   "Display all the hidden articles that are marked as dormant.
6029 Note that this command only works on a subset of the articles currently
6030 fetched for this group."
6031   (interactive)
6032   (unless gnus-newsgroup-dormant
6033     (error "There are no dormant articles in this group"))
6034   (prog1
6035       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6036     (gnus-summary-position-point)))
6037
6038 (defun gnus-summary-limit-exclude-dormant ()
6039   "Hide all dormant articles."
6040   (interactive)
6041   (prog1
6042       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6043     (gnus-summary-position-point)))
6044
6045 (defun gnus-summary-limit-exclude-childless-dormant ()
6046   "Hide all dormant articles that have no children."
6047   (interactive)
6048   (let ((data (gnus-data-list t))
6049         articles d children)
6050     ;; Find all articles that are either not dormant or have
6051     ;; children.
6052     (while (setq d (pop data))
6053       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6054                 (and (setq children
6055                            (gnus-article-children (gnus-data-number d)))
6056                      (let (found)
6057                        (while children
6058                          (when (memq (car children) articles)
6059                            (setq children nil
6060                                  found t))
6061                          (pop children))
6062                        found)))
6063         (push (gnus-data-number d) articles)))
6064     ;; Do the limiting.
6065     (prog1
6066         (gnus-summary-limit articles)
6067       (gnus-summary-position-point))))
6068
6069 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6070   "Mark all unread excluded articles as read.
6071 If ALL, mark even excluded ticked and dormants as read."
6072   (interactive "P")
6073   (let ((articles (gnus-sorted-complement
6074                    (sort
6075                     (mapcar (lambda (h) (mail-header-number h))
6076                             gnus-newsgroup-headers)
6077                     '<)
6078                    (sort gnus-newsgroup-limit '<)))
6079         article)
6080     (setq gnus-newsgroup-unreads
6081           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6082     (if all
6083         (setq gnus-newsgroup-dormant nil
6084               gnus-newsgroup-marked nil
6085               gnus-newsgroup-reads
6086               (nconc
6087                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6088                gnus-newsgroup-reads))
6089       (while (setq article (pop articles))
6090         (unless (or (memq article gnus-newsgroup-dormant)
6091                     (memq article gnus-newsgroup-marked))
6092           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6093
6094 (defun gnus-summary-limit (articles &optional pop)
6095   (if pop
6096       ;; We pop the previous limit off the stack and use that.
6097       (setq articles (car gnus-newsgroup-limits)
6098             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6099     ;; We use the new limit, so we push the old limit on the stack.
6100     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6101   ;; Set the limit.
6102   (setq gnus-newsgroup-limit articles)
6103   (let ((total (length gnus-newsgroup-data))
6104         (data (gnus-data-find-list (gnus-summary-article-number)))
6105         (gnus-summary-mark-below nil)   ; Inhibit this.
6106         found)
6107     ;; This will do all the work of generating the new summary buffer
6108     ;; according to the new limit.
6109     (gnus-summary-prepare)
6110     ;; Hide any threads, possibly.
6111     (and gnus-show-threads
6112          gnus-thread-hide-subtree
6113          (gnus-summary-hide-all-threads))
6114     ;; Try to return to the article you were at, or one in the
6115     ;; neighborhood.
6116     (when data
6117       ;; We try to find some article after the current one.
6118       (while data
6119         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6120           (setq data nil
6121                 found t))
6122         (setq data (cdr data))))
6123     (unless found
6124       ;; If there is no data, that means that we were after the last
6125       ;; article.  The same goes when we can't find any articles
6126       ;; after the current one.
6127       (goto-char (point-max))
6128       (gnus-summary-find-prev))
6129     (gnus-set-mode-line 'summary)
6130     ;; We return how many articles were removed from the summary
6131     ;; buffer as a result of the new limit.
6132     (- total (length gnus-newsgroup-data))))
6133
6134 (defsubst gnus-invisible-cut-children (threads)
6135   (let ((num 0))
6136     (while threads
6137       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6138         (incf num))
6139       (pop threads))
6140     (< num 2)))
6141
6142 (defsubst gnus-cut-thread (thread)
6143   "Go forwards in the thread until we find an article that we want to display."
6144   (when (or (eq gnus-fetch-old-headers 'some)
6145             (eq gnus-fetch-old-headers 'invisible)          
6146             (eq gnus-build-sparse-threads 'some)
6147             (eq gnus-build-sparse-threads 'more))
6148     ;; Deal with old-fetched headers and sparse threads.
6149     (while (and
6150             thread
6151             (or
6152              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6153              (gnus-summary-article-ancient-p
6154               (mail-header-number (car thread))))
6155             (if (or (<= (length (cdr thread)) 1)
6156                     (eq gnus-fetch-old-headers 'invisible))
6157                 (setq gnus-newsgroup-limit
6158                       (delq (mail-header-number (car thread))
6159                             gnus-newsgroup-limit)
6160                       thread (cadr thread))
6161               (when (gnus-invisible-cut-children (cdr thread))
6162                 (let ((th (cdr thread)))
6163                   (while th
6164                     (if (memq (mail-header-number (caar th))
6165                               gnus-newsgroup-limit)
6166                         (setq thread (car th)
6167                               th nil)
6168                       (setq th (cdr th))))))))))
6169   thread)
6170
6171 (defun gnus-cut-threads (threads)
6172   "Cut off all uninteresting articles from the beginning of threads."
6173   (when (or (eq gnus-fetch-old-headers 'some)
6174             (eq gnus-fetch-old-headers 'invisible)
6175             (eq gnus-build-sparse-threads 'some)
6176             (eq gnus-build-sparse-threads 'more))
6177     (let ((th threads))
6178       (while th
6179         (setcar th (gnus-cut-thread (car th)))
6180         (setq th (cdr th)))))
6181   ;; Remove nixed out threads.
6182   (delq nil threads))
6183
6184 (defun gnus-summary-initial-limit (&optional show-if-empty)
6185   "Figure out what the initial limit is supposed to be on group entry.
6186 This entails weeding out unwanted dormants, low-scored articles,
6187 fetch-old-headers verbiage, and so on."
6188   ;; Most groups have nothing to remove.
6189   (if (or gnus-inhibit-limiting
6190           (and (null gnus-newsgroup-dormant)
6191                (not (eq gnus-fetch-old-headers 'some))
6192                (not (eq gnus-fetch-old-headers 'invisible))
6193                (null gnus-summary-expunge-below)
6194                (not (eq gnus-build-sparse-threads 'some))
6195                (not (eq gnus-build-sparse-threads 'more))
6196                (null gnus-thread-expunge-below)
6197                (not gnus-use-nocem)))
6198       ()                                ; Do nothing.
6199     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6200     (setq gnus-newsgroup-limit nil)
6201     (mapatoms
6202      (lambda (node)
6203        (unless (car (symbol-value node))
6204          ;; These threads have no parents -- they are roots.
6205          (let ((nodes (cdr (symbol-value node)))
6206                thread)
6207            (while nodes
6208              (if (and gnus-thread-expunge-below
6209                       (< (gnus-thread-total-score (car nodes))
6210                          gnus-thread-expunge-below))
6211                  (gnus-expunge-thread (pop nodes))
6212                (setq thread (pop nodes))
6213                (gnus-summary-limit-children thread))))))
6214      gnus-newsgroup-dependencies)
6215     ;; If this limitation resulted in an empty group, we might
6216     ;; pop the previous limit and use it instead.
6217     (when (and (not gnus-newsgroup-limit)
6218                show-if-empty)
6219       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6220     gnus-newsgroup-limit))
6221
6222 (defun gnus-summary-limit-children (thread)
6223   "Return 1 if this subthread is visible and 0 if it is not."
6224   ;; First we get the number of visible children to this thread.  This
6225   ;; is done by recursing down the thread using this function, so this
6226   ;; will really go down to a leaf article first, before slowly
6227   ;; working its way up towards the root.
6228   (when thread
6229     (let ((children
6230            (if (cdr thread)
6231                (apply '+ (mapcar 'gnus-summary-limit-children
6232                                  (cdr thread)))
6233              0))
6234           (number (mail-header-number (car thread)))
6235           score)
6236       (if (and
6237            (not (memq number gnus-newsgroup-marked))
6238            (or
6239             ;; If this article is dormant and has absolutely no visible
6240             ;; children, then this article isn't visible.
6241             (and (memq number gnus-newsgroup-dormant)
6242                  (zerop children))
6243             ;; If this is "fetch-old-headered" and there is no
6244             ;; visible children, then we don't want this article.
6245             (and (eq gnus-fetch-old-headers 'some)
6246                  (gnus-summary-article-ancient-p number)
6247                  (zerop children))
6248             ;; If this is "fetch-old-headered" and `invisible', then
6249             ;; we don't want this article.
6250             (and (eq gnus-fetch-old-headers 'invisible)
6251                  (gnus-summary-article-ancient-p number))
6252             ;; If this is a sparsely inserted article with no children,
6253             ;; we don't want it.
6254             (and (eq gnus-build-sparse-threads 'some)
6255                  (gnus-summary-article-sparse-p number)
6256                  (zerop children))
6257             ;; If we use expunging, and this article is really
6258             ;; low-scored, then we don't want this article.
6259             (when (and gnus-summary-expunge-below
6260                        (< (setq score
6261                                 (or (cdr (assq number gnus-newsgroup-scored))
6262                                     gnus-summary-default-score))
6263                           gnus-summary-expunge-below))
6264               ;; We increase the expunge-tally here, but that has
6265               ;; nothing to do with the limits, really.
6266               (incf gnus-newsgroup-expunged-tally)
6267               ;; We also mark as read here, if that's wanted.
6268               (when (and gnus-summary-mark-below
6269                          (< score gnus-summary-mark-below))
6270                 (setq gnus-newsgroup-unreads
6271                       (delq number gnus-newsgroup-unreads))
6272                 (if gnus-newsgroup-auto-expire
6273                     (push number gnus-newsgroup-expirable)
6274                   (push (cons number gnus-low-score-mark)
6275                         gnus-newsgroup-reads)))
6276               t)
6277             ;; Check NoCeM things.
6278             (if (and gnus-use-nocem
6279                      (gnus-nocem-unwanted-article-p
6280                       (mail-header-id (car thread))))
6281                 (progn
6282                   (setq gnus-newsgroup-unreads
6283                         (delq number gnus-newsgroup-unreads))
6284                   t))))
6285           ;; Nope, invisible article.
6286           0
6287         ;; Ok, this article is to be visible, so we add it to the limit
6288         ;; and return 1.
6289         (push number gnus-newsgroup-limit)
6290         1))))
6291
6292 (defun gnus-expunge-thread (thread)
6293   "Mark all articles in THREAD as read."
6294   (let* ((number (mail-header-number (car thread))))
6295     (incf gnus-newsgroup-expunged-tally)
6296     ;; We also mark as read here, if that's wanted.
6297     (setq gnus-newsgroup-unreads
6298           (delq number gnus-newsgroup-unreads))
6299     (if gnus-newsgroup-auto-expire
6300         (push number gnus-newsgroup-expirable)
6301       (push (cons number gnus-low-score-mark)
6302             gnus-newsgroup-reads)))
6303   ;; Go recursively through all subthreads.
6304   (mapcar 'gnus-expunge-thread (cdr thread)))
6305
6306 ;; Summary article oriented commands
6307
6308 (defun gnus-summary-refer-parent-article (n)
6309   "Refer parent article N times.
6310 If N is negative, go to ancestor -N instead.
6311 The difference between N and the number of articles fetched is returned."
6312   (interactive "p")
6313   (let ((skip 1)
6314         error header ref)
6315     (when (not (natnump n))
6316       (setq skip (abs n)
6317             n 1))
6318     (while (and (> n 0)
6319                 (not error))
6320       (setq header (gnus-summary-article-header))
6321       (if (and (eq (mail-header-number header)
6322                    (cdr gnus-article-current))
6323                (equal gnus-newsgroup-name
6324                       (car gnus-article-current)))
6325           ;; If we try to find the parent of the currently
6326           ;; displayed article, then we take a look at the actual
6327           ;; References header, since this is slightly more
6328           ;; reliable than the References field we got from the
6329           ;; server.
6330           (save-excursion
6331             (set-buffer gnus-original-article-buffer)
6332             (nnheader-narrow-to-headers)
6333             (unless (setq ref (message-fetch-field "references"))
6334               (setq ref (message-fetch-field "in-reply-to")))
6335             (widen))
6336         (setq ref
6337               ;; It's not the current article, so we take a bet on
6338               ;; the value we got from the server.
6339               (mail-header-references header)))
6340       (if (and ref
6341                (not (equal ref "")))
6342           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6343             (gnus-message 1 "Couldn't find parent"))
6344         (gnus-message 1 "No references in article %d"
6345                       (gnus-summary-article-number))
6346         (setq error t))
6347       (decf n))
6348     (gnus-summary-position-point)
6349     n))
6350
6351 (defun gnus-summary-refer-references ()
6352   "Fetch all articles mentioned in the References header.
6353 Return the number of articles fetched."
6354   (interactive)
6355   (let ((ref (mail-header-references (gnus-summary-article-header)))
6356         (current (gnus-summary-article-number))
6357         (n 0))
6358     (if (or (not ref)
6359             (equal ref ""))
6360         (error "No References in the current article")
6361       ;; For each Message-ID in the References header...
6362       (while (string-match "<[^>]*>" ref)
6363         (incf n)
6364         ;; ... fetch that article.
6365         (gnus-summary-refer-article
6366          (prog1 (match-string 0 ref)
6367            (setq ref (substring ref (match-end 0))))))
6368       (gnus-summary-goto-subject current)
6369       (gnus-summary-position-point)
6370       n)))
6371
6372 (defun gnus-summary-refer-thread (&optional limit)
6373   "Fetch all articles in the current thread.
6374 If LIMIT (the numerical prefix), fetch that many old headers instead
6375 of what's specified by the `gnus-refer-thread-limit' variable."
6376   (interactive "P")
6377   (let ((id (mail-header-id (gnus-summary-article-header)))
6378         (limit (if limit (prefix-numeric-value limit)
6379                  gnus-refer-thread-limit)))
6380     ;; We want to fetch LIMIT *old* headers, but we also have to
6381     ;; re-fetch all the headers in the current buffer, because many of
6382     ;; them may be undisplayed.  So we adjust LIMIT.
6383     (when (numberp limit)
6384       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6385     (unless (eq gnus-fetch-old-headers 'invisible)
6386       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6387       ;; Retrieve the headers and read them in.
6388       (if (eq (gnus-retrieve-headers
6389                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6390               'nov)
6391           (gnus-build-all-threads)
6392         (error "Can't fetch thread from backends that don't support NOV"))
6393       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6394     (gnus-summary-limit-include-thread id)))
6395
6396 (defun gnus-summary-refer-article (message-id &optional arg)
6397   "Fetch an article specified by MESSAGE-ID.
6398 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6399 or `gnus-select-method', no matter what backend the article comes from."
6400   (interactive "sMessage-ID: \nP")
6401   (when (and (stringp message-id)
6402              (not (zerop (length message-id))))
6403     ;; Construct the correct Message-ID if necessary.
6404     ;; Suggested by tale@pawl.rpi.edu.
6405     (unless (string-match "^<" message-id)
6406       (setq message-id (concat "<" message-id)))
6407     (unless (string-match ">$" message-id)
6408       (setq message-id (concat message-id ">")))
6409     (let* ((header (gnus-id-to-header message-id))
6410            (sparse (and header
6411                         (gnus-summary-article-sparse-p
6412                          (mail-header-number header))
6413                         (memq (mail-header-number header)
6414                               gnus-newsgroup-limit))))
6415       (cond
6416        ;; If the article is present in the buffer we just go to it.
6417        ((and header
6418              (or (not (gnus-summary-article-sparse-p
6419                        (mail-header-number header)))
6420                  sparse))
6421         (prog1
6422             (gnus-summary-goto-article
6423              (mail-header-number header) nil t)
6424           (when sparse
6425             (gnus-summary-update-article (mail-header-number header)))))
6426        (t
6427         ;; We fetch the article
6428         (let ((gnus-override-method
6429                (cond ((gnus-news-group-p gnus-newsgroup-name)
6430                       gnus-refer-article-method)
6431                      (arg
6432                       (or gnus-refer-article-method gnus-select-method))
6433                      (t nil)))
6434               number)
6435           ;; Start the special refer-article method, if necessary.
6436           (when (and gnus-refer-article-method
6437                      (gnus-news-group-p gnus-newsgroup-name))
6438             (gnus-check-server gnus-refer-article-method))
6439           ;; Fetch the header, and display the article.
6440           (if (setq number (gnus-summary-insert-subject message-id))
6441               (gnus-summary-select-article nil nil nil number)
6442             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6443
6444 (defun gnus-summary-edit-parameters ()
6445   "Edit the group parameters of the current group."
6446   (interactive)
6447   (gnus-group-edit-group gnus-newsgroup-name 'params))
6448
6449 (defun gnus-summary-enter-digest-group (&optional force)
6450   "Enter an nndoc group based on the current article.
6451 If FORCE, force a digest interpretation.  If not, try
6452 to guess what the document format is."
6453   (interactive "P")
6454   (let ((conf gnus-current-window-configuration))
6455     (save-excursion
6456       (gnus-summary-select-article))
6457     (setq gnus-current-window-configuration conf)
6458     (let* ((name (format "%s-%d"
6459                          (gnus-group-prefixed-name
6460                           gnus-newsgroup-name (list 'nndoc ""))
6461                          (save-excursion
6462                            (set-buffer gnus-summary-buffer)
6463                            gnus-current-article)))
6464            (ogroup gnus-newsgroup-name)
6465            (params (append (gnus-info-params (gnus-get-info ogroup))
6466                            (list (cons 'to-group ogroup))
6467                            (list (cons 'save-article-group ogroup))))
6468            (case-fold-search t)
6469            (buf (current-buffer))
6470            dig)
6471       (save-excursion
6472         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6473         (insert-buffer-substring gnus-original-article-buffer)
6474         ;; Remove lines that may lead nndoc to misinterpret the
6475         ;; document type.
6476         (narrow-to-region
6477          (goto-char (point-min))
6478          (or (search-forward "\n\n" nil t) (point)))
6479         (goto-char (point-min))
6480         (delete-matching-lines "^\\(Path\\):\\|^From ")
6481         (widen))
6482       (unwind-protect
6483           (if (gnus-group-read-ephemeral-group
6484                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6485                             (nndoc-article-type
6486                              ,(if force 'digest 'guess))) t)
6487               ;; Make all postings to this group go to the parent group.
6488               (nconc (gnus-info-params (gnus-get-info name))
6489                      params)
6490             ;; Couldn't select this doc group.
6491             (switch-to-buffer buf)
6492             (gnus-set-global-variables)
6493             (gnus-configure-windows 'summary)
6494             (gnus-message 3 "Article couldn't be entered?"))
6495         (kill-buffer dig)))))
6496
6497 (defun gnus-summary-read-document (n)
6498   "Open a new group based on the current article(s).
6499 This will allow you to read digests and other similar
6500 documents as newsgroups.
6501 Obeys the standard process/prefix convention."
6502   (interactive "P")
6503   (let* ((articles (gnus-summary-work-articles n))
6504          (ogroup gnus-newsgroup-name)
6505          (params (append (gnus-info-params (gnus-get-info ogroup))
6506                          (list (cons 'to-group ogroup))))
6507          article group egroup groups vgroup)
6508     (while (setq article (pop articles))
6509       (setq group (format "%s-%d" gnus-newsgroup-name article))
6510       (gnus-summary-remove-process-mark article)
6511       (when (gnus-summary-display-article article)
6512         (save-excursion
6513           (with-temp-buffer
6514             (insert-buffer-substring gnus-original-article-buffer)
6515             ;; Remove some headers that may lead nndoc to make
6516             ;; the wrong guess.
6517             (message-narrow-to-head)
6518             (goto-char (point-min))
6519             (delete-matching-lines "^\\(Path\\):\\|^From ")
6520             (widen)
6521             (if (setq egroup
6522                       (gnus-group-read-ephemeral-group
6523                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6524                                      (nndoc-article-type guess))
6525                        t nil t))
6526                 (progn
6527                   ;; Make all postings to this group go to the parent group.
6528                   (nconc (gnus-info-params (gnus-get-info egroup))
6529                          params)
6530                   (push egroup groups))
6531               ;; Couldn't select this doc group.
6532               (gnus-error 3 "Article couldn't be entered"))))))
6533     ;; Now we have selected all the documents.
6534     (cond
6535      ((not groups)
6536       (error "None of the articles could be interpreted as documents"))
6537      ((gnus-group-read-ephemeral-group
6538        (setq vgroup (format
6539                      "nnvirtual:%s-%s" gnus-newsgroup-name
6540                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6541        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6542        t
6543        (cons (current-buffer) 'summary)))
6544      (t
6545       (error "Couldn't select virtual nndoc group")))))
6546
6547 (defun gnus-summary-isearch-article (&optional regexp-p)
6548   "Do incremental search forward on the current article.
6549 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6550   (interactive "P")
6551   (gnus-summary-select-article)
6552   (gnus-configure-windows 'article)
6553   (gnus-eval-in-buffer-window gnus-article-buffer
6554     (save-restriction
6555       (widen)
6556       (isearch-forward regexp-p))))
6557
6558 (defun gnus-summary-search-article-forward (regexp &optional backward)
6559   "Search for an article containing REGEXP forward.
6560 If BACKWARD, search backward instead."
6561   (interactive
6562    (list (read-string
6563           (format "Search article %s (regexp%s): "
6564                   (if current-prefix-arg "backward" "forward")
6565                   (if gnus-last-search-regexp
6566                       (concat ", default " gnus-last-search-regexp)
6567                     "")))
6568          current-prefix-arg))
6569   (if (string-equal regexp "")
6570       (setq regexp (or gnus-last-search-regexp ""))
6571     (setq gnus-last-search-regexp regexp))
6572   (if (gnus-summary-search-article regexp backward)
6573       (gnus-summary-show-thread)
6574     (error "Search failed: \"%s\"" regexp)))
6575
6576 (defun gnus-summary-search-article-backward (regexp)
6577   "Search for an article containing REGEXP backward."
6578   (interactive
6579    (list (read-string
6580           (format "Search article backward (regexp%s): "
6581                   (if gnus-last-search-regexp
6582                       (concat ", default " gnus-last-search-regexp)
6583                     "")))))
6584   (gnus-summary-search-article-forward regexp 'backward))
6585
6586 (defun gnus-summary-search-article (regexp &optional backward)
6587   "Search for an article containing REGEXP.
6588 Optional argument BACKWARD means do search for backward.
6589 `gnus-select-article-hook' is not called during the search."
6590   ;; We have to require this here to make sure that the following
6591   ;; dynamic binding isn't shadowed by autoloading.
6592   (require 'gnus-async)
6593   (let ((gnus-select-article-hook nil)  ;Disable hook.
6594         (gnus-article-display-hook nil)
6595         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6596         (gnus-use-article-prefetch nil)
6597         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6598         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6599         (sum (current-buffer))
6600         (found nil)
6601         point)
6602     (gnus-save-hidden-threads
6603       (gnus-summary-select-article)
6604       (set-buffer gnus-article-buffer)
6605       (when backward
6606         (forward-line -1))
6607       (while (not found)
6608         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6609         (if (if backward
6610                 (re-search-backward regexp nil t)
6611               (re-search-forward regexp nil t))
6612             ;; We found the regexp.
6613             (progn
6614               (setq found 'found)
6615               (beginning-of-line)
6616               (set-window-start
6617                (get-buffer-window (current-buffer))
6618                (point))
6619               (forward-line 1)
6620               (set-buffer sum)
6621               (setq point (point)))
6622           ;; We didn't find it, so we go to the next article.
6623           (set-buffer sum)
6624           (setq found 'not)
6625           (while (eq found 'not)
6626             (if (not (if backward (gnus-summary-find-prev)
6627                        (gnus-summary-find-next)))
6628                 ;; No more articles.
6629                 (setq found t)
6630               ;; Select the next article and adjust point.
6631               (unless (gnus-summary-article-sparse-p
6632                        (gnus-summary-article-number))
6633                 (setq found nil)
6634                 (gnus-summary-select-article)
6635                 (set-buffer gnus-article-buffer)
6636                 (widen)
6637                 (goto-char (if backward (point-max) (point-min))))))))
6638       (gnus-message 7 ""))
6639     ;; Return whether we found the regexp.
6640     (when (eq found 'found)
6641       (goto-char point)
6642       (gnus-summary-show-thread)
6643       (gnus-summary-goto-subject gnus-current-article)
6644       (gnus-summary-position-point)
6645       t)))
6646
6647 (defun gnus-summary-find-matching (header regexp &optional backward unread
6648                                           not-case-fold)
6649   "Return a list of all articles that match REGEXP on HEADER.
6650 The search stars on the current article and goes forwards unless
6651 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6652 If UNREAD is non-nil, only unread articles will
6653 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6654 in the comparisons."
6655   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6656                 (gnus-data-find-list
6657                  (gnus-summary-article-number) (gnus-data-list backward))))
6658         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6659         (case-fold-search (not not-case-fold))
6660         articles d)
6661     (unless (fboundp (intern (concat "mail-header-" header)))
6662       (error "%s is not a valid header" header))
6663     (while data
6664       (setq d (car data))
6665       (and (or (not unread)             ; We want all articles...
6666                (gnus-data-unread-p d))  ; Or just unreads.
6667            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6668            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6669            (push (gnus-data-number d) articles)) ; Success!
6670       (setq data (cdr data)))
6671     (nreverse articles)))
6672
6673 (defun gnus-summary-execute-command (header regexp command &optional backward)
6674   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6675 If HEADER is an empty string (or nil), the match is done on the entire
6676 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6677   (interactive
6678    (list (let ((completion-ignore-case t))
6679            (completing-read
6680             "Header name: "
6681             (mapcar (lambda (string) (list string))
6682                     '("Number" "Subject" "From" "Lines" "Date"
6683                       "Message-ID" "Xref" "References" "Body"))
6684             nil 'require-match))
6685          (read-string "Regexp: ")
6686          (read-key-sequence "Command: ")
6687          current-prefix-arg))
6688   (when (equal header "Body")
6689     (setq header ""))
6690   ;; Hidden thread subtrees must be searched as well.
6691   (gnus-summary-show-all-threads)
6692   ;; We don't want to change current point nor window configuration.
6693   (save-excursion
6694     (save-window-excursion
6695       (gnus-message 6 "Executing %s..." (key-description command))
6696       ;; We'd like to execute COMMAND interactively so as to give arguments.
6697       (gnus-execute header regexp
6698                     `(call-interactively ',(key-binding command))
6699                     backward)
6700       (gnus-message 6 "Executing %s...done" (key-description command)))))
6701
6702 (defun gnus-summary-beginning-of-article ()
6703   "Scroll the article back to the beginning."
6704   (interactive)
6705   (gnus-summary-select-article)
6706   (gnus-configure-windows 'article)
6707   (gnus-eval-in-buffer-window gnus-article-buffer
6708     (widen)
6709     (goto-char (point-min))
6710     (when gnus-page-broken
6711       (gnus-narrow-to-page))))
6712
6713 (defun gnus-summary-end-of-article ()
6714   "Scroll to the end of the article."
6715   (interactive)
6716   (gnus-summary-select-article)
6717   (gnus-configure-windows 'article)
6718   (gnus-eval-in-buffer-window gnus-article-buffer
6719     (widen)
6720     (goto-char (point-max))
6721     (recenter -3)
6722     (when gnus-page-broken
6723       (gnus-narrow-to-page))))
6724
6725 (defun gnus-summary-print-article (&optional filename n)
6726   "Generate and print a PostScript image of the N next (mail) articles.
6727
6728 If N is negative, print the N previous articles.  If N is nil and articles
6729 have been marked with the process mark, print these instead.
6730
6731 If the optional second argument FILENAME is nil, send the image to the
6732 printer.  If FILENAME is a string, save the PostScript image in a file with
6733 that name.  If FILENAME is a number, prompt the user for the name of the file
6734 to save in."
6735   (interactive (list (ps-print-preprint current-prefix-arg)
6736                      current-prefix-arg))
6737   (dolist (article (gnus-summary-work-articles n))
6738     (gnus-summary-select-article nil nil 'pseudo article)
6739     (gnus-eval-in-buffer-window gnus-article-buffer
6740       (let ((buffer (generate-new-buffer " *print*")))
6741         (unwind-protect
6742             (progn
6743               (copy-to-buffer buffer (point-min) (point-max))
6744               (set-buffer buffer)
6745               (gnus-article-delete-invisible-text)
6746               (let ((ps-left-header
6747                      (list 
6748                       (concat "("
6749                               (mail-header-subject gnus-current-headers) ")")
6750                       (concat "("
6751                               (mail-header-from gnus-current-headers) ")")))
6752                     (ps-right-header 
6753                      (list 
6754                       "/pagenumberstring load" 
6755                       (concat "("
6756                               (mail-header-date gnus-current-headers) ")"))))
6757                 (gnus-run-hooks 'gnus-ps-print-hook)
6758                 (save-excursion
6759                   (ps-print-buffer-with-faces filename))))
6760           (kill-buffer buffer))))))
6761
6762 (defun gnus-summary-show-article (&optional arg)
6763   "Force re-fetching of the current article.
6764 If ARG (the prefix) is non-nil, show the raw article without any
6765 article massaging functions being run."
6766   (interactive "P")
6767   (if (not arg)
6768       ;; Select the article the normal way.
6769       (gnus-summary-select-article nil 'force)
6770     ;; Bind the article treatment functions to nil.
6771     (let ((gnus-have-all-headers t)
6772           gnus-article-display-hook
6773           gnus-article-prepare-hook
6774           gnus-break-pages
6775           gnus-show-mime
6776           gnus-visual)
6777       (gnus-summary-select-article nil 'force)))
6778   (gnus-summary-goto-subject gnus-current-article)
6779   (gnus-summary-position-point))
6780
6781 (defun gnus-summary-verbose-headers (&optional arg)
6782   "Toggle permanent full header display.
6783 If ARG is a positive number, turn header display on.
6784 If ARG is a negative number, turn header display off."
6785   (interactive "P")
6786   (setq gnus-show-all-headers
6787         (cond ((or (not (numberp arg))
6788                    (zerop arg))
6789                (not gnus-show-all-headers))
6790               ((natnump arg)
6791                t)))
6792   (gnus-summary-show-article))
6793
6794 (defun gnus-summary-toggle-header (&optional arg)
6795   "Show the headers if they are hidden, or hide them if they are shown.
6796 If ARG is a positive number, show the entire header.
6797 If ARG is a negative number, hide the unwanted header lines."
6798   (interactive "P")
6799   (save-excursion
6800     (set-buffer gnus-article-buffer)
6801     (let* ((buffer-read-only nil)
6802            (inhibit-point-motion-hooks t)
6803            (hidden (text-property-any
6804                     (goto-char (point-min)) (search-forward "\n\n")
6805                     'invisible t))
6806            e)
6807       (goto-char (point-min))
6808       (when (search-forward "\n\n" nil t)
6809         (delete-region (point-min) (1- (point))))
6810       (goto-char (point-min))
6811       (save-excursion
6812         (set-buffer gnus-original-article-buffer)
6813         (goto-char (point-min))
6814         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6815       (insert-buffer-substring gnus-original-article-buffer 1 e)
6816       (let ((article-inhibit-hiding t))
6817         (gnus-run-hooks 'gnus-article-display-hook))
6818       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6819         (gnus-article-hide-headers)))))
6820
6821 (defun gnus-summary-show-all-headers ()
6822   "Make all header lines visible."
6823   (interactive)
6824   (gnus-article-show-all-headers))
6825
6826 (defun gnus-summary-toggle-mime (&optional arg)
6827   "Toggle MIME processing.
6828 If ARG is a positive number, turn MIME processing on."
6829   (interactive "P")
6830   (setq gnus-show-mime
6831         (if (null arg) (not gnus-show-mime)
6832           (> (prefix-numeric-value arg) 0)))
6833   (gnus-summary-select-article t 'force))
6834
6835 (defun gnus-summary-caesar-message (&optional arg)
6836   "Caesar rotate the current article by 13.
6837 The numerical prefix specifies how many places to rotate each letter
6838 forward."
6839   (interactive "P")
6840   (gnus-summary-select-article)
6841   (let ((mail-header-separator ""))
6842     (gnus-eval-in-buffer-window gnus-article-buffer
6843       (save-restriction
6844         (widen)
6845         (let ((start (window-start))
6846               buffer-read-only)
6847           (message-caesar-buffer-body arg)
6848           (set-window-start (get-buffer-window (current-buffer)) start))))))
6849
6850 (defun gnus-summary-stop-page-breaking ()
6851   "Stop page breaking in the current article."
6852   (interactive)
6853   (gnus-summary-select-article)
6854   (gnus-eval-in-buffer-window gnus-article-buffer
6855     (widen)
6856     (when (gnus-visual-p 'page-marker)
6857       (let ((buffer-read-only nil))
6858         (gnus-remove-text-with-property 'gnus-prev)
6859         (gnus-remove-text-with-property 'gnus-next))
6860       (setq gnus-page-broken nil))))
6861
6862 (defun gnus-summary-move-article (&optional n to-newsgroup
6863                                             select-method action)
6864   "Move the current article to a different newsgroup.
6865 If N is a positive number, move the N next articles.
6866 If N is a negative number, move the N previous articles.
6867 If N is nil and any articles have been marked with the process mark,
6868 move those articles instead.
6869 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6870 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6871 re-spool using this method.
6872
6873 For this function to work, both the current newsgroup and the
6874 newsgroup that you want to move to have to support the `request-move'
6875 and `request-accept' functions."
6876   (interactive "P")
6877   (unless action
6878     (setq action 'move))
6879   ;; Disable marking as read.
6880   (let (gnus-mark-article-hook)
6881     (save-window-excursion
6882       (gnus-summary-select-article)))
6883   ;; Check whether the source group supports the required functions.
6884   (cond ((and (eq action 'move)
6885               (not (gnus-check-backend-function
6886                     'request-move-article gnus-newsgroup-name)))
6887          (error "The current group does not support article moving"))
6888         ((and (eq action 'crosspost)
6889               (not (gnus-check-backend-function
6890                     'request-replace-article gnus-newsgroup-name)))
6891          (error "The current group does not support article editing")))
6892   (let ((articles (gnus-summary-work-articles n))
6893         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6894         (names '((move "Move" "Moving")
6895                  (copy "Copy" "Copying")
6896                  (crosspost "Crosspost" "Crossposting")))
6897         (copy-buf (save-excursion
6898                     (nnheader-set-temp-buffer " *copy article*")))
6899         art-group to-method new-xref article to-groups)
6900     (unless (assq action names)
6901       (error "Unknown action %s" action))
6902     ;; Read the newsgroup name.
6903     (when (and (not to-newsgroup)
6904                (not select-method))
6905       (setq to-newsgroup
6906             (gnus-read-move-group-name
6907              (cadr (assq action names))
6908              (symbol-value (intern (format "gnus-current-%s-group" action)))
6909              articles prefix))
6910       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6911     (setq to-method (or select-method
6912                         (gnus-group-name-to-method to-newsgroup)))
6913     ;; Check the method we are to move this article to...
6914     (unless (gnus-check-backend-function
6915              'request-accept-article (car to-method))
6916       (error "%s does not support article copying" (car to-method)))
6917     (unless (gnus-check-server to-method)
6918       (error "Can't open server %s" (car to-method)))
6919     (gnus-message 6 "%s to %s: %s..."
6920                   (caddr (assq action names))
6921                   (or (car select-method) to-newsgroup) articles)
6922     (while articles
6923       (setq article (pop articles))
6924       (setq
6925        art-group
6926        (cond
6927         ;; Move the article.
6928         ((eq action 'move)
6929          ;; Remove this article from future suppression.
6930          (gnus-dup-unsuppress-article article)
6931          (gnus-request-move-article
6932           article                       ; Article to move
6933           gnus-newsgroup-name           ; From newsgroup
6934           (nth 1 (gnus-find-method-for-group
6935                   gnus-newsgroup-name)) ; Server
6936           (list 'gnus-request-accept-article
6937                 to-newsgroup (list 'quote select-method)
6938                 (not articles))         ; Accept form
6939           (not articles)))              ; Only save nov last time
6940         ;; Copy the article.
6941         ((eq action 'copy)
6942          (save-excursion
6943            (set-buffer copy-buf)
6944            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
6945              (gnus-request-accept-article
6946               to-newsgroup select-method (not articles)))))
6947         ;; Crosspost the article.
6948         ((eq action 'crosspost)
6949          (let ((xref (message-tokenize-header
6950                       (mail-header-xref (gnus-summary-article-header article))
6951                       " ")))
6952            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
6953                                   ":" article))
6954            (unless xref
6955              (setq xref (list (system-name))))
6956            (setq new-xref
6957                  (concat
6958                   (mapconcat 'identity
6959                              (delete "Xref:" (delete new-xref xref))
6960                              " ")
6961                   " " new-xref))
6962            (save-excursion
6963              (set-buffer copy-buf)
6964              ;; First put the article in the destination group.
6965              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6966              (when (consp (setq art-group
6967                                 (gnus-request-accept-article
6968                                  to-newsgroup select-method (not articles))))
6969                (setq new-xref (concat new-xref " " (car art-group)
6970                                       ":" (cdr art-group)))
6971                ;; Now we have the new Xrefs header, so we insert
6972                ;; it and replace the new article.
6973                (nnheader-replace-header "Xref" new-xref)
6974                (gnus-request-replace-article
6975                 (cdr art-group) to-newsgroup (current-buffer))
6976                art-group))))))
6977       (cond
6978        ((not art-group)
6979         (gnus-message 1 "Couldn't %s article %s"
6980                       (cadr (assq action names)) article))
6981        ((and (eq art-group 'junk)
6982              (eq action 'move))
6983         (gnus-summary-mark-article article gnus-canceled-mark)
6984         (gnus-message 4 "Deleted article %s" article))
6985        (t
6986         (let* ((pto-group (gnus-group-prefixed-name
6987                            (car art-group) to-method))
6988                (entry
6989                 (gnus-gethash pto-group gnus-newsrc-hashtb))
6990                (info (nth 2 entry))
6991                (to-group (gnus-info-group info)))
6992           ;; Update the group that has been moved to.
6993           (when (and info
6994                      (memq action '(move copy)))
6995             (unless (member to-group to-groups)
6996               (push to-group to-groups))
6997
6998             (unless (memq article gnus-newsgroup-unreads)
6999               (gnus-info-set-read
7000                info (gnus-add-to-range (gnus-info-read info)
7001                                        (list (cdr art-group)))))
7002
7003             ;; Copy any marks over to the new group.
7004             (let ((marks gnus-article-mark-lists)
7005                   (to-article (cdr art-group)))
7006
7007               ;; See whether the article is to be put in the cache.
7008               (when gnus-use-cache
7009                 (gnus-cache-possibly-enter-article
7010                  to-group to-article
7011                  (let ((header (copy-sequence
7012                                 (gnus-summary-article-header article))))
7013                    (mail-header-set-number header to-article)
7014                    header)
7015                  (memq article gnus-newsgroup-marked)
7016                  (memq article gnus-newsgroup-dormant)
7017                  (memq article gnus-newsgroup-unreads)))
7018
7019               (when (and (equal to-group gnus-newsgroup-name)
7020                          (not (memq article gnus-newsgroup-unreads)))
7021                 ;; Mark this article as read in this group.
7022                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7023                 (setcdr (gnus-active to-group) to-article)
7024                 (setcdr gnus-newsgroup-active to-article))
7025
7026               (while marks
7027                 (when (memq article (symbol-value
7028                                      (intern (format "gnus-newsgroup-%s"
7029                                                      (caar marks)))))
7030                   ;; If the other group is the same as this group,
7031                   ;; then we have to add the mark to the list.
7032                   (when (equal to-group gnus-newsgroup-name)
7033                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7034                          (cons to-article
7035                                (symbol-value
7036                                 (intern (format "gnus-newsgroup-%s"
7037                                                 (caar marks)))))))
7038                   ;; Copy the marks to other group.
7039                   (gnus-add-marked-articles
7040                    to-group (cdar marks) (list to-article) info))
7041                 (setq marks (cdr marks)))
7042
7043               (gnus-dribble-enter
7044                (concat "(gnus-group-set-info '"
7045                        (gnus-prin1-to-string (gnus-get-info to-group))
7046                        ")"))))
7047
7048           ;; Update the Xref header in this article to point to
7049           ;; the new crossposted article we have just created.
7050           (when (eq action 'crosspost)
7051             (save-excursion
7052               (set-buffer copy-buf)
7053               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7054               (nnheader-replace-header "Xref" new-xref)
7055               (gnus-request-replace-article
7056                article gnus-newsgroup-name (current-buffer)))))
7057
7058         ;;;!!!Why is this necessary?
7059         (set-buffer gnus-summary-buffer)
7060         
7061         (gnus-summary-goto-subject article)
7062         (when (eq action 'move)
7063           (gnus-summary-mark-article article gnus-canceled-mark))))
7064       (gnus-summary-remove-process-mark article))
7065     ;; Re-activate all groups that have been moved to.
7066     (while to-groups
7067       (save-excursion
7068         (set-buffer gnus-group-buffer)
7069         (when (gnus-group-goto-group (car to-groups) t)
7070           (gnus-group-get-new-news-this-group 1 t))
7071         (pop to-groups)))
7072
7073     (gnus-kill-buffer copy-buf)
7074     (gnus-summary-position-point)
7075     (gnus-set-mode-line 'summary)))
7076
7077 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7078   "Move the current article to a different newsgroup.
7079 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7080 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7081 re-spool using this method."
7082   (interactive "P")
7083   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7084
7085 (defun gnus-summary-crosspost-article (&optional n)
7086   "Crosspost the current article to some other group."
7087   (interactive "P")
7088   (gnus-summary-move-article n nil nil 'crosspost))
7089
7090 (defcustom gnus-summary-respool-default-method nil
7091   "Default method for respooling an article.
7092 If nil, use to the current newsgroup method."
7093   :type `(choice (gnus-select-method :value (nnml ""))
7094                  (const nil))
7095   :group 'gnus-summary-mail)
7096
7097 (defun gnus-summary-respool-article (&optional n method)
7098   "Respool the current article.
7099 The article will be squeezed through the mail spooling process again,
7100 which means that it will be put in some mail newsgroup or other
7101 depending on `nnmail-split-methods'.
7102 If N is a positive number, respool the N next articles.
7103 If N is a negative number, respool the N previous articles.
7104 If N is nil and any articles have been marked with the process mark,
7105 respool those articles instead.
7106
7107 Respooling can be done both from mail groups and \"real\" newsgroups.
7108 In the former case, the articles in question will be moved from the
7109 current group into whatever groups they are destined to.  In the
7110 latter case, they will be copied into the relevant groups."
7111   (interactive
7112    (list current-prefix-arg
7113          (let* ((methods (gnus-methods-using 'respool))
7114                 (methname
7115                  (symbol-name (or gnus-summary-respool-default-method
7116                                   (car (gnus-find-method-for-group
7117                                         gnus-newsgroup-name)))))
7118                 (method
7119                  (gnus-completing-read
7120                   methname "What backend do you want to use when respooling?"
7121                   methods nil t nil 'gnus-mail-method-history))
7122                 ms)
7123            (cond
7124             ((zerop (length (setq ms (gnus-servers-using-backend
7125                                       (intern method)))))
7126              (list (intern method) ""))
7127             ((= 1 (length ms))
7128              (car ms))
7129             (t
7130              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7131                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7132                            ms-alist))))))))
7133   (unless method
7134     (error "No method given for respooling"))
7135   (if (assoc (symbol-name
7136               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7137              (gnus-methods-using 'respool))
7138       (gnus-summary-move-article n nil method)
7139     (gnus-summary-copy-article n nil method)))
7140
7141 (defun gnus-summary-import-article (file)
7142   "Import an arbitrary file into a mail newsgroup."
7143   (interactive "fImport file: ")
7144   (let ((group gnus-newsgroup-name)
7145         (now (current-time))
7146         atts lines)
7147     (unless (gnus-check-backend-function 'request-accept-article group)
7148       (error "%s does not support article importing" group))
7149     (or (file-readable-p file)
7150         (not (file-regular-p file))
7151         (error "Can't read %s" file))
7152     (save-excursion
7153       (set-buffer (gnus-get-buffer-create " *import file*"))
7154       (buffer-disable-undo (current-buffer))
7155       (erase-buffer)
7156       (insert-file-contents file)
7157       (goto-char (point-min))
7158       (unless (nnheader-article-p)
7159         ;; This doesn't look like an article, so we fudge some headers.
7160         (setq atts (file-attributes file)
7161               lines (count-lines (point-min) (point-max)))
7162         (insert "From: " (read-string "From: ") "\n"
7163                 "Subject: " (read-string "Subject: ") "\n"
7164                 "Date: " (message-make-date (nth 5 atts))
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       ;; Let the backend know about the mark change.
7647       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7648       (push article gnus-newsgroup-expirable))
7649     ;; Set the mark in the buffer.
7650     (gnus-summary-update-mark mark 'unread)
7651     t))
7652
7653 (defun gnus-summary-mark-article-as-unread (mark)
7654   "Mark the current article quickly as unread with MARK."
7655   (let* ((article (gnus-summary-article-number))
7656          (old-mark (gnus-summary-article-mark article)))
7657     ;; Allow the backend to change the mark.
7658     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7659     (if (eq mark old-mark)
7660         t
7661       (if (<= article 0)
7662           (progn
7663             (gnus-error 1 "Can't mark negative article numbers")
7664             nil)
7665         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7666         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7667         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7668         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7669         (cond ((= mark gnus-ticked-mark)
7670                (push article gnus-newsgroup-marked))
7671               ((= mark gnus-dormant-mark)
7672                (push article gnus-newsgroup-dormant))
7673               (t
7674                (push article gnus-newsgroup-unreads)))
7675         (gnus-pull article gnus-newsgroup-reads)
7676
7677         ;; See whether the article is to be put in the cache.
7678         (and gnus-use-cache
7679              (vectorp (gnus-summary-article-header article))
7680              (save-excursion
7681                (gnus-cache-possibly-enter-article
7682                 gnus-newsgroup-name article
7683                 (gnus-summary-article-header article)
7684                 (= mark gnus-ticked-mark)
7685                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7686
7687         ;; Fix the mark.
7688         (gnus-summary-update-mark mark 'unread)
7689         t))))
7690
7691 (defun gnus-summary-mark-article (&optional article mark no-expire)
7692   "Mark ARTICLE with MARK.  MARK can be any character.
7693 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7694 `??' (dormant) and `?E' (expirable).
7695 If MARK is nil, then the default character `?D' is used.
7696 If ARTICLE is nil, then the article on the current line will be
7697 marked."
7698   ;; The mark might be a string.
7699   (when (stringp mark)
7700     (setq mark (aref mark 0)))
7701   ;; If no mark is given, then we check auto-expiring.
7702   (and (not no-expire)
7703        gnus-newsgroup-auto-expire
7704        (or (not mark)
7705            (and (gnus-characterp mark)
7706                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7707                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7708                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7709                     (= mark gnus-duplicate-mark))))
7710        (setq mark gnus-expirable-mark))
7711   (let* ((mark (or mark gnus-del-mark))
7712          (article (or article (gnus-summary-article-number)))
7713          (old-mark (gnus-summary-article-mark article)))
7714     ;; Allow the backend to change the mark.
7715     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7716     (if (eq mark old-mark)
7717         t
7718       (unless article
7719         (error "No article on current line"))
7720       (if (not (if (or (= mark gnus-unread-mark)
7721                        (= mark gnus-ticked-mark)
7722                        (= mark gnus-dormant-mark))
7723                    (gnus-mark-article-as-unread article mark)
7724                  (gnus-mark-article-as-read article mark)))
7725           t
7726         ;; See whether the article is to be put in the cache.
7727         (and gnus-use-cache
7728              (not (= mark gnus-canceled-mark))
7729              (vectorp (gnus-summary-article-header article))
7730              (save-excursion
7731                (gnus-cache-possibly-enter-article
7732                 gnus-newsgroup-name article
7733                 (gnus-summary-article-header article)
7734                 (= mark gnus-ticked-mark)
7735                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7736
7737         (when (gnus-summary-goto-subject article nil t)
7738           (let ((buffer-read-only nil))
7739             (gnus-summary-show-thread)
7740             ;; Fix the mark.
7741             (gnus-summary-update-mark mark 'unread)
7742             t))))))
7743
7744 (defun gnus-summary-update-secondary-mark (article)
7745   "Update the secondary (read, process, cache) mark."
7746   (gnus-summary-update-mark
7747    (cond ((memq article gnus-newsgroup-processable)
7748           gnus-process-mark)
7749          ((memq article gnus-newsgroup-cached)
7750           gnus-cached-mark)
7751          ((memq article gnus-newsgroup-replied)
7752           gnus-replied-mark)
7753          ((memq article gnus-newsgroup-saved)
7754           gnus-saved-mark)
7755          (t gnus-unread-mark))
7756    'replied)
7757   (when (gnus-visual-p 'summary-highlight 'highlight)
7758     (gnus-run-hooks 'gnus-summary-update-hook))
7759   t)
7760
7761 (defun gnus-summary-update-mark (mark type)
7762   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7763         (buffer-read-only nil))
7764     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7765     (when (looking-at "\r")
7766       (incf forward))
7767     (when (and forward
7768                (<= (+ forward (point)) (point-max)))
7769       ;; Go to the right position on the line.
7770       (goto-char (+ forward (point)))
7771       ;; Replace the old mark with the new mark.
7772       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7773       ;; Optionally update the marks by some user rule.
7774       (when (eq type 'unread)
7775         (gnus-data-set-mark
7776          (gnus-data-find (gnus-summary-article-number)) mark)
7777         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7778
7779 (defun gnus-mark-article-as-read (article &optional mark)
7780   "Enter ARTICLE in the pertinent lists and remove it from others."
7781   ;; Make the article expirable.
7782   (let ((mark (or mark gnus-del-mark)))
7783     (if (= mark gnus-expirable-mark)
7784         (push article gnus-newsgroup-expirable)
7785       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7786     ;; Remove from unread and marked lists.
7787     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7788     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7789     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7790     (push (cons article mark) gnus-newsgroup-reads)
7791     ;; Possibly remove from cache, if that is used.
7792     (when gnus-use-cache
7793       (gnus-cache-enter-remove-article article))
7794     t))
7795
7796 (defun gnus-mark-article-as-unread (article &optional mark)
7797   "Enter ARTICLE in the pertinent lists and remove it from others."
7798   (let ((mark (or mark gnus-ticked-mark)))
7799     (if (<= article 0)
7800         (progn
7801           (gnus-error 1 "Can't mark negative article numbers")
7802           nil)
7803       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7804             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7805             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7806             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7807
7808       ;; Unsuppress duplicates?
7809       (when gnus-suppress-duplicates
7810         (gnus-dup-unsuppress-article article))
7811
7812       (cond ((= mark gnus-ticked-mark)
7813              (push article gnus-newsgroup-marked))
7814             ((= mark gnus-dormant-mark)
7815              (push article gnus-newsgroup-dormant))
7816             (t
7817              (push article gnus-newsgroup-unreads)))
7818       (gnus-pull article gnus-newsgroup-reads)
7819       t)))
7820
7821 (defalias 'gnus-summary-mark-as-unread-forward
7822   'gnus-summary-tick-article-forward)
7823 (make-obsolete 'gnus-summary-mark-as-unread-forward
7824                'gnus-summary-tick-article-forward)
7825 (defun gnus-summary-tick-article-forward (n)
7826   "Tick N articles forwards.
7827 If N is negative, tick backwards instead.
7828 The difference between N and the number of articles ticked is returned."
7829   (interactive "p")
7830   (gnus-summary-mark-forward n gnus-ticked-mark))
7831
7832 (defalias 'gnus-summary-mark-as-unread-backward
7833   'gnus-summary-tick-article-backward)
7834 (make-obsolete 'gnus-summary-mark-as-unread-backward
7835                'gnus-summary-tick-article-backward)
7836 (defun gnus-summary-tick-article-backward (n)
7837   "Tick N articles backwards.
7838 The difference between N and the number of articles ticked is returned."
7839   (interactive "p")
7840   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7841
7842 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7843 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7844 (defun gnus-summary-tick-article (&optional article clear-mark)
7845   "Mark current article as unread.
7846 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7847 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7848   (interactive)
7849   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7850                                        gnus-ticked-mark)))
7851
7852 (defun gnus-summary-mark-as-read-forward (n)
7853   "Mark N articles as read forwards.
7854 If N is negative, mark backwards instead.
7855 The difference between N and the actual number of articles marked is
7856 returned."
7857   (interactive "p")
7858   (gnus-summary-mark-forward n gnus-del-mark t))
7859
7860 (defun gnus-summary-mark-as-read-backward (n)
7861   "Mark the N articles as read backwards.
7862 The difference between N and the actual number of articles marked is
7863 returned."
7864   (interactive "p")
7865   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7866
7867 (defun gnus-summary-mark-as-read (&optional article mark)
7868   "Mark current article as read.
7869 ARTICLE specifies the article to be marked as read.
7870 MARK specifies a string to be inserted at the beginning of the line."
7871   (gnus-summary-mark-article article mark))
7872
7873 (defun gnus-summary-clear-mark-forward (n)
7874   "Clear marks from N articles forward.
7875 If N is negative, clear backward instead.
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-clear-mark-backward (n)
7881   "Clear marks from N articles backward.
7882 The difference between N and the number of marks cleared is returned."
7883   (interactive "p")
7884   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7885
7886 (defun gnus-summary-mark-unread-as-read ()
7887   "Intended to be used by `gnus-summary-mark-article-hook'."
7888   (when (memq gnus-current-article gnus-newsgroup-unreads)
7889     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7890
7891 (defun gnus-summary-mark-read-and-unread-as-read ()
7892   "Intended to be used by `gnus-summary-mark-article-hook'."
7893   (let ((mark (gnus-summary-article-mark)))
7894     (when (or (gnus-unread-mark-p mark)
7895               (gnus-read-mark-p mark))
7896       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7897
7898 (defun gnus-summary-mark-region-as-read (point mark all)
7899   "Mark all unread articles between point and mark as read.
7900 If given a prefix, mark all articles between point and mark as read,
7901 even ticked and dormant ones."
7902   (interactive "r\nP")
7903   (save-excursion
7904     (let (article)
7905       (goto-char point)
7906       (beginning-of-line)
7907       (while (and
7908               (< (point) mark)
7909               (progn
7910                 (when (or all
7911                           (memq (setq article (gnus-summary-article-number))
7912                                 gnus-newsgroup-unreads))
7913                   (gnus-summary-mark-article article gnus-del-mark))
7914                 t)
7915               (gnus-summary-find-next))))))
7916
7917 (defun gnus-summary-mark-below (score mark)
7918   "Mark articles with score less than SCORE with MARK."
7919   (interactive "P\ncMark: ")
7920   (setq score (if score
7921                   (prefix-numeric-value score)
7922                 (or gnus-summary-default-score 0)))
7923   (save-excursion
7924     (set-buffer gnus-summary-buffer)
7925     (goto-char (point-min))
7926     (while
7927         (progn
7928           (and (< (gnus-summary-article-score) score)
7929                (gnus-summary-mark-article nil mark))
7930           (gnus-summary-find-next)))))
7931
7932 (defun gnus-summary-kill-below (&optional score)
7933   "Mark articles with score below SCORE as read."
7934   (interactive "P")
7935   (gnus-summary-mark-below score gnus-killed-mark))
7936
7937 (defun gnus-summary-clear-above (&optional score)
7938   "Clear all marks from articles with score above SCORE."
7939   (interactive "P")
7940   (gnus-summary-mark-above score gnus-unread-mark))
7941
7942 (defun gnus-summary-tick-above (&optional score)
7943   "Tick all articles with score above SCORE."
7944   (interactive "P")
7945   (gnus-summary-mark-above score gnus-ticked-mark))
7946
7947 (defun gnus-summary-mark-above (score mark)
7948   "Mark articles with score over SCORE with MARK."
7949   (interactive "P\ncMark: ")
7950   (setq score (if score
7951                   (prefix-numeric-value score)
7952                 (or gnus-summary-default-score 0)))
7953   (save-excursion
7954     (set-buffer gnus-summary-buffer)
7955     (goto-char (point-min))
7956     (while (and (progn
7957                   (when (> (gnus-summary-article-score) score)
7958                     (gnus-summary-mark-article nil mark))
7959                   t)
7960                 (gnus-summary-find-next)))))
7961
7962 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7963 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7964 (defun gnus-summary-limit-include-expunged (&optional no-error)
7965   "Display all the hidden articles that were expunged for low scores."
7966   (interactive)
7967   (let ((buffer-read-only nil))
7968     (let ((scored gnus-newsgroup-scored)
7969           headers h)
7970       (while scored
7971         (unless (gnus-summary-goto-subject (caar scored))
7972           (and (setq h (gnus-summary-article-header (caar scored)))
7973                (< (cdar scored) gnus-summary-expunge-below)
7974                (push h headers)))
7975         (setq scored (cdr scored)))
7976       (if (not headers)
7977           (when (not no-error)
7978             (error "No expunged articles hidden"))
7979         (goto-char (point-min))
7980         (gnus-summary-prepare-unthreaded (nreverse headers))
7981         (goto-char (point-min))
7982         (gnus-summary-position-point)
7983         t))))
7984
7985 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7986   "Mark all unread articles in this newsgroup as read.
7987 If prefix argument ALL is non-nil, ticked and dormant articles will
7988 also be marked as read.
7989 If QUIETLY is non-nil, no questions will be asked.
7990 If TO-HERE is non-nil, it should be a point in the buffer.  All
7991 articles before this point will be marked as read.
7992 Note that this function will only catch up the unread article
7993 in the current summary buffer limitation.
7994 The number of articles marked as read is returned."
7995   (interactive "P")
7996   (prog1
7997       (save-excursion
7998         (when (or quietly
7999                   (not gnus-interactive-catchup) ;Without confirmation?
8000                   gnus-expert-user
8001                   (gnus-y-or-n-p
8002                    (if all
8003                        "Mark absolutely all articles as read? "
8004                      "Mark all unread articles as read? ")))
8005           (if (and not-mark
8006                    (not gnus-newsgroup-adaptive)
8007                    (not gnus-newsgroup-auto-expire)
8008                    (not gnus-suppress-duplicates)
8009                    (or (not gnus-use-cache)
8010                        (eq gnus-use-cache 'passive)))
8011               (progn
8012                 (when all
8013                   (setq gnus-newsgroup-marked nil
8014                         gnus-newsgroup-dormant nil))
8015                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8016             ;; We actually mark all articles as canceled, which we
8017             ;; have to do when using auto-expiry or adaptive scoring.
8018             (gnus-summary-show-all-threads)
8019             (when (gnus-summary-first-subject (not all) t)
8020               (while (and
8021                       (if to-here (< (point) to-here) t)
8022                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8023                       (gnus-summary-find-next (not all) nil nil t))))
8024             (gnus-set-mode-line 'summary))
8025           t))
8026     (gnus-summary-position-point)))
8027
8028 (defun gnus-summary-catchup-to-here (&optional all)
8029   "Mark all unticked articles before the current one as read.
8030 If ALL is non-nil, also mark ticked and dormant articles as read."
8031   (interactive "P")
8032   (save-excursion
8033     (gnus-save-hidden-threads
8034       (let ((beg (point)))
8035         ;; We check that there are unread articles.
8036         (when (or all (gnus-summary-find-prev))
8037           (gnus-summary-catchup all t beg)))))
8038   (gnus-summary-position-point))
8039
8040 (defun gnus-summary-catchup-all (&optional quietly)
8041   "Mark all articles in this newsgroup as read."
8042   (interactive "P")
8043   (gnus-summary-catchup t quietly))
8044
8045 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8046   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8047 If prefix argument ALL is non-nil, all articles are marked as read."
8048   (interactive "P")
8049   (when (gnus-summary-catchup all quietly nil 'fast)
8050     ;; Select next newsgroup or exit.
8051     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8052              (eq gnus-auto-select-next 'quietly))
8053         (gnus-summary-next-group nil)
8054       (gnus-summary-exit))))
8055
8056 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8057   "Mark all articles in this newsgroup as read, and then exit."
8058   (interactive "P")
8059   (gnus-summary-catchup-and-exit t quietly))
8060
8061 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8062 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8063   "Mark all articles in this group as read and select the next group.
8064 If given a prefix, mark all articles, unread as well as ticked, as
8065 read."
8066   (interactive "P")
8067   (save-excursion
8068     (gnus-summary-catchup all))
8069   (gnus-summary-next-article t nil nil t))
8070
8071 ;; Thread-based commands.
8072
8073 (defun gnus-summary-articles-in-thread (&optional article)
8074   "Return a list of all articles in the current thread.
8075 If ARTICLE is non-nil, return all articles in the thread that starts
8076 with that article."
8077   (let* ((article (or article (gnus-summary-article-number)))
8078          (data (gnus-data-find-list article))
8079          (top-level (gnus-data-level (car data)))
8080          (top-subject
8081           (cond ((null gnus-thread-operation-ignore-subject)
8082                  (gnus-simplify-subject-re
8083                   (mail-header-subject (gnus-data-header (car data)))))
8084                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8085                  (gnus-simplify-subject-fuzzy
8086                   (mail-header-subject (gnus-data-header (car data)))))
8087                 (t nil)))
8088          (end-point (save-excursion
8089                       (if (gnus-summary-go-to-next-thread)
8090                           (point) (point-max))))
8091          articles)
8092     (while (and data
8093                 (< (gnus-data-pos (car data)) end-point))
8094       (when (or (not top-subject)
8095                 (string= top-subject
8096                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8097                              (gnus-simplify-subject-fuzzy
8098                               (mail-header-subject
8099                                (gnus-data-header (car data))))
8100                            (gnus-simplify-subject-re
8101                             (mail-header-subject
8102                              (gnus-data-header (car data)))))))
8103         (push (gnus-data-number (car data)) articles))
8104       (unless (and (setq data (cdr data))
8105                    (> (gnus-data-level (car data)) top-level))
8106         (setq data nil)))
8107     ;; Return the list of articles.
8108     (nreverse articles)))
8109
8110 (defun gnus-summary-rethread-current ()
8111   "Rethread the thread the current article is part of."
8112   (interactive)
8113   (let* ((gnus-show-threads t)
8114          (article (gnus-summary-article-number))
8115          (id (mail-header-id (gnus-summary-article-header)))
8116          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8117     (unless id
8118       (error "No article on the current line"))
8119     (gnus-rebuild-thread id)
8120     (gnus-summary-goto-subject article)))
8121
8122 (defun gnus-summary-reparent-thread ()
8123   "Make the current article child of the marked (or previous) article.
8124
8125 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8126 is non-nil or the Subject: of both articles are the same."
8127   (interactive)
8128   (unless (not (gnus-group-read-only-p))
8129     (error "The current newsgroup does not support article editing"))
8130   (unless (<= (length gnus-newsgroup-processable) 1)
8131     (error "No more than one article may be marked"))
8132   (save-window-excursion
8133     (let ((gnus-article-buffer " *reparent*")
8134           (current-article (gnus-summary-article-number))
8135           ;; First grab the marked article, otherwise one line up.
8136           (parent-article (if (not (null gnus-newsgroup-processable))
8137                               (car gnus-newsgroup-processable)
8138                             (save-excursion
8139                               (if (eq (forward-line -1) 0)
8140                                   (gnus-summary-article-number)
8141                                 (error "Beginning of summary buffer"))))))
8142       (unless (not (eq current-article parent-article))
8143         (error "An article may not be self-referential"))
8144       (let ((message-id (mail-header-id
8145                          (gnus-summary-article-header parent-article))))
8146         (unless (and message-id (not (equal message-id "")))
8147           (error "No message-id in desired parent"))
8148         ;; We don't want the article to be marked as read.
8149         (let (gnus-mark-article-hook)
8150           (gnus-summary-select-article t t nil current-article))
8151         (set-buffer gnus-original-article-buffer)
8152         (let ((buf (format "%s" (buffer-string))))
8153           (with-temp-buffer
8154             (insert buf)
8155             (goto-char (point-min))
8156             (if (re-search-forward "^References: " nil t)
8157                 (progn
8158                   (re-search-forward "^[^ \t]" nil t)
8159                   (forward-line -1)
8160                   (end-of-line)
8161                   (insert " " message-id))
8162               (insert "References: " message-id "\n"))
8163             (unless (gnus-request-replace-article
8164                      current-article (car gnus-article-current)
8165                      (current-buffer))
8166               (error "Couldn't replace article"))))
8167         (set-buffer gnus-summary-buffer)
8168         (gnus-summary-unmark-all-processable)
8169         (gnus-summary-update-article current-article)
8170         (gnus-summary-rethread-current)
8171         (gnus-message 3 "Article %d is now the child of article %d"
8172                       current-article parent-article)))))
8173
8174 (defun gnus-summary-toggle-threads (&optional arg)
8175   "Toggle showing conversation threads.
8176 If ARG is positive number, turn showing conversation threads on."
8177   (interactive "P")
8178   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8179     (setq gnus-show-threads
8180           (if (null arg) (not gnus-show-threads)
8181             (> (prefix-numeric-value arg) 0)))
8182     (gnus-summary-prepare)
8183     (gnus-summary-goto-subject current)
8184     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8185     (gnus-summary-position-point)))
8186
8187 (defun gnus-summary-show-all-threads ()
8188   "Show all threads."
8189   (interactive)
8190   (save-excursion
8191     (let ((buffer-read-only nil))
8192       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8193   (gnus-summary-position-point))
8194
8195 (defun gnus-summary-show-thread ()
8196   "Show thread subtrees.
8197 Returns nil if no thread was there to be shown."
8198   (interactive)
8199   (let ((buffer-read-only nil)
8200         (orig (point))
8201         ;; first goto end then to beg, to have point at beg after let
8202         (end (progn (end-of-line) (point)))
8203         (beg (progn (beginning-of-line) (point))))
8204     (prog1
8205         ;; Any hidden lines here?
8206         (search-forward "\r" end t)
8207       (subst-char-in-region beg end ?\^M ?\n t)
8208       (goto-char orig)
8209       (gnus-summary-position-point))))
8210
8211 (defun gnus-summary-hide-all-threads ()
8212   "Hide all thread subtrees."
8213   (interactive)
8214   (save-excursion
8215     (goto-char (point-min))
8216     (gnus-summary-hide-thread)
8217     (while (zerop (gnus-summary-next-thread 1 t))
8218       (gnus-summary-hide-thread)))
8219   (gnus-summary-position-point))
8220
8221 (defun gnus-summary-hide-thread ()
8222   "Hide thread subtrees.
8223 Returns nil if no threads were there to be hidden."
8224   (interactive)
8225   (let ((buffer-read-only nil)
8226         (start (point))
8227         (article (gnus-summary-article-number)))
8228     (goto-char start)
8229     ;; Go forward until either the buffer ends or the subthread
8230     ;; ends.
8231     (when (and (not (eobp))
8232                (or (zerop (gnus-summary-next-thread 1 t))
8233                    (goto-char (point-max))))
8234       (prog1
8235           (if (and (> (point) start)
8236                    (search-backward "\n" start t))
8237               (progn
8238                 (subst-char-in-region start (point) ?\n ?\^M)
8239                 (gnus-summary-goto-subject article))
8240             (goto-char start)
8241             nil)
8242         ;;(gnus-summary-position-point)
8243         ))))
8244
8245 (defun gnus-summary-go-to-next-thread (&optional previous)
8246   "Go to the same level (or less) next thread.
8247 If PREVIOUS is non-nil, go to previous thread instead.
8248 Return the article number moved to, or nil if moving was impossible."
8249   (let ((level (gnus-summary-thread-level))
8250         (way (if previous -1 1))
8251         (beg (point)))
8252     (forward-line way)
8253     (while (and (not (eobp))
8254                 (< level (gnus-summary-thread-level)))
8255       (forward-line way))
8256     (if (eobp)
8257         (progn
8258           (goto-char beg)
8259           nil)
8260       (setq beg (point))
8261       (prog1
8262           (gnus-summary-article-number)
8263         (goto-char beg)))))
8264
8265 (defun gnus-summary-next-thread (n &optional silent)
8266   "Go to the same level next N'th thread.
8267 If N is negative, search backward instead.
8268 Returns the difference between N and the number of skips actually
8269 done.
8270
8271 If SILENT, don't output messages."
8272   (interactive "p")
8273   (let ((backward (< n 0))
8274         (n (abs n)))
8275     (while (and (> n 0)
8276                 (gnus-summary-go-to-next-thread backward))
8277       (decf n))
8278     (unless silent
8279       (gnus-summary-position-point))
8280     (when (and (not silent) (/= 0 n))
8281       (gnus-message 7 "No more threads"))
8282     n))
8283
8284 (defun gnus-summary-prev-thread (n)
8285   "Go to the same level previous N'th thread.
8286 Returns the difference between N and the number of skips actually
8287 done."
8288   (interactive "p")
8289   (gnus-summary-next-thread (- n)))
8290
8291 (defun gnus-summary-go-down-thread ()
8292   "Go down one level in the current thread."
8293   (let ((children (gnus-summary-article-children)))
8294     (when children
8295       (gnus-summary-goto-subject (car children)))))
8296
8297 (defun gnus-summary-go-up-thread ()
8298   "Go up one level in the current thread."
8299   (let ((parent (gnus-summary-article-parent)))
8300     (when parent
8301       (gnus-summary-goto-subject parent))))
8302
8303 (defun gnus-summary-down-thread (n)
8304   "Go down thread N steps.
8305 If N is negative, go up instead.
8306 Returns the difference between N and how many steps down that were
8307 taken."
8308   (interactive "p")
8309   (let ((up (< n 0))
8310         (n (abs n)))
8311     (while (and (> n 0)
8312                 (if up (gnus-summary-go-up-thread)
8313                   (gnus-summary-go-down-thread)))
8314       (setq n (1- n)))
8315     (gnus-summary-position-point)
8316     (when (/= 0 n)
8317       (gnus-message 7 "Can't go further"))
8318     n))
8319
8320 (defun gnus-summary-up-thread (n)
8321   "Go up thread N steps.
8322 If N is negative, go up instead.
8323 Returns the difference between N and how many steps down that were
8324 taken."
8325   (interactive "p")
8326   (gnus-summary-down-thread (- n)))
8327
8328 (defun gnus-summary-top-thread ()
8329   "Go to the top of the thread."
8330   (interactive)
8331   (while (gnus-summary-go-up-thread))
8332   (gnus-summary-article-number))
8333
8334 (defun gnus-summary-kill-thread (&optional unmark)
8335   "Mark articles under current thread as read.
8336 If the prefix argument is positive, remove any kinds of marks.
8337 If the prefix argument is negative, tick articles instead."
8338   (interactive "P")
8339   (when unmark
8340     (setq unmark (prefix-numeric-value unmark)))
8341   (let ((articles (gnus-summary-articles-in-thread)))
8342     (save-excursion
8343       ;; Expand the thread.
8344       (gnus-summary-show-thread)
8345       ;; Mark all the articles.
8346       (while articles
8347         (gnus-summary-goto-subject (car articles))
8348         (cond ((null unmark)
8349                (gnus-summary-mark-article-as-read gnus-killed-mark))
8350               ((> unmark 0)
8351                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8352               (t
8353                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8354         (setq articles (cdr articles))))
8355     ;; Hide killed subtrees.
8356     (and (null unmark)
8357          gnus-thread-hide-killed
8358          (gnus-summary-hide-thread))
8359     ;; If marked as read, go to next unread subject.
8360     (when (null unmark)
8361       ;; Go to next unread subject.
8362       (gnus-summary-next-subject 1 t)))
8363   (gnus-set-mode-line 'summary))
8364
8365 ;; Summary sorting commands
8366
8367 (defun gnus-summary-sort-by-number (&optional reverse)
8368   "Sort the summary buffer by article number.
8369 Argument REVERSE means reverse order."
8370   (interactive "P")
8371   (gnus-summary-sort 'number reverse))
8372
8373 (defun gnus-summary-sort-by-author (&optional reverse)
8374   "Sort the summary buffer by author name alphabetically.
8375 If case-fold-search is non-nil, case of letters is ignored.
8376 Argument REVERSE means reverse order."
8377   (interactive "P")
8378   (gnus-summary-sort 'author reverse))
8379
8380 (defun gnus-summary-sort-by-subject (&optional reverse)
8381   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8382 If case-fold-search is non-nil, case of letters is ignored.
8383 Argument REVERSE means reverse order."
8384   (interactive "P")
8385   (gnus-summary-sort 'subject reverse))
8386
8387 (defun gnus-summary-sort-by-date (&optional reverse)
8388   "Sort the summary buffer by date.
8389 Argument REVERSE means reverse order."
8390   (interactive "P")
8391   (gnus-summary-sort 'date reverse))
8392
8393 (defun gnus-summary-sort-by-score (&optional reverse)
8394   "Sort the summary buffer by score.
8395 Argument REVERSE means reverse order."
8396   (interactive "P")
8397   (gnus-summary-sort 'score reverse))
8398
8399 (defun gnus-summary-sort-by-lines (&optional reverse)
8400   "Sort the summary buffer by article length.
8401 Argument REVERSE means reverse order."
8402   (interactive "P")
8403   (gnus-summary-sort 'lines reverse))
8404
8405 (defun gnus-summary-sort (predicate reverse)
8406   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8407   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8408          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8409          (gnus-thread-sort-functions
8410           (list
8411            (if (not reverse)
8412                thread
8413              `(lambda (t1 t2)
8414                 (,thread t2 t1)))))
8415          (gnus-article-sort-functions
8416           (list
8417            (if (not reverse)
8418                article
8419              `(lambda (t1 t2)
8420                 (,article t2 t1)))))
8421          (buffer-read-only)
8422          (gnus-summary-prepare-hook nil))
8423     ;; We do the sorting by regenerating the threads.
8424     (gnus-summary-prepare)
8425     ;; Hide subthreads if needed.
8426     (when (and gnus-show-threads gnus-thread-hide-subtree)
8427       (gnus-summary-hide-all-threads))))
8428
8429 ;; Summary saving commands.
8430
8431 (defun gnus-summary-save-article (&optional n not-saved)
8432   "Save the current article using the default saver function.
8433 If N is a positive number, save the N next articles.
8434 If N is a negative number, save the N previous articles.
8435 If N is nil and any articles have been marked with the process mark,
8436 save those articles instead.
8437 The variable `gnus-default-article-saver' specifies the saver function."
8438   (interactive "P")
8439   (let* ((articles (gnus-summary-work-articles n))
8440          (save-buffer (save-excursion
8441                         (nnheader-set-temp-buffer " *Gnus Save*")))
8442          (num (length articles))
8443          header article file)
8444     (while articles
8445       (setq header (gnus-summary-article-header
8446                     (setq article (pop articles))))
8447       (if (not (vectorp header))
8448           ;; This is a pseudo-article.
8449           (if (assq 'name header)
8450               (gnus-copy-file (cdr (assq 'name header)))
8451             (gnus-message 1 "Article %d is unsaveable" article))
8452         ;; This is a real article.
8453         (save-window-excursion
8454           (gnus-summary-select-article t nil nil article))
8455         (save-excursion
8456           (set-buffer save-buffer)
8457           (erase-buffer)
8458           (insert-buffer-substring gnus-original-article-buffer))
8459         (setq file (gnus-article-save save-buffer file num))
8460         (gnus-summary-remove-process-mark article)
8461         (unless not-saved
8462           (gnus-summary-set-saved-mark article))))
8463     (gnus-kill-buffer save-buffer)
8464     (gnus-summary-position-point)
8465     (gnus-set-mode-line 'summary)
8466     n))
8467
8468 (defun gnus-summary-pipe-output (&optional arg)
8469   "Pipe the current article to a subprocess.
8470 If N is a positive number, pipe the N next articles.
8471 If N is a negative number, pipe the N previous articles.
8472 If N is nil and any articles have been marked with the process mark,
8473 pipe those articles instead."
8474   (interactive "P")
8475   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8476     (gnus-summary-save-article arg t))
8477   (gnus-configure-windows 'pipe))
8478
8479 (defun gnus-summary-save-article-mail (&optional arg)
8480   "Append the current article to an mail file.
8481 If N is a positive number, save the N next articles.
8482 If N is a negative number, save the N previous articles.
8483 If N is nil and any articles have been marked with the process mark,
8484 save those articles instead."
8485   (interactive "P")
8486   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8487     (gnus-summary-save-article arg)))
8488
8489 (defun gnus-summary-save-article-rmail (&optional arg)
8490   "Append the current article to an rmail file.
8491 If N is a positive number, save the N next articles.
8492 If N is a negative number, save the N previous articles.
8493 If N is nil and any articles have been marked with the process mark,
8494 save those articles instead."
8495   (interactive "P")
8496   (let ((gnus-default-article-saver 'rmail-output-to-rmail-file))
8497     (gnus-summary-save-article arg)))
8498
8499 (defun gnus-summary-save-article-file (&optional arg)
8500   "Append the current article to a file.
8501 If N is a positive number, save the N next articles.
8502 If N is a negative number, save the N previous articles.
8503 If N is nil and any articles have been marked with the process mark,
8504 save those articles instead."
8505   (interactive "P")
8506   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8507     (gnus-summary-save-article arg)))
8508
8509 (defun gnus-summary-write-article-file (&optional arg)
8510   "Write the current article to a file, deleting the previous file.
8511 If N is a positive number, save the N next articles.
8512 If N is a negative number, save the N previous articles.
8513 If N is nil and any articles have been marked with the process mark,
8514 save those articles instead."
8515   (interactive "P")
8516   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8517     (gnus-summary-save-article arg)))
8518
8519 (defun gnus-summary-save-article-body-file (&optional arg)
8520   "Append the current article body to a file.
8521 If N is a positive number, save the N next articles.
8522 If N is a negative number, save the N previous articles.
8523 If N is nil and any articles have been marked with the process mark,
8524 save those articles instead."
8525   (interactive "P")
8526   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8527     (gnus-summary-save-article arg)))
8528
8529 (defun gnus-summary-pipe-message (program)
8530   "Pipe the current article through PROGRAM."
8531   (interactive "sProgram: ")
8532   (gnus-summary-select-article)
8533   (let ((mail-header-separator ""))
8534     (gnus-eval-in-buffer-window gnus-article-buffer
8535       (save-restriction
8536         (widen)
8537         (let ((start (window-start))
8538               buffer-read-only)
8539           (message-pipe-buffer-body program)
8540           (set-window-start (get-buffer-window (current-buffer)) start))))))
8541
8542 (defun gnus-get-split-value (methods)
8543   "Return a value based on the split METHODS."
8544   (let (split-name method result match)
8545     (when methods
8546       (save-excursion
8547         (set-buffer gnus-original-article-buffer)
8548         (save-restriction
8549           (nnheader-narrow-to-headers)
8550           (while methods
8551             (goto-char (point-min))
8552             (setq method (pop methods))
8553             (setq match (car method))
8554             (when (cond
8555                    ((stringp match)
8556                     ;; Regular expression.
8557                     (ignore-errors
8558                       (re-search-forward match nil t)))
8559                    ((gnus-functionp match)
8560                     ;; Function.
8561                     (save-restriction
8562                       (widen)
8563                       (setq result (funcall match gnus-newsgroup-name))))
8564                    ((consp match)
8565                     ;; Form.
8566                     (save-restriction
8567                       (widen)
8568                       (setq result (eval match)))))
8569               (setq split-name (append (cdr method) split-name))
8570               (cond ((stringp result)
8571                      (push (expand-file-name
8572                             result gnus-article-save-directory)
8573                            split-name))
8574                     ((consp result)
8575                      (setq split-name (append result split-name)))))))))
8576     split-name))
8577
8578 (defun gnus-valid-move-group-p (group)
8579   (and (boundp group)
8580        (symbol-name group)
8581        (memq 'respool
8582              (assoc (symbol-name
8583                      (car (gnus-find-method-for-group
8584                            (symbol-name group))))
8585                     gnus-valid-select-methods))))
8586
8587 (defun gnus-read-move-group-name (prompt default articles prefix)
8588   "Read a group name."
8589   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8590          (minibuffer-confirm-incomplete nil) ; XEmacs
8591          (prom
8592           (format "%s %s to:"
8593                   prompt
8594                   (if (> (length articles) 1)
8595                       (format "these %d articles" (length articles))
8596                     "this article")))
8597          (to-newsgroup
8598           (cond
8599            ((null split-name)
8600             (gnus-completing-read default prom
8601                                   gnus-active-hashtb
8602                                   'gnus-valid-move-group-p
8603                                   nil prefix
8604                                   'gnus-group-history))
8605            ((= 1 (length split-name))
8606             (gnus-completing-read (car split-name) prom
8607                                   gnus-active-hashtb
8608                                   'gnus-valid-move-group-p
8609                                   nil nil
8610                                   'gnus-group-history))
8611            (t
8612             (gnus-completing-read nil prom
8613                                   (mapcar (lambda (el) (list el))
8614                                           (nreverse split-name))
8615                                   nil nil nil
8616                                   'gnus-group-history)))))
8617     (when to-newsgroup
8618       (if (or (string= to-newsgroup "")
8619               (string= to-newsgroup prefix))
8620           (setq to-newsgroup default))
8621       (unless to-newsgroup
8622         (error "No group name entered"))
8623       (or (gnus-active to-newsgroup)
8624           (gnus-activate-group to-newsgroup)
8625           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8626                                      to-newsgroup))
8627               (or (and (gnus-request-create-group
8628                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8629                        (gnus-activate-group to-newsgroup nil nil
8630                                             (gnus-group-name-to-method
8631                                              to-newsgroup)))
8632                   (error "Couldn't create group %s" to-newsgroup)))
8633           (error "No such group: %s" to-newsgroup)))
8634     to-newsgroup))
8635
8636 ;; Summary extract commands
8637
8638 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8639   (let ((buffer-read-only nil)
8640         (article (gnus-summary-article-number))
8641         after-article b e)
8642     (unless (gnus-summary-goto-subject article)
8643       (error "No such article: %d" article))
8644     (gnus-summary-position-point)
8645     ;; If all commands are to be bunched up on one line, we collect
8646     ;; them here.
8647     (unless gnus-view-pseudos-separately
8648       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8649             files action)
8650         (while ps
8651           (setq action (cdr (assq 'action (car ps))))
8652           (setq files (list (cdr (assq 'name (car ps)))))
8653           (while (and ps (cdr ps)
8654                       (string= (or action "1")
8655                                (or (cdr (assq 'action (cadr ps))) "2")))
8656             (push (cdr (assq 'name (cadr ps))) files)
8657             (setcdr ps (cddr ps)))
8658           (when files
8659             (when (not (string-match "%s" action))
8660               (push " " files))
8661             (push " " files)
8662             (when (assq 'execute (car ps))
8663               (setcdr (assq 'execute (car ps))
8664                       (funcall (if (string-match "%s" action)
8665                                    'format 'concat)
8666                                action
8667                                (mapconcat
8668                                 (lambda (f)
8669                                   (if (equal f " ")
8670                                       f
8671                                     (gnus-quote-arg-for-sh-or-csh f)))
8672                                 files " ")))))
8673           (setq ps (cdr ps)))))
8674     (if (and gnus-view-pseudos (not not-view))
8675         (while pslist
8676           (when (assq 'execute (car pslist))
8677             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8678                                   (eq gnus-view-pseudos 'not-confirm)))
8679           (setq pslist (cdr pslist)))
8680       (save-excursion
8681         (while pslist
8682           (setq after-article (or (cdr (assq 'article (car pslist)))
8683                                   (gnus-summary-article-number)))
8684           (gnus-summary-goto-subject after-article)
8685           (forward-line 1)
8686           (setq b (point))
8687           (insert "    " (file-name-nondirectory
8688                           (cdr (assq 'name (car pslist))))
8689                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8690           (setq e (point))
8691           (forward-line -1)             ; back to `b'
8692           (gnus-add-text-properties
8693            b (1- e) (list 'gnus-number gnus-reffed-article-number
8694                           gnus-mouse-face-prop gnus-mouse-face))
8695           (gnus-data-enter
8696            after-article gnus-reffed-article-number
8697            gnus-unread-mark b (car pslist) 0 (- e b))
8698           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8699           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8700           (setq pslist (cdr pslist)))))))
8701
8702 (defun gnus-pseudos< (p1 p2)
8703   (let ((c1 (cdr (assq 'action p1)))
8704         (c2 (cdr (assq 'action p2))))
8705     (and c1 c2 (string< c1 c2))))
8706
8707 (defun gnus-request-pseudo-article (props)
8708   (cond ((assq 'execute props)
8709          (gnus-execute-command (cdr (assq 'execute props)))))
8710   (let ((gnus-current-article (gnus-summary-article-number)))
8711     (gnus-run-hooks 'gnus-mark-article-hook)))
8712
8713 (defun gnus-execute-command (command &optional automatic)
8714   (save-excursion
8715     (gnus-article-setup-buffer)
8716     (set-buffer gnus-article-buffer)
8717     (setq buffer-read-only nil)
8718     (let ((command (if automatic command
8719                      (read-string "Command: " (cons command 0)))))
8720       (erase-buffer)
8721       (insert "$ " command "\n\n")
8722       (if gnus-view-pseudo-asynchronously
8723           (start-process "gnus-execute" (current-buffer) shell-file-name
8724                          shell-command-switch command)
8725         (call-process shell-file-name nil t nil
8726                       shell-command-switch command)))))
8727
8728 ;; Summary kill commands.
8729
8730 (defun gnus-summary-edit-global-kill (article)
8731   "Edit the \"global\" kill file."
8732   (interactive (list (gnus-summary-article-number)))
8733   (gnus-group-edit-global-kill article))
8734
8735 (defun gnus-summary-edit-local-kill ()
8736   "Edit a local kill file applied to the current newsgroup."
8737   (interactive)
8738   (setq gnus-current-headers (gnus-summary-article-header))
8739   (gnus-group-edit-local-kill
8740    (gnus-summary-article-number) gnus-newsgroup-name))
8741
8742 ;;; Header reading.
8743
8744 (defun gnus-read-header (id &optional header)
8745   "Read the headers of article ID and enter them into the Gnus system."
8746   (let ((group gnus-newsgroup-name)
8747         (gnus-override-method
8748          (and (gnus-news-group-p gnus-newsgroup-name)
8749               gnus-refer-article-method))
8750         where)
8751     ;; First we check to see whether the header in question is already
8752     ;; fetched.
8753     (if (stringp id)
8754         ;; This is a Message-ID.
8755         (setq header (or header (gnus-id-to-header id)))
8756       ;; This is an article number.
8757       (setq header (or header (gnus-summary-article-header id))))
8758     (if (and header
8759              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8760         ;; We have found the header.
8761         header
8762       ;; If this is a sparse article, we have to nix out its
8763       ;; previous entry in the thread hashtb.
8764       (when (and header
8765                  (gnus-summary-article-sparse-p (mail-header-number header)))
8766         (let* ((parent (gnus-parent-id (mail-header-references header)))
8767                (thread (and parent (gnus-id-to-thread parent))))
8768           (when thread
8769             (delq (assq header thread) thread))))
8770       ;; We have to really fetch the header to this article.
8771       (save-excursion
8772         (set-buffer nntp-server-buffer)
8773         (when (setq where (gnus-request-head id group))
8774           (nnheader-fold-continuation-lines)
8775           (goto-char (point-max))
8776           (insert ".\n")
8777           (goto-char (point-min))
8778           (insert "211 ")
8779           (princ (cond
8780                   ((numberp id) id)
8781                   ((cdr where) (cdr where))
8782                   (header (mail-header-number header))
8783                   (t gnus-reffed-article-number))
8784                  (current-buffer))
8785           (insert " Article retrieved.\n"))
8786         (if (or (not where)
8787                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8788             ()                          ; Malformed head.
8789           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8790             (when (and (stringp id)
8791                        (not (string= (gnus-group-real-name group)
8792                                      (car where))))
8793               ;; If we fetched by Message-ID and the article came
8794               ;; from a different group, we fudge some bogus article
8795               ;; numbers for this article.
8796               (mail-header-set-number header gnus-reffed-article-number))
8797             (save-excursion
8798               (set-buffer gnus-summary-buffer)
8799               (decf gnus-reffed-article-number)
8800               (gnus-remove-header (mail-header-number header))
8801               (push header gnus-newsgroup-headers)
8802               (setq gnus-current-headers header)
8803               (push (mail-header-number header) gnus-newsgroup-limit)))
8804           header)))))
8805
8806 (defun gnus-remove-header (number)
8807   "Remove header NUMBER from `gnus-newsgroup-headers'."
8808   (if (and gnus-newsgroup-headers
8809            (= number (mail-header-number (car gnus-newsgroup-headers))))
8810       (pop gnus-newsgroup-headers)
8811     (let ((headers gnus-newsgroup-headers))
8812       (while (and (cdr headers)
8813                   (not (= number (mail-header-number (cadr headers)))))
8814         (pop headers))
8815       (when (cdr headers)
8816         (setcdr headers (cddr headers))))))
8817
8818 ;;;
8819 ;;; summary highlights
8820 ;;;
8821
8822 (defun gnus-highlight-selected-summary ()
8823   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8824   ;; Highlight selected article in summary buffer
8825   (when gnus-summary-selected-face
8826     (save-excursion
8827       (let* ((beg (progn (beginning-of-line) (point)))
8828              (end (progn (end-of-line) (point)))
8829              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8830              (from (if (get-text-property beg gnus-mouse-face-prop)
8831                        beg
8832                      (or (next-single-property-change
8833                           beg gnus-mouse-face-prop nil end)
8834                          beg)))
8835              (to
8836               (if (= from end)
8837                   (- from 2)
8838                 (or (next-single-property-change
8839                      from gnus-mouse-face-prop nil end)
8840                     end))))
8841         ;; If no mouse-face prop on line we will have to = from = end,
8842         ;; so we highlight the entire line instead.
8843         (when (= (+ to 2) from)
8844           (setq from beg)
8845           (setq to end))
8846         (if gnus-newsgroup-selected-overlay
8847             ;; Move old overlay.
8848             (gnus-move-overlay
8849              gnus-newsgroup-selected-overlay from to (current-buffer))
8850           ;; Create new overlay.
8851           (gnus-overlay-put
8852            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8853            'face gnus-summary-selected-face))))))
8854
8855 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8856 (defun gnus-summary-highlight-line ()
8857   "Highlight current line according to `gnus-summary-highlight'."
8858   (let* ((list gnus-summary-highlight)
8859          (p (point))
8860          (end (progn (end-of-line) (point)))
8861          ;; now find out where the line starts and leave point there.
8862          (beg (progn (beginning-of-line) (point)))
8863          (article (gnus-summary-article-number))
8864          (score (or (cdr (assq (or article gnus-current-article)
8865                                gnus-newsgroup-scored))
8866                     gnus-summary-default-score 0))
8867          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8868          (inhibit-read-only t))
8869     ;; Eval the cars of the lists until we find a match.
8870     (let ((default gnus-summary-default-score))
8871       (while (and list
8872                   (not (eval (caar list))))
8873         (setq list (cdr list))))
8874     (let ((face (cdar list)))
8875       (unless (eq face (get-text-property beg 'face))
8876         (gnus-put-text-property-excluding-characters-with-faces
8877          beg end 'face
8878          (setq face (if (boundp face) (symbol-value face) face)))
8879         (when gnus-summary-highlight-line-function
8880           (funcall gnus-summary-highlight-line-function article face))))
8881     (goto-char p)))
8882
8883 (defun gnus-update-read-articles (group unread &optional compute)
8884   "Update the list of read articles in GROUP."
8885   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8886          (entry (gnus-gethash group gnus-newsrc-hashtb))
8887          (info (nth 2 entry))
8888          (prev 1)
8889          (unread (sort (copy-sequence unread) '<))
8890          read)
8891     (if (or (not info) (not active))
8892         ;; There is no info on this group if it was, in fact,
8893         ;; killed.  Gnus stores no information on killed groups, so
8894         ;; there's nothing to be done.
8895         ;; One could store the information somewhere temporarily,
8896         ;; perhaps...  Hmmm...
8897         ()
8898       ;; Remove any negative articles numbers.
8899       (while (and unread (< (car unread) 0))
8900         (setq unread (cdr unread)))
8901       ;; Remove any expired article numbers
8902       (while (and unread (< (car unread) (car active)))
8903         (setq unread (cdr unread)))
8904       ;; Compute the ranges of read articles by looking at the list of
8905       ;; unread articles.
8906       (while unread
8907         (when (/= (car unread) prev)
8908           (push (if (= prev (1- (car unread))) prev
8909                   (cons prev (1- (car unread))))
8910                 read))
8911         (setq prev (1+ (car unread)))
8912         (setq unread (cdr unread)))
8913       (when (<= prev (cdr active))
8914         (push (cons prev (cdr active)) read))
8915       (if compute
8916           (if (> (length read) 1) (nreverse read) read)
8917         (save-excursion
8918           (set-buffer gnus-group-buffer)
8919           (gnus-undo-register
8920             `(progn
8921                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
8922                (gnus-info-set-read ',info ',(gnus-info-read info))
8923                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
8924                (gnus-group-update-group ,group t))))
8925         ;; Enter this list into the group info.
8926         (gnus-info-set-read
8927          info (if (> (length read) 1) (nreverse read) read))
8928         ;; Set the number of unread articles in gnus-newsrc-hashtb.
8929         (gnus-get-unread-articles-in-group info (gnus-active group))
8930         t))))
8931
8932 (defun gnus-offer-save-summaries ()
8933   "Offer to save all active summary buffers."
8934   (save-excursion
8935     (let ((buflist (buffer-list))
8936           buffers bufname)
8937       ;; Go through all buffers and find all summaries.
8938       (while buflist
8939         (and (setq bufname (buffer-name (car buflist)))
8940              (string-match "Summary" bufname)
8941              (save-excursion
8942                (set-buffer bufname)
8943                ;; We check that this is, indeed, a summary buffer.
8944                (and (eq major-mode 'gnus-summary-mode)
8945                     ;; Also make sure this isn't bogus.
8946                     gnus-newsgroup-prepared
8947                     ;; Also make sure that this isn't a dead summary buffer.
8948                     (not gnus-dead-summary-mode)))
8949              (push bufname buffers))
8950         (setq buflist (cdr buflist)))
8951       ;; Go through all these summary buffers and offer to save them.
8952       (when buffers
8953         (map-y-or-n-p
8954          "Update summary buffer %s? "
8955          (lambda (buf)
8956            (switch-to-buffer buf)
8957            (gnus-summary-exit))
8958          buffers)))))
8959
8960 (gnus-ems-redefine)
8961
8962 (provide 'gnus-sum)
8963
8964 (run-hooks 'gnus-sum-load-hook)
8965
8966 ;;; gnus-sum.el ends here