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