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