Sync up with Pterodactyl Gnus 0.10.
[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 (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           )
4576         (forward-line 1))
4577       ;; A common bug in inn is that if you have posted an article and
4578       ;; then retrieves the active file, it will answer correctly --
4579       ;; the new article is included.  However, a NOV entry for the
4580       ;; article may not have been generated yet, so this may fail.
4581       ;; We work around this problem by retrieving the last few
4582       ;; headers using HEAD.
4583       (if (or (not also-fetch-heads)
4584               (not sequence))
4585           ;; We (probably) got all the headers.
4586           (nreverse headers)
4587         (let ((gnus-nov-is-evil t))
4588           (nconc
4589            (nreverse headers)
4590            (when (gnus-retrieve-headers sequence group)
4591              (gnus-get-newsgroup-headers))))))))
4592
4593 (defun gnus-article-get-xrefs ()
4594   "Fill in the Xref value in `gnus-current-headers', if necessary.
4595 This is meant to be called in `gnus-article-internal-prepare-hook'."
4596   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4597                                  gnus-current-headers)))
4598     (or (not gnus-use-cross-reference)
4599         (not headers)
4600         (and (mail-header-xref headers)
4601              (not (string= (mail-header-xref headers) "")))
4602         (let ((case-fold-search t)
4603               xref)
4604           (save-restriction
4605             (nnheader-narrow-to-headers)
4606             (goto-char (point-min))
4607             (when (or (and (eq (downcase (following-char)) ?x)
4608                            (looking-at "Xref:"))
4609                       (search-forward "\nXref:" nil t))
4610               (goto-char (1+ (match-end 0)))
4611               (setq xref (buffer-substring (point)
4612                                            (progn (end-of-line) (point))))
4613               (mail-header-set-xref headers xref)))))))
4614
4615 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4616   "Find article ID and insert the summary line for that article.
4617 OLD-HEADER can either be a header or a line number to insert
4618 the subject line on."
4619   (let* ((line (and (numberp old-header) old-header))
4620          (old-header (and (vectorp old-header) old-header))
4621          (header (cond ((and old-header use-old-header)
4622                        old-header)
4623                       ((and (numberp id)
4624                             (gnus-number-to-header id))
4625                        (gnus-number-to-header id))
4626                       (t
4627                        (gnus-read-header id))))
4628         (number (and (numberp id) id))
4629         d)
4630     (when header
4631       ;; Rebuild the thread that this article is part of and go to the
4632       ;; article we have fetched.
4633       (when (and (not gnus-show-threads)
4634                  old-header)
4635         (when (and number
4636                    (setq d (gnus-data-find (mail-header-number old-header))))
4637           (goto-char (gnus-data-pos d))
4638           (gnus-data-remove
4639            number
4640            (- (gnus-point-at-bol)
4641               (prog1
4642                   (1+ (gnus-point-at-eol))
4643                 (gnus-delete-line))))))
4644       (when old-header
4645         (mail-header-set-number header (mail-header-number old-header)))
4646       (setq gnus-newsgroup-sparse
4647             (delq (setq number (mail-header-number header))
4648                   gnus-newsgroup-sparse))
4649       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4650       (push number gnus-newsgroup-limit)
4651       (gnus-rebuild-thread (mail-header-id header) line)
4652       (gnus-summary-goto-subject number nil t))
4653     (when (and (numberp number)
4654                (> number 0))
4655       ;; We have to update the boundaries even if we can't fetch the
4656       ;; article if ID is a number -- so that the next `P' or `N'
4657       ;; command will fetch the previous (or next) article even
4658       ;; if the one we tried to fetch this time has been canceled.
4659       (when (> number gnus-newsgroup-end)
4660         (setq gnus-newsgroup-end number))
4661       (when (< number gnus-newsgroup-begin)
4662         (setq gnus-newsgroup-begin number))
4663       (setq gnus-newsgroup-unselected
4664             (delq number gnus-newsgroup-unselected)))
4665     ;; Report back a success?
4666     (and header (mail-header-number header))))
4667
4668 ;;; Process/prefix in the summary buffer
4669
4670 (defun gnus-summary-work-articles (n)
4671   "Return a list of articles to be worked upon.
4672 The prefix argument, the list of process marked articles, and the
4673 current article will be taken into consideration."
4674   (save-excursion
4675     (set-buffer gnus-summary-buffer)
4676     (cond
4677      (n
4678       ;; A numerical prefix has been given.
4679       (setq n (prefix-numeric-value n))
4680       (let ((backward (< n 0))
4681             (n (abs (prefix-numeric-value n)))
4682             articles article)
4683         (save-excursion
4684           (while
4685               (and (> n 0)
4686                    (push (setq article (gnus-summary-article-number))
4687                          articles)
4688                    (if backward
4689                        (gnus-summary-find-prev nil article)
4690                      (gnus-summary-find-next nil article)))
4691             (decf n)))
4692         (nreverse articles)))
4693      ((and (gnus-region-active-p) (mark))
4694       (message "region active")
4695       ;; Work on the region between point and mark.
4696       (let ((max (max (point) (mark)))
4697             articles article)
4698         (save-excursion
4699           (goto-char (min (point) (mark)))
4700           (while
4701               (and
4702                (push (setq article (gnus-summary-article-number)) articles)
4703                (gnus-summary-find-next nil article)
4704                (< (point) max)))
4705           (nreverse articles))))
4706      (gnus-newsgroup-processable
4707       ;; There are process-marked articles present.
4708       ;; Save current state.
4709       (gnus-summary-save-process-mark)
4710       ;; Return the list.
4711       (reverse gnus-newsgroup-processable))
4712      (t
4713       ;; Just return the current article.
4714       (list (gnus-summary-article-number))))))
4715
4716 (defmacro gnus-summary-iterate (arg &rest forms)
4717   "Iterate over the process/prefixed articles and do FORMS.
4718 ARG is the interactive prefix given to the command.  FORMS will be
4719 executed with point over the summary line of the articles."
4720   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4721     `(let ((,articles (gnus-summary-work-articles ,arg)))
4722        (while ,articles
4723          (gnus-summary-goto-subject (car ,articles))
4724          ,@forms))))
4725
4726 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4727 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4728
4729 (defun gnus-summary-save-process-mark ()
4730   "Push the current set of process marked articles on the stack."
4731   (interactive)
4732   (push (copy-sequence gnus-newsgroup-processable)
4733         gnus-newsgroup-process-stack))
4734
4735 (defun gnus-summary-kill-process-mark ()
4736   "Push the current set of process marked articles on the stack and unmark."
4737   (interactive)
4738   (gnus-summary-save-process-mark)
4739   (gnus-summary-unmark-all-processable))
4740
4741 (defun gnus-summary-yank-process-mark ()
4742   "Pop the last process mark state off the stack and restore it."
4743   (interactive)
4744   (unless gnus-newsgroup-process-stack
4745     (error "Empty mark stack"))
4746   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4747
4748 (defun gnus-summary-process-mark-set (set)
4749   "Make SET into the current process marked articles."
4750   (gnus-summary-unmark-all-processable)
4751   (while set
4752     (gnus-summary-set-process-mark (pop set))))
4753
4754 ;;; Searching and stuff
4755
4756 (defun gnus-summary-search-group (&optional backward use-level)
4757   "Search for next unread newsgroup.
4758 If optional argument BACKWARD is non-nil, search backward instead."
4759   (save-excursion
4760     (set-buffer gnus-group-buffer)
4761     (when (gnus-group-search-forward
4762            backward nil (if use-level (gnus-group-group-level) nil))
4763       (gnus-group-group-name))))
4764
4765 (defun gnus-summary-best-group (&optional exclude-group)
4766   "Find the name of the best unread group.
4767 If EXCLUDE-GROUP, do not go to this group."
4768   (save-excursion
4769     (set-buffer gnus-group-buffer)
4770     (save-excursion
4771       (gnus-group-best-unread-group exclude-group))))
4772
4773 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4774   (if backward (gnus-summary-find-prev)
4775     (let* ((dummy (gnus-summary-article-intangible-p))
4776            (article (or article (gnus-summary-article-number)))
4777            (arts (gnus-data-find-list article))
4778            result)
4779       (when (and (not dummy)
4780                  (or (not gnus-summary-check-current)
4781                      (not unread)
4782                      (not (gnus-data-unread-p (car arts)))))
4783         (setq arts (cdr arts)))
4784       (when (setq result
4785                   (if unread
4786                       (progn
4787                         (while arts
4788                           (when (or (and undownloaded
4789                                          (eq gnus-undownloaded-mark
4790                                              (gnus-data-mark (car arts))))
4791                                     (gnus-data-unread-p (car arts)))
4792                             (setq result (car arts)
4793                                   arts nil))
4794                           (setq arts (cdr arts)))
4795                         result)
4796                     (car arts)))
4797         (goto-char (gnus-data-pos result))
4798         (gnus-data-number result)))))
4799
4800 (defun gnus-summary-find-prev (&optional unread article)
4801   (let* ((eobp (eobp))
4802          (article (or article (gnus-summary-article-number)))
4803          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4804          result)
4805     (when (and (not eobp)
4806                (or (not gnus-summary-check-current)
4807                    (not unread)
4808                    (not (gnus-data-unread-p (car arts)))))
4809       (setq arts (cdr arts)))
4810     (when (setq result
4811                 (if unread
4812                     (progn
4813                       (while arts
4814                         (when (gnus-data-unread-p (car arts))
4815                           (setq result (car arts)
4816                                 arts nil))
4817                         (setq arts (cdr arts)))
4818                       result)
4819                   (car arts)))
4820       (goto-char (gnus-data-pos result))
4821       (gnus-data-number result))))
4822
4823 (defun gnus-summary-find-subject (subject &optional unread backward article)
4824   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4825          (article (or article (gnus-summary-article-number)))
4826          (articles (gnus-data-list backward))
4827          (arts (gnus-data-find-list article articles))
4828          result)
4829     (when (or (not gnus-summary-check-current)
4830               (not unread)
4831               (not (gnus-data-unread-p (car arts))))
4832       (setq arts (cdr arts)))
4833     (while arts
4834       (and (or (not unread)
4835                (gnus-data-unread-p (car arts)))
4836            (vectorp (gnus-data-header (car arts)))
4837            (gnus-subject-equal
4838             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4839            (setq result (car arts)
4840                  arts nil))
4841       (setq arts (cdr arts)))
4842     (and result
4843          (goto-char (gnus-data-pos result))
4844          (gnus-data-number result))))
4845
4846 (defun gnus-summary-search-forward (&optional unread subject backward)
4847   "Search forward for an article.
4848 If UNREAD, look for unread articles.  If SUBJECT, look for
4849 articles with that subject.  If BACKWARD, search backward instead."
4850   (cond (subject (gnus-summary-find-subject subject unread backward))
4851         (backward (gnus-summary-find-prev unread))
4852         (t (gnus-summary-find-next unread))))
4853
4854 (defun gnus-recenter (&optional n)
4855   "Center point in window and redisplay frame.
4856 Also do horizontal recentering."
4857   (interactive "P")
4858   (when (and gnus-auto-center-summary
4859              (not (eq gnus-auto-center-summary 'vertical)))
4860     (gnus-horizontal-recenter))
4861   (recenter n))
4862
4863 (defun gnus-summary-recenter ()
4864   "Center point in the summary window.
4865 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4866 displayed, no centering will be performed."
4867   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4868   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4869   (let* ((top (cond ((< (window-height) 4) 0)
4870                     ((< (window-height) 7) 1)
4871                     (t 2)))
4872          (height (1- (window-height)))
4873          (bottom (save-excursion (goto-char (point-max))
4874                                  (forward-line (- height))
4875                                  (point)))
4876          (window (get-buffer-window (current-buffer))))
4877     ;; The user has to want it.
4878     (when gnus-auto-center-summary
4879       (when (get-buffer-window gnus-article-buffer)
4880         ;; Only do recentering when the article buffer is displayed,
4881         ;; Set the window start to either `bottom', which is the biggest
4882         ;; possible valid number, or the second line from the top,
4883         ;; whichever is the least.
4884         (set-window-start
4885          window (min bottom (save-excursion
4886                               (forward-line (- top)) (point)))))
4887       ;; Do horizontal recentering while we're at it.
4888       (when (and (get-buffer-window (current-buffer) t)
4889                  (not (eq gnus-auto-center-summary 'vertical)))
4890         (let ((selected (selected-window)))
4891           (select-window (get-buffer-window (current-buffer) t))
4892           (gnus-summary-position-point)
4893           (gnus-horizontal-recenter)
4894           (select-window selected))))))
4895
4896 (defun gnus-summary-jump-to-group (newsgroup)
4897   "Move point to NEWSGROUP in group mode buffer."
4898   ;; Keep update point of group mode buffer if visible.
4899   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4900       (save-window-excursion
4901         ;; Take care of tree window mode.
4902         (when (get-buffer-window gnus-group-buffer)
4903           (pop-to-buffer gnus-group-buffer))
4904         (gnus-group-jump-to-group newsgroup))
4905     (save-excursion
4906       ;; Take care of tree window mode.
4907       (if (get-buffer-window gnus-group-buffer)
4908           (pop-to-buffer gnus-group-buffer)
4909         (set-buffer gnus-group-buffer))
4910       (gnus-group-jump-to-group newsgroup))))
4911
4912 ;; This function returns a list of article numbers based on the
4913 ;; difference between the ranges of read articles in this group and
4914 ;; the range of active articles.
4915 (defun gnus-list-of-unread-articles (group)
4916   (let* ((read (gnus-info-read (gnus-get-info group)))
4917          (active (or (gnus-active group) (gnus-activate-group group)))
4918          (last (cdr active))
4919          first nlast unread)
4920     ;; If none are read, then all are unread.
4921     (if (not read)
4922         (setq first (car active))
4923       ;; If the range of read articles is a single range, then the
4924       ;; first unread article is the article after the last read
4925       ;; article.  Sounds logical, doesn't it?
4926       (if (not (listp (cdr read)))
4927           (setq first (max (car active) (1+ (cdr read))))
4928         ;; `read' is a list of ranges.
4929         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
4930                                   (caar read)))
4931                   1)
4932           (setq first (car active)))
4933         (while read
4934           (when first
4935             (while (< first nlast)
4936               (push first unread)
4937               (setq first (1+ first))))
4938           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4939           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4940           (setq read (cdr read)))))
4941     ;; And add the last unread articles.
4942     (while (<= first last)
4943       (push first unread)
4944       (setq first (1+ first)))
4945     ;; Return the list of unread articles.
4946     (delq 0 (nreverse unread))))
4947
4948 (defun gnus-list-of-read-articles (group)
4949   "Return a list of unread, unticked and non-dormant articles."
4950   (let* ((info (gnus-get-info group))
4951          (marked (gnus-info-marks info))
4952          (active (gnus-active group)))
4953     (and info active
4954          (gnus-set-difference
4955           (gnus-sorted-complement
4956            (gnus-uncompress-range active)
4957            (gnus-list-of-unread-articles group))
4958           (append
4959            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4960            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4961
4962 ;; Various summary commands
4963
4964 (defun gnus-summary-select-article-buffer ()
4965   "Reconfigure windows to show article buffer."
4966   (interactive)
4967   (if (not (gnus-buffer-live-p gnus-article-buffer))
4968       (error "There is no article buffer for this summary buffer")
4969     (gnus-configure-windows 'article)
4970     (select-window (get-buffer-window gnus-article-buffer))))
4971
4972 (defun gnus-summary-universal-argument (arg)
4973   "Perform any operation on all articles that are process/prefixed."
4974   (interactive "P")
4975   (let ((articles (gnus-summary-work-articles arg))
4976         func article)
4977     (if (eq
4978          (setq
4979           func
4980           (key-binding
4981            (read-key-sequence
4982             (substitute-command-keys
4983              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4984              ))))
4985          'undefined)
4986         (gnus-error 1 "Undefined key")
4987       (save-excursion
4988         (while articles
4989           (gnus-summary-goto-subject (setq article (pop articles)))
4990           (let (gnus-newsgroup-processable)
4991             (command-execute func))
4992           (gnus-summary-remove-process-mark article)))))
4993   (gnus-summary-position-point))
4994
4995 (defun gnus-summary-toggle-truncation (&optional arg)
4996   "Toggle truncation of summary lines.
4997 With arg, turn line truncation on iff arg is positive."
4998   (interactive "P")
4999   (setq truncate-lines
5000         (if (null arg) (not truncate-lines)
5001           (> (prefix-numeric-value arg) 0)))
5002   (redraw-display))
5003
5004 (defun gnus-summary-reselect-current-group (&optional all rescan)
5005   "Exit and then reselect the current newsgroup.
5006 The prefix argument ALL means to select all articles."
5007   (interactive "P")
5008   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5009     (error "Ephemeral groups can't be reselected"))
5010   (let ((current-subject (gnus-summary-article-number))
5011         (group gnus-newsgroup-name))
5012     (setq gnus-newsgroup-begin nil)
5013     (gnus-summary-exit)
5014     ;; We have to adjust the point of group mode buffer because
5015     ;; point was moved to the next unread newsgroup by exiting.
5016     (gnus-summary-jump-to-group group)
5017     (when rescan
5018       (save-excursion
5019         (gnus-group-get-new-news-this-group 1)))
5020     (gnus-group-read-group all t)
5021     (gnus-summary-goto-subject current-subject nil t)))
5022
5023 (defun gnus-summary-rescan-group (&optional all)
5024   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5025   (interactive "P")
5026   (gnus-summary-reselect-current-group all t))
5027
5028 (defun gnus-summary-update-info (&optional non-destructive)
5029   (save-excursion
5030     (let ((group gnus-newsgroup-name))
5031       (when group
5032         (when gnus-newsgroup-kill-headers
5033           (setq gnus-newsgroup-killed
5034                 (gnus-compress-sequence
5035                  (nconc
5036                   (gnus-set-sorted-intersection
5037                    (gnus-uncompress-range gnus-newsgroup-killed)
5038                    (setq gnus-newsgroup-unselected
5039                          (sort gnus-newsgroup-unselected '<)))
5040                   (setq gnus-newsgroup-unreads
5041                         (sort gnus-newsgroup-unreads '<)))
5042                  t)))
5043         (unless (listp (cdr gnus-newsgroup-killed))
5044           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5045         (let ((headers gnus-newsgroup-headers))
5046           ;; Set the new ranges of read articles.
5047           (save-excursion
5048             (set-buffer gnus-group-buffer)
5049             (gnus-undo-force-boundary))
5050           (gnus-update-read-articles
5051            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5052           ;; Set the current article marks.
5053           (let ((gnus-newsgroup-scored 
5054                  (if (and (not gnus-save-score)
5055                           (not non-destructive))
5056                      nil
5057                    gnus-newsgroup-scored)))
5058             (save-excursion
5059               (gnus-update-marks)))
5060           ;; Do the cross-ref thing.
5061           (when gnus-use-cross-reference
5062             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5063           ;; Do not switch windows but change the buffer to work.
5064           (set-buffer gnus-group-buffer)
5065           (unless (gnus-ephemeral-group-p group)
5066             (gnus-group-update-group group)))))))
5067
5068 (defun gnus-summary-save-newsrc (&optional force)
5069   "Save the current number of read/marked articles in the dribble buffer.
5070 The dribble buffer will then be saved.
5071 If FORCE (the prefix), also save the .newsrc file(s)."
5072   (interactive "P")
5073   (gnus-summary-update-info t)
5074   (if force
5075       (gnus-save-newsrc-file)
5076     (gnus-dribble-save)))
5077
5078 (defun gnus-summary-exit (&optional temporary)
5079   "Exit reading current newsgroup, and then return to group selection mode.
5080 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5081   (interactive)
5082   (gnus-set-global-variables)
5083   (gnus-kill-save-kill-buffer)
5084   (gnus-async-halt-prefetch)
5085   (let* ((group gnus-newsgroup-name)
5086          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5087          (mode major-mode)
5088          (group-point nil)
5089          (buf (current-buffer)))
5090     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5091     ;; If we have several article buffers, we kill them at exit.
5092     (unless gnus-single-article-buffer
5093       (gnus-kill-buffer gnus-original-article-buffer)
5094       (setq gnus-article-current nil))
5095     (when gnus-use-cache
5096       (gnus-cache-possibly-remove-articles)
5097       (gnus-cache-save-buffers))
5098     (gnus-async-prefetch-remove-group group)
5099     (when gnus-suppress-duplicates
5100       (gnus-dup-enter-articles))
5101     (when gnus-use-trees
5102       (gnus-tree-close group))
5103     ;; Remove entries for this group.
5104     (nnmail-purge-split-history (gnus-group-real-name group))
5105     ;; Make all changes in this group permanent.
5106     (unless quit-config
5107       (gnus-run-hooks 'gnus-exit-group-hook)
5108       (gnus-summary-update-info)
5109       ;; Do adaptive scoring, and possibly save score files.
5110       (when gnus-newsgroup-adaptive
5111         (gnus-score-adaptive))
5112       (when gnus-use-scoring
5113         (gnus-score-save)))
5114     (gnus-close-group group)
5115     ;; Make sure where we were, and go to next newsgroup.
5116     (set-buffer gnus-group-buffer)
5117     (unless quit-config
5118       (gnus-group-jump-to-group group))
5119     (gnus-run-hooks 'gnus-summary-exit-hook)
5120     (unless (or quit-config
5121                 ;; If this group has disappeared from the summary
5122                 ;; buffer, don't skip forwards.
5123                 (not (string= group (gnus-group-group-name))))
5124       (gnus-group-next-unread-group 1))
5125     (setq group-point (point))
5126     (if temporary
5127         nil                             ;Nothing to do.
5128       ;; If we have several article buffers, we kill them at exit.
5129       (unless gnus-single-article-buffer
5130         (gnus-kill-buffer gnus-article-buffer)
5131         (gnus-kill-buffer gnus-original-article-buffer)
5132         (setq gnus-article-current nil))
5133       (set-buffer buf)
5134       (if (not gnus-kill-summary-on-exit)
5135           (gnus-deaden-summary)
5136         ;; We set all buffer-local variables to nil.  It is unclear why
5137         ;; this is needed, but if we don't, buffer-local variables are
5138         ;; not garbage-collected, it seems.  This would the lead to en
5139         ;; ever-growing Emacs.
5140         (gnus-summary-clear-local-variables)
5141         (when (get-buffer gnus-article-buffer)
5142           (bury-buffer gnus-article-buffer))
5143         ;; We clear the global counterparts of the buffer-local
5144         ;; variables as well, just to be on the safe side.
5145         (set-buffer gnus-group-buffer)
5146         (gnus-summary-clear-local-variables)
5147         ;; Return to group mode buffer.
5148         (when (eq mode 'gnus-summary-mode)
5149           (gnus-kill-buffer buf)))
5150       (setq gnus-current-select-method gnus-select-method)
5151       (pop-to-buffer gnus-group-buffer)
5152       ;; Clear the current group name.
5153       (if (not quit-config)
5154           (progn
5155             (goto-char group-point)
5156             (gnus-configure-windows 'group 'force))
5157         (gnus-handle-ephemeral-exit quit-config))
5158       (unless quit-config
5159         (setq gnus-newsgroup-name nil)))))
5160
5161 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5162 (defun gnus-summary-exit-no-update (&optional no-questions)
5163   "Quit reading current newsgroup without updating read article info."
5164   (interactive)
5165   (let* ((group gnus-newsgroup-name)
5166          (quit-config (gnus-group-quit-config group)))
5167     (when (or no-questions
5168               gnus-expert-user
5169               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5170       (gnus-async-halt-prefetch)
5171       (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5172       ;; If we have several article buffers, we kill them at exit.
5173       (unless gnus-single-article-buffer
5174         (gnus-kill-buffer gnus-article-buffer)
5175         (gnus-kill-buffer gnus-original-article-buffer)
5176         (setq gnus-article-current nil))
5177       (if (not gnus-kill-summary-on-exit)
5178           (gnus-deaden-summary)
5179         (gnus-close-group group)
5180         (gnus-summary-clear-local-variables)
5181         (set-buffer gnus-group-buffer)
5182         (gnus-summary-clear-local-variables)
5183         (when (get-buffer gnus-summary-buffer)
5184           (kill-buffer gnus-summary-buffer)))
5185       (unless gnus-single-article-buffer
5186         (setq gnus-article-current nil))
5187       (when gnus-use-trees
5188         (gnus-tree-close group))
5189       (gnus-async-prefetch-remove-group group)
5190       (when (get-buffer gnus-article-buffer)
5191         (bury-buffer gnus-article-buffer))
5192       ;; Return to the group buffer.
5193       (gnus-configure-windows 'group 'force)
5194       ;; Clear the current group name.
5195       (setq gnus-newsgroup-name nil)
5196       (when (equal (gnus-group-group-name) group)
5197         (gnus-group-next-unread-group 1))
5198       (when quit-config
5199         (gnus-handle-ephemeral-exit quit-config)))))
5200
5201 (defun gnus-handle-ephemeral-exit (quit-config)
5202   "Handle movement when leaving an ephemeral group.
5203 The state which existed when entering the ephemeral is reset."
5204   (if (not (buffer-name (car quit-config)))
5205       (gnus-configure-windows 'group 'force)
5206     (set-buffer (car quit-config))
5207     (cond ((eq major-mode 'gnus-summary-mode)
5208            (gnus-set-global-variables))
5209           ((eq major-mode 'gnus-article-mode)
5210            (save-excursion
5211              ;; The `gnus-summary-buffer' variable may point
5212              ;; to the old summary buffer when using a single
5213              ;; article buffer.
5214              (unless (gnus-buffer-live-p gnus-summary-buffer)
5215                (set-buffer gnus-group-buffer))
5216              (set-buffer gnus-summary-buffer)
5217              (gnus-set-global-variables))))
5218     (if (or (eq (cdr quit-config) 'article)
5219             (eq (cdr quit-config) 'pick))
5220         (progn
5221           ;; The current article may be from the ephemeral group
5222           ;; thus it is best that we reload this article
5223           (gnus-summary-show-article)
5224           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5225               (gnus-configure-windows 'pick 'force)
5226             (gnus-configure-windows (cdr quit-config) 'force)))
5227       (gnus-configure-windows (cdr quit-config) 'force))
5228     (when (eq major-mode 'gnus-summary-mode)
5229       (gnus-summary-next-subject 1 nil t)
5230       (gnus-summary-recenter)
5231       (gnus-summary-position-point))))
5232
5233 (defun gnus-summary-preview-mime-message (arg)
5234   "MIME decode and play this message."
5235   (interactive "P")
5236   (or gnus-show-mime
5237       (let ((gnus-break-pages nil)
5238             (gnus-show-mime t))
5239         (gnus-summary-select-article t t)
5240         ))
5241   (select-window (get-buffer-window gnus-article-buffer))
5242   )
5243
5244 ;;; Dead summaries.
5245
5246 (defvar gnus-dead-summary-mode-map nil)
5247
5248 (unless gnus-dead-summary-mode-map
5249   (setq gnus-dead-summary-mode-map (make-keymap))
5250   (suppress-keymap gnus-dead-summary-mode-map)
5251   (substitute-key-definition
5252    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5253   (let ((keys '("\C-d" "\r" "\177" [delete])))
5254     (while keys
5255       (define-key gnus-dead-summary-mode-map
5256         (pop keys) 'gnus-summary-wake-up-the-dead))))
5257
5258 (defvar gnus-dead-summary-mode nil
5259   "Minor mode for Gnus summary buffers.")
5260
5261 (defun gnus-dead-summary-mode (&optional arg)
5262   "Minor mode for Gnus summary buffers."
5263   (interactive "P")
5264   (when (eq major-mode 'gnus-summary-mode)
5265     (make-local-variable 'gnus-dead-summary-mode)
5266     (setq gnus-dead-summary-mode
5267           (if (null arg) (not gnus-dead-summary-mode)
5268             (> (prefix-numeric-value arg) 0)))
5269     (when gnus-dead-summary-mode
5270       (gnus-add-minor-mode
5271        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5272
5273 (defun gnus-deaden-summary ()
5274   "Make the current summary buffer into a dead summary buffer."
5275   ;; Kill any previous dead summary buffer.
5276   (when (and gnus-dead-summary
5277              (buffer-name gnus-dead-summary))
5278     (save-excursion
5279       (set-buffer gnus-dead-summary)
5280       (when gnus-dead-summary-mode
5281         (kill-buffer (current-buffer)))))
5282   ;; Make this the current dead summary.
5283   (setq gnus-dead-summary (current-buffer))
5284   (gnus-dead-summary-mode 1)
5285   (let ((name (buffer-name)))
5286     (when (string-match "Summary" name)
5287       (rename-buffer
5288        (concat (substring name 0 (match-beginning 0)) "Dead "
5289                (substring name (match-beginning 0)))
5290        t))))
5291
5292 (defun gnus-kill-or-deaden-summary (buffer)
5293   "Kill or deaden the summary BUFFER."
5294   (save-excursion
5295     (when (and (buffer-name buffer)
5296                (not gnus-single-article-buffer))
5297       (save-excursion
5298         (set-buffer buffer)
5299         (gnus-kill-buffer gnus-article-buffer)
5300         (gnus-kill-buffer gnus-original-article-buffer)))
5301     (cond (gnus-kill-summary-on-exit
5302            (when (and gnus-use-trees
5303                       (gnus-buffer-exists-p buffer))
5304              (save-excursion
5305                (set-buffer buffer)
5306                (gnus-tree-close gnus-newsgroup-name)))
5307            (gnus-kill-buffer buffer))
5308           ((gnus-buffer-exists-p buffer)
5309            (save-excursion
5310              (set-buffer buffer)
5311              (gnus-deaden-summary))))))
5312
5313 (defun gnus-summary-wake-up-the-dead (&rest args)
5314   "Wake up the dead summary buffer."
5315   (interactive)
5316   (gnus-dead-summary-mode -1)
5317   (let ((name (buffer-name)))
5318     (when (string-match "Dead " name)
5319       (rename-buffer
5320        (concat (substring name 0 (match-beginning 0))
5321                (substring name (match-end 0)))
5322        t)))
5323   (gnus-message 3 "This dead summary is now alive again"))
5324
5325 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5326 (defun gnus-summary-fetch-faq (&optional faq-dir)
5327   "Fetch the FAQ for the current group.
5328 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5329 in."
5330   (interactive
5331    (list
5332     (when current-prefix-arg
5333       (completing-read
5334        "Faq dir: " (and (listp gnus-group-faq-directory)
5335                         (mapcar (lambda (file) (list file))
5336                                 gnus-group-faq-directory))))))
5337   (let (gnus-faq-buffer)
5338     (when (setq gnus-faq-buffer
5339                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5340       (gnus-configure-windows 'summary-faq))))
5341
5342 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5343 (defun gnus-summary-describe-group (&optional force)
5344   "Describe the current newsgroup."
5345   (interactive "P")
5346   (gnus-group-describe-group force gnus-newsgroup-name))
5347
5348 (defun gnus-summary-describe-briefly ()
5349   "Describe summary mode commands briefly."
5350   (interactive)
5351   (gnus-message 6
5352                 (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")))
5353
5354 ;; Walking around group mode buffer from summary mode.
5355
5356 (defun gnus-summary-next-group (&optional no-article target-group backward)
5357   "Exit current newsgroup and then select next unread newsgroup.
5358 If prefix argument NO-ARTICLE is non-nil, no article is selected
5359 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5360 previous group instead."
5361   (interactive "P")
5362   ;; Stop pre-fetching.
5363   (gnus-async-halt-prefetch)
5364   (let ((current-group gnus-newsgroup-name)
5365         (current-buffer (current-buffer))
5366         entered)
5367     ;; First we semi-exit this group to update Xrefs and all variables.
5368     ;; We can't do a real exit, because the window conf must remain
5369     ;; the same in case the user is prompted for info, and we don't
5370     ;; want the window conf to change before that...
5371     (gnus-summary-exit t)
5372     (while (not entered)
5373       ;; Then we find what group we are supposed to enter.
5374       (set-buffer gnus-group-buffer)
5375       (gnus-group-jump-to-group current-group)
5376       (setq target-group
5377             (or target-group
5378                 (if (eq gnus-keep-same-level 'best)
5379                     (gnus-summary-best-group gnus-newsgroup-name)
5380                   (gnus-summary-search-group backward gnus-keep-same-level))))
5381       (if (not target-group)
5382           ;; There are no further groups, so we return to the group
5383           ;; buffer.
5384           (progn
5385             (gnus-message 5 "Returning to the group buffer")
5386             (setq entered t)
5387             (when (gnus-buffer-live-p current-buffer)
5388               (set-buffer current-buffer)
5389               (gnus-summary-exit))
5390             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5391         ;; We try to enter the target group.
5392         (gnus-group-jump-to-group target-group)
5393         (let ((unreads (gnus-group-group-unread)))
5394           (if (and (or (eq t unreads)
5395                        (and unreads (not (zerop unreads))))
5396                    (gnus-summary-read-group
5397                     target-group nil no-article
5398                     (and (buffer-name current-buffer) current-buffer)
5399                     nil backward))
5400               (setq entered t)
5401             (setq current-group target-group
5402                   target-group nil)))))))
5403
5404 (defun gnus-summary-prev-group (&optional no-article)
5405   "Exit current newsgroup and then select previous unread newsgroup.
5406 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5407   (interactive "P")
5408   (gnus-summary-next-group no-article nil t))
5409
5410 ;; Walking around summary lines.
5411
5412 (defun gnus-summary-first-subject (&optional unread undownloaded)
5413   "Go to the first unread subject.
5414 If UNREAD is non-nil, go to the first unread article.
5415 Returns the article selected or nil if there are no unread articles."
5416   (interactive "P")
5417   (prog1
5418       (cond
5419        ;; Empty summary.
5420        ((null gnus-newsgroup-data)
5421         (gnus-message 3 "No articles in the group")
5422         nil)
5423        ;; Pick the first article.
5424        ((not unread)
5425         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5426         (gnus-data-number (car gnus-newsgroup-data)))
5427        ;; No unread articles.
5428        ((null gnus-newsgroup-unreads)
5429         (gnus-message 3 "No more unread articles")
5430         nil)
5431        ;; Find the first unread article.
5432        (t
5433         (let ((data gnus-newsgroup-data))
5434           (while (and data
5435                       (and (not (and undownloaded
5436                                      (eq gnus-undownloaded-mark
5437                                          (gnus-data-mark (car data)))))
5438                            (not (gnus-data-unread-p (car data)))))
5439             (setq data (cdr data)))
5440           (when data
5441             (goto-char (gnus-data-pos (car data)))
5442             (gnus-data-number (car data))))))
5443     (gnus-summary-position-point)))
5444
5445 (defun gnus-summary-next-subject (n &optional unread dont-display)
5446   "Go to next N'th summary line.
5447 If N is negative, go to the previous N'th subject line.
5448 If UNREAD is non-nil, only unread articles are selected.
5449 The difference between N and the actual number of steps taken is
5450 returned."
5451   (interactive "p")
5452   (let ((backward (< n 0))
5453         (n (abs n)))
5454     (while (and (> n 0)
5455                 (if backward
5456                     (gnus-summary-find-prev unread)
5457                   (gnus-summary-find-next unread)))
5458       (gnus-summary-show-thread)
5459       (setq n (1- n)))
5460     (when (/= 0 n)
5461       (gnus-message 7 "No more%s articles"
5462                     (if unread " unread" "")))
5463     (unless dont-display
5464       (gnus-summary-recenter)
5465       (gnus-summary-position-point))
5466     n))
5467
5468 (defun gnus-summary-next-unread-subject (n)
5469   "Go to next N'th unread summary line."
5470   (interactive "p")
5471   (gnus-summary-next-subject n t))
5472
5473 (defun gnus-summary-prev-subject (n &optional unread)
5474   "Go to previous N'th summary line.
5475 If optional argument UNREAD is non-nil, only unread article is selected."
5476   (interactive "p")
5477   (gnus-summary-next-subject (- n) unread))
5478
5479 (defun gnus-summary-prev-unread-subject (n)
5480   "Go to previous N'th unread summary line."
5481   (interactive "p")
5482   (gnus-summary-next-subject (- n) t))
5483
5484 (defun gnus-summary-goto-subject (article &optional force silent)
5485   "Go the subject line of ARTICLE.
5486 If FORCE, also allow jumping to articles not currently shown."
5487   (interactive "nArticle number: ")
5488   (let ((b (point))
5489         (data (gnus-data-find article)))
5490     ;; We read in the article if we have to.
5491     (and (not data)
5492          force
5493          (gnus-summary-insert-subject
5494           article
5495           (if (or (numberp force) (vectorp force)) force)
5496           t)
5497          (setq data (gnus-data-find article)))
5498     (goto-char b)
5499     (if (not data)
5500         (progn
5501           (unless silent
5502             (gnus-message 3 "Can't find article %d" article))
5503           nil)
5504       (goto-char (gnus-data-pos data))
5505       (gnus-summary-position-point)
5506       article)))
5507
5508 ;; Walking around summary lines with displaying articles.
5509
5510 (defun gnus-summary-expand-window (&optional arg)
5511   "Make the summary buffer take up the entire Emacs frame.
5512 Given a prefix, will force an `article' buffer configuration."
5513   (interactive "P")
5514   (if arg
5515       (gnus-configure-windows 'article 'force)
5516     (gnus-configure-windows 'summary 'force)))
5517
5518 (defun gnus-summary-display-article (article &optional all-header)
5519   "Display ARTICLE in article buffer."
5520   (gnus-set-global-variables)
5521   (if (null article)
5522       nil
5523     (prog1
5524         (if gnus-summary-display-article-function
5525             (funcall gnus-summary-display-article-function article all-header)
5526           (gnus-article-prepare article all-header))
5527       (gnus-run-hooks 'gnus-select-article-hook)
5528       (when (and gnus-current-article
5529                  (not (zerop gnus-current-article)))
5530         (gnus-summary-goto-subject gnus-current-article))
5531       (gnus-summary-recenter)
5532       (when (and gnus-use-trees gnus-show-threads)
5533         (gnus-possibly-generate-tree article)
5534         (gnus-highlight-selected-tree article))
5535       ;; Successfully display article.
5536       (gnus-article-set-window-start
5537        (cdr (assq article gnus-newsgroup-bookmarks))))))
5538
5539 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5540   "Select the current article.
5541 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5542 non-nil, the article will be re-fetched even if it already present in
5543 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5544 be displayed."
5545   ;; Make sure we are in the summary buffer to work around bbdb bug.
5546   (unless (eq major-mode 'gnus-summary-mode)
5547     (set-buffer gnus-summary-buffer))
5548   (let ((article (or article (gnus-summary-article-number)))
5549         (all-headers (not (not all-headers))) ;Must be T or NIL.
5550         gnus-summary-display-article-function
5551         did)
5552     (and (not pseudo)
5553          (gnus-summary-article-pseudo-p article)
5554          (error "This is a pseudo-article"))
5555     (prog1
5556         (save-excursion
5557           (set-buffer gnus-summary-buffer)
5558           (if (or (and gnus-single-article-buffer
5559                        (or (null gnus-current-article)
5560                            (null gnus-article-current)
5561                            (null (get-buffer gnus-article-buffer))
5562                            (not (eq article (cdr gnus-article-current)))
5563                            (not (equal (car gnus-article-current)
5564                                        gnus-newsgroup-name))))
5565                   (and (not gnus-single-article-buffer)
5566                        (or (null gnus-current-article)
5567                            (not (eq gnus-current-article article))))
5568                   force)
5569               ;; The requested article is different from the current article.
5570               (prog1
5571                   (gnus-summary-display-article article all-headers)
5572                 (setq did article))
5573             (when (or all-headers gnus-show-all-headers)
5574               (gnus-article-show-all-headers))
5575             'old))
5576       (when did
5577         (gnus-article-set-window-start
5578          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5579
5580 (defun gnus-summary-set-current-mark (&optional current-mark)
5581   "Obsolete function."
5582   nil)
5583
5584 (defun gnus-summary-next-article (&optional unread subject backward push)
5585   "Select the next article.
5586 If UNREAD, only unread articles are selected.
5587 If SUBJECT, only articles with SUBJECT are selected.
5588 If BACKWARD, the previous article is selected instead of the next."
5589   (interactive "P")
5590   (cond
5591    ;; Is there such an article?
5592    ((and (gnus-summary-search-forward unread subject backward)
5593          (or (gnus-summary-display-article (gnus-summary-article-number))
5594              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5595     (gnus-summary-position-point))
5596    ;; If not, we try the first unread, if that is wanted.
5597    ((and subject
5598          gnus-auto-select-same
5599          (gnus-summary-first-unread-article))
5600     (gnus-summary-position-point)
5601     (gnus-message 6 "Wrapped"))
5602    ;; Try to get next/previous article not displayed in this group.
5603    ((and gnus-auto-extend-newsgroup
5604          (not unread) (not subject))
5605     (gnus-summary-goto-article
5606      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5607      nil (count-lines (point-min) (point))))
5608    ;; Go to next/previous group.
5609    (t
5610     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5611       (gnus-summary-jump-to-group gnus-newsgroup-name))
5612     (let ((cmd last-command-char)
5613           (point
5614            (save-excursion
5615              (set-buffer gnus-group-buffer)
5616              (point)))
5617           (group
5618            (if (eq gnus-keep-same-level 'best)
5619                (gnus-summary-best-group gnus-newsgroup-name)
5620              (gnus-summary-search-group backward gnus-keep-same-level))))
5621       ;; For some reason, the group window gets selected.  We change
5622       ;; it back.
5623       (select-window (get-buffer-window (current-buffer)))
5624       ;; Select next unread newsgroup automagically.
5625       (cond
5626        ((or (not gnus-auto-select-next)
5627             (not cmd))
5628         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5629        ((or (eq gnus-auto-select-next 'quietly)
5630             (and (eq gnus-auto-select-next 'slightly-quietly)
5631                  push)
5632             (and (eq gnus-auto-select-next 'almost-quietly)
5633                  (gnus-summary-last-article-p)))
5634         ;; Select quietly.
5635         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5636             (gnus-summary-exit)
5637           (gnus-message 7 "No more%s articles (%s)..."
5638                         (if unread " unread" "")
5639                         (if group (concat "selecting " group)
5640                           "exiting"))
5641           (gnus-summary-next-group nil group backward)))
5642        (t
5643         (when (gnus-key-press-event-p last-input-event)
5644           (gnus-summary-walk-group-buffer
5645            gnus-newsgroup-name cmd unread backward point))))))))
5646
5647 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5648   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5649                       (?\C-p (gnus-group-prev-unread-group 1))))
5650         (cursor-in-echo-area t)
5651         keve key group ended)
5652     (save-excursion
5653       (set-buffer gnus-group-buffer)
5654       (goto-char start)
5655       (setq group
5656             (if (eq gnus-keep-same-level 'best)
5657                 (gnus-summary-best-group gnus-newsgroup-name)
5658               (gnus-summary-search-group backward gnus-keep-same-level))))
5659     (while (not ended)
5660       (gnus-message
5661        5 "No more%s articles%s" (if unread " unread" "")
5662        (if (and group
5663                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5664            (format " (Type %s for %s [%s])"
5665                    (single-key-description cmd) group
5666                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5667          (format " (Type %s to exit %s)"
5668                  (single-key-description cmd)
5669                  gnus-newsgroup-name)))
5670       ;; Confirm auto selection.
5671       (setq key (car (setq keve (gnus-read-event-char))))
5672       (setq ended t)
5673       (cond
5674        ((assq key keystrokes)
5675         (let ((obuf (current-buffer)))
5676           (switch-to-buffer gnus-group-buffer)
5677           (when group
5678             (gnus-group-jump-to-group group))
5679           (eval (cadr (assq key keystrokes)))
5680           (setq group (gnus-group-group-name))
5681           (switch-to-buffer obuf))
5682         (setq ended nil))
5683        ((equal key cmd)
5684         (if (or (not group)
5685                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5686             (gnus-summary-exit)
5687           (gnus-summary-next-group nil group backward)))
5688        (t
5689         (push (cdr keve) unread-command-events))))))
5690
5691 (defun gnus-summary-next-unread-article ()
5692   "Select unread article after current one."
5693   (interactive)
5694   (gnus-summary-next-article
5695    (or (not (eq gnus-summary-goto-unread 'never))
5696        (gnus-summary-last-article-p (gnus-summary-article-number)))
5697    (and gnus-auto-select-same
5698         (gnus-summary-article-subject))))
5699
5700 (defun gnus-summary-prev-article (&optional unread subject)
5701   "Select the article after the current one.
5702 If UNREAD is non-nil, only unread articles are selected."
5703   (interactive "P")
5704   (gnus-summary-next-article unread subject t))
5705
5706 (defun gnus-summary-prev-unread-article ()
5707   "Select unread article before current one."
5708   (interactive)
5709   (gnus-summary-prev-article
5710    (or (not (eq gnus-summary-goto-unread 'never))
5711        (gnus-summary-first-article-p (gnus-summary-article-number)))
5712    (and gnus-auto-select-same
5713         (gnus-summary-article-subject))))
5714
5715 (defun gnus-summary-next-page (&optional lines circular)
5716   "Show next page of the selected article.
5717 If at the end of the current article, select the next article.
5718 LINES says how many lines should be scrolled up.
5719
5720 If CIRCULAR is non-nil, go to the start of the article instead of
5721 selecting the next article when reaching the end of the current
5722 article."
5723   (interactive "P")
5724   (setq gnus-summary-buffer (current-buffer))
5725   (gnus-set-global-variables)
5726   (let ((article (gnus-summary-article-number))
5727         (article-window (get-buffer-window gnus-article-buffer t))
5728         endp)
5729     ;; If the buffer is empty, we have no article.
5730     (unless article
5731       (error "No article to select"))
5732     (gnus-configure-windows 'article)
5733     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5734         (if (and (eq gnus-summary-goto-unread 'never)
5735                  (not (gnus-summary-last-article-p article)))
5736             (gnus-summary-next-article)
5737           (gnus-summary-next-unread-article))
5738       (if (or (null gnus-current-article)
5739               (null gnus-article-current)
5740               (/= article (cdr gnus-article-current))
5741               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5742           ;; Selected subject is different from current article's.
5743           (gnus-summary-display-article article)
5744         (when article-window
5745           (gnus-eval-in-buffer-window gnus-article-buffer
5746             (setq endp (gnus-article-next-page lines)))
5747           (when endp
5748             (cond (circular
5749                    (gnus-summary-beginning-of-article))
5750                   (lines
5751                    (gnus-message 3 "End of message"))
5752                   ((null lines)
5753                    (if (and (eq gnus-summary-goto-unread 'never)
5754                             (not (gnus-summary-last-article-p article)))
5755                        (gnus-summary-next-article)
5756                      (gnus-summary-next-unread-article))))))))
5757     (gnus-summary-recenter)
5758     (gnus-summary-position-point)))
5759
5760 (defun gnus-summary-prev-page (&optional lines move)
5761   "Show previous page of selected article.
5762 Argument LINES specifies lines to be scrolled down.
5763 If MOVE, move to the previous unread article if point is at
5764 the beginning of the buffer."
5765   (interactive "P")
5766   (let ((article (gnus-summary-article-number))
5767         (article-window (get-buffer-window gnus-article-buffer t))
5768         endp)
5769     (gnus-configure-windows 'article)
5770     (if (or (null gnus-current-article)
5771             (null gnus-article-current)
5772             (/= article (cdr gnus-article-current))
5773             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5774         ;; Selected subject is different from current article's.
5775         (gnus-summary-display-article article)
5776       (gnus-summary-recenter)
5777       (when article-window
5778         (gnus-eval-in-buffer-window gnus-article-buffer
5779           (setq endp (gnus-article-prev-page lines)))
5780         (when (and move endp)
5781           (cond (lines
5782                  (gnus-message 3 "Beginning of message"))
5783                 ((null lines)
5784                  (if (and (eq gnus-summary-goto-unread 'never)
5785                           (not (gnus-summary-first-article-p article)))
5786                      (gnus-summary-prev-article)
5787                    (gnus-summary-prev-unread-article))))))))
5788   (gnus-summary-position-point))
5789
5790 (defun gnus-summary-prev-page-or-article (&optional lines)
5791   "Show previous page of selected article.
5792 Argument LINES specifies lines to be scrolled down.
5793 If at the beginning of the article, go to the next article."
5794   (interactive "P")
5795   (gnus-summary-prev-page lines t))
5796
5797 (defun gnus-summary-scroll-up (lines)
5798   "Scroll up (or down) one line current article.
5799 Argument LINES specifies lines to be scrolled up (or down if negative)."
5800   (interactive "p")
5801   (gnus-configure-windows 'article)
5802   (gnus-summary-show-thread)
5803   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5804     (gnus-eval-in-buffer-window gnus-article-buffer
5805       (cond ((> lines 0)
5806              (when (gnus-article-next-page lines)
5807                (gnus-message 3 "End of message")))
5808             ((< lines 0)
5809              (gnus-article-prev-page (- lines))))))
5810   (gnus-summary-recenter)
5811   (gnus-summary-position-point))
5812
5813 (defun gnus-summary-scroll-down (lines)
5814   "Scroll down (or up) one line current article.
5815 Argument LINES specifies lines to be scrolled down (or up if negative)."
5816   (interactive "p")
5817   (gnus-summary-scroll-up (- lines)))
5818
5819 (defun gnus-summary-next-same-subject ()
5820   "Select next article which has the same subject as current one."
5821   (interactive)
5822   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5823
5824 (defun gnus-summary-prev-same-subject ()
5825   "Select previous article which has the same subject as current one."
5826   (interactive)
5827   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5828
5829 (defun gnus-summary-next-unread-same-subject ()
5830   "Select next unread article which has the same subject as current one."
5831   (interactive)
5832   (gnus-summary-next-article t (gnus-summary-article-subject)))
5833
5834 (defun gnus-summary-prev-unread-same-subject ()
5835   "Select previous unread article which has the same subject as current one."
5836   (interactive)
5837   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5838
5839 (defun gnus-summary-first-unread-article ()
5840   "Select the first unread article.
5841 Return nil if there are no unread articles."
5842   (interactive)
5843   (prog1
5844       (when (gnus-summary-first-subject t)
5845         (gnus-summary-show-thread)
5846         (gnus-summary-first-subject t)
5847         (gnus-summary-display-article (gnus-summary-article-number)))
5848     (gnus-summary-position-point)))
5849
5850 (defun gnus-summary-first-article ()
5851   "Select the first article.
5852 Return nil if there are no articles."
5853   (interactive)
5854   (prog1
5855       (when (gnus-summary-first-subject)
5856       (gnus-summary-show-thread)
5857       (gnus-summary-first-subject)
5858       (gnus-summary-display-article (gnus-summary-article-number)))
5859     (gnus-summary-position-point)))
5860
5861 (defun gnus-summary-best-unread-article ()
5862   "Select the unread article with the highest score."
5863   (interactive)
5864   (let ((best -1000000)
5865         (data gnus-newsgroup-data)
5866         article score)
5867     (while data
5868       (and (gnus-data-unread-p (car data))
5869            (> (setq score
5870                     (gnus-summary-article-score (gnus-data-number (car data))))
5871               best)
5872            (setq best score
5873                  article (gnus-data-number (car data))))
5874       (setq data (cdr data)))
5875     (prog1
5876         (if article
5877             (gnus-summary-goto-article article)
5878           (error "No unread articles"))
5879       (gnus-summary-position-point))))
5880
5881 (defun gnus-summary-last-subject ()
5882   "Go to the last displayed subject line in the group."
5883   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5884     (when article
5885       (gnus-summary-goto-subject article))))
5886
5887 (defun gnus-summary-goto-article (article &optional all-headers force)
5888   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5889 If ALL-HEADERS is non-nil, no header lines are hidden.
5890 If FORCE, go to the article even if it isn't displayed.  If FORCE
5891 is a number, it is the line the article is to be displayed on."
5892   (interactive
5893    (list
5894     (completing-read
5895      "Article number or Message-ID: "
5896      (mapcar (lambda (number) (list (int-to-string number)))
5897              gnus-newsgroup-limit))
5898     current-prefix-arg
5899     t))
5900   (prog1
5901       (if (and (stringp article)
5902                (string-match "@" article))
5903           (gnus-summary-refer-article article)
5904         (when (stringp article)
5905           (setq article (string-to-number article)))
5906         (if (gnus-summary-goto-subject article force)
5907             (gnus-summary-display-article article all-headers)
5908           (gnus-message 4 "Couldn't go to article %s" article) nil))
5909     (gnus-summary-position-point)))
5910
5911 (defun gnus-summary-goto-last-article ()
5912   "Go to the previously read article."
5913   (interactive)
5914   (prog1
5915       (when gnus-last-article
5916         (gnus-summary-goto-article gnus-last-article nil t))
5917     (gnus-summary-position-point)))
5918
5919 (defun gnus-summary-pop-article (number)
5920   "Pop one article off the history and go to the previous.
5921 NUMBER articles will be popped off."
5922   (interactive "p")
5923   (let (to)
5924     (setq gnus-newsgroup-history
5925           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5926     (if to
5927         (gnus-summary-goto-article (car to) nil t)
5928       (error "Article history empty")))
5929   (gnus-summary-position-point))
5930
5931 ;; Summary commands and functions for limiting the summary buffer.
5932
5933 (defun gnus-summary-limit-to-articles (n)
5934   "Limit the summary buffer to the next N articles.
5935 If not given a prefix, use the process marked articles instead."
5936   (interactive "P")
5937   (prog1
5938       (let ((articles (gnus-summary-work-articles n)))
5939         (setq gnus-newsgroup-processable nil)
5940         (gnus-summary-limit articles))
5941     (gnus-summary-position-point)))
5942
5943 (defun gnus-summary-pop-limit (&optional total)
5944   "Restore the previous limit.
5945 If given a prefix, remove all limits."
5946   (interactive "P")
5947   (when total
5948     (setq gnus-newsgroup-limits
5949           (list (mapcar (lambda (h) (mail-header-number h))
5950                         gnus-newsgroup-headers))))
5951   (unless gnus-newsgroup-limits
5952     (error "No limit to pop"))
5953   (prog1
5954       (gnus-summary-limit nil 'pop)
5955     (gnus-summary-position-point)))
5956
5957 (defun gnus-summary-limit-to-subject (subject &optional header)
5958   "Limit the summary buffer to articles that have subjects that match a regexp."
5959   (interactive "sLimit to subject (regexp): ")
5960   (unless header
5961     (setq header "subject"))
5962   (when (not (equal "" subject))
5963     (prog1
5964         (let ((articles (gnus-summary-find-matching
5965                          (or header "subject") subject 'all)))
5966           (unless articles
5967             (error "Found no matches for \"%s\"" subject))
5968           (gnus-summary-limit articles))
5969       (gnus-summary-position-point))))
5970
5971 (defun gnus-summary-limit-to-author (from)
5972   "Limit the summary buffer to articles that have authors that match a regexp."
5973   (interactive "sLimit to author (regexp): ")
5974   (gnus-summary-limit-to-subject from "from"))
5975
5976 (defun gnus-summary-limit-to-age (age &optional younger-p)
5977   "Limit the summary buffer to articles that are older than (or equal) AGE days.
5978 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5979 articles that are younger than AGE days."
5980   (interactive "nTime in days: \nP")
5981   (prog1
5982       (let ((data gnus-newsgroup-data)
5983             (cutoff (nnmail-days-to-time age))
5984             articles d date is-younger)
5985         (while (setq d (pop data))
5986           (when (and (vectorp (gnus-data-header d))
5987                      (setq date (mail-header-date (gnus-data-header d))))
5988             (setq is-younger (nnmail-time-less
5989                               (nnmail-time-since (nnmail-date-to-time date))
5990                               cutoff))
5991             (when (if younger-p
5992                       is-younger
5993                     (not is-younger))
5994               (push (gnus-data-number d) articles))))
5995         (gnus-summary-limit (nreverse articles)))
5996     (gnus-summary-position-point)))
5997
5998 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5999 (make-obsolete
6000  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6001
6002 (defun gnus-summary-limit-to-unread (&optional all)
6003   "Limit the summary buffer to articles that are not marked as read.
6004 If ALL is non-nil, limit strictly to unread articles."
6005   (interactive "P")
6006   (if all
6007       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6008     (gnus-summary-limit-to-marks
6009      ;; Concat all the marks that say that an article is read and have
6010      ;; those removed.
6011      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6012            gnus-killed-mark gnus-kill-file-mark
6013            gnus-low-score-mark gnus-expirable-mark
6014            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6015            gnus-duplicate-mark gnus-souped-mark)
6016      'reverse)))
6017
6018 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6019 (make-obsolete 'gnus-summary-delete-marked-with
6020                'gnus-summary-limit-exlude-marks)
6021
6022 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6023   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6024 If REVERSE, limit the summary buffer to articles that are marked
6025 with MARKS.  MARKS can either be a string of marks or a list of marks.
6026 Returns how many articles were removed."
6027   (interactive "sMarks: ")
6028   (gnus-summary-limit-to-marks marks t))
6029
6030 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6031   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6032 If REVERSE (the prefix), limit the summary buffer to articles that are
6033 not marked with MARKS.  MARKS can either be a string of marks or a
6034 list of marks.
6035 Returns how many articles were removed."
6036   (interactive "sMarks: \nP")
6037   (prog1
6038       (let ((data gnus-newsgroup-data)
6039             (marks (if (listp marks) marks
6040                      (append marks nil))) ; Transform to list.
6041             articles)
6042         (while data
6043           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6044                   (memq (gnus-data-mark (car data)) marks))
6045             (push (gnus-data-number (car data)) articles))
6046           (setq data (cdr data)))
6047         (gnus-summary-limit articles))
6048     (gnus-summary-position-point)))
6049
6050 (defun gnus-summary-limit-to-score (&optional score)
6051   "Limit to articles with score at or above SCORE."
6052   (interactive "P")
6053   (setq score (if score
6054                   (prefix-numeric-value score)
6055                 (or gnus-summary-default-score 0)))
6056   (let ((data gnus-newsgroup-data)
6057         articles)
6058     (while data
6059       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6060                 score)
6061         (push (gnus-data-number (car data)) articles))
6062       (setq data (cdr data)))
6063     (prog1
6064         (gnus-summary-limit articles)
6065       (gnus-summary-position-point))))
6066
6067 (defun gnus-summary-limit-include-thread (id)
6068   "Display all the hidden articles that in the current thread."
6069   (interactive (list (mail-header-id (gnus-summary-article-header))))
6070   (let ((articles (gnus-articles-in-thread
6071                    (gnus-id-to-thread (gnus-root-id id)))))
6072     (prog1
6073         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6074       (gnus-summary-position-point))))
6075
6076 (defun gnus-summary-limit-include-dormant ()
6077   "Display all the hidden articles that are marked as dormant.
6078 Note that this command only works on a subset of the articles currently
6079 fetched for this group."
6080   (interactive)
6081   (unless gnus-newsgroup-dormant
6082     (error "There are no dormant articles in this group"))
6083   (prog1
6084       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6085     (gnus-summary-position-point)))
6086
6087 (defun gnus-summary-limit-exclude-dormant ()
6088   "Hide all dormant articles."
6089   (interactive)
6090   (prog1
6091       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6092     (gnus-summary-position-point)))
6093
6094 (defun gnus-summary-limit-exclude-childless-dormant ()
6095   "Hide all dormant articles that have no children."
6096   (interactive)
6097   (let ((data (gnus-data-list t))
6098         articles d children)
6099     ;; Find all articles that are either not dormant or have
6100     ;; children.
6101     (while (setq d (pop data))
6102       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6103                 (and (setq children
6104                            (gnus-article-children (gnus-data-number d)))
6105                      (let (found)
6106                        (while children
6107                          (when (memq (car children) articles)
6108                            (setq children nil
6109                                  found t))
6110                          (pop children))
6111                        found)))
6112         (push (gnus-data-number d) articles)))
6113     ;; Do the limiting.
6114     (prog1
6115         (gnus-summary-limit articles)
6116       (gnus-summary-position-point))))
6117
6118 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6119   "Mark all unread excluded articles as read.
6120 If ALL, mark even excluded ticked and dormants as read."
6121   (interactive "P")
6122   (let ((articles (gnus-sorted-complement
6123                    (sort
6124                     (mapcar (lambda (h) (mail-header-number h))
6125                             gnus-newsgroup-headers)
6126                     '<)
6127                    (sort gnus-newsgroup-limit '<)))
6128         article)
6129     (setq gnus-newsgroup-unreads
6130           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6131     (if all
6132         (setq gnus-newsgroup-dormant nil
6133               gnus-newsgroup-marked nil
6134               gnus-newsgroup-reads
6135               (nconc
6136                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6137                gnus-newsgroup-reads))
6138       (while (setq article (pop articles))
6139         (unless (or (memq article gnus-newsgroup-dormant)
6140                     (memq article gnus-newsgroup-marked))
6141           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6142
6143 (defun gnus-summary-limit (articles &optional pop)
6144   (if pop
6145       ;; We pop the previous limit off the stack and use that.
6146       (setq articles (car gnus-newsgroup-limits)
6147             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6148     ;; We use the new limit, so we push the old limit on the stack.
6149     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6150   ;; Set the limit.
6151   (setq gnus-newsgroup-limit articles)
6152   (let ((total (length gnus-newsgroup-data))
6153         (data (gnus-data-find-list (gnus-summary-article-number)))
6154         (gnus-summary-mark-below nil)   ; Inhibit this.
6155         found)
6156     ;; This will do all the work of generating the new summary buffer
6157     ;; according to the new limit.
6158     (gnus-summary-prepare)
6159     ;; Hide any threads, possibly.
6160     (and gnus-show-threads
6161          gnus-thread-hide-subtree
6162          (gnus-summary-hide-all-threads))
6163     ;; Try to return to the article you were at, or one in the
6164     ;; neighborhood.
6165     (when data
6166       ;; We try to find some article after the current one.
6167       (while data
6168         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6169           (setq data nil
6170                 found t))
6171         (setq data (cdr data))))
6172     (unless found
6173       ;; If there is no data, that means that we were after the last
6174       ;; article.  The same goes when we can't find any articles
6175       ;; after the current one.
6176       (goto-char (point-max))
6177       (gnus-summary-find-prev))
6178     (gnus-set-mode-line 'summary)
6179     ;; We return how many articles were removed from the summary
6180     ;; buffer as a result of the new limit.
6181     (- total (length gnus-newsgroup-data))))
6182
6183 (defsubst gnus-invisible-cut-children (threads)
6184   (let ((num 0))
6185     (while threads
6186       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6187         (incf num))
6188       (pop threads))
6189     (< num 2)))
6190
6191 (defsubst gnus-cut-thread (thread)
6192   "Go forwards in the thread until we find an article that we want to display."
6193   (when (or (eq gnus-fetch-old-headers 'some)
6194             (eq gnus-fetch-old-headers 'invisible)          
6195             (eq gnus-build-sparse-threads 'some)
6196             (eq gnus-build-sparse-threads 'more))
6197     ;; Deal with old-fetched headers and sparse threads.
6198     (while (and
6199             thread
6200             (or
6201              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6202              (gnus-summary-article-ancient-p
6203               (mail-header-number (car thread))))
6204             (if (or (<= (length (cdr thread)) 1)
6205                     (eq gnus-fetch-old-headers 'invisible))
6206                 (setq gnus-newsgroup-limit
6207                       (delq (mail-header-number (car thread))
6208                             gnus-newsgroup-limit)
6209                       thread (cadr thread))
6210               (when (gnus-invisible-cut-children (cdr thread))
6211                 (let ((th (cdr thread)))
6212                   (while th
6213                     (if (memq (mail-header-number (caar th))
6214                               gnus-newsgroup-limit)
6215                         (setq thread (car th)
6216                               th nil)
6217                       (setq th (cdr th))))))))))
6218   thread)
6219
6220 (defun gnus-cut-threads (threads)
6221   "Cut off all uninteresting articles from the beginning of threads."
6222   (when (or (eq gnus-fetch-old-headers 'some)
6223             (eq gnus-fetch-old-headers 'invisible)
6224             (eq gnus-build-sparse-threads 'some)
6225             (eq gnus-build-sparse-threads 'more))
6226     (let ((th threads))
6227       (while th
6228         (setcar th (gnus-cut-thread (car th)))
6229         (setq th (cdr th)))))
6230   ;; Remove nixed out threads.
6231   (delq nil threads))
6232
6233 (defun gnus-summary-initial-limit (&optional show-if-empty)
6234   "Figure out what the initial limit is supposed to be on group entry.
6235 This entails weeding out unwanted dormants, low-scored articles,
6236 fetch-old-headers verbiage, and so on."
6237   ;; Most groups have nothing to remove.
6238   (if (or gnus-inhibit-limiting
6239           (and (null gnus-newsgroup-dormant)
6240                (not (eq gnus-fetch-old-headers 'some))
6241                (not (eq gnus-fetch-old-headers 'invisible))
6242                (null gnus-summary-expunge-below)
6243                (not (eq gnus-build-sparse-threads 'some))
6244                (not (eq gnus-build-sparse-threads 'more))
6245                (null gnus-thread-expunge-below)
6246                (not gnus-use-nocem)))
6247       ()                                ; Do nothing.
6248     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6249     (setq gnus-newsgroup-limit nil)
6250     (mapatoms
6251      (lambda (node)
6252        (unless (car (symbol-value node))
6253          ;; These threads have no parents -- they are roots.
6254          (let ((nodes (cdr (symbol-value node)))
6255                thread)
6256            (while nodes
6257              (if (and gnus-thread-expunge-below
6258                       (< (gnus-thread-total-score (car nodes))
6259                          gnus-thread-expunge-below))
6260                  (gnus-expunge-thread (pop nodes))
6261                (setq thread (pop nodes))
6262                (gnus-summary-limit-children thread))))))
6263      gnus-newsgroup-dependencies)
6264     ;; If this limitation resulted in an empty group, we might
6265     ;; pop the previous limit and use it instead.
6266     (when (and (not gnus-newsgroup-limit)
6267                show-if-empty)
6268       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6269     gnus-newsgroup-limit))
6270
6271 (defun gnus-summary-limit-children (thread)
6272   "Return 1 if this subthread is visible and 0 if it is not."
6273   ;; First we get the number of visible children to this thread.  This
6274   ;; is done by recursing down the thread using this function, so this
6275   ;; will really go down to a leaf article first, before slowly
6276   ;; working its way up towards the root.
6277   (when thread
6278     (let ((children
6279            (if (cdr thread)
6280                (apply '+ (mapcar 'gnus-summary-limit-children
6281                                  (cdr thread)))
6282              0))
6283           (number (mail-header-number (car thread)))
6284           score)
6285       (if (and
6286            (not (memq number gnus-newsgroup-marked))
6287            (or
6288             ;; If this article is dormant and has absolutely no visible
6289             ;; children, then this article isn't visible.
6290             (and (memq number gnus-newsgroup-dormant)
6291                  (zerop children))
6292             ;; If this is "fetch-old-headered" and there is no
6293             ;; visible children, then we don't want this article.
6294             (and (eq gnus-fetch-old-headers 'some)
6295                  (gnus-summary-article-ancient-p number)
6296                  (zerop children))
6297             ;; If this is "fetch-old-headered" and `invisible', then
6298             ;; we don't want this article.
6299             (and (eq gnus-fetch-old-headers 'invisible)
6300                  (gnus-summary-article-ancient-p number))
6301             ;; If this is a sparsely inserted article with no children,
6302             ;; we don't want it.
6303             (and (eq gnus-build-sparse-threads 'some)
6304                  (gnus-summary-article-sparse-p number)
6305                  (zerop children))
6306             ;; If we use expunging, and this article is really
6307             ;; low-scored, then we don't want this article.
6308             (when (and gnus-summary-expunge-below
6309                        (< (setq score
6310                                 (or (cdr (assq number gnus-newsgroup-scored))
6311                                     gnus-summary-default-score))
6312                           gnus-summary-expunge-below))
6313               ;; We increase the expunge-tally here, but that has
6314               ;; nothing to do with the limits, really.
6315               (incf gnus-newsgroup-expunged-tally)
6316               ;; We also mark as read here, if that's wanted.
6317               (when (and gnus-summary-mark-below
6318                          (< score gnus-summary-mark-below))
6319                 (setq gnus-newsgroup-unreads
6320                       (delq number gnus-newsgroup-unreads))
6321                 (if gnus-newsgroup-auto-expire
6322                     (push number gnus-newsgroup-expirable)
6323                   (push (cons number gnus-low-score-mark)
6324                         gnus-newsgroup-reads)))
6325               t)
6326             ;; Check NoCeM things.
6327             (if (and gnus-use-nocem
6328                      (gnus-nocem-unwanted-article-p
6329                       (mail-header-id (car thread))))
6330                 (progn
6331                   (setq gnus-newsgroup-unreads
6332                         (delq number gnus-newsgroup-unreads))
6333                   t))))
6334           ;; Nope, invisible article.
6335           0
6336         ;; Ok, this article is to be visible, so we add it to the limit
6337         ;; and return 1.
6338         (push number gnus-newsgroup-limit)
6339         1))))
6340
6341 (defun gnus-expunge-thread (thread)
6342   "Mark all articles in THREAD as read."
6343   (let* ((number (mail-header-number (car thread))))
6344     (incf gnus-newsgroup-expunged-tally)
6345     ;; We also mark as read here, if that's wanted.
6346     (setq gnus-newsgroup-unreads
6347           (delq number gnus-newsgroup-unreads))
6348     (if gnus-newsgroup-auto-expire
6349         (push number gnus-newsgroup-expirable)
6350       (push (cons number gnus-low-score-mark)
6351             gnus-newsgroup-reads)))
6352   ;; Go recursively through all subthreads.
6353   (mapcar 'gnus-expunge-thread (cdr thread)))
6354
6355 ;; Summary article oriented commands
6356
6357 (defun gnus-summary-refer-parent-article (n)
6358   "Refer parent article N times.
6359 If N is negative, go to ancestor -N instead.
6360 The difference between N and the number of articles fetched is returned."
6361   (interactive "p")
6362   (let ((skip 1)
6363         error header ref)
6364     (when (not (natnump n))
6365       (setq skip (abs n)
6366             n 1))
6367     (while (and (> n 0)
6368                 (not error))
6369       (setq header (gnus-summary-article-header))
6370       (if (and (eq (mail-header-number header)
6371                    (cdr gnus-article-current))
6372                (equal gnus-newsgroup-name
6373                       (car gnus-article-current)))
6374           ;; If we try to find the parent of the currently
6375           ;; displayed article, then we take a look at the actual
6376           ;; References header, since this is slightly more
6377           ;; reliable than the References field we got from the
6378           ;; server.
6379           (save-excursion
6380             (set-buffer gnus-original-article-buffer)
6381             (nnheader-narrow-to-headers)
6382             (unless (setq ref (message-fetch-field "references"))
6383               (setq ref (message-fetch-field "in-reply-to")))
6384             (widen))
6385         (setq ref
6386               ;; It's not the current article, so we take a bet on
6387               ;; the value we got from the server.
6388               (mail-header-references header)))
6389       (if (and ref
6390                (not (equal ref "")))
6391           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6392             (gnus-message 1 "Couldn't find parent"))
6393         (gnus-message 1 "No references in article %d"
6394                       (gnus-summary-article-number))
6395         (setq error t))
6396       (decf n))
6397     (gnus-summary-position-point)
6398     n))
6399
6400 (defun gnus-summary-refer-references ()
6401   "Fetch all articles mentioned in the References header.
6402 Return the number of articles fetched."
6403   (interactive)
6404   (let ((ref (mail-header-references (gnus-summary-article-header)))
6405         (current (gnus-summary-article-number))
6406         (n 0))
6407     (if (or (not ref)
6408             (equal ref ""))
6409         (error "No References in the current article")
6410       ;; For each Message-ID in the References header...
6411       (while (string-match "<[^>]*>" ref)
6412         (incf n)
6413         ;; ... fetch that article.
6414         (gnus-summary-refer-article
6415          (prog1 (match-string 0 ref)
6416            (setq ref (substring ref (match-end 0))))))
6417       (gnus-summary-goto-subject current)
6418       (gnus-summary-position-point)
6419       n)))
6420
6421 (defun gnus-summary-refer-thread (&optional limit)
6422   "Fetch all articles in the current thread.
6423 If LIMIT (the numerical prefix), fetch that many old headers instead
6424 of what's specified by the `gnus-refer-thread-limit' variable."
6425   (interactive "P")
6426   (let ((id (mail-header-id (gnus-summary-article-header)))
6427         (limit (if limit (prefix-numeric-value limit)
6428                  gnus-refer-thread-limit)))
6429     ;; We want to fetch LIMIT *old* headers, but we also have to
6430     ;; re-fetch all the headers in the current buffer, because many of
6431     ;; them may be undisplayed.  So we adjust LIMIT.
6432     (when (numberp limit)
6433       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6434     (unless (eq gnus-fetch-old-headers 'invisible)
6435       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6436       ;; Retrieve the headers and read them in.
6437       (if (eq (gnus-retrieve-headers
6438                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6439               'nov)
6440           (gnus-build-all-threads)
6441         (error "Can't fetch thread from backends that don't support NOV"))
6442       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6443     (gnus-summary-limit-include-thread id)))
6444
6445 (defun gnus-summary-refer-article (message-id &optional arg)
6446   "Fetch an article specified by MESSAGE-ID.
6447 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6448 or `gnus-select-method', no matter what backend the article comes from."
6449   (interactive "sMessage-ID: \nP")
6450   (when (and (stringp message-id)
6451              (not (zerop (length message-id))))
6452     ;; Construct the correct Message-ID if necessary.
6453     ;; Suggested by tale@pawl.rpi.edu.
6454     (unless (string-match "^<" message-id)
6455       (setq message-id (concat "<" message-id)))
6456     (unless (string-match ">$" message-id)
6457       (setq message-id (concat message-id ">")))
6458     (let* ((header (gnus-id-to-header message-id))
6459            (sparse (and header
6460                         (gnus-summary-article-sparse-p
6461                          (mail-header-number header))
6462                         (memq (mail-header-number header)
6463                               gnus-newsgroup-limit))))
6464       (cond
6465        ;; If the article is present in the buffer we just go to it.
6466        ((and header
6467              (or (not (gnus-summary-article-sparse-p
6468                        (mail-header-number header)))
6469                  sparse))
6470         (prog1
6471             (gnus-summary-goto-article
6472              (mail-header-number header) nil t)
6473           (when sparse
6474             (gnus-summary-update-article (mail-header-number header)))))
6475        (t
6476         ;; We fetch the article
6477         (let ((gnus-override-method
6478                (cond ((gnus-news-group-p gnus-newsgroup-name)
6479                       gnus-refer-article-method)
6480                      (arg
6481                       (or gnus-refer-article-method gnus-select-method))
6482                      (t nil)))
6483               number)
6484           ;; Start the special refer-article method, if necessary.
6485           (when (and gnus-refer-article-method
6486                      (gnus-news-group-p gnus-newsgroup-name))
6487             (gnus-check-server gnus-refer-article-method))
6488           ;; Fetch the header, and display the article.
6489           (if (setq number (gnus-summary-insert-subject message-id))
6490               (gnus-summary-select-article nil nil nil number)
6491             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6492
6493 (defun gnus-summary-edit-parameters ()
6494   "Edit the group parameters of the current group."
6495   (interactive)
6496   (gnus-group-edit-group gnus-newsgroup-name 'params))
6497
6498 (defun gnus-summary-enter-digest-group (&optional force)
6499   "Enter an nndoc group based on the current article.
6500 If FORCE, force a digest interpretation.  If not, try
6501 to guess what the document format is."
6502   (interactive "P")
6503   (let ((conf gnus-current-window-configuration))
6504     (save-excursion
6505       (gnus-summary-select-article))
6506     (setq gnus-current-window-configuration conf)
6507     (let* ((name (format "%s-%d"
6508                          (gnus-group-prefixed-name
6509                           gnus-newsgroup-name (list 'nndoc ""))
6510                          (save-excursion
6511                            (set-buffer gnus-summary-buffer)
6512                            gnus-current-article)))
6513            (ogroup gnus-newsgroup-name)
6514            (params (append (gnus-info-params (gnus-get-info ogroup))
6515                            (list (cons 'to-group ogroup))
6516                            (list (cons 'save-article-group ogroup))))
6517            (case-fold-search t)
6518            (buf (current-buffer))
6519            dig)
6520       (save-excursion
6521         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6522         (insert-buffer-substring gnus-original-article-buffer)
6523         ;; Remove lines that may lead nndoc to misinterpret the
6524         ;; document type.
6525         (narrow-to-region
6526          (goto-char (point-min))
6527          (or (search-forward "\n\n" nil t) (point)))
6528         (goto-char (point-min))
6529         (delete-matching-lines "^\\(Path\\):\\|^From ")
6530         (widen))
6531       (unwind-protect
6532           (if (gnus-group-read-ephemeral-group
6533                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6534                             (nndoc-article-type
6535                              ,(if force 'digest 'guess))) t)
6536               ;; Make all postings to this group go to the parent group.
6537               (nconc (gnus-info-params (gnus-get-info name))
6538                      params)
6539             ;; Couldn't select this doc group.
6540             (switch-to-buffer buf)
6541             (gnus-set-global-variables)
6542             (gnus-configure-windows 'summary)
6543             (gnus-message 3 "Article couldn't be entered?"))
6544         (kill-buffer dig)))))
6545
6546 (defun gnus-summary-read-document (n)
6547   "Open a new group based on the current article(s).
6548 This will allow you to read digests and other similar
6549 documents as newsgroups.
6550 Obeys the standard process/prefix convention."
6551   (interactive "P")
6552   (let* ((articles (gnus-summary-work-articles n))
6553          (ogroup gnus-newsgroup-name)
6554          (params (append (gnus-info-params (gnus-get-info ogroup))
6555                          (list (cons 'to-group ogroup))))
6556          article group egroup groups vgroup)
6557     (while (setq article (pop articles))
6558       (setq group (format "%s-%d" gnus-newsgroup-name article))
6559       (gnus-summary-remove-process-mark article)
6560       (when (gnus-summary-display-article article)
6561         (save-excursion
6562           (with-temp-buffer
6563             (insert-buffer-substring gnus-original-article-buffer)
6564             ;; Remove some headers that may lead nndoc to make
6565             ;; the wrong guess.
6566             (message-narrow-to-head)
6567             (goto-char (point-min))
6568             (delete-matching-lines "^\\(Path\\):\\|^From ")
6569             (widen)
6570             (if (setq egroup
6571                       (gnus-group-read-ephemeral-group
6572                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6573                                      (nndoc-article-type guess))
6574                        t nil t))
6575                 (progn
6576                   ;; Make all postings to this group go to the parent group.
6577                   (nconc (gnus-info-params (gnus-get-info egroup))
6578                          params)
6579                   (push egroup groups))
6580               ;; Couldn't select this doc group.
6581               (gnus-error 3 "Article couldn't be entered"))))))
6582     ;; Now we have selected all the documents.
6583     (cond
6584      ((not groups)
6585       (error "None of the articles could be interpreted as documents"))
6586      ((gnus-group-read-ephemeral-group
6587        (setq vgroup (format
6588                      "nnvirtual:%s-%s" gnus-newsgroup-name
6589                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6590        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6591        t
6592        (cons (current-buffer) 'summary)))
6593      (t
6594       (error "Couldn't select virtual nndoc group")))))
6595
6596 (defun gnus-summary-isearch-article (&optional regexp-p)
6597   "Do incremental search forward on the current article.
6598 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6599   (interactive "P")
6600   (gnus-summary-select-article)
6601   (gnus-configure-windows 'article)
6602   (gnus-eval-in-buffer-window gnus-article-buffer
6603     (save-restriction
6604       (widen)
6605       (isearch-forward regexp-p))))
6606
6607 (defun gnus-summary-search-article-forward (regexp &optional backward)
6608   "Search for an article containing REGEXP forward.
6609 If BACKWARD, search backward instead."
6610   (interactive
6611    (list (read-string
6612           (format "Search article %s (regexp%s): "
6613                   (if current-prefix-arg "backward" "forward")
6614                   (if gnus-last-search-regexp
6615                       (concat ", default " gnus-last-search-regexp)
6616                     "")))
6617          current-prefix-arg))
6618   (if (string-equal regexp "")
6619       (setq regexp (or gnus-last-search-regexp ""))
6620     (setq gnus-last-search-regexp regexp))
6621   (if (gnus-summary-search-article regexp backward)
6622       (gnus-summary-show-thread)
6623     (error "Search failed: \"%s\"" regexp)))
6624
6625 (defun gnus-summary-search-article-backward (regexp)
6626   "Search for an article containing REGEXP backward."
6627   (interactive
6628    (list (read-string
6629           (format "Search article backward (regexp%s): "
6630                   (if gnus-last-search-regexp
6631                       (concat ", default " gnus-last-search-regexp)
6632                     "")))))
6633   (gnus-summary-search-article-forward regexp 'backward))
6634
6635 (defun gnus-summary-search-article (regexp &optional backward)
6636   "Search for an article containing REGEXP.
6637 Optional argument BACKWARD means do search for backward.
6638 `gnus-select-article-hook' is not called during the search."
6639   ;; We have to require this here to make sure that the following
6640   ;; dynamic binding isn't shadowed by autoloading.
6641   (require 'gnus-async)
6642   (let ((gnus-select-article-hook nil)  ;Disable hook.
6643         (gnus-article-display-hook nil)
6644         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6645         (gnus-use-article-prefetch nil)
6646         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6647         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6648         (sum (current-buffer))
6649         (found nil)
6650         point)
6651     (gnus-save-hidden-threads
6652       (gnus-summary-select-article)
6653       (set-buffer gnus-article-buffer)
6654       (when backward
6655         (forward-line -1))
6656       (while (not found)
6657         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6658         (if (if backward
6659                 (re-search-backward regexp nil t)
6660               (re-search-forward regexp nil t))
6661             ;; We found the regexp.
6662             (progn
6663               (setq found 'found)
6664               (beginning-of-line)
6665               (set-window-start
6666                (get-buffer-window (current-buffer))
6667                (point))
6668               (forward-line 1)
6669               (set-buffer sum)
6670               (setq point (point)))
6671           ;; We didn't find it, so we go to the next article.
6672           (set-buffer sum)
6673           (setq found 'not)
6674           (while (eq found 'not)
6675             (if (not (if backward (gnus-summary-find-prev)
6676                        (gnus-summary-find-next)))
6677                 ;; No more articles.
6678                 (setq found t)
6679               ;; Select the next article and adjust point.
6680               (unless (gnus-summary-article-sparse-p
6681                        (gnus-summary-article-number))
6682                 (setq found nil)
6683                 (gnus-summary-select-article)
6684                 (set-buffer gnus-article-buffer)
6685                 (widen)
6686                 (goto-char (if backward (point-max) (point-min))))))))
6687       (gnus-message 7 ""))
6688     ;; Return whether we found the regexp.
6689     (when (eq found 'found)
6690       (goto-char point)
6691       (gnus-summary-show-thread)
6692       (gnus-summary-goto-subject gnus-current-article)
6693       (gnus-summary-position-point)
6694       t)))
6695
6696 (defun gnus-summary-find-matching (header regexp &optional backward unread
6697                                           not-case-fold)
6698   "Return a list of all articles that match REGEXP on HEADER.
6699 The search stars on the current article and goes forwards unless
6700 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6701 If UNREAD is non-nil, only unread articles will
6702 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6703 in the comparisons."
6704   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6705                 (gnus-data-find-list
6706                  (gnus-summary-article-number) (gnus-data-list backward))))
6707         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6708         (case-fold-search (not not-case-fold))
6709         articles d)
6710     (unless (fboundp (intern (concat "mail-header-" header)))
6711       (error "%s is not a valid header" header))
6712     (while data
6713       (setq d (car data))
6714       (and (or (not unread)             ; We want all articles...
6715                (gnus-data-unread-p d))  ; Or just unreads.
6716            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6717            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6718            (push (gnus-data-number d) articles)) ; Success!
6719       (setq data (cdr data)))
6720     (nreverse articles)))
6721
6722 (defun gnus-summary-execute-command (header regexp command &optional backward)
6723   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6724 If HEADER is an empty string (or nil), the match is done on the entire
6725 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6726   (interactive
6727    (list (let ((completion-ignore-case t))
6728            (completing-read
6729             "Header name: "
6730             (mapcar (lambda (string) (list string))
6731                     '("Number" "Subject" "From" "Lines" "Date"
6732                       "Message-ID" "Xref" "References" "Body"))
6733             nil 'require-match))
6734          (read-string "Regexp: ")
6735          (read-key-sequence "Command: ")
6736          current-prefix-arg))
6737   (when (equal header "Body")
6738     (setq header ""))
6739   ;; Hidden thread subtrees must be searched as well.
6740   (gnus-summary-show-all-threads)
6741   ;; We don't want to change current point nor window configuration.
6742   (save-excursion
6743     (save-window-excursion
6744       (gnus-message 6 "Executing %s..." (key-description command))
6745       ;; We'd like to execute COMMAND interactively so as to give arguments.
6746       (gnus-execute header regexp
6747                     `(call-interactively ',(key-binding command))
6748                     backward)
6749       (gnus-message 6 "Executing %s...done" (key-description command)))))
6750
6751 (defun gnus-summary-beginning-of-article ()
6752   "Scroll the article back to the beginning."
6753   (interactive)
6754   (gnus-summary-select-article)
6755   (gnus-configure-windows 'article)
6756   (gnus-eval-in-buffer-window gnus-article-buffer
6757     (widen)
6758     (goto-char (point-min))
6759     (when gnus-page-broken
6760       (gnus-narrow-to-page))))
6761
6762 (defun gnus-summary-end-of-article ()
6763   "Scroll to the end of the article."
6764   (interactive)
6765   (gnus-summary-select-article)
6766   (gnus-configure-windows 'article)
6767   (gnus-eval-in-buffer-window gnus-article-buffer
6768     (widen)
6769     (goto-char (point-max))
6770     (recenter -3)
6771     (when gnus-page-broken
6772       (gnus-narrow-to-page))))
6773
6774 (defun gnus-summary-print-article (&optional filename n)
6775   "Generate and print a PostScript image of the N next (mail) articles.
6776
6777 If N is negative, print the N previous articles.  If N is nil and articles
6778 have been marked with the process mark, print these instead.
6779
6780 If the optional second argument FILENAME is nil, send the image to the
6781 printer.  If FILENAME is a string, save the PostScript image in a file with
6782 that name.  If FILENAME is a number, prompt the user for the name of the file
6783 to save in."
6784   (interactive (list (ps-print-preprint current-prefix-arg)
6785                      current-prefix-arg))
6786   (dolist (article (gnus-summary-work-articles n))
6787     (gnus-summary-select-article nil nil 'pseudo article)
6788     (gnus-eval-in-buffer-window gnus-article-buffer
6789       (let ((buffer (generate-new-buffer " *print*")))
6790         (unwind-protect
6791             (progn
6792               (copy-to-buffer buffer (point-min) (point-max))
6793               (set-buffer buffer)
6794               (gnus-article-delete-invisible-text)
6795               (let ((ps-left-header
6796                      (list 
6797                       (concat "("
6798                               (mail-header-subject gnus-current-headers) ")")
6799                       (concat "("
6800                               (mail-header-from gnus-current-headers) ")")))
6801                     (ps-right-header 
6802                      (list 
6803                       "/pagenumberstring load" 
6804                       (concat "("
6805                               (mail-header-date gnus-current-headers) ")"))))
6806                 (gnus-run-hooks 'gnus-ps-print-hook)
6807                 (save-excursion
6808                   (ps-print-buffer-with-faces filename))))
6809           (kill-buffer buffer))))))
6810
6811 (defun gnus-summary-show-article (&optional arg)
6812   "Force re-fetching of the current article.
6813 If ARG (the prefix) is non-nil, show the raw article without any
6814 article massaging functions being run."
6815   (interactive "P")
6816   (if (not arg)
6817       ;; Select the article the normal way.
6818       (gnus-summary-select-article nil 'force)
6819     ;; Bind the article treatment functions to nil.
6820     (let ((gnus-have-all-headers t)
6821           gnus-article-display-hook
6822           gnus-article-prepare-hook
6823           gnus-break-pages
6824           gnus-show-mime
6825           gnus-visual)
6826       (gnus-summary-select-article nil 'force)))
6827   (gnus-summary-goto-subject gnus-current-article)
6828   (gnus-summary-position-point))
6829
6830 (defun gnus-summary-verbose-headers (&optional arg)
6831   "Toggle permanent full header display.
6832 If ARG is a positive number, turn header display on.
6833 If ARG is a negative number, turn header display off."
6834   (interactive "P")
6835   (setq gnus-show-all-headers
6836         (cond ((or (not (numberp arg))
6837                    (zerop arg))
6838                (not gnus-show-all-headers))
6839               ((natnump arg)
6840                t)))
6841   (gnus-summary-show-article))
6842
6843 (defun gnus-summary-toggle-header (&optional arg)
6844   "Show the headers if they are hidden, or hide them if they are shown.
6845 If ARG is a positive number, show the entire header.
6846 If ARG is a negative number, hide the unwanted header lines."
6847   (interactive "P")
6848   (save-excursion
6849     (set-buffer gnus-article-buffer)
6850     (let* ((buffer-read-only nil)
6851            (inhibit-point-motion-hooks t)
6852            (hidden (text-property-any
6853                     (goto-char (point-min)) (search-forward "\n\n")
6854                     'invisible t))
6855            e)
6856       (goto-char (point-min))
6857       (when (search-forward "\n\n" nil t)
6858         (delete-region (point-min) (1- (point))))
6859       (goto-char (point-min))
6860       (save-excursion
6861         (set-buffer gnus-original-article-buffer)
6862         (goto-char (point-min))
6863         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6864       (insert-buffer-substring gnus-original-article-buffer 1 e)
6865       (let ((article-inhibit-hiding t))
6866         (gnus-run-hooks 'gnus-article-display-hook))
6867       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6868         (gnus-article-hide-headers)))))
6869
6870 (defun gnus-summary-show-all-headers ()
6871   "Make all header lines visible."
6872   (interactive)
6873   (gnus-article-show-all-headers))
6874
6875 (defun gnus-summary-toggle-mime (&optional arg)
6876   "Toggle MIME processing.
6877 If ARG is a positive number, turn MIME processing on."
6878   (interactive "P")
6879   (setq gnus-show-mime
6880         (if (null arg) (not gnus-show-mime)
6881           (> (prefix-numeric-value arg) 0)))
6882   (gnus-summary-select-article t 'force))
6883
6884 (defun gnus-summary-caesar-message (&optional arg)
6885   "Caesar rotate the current article by 13.
6886 The numerical prefix specifies how many places to rotate each letter
6887 forward."
6888   (interactive "P")
6889   (gnus-summary-select-article)
6890   (let ((mail-header-separator ""))
6891     (gnus-eval-in-buffer-window gnus-article-buffer
6892       (save-restriction
6893         (widen)
6894         (let ((start (window-start))
6895               buffer-read-only)
6896           (message-caesar-buffer-body arg)
6897           (set-window-start (get-buffer-window (current-buffer)) start))))))
6898
6899 (defun gnus-summary-stop-page-breaking ()
6900   "Stop page breaking in the current article."
6901   (interactive)
6902   (gnus-summary-select-article)
6903   (gnus-eval-in-buffer-window gnus-article-buffer
6904     (widen)
6905     (when (gnus-visual-p 'page-marker)
6906       (let ((buffer-read-only nil))
6907         (gnus-remove-text-with-property 'gnus-prev)
6908         (gnus-remove-text-with-property 'gnus-next))
6909       (setq gnus-page-broken nil))))
6910
6911 (defun gnus-summary-move-article (&optional n to-newsgroup
6912                                             select-method action)
6913   "Move the current article to a different newsgroup.
6914 If N is a positive number, move the N next articles.
6915 If N is a negative number, move the N previous articles.
6916 If N is nil and any articles have been marked with the process mark,
6917 move those articles instead.
6918 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6919 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6920 re-spool using this method.
6921
6922 For this function to work, both the current newsgroup and the
6923 newsgroup that you want to move to have to support the `request-move'
6924 and `request-accept' functions."
6925   (interactive "P")
6926   (unless action
6927     (setq action 'move))
6928   ;; Disable marking as read.
6929   (let (gnus-mark-article-hook)
6930     (save-window-excursion
6931       (gnus-summary-select-article)))
6932   ;; Check whether the source group supports the required functions.
6933   (cond ((and (eq action 'move)
6934               (not (gnus-check-backend-function
6935                     'request-move-article gnus-newsgroup-name)))
6936          (error "The current group does not support article moving"))
6937         ((and (eq action 'crosspost)
6938               (not (gnus-check-backend-function
6939                     'request-replace-article gnus-newsgroup-name)))
6940          (error "The current group does not support article editing")))
6941   (let ((articles (gnus-summary-work-articles n))
6942         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6943         (names '((move "Move" "Moving")
6944                  (copy "Copy" "Copying")
6945                  (crosspost "Crosspost" "Crossposting")))
6946         (copy-buf (save-excursion
6947                     (nnheader-set-temp-buffer " *copy article*")))
6948         art-group to-method new-xref article to-groups)
6949     (unless (assq action names)
6950       (error "Unknown action %s" action))
6951     ;; Read the newsgroup name.
6952     (when (and (not to-newsgroup)
6953                (not select-method))
6954       (setq to-newsgroup
6955             (gnus-read-move-group-name
6956              (cadr (assq action names))
6957              (symbol-value (intern (format "gnus-current-%s-group" action)))
6958              articles prefix))
6959       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6960     (setq to-method (or select-method
6961                         (gnus-group-name-to-method to-newsgroup)))
6962     ;; Check the method we are to move this article to...
6963     (unless (gnus-check-backend-function
6964              'request-accept-article (car to-method))
6965       (error "%s does not support article copying" (car to-method)))
6966     (unless (gnus-check-server to-method)
6967       (error "Can't open server %s" (car to-method)))
6968     (gnus-message 6 "%s to %s: %s..."
6969                   (caddr (assq action names))
6970                   (or (car select-method) to-newsgroup) articles)
6971     (while articles
6972       (setq article (pop articles))
6973       (setq
6974        art-group
6975        (cond
6976         ;; Move the article.
6977         ((eq action 'move)
6978          ;; Remove this article from future suppression.
6979          (gnus-dup-unsuppress-article article)
6980          (gnus-request-move-article
6981           article                       ; Article to move
6982           gnus-newsgroup-name           ; From newsgroup
6983           (nth 1 (gnus-find-method-for-group
6984                   gnus-newsgroup-name)) ; Server
6985           (list 'gnus-request-accept-article
6986                 to-newsgroup (list 'quote select-method)
6987                 (not articles))         ; Accept form
6988           (not articles)))              ; Only save nov last time
6989         ;; Copy the article.
6990         ((eq action 'copy)
6991          (save-excursion
6992            (set-buffer copy-buf)
6993            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
6994              (gnus-request-accept-article
6995               to-newsgroup select-method (not articles)))))
6996         ;; Crosspost the article.
6997         ((eq action 'crosspost)
6998          (let ((xref (message-tokenize-header
6999                       (mail-header-xref (gnus-summary-article-header article))
7000                       " ")))
7001            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7002                                   ":" article))
7003            (unless xref
7004              (setq xref (list (system-name))))
7005            (setq new-xref
7006                  (concat
7007                   (mapconcat 'identity
7008                              (delete "Xref:" (delete new-xref xref))
7009                              " ")
7010                   " " new-xref))
7011            (save-excursion
7012              (set-buffer copy-buf)
7013              ;; First put the article in the destination group.
7014              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7015              (when (consp (setq art-group
7016                                 (gnus-request-accept-article
7017                                  to-newsgroup select-method (not articles))))
7018                (setq new-xref (concat new-xref " " (car art-group)
7019                                       ":" (cdr art-group)))
7020                ;; Now we have the new Xrefs header, so we insert
7021                ;; it and replace the new article.
7022                (nnheader-replace-header "Xref" new-xref)
7023                (gnus-request-replace-article
7024                 (cdr art-group) to-newsgroup (current-buffer))
7025                art-group))))))
7026       (cond
7027        ((not art-group)
7028         (gnus-message 1 "Couldn't %s article %s"
7029                       (cadr (assq action names)) article))
7030        ((and (eq art-group 'junk)
7031              (eq action 'move))
7032         (gnus-summary-mark-article article gnus-canceled-mark)
7033         (gnus-message 4 "Deleted article %s" article))
7034        (t
7035         (let* ((pto-group (gnus-group-prefixed-name
7036                            (car art-group) to-method))
7037                (entry
7038                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7039                (info (nth 2 entry))
7040                (to-group (gnus-info-group info)))
7041           ;; Update the group that has been moved to.
7042           (when (and info
7043                      (memq action '(move copy)))
7044             (unless (member to-group to-groups)
7045               (push to-group to-groups))
7046
7047             (unless (memq article gnus-newsgroup-unreads)
7048               (gnus-info-set-read
7049                info (gnus-add-to-range (gnus-info-read info)
7050                                        (list (cdr art-group)))))
7051
7052             ;; Copy any marks over to the new group.
7053             (let ((marks gnus-article-mark-lists)
7054                   (to-article (cdr art-group)))
7055
7056               ;; See whether the article is to be put in the cache.
7057               (when gnus-use-cache
7058                 (gnus-cache-possibly-enter-article
7059                  to-group to-article
7060                  (let ((header (copy-sequence
7061                                 (gnus-summary-article-header article))))
7062                    (mail-header-set-number header to-article)
7063                    header)
7064                  (memq article gnus-newsgroup-marked)
7065                  (memq article gnus-newsgroup-dormant)
7066                  (memq article gnus-newsgroup-unreads)))
7067
7068               (when (and (equal to-group gnus-newsgroup-name)
7069                          (not (memq article gnus-newsgroup-unreads)))
7070                 ;; Mark this article as read in this group.
7071                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7072                 (setcdr (gnus-active to-group) to-article)
7073                 (setcdr gnus-newsgroup-active to-article))
7074
7075               (while marks
7076                 (when (memq article (symbol-value
7077                                      (intern (format "gnus-newsgroup-%s"
7078                                                      (caar marks)))))
7079                   ;; If the other group is the same as this group,
7080                   ;; then we have to add the mark to the list.
7081                   (when (equal to-group gnus-newsgroup-name)
7082                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7083                          (cons to-article
7084                                (symbol-value
7085                                 (intern (format "gnus-newsgroup-%s"
7086                                                 (caar marks)))))))
7087                   ;; Copy the marks to other group.
7088                   (gnus-add-marked-articles
7089                    to-group (cdar marks) (list to-article) info))
7090                 (setq marks (cdr marks)))
7091
7092               (gnus-dribble-enter
7093                (concat "(gnus-group-set-info '"
7094                        (gnus-prin1-to-string (gnus-get-info to-group))
7095                        ")"))))
7096
7097           ;; Update the Xref header in this article to point to
7098           ;; the new crossposted article we have just created.
7099           (when (eq action 'crosspost)
7100             (save-excursion
7101               (set-buffer copy-buf)
7102               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7103               (nnheader-replace-header "Xref" new-xref)
7104               (gnus-request-replace-article
7105                article gnus-newsgroup-name (current-buffer)))))
7106
7107         ;;;!!!Why is this necessary?
7108         (set-buffer gnus-summary-buffer)
7109         
7110         (gnus-summary-goto-subject article)
7111         (when (eq action 'move)
7112           (gnus-summary-mark-article article gnus-canceled-mark))))
7113       (gnus-summary-remove-process-mark article))
7114     ;; Re-activate all groups that have been moved to.
7115     (while to-groups
7116       (save-excursion
7117         (set-buffer gnus-group-buffer)
7118         (when (gnus-group-goto-group (car to-groups) t)
7119           (gnus-group-get-new-news-this-group 1 t))
7120         (pop to-groups)))
7121
7122     (gnus-kill-buffer copy-buf)
7123     (gnus-summary-position-point)
7124     (gnus-set-mode-line 'summary)))
7125
7126 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7127   "Move the current article to a different newsgroup.
7128 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7129 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7130 re-spool using this method."
7131   (interactive "P")
7132   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7133
7134 (defun gnus-summary-crosspost-article (&optional n)
7135   "Crosspost the current article to some other group."
7136   (interactive "P")
7137   (gnus-summary-move-article n nil nil 'crosspost))
7138
7139 (defcustom gnus-summary-respool-default-method nil
7140   "Default method for respooling an article.
7141 If nil, use to the current newsgroup method."
7142   :type `(choice (gnus-select-method :value (nnml ""))
7143                  (const nil))
7144   :group 'gnus-summary-mail)
7145
7146 (defun gnus-summary-respool-article (&optional n method)
7147   "Respool the current article.
7148 The article will be squeezed through the mail spooling process again,
7149 which means that it will be put in some mail newsgroup or other
7150 depending on `nnmail-split-methods'.
7151 If N is a positive number, respool the N next articles.
7152 If N is a negative number, respool the N previous articles.
7153 If N is nil and any articles have been marked with the process mark,
7154 respool those articles instead.
7155
7156 Respooling can be done both from mail groups and \"real\" newsgroups.
7157 In the former case, the articles in question will be moved from the
7158 current group into whatever groups they are destined to.  In the
7159 latter case, they will be copied into the relevant groups."
7160   (interactive
7161    (list current-prefix-arg
7162          (let* ((methods (gnus-methods-using 'respool))
7163                 (methname
7164                  (symbol-name (or gnus-summary-respool-default-method
7165                                   (car (gnus-find-method-for-group
7166                                         gnus-newsgroup-name)))))
7167                 (method
7168                  (gnus-completing-read
7169                   methname "What backend do you want to use when respooling?"
7170                   methods nil t nil 'gnus-mail-method-history))
7171                 ms)
7172            (cond
7173             ((zerop (length (setq ms (gnus-servers-using-backend
7174                                       (intern method)))))
7175              (list (intern method) ""))
7176             ((= 1 (length ms))
7177              (car ms))
7178             (t
7179              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7180                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7181                            ms-alist))))))))
7182   (unless method
7183     (error "No method given for respooling"))
7184   (if (assoc (symbol-name
7185               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7186              (gnus-methods-using 'respool))
7187       (gnus-summary-move-article n nil method)
7188     (gnus-summary-copy-article n nil method)))
7189
7190 (defun gnus-summary-import-article (file)
7191   "Import an arbitrary file into a mail newsgroup."
7192   (interactive "fImport file: ")
7193   (let ((group gnus-newsgroup-name)
7194         (now (current-time))
7195         atts lines)
7196     (unless (gnus-check-backend-function 'request-accept-article group)
7197       (error "%s does not support article importing" group))
7198     (or (file-readable-p file)
7199         (not (file-regular-p file))
7200         (error "Can't read %s" file))
7201     (save-excursion
7202       (set-buffer (gnus-get-buffer-create " *import file*"))
7203       (buffer-disable-undo (current-buffer))
7204       (erase-buffer)
7205       (nnheader-insert-file-contents file)
7206       (goto-char (point-min))
7207       (unless (nnheader-article-p)
7208         ;; This doesn't look like an article, so we fudge some headers.
7209         (setq atts (file-attributes file)
7210               lines (count-lines (point-min) (point-max)))
7211         (insert "From: " (read-string "From: ") "\n"
7212                 "Subject: " (read-string "Subject: ") "\n"
7213                 "Date: " (timezone-make-date-arpa-standard
7214                           (current-time-string (nth 5 atts))
7215                           (current-time-zone now)
7216                           (current-time-zone now))
7217                 "\n"
7218                 "Message-ID: " (message-make-message-id) "\n"
7219                 "Lines: " (int-to-string lines) "\n"
7220                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7221       (gnus-request-accept-article group nil t)
7222       (kill-buffer (current-buffer)))))
7223
7224 (defun gnus-summary-article-posted-p ()
7225   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7226 This will be the case if the article has both been mailed and posted."
7227   (interactive)
7228   (let ((id (mail-header-references (gnus-summary-article-header)))
7229         (gnus-override-method
7230          (or gnus-refer-article-method gnus-select-method)))
7231     (if (gnus-request-head id "")
7232         (gnus-message 2 "The current message was found on %s"
7233                       gnus-override-method)
7234       (gnus-message 2 "The current message couldn't be found on %s"
7235                     gnus-override-method)
7236       nil)))
7237
7238 (defun gnus-summary-expire-articles (&optional now)
7239   "Expire all articles that are marked as expirable in the current group."
7240   (interactive)
7241   (when (gnus-check-backend-function
7242          'request-expire-articles gnus-newsgroup-name)
7243     ;; This backend supports expiry.
7244     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7245            (expirable (if total
7246                           (progn
7247                             ;; We need to update the info for
7248                             ;; this group for `gnus-list-of-read-articles'
7249                             ;; to give us the right answer.
7250                             (gnus-run-hooks 'gnus-exit-group-hook)
7251                             (gnus-summary-update-info)
7252                             (gnus-list-of-read-articles gnus-newsgroup-name))
7253                         (setq gnus-newsgroup-expirable
7254                               (sort gnus-newsgroup-expirable '<))))
7255            (expiry-wait (if now 'immediate
7256                           (gnus-group-find-parameter
7257                            gnus-newsgroup-name 'expiry-wait)))
7258            es)
7259       (when expirable
7260         ;; There are expirable articles in this group, so we run them
7261         ;; through the expiry process.
7262         (gnus-message 6 "Expiring articles...")
7263         ;; The list of articles that weren't expired is returned.
7264         (save-excursion
7265           (if expiry-wait
7266               (let ((nnmail-expiry-wait-function nil)
7267                     (nnmail-expiry-wait expiry-wait))
7268                 (setq es (gnus-request-expire-articles
7269                           expirable gnus-newsgroup-name)))
7270             (setq es (gnus-request-expire-articles
7271                       expirable gnus-newsgroup-name))))
7272         (unless total
7273           (setq gnus-newsgroup-expirable es))
7274         ;; We go through the old list of expirable, and mark all
7275         ;; really expired articles as nonexistent.
7276         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7277           (let ((gnus-use-cache nil))
7278             (while expirable
7279               (unless (memq (car expirable) es)
7280                 (when (gnus-data-find (car expirable))
7281                   (gnus-summary-mark-article
7282                    (car expirable) gnus-canceled-mark)))
7283               (setq expirable (cdr expirable)))))
7284         (gnus-message 6 "Expiring articles...done")))))
7285
7286 (defun gnus-summary-expire-articles-now ()
7287   "Expunge all expirable articles in the current group.
7288 This means that *all* articles that are marked as expirable will be
7289 deleted forever, right now."
7290   (interactive)
7291   (or gnus-expert-user
7292       (gnus-yes-or-no-p
7293        "Are you really, really, really sure you want to delete all these messages? ")
7294       (error "Phew!"))
7295   (gnus-summary-expire-articles t))
7296
7297 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7298 (defun gnus-summary-delete-article (&optional n)
7299   "Delete the N next (mail) articles.
7300 This command actually deletes articles.  This is not a marking
7301 command.  The article will disappear forever from your life, never to
7302 return.
7303 If N is negative, delete backwards.
7304 If N is nil and articles have been marked with the process mark,
7305 delete these instead."
7306   (interactive "P")
7307   (unless (gnus-check-backend-function 'request-expire-articles
7308                                        gnus-newsgroup-name)
7309     (error "The current newsgroup does not support article deletion"))
7310   ;; Compute the list of articles to delete.
7311   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7312         not-deleted)
7313     (if (and gnus-novice-user
7314              (not (gnus-yes-or-no-p
7315                    (format "Do you really want to delete %s forever? "
7316                            (if (> (length articles) 1)
7317                                (format "these %s articles" (length articles))
7318                              "this article")))))
7319         ()
7320       ;; Delete the articles.
7321       (setq not-deleted (gnus-request-expire-articles
7322                          articles gnus-newsgroup-name 'force))
7323       (while articles
7324         (gnus-summary-remove-process-mark (car articles))
7325         ;; The backend might not have been able to delete the article
7326         ;; after all.
7327         (unless (memq (car articles) not-deleted)
7328           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7329         (setq articles (cdr articles)))
7330       (when not-deleted
7331         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7332     (gnus-summary-position-point)
7333     (gnus-set-mode-line 'summary)
7334     not-deleted))
7335
7336 (defun gnus-summary-edit-article (&optional force)
7337   "Edit the current article.
7338 This will have permanent effect only in mail groups.
7339 If FORCE is non-nil, allow editing of articles even in read-only
7340 groups."
7341   (interactive "P")
7342   (save-excursion
7343     (set-buffer gnus-summary-buffer)
7344     (gnus-set-global-variables)
7345     (when (and (not force)
7346                (gnus-group-read-only-p))
7347       (error "The current newsgroup does not support article editing"))
7348     ;; Select article if needed.
7349     (unless (eq (gnus-summary-article-number)
7350                 gnus-current-article)
7351       (gnus-summary-select-article t))
7352     (gnus-article-date-original)
7353     (gnus-article-edit-article
7354      `(lambda (no-highlight)
7355         (gnus-summary-edit-article-done
7356          ,(or (mail-header-references gnus-current-headers) "")
7357          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7358
7359 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7360
7361 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7362                                                  no-highlight)
7363   "Make edits to the current article permanent."
7364   (interactive)
7365   ;; Replace the article.
7366   (let ((buf (current-buffer)))
7367     (with-temp-buffer
7368       (insert-buffer buf)
7369       (if (and (not read-only)
7370                (not (gnus-request-replace-article
7371                      (cdr gnus-article-current) (car gnus-article-current)
7372                      (current-buffer))))
7373           (error "Couldn't replace article")
7374         ;; Update the summary buffer.
7375         (if (and references
7376                  (equal (message-tokenize-header references " ")
7377                         (message-tokenize-header
7378                          (or (message-fetch-field "references") "") " ")))
7379             ;; We only have to update this line.
7380             (save-excursion
7381               (save-restriction
7382                 (message-narrow-to-head)
7383                 (let ((head (buffer-string))
7384                       header)
7385                   (with-temp-buffer
7386                     (insert (format "211 %d Article retrieved.\n"
7387                                     (cdr gnus-article-current)))
7388                     (insert head)
7389                     (insert ".\n")
7390                     (let ((nntp-server-buffer (current-buffer)))
7391                       (setq header (car (gnus-get-newsgroup-headers
7392                                          (save-excursion
7393                                            (set-buffer gnus-summary-buffer)
7394                                            gnus-newsgroup-dependencies)
7395                                          t))))
7396                     (save-excursion
7397                       (set-buffer gnus-summary-buffer)
7398                       (gnus-data-set-header
7399                        (gnus-data-find (cdr gnus-article-current))
7400                        header)
7401                       (gnus-summary-update-article-line
7402                        (cdr gnus-article-current) header))))))
7403           ;; Update threads.
7404           (set-buffer (or buffer gnus-summary-buffer))
7405           (gnus-summary-update-article (cdr gnus-article-current)))
7406         ;; Prettify the article buffer again.
7407         (unless no-highlight
7408           (save-excursion
7409             (set-buffer gnus-article-buffer)
7410             (gnus-run-hooks 'gnus-article-display-hook)
7411             (set-buffer gnus-original-article-buffer)
7412             (gnus-request-article
7413              (cdr gnus-article-current)
7414              (car gnus-article-current) (current-buffer))))
7415         ;; Prettify the summary buffer line.
7416         (when (gnus-visual-p 'summary-highlight 'highlight)
7417           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7418
7419 (defun gnus-summary-edit-wash (key)
7420   "Perform editing command KEY in the article buffer."
7421   (interactive
7422    (list
7423     (progn
7424       (message "%s" (concat (this-command-keys) "- "))
7425       (read-char))))
7426   (message "")
7427   (gnus-summary-edit-article)
7428   (execute-kbd-macro (concat (this-command-keys) key))
7429   (gnus-article-edit-done))
7430
7431 ;;; Respooling
7432
7433 (defun gnus-summary-respool-query (&optional silent trace)
7434   "Query where the respool algorithm would put this article."
7435   (interactive)
7436   (let (gnus-mark-article-hook)
7437     (gnus-summary-select-article)
7438     (save-excursion
7439       (set-buffer gnus-original-article-buffer)
7440       (save-restriction
7441         (message-narrow-to-head)
7442         (let ((groups (nnmail-article-group 'identity trace)))
7443           (unless silent
7444             (if groups
7445                 (message "This message would go to %s"
7446                          (mapconcat 'car groups ", "))
7447               (message "This message would go to no groups"))
7448             groups))))))
7449
7450 (defun gnus-summary-respool-trace ()
7451   "Trace where the respool algorithm would put this article.
7452 Display a buffer showing all fancy splitting patterns which matched."
7453   (interactive)
7454   (gnus-summary-respool-query nil t))
7455
7456 ;; Summary marking commands.
7457
7458 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7459   "Mark articles which has the same subject as read, and then select the next.
7460 If UNMARK is positive, remove any kind of mark.
7461 If UNMARK is negative, tick articles."
7462   (interactive "P")
7463   (when unmark
7464     (setq unmark (prefix-numeric-value unmark)))
7465   (let ((count
7466          (gnus-summary-mark-same-subject
7467           (gnus-summary-article-subject) unmark)))
7468     ;; Select next unread article.  If auto-select-same mode, should
7469     ;; select the first unread article.
7470     (gnus-summary-next-article t (and gnus-auto-select-same
7471                                       (gnus-summary-article-subject)))
7472     (gnus-message 7 "%d article%s marked as %s"
7473                   count (if (= count 1) " is" "s are")
7474                   (if unmark "unread" "read"))))
7475
7476 (defun gnus-summary-kill-same-subject (&optional unmark)
7477   "Mark articles which has the same subject as read.
7478 If UNMARK is positive, remove any kind of mark.
7479 If UNMARK is negative, tick articles."
7480   (interactive "P")
7481   (when unmark
7482     (setq unmark (prefix-numeric-value unmark)))
7483   (let ((count
7484          (gnus-summary-mark-same-subject
7485           (gnus-summary-article-subject) unmark)))
7486     ;; If marked as read, go to next unread subject.
7487     (when (null unmark)
7488       ;; Go to next unread subject.
7489       (gnus-summary-next-subject 1 t))
7490     (gnus-message 7 "%d articles are marked as %s"
7491                   count (if unmark "unread" "read"))))
7492
7493 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7494   "Mark articles with same SUBJECT as read, and return marked number.
7495 If optional argument UNMARK is positive, remove any kinds of marks.
7496 If optional argument UNMARK is negative, mark articles as unread instead."
7497   (let ((count 1))
7498     (save-excursion
7499       (cond
7500        ((null unmark)                   ; Mark as read.
7501         (while (and
7502                 (progn
7503                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7504                   (gnus-summary-show-thread) t)
7505                 (gnus-summary-find-subject subject))
7506           (setq count (1+ count))))
7507        ((> unmark 0)                    ; Tick.
7508         (while (and
7509                 (progn
7510                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7511                   (gnus-summary-show-thread) t)
7512                 (gnus-summary-find-subject subject))
7513           (setq count (1+ count))))
7514        (t                               ; Mark as unread.
7515         (while (and
7516                 (progn
7517                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7518                   (gnus-summary-show-thread) t)
7519                 (gnus-summary-find-subject subject))
7520           (setq count (1+ count)))))
7521       (gnus-set-mode-line 'summary)
7522       ;; Return the number of marked articles.
7523       count)))
7524
7525 (defun gnus-summary-mark-as-processable (n &optional unmark)
7526   "Set the process mark on the next N articles.
7527 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7528 the process mark instead.  The difference between N and the actual
7529 number of articles marked is returned."
7530   (interactive "p")
7531   (let ((backward (< n 0))
7532         (n (abs n)))
7533     (while (and
7534             (> n 0)
7535             (if unmark
7536                 (gnus-summary-remove-process-mark
7537                  (gnus-summary-article-number))
7538               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7539             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7540       (setq n (1- n)))
7541     (when (/= 0 n)
7542       (gnus-message 7 "No more articles"))
7543     (gnus-summary-recenter)
7544     (gnus-summary-position-point)
7545     n))
7546
7547 (defun gnus-summary-unmark-as-processable (n)
7548   "Remove the process mark from the next N articles.
7549 If N is negative, unmark backward instead.  The difference between N and
7550 the actual number of articles unmarked is returned."
7551   (interactive "p")
7552   (gnus-summary-mark-as-processable n t))
7553
7554 (defun gnus-summary-unmark-all-processable ()
7555   "Remove the process mark from all articles."
7556   (interactive)
7557   (save-excursion
7558     (while gnus-newsgroup-processable
7559       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7560   (gnus-summary-position-point))
7561
7562 (defun gnus-summary-mark-as-expirable (n)
7563   "Mark N articles forward as expirable.
7564 If N is negative, mark backward instead.  The difference between N and
7565 the actual number of articles marked is returned."
7566   (interactive "p")
7567   (gnus-summary-mark-forward n gnus-expirable-mark))
7568
7569 (defun gnus-summary-mark-article-as-replied (article)
7570   "Mark ARTICLE replied and update the summary line."
7571   (push article gnus-newsgroup-replied)
7572   (let ((buffer-read-only nil))
7573     (when (gnus-summary-goto-subject article)
7574       (gnus-summary-update-secondary-mark article))))
7575
7576 (defun gnus-summary-set-bookmark (article)
7577   "Set a bookmark in current article."
7578   (interactive (list (gnus-summary-article-number)))
7579   (when (or (not (get-buffer gnus-article-buffer))
7580             (not gnus-current-article)
7581             (not gnus-article-current)
7582             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7583     (error "No current article selected"))
7584   ;; Remove old bookmark, if one exists.
7585   (let ((old (assq article gnus-newsgroup-bookmarks)))
7586     (when old
7587       (setq gnus-newsgroup-bookmarks
7588             (delq old gnus-newsgroup-bookmarks))))
7589   ;; Set the new bookmark, which is on the form
7590   ;; (article-number . line-number-in-body).
7591   (push
7592    (cons article
7593          (save-excursion
7594            (set-buffer gnus-article-buffer)
7595            (count-lines
7596             (min (point)
7597                  (save-excursion
7598                    (goto-char (point-min))
7599                    (search-forward "\n\n" nil t)
7600                    (point)))
7601             (point))))
7602    gnus-newsgroup-bookmarks)
7603   (gnus-message 6 "A bookmark has been added to the current article."))
7604
7605 (defun gnus-summary-remove-bookmark (article)
7606   "Remove the bookmark from the current article."
7607   (interactive (list (gnus-summary-article-number)))
7608   ;; Remove old bookmark, if one exists.
7609   (let ((old (assq article gnus-newsgroup-bookmarks)))
7610     (if old
7611         (progn
7612           (setq gnus-newsgroup-bookmarks
7613                 (delq old gnus-newsgroup-bookmarks))
7614           (gnus-message 6 "Removed bookmark."))
7615       (gnus-message 6 "No bookmark in current article."))))
7616
7617 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7618 (defun gnus-summary-mark-as-dormant (n)
7619   "Mark N articles forward as dormant.
7620 If N is negative, mark backward instead.  The difference between N and
7621 the actual number of articles marked is returned."
7622   (interactive "p")
7623   (gnus-summary-mark-forward n gnus-dormant-mark))
7624
7625 (defun gnus-summary-set-process-mark (article)
7626   "Set the process mark on ARTICLE and update the summary line."
7627   (setq gnus-newsgroup-processable
7628         (cons article
7629               (delq article gnus-newsgroup-processable)))
7630   (when (gnus-summary-goto-subject article)
7631     (gnus-summary-show-thread)
7632     (gnus-summary-goto-subject article)
7633     (gnus-summary-update-secondary-mark article)))
7634
7635 (defun gnus-summary-remove-process-mark (article)
7636   "Remove the process mark from ARTICLE and update the summary line."
7637   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7638   (when (gnus-summary-goto-subject article)
7639     (gnus-summary-show-thread)
7640     (gnus-summary-goto-subject article)
7641     (gnus-summary-update-secondary-mark article)))
7642
7643 (defun gnus-summary-set-saved-mark (article)
7644   "Set the process mark on ARTICLE and update the summary line."
7645   (push article gnus-newsgroup-saved)
7646   (when (gnus-summary-goto-subject article)
7647     (gnus-summary-update-secondary-mark article)))
7648
7649 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7650   "Mark N articles as read forwards.
7651 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7652 The difference between N and the actual number of articles marked is
7653 returned."
7654   (interactive "p")
7655   (let ((backward (< n 0))
7656         (gnus-summary-goto-unread
7657          (and gnus-summary-goto-unread
7658               (not (eq gnus-summary-goto-unread 'never))
7659               (not (memq mark (list gnus-unread-mark
7660                                     gnus-ticked-mark gnus-dormant-mark)))))
7661         (n (abs n))
7662         (mark (or mark gnus-del-mark)))
7663     (while (and (> n 0)
7664                 (gnus-summary-mark-article nil mark no-expire)
7665                 (zerop (gnus-summary-next-subject
7666                         (if backward -1 1)
7667                         (and gnus-summary-goto-unread
7668                              (not (eq gnus-summary-goto-unread 'never)))
7669                         t)))
7670       (setq n (1- n)))
7671     (when (/= 0 n)
7672       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7673     (gnus-summary-recenter)
7674     (gnus-summary-position-point)
7675     (gnus-set-mode-line 'summary)
7676     n))
7677
7678 (defun gnus-summary-mark-article-as-read (mark)
7679   "Mark the current article quickly as read with MARK."
7680   (let ((article (gnus-summary-article-number)))
7681     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7682     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7683     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7684     (push (cons article mark) gnus-newsgroup-reads)
7685     ;; Possibly remove from cache, if that is used.
7686     (when gnus-use-cache
7687       (gnus-cache-enter-remove-article article))
7688     ;; Allow the backend to change the mark.
7689     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7690     ;; Check for auto-expiry.
7691     (when (and gnus-newsgroup-auto-expire
7692                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7693                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7694                    (= mark gnus-ancient-mark)
7695                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7696                    (= mark gnus-duplicate-mark)))
7697       (setq mark gnus-expirable-mark)
7698       (push article gnus-newsgroup-expirable))
7699     ;; Set the mark in the buffer.
7700     (gnus-summary-update-mark mark 'unread)
7701     t))
7702
7703 (defun gnus-summary-mark-article-as-unread (mark)
7704   "Mark the current article quickly as unread with MARK."
7705   (let* ((article (gnus-summary-article-number))
7706          (old-mark (gnus-summary-article-mark article)))
7707     (if (eq mark old-mark)
7708         t
7709       (if (<= article 0)
7710           (progn
7711             (gnus-error 1 "Can't mark negative article numbers")
7712             nil)
7713         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7714         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7715         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7716         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7717         (cond ((= mark gnus-ticked-mark)
7718                (push article gnus-newsgroup-marked))
7719               ((= mark gnus-dormant-mark)
7720                (push article gnus-newsgroup-dormant))
7721               (t
7722                (push article gnus-newsgroup-unreads)))
7723         (gnus-pull article gnus-newsgroup-reads)
7724
7725         ;; See whether the article is to be put in the cache.
7726         (and gnus-use-cache
7727              (vectorp (gnus-summary-article-header article))
7728              (save-excursion
7729                (gnus-cache-possibly-enter-article
7730                 gnus-newsgroup-name article
7731                 (gnus-summary-article-header article)
7732                 (= mark gnus-ticked-mark)
7733                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7734
7735         ;; Fix the mark.
7736         (gnus-summary-update-mark mark 'unread)
7737         t))))
7738
7739 (defun gnus-summary-mark-article (&optional article mark no-expire)
7740   "Mark ARTICLE with MARK.  MARK can be any character.
7741 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7742 `??' (dormant) and `?E' (expirable).
7743 If MARK is nil, then the default character `?D' is used.
7744 If ARTICLE is nil, then the article on the current line will be
7745 marked."
7746   ;; The mark might be a string.
7747   (when (stringp mark)
7748     (setq mark (aref mark 0)))
7749   ;; If no mark is given, then we check auto-expiring.
7750   (and (not no-expire)
7751        gnus-newsgroup-auto-expire
7752        (or (not mark)
7753            (and (gnus-characterp mark)
7754                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7755                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7756                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7757                     (= mark gnus-duplicate-mark))))
7758        (setq mark gnus-expirable-mark))
7759   (let* ((mark (or mark gnus-del-mark))
7760          (article (or article (gnus-summary-article-number)))
7761          (old-mark (gnus-summary-article-mark article)))
7762     (if (eq mark old-mark)
7763         t
7764       (unless article
7765         (error "No article on current line"))
7766       (if (not (if (or (= mark gnus-unread-mark)
7767                        (= mark gnus-ticked-mark)
7768                        (= mark gnus-dormant-mark))
7769                    (gnus-mark-article-as-unread article mark)
7770                  (gnus-mark-article-as-read article mark)))
7771           t
7772         ;; See whether the article is to be put in the cache.
7773         (and gnus-use-cache
7774              (not (= mark gnus-canceled-mark))
7775              (vectorp (gnus-summary-article-header article))
7776              (save-excursion
7777                (gnus-cache-possibly-enter-article
7778                 gnus-newsgroup-name article
7779                 (gnus-summary-article-header article)
7780                 (= mark gnus-ticked-mark)
7781                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7782
7783         (when (gnus-summary-goto-subject article nil t)
7784           (let ((buffer-read-only nil))
7785             (gnus-summary-show-thread)
7786             ;; Fix the mark.
7787             (gnus-summary-update-mark mark 'unread)
7788             t))))))
7789
7790 (defun gnus-summary-update-secondary-mark (article)
7791   "Update the secondary (read, process, cache) mark."
7792   (gnus-summary-update-mark
7793    (cond ((memq article gnus-newsgroup-processable)
7794           gnus-process-mark)
7795          ((memq article gnus-newsgroup-cached)
7796           gnus-cached-mark)
7797          ((memq article gnus-newsgroup-replied)
7798           gnus-replied-mark)
7799          ((memq article gnus-newsgroup-saved)
7800           gnus-saved-mark)
7801          (t gnus-unread-mark))
7802    'replied)
7803   (when (gnus-visual-p 'summary-highlight 'highlight)
7804     (gnus-run-hooks 'gnus-summary-update-hook))
7805   t)
7806
7807 (defun gnus-summary-update-mark (mark type)
7808   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7809         (buffer-read-only nil))
7810     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7811     (when (looking-at "\r")
7812       (incf forward))
7813     (when (and forward
7814                (<= (+ forward (point)) (point-max)))
7815       ;; Go to the right position on the line.
7816       (goto-char (+ forward (point)))
7817       ;; Replace the old mark with the new mark.
7818       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7819       ;; Optionally update the marks by some user rule.
7820       (when (eq type 'unread)
7821         (gnus-data-set-mark
7822          (gnus-data-find (gnus-summary-article-number)) mark)
7823         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7824
7825 (defun gnus-mark-article-as-read (article &optional mark)
7826   "Enter ARTICLE in the pertinent lists and remove it from others."
7827   ;; Make the article expirable.
7828   (let ((mark (or mark gnus-del-mark)))
7829     (if (= mark gnus-expirable-mark)
7830         (push article gnus-newsgroup-expirable)
7831       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7832     ;; Remove from unread and marked lists.
7833     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7834     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7835     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7836     (push (cons article mark) gnus-newsgroup-reads)
7837     ;; Possibly remove from cache, if that is used.
7838     (when gnus-use-cache
7839       (gnus-cache-enter-remove-article article))
7840     t))
7841
7842 (defun gnus-mark-article-as-unread (article &optional mark)
7843   "Enter ARTICLE in the pertinent lists and remove it from others."
7844   (let ((mark (or mark gnus-ticked-mark)))
7845     (if (<= article 0)
7846         (progn
7847           (gnus-error 1 "Can't mark negative article numbers")
7848           nil)
7849       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7850             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7851             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7852             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7853
7854       ;; Unsuppress duplicates?
7855       (when gnus-suppress-duplicates
7856         (gnus-dup-unsuppress-article article))
7857
7858       (cond ((= mark gnus-ticked-mark)
7859              (push article gnus-newsgroup-marked))
7860             ((= mark gnus-dormant-mark)
7861              (push article gnus-newsgroup-dormant))
7862             (t
7863              (push article gnus-newsgroup-unreads)))
7864       (gnus-pull article gnus-newsgroup-reads)
7865       t)))
7866
7867 (defalias 'gnus-summary-mark-as-unread-forward
7868   'gnus-summary-tick-article-forward)
7869 (make-obsolete 'gnus-summary-mark-as-unread-forward
7870                'gnus-summary-tick-article-forward)
7871 (defun gnus-summary-tick-article-forward (n)
7872   "Tick N articles forwards.
7873 If N is negative, tick backwards instead.
7874 The difference between N and the number of articles ticked is returned."
7875   (interactive "p")
7876   (gnus-summary-mark-forward n gnus-ticked-mark))
7877
7878 (defalias 'gnus-summary-mark-as-unread-backward
7879   'gnus-summary-tick-article-backward)
7880 (make-obsolete 'gnus-summary-mark-as-unread-backward
7881                'gnus-summary-tick-article-backward)
7882 (defun gnus-summary-tick-article-backward (n)
7883   "Tick N articles backwards.
7884 The difference between N and the number of articles ticked is returned."
7885   (interactive "p")
7886   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7887
7888 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7889 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7890 (defun gnus-summary-tick-article (&optional article clear-mark)
7891   "Mark current article as unread.
7892 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7893 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7894   (interactive)
7895   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7896                                        gnus-ticked-mark)))
7897
7898 (defun gnus-summary-mark-as-read-forward (n)
7899   "Mark N articles as read forwards.
7900 If N is negative, mark backwards instead.
7901 The difference between N and the actual number of articles marked is
7902 returned."
7903   (interactive "p")
7904   (gnus-summary-mark-forward n gnus-del-mark t))
7905
7906 (defun gnus-summary-mark-as-read-backward (n)
7907   "Mark the N articles as read backwards.
7908 The difference between N and the actual number of articles marked is
7909 returned."
7910   (interactive "p")
7911   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7912
7913 (defun gnus-summary-mark-as-read (&optional article mark)
7914   "Mark current article as read.
7915 ARTICLE specifies the article to be marked as read.
7916 MARK specifies a string to be inserted at the beginning of the line."
7917   (gnus-summary-mark-article article mark))
7918
7919 (defun gnus-summary-clear-mark-forward (n)
7920   "Clear marks from N articles forward.
7921 If N is negative, clear backward instead.
7922 The difference between N and the number of marks cleared is returned."
7923   (interactive "p")
7924   (gnus-summary-mark-forward n gnus-unread-mark))
7925
7926 (defun gnus-summary-clear-mark-backward (n)
7927   "Clear marks from N articles backward.
7928 The difference between N and the number of marks cleared is returned."
7929   (interactive "p")
7930   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7931
7932 (defun gnus-summary-mark-unread-as-read ()
7933   "Intended to be used by `gnus-summary-mark-article-hook'."
7934   (when (memq gnus-current-article gnus-newsgroup-unreads)
7935     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7936
7937 (defun gnus-summary-mark-read-and-unread-as-read ()
7938   "Intended to be used by `gnus-summary-mark-article-hook'."
7939   (let ((mark (gnus-summary-article-mark)))
7940     (when (or (gnus-unread-mark-p mark)
7941               (gnus-read-mark-p mark))
7942       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7943
7944 (defun gnus-summary-mark-region-as-read (point mark all)
7945   "Mark all unread articles between point and mark as read.
7946 If given a prefix, mark all articles between point and mark as read,
7947 even ticked and dormant ones."
7948   (interactive "r\nP")
7949   (save-excursion
7950     (let (article)
7951       (goto-char point)
7952       (beginning-of-line)
7953       (while (and
7954               (< (point) mark)
7955               (progn
7956                 (when (or all
7957                           (memq (setq article (gnus-summary-article-number))
7958                                 gnus-newsgroup-unreads))
7959                   (gnus-summary-mark-article article gnus-del-mark))
7960                 t)
7961               (gnus-summary-find-next))))))
7962
7963 (defun gnus-summary-mark-below (score mark)
7964   "Mark articles with score less than SCORE with MARK."
7965   (interactive "P\ncMark: ")
7966   (setq score (if score
7967                   (prefix-numeric-value score)
7968                 (or gnus-summary-default-score 0)))
7969   (save-excursion
7970     (set-buffer gnus-summary-buffer)
7971     (goto-char (point-min))
7972     (while
7973         (progn
7974           (and (< (gnus-summary-article-score) score)
7975                (gnus-summary-mark-article nil mark))
7976           (gnus-summary-find-next)))))
7977
7978 (defun gnus-summary-kill-below (&optional score)
7979   "Mark articles with score below SCORE as read."
7980   (interactive "P")
7981   (gnus-summary-mark-below score gnus-killed-mark))
7982
7983 (defun gnus-summary-clear-above (&optional score)
7984   "Clear all marks from articles with score above SCORE."
7985   (interactive "P")
7986   (gnus-summary-mark-above score gnus-unread-mark))
7987
7988 (defun gnus-summary-tick-above (&optional score)
7989   "Tick all articles with score above SCORE."
7990   (interactive "P")
7991   (gnus-summary-mark-above score gnus-ticked-mark))
7992
7993 (defun gnus-summary-mark-above (score mark)
7994   "Mark articles with score over SCORE with MARK."
7995   (interactive "P\ncMark: ")
7996   (setq score (if score
7997                   (prefix-numeric-value score)
7998                 (or gnus-summary-default-score 0)))
7999   (save-excursion
8000     (set-buffer gnus-summary-buffer)
8001     (goto-char (point-min))
8002     (while (and (progn
8003                   (when (> (gnus-summary-article-score) score)
8004                     (gnus-summary-mark-article nil mark))
8005                   t)
8006                 (gnus-summary-find-next)))))
8007
8008 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8009 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8010 (defun gnus-summary-limit-include-expunged (&optional no-error)
8011   "Display all the hidden articles that were expunged for low scores."
8012   (interactive)
8013   (let ((buffer-read-only nil))
8014     (let ((scored gnus-newsgroup-scored)
8015           headers h)
8016       (while scored
8017         (unless (gnus-summary-goto-subject (caar scored))
8018           (and (setq h (gnus-summary-article-header (caar scored)))
8019                (< (cdar scored) gnus-summary-expunge-below)
8020                (push h headers)))
8021         (setq scored (cdr scored)))
8022       (if (not headers)
8023           (when (not no-error)
8024             (error "No expunged articles hidden"))
8025         (goto-char (point-min))
8026         (gnus-summary-prepare-unthreaded (nreverse headers))
8027         (goto-char (point-min))
8028         (gnus-summary-position-point)
8029         t))))
8030
8031 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8032   "Mark all unread articles in this newsgroup as read.
8033 If prefix argument ALL is non-nil, ticked and dormant articles will
8034 also be marked as read.
8035 If QUIETLY is non-nil, no questions will be asked.
8036 If TO-HERE is non-nil, it should be a point in the buffer.  All
8037 articles before this point will be marked as read.
8038 Note that this function will only catch up the unread article
8039 in the current summary buffer limitation.
8040 The number of articles marked as read is returned."
8041   (interactive "P")
8042   (prog1
8043       (save-excursion
8044         (when (or quietly
8045                   (not gnus-interactive-catchup) ;Without confirmation?
8046                   gnus-expert-user
8047                   (gnus-y-or-n-p
8048                    (if all
8049                        "Mark absolutely all articles as read? "
8050                      "Mark all unread articles as read? ")))
8051           (if (and not-mark
8052                    (not gnus-newsgroup-adaptive)
8053                    (not gnus-newsgroup-auto-expire)
8054                    (not gnus-suppress-duplicates)
8055                    (or (not gnus-use-cache)
8056                        (eq gnus-use-cache 'passive)))
8057               (progn
8058                 (when all
8059                   (setq gnus-newsgroup-marked nil
8060                         gnus-newsgroup-dormant nil))
8061                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8062             ;; We actually mark all articles as canceled, which we
8063             ;; have to do when using auto-expiry or adaptive scoring.
8064             (gnus-summary-show-all-threads)
8065             (when (gnus-summary-first-subject (not all) t)
8066               (while (and
8067                       (if to-here (< (point) to-here) t)
8068                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8069                       (gnus-summary-find-next (not all) nil nil t))))
8070             (gnus-set-mode-line 'summary))
8071           t))
8072     (gnus-summary-position-point)))
8073
8074 (defun gnus-summary-catchup-to-here (&optional all)
8075   "Mark all unticked articles before the current one as read.
8076 If ALL is non-nil, also mark ticked and dormant articles as read."
8077   (interactive "P")
8078   (save-excursion
8079     (gnus-save-hidden-threads
8080       (let ((beg (point)))
8081         ;; We check that there are unread articles.
8082         (when (or all (gnus-summary-find-prev))
8083           (gnus-summary-catchup all t beg)))))
8084   (gnus-summary-position-point))
8085
8086 (defun gnus-summary-catchup-all (&optional quietly)
8087   "Mark all articles in this newsgroup as read."
8088   (interactive "P")
8089   (gnus-summary-catchup t quietly))
8090
8091 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8092   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8093 If prefix argument ALL is non-nil, all articles are marked as read."
8094   (interactive "P")
8095   (when (gnus-summary-catchup all quietly nil 'fast)
8096     ;; Select next newsgroup or exit.
8097     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8098              (eq gnus-auto-select-next 'quietly))
8099         (gnus-summary-next-group nil)
8100       (gnus-summary-exit))))
8101
8102 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8103   "Mark all articles in this newsgroup as read, and then exit."
8104   (interactive "P")
8105   (gnus-summary-catchup-and-exit t quietly))
8106
8107 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8108 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8109   "Mark all articles in this group as read and select the next group.
8110 If given a prefix, mark all articles, unread as well as ticked, as
8111 read."
8112   (interactive "P")
8113   (save-excursion
8114     (gnus-summary-catchup all))
8115   (gnus-summary-next-article t nil nil t))
8116
8117 ;; Thread-based commands.
8118
8119 (defun gnus-summary-articles-in-thread (&optional article)
8120   "Return a list of all articles in the current thread.
8121 If ARTICLE is non-nil, return all articles in the thread that starts
8122 with that article."
8123   (let* ((article (or article (gnus-summary-article-number)))
8124          (data (gnus-data-find-list article))
8125          (top-level (gnus-data-level (car data)))
8126          (top-subject
8127           (cond ((null gnus-thread-operation-ignore-subject)
8128                  (gnus-simplify-subject-re
8129                   (mail-header-subject (gnus-data-header (car data)))))
8130                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8131                  (gnus-simplify-subject-fuzzy
8132                   (mail-header-subject (gnus-data-header (car data)))))
8133                 (t nil)))
8134          (end-point (save-excursion
8135                       (if (gnus-summary-go-to-next-thread)
8136                           (point) (point-max))))
8137          articles)
8138     (while (and data
8139                 (< (gnus-data-pos (car data)) end-point))
8140       (when (or (not top-subject)
8141                 (string= top-subject
8142                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8143                              (gnus-simplify-subject-fuzzy
8144                               (mail-header-subject
8145                                (gnus-data-header (car data))))
8146                            (gnus-simplify-subject-re
8147                             (mail-header-subject
8148                              (gnus-data-header (car data)))))))
8149         (push (gnus-data-number (car data)) articles))
8150       (unless (and (setq data (cdr data))
8151                    (> (gnus-data-level (car data)) top-level))
8152         (setq data nil)))
8153     ;; Return the list of articles.
8154     (nreverse articles)))
8155
8156 (defun gnus-summary-rethread-current ()
8157   "Rethread the thread the current article is part of."
8158   (interactive)
8159   (let* ((gnus-show-threads t)
8160          (article (gnus-summary-article-number))
8161          (id (mail-header-id (gnus-summary-article-header)))
8162          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8163     (unless id
8164       (error "No article on the current line"))
8165     (gnus-rebuild-thread id)
8166     (gnus-summary-goto-subject article)))
8167
8168 (defun gnus-summary-reparent-thread ()
8169   "Make the current article child of the marked (or previous) article.
8170
8171 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8172 is non-nil or the Subject: of both articles are the same."
8173   (interactive)
8174   (unless (not (gnus-group-read-only-p))
8175     (error "The current newsgroup does not support article editing"))
8176   (unless (<= (length gnus-newsgroup-processable) 1)
8177     (error "No more than one article may be marked"))
8178   (save-window-excursion
8179     (let ((gnus-article-buffer " *reparent*")
8180           (current-article (gnus-summary-article-number))
8181           ;; First grab the marked article, otherwise one line up.
8182           (parent-article (if (not (null gnus-newsgroup-processable))
8183                               (car gnus-newsgroup-processable)
8184                             (save-excursion
8185                               (if (eq (forward-line -1) 0)
8186                                   (gnus-summary-article-number)
8187                                 (error "Beginning of summary buffer"))))))
8188       (unless (not (eq current-article parent-article))
8189         (error "An article may not be self-referential"))
8190       (let ((message-id (mail-header-id
8191                          (gnus-summary-article-header parent-article))))
8192         (unless (and message-id (not (equal message-id "")))
8193           (error "No message-id in desired parent"))
8194         ;; We don't want the article to be marked as read.
8195         (let (gnus-mark-article-hook)
8196           (gnus-summary-select-article t t nil current-article))
8197         (set-buffer gnus-original-article-buffer)
8198         (let ((buf (format "%s" (buffer-string))))
8199           (with-temp-buffer
8200             (insert buf)
8201             (goto-char (point-min))
8202             (if (re-search-forward "^References: " nil t)
8203                 (progn
8204                   (re-search-forward "^[^ \t]" nil t)
8205                   (forward-line -1)
8206                   (end-of-line)
8207                   (insert " " message-id))
8208               (insert "References: " message-id "\n"))
8209             (unless (gnus-request-replace-article
8210                      current-article (car gnus-article-current)
8211                      (current-buffer))
8212               (error "Couldn't replace article"))))
8213         (set-buffer gnus-summary-buffer)
8214         (gnus-summary-unmark-all-processable)
8215         (gnus-summary-update-article current-article)
8216         (gnus-summary-rethread-current)
8217         (gnus-message 3 "Article %d is now the child of article %d"
8218                       current-article parent-article)))))
8219
8220 (defun gnus-summary-toggle-threads (&optional arg)
8221   "Toggle showing conversation threads.
8222 If ARG is positive number, turn showing conversation threads on."
8223   (interactive "P")
8224   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8225     (setq gnus-show-threads
8226           (if (null arg) (not gnus-show-threads)
8227             (> (prefix-numeric-value arg) 0)))
8228     (gnus-summary-prepare)
8229     (gnus-summary-goto-subject current)
8230     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8231     (gnus-summary-position-point)))
8232
8233 (defun gnus-summary-show-all-threads ()
8234   "Show all threads."
8235   (interactive)
8236   (save-excursion
8237     (let ((buffer-read-only nil))
8238       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8239   (gnus-summary-position-point))
8240
8241 (defun gnus-summary-show-thread ()
8242   "Show thread subtrees.
8243 Returns nil if no thread was there to be shown."
8244   (interactive)
8245   (let ((buffer-read-only nil)
8246         (orig (point))
8247         ;; first goto end then to beg, to have point at beg after let
8248         (end (progn (end-of-line) (point)))
8249         (beg (progn (beginning-of-line) (point))))
8250     (prog1
8251         ;; Any hidden lines here?
8252         (search-forward "\r" end t)
8253       (subst-char-in-region beg end ?\^M ?\n t)
8254       (goto-char orig)
8255       (gnus-summary-position-point))))
8256
8257 (defun gnus-summary-hide-all-threads ()
8258   "Hide all thread subtrees."
8259   (interactive)
8260   (save-excursion
8261     (goto-char (point-min))
8262     (gnus-summary-hide-thread)
8263     (while (zerop (gnus-summary-next-thread 1 t))
8264       (gnus-summary-hide-thread)))
8265   (gnus-summary-position-point))
8266
8267 (defun gnus-summary-hide-thread ()
8268   "Hide thread subtrees.
8269 Returns nil if no threads were there to be hidden."
8270   (interactive)
8271   (let ((buffer-read-only nil)
8272         (start (point))
8273         (article (gnus-summary-article-number)))
8274     (goto-char start)
8275     ;; Go forward until either the buffer ends or the subthread
8276     ;; ends.
8277     (when (and (not (eobp))
8278                (or (zerop (gnus-summary-next-thread 1 t))
8279                    (goto-char (point-max))))
8280       (prog1
8281           (if (and (> (point) start)
8282                    (search-backward "\n" start t))
8283               (progn
8284                 (subst-char-in-region start (point) ?\n ?\^M)
8285                 (gnus-summary-goto-subject article))
8286             (goto-char start)
8287             nil)
8288         ;;(gnus-summary-position-point)
8289         ))))
8290
8291 (defun gnus-summary-go-to-next-thread (&optional previous)
8292   "Go to the same level (or less) next thread.
8293 If PREVIOUS is non-nil, go to previous thread instead.
8294 Return the article number moved to, or nil if moving was impossible."
8295   (let ((level (gnus-summary-thread-level))
8296         (way (if previous -1 1))
8297         (beg (point)))
8298     (forward-line way)
8299     (while (and (not (eobp))
8300                 (< level (gnus-summary-thread-level)))
8301       (forward-line way))
8302     (if (eobp)
8303         (progn
8304           (goto-char beg)
8305           nil)
8306       (setq beg (point))
8307       (prog1
8308           (gnus-summary-article-number)
8309         (goto-char beg)))))
8310
8311 (defun gnus-summary-next-thread (n &optional silent)
8312   "Go to the same level next N'th thread.
8313 If N is negative, search backward instead.
8314 Returns the difference between N and the number of skips actually
8315 done.
8316
8317 If SILENT, don't output messages."
8318   (interactive "p")
8319   (let ((backward (< n 0))
8320         (n (abs n)))
8321     (while (and (> n 0)
8322                 (gnus-summary-go-to-next-thread backward))
8323       (decf n))
8324     (unless silent
8325       (gnus-summary-position-point))
8326     (when (and (not silent) (/= 0 n))
8327       (gnus-message 7 "No more threads"))
8328     n))
8329
8330 (defun gnus-summary-prev-thread (n)
8331   "Go to the same level previous N'th thread.
8332 Returns the difference between N and the number of skips actually
8333 done."
8334   (interactive "p")
8335   (gnus-summary-next-thread (- n)))
8336
8337 (defun gnus-summary-go-down-thread ()
8338   "Go down one level in the current thread."
8339   (let ((children (gnus-summary-article-children)))
8340     (when children
8341       (gnus-summary-goto-subject (car children)))))
8342
8343 (defun gnus-summary-go-up-thread ()
8344   "Go up one level in the current thread."
8345   (let ((parent (gnus-summary-article-parent)))
8346     (when parent
8347       (gnus-summary-goto-subject parent))))
8348
8349 (defun gnus-summary-down-thread (n)
8350   "Go down thread N steps.
8351 If N is negative, go up instead.
8352 Returns the difference between N and how many steps down that were
8353 taken."
8354   (interactive "p")
8355   (let ((up (< n 0))
8356         (n (abs n)))
8357     (while (and (> n 0)
8358                 (if up (gnus-summary-go-up-thread)
8359                   (gnus-summary-go-down-thread)))
8360       (setq n (1- n)))
8361     (gnus-summary-position-point)
8362     (when (/= 0 n)
8363       (gnus-message 7 "Can't go further"))
8364     n))
8365
8366 (defun gnus-summary-up-thread (n)
8367   "Go up thread N steps.
8368 If N is negative, go up instead.
8369 Returns the difference between N and how many steps down that were
8370 taken."
8371   (interactive "p")
8372   (gnus-summary-down-thread (- n)))
8373
8374 (defun gnus-summary-top-thread ()
8375   "Go to the top of the thread."
8376   (interactive)
8377   (while (gnus-summary-go-up-thread))
8378   (gnus-summary-article-number))
8379
8380 (defun gnus-summary-kill-thread (&optional unmark)
8381   "Mark articles under current thread as read.
8382 If the prefix argument is positive, remove any kinds of marks.
8383 If the prefix argument is negative, tick articles instead."
8384   (interactive "P")
8385   (when unmark
8386     (setq unmark (prefix-numeric-value unmark)))
8387   (let ((articles (gnus-summary-articles-in-thread)))
8388     (save-excursion
8389       ;; Expand the thread.
8390       (gnus-summary-show-thread)
8391       ;; Mark all the articles.
8392       (while articles
8393         (gnus-summary-goto-subject (car articles))
8394         (cond ((null unmark)
8395                (gnus-summary-mark-article-as-read gnus-killed-mark))
8396               ((> unmark 0)
8397                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8398               (t
8399                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8400         (setq articles (cdr articles))))
8401     ;; Hide killed subtrees.
8402     (and (null unmark)
8403          gnus-thread-hide-killed
8404          (gnus-summary-hide-thread))
8405     ;; If marked as read, go to next unread subject.
8406     (when (null unmark)
8407       ;; Go to next unread subject.
8408       (gnus-summary-next-subject 1 t)))
8409   (gnus-set-mode-line 'summary))
8410
8411 ;; Summary sorting commands
8412
8413 (defun gnus-summary-sort-by-number (&optional reverse)
8414   "Sort the summary buffer by article number.
8415 Argument REVERSE means reverse order."
8416   (interactive "P")
8417   (gnus-summary-sort 'number reverse))
8418
8419 (defun gnus-summary-sort-by-author (&optional reverse)
8420   "Sort the summary buffer by author name alphabetically.
8421 If case-fold-search is non-nil, case of letters is ignored.
8422 Argument REVERSE means reverse order."
8423   (interactive "P")
8424   (gnus-summary-sort 'author reverse))
8425
8426 (defun gnus-summary-sort-by-subject (&optional reverse)
8427   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8428 If case-fold-search is non-nil, case of letters is ignored.
8429 Argument REVERSE means reverse order."
8430   (interactive "P")
8431   (gnus-summary-sort 'subject reverse))
8432
8433 (defun gnus-summary-sort-by-date (&optional reverse)
8434   "Sort the summary buffer by date.
8435 Argument REVERSE means reverse order."
8436   (interactive "P")
8437   (gnus-summary-sort 'date reverse))
8438
8439 (defun gnus-summary-sort-by-score (&optional reverse)
8440   "Sort the summary buffer by score.
8441 Argument REVERSE means reverse order."
8442   (interactive "P")
8443   (gnus-summary-sort 'score reverse))
8444
8445 (defun gnus-summary-sort-by-lines (&optional reverse)
8446   "Sort the summary buffer by article length.
8447 Argument REVERSE means reverse order."
8448   (interactive "P")
8449   (gnus-summary-sort 'lines reverse))
8450
8451 (defun gnus-summary-sort (predicate reverse)
8452   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8453   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8454          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8455          (gnus-thread-sort-functions
8456           (list
8457            (if (not reverse)
8458                thread
8459              `(lambda (t1 t2)
8460                 (,thread t2 t1)))))
8461          (gnus-article-sort-functions
8462           (list
8463            (if (not reverse)
8464                article
8465              `(lambda (t1 t2)
8466                 (,article t2 t1)))))
8467          (buffer-read-only)
8468          (gnus-summary-prepare-hook nil))
8469     ;; We do the sorting by regenerating the threads.
8470     (gnus-summary-prepare)
8471     ;; Hide subthreads if needed.
8472     (when (and gnus-show-threads gnus-thread-hide-subtree)
8473       (gnus-summary-hide-all-threads))))
8474
8475 ;; Summary saving commands.
8476
8477 (defun gnus-summary-save-article (&optional n not-saved)
8478   "Save the current article using the default saver function.
8479 If N is a positive number, save the N next articles.
8480 If N is a negative number, save the N previous articles.
8481 If N is nil and any articles have been marked with the process mark,
8482 save those articles instead.
8483 The variable `gnus-default-article-saver' specifies the saver function."
8484   (interactive "P")
8485   (let* ((articles (gnus-summary-work-articles n))
8486          (save-buffer (save-excursion
8487                         (nnheader-set-temp-buffer " *Gnus Save*")))
8488          (num (length articles))
8489          header article file)
8490     (while articles
8491       (setq header (gnus-summary-article-header
8492                     (setq article (pop articles))))
8493       (if (not (vectorp header))
8494           ;; This is a pseudo-article.
8495           (if (assq 'name header)
8496               (gnus-copy-file (cdr (assq 'name header)))
8497             (gnus-message 1 "Article %d is unsaveable" article))
8498         ;; This is a real article.
8499         (save-window-excursion
8500           (gnus-summary-select-article t nil nil article))
8501         (save-excursion
8502           (set-buffer save-buffer)
8503           (erase-buffer)
8504           (insert-buffer-substring gnus-original-article-buffer))
8505         (setq file (gnus-article-save save-buffer file num))
8506         (gnus-summary-remove-process-mark article)
8507         (unless not-saved
8508           (gnus-summary-set-saved-mark article))))
8509     (gnus-kill-buffer save-buffer)
8510     (gnus-summary-position-point)
8511     (gnus-set-mode-line 'summary)
8512     n))
8513
8514 (defun gnus-summary-pipe-output (&optional arg)
8515   "Pipe the current article to a subprocess.
8516 If N is a positive number, pipe the N next articles.
8517 If N is a negative number, pipe the N previous articles.
8518 If N is nil and any articles have been marked with the process mark,
8519 pipe those articles instead."
8520   (interactive "P")
8521   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8522     (gnus-summary-save-article arg t))
8523   (gnus-configure-windows 'pipe))
8524
8525 (defun gnus-summary-save-article-mail (&optional arg)
8526   "Append the current article to an mail file.
8527 If N is a positive number, save the N next articles.
8528 If N is a negative number, save the N previous articles.
8529 If N is nil and any articles have been marked with the process mark,
8530 save those articles instead."
8531   (interactive "P")
8532   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8533     (gnus-summary-save-article arg)))
8534
8535 (defun gnus-summary-save-article-rmail (&optional arg)
8536   "Append the current article to an rmail file.
8537 If N is a positive number, save the N next articles.
8538 If N is a negative number, save the N previous articles.
8539 If N is nil and any articles have been marked with the process mark,
8540 save those articles instead."
8541   (interactive "P")
8542   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8543     (gnus-summary-save-article arg)))
8544
8545 (defun gnus-summary-save-article-file (&optional arg)
8546   "Append the current article to a file.
8547 If N is a positive number, save the N next articles.
8548 If N is a negative number, save the N previous articles.
8549 If N is nil and any articles have been marked with the process mark,
8550 save those articles instead."
8551   (interactive "P")
8552   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8553     (gnus-summary-save-article arg)))
8554
8555 (defun gnus-summary-write-article-file (&optional arg)
8556   "Write the current article to a file, deleting the previous file.
8557 If N is a positive number, save the N next articles.
8558 If N is a negative number, save the N previous articles.
8559 If N is nil and any articles have been marked with the process mark,
8560 save those articles instead."
8561   (interactive "P")
8562   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8563     (gnus-summary-save-article arg)))
8564
8565 (defun gnus-summary-save-article-body-file (&optional arg)
8566   "Append the current article body to a file.
8567 If N is a positive number, save the N next articles.
8568 If N is a negative number, save the N previous articles.
8569 If N is nil and any articles have been marked with the process mark,
8570 save those articles instead."
8571   (interactive "P")
8572   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8573     (gnus-summary-save-article arg)))
8574
8575 (defun gnus-summary-pipe-message (program)
8576   "Pipe the current article through PROGRAM."
8577   (interactive "sProgram: ")
8578   (gnus-summary-select-article)
8579   (let ((mail-header-separator ""))
8580     (gnus-eval-in-buffer-window gnus-article-buffer
8581       (save-restriction
8582         (widen)
8583         (let ((start (window-start))
8584               buffer-read-only)
8585           (message-pipe-buffer-body program)
8586           (set-window-start (get-buffer-window (current-buffer)) start))))))
8587
8588 (defun gnus-get-split-value (methods)
8589   "Return a value based on the split METHODS."
8590   (let (split-name method result match)
8591     (when methods
8592       (save-excursion
8593         (set-buffer gnus-original-article-buffer)
8594         (save-restriction
8595           (nnheader-narrow-to-headers)
8596           (while methods
8597             (goto-char (point-min))
8598             (setq method (pop methods))
8599             (setq match (car method))
8600             (when (cond
8601                    ((stringp match)
8602                     ;; Regular expression.
8603                     (ignore-errors
8604                       (re-search-forward match nil t)))
8605                    ((gnus-functionp match)
8606                     ;; Function.
8607                     (save-restriction
8608                       (widen)
8609                       (setq result (funcall match gnus-newsgroup-name))))
8610                    ((consp match)
8611                     ;; Form.
8612                     (save-restriction
8613                       (widen)
8614                       (setq result (eval match)))))
8615               (setq split-name (append (cdr method) split-name))
8616               (cond ((stringp result)
8617                      (push (expand-file-name
8618                             result gnus-article-save-directory)
8619                            split-name))
8620                     ((consp result)
8621                      (setq split-name (append result split-name)))))))))
8622     split-name))
8623
8624 (defun gnus-valid-move-group-p (group)
8625   (and (boundp group)
8626        (symbol-name group)
8627        (memq 'respool
8628              (assoc (symbol-name
8629                      (car (gnus-find-method-for-group
8630                            (symbol-name group))))
8631                     gnus-valid-select-methods))))
8632
8633 (defun gnus-read-move-group-name (prompt default articles prefix)
8634   "Read a group name."
8635   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8636          (minibuffer-confirm-incomplete nil) ; XEmacs
8637          (prom
8638           (format "%s %s to:"
8639                   prompt
8640                   (if (> (length articles) 1)
8641                       (format "these %d articles" (length articles))
8642                     "this article")))
8643          (to-newsgroup
8644           (cond
8645            ((null split-name)
8646             (gnus-completing-read default prom
8647                                   gnus-active-hashtb
8648                                   'gnus-valid-move-group-p
8649                                   nil prefix
8650                                   'gnus-group-history))
8651            ((= 1 (length split-name))
8652             (gnus-completing-read (car split-name) prom
8653                                   gnus-active-hashtb
8654                                   'gnus-valid-move-group-p
8655                                   nil nil
8656                                   'gnus-group-history))
8657            (t
8658             (gnus-completing-read nil prom
8659                                   (mapcar (lambda (el) (list el))
8660                                           (nreverse split-name))
8661                                   nil nil nil
8662                                   'gnus-group-history)))))
8663     (when to-newsgroup
8664       (if (or (string= to-newsgroup "")
8665               (string= to-newsgroup prefix))
8666           (setq to-newsgroup default))
8667       (unless to-newsgroup
8668         (error "No group name entered"))
8669       (or (gnus-active to-newsgroup)
8670           (gnus-activate-group to-newsgroup)
8671           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8672                                      to-newsgroup))
8673               (or (and (gnus-request-create-group
8674                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8675                        (gnus-activate-group to-newsgroup nil nil
8676                                             (gnus-group-name-to-method
8677                                              to-newsgroup)))
8678                   (error "Couldn't create group %s" to-newsgroup)))
8679           (error "No such group: %s" to-newsgroup)))
8680     to-newsgroup))
8681
8682 ;; Summary extract commands
8683
8684 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8685   (let ((buffer-read-only nil)
8686         (article (gnus-summary-article-number))
8687         after-article b e)
8688     (unless (gnus-summary-goto-subject article)
8689       (error "No such article: %d" article))
8690     (gnus-summary-position-point)
8691     ;; If all commands are to be bunched up on one line, we collect
8692     ;; them here.
8693     (unless gnus-view-pseudos-separately
8694       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8695             files action)
8696         (while ps
8697           (setq action (cdr (assq 'action (car ps))))
8698           (setq files (list (cdr (assq 'name (car ps)))))
8699           (while (and ps (cdr ps)
8700                       (string= (or action "1")
8701                                (or (cdr (assq 'action (cadr ps))) "2")))
8702             (push (cdr (assq 'name (cadr ps))) files)
8703             (setcdr ps (cddr ps)))
8704           (when files
8705             (when (not (string-match "%s" action))
8706               (push " " files))
8707             (push " " files)
8708             (when (assq 'execute (car ps))
8709               (setcdr (assq 'execute (car ps))
8710                       (funcall (if (string-match "%s" action)
8711                                    'format 'concat)
8712                                action
8713                                (mapconcat
8714                                 (lambda (f)
8715                                   (if (equal f " ")
8716                                       f
8717                                     (gnus-quote-arg-for-sh-or-csh f)))
8718                                 files " ")))))
8719           (setq ps (cdr ps)))))
8720     (if (and gnus-view-pseudos (not not-view))
8721         (while pslist
8722           (when (assq 'execute (car pslist))
8723             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8724                                   (eq gnus-view-pseudos 'not-confirm)))
8725           (setq pslist (cdr pslist)))
8726       (save-excursion
8727         (while pslist
8728           (setq after-article (or (cdr (assq 'article (car pslist)))
8729                                   (gnus-summary-article-number)))
8730           (gnus-summary-goto-subject after-article)
8731           (forward-line 1)
8732           (setq b (point))
8733           (insert "    " (file-name-nondirectory
8734                           (cdr (assq 'name (car pslist))))
8735                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8736           (setq e (point))
8737           (forward-line -1)             ; back to `b'
8738           (gnus-add-text-properties
8739            b (1- e) (list 'gnus-number gnus-reffed-article-number
8740                           gnus-mouse-face-prop gnus-mouse-face))
8741           (gnus-data-enter
8742            after-article gnus-reffed-article-number
8743            gnus-unread-mark b (car pslist) 0 (- e b))
8744           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8745           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8746           (setq pslist (cdr pslist)))))))
8747
8748 (defun gnus-pseudos< (p1 p2)
8749   (let ((c1 (cdr (assq 'action p1)))
8750         (c2 (cdr (assq 'action p2))))
8751     (and c1 c2 (string< c1 c2))))
8752
8753 (defun gnus-request-pseudo-article (props)
8754   (cond ((assq 'execute props)
8755          (gnus-execute-command (cdr (assq 'execute props)))))
8756   (let ((gnus-current-article (gnus-summary-article-number)))
8757     (gnus-run-hooks 'gnus-mark-article-hook)))
8758
8759 (defun gnus-execute-command (command &optional automatic)
8760   (save-excursion
8761     (gnus-article-setup-buffer)
8762     (set-buffer gnus-article-buffer)
8763     (setq buffer-read-only nil)
8764     (let ((command (if automatic command
8765                      (read-string "Command: " (cons command 0)))))
8766       (erase-buffer)
8767       (insert "$ " command "\n\n")
8768       (if gnus-view-pseudo-asynchronously
8769           (start-process "gnus-execute" (current-buffer) shell-file-name
8770                          shell-command-switch command)
8771         (call-process shell-file-name nil t nil
8772                       shell-command-switch command)))))
8773
8774 ;; Summary kill commands.
8775
8776 (defun gnus-summary-edit-global-kill (article)
8777   "Edit the \"global\" kill file."
8778   (interactive (list (gnus-summary-article-number)))
8779   (gnus-group-edit-global-kill article))
8780
8781 (defun gnus-summary-edit-local-kill ()
8782   "Edit a local kill file applied to the current newsgroup."
8783   (interactive)
8784   (setq gnus-current-headers (gnus-summary-article-header))
8785   (gnus-group-edit-local-kill
8786    (gnus-summary-article-number) gnus-newsgroup-name))
8787
8788 ;;; Header reading.
8789
8790 (defun gnus-read-header (id &optional header)
8791   "Read the headers of article ID and enter them into the Gnus system."
8792   (let ((group gnus-newsgroup-name)
8793         (gnus-override-method
8794          (and (gnus-news-group-p gnus-newsgroup-name)
8795               gnus-refer-article-method))
8796         where)
8797     ;; First we check to see whether the header in question is already
8798     ;; fetched.
8799     (if (stringp id)
8800         ;; This is a Message-ID.
8801         (setq header (or header (gnus-id-to-header id)))
8802       ;; This is an article number.
8803       (setq header (or header (gnus-summary-article-header id))))
8804     (if (and header
8805              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8806         ;; We have found the header.
8807         header
8808       ;; If this is a sparse article, we have to nix out its
8809       ;; previous entry in the thread hashtb.
8810       (when (and header
8811                  (gnus-summary-article-sparse-p (mail-header-number header)))
8812         (let* ((parent (gnus-parent-id (mail-header-references header)))
8813                (thread (and parent (gnus-id-to-thread parent))))
8814           (when thread
8815             (delq (assq header thread) thread))))
8816       ;; We have to really fetch the header to this article.
8817       (save-excursion
8818         (set-buffer nntp-server-buffer)
8819         (when (setq where (gnus-request-head id group))
8820           (nnheader-fold-continuation-lines)
8821           (goto-char (point-max))
8822           (insert ".\n")
8823           (goto-char (point-min))
8824           (insert "211 ")
8825           (princ (cond
8826                   ((numberp id) id)
8827                   ((cdr where) (cdr where))
8828                   (header (mail-header-number header))
8829                   (t gnus-reffed-article-number))
8830                  (current-buffer))
8831           (insert " Article retrieved.\n"))
8832         (if (or (not where)
8833                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8834             ()                          ; Malformed head.
8835           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8836             (when (and (stringp id)
8837                        (not (string= (gnus-group-real-name group)
8838                                      (car where))))
8839               ;; If we fetched by Message-ID and the article came
8840               ;; from a different group, we fudge some bogus article
8841               ;; numbers for this article.
8842               (mail-header-set-number header gnus-reffed-article-number))
8843             (save-excursion
8844               (set-buffer gnus-summary-buffer)
8845               (decf gnus-reffed-article-number)
8846               (gnus-remove-header (mail-header-number header))
8847               (push header gnus-newsgroup-headers)
8848               (setq gnus-current-headers header)
8849               (push (mail-header-number header) gnus-newsgroup-limit)))
8850           header)))))
8851
8852 (defun gnus-remove-header (number)
8853   "Remove header NUMBER from `gnus-newsgroup-headers'."
8854   (if (and gnus-newsgroup-headers
8855            (= number (mail-header-number (car gnus-newsgroup-headers))))
8856       (pop gnus-newsgroup-headers)
8857     (let ((headers gnus-newsgroup-headers))
8858       (while (and (cdr headers)
8859                   (not (= number (mail-header-number (cadr headers)))))
8860         (pop headers))
8861       (when (cdr headers)
8862         (setcdr headers (cddr headers))))))
8863
8864 ;;;
8865 ;;; summary highlights
8866 ;;;
8867
8868 (defun gnus-highlight-selected-summary ()
8869   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8870   ;; Highlight selected article in summary buffer
8871   (when gnus-summary-selected-face
8872     (save-excursion
8873       (let* ((beg (progn (beginning-of-line) (point)))
8874              (end (progn (end-of-line) (point)))
8875              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8876              (from (if (get-text-property beg gnus-mouse-face-prop)
8877                        beg
8878                      (or (next-single-property-change
8879                           beg gnus-mouse-face-prop nil end)
8880                          beg)))
8881              (to
8882               (if (= from end)
8883                   (- from 2)
8884                 (or (next-single-property-change
8885                      from gnus-mouse-face-prop nil end)
8886                     end))))
8887         ;; If no mouse-face prop on line we will have to = from = end,
8888         ;; so we highlight the entire line instead.
8889         (when (= (+ to 2) from)
8890           (setq from beg)
8891           (setq to end))
8892         (if gnus-newsgroup-selected-overlay
8893             ;; Move old overlay.
8894             (gnus-move-overlay
8895              gnus-newsgroup-selected-overlay from to (current-buffer))
8896           ;; Create new overlay.
8897           (gnus-overlay-put
8898            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8899            'face gnus-summary-selected-face))))))
8900
8901 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8902 (defun gnus-summary-highlight-line ()
8903   "Highlight current line according to `gnus-summary-highlight'."
8904   (let* ((list gnus-summary-highlight)
8905          (p (point))
8906          (end (progn (end-of-line) (point)))
8907          ;; now find out where the line starts and leave point there.
8908          (beg (progn (beginning-of-line) (point)))
8909          (article (gnus-summary-article-number))
8910          (score (or (cdr (assq (or article gnus-current-article)
8911                                gnus-newsgroup-scored))
8912                     gnus-summary-default-score 0))
8913          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8914          (inhibit-read-only t))
8915     ;; Eval the cars of the lists until we find a match.
8916     (let ((default gnus-summary-default-score))
8917       (while (and list
8918                   (not (eval (caar list))))
8919         (setq list (cdr list))))
8920     (let ((face (cdar list)))
8921       (unless (eq face (get-text-property beg 'face))
8922         (gnus-put-text-property-excluding-characters-with-faces
8923          beg end 'face
8924          (setq face (if (boundp face) (symbol-value face) face)))
8925         (when gnus-summary-highlight-line-function
8926           (funcall gnus-summary-highlight-line-function article face))))
8927     (goto-char p)))
8928
8929 (defun gnus-update-read-articles (group unread &optional compute)
8930   "Update the list of read articles in GROUP."
8931   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8932          (entry (gnus-gethash group gnus-newsrc-hashtb))
8933          (info (nth 2 entry))
8934          (prev 1)
8935          (unread (sort (copy-sequence unread) '<))
8936          read)
8937     (if (or (not info) (not active))
8938         ;; There is no info on this group if it was, in fact,
8939         ;; killed.  Gnus stores no information on killed groups, so
8940         ;; there's nothing to be done.
8941         ;; One could store the information somewhere temporarily,
8942         ;; perhaps...  Hmmm...
8943         ()
8944       ;; Remove any negative articles numbers.
8945       (while (and unread (< (car unread) 0))
8946         (setq unread (cdr unread)))
8947       ;; Remove any expired article numbers
8948       (while (and unread (< (car unread) (car active)))
8949         (setq unread (cdr unread)))
8950       ;; Compute the ranges of read articles by looking at the list of
8951       ;; unread articles.
8952       (while unread
8953         (when (/= (car unread) prev)
8954           (push (if (= prev (1- (car unread))) prev
8955                   (cons prev (1- (car unread))))
8956                 read))
8957         (setq prev (1+ (car unread)))
8958         (setq unread (cdr unread)))
8959       (when (<= prev (cdr active))
8960         (push (cons prev (cdr active)) read))
8961       (if compute
8962           (if (> (length read) 1) (nreverse read) read)
8963         (save-excursion
8964           (set-buffer gnus-group-buffer)
8965           (gnus-undo-register
8966             `(progn
8967                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
8968                (gnus-info-set-read ',info ',(gnus-info-read info))
8969                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
8970                (gnus-group-update-group ,group t))))
8971         ;; Enter this list into the group info.
8972         (gnus-info-set-read
8973          info (if (> (length read) 1) (nreverse read) read))
8974         ;; Set the number of unread articles in gnus-newsrc-hashtb.
8975         (gnus-get-unread-articles-in-group info (gnus-active group))
8976         t))))
8977
8978 (defun gnus-offer-save-summaries ()
8979   "Offer to save all active summary buffers."
8980   (save-excursion
8981     (let ((buflist (buffer-list))
8982           buffers bufname)
8983       ;; Go through all buffers and find all summaries.
8984       (while buflist
8985         (and (setq bufname (buffer-name (car buflist)))
8986              (string-match "Summary" bufname)
8987              (save-excursion
8988                (set-buffer bufname)
8989                ;; We check that this is, indeed, a summary buffer.
8990                (and (eq major-mode 'gnus-summary-mode)
8991                     ;; Also make sure this isn't bogus.
8992                     gnus-newsgroup-prepared
8993                     ;; Also make sure that this isn't a dead summary buffer.
8994                     (not gnus-dead-summary-mode)))
8995              (push bufname buffers))
8996         (setq buflist (cdr buflist)))
8997       ;; Go through all these summary buffers and offer to save them.
8998       (when buffers
8999         (map-y-or-n-p
9000          "Update summary buffer %s? "
9001          (lambda (buf)
9002            (switch-to-buffer buf)
9003            (gnus-summary-exit))
9004          buffers)))))
9005
9006
9007 ;;; @ for mime-partial
9008 ;;;
9009
9010 (defun gnus-request-partial-message ()
9011   (save-excursion
9012     (let ((number (gnus-summary-article-number))
9013           (group gnus-newsgroup-name)
9014           (mother gnus-article-buffer))
9015       (set-buffer (get-buffer-create " *Partial Article*"))
9016       (erase-buffer)
9017       (setq mime-preview-buffer mother)
9018       (gnus-request-article-this-buffer number group)
9019       (mime-parse-buffer)
9020       )))
9021
9022 (autoload 'mime-combine-message/partial-pieces-automatically
9023   "mime-partial"
9024   "Internal method to combine message/partial messages automatically.")
9025
9026 (mime-add-condition
9027  'action '((type . message)(subtype . partial)
9028            (major-mode . gnus-original-article-mode)
9029            (method . mime-combine-message/partial-pieces-automatically)
9030            (summary-buffer-exp . gnus-summary-buffer)
9031            (request-partial-message-method . gnus-request-partial-message)
9032            ))
9033
9034
9035 ;;; @ end
9036 ;;;
9037
9038 (gnus-ems-redefine)
9039
9040 (provide 'gnus-sum)
9041
9042 (run-hooks 'gnus-sum-load-hook)
9043
9044 ;;; gnus-sum.el ends here