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