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