Sync up with Pteruductyl Gnus v0.71
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, MIME
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mime-view)
39
40 ;; Avoid byte-compile warnings.
41 (eval-when-compile
42   (defvar gnus-article-decoded-p)
43   (defvar gnus-decode-encoded-word-function)
44   )
45
46 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
47 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
48
49 (defcustom gnus-kill-summary-on-exit t
50   "*If non-nil, kill the summary buffer when you exit from it.
51 If nil, the summary will become a \"*Dead Summary*\" buffer, and
52 it will be killed sometime later."
53   :group 'gnus-summary-exit
54   :type 'boolean)
55
56 (defcustom gnus-fetch-old-headers nil
57   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
58 If an unread article in the group refers to an older, already read (or
59 just marked as read) article, the old article will not normally be
60 displayed in the Summary buffer.  If this variable is non-nil, Gnus
61 will attempt to grab the headers to the old articles, and thereby
62 build complete threads.  If it has the value `some', only enough
63 headers to connect otherwise loose threads will be displayed.  This
64 variable can also be a number.  In that case, no more than that number
65 of old headers will be fetched.  If it has the value `invisible', all
66 old headers will be fetched, but none will be displayed.
67
68 The server has to support NOV for any of this to work."
69   :group 'gnus-thread
70   :type '(choice (const :tag "off" nil)
71                  (const some)
72                  number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-refer-thread-limit 200
76   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
77 If t, fetch all the available old headers."
78   :group 'gnus-thread
79   :type '(choice number
80                  (sexp :menu-tag "other" t)))
81
82 (defcustom gnus-summary-make-false-root 'adopt
83   "*nil means that Gnus won't gather loose threads.
84 If the root of a thread has expired or been read in a previous
85 session, the information necessary to build a complete thread has been
86 lost.  Instead of having many small sub-threads from this original thread
87 scattered all over the summary buffer, Gnus can gather them.
88
89 If non-nil, Gnus will try to gather all loose sub-threads from an
90 original thread into one large thread.
91
92 If this variable is non-nil, it should be one of `none', `adopt',
93 `dummy' or `empty'.
94
95 If this variable is `none', Gnus will not make a false root, but just
96 present the sub-threads after another.
97 If this variable is `dummy', Gnus will create a dummy root that will
98 have all the sub-threads as children.
99 If this variable is `adopt', Gnus will make one of the \"children\"
100 the parent and mark all the step-children as such.
101 If this variable is `empty', the \"children\" are printed with empty
102 subject fields.  (Or rather, they will be printed with a string
103 given by the `gnus-summary-same-subject' variable.)"
104   :group 'gnus-thread
105   :type '(choice (const :tag "off" nil)
106                  (const none)
107                  (const dummy)
108                  (const adopt)
109                  (const empty)))
110
111 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
112   "*A regexp to match subjects to be excluded from loose thread gathering.
113 As loose thread gathering is done on subjects only, that means that
114 there can be many false gatherings performed.  By rooting out certain
115 common subjects, gathering might become saner."
116   :group 'gnus-thread
117   :type 'regexp)
118
119 (defcustom gnus-summary-gather-subject-limit nil
120   "*Maximum length of subject comparisons when gathering loose threads.
121 Use nil to compare full subjects.  Setting this variable to a low
122 number will help gather threads that have been corrupted by
123 newsreaders chopping off subject lines, but it might also mean that
124 unrelated articles that have subject that happen to begin with the
125 same few characters will be incorrectly gathered.
126
127 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
128 comparing subjects."
129   :group 'gnus-thread
130   :type '(choice (const :tag "off" nil)
131                  (const fuzzy)
132                  (sexp :menu-tag "on" t)))
133
134 (defcustom gnus-simplify-subject-functions nil
135   "List of functions taking a string argument that simplify subjects.
136 The functions are applied recursively.
137
138 Useful functions to put in this list include: `gnus-simplify-subject-re',
139 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
140   :group 'gnus-thread
141   :type '(repeat function))
142
143 (defcustom gnus-simplify-ignored-prefixes nil
144   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  regexp))
148
149 (defcustom gnus-build-sparse-threads nil
150   "*If non-nil, fill in the gaps in threads.
151 If `some', only fill in the gaps that are needed to tie loose threads
152 together.  If `more', fill in all leaf nodes that Gnus can find.  If
153 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
154   :group 'gnus-thread
155   :type '(choice (const :tag "off" nil)
156                  (const some)
157                  (const more)
158                  (sexp :menu-tag "all" t)))
159
160 (defcustom gnus-summary-thread-gathering-function
161   'gnus-gather-threads-by-subject
162   "*Function used for gathering loose threads.
163 There are two pre-defined functions: `gnus-gather-threads-by-subject',
164 which only takes Subjects into consideration; and
165 `gnus-gather-threads-by-references', which compared the References
166 headers of the articles to find matches."
167   :group 'gnus-thread
168   :type '(radio (function-item gnus-gather-threads-by-subject)
169                 (function-item gnus-gather-threads-by-references)
170                 (function :tag "other")))
171
172 (defcustom gnus-summary-same-subject ""
173   "*String indicating that the current article has the same subject as the previous.
174 This variable will only be used if the value of
175 `gnus-summary-make-false-root' is `empty'."
176   :group 'gnus-summary-format
177   :type 'string)
178
179 (defcustom gnus-summary-goto-unread t
180   "*If t, marking commands will go to the next unread article.
181 If `never', commands that usually go to the next unread article, will
182 go to the next article, whether it is read or not.
183 If nil, only the marking commands will go to the next (un)read article."
184   :group 'gnus-summary-marks
185   :link '(custom-manual "(gnus)Setting Marks")
186   :type '(choice (const :tag "off" nil)
187                  (const never)
188                  (sexp :menu-tag "on" t)))
189
190 (defcustom gnus-summary-default-score 0
191   "*Default article score level.
192 All scores generated by the score files will be added to this score.
193 If this variable is nil, scoring will be disabled."
194   :group 'gnus-score-default
195   :type '(choice (const :tag "disable")
196                  integer))
197
198 (defcustom gnus-summary-zcore-fuzz 0
199   "*Fuzziness factor for the zcore in the summary buffer.
200 Articles with scores closer than this to `gnus-summary-default-score'
201 will not be marked."
202   :group 'gnus-summary-format
203   :type 'integer)
204
205 (defcustom gnus-simplify-subject-fuzzy-regexp nil
206   "*Strings to be removed when doing fuzzy matches.
207 This can either be a regular expression or list of regular expressions
208 that will be removed from subject strings if fuzzy subject
209 simplification is selected."
210   :group 'gnus-thread
211   :type '(repeat regexp))
212
213 (defcustom gnus-show-threads t
214   "*If non-nil, display threads in summary mode."
215   :group 'gnus-thread
216   :type 'boolean)
217
218 (defcustom gnus-thread-hide-subtree nil
219   "*If non-nil, hide all threads initially.
220 If threads are hidden, you have to run the command
221 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
222 to expose hidden threads."
223   :group 'gnus-thread
224   :type 'boolean)
225
226 (defcustom gnus-thread-hide-killed t
227   "*If non-nil, hide killed threads automatically."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-ignore-subject t
232   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
233 If nil, articles that have different subjects from their parents will
234 start separate threads."
235   :group 'gnus-thread
236   :type 'boolean)
237
238 (defcustom gnus-thread-operation-ignore-subject t
239   "*If non-nil, subjects will be ignored when doing thread commands.
240 This affects commands like `gnus-summary-kill-thread' and
241 `gnus-summary-lower-thread'.
242
243 If this variable is nil, articles in the same thread with different
244 subjects will not be included in the operation in question.  If this
245 variable is `fuzzy', only articles that have subjects that are fuzzily
246 equal will be included."
247   :group 'gnus-thread
248   :type '(choice (const :tag "off" nil)
249                  (const fuzzy)
250                  (sexp :tag "on" t)))
251
252 (defcustom gnus-thread-indent-level 4
253   "*Number that says how much each sub-thread should be indented."
254   :group 'gnus-thread
255   :type 'integer)
256
257 (defcustom gnus-auto-extend-newsgroup t
258   "*If non-nil, extend newsgroup forward and backward when requested."
259   :group 'gnus-summary-choose
260   :type 'boolean)
261
262 (defcustom gnus-auto-select-first t
263   "*If nil, don't select the first unread article when entering a group.
264 If this variable is `best', select the highest-scored unread article
265 in the group.  If t, select the first unread article.
266
267 This variable can also be a function to place point on a likely
268 subject line.  Useful values include `gnus-summary-first-unread-subject', 
269 `gnus-summary-first-unread-article' and 
270 `gnus-summary-best-unread-article'.
271
272 If you want to prevent automatic selection of the first unread article
273 in some newsgroups, set the variable to nil in
274 `gnus-select-group-hook'."
275   :group 'gnus-group-select
276   :type '(choice (const :tag "none" nil)
277                  (const best)
278                  (sexp :menu-tag "first" t)
279                  (function-item gnus-summary-first-unread-subject)
280                  (function-item gnus-summary-first-unread-article)
281                  (function-item gnus-summary-best-unread-article)))
282
283 (defcustom gnus-auto-select-next t
284   "*If non-nil, offer to go to the next group from the end of the previous.
285 If the value is t and the next newsgroup is empty, Gnus will exit
286 summary mode and go back to group mode.  If the value is neither nil
287 nor t, Gnus will select the following unread newsgroup.  In
288 particular, if the value is the symbol `quietly', the next unread
289 newsgroup will be selected without any confirmation, and if it is
290 `almost-quietly', the next group will be selected without any
291 confirmation if you are located on the last article in the group.
292 Finally, if this variable is `slightly-quietly', the `Z n' command
293 will go to the next group without confirmation."
294   :group 'gnus-summary-maneuvering
295   :type '(choice (const :tag "off" nil)
296                  (const quietly)
297                  (const almost-quietly)
298                  (const slightly-quietly)
299                  (sexp :menu-tag "on" t)))
300
301 (defcustom gnus-auto-select-same nil
302   "*If non-nil, select the next article with the same subject.
303 If there are no more articles with the same subject, go to
304 the first unread article."
305   :group 'gnus-summary-maneuvering
306   :type 'boolean)
307
308 (defcustom gnus-summary-check-current nil
309   "*If non-nil, consider the current article when moving.
310 The \"unread\" movement commands will stay on the same line if the
311 current article is unread."
312   :group 'gnus-summary-maneuvering
313   :type 'boolean)
314
315 (defcustom gnus-auto-center-summary t
316   "*If non-nil, always center the current summary buffer.
317 In particular, if `vertical' do only vertical recentering.  If non-nil
318 and non-`vertical', do both horizontal and vertical recentering."
319   :group 'gnus-summary-maneuvering
320   :type '(choice (const :tag "none" nil)
321                  (const vertical)
322                  (integer :tag "height")
323                  (sexp :menu-tag "both" t)))
324
325 (defcustom gnus-show-all-headers nil
326   "*If non-nil, don't hide any headers."
327   :group 'gnus-article-hiding
328   :group 'gnus-article-headers
329   :type 'boolean)
330
331 (defcustom gnus-summary-ignore-duplicates nil
332   "*If non-nil, ignore articles with identical Message-ID headers."
333   :group 'gnus-summary
334   :type 'boolean)
335
336 (defcustom gnus-single-article-buffer t
337   "*If non-nil, display all articles in the same buffer.
338 If nil, each group will get its own article buffer."
339   :group 'gnus-article-various
340   :type 'boolean)
341
342 (defcustom gnus-break-pages t
343   "*If non-nil, do page breaking on articles.
344 The page delimiter is specified by the `gnus-page-delimiter'
345 variable."
346   :group 'gnus-article-various
347   :type 'boolean)
348
349 (defcustom gnus-show-mime t
350   "*If non-nil, do mime processing of articles.
351 The articles will simply be fed to the function given by
352 `gnus-article-display-method-for-mime'."
353   :group 'gnus-article-mime
354   :type 'boolean)
355
356 (defcustom gnus-move-split-methods nil
357   "*Variable used to suggest where articles are to be moved to.
358 It uses the same syntax as the `gnus-split-methods' variable."
359   :group 'gnus-summary-mail
360   :type '(repeat (choice (list :value (fun) function)
361                          (cons :value ("" "") regexp (repeat string))
362                          (sexp :value nil))))
363
364 (defcustom gnus-unread-mark ? 
365   "*Mark used for unread articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-ticked-mark ?!
370   "*Mark used for ticked articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-dormant-mark ??
375   "*Mark used for dormant articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-del-mark ?r
380   "*Mark used for del'd articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-read-mark ?R
385   "*Mark used for read articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-expirable-mark ?E
390   "*Mark used for expirable articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-killed-mark ?K
395   "*Mark used for killed articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-souped-mark ?F
400   "*Mark used for killed articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-kill-file-mark ?X
405   "*Mark used for articles killed by kill files."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-low-score-mark ?Y
410   "*Mark used for articles with a low score."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-catchup-mark ?C
415   "*Mark used for articles that are caught up."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-replied-mark ?A
420   "*Mark used for articles that have been replied to."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-cached-mark ?*
425   "*Mark used for articles that are in the cache."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-saved-mark ?S
430   "*Mark used for articles that have been saved to."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-ancient-mark ?O
435   "*Mark used for ancient articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-sparse-mark ?Q
440   "*Mark used for sparsely reffed articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-canceled-mark ?G
445   "*Mark used for canceled articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-duplicate-mark ?M
450   "*Mark used for duplicate articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-undownloaded-mark ?@
455   "*Mark used for articles that weren't downloaded."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-downloadable-mark ?%
460   "*Mark used for articles that are to be downloaded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-unsendable-mark ?=
465   "*Mark used for articles that won't be sent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-score-over-mark ?+
470   "*Score mark used for articles with high scores."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-score-below-mark ?-
475   "*Score mark used for articles with low scores."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-empty-thread-mark ? 
480   "*There is no thread under the article."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-not-empty-thread-mark ?=
485   "*There is a thread under the article."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-view-pseudo-asynchronously nil
490   "*If non-nil, Gnus will view pseudo-articles asynchronously."
491   :group 'gnus-extract-view
492   :type 'boolean)
493
494 (defcustom gnus-auto-expirable-marks
495   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
496         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
497         gnus-souped-mark gnus-duplicate-mark)
498   "*The list of marks converted into expiration if a group is auto-expirable."
499   :group 'gnus-summary
500   :type '(repeat character))
501
502 (defcustom gnus-inhibit-user-auto-expire t
503   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
504   :group 'gnus-summary
505   :type 'boolean)
506
507 (defcustom gnus-view-pseudos nil
508   "*If `automatic', pseudo-articles will be viewed automatically.
509 If `not-confirm', pseudos will be viewed automatically, and the user
510 will not be asked to confirm the command."
511   :group 'gnus-extract-view
512   :type '(choice (const :tag "off" nil)
513                  (const automatic)
514                  (const not-confirm)))
515
516 (defcustom gnus-view-pseudos-separately t
517   "*If non-nil, one pseudo-article will be created for each file to be viewed.
518 If nil, all files that use the same viewing command will be given as a
519 list of parameters to that command."
520   :group 'gnus-extract-view
521   :type 'boolean)
522
523 (defcustom gnus-insert-pseudo-articles t
524   "*If non-nil, insert pseudo-articles when decoding articles."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-summary-dummy-line-format
529   "  %(:                          :%) %S\n"
530   "*The format specification for the dummy roots in the summary buffer.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions.
533
534 %S  The subject"
535   :group 'gnus-threading
536   :type 'string)
537
538 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
539   "*The format specification for the summary mode line.
540 It works along the same lines as a normal formatting string,
541 with some simple extensions:
542
543 %G  Group name
544 %p  Unprefixed group name
545 %A  Current article number
546 %z  Current article score
547 %V  Gnus version
548 %U  Number of unread articles in the group
549 %e  Number of unselected articles in the group
550 %Z  A string with unread/unselected article counts
551 %g  Shortish group name
552 %S  Subject of the current article
553 %u  User-defined spec
554 %s  Current score file name
555 %d  Number of dormant articles
556 %r  Number of articles that have been marked as read in this session
557 %E  Number of articles expunged by the score files"
558   :group 'gnus-summary-format
559   :type 'string)
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-treading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting (not quitting) summary mode."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-summary-update-hook
708   (list 'gnus-summary-highlight-line)
709   "*A hook called when a summary line is changed.
710 The hook will not be called if `gnus-visual' is nil.
711
712 The default function `gnus-summary-highlight-line' will
713 highlight the line according to the `gnus-summary-highlight'
714 variable."
715   :group 'gnus-summary-visual
716   :type 'hook)
717
718 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
719   "*A hook called when an article is selected for the first time.
720 The hook is intended to mark an article as read (or unread)
721 automatically when it is selected."
722   :group 'gnus-summary-choose
723   :type 'hook)
724
725 (defcustom gnus-group-no-more-groups-hook nil
726   "*A hook run when returning to group mode having no more (unread) groups."
727   :group 'gnus-group-select
728   :type 'hook)
729
730 (defcustom gnus-ps-print-hook nil
731   "*A hook run before ps-printing something from Gnus."
732   :group 'gnus-summary
733   :type 'hook)
734
735 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
736   "Face used for highlighting the current article in the summary buffer."
737   :group 'gnus-summary-visual
738   :type 'face)
739
740 (defcustom gnus-summary-highlight
741   '(((= mark gnus-canceled-mark)
742      . gnus-summary-cancelled-face)
743     ((and (> score default)
744           (or (= mark gnus-dormant-mark)
745               (= mark gnus-ticked-mark)))
746      . gnus-summary-high-ticked-face)
747     ((and (< score default)
748           (or (= mark gnus-dormant-mark)
749               (= mark gnus-ticked-mark)))
750      . gnus-summary-low-ticked-face)
751     ((or (= mark gnus-dormant-mark)
752          (= mark gnus-ticked-mark))
753      . gnus-summary-normal-ticked-face)
754     ((and (> score default) (= mark gnus-ancient-mark))
755      . gnus-summary-high-ancient-face)
756     ((and (< score default) (= mark gnus-ancient-mark))
757      . gnus-summary-low-ancient-face)
758     ((= mark gnus-ancient-mark)
759      . gnus-summary-normal-ancient-face)
760     ((and (> score default) (= mark gnus-unread-mark))
761      . gnus-summary-high-unread-face)
762     ((and (< score default) (= mark gnus-unread-mark))
763      . gnus-summary-low-unread-face)
764     ((= mark gnus-unread-mark)
765      . gnus-summary-normal-unread-face)
766     ((and (> score default) (memq mark (list gnus-downloadable-mark
767                                              gnus-undownloaded-mark)))
768      . gnus-summary-high-unread-face)
769     ((and (< score default) (memq mark (list gnus-downloadable-mark
770                                              gnus-undownloaded-mark)))
771      . gnus-summary-low-unread-face)
772     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
773      . gnus-summary-normal-unread-face)
774     ((> score default)
775      . gnus-summary-high-read-face)
776     ((< score default)
777      . gnus-summary-low-read-face)
778     (t
779      . gnus-summary-normal-read-face))
780   "*Controls the highlighting of summary buffer lines.
781
782 A list of (FORM . FACE) pairs.  When deciding how a a particular
783 summary line should be displayed, each form is evaluated.  The content
784 of the face field after the first true form is used.  You can change
785 how those summary lines are displayed, by editing the face field.
786
787 You can use the following variables in the FORM field.
788
789 score:   The articles score
790 default: The default article score.
791 below:   The score below which articles are automatically marked as read.
792 mark:    The articles mark."
793   :group 'gnus-summary-visual
794   :type '(repeat (cons (sexp :tag "Form" nil)
795                        face)))
796
797 (defcustom gnus-alter-header-function nil
798   "Function called to allow alteration of article header structures.
799 The function is called with one parameter, the article header vector,
800 which it may alter in any way.")
801
802 (defcustom gnus-extra-headers nil
803   "*Extra headers to parse."
804   :group 'gnus-summary
805   :type '(repeat symbol))
806
807 (defcustom gnus-ignored-from-addresses
808   (and user-mail-address (regexp-quote user-mail-address))
809   "*Regexp of From headers that may be suppressed in favor of To headers."
810   :group 'gnus-summary
811   :type 'regexp)
812
813 (defcustom gnus-group-charset-alist 
814   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
815     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
816     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
817     ("^relcom\\>" koi8-r)
818     (".*" iso-8859-1))
819   "Alist of regexps (to match group names) and default charsets to be used."
820   :type '(repeat (list (regexp :tag "Group")
821                        (symbol :tag "Charset")))
822   :group 'gnus-charset)
823
824 ;;; Internal variables
825
826 (defvar gnus-scores-exclude-files nil)
827 (defvar gnus-page-broken nil)
828 (defvar gnus-inhibit-mime-unbuttonizing nil)
829
830 (defvar gnus-original-article nil)
831 (defvar gnus-article-internal-prepare-hook nil)
832 (defvar gnus-newsgroup-process-stack nil)
833
834 (defvar gnus-thread-indent-array nil)
835 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
836
837 ;; Avoid highlighting in kill files.
838 (defvar gnus-summary-inhibit-highlight nil)
839 (defvar gnus-newsgroup-selected-overlay nil)
840 (defvar gnus-inhibit-limiting nil)
841 (defvar gnus-newsgroup-adaptive-score-file nil)
842 (defvar gnus-current-score-file nil)
843 (defvar gnus-current-move-group nil)
844 (defvar gnus-current-copy-group nil)
845 (defvar gnus-current-crosspost-group nil)
846
847 (defvar gnus-newsgroup-dependencies nil)
848 (defvar gnus-newsgroup-adaptive nil)
849 (defvar gnus-summary-display-article-function nil)
850 (defvar gnus-summary-highlight-line-function nil
851   "Function called after highlighting a summary line.")
852
853 (defvar gnus-summary-line-format-alist
854   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
855     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
856     (?s gnus-tmp-subject-or-nil ?s)
857     (?n gnus-tmp-name ?s)
858     (?A (std11-address-string
859          (car (mime-read-field 'From gnus-tmp-header))) ?s)
860     (?a (or (std11-full-name-string
861              (car (mime-read-field 'From gnus-tmp-header)))
862             gnus-tmp-from) ?s)
863     (?F gnus-tmp-from ?s)
864     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
865     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
866     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
867     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
868     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
869     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
870     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
871     (?L gnus-tmp-lines ?d)
872     (?I gnus-tmp-indentation ?s)
873     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
874     (?R gnus-tmp-replied ?c)
875     (?\[ gnus-tmp-opening-bracket ?c)
876     (?\] gnus-tmp-closing-bracket ?c)
877     (?\> (make-string gnus-tmp-level ? ) ?s)
878     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
879     (?i gnus-tmp-score ?d)
880     (?z gnus-tmp-score-char ?c)
881     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
882     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
883     (?U gnus-tmp-unread ?c)
884     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
885     (?t (gnus-summary-number-of-articles-in-thread
886          (and (boundp 'thread) (car thread)) gnus-tmp-level)
887         ?d)
888     (?e (gnus-summary-number-of-articles-in-thread
889          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
890         ?c)
891     (?u gnus-tmp-user-defined ?s)
892     (?P (gnus-pick-line-number) ?d))
893   "An alist of format specifications that can appear in summary lines,
894 and what variables they correspond with, along with the type of the
895 variable (string, integer, character, etc).")
896
897 (defvar gnus-summary-dummy-line-format-alist
898   `((?S gnus-tmp-subject ?s)
899     (?N gnus-tmp-number ?d)
900     (?u gnus-tmp-user-defined ?s)))
901
902 (defvar gnus-summary-mode-line-format-alist
903   `((?G gnus-tmp-group-name ?s)
904     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
905     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
906     (?A gnus-tmp-article-number ?d)
907     (?Z gnus-tmp-unread-and-unselected ?s)
908     (?V gnus-version ?s)
909     (?U gnus-tmp-unread-and-unticked ?d)
910     (?S gnus-tmp-subject ?s)
911     (?e gnus-tmp-unselected ?d)
912     (?u gnus-tmp-user-defined ?s)
913     (?d (length gnus-newsgroup-dormant) ?d)
914     (?t (length gnus-newsgroup-marked) ?d)
915     (?r (length gnus-newsgroup-reads) ?d)
916     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
917     (?E gnus-newsgroup-expunged-tally ?d)
918     (?s (gnus-current-score-file-nondirectory) ?s)))
919
920 (defvar gnus-last-search-regexp nil
921   "Default regexp for article search command.")
922
923 (defvar gnus-last-shell-command nil
924   "Default shell command on article.")
925
926 (defvar gnus-newsgroup-begin nil)
927 (defvar gnus-newsgroup-end nil)
928 (defvar gnus-newsgroup-last-rmail nil)
929 (defvar gnus-newsgroup-last-mail nil)
930 (defvar gnus-newsgroup-last-folder nil)
931 (defvar gnus-newsgroup-last-file nil)
932 (defvar gnus-newsgroup-auto-expire nil)
933 (defvar gnus-newsgroup-active nil)
934
935 (defvar gnus-newsgroup-data nil)
936 (defvar gnus-newsgroup-data-reverse nil)
937 (defvar gnus-newsgroup-limit nil)
938 (defvar gnus-newsgroup-limits nil)
939
940 (defvar gnus-newsgroup-unreads nil
941   "List of unread articles in the current newsgroup.")
942
943 (defvar gnus-newsgroup-unselected nil
944   "List of unselected unread articles in the current newsgroup.")
945
946 (defvar gnus-newsgroup-reads nil
947   "Alist of read articles and article marks in the current newsgroup.")
948
949 (defvar gnus-newsgroup-expunged-tally nil)
950
951 (defvar gnus-newsgroup-marked nil
952   "List of ticked articles in the current newsgroup (a subset of unread art).")
953
954 (defvar gnus-newsgroup-killed nil
955   "List of ranges of articles that have been through the scoring process.")
956
957 (defvar gnus-newsgroup-cached nil
958   "List of articles that come from the article cache.")
959
960 (defvar gnus-newsgroup-saved nil
961   "List of articles that have been saved.")
962
963 (defvar gnus-newsgroup-kill-headers nil)
964
965 (defvar gnus-newsgroup-replied nil
966   "List of articles that have been replied to in the current newsgroup.")
967
968 (defvar gnus-newsgroup-expirable nil
969   "List of articles in the current newsgroup that can be expired.")
970
971 (defvar gnus-newsgroup-processable nil
972   "List of articles in the current newsgroup that can be processed.")
973
974 (defvar gnus-newsgroup-downloadable nil
975   "List of articles in the current newsgroup that can be processed.")
976
977 (defvar gnus-newsgroup-undownloaded nil
978   "List of articles in the current newsgroup that haven't been downloaded..")
979
980 (defvar gnus-newsgroup-unsendable nil
981   "List of articles in the current newsgroup that won't be sent.")
982
983 (defvar gnus-newsgroup-bookmarks nil
984   "List of articles in the current newsgroup that have bookmarks.")
985
986 (defvar gnus-newsgroup-dormant nil
987   "List of dormant articles in the current newsgroup.")
988
989 (defvar gnus-newsgroup-scored nil
990   "List of scored articles in the current newsgroup.")
991
992 (defvar gnus-newsgroup-headers nil
993   "List of article headers in the current newsgroup.")
994
995 (defvar gnus-newsgroup-threads nil)
996
997 (defvar gnus-newsgroup-prepared nil
998   "Whether the current group has been prepared properly.")
999
1000 (defvar gnus-newsgroup-ancient nil
1001   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1002
1003 (defvar gnus-newsgroup-sparse nil)
1004
1005 (defvar gnus-current-article nil)
1006 (defvar gnus-article-current nil)
1007 (defvar gnus-current-headers nil)
1008 (defvar gnus-have-all-headers nil)
1009 (defvar gnus-last-article nil)
1010 (defvar gnus-newsgroup-history nil)
1011 (defvar gnus-newsgroup-charset nil)
1012
1013 (defconst gnus-summary-local-variables
1014   '(gnus-newsgroup-name
1015     gnus-newsgroup-begin gnus-newsgroup-end
1016     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1017     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1018     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1019     gnus-newsgroup-unselected gnus-newsgroup-marked
1020     gnus-newsgroup-reads gnus-newsgroup-saved
1021     gnus-newsgroup-replied gnus-newsgroup-expirable
1022     gnus-newsgroup-processable gnus-newsgroup-killed
1023     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1024     gnus-newsgroup-unsendable
1025     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1026     gnus-newsgroup-headers gnus-newsgroup-threads
1027     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1028     gnus-current-article gnus-current-headers gnus-have-all-headers
1029     gnus-last-article gnus-article-internal-prepare-hook
1030     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1031     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1032     gnus-thread-expunge-below
1033     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1034     (gnus-summary-mark-below . global)
1035     gnus-newsgroup-active gnus-scores-exclude-files
1036     gnus-newsgroup-history gnus-newsgroup-ancient
1037     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1038     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1039     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1040     (gnus-newsgroup-expunged-tally . 0)
1041     gnus-cache-removable-articles gnus-newsgroup-cached
1042     gnus-newsgroup-data gnus-newsgroup-data-reverse
1043     gnus-newsgroup-limit gnus-newsgroup-limits
1044     gnus-newsgroup-charset)
1045   "Variables that are buffer-local to the summary buffers.")
1046
1047 ;; Byte-compiler warning.
1048 (defvar gnus-article-mode-map)
1049
1050 ;; Subject simplification.
1051
1052 (defun gnus-simplify-whitespace (str)
1053   "Remove excessive whitespace."
1054   (let ((mystr str))
1055     ;; Multiple spaces.
1056     (while (string-match "[ \t][ \t]+" mystr)
1057       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1058                           " "
1059                           (substring mystr (match-end 0)))))
1060     ;; Leading spaces.
1061     (when (string-match "^[ \t]+" mystr)
1062       (setq mystr (substring mystr (match-end 0))))
1063     ;; Trailing spaces.
1064     (when (string-match "[ \t]+$" mystr)
1065       (setq mystr (substring mystr 0 (match-beginning 0))))
1066     mystr))
1067
1068 (defsubst gnus-simplify-subject-re (subject)
1069   "Remove \"Re:\" from subject lines."
1070   (if (string-match "^[Rr][Ee]: *" subject)
1071       (substring subject (match-end 0))
1072     subject))
1073
1074 (defun gnus-simplify-subject (subject &optional re-only)
1075   "Remove `Re:' and words in parentheses.
1076 If RE-ONLY is non-nil, strip leading `Re:'s only."
1077   (let ((case-fold-search t))           ;Ignore case.
1078     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1079     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1080       (setq subject (substring subject (match-end 0))))
1081     ;; Remove uninteresting prefixes.
1082     (when (and (not re-only)
1083                gnus-simplify-ignored-prefixes
1084                (string-match gnus-simplify-ignored-prefixes subject))
1085       (setq subject (substring subject (match-end 0))))
1086     ;; Remove words in parentheses from end.
1087     (unless re-only
1088       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1089         (setq subject (substring subject 0 (match-beginning 0)))))
1090     ;; Return subject string.
1091     subject))
1092
1093 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1094 ;; all whitespace.
1095 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1096   (goto-char (point-min))
1097   (while (re-search-forward regexp nil t)
1098       (replace-match (or newtext ""))))
1099
1100 (defun gnus-simplify-buffer-fuzzy ()
1101   "Simplify string in the buffer fuzzily.
1102 The string in the accessible portion of the current buffer is simplified.
1103 It is assumed to be a single-line subject.
1104 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1105 matter is removed.  Additional things can be deleted by setting
1106 gnus-simplify-subject-fuzzy-regexp."
1107   (let ((case-fold-search t)
1108         (modified-tick))
1109     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1110
1111     (while (not (eq modified-tick (buffer-modified-tick)))
1112       (setq modified-tick (buffer-modified-tick))
1113       (cond
1114        ((listp gnus-simplify-subject-fuzzy-regexp)
1115         (mapcar 'gnus-simplify-buffer-fuzzy-step
1116                 gnus-simplify-subject-fuzzy-regexp))
1117        (gnus-simplify-subject-fuzzy-regexp
1118         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1119       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1120       (gnus-simplify-buffer-fuzzy-step
1121        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1122       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1123
1124     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1125     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1126     (gnus-simplify-buffer-fuzzy-step " $")
1127     (gnus-simplify-buffer-fuzzy-step "^ +")))
1128
1129 (defun gnus-simplify-subject-fuzzy (subject)
1130   "Simplify a subject string fuzzily.
1131 See `gnus-simplify-buffer-fuzzy' for details."
1132   (save-excursion
1133     (gnus-set-work-buffer)
1134     (let ((case-fold-search t))
1135       ;; Remove uninteresting prefixes.
1136       (when (and gnus-simplify-ignored-prefixes
1137                  (string-match gnus-simplify-ignored-prefixes subject))
1138         (setq subject (substring subject (match-end 0))))
1139       (insert subject)
1140       (inline (gnus-simplify-buffer-fuzzy))
1141       (buffer-string))))
1142
1143 (defsubst gnus-simplify-subject-fully (subject)
1144   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1145   (cond
1146    (gnus-simplify-subject-functions
1147     (gnus-map-function gnus-simplify-subject-functions subject))
1148    ((null gnus-summary-gather-subject-limit)
1149     (gnus-simplify-subject-re subject))
1150    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1151     (gnus-simplify-subject-fuzzy subject))
1152    ((numberp gnus-summary-gather-subject-limit)
1153     (gnus-limit-string (gnus-simplify-subject-re subject)
1154                        gnus-summary-gather-subject-limit))
1155    (t
1156     subject)))
1157
1158 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1159   "Check whether two subjects are equal.
1160 If optional argument simple-first is t, first argument is already
1161 simplified."
1162   (cond
1163    ((null simple-first)
1164     (equal (gnus-simplify-subject-fully s1)
1165            (gnus-simplify-subject-fully s2)))
1166    (t
1167     (equal s1
1168            (gnus-simplify-subject-fully s2)))))
1169
1170 (defun gnus-summary-bubble-group ()
1171   "Increase the score of the current group.
1172 This is a handy function to add to `gnus-summary-exit-hook' to
1173 increase the score of each group you read."
1174   (gnus-group-add-score gnus-newsgroup-name))
1175
1176 \f
1177 ;;;
1178 ;;; Gnus summary mode
1179 ;;;
1180
1181 (put 'gnus-summary-mode 'mode-class 'special)
1182
1183 (when t
1184   ;; Non-orthogonal keys
1185
1186   (gnus-define-keys gnus-summary-mode-map
1187     " " gnus-summary-next-page
1188     "\177" gnus-summary-prev-page
1189     [delete] gnus-summary-prev-page
1190     [backspace] gnus-summary-prev-page
1191     "\r" gnus-summary-scroll-up
1192     "\M-\r" gnus-summary-scroll-down
1193     "n" gnus-summary-next-unread-article
1194     "p" gnus-summary-prev-unread-article
1195     "N" gnus-summary-next-article
1196     "P" gnus-summary-prev-article
1197     "\M-\C-n" gnus-summary-next-same-subject
1198     "\M-\C-p" gnus-summary-prev-same-subject
1199     "\M-n" gnus-summary-next-unread-subject
1200     "\M-p" gnus-summary-prev-unread-subject
1201     "." gnus-summary-first-unread-article
1202     "," gnus-summary-best-unread-article
1203     "\M-s" gnus-summary-search-article-forward
1204     "\M-r" gnus-summary-search-article-backward
1205     "<" gnus-summary-beginning-of-article
1206     ">" gnus-summary-end-of-article
1207     "j" gnus-summary-goto-article
1208     "^" gnus-summary-refer-parent-article
1209     "\M-^" gnus-summary-refer-article
1210     "u" gnus-summary-tick-article-forward
1211     "!" gnus-summary-tick-article-forward
1212     "U" gnus-summary-tick-article-backward
1213     "d" gnus-summary-mark-as-read-forward
1214     "D" gnus-summary-mark-as-read-backward
1215     "E" gnus-summary-mark-as-expirable
1216     "\M-u" gnus-summary-clear-mark-forward
1217     "\M-U" gnus-summary-clear-mark-backward
1218     "k" gnus-summary-kill-same-subject-and-select
1219     "\C-k" gnus-summary-kill-same-subject
1220     "\M-\C-k" gnus-summary-kill-thread
1221     "\M-\C-l" gnus-summary-lower-thread
1222     "e" gnus-summary-edit-article
1223     "#" gnus-summary-mark-as-processable
1224     "\M-#" gnus-summary-unmark-as-processable
1225     "\M-\C-t" gnus-summary-toggle-threads
1226     "\M-\C-s" gnus-summary-show-thread
1227     "\M-\C-h" gnus-summary-hide-thread
1228     "\M-\C-f" gnus-summary-next-thread
1229     "\M-\C-b" gnus-summary-prev-thread
1230     "\M-\C-u" gnus-summary-up-thread
1231     "\M-\C-d" gnus-summary-down-thread
1232     "&" gnus-summary-execute-command
1233     "c" gnus-summary-catchup-and-exit
1234     "\C-w" gnus-summary-mark-region-as-read
1235     "\C-t" gnus-summary-toggle-truncation
1236     "?" gnus-summary-mark-as-dormant
1237     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1238     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1239     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1240     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1241     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1242     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1243     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1244     "=" gnus-summary-expand-window
1245     "\C-x\C-s" gnus-summary-reselect-current-group
1246     "\M-g" gnus-summary-rescan-group
1247     "w" gnus-summary-stop-page-breaking
1248     "\C-c\C-r" gnus-summary-caesar-message
1249     "\M-t" gnus-summary-toggle-mime
1250     "f" gnus-summary-followup
1251     "F" gnus-summary-followup-with-original
1252     "C" gnus-summary-cancel-article
1253     "r" gnus-summary-reply
1254     "R" gnus-summary-reply-with-original
1255     "\C-c\C-f" gnus-summary-mail-forward
1256     "o" gnus-summary-save-article
1257     "\C-o" gnus-summary-save-article-mail
1258     "|" gnus-summary-pipe-output
1259     "\M-k" gnus-summary-edit-local-kill
1260     "\M-K" gnus-summary-edit-global-kill
1261     ;; "V" gnus-version
1262     "\C-c\C-d" gnus-summary-describe-group
1263     "q" gnus-summary-exit
1264     "Q" gnus-summary-exit-no-update
1265     "\C-c\C-i" gnus-info-find-node
1266     gnus-mouse-2 gnus-mouse-pick-article
1267     "m" gnus-summary-mail-other-window
1268     "a" gnus-summary-post-news
1269     "x" gnus-summary-limit-to-unread
1270     "s" gnus-summary-isearch-article
1271     "t" gnus-article-hide-headers
1272     "g" gnus-summary-show-article
1273     "l" gnus-summary-goto-last-article
1274     "v" gnus-summary-preview-mime-message
1275     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1276     "\C-d" gnus-summary-enter-digest-group
1277     "\M-\C-d" gnus-summary-read-document
1278     "\M-\C-e" gnus-summary-edit-parameters
1279     "\M-\C-g" gnus-summary-customize-parameters
1280     "\C-c\C-b" gnus-bug
1281     "*" gnus-cache-enter-article
1282     "\M-*" gnus-cache-remove-article
1283     "\M-&" gnus-summary-universal-argument
1284     "\C-l" gnus-recenter
1285     "I" gnus-summary-increase-score
1286     "L" gnus-summary-lower-score
1287     "\M-i" gnus-symbolic-argument
1288     "h" gnus-summary-select-article-buffer
1289     
1290     "V" gnus-summary-score-map
1291     "X" gnus-uu-extract-map
1292     "S" gnus-summary-send-map)
1293
1294   ;; Sort of orthogonal keymap
1295   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1296     "t" gnus-summary-tick-article-forward
1297     "!" gnus-summary-tick-article-forward
1298     "d" gnus-summary-mark-as-read-forward
1299     "r" gnus-summary-mark-as-read-forward
1300     "c" gnus-summary-clear-mark-forward
1301     " " gnus-summary-clear-mark-forward
1302     "e" gnus-summary-mark-as-expirable
1303     "x" gnus-summary-mark-as-expirable
1304     "?" gnus-summary-mark-as-dormant
1305     "b" gnus-summary-set-bookmark
1306     "B" gnus-summary-remove-bookmark
1307     "#" gnus-summary-mark-as-processable
1308     "\M-#" gnus-summary-unmark-as-processable
1309     "S" gnus-summary-limit-include-expunged
1310     "C" gnus-summary-catchup
1311     "H" gnus-summary-catchup-to-here
1312     "\C-c" gnus-summary-catchup-all
1313     "k" gnus-summary-kill-same-subject-and-select
1314     "K" gnus-summary-kill-same-subject
1315     "P" gnus-uu-mark-map)
1316
1317   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1318     "c" gnus-summary-clear-above
1319     "u" gnus-summary-tick-above
1320     "m" gnus-summary-mark-above
1321     "k" gnus-summary-kill-below)
1322
1323   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1324     "/" gnus-summary-limit-to-subject
1325     "n" gnus-summary-limit-to-articles
1326     "w" gnus-summary-pop-limit
1327     "s" gnus-summary-limit-to-subject
1328     "a" gnus-summary-limit-to-author
1329     "u" gnus-summary-limit-to-unread
1330     "m" gnus-summary-limit-to-marks
1331     "v" gnus-summary-limit-to-score
1332     "*" gnus-summary-limit-include-cached
1333     "D" gnus-summary-limit-include-dormant
1334     "T" gnus-summary-limit-include-thread
1335     "d" gnus-summary-limit-exclude-dormant
1336     "t" gnus-summary-limit-to-age
1337     "E" gnus-summary-limit-include-expunged
1338     "c" gnus-summary-limit-exclude-childless-dormant
1339     "C" gnus-summary-limit-mark-excluded-as-read)
1340
1341   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1342     "n" gnus-summary-next-unread-article
1343     "p" gnus-summary-prev-unread-article
1344     "N" gnus-summary-next-article
1345     "P" gnus-summary-prev-article
1346     "\C-n" gnus-summary-next-same-subject
1347     "\C-p" gnus-summary-prev-same-subject
1348     "\M-n" gnus-summary-next-unread-subject
1349     "\M-p" gnus-summary-prev-unread-subject
1350     "f" gnus-summary-first-unread-article
1351     "b" gnus-summary-best-unread-article
1352     "j" gnus-summary-goto-article
1353     "g" gnus-summary-goto-subject
1354     "l" gnus-summary-goto-last-article
1355     "o" gnus-summary-pop-article)
1356
1357   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1358     "k" gnus-summary-kill-thread
1359     "l" gnus-summary-lower-thread
1360     "i" gnus-summary-raise-thread
1361     "T" gnus-summary-toggle-threads
1362     "t" gnus-summary-rethread-current
1363     "^" gnus-summary-reparent-thread
1364     "s" gnus-summary-show-thread
1365     "S" gnus-summary-show-all-threads
1366     "h" gnus-summary-hide-thread
1367     "H" gnus-summary-hide-all-threads
1368     "n" gnus-summary-next-thread
1369     "p" gnus-summary-prev-thread
1370     "u" gnus-summary-up-thread
1371     "o" gnus-summary-top-thread
1372     "d" gnus-summary-down-thread
1373     "#" gnus-uu-mark-thread
1374     "\M-#" gnus-uu-unmark-thread)
1375
1376   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1377     "g" gnus-summary-prepare
1378     "c" gnus-summary-insert-cached-articles)
1379
1380   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1381     "c" gnus-summary-catchup-and-exit
1382     "C" gnus-summary-catchup-all-and-exit
1383     "E" gnus-summary-exit-no-update
1384     "Q" gnus-summary-exit
1385     "Z" gnus-summary-exit
1386     "n" gnus-summary-catchup-and-goto-next-group
1387     "R" gnus-summary-reselect-current-group
1388     "G" gnus-summary-rescan-group
1389     "N" gnus-summary-next-group
1390     "s" gnus-summary-save-newsrc
1391     "P" gnus-summary-prev-group)
1392
1393   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1394     " " gnus-summary-next-page
1395     "n" gnus-summary-next-page
1396     "\177" gnus-summary-prev-page
1397     [delete] gnus-summary-prev-page
1398     "p" gnus-summary-prev-page
1399     "\r" gnus-summary-scroll-up
1400     "\M-\r" gnus-summary-scroll-down
1401     "<" gnus-summary-beginning-of-article
1402     ">" gnus-summary-end-of-article
1403     "b" gnus-summary-beginning-of-article
1404     "e" gnus-summary-end-of-article
1405     "^" gnus-summary-refer-parent-article
1406     "r" gnus-summary-refer-parent-article
1407     "R" gnus-summary-refer-references
1408     "T" gnus-summary-refer-thread
1409     "g" gnus-summary-show-article
1410     "s" gnus-summary-isearch-article
1411     "P" gnus-summary-print-article)
1412
1413   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1414     "b" gnus-article-add-buttons
1415     "B" gnus-article-add-buttons-to-head
1416     "o" gnus-article-treat-overstrike
1417     "e" gnus-article-emphasize
1418     "w" gnus-article-fill-cited-article
1419     "c" gnus-article-remove-cr
1420     "f" gnus-article-display-x-face
1421     "l" gnus-summary-stop-page-breaking
1422     "r" gnus-summary-caesar-message
1423     "t" gnus-article-hide-headers
1424     "v" gnus-summary-verbose-headers
1425     "m" gnus-summary-toggle-mime
1426     "h" gnus-article-treat-html
1427     "d" gnus-article-treat-dumbquotes)
1428
1429   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1430     "a" gnus-article-hide
1431     "h" gnus-article-hide-headers
1432     "b" gnus-article-hide-boring-headers
1433     "s" gnus-article-hide-signature
1434     "c" gnus-article-hide-citation
1435     "C" gnus-article-hide-citation-in-followups
1436     "p" gnus-article-hide-pgp
1437     "P" gnus-article-hide-pem
1438     "\C-c" gnus-article-hide-citation-maybe)
1439
1440   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1441     "a" gnus-article-highlight
1442     "h" gnus-article-highlight-headers
1443     "c" gnus-article-highlight-citation
1444     "s" gnus-article-highlight-signature)
1445
1446   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1447     "z" gnus-article-date-ut
1448     "u" gnus-article-date-ut
1449     "l" gnus-article-date-local
1450     "e" gnus-article-date-lapsed
1451     "o" gnus-article-date-original
1452     "i" gnus-article-date-iso8601
1453     "s" gnus-article-date-user)
1454
1455   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1456     "t" gnus-article-remove-trailing-blank-lines
1457     "l" gnus-article-strip-leading-blank-lines
1458     "m" gnus-article-strip-multiple-blank-lines
1459     "a" gnus-article-strip-blank-lines
1460     "A" gnus-article-strip-all-blank-lines
1461     "s" gnus-article-strip-leading-space
1462     "e" gnus-article-strip-trailing-space)
1463
1464   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1465     "v" gnus-version
1466     "f" gnus-summary-fetch-faq
1467     "d" gnus-summary-describe-group
1468     "h" gnus-summary-describe-briefly
1469     "i" gnus-info-find-node)
1470
1471   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1472     "e" gnus-summary-expire-articles
1473     "\M-\C-e" gnus-summary-expire-articles-now
1474     "\177" gnus-summary-delete-article
1475     [delete] gnus-summary-delete-article
1476     "m" gnus-summary-move-article
1477     "r" gnus-summary-respool-article
1478     "w" gnus-summary-edit-article
1479     "c" gnus-summary-copy-article
1480     "B" gnus-summary-crosspost-article
1481     "q" gnus-summary-respool-query
1482     "t" gnus-summary-respool-trace
1483     "i" gnus-summary-import-article
1484     "p" gnus-summary-article-posted-p)
1485
1486   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1487     "o" gnus-summary-save-article
1488     "m" gnus-summary-save-article-mail
1489     "F" gnus-summary-write-article-file
1490     "r" gnus-summary-save-article-rmail
1491     "f" gnus-summary-save-article-file
1492     "b" gnus-summary-save-article-body-file
1493     "h" gnus-summary-save-article-folder
1494     "v" gnus-summary-save-article-vm
1495     "p" gnus-summary-pipe-output
1496     "s" gnus-soup-add-article)
1497
1498   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1499     "b" gnus-summary-display-buttonized
1500     "m" gnus-summary-repair-multipart
1501     "v" gnus-article-view-part
1502     "o" gnus-article-save-part
1503     "c" gnus-article-copy-part
1504     "e" gnus-article-externalize-part
1505     "|" gnus-article-pipe-part)
1506   )
1507
1508 (defun gnus-summary-make-menu-bar ()
1509   (gnus-turn-off-edit-menu 'summary)
1510
1511   (unless (boundp 'gnus-summary-misc-menu)
1512
1513     (easy-menu-define
1514      gnus-summary-kill-menu gnus-summary-mode-map ""
1515      (cons
1516       "Score"
1517       (nconc
1518        (list
1519         ["Enter score..." gnus-summary-score-entry t]
1520         ["Customize" gnus-score-customize t])
1521        (gnus-make-score-map 'increase)
1522        (gnus-make-score-map 'lower)
1523        '(("Mark"
1524           ["Kill below" gnus-summary-kill-below t]
1525           ["Mark above" gnus-summary-mark-above t]
1526           ["Tick above" gnus-summary-tick-above t]
1527           ["Clear above" gnus-summary-clear-above t])
1528          ["Current score" gnus-summary-current-score t]
1529          ["Set score" gnus-summary-set-score t]
1530          ["Switch current score file..." gnus-score-change-score-file t]
1531          ["Set mark below..." gnus-score-set-mark-below t]
1532          ["Set expunge below..." gnus-score-set-expunge-below t]
1533          ["Edit current score file" gnus-score-edit-current-scores t]
1534          ["Edit score file" gnus-score-edit-file t]
1535          ["Trace score" gnus-score-find-trace t]
1536          ["Find words" gnus-score-find-favourite-words t]
1537          ["Rescore buffer" gnus-summary-rescore t]
1538          ["Increase score..." gnus-summary-increase-score t]
1539          ["Lower score..." gnus-summary-lower-score t]))))
1540
1541     ;; Define both the Article menu in the summary buffer and the equivalent
1542     ;; Commands menu in the article buffer here for consistency.
1543     (let ((innards
1544            '(("Hide"
1545               ["All" gnus-article-hide t]
1546               ["Headers" gnus-article-hide-headers t]
1547               ["Signature" gnus-article-hide-signature t]
1548               ["Citation" gnus-article-hide-citation t]
1549               ["PGP" gnus-article-hide-pgp t]
1550               ["Boring headers" gnus-article-hide-boring-headers t])
1551              ("Highlight"
1552               ["All" gnus-article-highlight t]
1553               ["Headers" gnus-article-highlight-headers t]
1554               ["Signature" gnus-article-highlight-signature t]
1555               ["Citation" gnus-article-highlight-citation t])
1556              ("Date"
1557               ["Local" gnus-article-date-local t]
1558               ["ISO8601" gnus-article-date-iso8601 t]
1559               ["UT" gnus-article-date-ut t]
1560               ["Original" gnus-article-date-original t]
1561               ["Lapsed" gnus-article-date-lapsed t]
1562               ["User-defined" gnus-article-date-user t])
1563              ("Washing"
1564               ("Remove Blanks"
1565                ["Leading" gnus-article-strip-leading-blank-lines t]
1566                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1567                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1568                ["All of the above" gnus-article-strip-blank-lines t]
1569                ["All" gnus-article-strip-all-blank-lines t]
1570                ["Leading space" gnus-article-strip-leading-space t]
1571                ["Trailing space" gnus-article-strip-trailing-space t])
1572               ["Overstrike" gnus-article-treat-overstrike t]
1573               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1574               ["Emphasis" gnus-article-emphasize t]
1575               ["Word wrap" gnus-article-fill-cited-article t]
1576               ["CR" gnus-article-remove-cr t]
1577               ["Show X-Face" gnus-article-display-x-face t]
1578               ["UnHTMLize" gnus-article-treat-html t]
1579               ["Rot 13" gnus-summary-caesar-message t]
1580               ["Unix pipe" gnus-summary-pipe-message t]
1581               ["Add buttons" gnus-article-add-buttons t]
1582               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1583               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1584               ["Toggle MIME" gnus-summary-toggle-mime t]
1585               ["Verbose header" gnus-summary-verbose-headers t]
1586               ["Toggle header" gnus-summary-toggle-header t])
1587              ("Output"
1588               ["Save in default format" gnus-summary-save-article t]
1589               ["Save in file" gnus-summary-save-article-file t]
1590               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1591               ["Save in MH folder" gnus-summary-save-article-folder t]
1592               ["Save in VM folder" gnus-summary-save-article-vm t]
1593               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1594               ["Save body in file" gnus-summary-save-article-body-file t]
1595               ["Pipe through a filter" gnus-summary-pipe-output t]
1596               ["Add to SOUP packet" gnus-soup-add-article t]
1597               ["Print" gnus-summary-print-article t])
1598              ("Backend"
1599               ["Respool article..." gnus-summary-respool-article t]
1600               ["Move article..." gnus-summary-move-article
1601                (gnus-check-backend-function
1602                 'request-move-article gnus-newsgroup-name)]
1603               ["Copy article..." gnus-summary-copy-article t]
1604               ["Crosspost article..." gnus-summary-crosspost-article
1605                (gnus-check-backend-function
1606                 'request-replace-article gnus-newsgroup-name)]
1607               ["Import file..." gnus-summary-import-article t]
1608               ["Check if posted" gnus-summary-article-posted-p t]
1609               ["Edit article" gnus-summary-edit-article
1610                (not (gnus-group-read-only-p))]
1611               ["Delete article" gnus-summary-delete-article
1612                (gnus-check-backend-function
1613                 'request-expire-articles gnus-newsgroup-name)]
1614               ["Query respool" gnus-summary-respool-query t]
1615               ["Trace respool" gnus-summary-respool-trace t]
1616               ["Delete expirable articles" gnus-summary-expire-articles-now
1617                (gnus-check-backend-function
1618                 'request-expire-articles gnus-newsgroup-name)])
1619              ("Extract"
1620               ["Uudecode" gnus-uu-decode-uu t]
1621               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1622               ["Unshar" gnus-uu-decode-unshar t]
1623               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1624               ["Save" gnus-uu-decode-save t]
1625               ["Binhex" gnus-uu-decode-binhex t]
1626               ["Postscript" gnus-uu-decode-postscript t])
1627              ("Cache"
1628               ["Enter article" gnus-cache-enter-article t]
1629               ["Remove article" gnus-cache-remove-article t])
1630              ["Select article buffer" gnus-summary-select-article-buffer t]
1631              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1632              ["Isearch article..." gnus-summary-isearch-article t]
1633              ["Beginning of the article" gnus-summary-beginning-of-article t]
1634              ["End of the article" gnus-summary-end-of-article t]
1635              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1636              ["Fetch referenced articles" gnus-summary-refer-references t]
1637              ["Fetch current thread" gnus-summary-refer-thread t]
1638              ["Fetch article with id..." gnus-summary-refer-article t]
1639              ["Redisplay" gnus-summary-show-article t])))
1640       (easy-menu-define
1641        gnus-summary-article-menu gnus-summary-mode-map ""
1642        (cons "Article" innards))
1643
1644       (easy-menu-define
1645        gnus-article-commands-menu gnus-article-mode-map ""
1646        (cons "Commands" innards)))
1647
1648     (easy-menu-define
1649      gnus-summary-thread-menu gnus-summary-mode-map ""
1650      '("Threads"
1651        ["Toggle threading" gnus-summary-toggle-threads t]
1652        ["Hide threads" gnus-summary-hide-all-threads t]
1653        ["Show threads" gnus-summary-show-all-threads t]
1654        ["Hide thread" gnus-summary-hide-thread t]
1655        ["Show thread" gnus-summary-show-thread t]
1656        ["Go to next thread" gnus-summary-next-thread t]
1657        ["Go to previous thread" gnus-summary-prev-thread t]
1658        ["Go down thread" gnus-summary-down-thread t]
1659        ["Go up thread" gnus-summary-up-thread t]
1660        ["Top of thread" gnus-summary-top-thread t]
1661        ["Mark thread as read" gnus-summary-kill-thread t]
1662        ["Lower thread score" gnus-summary-lower-thread t]
1663        ["Raise thread score" gnus-summary-raise-thread t]
1664        ["Rethread current" gnus-summary-rethread-current t]
1665        ))
1666
1667     (easy-menu-define
1668      gnus-summary-post-menu gnus-summary-mode-map ""
1669      '("Post"
1670        ["Post an article" gnus-summary-post-news t]
1671        ["Followup" gnus-summary-followup t]
1672        ["Followup and yank" gnus-summary-followup-with-original t]
1673        ["Supersede article" gnus-summary-supersede-article t]
1674        ["Cancel article" gnus-summary-cancel-article t]
1675        ["Reply" gnus-summary-reply t]
1676        ["Reply and yank" gnus-summary-reply-with-original t]
1677        ["Wide reply" gnus-summary-wide-reply t]
1678        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1679        ["Mail forward" gnus-summary-mail-forward t]
1680        ["Post forward" gnus-summary-post-forward t]
1681        ["Digest and mail" gnus-summary-mail-digest t]
1682        ["Digest and post" gnus-summary-post-digest t]
1683        ["Resend message" gnus-summary-resend-message t]
1684        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1685        ["Send a mail" gnus-summary-mail-other-window t]
1686        ["Uuencode and post" gnus-uu-post-news t]
1687        ["Followup via news" gnus-summary-followup-to-mail t]
1688        ["Followup via news and yank"
1689         gnus-summary-followup-to-mail-with-original t]
1690        ;;("Draft"
1691        ;;["Send" gnus-summary-send-draft t]
1692        ;;["Send bounced" gnus-resend-bounced-mail t])
1693        ))
1694
1695     (easy-menu-define
1696      gnus-summary-misc-menu gnus-summary-mode-map ""
1697      '("Misc"
1698        ("Mark Read"
1699         ["Mark as read" gnus-summary-mark-as-read-forward t]
1700         ["Mark same subject and select"
1701          gnus-summary-kill-same-subject-and-select t]
1702         ["Mark same subject" gnus-summary-kill-same-subject t]
1703         ["Catchup" gnus-summary-catchup t]
1704         ["Catchup all" gnus-summary-catchup-all t]
1705         ["Catchup to here" gnus-summary-catchup-to-here t]
1706         ["Catchup region" gnus-summary-mark-region-as-read t]
1707         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1708        ("Mark Various"
1709         ["Tick" gnus-summary-tick-article-forward t]
1710         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1711         ["Remove marks" gnus-summary-clear-mark-forward t]
1712         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1713         ["Set bookmark" gnus-summary-set-bookmark t]
1714         ["Remove bookmark" gnus-summary-remove-bookmark t])
1715        ("Mark Limit"
1716         ["Marks..." gnus-summary-limit-to-marks t]
1717         ["Subject..." gnus-summary-limit-to-subject t]
1718         ["Author..." gnus-summary-limit-to-author t]
1719         ["Age..." gnus-summary-limit-to-age t]
1720         ["Score" gnus-summary-limit-to-score t]
1721         ["Unread" gnus-summary-limit-to-unread t]
1722         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1723         ["Articles" gnus-summary-limit-to-articles t]
1724         ["Pop limit" gnus-summary-pop-limit t]
1725         ["Show dormant" gnus-summary-limit-include-dormant t]
1726         ["Hide childless dormant"
1727          gnus-summary-limit-exclude-childless-dormant t]
1728         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1729         ["Show expunged" gnus-summary-show-all-expunged t])
1730        ("Process Mark"
1731         ["Set mark" gnus-summary-mark-as-processable t]
1732         ["Remove mark" gnus-summary-unmark-as-processable t]
1733         ["Remove all marks" gnus-summary-unmark-all-processable t]
1734         ["Mark above" gnus-uu-mark-over t]
1735         ["Mark series" gnus-uu-mark-series t]
1736         ["Mark region" gnus-uu-mark-region t]
1737         ["Unmark region" gnus-uu-unmark-region t]
1738         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1739         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1740         ["Mark all" gnus-uu-mark-all t]
1741         ["Mark buffer" gnus-uu-mark-buffer t]
1742         ["Mark sparse" gnus-uu-mark-sparse t]
1743         ["Mark thread" gnus-uu-mark-thread t]
1744         ["Unmark thread" gnus-uu-unmark-thread t]
1745         ("Process Mark Sets"
1746          ["Kill" gnus-summary-kill-process-mark t]
1747          ["Yank" gnus-summary-yank-process-mark
1748           gnus-newsgroup-process-stack]
1749          ["Save" gnus-summary-save-process-mark t]))
1750        ("Scroll article"
1751         ["Page forward" gnus-summary-next-page t]
1752         ["Page backward" gnus-summary-prev-page t]
1753         ["Line forward" gnus-summary-scroll-up t])
1754        ("Move"
1755         ["Next unread article" gnus-summary-next-unread-article t]
1756         ["Previous unread article" gnus-summary-prev-unread-article t]
1757         ["Next article" gnus-summary-next-article t]
1758         ["Previous article" gnus-summary-prev-article t]
1759         ["Next unread subject" gnus-summary-next-unread-subject t]
1760         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1761         ["Next article same subject" gnus-summary-next-same-subject t]
1762         ["Previous article same subject" gnus-summary-prev-same-subject t]
1763         ["First unread article" gnus-summary-first-unread-article t]
1764         ["Best unread article" gnus-summary-best-unread-article t]
1765         ["Go to subject number..." gnus-summary-goto-subject t]
1766         ["Go to article number..." gnus-summary-goto-article t]
1767         ["Go to the last article" gnus-summary-goto-last-article t]
1768         ["Pop article off history" gnus-summary-pop-article t])
1769        ("Sort"
1770         ["Sort by number" gnus-summary-sort-by-number t]
1771         ["Sort by author" gnus-summary-sort-by-author t]
1772         ["Sort by subject" gnus-summary-sort-by-subject t]
1773         ["Sort by date" gnus-summary-sort-by-date t]
1774         ["Sort by score" gnus-summary-sort-by-score t]
1775         ["Sort by lines" gnus-summary-sort-by-lines t])
1776        ("Help"
1777         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1778         ["Describe group" gnus-summary-describe-group t]
1779         ["Read manual" gnus-info-find-node t])
1780        ("Modes"
1781         ["Pick and read" gnus-pick-mode t]
1782         ["Binary" gnus-binary-mode t])
1783        ("Regeneration"
1784         ["Regenerate" gnus-summary-prepare t]
1785         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1786         ["Toggle threading" gnus-summary-toggle-threads t])
1787        ["Filter articles..." gnus-summary-execute-command t]
1788        ["Run command on subjects..." gnus-summary-universal-argument t]
1789        ["Search articles forward..." gnus-summary-search-article-forward t]
1790        ["Search articles backward..." gnus-summary-search-article-backward t]
1791        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1792        ["Expand window" gnus-summary-expand-window t]
1793        ["Expire expirable articles" gnus-summary-expire-articles
1794         (gnus-check-backend-function
1795          'request-expire-articles gnus-newsgroup-name)]
1796        ["Edit local kill file" gnus-summary-edit-local-kill t]
1797        ["Edit main kill file" gnus-summary-edit-global-kill t]
1798        ["Edit group parameters" gnus-summary-edit-parameters t]
1799        ["Customize group parameters" gnus-summary-customize-parameters t]
1800        ["Send a bug report" gnus-bug t]
1801        ("Exit"
1802         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1803         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1804         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1805         ["Exit group" gnus-summary-exit t]
1806         ["Exit group without updating" gnus-summary-exit-no-update t]
1807         ["Exit and goto next group" gnus-summary-next-group t]
1808         ["Exit and goto prev group" gnus-summary-prev-group t]
1809         ["Reselect group" gnus-summary-reselect-current-group t]
1810         ["Rescan group" gnus-summary-rescan-group t]
1811         ["Update dribble" gnus-summary-save-newsrc t])))
1812
1813     (gnus-run-hooks 'gnus-summary-menu-hook)))
1814
1815 (defun gnus-score-set-default (var value)
1816   "A version of set that updates the GNU Emacs menu-bar."
1817   (set var value)
1818   ;; It is the message that forces the active status to be updated.
1819   (message ""))
1820
1821 (defun gnus-make-score-map (type)
1822   "Make a summary score map of type TYPE."
1823   (if t
1824       nil
1825     (let ((headers '(("author" "from" string)
1826                      ("subject" "subject" string)
1827                      ("article body" "body" string)
1828                      ("article head" "head" string)
1829                      ("xref" "xref" string)
1830                      ("extra header" "extra" string)
1831                      ("lines" "lines" number)
1832                      ("followups to author" "followup" string)))
1833           (types '((number ("less than" <)
1834                            ("greater than" >)
1835                            ("equal" =))
1836                    (string ("substring" s)
1837                            ("exact string" e)
1838                            ("fuzzy string" f)
1839                            ("regexp" r))))
1840           (perms '(("temporary" (current-time-string))
1841                    ("permanent" nil)
1842                    ("immediate" now)))
1843           header)
1844       (list
1845        (apply
1846         'nconc
1847         (list
1848          (if (eq type 'lower)
1849              "Lower score"
1850            "Increase score"))
1851         (let (outh)
1852           (while headers
1853             (setq header (car headers))
1854             (setq outh
1855                   (cons
1856                    (apply
1857                     'nconc
1858                     (list (car header))
1859                     (let ((ts (cdr (assoc (nth 2 header) types)))
1860                           outt)
1861                       (while ts
1862                         (setq outt
1863                               (cons
1864                                (apply
1865                                 'nconc
1866                                 (list (caar ts))
1867                                 (let ((ps perms)
1868                                       outp)
1869                                   (while ps
1870                                     (setq outp
1871                                           (cons
1872                                            (vector
1873                                             (caar ps)
1874                                             (list
1875                                              'gnus-summary-score-entry
1876                                              (nth 1 header)
1877                                              (if (or (string= (nth 1 header)
1878                                                               "head")
1879                                                      (string= (nth 1 header)
1880                                                               "body"))
1881                                                  ""
1882                                                (list 'gnus-summary-header
1883                                                      (nth 1 header)))
1884                                              (list 'quote (nth 1 (car ts)))
1885                                              (list 'gnus-score-default nil)
1886                                              (nth 1 (car ps))
1887                                              t)
1888                                             t)
1889                                            outp))
1890                                     (setq ps (cdr ps)))
1891                                   (list (nreverse outp))))
1892                                outt))
1893                         (setq ts (cdr ts)))
1894                       (list (nreverse outt))))
1895                    outh))
1896             (setq headers (cdr headers)))
1897           (list (nreverse outh))))))))
1898
1899 \f
1900
1901 (defun gnus-summary-mode (&optional group)
1902   "Major mode for reading articles.
1903
1904 All normal editing commands are switched off.
1905 \\<gnus-summary-mode-map>
1906 Each line in this buffer represents one article.  To read an
1907 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1908 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1909 respectively.
1910
1911 You can also post articles and send mail from this buffer.  To
1912 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1913 of an article, type `\\[gnus-summary-reply]'.
1914
1915 There are approx. one gazillion commands you can execute in this
1916 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1917
1918 The following commands are available:
1919
1920 \\{gnus-summary-mode-map}"
1921   (interactive)
1922   (when (gnus-visual-p 'summary-menu 'menu)
1923     (gnus-summary-make-menu-bar))
1924   (kill-all-local-variables)
1925   (gnus-summary-make-local-variables)
1926   (gnus-make-thread-indent-array)
1927   (gnus-simplify-mode-line)
1928   (setq major-mode 'gnus-summary-mode)
1929   (setq mode-name "Summary")
1930   (make-local-variable 'minor-mode-alist)
1931   (use-local-map gnus-summary-mode-map)
1932   (buffer-disable-undo)
1933   (setq buffer-read-only t)             ;Disable modification
1934   (setq truncate-lines t)
1935   (setq selective-display t)
1936   (setq selective-display-ellipses t)   ;Display `...'
1937   (gnus-summary-set-display-table)
1938   (gnus-set-default-directory)
1939   (setq gnus-newsgroup-name group)
1940   (make-local-variable 'gnus-summary-line-format)
1941   (make-local-variable 'gnus-summary-line-format-spec)
1942   (make-local-variable 'gnus-summary-dummy-line-format)
1943   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1944   (make-local-variable 'gnus-summary-mark-positions)
1945   (make-local-hook 'post-command-hook)
1946   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1947   (make-local-hook 'pre-command-hook)
1948   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1949   (gnus-run-hooks 'gnus-summary-mode-hook)
1950   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1951   (gnus-update-summary-mark-positions))
1952
1953 (defun gnus-summary-make-local-variables ()
1954   "Make all the local summary buffer variables."
1955   (let ((locals gnus-summary-local-variables)
1956         global local)
1957     (while (setq local (pop locals))
1958       (if (consp local)
1959           (progn
1960             (if (eq (cdr local) 'global)
1961                 ;; Copy the global value of the variable.
1962                 (setq global (symbol-value (car local)))
1963               ;; Use the value from the list.
1964               (setq global (eval (cdr local))))
1965             (make-local-variable (car local))
1966             (set (car local) global))
1967         ;; Simple nil-valued local variable.
1968         (make-local-variable local)
1969         (set local nil)))))
1970
1971 (defun gnus-summary-clear-local-variables ()
1972   (let ((locals gnus-summary-local-variables))
1973     (while locals
1974       (if (consp (car locals))
1975           (and (vectorp (caar locals))
1976                (set (caar locals) nil))
1977         (and (vectorp (car locals))
1978              (set (car locals) nil)))
1979       (setq locals (cdr locals)))))
1980
1981 ;; Summary data functions.
1982
1983 (defmacro gnus-data-number (data)
1984   `(car ,data))
1985
1986 (defmacro gnus-data-set-number (data number)
1987   `(setcar ,data ,number))
1988
1989 (defmacro gnus-data-mark (data)
1990   `(nth 1 ,data))
1991
1992 (defmacro gnus-data-set-mark (data mark)
1993   `(setcar (nthcdr 1 ,data) ,mark))
1994
1995 (defmacro gnus-data-pos (data)
1996   `(nth 2 ,data))
1997
1998 (defmacro gnus-data-set-pos (data pos)
1999   `(setcar (nthcdr 2 ,data) ,pos))
2000
2001 (defmacro gnus-data-header (data)
2002   `(nth 3 ,data))
2003
2004 (defmacro gnus-data-set-header (data header)
2005   `(setf (nth 3 ,data) ,header))
2006
2007 (defmacro gnus-data-level (data)
2008   `(nth 4 ,data))
2009
2010 (defmacro gnus-data-unread-p (data)
2011   `(= (nth 1 ,data) gnus-unread-mark))
2012
2013 (defmacro gnus-data-read-p (data)
2014   `(/= (nth 1 ,data) gnus-unread-mark))
2015
2016 (defmacro gnus-data-pseudo-p (data)
2017   `(consp (nth 3 ,data)))
2018
2019 (defmacro gnus-data-find (number)
2020   `(assq ,number gnus-newsgroup-data))
2021
2022 (defmacro gnus-data-find-list (number &optional data)
2023   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2024      (memq (assq ,number bdata)
2025            bdata)))
2026
2027 (defmacro gnus-data-make (number mark pos header level)
2028   `(list ,number ,mark ,pos ,header ,level))
2029
2030 (defun gnus-data-enter (after-article number mark pos header level offset)
2031   (let ((data (gnus-data-find-list after-article)))
2032     (unless data
2033       (error "No such article: %d" after-article))
2034     (setcdr data (cons (gnus-data-make number mark pos header level)
2035                        (cdr data)))
2036     (setq gnus-newsgroup-data-reverse nil)
2037     (gnus-data-update-list (cddr data) offset)))
2038
2039 (defun gnus-data-enter-list (after-article list &optional offset)
2040   (when list
2041     (let ((data (and after-article (gnus-data-find-list after-article)))
2042           (ilist list))
2043       (if (not (or data
2044                    after-article))
2045           (let ((odata gnus-newsgroup-data))
2046             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2047             (when offset
2048               (gnus-data-update-list odata offset)))
2049         ;; Find the last element in the list to be spliced into the main
2050         ;; list.
2051         (while (cdr list)
2052           (setq list (cdr list)))
2053         (if (not data)
2054             (progn
2055               (setcdr list gnus-newsgroup-data)
2056               (setq gnus-newsgroup-data ilist)
2057               (when offset
2058                 (gnus-data-update-list (cdr list) offset)))
2059           (setcdr list (cdr data))
2060           (setcdr data ilist)
2061           (when offset
2062             (gnus-data-update-list (cdr list) offset))))
2063       (setq gnus-newsgroup-data-reverse nil))))
2064
2065 (defun gnus-data-remove (article &optional offset)
2066   (let ((data gnus-newsgroup-data))
2067     (if (= (gnus-data-number (car data)) article)
2068         (progn
2069           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2070                 gnus-newsgroup-data-reverse nil)
2071           (when offset
2072             (gnus-data-update-list gnus-newsgroup-data offset)))
2073       (while (cdr data)
2074         (when (= (gnus-data-number (cadr data)) article)
2075           (setcdr data (cddr data))
2076           (when offset
2077             (gnus-data-update-list (cdr data) offset))
2078           (setq data nil
2079                 gnus-newsgroup-data-reverse nil))
2080         (setq data (cdr data))))))
2081
2082 (defmacro gnus-data-list (backward)
2083   `(if ,backward
2084        (or gnus-newsgroup-data-reverse
2085            (setq gnus-newsgroup-data-reverse
2086                  (reverse gnus-newsgroup-data)))
2087      gnus-newsgroup-data))
2088
2089 (defun gnus-data-update-list (data offset)
2090   "Add OFFSET to the POS of all data entries in DATA."
2091   (setq gnus-newsgroup-data-reverse nil)
2092   (while data
2093     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2094     (setq data (cdr data))))
2095
2096 (defun gnus-summary-article-pseudo-p (article)
2097   "Say whether this article is a pseudo article or not."
2098   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2099
2100 (defmacro gnus-summary-article-sparse-p (article)
2101   "Say whether this article is a sparse article or not."
2102   `(memq ,article gnus-newsgroup-sparse))
2103
2104 (defmacro gnus-summary-article-ancient-p (article)
2105   "Say whether this article is a sparse article or not."
2106   `(memq ,article gnus-newsgroup-ancient))
2107
2108 (defun gnus-article-parent-p (number)
2109   "Say whether this article is a parent or not."
2110   (let ((data (gnus-data-find-list number)))
2111     (and (cdr data)                     ; There has to be an article after...
2112          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2113             (gnus-data-level (nth 1 data))))))
2114
2115 (defun gnus-article-children (number)
2116   "Return a list of all children to NUMBER."
2117   (let* ((data (gnus-data-find-list number))
2118          (level (gnus-data-level (car data)))
2119          children)
2120     (setq data (cdr data))
2121     (while (and data
2122                 (= (gnus-data-level (car data)) (1+ level)))
2123       (push (gnus-data-number (car data)) children)
2124       (setq data (cdr data)))
2125     children))
2126
2127 (defmacro gnus-summary-skip-intangible ()
2128   "If the current article is intangible, then jump to a different article."
2129   '(let ((to (get-text-property (point) 'gnus-intangible)))
2130      (and to (gnus-summary-goto-subject to))))
2131
2132 (defmacro gnus-summary-article-intangible-p ()
2133   "Say whether this article is intangible or not."
2134   '(get-text-property (point) 'gnus-intangible))
2135
2136 (defun gnus-article-read-p (article)
2137   "Say whether ARTICLE is read or not."
2138   (not (or (memq article gnus-newsgroup-marked)
2139            (memq article gnus-newsgroup-unreads)
2140            (memq article gnus-newsgroup-unselected)
2141            (memq article gnus-newsgroup-dormant))))
2142
2143 ;; Some summary mode macros.
2144
2145 (defmacro gnus-summary-article-number ()
2146   "The article number of the article on the current line.
2147 If there isn's an article number here, then we return the current
2148 article number."
2149   '(progn
2150      (gnus-summary-skip-intangible)
2151      (or (get-text-property (point) 'gnus-number)
2152          (gnus-summary-last-subject))))
2153
2154 (defmacro gnus-summary-article-header (&optional number)
2155   "Return the header of article NUMBER."
2156   `(gnus-data-header (gnus-data-find
2157                       ,(or number '(gnus-summary-article-number)))))
2158
2159 (defmacro gnus-summary-thread-level (&optional number)
2160   "Return the level of thread that starts with article NUMBER."
2161   `(if (and (eq gnus-summary-make-false-root 'dummy)
2162             (get-text-property (point) 'gnus-intangible))
2163        0
2164      (gnus-data-level (gnus-data-find
2165                        ,(or number '(gnus-summary-article-number))))))
2166
2167 (defmacro gnus-summary-article-mark (&optional number)
2168   "Return the mark of article NUMBER."
2169   `(gnus-data-mark (gnus-data-find
2170                     ,(or number '(gnus-summary-article-number)))))
2171
2172 (defmacro gnus-summary-article-pos (&optional number)
2173   "Return the position of the line of article NUMBER."
2174   `(gnus-data-pos (gnus-data-find
2175                    ,(or number '(gnus-summary-article-number)))))
2176
2177 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2178 (defmacro gnus-summary-article-subject (&optional number)
2179   "Return current subject string or nil if nothing."
2180   `(let ((headers
2181           ,(if number
2182                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2183              '(gnus-data-header (assq (gnus-summary-article-number)
2184                                       gnus-newsgroup-data)))))
2185      (and headers
2186           (vectorp headers)
2187           (mail-header-subject headers))))
2188
2189 (defmacro gnus-summary-article-score (&optional number)
2190   "Return current article score."
2191   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2192                   gnus-newsgroup-scored))
2193        gnus-summary-default-score 0))
2194
2195 (defun gnus-summary-article-children (&optional number)
2196   "Return a list of article numbers that are children of article NUMBER."
2197   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2198          (level (gnus-data-level (car data)))
2199          l children)
2200     (while (and (setq data (cdr data))
2201                 (> (setq l (gnus-data-level (car data))) level))
2202       (and (= (1+ level) l)
2203            (push (gnus-data-number (car data))
2204                  children)))
2205     (nreverse children)))
2206
2207 (defun gnus-summary-article-parent (&optional number)
2208   "Return the article number of the parent of article NUMBER."
2209   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2210                                     (gnus-data-list t)))
2211          (level (gnus-data-level (car data))))
2212     (if (zerop level)
2213         ()                              ; This is a root.
2214       ;; We search until we find an article with a level less than
2215       ;; this one.  That function has to be the parent.
2216       (while (and (setq data (cdr data))
2217                   (not (< (gnus-data-level (car data)) level))))
2218       (and data (gnus-data-number (car data))))))
2219
2220 (defun gnus-unread-mark-p (mark)
2221   "Say whether MARK is the unread mark."
2222   (= mark gnus-unread-mark))
2223
2224 (defun gnus-read-mark-p (mark)
2225   "Say whether MARK is one of the marks that mark as read.
2226 This is all marks except unread, ticked, dormant, and expirable."
2227   (not (or (= mark gnus-unread-mark)
2228            (= mark gnus-ticked-mark)
2229            (= mark gnus-dormant-mark)
2230            (= mark gnus-expirable-mark))))
2231
2232 (defmacro gnus-article-mark (number)
2233   "Return the MARK of article NUMBER.
2234 This macro should only be used when computing the mark the \"first\"
2235 time; i.e., when generating the summary lines.  After that,
2236 `gnus-summary-article-mark' should be used to examine the
2237 marks of articles."
2238   `(cond
2239     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2240     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2241     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2242     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2243     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2244     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2245     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2246     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2247            gnus-ancient-mark))))
2248
2249 ;; Saving hidden threads.
2250
2251 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2252 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2253
2254 (defmacro gnus-save-hidden-threads (&rest forms)
2255   "Save hidden threads, eval FORMS, and restore the hidden threads."
2256   (let ((config (make-symbol "config")))
2257     `(let ((,config (gnus-hidden-threads-configuration)))
2258        (unwind-protect
2259            (save-excursion
2260              ,@forms)
2261          (gnus-restore-hidden-threads-configuration ,config)))))
2262
2263 (defun gnus-data-compute-positions ()
2264   "Compute the positions of all articles."
2265   (setq gnus-newsgroup-data-reverse nil)
2266   (let ((data gnus-newsgroup-data))
2267     (save-excursion
2268       (gnus-save-hidden-threads
2269         (gnus-summary-show-all-threads)
2270         (goto-char (point-min))
2271         (while data
2272           (while (get-text-property (point) 'gnus-intangible)
2273             (forward-line 1))
2274           (gnus-data-set-pos (car data) (+ (point) 3))
2275           (setq data (cdr data))
2276           (forward-line 1))))))
2277
2278 (defun gnus-hidden-threads-configuration ()
2279   "Return the current hidden threads configuration."
2280   (save-excursion
2281     (let (config)
2282       (goto-char (point-min))
2283       (while (search-forward "\r" nil t)
2284         (push (1- (point)) config))
2285       config)))
2286
2287 (defun gnus-restore-hidden-threads-configuration (config)
2288   "Restore hidden threads configuration from CONFIG."
2289   (let (point buffer-read-only)
2290     (while (setq point (pop config))
2291       (when (and (< point (point-max))
2292                  (goto-char point)
2293                  (eq (char-after) ?\n))
2294         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2295
2296 ;; Various summary mode internalish functions.
2297
2298 (defun gnus-mouse-pick-article (e)
2299   (interactive "e")
2300   (mouse-set-point e)
2301   (gnus-summary-next-page nil t))
2302
2303 (defun gnus-summary-set-display-table ()
2304   ;; Change the display table.  Odd characters have a tendency to mess
2305   ;; up nicely formatted displays - we make all possible glyphs
2306   ;; display only a single character.
2307
2308   ;; We start from the standard display table, if any.
2309   (let ((table (or (copy-sequence standard-display-table)
2310                    (make-display-table)))
2311         (i 32))
2312     ;; Nix out all the control chars...
2313     (while (>= (setq i (1- i)) 0)
2314       (aset table i [??]))
2315     ;; ... but not newline and cr, of course.  (cr is necessary for the
2316     ;; selective display).
2317     (aset table ?\n nil)
2318     (aset table ?\r nil)
2319     ;; We keep TAB as well.
2320     (aset table ?\t nil)
2321     ;; We nix out any glyphs over 126 that are not set already.
2322     (let ((i 256))
2323       (while (>= (setq i (1- i)) 127)
2324         ;; Only modify if the entry is nil.
2325         (unless (aref table i)
2326           (aset table i [??]))))
2327     (setq buffer-display-table table)))
2328
2329 (defun gnus-summary-setup-buffer (group)
2330   "Initialize summary buffer."
2331   (let ((buffer (concat "*Summary " group "*")))
2332     (if (get-buffer buffer)
2333         (progn
2334           (set-buffer buffer)
2335           (setq gnus-summary-buffer (current-buffer))
2336           (not gnus-newsgroup-prepared))
2337       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2338       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2339       (gnus-summary-mode group)
2340       (when gnus-carpal
2341         (gnus-carpal-setup-buffer 'summary))
2342       (unless gnus-single-article-buffer
2343         (make-local-variable 'gnus-article-buffer)
2344         (make-local-variable 'gnus-article-current)
2345         (make-local-variable 'gnus-original-article-buffer))
2346       (setq gnus-newsgroup-name group)
2347       t)))
2348
2349 (defun gnus-set-global-variables ()
2350   ;; Set the global equivalents of the summary buffer-local variables
2351   ;; to the latest values they had.  These reflect the summary buffer
2352   ;; that was in action when the last article was fetched.
2353   (when (eq major-mode 'gnus-summary-mode)
2354     (setq gnus-summary-buffer (current-buffer))
2355     (let ((name gnus-newsgroup-name)
2356           (marked gnus-newsgroup-marked)
2357           (unread gnus-newsgroup-unreads)
2358           (headers gnus-current-headers)
2359           (data gnus-newsgroup-data)
2360           (summary gnus-summary-buffer)
2361           (article-buffer gnus-article-buffer)
2362           (original gnus-original-article-buffer)
2363           (gac gnus-article-current)
2364           (reffed gnus-reffed-article-number)
2365           (score-file gnus-current-score-file)
2366           (default-charset gnus-newsgroup-charset))
2367       (save-excursion
2368         (set-buffer gnus-group-buffer)
2369         (setq gnus-newsgroup-name name
2370               gnus-newsgroup-marked marked
2371               gnus-newsgroup-unreads unread
2372               gnus-current-headers headers
2373               gnus-newsgroup-data data
2374               gnus-article-current gac
2375               gnus-summary-buffer summary
2376               gnus-article-buffer article-buffer
2377               gnus-original-article-buffer original
2378               gnus-reffed-article-number reffed
2379               gnus-current-score-file score-file
2380               gnus-newsgroup-charset default-charset)
2381         ;; The article buffer also has local variables.
2382         (when (gnus-buffer-live-p gnus-article-buffer)
2383           (set-buffer gnus-article-buffer)
2384           (setq gnus-summary-buffer summary))))))
2385
2386 (defun gnus-summary-article-unread-p (article)
2387   "Say whether ARTICLE is unread or not."
2388   (memq article gnus-newsgroup-unreads))
2389
2390 (defun gnus-summary-first-article-p (&optional article)
2391   "Return whether ARTICLE is the first article in the buffer."
2392   (if (not (setq article (or article (gnus-summary-article-number))))
2393       nil
2394     (eq article (caar gnus-newsgroup-data))))
2395
2396 (defun gnus-summary-last-article-p (&optional article)
2397   "Return whether ARTICLE is the last article in the buffer."
2398   (if (not (setq article (or article (gnus-summary-article-number))))
2399       t                                 ; All non-existent numbers are the last article.  :-)
2400     (not (cdr (gnus-data-find-list article)))))
2401
2402 (defun gnus-make-thread-indent-array ()
2403   (let ((n 200))
2404     (unless (and gnus-thread-indent-array
2405                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2406       (setq gnus-thread-indent-array (make-vector 201 "")
2407             gnus-thread-indent-array-level gnus-thread-indent-level)
2408       (while (>= n 0)
2409         (aset gnus-thread-indent-array n
2410               (make-string (* n gnus-thread-indent-level) ? ))
2411         (setq n (1- n))))))
2412
2413 (defun gnus-update-summary-mark-positions ()
2414   "Compute where the summary marks are to go."
2415   (save-excursion
2416     (when (gnus-buffer-exists-p gnus-summary-buffer)
2417       (set-buffer gnus-summary-buffer))
2418     (let ((gnus-replied-mark 129)
2419           (gnus-score-below-mark 130)
2420           (gnus-score-over-mark 130)
2421           (gnus-download-mark 131)
2422           (spec gnus-summary-line-format-spec)
2423           gnus-visual pos)
2424       (save-excursion
2425         (gnus-set-work-buffer)
2426         (let ((gnus-summary-line-format-spec spec)
2427               (gnus-newsgroup-downloadable '((0 . t))))
2428           (gnus-summary-insert-line
2429            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2430            0 nil 128 t nil "" nil 1)
2431           (goto-char (point-min))
2432           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2433                                              (- (point) 2)))))
2434           (goto-char (point-min))
2435           (push (cons 'replied (and (search-forward "\201" nil t)
2436                                     (- (point) 2)))
2437                 pos)
2438           (goto-char (point-min))
2439           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2440                 pos)
2441           (goto-char (point-min))
2442           (push (cons 'download
2443                       (and (search-forward "\203" nil t) (- (point) 2)))
2444                 pos)))
2445       (setq gnus-summary-mark-positions pos))))
2446
2447 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2448   "Insert a dummy root in the summary buffer."
2449   (beginning-of-line)
2450   (gnus-add-text-properties
2451    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2452    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2453
2454 (defun gnus-summary-from-or-to-or-newsgroups (header)
2455   (let ((to (cdr (assq 'To (mail-header-extra header))))
2456         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2457         (mail-parse-charset gnus-newsgroup-charset))
2458     (cond
2459      ((and to
2460            gnus-ignored-from-addresses
2461            (string-match gnus-ignored-from-addresses
2462                          (mail-header-from header)))
2463       (concat "-> "
2464               (or (car (funcall gnus-extract-address-components
2465                                 (funcall
2466                                  gnus-decode-encoded-word-function to)))
2467                   (funcall gnus-decode-encoded-word-function to))))
2468      ((and newsgroups
2469            gnus-ignored-from-addresses
2470            (string-match gnus-ignored-from-addresses
2471                          (mail-header-from header)))
2472       (concat "=> " newsgroups))
2473      (t
2474       (or (car (funcall gnus-extract-address-components
2475                         (mail-header-from header)))
2476           (mail-header-from header))))))
2477
2478 (defun gnus-summary-insert-line (gnus-tmp-header
2479                                  gnus-tmp-level gnus-tmp-current
2480                                  gnus-tmp-unread gnus-tmp-replied
2481                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2482                                  &optional gnus-tmp-dummy gnus-tmp-score
2483                                  gnus-tmp-process)
2484   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2485          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2486          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2487          (gnus-tmp-score-char
2488           (if (or (null gnus-summary-default-score)
2489                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2490                       gnus-summary-zcore-fuzz))
2491               ? 
2492             (if (< gnus-tmp-score gnus-summary-default-score)
2493                 gnus-score-below-mark gnus-score-over-mark)))
2494          (gnus-tmp-replied
2495           (cond (gnus-tmp-process gnus-process-mark)
2496                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2497                  gnus-cached-mark)
2498                 (gnus-tmp-replied gnus-replied-mark)
2499                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2500                  gnus-saved-mark)
2501                 (t gnus-unread-mark)))
2502          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2503          (gnus-tmp-name
2504           (cond
2505            ((string-match "<[^>]+> *$" gnus-tmp-from)
2506             (let ((beg (match-beginning 0)))
2507               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2508                        (substring gnus-tmp-from (1+ (match-beginning 0))
2509                                   (1- (match-end 0))))
2510                   (substring gnus-tmp-from 0 beg))))
2511            ((string-match "(.+)" gnus-tmp-from)
2512             (substring gnus-tmp-from
2513                        (1+ (match-beginning 0)) (1- (match-end 0))))
2514            (t gnus-tmp-from)))
2515          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2516          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2517          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2518          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2519          (buffer-read-only nil))
2520     (when (string= gnus-tmp-name "")
2521       (setq gnus-tmp-name gnus-tmp-from))
2522     (unless (numberp gnus-tmp-lines)
2523       (setq gnus-tmp-lines 0))
2524     (gnus-put-text-property-excluding-characters-with-faces
2525      (point)
2526      (progn (eval gnus-summary-line-format-spec) (point))
2527      'gnus-number gnus-tmp-number)
2528     (when (gnus-visual-p 'summary-highlight 'highlight)
2529       (forward-line -1)
2530       (gnus-run-hooks 'gnus-summary-update-hook)
2531       (forward-line 1))))
2532
2533 (defun gnus-summary-update-line (&optional dont-update)
2534   ;; Update summary line after change.
2535   (when (and gnus-summary-default-score
2536              (not gnus-summary-inhibit-highlight))
2537     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2538            (article (gnus-summary-article-number))
2539            (score (gnus-summary-article-score article)))
2540       (unless dont-update
2541         (if (and gnus-summary-mark-below
2542                  (< (gnus-summary-article-score)
2543                     gnus-summary-mark-below))
2544             ;; This article has a low score, so we mark it as read.
2545             (when (memq article gnus-newsgroup-unreads)
2546               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2547           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2548             ;; This article was previously marked as read on account
2549             ;; of a low score, but now it has risen, so we mark it as
2550             ;; unread.
2551             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2552         (gnus-summary-update-mark
2553          (if (or (null gnus-summary-default-score)
2554                  (<= (abs (- score gnus-summary-default-score))
2555                      gnus-summary-zcore-fuzz))
2556              ? 
2557            (if (< score gnus-summary-default-score)
2558                gnus-score-below-mark gnus-score-over-mark))
2559          'score))
2560       ;; Do visual highlighting.
2561       (when (gnus-visual-p 'summary-highlight 'highlight)
2562         (gnus-run-hooks 'gnus-summary-update-hook)))))
2563
2564 (defvar gnus-tmp-new-adopts nil)
2565
2566 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2567   "Return the number of articles in THREAD.
2568 This may be 0 in some cases -- if none of the articles in
2569 the thread are to be displayed."
2570   (let* ((number
2571           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2572           (cond
2573            ((not (listp thread))
2574             1)
2575            ((and (consp thread) (cdr thread))
2576             (apply
2577              '+ 1 (mapcar
2578                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2579            ((null thread)
2580             1)
2581            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2582             1)
2583            (t 0))))
2584     (when (and level (zerop level) gnus-tmp-new-adopts)
2585       (incf number
2586             (apply '+ (mapcar
2587                        'gnus-summary-number-of-articles-in-thread
2588                        gnus-tmp-new-adopts))))
2589     (if char
2590         (if (> number 1) gnus-not-empty-thread-mark
2591           gnus-empty-thread-mark)
2592       number)))
2593
2594 (defun gnus-summary-set-local-parameters (group)
2595   "Go through the local params of GROUP and set all variable specs in that list."
2596   (let ((params (gnus-group-find-parameter group))
2597         elem)
2598     (while params
2599       (setq elem (car params)
2600             params (cdr params))
2601       (and (consp elem)                 ; Has to be a cons.
2602            (consp (cdr elem))           ; The cdr has to be a list.
2603            (symbolp (car elem))         ; Has to be a symbol in there.
2604            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2605            (ignore-errors               ; So we set it.
2606              (make-local-variable (car elem))
2607              (set (car elem) (eval (nth 1 elem))))))))
2608
2609 (defun gnus-summary-read-group (group &optional show-all no-article
2610                                       kill-buffer no-display backward
2611                                       select-articles)
2612   "Start reading news in newsgroup GROUP.
2613 If SHOW-ALL is non-nil, already read articles are also listed.
2614 If NO-ARTICLE is non-nil, no article is selected initially.
2615 If NO-DISPLAY, don't generate a summary buffer."
2616   (let (result)
2617     (while (and group
2618                 (null (setq result
2619                             (let ((gnus-auto-select-next nil))
2620                               (or (gnus-summary-read-group-1
2621                                    group show-all no-article
2622                                    kill-buffer no-display
2623                                    select-articles)
2624                                   (setq show-all nil
2625                                         select-articles nil)))))
2626                 (eq gnus-auto-select-next 'quietly))
2627       (set-buffer gnus-group-buffer)
2628       ;; The entry function called above goes to the next
2629       ;; group automatically, so we go two groups back
2630       ;; if we are searching for the previous group.
2631       (when backward
2632         (gnus-group-prev-unread-group 2))
2633       (if (not (equal group (gnus-group-group-name)))
2634           (setq group (gnus-group-group-name))
2635         (setq group nil)))
2636     result))
2637
2638 (defun gnus-summary-read-group-1 (group show-all no-article
2639                                         kill-buffer no-display
2640                                         &optional select-articles)
2641   ;; Killed foreign groups can't be entered.
2642   (when (and (not (gnus-group-native-p group))
2643              (not (gnus-gethash group gnus-newsrc-hashtb)))
2644     (error "Dead non-native groups can't be entered"))
2645   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2646   (let* ((new-group (gnus-summary-setup-buffer group))
2647          (quit-config (gnus-group-quit-config group))
2648          (did-select (and new-group (gnus-select-newsgroup
2649                                      group show-all select-articles))))
2650     (cond
2651      ;; This summary buffer exists already, so we just select it.
2652      ((not new-group)
2653       (gnus-set-global-variables)
2654       (when kill-buffer
2655         (gnus-kill-or-deaden-summary kill-buffer))
2656       (gnus-configure-windows 'summary 'force)
2657       (gnus-set-mode-line 'summary)
2658       (gnus-summary-position-point)
2659       (message "")
2660       t)
2661      ;; We couldn't select this group.
2662      ((null did-select)
2663       (when (and (eq major-mode 'gnus-summary-mode)
2664                  (not (equal (current-buffer) kill-buffer)))
2665         (kill-buffer (current-buffer))
2666         (if (not quit-config)
2667             (progn
2668               ;; Update the info -- marks might need to be removed,
2669               ;; for instance.
2670               (gnus-summary-update-info)
2671               (set-buffer gnus-group-buffer)
2672               (gnus-group-jump-to-group group)
2673               (gnus-group-next-unread-group 1))
2674           (gnus-handle-ephemeral-exit quit-config)))
2675       (gnus-message 3 "Can't select group")
2676       nil)
2677      ;; The user did a `C-g' while prompting for number of articles,
2678      ;; so we exit this group.
2679      ((eq did-select 'quit)
2680       (and (eq major-mode 'gnus-summary-mode)
2681            (not (equal (current-buffer) kill-buffer))
2682            (kill-buffer (current-buffer)))
2683       (when kill-buffer
2684         (gnus-kill-or-deaden-summary kill-buffer))
2685       (if (not quit-config)
2686           (progn
2687             (set-buffer gnus-group-buffer)
2688             (gnus-group-jump-to-group group)
2689             (gnus-group-next-unread-group 1)
2690             (gnus-configure-windows 'group 'force))
2691         (gnus-handle-ephemeral-exit quit-config))
2692       ;; Finally signal the quit.
2693       (signal 'quit nil))
2694      ;; The group was successfully selected.
2695      (t
2696       (gnus-set-global-variables)
2697       ;; Save the active value in effect when the group was entered.
2698       (setq gnus-newsgroup-active
2699             (gnus-copy-sequence
2700              (gnus-active gnus-newsgroup-name)))
2701       ;; You can change the summary buffer in some way with this hook.
2702       (gnus-run-hooks 'gnus-select-group-hook)
2703       ;; Set any local variables in the group parameters.
2704       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2705       (gnus-update-format-specifications
2706        nil 'summary 'summary-mode 'summary-dummy)
2707       ;; Do score processing.
2708       (when gnus-use-scoring
2709         (gnus-possibly-score-headers))
2710       ;; Check whether to fill in the gaps in the threads.
2711       (when gnus-build-sparse-threads
2712         (gnus-build-sparse-threads))
2713       ;; Find the initial limit.
2714       (if gnus-show-threads
2715           (if show-all
2716               (let ((gnus-newsgroup-dormant nil))
2717                 (gnus-summary-initial-limit show-all))
2718             (gnus-summary-initial-limit show-all))
2719         (setq gnus-newsgroup-limit
2720               (mapcar
2721                (lambda (header) (mail-header-number header))
2722                gnus-newsgroup-headers)))
2723       ;; Generate the summary buffer.
2724       (unless no-display
2725         (gnus-summary-prepare))
2726       (when gnus-use-trees
2727         (gnus-tree-open group)
2728         (setq gnus-summary-highlight-line-function
2729               'gnus-tree-highlight-article))
2730       ;; If the summary buffer is empty, but there are some low-scored
2731       ;; articles or some excluded dormants, we include these in the
2732       ;; buffer.
2733       (when (and (zerop (buffer-size))
2734                  (not no-display))
2735         (cond (gnus-newsgroup-dormant
2736                (gnus-summary-limit-include-dormant))
2737               ((and gnus-newsgroup-scored show-all)
2738                (gnus-summary-limit-include-expunged t))))
2739       ;; Function `gnus-apply-kill-file' must be called in this hook.
2740       (gnus-run-hooks 'gnus-apply-kill-hook)
2741       (if (and (zerop (buffer-size))
2742                (not no-display))
2743           (progn
2744             ;; This newsgroup is empty.
2745             (gnus-summary-catchup-and-exit nil t)
2746             (gnus-message 6 "No unread news")
2747             (when kill-buffer
2748               (gnus-kill-or-deaden-summary kill-buffer))
2749             ;; Return nil from this function.
2750             nil)
2751         ;; Hide conversation thread subtrees.  We cannot do this in
2752         ;; gnus-summary-prepare-hook since kill processing may not
2753         ;; work with hidden articles.
2754         (and gnus-show-threads
2755              gnus-thread-hide-subtree
2756              (gnus-summary-hide-all-threads))
2757         (when kill-buffer
2758           (gnus-kill-or-deaden-summary kill-buffer))
2759         ;; Show first unread article if requested.
2760         (if (and (not no-article)
2761                  (not no-display)
2762                  gnus-newsgroup-unreads
2763                  gnus-auto-select-first)
2764             (progn
2765               (gnus-configure-windows 'summary)
2766               (cond
2767                ((eq gnus-auto-select-first 'best)
2768                 (gnus-summary-best-unread-article))
2769                ((eq gnus-auto-select-first t)
2770                 (gnus-summary-first-unread-article))
2771                ((gnus-functionp gnus-auto-select-first)
2772                 (funcall gnus-auto-select-first))))
2773           ;; Don't select any articles, just move point to the first
2774           ;; article in the group.
2775           (goto-char (point-min))
2776           (gnus-summary-position-point)
2777           (gnus-configure-windows 'summary 'force)
2778           (gnus-set-mode-line 'summary))
2779         (when (get-buffer-window gnus-group-buffer t)
2780           ;; Gotta use windows, because recenter does weird stuff if
2781           ;; the current buffer ain't the displayed window.
2782           (let ((owin (selected-window)))
2783             (select-window (get-buffer-window gnus-group-buffer t))
2784             (when (gnus-group-goto-group group)
2785               (recenter))
2786             (select-window owin)))
2787         ;; Mark this buffer as "prepared".
2788         (setq gnus-newsgroup-prepared t)
2789         (gnus-run-hooks 'gnus-summary-prepared-hook)
2790         t)))))
2791
2792 (defun gnus-summary-prepare ()
2793   "Generate the summary buffer."
2794   (interactive)
2795   (let ((buffer-read-only nil))
2796     (erase-buffer)
2797     (setq gnus-newsgroup-data nil
2798           gnus-newsgroup-data-reverse nil)
2799     (gnus-run-hooks 'gnus-summary-generate-hook)
2800     ;; Generate the buffer, either with threads or without.
2801     (when gnus-newsgroup-headers
2802       (gnus-summary-prepare-threads
2803        (if gnus-show-threads
2804            (gnus-sort-gathered-threads
2805             (funcall gnus-summary-thread-gathering-function
2806                      (gnus-sort-threads
2807                       (gnus-cut-threads (gnus-make-threads)))))
2808          ;; Unthreaded display.
2809          (gnus-sort-articles gnus-newsgroup-headers))))
2810     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2811     ;; Call hooks for modifying summary buffer.
2812     (goto-char (point-min))
2813     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2814
2815 (defsubst gnus-general-simplify-subject (subject)
2816   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2817   (setq subject
2818         (cond
2819          ;; Truncate the subject.
2820          (gnus-simplify-subject-functions
2821           (gnus-map-function gnus-simplify-subject-functions subject))
2822          ((numberp gnus-summary-gather-subject-limit)
2823           (setq subject (gnus-simplify-subject-re subject))
2824           (if (> (length subject) gnus-summary-gather-subject-limit)
2825               (substring subject 0 gnus-summary-gather-subject-limit)
2826             subject))
2827          ;; Fuzzily simplify it.
2828          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2829           (gnus-simplify-subject-fuzzy subject))
2830          ;; Just remove the leading "Re:".
2831          (t
2832           (gnus-simplify-subject-re subject))))
2833
2834   (if (and gnus-summary-gather-exclude-subject
2835            (string-match gnus-summary-gather-exclude-subject subject))
2836       nil                               ; This article shouldn't be gathered
2837     subject))
2838
2839 (defun gnus-summary-simplify-subject-query ()
2840   "Query where the respool algorithm would put this article."
2841   (interactive)
2842   (gnus-summary-select-article)
2843   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2844
2845 (defun gnus-gather-threads-by-subject (threads)
2846   "Gather threads by looking at Subject headers."
2847   (if (not gnus-summary-make-false-root)
2848       threads
2849     (let ((hashtb (gnus-make-hashtable 1024))
2850           (prev threads)
2851           (result threads)
2852           subject hthread whole-subject)
2853       (while threads
2854         (setq subject (gnus-general-simplify-subject
2855                        (setq whole-subject (mail-header-subject
2856                                             (caar threads)))))
2857         (when subject
2858           (if (setq hthread (gnus-gethash subject hashtb))
2859               (progn
2860                 ;; We enter a dummy root into the thread, if we
2861                 ;; haven't done that already.
2862                 (unless (stringp (caar hthread))
2863                   (setcar hthread (list whole-subject (car hthread))))
2864                 ;; We add this new gathered thread to this gathered
2865                 ;; thread.
2866                 (setcdr (car hthread)
2867                         (nconc (cdar hthread) (list (car threads))))
2868                 ;; Remove it from the list of threads.
2869                 (setcdr prev (cdr threads))
2870                 (setq threads prev))
2871             ;; Enter this thread into the hash table.
2872             (gnus-sethash subject threads hashtb)))
2873         (setq prev threads)
2874         (setq threads (cdr threads)))
2875       result)))
2876
2877 (defun gnus-gather-threads-by-references (threads)
2878   "Gather threads by looking at References headers."
2879   (let ((idhashtb (gnus-make-hashtable 1024))
2880         (thhashtb (gnus-make-hashtable 1024))
2881         (prev threads)
2882         (result threads)
2883         ids references id gthread gid entered ref)
2884     (while threads
2885       (when (setq references (mail-header-references (caar threads)))
2886         (setq id (mail-header-id (caar threads))
2887               ids (gnus-split-references references)
2888               entered nil)
2889         (while (setq ref (pop ids))
2890           (setq ids (delete ref ids))
2891           (if (not (setq gid (gnus-gethash ref idhashtb)))
2892               (progn
2893                 (gnus-sethash ref id idhashtb)
2894                 (gnus-sethash id threads thhashtb))
2895             (setq gthread (gnus-gethash gid thhashtb))
2896             (unless entered
2897               ;; We enter a dummy root into the thread, if we
2898               ;; haven't done that already.
2899               (unless (stringp (caar gthread))
2900                 (setcar gthread (list (mail-header-subject (caar gthread))
2901                                       (car gthread))))
2902               ;; We add this new gathered thread to this gathered
2903               ;; thread.
2904               (setcdr (car gthread)
2905                       (nconc (cdar gthread) (list (car threads)))))
2906             ;; Add it into the thread hash table.
2907             (gnus-sethash id gthread thhashtb)
2908             (setq entered t)
2909             ;; Remove it from the list of threads.
2910             (setcdr prev (cdr threads))
2911             (setq threads prev))))
2912       (setq prev threads)
2913       (setq threads (cdr threads)))
2914     result))
2915
2916 (defun gnus-sort-gathered-threads (threads)
2917   "Sort subtreads inside each gathered thread by article number."
2918   (let ((result threads))
2919     (while threads
2920       (when (stringp (caar threads))
2921         (setcdr (car threads)
2922                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2923       (setq threads (cdr threads)))
2924     result))
2925
2926 (defun gnus-thread-loop-p (root thread)
2927   "Say whether ROOT is in THREAD."
2928   (let ((stack (list thread))
2929         (infloop 0)
2930         th)
2931     (while (setq thread (pop stack))
2932       (setq th (cdr thread))
2933       (while (and th
2934                   (not (eq (caar th) root)))
2935         (pop th))
2936       (if th
2937           ;; We have found a loop.
2938           (let (ref-dep)
2939             (setcdr thread (delq (car th) (cdr thread)))
2940             (if (boundp (setq ref-dep (intern "none"
2941                                               gnus-newsgroup-dependencies)))
2942                 (setcdr (symbol-value ref-dep)
2943                         (nconc (cdr (symbol-value ref-dep))
2944                                (list (car th))))
2945               (set ref-dep (list nil (car th))))
2946             (setq infloop 1
2947                   stack nil))
2948         ;; Push all the subthreads onto the stack.
2949         (push (cdr thread) stack)))
2950     infloop))
2951
2952 (defun gnus-make-threads ()
2953   "Go through the dependency hashtb and find the roots.  Return all threads."
2954   (let (threads)
2955     (while (catch 'infloop
2956              (mapatoms
2957               (lambda (refs)
2958                 ;; Deal with self-referencing References loops.
2959                 (when (and (car (symbol-value refs))
2960                            (not (zerop
2961                                  (apply
2962                                   '+
2963                                   (mapcar
2964                                    (lambda (thread)
2965                                      (gnus-thread-loop-p
2966                                       (car (symbol-value refs)) thread))
2967                                    (cdr (symbol-value refs)))))))
2968                   (setq threads nil)
2969                   (throw 'infloop t))
2970                 (unless (car (symbol-value refs))
2971                   ;; These threads do not refer back to any other articles,
2972                   ;; so they're roots.
2973                   (setq threads (append (cdr (symbol-value refs)) threads))))
2974               gnus-newsgroup-dependencies)))
2975     threads))
2976
2977 ;; Build the thread tree.
2978 (defsubst gnus-dependencies-add-header (header dependencies force-new)
2979   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2980
2981 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2982 if it was already present.
2983
2984 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
2985 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
2986 Message-IDs will be renamed be renamed to a unique Message-ID before
2987 being entered.
2988
2989 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
2990   (let* ((id (mail-header-id header))
2991          (id-dep (and id (intern id dependencies)))
2992          ref ref-dep ref-header)
2993     ;; Enter this `header' in the `dependencies' table.
2994     (cond
2995      ((not id-dep)
2996       (setq header nil))
2997      ;; The first two cases do the normal part: enter a new `header'
2998      ;; in the `dependencies' table.
2999      ((not (boundp id-dep))
3000       (set id-dep (list header)))
3001      ((null (car (symbol-value id-dep)))
3002       (setcar (symbol-value id-dep) header))
3003
3004      ;; From here the `header' was already present in the
3005      ;; `dependencies' table.
3006      (force-new
3007       ;; Overrides an existing entry;
3008       ;; just set the header part of the entry.
3009       (setcar (symbol-value id-dep) header))
3010
3011      ;; Renames the existing `header' to a unique Message-ID.
3012      ((not gnus-summary-ignore-duplicates)
3013       ;; An article with this Message-ID has already been seen.
3014       ;; We rename the Message-ID.
3015       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3016            (list header))
3017       (mail-header-set-id header id))
3018
3019      ;; The last case ignores an existing entry, except it adds any
3020      ;; additional Xrefs (in case the two articles came from different
3021      ;; servers.
3022      ;; Also sets `header' to `nil' meaning that the `dependencies'
3023      ;; table was *not* modified.
3024      (t
3025       (mail-header-set-xref
3026        (car (symbol-value id-dep))
3027        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3028                    "")
3029                (or (mail-header-xref header) "")))
3030       (setq header nil)))
3031
3032     (when header
3033       ;; First check if that we are not creating a References loop.
3034       (setq ref (gnus-parent-id (mail-header-references header)))
3035       (while (and ref
3036                   (setq ref-dep (intern-soft ref dependencies))
3037                   (boundp ref-dep)
3038                   (setq ref-header (car (symbol-value ref-dep))))
3039         (if (string= id ref)
3040             ;; Yuk!  This is a reference loop.  Make the article be a
3041             ;; root article.
3042             (progn
3043               (mail-header-set-references (car (symbol-value id-dep)) "none")
3044               (setq ref nil))
3045           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3046       (setq ref (gnus-parent-id (mail-header-references header)))
3047       (setq ref-dep (intern (or ref "none") dependencies))
3048       (if (boundp ref-dep)
3049           (setcdr (symbol-value ref-dep)
3050                   (nconc (cdr (symbol-value ref-dep))
3051                          (list (symbol-value id-dep))))
3052         (set ref-dep (list nil (symbol-value id-dep)))))
3053     header))
3054
3055 (defun gnus-build-sparse-threads ()
3056   (let ((headers gnus-newsgroup-headers)
3057         (gnus-summary-ignore-duplicates t)
3058         header references generation relations
3059         subject child end new-child date)
3060     ;; First we create an alist of generations/relations, where
3061     ;; generations is how much we trust the relation, and the relation
3062     ;; is parent/child.
3063     (gnus-message 7 "Making sparse threads...")
3064     (save-excursion
3065       (nnheader-set-temp-buffer " *gnus sparse threads*")
3066       (while (setq header (pop headers))
3067         (when (and (setq references (mail-header-references header))
3068                    (not (string= references "")))
3069           (insert references)
3070           (setq child (mail-header-id header)
3071                 subject (mail-header-subject header)
3072                 date (mail-header-date header)
3073                 generation 0)
3074           (while (search-backward ">" nil t)
3075             (setq end (1+ (point)))
3076             (when (search-backward "<" nil t)
3077               (setq new-child (buffer-substring (point) end))
3078               (push (list (incf generation)
3079                           child (setq child new-child)
3080                           subject date)
3081                     relations)))
3082           (when child
3083             (push (list (1+ generation) child nil subject) relations))
3084           (erase-buffer)))
3085       (kill-buffer (current-buffer)))
3086     ;; Sort over trustworthiness.
3087     (mapcar
3088      (lambda (relation)
3089        (when (gnus-dependencies-add-header
3090               (make-full-mail-header
3091                gnus-reffed-article-number
3092                (nth 3 relation) "" (or (nth 4 relation) "")
3093                (nth 1 relation)
3094                (or (nth 2 relation) "") 0 0 "")
3095               gnus-newsgroup-dependencies nil)
3096          (push gnus-reffed-article-number gnus-newsgroup-limit)
3097          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3098          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3099                gnus-newsgroup-reads)
3100          (decf gnus-reffed-article-number)))
3101      (sort relations 'car-less-than-car))
3102     (gnus-message 7 "Making sparse threads...done")))
3103
3104 (defun gnus-build-old-threads ()
3105   ;; Look at all the articles that refer back to old articles, and
3106   ;; fetch the headers for the articles that aren't there.  This will
3107   ;; build complete threads - if the roots haven't been expired by the
3108   ;; server, that is.
3109   (let (id heads)
3110     (mapatoms
3111      (lambda (refs)
3112        (when (not (car (symbol-value refs)))
3113          (setq heads (cdr (symbol-value refs)))
3114          (while heads
3115            (if (memq (mail-header-number (caar heads))
3116                      gnus-newsgroup-dormant)
3117                (setq heads (cdr heads))
3118              (setq id (symbol-name refs))
3119              (while (and (setq id (gnus-build-get-header id))
3120                          (not (car (gnus-id-to-thread id)))))
3121              (setq heads nil)))))
3122      gnus-newsgroup-dependencies)))
3123
3124 (defmacro gnus-nov-read-integer ()
3125   '(prog1
3126        (if (eq (char-after) ?\t)
3127            0
3128          (let ((num (ignore-errors (read buffer))))
3129            (if (numberp num) num 0)))
3130      (unless (eobp)
3131        (search-forward "\t" eol 'move))))
3132
3133 (defmacro gnus-nov-skip-field ()
3134   '(search-forward "\t" eol 'move))
3135
3136 (defmacro gnus-nov-field ()
3137   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3138
3139 (defmacro gnus-nov-parse-extra ()
3140   '(let (out string)
3141      (while (not (memq (char-after) '(?\n nil)))
3142        (setq string (gnus-nov-field))
3143        (when (string-match "^\\([^ :]+\\): " string)
3144          (push (cons (intern (match-string 1 string))
3145                      (substring string (match-end 0)))
3146                out)))
3147      out))
3148
3149 ;; This function has to be called with point after the article number
3150 ;; on the beginning of the line.
3151 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3152   (let ((eol (gnus-point-at-eol))
3153         (buffer (current-buffer))
3154         header)
3155
3156     ;; overview: [num subject from date id refs chars lines misc]
3157     (unwind-protect
3158         (progn
3159           (narrow-to-region (point) eol)
3160           (unless (eobp)
3161             (forward-char))
3162
3163           (setq header
3164                 (make-full-mail-header
3165                  number                 ; number
3166                  (gnus-nov-field)       ; subject
3167                  (gnus-nov-field)       ; from
3168                  (gnus-nov-field)       ; date
3169                  (or (gnus-nov-field)
3170                      (nnheader-generate-fake-message-id)) ; id
3171                  (gnus-nov-field)       ; refs
3172                  (gnus-nov-read-integer) ; chars
3173                  (gnus-nov-read-integer) ; lines
3174                  (unless (eq (char-after) ?\n)
3175                    (gnus-nov-field))    ; misc
3176                  (gnus-nov-parse-extra)))) ; extra
3177
3178       (widen))
3179
3180     (when gnus-alter-header-function
3181       (funcall gnus-alter-header-function header))
3182     (gnus-dependencies-add-header header dependencies force-new)))
3183
3184 (defun gnus-build-get-header (id)
3185   ;; Look through the buffer of NOV lines and find the header to
3186   ;; ID.  Enter this line into the dependencies hash table, and return
3187   ;; the id of the parent article (if any).
3188   (let ((deps gnus-newsgroup-dependencies)
3189         found header)
3190     (prog1
3191         (save-excursion
3192           (set-buffer nntp-server-buffer)
3193           (let ((case-fold-search nil))
3194             (goto-char (point-min))
3195             (while (and (not found)
3196                         (search-forward id nil t))
3197               (beginning-of-line)
3198               (setq found (looking-at
3199                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3200                                    (regexp-quote id))))
3201               (or found (beginning-of-line 2)))
3202             (when found
3203               (beginning-of-line)
3204               (and
3205                (setq header (gnus-nov-parse-line
3206                              (read (current-buffer)) deps))
3207                (gnus-parent-id (mail-header-references header))))))
3208       (when header
3209         (let ((number (mail-header-number header)))
3210           (push number gnus-newsgroup-limit)
3211           (push header gnus-newsgroup-headers)
3212           (if (memq number gnus-newsgroup-unselected)
3213               (progn
3214                 (push number gnus-newsgroup-unreads)
3215                 (setq gnus-newsgroup-unselected
3216                       (delq number gnus-newsgroup-unselected)))
3217             (push number gnus-newsgroup-ancient)))))))
3218
3219 (defun gnus-build-all-threads ()
3220   "Read all the headers."
3221   (let ((gnus-summary-ignore-duplicates t)
3222         (dependencies gnus-newsgroup-dependencies)
3223         header article)
3224     (save-excursion
3225       (set-buffer nntp-server-buffer)
3226       (let ((case-fold-search nil))
3227         (goto-char (point-min))
3228         (while (not (eobp))
3229           (ignore-errors
3230             (setq article (read (current-buffer))
3231                   header (gnus-nov-parse-line
3232                           article dependencies)))
3233           (when header
3234             (save-excursion
3235               (set-buffer gnus-summary-buffer)
3236               (push header gnus-newsgroup-headers)
3237               (if (memq (setq article (mail-header-number header))
3238                         gnus-newsgroup-unselected)
3239                   (progn
3240                     (push article gnus-newsgroup-unreads)
3241                     (setq gnus-newsgroup-unselected
3242                           (delq article gnus-newsgroup-unselected)))
3243                 (push article gnus-newsgroup-ancient)))
3244             (forward-line 1)))))))
3245
3246 (defun gnus-summary-update-article-line (article header)
3247   "Update the line for ARTICLE using HEADERS."
3248   (let* ((id (mail-header-id header))
3249          (thread (gnus-id-to-thread id)))
3250     (unless thread
3251       (error "Article in no thread"))
3252     ;; Update the thread.
3253     (setcar thread header)
3254     (gnus-summary-goto-subject article)
3255     (let* ((datal (gnus-data-find-list article))
3256            (data (car datal))
3257            (length (when (cdr datal)
3258                      (- (gnus-data-pos data)
3259                         (gnus-data-pos (cadr datal)))))
3260            (buffer-read-only nil)
3261            (level (gnus-summary-thread-level)))
3262       (gnus-delete-line)
3263       (gnus-summary-insert-line
3264        header level nil (gnus-article-mark article)
3265        (memq article gnus-newsgroup-replied)
3266        (memq article gnus-newsgroup-expirable)
3267        ;; Only insert the Subject string when it's different
3268        ;; from the previous Subject string.
3269        (if (gnus-subject-equal
3270             (condition-case ()
3271                 (mail-header-subject
3272                  (gnus-data-header
3273                   (cadr
3274                    (gnus-data-find-list
3275                     article
3276                     (gnus-data-list t)))))
3277               ;; Error on the side of excessive subjects.
3278               (error ""))
3279             (mail-header-subject header))
3280            ""
3281          (mail-header-subject header))
3282        nil (cdr (assq article gnus-newsgroup-scored))
3283        (memq article gnus-newsgroup-processable))
3284       (when length
3285         (gnus-data-update-list
3286          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3287
3288 (defun gnus-summary-update-article (article &optional iheader)
3289   "Update ARTICLE in the summary buffer."
3290   (set-buffer gnus-summary-buffer)
3291   (let* ((header (gnus-summary-article-header article))
3292          (id (mail-header-id header))
3293          (data (gnus-data-find article))
3294          (thread (gnus-id-to-thread id))
3295          (references (mail-header-references header))
3296          (parent
3297           (gnus-id-to-thread
3298            (or (gnus-parent-id
3299                 (when (and references
3300                            (not (equal "" references)))
3301                   references))
3302                "none")))
3303          (buffer-read-only nil)
3304          (old (car thread)))
3305     (when thread
3306       (unless iheader
3307         (setcar thread nil)
3308         (when parent
3309           (delq thread parent)))
3310       (if (gnus-summary-insert-subject id header)
3311           ;; Set the (possibly) new article number in the data structure.
3312           (gnus-data-set-number data (gnus-id-to-article id))
3313         (setcar thread old)
3314         nil))))
3315
3316 (defun gnus-rebuild-thread (id &optional line)
3317   "Rebuild the thread containing ID.
3318 If LINE, insert the rebuilt thread starting on line LINE."
3319   (let ((buffer-read-only nil)
3320         old-pos current thread data)
3321     (if (not gnus-show-threads)
3322         (setq thread (list (car (gnus-id-to-thread id))))
3323       ;; Get the thread this article is part of.
3324       (setq thread (gnus-remove-thread id)))
3325     (setq old-pos (gnus-point-at-bol))
3326     (setq current (save-excursion
3327                     (and (zerop (forward-line -1))
3328                          (gnus-summary-article-number))))
3329     ;; If this is a gathered thread, we have to go some re-gathering.
3330     (when (stringp (car thread))
3331       (let ((subject (car thread))
3332             roots thr)
3333         (setq thread (cdr thread))
3334         (while thread
3335           (unless (memq (setq thr (gnus-id-to-thread
3336                                    (gnus-root-id
3337                                     (mail-header-id (caar thread)))))
3338                         roots)
3339             (push thr roots))
3340           (setq thread (cdr thread)))
3341         ;; We now have all (unique) roots.
3342         (if (= (length roots) 1)
3343             ;; All the loose roots are now one solid root.
3344             (setq thread (car roots))
3345           (setq thread (cons subject (gnus-sort-threads roots))))))
3346     (let (threads)
3347       ;; We then insert this thread into the summary buffer.
3348       (when line
3349         (goto-char (point-min))
3350         (forward-line (1- line)))
3351       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3352         (if gnus-show-threads
3353             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3354           (gnus-summary-prepare-unthreaded thread))
3355         (setq data (nreverse gnus-newsgroup-data))
3356         (setq threads gnus-newsgroup-threads))
3357       ;; We splice the new data into the data structure.
3358       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3359       ;;!!! then we want to insert at the beginning of the buffer.
3360       ;;!!! That happens to be true with Gnus now, but that may
3361       ;;!!! change in the future.  Perhaps.
3362       (gnus-data-enter-list
3363        (if line nil current) data (- (point) old-pos))
3364       (setq gnus-newsgroup-threads
3365             (nconc threads gnus-newsgroup-threads))
3366       (gnus-data-compute-positions))))
3367
3368 (defun gnus-number-to-header (number)
3369   "Return the header for article NUMBER."
3370   (let ((headers gnus-newsgroup-headers))
3371     (while (and headers
3372                 (not (= number (mail-header-number (car headers)))))
3373       (pop headers))
3374     (when headers
3375       (car headers))))
3376
3377 (defun gnus-parent-headers (in-headers &optional generation)
3378   "Return the headers of the GENERATIONeth parent of HEADERS."
3379   (unless generation
3380     (setq generation 1))
3381   (let ((parent t)
3382         (headers in-headers)
3383         references)
3384     (while (and parent
3385                 (not (zerop generation))
3386                 (setq references (mail-header-references headers)))
3387       (setq headers (if (and references
3388                              (setq parent (gnus-parent-id references)))
3389                         (car (gnus-id-to-thread parent))
3390                       nil))
3391       (decf generation))
3392     (and (not (eq headers in-headers))
3393          headers)))
3394
3395 (defun gnus-id-to-thread (id)
3396   "Return the (sub-)thread where ID appears."
3397   (gnus-gethash id gnus-newsgroup-dependencies))
3398
3399 (defun gnus-id-to-article (id)
3400   "Return the article number of ID."
3401   (let ((thread (gnus-id-to-thread id)))
3402     (when (and thread
3403                (car thread))
3404       (mail-header-number (car thread)))))
3405
3406 (defun gnus-id-to-header (id)
3407   "Return the article headers of ID."
3408   (car (gnus-id-to-thread id)))
3409
3410 (defun gnus-article-displayed-root-p (article)
3411   "Say whether ARTICLE is a root(ish) article."
3412   (let ((level (gnus-summary-thread-level article))
3413         (refs (mail-header-references  (gnus-summary-article-header article)))
3414         particle)
3415     (cond
3416      ((null level) nil)
3417      ((zerop level) t)
3418      ((null refs) t)
3419      ((null (gnus-parent-id refs)) t)
3420      ((and (= 1 level)
3421            (null (setq particle (gnus-id-to-article
3422                                  (gnus-parent-id refs))))
3423            (null (gnus-summary-thread-level particle)))))))
3424
3425 (defun gnus-root-id (id)
3426   "Return the id of the root of the thread where ID appears."
3427   (let (last-id prev)
3428     (while (and id (setq prev (car (gnus-id-to-thread id))))
3429       (setq last-id id
3430             id (gnus-parent-id (mail-header-references prev))))
3431     last-id))
3432
3433 (defun gnus-articles-in-thread (thread)
3434   "Return the list of articles in THREAD."
3435   (cons (mail-header-number (car thread))
3436         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3437
3438 (defun gnus-remove-thread (id &optional dont-remove)
3439   "Remove the thread that has ID in it."
3440   (let (headers thread last-id)
3441     ;; First go up in this thread until we find the root.
3442     (setq last-id (gnus-root-id id)
3443           headers (message-flatten-list (gnus-id-to-thread last-id)))
3444     ;; We have now found the real root of this thread.  It might have
3445     ;; been gathered into some loose thread, so we have to search
3446     ;; through the threads to find the thread we wanted.
3447     (let ((threads gnus-newsgroup-threads)
3448           sub)
3449       (while threads
3450         (setq sub (car threads))
3451         (if (stringp (car sub))
3452             ;; This is a gathered thread, so we look at the roots
3453             ;; below it to find whether this article is in this
3454             ;; gathered root.
3455             (progn
3456               (setq sub (cdr sub))
3457               (while sub
3458                 (when (member (caar sub) headers)
3459                   (setq thread (car threads)
3460                         threads nil
3461                         sub nil))
3462                 (setq sub (cdr sub))))
3463           ;; It's an ordinary thread, so we check it.
3464           (when (eq (car sub) (car headers))
3465             (setq thread sub
3466                   threads nil)))
3467         (setq threads (cdr threads)))
3468       ;; If this article is in no thread, then it's a root.
3469       (if thread
3470           (unless dont-remove
3471             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3472         (setq thread (gnus-id-to-thread last-id)))
3473       (when thread
3474         (prog1
3475             thread                      ; We return this thread.
3476           (unless dont-remove
3477             (if (stringp (car thread))
3478                 (progn
3479                   ;; If we use dummy roots, then we have to remove the
3480                   ;; dummy root as well.
3481                   (when (eq gnus-summary-make-false-root 'dummy)
3482                     ;; We go to the dummy root by going to
3483                     ;; the first sub-"thread", and then one line up.
3484                     (gnus-summary-goto-article
3485                      (mail-header-number (caadr thread)))
3486                     (forward-line -1)
3487                     (gnus-delete-line)
3488                     (gnus-data-compute-positions))
3489                   (setq thread (cdr thread))
3490                   (while thread
3491                     (gnus-remove-thread-1 (car thread))
3492                     (setq thread (cdr thread))))
3493               (gnus-summary-show-all-threads)
3494               (gnus-remove-thread-1 thread))))))))
3495
3496 (defun gnus-remove-thread-1 (thread)
3497   "Remove the thread THREAD recursively."
3498   (let ((number (mail-header-number (pop thread)))
3499         d)
3500     (setq thread (reverse thread))
3501     (while thread
3502       (gnus-remove-thread-1 (pop thread)))
3503     (when (setq d (gnus-data-find number))
3504       (goto-char (gnus-data-pos d))
3505       (gnus-data-remove
3506        number
3507        (- (gnus-point-at-bol)
3508           (prog1
3509               (1+ (gnus-point-at-eol))
3510             (gnus-delete-line)))))))
3511
3512 (defun gnus-sort-threads (threads)
3513   "Sort THREADS."
3514   (if (not gnus-thread-sort-functions)
3515       threads
3516     (gnus-message 8 "Sorting threads...")
3517     (prog1
3518         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3519       (gnus-message 8 "Sorting threads...done"))))
3520
3521 (defun gnus-sort-articles (articles)
3522   "Sort ARTICLES."
3523   (when gnus-article-sort-functions
3524     (gnus-message 7 "Sorting articles...")
3525     (prog1
3526         (setq gnus-newsgroup-headers
3527               (sort articles (gnus-make-sort-function
3528                               gnus-article-sort-functions)))
3529       (gnus-message 7 "Sorting articles...done"))))
3530
3531 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3532 (defmacro gnus-thread-header (thread)
3533   ;; Return header of first article in THREAD.
3534   ;; Note that THREAD must never, ever be anything else than a variable -
3535   ;; using some other form will lead to serious barfage.
3536   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3537   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3538   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3539         (vector thread) 2))
3540
3541 (defsubst gnus-article-sort-by-number (h1 h2)
3542   "Sort articles by article number."
3543   (< (mail-header-number h1)
3544      (mail-header-number h2)))
3545
3546 (defun gnus-thread-sort-by-number (h1 h2)
3547   "Sort threads by root article number."
3548   (gnus-article-sort-by-number
3549    (gnus-thread-header h1) (gnus-thread-header h2)))
3550
3551 (defsubst gnus-article-sort-by-lines (h1 h2)
3552   "Sort articles by article Lines header."
3553   (< (mail-header-lines h1)
3554      (mail-header-lines h2)))
3555
3556 (defun gnus-thread-sort-by-lines (h1 h2)
3557   "Sort threads by root article Lines header."
3558   (gnus-article-sort-by-lines
3559    (gnus-thread-header h1) (gnus-thread-header h2)))
3560
3561 (defsubst gnus-article-sort-by-author (h1 h2)
3562   "Sort articles by root author."
3563   (string-lessp
3564    (let ((addr (mime-read-field 'From h1)))
3565      (or (std11-full-name-string addr)
3566          (std11-address-string addr)
3567          ""))
3568    (let ((addr (mime-read-field 'From h2)))
3569      (or (std11-full-name-string addr)
3570          (std11-address-string addr)
3571          ""))
3572    ))
3573
3574 (defun gnus-thread-sort-by-author (h1 h2)
3575   "Sort threads by root author."
3576   (gnus-article-sort-by-author
3577    (gnus-thread-header h1)  (gnus-thread-header h2)))
3578
3579 (defsubst gnus-article-sort-by-subject (h1 h2)
3580   "Sort articles by root subject."
3581   (string-lessp
3582    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3583    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3584
3585 (defun gnus-thread-sort-by-subject (h1 h2)
3586   "Sort threads by root subject."
3587   (gnus-article-sort-by-subject
3588    (gnus-thread-header h1) (gnus-thread-header h2)))
3589
3590 (defsubst gnus-article-sort-by-date (h1 h2)
3591   "Sort articles by root article date."
3592   (time-less-p
3593    (gnus-date-get-time (mail-header-date h1))
3594    (gnus-date-get-time (mail-header-date h2))))
3595
3596 (defun gnus-thread-sort-by-date (h1 h2)
3597   "Sort threads by root article date."
3598   (gnus-article-sort-by-date
3599    (gnus-thread-header h1) (gnus-thread-header h2)))
3600
3601 (defsubst gnus-article-sort-by-score (h1 h2)
3602   "Sort articles by root article score.
3603 Unscored articles will be counted as having a score of zero."
3604   (> (or (cdr (assq (mail-header-number h1)
3605                     gnus-newsgroup-scored))
3606          gnus-summary-default-score 0)
3607      (or (cdr (assq (mail-header-number h2)
3608                     gnus-newsgroup-scored))
3609          gnus-summary-default-score 0)))
3610
3611 (defun gnus-thread-sort-by-score (h1 h2)
3612   "Sort threads by root article score."
3613   (gnus-article-sort-by-score
3614    (gnus-thread-header h1) (gnus-thread-header h2)))
3615
3616 (defun gnus-thread-sort-by-total-score (h1 h2)
3617   "Sort threads by the sum of all scores in the thread.
3618 Unscored articles will be counted as having a score of zero."
3619   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3620
3621 (defun gnus-thread-total-score (thread)
3622   ;; This function find the total score of THREAD.
3623   (cond ((null thread)
3624          0)
3625         ((consp thread)
3626          (if (stringp (car thread))
3627              (apply gnus-thread-score-function 0
3628                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3629            (gnus-thread-total-score-1 thread)))
3630         (t
3631          (gnus-thread-total-score-1 (list thread)))))
3632
3633 (defun gnus-thread-total-score-1 (root)
3634   ;; This function find the total score of the thread below ROOT.
3635   (setq root (car root))
3636   (apply gnus-thread-score-function
3637          (or (append
3638               (mapcar 'gnus-thread-total-score
3639                       (cdr (gnus-id-to-thread (mail-header-id root))))
3640               (when (> (mail-header-number root) 0)
3641                 (list (or (cdr (assq (mail-header-number root)
3642                                      gnus-newsgroup-scored))
3643                           gnus-summary-default-score 0))))
3644              (list gnus-summary-default-score)
3645              '(0))))
3646
3647 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3648 (defvar gnus-tmp-prev-subject nil)
3649 (defvar gnus-tmp-false-parent nil)
3650 (defvar gnus-tmp-root-expunged nil)
3651 (defvar gnus-tmp-dummy-line nil)
3652
3653 (defvar gnus-tmp-header)
3654 (defun gnus-extra-header (type &optional header)
3655   "Return the extra header of TYPE."
3656   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3657       ""))
3658
3659 (defun gnus-summary-prepare-threads (threads)
3660   "Prepare summary buffer from THREADS and indentation LEVEL.
3661 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3662 or a straight list of headers."
3663   (gnus-message 7 "Generating summary...")
3664
3665   (setq gnus-newsgroup-threads threads)
3666   (beginning-of-line)
3667
3668   (let ((gnus-tmp-level 0)
3669         (default-score (or gnus-summary-default-score 0))
3670         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3671         thread number subject stack state gnus-tmp-gathered beg-match
3672         new-roots gnus-tmp-new-adopts thread-end
3673         gnus-tmp-header gnus-tmp-unread
3674         gnus-tmp-replied gnus-tmp-subject-or-nil
3675         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3676         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3677         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3678
3679     (setq gnus-tmp-prev-subject nil)
3680
3681     (if (vectorp (car threads))
3682         ;; If this is a straight (sic) list of headers, then a
3683         ;; threaded summary display isn't required, so we just create
3684         ;; an unthreaded one.
3685         (gnus-summary-prepare-unthreaded threads)
3686
3687       ;; Do the threaded display.
3688
3689       (while (or threads stack gnus-tmp-new-adopts new-roots)
3690
3691         (if (and (= gnus-tmp-level 0)
3692                  (or (not stack)
3693                      (= (caar stack) 0))
3694                  (not gnus-tmp-false-parent)
3695                  (or gnus-tmp-new-adopts new-roots))
3696             (if gnus-tmp-new-adopts
3697                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3698                       thread (list (car gnus-tmp-new-adopts))
3699                       gnus-tmp-header (caar thread)
3700                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3701               (when new-roots
3702                 (setq thread (list (car new-roots))
3703                       gnus-tmp-header (caar thread)
3704                       new-roots (cdr new-roots))))
3705
3706           (if threads
3707               ;; If there are some threads, we do them before the
3708               ;; threads on the stack.
3709               (setq thread threads
3710                     gnus-tmp-header (caar thread))
3711             ;; There were no current threads, so we pop something off
3712             ;; the stack.
3713             (setq state (car stack)
3714                   gnus-tmp-level (car state)
3715                   thread (cdr state)
3716                   stack (cdr stack)
3717                   gnus-tmp-header (caar thread))))
3718
3719         (setq gnus-tmp-false-parent nil)
3720         (setq gnus-tmp-root-expunged nil)
3721         (setq thread-end nil)
3722
3723         (if (stringp gnus-tmp-header)
3724             ;; The header is a dummy root.
3725             (cond
3726              ((eq gnus-summary-make-false-root 'adopt)
3727               ;; We let the first article adopt the rest.
3728               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3729                                                (cddar thread)))
3730               (setq gnus-tmp-gathered
3731                     (nconc (mapcar
3732                             (lambda (h) (mail-header-number (car h)))
3733                             (cddar thread))
3734                            gnus-tmp-gathered))
3735               (setq thread (cons (list (caar thread)
3736                                        (cadar thread))
3737                                  (cdr thread)))
3738               (setq gnus-tmp-level -1
3739                     gnus-tmp-false-parent t))
3740              ((eq gnus-summary-make-false-root 'empty)
3741               ;; We print adopted articles with empty subject fields.
3742               (setq gnus-tmp-gathered
3743                     (nconc (mapcar
3744                             (lambda (h) (mail-header-number (car h)))
3745                             (cddar thread))
3746                            gnus-tmp-gathered))
3747               (setq gnus-tmp-level -1))
3748              ((eq gnus-summary-make-false-root 'dummy)
3749               ;; We remember that we probably want to output a dummy
3750               ;; root.
3751               (setq gnus-tmp-dummy-line gnus-tmp-header)
3752               (setq gnus-tmp-prev-subject gnus-tmp-header))
3753              (t
3754               ;; We do not make a root for the gathered
3755               ;; sub-threads at all.
3756               (setq gnus-tmp-level -1)))
3757
3758           (setq number (mail-header-number gnus-tmp-header)
3759                 subject (mail-header-subject gnus-tmp-header))
3760
3761           (cond
3762            ;; If the thread has changed subject, we might want to make
3763            ;; this subthread into a root.
3764            ((and (null gnus-thread-ignore-subject)
3765                  (not (zerop gnus-tmp-level))
3766                  gnus-tmp-prev-subject
3767                  (not (inline
3768                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3769             (setq new-roots (nconc new-roots (list (car thread)))
3770                   thread-end t
3771                   gnus-tmp-header nil))
3772            ;; If the article lies outside the current limit,
3773            ;; then we do not display it.
3774            ((not (memq number gnus-newsgroup-limit))
3775             (setq gnus-tmp-gathered
3776                   (nconc (mapcar
3777                           (lambda (h) (mail-header-number (car h)))
3778                           (cdar thread))
3779                          gnus-tmp-gathered))
3780             (setq gnus-tmp-new-adopts (if (cdar thread)
3781                                           (append gnus-tmp-new-adopts
3782                                                   (cdar thread))
3783                                         gnus-tmp-new-adopts)
3784                   thread-end t
3785                   gnus-tmp-header nil)
3786             (when (zerop gnus-tmp-level)
3787               (setq gnus-tmp-root-expunged t)))
3788            ;; Perhaps this article is to be marked as read?
3789            ((and gnus-summary-mark-below
3790                  (< (or (cdr (assq number gnus-newsgroup-scored))
3791                         default-score)
3792                     gnus-summary-mark-below)
3793                  ;; Don't touch sparse articles.
3794                  (not (gnus-summary-article-sparse-p number))
3795                  (not (gnus-summary-article-ancient-p number)))
3796             (setq gnus-newsgroup-unreads
3797                   (delq number gnus-newsgroup-unreads))
3798             (if gnus-newsgroup-auto-expire
3799                 (push number gnus-newsgroup-expirable)
3800               (push (cons number gnus-low-score-mark)
3801                     gnus-newsgroup-reads))))
3802
3803           (when gnus-tmp-header
3804             ;; We may have an old dummy line to output before this
3805             ;; article.
3806             (when (and gnus-tmp-dummy-line
3807                        (gnus-subject-equal
3808                         gnus-tmp-dummy-line
3809                         (mail-header-subject gnus-tmp-header)))
3810               (gnus-summary-insert-dummy-line
3811                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3812               (setq gnus-tmp-dummy-line nil))
3813
3814             ;; Compute the mark.
3815             (setq gnus-tmp-unread (gnus-article-mark number))
3816
3817             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3818                                   gnus-tmp-header gnus-tmp-level)
3819                   gnus-newsgroup-data)
3820
3821             ;; Actually insert the line.
3822             (setq
3823              gnus-tmp-subject-or-nil
3824              (cond
3825               ((and gnus-thread-ignore-subject
3826                     gnus-tmp-prev-subject
3827                     (not (inline (gnus-subject-equal
3828                                   gnus-tmp-prev-subject subject))))
3829                subject)
3830               ((zerop gnus-tmp-level)
3831                (if (and (eq gnus-summary-make-false-root 'empty)
3832                         (memq number gnus-tmp-gathered)
3833                         gnus-tmp-prev-subject
3834                         (inline (gnus-subject-equal
3835                                  gnus-tmp-prev-subject subject)))
3836                    gnus-summary-same-subject
3837                  subject))
3838               (t gnus-summary-same-subject)))
3839             (if (and (eq gnus-summary-make-false-root 'adopt)
3840                      (= gnus-tmp-level 1)
3841                      (memq number gnus-tmp-gathered))
3842                 (setq gnus-tmp-opening-bracket ?\<
3843                       gnus-tmp-closing-bracket ?\>)
3844               (setq gnus-tmp-opening-bracket ?\[
3845                     gnus-tmp-closing-bracket ?\]))
3846             (setq
3847              gnus-tmp-indentation
3848              (aref gnus-thread-indent-array gnus-tmp-level)
3849              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3850              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3851                                 gnus-summary-default-score 0)
3852              gnus-tmp-score-char
3853              (if (or (null gnus-summary-default-score)
3854                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3855                          gnus-summary-zcore-fuzz))
3856                  ? 
3857                (if (< gnus-tmp-score gnus-summary-default-score)
3858                    gnus-score-below-mark gnus-score-over-mark))
3859              gnus-tmp-replied
3860              (cond ((memq number gnus-newsgroup-processable)
3861                     gnus-process-mark)
3862                    ((memq number gnus-newsgroup-cached)
3863                     gnus-cached-mark)
3864                    ((memq number gnus-newsgroup-replied)
3865                     gnus-replied-mark)
3866                    ((memq number gnus-newsgroup-saved)
3867                     gnus-saved-mark)
3868                    (t gnus-unread-mark))
3869              gnus-tmp-from (mail-header-from gnus-tmp-header)
3870              gnus-tmp-name
3871              (cond
3872               ((string-match "<[^>]+> *$" gnus-tmp-from)
3873                (setq beg-match (match-beginning 0))
3874                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3875                         (substring gnus-tmp-from (1+ (match-beginning 0))
3876                                    (1- (match-end 0))))
3877                    (substring gnus-tmp-from 0 beg-match)))
3878               ((string-match "(.+)" gnus-tmp-from)
3879                (substring gnus-tmp-from
3880                           (1+ (match-beginning 0)) (1- (match-end 0))))
3881               (t gnus-tmp-from)))
3882             (when (string= gnus-tmp-name "")
3883               (setq gnus-tmp-name gnus-tmp-from))
3884             (unless (numberp gnus-tmp-lines)
3885               (setq gnus-tmp-lines 0))
3886             (gnus-put-text-property
3887              (point)
3888              (progn (eval gnus-summary-line-format-spec) (point))
3889              'gnus-number number)
3890             (when gnus-visual-p
3891               (forward-line -1)
3892               (gnus-run-hooks 'gnus-summary-update-hook)
3893               (forward-line 1))
3894
3895             (setq gnus-tmp-prev-subject subject)))
3896
3897         (when (nth 1 thread)
3898           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3899         (incf gnus-tmp-level)
3900         (setq threads (if thread-end nil (cdar thread)))
3901         (unless threads
3902           (setq gnus-tmp-level 0)))))
3903   (gnus-message 7 "Generating summary...done"))
3904
3905 (defun gnus-summary-prepare-unthreaded (headers)
3906   "Generate an unthreaded summary buffer based on HEADERS."
3907   (let (header number mark)
3908
3909     (beginning-of-line)
3910
3911     (while headers
3912       ;; We may have to root out some bad articles...
3913       (when (memq (setq number (mail-header-number
3914                                 (setq header (pop headers))))
3915                   gnus-newsgroup-limit)
3916         ;; Mark article as read when it has a low score.
3917         (when (and gnus-summary-mark-below
3918                    (< (or (cdr (assq number gnus-newsgroup-scored))
3919                           gnus-summary-default-score 0)
3920                       gnus-summary-mark-below)
3921                    (not (gnus-summary-article-ancient-p number)))
3922           (setq gnus-newsgroup-unreads
3923                 (delq number gnus-newsgroup-unreads))
3924           (if gnus-newsgroup-auto-expire
3925               (push number gnus-newsgroup-expirable)
3926             (push (cons number gnus-low-score-mark)
3927                   gnus-newsgroup-reads)))
3928
3929         (setq mark (gnus-article-mark number))
3930         (push (gnus-data-make number mark (1+ (point)) header 0)
3931               gnus-newsgroup-data)
3932         (gnus-summary-insert-line
3933          header 0 number
3934          mark (memq number gnus-newsgroup-replied)
3935          (memq number gnus-newsgroup-expirable)
3936          (mail-header-subject header) nil
3937          (cdr (assq number gnus-newsgroup-scored))
3938          (memq number gnus-newsgroup-processable))))))
3939
3940 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3941   "Select newsgroup GROUP.
3942 If READ-ALL is non-nil, all articles in the group are selected.
3943 If SELECT-ARTICLES, only select those articles from GROUP."
3944   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3945          ;;!!! Dirty hack; should be removed.
3946          (gnus-summary-ignore-duplicates
3947           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3948               t
3949             gnus-summary-ignore-duplicates))
3950          (info (nth 2 entry))
3951          articles fetched-articles cached)
3952
3953     (unless (gnus-check-server
3954              (setq gnus-current-select-method
3955                    (gnus-find-method-for-group group)))
3956       (error "Couldn't open server"))
3957
3958     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3959         (gnus-activate-group group)     ; Or we can activate it...
3960         (progn                          ; Or we bug out.
3961           (when (equal major-mode 'gnus-summary-mode)
3962             (kill-buffer (current-buffer)))
3963           (error "Couldn't request group %s: %s"
3964                  group (gnus-status-message group))))
3965
3966     (unless (gnus-request-group group t)
3967       (when (equal major-mode 'gnus-summary-mode)
3968         (kill-buffer (current-buffer)))
3969       (error "Couldn't request group %s: %s"
3970              group (gnus-status-message group)))
3971
3972     (setq gnus-newsgroup-name group)
3973     (setq gnus-newsgroup-unselected nil)
3974     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3975     (gnus-summary-setup-default-charset)
3976
3977     ;; Adjust and set lists of article marks.
3978     (when info
3979       (gnus-adjust-marked-articles info))
3980
3981     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3982     (when (gnus-virtual-group-p group)
3983       (setq cached gnus-newsgroup-cached))
3984
3985     (setq gnus-newsgroup-unreads
3986           (gnus-set-difference
3987            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3988            gnus-newsgroup-dormant))
3989
3990     (setq gnus-newsgroup-processable nil)
3991
3992     (gnus-update-read-articles group gnus-newsgroup-unreads)
3993
3994     (if (setq articles select-articles)
3995         (setq gnus-newsgroup-unselected
3996               (gnus-sorted-intersection
3997                gnus-newsgroup-unreads
3998                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3999       (setq articles (gnus-articles-to-read group read-all)))
4000
4001     (cond
4002      ((null articles)
4003       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4004       'quit)
4005      ((eq articles 0) nil)
4006      (t
4007       ;; Init the dependencies hash table.
4008       (setq gnus-newsgroup-dependencies
4009             (gnus-make-hashtable (length articles)))
4010       (gnus-set-global-variables)
4011       ;; Retrieve the headers and read them in.
4012       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4013       (setq gnus-newsgroup-headers
4014             (if (eq 'nov
4015                     (setq gnus-headers-retrieved-by
4016                           (gnus-retrieve-headers
4017                            articles gnus-newsgroup-name
4018                            ;; We might want to fetch old headers, but
4019                            ;; not if there is only 1 article.
4020                            (and (or (and
4021                                      (not (eq gnus-fetch-old-headers 'some))
4022                                      (not (numberp gnus-fetch-old-headers)))
4023                                     (> (length articles) 1))
4024                                 gnus-fetch-old-headers))))
4025                 (gnus-get-newsgroup-headers-xover
4026                  articles nil nil gnus-newsgroup-name t)
4027               (gnus-get-newsgroup-headers)))
4028       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4029
4030       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4031       (when cached
4032         (setq gnus-newsgroup-cached cached))
4033
4034       ;; Suppress duplicates?
4035       (when gnus-suppress-duplicates
4036         (gnus-dup-suppress-articles))
4037
4038       ;; Set the initial limit.
4039       (setq gnus-newsgroup-limit (copy-sequence articles))
4040       ;; Remove canceled articles from the list of unread articles.
4041       (setq gnus-newsgroup-unreads
4042             (gnus-set-sorted-intersection
4043              gnus-newsgroup-unreads
4044              (setq fetched-articles
4045                    (mapcar (lambda (headers) (mail-header-number headers))
4046                            gnus-newsgroup-headers))))
4047       ;; Removed marked articles that do not exist.
4048       (gnus-update-missing-marks
4049        (gnus-sorted-complement fetched-articles articles))
4050       ;; We might want to build some more threads first.
4051       (when (and gnus-fetch-old-headers
4052                  (eq gnus-headers-retrieved-by 'nov))
4053         (if (eq gnus-fetch-old-headers 'invisible)
4054             (gnus-build-all-threads)
4055           (gnus-build-old-threads)))
4056       ;; Let the Gnus agent mark articles as read.
4057       (when gnus-agent
4058         (gnus-agent-get-undownloaded-list))
4059       ;; Check whether auto-expire is to be done in this group.
4060       (setq gnus-newsgroup-auto-expire
4061             (gnus-group-auto-expirable-p group))
4062       ;; Set up the article buffer now, if necessary.
4063       (unless gnus-single-article-buffer
4064         (gnus-article-setup-buffer))
4065       ;; First and last article in this newsgroup.
4066       (when gnus-newsgroup-headers
4067         (setq gnus-newsgroup-begin
4068               (mail-header-number (car gnus-newsgroup-headers))
4069               gnus-newsgroup-end
4070               (mail-header-number
4071                (gnus-last-element gnus-newsgroup-headers))))
4072       ;; GROUP is successfully selected.
4073       (or gnus-newsgroup-headers t)))))
4074
4075 (defun gnus-articles-to-read (group &optional read-all)
4076   ;; Find out what articles the user wants to read.
4077   (let* ((articles
4078           ;; Select all articles if `read-all' is non-nil, or if there
4079           ;; are no unread articles.
4080           (if (or read-all
4081                   (and (zerop (length gnus-newsgroup-marked))
4082                        (zerop (length gnus-newsgroup-unreads)))
4083                   (eq (gnus-group-find-parameter group 'display)
4084                       'all))
4085               (gnus-uncompress-range (gnus-active group))
4086             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4087                           (copy-sequence gnus-newsgroup-unreads))
4088                   '<)))
4089          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4090          (scored (length scored-list))
4091          (number (length articles))
4092          (marked (+ (length gnus-newsgroup-marked)
4093                     (length gnus-newsgroup-dormant)))
4094          (select
4095           (cond
4096            ((numberp read-all)
4097             read-all)
4098            (t
4099             (condition-case ()
4100                 (cond
4101                  ((and (or (<= scored marked) (= scored number))
4102                        (numberp gnus-large-newsgroup)
4103                        (> number gnus-large-newsgroup))
4104                   (let ((input
4105                          (read-string
4106                           (format
4107                            "How many articles from %s (default %d): "
4108                            (gnus-limit-string gnus-newsgroup-name 35)
4109                            number))))
4110                     (if (string-match "^[ \t]*$" input) number input)))
4111                  ((and (> scored marked) (< scored number)
4112                        (> (- scored number) 20))
4113                   (let ((input
4114                          (read-string
4115                           (format "%s %s (%d scored, %d total): "
4116                                   "How many articles from"
4117                                   group scored number))))
4118                     (if (string-match "^[ \t]*$" input)
4119                         number input)))
4120                  (t number))
4121               (quit nil))))))
4122     (setq select (if (stringp select) (string-to-number select) select))
4123     (if (or (null select) (zerop select))
4124         select
4125       (if (and (not (zerop scored)) (<= (abs select) scored))
4126           (progn
4127             (setq articles (sort scored-list '<))
4128             (setq number (length articles)))
4129         (setq articles (copy-sequence articles)))
4130
4131       (when (< (abs select) number)
4132         (if (< select 0)
4133             ;; Select the N oldest articles.
4134             (setcdr (nthcdr (1- (abs select)) articles) nil)
4135           ;; Select the N most recent articles.
4136           (setq articles (nthcdr (- number select) articles))))
4137       (setq gnus-newsgroup-unselected
4138             (gnus-sorted-intersection
4139              gnus-newsgroup-unreads
4140              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4141       articles)))
4142
4143 (defun gnus-killed-articles (killed articles)
4144   (let (out)
4145     (while articles
4146       (when (inline (gnus-member-of-range (car articles) killed))
4147         (push (car articles) out))
4148       (setq articles (cdr articles)))
4149     out))
4150
4151 (defun gnus-uncompress-marks (marks)
4152   "Uncompress the mark ranges in MARKS."
4153   (let ((uncompressed '(score bookmark))
4154         out)
4155     (while marks
4156       (if (memq (caar marks) uncompressed)
4157           (push (car marks) out)
4158         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4159       (setq marks (cdr marks)))
4160     out))
4161
4162 (defun gnus-adjust-marked-articles (info)
4163   "Set all article lists and remove all marks that are no longer legal."
4164   (let* ((marked-lists (gnus-info-marks info))
4165          (active (gnus-active (gnus-info-group info)))
4166          (min (car active))
4167          (max (cdr active))
4168          (types gnus-article-mark-lists)
4169          (uncompressed '(score bookmark killed))
4170          marks var articles article mark)
4171
4172     (while marked-lists
4173       (setq marks (pop marked-lists))
4174       (set (setq var (intern (format "gnus-newsgroup-%s"
4175                                      (car (rassq (setq mark (car marks))
4176                                                  types)))))
4177            (if (memq (car marks) uncompressed) (cdr marks)
4178              (gnus-uncompress-range (cdr marks))))
4179
4180       (setq articles (symbol-value var))
4181
4182       ;; All articles have to be subsets of the active articles.
4183       (cond
4184        ;; Adjust "simple" lists.
4185        ((memq mark '(tick dormant expire reply save))
4186         (while articles
4187           (when (or (< (setq article (pop articles)) min) (> article max))
4188             (set var (delq article (symbol-value var))))))
4189        ;; Adjust assocs.
4190        ((memq mark uncompressed)
4191         (when (not (listp (cdr (symbol-value var))))
4192           (set var (list (symbol-value var))))
4193         (when (not (listp (cdr articles)))
4194           (setq articles (list articles)))
4195         (while articles
4196           (when (or (not (consp (setq article (pop articles))))
4197                     (< (car article) min)
4198                     (> (car article) max))
4199             (set var (delq article (symbol-value var))))))))))
4200
4201 (defun gnus-update-missing-marks (missing)
4202   "Go through the list of MISSING articles and remove them from the mark lists."
4203   (when missing
4204     (let ((types gnus-article-mark-lists)
4205           var m)
4206       ;; Go through all types.
4207       (while types
4208         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4209         (when (symbol-value var)
4210           ;; This list has articles.  So we delete all missing articles
4211           ;; from it.
4212           (setq m missing)
4213           (while m
4214             (set var (delq (pop m) (symbol-value var)))))))))
4215
4216 (defun gnus-update-marks ()
4217   "Enter the various lists of marked articles into the newsgroup info list."
4218   (let ((types gnus-article-mark-lists)
4219         (info (gnus-get-info gnus-newsgroup-name))
4220         (uncompressed '(score bookmark killed))
4221         type list newmarked symbol delta-marks)
4222     (when info
4223       ;; Add all marks lists that are non-nil to the list of marks lists.
4224       (while (setq type (pop types))
4225         (when (setq list (symbol-value
4226                           (setq symbol
4227                                 (intern (format "gnus-newsgroup-%s"
4228                                                 (car type))))))
4229
4230           ;; Get rid of the entries of the articles that have the
4231           ;; default score.
4232           (when (and (eq (cdr type) 'score)
4233                      gnus-save-score
4234                      list)
4235             (let* ((arts list)
4236                    (prev (cons nil list))
4237                    (all prev))
4238               (while arts
4239                 (if (or (not (consp (car arts)))
4240                         (= (cdar arts) gnus-summary-default-score))
4241                     (setcdr prev (cdr arts))
4242                   (setq prev arts))
4243                 (setq arts (cdr arts)))
4244               (setq list (cdr all))))
4245
4246          (when (gnus-check-backend-function 'request-set-mark 
4247                                             gnus-newsgroup-name)
4248            ;; score & bookmark are not proper flags (they are cons cells)
4249            ;; cache is a internal gnus flag
4250            (unless (memq (cdr type) '(cache score bookmark))
4251              (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4252                     (del (gnus-remove-from-range old list))
4253                     (add (gnus-remove-from-range list old)))
4254                (if add
4255                    (push (list add 'add (list (cdr type))) delta-marks))
4256                (if del
4257                    (push (list del 'del (list (cdr type))) delta-marks)))))
4258          
4259           (push (cons (cdr type)
4260                       (if (memq (cdr type) uncompressed) list
4261                         (gnus-compress-sequence
4262                          (set symbol (sort list '<)) t)))
4263                 newmarked)))
4264
4265       (if delta-marks
4266          (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4267
4268       ;; Enter these new marks into the info of the group.
4269       (if (nthcdr 3 info)
4270           (setcar (nthcdr 3 info) newmarked)
4271         ;; Add the marks lists to the end of the info.
4272         (when newmarked
4273           (setcdr (nthcdr 2 info) (list newmarked))))
4274
4275       ;; Cut off the end of the info if there's nothing else there.
4276       (let ((i 5))
4277         (while (and (> i 2)
4278                     (not (nth i info)))
4279           (when (nthcdr (decf i) info)
4280             (setcdr (nthcdr i info) nil)))))))
4281
4282 (defun gnus-set-mode-line (where)
4283   "This function sets the mode line of the article or summary buffers.
4284 If WHERE is `summary', the summary mode line format will be used."
4285   ;; Is this mode line one we keep updated?
4286   (when (and (memq where gnus-updated-mode-lines)
4287              (symbol-value
4288               (intern (format "gnus-%s-mode-line-format-spec" where))))
4289     (let (mode-string)
4290       (save-excursion
4291         ;; We evaluate this in the summary buffer since these
4292         ;; variables are buffer-local to that buffer.
4293         (set-buffer gnus-summary-buffer)
4294         ;; We bind all these variables that are used in the `eval' form
4295         ;; below.
4296         (let* ((mformat (symbol-value
4297                          (intern
4298                           (format "gnus-%s-mode-line-format-spec" where))))
4299                (gnus-tmp-group-name gnus-newsgroup-name)
4300                (gnus-tmp-article-number (or gnus-current-article 0))
4301                (gnus-tmp-unread gnus-newsgroup-unreads)
4302                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4303                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4304                (gnus-tmp-unread-and-unselected
4305                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4306                             (zerop gnus-tmp-unselected))
4307                        "")
4308                       ((zerop gnus-tmp-unselected)
4309                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4310                       (t (format "{%d(+%d) more}"
4311                                  gnus-tmp-unread-and-unticked
4312                                  gnus-tmp-unselected))))
4313                (gnus-tmp-subject
4314                 (if (and gnus-current-headers
4315                          (vectorp gnus-current-headers))
4316                     (gnus-mode-string-quote
4317                      (mail-header-subject gnus-current-headers))
4318                   ""))
4319                bufname-length max-len
4320                gnus-tmp-header);; passed as argument to any user-format-funcs
4321           (setq mode-string (eval mformat))
4322           (setq bufname-length (if (string-match "%b" mode-string)
4323                                    (- (length
4324                                        (buffer-name
4325                                         (if (eq where 'summary)
4326                                             nil
4327                                           (get-buffer gnus-article-buffer))))
4328                                       2)
4329                                  0))
4330           (setq max-len (max 4 (if gnus-mode-non-string-length
4331                                    (- (window-width)
4332                                       gnus-mode-non-string-length
4333                                       bufname-length)
4334                                  (length mode-string))))
4335           ;; We might have to chop a bit of the string off...
4336           (when (> (length mode-string) max-len)
4337             (setq mode-string
4338                   (concat (gnus-truncate-string mode-string (- max-len 3))
4339                           "...")))
4340           ;; Pad the mode string a bit.
4341           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4342       ;; Update the mode line.
4343       (setq mode-line-buffer-identification
4344             (gnus-mode-line-buffer-identification (list mode-string)))
4345       (set-buffer-modified-p t))))
4346
4347 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4348   "Go through the HEADERS list and add all Xrefs to a hash table.
4349 The resulting hash table is returned, or nil if no Xrefs were found."
4350   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4351          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4352          (xref-hashtb (gnus-make-hashtable))
4353          start group entry number xrefs header)
4354     (while headers
4355       (setq header (pop headers))
4356       (when (and (setq xrefs (mail-header-xref header))
4357                  (not (memq (setq number (mail-header-number header))
4358                             unreads)))
4359         (setq start 0)
4360         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4361           (setq start (match-end 0))
4362           (setq group (if prefix
4363                           (concat prefix (substring xrefs (match-beginning 1)
4364                                                     (match-end 1)))
4365                         (substring xrefs (match-beginning 1) (match-end 1))))
4366           (setq number
4367                 (string-to-int (substring xrefs (match-beginning 2)
4368                                           (match-end 2))))
4369           (if (setq entry (gnus-gethash group xref-hashtb))
4370               (setcdr entry (cons number (cdr entry)))
4371             (gnus-sethash group (cons number nil) xref-hashtb)))))
4372     (and start xref-hashtb)))
4373
4374 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4375   "Look through all the headers and mark the Xrefs as read."
4376   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4377         name entry info xref-hashtb idlist method nth4)
4378     (save-excursion
4379       (set-buffer gnus-group-buffer)
4380       (when (setq xref-hashtb
4381                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4382         (mapatoms
4383          (lambda (group)
4384            (unless (string= from-newsgroup (setq name (symbol-name group)))
4385              (setq idlist (symbol-value group))
4386              ;; Dead groups are not updated.
4387              (and (prog1
4388                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4389                             info (nth 2 entry))
4390                     (when (stringp (setq nth4 (gnus-info-method info)))
4391                       (setq nth4 (gnus-server-to-method nth4))))
4392                   ;; Only do the xrefs if the group has the same
4393                   ;; select method as the group we have just read.
4394                   (or (gnus-methods-equal-p
4395                        nth4 (gnus-find-method-for-group from-newsgroup))
4396                       virtual
4397                       (equal nth4 (setq method (gnus-find-method-for-group
4398                                                 from-newsgroup)))
4399                       (and (equal (car nth4) (car method))
4400                            (equal (nth 1 nth4) (nth 1 method))))
4401                   gnus-use-cross-reference
4402                   (or (not (eq gnus-use-cross-reference t))
4403                       virtual
4404                       ;; Only do cross-references on subscribed
4405                       ;; groups, if that is what is wanted.
4406                       (<= (gnus-info-level info) gnus-level-subscribed))
4407                   (gnus-group-make-articles-read name idlist))))
4408          xref-hashtb)))))
4409
4410 (defun gnus-compute-read-articles (group articles)
4411   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4412          (info (nth 2 entry))
4413          (active (gnus-active group))
4414          ninfo)
4415     (when entry
4416       ;; First peel off all illegal article numbers.
4417       (when active
4418         (let ((ids articles)
4419               id first)
4420           (while (setq id (pop ids))
4421             (when (and first (> id (cdr active)))
4422               ;; We'll end up in this situation in one particular
4423               ;; obscure situation.  If you re-scan a group and get
4424               ;; a new article that is cross-posted to a different
4425               ;; group that has not been re-scanned, you might get
4426               ;; crossposted article that has a higher number than
4427               ;; Gnus believes possible.  So we re-activate this
4428               ;; group as well.  This might mean doing the
4429               ;; crossposting thingy will *increase* the number
4430               ;; of articles in some groups.  Tsk, tsk.
4431               (setq active (or (gnus-activate-group group) active)))
4432             (when (or (> id (cdr active))
4433                       (< id (car active)))
4434               (setq articles (delq id articles))))))
4435       ;; If the read list is nil, we init it.
4436       (if (and active
4437                (null (gnus-info-read info))
4438                (> (car active) 1))
4439           (setq ninfo (cons 1 (1- (car active))))
4440         (setq ninfo (gnus-info-read info)))
4441       ;; Then we add the read articles to the range.
4442       (gnus-add-to-range
4443        ninfo (setq articles (sort articles '<))))))
4444
4445 (defun gnus-group-make-articles-read (group articles)
4446   "Update the info of GROUP to say that ARTICLES are read."
4447   (let* ((num 0)
4448          (entry (gnus-gethash group gnus-newsrc-hashtb))
4449          (info (nth 2 entry))
4450          (active (gnus-active group))
4451          range)
4452     (when entry
4453       (setq range (gnus-compute-read-articles group articles))
4454       (save-excursion
4455         (set-buffer gnus-group-buffer)
4456         (gnus-undo-register
4457           `(progn
4458              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4459              (gnus-info-set-read ',info ',(gnus-info-read info))
4460              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4461              (gnus-group-update-group ,group t))))
4462       ;; Add the read articles to the range.
4463       (gnus-info-set-read info range)
4464       ;; Then we have to re-compute how many unread
4465       ;; articles there are in this group.
4466       (when active
4467         (cond
4468          ((not range)
4469           (setq num (- (1+ (cdr active)) (car active))))
4470          ((not (listp (cdr range)))
4471           (setq num (- (cdr active) (- (1+ (cdr range))
4472                                        (car range)))))
4473          (t
4474           (while range
4475             (if (numberp (car range))
4476                 (setq num (1+ num))
4477               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4478             (setq range (cdr range)))
4479           (setq num (- (cdr active) num))))
4480         ;; Update the number of unread articles.
4481         (setcar entry num)
4482         ;; Update the group buffer.
4483         (gnus-group-update-group group t)))))
4484
4485 (defun gnus-methods-equal-p (m1 m2)
4486   (let ((m1 (or m1 gnus-select-method))
4487         (m2 (or m2 gnus-select-method)))
4488     (or (equal m1 m2)
4489         (and (eq (car m1) (car m2))
4490              (or (not (memq 'address (assoc (symbol-name (car m1))
4491                                             gnus-valid-select-methods)))
4492                  (equal (nth 1 m1) (nth 1 m2)))))))
4493
4494 (defvar gnus-newsgroup-none-id 0)
4495
4496 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4497   (let ((cur nntp-server-buffer)
4498         (dependencies
4499          (or dependencies
4500              (save-excursion (set-buffer gnus-summary-buffer)
4501                              gnus-newsgroup-dependencies)))
4502         headers id end ref
4503         (mail-parse-charset gnus-newsgroup-charset))
4504     (save-excursion
4505       (set-buffer nntp-server-buffer)
4506       ;; Translate all TAB characters into SPACE characters.
4507       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4508       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4509       (gnus-run-hooks 'gnus-parse-headers-hook)
4510       (let ((case-fold-search t)
4511             in-reply-to header p lines chars ctype)
4512         (goto-char (point-min))
4513         ;; Search to the beginning of the next header.  Error messages
4514         ;; do not begin with 2 or 3.
4515         (while (re-search-forward "^[23][0-9]+ " nil t)
4516           (setq id nil
4517                 ref nil)
4518           ;; This implementation of this function, with nine
4519           ;; search-forwards instead of the one re-search-forward and
4520           ;; a case (which basically was the old function) is actually
4521           ;; about twice as fast, even though it looks messier.  You
4522           ;; can't have everything, I guess.  Speed and elegance
4523           ;; doesn't always go hand in hand.
4524           (setq
4525            header
4526            (make-full-mail-header
4527             ;; Number.
4528             (prog1
4529                 (read cur)
4530               (end-of-line)
4531               (setq p (point))
4532               (narrow-to-region (point)
4533                                 (or (and (search-forward "\n.\n" nil t)
4534                                          (- (point) 2))
4535                                     (point))))
4536             ;; Subject.
4537             (progn
4538               (goto-char p)
4539               (if (search-forward "\nsubject: " nil t)
4540                   (buffer-substring (match-end 0) (std11-field-end))
4541                 "(none)"))
4542             ;; From.
4543             (progn
4544               (goto-char p)
4545               (if (search-forward "\nfrom: " nil t)
4546                   (buffer-substring (match-end 0) (std11-field-end))
4547                 "(nobody)"))
4548             ;; Date.
4549             (progn
4550               (goto-char p)
4551               (if (search-forward "\ndate: " nil t)
4552                   (buffer-substring (match-end 0) (std11-field-end))
4553                 ""))
4554             ;; Message-ID.
4555             (progn
4556               (goto-char p)
4557               (setq id (if (re-search-forward
4558                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4559                            ;; We do it this way to make sure the Message-ID
4560                            ;; is (somewhat) syntactically valid.
4561                            (buffer-substring (match-beginning 1)
4562                                              (match-end 1))
4563                          ;; If there was no message-id, we just fake one
4564                          ;; to make subsequent routines simpler.
4565                          (nnheader-generate-fake-message-id))))
4566             ;; References.
4567             (progn
4568               (goto-char p)
4569               (if (search-forward "\nreferences: " nil t)
4570                   (progn
4571                     (setq end (point))
4572                     (prog1
4573                         (buffer-substring (match-end 0) (std11-field-end))
4574                       (setq ref
4575                             (buffer-substring
4576                              (progn
4577                                ;; (end-of-line)
4578                                (search-backward ">" end t)
4579                                (1+ (point)))
4580                              (progn
4581                                (search-backward "<" end t)
4582                                (point))))))
4583                 ;; Get the references from the in-reply-to header if there
4584                 ;; were no references and the in-reply-to header looks
4585                 ;; promising.
4586                 (if (and (search-forward "\nin-reply-to: " nil t)
4587                          (setq in-reply-to
4588                                (buffer-substring (match-end 0)
4589                                                  (std11-field-end)))
4590                          (string-match "<[^>]+>" in-reply-to))
4591                     (let (ref2)
4592                       (setq ref (substring in-reply-to (match-beginning 0)
4593                                            (match-end 0)))
4594                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4595                         (setq ref2 (substring in-reply-to (match-beginning 0)
4596                                               (match-end 0)))
4597                         (when (> (length ref2) (length ref))
4598                           (setq ref ref2)))
4599                       ref)
4600                   (setq ref nil))))
4601             ;; Chars.
4602             (progn
4603               (goto-char p)
4604               (if (search-forward "\nchars: " nil t)
4605                   (if (numberp (setq chars (ignore-errors (read cur))))
4606                       chars 0)
4607                 0))
4608             ;; Lines.
4609             (progn
4610               (goto-char p)
4611               (if (search-forward "\nlines: " nil t)
4612                   (if (numberp (setq lines (ignore-errors (read cur))))
4613                       lines 0)
4614                 0))
4615             ;; Xref.
4616             (progn
4617               (goto-char p)
4618               (and (search-forward "\nxref: " nil t)
4619                    (buffer-substring (match-end 0) (std11-field-end))))
4620             ;; Extra.
4621             (when gnus-extra-headers
4622               (let ((extra gnus-extra-headers)
4623                     out)
4624                 (while extra
4625                   (goto-char p)
4626                   (when (search-forward
4627                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4628                     (push (cons (car extra)
4629                                 (buffer-substring (match-end 0)
4630                                                   (std11-field-end)))
4631                           out))
4632                   (pop extra))
4633                 out))))
4634           (goto-char p)
4635           (if (and (search-forward "\ncontent-type: " nil t)
4636                    (setq ctype
4637                          (buffer-substring (match-end 0) (std11-field-end))))
4638               (mime-entity-set-content-type-internal
4639                header (mime-parse-Content-Type ctype)))
4640           (when (equal id ref)
4641             (setq ref nil))
4642
4643           (when gnus-alter-header-function
4644             (funcall gnus-alter-header-function header)
4645             (setq id (mail-header-id header)
4646                   ref (gnus-parent-id (mail-header-references header))))
4647
4648           (when (setq header
4649                       (gnus-dependencies-add-header
4650                        header dependencies force-new))
4651             (push header headers))
4652           (goto-char (point-max))
4653           (widen))
4654         (nreverse headers)))))
4655
4656 ;; Goes through the xover lines and returns a list of vectors
4657 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4658                                                   force-new dependencies
4659                                                   group also-fetch-heads)
4660   "Parse the news overview data in the server buffer, and return a
4661 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4662   ;; Get the Xref when the users reads the articles since most/some
4663   ;; NNTP servers do not include Xrefs when using XOVER.
4664   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4665   (let ((mail-parse-charset gnus-newsgroup-charset)
4666         (cur nntp-server-buffer)
4667         (dependencies (or dependencies gnus-newsgroup-dependencies))
4668         number headers header)
4669     (save-excursion
4670       (set-buffer nntp-server-buffer)
4671       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4672       ;; Allow the user to mangle the headers before parsing them.
4673       (gnus-run-hooks 'gnus-parse-headers-hook)
4674       (goto-char (point-min))
4675       (while (not (eobp))
4676         (condition-case ()
4677             (while (and sequence (not (eobp)))
4678               (setq number (read cur))
4679               (while (and sequence
4680                           (< (car sequence) number))
4681                 (setq sequence (cdr sequence)))
4682               (and sequence
4683                    (eq number (car sequence))
4684                    (progn
4685                      (setq sequence (cdr sequence))
4686                      (setq header (inline
4687                                     (gnus-nov-parse-line
4688                                      number dependencies force-new))))
4689                    (push header headers))
4690               (forward-line 1))
4691           (error
4692            (gnus-error 4 "Strange nov line (%d)"
4693                        (count-lines (point-min) (point)))))
4694         (forward-line 1))
4695       ;; A common bug in inn is that if you have posted an article and
4696       ;; then retrieves the active file, it will answer correctly --
4697       ;; the new article is included.  However, a NOV entry for the
4698       ;; article may not have been generated yet, so this may fail.
4699       ;; We work around this problem by retrieving the last few
4700       ;; headers using HEAD.
4701       (if (or (not also-fetch-heads)
4702               (not sequence))
4703           ;; We (probably) got all the headers.
4704           (nreverse headers)
4705         (let ((gnus-nov-is-evil t))
4706           (nconc
4707            (nreverse headers)
4708            (when (gnus-retrieve-headers sequence group)
4709              (gnus-get-newsgroup-headers))))))))
4710
4711 (defun gnus-article-get-xrefs ()
4712   "Fill in the Xref value in `gnus-current-headers', if necessary.
4713 This is meant to be called in `gnus-article-internal-prepare-hook'."
4714   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4715                                  gnus-current-headers)))
4716     (or (not gnus-use-cross-reference)
4717         (not headers)
4718         (and (mail-header-xref headers)
4719              (not (string= (mail-header-xref headers) "")))
4720         (let ((case-fold-search t)
4721               xref)
4722           (save-restriction
4723             (nnheader-narrow-to-headers)
4724             (goto-char (point-min))
4725             (when (or (and (eq (downcase (char-after)) ?x)
4726                            (looking-at "Xref:"))
4727                       (search-forward "\nXref:" nil t))
4728               (goto-char (1+ (match-end 0)))
4729               (setq xref (buffer-substring (point)
4730                                            (progn (end-of-line) (point))))
4731               (mail-header-set-xref headers xref)))))))
4732
4733 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4734   "Find article ID and insert the summary line for that article.
4735 OLD-HEADER can either be a header or a line number to insert
4736 the subject line on."
4737   (let* ((line (and (numberp old-header) old-header))
4738          (old-header (and (vectorp old-header) old-header))
4739          (header (cond ((and old-header use-old-header)
4740                         old-header)
4741                        ((and (numberp id)
4742                              (gnus-number-to-header id))
4743                         (gnus-number-to-header id))
4744                        (t
4745                         (gnus-read-header id))))
4746          (number (and (numberp id) id))
4747          d)
4748     (when header
4749       ;; Rebuild the thread that this article is part of and go to the
4750       ;; article we have fetched.
4751       (when (and (not gnus-show-threads)
4752                  old-header)
4753         (when (and number
4754                    (setq d (gnus-data-find (mail-header-number old-header))))
4755           (goto-char (gnus-data-pos d))
4756           (gnus-data-remove
4757            number
4758            (- (gnus-point-at-bol)
4759               (prog1
4760                   (1+ (gnus-point-at-eol))
4761                 (gnus-delete-line))))))
4762       (when old-header
4763         (mail-header-set-number header (mail-header-number old-header)))
4764       (setq gnus-newsgroup-sparse
4765             (delq (setq number (mail-header-number header))
4766                   gnus-newsgroup-sparse))
4767       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4768       (push number gnus-newsgroup-limit)
4769       (gnus-rebuild-thread (mail-header-id header) line)
4770       (gnus-summary-goto-subject number nil t))
4771     (when (and (numberp number)
4772                (> number 0))
4773       ;; We have to update the boundaries even if we can't fetch the
4774       ;; article if ID is a number -- so that the next `P' or `N'
4775       ;; command will fetch the previous (or next) article even
4776       ;; if the one we tried to fetch this time has been canceled.
4777       (when (> number gnus-newsgroup-end)
4778         (setq gnus-newsgroup-end number))
4779       (when (< number gnus-newsgroup-begin)
4780         (setq gnus-newsgroup-begin number))
4781       (setq gnus-newsgroup-unselected
4782             (delq number gnus-newsgroup-unselected)))
4783     ;; Report back a success?
4784     (and header (mail-header-number header))))
4785
4786 ;;; Process/prefix in the summary buffer
4787
4788 (defun gnus-summary-work-articles (n)
4789   "Return a list of articles to be worked upon.
4790 The prefix argument, the list of process marked articles, and the
4791 current article will be taken into consideration."
4792   (save-excursion
4793     (set-buffer gnus-summary-buffer)
4794     (cond
4795      (n
4796       ;; A numerical prefix has been given.
4797       (setq n (prefix-numeric-value n))
4798       (let ((backward (< n 0))
4799             (n (abs (prefix-numeric-value n)))
4800             articles article)
4801         (save-excursion
4802           (while
4803               (and (> n 0)
4804                    (push (setq article (gnus-summary-article-number))
4805                          articles)
4806                    (if backward
4807                        (gnus-summary-find-prev nil article)
4808                      (gnus-summary-find-next nil article)))
4809             (decf n)))
4810         (nreverse articles)))
4811      ((and (gnus-region-active-p) (mark))
4812       (message "region active")
4813       ;; Work on the region between point and mark.
4814       (let ((max (max (point) (mark)))
4815             articles article)
4816         (save-excursion
4817           (goto-char (min (point) (mark)))
4818           (while
4819               (and
4820                (push (setq article (gnus-summary-article-number)) articles)
4821                (gnus-summary-find-next nil article)
4822                (< (point) max)))
4823           (nreverse articles))))
4824      (gnus-newsgroup-processable
4825       ;; There are process-marked articles present.
4826       ;; Save current state.
4827       (gnus-summary-save-process-mark)
4828       ;; Return the list.
4829       (reverse gnus-newsgroup-processable))
4830      (t
4831       ;; Just return the current article.
4832       (list (gnus-summary-article-number))))))
4833
4834 (defmacro gnus-summary-iterate (arg &rest forms)
4835   "Iterate over the process/prefixed articles and do FORMS.
4836 ARG is the interactive prefix given to the command.  FORMS will be
4837 executed with point over the summary line of the articles."
4838   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4839     `(let ((,articles (gnus-summary-work-articles ,arg)))
4840        (while ,articles
4841          (gnus-summary-goto-subject (car ,articles))
4842          ,@forms))))
4843
4844 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4845 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4846
4847 (defun gnus-summary-save-process-mark ()
4848   "Push the current set of process marked articles on the stack."
4849   (interactive)
4850   (push (copy-sequence gnus-newsgroup-processable)
4851         gnus-newsgroup-process-stack))
4852
4853 (defun gnus-summary-kill-process-mark ()
4854   "Push the current set of process marked articles on the stack and unmark."
4855   (interactive)
4856   (gnus-summary-save-process-mark)
4857   (gnus-summary-unmark-all-processable))
4858
4859 (defun gnus-summary-yank-process-mark ()
4860   "Pop the last process mark state off the stack and restore it."
4861   (interactive)
4862   (unless gnus-newsgroup-process-stack
4863     (error "Empty mark stack"))
4864   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4865
4866 (defun gnus-summary-process-mark-set (set)
4867   "Make SET into the current process marked articles."
4868   (gnus-summary-unmark-all-processable)
4869   (while set
4870     (gnus-summary-set-process-mark (pop set))))
4871
4872 ;;; Searching and stuff
4873
4874 (defun gnus-summary-search-group (&optional backward use-level)
4875   "Search for next unread newsgroup.
4876 If optional argument BACKWARD is non-nil, search backward instead."
4877   (save-excursion
4878     (set-buffer gnus-group-buffer)
4879     (when (gnus-group-search-forward
4880            backward nil (if use-level (gnus-group-group-level) nil))
4881       (gnus-group-group-name))))
4882
4883 (defun gnus-summary-best-group (&optional exclude-group)
4884   "Find the name of the best unread group.
4885 If EXCLUDE-GROUP, do not go to this group."
4886   (save-excursion
4887     (set-buffer gnus-group-buffer)
4888     (save-excursion
4889       (gnus-group-best-unread-group exclude-group))))
4890
4891 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4892   (if backward (gnus-summary-find-prev)
4893     (let* ((dummy (gnus-summary-article-intangible-p))
4894            (article (or article (gnus-summary-article-number)))
4895            (arts (gnus-data-find-list article))
4896            result)
4897       (when (and (not dummy)
4898                  (or (not gnus-summary-check-current)
4899                      (not unread)
4900                      (not (gnus-data-unread-p (car arts)))))
4901         (setq arts (cdr arts)))
4902       (when (setq result
4903                   (if unread
4904                       (progn
4905                         (while arts
4906                           (when (or (and undownloaded
4907                                          (eq gnus-undownloaded-mark
4908                                              (gnus-data-mark (car arts))))
4909                                     (gnus-data-unread-p (car arts)))
4910                             (setq result (car arts)
4911                                   arts nil))
4912                           (setq arts (cdr arts)))
4913                         result)
4914                     (car arts)))
4915         (goto-char (gnus-data-pos result))
4916         (gnus-data-number result)))))
4917
4918 (defun gnus-summary-find-prev (&optional unread article)
4919   (let* ((eobp (eobp))
4920          (article (or article (gnus-summary-article-number)))
4921          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4922          result)
4923     (when (and (not eobp)
4924                (or (not gnus-summary-check-current)
4925                    (not unread)
4926                    (not (gnus-data-unread-p (car arts)))))
4927       (setq arts (cdr arts)))
4928     (when (setq result
4929                 (if unread
4930                     (progn
4931                       (while arts
4932                         (when (gnus-data-unread-p (car arts))
4933                           (setq result (car arts)
4934                                 arts nil))
4935                         (setq arts (cdr arts)))
4936                       result)
4937                   (car arts)))
4938       (goto-char (gnus-data-pos result))
4939       (gnus-data-number result))))
4940
4941 (defun gnus-summary-find-subject (subject &optional unread backward article)
4942   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4943          (article (or article (gnus-summary-article-number)))
4944          (articles (gnus-data-list backward))
4945          (arts (gnus-data-find-list article articles))
4946          result)
4947     (when (or (not gnus-summary-check-current)
4948               (not unread)
4949               (not (gnus-data-unread-p (car arts))))
4950       (setq arts (cdr arts)))
4951     (while arts
4952       (and (or (not unread)
4953                (gnus-data-unread-p (car arts)))
4954            (vectorp (gnus-data-header (car arts)))
4955            (gnus-subject-equal
4956             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4957            (setq result (car arts)
4958                  arts nil))
4959       (setq arts (cdr arts)))
4960     (and result
4961          (goto-char (gnus-data-pos result))
4962          (gnus-data-number result))))
4963
4964 (defun gnus-summary-search-forward (&optional unread subject backward)
4965   "Search forward for an article.
4966 If UNREAD, look for unread articles.  If SUBJECT, look for
4967 articles with that subject.  If BACKWARD, search backward instead."
4968   (cond (subject (gnus-summary-find-subject subject unread backward))
4969         (backward (gnus-summary-find-prev unread))
4970         (t (gnus-summary-find-next unread))))
4971
4972 (defun gnus-recenter (&optional n)
4973   "Center point in window and redisplay frame.
4974 Also do horizontal recentering."
4975   (interactive "P")
4976   (when (and gnus-auto-center-summary
4977              (not (eq gnus-auto-center-summary 'vertical)))
4978     (gnus-horizontal-recenter))
4979   (recenter n))
4980
4981 (defun gnus-summary-recenter ()
4982   "Center point in the summary window.
4983 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4984 displayed, no centering will be performed."
4985   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4986   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4987   (let* ((top (cond ((< (window-height) 4) 0)
4988                     ((< (window-height) 7) 1)
4989                     (t (if (numberp gnus-auto-center-summary)
4990                            gnus-auto-center-summary
4991                          2))))
4992          (height (1- (window-height)))
4993          (bottom (save-excursion (goto-char (point-max))
4994                                  (forward-line (- height))
4995                                  (point)))
4996          (window (get-buffer-window (current-buffer))))
4997     ;; The user has to want it.
4998     (when gnus-auto-center-summary
4999       (when (get-buffer-window gnus-article-buffer)
5000         ;; Only do recentering when the article buffer is displayed,
5001         ;; Set the window start to either `bottom', which is the biggest
5002         ;; possible valid number, or the second line from the top,
5003         ;; whichever is the least.
5004         (set-window-start
5005          window (min bottom (save-excursion
5006                               (forward-line (- top)) (point)))))
5007       ;; Do horizontal recentering while we're at it.
5008       (when (and (get-buffer-window (current-buffer) t)
5009                  (not (eq gnus-auto-center-summary 'vertical)))
5010         (let ((selected (selected-window)))
5011           (select-window (get-buffer-window (current-buffer) t))
5012           (gnus-summary-position-point)
5013           (gnus-horizontal-recenter)
5014           (select-window selected))))))
5015
5016 (defun gnus-summary-jump-to-group (newsgroup)
5017   "Move point to NEWSGROUP in group mode buffer."
5018   ;; Keep update point of group mode buffer if visible.
5019   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5020       (save-window-excursion
5021         ;; Take care of tree window mode.
5022         (when (get-buffer-window gnus-group-buffer)
5023           (pop-to-buffer gnus-group-buffer))
5024         (gnus-group-jump-to-group newsgroup))
5025     (save-excursion
5026       ;; Take care of tree window mode.
5027       (if (get-buffer-window gnus-group-buffer)
5028           (pop-to-buffer gnus-group-buffer)
5029         (set-buffer gnus-group-buffer))
5030       (gnus-group-jump-to-group newsgroup))))
5031
5032 ;; This function returns a list of article numbers based on the
5033 ;; difference between the ranges of read articles in this group and
5034 ;; the range of active articles.
5035 (defun gnus-list-of-unread-articles (group)
5036   (let* ((read (gnus-info-read (gnus-get-info group)))
5037          (active (or (gnus-active group) (gnus-activate-group group)))
5038          (last (cdr active))
5039          first nlast unread)
5040     ;; If none are read, then all are unread.
5041     (if (not read)
5042         (setq first (car active))
5043       ;; If the range of read articles is a single range, then the
5044       ;; first unread article is the article after the last read
5045       ;; article.  Sounds logical, doesn't it?
5046       (if (not (listp (cdr read)))
5047           (setq first (max (car active) (1+ (cdr read))))
5048         ;; `read' is a list of ranges.
5049         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5050                                   (caar read)))
5051                   1)
5052           (setq first (car active)))
5053         (while read
5054           (when first
5055             (while (< first nlast)
5056               (push first unread)
5057               (setq first (1+ first))))
5058           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5059           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5060           (setq read (cdr read)))))
5061     ;; And add the last unread articles.
5062     (while (<= first last)
5063       (push first unread)
5064       (setq first (1+ first)))
5065     ;; Return the list of unread articles.
5066     (delq 0 (nreverse unread))))
5067
5068 (defun gnus-list-of-read-articles (group)
5069   "Return a list of unread, unticked and non-dormant articles."
5070   (let* ((info (gnus-get-info group))
5071          (marked (gnus-info-marks info))
5072          (active (gnus-active group)))
5073     (and info active
5074          (gnus-set-difference
5075           (gnus-sorted-complement
5076            (gnus-uncompress-range active)
5077            (gnus-list-of-unread-articles group))
5078           (append
5079            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5080            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5081
5082 ;; Various summary commands
5083
5084 (defun gnus-summary-select-article-buffer ()
5085   "Reconfigure windows to show article buffer."
5086   (interactive)
5087   (if (not (gnus-buffer-live-p gnus-article-buffer))
5088       (error "There is no article buffer for this summary buffer")
5089     (gnus-configure-windows 'article)
5090     (select-window (get-buffer-window gnus-article-buffer))))
5091
5092 (defun gnus-summary-universal-argument (arg)
5093   "Perform any operation on all articles that are process/prefixed."
5094   (interactive "P")
5095   (let ((articles (gnus-summary-work-articles arg))
5096         func article)
5097     (if (eq
5098          (setq
5099           func
5100           (key-binding
5101            (read-key-sequence
5102             (substitute-command-keys
5103              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5104              ))))
5105          'undefined)
5106         (gnus-error 1 "Undefined key")
5107       (save-excursion
5108         (while articles
5109           (gnus-summary-goto-subject (setq article (pop articles)))
5110           (let (gnus-newsgroup-processable)
5111             (command-execute func))
5112           (gnus-summary-remove-process-mark article)))))
5113   (gnus-summary-position-point))
5114
5115 (defun gnus-summary-toggle-truncation (&optional arg)
5116   "Toggle truncation of summary lines.
5117 With arg, turn line truncation on iff arg is positive."
5118   (interactive "P")
5119   (setq truncate-lines
5120         (if (null arg) (not truncate-lines)
5121           (> (prefix-numeric-value arg) 0)))
5122   (redraw-display))
5123
5124 (defun gnus-summary-reselect-current-group (&optional all rescan)
5125   "Exit and then reselect the current newsgroup.
5126 The prefix argument ALL means to select all articles."
5127   (interactive "P")
5128   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5129     (error "Ephemeral groups can't be reselected"))
5130   (let ((current-subject (gnus-summary-article-number))
5131         (group gnus-newsgroup-name))
5132     (setq gnus-newsgroup-begin nil)
5133     (gnus-summary-exit)
5134     ;; We have to adjust the point of group mode buffer because
5135     ;; point was moved to the next unread newsgroup by exiting.
5136     (gnus-summary-jump-to-group group)
5137     (when rescan
5138       (save-excursion
5139         (gnus-group-get-new-news-this-group 1)))
5140     (gnus-group-read-group all t)
5141     (gnus-summary-goto-subject current-subject nil t)))
5142
5143 (defun gnus-summary-rescan-group (&optional all)
5144   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5145   (interactive "P")
5146   (gnus-summary-reselect-current-group all t))
5147
5148 (defun gnus-summary-update-info (&optional non-destructive)
5149   (save-excursion
5150     (let ((group gnus-newsgroup-name))
5151       (when group
5152         (when gnus-newsgroup-kill-headers
5153           (setq gnus-newsgroup-killed
5154                 (gnus-compress-sequence
5155                  (nconc
5156                   (gnus-set-sorted-intersection
5157                    (gnus-uncompress-range gnus-newsgroup-killed)
5158                    (setq gnus-newsgroup-unselected
5159                          (sort gnus-newsgroup-unselected '<)))
5160                   (setq gnus-newsgroup-unreads
5161                         (sort gnus-newsgroup-unreads '<)))
5162                  t)))
5163         (unless (listp (cdr gnus-newsgroup-killed))
5164           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5165         (let ((headers gnus-newsgroup-headers))
5166           ;; Set the new ranges of read articles.
5167           (save-excursion
5168             (set-buffer gnus-group-buffer)
5169             (gnus-undo-force-boundary))
5170           (gnus-update-read-articles
5171            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5172           ;; Set the current article marks.
5173           (let ((gnus-newsgroup-scored
5174                  (if (and (not gnus-save-score)
5175                           (not non-destructive))
5176                      nil
5177                    gnus-newsgroup-scored)))
5178             (save-excursion
5179               (gnus-update-marks)))
5180           ;; Do the cross-ref thing.
5181           (when gnus-use-cross-reference
5182             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5183           ;; Do not switch windows but change the buffer to work.
5184           (set-buffer gnus-group-buffer)
5185           (unless (gnus-ephemeral-group-p group)
5186             (gnus-group-update-group group)))))))
5187
5188 (defun gnus-summary-save-newsrc (&optional force)
5189   "Save the current number of read/marked articles in the dribble buffer.
5190 The dribble buffer will then be saved.
5191 If FORCE (the prefix), also save the .newsrc file(s)."
5192   (interactive "P")
5193   (gnus-summary-update-info t)
5194   (if force
5195       (gnus-save-newsrc-file)
5196     (gnus-dribble-save)))
5197
5198 (defun gnus-summary-exit (&optional temporary)
5199   "Exit reading current newsgroup, and then return to group selection mode.
5200 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5201   (interactive)
5202   (gnus-set-global-variables)
5203   (gnus-kill-save-kill-buffer)
5204   (gnus-async-halt-prefetch)
5205   (let* ((group gnus-newsgroup-name)
5206          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5207          (mode major-mode)
5208          (group-point nil)
5209          (buf (current-buffer)))
5210     (unless quit-config
5211       ;; Do adaptive scoring, and possibly save score files.
5212       (when gnus-newsgroup-adaptive
5213         (gnus-score-adaptive))
5214       (when gnus-use-scoring
5215         (gnus-score-save)))
5216     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5217     ;; If we have several article buffers, we kill them at exit.
5218     (unless gnus-single-article-buffer
5219       (gnus-kill-buffer gnus-original-article-buffer)
5220       (setq gnus-article-current nil))
5221     (when gnus-use-cache
5222       (gnus-cache-possibly-remove-articles)
5223       (gnus-cache-save-buffers))
5224     (gnus-async-prefetch-remove-group group)
5225     (when gnus-suppress-duplicates
5226       (gnus-dup-enter-articles))
5227     (when gnus-use-trees
5228       (gnus-tree-close group))
5229     ;; Remove entries for this group.
5230     (nnmail-purge-split-history (gnus-group-real-name group))
5231     ;; Make all changes in this group permanent.
5232     (unless quit-config
5233       (gnus-run-hooks 'gnus-exit-group-hook)
5234       (gnus-summary-update-info))
5235     (gnus-close-group group)
5236     ;; Make sure where we were, and go to next newsgroup.
5237     (set-buffer gnus-group-buffer)
5238     (unless quit-config
5239       (gnus-group-jump-to-group group))
5240     (gnus-run-hooks 'gnus-summary-exit-hook)
5241     (unless (or quit-config
5242                 ;; If this group has disappeared from the summary
5243                 ;; buffer, don't skip forwards.
5244                 (not (string= group (gnus-group-group-name))))
5245       (gnus-group-next-unread-group 1))
5246     (setq group-point (point))
5247     (if temporary
5248         nil                             ;Nothing to do.
5249       ;; If we have several article buffers, we kill them at exit.
5250       (unless gnus-single-article-buffer
5251         (gnus-kill-buffer gnus-article-buffer)
5252         (gnus-kill-buffer gnus-original-article-buffer)
5253         (setq gnus-article-current nil))
5254       (set-buffer buf)
5255       (if (not gnus-kill-summary-on-exit)
5256           (gnus-deaden-summary)
5257         ;; We set all buffer-local variables to nil.  It is unclear why
5258         ;; this is needed, but if we don't, buffer-local variables are
5259         ;; not garbage-collected, it seems.  This would the lead to en
5260         ;; ever-growing Emacs.
5261         (gnus-summary-clear-local-variables)
5262         (when (get-buffer gnus-article-buffer)
5263           (bury-buffer gnus-article-buffer))
5264         ;; We clear the global counterparts of the buffer-local
5265         ;; variables as well, just to be on the safe side.
5266         (set-buffer gnus-group-buffer)
5267         (gnus-summary-clear-local-variables)
5268         ;; Return to group mode buffer.
5269         (when (eq mode 'gnus-summary-mode)
5270           (gnus-kill-buffer buf)))
5271       (setq gnus-current-select-method gnus-select-method)
5272       (pop-to-buffer gnus-group-buffer)
5273       (if (not quit-config)
5274           (progn
5275             (goto-char group-point)
5276             (gnus-configure-windows 'group 'force))
5277         (gnus-handle-ephemeral-exit quit-config))
5278       ;; Clear the current group name.
5279       (unless quit-config
5280         (setq gnus-newsgroup-name nil)))))
5281
5282 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5283 (defun gnus-summary-exit-no-update (&optional no-questions)
5284   "Quit reading current newsgroup without updating read article info."
5285   (interactive)
5286   (let* ((group gnus-newsgroup-name)
5287          (quit-config (gnus-group-quit-config group)))
5288     (when (or no-questions
5289               gnus-expert-user
5290               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5291       (gnus-async-halt-prefetch)
5292       (mapcar 'funcall
5293               (delq 'gnus-summary-expire-articles 
5294                     (copy-list gnus-summary-prepare-exit-hook)))
5295       ;; If we have several article buffers, we kill them at exit.
5296       (unless gnus-single-article-buffer
5297         (gnus-kill-buffer gnus-article-buffer)
5298         (gnus-kill-buffer gnus-original-article-buffer)
5299         (setq gnus-article-current nil))
5300       (if (not gnus-kill-summary-on-exit)
5301           (gnus-deaden-summary)
5302         (gnus-close-group group)
5303         (gnus-summary-clear-local-variables)
5304         (set-buffer gnus-group-buffer)
5305         (gnus-summary-clear-local-variables)
5306         (when (get-buffer gnus-summary-buffer)
5307           (kill-buffer gnus-summary-buffer)))
5308       (unless gnus-single-article-buffer
5309         (setq gnus-article-current nil))
5310       (when gnus-use-trees
5311         (gnus-tree-close group))
5312       (gnus-async-prefetch-remove-group group)
5313       (when (get-buffer gnus-article-buffer)
5314         (bury-buffer gnus-article-buffer))
5315       ;; Return to the group buffer.
5316       (gnus-configure-windows 'group 'force)
5317       ;; Clear the current group name.
5318       (setq gnus-newsgroup-name nil)
5319       (when (equal (gnus-group-group-name) group)
5320         (gnus-group-next-unread-group 1))
5321       (when quit-config
5322         (gnus-handle-ephemeral-exit quit-config)))))
5323
5324 (defun gnus-handle-ephemeral-exit (quit-config)
5325   "Handle movement when leaving an ephemeral group.
5326 The state which existed when entering the ephemeral is reset."
5327   (if (not (buffer-name (car quit-config)))
5328       (gnus-configure-windows 'group 'force)
5329     (set-buffer (car quit-config))
5330     (cond ((eq major-mode 'gnus-summary-mode)
5331            (gnus-set-global-variables))
5332           ((eq major-mode 'gnus-article-mode)
5333            (save-excursion
5334              ;; The `gnus-summary-buffer' variable may point
5335              ;; to the old summary buffer when using a single
5336              ;; article buffer.
5337              (unless (gnus-buffer-live-p gnus-summary-buffer)
5338                (set-buffer gnus-group-buffer))
5339              (set-buffer gnus-summary-buffer)
5340              (gnus-set-global-variables))))
5341     (if (or (eq (cdr quit-config) 'article)
5342             (eq (cdr quit-config) 'pick))
5343         (progn
5344           ;; The current article may be from the ephemeral group
5345           ;; thus it is best that we reload this article
5346           (gnus-summary-show-article)
5347           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5348               (gnus-configure-windows 'pick 'force)
5349             (gnus-configure-windows (cdr quit-config) 'force)))
5350       (gnus-configure-windows (cdr quit-config) 'force))
5351     (when (eq major-mode 'gnus-summary-mode)
5352       (gnus-summary-next-subject 1 nil t)
5353       (gnus-summary-recenter)
5354       (gnus-summary-position-point))))
5355
5356 (defun gnus-summary-preview-mime-message ()
5357   "MIME decode and play this message."
5358   (interactive)
5359   (let ((gnus-break-pages nil)
5360         (gnus-show-mime t))
5361     (gnus-summary-select-article gnus-show-all-headers t))
5362   (select-window (get-buffer-window gnus-article-buffer)))
5363
5364 ;;; Dead summaries.
5365
5366 (defvar gnus-dead-summary-mode-map nil)
5367
5368 (unless gnus-dead-summary-mode-map
5369   (setq gnus-dead-summary-mode-map (make-keymap))
5370   (suppress-keymap gnus-dead-summary-mode-map)
5371   (substitute-key-definition
5372    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5373   (let ((keys '("\C-d" "\r" "\177" [delete])))
5374     (while keys
5375       (define-key gnus-dead-summary-mode-map
5376         (pop keys) 'gnus-summary-wake-up-the-dead))))
5377
5378 (defvar gnus-dead-summary-mode nil
5379   "Minor mode for Gnus summary buffers.")
5380
5381 (defun gnus-dead-summary-mode (&optional arg)
5382   "Minor mode for Gnus summary buffers."
5383   (interactive "P")
5384   (when (eq major-mode 'gnus-summary-mode)
5385     (make-local-variable 'gnus-dead-summary-mode)
5386     (setq gnus-dead-summary-mode
5387           (if (null arg) (not gnus-dead-summary-mode)
5388             (> (prefix-numeric-value arg) 0)))
5389     (when gnus-dead-summary-mode
5390       (gnus-add-minor-mode
5391        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5392
5393 (defun gnus-deaden-summary ()
5394   "Make the current summary buffer into a dead summary buffer."
5395   ;; Kill any previous dead summary buffer.
5396   (when (and gnus-dead-summary
5397              (buffer-name gnus-dead-summary))
5398     (save-excursion
5399       (set-buffer gnus-dead-summary)
5400       (when gnus-dead-summary-mode
5401         (kill-buffer (current-buffer)))))
5402   ;; Make this the current dead summary.
5403   (setq gnus-dead-summary (current-buffer))
5404   (gnus-dead-summary-mode 1)
5405   (let ((name (buffer-name)))
5406     (when (string-match "Summary" name)
5407       (rename-buffer
5408        (concat (substring name 0 (match-beginning 0)) "Dead "
5409                (substring name (match-beginning 0)))
5410        t))))
5411
5412 (defun gnus-kill-or-deaden-summary (buffer)
5413   "Kill or deaden the summary BUFFER."
5414   (save-excursion
5415     (when (and (buffer-name buffer)
5416                (not gnus-single-article-buffer))
5417       (save-excursion
5418         (set-buffer buffer)
5419         (gnus-kill-buffer gnus-article-buffer)
5420         (gnus-kill-buffer gnus-original-article-buffer)))
5421     (cond (gnus-kill-summary-on-exit
5422            (when (and gnus-use-trees
5423                       (gnus-buffer-exists-p buffer))
5424              (save-excursion
5425                (set-buffer buffer)
5426                (gnus-tree-close gnus-newsgroup-name)))
5427            (gnus-kill-buffer buffer))
5428           ((gnus-buffer-exists-p buffer)
5429            (save-excursion
5430              (set-buffer buffer)
5431              (gnus-deaden-summary))))))
5432
5433 (defun gnus-summary-wake-up-the-dead (&rest args)
5434   "Wake up the dead summary buffer."
5435   (interactive)
5436   (gnus-dead-summary-mode -1)
5437   (let ((name (buffer-name)))
5438     (when (string-match "Dead " name)
5439       (rename-buffer
5440        (concat (substring name 0 (match-beginning 0))
5441                (substring name (match-end 0)))
5442        t)))
5443   (gnus-message 3 "This dead summary is now alive again"))
5444
5445 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5446 (defun gnus-summary-fetch-faq (&optional faq-dir)
5447   "Fetch the FAQ for the current group.
5448 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5449 in."
5450   (interactive
5451    (list
5452     (when current-prefix-arg
5453       (completing-read
5454        "Faq dir: " (and (listp gnus-group-faq-directory)
5455                         (mapcar (lambda (file) (list file))
5456                                 gnus-group-faq-directory))))))
5457   (let (gnus-faq-buffer)
5458     (when (setq gnus-faq-buffer
5459                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5460       (gnus-configure-windows 'summary-faq))))
5461
5462 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5463 (defun gnus-summary-describe-group (&optional force)
5464   "Describe the current newsgroup."
5465   (interactive "P")
5466   (gnus-group-describe-group force gnus-newsgroup-name))
5467
5468 (defun gnus-summary-describe-briefly ()
5469   "Describe summary mode commands briefly."
5470   (interactive)
5471   (gnus-message 6 (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")))
5472
5473 ;; Walking around group mode buffer from summary mode.
5474
5475 (defun gnus-summary-next-group (&optional no-article target-group backward)
5476   "Exit current newsgroup and then select next unread newsgroup.
5477 If prefix argument NO-ARTICLE is non-nil, no article is selected
5478 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5479 previous group instead."
5480   (interactive "P")
5481   ;; Stop pre-fetching.
5482   (gnus-async-halt-prefetch)
5483   (let ((current-group gnus-newsgroup-name)
5484         (current-buffer (current-buffer))
5485         entered)
5486     ;; First we semi-exit this group to update Xrefs and all variables.
5487     ;; We can't do a real exit, because the window conf must remain
5488     ;; the same in case the user is prompted for info, and we don't
5489     ;; want the window conf to change before that...
5490     (gnus-summary-exit t)
5491     (while (not entered)
5492       ;; Then we find what group we are supposed to enter.
5493       (set-buffer gnus-group-buffer)
5494       (gnus-group-jump-to-group current-group)
5495       (setq target-group
5496             (or target-group
5497                 (if (eq gnus-keep-same-level 'best)
5498                     (gnus-summary-best-group gnus-newsgroup-name)
5499                   (gnus-summary-search-group backward gnus-keep-same-level))))
5500       (if (not target-group)
5501           ;; There are no further groups, so we return to the group
5502           ;; buffer.
5503           (progn
5504             (gnus-message 5 "Returning to the group buffer")
5505             (setq entered t)
5506             (when (gnus-buffer-live-p current-buffer)
5507               (set-buffer current-buffer)
5508               (gnus-summary-exit))
5509             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5510         ;; We try to enter the target group.
5511         (gnus-group-jump-to-group target-group)
5512         (let ((unreads (gnus-group-group-unread)))
5513           (if (and (or (eq t unreads)
5514                        (and unreads (not (zerop unreads))))
5515                    (gnus-summary-read-group
5516                     target-group nil no-article
5517                     (and (buffer-name current-buffer) current-buffer)
5518                     nil backward))
5519               (setq entered t)
5520             (setq current-group target-group
5521                   target-group nil)))))))
5522
5523 (defun gnus-summary-prev-group (&optional no-article)
5524   "Exit current newsgroup and then select previous unread newsgroup.
5525 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5526   (interactive "P")
5527   (gnus-summary-next-group no-article nil t))
5528
5529 ;; Walking around summary lines.
5530
5531 (defun gnus-summary-first-subject (&optional unread undownloaded)
5532   "Go to the first unread subject.
5533 If UNREAD is non-nil, go to the first unread article.
5534 Returns the article selected or nil if there are no unread articles."
5535   (interactive "P")
5536   (prog1
5537       (cond
5538        ;; Empty summary.
5539        ((null gnus-newsgroup-data)
5540         (gnus-message 3 "No articles in the group")
5541         nil)
5542        ;; Pick the first article.
5543        ((not unread)
5544         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5545         (gnus-data-number (car gnus-newsgroup-data)))
5546        ;; No unread articles.
5547        ((null gnus-newsgroup-unreads)
5548         (gnus-message 3 "No more unread articles")
5549         nil)
5550        ;; Find the first unread article.
5551        (t
5552         (let ((data gnus-newsgroup-data))
5553           (while (and data
5554                       (and (not (and undownloaded
5555                                      (eq gnus-undownloaded-mark
5556                                          (gnus-data-mark (car data)))))
5557                            (not (gnus-data-unread-p (car data)))))
5558             (setq data (cdr data)))
5559           (when data
5560             (goto-char (gnus-data-pos (car data)))
5561             (gnus-data-number (car data))))))
5562     (gnus-summary-position-point)))
5563
5564 (defun gnus-summary-next-subject (n &optional unread dont-display)
5565   "Go to next N'th summary line.
5566 If N is negative, go to the previous N'th subject line.
5567 If UNREAD is non-nil, only unread articles are selected.
5568 The difference between N and the actual number of steps taken is
5569 returned."
5570   (interactive "p")
5571   (let ((backward (< n 0))
5572         (n (abs n)))
5573     (while (and (> n 0)
5574                 (if backward
5575                     (gnus-summary-find-prev unread)
5576                   (gnus-summary-find-next unread)))
5577       (gnus-summary-show-thread)
5578       (setq n (1- n)))
5579     (when (/= 0 n)
5580       (gnus-message 7 "No more%s articles"
5581                     (if unread " unread" "")))
5582     (unless dont-display
5583       (gnus-summary-recenter)
5584       (gnus-summary-position-point))
5585     n))
5586
5587 (defun gnus-summary-next-unread-subject (n)
5588   "Go to next N'th unread summary line."
5589   (interactive "p")
5590   (gnus-summary-next-subject n t))
5591
5592 (defun gnus-summary-prev-subject (n &optional unread)
5593   "Go to previous N'th summary line.
5594 If optional argument UNREAD is non-nil, only unread article is selected."
5595   (interactive "p")
5596   (gnus-summary-next-subject (- n) unread))
5597
5598 (defun gnus-summary-prev-unread-subject (n)
5599   "Go to previous N'th unread summary line."
5600   (interactive "p")
5601   (gnus-summary-next-subject (- n) t))
5602
5603 (defun gnus-summary-goto-subject (article &optional force silent)
5604   "Go the subject line of ARTICLE.
5605 If FORCE, also allow jumping to articles not currently shown."
5606   (interactive "nArticle number: ")
5607   (let ((b (point))
5608         (data (gnus-data-find article)))
5609     ;; We read in the article if we have to.
5610     (and (not data)
5611          force
5612          (gnus-summary-insert-subject
5613           article
5614           (if (or (numberp force) (vectorp force)) force)
5615           t)
5616          (setq data (gnus-data-find article)))
5617     (goto-char b)
5618     (if (not data)
5619         (progn
5620           (unless silent
5621             (gnus-message 3 "Can't find article %d" article))
5622           nil)
5623       (goto-char (gnus-data-pos data))
5624       (gnus-summary-position-point)
5625       article)))
5626
5627 ;; Walking around summary lines with displaying articles.
5628
5629 (defun gnus-summary-expand-window (&optional arg)
5630   "Make the summary buffer take up the entire Emacs frame.
5631 Given a prefix, will force an `article' buffer configuration."
5632   (interactive "P")
5633   (if arg
5634       (gnus-configure-windows 'article 'force)
5635     (gnus-configure-windows 'summary 'force)))
5636
5637 (defun gnus-summary-display-article (article &optional all-header)
5638   "Display ARTICLE in article buffer."
5639   (gnus-set-global-variables)
5640   (if (null article)
5641       nil
5642     (prog1
5643         (if gnus-summary-display-article-function
5644             (funcall gnus-summary-display-article-function article all-header)
5645           (gnus-article-prepare article all-header))
5646       (gnus-run-hooks 'gnus-select-article-hook)
5647       (when (and gnus-current-article
5648                  (not (zerop gnus-current-article)))
5649         (gnus-summary-goto-subject gnus-current-article))
5650       (gnus-summary-recenter)
5651       (when (and gnus-use-trees gnus-show-threads)
5652         (gnus-possibly-generate-tree article)
5653         (gnus-highlight-selected-tree article))
5654       ;; Successfully display article.
5655       (gnus-article-set-window-start
5656        (cdr (assq article gnus-newsgroup-bookmarks))))))
5657
5658 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5659   "Select the current article.
5660 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5661 non-nil, the article will be re-fetched even if it already present in
5662 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5663 be displayed."
5664   ;; Make sure we are in the summary buffer to work around bbdb bug.
5665   (unless (eq major-mode 'gnus-summary-mode)
5666     (set-buffer gnus-summary-buffer))
5667   (let ((article (or article (gnus-summary-article-number)))
5668         (all-headers (not (not all-headers))) ;Must be T or NIL.
5669         gnus-summary-display-article-function
5670         did)
5671     (and (not pseudo)
5672          (gnus-summary-article-pseudo-p article)
5673          (error "This is a pseudo-article"))
5674     (prog1
5675         (save-excursion
5676           (set-buffer gnus-summary-buffer)
5677           (if (or (and gnus-single-article-buffer
5678                        (or (null gnus-current-article)
5679                            (null gnus-article-current)
5680                            (null (get-buffer gnus-article-buffer))
5681                            (not (eq article (cdr gnus-article-current)))
5682                            (not (equal (car gnus-article-current)
5683                                        gnus-newsgroup-name))))
5684                   (and (not gnus-single-article-buffer)
5685                        (or (null gnus-current-article)
5686                            (not (eq gnus-current-article article))))
5687                   force)
5688               ;; The requested article is different from the current article.
5689               (prog1
5690                   (gnus-summary-display-article article all-headers)
5691                 (setq did article))
5692             (when (or all-headers gnus-show-all-headers)
5693               (gnus-article-show-all-headers))
5694             'old))
5695       (when did
5696         (gnus-article-set-window-start
5697          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5698
5699 (defun gnus-summary-set-current-mark (&optional current-mark)
5700   "Obsolete function."
5701   nil)
5702
5703 (defun gnus-summary-next-article (&optional unread subject backward push)
5704   "Select the next article.
5705 If UNREAD, only unread articles are selected.
5706 If SUBJECT, only articles with SUBJECT are selected.
5707 If BACKWARD, the previous article is selected instead of the next."
5708   (interactive "P")
5709   (cond
5710    ;; Is there such an article?
5711    ((and (gnus-summary-search-forward unread subject backward)
5712          (or (gnus-summary-display-article (gnus-summary-article-number))
5713              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5714     (gnus-summary-position-point))
5715    ;; If not, we try the first unread, if that is wanted.
5716    ((and subject
5717          gnus-auto-select-same
5718          (gnus-summary-first-unread-article))
5719     (gnus-summary-position-point)
5720     (gnus-message 6 "Wrapped"))
5721    ;; Try to get next/previous article not displayed in this group.
5722    ((and gnus-auto-extend-newsgroup
5723          (not unread) (not subject))
5724     (gnus-summary-goto-article
5725      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5726      nil (count-lines (point-min) (point))))
5727    ;; Go to next/previous group.
5728    (t
5729     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5730       (gnus-summary-jump-to-group gnus-newsgroup-name))
5731     (let ((cmd last-command-char)
5732           (point
5733            (save-excursion
5734              (set-buffer gnus-group-buffer)
5735              (point)))
5736           (group
5737            (if (eq gnus-keep-same-level 'best)
5738                (gnus-summary-best-group gnus-newsgroup-name)
5739              (gnus-summary-search-group backward gnus-keep-same-level))))
5740       ;; For some reason, the group window gets selected.  We change
5741       ;; it back.
5742       (select-window (get-buffer-window (current-buffer)))
5743       ;; Select next unread newsgroup automagically.
5744       (cond
5745        ((or (not gnus-auto-select-next)
5746             (not cmd))
5747         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5748        ((or (eq gnus-auto-select-next 'quietly)
5749             (and (eq gnus-auto-select-next 'slightly-quietly)
5750                  push)
5751             (and (eq gnus-auto-select-next 'almost-quietly)
5752                  (gnus-summary-last-article-p)))
5753         ;; Select quietly.
5754         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5755             (gnus-summary-exit)
5756           (gnus-message 7 "No more%s articles (%s)..."
5757                         (if unread " unread" "")
5758                         (if group (concat "selecting " group)
5759                           "exiting"))
5760           (gnus-summary-next-group nil group backward)))
5761        (t
5762         (when (gnus-key-press-event-p last-input-event)
5763           (gnus-summary-walk-group-buffer
5764            gnus-newsgroup-name cmd unread backward point))))))))
5765
5766 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5767   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5768                       (?\C-p (gnus-group-prev-unread-group 1))))
5769         (cursor-in-echo-area t)
5770         keve key group ended)
5771     (save-excursion
5772       (set-buffer gnus-group-buffer)
5773       (goto-char start)
5774       (setq group
5775             (if (eq gnus-keep-same-level 'best)
5776                 (gnus-summary-best-group gnus-newsgroup-name)
5777               (gnus-summary-search-group backward gnus-keep-same-level))))
5778     (while (not ended)
5779       (gnus-message
5780        5 "No more%s articles%s" (if unread " unread" "")
5781        (if (and group
5782                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5783            (format " (Type %s for %s [%s])"
5784                    (single-key-description cmd) group
5785                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5786          (format " (Type %s to exit %s)"
5787                  (single-key-description cmd)
5788                  gnus-newsgroup-name)))
5789       ;; Confirm auto selection.
5790       (setq key (car (setq keve (gnus-read-event-char))))
5791       (setq ended t)
5792       (cond
5793        ((assq key keystrokes)
5794         (let ((obuf (current-buffer)))
5795           (switch-to-buffer gnus-group-buffer)
5796           (when group
5797             (gnus-group-jump-to-group group))
5798           (eval (cadr (assq key keystrokes)))
5799           (setq group (gnus-group-group-name))
5800           (switch-to-buffer obuf))
5801         (setq ended nil))
5802        ((equal key cmd)
5803         (if (or (not group)
5804                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5805             (gnus-summary-exit)
5806           (gnus-summary-next-group nil group backward)))
5807        (t
5808         (push (cdr keve) unread-command-events))))))
5809
5810 (defun gnus-summary-next-unread-article ()
5811   "Select unread article after current one."
5812   (interactive)
5813   (gnus-summary-next-article
5814    (or (not (eq gnus-summary-goto-unread 'never))
5815        (gnus-summary-last-article-p (gnus-summary-article-number)))
5816    (and gnus-auto-select-same
5817         (gnus-summary-article-subject))))
5818
5819 (defun gnus-summary-prev-article (&optional unread subject)
5820   "Select the article after the current one.
5821 If UNREAD is non-nil, only unread articles are selected."
5822   (interactive "P")
5823   (gnus-summary-next-article unread subject t))
5824
5825 (defun gnus-summary-prev-unread-article ()
5826   "Select unread article before current one."
5827   (interactive)
5828   (gnus-summary-prev-article
5829    (or (not (eq gnus-summary-goto-unread 'never))
5830        (gnus-summary-first-article-p (gnus-summary-article-number)))
5831    (and gnus-auto-select-same
5832         (gnus-summary-article-subject))))
5833
5834 (defun gnus-summary-next-page (&optional lines circular)
5835   "Show next page of the selected article.
5836 If at the end of the current article, select the next article.
5837 LINES says how many lines should be scrolled up.
5838
5839 If CIRCULAR is non-nil, go to the start of the article instead of
5840 selecting the next article when reaching the end of the current
5841 article."
5842   (interactive "P")
5843   (setq gnus-summary-buffer (current-buffer))
5844   (gnus-set-global-variables)
5845   (let ((article (gnus-summary-article-number))
5846         (article-window (get-buffer-window gnus-article-buffer t))
5847         endp)
5848     ;; If the buffer is empty, we have no article.
5849     (unless article
5850       (error "No article to select"))
5851     (gnus-configure-windows 'article)
5852     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5853         (if (and (eq gnus-summary-goto-unread 'never)
5854                  (not (gnus-summary-last-article-p article)))
5855             (gnus-summary-next-article)
5856           (gnus-summary-next-unread-article))
5857       (if (or (null gnus-current-article)
5858               (null gnus-article-current)
5859               (/= article (cdr gnus-article-current))
5860               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5861           ;; Selected subject is different from current article's.
5862           (gnus-summary-display-article article)
5863         (when article-window
5864           (gnus-eval-in-buffer-window gnus-article-buffer
5865             (setq endp (gnus-article-next-page lines)))
5866           (when endp
5867             (cond (circular
5868                    (gnus-summary-beginning-of-article))
5869                   (lines
5870                    (gnus-message 3 "End of message"))
5871                   ((null lines)
5872                    (if (and (eq gnus-summary-goto-unread 'never)
5873                             (not (gnus-summary-last-article-p article)))
5874                        (gnus-summary-next-article)
5875                      (gnus-summary-next-unread-article))))))))
5876     (gnus-summary-recenter)
5877     (gnus-summary-position-point)))
5878
5879 (defun gnus-summary-prev-page (&optional lines move)
5880   "Show previous page of selected article.
5881 Argument LINES specifies lines to be scrolled down.
5882 If MOVE, move to the previous unread article if point is at
5883 the beginning of the buffer."
5884   (interactive "P")
5885   (let ((article (gnus-summary-article-number))
5886         (article-window (get-buffer-window gnus-article-buffer t))
5887         endp)
5888     (gnus-configure-windows 'article)
5889     (if (or (null gnus-current-article)
5890             (null gnus-article-current)
5891             (/= article (cdr gnus-article-current))
5892             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5893         ;; Selected subject is different from current article's.
5894         (gnus-summary-display-article article)
5895       (gnus-summary-recenter)
5896       (when article-window
5897         (gnus-eval-in-buffer-window gnus-article-buffer
5898           (setq endp (gnus-article-prev-page lines)))
5899         (when (and move endp)
5900           (cond (lines
5901                  (gnus-message 3 "Beginning of message"))
5902                 ((null lines)
5903                  (if (and (eq gnus-summary-goto-unread 'never)
5904                           (not (gnus-summary-first-article-p article)))
5905                      (gnus-summary-prev-article)
5906                    (gnus-summary-prev-unread-article))))))))
5907   (gnus-summary-position-point))
5908
5909 (defun gnus-summary-prev-page-or-article (&optional lines)
5910   "Show previous page of selected article.
5911 Argument LINES specifies lines to be scrolled down.
5912 If at the beginning of the article, go to the next article."
5913   (interactive "P")
5914   (gnus-summary-prev-page lines t))
5915
5916 (defun gnus-summary-scroll-up (lines)
5917   "Scroll up (or down) one line current article.
5918 Argument LINES specifies lines to be scrolled up (or down if negative)."
5919   (interactive "p")
5920   (gnus-configure-windows 'article)
5921   (gnus-summary-show-thread)
5922   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5923     (gnus-eval-in-buffer-window gnus-article-buffer
5924       (cond ((> lines 0)
5925              (when (gnus-article-next-page lines)
5926                (gnus-message 3 "End of message")))
5927             ((< lines 0)
5928              (gnus-article-prev-page (- lines))))))
5929   (gnus-summary-recenter)
5930   (gnus-summary-position-point))
5931
5932 (defun gnus-summary-scroll-down (lines)
5933   "Scroll down (or up) one line current article.
5934 Argument LINES specifies lines to be scrolled down (or up if negative)."
5935   (interactive "p")
5936   (gnus-summary-scroll-up (- lines)))
5937
5938 (defun gnus-summary-next-same-subject ()
5939   "Select next article which has the same subject as current one."
5940   (interactive)
5941   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5942
5943 (defun gnus-summary-prev-same-subject ()
5944   "Select previous article which has the same subject as current one."
5945   (interactive)
5946   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5947
5948 (defun gnus-summary-next-unread-same-subject ()
5949   "Select next unread article which has the same subject as current one."
5950   (interactive)
5951   (gnus-summary-next-article t (gnus-summary-article-subject)))
5952
5953 (defun gnus-summary-prev-unread-same-subject ()
5954   "Select previous unread article which has the same subject as current one."
5955   (interactive)
5956   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5957
5958 (defun gnus-summary-first-unread-article ()
5959   "Select the first unread article.
5960 Return nil if there are no unread articles."
5961   (interactive)
5962   (prog1
5963       (when (gnus-summary-first-subject t)
5964         (gnus-summary-show-thread)
5965         (gnus-summary-first-subject t)
5966         (gnus-summary-display-article (gnus-summary-article-number)))
5967     (gnus-summary-position-point)))
5968
5969 (defun gnus-summary-first-unread-subject ()
5970   "Place the point on the subject line of the first unread article.
5971 Return nil if there are no unread articles."
5972   (interactive)
5973   (prog1
5974       (when (gnus-summary-first-subject t)
5975         (gnus-summary-show-thread)
5976         (gnus-summary-first-subject t))
5977     (gnus-summary-position-point)))
5978
5979 (defun gnus-summary-first-article ()
5980   "Select the first article.
5981 Return nil if there are no articles."
5982   (interactive)
5983   (prog1
5984       (when (gnus-summary-first-subject)
5985         (gnus-summary-show-thread)
5986         (gnus-summary-first-subject)
5987         (gnus-summary-display-article (gnus-summary-article-number)))
5988     (gnus-summary-position-point)))
5989
5990 (defun gnus-summary-best-unread-article ()
5991   "Select the unread article with the highest score."
5992   (interactive)
5993   (let ((best -1000000)
5994         (data gnus-newsgroup-data)
5995         article score)
5996     (while data
5997       (and (gnus-data-unread-p (car data))
5998            (> (setq score
5999                     (gnus-summary-article-score (gnus-data-number (car data))))
6000               best)
6001            (setq best score
6002                  article (gnus-data-number (car data))))
6003       (setq data (cdr data)))
6004     (prog1
6005         (if article
6006             (gnus-summary-goto-article article)
6007           (error "No unread articles"))
6008       (gnus-summary-position-point))))
6009
6010 (defun gnus-summary-last-subject ()
6011   "Go to the last displayed subject line in the group."
6012   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6013     (when article
6014       (gnus-summary-goto-subject article))))
6015
6016 (defun gnus-summary-goto-article (article &optional all-headers force)
6017   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6018 If ALL-HEADERS is non-nil, no header lines are hidden.
6019 If FORCE, go to the article even if it isn't displayed.  If FORCE
6020 is a number, it is the line the article is to be displayed on."
6021   (interactive
6022    (list
6023     (completing-read
6024      "Article number or Message-ID: "
6025      (mapcar (lambda (number) (list (int-to-string number)))
6026              gnus-newsgroup-limit))
6027     current-prefix-arg
6028     t))
6029   (prog1
6030       (if (and (stringp article)
6031                (string-match "@" article))
6032           (gnus-summary-refer-article article)
6033         (when (stringp article)
6034           (setq article (string-to-number article)))
6035         (if (gnus-summary-goto-subject article force)
6036             (gnus-summary-display-article article all-headers)
6037           (gnus-message 4 "Couldn't go to article %s" article) nil))
6038     (gnus-summary-position-point)))
6039
6040 (defun gnus-summary-goto-last-article ()
6041   "Go to the previously read article."
6042   (interactive)
6043   (prog1
6044       (when gnus-last-article
6045         (gnus-summary-goto-article gnus-last-article nil t))
6046     (gnus-summary-position-point)))
6047
6048 (defun gnus-summary-pop-article (number)
6049   "Pop one article off the history and go to the previous.
6050 NUMBER articles will be popped off."
6051   (interactive "p")
6052   (let (to)
6053     (setq gnus-newsgroup-history
6054           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6055     (if to
6056         (gnus-summary-goto-article (car to) nil t)
6057       (error "Article history empty")))
6058   (gnus-summary-position-point))
6059
6060 ;; Summary commands and functions for limiting the summary buffer.
6061
6062 (defun gnus-summary-limit-to-articles (n)
6063   "Limit the summary buffer to the next N articles.
6064 If not given a prefix, use the process marked articles instead."
6065   (interactive "P")
6066   (prog1
6067       (let ((articles (gnus-summary-work-articles n)))
6068         (setq gnus-newsgroup-processable nil)
6069         (gnus-summary-limit articles))
6070     (gnus-summary-position-point)))
6071
6072 (defun gnus-summary-pop-limit (&optional total)
6073   "Restore the previous limit.
6074 If given a prefix, remove all limits."
6075   (interactive "P")
6076   (when total
6077     (setq gnus-newsgroup-limits
6078           (list (mapcar (lambda (h) (mail-header-number h))
6079                         gnus-newsgroup-headers))))
6080   (unless gnus-newsgroup-limits
6081     (error "No limit to pop"))
6082   (prog1
6083       (gnus-summary-limit nil 'pop)
6084     (gnus-summary-position-point)))
6085
6086 (defun gnus-summary-limit-to-subject (subject &optional header)
6087   "Limit the summary buffer to articles that have subjects that match a regexp."
6088   (interactive "sLimit to subject (regexp): ")
6089   (unless header
6090     (setq header "subject"))
6091   (when (not (equal "" subject))
6092     (prog1
6093         (let ((articles (gnus-summary-find-matching
6094                          (or header "subject") subject 'all)))
6095           (unless articles
6096             (error "Found no matches for \"%s\"" subject))
6097           (gnus-summary-limit articles))
6098       (gnus-summary-position-point))))
6099
6100 (defun gnus-summary-limit-to-author (from)
6101   "Limit the summary buffer to articles that have authors that match a regexp."
6102   (interactive "sLimit to author (regexp): ")
6103   (gnus-summary-limit-to-subject from "from"))
6104
6105 (defun gnus-summary-limit-to-age (age &optional younger-p)
6106   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6107 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6108 articles that are younger than AGE days."
6109   (interactive "nTime in days: \nP")
6110   (prog1
6111       (let ((data gnus-newsgroup-data)
6112             (cutoff (days-to-time age))
6113             articles d date is-younger)
6114         (while (setq d (pop data))
6115           (when (and (vectorp (gnus-data-header d))
6116                      (setq date (mail-header-date (gnus-data-header d))))
6117             (setq is-younger (time-less-p
6118                               (time-since (date-to-time date))
6119                               cutoff))
6120             (when (if younger-p
6121                       is-younger
6122                     (not is-younger))
6123               (push (gnus-data-number d) articles))))
6124         (gnus-summary-limit (nreverse articles)))
6125     (gnus-summary-position-point)))
6126
6127 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6128 (make-obsolete
6129  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6130
6131 (defun gnus-summary-limit-to-unread (&optional all)
6132   "Limit the summary buffer to articles that are not marked as read.
6133 If ALL is non-nil, limit strictly to unread articles."
6134   (interactive "P")
6135   (if all
6136       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6137     (gnus-summary-limit-to-marks
6138      ;; Concat all the marks that say that an article is read and have
6139      ;; those removed.
6140      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6141            gnus-killed-mark gnus-kill-file-mark
6142            gnus-low-score-mark gnus-expirable-mark
6143            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6144            gnus-duplicate-mark gnus-souped-mark)
6145      'reverse)))
6146
6147 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6148 (make-obsolete 'gnus-summary-delete-marked-with
6149                'gnus-summary-limit-exlude-marks)
6150
6151 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6152   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6153 If REVERSE, limit the summary buffer to articles that are marked
6154 with MARKS.  MARKS can either be a string of marks or a list of marks.
6155 Returns how many articles were removed."
6156   (interactive "sMarks: ")
6157   (gnus-summary-limit-to-marks marks t))
6158
6159 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6160   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6161 If REVERSE (the prefix), limit the summary buffer to articles that are
6162 not marked with MARKS.  MARKS can either be a string of marks or a
6163 list of marks.
6164 Returns how many articles were removed."
6165   (interactive "sMarks: \nP")
6166   (prog1
6167       (let ((data gnus-newsgroup-data)
6168             (marks (if (listp marks) marks
6169                      (append marks nil))) ; Transform to list.
6170             articles)
6171         (while data
6172           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6173                   (memq (gnus-data-mark (car data)) marks))
6174             (push (gnus-data-number (car data)) articles))
6175           (setq data (cdr data)))
6176         (gnus-summary-limit articles))
6177     (gnus-summary-position-point)))
6178
6179 (defun gnus-summary-limit-to-score (&optional score)
6180   "Limit to articles with score at or above SCORE."
6181   (interactive "P")
6182   (setq score (if score
6183                   (prefix-numeric-value score)
6184                 (or gnus-summary-default-score 0)))
6185   (let ((data gnus-newsgroup-data)
6186         articles)
6187     (while data
6188       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6189                 score)
6190         (push (gnus-data-number (car data)) articles))
6191       (setq data (cdr data)))
6192     (prog1
6193         (gnus-summary-limit articles)
6194       (gnus-summary-position-point))))
6195
6196 (defun gnus-summary-limit-include-thread (id)
6197   "Display all the hidden articles that in the current thread."
6198   (interactive (list (mail-header-id (gnus-summary-article-header))))
6199   (let ((articles (gnus-articles-in-thread
6200                    (gnus-id-to-thread (gnus-root-id id)))))
6201     (prog1
6202         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6203       (gnus-summary-position-point))))
6204
6205 (defun gnus-summary-limit-include-dormant ()
6206   "Display all the hidden articles that are marked as dormant.
6207 Note that this command only works on a subset of the articles currently
6208 fetched for this group."
6209   (interactive)
6210   (unless gnus-newsgroup-dormant
6211     (error "There are no dormant articles in this group"))
6212   (prog1
6213       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-limit-exclude-dormant ()
6217   "Hide all dormant articles."
6218   (interactive)
6219   (prog1
6220       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6221     (gnus-summary-position-point)))
6222
6223 (defun gnus-summary-limit-exclude-childless-dormant ()
6224   "Hide all dormant articles that have no children."
6225   (interactive)
6226   (let ((data (gnus-data-list t))
6227         articles d children)
6228     ;; Find all articles that are either not dormant or have
6229     ;; children.
6230     (while (setq d (pop data))
6231       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6232                 (and (setq children
6233                            (gnus-article-children (gnus-data-number d)))
6234                      (let (found)
6235                        (while children
6236                          (when (memq (car children) articles)
6237                            (setq children nil
6238                                  found t))
6239                          (pop children))
6240                        found)))
6241         (push (gnus-data-number d) articles)))
6242     ;; Do the limiting.
6243     (prog1
6244         (gnus-summary-limit articles)
6245       (gnus-summary-position-point))))
6246
6247 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6248   "Mark all unread excluded articles as read.
6249 If ALL, mark even excluded ticked and dormants as read."
6250   (interactive "P")
6251   (let ((articles (gnus-sorted-complement
6252                    (sort
6253                     (mapcar (lambda (h) (mail-header-number h))
6254                             gnus-newsgroup-headers)
6255                     '<)
6256                    (sort gnus-newsgroup-limit '<)))
6257         article)
6258     (setq gnus-newsgroup-unreads
6259           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6260     (if all
6261         (setq gnus-newsgroup-dormant nil
6262               gnus-newsgroup-marked nil
6263               gnus-newsgroup-reads
6264               (nconc
6265                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6266                gnus-newsgroup-reads))
6267       (while (setq article (pop articles))
6268         (unless (or (memq article gnus-newsgroup-dormant)
6269                     (memq article gnus-newsgroup-marked))
6270           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6271
6272 (defun gnus-summary-limit (articles &optional pop)
6273   (if pop
6274       ;; We pop the previous limit off the stack and use that.
6275       (setq articles (car gnus-newsgroup-limits)
6276             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6277     ;; We use the new limit, so we push the old limit on the stack.
6278     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6279   ;; Set the limit.
6280   (setq gnus-newsgroup-limit articles)
6281   (let ((total (length gnus-newsgroup-data))
6282         (data (gnus-data-find-list (gnus-summary-article-number)))
6283         (gnus-summary-mark-below nil)   ; Inhibit this.
6284         found)
6285     ;; This will do all the work of generating the new summary buffer
6286     ;; according to the new limit.
6287     (gnus-summary-prepare)
6288     ;; Hide any threads, possibly.
6289     (and gnus-show-threads
6290          gnus-thread-hide-subtree
6291          (gnus-summary-hide-all-threads))
6292     ;; Try to return to the article you were at, or one in the
6293     ;; neighborhood.
6294     (when data
6295       ;; We try to find some article after the current one.
6296       (while data
6297         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6298           (setq data nil
6299                 found t))
6300         (setq data (cdr data))))
6301     (unless found
6302       ;; If there is no data, that means that we were after the last
6303       ;; article.  The same goes when we can't find any articles
6304       ;; after the current one.
6305       (goto-char (point-max))
6306       (gnus-summary-find-prev))
6307     (gnus-set-mode-line 'summary)
6308     ;; We return how many articles were removed from the summary
6309     ;; buffer as a result of the new limit.
6310     (- total (length gnus-newsgroup-data))))
6311
6312 (defsubst gnus-invisible-cut-children (threads)
6313   (let ((num 0))
6314     (while threads
6315       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6316         (incf num))
6317       (pop threads))
6318     (< num 2)))
6319
6320 (defsubst gnus-cut-thread (thread)
6321   "Go forwards in the thread until we find an article that we want to display."
6322   (when (or (eq gnus-fetch-old-headers 'some)
6323             (eq gnus-fetch-old-headers 'invisible)
6324             (eq gnus-build-sparse-threads 'some)
6325             (eq gnus-build-sparse-threads 'more))
6326     ;; Deal with old-fetched headers and sparse threads.
6327     (while (and
6328             thread
6329             (or
6330              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6331              (gnus-summary-article-ancient-p
6332               (mail-header-number (car thread))))
6333             (if (or (<= (length (cdr thread)) 1)
6334                     (eq gnus-fetch-old-headers 'invisible))
6335                 (setq gnus-newsgroup-limit
6336                       (delq (mail-header-number (car thread))
6337                             gnus-newsgroup-limit)
6338                       thread (cadr thread))
6339               (when (gnus-invisible-cut-children (cdr thread))
6340                 (let ((th (cdr thread)))
6341                   (while th
6342                     (if (memq (mail-header-number (caar th))
6343                               gnus-newsgroup-limit)
6344                         (setq thread (car th)
6345                               th nil)
6346                       (setq th (cdr th))))))))))
6347   thread)
6348
6349 (defun gnus-cut-threads (threads)
6350   "Cut off all uninteresting articles from the beginning of threads."
6351   (when (or (eq gnus-fetch-old-headers 'some)
6352             (eq gnus-fetch-old-headers 'invisible)
6353             (eq gnus-build-sparse-threads 'some)
6354             (eq gnus-build-sparse-threads 'more))
6355     (let ((th threads))
6356       (while th
6357         (setcar th (gnus-cut-thread (car th)))
6358         (setq th (cdr th)))))
6359   ;; Remove nixed out threads.
6360   (delq nil threads))
6361
6362 (defun gnus-summary-initial-limit (&optional show-if-empty)
6363   "Figure out what the initial limit is supposed to be on group entry.
6364 This entails weeding out unwanted dormants, low-scored articles,
6365 fetch-old-headers verbiage, and so on."
6366   ;; Most groups have nothing to remove.
6367   (if (or gnus-inhibit-limiting
6368           (and (null gnus-newsgroup-dormant)
6369                (not (eq gnus-fetch-old-headers 'some))
6370                (not (eq gnus-fetch-old-headers 'invisible))
6371                (null gnus-summary-expunge-below)
6372                (not (eq gnus-build-sparse-threads 'some))
6373                (not (eq gnus-build-sparse-threads 'more))
6374                (null gnus-thread-expunge-below)
6375                (not gnus-use-nocem)))
6376       ()                                ; Do nothing.
6377     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6378     (setq gnus-newsgroup-limit nil)
6379     (mapatoms
6380      (lambda (node)
6381        (unless (car (symbol-value node))
6382          ;; These threads have no parents -- they are roots.
6383          (let ((nodes (cdr (symbol-value node)))
6384                thread)
6385            (while nodes
6386              (if (and gnus-thread-expunge-below
6387                       (< (gnus-thread-total-score (car nodes))
6388                          gnus-thread-expunge-below))
6389                  (gnus-expunge-thread (pop nodes))
6390                (setq thread (pop nodes))
6391                (gnus-summary-limit-children thread))))))
6392      gnus-newsgroup-dependencies)
6393     ;; If this limitation resulted in an empty group, we might
6394     ;; pop the previous limit and use it instead.
6395     (when (and (not gnus-newsgroup-limit)
6396                show-if-empty)
6397       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6398     gnus-newsgroup-limit))
6399
6400 (defun gnus-summary-limit-children (thread)
6401   "Return 1 if this subthread is visible and 0 if it is not."
6402   ;; First we get the number of visible children to this thread.  This
6403   ;; is done by recursing down the thread using this function, so this
6404   ;; will really go down to a leaf article first, before slowly
6405   ;; working its way up towards the root.
6406   (when thread
6407     (let ((children
6408            (if (cdr thread)
6409                (apply '+ (mapcar 'gnus-summary-limit-children
6410                                  (cdr thread)))
6411              0))
6412           (number (mail-header-number (car thread)))
6413           score)
6414       (if (and
6415            (not (memq number gnus-newsgroup-marked))
6416            (or
6417             ;; If this article is dormant and has absolutely no visible
6418             ;; children, then this article isn't visible.
6419             (and (memq number gnus-newsgroup-dormant)
6420                  (zerop children))
6421             ;; If this is "fetch-old-headered" and there is no
6422             ;; visible children, then we don't want this article.
6423             (and (eq gnus-fetch-old-headers 'some)
6424                  (gnus-summary-article-ancient-p number)
6425                  (zerop children))
6426             ;; If this is "fetch-old-headered" and `invisible', then
6427             ;; we don't want this article.
6428             (and (eq gnus-fetch-old-headers 'invisible)
6429                  (gnus-summary-article-ancient-p number))
6430             ;; If this is a sparsely inserted article with no children,
6431             ;; we don't want it.
6432             (and (eq gnus-build-sparse-threads 'some)
6433                  (gnus-summary-article-sparse-p number)
6434                  (zerop children))
6435             ;; If we use expunging, and this article is really
6436             ;; low-scored, then we don't want this article.
6437             (when (and gnus-summary-expunge-below
6438                        (< (setq score
6439                                 (or (cdr (assq number gnus-newsgroup-scored))
6440                                     gnus-summary-default-score))
6441                           gnus-summary-expunge-below))
6442               ;; We increase the expunge-tally here, but that has
6443               ;; nothing to do with the limits, really.
6444               (incf gnus-newsgroup-expunged-tally)
6445               ;; We also mark as read here, if that's wanted.
6446               (when (and gnus-summary-mark-below
6447                          (< score gnus-summary-mark-below))
6448                 (setq gnus-newsgroup-unreads
6449                       (delq number gnus-newsgroup-unreads))
6450                 (if gnus-newsgroup-auto-expire
6451                     (push number gnus-newsgroup-expirable)
6452                   (push (cons number gnus-low-score-mark)
6453                         gnus-newsgroup-reads)))
6454               t)
6455             ;; Check NoCeM things.
6456             (if (and gnus-use-nocem
6457                      (gnus-nocem-unwanted-article-p
6458                       (mail-header-id (car thread))))
6459                 (progn
6460                   (setq gnus-newsgroup-unreads
6461                         (delq number gnus-newsgroup-unreads))
6462                   t))))
6463           ;; Nope, invisible article.
6464           0
6465         ;; Ok, this article is to be visible, so we add it to the limit
6466         ;; and return 1.
6467         (push number gnus-newsgroup-limit)
6468         1))))
6469
6470 (defun gnus-expunge-thread (thread)
6471   "Mark all articles in THREAD as read."
6472   (let* ((number (mail-header-number (car thread))))
6473     (incf gnus-newsgroup-expunged-tally)
6474     ;; We also mark as read here, if that's wanted.
6475     (setq gnus-newsgroup-unreads
6476           (delq number gnus-newsgroup-unreads))
6477     (if gnus-newsgroup-auto-expire
6478         (push number gnus-newsgroup-expirable)
6479       (push (cons number gnus-low-score-mark)
6480             gnus-newsgroup-reads)))
6481   ;; Go recursively through all subthreads.
6482   (mapcar 'gnus-expunge-thread (cdr thread)))
6483
6484 ;; Summary article oriented commands
6485
6486 (defun gnus-summary-refer-parent-article (n)
6487   "Refer parent article N times.
6488 If N is negative, go to ancestor -N instead.
6489 The difference between N and the number of articles fetched is returned."
6490   (interactive "p")
6491   (let ((skip 1)
6492         error header ref)
6493     (when (not (natnump n))
6494       (setq skip (abs n)
6495             n 1))
6496     (while (and (> n 0)
6497                 (not error))
6498       (setq header (gnus-summary-article-header))
6499       (if (and (eq (mail-header-number header)
6500                    (cdr gnus-article-current))
6501                (equal gnus-newsgroup-name
6502                       (car gnus-article-current)))
6503           ;; If we try to find the parent of the currently
6504           ;; displayed article, then we take a look at the actual
6505           ;; References header, since this is slightly more
6506           ;; reliable than the References field we got from the
6507           ;; server.
6508           (save-excursion
6509             (set-buffer gnus-original-article-buffer)
6510             (nnheader-narrow-to-headers)
6511             (unless (setq ref (message-fetch-field "references"))
6512               (setq ref (message-fetch-field "in-reply-to")))
6513             (widen))
6514         (setq ref
6515               ;; It's not the current article, so we take a bet on
6516               ;; the value we got from the server.
6517               (mail-header-references header)))
6518       (if (and ref
6519                (not (equal ref "")))
6520           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6521             (gnus-message 1 "Couldn't find parent"))
6522         (gnus-message 1 "No references in article %d"
6523                       (gnus-summary-article-number))
6524         (setq error t))
6525       (decf n))
6526     (gnus-summary-position-point)
6527     n))
6528
6529 (defun gnus-summary-refer-references ()
6530   "Fetch all articles mentioned in the References header.
6531 Return the number of articles fetched."
6532   (interactive)
6533   (let ((ref (mail-header-references (gnus-summary-article-header)))
6534         (current (gnus-summary-article-number))
6535         (n 0))
6536     (if (or (not ref)
6537             (equal ref ""))
6538         (error "No References in the current article")
6539       ;; For each Message-ID in the References header...
6540       (while (string-match "<[^>]*>" ref)
6541         (incf n)
6542         ;; ... fetch that article.
6543         (gnus-summary-refer-article
6544          (prog1 (match-string 0 ref)
6545            (setq ref (substring ref (match-end 0))))))
6546       (gnus-summary-goto-subject current)
6547       (gnus-summary-position-point)
6548       n)))
6549
6550 (defun gnus-summary-refer-thread (&optional limit)
6551   "Fetch all articles in the current thread.
6552 If LIMIT (the numerical prefix), fetch that many old headers instead
6553 of what's specified by the `gnus-refer-thread-limit' variable."
6554   (interactive "P")
6555   (let ((id (mail-header-id (gnus-summary-article-header)))
6556         (limit (if limit (prefix-numeric-value limit)
6557                  gnus-refer-thread-limit)))
6558     ;; We want to fetch LIMIT *old* headers, but we also have to
6559     ;; re-fetch all the headers in the current buffer, because many of
6560     ;; them may be undisplayed.  So we adjust LIMIT.
6561     (when (numberp limit)
6562       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6563     (unless (eq gnus-fetch-old-headers 'invisible)
6564       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6565       ;; Retrieve the headers and read them in.
6566       (if (eq (gnus-retrieve-headers
6567                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6568               'nov)
6569           (gnus-build-all-threads)
6570         (error "Can't fetch thread from backends that don't support NOV"))
6571       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6572     (gnus-summary-limit-include-thread id)))
6573
6574 (defun gnus-summary-refer-article (message-id &optional arg)
6575   "Fetch an article specified by MESSAGE-ID.
6576 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6577 or `gnus-select-method', no matter what backend the article comes from."
6578   (interactive "sMessage-ID: \nP")
6579   (when (and (stringp message-id)
6580              (not (zerop (length message-id))))
6581     ;; Construct the correct Message-ID if necessary.
6582     ;; Suggested by tale@pawl.rpi.edu.
6583     (unless (string-match "^<" message-id)
6584       (setq message-id (concat "<" message-id)))
6585     (unless (string-match ">$" message-id)
6586       (setq message-id (concat message-id ">")))
6587     (let* ((header (gnus-id-to-header message-id))
6588            (sparse (and header
6589                         (gnus-summary-article-sparse-p
6590                          (mail-header-number header))
6591                         (memq (mail-header-number header)
6592                               gnus-newsgroup-limit))))
6593       (cond
6594        ;; If the article is present in the buffer we just go to it.
6595        ((and header
6596              (or (not (gnus-summary-article-sparse-p
6597                        (mail-header-number header)))
6598                  sparse))
6599         (prog1
6600             (gnus-summary-goto-article
6601              (mail-header-number header) nil t)
6602           (when sparse
6603             (gnus-summary-update-article (mail-header-number header)))))
6604        (t
6605         ;; We fetch the article
6606         (let ((gnus-override-method
6607                (cond ((gnus-news-group-p gnus-newsgroup-name)
6608                       gnus-refer-article-method)
6609                      (arg
6610                       (or gnus-refer-article-method gnus-select-method))
6611                      (t nil)))
6612               number)
6613           ;; Start the special refer-article method, if necessary.
6614           (when (and gnus-refer-article-method
6615                      (gnus-news-group-p gnus-newsgroup-name))
6616             (gnus-check-server gnus-refer-article-method))
6617           ;; Fetch the header, and display the article.
6618           (if (setq number (gnus-summary-insert-subject message-id))
6619               (gnus-summary-select-article nil nil nil number)
6620             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6621
6622 (defun gnus-summary-edit-parameters ()
6623   "Edit the group parameters of the current group."
6624   (interactive)
6625   (gnus-group-edit-group gnus-newsgroup-name 'params))
6626
6627 (defun gnus-summary-customize-parameters ()
6628   "Customize the group parameters of the current group."
6629   (interactive)
6630   (gnus-group-customize gnus-newsgroup-name))
6631
6632 (defun gnus-summary-enter-digest-group (&optional force)
6633   "Enter an nndoc group based on the current article.
6634 If FORCE, force a digest interpretation.  If not, try
6635 to guess what the document format is."
6636   (interactive "P")
6637   (let ((conf gnus-current-window-configuration))
6638     (save-excursion
6639       (gnus-summary-select-article))
6640     (setq gnus-current-window-configuration conf)
6641     (let* ((name (format "%s-%d"
6642                          (gnus-group-prefixed-name
6643                           gnus-newsgroup-name (list 'nndoc ""))
6644                          (save-excursion
6645                            (set-buffer gnus-summary-buffer)
6646                            gnus-current-article)))
6647            (ogroup gnus-newsgroup-name)
6648            (params (append (gnus-info-params (gnus-get-info ogroup))
6649                            (list (cons 'to-group ogroup))
6650                            (list (cons 'save-article-group ogroup))))
6651            (case-fold-search t)
6652            (buf (current-buffer))
6653            dig)
6654       (save-excursion
6655         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6656         (insert-buffer-substring gnus-original-article-buffer)
6657         ;; Remove lines that may lead nndoc to misinterpret the
6658         ;; document type.
6659         (narrow-to-region
6660          (goto-char (point-min))
6661          (or (search-forward "\n\n" nil t) (point)))
6662         (goto-char (point-min))
6663         (delete-matching-lines "^\\(Path\\):\\|^From ")
6664         (widen))
6665       (unwind-protect
6666           (if (gnus-group-read-ephemeral-group
6667                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6668                             (nndoc-article-type
6669                              ,(if force 'digest 'guess))) t)
6670               ;; Make all postings to this group go to the parent group.
6671               (nconc (gnus-info-params (gnus-get-info name))
6672                      params)
6673             ;; Couldn't select this doc group.
6674             (switch-to-buffer buf)
6675             (gnus-set-global-variables)
6676             (gnus-configure-windows 'summary)
6677             (gnus-message 3 "Article couldn't be entered?"))
6678         (kill-buffer dig)))))
6679
6680 (defun gnus-summary-read-document (n)
6681   "Open a new group based on the current article(s).
6682 This will allow you to read digests and other similar
6683 documents as newsgroups.
6684 Obeys the standard process/prefix convention."
6685   (interactive "P")
6686   (let* ((articles (gnus-summary-work-articles n))
6687          (ogroup gnus-newsgroup-name)
6688          (params (append (gnus-info-params (gnus-get-info ogroup))
6689                          (list (cons 'to-group ogroup))))
6690          article group egroup groups vgroup)
6691     (while (setq article (pop articles))
6692       (setq group (format "%s-%d" gnus-newsgroup-name article))
6693       (gnus-summary-remove-process-mark article)
6694       (when (gnus-summary-display-article article)
6695         (save-excursion
6696           (with-temp-buffer
6697             (insert-buffer-substring gnus-original-article-buffer)
6698             ;; Remove some headers that may lead nndoc to make
6699             ;; the wrong guess.
6700             (message-narrow-to-head)
6701             (goto-char (point-min))
6702             (delete-matching-lines "^\\(Path\\):\\|^From ")
6703             (widen)
6704             (if (setq egroup
6705                       (gnus-group-read-ephemeral-group
6706                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6707                                      (nndoc-article-type guess))
6708                        t nil t))
6709                 (progn
6710                   ;; Make all postings to this group go to the parent group.
6711                   (nconc (gnus-info-params (gnus-get-info egroup))
6712                          params)
6713                   (push egroup groups))
6714               ;; Couldn't select this doc group.
6715               (gnus-error 3 "Article couldn't be entered"))))))
6716     ;; Now we have selected all the documents.
6717     (cond
6718      ((not groups)
6719       (error "None of the articles could be interpreted as documents"))
6720      ((gnus-group-read-ephemeral-group
6721        (setq vgroup (format
6722                      "nnvirtual:%s-%s" gnus-newsgroup-name
6723                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6724        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6725        t
6726        (cons (current-buffer) 'summary)))
6727      (t
6728       (error "Couldn't select virtual nndoc group")))))
6729
6730 (defun gnus-summary-isearch-article (&optional regexp-p)
6731   "Do incremental search forward on the current article.
6732 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6733   (interactive "P")
6734   (gnus-summary-select-article)
6735   (gnus-configure-windows 'article)
6736   (gnus-eval-in-buffer-window gnus-article-buffer
6737     (save-restriction
6738       (widen)
6739       (isearch-forward regexp-p))))
6740
6741 (defun gnus-summary-search-article-forward (regexp &optional backward)
6742   "Search for an article containing REGEXP forward.
6743 If BACKWARD, search backward instead."
6744   (interactive
6745    (list (read-string
6746           (format "Search article %s (regexp%s): "
6747                   (if current-prefix-arg "backward" "forward")
6748                   (if gnus-last-search-regexp
6749                       (concat ", default " gnus-last-search-regexp)
6750                     "")))
6751          current-prefix-arg))
6752   (if (string-equal regexp "")
6753       (setq regexp (or gnus-last-search-regexp ""))
6754     (setq gnus-last-search-regexp regexp))
6755   (if (gnus-summary-search-article regexp backward)
6756       (gnus-summary-show-thread)
6757     (error "Search failed: \"%s\"" regexp)))
6758
6759 (defun gnus-summary-search-article-backward (regexp)
6760   "Search for an article containing REGEXP backward."
6761   (interactive
6762    (list (read-string
6763           (format "Search article backward (regexp%s): "
6764                   (if gnus-last-search-regexp
6765                       (concat ", default " gnus-last-search-regexp)
6766                     "")))))
6767   (gnus-summary-search-article-forward regexp 'backward))
6768
6769 (defun gnus-summary-search-article (regexp &optional backward)
6770   "Search for an article containing REGEXP.
6771 Optional argument BACKWARD means do search for backward.
6772 `gnus-select-article-hook' is not called during the search."
6773   ;; We have to require this here to make sure that the following
6774   ;; dynamic binding isn't shadowed by autoloading.
6775   (require 'gnus-async)
6776   (require 'gnus-art)
6777   (let ((gnus-select-article-hook nil)  ;Disable hook.
6778         (gnus-article-display-hook nil)
6779         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6780         (gnus-use-article-prefetch nil)
6781         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6782         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6783         (sum (current-buffer))
6784         (found nil)
6785         point)
6786     (gnus-save-hidden-threads
6787       (gnus-summary-select-article)
6788       (set-buffer gnus-article-buffer)
6789       (when backward
6790         (forward-line -1))
6791       (while (not found)
6792         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6793         (if (if backward
6794                 (re-search-backward regexp nil t)
6795               (re-search-forward regexp nil t))
6796             ;; We found the regexp.
6797             (progn
6798               (setq found 'found)
6799               (beginning-of-line)
6800               (set-window-start
6801                (get-buffer-window (current-buffer))
6802                (point))
6803               (forward-line 1)
6804               (set-buffer sum)
6805               (setq point (point)))
6806           ;; We didn't find it, so we go to the next article.
6807           (set-buffer sum)
6808           (setq found 'not)
6809           (while (eq found 'not)
6810             (if (not (if backward (gnus-summary-find-prev)
6811                        (gnus-summary-find-next)))
6812                 ;; No more articles.
6813                 (setq found t)
6814               ;; Select the next article and adjust point.
6815               (unless (gnus-summary-article-sparse-p
6816                        (gnus-summary-article-number))
6817                 (setq found nil)
6818                 (gnus-summary-select-article)
6819                 (set-buffer gnus-article-buffer)
6820                 (widen)
6821                 (goto-char (if backward (point-max) (point-min))))))))
6822       (gnus-message 7 ""))
6823     ;; Return whether we found the regexp.
6824     (when (eq found 'found)
6825       (goto-char point)
6826       (gnus-summary-show-thread)
6827       (gnus-summary-goto-subject gnus-current-article)
6828       (gnus-summary-position-point)
6829       t)))
6830
6831 (defun gnus-summary-find-matching (header regexp &optional backward unread
6832                                           not-case-fold)
6833   "Return a list of all articles that match REGEXP on HEADER.
6834 The search stars on the current article and goes forwards unless
6835 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6836 If UNREAD is non-nil, only unread articles will
6837 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6838 in the comparisons."
6839   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6840                 (gnus-data-find-list
6841                  (gnus-summary-article-number) (gnus-data-list backward))))
6842         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6843         (case-fold-search (not not-case-fold))
6844         articles d)
6845     (unless (fboundp (intern (concat "mail-header-" header)))
6846       (error "%s is not a valid header" header))
6847     (while data
6848       (setq d (car data))
6849       (and (or (not unread)             ; We want all articles...
6850                (gnus-data-unread-p d))  ; Or just unreads.
6851            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6852            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6853            (push (gnus-data-number d) articles)) ; Success!
6854       (setq data (cdr data)))
6855     (nreverse articles)))
6856
6857 (defun gnus-summary-execute-command (header regexp command &optional backward)
6858   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6859 If HEADER is an empty string (or nil), the match is done on the entire
6860 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6861   (interactive
6862    (list (let ((completion-ignore-case t))
6863            (completing-read
6864             "Header name: "
6865             (mapcar (lambda (string) (list string))
6866                     '("Number" "Subject" "From" "Lines" "Date"
6867                       "Message-ID" "Xref" "References" "Body"))
6868             nil 'require-match))
6869          (read-string "Regexp: ")
6870          (read-key-sequence "Command: ")
6871          current-prefix-arg))
6872   (when (equal header "Body")
6873     (setq header ""))
6874   ;; Hidden thread subtrees must be searched as well.
6875   (gnus-summary-show-all-threads)
6876   ;; We don't want to change current point nor window configuration.
6877   (save-excursion
6878     (save-window-excursion
6879       (gnus-message 6 "Executing %s..." (key-description command))
6880       ;; We'd like to execute COMMAND interactively so as to give arguments.
6881       (gnus-execute header regexp
6882                     `(call-interactively ',(key-binding command))
6883                     backward)
6884       (gnus-message 6 "Executing %s...done" (key-description command)))))
6885
6886 (defun gnus-summary-beginning-of-article ()
6887   "Scroll the article back to the beginning."
6888   (interactive)
6889   (gnus-summary-select-article)
6890   (gnus-configure-windows 'article)
6891   (gnus-eval-in-buffer-window gnus-article-buffer
6892     (widen)
6893     (goto-char (point-min))
6894     (when gnus-page-broken
6895       (gnus-narrow-to-page))))
6896
6897 (defun gnus-summary-end-of-article ()
6898   "Scroll to the end of the article."
6899   (interactive)
6900   (gnus-summary-select-article)
6901   (gnus-configure-windows 'article)
6902   (gnus-eval-in-buffer-window gnus-article-buffer
6903     (widen)
6904     (goto-char (point-max))
6905     (recenter -3)
6906     (when gnus-page-broken
6907       (gnus-narrow-to-page))))
6908
6909 (defun gnus-summary-print-article (&optional filename n)
6910   "Generate and print a PostScript image of the N next (mail) articles.
6911
6912 If N is negative, print the N previous articles.  If N is nil and articles
6913 have been marked with the process mark, print these instead.
6914
6915 If the optional second argument FILENAME is nil, send the image to the
6916 printer.  If FILENAME is a string, save the PostScript image in a file with
6917 that name.  If FILENAME is a number, prompt the user for the name of the file
6918 to save in."
6919   (interactive (list (ps-print-preprint current-prefix-arg)
6920                      current-prefix-arg))
6921   (dolist (article (gnus-summary-work-articles n))
6922     (gnus-summary-select-article nil nil 'pseudo article)
6923     (gnus-eval-in-buffer-window gnus-article-buffer
6924       (let ((buffer (generate-new-buffer " *print*")))
6925         (unwind-protect
6926             (progn
6927               (copy-to-buffer buffer (point-min) (point-max))
6928               (set-buffer buffer)
6929               (gnus-article-delete-invisible-text)
6930               (let ((ps-left-header
6931                      (list
6932                       (concat "("
6933                               (mail-header-subject gnus-current-headers) ")")
6934                       (concat "("
6935                               (mail-header-from gnus-current-headers) ")")))
6936                     (ps-right-header
6937                      (list
6938                       "/pagenumberstring load"
6939                       (concat "("
6940                               (mail-header-date gnus-current-headers) ")"))))
6941                 (gnus-run-hooks 'gnus-ps-print-hook)
6942                 (save-excursion
6943                   (ps-print-buffer-with-faces filename))))
6944           (kill-buffer buffer))))))
6945
6946 (defun gnus-summary-show-article (&optional arg)
6947   "Force re-fetching of the current article.
6948 If ARG (the prefix) is non-nil, show the raw article without any
6949 article massaging functions being run."
6950   (interactive "P")
6951   (if (not arg)
6952       ;; Select the article the normal way.
6953       (gnus-summary-select-article nil 'force)
6954     ;; We have to require this here to make sure that the following
6955     ;; dynamic binding isn't shadowed by autoloading.
6956     (require 'gnus-async)
6957     (require 'gnus-art)
6958     ;; Bind the article treatment functions to nil.
6959     (let ((gnus-have-all-headers t)
6960           gnus-article-display-hook
6961           gnus-article-prepare-hook
6962           gnus-article-decode-hook
6963           gnus-break-pages
6964           gnus-show-mime
6965           gnus-visual)
6966       (gnus-summary-select-article nil 'force)))
6967   (gnus-summary-goto-subject gnus-current-article)
6968   (gnus-summary-position-point))
6969
6970 (defun gnus-summary-verbose-headers (&optional arg)
6971   "Toggle permanent full header display.
6972 If ARG is a positive number, turn header display on.
6973 If ARG is a negative number, turn header display off."
6974   (interactive "P")
6975   (setq gnus-show-all-headers
6976         (cond ((or (not (numberp arg))
6977                    (zerop arg))
6978                (not gnus-show-all-headers))
6979               ((natnump arg)
6980                t)))
6981   (gnus-summary-show-article))
6982
6983 (defun gnus-summary-toggle-header (&optional arg)
6984   "Show the headers if they are hidden, or hide them if they are shown.
6985 If ARG is a positive number, show the entire header.
6986 If ARG is a negative number, hide the unwanted header lines."
6987   (interactive "P")
6988   (save-excursion
6989     (set-buffer gnus-article-buffer)
6990     (let* ((buffer-read-only nil)
6991            (inhibit-point-motion-hooks t)
6992            (hidden (text-property-any
6993                     (goto-char (point-min)) (search-forward "\n\n")
6994                     'invisible t))
6995            e)
6996       (goto-char (point-min))
6997       (when (search-forward "\n\n" nil t)
6998         (delete-region (point-min) (1- (point))))
6999       (goto-char (point-min))
7000       (save-excursion
7001         (set-buffer gnus-original-article-buffer)
7002         (goto-char (point-min))
7003         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7004       (insert-buffer-substring gnus-original-article-buffer 1 e)
7005       (let ((article-inhibit-hiding t))
7006         (gnus-run-hooks 'gnus-article-display-hook))
7007       (if (or (not hidden) (and (numberp arg) (< arg 0)))
7008           (let ((gnus-treat-hide-headers nil)
7009                 (gnus-treat-hide-boring-headers nil))
7010             (gnus-treat-article 'head))
7011         (gnus-treat-article 'head)))))
7012
7013 (defun gnus-summary-show-all-headers ()
7014   "Make all header lines visible."
7015   (interactive)
7016   (gnus-article-show-all-headers))
7017
7018 (defun gnus-summary-toggle-mime (&optional arg)
7019   "Toggle MIME processing.
7020 If ARG is a positive number, turn MIME processing on."
7021   (interactive "P")
7022   (setq gnus-show-mime
7023         (if (null arg)
7024             (not gnus-show-mime)
7025           (> (prefix-numeric-value arg) 0)))
7026   (gnus-summary-select-article t 'force))
7027
7028 (defun gnus-summary-caesar-message (&optional arg)
7029   "Caesar rotate the current article by 13.
7030 The numerical prefix specifies how many places to rotate each letter
7031 forward."
7032   (interactive "P")
7033   (gnus-summary-select-article)
7034   (let ((mail-header-separator ""))
7035     (gnus-eval-in-buffer-window gnus-article-buffer
7036       (save-restriction
7037         (widen)
7038         (let ((start (window-start))
7039               buffer-read-only)
7040           (message-caesar-buffer-body arg)
7041           (set-window-start (get-buffer-window (current-buffer)) start))))))
7042
7043 (defun gnus-summary-stop-page-breaking ()
7044   "Stop page breaking in the current article."
7045   (interactive)
7046   (gnus-summary-select-article)
7047   (gnus-eval-in-buffer-window gnus-article-buffer
7048     (widen)
7049     (when (gnus-visual-p 'page-marker)
7050       (let ((buffer-read-only nil))
7051         (gnus-remove-text-with-property 'gnus-prev)
7052         (gnus-remove-text-with-property 'gnus-next))
7053       (setq gnus-page-broken nil))))
7054
7055 (defun gnus-summary-move-article (&optional n to-newsgroup
7056                                             select-method action)
7057   "Move the current article to a different newsgroup.
7058 If N is a positive number, move the N next articles.
7059 If N is a negative number, move the N previous articles.
7060 If N is nil and any articles have been marked with the process mark,
7061 move those articles instead.
7062 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7063 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7064 re-spool using this method.
7065
7066 For this function to work, both the current newsgroup and the
7067 newsgroup that you want to move to have to support the `request-move'
7068 and `request-accept' functions."
7069   (interactive "P")
7070   (unless action
7071     (setq action 'move))
7072   ;; Disable marking as read.
7073   (let (gnus-mark-article-hook)
7074     (save-window-excursion
7075       (gnus-summary-select-article)))
7076   ;; Check whether the source group supports the required functions.
7077   (cond ((and (eq action 'move)
7078               (not (gnus-check-backend-function
7079                     'request-move-article gnus-newsgroup-name)))
7080          (error "The current group does not support article moving"))
7081         ((and (eq action 'crosspost)
7082               (not (gnus-check-backend-function
7083                     'request-replace-article gnus-newsgroup-name)))
7084          (error "The current group does not support article editing")))
7085   (let ((articles (gnus-summary-work-articles n))
7086         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7087         (names '((move "Move" "Moving")
7088                  (copy "Copy" "Copying")
7089                  (crosspost "Crosspost" "Crossposting")))
7090         (copy-buf (save-excursion
7091                     (nnheader-set-temp-buffer " *copy article*")))
7092         art-group to-method new-xref article to-groups)
7093     (unless (assq action names)
7094       (error "Unknown action %s" action))
7095     ;; Read the newsgroup name.
7096     (when (and (not to-newsgroup)
7097                (not select-method))
7098       (setq to-newsgroup
7099             (gnus-read-move-group-name
7100              (cadr (assq action names))
7101              (symbol-value (intern (format "gnus-current-%s-group" action)))
7102              articles prefix))
7103       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7104     (setq to-method (or select-method
7105                         (gnus-group-name-to-method to-newsgroup)))
7106     ;; Check the method we are to move this article to...
7107     (unless (gnus-check-backend-function
7108              'request-accept-article (car to-method))
7109       (error "%s does not support article copying" (car to-method)))
7110     (unless (gnus-check-server to-method)
7111       (error "Can't open server %s" (car to-method)))
7112     (gnus-message 6 "%s to %s: %s..."
7113                   (caddr (assq action names))
7114                   (or (car select-method) to-newsgroup) articles)
7115     (while articles
7116       (setq article (pop articles))
7117       (setq
7118        art-group
7119        (cond
7120         ;; Move the article.
7121         ((eq action 'move)
7122          ;; Remove this article from future suppression.
7123          (gnus-dup-unsuppress-article article)
7124          (gnus-request-move-article
7125           article                       ; Article to move
7126           gnus-newsgroup-name           ; From newsgroup
7127           (nth 1 (gnus-find-method-for-group
7128                   gnus-newsgroup-name)) ; Server
7129           (list 'gnus-request-accept-article
7130                 to-newsgroup (list 'quote select-method)
7131                 (not articles) t)               ; Accept form
7132           (not articles)))              ; Only save nov last time
7133         ;; Copy the article.
7134         ((eq action 'copy)
7135          (save-excursion
7136            (set-buffer copy-buf)
7137            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7138              (gnus-request-accept-article
7139               to-newsgroup select-method (not articles) t))))
7140         ;; Crosspost the article.
7141         ((eq action 'crosspost)
7142          (let ((xref (message-tokenize-header
7143                       (mail-header-xref (gnus-summary-article-header article))
7144                       " ")))
7145            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7146                                   ":" article))
7147            (unless xref
7148              (setq xref (list (system-name))))
7149            (setq new-xref
7150                  (concat
7151                   (mapconcat 'identity
7152                              (delete "Xref:" (delete new-xref xref))
7153                              " ")
7154                   " " new-xref))
7155            (save-excursion
7156              (set-buffer copy-buf)
7157              ;; First put the article in the destination group.
7158              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7159              (when (consp (setq art-group
7160                                 (gnus-request-accept-article
7161                                  to-newsgroup select-method (not articles))))
7162                (setq new-xref (concat new-xref " " (car art-group)
7163                                       ":" (cdr art-group)))
7164                ;; Now we have the new Xrefs header, so we insert
7165                ;; it and replace the new article.
7166                (nnheader-replace-header "Xref" new-xref)
7167                (gnus-request-replace-article
7168                 (cdr art-group) to-newsgroup (current-buffer))
7169                art-group))))))
7170       (cond
7171        ((not art-group)
7172         (gnus-message 1 "Couldn't %s article %s"
7173                       (cadr (assq action names)) article))
7174        ((and (eq art-group 'junk)
7175              (eq action 'move))
7176         (gnus-summary-mark-article article gnus-canceled-mark)
7177         (gnus-message 4 "Deleted article %s" article))
7178        (t
7179         (let* ((pto-group (gnus-group-prefixed-name
7180                            (car art-group) to-method))
7181                (entry
7182                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7183                (info (nth 2 entry))
7184                (to-group (gnus-info-group info)))
7185           ;; Update the group that has been moved to.
7186           (when (and info
7187                      (memq action '(move copy)))
7188             (unless (member to-group to-groups)
7189               (push to-group to-groups))
7190
7191             (unless (memq article gnus-newsgroup-unreads)
7192               (gnus-info-set-read
7193                info (gnus-add-to-range (gnus-info-read info)
7194                                        (list (cdr art-group)))))
7195
7196             ;; Copy any marks over to the new group.
7197             (let ((marks gnus-article-mark-lists)
7198                   (to-article (cdr art-group)))
7199
7200               ;; See whether the article is to be put in the cache.
7201               (when gnus-use-cache
7202                 (gnus-cache-possibly-enter-article
7203                  to-group to-article
7204                  (let ((header (copy-sequence
7205                                 (gnus-summary-article-header article))))
7206                    (mail-header-set-number header to-article)
7207                    header)
7208                  (memq article gnus-newsgroup-marked)
7209                  (memq article gnus-newsgroup-dormant)
7210                  (memq article gnus-newsgroup-unreads)))
7211
7212               (when (and (equal to-group gnus-newsgroup-name)
7213                          (not (memq article gnus-newsgroup-unreads)))
7214                 ;; Mark this article as read in this group.
7215                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7216                 (setcdr (gnus-active to-group) to-article)
7217                 (setcdr gnus-newsgroup-active to-article))
7218
7219               (while marks
7220                 (when (memq article (symbol-value
7221                                      (intern (format "gnus-newsgroup-%s"
7222                                                      (caar marks)))))
7223                   ;; If the other group is the same as this group,
7224                   ;; then we have to add the mark to the list.
7225                   (when (equal to-group gnus-newsgroup-name)
7226                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7227                          (cons to-article
7228                                (symbol-value
7229                                 (intern (format "gnus-newsgroup-%s"
7230                                                 (caar marks)))))))
7231                   ;; Copy the marks to other group.
7232                   (gnus-add-marked-articles
7233                    to-group (cdar marks) (list to-article) info))
7234                 (setq marks (cdr marks)))
7235
7236               (gnus-dribble-enter
7237                (concat "(gnus-group-set-info '"
7238                        (gnus-prin1-to-string (gnus-get-info to-group))
7239                        ")"))))
7240
7241           ;; Update the Xref header in this article to point to
7242           ;; the new crossposted article we have just created.
7243           (when (eq action 'crosspost)
7244             (save-excursion
7245               (set-buffer copy-buf)
7246               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7247               (nnheader-replace-header "Xref" new-xref)
7248               (gnus-request-replace-article
7249                article gnus-newsgroup-name (current-buffer)))))
7250
7251         ;;;!!!Why is this necessary?
7252         (set-buffer gnus-summary-buffer)
7253
7254         (gnus-summary-goto-subject article)
7255         (when (eq action 'move)
7256           (gnus-summary-mark-article article gnus-canceled-mark))))
7257       (gnus-summary-remove-process-mark article))
7258     ;; Re-activate all groups that have been moved to.
7259     (while to-groups
7260       (save-excursion
7261         (set-buffer gnus-group-buffer)
7262         (when (gnus-group-goto-group (car to-groups) t)
7263           (gnus-group-get-new-news-this-group 1 t))
7264         (pop to-groups)))
7265
7266     (gnus-kill-buffer copy-buf)
7267     (gnus-summary-position-point)
7268     (gnus-set-mode-line 'summary)))
7269
7270 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7271   "Move the current article to a different newsgroup.
7272 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7273 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7274 re-spool using this method."
7275   (interactive "P")
7276   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7277
7278 (defun gnus-summary-crosspost-article (&optional n)
7279   "Crosspost the current article to some other group."
7280   (interactive "P")
7281   (gnus-summary-move-article n nil nil 'crosspost))
7282
7283 (defcustom gnus-summary-respool-default-method nil
7284   "Default method for respooling an article.
7285 If nil, use to the current newsgroup method."
7286   :type '(choice (gnus-select-method :value (nnml ""))
7287                  (const nil))
7288   :group 'gnus-summary-mail)
7289
7290 (defun gnus-summary-respool-article (&optional n method)
7291   "Respool the current article.
7292 The article will be squeezed through the mail spooling process again,
7293 which means that it will be put in some mail newsgroup or other
7294 depending on `nnmail-split-methods'.
7295 If N is a positive number, respool the N next articles.
7296 If N is a negative number, respool the N previous articles.
7297 If N is nil and any articles have been marked with the process mark,
7298 respool those articles instead.
7299
7300 Respooling can be done both from mail groups and \"real\" newsgroups.
7301 In the former case, the articles in question will be moved from the
7302 current group into whatever groups they are destined to.  In the
7303 latter case, they will be copied into the relevant groups."
7304   (interactive
7305    (list current-prefix-arg
7306          (let* ((methods (gnus-methods-using 'respool))
7307                 (methname
7308                  (symbol-name (or gnus-summary-respool-default-method
7309                                   (car (gnus-find-method-for-group
7310                                         gnus-newsgroup-name)))))
7311                 (method
7312                  (gnus-completing-read
7313                   methname "What backend do you want to use when respooling?"
7314                   methods nil t nil 'gnus-mail-method-history))
7315                 ms)
7316            (cond
7317             ((zerop (length (setq ms (gnus-servers-using-backend
7318                                       (intern method)))))
7319              (list (intern method) ""))
7320             ((= 1 (length ms))
7321              (car ms))
7322             (t
7323              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7324                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7325                            ms-alist))))))))
7326   (unless method
7327     (error "No method given for respooling"))
7328   (if (assoc (symbol-name
7329               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7330              (gnus-methods-using 'respool))
7331       (gnus-summary-move-article n nil method)
7332     (gnus-summary-copy-article n nil method)))
7333
7334 (defun gnus-summary-import-article (file)
7335   "Import an arbitrary file into a mail newsgroup."
7336   (interactive "fImport file: ")
7337   (let ((group gnus-newsgroup-name)
7338         (now (current-time))
7339         atts lines)
7340     (unless (gnus-check-backend-function 'request-accept-article group)
7341       (error "%s does not support article importing" group))
7342     (or (file-readable-p file)
7343         (not (file-regular-p file))
7344         (error "Can't read %s" file))
7345     (save-excursion
7346       (set-buffer (gnus-get-buffer-create " *import file*"))
7347       (erase-buffer)
7348       (nnheader-insert-file-contents file)
7349       (goto-char (point-min))
7350       (unless (nnheader-article-p)
7351         ;; This doesn't look like an article, so we fudge some headers.
7352         (setq atts (file-attributes file)
7353               lines (count-lines (point-min) (point-max)))
7354         (insert "From: " (read-string "From: ") "\n"
7355                 "Subject: " (read-string "Subject: ") "\n"
7356                 "Date: " (message-make-date (nth 5 atts))
7357                 "\n"
7358                 "Message-ID: " (message-make-message-id) "\n"
7359                 "Lines: " (int-to-string lines) "\n"
7360                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7361       (gnus-request-accept-article group nil t)
7362       (kill-buffer (current-buffer)))))
7363
7364 (defun gnus-summary-article-posted-p ()
7365   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7366 This will be the case if the article has both been mailed and posted."
7367   (interactive)
7368   (let ((id (mail-header-references (gnus-summary-article-header)))
7369         (gnus-override-method
7370          (or gnus-refer-article-method gnus-select-method)))
7371     (if (gnus-request-head id "")
7372         (gnus-message 2 "The current message was found on %s"
7373                       gnus-override-method)
7374       (gnus-message 2 "The current message couldn't be found on %s"
7375                     gnus-override-method)
7376       nil)))
7377
7378 (defun gnus-summary-expire-articles (&optional now)
7379   "Expire all articles that are marked as expirable in the current group."
7380   (interactive)
7381   (when (gnus-check-backend-function
7382          'request-expire-articles gnus-newsgroup-name)
7383     ;; This backend supports expiry.
7384     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7385            (expirable (if total
7386                           (progn
7387                             ;; We need to update the info for
7388                             ;; this group for `gnus-list-of-read-articles'
7389                             ;; to give us the right answer.
7390                             (gnus-run-hooks 'gnus-exit-group-hook)
7391                             (gnus-summary-update-info)
7392                             (gnus-list-of-read-articles gnus-newsgroup-name))
7393                         (setq gnus-newsgroup-expirable
7394                               (sort gnus-newsgroup-expirable '<))))
7395            (expiry-wait (if now 'immediate
7396                           (gnus-group-find-parameter
7397                            gnus-newsgroup-name 'expiry-wait)))
7398            es)
7399       (when expirable
7400         ;; There are expirable articles in this group, so we run them
7401         ;; through the expiry process.
7402         (gnus-message 6 "Expiring articles...")
7403         ;; The list of articles that weren't expired is returned.
7404         (save-excursion
7405           (if expiry-wait
7406               (let ((nnmail-expiry-wait-function nil)
7407                     (nnmail-expiry-wait expiry-wait))
7408                 (setq es (gnus-request-expire-articles
7409                           expirable gnus-newsgroup-name)))
7410             (setq es (gnus-request-expire-articles
7411                       expirable gnus-newsgroup-name))))
7412         (unless total
7413           (setq gnus-newsgroup-expirable es))
7414         ;; We go through the old list of expirable, and mark all
7415         ;; really expired articles as nonexistent.
7416         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7417           (let ((gnus-use-cache nil))
7418             (while expirable
7419               (unless (memq (car expirable) es)
7420                 (when (gnus-data-find (car expirable))
7421                   (gnus-summary-mark-article
7422                    (car expirable) gnus-canceled-mark)))
7423               (setq expirable (cdr expirable)))))
7424         (gnus-message 6 "Expiring articles...done")))))
7425
7426 (defun gnus-summary-expire-articles-now ()
7427   "Expunge all expirable articles in the current group.
7428 This means that *all* articles that are marked as expirable will be
7429 deleted forever, right now."
7430   (interactive)
7431   (or gnus-expert-user
7432       (gnus-yes-or-no-p
7433        "Are you really, really, really sure you want to delete all these messages? ")
7434       (error "Phew!"))
7435   (gnus-summary-expire-articles t))
7436
7437 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7438 (defun gnus-summary-delete-article (&optional n)
7439   "Delete the N next (mail) articles.
7440 This command actually deletes articles.  This is not a marking
7441 command.  The article will disappear forever from your life, never to
7442 return.
7443 If N is negative, delete backwards.
7444 If N is nil and articles have been marked with the process mark,
7445 delete these instead."
7446   (interactive "P")
7447   (unless (gnus-check-backend-function 'request-expire-articles
7448                                        gnus-newsgroup-name)
7449     (error "The current newsgroup does not support article deletion"))
7450   ;; Compute the list of articles to delete.
7451   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7452         not-deleted)
7453     (if (and gnus-novice-user
7454              (not (gnus-yes-or-no-p
7455                    (format "Do you really want to delete %s forever? "
7456                            (if (> (length articles) 1)
7457                                (format "these %s articles" (length articles))
7458                              "this article")))))
7459         ()
7460       ;; Delete the articles.
7461       (setq not-deleted (gnus-request-expire-articles
7462                          articles gnus-newsgroup-name 'force))
7463       (while articles
7464         (gnus-summary-remove-process-mark (car articles))
7465         ;; The backend might not have been able to delete the article
7466         ;; after all.
7467         (unless (memq (car articles) not-deleted)
7468           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7469         (setq articles (cdr articles)))
7470       (when not-deleted
7471         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7472     (gnus-summary-position-point)
7473     (gnus-set-mode-line 'summary)
7474     not-deleted))
7475
7476 (defun gnus-summary-edit-article (&optional force)
7477   "Edit the current article.
7478 This will have permanent effect only in mail groups.
7479 If FORCE is non-nil, allow editing of articles even in read-only
7480 groups."
7481   (interactive "P")
7482   (save-excursion
7483     (set-buffer gnus-summary-buffer)
7484     (gnus-set-global-variables)
7485     (when (and (not force)
7486                (gnus-group-read-only-p))
7487       (error "The current newsgroup does not support article editing"))
7488     ;; Select article if needed.
7489     (unless (eq (gnus-summary-article-number)
7490                 gnus-current-article)
7491       (gnus-summary-select-article t))
7492     (gnus-article-date-original)
7493     (gnus-article-edit-article
7494      `(lambda (no-highlight)
7495         (gnus-summary-edit-article-done
7496          ,(or (mail-header-references gnus-current-headers) "")
7497          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7498
7499 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7500
7501 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7502                                                  no-highlight)
7503   "Make edits to the current article permanent."
7504   (interactive)
7505   ;; Replace the article.
7506   (let ((buf (current-buffer)))
7507     (with-temp-buffer
7508       (insert-buffer buf)
7509       (if (and (not read-only)
7510                (not (gnus-request-replace-article
7511                      (cdr gnus-article-current) (car gnus-article-current)
7512                      (current-buffer))))
7513           (error "Couldn't replace article")
7514         ;; Update the summary buffer.
7515         (if (and references
7516                  (equal (message-tokenize-header references " ")
7517                         (message-tokenize-header
7518                          (or (message-fetch-field "references") "") " ")))
7519             ;; We only have to update this line.
7520             (save-excursion
7521               (save-restriction
7522                 (message-narrow-to-head)
7523                 (let ((head (buffer-string))
7524                       header)
7525                   (with-temp-buffer
7526                     (insert (format "211 %d Article retrieved.\n"
7527                                     (cdr gnus-article-current)))
7528                     (insert head)
7529                     (insert ".\n")
7530                     (let ((nntp-server-buffer (current-buffer)))
7531                       (setq header (car (gnus-get-newsgroup-headers
7532                                          (save-excursion
7533                                            (set-buffer gnus-summary-buffer)
7534                                            gnus-newsgroup-dependencies)
7535                                          t))))
7536                     (save-excursion
7537                       (set-buffer gnus-summary-buffer)
7538                       (gnus-data-set-header
7539                        (gnus-data-find (cdr gnus-article-current))
7540                        header)
7541                       (gnus-summary-update-article-line
7542                        (cdr gnus-article-current) header))))))
7543           ;; Update threads.
7544           (set-buffer (or buffer gnus-summary-buffer))
7545           (gnus-summary-update-article (cdr gnus-article-current)))
7546         ;; Prettify the article buffer again.
7547         (unless no-highlight
7548           (save-excursion
7549             (set-buffer gnus-article-buffer)
7550             ;;;!!! Fix this -- article should be rehighlighted.
7551             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7552             (set-buffer gnus-original-article-buffer)
7553             (gnus-request-article
7554              (cdr gnus-article-current)
7555              (car gnus-article-current) (current-buffer))))
7556         ;; Prettify the summary buffer line.
7557         (when (gnus-visual-p 'summary-highlight 'highlight)
7558           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7559
7560 (defun gnus-summary-edit-wash (key)
7561   "Perform editing command KEY in the article buffer."
7562   (interactive
7563    (list
7564     (progn
7565       (message "%s" (concat (this-command-keys) "- "))
7566       (read-char))))
7567   (message "")
7568   (gnus-summary-edit-article)
7569   (execute-kbd-macro (concat (this-command-keys) key))
7570   (gnus-article-edit-done))
7571
7572 ;;; Respooling
7573
7574 (defun gnus-summary-respool-query (&optional silent trace)
7575   "Query where the respool algorithm would put this article."
7576   (interactive)
7577   (let (gnus-mark-article-hook)
7578     (gnus-summary-select-article)
7579     (save-excursion
7580       (set-buffer gnus-original-article-buffer)
7581       (save-restriction
7582         (message-narrow-to-head)
7583         (let ((groups (nnmail-article-group 'identity trace)))
7584           (unless silent
7585             (if groups
7586                 (message "This message would go to %s"
7587                          (mapconcat 'car groups ", "))
7588               (message "This message would go to no groups"))
7589             groups))))))
7590
7591 (defun gnus-summary-respool-trace ()
7592   "Trace where the respool algorithm would put this article.
7593 Display a buffer showing all fancy splitting patterns which matched."
7594   (interactive)
7595   (gnus-summary-respool-query nil t))
7596
7597 ;; Summary marking commands.
7598
7599 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7600   "Mark articles which has the same subject as read, and then select the next.
7601 If UNMARK is positive, remove any kind of mark.
7602 If UNMARK is negative, tick articles."
7603   (interactive "P")
7604   (when unmark
7605     (setq unmark (prefix-numeric-value unmark)))
7606   (let ((count
7607          (gnus-summary-mark-same-subject
7608           (gnus-summary-article-subject) unmark)))
7609     ;; Select next unread article.  If auto-select-same mode, should
7610     ;; select the first unread article.
7611     (gnus-summary-next-article t (and gnus-auto-select-same
7612                                       (gnus-summary-article-subject)))
7613     (gnus-message 7 "%d article%s marked as %s"
7614                   count (if (= count 1) " is" "s are")
7615                   (if unmark "unread" "read"))))
7616
7617 (defun gnus-summary-kill-same-subject (&optional unmark)
7618   "Mark articles which has the same subject as read.
7619 If UNMARK is positive, remove any kind of mark.
7620 If UNMARK is negative, tick articles."
7621   (interactive "P")
7622   (when unmark
7623     (setq unmark (prefix-numeric-value unmark)))
7624   (let ((count
7625          (gnus-summary-mark-same-subject
7626           (gnus-summary-article-subject) unmark)))
7627     ;; If marked as read, go to next unread subject.
7628     (when (null unmark)
7629       ;; Go to next unread subject.
7630       (gnus-summary-next-subject 1 t))
7631     (gnus-message 7 "%d articles are marked as %s"
7632                   count (if unmark "unread" "read"))))
7633
7634 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7635   "Mark articles with same SUBJECT as read, and return marked number.
7636 If optional argument UNMARK is positive, remove any kinds of marks.
7637 If optional argument UNMARK is negative, mark articles as unread instead."
7638   (let ((count 1))
7639     (save-excursion
7640       (cond
7641        ((null unmark)                   ; Mark as read.
7642         (while (and
7643                 (progn
7644                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7645                   (gnus-summary-show-thread) t)
7646                 (gnus-summary-find-subject subject))
7647           (setq count (1+ count))))
7648        ((> unmark 0)                    ; Tick.
7649         (while (and
7650                 (progn
7651                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7652                   (gnus-summary-show-thread) t)
7653                 (gnus-summary-find-subject subject))
7654           (setq count (1+ count))))
7655        (t                               ; Mark as unread.
7656         (while (and
7657                 (progn
7658                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7659                   (gnus-summary-show-thread) t)
7660                 (gnus-summary-find-subject subject))
7661           (setq count (1+ count)))))
7662       (gnus-set-mode-line 'summary)
7663       ;; Return the number of marked articles.
7664       count)))
7665
7666 (defun gnus-summary-mark-as-processable (n &optional unmark)
7667   "Set the process mark on the next N articles.
7668 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7669 the process mark instead.  The difference between N and the actual
7670 number of articles marked is returned."
7671   (interactive "p")
7672   (let ((backward (< n 0))
7673         (n (abs n)))
7674     (while (and
7675             (> n 0)
7676             (if unmark
7677                 (gnus-summary-remove-process-mark
7678                  (gnus-summary-article-number))
7679               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7680             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7681       (setq n (1- n)))
7682     (when (/= 0 n)
7683       (gnus-message 7 "No more articles"))
7684     (gnus-summary-recenter)
7685     (gnus-summary-position-point)
7686     n))
7687
7688 (defun gnus-summary-unmark-as-processable (n)
7689   "Remove the process mark from the next N articles.
7690 If N is negative, unmark backward instead.  The difference between N and
7691 the actual number of articles unmarked is returned."
7692   (interactive "p")
7693   (gnus-summary-mark-as-processable n t))
7694
7695 (defun gnus-summary-unmark-all-processable ()
7696   "Remove the process mark from all articles."
7697   (interactive)
7698   (save-excursion
7699     (while gnus-newsgroup-processable
7700       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7701   (gnus-summary-position-point))
7702
7703 (defun gnus-summary-mark-as-expirable (n)
7704   "Mark N articles forward as expirable.
7705 If N is negative, mark backward instead.  The difference between N and
7706 the actual number of articles marked is returned."
7707   (interactive "p")
7708   (gnus-summary-mark-forward n gnus-expirable-mark))
7709
7710 (defun gnus-summary-mark-article-as-replied (article)
7711   "Mark ARTICLE replied and update the summary line."
7712   (push article gnus-newsgroup-replied)
7713   (let ((buffer-read-only nil))
7714     (when (gnus-summary-goto-subject article nil t)
7715       (gnus-summary-update-secondary-mark article))))
7716
7717 (defun gnus-summary-set-bookmark (article)
7718   "Set a bookmark in current article."
7719   (interactive (list (gnus-summary-article-number)))
7720   (when (or (not (get-buffer gnus-article-buffer))
7721             (not gnus-current-article)
7722             (not gnus-article-current)
7723             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7724     (error "No current article selected"))
7725   ;; Remove old bookmark, if one exists.
7726   (let ((old (assq article gnus-newsgroup-bookmarks)))
7727     (when old
7728       (setq gnus-newsgroup-bookmarks
7729             (delq old gnus-newsgroup-bookmarks))))
7730   ;; Set the new bookmark, which is on the form
7731   ;; (article-number . line-number-in-body).
7732   (push
7733    (cons article
7734          (save-excursion
7735            (set-buffer gnus-article-buffer)
7736            (count-lines
7737             (min (point)
7738                  (save-excursion
7739                    (goto-char (point-min))
7740                    (search-forward "\n\n" nil t)
7741                    (point)))
7742             (point))))
7743    gnus-newsgroup-bookmarks)
7744   (gnus-message 6 "A bookmark has been added to the current article."))
7745
7746 (defun gnus-summary-remove-bookmark (article)
7747   "Remove the bookmark from the current article."
7748   (interactive (list (gnus-summary-article-number)))
7749   ;; Remove old bookmark, if one exists.
7750   (let ((old (assq article gnus-newsgroup-bookmarks)))
7751     (if old
7752         (progn
7753           (setq gnus-newsgroup-bookmarks
7754                 (delq old gnus-newsgroup-bookmarks))
7755           (gnus-message 6 "Removed bookmark."))
7756       (gnus-message 6 "No bookmark in current article."))))
7757
7758 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7759 (defun gnus-summary-mark-as-dormant (n)
7760   "Mark N articles forward as dormant.
7761 If N is negative, mark backward instead.  The difference between N and
7762 the actual number of articles marked is returned."
7763   (interactive "p")
7764   (gnus-summary-mark-forward n gnus-dormant-mark))
7765
7766 (defun gnus-summary-set-process-mark (article)
7767   "Set the process mark on ARTICLE and update the summary line."
7768   (setq gnus-newsgroup-processable
7769         (cons article
7770               (delq article gnus-newsgroup-processable)))
7771   (when (gnus-summary-goto-subject article)
7772     (gnus-summary-show-thread)
7773     (gnus-summary-goto-subject article)
7774     (gnus-summary-update-secondary-mark article)))
7775
7776 (defun gnus-summary-remove-process-mark (article)
7777   "Remove the process mark from ARTICLE and update the summary line."
7778   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7779   (when (gnus-summary-goto-subject article)
7780     (gnus-summary-show-thread)
7781     (gnus-summary-goto-subject article)
7782     (gnus-summary-update-secondary-mark article)))
7783
7784 (defun gnus-summary-set-saved-mark (article)
7785   "Set the process mark on ARTICLE and update the summary line."
7786   (push article gnus-newsgroup-saved)
7787   (when (gnus-summary-goto-subject article)
7788     (gnus-summary-update-secondary-mark article)))
7789
7790 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7791   "Mark N articles as read forwards.
7792 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7793 The difference between N and the actual number of articles marked is
7794 returned."
7795   (interactive "p")
7796   (gnus-summary-show-thread)
7797   (let ((backward (< n 0))
7798         (gnus-summary-goto-unread
7799          (and gnus-summary-goto-unread
7800               (not (eq gnus-summary-goto-unread 'never))
7801               (not (memq mark (list gnus-unread-mark
7802                                     gnus-ticked-mark gnus-dormant-mark)))))
7803         (n (abs n))
7804         (mark (or mark gnus-del-mark)))
7805     (while (and (> n 0)
7806                 (gnus-summary-mark-article nil mark no-expire)
7807                 (zerop (gnus-summary-next-subject
7808                         (if backward -1 1)
7809                         (and gnus-summary-goto-unread
7810                              (not (eq gnus-summary-goto-unread 'never)))
7811                         t)))
7812       (setq n (1- n)))
7813     (when (/= 0 n)
7814       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7815     (gnus-summary-recenter)
7816     (gnus-summary-position-point)
7817     (gnus-set-mode-line 'summary)
7818     n))
7819
7820 (defun gnus-summary-mark-article-as-read (mark)
7821   "Mark the current article quickly as read with MARK."
7822   (let ((article (gnus-summary-article-number)))
7823     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7824     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7825     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7826     (push (cons article mark) gnus-newsgroup-reads)
7827     ;; Possibly remove from cache, if that is used.
7828     (when gnus-use-cache
7829       (gnus-cache-enter-remove-article article))
7830     ;; Allow the backend to change the mark.
7831     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7832     ;; Check for auto-expiry.
7833     (when (and gnus-newsgroup-auto-expire
7834                (memq mark gnus-auto-expirable-marks))
7835       (setq mark gnus-expirable-mark)
7836       ;; Let the backend know about the mark change.
7837       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7838       (push article gnus-newsgroup-expirable))
7839     ;; Set the mark in the buffer.
7840     (gnus-summary-update-mark mark 'unread)
7841     t))
7842
7843 (defun gnus-summary-mark-article-as-unread (mark)
7844   "Mark the current article quickly as unread with MARK."
7845   (let* ((article (gnus-summary-article-number))
7846          (old-mark (gnus-summary-article-mark article)))
7847     ;; Allow the backend to change the mark.
7848     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7849     (if (eq mark old-mark)
7850         t
7851       (if (<= article 0)
7852           (progn
7853             (gnus-error 1 "Can't mark negative article numbers")
7854             nil)
7855         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7856         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7857         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7858         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7859         (cond ((= mark gnus-ticked-mark)
7860                (push article gnus-newsgroup-marked))
7861               ((= mark gnus-dormant-mark)
7862                (push article gnus-newsgroup-dormant))
7863               (t
7864                (push article gnus-newsgroup-unreads)))
7865         (gnus-pull article gnus-newsgroup-reads)
7866
7867         ;; See whether the article is to be put in the cache.
7868         (and gnus-use-cache
7869              (vectorp (gnus-summary-article-header article))
7870              (save-excursion
7871                (gnus-cache-possibly-enter-article
7872                 gnus-newsgroup-name article
7873                 (gnus-summary-article-header article)
7874                 (= mark gnus-ticked-mark)
7875                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7876
7877         ;; Fix the mark.
7878         (gnus-summary-update-mark mark 'unread)
7879         t))))
7880
7881 (defun gnus-summary-mark-article (&optional article mark no-expire)
7882   "Mark ARTICLE with MARK.  MARK can be any character.
7883 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7884 `??' (dormant) and `?E' (expirable).
7885 If MARK is nil, then the default character `?r' is used.
7886 If ARTICLE is nil, then the article on the current line will be
7887 marked."
7888   ;; The mark might be a string.
7889   (when (stringp mark)
7890     (setq mark (aref mark 0)))
7891   ;; If no mark is given, then we check auto-expiring.
7892   (when (null mark)
7893     (setq mark gnus-del-mark))
7894   (when (and (not no-expire)
7895              gnus-newsgroup-auto-expire
7896              (memq mark gnus-auto-expirable-marks))
7897     (setq mark gnus-expirable-mark))
7898   (let ((article (or article (gnus-summary-article-number)))
7899         (old-mark (gnus-summary-article-mark article)))
7900     ;; Allow the backend to change the mark.
7901     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7902     (if (eq mark old-mark)
7903         t
7904       (unless article
7905         (error "No article on current line"))
7906       (if (not (if (or (= mark gnus-unread-mark)
7907                        (= mark gnus-ticked-mark)
7908                        (= mark gnus-dormant-mark))
7909                    (gnus-mark-article-as-unread article mark)
7910                  (gnus-mark-article-as-read article mark)))
7911           t
7912         ;; See whether the article is to be put in the cache.
7913         (and gnus-use-cache
7914              (not (= mark gnus-canceled-mark))
7915              (vectorp (gnus-summary-article-header article))
7916              (save-excursion
7917                (gnus-cache-possibly-enter-article
7918                 gnus-newsgroup-name article
7919                 (gnus-summary-article-header article)
7920                 (= mark gnus-ticked-mark)
7921                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7922
7923         (when (gnus-summary-goto-subject article nil t)
7924           (let ((buffer-read-only nil))
7925             (gnus-summary-show-thread)
7926             ;; Fix the mark.
7927             (gnus-summary-update-mark mark 'unread)
7928             t))))))
7929
7930 (defun gnus-summary-update-secondary-mark (article)
7931   "Update the secondary (read, process, cache) mark."
7932   (gnus-summary-update-mark
7933    (cond ((memq article gnus-newsgroup-processable)
7934           gnus-process-mark)
7935          ((memq article gnus-newsgroup-cached)
7936           gnus-cached-mark)
7937          ((memq article gnus-newsgroup-replied)
7938           gnus-replied-mark)
7939          ((memq article gnus-newsgroup-saved)
7940           gnus-saved-mark)
7941          (t gnus-unread-mark))
7942    'replied)
7943   (when (gnus-visual-p 'summary-highlight 'highlight)
7944     (gnus-run-hooks 'gnus-summary-update-hook))
7945   t)
7946
7947 (defun gnus-summary-update-mark (mark type)
7948   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7949         (buffer-read-only nil))
7950     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7951     (when forward
7952       (when (looking-at "\r")
7953         (incf forward))
7954       (when (<= (+ forward (point)) (point-max))
7955         ;; Go to the right position on the line.
7956         (goto-char (+ forward (point)))
7957         ;; Replace the old mark with the new mark.
7958         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7959         ;; Optionally update the marks by some user rule.
7960         (when (eq type 'unread)
7961           (gnus-data-set-mark
7962            (gnus-data-find (gnus-summary-article-number)) mark)
7963           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7964
7965 (defun gnus-mark-article-as-read (article &optional mark)
7966   "Enter ARTICLE in the pertinent lists and remove it from others."
7967   ;; Make the article expirable.
7968   (let ((mark (or mark gnus-del-mark)))
7969     (if (= mark gnus-expirable-mark)
7970         (push article gnus-newsgroup-expirable)
7971       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7972     ;; Remove from unread and marked lists.
7973     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7974     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7975     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7976     (push (cons article mark) gnus-newsgroup-reads)
7977     ;; Possibly remove from cache, if that is used.
7978     (when gnus-use-cache
7979       (gnus-cache-enter-remove-article article))
7980     t))
7981
7982 (defun gnus-mark-article-as-unread (article &optional mark)
7983   "Enter ARTICLE in the pertinent lists and remove it from others."
7984   (let ((mark (or mark gnus-ticked-mark)))
7985     (if (<= article 0)
7986         (progn
7987           (gnus-error 1 "Can't mark negative article numbers")
7988           nil)
7989       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7990             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7991             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7992             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7993
7994       ;; Unsuppress duplicates?
7995       (when gnus-suppress-duplicates
7996         (gnus-dup-unsuppress-article article))
7997
7998       (cond ((= mark gnus-ticked-mark)
7999              (push article gnus-newsgroup-marked))
8000             ((= mark gnus-dormant-mark)
8001              (push article gnus-newsgroup-dormant))
8002             (t
8003              (push article gnus-newsgroup-unreads)))
8004       (gnus-pull article gnus-newsgroup-reads)
8005       t)))
8006
8007 (defalias 'gnus-summary-mark-as-unread-forward
8008   'gnus-summary-tick-article-forward)
8009 (make-obsolete 'gnus-summary-mark-as-unread-forward
8010                'gnus-summary-tick-article-forward)
8011 (defun gnus-summary-tick-article-forward (n)
8012   "Tick N articles forwards.
8013 If N is negative, tick backwards instead.
8014 The difference between N and the number of articles ticked is returned."
8015   (interactive "p")
8016   (gnus-summary-mark-forward n gnus-ticked-mark))
8017
8018 (defalias 'gnus-summary-mark-as-unread-backward
8019   'gnus-summary-tick-article-backward)
8020 (make-obsolete 'gnus-summary-mark-as-unread-backward
8021                'gnus-summary-tick-article-backward)
8022 (defun gnus-summary-tick-article-backward (n)
8023   "Tick N articles backwards.
8024 The difference between N and the number of articles ticked is returned."
8025   (interactive "p")
8026   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8027
8028 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8029 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8030 (defun gnus-summary-tick-article (&optional article clear-mark)
8031   "Mark current article as unread.
8032 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8033 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8034   (interactive)
8035   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8036                                        gnus-ticked-mark)))
8037
8038 (defun gnus-summary-mark-as-read-forward (n)
8039   "Mark N articles as read forwards.
8040 If N is negative, mark backwards instead.
8041 The difference between N and the actual number of articles marked is
8042 returned."
8043   (interactive "p")
8044   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8045
8046 (defun gnus-summary-mark-as-read-backward (n)
8047   "Mark the N articles as read backwards.
8048 The difference between N and the actual number of articles marked is
8049 returned."
8050   (interactive "p")
8051   (gnus-summary-mark-forward (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8052
8053 (defun gnus-summary-mark-as-read (&optional article mark)
8054   "Mark current article as read.
8055 ARTICLE specifies the article to be marked as read.
8056 MARK specifies a string to be inserted at the beginning of the line."
8057   (gnus-summary-mark-article article mark))
8058
8059 (defun gnus-summary-clear-mark-forward (n)
8060   "Clear marks from N articles forward.
8061 If N is negative, clear backward instead.
8062 The difference between N and the number of marks cleared is returned."
8063   (interactive "p")
8064   (gnus-summary-mark-forward n gnus-unread-mark))
8065
8066 (defun gnus-summary-clear-mark-backward (n)
8067   "Clear marks from N articles backward.
8068 The difference between N and the number of marks cleared is returned."
8069   (interactive "p")
8070   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8071
8072 (defun gnus-summary-mark-unread-as-read ()
8073   "Intended to be used by `gnus-summary-mark-article-hook'."
8074   (when (memq gnus-current-article gnus-newsgroup-unreads)
8075     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8076
8077 (defun gnus-summary-mark-read-and-unread-as-read ()
8078   "Intended to be used by `gnus-summary-mark-article-hook'."
8079   (let ((mark (gnus-summary-article-mark)))
8080     (when (or (gnus-unread-mark-p mark)
8081               (gnus-read-mark-p mark))
8082       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8083
8084 (defun gnus-summary-mark-region-as-read (point mark all)
8085   "Mark all unread articles between point and mark as read.
8086 If given a prefix, mark all articles between point and mark as read,
8087 even ticked and dormant ones."
8088   (interactive "r\nP")
8089   (save-excursion
8090     (let (article)
8091       (goto-char point)
8092       (beginning-of-line)
8093       (while (and
8094               (< (point) mark)
8095               (progn
8096                 (when (or all
8097                           (memq (setq article (gnus-summary-article-number))
8098                                 gnus-newsgroup-unreads))
8099                   (gnus-summary-mark-article article gnus-del-mark))
8100                 t)
8101               (gnus-summary-find-next))))))
8102
8103 (defun gnus-summary-mark-below (score mark)
8104   "Mark articles with score less than SCORE with MARK."
8105   (interactive "P\ncMark: ")
8106   (setq score (if score
8107                   (prefix-numeric-value score)
8108                 (or gnus-summary-default-score 0)))
8109   (save-excursion
8110     (set-buffer gnus-summary-buffer)
8111     (goto-char (point-min))
8112     (while
8113         (progn
8114           (and (< (gnus-summary-article-score) score)
8115                (gnus-summary-mark-article nil mark))
8116           (gnus-summary-find-next)))))
8117
8118 (defun gnus-summary-kill-below (&optional score)
8119   "Mark articles with score below SCORE as read."
8120   (interactive "P")
8121   (gnus-summary-mark-below score gnus-killed-mark))
8122
8123 (defun gnus-summary-clear-above (&optional score)
8124   "Clear all marks from articles with score above SCORE."
8125   (interactive "P")
8126   (gnus-summary-mark-above score gnus-unread-mark))
8127
8128 (defun gnus-summary-tick-above (&optional score)
8129   "Tick all articles with score above SCORE."
8130   (interactive "P")
8131   (gnus-summary-mark-above score gnus-ticked-mark))
8132
8133 (defun gnus-summary-mark-above (score mark)
8134   "Mark articles with score over SCORE with MARK."
8135   (interactive "P\ncMark: ")
8136   (setq score (if score
8137                   (prefix-numeric-value score)
8138                 (or gnus-summary-default-score 0)))
8139   (save-excursion
8140     (set-buffer gnus-summary-buffer)
8141     (goto-char (point-min))
8142     (while (and (progn
8143                   (when (> (gnus-summary-article-score) score)
8144                     (gnus-summary-mark-article nil mark))
8145                   t)
8146                 (gnus-summary-find-next)))))
8147
8148 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8149 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8150 (defun gnus-summary-limit-include-expunged (&optional no-error)
8151   "Display all the hidden articles that were expunged for low scores."
8152   (interactive)
8153   (let ((buffer-read-only nil))
8154     (let ((scored gnus-newsgroup-scored)
8155           headers h)
8156       (while scored
8157         (unless (gnus-summary-goto-subject (caar scored))
8158           (and (setq h (gnus-summary-article-header (caar scored)))
8159                (< (cdar scored) gnus-summary-expunge-below)
8160                (push h headers)))
8161         (setq scored (cdr scored)))
8162       (if (not headers)
8163           (when (not no-error)
8164             (error "No expunged articles hidden"))
8165         (goto-char (point-min))
8166         (gnus-summary-prepare-unthreaded (nreverse headers))
8167         (goto-char (point-min))
8168         (gnus-summary-position-point)
8169         t))))
8170
8171 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8172   "Mark all unread articles in this newsgroup as read.
8173 If prefix argument ALL is non-nil, ticked and dormant articles will
8174 also be marked as read.
8175 If QUIETLY is non-nil, no questions will be asked.
8176 If TO-HERE is non-nil, it should be a point in the buffer.  All
8177 articles before this point will be marked as read.
8178 Note that this function will only catch up the unread article
8179 in the current summary buffer limitation.
8180 The number of articles marked as read is returned."
8181   (interactive "P")
8182   (prog1
8183       (save-excursion
8184         (when (or quietly
8185                   (not gnus-interactive-catchup) ;Without confirmation?
8186                   gnus-expert-user
8187                   (gnus-y-or-n-p
8188                    (if all
8189                        "Mark absolutely all articles as read? "
8190                      "Mark all unread articles as read? ")))
8191           (if (and not-mark
8192                    (not gnus-newsgroup-adaptive)
8193                    (not gnus-newsgroup-auto-expire)
8194                    (not gnus-suppress-duplicates)
8195                    (or (not gnus-use-cache)
8196                        (eq gnus-use-cache 'passive)))
8197               (progn
8198                 (when all
8199                   (setq gnus-newsgroup-marked nil
8200                         gnus-newsgroup-dormant nil))
8201                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8202             ;; We actually mark all articles as canceled, which we
8203             ;; have to do when using auto-expiry or adaptive scoring.
8204             (gnus-summary-show-all-threads)
8205             (when (gnus-summary-first-subject (not all) t)
8206               (while (and
8207                       (if to-here (< (point) to-here) t)
8208                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8209                       (gnus-summary-find-next (not all) nil nil t))))
8210             (gnus-set-mode-line 'summary))
8211           t))
8212     (gnus-summary-position-point)))
8213
8214 (defun gnus-summary-catchup-to-here (&optional all)
8215   "Mark all unticked articles before the current one as read.
8216 If ALL is non-nil, also mark ticked and dormant articles as read."
8217   (interactive "P")
8218   (save-excursion
8219     (gnus-save-hidden-threads
8220       (let ((beg (point)))
8221         ;; We check that there are unread articles.
8222         (when (or all (gnus-summary-find-prev))
8223           (gnus-summary-catchup all t beg)))))
8224   (gnus-summary-position-point))
8225
8226 (defun gnus-summary-catchup-all (&optional quietly)
8227   "Mark all articles in this newsgroup as read."
8228   (interactive "P")
8229   (gnus-summary-catchup t quietly))
8230
8231 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8232   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8233 If prefix argument ALL is non-nil, all articles are marked as read."
8234   (interactive "P")
8235   (when (gnus-summary-catchup all quietly nil 'fast)
8236     ;; Select next newsgroup or exit.
8237     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8238              (eq gnus-auto-select-next 'quietly))
8239         (gnus-summary-next-group nil)
8240       (gnus-summary-exit))))
8241
8242 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8243   "Mark all articles in this newsgroup as read, and then exit."
8244   (interactive "P")
8245   (gnus-summary-catchup-and-exit t quietly))
8246
8247 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8248 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8249   "Mark all articles in this group as read and select the next group.
8250 If given a prefix, mark all articles, unread as well as ticked, as
8251 read."
8252   (interactive "P")
8253   (save-excursion
8254     (gnus-summary-catchup all))
8255   (gnus-summary-next-article t nil nil t))
8256
8257 ;; Thread-based commands.
8258
8259 (defun gnus-summary-articles-in-thread (&optional article)
8260   "Return a list of all articles in the current thread.
8261 If ARTICLE is non-nil, return all articles in the thread that starts
8262 with that article."
8263   (let* ((article (or article (gnus-summary-article-number)))
8264          (data (gnus-data-find-list article))
8265          (top-level (gnus-data-level (car data)))
8266          (top-subject
8267           (cond ((null gnus-thread-operation-ignore-subject)
8268                  (gnus-simplify-subject-re
8269                   (mail-header-subject (gnus-data-header (car data)))))
8270                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8271                  (gnus-simplify-subject-fuzzy
8272                   (mail-header-subject (gnus-data-header (car data)))))
8273                 (t nil)))
8274          (end-point (save-excursion
8275                       (if (gnus-summary-go-to-next-thread)
8276                           (point) (point-max))))
8277          articles)
8278     (while (and data
8279                 (< (gnus-data-pos (car data)) end-point))
8280       (when (or (not top-subject)
8281                 (string= top-subject
8282                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8283                              (gnus-simplify-subject-fuzzy
8284                               (mail-header-subject
8285                                (gnus-data-header (car data))))
8286                            (gnus-simplify-subject-re
8287                             (mail-header-subject
8288                              (gnus-data-header (car data)))))))
8289         (push (gnus-data-number (car data)) articles))
8290       (unless (and (setq data (cdr data))
8291                    (> (gnus-data-level (car data)) top-level))
8292         (setq data nil)))
8293     ;; Return the list of articles.
8294     (nreverse articles)))
8295
8296 (defun gnus-summary-rethread-current ()
8297   "Rethread the thread the current article is part of."
8298   (interactive)
8299   (let* ((gnus-show-threads t)
8300          (article (gnus-summary-article-number))
8301          (id (mail-header-id (gnus-summary-article-header)))
8302          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8303     (unless id
8304       (error "No article on the current line"))
8305     (gnus-rebuild-thread id)
8306     (gnus-summary-goto-subject article)))
8307
8308 (defun gnus-summary-reparent-thread ()
8309   "Make the current article child of the marked (or previous) article.
8310
8311 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8312 is non-nil or the Subject: of both articles are the same."
8313   (interactive)
8314   (unless (not (gnus-group-read-only-p))
8315     (error "The current newsgroup does not support article editing"))
8316   (unless (<= (length gnus-newsgroup-processable) 1)
8317     (error "No more than one article may be marked"))
8318   (save-window-excursion
8319     (let ((gnus-article-buffer " *reparent*")
8320           (current-article (gnus-summary-article-number))
8321           ;; First grab the marked article, otherwise one line up.
8322           (parent-article (if (not (null gnus-newsgroup-processable))
8323                               (car gnus-newsgroup-processable)
8324                             (save-excursion
8325                               (if (eq (forward-line -1) 0)
8326                                   (gnus-summary-article-number)
8327                                 (error "Beginning of summary buffer"))))))
8328       (unless (not (eq current-article parent-article))
8329         (error "An article may not be self-referential"))
8330       (let ((message-id (mail-header-id
8331                          (gnus-summary-article-header parent-article))))
8332         (unless (and message-id (not (equal message-id "")))
8333           (error "No message-id in desired parent"))
8334         (gnus-with-article current-article
8335           (goto-char (point-min))
8336           (if (re-search-forward "^References: " nil t)
8337               (progn
8338                 (re-search-forward "^[^ \t]" nil t)
8339                 (forward-line -1)
8340                 (end-of-line)
8341                 (insert " " message-id))
8342             (insert "References: " message-id "\n")))
8343         (set-buffer gnus-summary-buffer)
8344         (gnus-summary-unmark-all-processable)
8345         (gnus-summary-update-article current-article)
8346         (gnus-summary-rethread-current)
8347         (gnus-message 3 "Article %d is now the child of article %d"
8348                       current-article parent-article)))))
8349
8350 (defun gnus-summary-toggle-threads (&optional arg)
8351   "Toggle showing conversation threads.
8352 If ARG is positive number, turn showing conversation threads on."
8353   (interactive "P")
8354   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8355     (setq gnus-show-threads
8356           (if (null arg) (not gnus-show-threads)
8357             (> (prefix-numeric-value arg) 0)))
8358     (gnus-summary-prepare)
8359     (gnus-summary-goto-subject current)
8360     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8361     (gnus-summary-position-point)))
8362
8363 (defun gnus-summary-show-all-threads ()
8364   "Show all threads."
8365   (interactive)
8366   (save-excursion
8367     (let ((buffer-read-only nil))
8368       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8369   (gnus-summary-position-point))
8370
8371 (defun gnus-summary-show-thread ()
8372   "Show thread subtrees.
8373 Returns nil if no thread was there to be shown."
8374   (interactive)
8375   (let ((buffer-read-only nil)
8376         (orig (point))
8377         ;; first goto end then to beg, to have point at beg after let
8378         (end (progn (end-of-line) (point)))
8379         (beg (progn (beginning-of-line) (point))))
8380     (prog1
8381         ;; Any hidden lines here?
8382         (search-forward "\r" end t)
8383       (subst-char-in-region beg end ?\^M ?\n t)
8384       (goto-char orig)
8385       (gnus-summary-position-point))))
8386
8387 (defun gnus-summary-hide-all-threads ()
8388   "Hide all thread subtrees."
8389   (interactive)
8390   (save-excursion
8391     (goto-char (point-min))
8392     (gnus-summary-hide-thread)
8393     (while (zerop (gnus-summary-next-thread 1 t))
8394       (gnus-summary-hide-thread)))
8395   (gnus-summary-position-point))
8396
8397 (defun gnus-summary-hide-thread ()
8398   "Hide thread subtrees.
8399 Returns nil if no threads were there to be hidden."
8400   (interactive)
8401   (let ((buffer-read-only nil)
8402         (start (point))
8403         (article (gnus-summary-article-number)))
8404     (goto-char start)
8405     ;; Go forward until either the buffer ends or the subthread
8406     ;; ends.
8407     (when (and (not (eobp))
8408                (or (zerop (gnus-summary-next-thread 1 t))
8409                    (goto-char (point-max))))
8410       (prog1
8411           (if (and (> (point) start)
8412                    (search-backward "\n" start t))
8413               (progn
8414                 (subst-char-in-region start (point) ?\n ?\^M)
8415                 (gnus-summary-goto-subject article))
8416             (goto-char start)
8417             nil)
8418         ;;(gnus-summary-position-point)
8419         ))))
8420
8421 (defun gnus-summary-go-to-next-thread (&optional previous)
8422   "Go to the same level (or less) next thread.
8423 If PREVIOUS is non-nil, go to previous thread instead.
8424 Return the article number moved to, or nil if moving was impossible."
8425   (let ((level (gnus-summary-thread-level))
8426         (way (if previous -1 1))
8427         (beg (point)))
8428     (forward-line way)
8429     (while (and (not (eobp))
8430                 (< level (gnus-summary-thread-level)))
8431       (forward-line way))
8432     (if (eobp)
8433         (progn
8434           (goto-char beg)
8435           nil)
8436       (setq beg (point))
8437       (prog1
8438           (gnus-summary-article-number)
8439         (goto-char beg)))))
8440
8441 (defun gnus-summary-next-thread (n &optional silent)
8442   "Go to the same level next N'th thread.
8443 If N is negative, search backward instead.
8444 Returns the difference between N and the number of skips actually
8445 done.
8446
8447 If SILENT, don't output messages."
8448   (interactive "p")
8449   (let ((backward (< n 0))
8450         (n (abs n)))
8451     (while (and (> n 0)
8452                 (gnus-summary-go-to-next-thread backward))
8453       (decf n))
8454     (unless silent
8455       (gnus-summary-position-point))
8456     (when (and (not silent) (/= 0 n))
8457       (gnus-message 7 "No more threads"))
8458     n))
8459
8460 (defun gnus-summary-prev-thread (n)
8461   "Go to the same level previous N'th thread.
8462 Returns the difference between N and the number of skips actually
8463 done."
8464   (interactive "p")
8465   (gnus-summary-next-thread (- n)))
8466
8467 (defun gnus-summary-go-down-thread ()
8468   "Go down one level in the current thread."
8469   (let ((children (gnus-summary-article-children)))
8470     (when children
8471       (gnus-summary-goto-subject (car children)))))
8472
8473 (defun gnus-summary-go-up-thread ()
8474   "Go up one level in the current thread."
8475   (let ((parent (gnus-summary-article-parent)))
8476     (when parent
8477       (gnus-summary-goto-subject parent))))
8478
8479 (defun gnus-summary-down-thread (n)
8480   "Go down thread N steps.
8481 If N is negative, go up instead.
8482 Returns the difference between N and how many steps down that were
8483 taken."
8484   (interactive "p")
8485   (let ((up (< n 0))
8486         (n (abs n)))
8487     (while (and (> n 0)
8488                 (if up (gnus-summary-go-up-thread)
8489                   (gnus-summary-go-down-thread)))
8490       (setq n (1- n)))
8491     (gnus-summary-position-point)
8492     (when (/= 0 n)
8493       (gnus-message 7 "Can't go further"))
8494     n))
8495
8496 (defun gnus-summary-up-thread (n)
8497   "Go up thread N steps.
8498 If N is negative, go up instead.
8499 Returns the difference between N and how many steps down that were
8500 taken."
8501   (interactive "p")
8502   (gnus-summary-down-thread (- n)))
8503
8504 (defun gnus-summary-top-thread ()
8505   "Go to the top of the thread."
8506   (interactive)
8507   (while (gnus-summary-go-up-thread))
8508   (gnus-summary-article-number))
8509
8510 (defun gnus-summary-kill-thread (&optional unmark)
8511   "Mark articles under current thread as read.
8512 If the prefix argument is positive, remove any kinds of marks.
8513 If the prefix argument is negative, tick articles instead."
8514   (interactive "P")
8515   (when unmark
8516     (setq unmark (prefix-numeric-value unmark)))
8517   (let ((articles (gnus-summary-articles-in-thread)))
8518     (save-excursion
8519       ;; Expand the thread.
8520       (gnus-summary-show-thread)
8521       ;; Mark all the articles.
8522       (while articles
8523         (gnus-summary-goto-subject (car articles))
8524         (cond ((null unmark)
8525                (gnus-summary-mark-article-as-read gnus-killed-mark))
8526               ((> unmark 0)
8527                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8528               (t
8529                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8530         (setq articles (cdr articles))))
8531     ;; Hide killed subtrees.
8532     (and (null unmark)
8533          gnus-thread-hide-killed
8534          (gnus-summary-hide-thread))
8535     ;; If marked as read, go to next unread subject.
8536     (when (null unmark)
8537       ;; Go to next unread subject.
8538       (gnus-summary-next-subject 1 t)))
8539   (gnus-set-mode-line 'summary))
8540
8541 ;; Summary sorting commands
8542
8543 (defun gnus-summary-sort-by-number (&optional reverse)
8544   "Sort the summary buffer by article number.
8545 Argument REVERSE means reverse order."
8546   (interactive "P")
8547   (gnus-summary-sort 'number reverse))
8548
8549 (defun gnus-summary-sort-by-author (&optional reverse)
8550   "Sort the summary buffer by author name alphabetically.
8551 If case-fold-search is non-nil, case of letters is ignored.
8552 Argument REVERSE means reverse order."
8553   (interactive "P")
8554   (gnus-summary-sort 'author reverse))
8555
8556 (defun gnus-summary-sort-by-subject (&optional reverse)
8557   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8558 If case-fold-search is non-nil, case of letters is ignored.
8559 Argument REVERSE means reverse order."
8560   (interactive "P")
8561   (gnus-summary-sort 'subject reverse))
8562
8563 (defun gnus-summary-sort-by-date (&optional reverse)
8564   "Sort the summary buffer by date.
8565 Argument REVERSE means reverse order."
8566   (interactive "P")
8567   (gnus-summary-sort 'date reverse))
8568
8569 (defun gnus-summary-sort-by-score (&optional reverse)
8570   "Sort the summary buffer by score.
8571 Argument REVERSE means reverse order."
8572   (interactive "P")
8573   (gnus-summary-sort 'score reverse))
8574
8575 (defun gnus-summary-sort-by-lines (&optional reverse)
8576   "Sort the summary buffer by article length.
8577 Argument REVERSE means reverse order."
8578   (interactive "P")
8579   (gnus-summary-sort 'lines reverse))
8580
8581 (defun gnus-summary-sort (predicate reverse)
8582   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8583   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8584          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8585          (gnus-thread-sort-functions
8586           (if (not reverse)
8587               thread
8588             `(lambda (t1 t2)
8589                (,thread t2 t1))))
8590          (gnus-article-sort-functions
8591           (if (not reverse)
8592               article
8593             `(lambda (t1 t2)
8594                (,article t2 t1))))
8595          (buffer-read-only)
8596          (gnus-summary-prepare-hook nil))
8597     ;; We do the sorting by regenerating the threads.
8598     (gnus-summary-prepare)
8599     ;; Hide subthreads if needed.
8600     (when (and gnus-show-threads gnus-thread-hide-subtree)
8601       (gnus-summary-hide-all-threads))))
8602
8603 ;; Summary saving commands.
8604
8605 (defun gnus-summary-save-article (&optional n not-saved)
8606   "Save the current article using the default saver function.
8607 If N is a positive number, save the N next articles.
8608 If N is a negative number, save the N previous articles.
8609 If N is nil and any articles have been marked with the process mark,
8610 save those articles instead.
8611 The variable `gnus-default-article-saver' specifies the saver function."
8612   (interactive "P")
8613   (let* ((articles (gnus-summary-work-articles n))
8614          (save-buffer (save-excursion
8615                         (nnheader-set-temp-buffer " *Gnus Save*")))
8616          (num (length articles))
8617          header article file)
8618     (while articles
8619       (setq header (gnus-summary-article-header
8620                     (setq article (pop articles))))
8621       (if (not (vectorp header))
8622           ;; This is a pseudo-article.
8623           (if (assq 'name header)
8624               (gnus-copy-file (cdr (assq 'name header)))
8625             (gnus-message 1 "Article %d is unsaveable" article))
8626         ;; This is a real article.
8627         (save-window-excursion
8628           (gnus-summary-select-article t nil nil article))
8629         (save-excursion
8630           (set-buffer save-buffer)
8631           (erase-buffer)
8632           (insert-buffer-substring gnus-original-article-buffer))
8633         (setq file (gnus-article-save save-buffer file num))
8634         (gnus-summary-remove-process-mark article)
8635         (unless not-saved
8636           (gnus-summary-set-saved-mark article))))
8637     (gnus-kill-buffer save-buffer)
8638     (gnus-summary-position-point)
8639     (gnus-set-mode-line 'summary)
8640     n))
8641
8642 (defun gnus-summary-pipe-output (&optional arg)
8643   "Pipe the current article to a subprocess.
8644 If N is a positive number, pipe the N next articles.
8645 If N is a negative number, pipe the N previous articles.
8646 If N is nil and any articles have been marked with the process mark,
8647 pipe those articles instead."
8648   (interactive "P")
8649   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8650     (gnus-summary-save-article arg t))
8651   (gnus-configure-windows 'pipe))
8652
8653 (defun gnus-summary-save-article-mail (&optional arg)
8654   "Append the current article to an mail file.
8655 If N is a positive number, save the N next articles.
8656 If N is a negative number, save the N previous articles.
8657 If N is nil and any articles have been marked with the process mark,
8658 save those articles instead."
8659   (interactive "P")
8660   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8661     (gnus-summary-save-article arg)))
8662
8663 (defun gnus-summary-save-article-rmail (&optional arg)
8664   "Append the current article to an rmail file.
8665 If N is a positive number, save the N next articles.
8666 If N is a negative number, save the N previous articles.
8667 If N is nil and any articles have been marked with the process mark,
8668 save those articles instead."
8669   (interactive "P")
8670   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8671     (gnus-summary-save-article arg)))
8672
8673 (defun gnus-summary-save-article-file (&optional arg)
8674   "Append the current article to a file.
8675 If N is a positive number, save the N next articles.
8676 If N is a negative number, save the N previous articles.
8677 If N is nil and any articles have been marked with the process mark,
8678 save those articles instead."
8679   (interactive "P")
8680   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8681     (gnus-summary-save-article arg)))
8682
8683 (defun gnus-summary-write-article-file (&optional arg)
8684   "Write the current article to a file, deleting the previous file.
8685 If N is a positive number, save the N next articles.
8686 If N is a negative number, save the N previous articles.
8687 If N is nil and any articles have been marked with the process mark,
8688 save those articles instead."
8689   (interactive "P")
8690   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8691     (gnus-summary-save-article arg)))
8692
8693 (defun gnus-summary-save-article-body-file (&optional arg)
8694   "Append the current article body to a file.
8695 If N is a positive number, save the N next articles.
8696 If N is a negative number, save the N previous articles.
8697 If N is nil and any articles have been marked with the process mark,
8698 save those articles instead."
8699   (interactive "P")
8700   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8701     (gnus-summary-save-article arg)))
8702
8703 (defun gnus-summary-pipe-message (program)
8704   "Pipe the current article through PROGRAM."
8705   (interactive "sProgram: ")
8706   (gnus-summary-select-article)
8707   (let ((mail-header-separator ""))
8708     (gnus-eval-in-buffer-window gnus-article-buffer
8709       (save-restriction
8710         (widen)
8711         (let ((start (window-start))
8712               buffer-read-only)
8713           (message-pipe-buffer-body program)
8714           (set-window-start (get-buffer-window (current-buffer)) start))))))
8715
8716 (defun gnus-get-split-value (methods)
8717   "Return a value based on the split METHODS."
8718   (let (split-name method result match)
8719     (when methods
8720       (save-excursion
8721         (set-buffer gnus-original-article-buffer)
8722         (save-restriction
8723           (nnheader-narrow-to-headers)
8724           (while methods
8725             (goto-char (point-min))
8726             (setq method (pop methods))
8727             (setq match (car method))
8728             (when (cond
8729                    ((stringp match)
8730                     ;; Regular expression.
8731                     (ignore-errors
8732                       (re-search-forward match nil t)))
8733                    ((gnus-functionp match)
8734                     ;; Function.
8735                     (save-restriction
8736                       (widen)
8737                       (setq result (funcall match gnus-newsgroup-name))))
8738                    ((consp match)
8739                     ;; Form.
8740                     (save-restriction
8741                       (widen)
8742                       (setq result (eval match)))))
8743               (setq split-name (append (cdr method) split-name))
8744               (cond ((stringp result)
8745                      (push (expand-file-name
8746                             result gnus-article-save-directory)
8747                            split-name))
8748                     ((consp result)
8749                      (setq split-name (append result split-name)))))))))
8750     split-name))
8751
8752 (defun gnus-valid-move-group-p (group)
8753   (and (boundp group)
8754        (symbol-name group)
8755        (symbol-value group)
8756        (memq 'respool
8757              (assoc (symbol-name
8758                      (car (gnus-find-method-for-group
8759                            (symbol-name group))))
8760                     gnus-valid-select-methods))))
8761
8762 (defun gnus-read-move-group-name (prompt default articles prefix)
8763   "Read a group name."
8764   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8765          (minibuffer-confirm-incomplete nil) ; XEmacs
8766          (prom
8767           (format "%s %s to:"
8768                   prompt
8769                   (if (> (length articles) 1)
8770                       (format "these %d articles" (length articles))
8771                     "this article")))
8772          (to-newsgroup
8773           (cond
8774            ((null split-name)
8775             (gnus-completing-read default prom
8776                                   gnus-active-hashtb
8777                                   'gnus-valid-move-group-p
8778                                   nil prefix
8779                                   'gnus-group-history))
8780            ((= 1 (length split-name))
8781             (gnus-completing-read (car split-name) prom
8782                                   gnus-active-hashtb
8783                                   'gnus-valid-move-group-p
8784                                   nil nil
8785                                   'gnus-group-history))
8786            (t
8787             (gnus-completing-read nil prom
8788                                   (mapcar (lambda (el) (list el))
8789                                           (nreverse split-name))
8790                                   nil nil nil
8791                                   'gnus-group-history)))))
8792     (when to-newsgroup
8793       (if (or (string= to-newsgroup "")
8794               (string= to-newsgroup prefix))
8795           (setq to-newsgroup default))
8796       (unless to-newsgroup
8797         (error "No group name entered"))
8798       (or (gnus-active to-newsgroup)
8799           (gnus-activate-group to-newsgroup)
8800           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8801                                      to-newsgroup))
8802               (or (and (gnus-request-create-group
8803                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8804                        (gnus-activate-group to-newsgroup nil nil
8805                                             (gnus-group-name-to-method
8806                                              to-newsgroup)))
8807                   (error "Couldn't create group %s" to-newsgroup)))
8808           (error "No such group: %s" to-newsgroup)))
8809     to-newsgroup))
8810
8811 ;; Summary extract commands
8812
8813 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8814   (let ((buffer-read-only nil)
8815         (article (gnus-summary-article-number))
8816         after-article b e)
8817     (unless (gnus-summary-goto-subject article)
8818       (error "No such article: %d" article))
8819     (gnus-summary-position-point)
8820     ;; If all commands are to be bunched up on one line, we collect
8821     ;; them here.
8822     (unless gnus-view-pseudos-separately
8823       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8824             files action)
8825         (while ps
8826           (setq action (cdr (assq 'action (car ps))))
8827           (setq files (list (cdr (assq 'name (car ps)))))
8828           (while (and ps (cdr ps)
8829                       (string= (or action "1")
8830                                (or (cdr (assq 'action (cadr ps))) "2")))
8831             (push (cdr (assq 'name (cadr ps))) files)
8832             (setcdr ps (cddr ps)))
8833           (when files
8834             (when (not (string-match "%s" action))
8835               (push " " files))
8836             (push " " files)
8837             (when (assq 'execute (car ps))
8838               (setcdr (assq 'execute (car ps))
8839                       (funcall (if (string-match "%s" action)
8840                                    'format 'concat)
8841                                action
8842                                (mapconcat
8843                                 (lambda (f)
8844                                   (if (equal f " ")
8845                                       f
8846                                     (gnus-quote-arg-for-sh-or-csh f)))
8847                                 files " ")))))
8848           (setq ps (cdr ps)))))
8849     (if (and gnus-view-pseudos (not not-view))
8850         (while pslist
8851           (when (assq 'execute (car pslist))
8852             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8853                                   (eq gnus-view-pseudos 'not-confirm)))
8854           (setq pslist (cdr pslist)))
8855       (save-excursion
8856         (while pslist
8857           (setq after-article (or (cdr (assq 'article (car pslist)))
8858                                   (gnus-summary-article-number)))
8859           (gnus-summary-goto-subject after-article)
8860           (forward-line 1)
8861           (setq b (point))
8862           (insert "    " (file-name-nondirectory
8863                           (cdr (assq 'name (car pslist))))
8864                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8865           (setq e (point))
8866           (forward-line -1)             ; back to `b'
8867           (gnus-add-text-properties
8868            b (1- e) (list 'gnus-number gnus-reffed-article-number
8869                           gnus-mouse-face-prop gnus-mouse-face))
8870           (gnus-data-enter
8871            after-article gnus-reffed-article-number
8872            gnus-unread-mark b (car pslist) 0 (- e b))
8873           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8874           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8875           (setq pslist (cdr pslist)))))))
8876
8877 (defun gnus-pseudos< (p1 p2)
8878   (let ((c1 (cdr (assq 'action p1)))
8879         (c2 (cdr (assq 'action p2))))
8880     (and c1 c2 (string< c1 c2))))
8881
8882 (defun gnus-request-pseudo-article (props)
8883   (cond ((assq 'execute props)
8884          (gnus-execute-command (cdr (assq 'execute props)))))
8885   (let ((gnus-current-article (gnus-summary-article-number)))
8886     (gnus-run-hooks 'gnus-mark-article-hook)))
8887
8888 (defun gnus-execute-command (command &optional automatic)
8889   (save-excursion
8890     (gnus-article-setup-buffer)
8891     (set-buffer gnus-article-buffer)
8892     (setq buffer-read-only nil)
8893     (let ((command (if automatic command
8894                      (read-string "Command: " (cons command 0)))))
8895       (erase-buffer)
8896       (insert "$ " command "\n\n")
8897       (if gnus-view-pseudo-asynchronously
8898           (start-process "gnus-execute" (current-buffer) shell-file-name
8899                          shell-command-switch command)
8900         (call-process shell-file-name nil t nil
8901                       shell-command-switch command)))))
8902
8903 ;; Summary kill commands.
8904
8905 (defun gnus-summary-edit-global-kill (article)
8906   "Edit the \"global\" kill file."
8907   (interactive (list (gnus-summary-article-number)))
8908   (gnus-group-edit-global-kill article))
8909
8910 (defun gnus-summary-edit-local-kill ()
8911   "Edit a local kill file applied to the current newsgroup."
8912   (interactive)
8913   (setq gnus-current-headers (gnus-summary-article-header))
8914   (gnus-group-edit-local-kill
8915    (gnus-summary-article-number) gnus-newsgroup-name))
8916
8917 ;;; Header reading.
8918
8919 (defun gnus-read-header (id &optional header)
8920   "Read the headers of article ID and enter them into the Gnus system."
8921   (let ((group gnus-newsgroup-name)
8922         (gnus-override-method
8923          (and (gnus-news-group-p gnus-newsgroup-name)
8924               gnus-refer-article-method))
8925         where)
8926     ;; First we check to see whether the header in question is already
8927     ;; fetched.
8928     (if (stringp id)
8929         ;; This is a Message-ID.
8930         (setq header (or header (gnus-id-to-header id)))
8931       ;; This is an article number.
8932       (setq header (or header (gnus-summary-article-header id))))
8933     (if (and header
8934              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8935         ;; We have found the header.
8936         header
8937       ;; If this is a sparse article, we have to nix out its
8938       ;; previous entry in the thread hashtb.
8939       (when (and header
8940                  (gnus-summary-article-sparse-p (mail-header-number header)))
8941         (let* ((parent (gnus-parent-id (mail-header-references header)))
8942                (thread (and parent (gnus-id-to-thread parent))))
8943           (when thread
8944             (delq (assq header thread) thread))))
8945       ;; We have to really fetch the header to this article.
8946       (save-excursion
8947         (set-buffer nntp-server-buffer)
8948         (when (setq where (gnus-request-head id group))
8949           (nnheader-fold-continuation-lines)
8950           (goto-char (point-max))
8951           (insert ".\n")
8952           (goto-char (point-min))
8953           (insert "211 ")
8954           (princ (cond
8955                   ((numberp id) id)
8956                   ((cdr where) (cdr where))
8957                   (header (mail-header-number header))
8958                   (t gnus-reffed-article-number))
8959                  (current-buffer))
8960           (insert " Article retrieved.\n"))
8961         (if (or (not where)
8962                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8963             ()                          ; Malformed head.
8964           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8965             (when (and (stringp id)
8966                        (not (string= (gnus-group-real-name group)
8967                                      (car where))))
8968               ;; If we fetched by Message-ID and the article came
8969               ;; from a different group, we fudge some bogus article
8970               ;; numbers for this article.
8971               (mail-header-set-number header gnus-reffed-article-number))
8972             (save-excursion
8973               (set-buffer gnus-summary-buffer)
8974               (decf gnus-reffed-article-number)
8975               (gnus-remove-header (mail-header-number header))
8976               (push header gnus-newsgroup-headers)
8977               (setq gnus-current-headers header)
8978               (push (mail-header-number header) gnus-newsgroup-limit)))
8979           header)))))
8980
8981 (defun gnus-remove-header (number)
8982   "Remove header NUMBER from `gnus-newsgroup-headers'."
8983   (if (and gnus-newsgroup-headers
8984            (= number (mail-header-number (car gnus-newsgroup-headers))))
8985       (pop gnus-newsgroup-headers)
8986     (let ((headers gnus-newsgroup-headers))
8987       (while (and (cdr headers)
8988                   (not (= number (mail-header-number (cadr headers)))))
8989         (pop headers))
8990       (when (cdr headers)
8991         (setcdr headers (cddr headers))))))
8992
8993 ;;;
8994 ;;; summary highlights
8995 ;;;
8996
8997 (defun gnus-highlight-selected-summary ()
8998   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8999   ;; Highlight selected article in summary buffer
9000   (when gnus-summary-selected-face
9001     (save-excursion
9002       (let* ((beg (progn (beginning-of-line) (point)))
9003              (end (progn (end-of-line) (point)))
9004              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9005              (from (if (get-text-property beg gnus-mouse-face-prop)
9006                        beg
9007                      (or (next-single-property-change
9008                           beg gnus-mouse-face-prop nil end)
9009                          beg)))
9010              (to
9011               (if (= from end)
9012                   (- from 2)
9013                 (or (next-single-property-change
9014                      from gnus-mouse-face-prop nil end)
9015                     end))))
9016         ;; If no mouse-face prop on line we will have to = from = end,
9017         ;; so we highlight the entire line instead.
9018         (when (= (+ to 2) from)
9019           (setq from beg)
9020           (setq to end))
9021         (if gnus-newsgroup-selected-overlay
9022             ;; Move old overlay.
9023             (gnus-move-overlay
9024              gnus-newsgroup-selected-overlay from to (current-buffer))
9025           ;; Create new overlay.
9026           (gnus-overlay-put
9027            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9028            'face gnus-summary-selected-face))))))
9029
9030 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9031 (defun gnus-summary-highlight-line ()
9032   "Highlight current line according to `gnus-summary-highlight'."
9033   (let* ((list gnus-summary-highlight)
9034          (p (point))
9035          (end (progn (end-of-line) (point)))
9036          ;; now find out where the line starts and leave point there.
9037          (beg (progn (beginning-of-line) (point)))
9038          (article (gnus-summary-article-number))
9039          (score (or (cdr (assq (or article gnus-current-article)
9040                                gnus-newsgroup-scored))
9041                     gnus-summary-default-score 0))
9042          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9043          (inhibit-read-only t))
9044     ;; Eval the cars of the lists until we find a match.
9045     (let ((default gnus-summary-default-score))
9046       (while (and list
9047                   (not (eval (caar list))))
9048         (setq list (cdr list))))
9049     (let ((face (cdar list)))
9050       (unless (eq face (get-text-property beg 'face))
9051         (gnus-put-text-property-excluding-characters-with-faces
9052          beg end 'face
9053          (setq face (if (boundp face) (symbol-value face) face)))
9054         (when gnus-summary-highlight-line-function
9055           (funcall gnus-summary-highlight-line-function article face))))
9056     (goto-char p)))
9057
9058 (defun gnus-update-read-articles (group unread &optional compute)
9059   "Update the list of read articles in GROUP."
9060   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9061          (entry (gnus-gethash group gnus-newsrc-hashtb))
9062          (info (nth 2 entry))
9063          (prev 1)
9064          (unread (sort (copy-sequence unread) '<))
9065          read)
9066     (if (or (not info) (not active))
9067         ;; There is no info on this group if it was, in fact,
9068         ;; killed.  Gnus stores no information on killed groups, so
9069         ;; there's nothing to be done.
9070         ;; One could store the information somewhere temporarily,
9071         ;; perhaps...  Hmmm...
9072         ()
9073       ;; Remove any negative articles numbers.
9074       (while (and unread (< (car unread) 0))
9075         (setq unread (cdr unread)))
9076       ;; Remove any expired article numbers
9077       (while (and unread (< (car unread) (car active)))
9078         (setq unread (cdr unread)))
9079       ;; Compute the ranges of read articles by looking at the list of
9080       ;; unread articles.
9081       (while unread
9082         (when (/= (car unread) prev)
9083           (push (if (= prev (1- (car unread))) prev
9084                   (cons prev (1- (car unread))))
9085                 read))
9086         (setq prev (1+ (car unread)))
9087         (setq unread (cdr unread)))
9088       (when (<= prev (cdr active))
9089         (push (cons prev (cdr active)) read))
9090       (setq read (if (> (length read) 1) (nreverse read) read))
9091       (if compute
9092           read
9093         (save-excursion
9094           (set-buffer gnus-group-buffer)
9095           (gnus-undo-register
9096             `(progn
9097                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9098                (gnus-info-set-read ',info ',(gnus-info-read info))
9099                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9100                (gnus-group-update-group ,group t))))
9101        ;; Propagate the read marks to the backend.
9102        (if (gnus-check-backend-function 'request-set-mark group)
9103            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9104                  (add (gnus-remove-from-range read (gnus-info-read info))))
9105              (when (or add del)
9106                (gnus-request-set-mark
9107                 group (delq nil (list (if add (list add 'add '(read)))
9108                                       (if del (list del 'del '(read)))))))))
9109         ;; Enter this list into the group info.
9110         (gnus-info-set-read info read)
9111         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9112         (gnus-get-unread-articles-in-group info (gnus-active group))
9113         t))))
9114
9115 (defun gnus-offer-save-summaries ()
9116   "Offer to save all active summary buffers."
9117   (save-excursion
9118     (let ((buflist (buffer-list))
9119           buffers bufname)
9120       ;; Go through all buffers and find all summaries.
9121       (while buflist
9122         (and (setq bufname (buffer-name (car buflist)))
9123              (string-match "Summary" bufname)
9124              (save-excursion
9125                (set-buffer bufname)
9126                ;; We check that this is, indeed, a summary buffer.
9127                (and (eq major-mode 'gnus-summary-mode)
9128                     ;; Also make sure this isn't bogus.
9129                     gnus-newsgroup-prepared
9130                     ;; Also make sure that this isn't a dead summary buffer.
9131                     (not gnus-dead-summary-mode)))
9132              (push bufname buffers))
9133         (setq buflist (cdr buflist)))
9134       ;; Go through all these summary buffers and offer to save them.
9135       (when buffers
9136         (map-y-or-n-p
9137          "Update summary buffer %s? "
9138          (lambda (buf)
9139            (switch-to-buffer buf)
9140            (gnus-summary-exit))
9141          buffers)))))
9142
9143
9144 ;;; @ for mime-partial
9145 ;;;
9146
9147 (defun gnus-request-partial-message ()
9148   (save-excursion
9149     (let ((number (gnus-summary-article-number))
9150           (group gnus-newsgroup-name)
9151           (mother gnus-article-buffer))
9152       (set-buffer (get-buffer-create " *Partial Article*"))
9153       (erase-buffer)
9154       (setq mime-preview-buffer mother)
9155       (gnus-request-article-this-buffer number group)
9156       (mime-parse-buffer)
9157       )))
9158
9159 (autoload 'mime-combine-message/partial-pieces-automatically
9160   "mime-partial"
9161   "Internal method to combine message/partial messages automatically.")
9162
9163 (mime-add-condition
9164  'action '((type . message)(subtype . partial)
9165            (major-mode . gnus-original-article-mode)
9166            (method . mime-combine-message/partial-pieces-automatically)
9167            (summary-buffer-exp . gnus-summary-buffer)
9168            (request-partial-message-method . gnus-request-partial-message)
9169            ))
9170
9171
9172 ;;; @ end
9173 ;;;
9174
9175 (defun gnus-summary-setup-default-charset ()
9176   "Setup newsgroup default charset."
9177   (let ((name (and gnus-newsgroup-name
9178                    (gnus-group-real-name gnus-newsgroup-name))))
9179     (setq gnus-newsgroup-charset
9180           (or (and gnus-newsgroup-name
9181                    (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9182                        (let ((alist gnus-group-charset-alist) 
9183                              elem (charset nil))
9184                          (while (setq elem (pop alist))
9185                            (when (and name
9186                                       (string-match (car elem) name))
9187                              (setq alist nil
9188                                    charset (cadr elem))))
9189                          charset)))
9190               gnus-default-charset))))
9191   
9192 ;;;
9193 ;;; Mime Commands
9194 ;;;
9195
9196 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9197   "Display the current article buffer fully MIME-buttonized.  
9198 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9199 treated as multipart/mixed."
9200   (interactive "P")
9201   (require 'gnus-art)
9202   (let ((gnus-unbuttonized-mime-types nil)
9203         (gnus-mime-display-multipart-as-mixed show-all-parts))
9204     (gnus-summary-show-article)))
9205
9206 (defun gnus-summary-repair-multipart (article)
9207   "Add a Content-Type header to a multipart article without one."
9208   (interactive (list (gnus-summary-article-number)))
9209   (gnus-with-article article
9210     (message-narrow-to-head)
9211     (goto-char (point-max))
9212     (widen)
9213     (when (search-forward "\n--" nil t)
9214       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9215         (message-narrow-to-head)
9216         (message-remove-header "Mime-Version")
9217         (message-remove-header "Content-Type")
9218         (goto-char (point-max))
9219         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9220                         separator))
9221         (insert "Mime-Version: 1.0\n")
9222         (widen))))
9223   (let (gnus-mark-article-hook)
9224     (gnus-summary-select-article t t nil article)))
9225
9226 (defun gnus-summary-toggle-display-buttonized ()
9227   "Toggle the buttonizing of the article buffer."
9228   (interactive)
9229   (require 'gnus-art)
9230   (if (setq gnus-inhibit-mime-unbuttonizing
9231             (not gnus-inhibit-mime-unbuttonizing))
9232       (let ((gnus-unbuttonized-mime-types nil))
9233         (gnus-summary-show-article))
9234     (gnus-summary-show-article)))
9235     
9236 (gnus-ems-redefine)
9237
9238 (provide 'gnus-sum)
9239
9240 (run-hooks 'gnus-sum-load-hook)
9241
9242 ;;; gnus-sum.el ends here