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