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