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