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