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