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