3b91756843ef470176c4e9ba6e03bb585a67da1c
[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 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   (require 'static))
44
45 (eval-and-compile
46   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
47
48 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
49 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
50
51 (defcustom gnus-kill-summary-on-exit t
52   "*If non-nil, kill the summary buffer when you exit from it.
53 If nil, the summary will become a \"*Dead Summary*\" buffer, and
54 it will be killed sometime later."
55   :group 'gnus-summary-exit
56   :type 'boolean)
57
58 (defcustom gnus-fetch-old-headers nil
59   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
60 If an unread article in the group refers to an older, already read (or
61 just marked as read) article, the old article will not normally be
62 displayed in the Summary buffer.  If this variable is non-nil, Gnus
63 will attempt to grab the headers to the old articles, and thereby
64 build complete threads.  If it has the value `some', only enough
65 headers to connect otherwise loose threads will be displayed.  This
66 variable can also be a number.  In that case, no more than that number
67 of old headers will be fetched.  If it has the value `invisible', all
68 old headers will be fetched, but none will be displayed.
69
70 The server has to support NOV for any of this to work."
71   :group 'gnus-thread
72   :type '(choice (const :tag "off" nil)
73                  (const some)
74                  number
75                  (sexp :menu-tag "other" t)))
76
77 (defcustom gnus-refer-thread-limit 200
78   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
79 If t, fetch all the available old headers."
80   :group 'gnus-thread
81   :type '(choice number
82                  (sexp :menu-tag "other" t)))
83
84 (defcustom gnus-summary-make-false-root 'adopt
85   "*nil means that Gnus won't gather loose threads.
86 If the root of a thread has expired or been read in a previous
87 session, the information necessary to build a complete thread has been
88 lost.  Instead of having many small sub-threads from this original thread
89 scattered all over the summary buffer, Gnus can gather them.
90
91 If non-nil, Gnus will try to gather all loose sub-threads from an
92 original thread into one large thread.
93
94 If this variable is non-nil, it should be one of `none', `adopt',
95 `dummy' or `empty'.
96
97 If this variable is `none', Gnus will not make a false root, but just
98 present the sub-threads after another.
99 If this variable is `dummy', Gnus will create a dummy root that will
100 have all the sub-threads as children.
101 If this variable is `adopt', Gnus will make one of the \"children\"
102 the parent and mark all the step-children as such.
103 If this variable is `empty', the \"children\" are printed with empty
104 subject fields.  (Or rather, they will be printed with a string
105 given by the `gnus-summary-same-subject' variable.)"
106   :group 'gnus-thread
107   :type '(choice (const :tag "off" nil)
108                  (const none)
109                  (const dummy)
110                  (const adopt)
111                  (const empty)))
112
113 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
114   "*A regexp to match subjects to be excluded from loose thread gathering.
115 As loose thread gathering is done on subjects only, that means that
116 there can be many false gatherings performed.  By rooting out certain
117 common subjects, gathering might become saner."
118   :group 'gnus-thread
119   :type 'regexp)
120
121 (defcustom gnus-summary-gather-subject-limit nil
122   "*Maximum length of subject comparisons when gathering loose threads.
123 Use nil to compare full subjects.  Setting this variable to a low
124 number will help gather threads that have been corrupted by
125 newsreaders chopping off subject lines, but it might also mean that
126 unrelated articles that have subject that happen to begin with the
127 same few characters will be incorrectly gathered.
128
129 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
130 comparing subjects."
131   :group 'gnus-thread
132   :type '(choice (const :tag "off" nil)
133                  (const fuzzy)
134                  (sexp :menu-tag "on" t)))
135
136 (defcustom gnus-simplify-subject-functions nil
137   "List of functions taking a string argument that simplify subjects.
138 The functions are applied recursively.
139
140 Useful functions to put in this list include: `gnus-simplify-subject-re',
141 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, marking commands will go to the next unread article.
183 If `never', commands that usually go to the next unread article, will
184 go to the next article, whether it is read or not.
185 If nil, only the marking commands will go to the next (un)read article."
186   :group 'gnus-summary-marks
187   :link '(custom-manual "(gnus)Setting Marks")
188   :type '(choice (const :tag "off" nil)
189                  (const never)
190                  (sexp :menu-tag "on" t)))
191
192 (defcustom gnus-summary-default-score 0
193   "*Default article score level.
194 All scores generated by the score files will be added to this score.
195 If this variable is nil, scoring will be disabled."
196   :group 'gnus-score-default
197   :type '(choice (const :tag "disable")
198                  integer))
199
200 (defcustom gnus-summary-zcore-fuzz 0
201   "*Fuzziness factor for the zcore in the summary buffer.
202 Articles with scores closer than this to `gnus-summary-default-score'
203 will not be marked."
204   :group 'gnus-summary-format
205   :type 'integer)
206
207 (defcustom gnus-simplify-subject-fuzzy-regexp nil
208   "*Strings to be removed when doing fuzzy matches.
209 This can either be a regular expression or list of regular expressions
210 that will be removed from subject strings if fuzzy subject
211 simplification is selected."
212   :group 'gnus-thread
213   :type '(repeat regexp))
214
215 (defcustom gnus-show-threads t
216   "*If non-nil, display threads in summary mode."
217   :group 'gnus-thread
218   :type 'boolean)
219
220 (defcustom gnus-thread-hide-subtree nil
221   "*If non-nil, hide all threads initially.
222 If threads are hidden, you have to run the command
223 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
224 to expose hidden threads."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-killed t
229   "*If non-nil, hide killed threads automatically."
230   :group 'gnus-thread
231   :type 'boolean)
232
233 (defcustom gnus-thread-ignore-subject t
234   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
235 If nil, articles that have different subjects from their parents will
236 start separate threads."
237   :group 'gnus-thread
238   :type 'boolean)
239
240 (defcustom gnus-thread-operation-ignore-subject t
241   "*If non-nil, subjects will be ignored when doing thread commands.
242 This affects commands like `gnus-summary-kill-thread' and
243 `gnus-summary-lower-thread'.
244
245 If this variable is nil, articles in the same thread with different
246 subjects will not be included in the operation in question.  If this
247 variable is `fuzzy', only articles that have subjects that are fuzzily
248 equal will be included."
249   :group 'gnus-thread
250   :type '(choice (const :tag "off" nil)
251                  (const fuzzy)
252                  (sexp :tag "on" t)))
253
254 (defcustom gnus-thread-indent-level 4
255   "*Number that says how much each sub-thread should be indented."
256   :group 'gnus-thread
257   :type 'integer)
258
259 (defcustom gnus-auto-extend-newsgroup t
260   "*If non-nil, extend newsgroup forward and backward when requested."
261   :group 'gnus-summary-choose
262   :type 'boolean)
263
264 (defcustom gnus-auto-select-first t
265   "*If nil, don't select the first unread article when entering a group.
266 If this variable is `best', select the highest-scored unread article
267 in the group.  If t, select the first unread article.
268
269 This variable can also be a function to place point on a likely
270 subject line.  Useful values include `gnus-summary-first-unread-subject',
271 `gnus-summary-first-unread-article' and
272 `gnus-summary-best-unread-article'.
273
274 If you want to prevent automatic selection of the first unread article
275 in some newsgroups, set the variable to nil in
276 `gnus-select-group-hook'."
277   :group 'gnus-group-select
278   :type '(choice (const :tag "none" nil)
279                  (const best)
280                  (sexp :menu-tag "first" t)
281                  (function-item gnus-summary-first-unread-subject)
282                  (function-item gnus-summary-first-unread-article)
283                  (function-item gnus-summary-best-unread-article)))
284
285 (defcustom gnus-dont-select-after-jump-to-other-group nil
286   "If non-nil, don't select the first unread article after entering the
287 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
288 it is depend on the value of `gnus-auto-select-first' whether to select
289 or not."
290   :group 'gnus-group-select
291   :type 'boolean)
292
293 (defcustom gnus-auto-select-next t
294   "*If non-nil, offer to go to the next group from the end of the previous.
295 If the value is t and the next newsgroup is empty, Gnus will exit
296 summary mode and go back to group mode.  If the value is neither nil
297 nor t, Gnus will select the following unread newsgroup.  In
298 particular, if the value is the symbol `quietly', the next unread
299 newsgroup will be selected without any confirmation, and if it is
300 `almost-quietly', the next group will be selected without any
301 confirmation if you are located on the last article in the group.
302 Finally, if this variable is `slightly-quietly', the `Z n' command
303 will go to the next group without confirmation."
304   :group 'gnus-summary-maneuvering
305   :type '(choice (const :tag "off" nil)
306                  (const quietly)
307                  (const almost-quietly)
308                  (const slightly-quietly)
309                  (sexp :menu-tag "on" t)))
310
311 (defcustom gnus-auto-select-same nil
312   "*If non-nil, select the next article with the same subject.
313 If there are no more articles with the same subject, go to
314 the first unread article."
315   :group 'gnus-summary-maneuvering
316   :type 'boolean)
317
318 (defcustom gnus-summary-check-current nil
319   "*If non-nil, consider the current article when moving.
320 The \"unread\" movement commands will stay on the same line if the
321 current article is unread."
322   :group 'gnus-summary-maneuvering
323   :type 'boolean)
324
325 (defcustom gnus-auto-center-summary t
326   "*If non-nil, always center the current summary buffer.
327 In particular, if `vertical' do only vertical recentering.  If non-nil
328 and non-`vertical', do both horizontal and vertical recentering."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "none" nil)
331                  (const vertical)
332                  (integer :tag "height")
333                  (sexp :menu-tag "both" t)))
334
335 (defcustom gnus-show-all-headers nil
336   "*If non-nil, don't hide any headers."
337   :group 'gnus-article-hiding
338   :group 'gnus-article-headers
339   :type 'boolean)
340
341 (defcustom gnus-summary-ignore-duplicates nil
342   "*If non-nil, ignore articles with identical Message-ID headers."
343   :group 'gnus-summary
344   :type 'boolean)
345
346 (defcustom gnus-single-article-buffer t
347   "*If non-nil, display all articles in the same buffer.
348 If nil, each group will get its own article buffer."
349   :group 'gnus-article-various
350   :type 'boolean)
351
352 (defcustom gnus-break-pages t
353   "*If non-nil, do page breaking on articles.
354 The page delimiter is specified by the `gnus-page-delimiter'
355 variable."
356   :group 'gnus-article-various
357   :type 'boolean)
358
359 (defcustom gnus-show-mime t
360   "*If non-nil, do mime processing of articles.
361 The articles will simply be fed to the function given by
362 `gnus-article-display-method-for-mime'."
363   :group 'gnus-article-mime
364   :type 'boolean)
365
366 (defcustom gnus-move-split-methods nil
367   "*Variable used to suggest where articles are to be moved to.
368 It uses the same syntax as the `gnus-split-methods' variable."
369   :group 'gnus-summary-mail
370   :type '(repeat (choice (list :value (fun) function)
371                          (cons :value ("" "") regexp (repeat string))
372                          (sexp :value nil))))
373
374 (defcustom gnus-unread-mark ? ;Whitespace
375   "*Mark used for unread articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-ticked-mark ?!
380   "*Mark used for ticked articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-dormant-mark ??
385   "*Mark used for dormant articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-del-mark ?r
390   "*Mark used for del'd articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-read-mark ?R
395   "*Mark used for read articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-expirable-mark ?E
400   "*Mark used for expirable articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-killed-mark ?K
405   "*Mark used for killed articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-souped-mark ?F
410   "*Mark used for killed articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-kill-file-mark ?X
415   "*Mark used for articles killed by kill files."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-low-score-mark ?Y
420   "*Mark used for articles with a low score."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-catchup-mark ?C
425   "*Mark used for articles that are caught up."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-replied-mark ?A
430   "*Mark used for articles that have been replied to."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-cached-mark ?*
435   "*Mark used for articles that are in the cache."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-saved-mark ?S
440   "*Mark used for articles that have been saved to."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-ancient-mark ?O
445   "*Mark used for ancient articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-sparse-mark ?Q
450   "*Mark used for sparsely reffed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-canceled-mark ?G
455   "*Mark used for canceled articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-duplicate-mark ?M
460   "*Mark used for duplicate articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-undownloaded-mark ?@
465   "*Mark used for articles that weren't downloaded."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-downloadable-mark ?%
470   "*Mark used for articles that are to be downloaded."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-unsendable-mark ?=
475   "*Mark used for articles that won't be sent."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-score-over-mark ?+
480   "*Score mark used for articles with high scores."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-score-below-mark ?-
485   "*Score mark used for articles with low scores."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-empty-thread-mark ? ;Whitespace
490   "*There is no thread under the article."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-not-empty-thread-mark ?=
495   "*There is a thread under the article."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-view-pseudo-asynchronously nil
500   "*If non-nil, Gnus will view pseudo-articles asynchronously."
501   :group 'gnus-extract-view
502   :type 'boolean)
503
504 (defcustom gnus-auto-expirable-marks
505   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
506         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
507         gnus-souped-mark gnus-duplicate-mark)
508   "*The list of marks converted into expiration if a group is auto-expirable."
509   :group 'gnus-summary
510   :type '(repeat character))
511
512 (defcustom gnus-inhibit-user-auto-expire t
513   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
514   :group 'gnus-summary
515   :type 'boolean)
516
517 (defcustom gnus-view-pseudos nil
518   "*If `automatic', pseudo-articles will be viewed automatically.
519 If `not-confirm', pseudos will be viewed automatically, and the user
520 will not be asked to confirm the command."
521   :group 'gnus-extract-view
522   :type '(choice (const :tag "off" nil)
523                  (const automatic)
524                  (const not-confirm)))
525
526 (defcustom gnus-view-pseudos-separately t
527   "*If non-nil, one pseudo-article will be created for each file to be viewed.
528 If nil, all files that use the same viewing command will be given as a
529 list of parameters to that command."
530   :group 'gnus-extract-view
531   :type 'boolean)
532
533 (defcustom gnus-insert-pseudo-articles t
534   "*If non-nil, insert pseudo-articles when decoding articles."
535   :group 'gnus-extract-view
536   :type 'boolean)
537
538 (defcustom gnus-summary-dummy-line-format
539   "  %(:                          :%) %S\n"
540   "*The format specification for the dummy roots in the summary buffer.
541 It works along the same lines as a normal formatting string,
542 with some simple extensions.
543
544 %S  The subject"
545   :group 'gnus-threading
546   :type 'string)
547
548 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
549   "*The format specification for the summary mode line.
550 It works along the same lines as a normal formatting string,
551 with some simple extensions:
552
553 %G  Group name
554 %p  Unprefixed group name
555 %A  Current article number
556 %z  Current article score
557 %V  Gnus version
558 %U  Number of unread articles in the group
559 %e  Number of unselected articles in the group
560 %Z  A string with unread/unselected article counts
561 %g  Shortish group name
562 %S  Subject of the current article
563 %u  User-defined spec
564 %s  Current score file name
565 %d  Number of dormant articles
566 %r  Number of articles that have been marked as read in this session
567 %E  Number of articles expunged by the score files"
568   :group 'gnus-summary-format
569   :type 'string)
570
571 (defcustom gnus-summary-mark-below 0
572   "*Mark all articles with a score below this variable as read.
573 This variable is local to each summary buffer and usually set by the
574 score file."
575   :group 'gnus-score-default
576   :type 'integer)
577
578 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
579   "*List of functions used for sorting articles in the summary buffer.
580 This variable is only used when not using a threaded display."
581   :group 'gnus-summary-sort
582   :type '(repeat (choice (function-item gnus-article-sort-by-number)
583                          (function-item gnus-article-sort-by-author)
584                          (function-item gnus-article-sort-by-subject)
585                          (function-item gnus-article-sort-by-date)
586                          (function-item gnus-article-sort-by-score)
587                          (function :tag "other"))))
588
589 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
590   "*List of functions used for sorting threads in the summary buffer.
591 By default, threads are sorted by article number.
592
593 Each function takes two threads and return non-nil if the first thread
594 should be sorted before the other.  If you use more than one function,
595 the primary sort function should be the last.  You should probably
596 always include `gnus-thread-sort-by-number' in the list of sorting
597 functions -- preferably first.
598
599 Ready-made functions include `gnus-thread-sort-by-number',
600 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
601 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
602 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
603   :group 'gnus-summary-sort
604   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
605                          (function-item gnus-thread-sort-by-author)
606                          (function-item gnus-thread-sort-by-subject)
607                          (function-item gnus-thread-sort-by-date)
608                          (function-item gnus-thread-sort-by-score)
609                          (function-item gnus-thread-sort-by-total-score)
610                          (function :tag "other"))))
611
612 (defcustom gnus-thread-score-function '+
613   "*Function used for calculating the total score of a thread.
614
615 The function is called with the scores of the article and each
616 subthread and should then return the score of the thread.
617
618 Some functions you can use are `+', `max', or `min'."
619   :group 'gnus-summary-sort
620   :type 'function)
621
622 (defcustom gnus-summary-expunge-below nil
623   "All articles that have a score less than this variable will be expunged.
624 This variable is local to the summary buffers."
625   :group 'gnus-score-default
626   :type '(choice (const :tag "off" nil)
627                  integer))
628
629 (defcustom gnus-thread-expunge-below nil
630   "All threads that have a total score less than this variable will be expunged.
631 See `gnus-thread-score-function' for en explanation of what a
632 \"thread score\" is.
633
634 This variable is local to the summary buffers."
635   :group 'gnus-threading
636   :group 'gnus-score-default
637   :type '(choice (const :tag "off" nil)
638                  integer))
639
640 (defcustom gnus-summary-mode-hook nil
641   "*A hook for Gnus summary mode.
642 This hook is run before any variables are set in the summary buffer."
643   :group 'gnus-summary-various
644   :type 'hook)
645
646 (defcustom gnus-summary-menu-hook nil
647   "*Hook run after the creation of the summary mode menu."
648   :group 'gnus-summary-visual
649   :type 'hook)
650
651 (defcustom gnus-summary-exit-hook nil
652   "*A hook called on exit from the summary buffer.
653 It will be called with point in the group buffer."
654   :group 'gnus-summary-exit
655   :type 'hook)
656
657 (defcustom gnus-summary-prepare-hook nil
658   "*A hook called after the summary buffer has been generated.
659 If you want to modify the summary buffer, you can use this hook."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-prepared-hook nil
664   "*A hook called as the last thing after the summary buffer has been generated."
665   :group 'gnus-summary-various
666   :type 'hook)
667
668 (defcustom gnus-summary-generate-hook nil
669   "*A hook run just before generating the summary buffer.
670 This hook is commonly used to customize threading variables and the
671 like."
672   :group 'gnus-summary-various
673   :type 'hook)
674
675 (defcustom gnus-select-group-hook nil
676   "*A hook called when a newsgroup is selected.
677
678 If you'd like to simplify subjects like the
679 `gnus-summary-next-same-subject' command does, you can use the
680 following hook:
681
682  (setq gnus-select-group-hook
683       (list
684         (lambda ()
685           (mapcar (lambda (header)
686                      (mail-header-set-subject
687                       header
688                       (gnus-simplify-subject
689                        (mail-header-subject header) 're-only)))
690                   gnus-newsgroup-headers))))"
691   :group 'gnus-group-select
692   :type 'hook)
693
694 (defcustom gnus-select-article-hook nil
695   "*A hook called when an article is selected."
696   :group 'gnus-summary-choose
697   :type 'hook)
698
699 (defcustom gnus-visual-mark-article-hook
700   (list 'gnus-highlight-selected-summary)
701   "*Hook run after selecting an article in the summary buffer.
702 It is meant to be used for highlighting the article in some way.  It
703 is not run if `gnus-visual' is nil."
704   :group 'gnus-summary-visual
705   :type 'hook)
706
707 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
708   "*A hook called before parsing the headers."
709   :group 'gnus-various
710   :type 'hook)
711
712 (defcustom gnus-exit-group-hook nil
713   "*A hook called when exiting (not quitting) summary mode."
714   :group 'gnus-various
715   :type 'hook)
716
717 (defcustom gnus-summary-update-hook
718   (list 'gnus-summary-highlight-line)
719   "*A hook called when a summary line is changed.
720 The hook will not be called if `gnus-visual' is nil.
721
722 The default function `gnus-summary-highlight-line' will
723 highlight the line according to the `gnus-summary-highlight'
724 variable."
725   :group 'gnus-summary-visual
726   :type 'hook)
727
728 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
729   "*A hook called when an article is selected for the first time.
730 The hook is intended to mark an article as read (or unread)
731 automatically when it is selected."
732   :group 'gnus-summary-choose
733   :type 'hook)
734
735 (defcustom gnus-group-no-more-groups-hook nil
736   "*A hook run when returning to group mode having no more (unread) groups."
737   :group 'gnus-group-select
738   :type 'hook)
739
740 (defcustom gnus-ps-print-hook nil
741   "*A hook run before ps-printing something from Gnus."
742   :group 'gnus-summary
743   :type 'hook)
744
745 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
746   "Face used for highlighting the current article in the summary buffer."
747   :group 'gnus-summary-visual
748   :type 'face)
749
750 (defcustom gnus-summary-highlight
751   '(((= mark gnus-canceled-mark)
752      . gnus-summary-cancelled-face)
753     ((and (> score default)
754           (or (= mark gnus-dormant-mark)
755               (= mark gnus-ticked-mark)))
756      . gnus-summary-high-ticked-face)
757     ((and (< score default)
758           (or (= mark gnus-dormant-mark)
759               (= mark gnus-ticked-mark)))
760      . gnus-summary-low-ticked-face)
761     ((or (= mark gnus-dormant-mark)
762          (= mark gnus-ticked-mark))
763      . gnus-summary-normal-ticked-face)
764     ((and (> score default) (= mark gnus-ancient-mark))
765      . gnus-summary-high-ancient-face)
766     ((and (< score default) (= mark gnus-ancient-mark))
767      . gnus-summary-low-ancient-face)
768     ((= mark gnus-ancient-mark)
769      . gnus-summary-normal-ancient-face)
770     ((and (> score default) (= mark gnus-unread-mark))
771      . gnus-summary-high-unread-face)
772     ((and (< score default) (= mark gnus-unread-mark))
773      . gnus-summary-low-unread-face)
774     ((= mark gnus-unread-mark)
775      . gnus-summary-normal-unread-face)
776     ((and (> score default) (memq mark (list gnus-downloadable-mark
777                                              gnus-undownloaded-mark)))
778      . gnus-summary-high-unread-face)
779     ((and (< score default) (memq mark (list gnus-downloadable-mark
780                                              gnus-undownloaded-mark)))
781      . gnus-summary-low-unread-face)
782     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
783      . gnus-summary-normal-unread-face)
784     ((> score default)
785      . gnus-summary-high-read-face)
786     ((< score default)
787      . gnus-summary-low-read-face)
788     (t
789      . gnus-summary-normal-read-face))
790   "*Controls the highlighting of summary buffer lines.
791
792 A list of (FORM . FACE) pairs.  When deciding how a a particular
793 summary line should be displayed, each form is evaluated.  The content
794 of the face field after the first true form is used.  You can change
795 how those summary lines are displayed, by editing the face field.
796
797 You can use the following variables in the FORM field.
798
799 score:   The articles score
800 default: The default article score.
801 below:   The score below which articles are automatically marked as read.
802 mark:    The articles mark."
803   :group 'gnus-summary-visual
804   :type '(repeat (cons (sexp :tag "Form" nil)
805                        face)))
806
807 (defcustom gnus-alter-header-function nil
808   "Function called to allow alteration of article header structures.
809 The function is called with one parameter, the article header vector,
810 which it may alter in any way.")
811
812 (defvar gnus-decode-encoded-word-function
813   (mime-find-field-decoder 'From 'nov)
814   "Variable that says which function should be used to decode a string with encoded words.")
815
816 (defcustom gnus-extra-headers nil
817   "*Extra headers to parse."
818   :group 'gnus-summary
819   :type '(repeat symbol))
820
821 (defcustom gnus-ignored-from-addresses
822   (and user-mail-address (regexp-quote user-mail-address))
823   "*Regexp of From headers that may be suppressed in favor of To headers."
824   :group 'gnus-summary
825   :type 'regexp)
826
827 (defcustom gnus-group-charset-alist
828   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
829     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
830     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
831     ("^relcom\\>" koi8-r)
832     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
833     ("^israel\\>" iso-8859-1)
834     ("^han\\>" euc-kr)
835     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
836     (".*" iso-8859-1))
837   "Alist of regexps (to match group names) and default charsets to be used when reading."
838   :type '(repeat (list (regexp :tag "Group")
839                        (symbol :tag "Charset")))
840   :group 'gnus-charset)
841
842 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
843   "List of charsets that should be ignored.
844 When these charsets are used in the \"charset\" parameter, the
845 default charset will be used instead."
846   :type '(repeat symbol)
847   :group 'gnus-charset)
848
849 (defcustom gnus-group-ignored-charsets-alist 
850   '(("alt\\.chinese\\.text" iso-8859-1))
851   "Alist of regexps (to match group names) and charsets that should be ignored.
852 When these charsets are used in the \"charset\" parameter, the
853 default charset will be used instead."
854   :type '(repeat (cons (regexp :tag "Group")
855                        (repeat symbol)))
856   :group 'gnus-charset)
857
858 (defcustom gnus-group-highlight-words-alist nil
859   "Alist of group regexps and highlight regexps.
860 This variable uses the same syntax as `gnus-emphasis-alist'."
861   :type '(repeat (cons (regexp :tag "Group")
862                        (repeat (list (regexp :tag "Highlight regexp")
863                                      (number :tag "Group for entire word" 0)
864                                      (number :tag "Group for displayed part" 0)
865                                      (symbol :tag "Face" 
866                                              gnus-emphasis-highlight-words)))))
867   :group 'gnus-summary-visual)
868
869 (defcustom gnus-use-wheel nil
870   "Use Intelli-mouse on summary movement"
871   :type 'boolean
872   :group 'gnus-summary-maneuvering)
873
874 (defcustom gnus-wheel-scroll-amount '(5 . 1)
875   "Amount to scroll messages by spinning the mouse wheel.
876 This is actually a cons cell, where the first item is the amount to scroll
877 on a normal wheel event, and the second is the amount to scroll when the
878 wheel is moved with the shift key depressed."
879   :type '(cons (integer :tag "Shift") integer)
880   :group 'gnus-summary-maneuvering)
881
882 (defcustom gnus-wheel-edge-resistance 2
883   "How hard it should be to change the current article
884 by moving the mouse over the edge of the article window."
885   :type 'integer
886   :group 'gnus-summary-maneuvering)
887
888 ;;; Internal variables
889
890 (defvar gnus-scores-exclude-files nil)
891 (defvar gnus-page-broken nil)
892 (defvar gnus-inhibit-mime-unbuttonizing nil)
893
894 (defvar gnus-original-article nil)
895 (defvar gnus-article-internal-prepare-hook nil)
896 (defvar gnus-newsgroup-process-stack nil)
897
898 (defvar gnus-thread-indent-array nil)
899 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
900
901 ;; Avoid highlighting in kill files.
902 (defvar gnus-summary-inhibit-highlight nil)
903 (defvar gnus-newsgroup-selected-overlay nil)
904 (defvar gnus-inhibit-limiting nil)
905 (defvar gnus-newsgroup-adaptive-score-file nil)
906 (defvar gnus-current-score-file nil)
907 (defvar gnus-current-move-group nil)
908 (defvar gnus-current-copy-group nil)
909 (defvar gnus-current-crosspost-group nil)
910
911 (defvar gnus-newsgroup-dependencies nil)
912 (defvar gnus-newsgroup-adaptive nil)
913 (defvar gnus-summary-display-article-function nil)
914 (defvar gnus-summary-highlight-line-function nil
915   "Function called after highlighting a summary line.")
916
917 (defvar gnus-summary-line-format-alist
918   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
919     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
920     (?s gnus-tmp-subject-or-nil ?s)
921     (?n gnus-tmp-name ?s)
922     (?A (std11-address-string
923          (car (mime-read-field 'From gnus-tmp-header))) ?s)
924     (?a (or (std11-full-name-string
925              (car (mime-read-field 'From gnus-tmp-header)))
926             gnus-tmp-from) ?s)
927     (?F gnus-tmp-from ?s)
928     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
929     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
930     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
931     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
932     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
933     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
934     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
935     (?L gnus-tmp-lines ?d)
936     (?I gnus-tmp-indentation ?s)
937     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
938     (?R gnus-tmp-replied ?c)
939     (?\[ gnus-tmp-opening-bracket ?c)
940     (?\] gnus-tmp-closing-bracket ?c)
941     (?\> (make-string gnus-tmp-level ? ) ?s)
942     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
943     (?i gnus-tmp-score ?d)
944     (?z gnus-tmp-score-char ?c)
945     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
946     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
947     (?U gnus-tmp-unread ?c)
948     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
949     (?t (gnus-summary-number-of-articles-in-thread
950          (and (boundp 'thread) (car thread)) gnus-tmp-level)
951         ?d)
952     (?e (gnus-summary-number-of-articles-in-thread
953          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
954         ?c)
955     (?u gnus-tmp-user-defined ?s)
956     (?P (gnus-pick-line-number) ?d))
957   "An alist of format specifications that can appear in summary lines,
958 and what variables they correspond with, along with the type of the
959 variable (string, integer, character, etc).")
960
961 (defvar gnus-summary-dummy-line-format-alist
962   `((?S gnus-tmp-subject ?s)
963     (?N gnus-tmp-number ?d)
964     (?u gnus-tmp-user-defined ?s)))
965
966 (defvar gnus-summary-mode-line-format-alist
967   `((?G gnus-tmp-group-name ?s)
968     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
969     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
970     (?A gnus-tmp-article-number ?d)
971     (?Z gnus-tmp-unread-and-unselected ?s)
972     (?V gnus-version ?s)
973     (?U gnus-tmp-unread-and-unticked ?d)
974     (?S gnus-tmp-subject ?s)
975     (?e gnus-tmp-unselected ?d)
976     (?u gnus-tmp-user-defined ?s)
977     (?d (length gnus-newsgroup-dormant) ?d)
978     (?t (length gnus-newsgroup-marked) ?d)
979     (?r (length gnus-newsgroup-reads) ?d)
980     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
981     (?E gnus-newsgroup-expunged-tally ?d)
982     (?s (gnus-current-score-file-nondirectory) ?s)))
983
984 (defvar gnus-last-search-regexp nil
985   "Default regexp for article search command.")
986
987 (defvar gnus-last-shell-command nil
988   "Default shell command on article.")
989
990 (defvar gnus-newsgroup-begin nil)
991 (defvar gnus-newsgroup-end nil)
992 (defvar gnus-newsgroup-last-rmail nil)
993 (defvar gnus-newsgroup-last-mail nil)
994 (defvar gnus-newsgroup-last-folder nil)
995 (defvar gnus-newsgroup-last-file nil)
996 (defvar gnus-newsgroup-auto-expire nil)
997 (defvar gnus-newsgroup-active nil)
998
999 (defvar gnus-newsgroup-data nil)
1000 (defvar gnus-newsgroup-data-reverse nil)
1001 (defvar gnus-newsgroup-limit nil)
1002 (defvar gnus-newsgroup-limits nil)
1003
1004 (defvar gnus-newsgroup-unreads nil
1005   "List of unread articles in the current newsgroup.")
1006
1007 (defvar gnus-newsgroup-unselected nil
1008   "List of unselected unread articles in the current newsgroup.")
1009
1010 (defvar gnus-newsgroup-reads nil
1011   "Alist of read articles and article marks in the current newsgroup.")
1012
1013 (defvar gnus-newsgroup-expunged-tally nil)
1014
1015 (defvar gnus-newsgroup-marked nil
1016   "List of ticked articles in the current newsgroup (a subset of unread art).")
1017
1018 (defvar gnus-newsgroup-killed nil
1019   "List of ranges of articles that have been through the scoring process.")
1020
1021 (defvar gnus-newsgroup-cached nil
1022   "List of articles that come from the article cache.")
1023
1024 (defvar gnus-newsgroup-saved nil
1025   "List of articles that have been saved.")
1026
1027 (defvar gnus-newsgroup-kill-headers nil)
1028
1029 (defvar gnus-newsgroup-replied nil
1030   "List of articles that have been replied to in the current newsgroup.")
1031
1032 (defvar gnus-newsgroup-expirable nil
1033   "List of articles in the current newsgroup that can be expired.")
1034
1035 (defvar gnus-newsgroup-processable nil
1036   "List of articles in the current newsgroup that can be processed.")
1037
1038 (defvar gnus-newsgroup-downloadable nil
1039   "List of articles in the current newsgroup that can be processed.")
1040
1041 (defvar gnus-newsgroup-undownloaded nil
1042   "List of articles in the current newsgroup that haven't been downloaded..")
1043
1044 (defvar gnus-newsgroup-unsendable nil
1045   "List of articles in the current newsgroup that won't be sent.")
1046
1047 (defvar gnus-newsgroup-bookmarks nil
1048   "List of articles in the current newsgroup that have bookmarks.")
1049
1050 (defvar gnus-newsgroup-dormant nil
1051   "List of dormant articles in the current newsgroup.")
1052
1053 (defvar gnus-newsgroup-scored nil
1054   "List of scored articles in the current newsgroup.")
1055
1056 (defvar gnus-newsgroup-headers nil
1057   "List of article headers in the current newsgroup.")
1058
1059 (defvar gnus-newsgroup-threads nil)
1060
1061 (defvar gnus-newsgroup-prepared nil
1062   "Whether the current group has been prepared properly.")
1063
1064 (defvar gnus-newsgroup-ancient nil
1065   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1066
1067 (defvar gnus-newsgroup-sparse nil)
1068
1069 (defvar gnus-current-article nil)
1070 (defvar gnus-article-current nil)
1071 (defvar gnus-current-headers nil)
1072 (defvar gnus-have-all-headers nil)
1073 (defvar gnus-last-article nil)
1074 (defvar gnus-newsgroup-history nil)
1075 (defvar gnus-newsgroup-charset nil)
1076 (defvar gnus-newsgroup-ephemeral-charset nil)
1077 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1078
1079 (defconst gnus-summary-local-variables
1080   '(gnus-newsgroup-name
1081     gnus-newsgroup-begin gnus-newsgroup-end
1082     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1083     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1084     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1085     gnus-newsgroup-unselected gnus-newsgroup-marked
1086     gnus-newsgroup-reads gnus-newsgroup-saved
1087     gnus-newsgroup-replied gnus-newsgroup-expirable
1088     gnus-newsgroup-processable gnus-newsgroup-killed
1089     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1090     gnus-newsgroup-unsendable
1091     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1092     gnus-newsgroup-headers gnus-newsgroup-threads
1093     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1094     gnus-current-article gnus-current-headers gnus-have-all-headers
1095     gnus-last-article gnus-article-internal-prepare-hook
1096     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1097     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1098     gnus-thread-expunge-below
1099     gnus-score-alist gnus-current-score-file
1100     (gnus-summary-expunge-below . global)
1101     (gnus-summary-mark-below . global)
1102     gnus-newsgroup-active gnus-scores-exclude-files
1103     gnus-newsgroup-history gnus-newsgroup-ancient
1104     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1105     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1106     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1107     (gnus-newsgroup-expunged-tally . 0)
1108     gnus-cache-removable-articles gnus-newsgroup-cached
1109     gnus-newsgroup-data gnus-newsgroup-data-reverse
1110     gnus-newsgroup-limit gnus-newsgroup-limits
1111     gnus-newsgroup-charset)
1112   "Variables that are buffer-local to the summary buffers.")
1113
1114 ;; Byte-compiler warning.
1115 (defvar gnus-article-mode-map)
1116
1117 ;; Subject simplification.
1118
1119 (defun gnus-simplify-whitespace (str)
1120   "Remove excessive whitespace."
1121   (let ((mystr str))
1122     ;; Multiple spaces.
1123     (while (string-match "[ \t][ \t]+" mystr)
1124       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1125                           " "
1126                           (substring mystr (match-end 0)))))
1127     ;; Leading spaces.
1128     (when (string-match "^[ \t]+" mystr)
1129       (setq mystr (substring mystr (match-end 0))))
1130     ;; Trailing spaces.
1131     (when (string-match "[ \t]+$" mystr)
1132       (setq mystr (substring mystr 0 (match-beginning 0))))
1133     mystr))
1134
1135 (defsubst gnus-simplify-subject-re (subject)
1136   "Remove \"Re:\" from subject lines."
1137   (if (string-match "^[Rr][Ee]: *" subject)
1138       (substring subject (match-end 0))
1139     subject))
1140
1141 (defun gnus-simplify-subject (subject &optional re-only)
1142   "Remove `Re:' and words in parentheses.
1143 If RE-ONLY is non-nil, strip leading `Re:'s only."
1144   (let ((case-fold-search t))           ;Ignore case.
1145     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1146     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1147       (setq subject (substring subject (match-end 0))))
1148     ;; Remove uninteresting prefixes.
1149     (when (and (not re-only)
1150                gnus-simplify-ignored-prefixes
1151                (string-match gnus-simplify-ignored-prefixes subject))
1152       (setq subject (substring subject (match-end 0))))
1153     ;; Remove words in parentheses from end.
1154     (unless re-only
1155       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1156         (setq subject (substring subject 0 (match-beginning 0)))))
1157     ;; Return subject string.
1158     subject))
1159
1160 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1161 ;; all whitespace.
1162 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1163   (goto-char (point-min))
1164   (while (re-search-forward regexp nil t)
1165       (replace-match (or newtext ""))))
1166
1167 (defun gnus-simplify-buffer-fuzzy ()
1168   "Simplify string in the buffer fuzzily.
1169 The string in the accessible portion of the current buffer is simplified.
1170 It is assumed to be a single-line subject.
1171 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1172 matter is removed.  Additional things can be deleted by setting
1173 gnus-simplify-subject-fuzzy-regexp."
1174   (let ((case-fold-search t)
1175         (modified-tick))
1176     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1177
1178     (while (not (eq modified-tick (buffer-modified-tick)))
1179       (setq modified-tick (buffer-modified-tick))
1180       (cond
1181        ((listp gnus-simplify-subject-fuzzy-regexp)
1182         (mapcar 'gnus-simplify-buffer-fuzzy-step
1183                 gnus-simplify-subject-fuzzy-regexp))
1184        (gnus-simplify-subject-fuzzy-regexp
1185         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1186       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1187       (gnus-simplify-buffer-fuzzy-step
1188        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1189       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1190
1191     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1192     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1193     (gnus-simplify-buffer-fuzzy-step " $")
1194     (gnus-simplify-buffer-fuzzy-step "^ +")))
1195
1196 (defun gnus-simplify-subject-fuzzy (subject)
1197   "Simplify a subject string fuzzily.
1198 See `gnus-simplify-buffer-fuzzy' for details."
1199   (save-excursion
1200     (gnus-set-work-buffer)
1201     (let ((case-fold-search t))
1202       ;; Remove uninteresting prefixes.
1203       (when (and gnus-simplify-ignored-prefixes
1204                  (string-match gnus-simplify-ignored-prefixes subject))
1205         (setq subject (substring subject (match-end 0))))
1206       (insert subject)
1207       (inline (gnus-simplify-buffer-fuzzy))
1208       (buffer-string))))
1209
1210 (defsubst gnus-simplify-subject-fully (subject)
1211   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1212   (cond
1213    (gnus-simplify-subject-functions
1214     (gnus-map-function gnus-simplify-subject-functions subject))
1215    ((null gnus-summary-gather-subject-limit)
1216     (gnus-simplify-subject-re subject))
1217    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1218     (gnus-simplify-subject-fuzzy subject))
1219    ((numberp gnus-summary-gather-subject-limit)
1220     (gnus-limit-string (gnus-simplify-subject-re subject)
1221                        gnus-summary-gather-subject-limit))
1222    (t
1223     subject)))
1224
1225 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1226   "Check whether two subjects are equal.
1227 If optional argument simple-first is t, first argument is already
1228 simplified."
1229   (cond
1230    ((null simple-first)
1231     (equal (gnus-simplify-subject-fully s1)
1232            (gnus-simplify-subject-fully s2)))
1233    (t
1234     (equal s1
1235            (gnus-simplify-subject-fully s2)))))
1236
1237 (defun gnus-summary-bubble-group ()
1238   "Increase the score of the current group.
1239 This is a handy function to add to `gnus-summary-exit-hook' to
1240 increase the score of each group you read."
1241   (gnus-group-add-score gnus-newsgroup-name))
1242
1243 \f
1244 ;;;
1245 ;;; Gnus summary mode
1246 ;;;
1247
1248 (put 'gnus-summary-mode 'mode-class 'special)
1249
1250 (when t
1251   ;; Non-orthogonal keys
1252
1253   (gnus-define-keys gnus-summary-mode-map
1254     " " gnus-summary-next-page
1255     "\177" gnus-summary-prev-page
1256     [delete] gnus-summary-prev-page
1257     [backspace] gnus-summary-prev-page
1258     "\r" gnus-summary-scroll-up
1259     "\M-\r" gnus-summary-scroll-down
1260     "n" gnus-summary-next-unread-article
1261     "p" gnus-summary-prev-unread-article
1262     "N" gnus-summary-next-article
1263     "P" gnus-summary-prev-article
1264     "\M-\C-n" gnus-summary-next-same-subject
1265     "\M-\C-p" gnus-summary-prev-same-subject
1266     "\M-n" gnus-summary-next-unread-subject
1267     "\M-p" gnus-summary-prev-unread-subject
1268     "." gnus-summary-first-unread-article
1269     "," gnus-summary-best-unread-article
1270     "\M-s" gnus-summary-search-article-forward
1271     "\M-r" gnus-summary-search-article-backward
1272     "<" gnus-summary-beginning-of-article
1273     ">" gnus-summary-end-of-article
1274     "j" gnus-summary-goto-article
1275     "^" gnus-summary-refer-parent-article
1276     "\M-^" gnus-summary-refer-article
1277     "u" gnus-summary-tick-article-forward
1278     "!" gnus-summary-tick-article-forward
1279     "U" gnus-summary-tick-article-backward
1280     "d" gnus-summary-mark-as-read-forward
1281     "D" gnus-summary-mark-as-read-backward
1282     "E" gnus-summary-mark-as-expirable
1283     "\M-u" gnus-summary-clear-mark-forward
1284     "\M-U" gnus-summary-clear-mark-backward
1285     "k" gnus-summary-kill-same-subject-and-select
1286     "\C-k" gnus-summary-kill-same-subject
1287     "\M-\C-k" gnus-summary-kill-thread
1288     "\M-\C-l" gnus-summary-lower-thread
1289     "e" gnus-summary-edit-article
1290     "#" gnus-summary-mark-as-processable
1291     "\M-#" gnus-summary-unmark-as-processable
1292     "\M-\C-t" gnus-summary-toggle-threads
1293     "\M-\C-s" gnus-summary-show-thread
1294     "\M-\C-h" gnus-summary-hide-thread
1295     "\M-\C-f" gnus-summary-next-thread
1296     "\M-\C-b" gnus-summary-prev-thread
1297     "\M-\C-u" gnus-summary-up-thread
1298     "\M-\C-d" gnus-summary-down-thread
1299     "&" gnus-summary-execute-command
1300     "c" gnus-summary-catchup-and-exit
1301     "\C-w" gnus-summary-mark-region-as-read
1302     "\C-t" gnus-summary-toggle-truncation
1303     "?" gnus-summary-mark-as-dormant
1304     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1305     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1306     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1307     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1308     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1309     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1310     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1311     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1312     "=" gnus-summary-expand-window
1313     "\C-x\C-s" gnus-summary-reselect-current-group
1314     "\M-g" gnus-summary-rescan-group
1315     "w" gnus-summary-stop-page-breaking
1316     "\C-c\C-r" gnus-summary-caesar-message
1317     "\M-t" gnus-summary-toggle-mime
1318     "f" gnus-summary-followup
1319     "F" gnus-summary-followup-with-original
1320     "C" gnus-summary-cancel-article
1321     "r" gnus-summary-reply
1322     "R" gnus-summary-reply-with-original
1323     "\C-c\C-f" gnus-summary-mail-forward
1324     "o" gnus-summary-save-article
1325     "\C-o" gnus-summary-save-article-mail
1326     "|" gnus-summary-pipe-output
1327     "\M-k" gnus-summary-edit-local-kill
1328     "\M-K" gnus-summary-edit-global-kill
1329     ;; "V" gnus-version
1330     "\C-c\C-d" gnus-summary-describe-group
1331     "q" gnus-summary-exit
1332     "Q" gnus-summary-exit-no-update
1333     "\C-c\C-i" gnus-info-find-node
1334     gnus-mouse-2 gnus-mouse-pick-article
1335     "m" gnus-summary-mail-other-window
1336     "a" gnus-summary-post-news
1337     "x" gnus-summary-limit-to-unread
1338     "s" gnus-summary-isearch-article
1339     "t" gnus-article-toggle-headers
1340     "g" gnus-summary-show-article
1341     "l" gnus-summary-goto-last-article
1342     "v" gnus-summary-preview-mime-message
1343     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1344     "\C-d" gnus-summary-enter-digest-group
1345     "\M-\C-d" gnus-summary-read-document
1346     "\M-\C-e" gnus-summary-edit-parameters
1347     "\M-\C-g" gnus-summary-customize-parameters
1348     "\C-c\C-b" gnus-bug
1349     "*" gnus-cache-enter-article
1350     "\M-*" gnus-cache-remove-article
1351     "\M-&" gnus-summary-universal-argument
1352     "\C-l" gnus-recenter
1353     "I" gnus-summary-increase-score
1354     "L" gnus-summary-lower-score
1355     "\M-i" gnus-symbolic-argument
1356     "h" gnus-summary-select-article-buffer
1357
1358     "V" gnus-summary-score-map
1359     "X" gnus-uu-extract-map
1360     "S" gnus-summary-send-map)
1361
1362   ;; Sort of orthogonal keymap
1363   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1364     "t" gnus-summary-tick-article-forward
1365     "!" gnus-summary-tick-article-forward
1366     "d" gnus-summary-mark-as-read-forward
1367     "r" gnus-summary-mark-as-read-forward
1368     "c" gnus-summary-clear-mark-forward
1369     " " gnus-summary-clear-mark-forward
1370     "e" gnus-summary-mark-as-expirable
1371     "x" gnus-summary-mark-as-expirable
1372     "?" gnus-summary-mark-as-dormant
1373     "b" gnus-summary-set-bookmark
1374     "B" gnus-summary-remove-bookmark
1375     "#" gnus-summary-mark-as-processable
1376     "\M-#" gnus-summary-unmark-as-processable
1377     "S" gnus-summary-limit-include-expunged
1378     "C" gnus-summary-catchup
1379     "H" gnus-summary-catchup-to-here
1380     "\C-c" gnus-summary-catchup-all
1381     "k" gnus-summary-kill-same-subject-and-select
1382     "K" gnus-summary-kill-same-subject
1383     "P" gnus-uu-mark-map)
1384
1385   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1386     "c" gnus-summary-clear-above
1387     "u" gnus-summary-tick-above
1388     "m" gnus-summary-mark-above
1389     "k" gnus-summary-kill-below)
1390
1391   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1392     "/" gnus-summary-limit-to-subject
1393     "n" gnus-summary-limit-to-articles
1394     "w" gnus-summary-pop-limit
1395     "s" gnus-summary-limit-to-subject
1396     "a" gnus-summary-limit-to-author
1397     "u" gnus-summary-limit-to-unread
1398     "m" gnus-summary-limit-to-marks
1399     "M" gnus-summary-limit-exclude-marks
1400     "v" gnus-summary-limit-to-score
1401     "*" gnus-summary-limit-include-cached
1402     "D" gnus-summary-limit-include-dormant
1403     "T" gnus-summary-limit-include-thread
1404     "d" gnus-summary-limit-exclude-dormant
1405     "t" gnus-summary-limit-to-age
1406     "x" gnus-summary-limit-to-extra 
1407     "E" gnus-summary-limit-include-expunged
1408     "c" gnus-summary-limit-exclude-childless-dormant
1409     "C" gnus-summary-limit-mark-excluded-as-read)
1410
1411   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1412     "n" gnus-summary-next-unread-article
1413     "p" gnus-summary-prev-unread-article
1414     "N" gnus-summary-next-article
1415     "P" gnus-summary-prev-article
1416     "\C-n" gnus-summary-next-same-subject
1417     "\C-p" gnus-summary-prev-same-subject
1418     "\M-n" gnus-summary-next-unread-subject
1419     "\M-p" gnus-summary-prev-unread-subject
1420     "f" gnus-summary-first-unread-article
1421     "b" gnus-summary-best-unread-article
1422     "j" gnus-summary-goto-article
1423     "g" gnus-summary-goto-subject
1424     "l" gnus-summary-goto-last-article
1425     "o" gnus-summary-pop-article)
1426
1427   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1428     "k" gnus-summary-kill-thread
1429     "l" gnus-summary-lower-thread
1430     "i" gnus-summary-raise-thread
1431     "T" gnus-summary-toggle-threads
1432     "t" gnus-summary-rethread-current
1433     "^" gnus-summary-reparent-thread
1434     "s" gnus-summary-show-thread
1435     "S" gnus-summary-show-all-threads
1436     "h" gnus-summary-hide-thread
1437     "H" gnus-summary-hide-all-threads
1438     "n" gnus-summary-next-thread
1439     "p" gnus-summary-prev-thread
1440     "u" gnus-summary-up-thread
1441     "o" gnus-summary-top-thread
1442     "d" gnus-summary-down-thread
1443     "#" gnus-uu-mark-thread
1444     "\M-#" gnus-uu-unmark-thread)
1445
1446   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1447     "g" gnus-summary-prepare
1448     "c" gnus-summary-insert-cached-articles)
1449
1450   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1451     "c" gnus-summary-catchup-and-exit
1452     "C" gnus-summary-catchup-all-and-exit
1453     "E" gnus-summary-exit-no-update
1454     "J" gnus-summary-jump-to-other-group
1455     "Q" gnus-summary-exit
1456     "Z" gnus-summary-exit
1457     "n" gnus-summary-catchup-and-goto-next-group
1458     "R" gnus-summary-reselect-current-group
1459     "G" gnus-summary-rescan-group
1460     "N" gnus-summary-next-group
1461     "s" gnus-summary-save-newsrc
1462     "P" gnus-summary-prev-group)
1463
1464   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1465     " " gnus-summary-next-page
1466     "n" gnus-summary-next-page
1467     "\177" gnus-summary-prev-page
1468     [delete] gnus-summary-prev-page
1469     "p" gnus-summary-prev-page
1470     "\r" gnus-summary-scroll-up
1471     "\M-\r" gnus-summary-scroll-down
1472     "<" gnus-summary-beginning-of-article
1473     ">" gnus-summary-end-of-article
1474     "b" gnus-summary-beginning-of-article
1475     "e" gnus-summary-end-of-article
1476     "^" gnus-summary-refer-parent-article
1477     "r" gnus-summary-refer-parent-article
1478     "D" gnus-summary-enter-digest-group
1479     "R" gnus-summary-refer-references
1480     "T" gnus-summary-refer-thread
1481     "g" gnus-summary-show-article
1482     "s" gnus-summary-isearch-article
1483     "P" gnus-summary-print-article
1484     "t" gnus-article-babel)
1485
1486   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1487     "b" gnus-article-add-buttons
1488     "B" gnus-article-add-buttons-to-head
1489     "o" gnus-article-treat-overstrike
1490     "e" gnus-article-emphasize
1491     "w" gnus-article-fill-cited-article
1492     "Q" gnus-article-fill-long-lines
1493     "C" gnus-article-capitalize-sentences
1494     "c" gnus-article-remove-cr
1495     "f" gnus-article-display-x-face
1496     "l" gnus-summary-stop-page-breaking
1497     "r" gnus-summary-caesar-message
1498     "t" gnus-article-toggle-headers
1499     "v" gnus-summary-verbose-headers
1500     "m" gnus-summary-toggle-mime
1501     "H" gnus-article-strip-headers-in-body
1502     "d" gnus-article-treat-dumbquotes)
1503
1504   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1505     "a" gnus-article-hide
1506     "h" gnus-article-toggle-headers
1507     "b" gnus-article-hide-boring-headers
1508     "s" gnus-article-hide-signature
1509     "c" gnus-article-hide-citation
1510     "C" gnus-article-hide-citation-in-followups
1511     "p" gnus-article-hide-pgp
1512     "B" gnus-article-strip-banner
1513     "P" gnus-article-hide-pem
1514     "\C-c" gnus-article-hide-citation-maybe)
1515
1516   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1517     "a" gnus-article-highlight
1518     "h" gnus-article-highlight-headers
1519     "c" gnus-article-highlight-citation
1520     "s" gnus-article-highlight-signature)
1521
1522   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1523     "z" gnus-article-date-ut
1524     "u" gnus-article-date-ut
1525     "l" gnus-article-date-local
1526     "e" gnus-article-date-lapsed
1527     "o" gnus-article-date-original
1528     "i" gnus-article-date-iso8601
1529     "s" gnus-article-date-user)
1530
1531   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1532     "t" gnus-article-remove-trailing-blank-lines
1533     "l" gnus-article-strip-leading-blank-lines
1534     "m" gnus-article-strip-multiple-blank-lines
1535     "a" gnus-article-strip-blank-lines
1536     "A" gnus-article-strip-all-blank-lines
1537     "s" gnus-article-strip-leading-space
1538     "e" gnus-article-strip-trailing-space)
1539
1540   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1541     "v" gnus-version
1542     "f" gnus-summary-fetch-faq
1543     "d" gnus-summary-describe-group
1544     "h" gnus-summary-describe-briefly
1545     "i" gnus-info-find-node)
1546
1547   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1548     "e" gnus-summary-expire-articles
1549     "\M-\C-e" gnus-summary-expire-articles-now
1550     "\177" gnus-summary-delete-article
1551     [delete] gnus-summary-delete-article
1552     [backspace] gnus-summary-delete-article
1553     "m" gnus-summary-move-article
1554     "r" gnus-summary-respool-article
1555     "w" gnus-summary-edit-article
1556     "c" gnus-summary-copy-article
1557     "B" gnus-summary-crosspost-article
1558     "q" gnus-summary-respool-query
1559     "t" gnus-summary-respool-trace
1560     "i" gnus-summary-import-article
1561     "p" gnus-summary-article-posted-p)
1562
1563   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1564     "o" gnus-summary-save-article
1565     "m" gnus-summary-save-article-mail
1566     "F" gnus-summary-write-article-file
1567     "r" gnus-summary-save-article-rmail
1568     "f" gnus-summary-save-article-file
1569     "b" gnus-summary-save-article-body-file
1570     "h" gnus-summary-save-article-folder
1571     "v" gnus-summary-save-article-vm
1572     "p" gnus-summary-pipe-output
1573     "s" gnus-soup-add-article)
1574
1575   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1576     "b" gnus-summary-display-buttonized
1577     "m" gnus-summary-repair-multipart
1578     "v" gnus-article-view-part
1579     "o" gnus-article-save-part
1580     "c" gnus-article-copy-part
1581     "e" gnus-article-externalize-part
1582     "i" gnus-article-inline-part
1583     "|" gnus-article-pipe-part)
1584   )
1585
1586 (defun gnus-summary-make-menu-bar ()
1587   (gnus-turn-off-edit-menu 'summary)
1588
1589   (unless (boundp 'gnus-summary-misc-menu)
1590
1591     (easy-menu-define
1592      gnus-summary-kill-menu gnus-summary-mode-map ""
1593      (cons
1594       "Score"
1595       (nconc
1596        (list
1597         ["Enter score..." gnus-summary-score-entry t]
1598         ["Customize" gnus-score-customize t])
1599        (gnus-make-score-map 'increase)
1600        (gnus-make-score-map 'lower)
1601        '(("Mark"
1602           ["Kill below" gnus-summary-kill-below t]
1603           ["Mark above" gnus-summary-mark-above t]
1604           ["Tick above" gnus-summary-tick-above t]
1605           ["Clear above" gnus-summary-clear-above t])
1606          ["Current score" gnus-summary-current-score t]
1607          ["Set score" gnus-summary-set-score t]
1608          ["Switch current score file..." gnus-score-change-score-file t]
1609          ["Set mark below..." gnus-score-set-mark-below t]
1610          ["Set expunge below..." gnus-score-set-expunge-below t]
1611          ["Edit current score file" gnus-score-edit-current-scores t]
1612          ["Edit score file" gnus-score-edit-file t]
1613          ["Trace score" gnus-score-find-trace t]
1614          ["Find words" gnus-score-find-favourite-words t]
1615          ["Rescore buffer" gnus-summary-rescore t]
1616          ["Increase score..." gnus-summary-increase-score t]
1617          ["Lower score..." gnus-summary-lower-score t]))))
1618
1619     ;; Define both the Article menu in the summary buffer and the equivalent
1620     ;; Commands menu in the article buffer here for consistency.
1621     (let ((innards
1622            '(("Hide"
1623               ["All" gnus-article-hide t]
1624               ["Headers" gnus-article-toggle-headers t]
1625               ["Signature" gnus-article-hide-signature t]
1626               ["Citation" gnus-article-hide-citation t]
1627               ["PGP" gnus-article-hide-pgp t]
1628               ["Banner" gnus-article-strip-banner t]
1629               ["Boring headers" gnus-article-hide-boring-headers t])
1630              ("Highlight"
1631               ["All" gnus-article-highlight t]
1632               ["Headers" gnus-article-highlight-headers t]
1633               ["Signature" gnus-article-highlight-signature t]
1634               ["Citation" gnus-article-highlight-citation t])
1635              ("Date"
1636               ["Local" gnus-article-date-local t]
1637               ["ISO8601" gnus-article-date-iso8601 t]
1638               ["UT" gnus-article-date-ut t]
1639               ["Original" gnus-article-date-original t]
1640               ["Lapsed" gnus-article-date-lapsed t]
1641               ["User-defined" gnus-article-date-user t])
1642              ("Washing"
1643               ("Remove Blanks"
1644                ["Leading" gnus-article-strip-leading-blank-lines t]
1645                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1646                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1647                ["All of the above" gnus-article-strip-blank-lines t]
1648                ["All" gnus-article-strip-all-blank-lines t]
1649                ["Leading space" gnus-article-strip-leading-space t]
1650                ["Trailing space" gnus-article-strip-trailing-space t])
1651               ["Overstrike" gnus-article-treat-overstrike t]
1652               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1653               ["Emphasis" gnus-article-emphasize t]
1654               ["Word wrap" gnus-article-fill-cited-article t]
1655               ["Fill long lines" gnus-article-fill-long-lines t]
1656               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1657               ["CR" gnus-article-remove-cr t]
1658               ["Show X-Face" gnus-article-display-x-face t]
1659               ["Rot 13" gnus-summary-caesar-message t]
1660               ["Unix pipe" gnus-summary-pipe-message t]
1661               ["Add buttons" gnus-article-add-buttons t]
1662               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1663               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1664               ["Toggle MIME" gnus-summary-toggle-mime t]
1665               ["Verbose header" gnus-summary-verbose-headers t]
1666               ["Toggle header" gnus-summary-toggle-header t])
1667              ("Output"
1668               ["Save in default format" gnus-summary-save-article t]
1669               ["Save in file" gnus-summary-save-article-file t]
1670               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1671               ["Save in MH folder" gnus-summary-save-article-folder t]
1672               ["Save in VM folder" gnus-summary-save-article-vm t]
1673               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1674               ["Save body in file" gnus-summary-save-article-body-file t]
1675               ["Pipe through a filter" gnus-summary-pipe-output t]
1676               ["Add to SOUP packet" gnus-soup-add-article t]
1677               ["Print" gnus-summary-print-article t])
1678              ("Backend"
1679               ["Respool article..." gnus-summary-respool-article t]
1680               ["Move article..." gnus-summary-move-article
1681                (gnus-check-backend-function
1682                 'request-move-article gnus-newsgroup-name)]
1683               ["Copy article..." gnus-summary-copy-article t]
1684               ["Crosspost article..." gnus-summary-crosspost-article
1685                (gnus-check-backend-function
1686                 'request-replace-article gnus-newsgroup-name)]
1687               ["Import file..." gnus-summary-import-article t]
1688               ["Check if posted" gnus-summary-article-posted-p t]
1689               ["Edit article" gnus-summary-edit-article
1690                (not (gnus-group-read-only-p))]
1691               ["Delete article" gnus-summary-delete-article
1692                (gnus-check-backend-function
1693                 'request-expire-articles gnus-newsgroup-name)]
1694               ["Query respool" gnus-summary-respool-query t]
1695               ["Trace respool" gnus-summary-respool-trace t]
1696               ["Delete expirable articles" gnus-summary-expire-articles-now
1697                (gnus-check-backend-function
1698                 'request-expire-articles gnus-newsgroup-name)])
1699              ("Extract"
1700               ["Uudecode" gnus-uu-decode-uu t]
1701               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1702               ["Unshar" gnus-uu-decode-unshar t]
1703               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1704               ["Save" gnus-uu-decode-save t]
1705               ["Binhex" gnus-uu-decode-binhex t]
1706               ["Postscript" gnus-uu-decode-postscript t])
1707              ("Cache"
1708               ["Enter article" gnus-cache-enter-article t]
1709               ["Remove article" gnus-cache-remove-article t])
1710              ["Translate" gnus-article-babel t]
1711              ["Select article buffer" gnus-summary-select-article-buffer t]
1712              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1713              ["Isearch article..." gnus-summary-isearch-article t]
1714              ["Beginning of the article" gnus-summary-beginning-of-article t]
1715              ["End of the article" gnus-summary-end-of-article t]
1716              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1717              ["Fetch referenced articles" gnus-summary-refer-references t]
1718              ["Fetch current thread" gnus-summary-refer-thread t]
1719              ["Fetch article with id..." gnus-summary-refer-article t]
1720              ["Redisplay" gnus-summary-show-article t])))
1721       (easy-menu-define
1722        gnus-summary-article-menu gnus-summary-mode-map ""
1723        (cons "Article" innards))
1724
1725       (easy-menu-define
1726        gnus-article-commands-menu gnus-article-mode-map ""
1727        (cons "Commands" innards)))
1728
1729     (easy-menu-define
1730      gnus-summary-thread-menu gnus-summary-mode-map ""
1731      '("Threads"
1732        ["Toggle threading" gnus-summary-toggle-threads t]
1733        ["Hide threads" gnus-summary-hide-all-threads t]
1734        ["Show threads" gnus-summary-show-all-threads t]
1735        ["Hide thread" gnus-summary-hide-thread t]
1736        ["Show thread" gnus-summary-show-thread t]
1737        ["Go to next thread" gnus-summary-next-thread t]
1738        ["Go to previous thread" gnus-summary-prev-thread t]
1739        ["Go down thread" gnus-summary-down-thread t]
1740        ["Go up thread" gnus-summary-up-thread t]
1741        ["Top of thread" gnus-summary-top-thread t]
1742        ["Mark thread as read" gnus-summary-kill-thread t]
1743        ["Lower thread score" gnus-summary-lower-thread t]
1744        ["Raise thread score" gnus-summary-raise-thread t]
1745        ["Rethread current" gnus-summary-rethread-current t]
1746        ))
1747
1748     (easy-menu-define
1749      gnus-summary-post-menu gnus-summary-mode-map ""
1750      '("Post"
1751        ["Post an article" gnus-summary-post-news t]
1752        ["Followup" gnus-summary-followup t]
1753        ["Followup and yank" gnus-summary-followup-with-original t]
1754        ["Supersede article" gnus-summary-supersede-article t]
1755        ["Cancel article" gnus-summary-cancel-article t]
1756        ["Reply" gnus-summary-reply t]
1757        ["Reply and yank" gnus-summary-reply-with-original t]
1758        ["Wide reply" gnus-summary-wide-reply t]
1759        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1760        ["Mail forward" gnus-summary-mail-forward t]
1761        ["Post forward" gnus-summary-post-forward t]
1762        ["Digest and mail" gnus-summary-mail-digest t]
1763        ["Digest and post" gnus-summary-post-digest t]
1764        ["Resend message" gnus-summary-resend-message t]
1765        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1766        ["Send a mail" gnus-summary-mail-other-window t]
1767        ["Uuencode and post" gnus-uu-post-news t]
1768        ["Followup via news" gnus-summary-followup-to-mail t]
1769        ["Followup via news and yank"
1770         gnus-summary-followup-to-mail-with-original t]
1771        ;;("Draft"
1772        ;;["Send" gnus-summary-send-draft t]
1773        ;;["Send bounced" gnus-resend-bounced-mail t])
1774        ))
1775
1776     (easy-menu-define
1777      gnus-summary-misc-menu gnus-summary-mode-map ""
1778      '("Misc"
1779        ("Mark Read"
1780         ["Mark as read" gnus-summary-mark-as-read-forward t]
1781         ["Mark same subject and select"
1782          gnus-summary-kill-same-subject-and-select t]
1783         ["Mark same subject" gnus-summary-kill-same-subject t]
1784         ["Catchup" gnus-summary-catchup t]
1785         ["Catchup all" gnus-summary-catchup-all t]
1786         ["Catchup to here" gnus-summary-catchup-to-here t]
1787         ["Catchup region" gnus-summary-mark-region-as-read t]
1788         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1789        ("Mark Various"
1790         ["Tick" gnus-summary-tick-article-forward t]
1791         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1792         ["Remove marks" gnus-summary-clear-mark-forward t]
1793         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1794         ["Set bookmark" gnus-summary-set-bookmark t]
1795         ["Remove bookmark" gnus-summary-remove-bookmark t])
1796        ("Mark Limit"
1797         ["Marks..." gnus-summary-limit-to-marks t]
1798         ["Subject..." gnus-summary-limit-to-subject t]
1799         ["Author..." gnus-summary-limit-to-author t]
1800         ["Age..." gnus-summary-limit-to-age t]
1801         ["Extra..." gnus-summary-limit-to-extra t]
1802         ["Score" gnus-summary-limit-to-score t]
1803         ["Unread" gnus-summary-limit-to-unread t]
1804         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1805         ["Articles" gnus-summary-limit-to-articles t]
1806         ["Pop limit" gnus-summary-pop-limit t]
1807         ["Show dormant" gnus-summary-limit-include-dormant t]
1808         ["Hide childless dormant"
1809          gnus-summary-limit-exclude-childless-dormant t]
1810         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1811         ["Hide marked" gnus-summary-limit-exclude-marks t]
1812         ["Show expunged" gnus-summary-show-all-expunged t])
1813        ("Process Mark"
1814         ["Set mark" gnus-summary-mark-as-processable t]
1815         ["Remove mark" gnus-summary-unmark-as-processable t]
1816         ["Remove all marks" gnus-summary-unmark-all-processable t]
1817         ["Mark above" gnus-uu-mark-over t]
1818         ["Mark series" gnus-uu-mark-series t]
1819         ["Mark region" gnus-uu-mark-region t]
1820         ["Unmark region" gnus-uu-unmark-region t]
1821         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1822         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1823         ["Mark all" gnus-uu-mark-all t]
1824         ["Mark buffer" gnus-uu-mark-buffer t]
1825         ["Mark sparse" gnus-uu-mark-sparse t]
1826         ["Mark thread" gnus-uu-mark-thread t]
1827         ["Unmark thread" gnus-uu-unmark-thread t]
1828         ("Process Mark Sets"
1829          ["Kill" gnus-summary-kill-process-mark t]
1830          ["Yank" gnus-summary-yank-process-mark
1831           gnus-newsgroup-process-stack]
1832          ["Save" gnus-summary-save-process-mark t]))
1833        ("Scroll article"
1834         ["Page forward" gnus-summary-next-page t]
1835         ["Page backward" gnus-summary-prev-page t]
1836         ["Line forward" gnus-summary-scroll-up t])
1837        ("Move"
1838         ["Next unread article" gnus-summary-next-unread-article t]
1839         ["Previous unread article" gnus-summary-prev-unread-article t]
1840         ["Next article" gnus-summary-next-article t]
1841         ["Previous article" gnus-summary-prev-article t]
1842         ["Next unread subject" gnus-summary-next-unread-subject t]
1843         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1844         ["Next article same subject" gnus-summary-next-same-subject t]
1845         ["Previous article same subject" gnus-summary-prev-same-subject t]
1846         ["First unread article" gnus-summary-first-unread-article t]
1847         ["Best unread article" gnus-summary-best-unread-article t]
1848         ["Go to subject number..." gnus-summary-goto-subject t]
1849         ["Go to article number..." gnus-summary-goto-article t]
1850         ["Go to the last article" gnus-summary-goto-last-article t]
1851         ["Pop article off history" gnus-summary-pop-article t])
1852        ("Sort"
1853         ["Sort by number" gnus-summary-sort-by-number t]
1854         ["Sort by author" gnus-summary-sort-by-author t]
1855         ["Sort by subject" gnus-summary-sort-by-subject t]
1856         ["Sort by date" gnus-summary-sort-by-date t]
1857         ["Sort by score" gnus-summary-sort-by-score t]
1858         ["Sort by lines" gnus-summary-sort-by-lines t]
1859         ["Sort by characters" gnus-summary-sort-by-chars t])
1860        ("Help"
1861         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1862         ["Describe group" gnus-summary-describe-group t]
1863         ["Read manual" gnus-info-find-node t])
1864        ("Modes"
1865         ["Pick and read" gnus-pick-mode t]
1866         ["Binary" gnus-binary-mode t])
1867        ("Regeneration"
1868         ["Regenerate" gnus-summary-prepare t]
1869         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1870         ["Toggle threading" gnus-summary-toggle-threads t])
1871        ["Filter articles..." gnus-summary-execute-command t]
1872        ["Run command on subjects..." gnus-summary-universal-argument t]
1873        ["Search articles forward..." gnus-summary-search-article-forward t]
1874        ["Search articles backward..." gnus-summary-search-article-backward t]
1875        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1876        ["Expand window" gnus-summary-expand-window t]
1877        ["Expire expirable articles" gnus-summary-expire-articles
1878         (gnus-check-backend-function
1879          'request-expire-articles gnus-newsgroup-name)]
1880        ["Edit local kill file" gnus-summary-edit-local-kill t]
1881        ["Edit main kill file" gnus-summary-edit-global-kill t]
1882        ["Edit group parameters" gnus-summary-edit-parameters t]
1883        ["Customize group parameters" gnus-summary-customize-parameters t]
1884        ["Send a bug report" gnus-bug t]
1885        ("Exit"
1886         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1887         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1888         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1889         ["Exit group" gnus-summary-exit t]
1890         ["Exit group without updating" gnus-summary-exit-no-update t]
1891         ["Exit and goto next group" gnus-summary-next-group t]
1892         ["Exit and goto prev group" gnus-summary-prev-group t]
1893         ["Reselect group" gnus-summary-reselect-current-group t]
1894         ["Rescan group" gnus-summary-rescan-group t]
1895         ["Update dribble" gnus-summary-save-newsrc t])))
1896
1897     (gnus-run-hooks 'gnus-summary-menu-hook)))
1898
1899 (defun gnus-score-set-default (var value)
1900   "A version of set that updates the GNU Emacs menu-bar."
1901   (set var value)
1902   ;; It is the message that forces the active status to be updated.
1903   (message ""))
1904
1905 (defun gnus-make-score-map (type)
1906   "Make a summary score map of type TYPE."
1907   (if t
1908       nil
1909     (let ((headers '(("author" "from" string)
1910                      ("subject" "subject" string)
1911                      ("article body" "body" string)
1912                      ("article head" "head" string)
1913                      ("xref" "xref" string)
1914                      ("extra header" "extra" string)
1915                      ("lines" "lines" number)
1916                      ("followups to author" "followup" string)))
1917           (types '((number ("less than" <)
1918                            ("greater than" >)
1919                            ("equal" =))
1920                    (string ("substring" s)
1921                            ("exact string" e)
1922                            ("fuzzy string" f)
1923                            ("regexp" r))))
1924           (perms '(("temporary" (current-time-string))
1925                    ("permanent" nil)
1926                    ("immediate" now)))
1927           header)
1928       (list
1929        (apply
1930         'nconc
1931         (list
1932          (if (eq type 'lower)
1933              "Lower score"
1934            "Increase score"))
1935         (let (outh)
1936           (while headers
1937             (setq header (car headers))
1938             (setq outh
1939                   (cons
1940                    (apply
1941                     'nconc
1942                     (list (car header))
1943                     (let ((ts (cdr (assoc (nth 2 header) types)))
1944                           outt)
1945                       (while ts
1946                         (setq outt
1947                               (cons
1948                                (apply
1949                                 'nconc
1950                                 (list (caar ts))
1951                                 (let ((ps perms)
1952                                       outp)
1953                                   (while ps
1954                                     (setq outp
1955                                           (cons
1956                                            (vector
1957                                             (caar ps)
1958                                             (list
1959                                              'gnus-summary-score-entry
1960                                              (nth 1 header)
1961                                              (if (or (string= (nth 1 header)
1962                                                               "head")
1963                                                      (string= (nth 1 header)
1964                                                               "body"))
1965                                                  ""
1966                                                (list 'gnus-summary-header
1967                                                      (nth 1 header)))
1968                                              (list 'quote (nth 1 (car ts)))
1969                                              (list 'gnus-score-default nil)
1970                                              (nth 1 (car ps))
1971                                              t)
1972                                             t)
1973                                            outp))
1974                                     (setq ps (cdr ps)))
1975                                   (list (nreverse outp))))
1976                                outt))
1977                         (setq ts (cdr ts)))
1978                       (list (nreverse outt))))
1979                    outh))
1980             (setq headers (cdr headers)))
1981           (list (nreverse outh))))))))
1982
1983 \f
1984
1985 (defun gnus-summary-mode (&optional group)
1986   "Major mode for reading articles.
1987
1988 All normal editing commands are switched off.
1989 \\<gnus-summary-mode-map>
1990 Each line in this buffer represents one article.  To read an
1991 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1992 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1993 respectively.
1994
1995 You can also post articles and send mail from this buffer.  To
1996 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1997 of an article, type `\\[gnus-summary-reply]'.
1998
1999 There are approx. one gazillion commands you can execute in this
2000 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2001
2002 The following commands are available:
2003
2004 \\{gnus-summary-mode-map}"
2005   (interactive)
2006   (when (gnus-visual-p 'summary-menu 'menu)
2007     (gnus-summary-make-menu-bar))
2008   (kill-all-local-variables)
2009   (gnus-summary-make-local-variables)
2010   (gnus-make-thread-indent-array)
2011   (gnus-simplify-mode-line)
2012   (setq major-mode 'gnus-summary-mode)
2013   (setq mode-name "Summary")
2014   (make-local-variable 'minor-mode-alist)
2015   (use-local-map gnus-summary-mode-map)
2016   (buffer-disable-undo)
2017   (setq buffer-read-only t)             ;Disable modification
2018   (setq truncate-lines t)
2019   (setq selective-display t)
2020   (setq selective-display-ellipses t)   ;Display `...'
2021   (gnus-summary-set-display-table)
2022   (gnus-set-default-directory)
2023   (setq gnus-newsgroup-name group)
2024   (make-local-variable 'gnus-summary-line-format)
2025   (make-local-variable 'gnus-summary-line-format-spec)
2026   (make-local-variable 'gnus-summary-dummy-line-format)
2027   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2028   (make-local-variable 'gnus-summary-mark-positions)
2029   (make-local-hook 'pre-command-hook)
2030   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2031   (gnus-run-hooks 'gnus-summary-mode-hook)
2032   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2033   (gnus-update-summary-mark-positions))
2034
2035 (defun gnus-summary-make-local-variables ()
2036   "Make all the local summary buffer variables."
2037   (let (global)
2038     (dolist (local gnus-summary-local-variables)
2039       (if (consp local)
2040           (progn
2041             (if (eq (cdr local) 'global)
2042                 ;; Copy the global value of the variable.
2043                 (setq global (symbol-value (car local)))
2044               ;; Use the value from the list.
2045               (setq global (eval (cdr local))))
2046             (set (make-local-variable (car local)) global))
2047         ;; Simple nil-valued local variable.
2048         (set (make-local-variable local) nil)))))
2049
2050 (defun gnus-summary-clear-local-variables ()
2051   (let ((locals gnus-summary-local-variables))
2052     (while locals
2053       (if (consp (car locals))
2054           (and (vectorp (caar locals))
2055                (set (caar locals) nil))
2056         (and (vectorp (car locals))
2057              (set (car locals) nil)))
2058       (setq locals (cdr locals)))))
2059
2060 ;; Summary data functions.
2061
2062 (defmacro gnus-data-number (data)
2063   `(car ,data))
2064
2065 (defmacro gnus-data-set-number (data number)
2066   `(setcar ,data ,number))
2067
2068 (defmacro gnus-data-mark (data)
2069   `(nth 1 ,data))
2070
2071 (defmacro gnus-data-set-mark (data mark)
2072   `(setcar (nthcdr 1 ,data) ,mark))
2073
2074 (defmacro gnus-data-pos (data)
2075   `(nth 2 ,data))
2076
2077 (defmacro gnus-data-set-pos (data pos)
2078   `(setcar (nthcdr 2 ,data) ,pos))
2079
2080 (defmacro gnus-data-header (data)
2081   `(nth 3 ,data))
2082
2083 (defmacro gnus-data-set-header (data header)
2084   `(setf (nth 3 ,data) ,header))
2085
2086 (defmacro gnus-data-level (data)
2087   `(nth 4 ,data))
2088
2089 (defmacro gnus-data-unread-p (data)
2090   `(= (nth 1 ,data) gnus-unread-mark))
2091
2092 (defmacro gnus-data-read-p (data)
2093   `(/= (nth 1 ,data) gnus-unread-mark))
2094
2095 (defmacro gnus-data-pseudo-p (data)
2096   `(consp (nth 3 ,data)))
2097
2098 (defmacro gnus-data-find (number)
2099   `(assq ,number gnus-newsgroup-data))
2100
2101 (defmacro gnus-data-find-list (number &optional data)
2102   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2103      (memq (assq ,number bdata)
2104            bdata)))
2105
2106 (defmacro gnus-data-make (number mark pos header level)
2107   `(list ,number ,mark ,pos ,header ,level))
2108
2109 (defun gnus-data-enter (after-article number mark pos header level offset)
2110   (let ((data (gnus-data-find-list after-article)))
2111     (unless data
2112       (error "No such article: %d" after-article))
2113     (setcdr data (cons (gnus-data-make number mark pos header level)
2114                        (cdr data)))
2115     (setq gnus-newsgroup-data-reverse nil)
2116     (gnus-data-update-list (cddr data) offset)))
2117
2118 (defun gnus-data-enter-list (after-article list &optional offset)
2119   (when list
2120     (let ((data (and after-article (gnus-data-find-list after-article)))
2121           (ilist list))
2122       (if (not (or data
2123                    after-article))
2124           (let ((odata gnus-newsgroup-data))
2125             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2126             (when offset
2127               (gnus-data-update-list odata offset)))
2128         ;; Find the last element in the list to be spliced into the main
2129         ;; list.
2130         (while (cdr list)
2131           (setq list (cdr list)))
2132         (if (not data)
2133             (progn
2134               (setcdr list gnus-newsgroup-data)
2135               (setq gnus-newsgroup-data ilist)
2136               (when offset
2137                 (gnus-data-update-list (cdr list) offset)))
2138           (setcdr list (cdr data))
2139           (setcdr data ilist)
2140           (when offset
2141             (gnus-data-update-list (cdr list) offset))))
2142       (setq gnus-newsgroup-data-reverse nil))))
2143
2144 (defun gnus-data-remove (article &optional offset)
2145   (let ((data gnus-newsgroup-data))
2146     (if (= (gnus-data-number (car data)) article)
2147         (progn
2148           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2149                 gnus-newsgroup-data-reverse nil)
2150           (when offset
2151             (gnus-data-update-list gnus-newsgroup-data offset)))
2152       (while (cdr data)
2153         (when (= (gnus-data-number (cadr data)) article)
2154           (setcdr data (cddr data))
2155           (when offset
2156             (gnus-data-update-list (cdr data) offset))
2157           (setq data nil
2158                 gnus-newsgroup-data-reverse nil))
2159         (setq data (cdr data))))))
2160
2161 (defmacro gnus-data-list (backward)
2162   `(if ,backward
2163        (or gnus-newsgroup-data-reverse
2164            (setq gnus-newsgroup-data-reverse
2165                  (reverse gnus-newsgroup-data)))
2166      gnus-newsgroup-data))
2167
2168 (defun gnus-data-update-list (data offset)
2169   "Add OFFSET to the POS of all data entries in DATA."
2170   (setq gnus-newsgroup-data-reverse nil)
2171   (while data
2172     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2173     (setq data (cdr data))))
2174
2175 (defun gnus-summary-article-pseudo-p (article)
2176   "Say whether this article is a pseudo article or not."
2177   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2178
2179 (defmacro gnus-summary-article-sparse-p (article)
2180   "Say whether this article is a sparse article or not."
2181   `(memq ,article gnus-newsgroup-sparse))
2182
2183 (defmacro gnus-summary-article-ancient-p (article)
2184   "Say whether this article is a sparse article or not."
2185   `(memq ,article gnus-newsgroup-ancient))
2186
2187 (defun gnus-article-parent-p (number)
2188   "Say whether this article is a parent or not."
2189   (let ((data (gnus-data-find-list number)))
2190     (and (cdr data)                     ; There has to be an article after...
2191          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2192             (gnus-data-level (nth 1 data))))))
2193
2194 (defun gnus-article-children (number)
2195   "Return a list of all children to NUMBER."
2196   (let* ((data (gnus-data-find-list number))
2197          (level (gnus-data-level (car data)))
2198          children)
2199     (setq data (cdr data))
2200     (while (and data
2201                 (= (gnus-data-level (car data)) (1+ level)))
2202       (push (gnus-data-number (car data)) children)
2203       (setq data (cdr data)))
2204     children))
2205
2206 (defmacro gnus-summary-skip-intangible ()
2207   "If the current article is intangible, then jump to a different article."
2208   '(let ((to (get-text-property (point) 'gnus-intangible)))
2209      (and to (gnus-summary-goto-subject to))))
2210
2211 (defmacro gnus-summary-article-intangible-p ()
2212   "Say whether this article is intangible or not."
2213   '(get-text-property (point) 'gnus-intangible))
2214
2215 (defun gnus-article-read-p (article)
2216   "Say whether ARTICLE is read or not."
2217   (not (or (memq article gnus-newsgroup-marked)
2218            (memq article gnus-newsgroup-unreads)
2219            (memq article gnus-newsgroup-unselected)
2220            (memq article gnus-newsgroup-dormant))))
2221
2222 ;; Some summary mode macros.
2223
2224 (defmacro gnus-summary-article-number ()
2225   "The article number of the article on the current line.
2226 If there isn's an article number here, then we return the current
2227 article number."
2228   '(progn
2229      (gnus-summary-skip-intangible)
2230      (or (get-text-property (point) 'gnus-number)
2231          (gnus-summary-last-subject))))
2232
2233 (defmacro gnus-summary-article-header (&optional number)
2234   "Return the header of article NUMBER."
2235   `(gnus-data-header (gnus-data-find
2236                       ,(or number '(gnus-summary-article-number)))))
2237
2238 (defmacro gnus-summary-thread-level (&optional number)
2239   "Return the level of thread that starts with article NUMBER."
2240   `(if (and (eq gnus-summary-make-false-root 'dummy)
2241             (get-text-property (point) 'gnus-intangible))
2242        0
2243      (gnus-data-level (gnus-data-find
2244                        ,(or number '(gnus-summary-article-number))))))
2245
2246 (defmacro gnus-summary-article-mark (&optional number)
2247   "Return the mark of article NUMBER."
2248   `(gnus-data-mark (gnus-data-find
2249                     ,(or number '(gnus-summary-article-number)))))
2250
2251 (defmacro gnus-summary-article-pos (&optional number)
2252   "Return the position of the line of article NUMBER."
2253   `(gnus-data-pos (gnus-data-find
2254                    ,(or number '(gnus-summary-article-number)))))
2255
2256 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2257 (defmacro gnus-summary-article-subject (&optional number)
2258   "Return current subject string or nil if nothing."
2259   `(let ((headers
2260           ,(if number
2261                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2262              '(gnus-data-header (assq (gnus-summary-article-number)
2263                                       gnus-newsgroup-data)))))
2264      (and headers
2265           (vectorp headers)
2266           (mail-header-subject headers))))
2267
2268 (defmacro gnus-summary-article-score (&optional number)
2269   "Return current article score."
2270   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2271                   gnus-newsgroup-scored))
2272        gnus-summary-default-score 0))
2273
2274 (defun gnus-summary-article-children (&optional number)
2275   "Return a list of article numbers that are children of article NUMBER."
2276   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2277          (level (gnus-data-level (car data)))
2278          l children)
2279     (while (and (setq data (cdr data))
2280                 (> (setq l (gnus-data-level (car data))) level))
2281       (and (= (1+ level) l)
2282            (push (gnus-data-number (car data))
2283                  children)))
2284     (nreverse children)))
2285
2286 (defun gnus-summary-article-parent (&optional number)
2287   "Return the article number of the parent of article NUMBER."
2288   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2289                                     (gnus-data-list t)))
2290          (level (gnus-data-level (car data))))
2291     (if (zerop level)
2292         ()                              ; This is a root.
2293       ;; We search until we find an article with a level less than
2294       ;; this one.  That function has to be the parent.
2295       (while (and (setq data (cdr data))
2296                   (not (< (gnus-data-level (car data)) level))))
2297       (and data (gnus-data-number (car data))))))
2298
2299 (defun gnus-unread-mark-p (mark)
2300   "Say whether MARK is the unread mark."
2301   (= mark gnus-unread-mark))
2302
2303 (defun gnus-read-mark-p (mark)
2304   "Say whether MARK is one of the marks that mark as read.
2305 This is all marks except unread, ticked, dormant, and expirable."
2306   (not (or (= mark gnus-unread-mark)
2307            (= mark gnus-ticked-mark)
2308            (= mark gnus-dormant-mark)
2309            (= mark gnus-expirable-mark))))
2310
2311 (defmacro gnus-article-mark (number)
2312   "Return the MARK of article NUMBER.
2313 This macro should only be used when computing the mark the \"first\"
2314 time; i.e., when generating the summary lines.  After that,
2315 `gnus-summary-article-mark' should be used to examine the
2316 marks of articles."
2317   `(cond
2318     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2319     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2320     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2321     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2322     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2323     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2324     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2325     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2326            gnus-ancient-mark))))
2327
2328 ;; Saving hidden threads.
2329
2330 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2331 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2332
2333 (defmacro gnus-save-hidden-threads (&rest forms)
2334   "Save hidden threads, eval FORMS, and restore the hidden threads."
2335   (let ((config (make-symbol "config")))
2336     `(let ((,config (gnus-hidden-threads-configuration)))
2337        (unwind-protect
2338            (save-excursion
2339              ,@forms)
2340          (gnus-restore-hidden-threads-configuration ,config)))))
2341
2342 (defun gnus-data-compute-positions ()
2343   "Compute the positions of all articles."
2344   (setq gnus-newsgroup-data-reverse nil)
2345   (let ((data gnus-newsgroup-data))
2346     (save-excursion
2347       (gnus-save-hidden-threads
2348         (gnus-summary-show-all-threads)
2349         (goto-char (point-min))
2350         (while data
2351           (while (get-text-property (point) 'gnus-intangible)
2352             (forward-line 1))
2353           (gnus-data-set-pos (car data) (+ (point) 3))
2354           (setq data (cdr data))
2355           (forward-line 1))))))
2356
2357 (defun gnus-hidden-threads-configuration ()
2358   "Return the current hidden threads configuration."
2359   (save-excursion
2360     (let (config)
2361       (goto-char (point-min))
2362       (while (search-forward "\r" nil t)
2363         (push (1- (point)) config))
2364       config)))
2365
2366 (defun gnus-restore-hidden-threads-configuration (config)
2367   "Restore hidden threads configuration from CONFIG."
2368   (let (point buffer-read-only)
2369     (while (setq point (pop config))
2370       (when (and (< point (point-max))
2371                  (goto-char point)
2372                  (eq (char-after) ?\n))
2373         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2374
2375 ;; Various summary mode internalish functions.
2376
2377 (defun gnus-mouse-pick-article (e)
2378   (interactive "e")
2379   (mouse-set-point e)
2380   (gnus-summary-next-page nil t))
2381
2382 (defun gnus-summary-set-display-table ()
2383   ;; Change the display table.  Odd characters have a tendency to mess
2384   ;; up nicely formatted displays - we make all possible glyphs
2385   ;; display only a single character.
2386
2387   ;; We start from the standard display table, if any.
2388   (let ((table (or (copy-sequence standard-display-table)
2389                    (make-display-table)))
2390         (i 32))
2391     ;; Nix out all the control chars...
2392     (while (>= (setq i (1- i)) 0)
2393       (aset table i [??]))
2394     ;; ... but not newline and cr, of course.  (cr is necessary for the
2395     ;; selective display).
2396     (aset table ?\n nil)
2397     (aset table ?\r nil)
2398     ;; We keep TAB as well.
2399     (aset table ?\t nil)
2400     ;; We nix out any glyphs over 126 that are not set already.
2401     (let ((i 256))
2402       (while (>= (setq i (1- i)) 127)
2403         ;; Only modify if the entry is nil.
2404         (unless (aref table i)
2405           (aset table i [??]))))
2406     (setq buffer-display-table table)))
2407
2408 (defun gnus-summary-setup-buffer (group)
2409   "Initialize summary buffer."
2410   (let ((buffer (concat "*Summary " group "*")))
2411     (if (get-buffer buffer)
2412         (progn
2413           (set-buffer buffer)
2414           (setq gnus-summary-buffer (current-buffer))
2415           (not gnus-newsgroup-prepared))
2416       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2417       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2418       (gnus-summary-mode group)
2419       (when gnus-carpal
2420         (gnus-carpal-setup-buffer 'summary))
2421       (unless gnus-single-article-buffer
2422         (make-local-variable 'gnus-article-buffer)
2423         (make-local-variable 'gnus-article-current)
2424         (make-local-variable 'gnus-original-article-buffer))
2425       (setq gnus-newsgroup-name group)
2426       t)))
2427
2428 (defun gnus-set-global-variables ()
2429   ;; Set the global equivalents of the summary buffer-local variables
2430   ;; to the latest values they had.  These reflect the summary buffer
2431   ;; that was in action when the last article was fetched.
2432   (when (eq major-mode 'gnus-summary-mode)
2433     (setq gnus-summary-buffer (current-buffer))
2434     (let ((name gnus-newsgroup-name)
2435           (marked gnus-newsgroup-marked)
2436           (unread gnus-newsgroup-unreads)
2437           (headers gnus-current-headers)
2438           (data gnus-newsgroup-data)
2439           (summary gnus-summary-buffer)
2440           (article-buffer gnus-article-buffer)
2441           (original gnus-original-article-buffer)
2442           (gac gnus-article-current)
2443           (reffed gnus-reffed-article-number)
2444           (score-file gnus-current-score-file)
2445           (default-charset gnus-newsgroup-charset))
2446       (save-excursion
2447         (set-buffer gnus-group-buffer)
2448         (setq gnus-newsgroup-name name
2449               gnus-newsgroup-marked marked
2450               gnus-newsgroup-unreads unread
2451               gnus-current-headers headers
2452               gnus-newsgroup-data data
2453               gnus-article-current gac
2454               gnus-summary-buffer summary
2455               gnus-article-buffer article-buffer
2456               gnus-original-article-buffer original
2457               gnus-reffed-article-number reffed
2458               gnus-current-score-file score-file
2459               gnus-newsgroup-charset default-charset)
2460         ;; The article buffer also has local variables.
2461         (when (gnus-buffer-live-p gnus-article-buffer)
2462           (set-buffer gnus-article-buffer)
2463           (setq gnus-summary-buffer summary))))))
2464
2465 (defun gnus-summary-article-unread-p (article)
2466   "Say whether ARTICLE is unread or not."
2467   (memq article gnus-newsgroup-unreads))
2468
2469 (defun gnus-summary-first-article-p (&optional article)
2470   "Return whether ARTICLE is the first article in the buffer."
2471   (if (not (setq article (or article (gnus-summary-article-number))))
2472       nil
2473     (eq article (caar gnus-newsgroup-data))))
2474
2475 (defun gnus-summary-last-article-p (&optional article)
2476   "Return whether ARTICLE is the last article in the buffer."
2477   (if (not (setq article (or article (gnus-summary-article-number))))
2478       ;; All non-existent numbers are the last article.  :-)
2479       t
2480     (not (cdr (gnus-data-find-list article)))))
2481
2482 (defun gnus-make-thread-indent-array ()
2483   (let ((n 200))
2484     (unless (and gnus-thread-indent-array
2485                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2486       (setq gnus-thread-indent-array (make-vector 201 "")
2487             gnus-thread-indent-array-level gnus-thread-indent-level)
2488       (while (>= n 0)
2489         (aset gnus-thread-indent-array n
2490               (make-string (* n gnus-thread-indent-level) ? ))
2491         (setq n (1- n))))))
2492
2493 (defun gnus-update-summary-mark-positions ()
2494   "Compute where the summary marks are to go."
2495   (save-excursion
2496     (when (gnus-buffer-exists-p gnus-summary-buffer)
2497       (set-buffer gnus-summary-buffer))
2498     (let ((gnus-replied-mark 129)
2499           (gnus-score-below-mark 130)
2500           (gnus-score-over-mark 130)
2501           (gnus-download-mark 131)
2502           (spec gnus-summary-line-format-spec)
2503           gnus-visual pos)
2504       (save-excursion
2505         (gnus-set-work-buffer)
2506         (let ((gnus-summary-line-format-spec spec)
2507               (gnus-newsgroup-downloadable '((0 . t))))
2508           (gnus-summary-insert-line
2509            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2510            0 nil 128 t nil "" nil 1)
2511           (goto-char (point-min))
2512           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2513                                              (- (point) 2)))))
2514           (goto-char (point-min))
2515           (push (cons 'replied (and (search-forward "\201" nil t)
2516                                     (- (point) 2)))
2517                 pos)
2518           (goto-char (point-min))
2519           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2520                 pos)
2521           (goto-char (point-min))
2522           (push (cons 'download
2523                       (and (search-forward "\203" nil t) (- (point) 2)))
2524                 pos)))
2525       (setq gnus-summary-mark-positions pos))))
2526
2527 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2528   "Insert a dummy root in the summary buffer."
2529   (beginning-of-line)
2530   (gnus-add-text-properties
2531    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2532    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2533
2534 (defun gnus-summary-from-or-to-or-newsgroups (header)
2535   (let ((to (cdr (assq 'To (mail-header-extra header))))
2536         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2537         (default-mime-charset (with-current-buffer gnus-summary-buffer
2538                                 default-mime-charset)))
2539     (cond
2540      ((and to
2541            gnus-ignored-from-addresses
2542            (string-match gnus-ignored-from-addresses
2543                          (mail-header-from header)))
2544       (concat "-> "
2545               (or (car (funcall gnus-extract-address-components
2546                                 (funcall
2547                                  gnus-decode-encoded-word-function to)))
2548                   (funcall gnus-decode-encoded-word-function to))))
2549      ((and newsgroups
2550            gnus-ignored-from-addresses
2551            (string-match gnus-ignored-from-addresses
2552                          (mail-header-from header)))
2553       (concat "=> " newsgroups))
2554      (t
2555       (or (car (funcall gnus-extract-address-components
2556                         (mail-header-from header)))
2557           (mail-header-from header))))))
2558
2559 (defun gnus-summary-insert-line (gnus-tmp-header
2560                                  gnus-tmp-level gnus-tmp-current
2561                                  gnus-tmp-unread gnus-tmp-replied
2562                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2563                                  &optional gnus-tmp-dummy gnus-tmp-score
2564                                  gnus-tmp-process)
2565   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2566          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2567          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2568          (gnus-tmp-score-char
2569           (if (or (null gnus-summary-default-score)
2570                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2571                       gnus-summary-zcore-fuzz))
2572               ? ;Whitespace
2573             (if (< gnus-tmp-score gnus-summary-default-score)
2574                 gnus-score-below-mark gnus-score-over-mark)))
2575          (gnus-tmp-replied
2576           (cond (gnus-tmp-process gnus-process-mark)
2577                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2578                  gnus-cached-mark)
2579                 (gnus-tmp-replied gnus-replied-mark)
2580                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2581                  gnus-saved-mark)
2582                 (t gnus-unread-mark)))
2583          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2584          (gnus-tmp-name
2585           (cond
2586            ((string-match "<[^>]+> *$" gnus-tmp-from)
2587             (let ((beg (match-beginning 0)))
2588               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2589                        (substring gnus-tmp-from (1+ (match-beginning 0))
2590                                   (1- (match-end 0))))
2591                   (substring gnus-tmp-from 0 beg))))
2592            ((string-match "(.+)" gnus-tmp-from)
2593             (substring gnus-tmp-from
2594                        (1+ (match-beginning 0)) (1- (match-end 0))))
2595            (t gnus-tmp-from)))
2596          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2597          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2598          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2599          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2600          (buffer-read-only nil))
2601     (when (string= gnus-tmp-name "")
2602       (setq gnus-tmp-name gnus-tmp-from))
2603     (unless (numberp gnus-tmp-lines)
2604       (setq gnus-tmp-lines 0))
2605     (gnus-put-text-property-excluding-characters-with-faces
2606      (point)
2607      (progn (eval gnus-summary-line-format-spec) (point))
2608      'gnus-number gnus-tmp-number)
2609     (when (gnus-visual-p 'summary-highlight 'highlight)
2610       (forward-line -1)
2611       (gnus-run-hooks 'gnus-summary-update-hook)
2612       (forward-line 1))))
2613
2614 (defun gnus-summary-update-line (&optional dont-update)
2615   ;; Update summary line after change.
2616   (when (and gnus-summary-default-score
2617              (not gnus-summary-inhibit-highlight))
2618     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2619            (article (gnus-summary-article-number))
2620            (score (gnus-summary-article-score article)))
2621       (unless dont-update
2622         (if (and gnus-summary-mark-below
2623                  (< (gnus-summary-article-score)
2624                     gnus-summary-mark-below))
2625             ;; This article has a low score, so we mark it as read.
2626             (when (memq article gnus-newsgroup-unreads)
2627               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2628           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2629             ;; This article was previously marked as read on account
2630             ;; of a low score, but now it has risen, so we mark it as
2631             ;; unread.
2632             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2633         (gnus-summary-update-mark
2634          (if (or (null gnus-summary-default-score)
2635                  (<= (abs (- score gnus-summary-default-score))
2636                      gnus-summary-zcore-fuzz))
2637              ? ;Whitespace
2638            (if (< score gnus-summary-default-score)
2639                gnus-score-below-mark gnus-score-over-mark))
2640          'score))
2641       ;; Do visual highlighting.
2642       (when (gnus-visual-p 'summary-highlight 'highlight)
2643         (gnus-run-hooks 'gnus-summary-update-hook)))))
2644
2645 (defvar gnus-tmp-new-adopts nil)
2646
2647 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2648   "Return the number of articles in THREAD.
2649 This may be 0 in some cases -- if none of the articles in
2650 the thread are to be displayed."
2651   (let* ((number
2652           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2653           (cond
2654            ((not (listp thread))
2655             1)
2656            ((and (consp thread) (cdr thread))
2657             (apply
2658              '+ 1 (mapcar
2659                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2660            ((null thread)
2661             1)
2662            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2663             1)
2664            (t 0))))
2665     (when (and level (zerop level) gnus-tmp-new-adopts)
2666       (incf number
2667             (apply '+ (mapcar
2668                        'gnus-summary-number-of-articles-in-thread
2669                        gnus-tmp-new-adopts))))
2670     (if char
2671         (if (> number 1) gnus-not-empty-thread-mark
2672           gnus-empty-thread-mark)
2673       number)))
2674
2675 (defun gnus-summary-set-local-parameters (group)
2676   "Go through the local params of GROUP and set all variable specs in that list."
2677   (let ((params (gnus-group-find-parameter group))
2678         elem)
2679     (while params
2680       (setq elem (car params)
2681             params (cdr params))
2682       (and (consp elem)                 ; Has to be a cons.
2683            (consp (cdr elem))           ; The cdr has to be a list.
2684            (symbolp (car elem))         ; Has to be a symbol in there.
2685            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2686            (ignore-errors               ; So we set it.
2687              (make-local-variable (car elem))
2688              (set (car elem) (eval (nth 1 elem))))))))
2689
2690 (defun gnus-summary-read-group (group &optional show-all no-article
2691                                       kill-buffer no-display backward
2692                                       select-articles)
2693   "Start reading news in newsgroup GROUP.
2694 If SHOW-ALL is non-nil, already read articles are also listed.
2695 If NO-ARTICLE is non-nil, no article is selected initially.
2696 If NO-DISPLAY, don't generate a summary buffer."
2697   (let (result)
2698     (while (and group
2699                 (null (setq result
2700                             (let ((gnus-auto-select-next nil))
2701                               (or (gnus-summary-read-group-1
2702                                    group show-all no-article
2703                                    kill-buffer no-display
2704                                    select-articles)
2705                                   (setq show-all nil
2706                                         select-articles nil)))))
2707                 (eq gnus-auto-select-next 'quietly))
2708       (set-buffer gnus-group-buffer)
2709       ;; The entry function called above goes to the next
2710       ;; group automatically, so we go two groups back
2711       ;; if we are searching for the previous group.
2712       (when backward
2713         (gnus-group-prev-unread-group 2))
2714       (if (not (equal group (gnus-group-group-name)))
2715           (setq group (gnus-group-group-name))
2716         (setq group nil)))
2717     result))
2718
2719 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2720   "Directly jump to the other GROUP from summary buffer.
2721 If SHOW-ALL is non-nil, already read articles are also listed."
2722   (interactive
2723    (if (eq gnus-summary-buffer (current-buffer))
2724        (list (completing-read
2725               "Group: " gnus-active-hashtb nil t
2726               (when (and gnus-newsgroup-name
2727                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2728                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2729               'gnus-group-history)
2730              current-prefix-arg)
2731      (error "%s must be invoked from a gnus summary buffer." this-command)))
2732   (unless (or (zerop (length group))
2733               (and gnus-newsgroup-name
2734                    (string-equal gnus-newsgroup-name group)))
2735     (gnus-summary-exit)
2736     (gnus-summary-read-group group show-all
2737                              gnus-dont-select-after-jump-to-other-group)))
2738
2739 (defun gnus-summary-read-group-1 (group show-all no-article
2740                                         kill-buffer no-display
2741                                         &optional select-articles)
2742   ;; Killed foreign groups can't be entered.
2743   (when (and (not (gnus-group-native-p group))
2744              (not (gnus-gethash group gnus-newsrc-hashtb)))
2745     (error "Dead non-native groups can't be entered"))
2746   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2747   (let* ((new-group (gnus-summary-setup-buffer group))
2748          (quit-config (gnus-group-quit-config group))
2749          (did-select (and new-group (gnus-select-newsgroup
2750                                      group show-all select-articles))))
2751     (cond
2752      ;; This summary buffer exists already, so we just select it.
2753      ((not new-group)
2754       (gnus-set-global-variables)
2755       (when kill-buffer
2756         (gnus-kill-or-deaden-summary kill-buffer))
2757       (gnus-configure-windows 'summary 'force)
2758       (gnus-set-mode-line 'summary)
2759       (gnus-summary-position-point)
2760       (message "")
2761       t)
2762      ;; We couldn't select this group.
2763      ((null did-select)
2764       (when (and (eq major-mode 'gnus-summary-mode)
2765                  (not (equal (current-buffer) kill-buffer)))
2766         (kill-buffer (current-buffer))
2767         (if (not quit-config)
2768             (progn
2769               ;; Update the info -- marks might need to be removed,
2770               ;; for instance.
2771               (gnus-summary-update-info)
2772               (set-buffer gnus-group-buffer)
2773               (gnus-group-jump-to-group group)
2774               (gnus-group-next-unread-group 1))
2775           (gnus-handle-ephemeral-exit quit-config)))
2776       (gnus-message 3 "Can't select group")
2777       nil)
2778      ;; The user did a `C-g' while prompting for number of articles,
2779      ;; so we exit this group.
2780      ((eq did-select 'quit)
2781       (and (eq major-mode 'gnus-summary-mode)
2782            (not (equal (current-buffer) kill-buffer))
2783            (kill-buffer (current-buffer)))
2784       (when kill-buffer
2785         (gnus-kill-or-deaden-summary kill-buffer))
2786       (if (not quit-config)
2787           (progn
2788             (set-buffer gnus-group-buffer)
2789             (gnus-group-jump-to-group group)
2790             (gnus-group-next-unread-group 1)
2791             (gnus-configure-windows 'group 'force))
2792         (gnus-handle-ephemeral-exit quit-config))
2793       ;; Finally signal the quit.
2794       (signal 'quit nil))
2795      ;; The group was successfully selected.
2796      (t
2797       (gnus-set-global-variables)
2798       ;; Save the active value in effect when the group was entered.
2799       (setq gnus-newsgroup-active
2800             (gnus-copy-sequence
2801              (gnus-active gnus-newsgroup-name)))
2802       ;; You can change the summary buffer in some way with this hook.
2803       (gnus-run-hooks 'gnus-select-group-hook)
2804       ;; Set any local variables in the group parameters.
2805       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2806       (gnus-update-format-specifications
2807        nil 'summary 'summary-mode 'summary-dummy)
2808       (gnus-update-summary-mark-positions)
2809       ;; Do score processing.
2810       (when gnus-use-scoring
2811         (gnus-possibly-score-headers))
2812       ;; Check whether to fill in the gaps in the threads.
2813       (when gnus-build-sparse-threads
2814         (gnus-build-sparse-threads))
2815       ;; Find the initial limit.
2816       (if gnus-show-threads
2817           (if show-all
2818               (let ((gnus-newsgroup-dormant nil))
2819                 (gnus-summary-initial-limit show-all))
2820             (gnus-summary-initial-limit show-all))
2821         (setq gnus-newsgroup-limit
2822               (mapcar
2823                (lambda (header) (mail-header-number header))
2824                gnus-newsgroup-headers)))
2825       ;; Generate the summary buffer.
2826       (unless no-display
2827         (gnus-summary-prepare))
2828       (when gnus-use-trees
2829         (gnus-tree-open group)
2830         (setq gnus-summary-highlight-line-function
2831               'gnus-tree-highlight-article))
2832       ;; If the summary buffer is empty, but there are some low-scored
2833       ;; articles or some excluded dormants, we include these in the
2834       ;; buffer.
2835       (when (and (zerop (buffer-size))
2836                  (not no-display))
2837         (cond (gnus-newsgroup-dormant
2838                (gnus-summary-limit-include-dormant))
2839               ((and gnus-newsgroup-scored show-all)
2840                (gnus-summary-limit-include-expunged t))))
2841       ;; Function `gnus-apply-kill-file' must be called in this hook.
2842       (gnus-run-hooks 'gnus-apply-kill-hook)
2843       (if (and (zerop (buffer-size))
2844                (not no-display))
2845           (progn
2846             ;; This newsgroup is empty.
2847             (gnus-summary-catchup-and-exit nil t)
2848             (gnus-message 6 "No unread news")
2849             (when kill-buffer
2850               (gnus-kill-or-deaden-summary kill-buffer))
2851             ;; Return nil from this function.
2852             nil)
2853         ;; Hide conversation thread subtrees.  We cannot do this in
2854         ;; gnus-summary-prepare-hook since kill processing may not
2855         ;; work with hidden articles.
2856         (and gnus-show-threads
2857              gnus-thread-hide-subtree
2858              (gnus-summary-hide-all-threads))
2859         (when kill-buffer
2860           (gnus-kill-or-deaden-summary kill-buffer))
2861         ;; Show first unread article if requested.
2862         (if (and (not no-article)
2863                  (not no-display)
2864                  gnus-newsgroup-unreads
2865                  gnus-auto-select-first)
2866             (progn
2867               (gnus-configure-windows 'summary)
2868               (cond
2869                ((eq gnus-auto-select-first 'best)
2870                 (gnus-summary-best-unread-article))
2871                ((eq gnus-auto-select-first t)
2872                 (gnus-summary-first-unread-article))
2873                ((gnus-functionp gnus-auto-select-first)
2874                 (funcall gnus-auto-select-first))))
2875           ;; Don't select any articles, just move point to the first
2876           ;; article in the group.
2877           (goto-char (point-min))
2878           (gnus-summary-position-point)
2879           (gnus-configure-windows 'summary 'force)
2880           (gnus-set-mode-line 'summary))
2881         (when (get-buffer-window gnus-group-buffer t)
2882           ;; Gotta use windows, because recenter does weird stuff if
2883           ;; the current buffer ain't the displayed window.
2884           (let ((owin (selected-window)))
2885             (select-window (get-buffer-window gnus-group-buffer t))
2886             (when (gnus-group-goto-group group)
2887               (recenter))
2888             (select-window owin)))
2889         ;; Mark this buffer as "prepared".
2890         (setq gnus-newsgroup-prepared t)
2891         (gnus-run-hooks 'gnus-summary-prepared-hook)
2892         t)))))
2893
2894 (defun gnus-summary-prepare ()
2895   "Generate the summary buffer."
2896   (interactive)
2897   (let ((buffer-read-only nil))
2898     (erase-buffer)
2899     (setq gnus-newsgroup-data nil
2900           gnus-newsgroup-data-reverse nil)
2901     (gnus-run-hooks 'gnus-summary-generate-hook)
2902     ;; Generate the buffer, either with threads or without.
2903     (when gnus-newsgroup-headers
2904       (gnus-summary-prepare-threads
2905        (if gnus-show-threads
2906            (gnus-sort-gathered-threads
2907             (funcall gnus-summary-thread-gathering-function
2908                      (gnus-sort-threads
2909                       (gnus-cut-threads (gnus-make-threads)))))
2910          ;; Unthreaded display.
2911          (gnus-sort-articles gnus-newsgroup-headers))))
2912     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2913     ;; Call hooks for modifying summary buffer.
2914     (goto-char (point-min))
2915     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2916
2917 (defsubst gnus-general-simplify-subject (subject)
2918   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2919   (setq subject
2920         (cond
2921          ;; Truncate the subject.
2922          (gnus-simplify-subject-functions
2923           (gnus-map-function gnus-simplify-subject-functions subject))
2924          ((numberp gnus-summary-gather-subject-limit)
2925           (setq subject (gnus-simplify-subject-re subject))
2926           (if (> (length subject) gnus-summary-gather-subject-limit)
2927               (substring subject 0 gnus-summary-gather-subject-limit)
2928             subject))
2929          ;; Fuzzily simplify it.
2930          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2931           (gnus-simplify-subject-fuzzy subject))
2932          ;; Just remove the leading "Re:".
2933          (t
2934           (gnus-simplify-subject-re subject))))
2935
2936   (if (and gnus-summary-gather-exclude-subject
2937            (string-match gnus-summary-gather-exclude-subject subject))
2938       nil                               ; This article shouldn't be gathered
2939     subject))
2940
2941 (defun gnus-summary-simplify-subject-query ()
2942   "Query where the respool algorithm would put this article."
2943   (interactive)
2944   (gnus-summary-select-article)
2945   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2946
2947 (defun gnus-gather-threads-by-subject (threads)
2948   "Gather threads by looking at Subject headers."
2949   (if (not gnus-summary-make-false-root)
2950       threads
2951     (let ((hashtb (gnus-make-hashtable 1024))
2952           (prev threads)
2953           (result threads)
2954           subject hthread whole-subject)
2955       (while threads
2956         (setq subject (gnus-general-simplify-subject
2957                        (setq whole-subject (mail-header-subject
2958                                             (caar threads)))))
2959         (when subject
2960           (if (setq hthread (gnus-gethash subject hashtb))
2961               (progn
2962                 ;; We enter a dummy root into the thread, if we
2963                 ;; haven't done that already.
2964                 (unless (stringp (caar hthread))
2965                   (setcar hthread (list whole-subject (car hthread))))
2966                 ;; We add this new gathered thread to this gathered
2967                 ;; thread.
2968                 (setcdr (car hthread)
2969                         (nconc (cdar hthread) (list (car threads))))
2970                 ;; Remove it from the list of threads.
2971                 (setcdr prev (cdr threads))
2972                 (setq threads prev))
2973             ;; Enter this thread into the hash table.
2974             (gnus-sethash subject threads hashtb)))
2975         (setq prev threads)
2976         (setq threads (cdr threads)))
2977       result)))
2978
2979 (defun gnus-gather-threads-by-references (threads)
2980   "Gather threads by looking at References headers."
2981   (let ((idhashtb (gnus-make-hashtable 1024))
2982         (thhashtb (gnus-make-hashtable 1024))
2983         (prev threads)
2984         (result threads)
2985         ids references id gthread gid entered ref)
2986     (while threads
2987       (when (setq references (mail-header-references (caar threads)))
2988         (setq id (mail-header-id (caar threads))
2989               ids (gnus-split-references references)
2990               entered nil)
2991         (while (setq ref (pop ids))
2992           (setq ids (delete ref ids))
2993           (if (not (setq gid (gnus-gethash ref idhashtb)))
2994               (progn
2995                 (gnus-sethash ref id idhashtb)
2996                 (gnus-sethash id threads thhashtb))
2997             (setq gthread (gnus-gethash gid thhashtb))
2998             (unless entered
2999               ;; We enter a dummy root into the thread, if we
3000               ;; haven't done that already.
3001               (unless (stringp (caar gthread))
3002                 (setcar gthread (list (mail-header-subject (caar gthread))
3003                                       (car gthread))))
3004               ;; We add this new gathered thread to this gathered
3005               ;; thread.
3006               (setcdr (car gthread)
3007                       (nconc (cdar gthread) (list (car threads)))))
3008             ;; Add it into the thread hash table.
3009             (gnus-sethash id gthread thhashtb)
3010             (setq entered t)
3011             ;; Remove it from the list of threads.
3012             (setcdr prev (cdr threads))
3013             (setq threads prev))))
3014       (setq prev threads)
3015       (setq threads (cdr threads)))
3016     result))
3017
3018 (defun gnus-sort-gathered-threads (threads)
3019   "Sort subtreads inside each gathered thread by article number."
3020   (let ((result threads))
3021     (while threads
3022       (when (stringp (caar threads))
3023         (setcdr (car threads)
3024                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
3025       (setq threads (cdr threads)))
3026     result))
3027
3028 (defun gnus-thread-loop-p (root thread)
3029   "Say whether ROOT is in THREAD."
3030   (let ((stack (list thread))
3031         (infloop 0)
3032         th)
3033     (while (setq thread (pop stack))
3034       (setq th (cdr thread))
3035       (while (and th
3036                   (not (eq (caar th) root)))
3037         (pop th))
3038       (if th
3039           ;; We have found a loop.
3040           (let (ref-dep)
3041             (setcdr thread (delq (car th) (cdr thread)))
3042             (if (boundp (setq ref-dep (intern "none"
3043                                               gnus-newsgroup-dependencies)))
3044                 (setcdr (symbol-value ref-dep)
3045                         (nconc (cdr (symbol-value ref-dep))
3046                                (list (car th))))
3047               (set ref-dep (list nil (car th))))
3048             (setq infloop 1
3049                   stack nil))
3050         ;; Push all the subthreads onto the stack.
3051         (push (cdr thread) stack)))
3052     infloop))
3053
3054 (defun gnus-make-threads ()
3055   "Go through the dependency hashtb and find the roots.  Return all threads."
3056   (let (threads)
3057     (while (catch 'infloop
3058              (mapatoms
3059               (lambda (refs)
3060                 ;; Deal with self-referencing References loops.
3061                 (when (and (car (symbol-value refs))
3062                            (not (zerop
3063                                  (apply
3064                                   '+
3065                                   (mapcar
3066                                    (lambda (thread)
3067                                      (gnus-thread-loop-p
3068                                       (car (symbol-value refs)) thread))
3069                                    (cdr (symbol-value refs)))))))
3070                   (setq threads nil)
3071                   (throw 'infloop t))
3072                 (unless (car (symbol-value refs))
3073                   ;; These threads do not refer back to any other articles,
3074                   ;; so they're roots.
3075                   (setq threads (append (cdr (symbol-value refs)) threads))))
3076               gnus-newsgroup-dependencies)))
3077     threads))
3078
3079 ;; Build the thread tree.
3080 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3081   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3082
3083 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3084 if it was already present.
3085
3086 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3087 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3088 Message-IDs will be renamed be renamed to a unique Message-ID before
3089 being entered.
3090
3091 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3092   (let* ((id (mail-header-id header))
3093          (id-dep (and id (intern id dependencies)))
3094          ref ref-dep ref-header)
3095     ;; Enter this `header' in the `dependencies' table.
3096     (cond
3097      ((not id-dep)
3098       (setq header nil))
3099      ;; The first two cases do the normal part: enter a new `header'
3100      ;; in the `dependencies' table.
3101      ((not (boundp id-dep))
3102       (set id-dep (list header)))
3103      ((null (car (symbol-value id-dep)))
3104       (setcar (symbol-value id-dep) header))
3105
3106      ;; From here the `header' was already present in the
3107      ;; `dependencies' table.
3108      (force-new
3109       ;; Overrides an existing entry;
3110       ;; just set the header part of the entry.
3111       (setcar (symbol-value id-dep) header))
3112
3113      ;; Renames the existing `header' to a unique Message-ID.
3114      ((not gnus-summary-ignore-duplicates)
3115       ;; An article with this Message-ID has already been seen.
3116       ;; We rename the Message-ID.
3117       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3118            (list header))
3119       (mail-header-set-id header id))
3120
3121      ;; The last case ignores an existing entry, except it adds any
3122      ;; additional Xrefs (in case the two articles came from different
3123      ;; servers.
3124      ;; Also sets `header' to `nil' meaning that the `dependencies'
3125      ;; table was *not* modified.
3126      (t
3127       (mail-header-set-xref
3128        (car (symbol-value id-dep))
3129        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3130                    "")
3131                (or (mail-header-xref header) "")))
3132       (setq header nil)))
3133
3134     (when header
3135       ;; First check if that we are not creating a References loop.
3136       (setq ref (gnus-parent-id (mail-header-references header)))
3137       (while (and ref
3138                   (setq ref-dep (intern-soft ref dependencies))
3139                   (boundp ref-dep)
3140                   (setq ref-header (car (symbol-value ref-dep))))
3141         (if (string= id ref)
3142             ;; Yuk!  This is a reference loop.  Make the article be a
3143             ;; root article.
3144             (progn
3145               (mail-header-set-references (car (symbol-value id-dep)) "none")
3146               (setq ref nil))
3147           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3148       (setq ref (gnus-parent-id (mail-header-references header)))
3149       (setq ref-dep (intern (or ref "none") dependencies))
3150       (if (boundp ref-dep)
3151           (setcdr (symbol-value ref-dep)
3152                   (nconc (cdr (symbol-value ref-dep))
3153                          (list (symbol-value id-dep))))
3154         (set ref-dep (list nil (symbol-value id-dep)))))
3155     header))
3156
3157 (defun gnus-build-sparse-threads ()
3158   (let ((headers gnus-newsgroup-headers)
3159         (gnus-summary-ignore-duplicates t)
3160         header references generation relations
3161         subject child end new-child date)
3162     ;; First we create an alist of generations/relations, where
3163     ;; generations is how much we trust the relation, and the relation
3164     ;; is parent/child.
3165     (gnus-message 7 "Making sparse threads...")
3166     (save-excursion
3167       (nnheader-set-temp-buffer " *gnus sparse threads*")
3168       (while (setq header (pop headers))
3169         (when (and (setq references (mail-header-references header))
3170                    (not (string= references "")))
3171           (insert references)
3172           (setq child (mail-header-id header)
3173                 subject (mail-header-subject header)
3174                 date (mail-header-date header)
3175                 generation 0)
3176           (while (search-backward ">" nil t)
3177             (setq end (1+ (point)))
3178             (when (search-backward "<" nil t)
3179               (setq new-child (buffer-substring (point) end))
3180               (push (list (incf generation)
3181                           child (setq child new-child)
3182                           subject date)
3183                     relations)))
3184           (when child
3185             (push (list (1+ generation) child nil subject) relations))
3186           (erase-buffer)))
3187       (kill-buffer (current-buffer)))
3188     ;; Sort over trustworthiness.
3189     (mapcar
3190      (lambda (relation)
3191        (when (gnus-dependencies-add-header
3192               (make-full-mail-header
3193                gnus-reffed-article-number
3194                (nth 3 relation) "" (or (nth 4 relation) "")
3195                (nth 1 relation)
3196                (or (nth 2 relation) "") 0 0 "")
3197               gnus-newsgroup-dependencies nil)
3198          (push gnus-reffed-article-number gnus-newsgroup-limit)
3199          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3200          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3201                gnus-newsgroup-reads)
3202          (decf gnus-reffed-article-number)))
3203      (sort relations 'car-less-than-car))
3204     (gnus-message 7 "Making sparse threads...done")))
3205
3206 (defun gnus-build-old-threads ()
3207   ;; Look at all the articles that refer back to old articles, and
3208   ;; fetch the headers for the articles that aren't there.  This will
3209   ;; build complete threads - if the roots haven't been expired by the
3210   ;; server, that is.
3211   (let (id heads)
3212     (mapatoms
3213      (lambda (refs)
3214        (when (not (car (symbol-value refs)))
3215          (setq heads (cdr (symbol-value refs)))
3216          (while heads
3217            (if (memq (mail-header-number (caar heads))
3218                      gnus-newsgroup-dormant)
3219                (setq heads (cdr heads))
3220              (setq id (symbol-name refs))
3221              (while (and (setq id (gnus-build-get-header id))
3222                          (not (car (gnus-id-to-thread id)))))
3223              (setq heads nil)))))
3224      gnus-newsgroup-dependencies)))
3225
3226 ;; This function has to be called with point after the article number
3227 ;; on the beginning of the line.
3228 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3229   (let ((eol (gnus-point-at-eol))
3230         (buffer (current-buffer))
3231         header)
3232
3233     ;; overview: [num subject from date id refs chars lines misc]
3234     (unless (eobp)
3235       (forward-char))
3236
3237     (setq header
3238           (make-full-mail-header
3239            number                               ; number
3240            (nnheader-nov-field)                 ; subject
3241            (nnheader-nov-field)                 ; from
3242            (nnheader-nov-field)                 ; date
3243            (nnheader-nov-read-message-id)       ; id
3244            (nnheader-nov-field)                 ; refs
3245            (nnheader-nov-read-integer)          ; chars
3246            (nnheader-nov-read-integer)          ; lines
3247            (unless (eobp)
3248              (nnheader-nov-field))              ; misc
3249            (nnheader-nov-parse-extra)))         ; extra
3250
3251     (when gnus-alter-header-function
3252       (funcall gnus-alter-header-function header))
3253     (gnus-dependencies-add-header header dependencies force-new)))
3254
3255 (defun gnus-build-get-header (id)
3256   ;; Look through the buffer of NOV lines and find the header to
3257   ;; ID.  Enter this line into the dependencies hash table, and return
3258   ;; the id of the parent article (if any).
3259   (let ((deps gnus-newsgroup-dependencies)
3260         found header)
3261     (prog1
3262         (save-excursion
3263           (set-buffer nntp-server-buffer)
3264           (let ((case-fold-search nil))
3265             (goto-char (point-min))
3266             (while (and (not found)
3267                         (search-forward id nil t))
3268               (beginning-of-line)
3269               (setq found (looking-at
3270                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3271                                    (regexp-quote id))))
3272               (or found (beginning-of-line 2)))
3273             (when found
3274               (beginning-of-line)
3275               (and
3276                (setq header (gnus-nov-parse-line
3277                              (read (current-buffer)) deps))
3278                (gnus-parent-id (mail-header-references header))))))
3279       (when header
3280         (let ((number (mail-header-number header)))
3281           (push number gnus-newsgroup-limit)
3282           (push header gnus-newsgroup-headers)
3283           (if (memq number gnus-newsgroup-unselected)
3284               (progn
3285                 (push number gnus-newsgroup-unreads)
3286                 (setq gnus-newsgroup-unselected
3287                       (delq number gnus-newsgroup-unselected)))
3288             (push number gnus-newsgroup-ancient)))))))
3289
3290 (defun gnus-build-all-threads ()
3291   "Read all the headers."
3292   (let ((gnus-summary-ignore-duplicates t)
3293         (dependencies gnus-newsgroup-dependencies)
3294         header article)
3295     (save-excursion
3296       (set-buffer nntp-server-buffer)
3297       (let ((case-fold-search nil))
3298         (goto-char (point-min))
3299         (while (not (eobp))
3300           (ignore-errors
3301             (setq article (read (current-buffer))
3302                   header (gnus-nov-parse-line
3303                           article dependencies)))
3304           (when header
3305             (save-excursion
3306               (set-buffer gnus-summary-buffer)
3307               (push header gnus-newsgroup-headers)
3308               (if (memq (setq article (mail-header-number header))
3309                         gnus-newsgroup-unselected)
3310                   (progn
3311                     (push article gnus-newsgroup-unreads)
3312                     (setq gnus-newsgroup-unselected
3313                           (delq article gnus-newsgroup-unselected)))
3314                 (push article gnus-newsgroup-ancient)))
3315             (forward-line 1)))))))
3316
3317 (defun gnus-summary-update-article-line (article header)
3318   "Update the line for ARTICLE using HEADERS."
3319   (let* ((id (mail-header-id header))
3320          (thread (gnus-id-to-thread id)))
3321     (unless thread
3322       (error "Article in no thread"))
3323     ;; Update the thread.
3324     (setcar thread header)
3325     (gnus-summary-goto-subject article)
3326     (let* ((datal (gnus-data-find-list article))
3327            (data (car datal))
3328            (length (when (cdr datal)
3329                      (- (gnus-data-pos data)
3330                         (gnus-data-pos (cadr datal)))))
3331            (buffer-read-only nil)
3332            (level (gnus-summary-thread-level)))
3333       (gnus-delete-line)
3334       (gnus-summary-insert-line
3335        header level nil (gnus-article-mark article)
3336        (memq article gnus-newsgroup-replied)
3337        (memq article gnus-newsgroup-expirable)
3338        ;; Only insert the Subject string when it's different
3339        ;; from the previous Subject string.
3340        (if (gnus-subject-equal
3341             (condition-case ()
3342                 (mail-header-subject
3343                  (gnus-data-header
3344                   (cadr
3345                    (gnus-data-find-list
3346                     article
3347                     (gnus-data-list t)))))
3348               ;; Error on the side of excessive subjects.
3349               (error ""))
3350             (mail-header-subject header))
3351            ""
3352          (mail-header-subject header))
3353        nil (cdr (assq article gnus-newsgroup-scored))
3354        (memq article gnus-newsgroup-processable))
3355       (when length
3356         (gnus-data-update-list
3357          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3358
3359 (defun gnus-summary-update-article (article &optional iheader)
3360   "Update ARTICLE in the summary buffer."
3361   (set-buffer gnus-summary-buffer)
3362   (let* ((header (gnus-summary-article-header article))
3363          (id (mail-header-id header))
3364          (data (gnus-data-find article))
3365          (thread (gnus-id-to-thread id))
3366          (references (mail-header-references header))
3367          (parent
3368           (gnus-id-to-thread
3369            (or (gnus-parent-id
3370                 (when (and references
3371                            (not (equal "" references)))
3372                   references))
3373                "none")))
3374          (buffer-read-only nil)
3375          (old (car thread)))
3376     (when thread
3377       (unless iheader
3378         (setcar thread nil)
3379         (when parent
3380           (delq thread parent)))
3381       (if (gnus-summary-insert-subject id header)
3382           ;; Set the (possibly) new article number in the data structure.
3383           (gnus-data-set-number data (gnus-id-to-article id))
3384         (setcar thread old)
3385         nil))))
3386
3387 (defun gnus-rebuild-thread (id &optional line)
3388   "Rebuild the thread containing ID.
3389 If LINE, insert the rebuilt thread starting on line LINE."
3390   (let ((buffer-read-only nil)
3391         old-pos current thread data)
3392     (if (not gnus-show-threads)
3393         (setq thread (list (car (gnus-id-to-thread id))))
3394       ;; Get the thread this article is part of.
3395       (setq thread (gnus-remove-thread id)))
3396     (setq old-pos (gnus-point-at-bol))
3397     (setq current (save-excursion
3398                     (and (zerop (forward-line -1))
3399                          (gnus-summary-article-number))))
3400     ;; If this is a gathered thread, we have to go some re-gathering.
3401     (when (stringp (car thread))
3402       (let ((subject (car thread))
3403             roots thr)
3404         (setq thread (cdr thread))
3405         (while thread
3406           (unless (memq (setq thr (gnus-id-to-thread
3407                                    (gnus-root-id
3408                                     (mail-header-id (caar thread)))))
3409                         roots)
3410             (push thr roots))
3411           (setq thread (cdr thread)))
3412         ;; We now have all (unique) roots.
3413         (if (= (length roots) 1)
3414             ;; All the loose roots are now one solid root.
3415             (setq thread (car roots))
3416           (setq thread (cons subject (gnus-sort-threads roots))))))
3417     (let (threads)
3418       ;; We then insert this thread into the summary buffer.
3419       (when line
3420         (goto-char (point-min))
3421         (forward-line (1- line)))
3422       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3423         (if gnus-show-threads
3424             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3425           (gnus-summary-prepare-unthreaded thread))
3426         (setq data (nreverse gnus-newsgroup-data))
3427         (setq threads gnus-newsgroup-threads))
3428       ;; We splice the new data into the data structure.
3429       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3430       ;;!!! then we want to insert at the beginning of the buffer.
3431       ;;!!! That happens to be true with Gnus now, but that may
3432       ;;!!! change in the future.  Perhaps.
3433       (gnus-data-enter-list
3434        (if line nil current) data (- (point) old-pos))
3435       (setq gnus-newsgroup-threads
3436             (nconc threads gnus-newsgroup-threads))
3437       (gnus-data-compute-positions))))
3438
3439 (defun gnus-number-to-header (number)
3440   "Return the header for article NUMBER."
3441   (let ((headers gnus-newsgroup-headers))
3442     (while (and headers
3443                 (not (= number (mail-header-number (car headers)))))
3444       (pop headers))
3445     (when headers
3446       (car headers))))
3447
3448 (defun gnus-parent-headers (in-headers &optional generation)
3449   "Return the headers of the GENERATIONeth parent of HEADERS."
3450   (unless generation
3451     (setq generation 1))
3452   (let ((parent t)
3453         (headers in-headers)
3454         references)
3455     (while (and parent
3456                 (not (zerop generation))
3457                 (setq references (mail-header-references headers)))
3458       (setq headers (if (and references
3459                              (setq parent (gnus-parent-id references)))
3460                         (car (gnus-id-to-thread parent))
3461                       nil))
3462       (decf generation))
3463     (and (not (eq headers in-headers))
3464          headers)))
3465
3466 (defun gnus-id-to-thread (id)
3467   "Return the (sub-)thread where ID appears."
3468   (gnus-gethash id gnus-newsgroup-dependencies))
3469
3470 (defun gnus-id-to-article (id)
3471   "Return the article number of ID."
3472   (let ((thread (gnus-id-to-thread id)))
3473     (when (and thread
3474                (car thread))
3475       (mail-header-number (car thread)))))
3476
3477 (defun gnus-id-to-header (id)
3478   "Return the article headers of ID."
3479   (car (gnus-id-to-thread id)))
3480
3481 (defun gnus-article-displayed-root-p (article)
3482   "Say whether ARTICLE is a root(ish) article."
3483   (let ((level (gnus-summary-thread-level article))
3484         (refs (mail-header-references  (gnus-summary-article-header article)))
3485         particle)
3486     (cond
3487      ((null level) nil)
3488      ((zerop level) t)
3489      ((null refs) t)
3490      ((null (gnus-parent-id refs)) t)
3491      ((and (= 1 level)
3492            (null (setq particle (gnus-id-to-article
3493                                  (gnus-parent-id refs))))
3494            (null (gnus-summary-thread-level particle)))))))
3495
3496 (defun gnus-root-id (id)
3497   "Return the id of the root of the thread where ID appears."
3498   (let (last-id prev)
3499     (while (and id (setq prev (car (gnus-id-to-thread id))))
3500       (setq last-id id
3501             id (gnus-parent-id (mail-header-references prev))))
3502     last-id))
3503
3504 (defun gnus-articles-in-thread (thread)
3505   "Return the list of articles in THREAD."
3506   (cons (mail-header-number (car thread))
3507         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3508
3509 (defun gnus-remove-thread (id &optional dont-remove)
3510   "Remove the thread that has ID in it."
3511   (let (headers thread last-id)
3512     ;; First go up in this thread until we find the root.
3513     (setq last-id (gnus-root-id id)
3514           headers (message-flatten-list (gnus-id-to-thread last-id)))
3515     ;; We have now found the real root of this thread.  It might have
3516     ;; been gathered into some loose thread, so we have to search
3517     ;; through the threads to find the thread we wanted.
3518     (let ((threads gnus-newsgroup-threads)
3519           sub)
3520       (while threads
3521         (setq sub (car threads))
3522         (if (stringp (car sub))
3523             ;; This is a gathered thread, so we look at the roots
3524             ;; below it to find whether this article is in this
3525             ;; gathered root.
3526             (progn
3527               (setq sub (cdr sub))
3528               (while sub
3529                 (when (member (caar sub) headers)
3530                   (setq thread (car threads)
3531                         threads nil
3532                         sub nil))
3533                 (setq sub (cdr sub))))
3534           ;; It's an ordinary thread, so we check it.
3535           (when (eq (car sub) (car headers))
3536             (setq thread sub
3537                   threads nil)))
3538         (setq threads (cdr threads)))
3539       ;; If this article is in no thread, then it's a root.
3540       (if thread
3541           (unless dont-remove
3542             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3543         (setq thread (gnus-id-to-thread last-id)))
3544       (when thread
3545         (prog1
3546             thread                      ; We return this thread.
3547           (unless dont-remove
3548             (if (stringp (car thread))
3549                 (progn
3550                   ;; If we use dummy roots, then we have to remove the
3551                   ;; dummy root as well.
3552                   (when (eq gnus-summary-make-false-root 'dummy)
3553                     ;; We go to the dummy root by going to
3554                     ;; the first sub-"thread", and then one line up.
3555                     (gnus-summary-goto-article
3556                      (mail-header-number (caadr thread)))
3557                     (forward-line -1)
3558                     (gnus-delete-line)
3559                     (gnus-data-compute-positions))
3560                   (setq thread (cdr thread))
3561                   (while thread
3562                     (gnus-remove-thread-1 (car thread))
3563                     (setq thread (cdr thread))))
3564               (gnus-summary-show-all-threads)
3565               (gnus-remove-thread-1 thread))))))))
3566
3567 (defun gnus-remove-thread-1 (thread)
3568   "Remove the thread THREAD recursively."
3569   (let ((number (mail-header-number (pop thread)))
3570         d)
3571     (setq thread (reverse thread))
3572     (while thread
3573       (gnus-remove-thread-1 (pop thread)))
3574     (when (setq d (gnus-data-find number))
3575       (goto-char (gnus-data-pos d))
3576       (gnus-data-remove
3577        number
3578        (- (gnus-point-at-bol)
3579           (prog1
3580               (1+ (gnus-point-at-eol))
3581             (gnus-delete-line)))))))
3582
3583 (defun gnus-sort-threads (threads)
3584   "Sort THREADS."
3585   (if (not gnus-thread-sort-functions)
3586       threads
3587     (gnus-message 8 "Sorting threads...")
3588     (prog1
3589         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3590       (gnus-message 8 "Sorting threads...done"))))
3591
3592 (defun gnus-sort-articles (articles)
3593   "Sort ARTICLES."
3594   (when gnus-article-sort-functions
3595     (gnus-message 7 "Sorting articles...")
3596     (prog1
3597         (setq gnus-newsgroup-headers
3598               (sort articles (gnus-make-sort-function
3599                               gnus-article-sort-functions)))
3600       (gnus-message 7 "Sorting articles...done"))))
3601
3602 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3603 (defmacro gnus-thread-header (thread)
3604   ;; Return header of first article in THREAD.
3605   ;; Note that THREAD must never, ever be anything else than a variable -
3606   ;; using some other form will lead to serious barfage.
3607   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3608   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3609   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3610         (vector thread) 2))
3611
3612 (defsubst gnus-article-sort-by-number (h1 h2)
3613   "Sort articles by article number."
3614   (< (mail-header-number h1)
3615      (mail-header-number h2)))
3616
3617 (defun gnus-thread-sort-by-number (h1 h2)
3618   "Sort threads by root article number."
3619   (gnus-article-sort-by-number
3620    (gnus-thread-header h1) (gnus-thread-header h2)))
3621
3622 (defsubst gnus-article-sort-by-lines (h1 h2)
3623   "Sort articles by article Lines header."
3624   (< (mail-header-lines h1)
3625      (mail-header-lines h2)))
3626
3627 (defun gnus-thread-sort-by-lines (h1 h2)
3628   "Sort threads by root article Lines header."
3629   (gnus-article-sort-by-lines
3630    (gnus-thread-header h1) (gnus-thread-header h2)))
3631
3632 (defsubst gnus-article-sort-by-chars (h1 h2)
3633   "Sort articles by octet length."
3634   (< (mail-header-chars h1)
3635      (mail-header-chars h2)))
3636
3637 (defun gnus-thread-sort-by-chars (h1 h2)
3638   "Sort threads by root article octet length."
3639   (gnus-article-sort-by-chars
3640    (gnus-thread-header h1) (gnus-thread-header h2)))
3641
3642 (defsubst gnus-article-sort-by-author (h1 h2)
3643   "Sort articles by root author."
3644   (string-lessp
3645    (let ((addr (car (mime-read-field 'From h1))))
3646      (or (std11-full-name-string addr)
3647          (std11-address-string addr)
3648          ""))
3649    (let ((addr (car (mime-read-field 'From h2))))
3650      (or (std11-full-name-string addr)
3651          (std11-address-string addr)
3652          ""))
3653    ))
3654
3655 (defun gnus-thread-sort-by-author (h1 h2)
3656   "Sort threads by root author."
3657   (gnus-article-sort-by-author
3658    (gnus-thread-header h1)  (gnus-thread-header h2)))
3659
3660 (defsubst gnus-article-sort-by-subject (h1 h2)
3661   "Sort articles by root subject."
3662   (string-lessp
3663    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3664    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3665
3666 (defun gnus-thread-sort-by-subject (h1 h2)
3667   "Sort threads by root subject."
3668   (gnus-article-sort-by-subject
3669    (gnus-thread-header h1) (gnus-thread-header h2)))
3670
3671 (defsubst gnus-article-sort-by-date (h1 h2)
3672   "Sort articles by root article date."
3673   (time-less-p
3674    (gnus-date-get-time (mail-header-date h1))
3675    (gnus-date-get-time (mail-header-date h2))))
3676
3677 (defun gnus-thread-sort-by-date (h1 h2)
3678   "Sort threads by root article date."
3679   (gnus-article-sort-by-date
3680    (gnus-thread-header h1) (gnus-thread-header h2)))
3681
3682 (defsubst gnus-article-sort-by-score (h1 h2)
3683   "Sort articles by root article score.
3684 Unscored articles will be counted as having a score of zero."
3685   (> (or (cdr (assq (mail-header-number h1)
3686                     gnus-newsgroup-scored))
3687          gnus-summary-default-score 0)
3688      (or (cdr (assq (mail-header-number h2)
3689                     gnus-newsgroup-scored))
3690          gnus-summary-default-score 0)))
3691
3692 (defun gnus-thread-sort-by-score (h1 h2)
3693   "Sort threads by root article score."
3694   (gnus-article-sort-by-score
3695    (gnus-thread-header h1) (gnus-thread-header h2)))
3696
3697 (defun gnus-thread-sort-by-total-score (h1 h2)
3698   "Sort threads by the sum of all scores in the thread.
3699 Unscored articles will be counted as having a score of zero."
3700   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3701
3702 (defun gnus-thread-total-score (thread)
3703   ;; This function find the total score of THREAD.
3704   (cond ((null thread)
3705          0)
3706         ((consp thread)
3707          (if (stringp (car thread))
3708              (apply gnus-thread-score-function 0
3709                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3710            (gnus-thread-total-score-1 thread)))
3711         (t
3712          (gnus-thread-total-score-1 (list thread)))))
3713
3714 (defun gnus-thread-total-score-1 (root)
3715   ;; This function find the total score of the thread below ROOT.
3716   (setq root (car root))
3717   (apply gnus-thread-score-function
3718          (or (append
3719               (mapcar 'gnus-thread-total-score
3720                       (cdr (gnus-id-to-thread (mail-header-id root))))
3721               (when (> (mail-header-number root) 0)
3722                 (list (or (cdr (assq (mail-header-number root)
3723                                      gnus-newsgroup-scored))
3724                           gnus-summary-default-score 0))))
3725              (list gnus-summary-default-score)
3726              '(0))))
3727
3728 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3729 (defvar gnus-tmp-prev-subject nil)
3730 (defvar gnus-tmp-false-parent nil)
3731 (defvar gnus-tmp-root-expunged nil)
3732 (defvar gnus-tmp-dummy-line nil)
3733
3734 (defvar gnus-tmp-header)
3735 (defun gnus-extra-header (type &optional header)
3736   "Return the extra header of TYPE."
3737   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3738       ""))
3739
3740 (defun gnus-summary-prepare-threads (threads)
3741   "Prepare summary buffer from THREADS and indentation LEVEL.
3742 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3743 or a straight list of headers."
3744   (gnus-message 7 "Generating summary...")
3745
3746   (setq gnus-newsgroup-threads threads)
3747   (beginning-of-line)
3748
3749   (let ((gnus-tmp-level 0)
3750         (default-score (or gnus-summary-default-score 0))
3751         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3752         thread number subject stack state gnus-tmp-gathered beg-match
3753         new-roots gnus-tmp-new-adopts thread-end
3754         gnus-tmp-header gnus-tmp-unread
3755         gnus-tmp-replied gnus-tmp-subject-or-nil
3756         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3757         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3758         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3759
3760     (setq gnus-tmp-prev-subject nil)
3761
3762     (if (vectorp (car threads))
3763         ;; If this is a straight (sic) list of headers, then a
3764         ;; threaded summary display isn't required, so we just create
3765         ;; an unthreaded one.
3766         (gnus-summary-prepare-unthreaded threads)
3767
3768       ;; Do the threaded display.
3769
3770       (while (or threads stack gnus-tmp-new-adopts new-roots)
3771
3772         (if (and (= gnus-tmp-level 0)
3773                  (or (not stack)
3774                      (= (caar stack) 0))
3775                  (not gnus-tmp-false-parent)
3776                  (or gnus-tmp-new-adopts new-roots))
3777             (if gnus-tmp-new-adopts
3778                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3779                       thread (list (car gnus-tmp-new-adopts))
3780                       gnus-tmp-header (caar thread)
3781                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3782               (when new-roots
3783                 (setq thread (list (car new-roots))
3784                       gnus-tmp-header (caar thread)
3785                       new-roots (cdr new-roots))))
3786
3787           (if threads
3788               ;; If there are some threads, we do them before the
3789               ;; threads on the stack.
3790               (setq thread threads
3791                     gnus-tmp-header (caar thread))
3792             ;; There were no current threads, so we pop something off
3793             ;; the stack.
3794             (setq state (car stack)
3795                   gnus-tmp-level (car state)
3796                   thread (cdr state)
3797                   stack (cdr stack)
3798                   gnus-tmp-header (caar thread))))
3799
3800         (setq gnus-tmp-false-parent nil)
3801         (setq gnus-tmp-root-expunged nil)
3802         (setq thread-end nil)
3803
3804         (if (stringp gnus-tmp-header)
3805             ;; The header is a dummy root.
3806             (cond
3807              ((eq gnus-summary-make-false-root 'adopt)
3808               ;; We let the first article adopt the rest.
3809               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3810                                                (cddar thread)))
3811               (setq gnus-tmp-gathered
3812                     (nconc (mapcar
3813                             (lambda (h) (mail-header-number (car h)))
3814                             (cddar thread))
3815                            gnus-tmp-gathered))
3816               (setq thread (cons (list (caar thread)
3817                                        (cadar thread))
3818                                  (cdr thread)))
3819               (setq gnus-tmp-level -1
3820                     gnus-tmp-false-parent t))
3821              ((eq gnus-summary-make-false-root 'empty)
3822               ;; We print adopted articles with empty subject fields.
3823               (setq gnus-tmp-gathered
3824                     (nconc (mapcar
3825                             (lambda (h) (mail-header-number (car h)))
3826                             (cddar thread))
3827                            gnus-tmp-gathered))
3828               (setq gnus-tmp-level -1))
3829              ((eq gnus-summary-make-false-root 'dummy)
3830               ;; We remember that we probably want to output a dummy
3831               ;; root.
3832               (setq gnus-tmp-dummy-line gnus-tmp-header)
3833               (setq gnus-tmp-prev-subject gnus-tmp-header))
3834              (t
3835               ;; We do not make a root for the gathered
3836               ;; sub-threads at all.
3837               (setq gnus-tmp-level -1)))
3838
3839           (setq number (mail-header-number gnus-tmp-header)
3840                 subject (mail-header-subject gnus-tmp-header))
3841
3842           (cond
3843            ;; If the thread has changed subject, we might want to make
3844            ;; this subthread into a root.
3845            ((and (null gnus-thread-ignore-subject)
3846                  (not (zerop gnus-tmp-level))
3847                  gnus-tmp-prev-subject
3848                  (not (inline
3849                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3850             (setq new-roots (nconc new-roots (list (car thread)))
3851                   thread-end t
3852                   gnus-tmp-header nil))
3853            ;; If the article lies outside the current limit,
3854            ;; then we do not display it.
3855            ((not (memq number gnus-newsgroup-limit))
3856             (setq gnus-tmp-gathered
3857                   (nconc (mapcar
3858                           (lambda (h) (mail-header-number (car h)))
3859                           (cdar thread))
3860                          gnus-tmp-gathered))
3861             (setq gnus-tmp-new-adopts (if (cdar thread)
3862                                           (append gnus-tmp-new-adopts
3863                                                   (cdar thread))
3864                                         gnus-tmp-new-adopts)
3865                   thread-end t
3866                   gnus-tmp-header nil)
3867             (when (zerop gnus-tmp-level)
3868               (setq gnus-tmp-root-expunged t)))
3869            ;; Perhaps this article is to be marked as read?
3870            ((and gnus-summary-mark-below
3871                  (< (or (cdr (assq number gnus-newsgroup-scored))
3872                         default-score)
3873                     gnus-summary-mark-below)
3874                  ;; Don't touch sparse articles.
3875                  (not (gnus-summary-article-sparse-p number))
3876                  (not (gnus-summary-article-ancient-p number)))
3877             (setq gnus-newsgroup-unreads
3878                   (delq number gnus-newsgroup-unreads))
3879             (if gnus-newsgroup-auto-expire
3880                 (push number gnus-newsgroup-expirable)
3881               (push (cons number gnus-low-score-mark)
3882                     gnus-newsgroup-reads))))
3883
3884           (when gnus-tmp-header
3885             ;; We may have an old dummy line to output before this
3886             ;; article.
3887             (when (and gnus-tmp-dummy-line
3888                        (gnus-subject-equal
3889                         gnus-tmp-dummy-line
3890                         (mail-header-subject gnus-tmp-header)))
3891               (gnus-summary-insert-dummy-line
3892                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3893               (setq gnus-tmp-dummy-line nil))
3894
3895             ;; Compute the mark.
3896             (setq gnus-tmp-unread (gnus-article-mark number))
3897
3898             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3899                                   gnus-tmp-header gnus-tmp-level)
3900                   gnus-newsgroup-data)
3901
3902             ;; Actually insert the line.
3903             (setq
3904              gnus-tmp-subject-or-nil
3905              (cond
3906               ((and gnus-thread-ignore-subject
3907                     gnus-tmp-prev-subject
3908                     (not (inline (gnus-subject-equal
3909                                   gnus-tmp-prev-subject subject))))
3910                subject)
3911               ((zerop gnus-tmp-level)
3912                (if (and (eq gnus-summary-make-false-root 'empty)
3913                         (memq number gnus-tmp-gathered)
3914                         gnus-tmp-prev-subject
3915                         (inline (gnus-subject-equal
3916                                  gnus-tmp-prev-subject subject)))
3917                    gnus-summary-same-subject
3918                  subject))
3919               (t gnus-summary-same-subject)))
3920             (if (and (eq gnus-summary-make-false-root 'adopt)
3921                      (= gnus-tmp-level 1)
3922                      (memq number gnus-tmp-gathered))
3923                 (setq gnus-tmp-opening-bracket ?\<
3924                       gnus-tmp-closing-bracket ?\>)
3925               (setq gnus-tmp-opening-bracket ?\[
3926                     gnus-tmp-closing-bracket ?\]))
3927             (setq
3928              gnus-tmp-indentation
3929              (aref gnus-thread-indent-array gnus-tmp-level)
3930              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3931              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3932                                 gnus-summary-default-score 0)
3933              gnus-tmp-score-char
3934              (if (or (null gnus-summary-default-score)
3935                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3936                          gnus-summary-zcore-fuzz))
3937                  ? ;Whitespace
3938                (if (< gnus-tmp-score gnus-summary-default-score)
3939                    gnus-score-below-mark gnus-score-over-mark))
3940              gnus-tmp-replied
3941              (cond ((memq number gnus-newsgroup-processable)
3942                     gnus-process-mark)
3943                    ((memq number gnus-newsgroup-cached)
3944                     gnus-cached-mark)
3945                    ((memq number gnus-newsgroup-replied)
3946                     gnus-replied-mark)
3947                    ((memq number gnus-newsgroup-saved)
3948                     gnus-saved-mark)
3949                    (t gnus-unread-mark))
3950              gnus-tmp-from (mail-header-from gnus-tmp-header)
3951              gnus-tmp-name
3952              (cond
3953               ((string-match "<[^>]+> *$" gnus-tmp-from)
3954                (setq beg-match (match-beginning 0))
3955                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3956                         (substring gnus-tmp-from (1+ (match-beginning 0))
3957                                    (1- (match-end 0))))
3958                    (substring gnus-tmp-from 0 beg-match)))
3959               ((string-match "(.+)" gnus-tmp-from)
3960                (substring gnus-tmp-from
3961                           (1+ (match-beginning 0)) (1- (match-end 0))))
3962               (t gnus-tmp-from)))
3963             (when (string= gnus-tmp-name "")
3964               (setq gnus-tmp-name gnus-tmp-from))
3965             (unless (numberp gnus-tmp-lines)
3966               (setq gnus-tmp-lines 0))
3967             (gnus-put-text-property
3968              (point)
3969              (progn (eval gnus-summary-line-format-spec) (point))
3970              'gnus-number number)
3971             (when gnus-visual-p
3972               (forward-line -1)
3973               (gnus-run-hooks 'gnus-summary-update-hook)
3974               (forward-line 1))
3975
3976             (setq gnus-tmp-prev-subject subject)))
3977
3978         (when (nth 1 thread)
3979           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3980         (incf gnus-tmp-level)
3981         (setq threads (if thread-end nil (cdar thread)))
3982         (unless threads
3983           (setq gnus-tmp-level 0)))))
3984   (gnus-message 7 "Generating summary...done"))
3985
3986 (defun gnus-summary-prepare-unthreaded (headers)
3987   "Generate an unthreaded summary buffer based on HEADERS."
3988   (let (header number mark)
3989
3990     (beginning-of-line)
3991
3992     (while headers
3993       ;; We may have to root out some bad articles...
3994       (when (memq (setq number (mail-header-number
3995                                 (setq header (pop headers))))
3996                   gnus-newsgroup-limit)
3997         ;; Mark article as read when it has a low score.
3998         (when (and gnus-summary-mark-below
3999                    (< (or (cdr (assq number gnus-newsgroup-scored))
4000                           gnus-summary-default-score 0)
4001                       gnus-summary-mark-below)
4002                    (not (gnus-summary-article-ancient-p number)))
4003           (setq gnus-newsgroup-unreads
4004                 (delq number gnus-newsgroup-unreads))
4005           (if gnus-newsgroup-auto-expire
4006               (push number gnus-newsgroup-expirable)
4007             (push (cons number gnus-low-score-mark)
4008                   gnus-newsgroup-reads)))
4009
4010         (setq mark (gnus-article-mark number))
4011         (push (gnus-data-make number mark (1+ (point)) header 0)
4012               gnus-newsgroup-data)
4013         (gnus-summary-insert-line
4014          header 0 number
4015          mark (memq number gnus-newsgroup-replied)
4016          (memq number gnus-newsgroup-expirable)
4017          (mail-header-subject header) nil
4018          (cdr (assq number gnus-newsgroup-scored))
4019          (memq number gnus-newsgroup-processable))))))
4020
4021 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4022   "Select newsgroup GROUP.
4023 If READ-ALL is non-nil, all articles in the group are selected.
4024 If SELECT-ARTICLES, only select those articles from GROUP."
4025   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4026          ;;!!! Dirty hack; should be removed.
4027          (gnus-summary-ignore-duplicates
4028           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4029               t
4030             gnus-summary-ignore-duplicates))
4031          (info (nth 2 entry))
4032          articles fetched-articles cached)
4033
4034     (unless (gnus-check-server
4035              (setq gnus-current-select-method
4036                    (gnus-find-method-for-group group)))
4037       (error "Couldn't open server"))
4038
4039     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4040         (gnus-activate-group group)     ; Or we can activate it...
4041         (progn                          ; Or we bug out.
4042           (when (equal major-mode 'gnus-summary-mode)
4043             (kill-buffer (current-buffer)))
4044           (error "Couldn't request group %s: %s"
4045                  group (gnus-status-message group))))
4046
4047     (unless (gnus-request-group group t)
4048       (when (equal major-mode 'gnus-summary-mode)
4049         (kill-buffer (current-buffer)))
4050       (error "Couldn't request group %s: %s"
4051              group (gnus-status-message group)))
4052
4053     (setq gnus-newsgroup-name group)
4054     (setq gnus-newsgroup-unselected nil)
4055     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4056     (gnus-summary-setup-default-charset)
4057
4058     ;; Adjust and set lists of article marks.
4059     (when info
4060       (gnus-adjust-marked-articles info))
4061
4062     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4063     (setq cached
4064           (if (gnus-virtual-group-p group)
4065               gnus-newsgroup-cached
4066             (gnus-cache-articles-in-group group)))
4067
4068     (setq gnus-newsgroup-unreads
4069           (gnus-set-difference
4070            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4071            gnus-newsgroup-dormant))
4072
4073     (setq gnus-newsgroup-processable nil)
4074
4075     (gnus-update-read-articles group gnus-newsgroup-unreads)
4076
4077     (if (setq articles select-articles)
4078         (setq gnus-newsgroup-unselected
4079               (gnus-sorted-intersection
4080                gnus-newsgroup-unreads
4081                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4082       (setq articles (gnus-articles-to-read group read-all)))
4083
4084     (cond
4085      ((null articles)
4086       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4087       'quit)
4088      ((eq articles 0) nil)
4089      (t
4090       ;; Init the dependencies hash table.
4091       (setq gnus-newsgroup-dependencies
4092             (gnus-make-hashtable (length articles)))
4093       (gnus-set-global-variables)
4094       ;; Retrieve the headers and read them in.
4095       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4096       (setq gnus-newsgroup-headers
4097             (gnus-retrieve-parsed-headers
4098              articles gnus-newsgroup-name
4099              ;; We might want to fetch old headers, but
4100              ;; not if there is only 1 article.
4101              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4102                            (not (numberp gnus-fetch-old-headers)))
4103                       (> (length articles) 1))
4104                   gnus-fetch-old-headers)))
4105       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4106
4107       ;; Suppress duplicates?
4108       (when gnus-suppress-duplicates
4109         (gnus-dup-suppress-articles))
4110
4111       ;; Set the initial limit.
4112       (setq gnus-newsgroup-limit (copy-sequence articles))
4113       ;; Remove canceled articles from the list of unread articles.
4114       (setq gnus-newsgroup-unreads
4115             (gnus-set-sorted-intersection
4116              gnus-newsgroup-unreads
4117              (setq fetched-articles
4118                    (mapcar (lambda (headers) (mail-header-number headers))
4119                            gnus-newsgroup-headers))))
4120       ;; Removed marked articles that do not exist.
4121       (gnus-update-missing-marks
4122        (gnus-sorted-complement fetched-articles articles))
4123
4124       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4125       (when cached
4126         (setq gnus-newsgroup-cached cached))
4127
4128       ;; We might want to build some more threads first.
4129       (when (and gnus-fetch-old-headers
4130                  (eq gnus-headers-retrieved-by 'nov))
4131         (if (eq gnus-fetch-old-headers 'invisible)
4132             (gnus-build-all-threads)
4133           (gnus-build-old-threads)))
4134       ;; Let the Gnus agent mark articles as read.
4135       (when gnus-agent
4136         (gnus-agent-get-undownloaded-list))
4137       ;; Check whether auto-expire is to be done in this group.
4138       (setq gnus-newsgroup-auto-expire
4139             (gnus-group-auto-expirable-p group))
4140       ;; Set up the article buffer now, if necessary.
4141       (unless gnus-single-article-buffer
4142         (gnus-article-setup-buffer))
4143       ;; First and last article in this newsgroup.
4144       (when gnus-newsgroup-headers
4145         (setq gnus-newsgroup-begin
4146               (mail-header-number (car gnus-newsgroup-headers))
4147               gnus-newsgroup-end
4148               (mail-header-number
4149                (gnus-last-element gnus-newsgroup-headers))))
4150       ;; GROUP is successfully selected.
4151       (or gnus-newsgroup-headers t)))))
4152
4153 (defun gnus-articles-to-read (group &optional read-all)
4154   ;; Find out what articles the user wants to read.
4155   (let* ((articles
4156           ;; Select all articles if `read-all' is non-nil, or if there
4157           ;; are no unread articles.
4158           (if (or read-all
4159                   (and (zerop (length gnus-newsgroup-marked))
4160                        (zerop (length gnus-newsgroup-unreads)))
4161                   (eq (gnus-group-find-parameter group 'display)
4162                       'all))
4163               (gnus-uncompress-range (gnus-active group))
4164             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4165                           (copy-sequence gnus-newsgroup-unreads))
4166                   '<)))
4167          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4168          (scored (length scored-list))
4169          (number (length articles))
4170          (marked (+ (length gnus-newsgroup-marked)
4171                     (length gnus-newsgroup-dormant)))
4172          (select
4173           (cond
4174            ((numberp read-all)
4175             read-all)
4176            (t
4177             (condition-case ()
4178                 (cond
4179                  ((and (or (<= scored marked) (= scored number))
4180                        (natnump gnus-large-newsgroup)
4181                        (> number gnus-large-newsgroup))
4182                   (let ((input (read-from-minibuffer
4183                                 (format
4184                                  "How many articles from %s (max %d): "
4185                                  (gnus-limit-string gnus-newsgroup-name 35)
4186                                  number)
4187                                 (static-if (< emacs-major-version 20)
4188                                     (number-to-string gnus-large-newsgroup)
4189                                   (cons
4190                                    (number-to-string gnus-large-newsgroup)
4191                                    0)))))
4192                     (if (string-match "^[ \t]*$" input)
4193                         number
4194                       input)))
4195                  ((and (> scored marked) (< scored number)
4196                        (> (- scored number) 20))
4197                   (let ((input
4198                          (read-string
4199                           (format "%s %s (%d scored, %d total): "
4200                                   "How many articles from"
4201                                   group scored number))))
4202                     (if (string-match "^[ \t]*$" input)
4203                         number input)))
4204                  (t number))
4205               (quit nil))))))
4206     (setq select (if (stringp select) (string-to-number select) select))
4207     (if (or (null select) (zerop select))
4208         select
4209       (if (and (not (zerop scored)) (<= (abs select) scored))
4210           (progn
4211             (setq articles (sort scored-list '<))
4212             (setq number (length articles)))
4213         (setq articles (copy-sequence articles)))
4214
4215       (when (< (abs select) number)
4216         (if (< select 0)
4217             ;; Select the N oldest articles.
4218             (setcdr (nthcdr (1- (abs select)) articles) nil)
4219           ;; Select the N most recent articles.
4220           (setq articles (nthcdr (- number select) articles))))
4221       (setq gnus-newsgroup-unselected
4222             (gnus-sorted-intersection
4223              gnus-newsgroup-unreads
4224              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4225       articles)))
4226
4227 (defun gnus-killed-articles (killed articles)
4228   (let (out)
4229     (while articles
4230       (when (inline (gnus-member-of-range (car articles) killed))
4231         (push (car articles) out))
4232       (setq articles (cdr articles)))
4233     out))
4234
4235 (defun gnus-uncompress-marks (marks)
4236   "Uncompress the mark ranges in MARKS."
4237   (let ((uncompressed '(score bookmark))
4238         out)
4239     (while marks
4240       (if (memq (caar marks) uncompressed)
4241           (push (car marks) out)
4242         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4243       (setq marks (cdr marks)))
4244     out))
4245
4246 (defun gnus-adjust-marked-articles (info)
4247   "Set all article lists and remove all marks that are no longer valid."
4248   (let* ((marked-lists (gnus-info-marks info))
4249          (active (gnus-active (gnus-info-group info)))
4250          (min (car active))
4251          (max (cdr active))
4252          (types gnus-article-mark-lists)
4253          (uncompressed '(score bookmark killed))
4254          marks var articles article mark)
4255
4256     (while marked-lists
4257       (setq marks (pop marked-lists))
4258       (set (setq var (intern (format "gnus-newsgroup-%s"
4259                                      (car (rassq (setq mark (car marks))
4260                                                  types)))))
4261            (if (memq (car marks) uncompressed) (cdr marks)
4262              (gnus-uncompress-range (cdr marks))))
4263
4264       (setq articles (symbol-value var))
4265
4266       ;; All articles have to be subsets of the active articles.
4267       (cond
4268        ;; Adjust "simple" lists.
4269        ((memq mark '(tick dormant expire reply save))
4270         (while articles
4271           (when (or (< (setq article (pop articles)) min) (> article max))
4272             (set var (delq article (symbol-value var))))))
4273        ;; Adjust assocs.
4274        ((memq mark uncompressed)
4275         (when (not (listp (cdr (symbol-value var))))
4276           (set var (list (symbol-value var))))
4277         (when (not (listp (cdr articles)))
4278           (setq articles (list articles)))
4279         (while articles
4280           (when (or (not (consp (setq article (pop articles))))
4281                     (< (car article) min)
4282                     (> (car article) max))
4283             (set var (delq article (symbol-value var))))))))))
4284
4285 (defun gnus-update-missing-marks (missing)
4286   "Go through the list of MISSING articles and remove them from the mark lists."
4287   (when missing
4288     (let ((types gnus-article-mark-lists)
4289           var m)
4290       ;; Go through all types.
4291       (while types
4292         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4293         (when (symbol-value var)
4294           ;; This list has articles.  So we delete all missing articles
4295           ;; from it.
4296           (setq m missing)
4297           (while m
4298             (set var (delq (pop m) (symbol-value var)))))))))
4299
4300 (defun gnus-update-marks ()
4301   "Enter the various lists of marked articles into the newsgroup info list."
4302   (let ((types gnus-article-mark-lists)
4303         (info (gnus-get-info gnus-newsgroup-name))
4304         (uncompressed '(score bookmark killed))
4305         type list newmarked symbol delta-marks)
4306     (when info
4307       ;; Add all marks lists to the list of marks lists.
4308       (while (setq type (pop types))
4309         (setq list (symbol-value
4310                           (setq symbol
4311                                 (intern (format "gnus-newsgroup-%s"
4312                                                 (car type))))))
4313
4314         (when list
4315           ;; Get rid of the entries of the articles that have the
4316           ;; default score.
4317           (when (and (eq (cdr type) 'score)
4318                      gnus-save-score
4319                      list)
4320             (let* ((arts list)
4321                    (prev (cons nil list))
4322                    (all prev))
4323               (while arts
4324                 (if (or (not (consp (car arts)))
4325                         (= (cdar arts) gnus-summary-default-score))
4326                     (setcdr prev (cdr arts))
4327                   (setq prev arts))
4328                 (setq arts (cdr arts)))
4329               (setq list (cdr all)))))
4330
4331        (or (memq (cdr type) uncompressed)
4332            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4333        
4334        (when (gnus-check-backend-function 'request-set-mark
4335                                           gnus-newsgroup-name)
4336          ;; uncompressed:s are not proper flags (they are cons cells)
4337          ;; cache is a internal gnus flag
4338          (unless (memq (cdr type) (cons 'cache uncompressed))
4339            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4340                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4341                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4342              (if add
4343                  (push (list add 'add (list (cdr type))) delta-marks))
4344              (if del
4345                  (push (list del 'del (list (cdr type))) delta-marks)))))
4346           
4347         (when list
4348          (push (cons (cdr type) list) newmarked)))
4349
4350       (when delta-marks
4351         (unless (gnus-check-group gnus-newsgroup-name)
4352           (error "Can't open server for %s" gnus-newsgroup-name))
4353         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4354           
4355       ;; Enter these new marks into the info of the group.
4356       (if (nthcdr 3 info)
4357           (setcar (nthcdr 3 info) newmarked)
4358         ;; Add the marks lists to the end of the info.
4359         (when newmarked
4360           (setcdr (nthcdr 2 info) (list newmarked))))
4361
4362       ;; Cut off the end of the info if there's nothing else there.
4363       (let ((i 5))
4364         (while (and (> i 2)
4365                     (not (nth i info)))
4366           (when (nthcdr (decf i) info)
4367             (setcdr (nthcdr i info) nil)))))))
4368
4369 (defun gnus-set-mode-line (where)
4370   "This function sets the mode line of the article or summary buffers.
4371 If WHERE is `summary', the summary mode line format will be used."
4372   ;; Is this mode line one we keep updated?
4373   (when (and (memq where gnus-updated-mode-lines)
4374              (symbol-value
4375               (intern (format "gnus-%s-mode-line-format-spec" where))))
4376     (let (mode-string)
4377       (save-excursion
4378         ;; We evaluate this in the summary buffer since these
4379         ;; variables are buffer-local to that buffer.
4380         (set-buffer gnus-summary-buffer)
4381         ;; We bind all these variables that are used in the `eval' form
4382         ;; below.
4383         (let* ((mformat (symbol-value
4384                          (intern
4385                           (format "gnus-%s-mode-line-format-spec" where))))
4386                (gnus-tmp-group-name gnus-newsgroup-name)
4387                (gnus-tmp-article-number (or gnus-current-article 0))
4388                (gnus-tmp-unread gnus-newsgroup-unreads)
4389                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4390                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4391                (gnus-tmp-unread-and-unselected
4392                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4393                             (zerop gnus-tmp-unselected))
4394                        "")
4395                       ((zerop gnus-tmp-unselected)
4396                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4397                       (t (format "{%d(+%d) more}"
4398                                  gnus-tmp-unread-and-unticked
4399                                  gnus-tmp-unselected))))
4400                (gnus-tmp-subject
4401                 (if (and gnus-current-headers
4402                          (vectorp gnus-current-headers))
4403                     (gnus-mode-string-quote
4404                      (mail-header-subject gnus-current-headers))
4405                   ""))
4406                bufname-length max-len
4407                gnus-tmp-header);; passed as argument to any user-format-funcs
4408           (setq mode-string (eval mformat))
4409           (setq bufname-length (if (string-match "%b" mode-string)
4410                                    (- (length
4411                                        (buffer-name
4412                                         (if (eq where 'summary)
4413                                             nil
4414                                           (get-buffer gnus-article-buffer))))
4415                                       2)
4416                                  0))
4417           (setq max-len (max 4 (if gnus-mode-non-string-length
4418                                    (- (window-width)
4419                                       gnus-mode-non-string-length
4420                                       bufname-length)
4421                                  (length mode-string))))
4422           ;; We might have to chop a bit of the string off...
4423           (when (> (length mode-string) max-len)
4424             (setq mode-string
4425                   (concat (gnus-truncate-string mode-string (- max-len 3))
4426                           "...")))
4427           ;; Pad the mode string a bit.
4428           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4429       ;; Update the mode line.
4430       (setq mode-line-buffer-identification
4431             (gnus-mode-line-buffer-identification (list mode-string)))
4432       (set-buffer-modified-p t))))
4433
4434 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4435   "Go through the HEADERS list and add all Xrefs to a hash table.
4436 The resulting hash table is returned, or nil if no Xrefs were found."
4437   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4438          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4439          (xref-hashtb (gnus-make-hashtable))
4440          start group entry number xrefs header)
4441     (while headers
4442       (setq header (pop headers))
4443       (when (and (setq xrefs (mail-header-xref header))
4444                  (not (memq (setq number (mail-header-number header))
4445                             unreads)))
4446         (setq start 0)
4447         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4448           (setq start (match-end 0))
4449           (setq group (if prefix
4450                           (concat prefix (substring xrefs (match-beginning 1)
4451                                                     (match-end 1)))
4452                         (substring xrefs (match-beginning 1) (match-end 1))))
4453           (setq number
4454                 (string-to-int (substring xrefs (match-beginning 2)
4455                                           (match-end 2))))
4456           (if (setq entry (gnus-gethash group xref-hashtb))
4457               (setcdr entry (cons number (cdr entry)))
4458             (gnus-sethash group (cons number nil) xref-hashtb)))))
4459     (and start xref-hashtb)))
4460
4461 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4462   "Look through all the headers and mark the Xrefs as read."
4463   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4464         name entry info xref-hashtb idlist method nth4)
4465     (save-excursion
4466       (set-buffer gnus-group-buffer)
4467       (when (setq xref-hashtb
4468                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4469         (mapatoms
4470          (lambda (group)
4471            (unless (string= from-newsgroup (setq name (symbol-name group)))
4472              (setq idlist (symbol-value group))
4473              ;; Dead groups are not updated.
4474              (and (prog1
4475                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4476                             info (nth 2 entry))
4477                     (when (stringp (setq nth4 (gnus-info-method info)))
4478                       (setq nth4 (gnus-server-to-method nth4))))
4479                   ;; Only do the xrefs if the group has the same
4480                   ;; select method as the group we have just read.
4481                   (or (gnus-methods-equal-p
4482                        nth4 (gnus-find-method-for-group from-newsgroup))
4483                       virtual
4484                       (equal nth4 (setq method (gnus-find-method-for-group
4485                                                 from-newsgroup)))
4486                       (and (equal (car nth4) (car method))
4487                            (equal (nth 1 nth4) (nth 1 method))))
4488                   gnus-use-cross-reference
4489                   (or (not (eq gnus-use-cross-reference t))
4490                       virtual
4491                       ;; Only do cross-references on subscribed
4492                       ;; groups, if that is what is wanted.
4493                       (<= (gnus-info-level info) gnus-level-subscribed))
4494                   (gnus-group-make-articles-read name idlist))))
4495          xref-hashtb)))))
4496
4497 (defun gnus-compute-read-articles (group articles)
4498   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4499          (info (nth 2 entry))
4500          (active (gnus-active group))
4501          ninfo)
4502     (when entry
4503       ;; First peel off all invalid article numbers.
4504       (when active
4505         (let ((ids articles)
4506               id first)
4507           (while (setq id (pop ids))
4508             (when (and first (> id (cdr active)))
4509               ;; We'll end up in this situation in one particular
4510               ;; obscure situation.  If you re-scan a group and get
4511               ;; a new article that is cross-posted to a different
4512               ;; group that has not been re-scanned, you might get
4513               ;; crossposted article that has a higher number than
4514               ;; Gnus believes possible.  So we re-activate this
4515               ;; group as well.  This might mean doing the
4516               ;; crossposting thingy will *increase* the number
4517               ;; of articles in some groups.  Tsk, tsk.
4518               (setq active (or (gnus-activate-group group) active)))
4519             (when (or (> id (cdr active))
4520                       (< id (car active)))
4521               (setq articles (delq id articles))))))
4522       ;; If the read list is nil, we init it.
4523       (if (and active
4524                (null (gnus-info-read info))
4525                (> (car active) 1))
4526           (setq ninfo (cons 1 (1- (car active))))
4527         (setq ninfo (gnus-info-read info)))
4528       ;; Then we add the read articles to the range.
4529       (gnus-add-to-range
4530        ninfo (setq articles (sort articles '<))))))
4531
4532 (defun gnus-group-make-articles-read (group articles)
4533   "Update the info of GROUP to say that ARTICLES are read."
4534   (let* ((num 0)
4535          (entry (gnus-gethash group gnus-newsrc-hashtb))
4536          (info (nth 2 entry))
4537          (active (gnus-active group))
4538          range)
4539     (when entry
4540       (setq range (gnus-compute-read-articles group articles))
4541       (save-excursion
4542         (set-buffer gnus-group-buffer)
4543         (gnus-undo-register
4544           `(progn
4545              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4546              (gnus-info-set-read ',info ',(gnus-info-read info))
4547              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4548              (gnus-group-update-group ,group t))))
4549       ;; Add the read articles to the range.
4550       (gnus-info-set-read info range)
4551       ;; Then we have to re-compute how many unread
4552       ;; articles there are in this group.
4553       (when active
4554         (cond
4555          ((not range)
4556           (setq num (- (1+ (cdr active)) (car active))))
4557          ((not (listp (cdr range)))
4558           (setq num (- (cdr active) (- (1+ (cdr range))
4559                                        (car range)))))
4560          (t
4561           (while range
4562             (if (numberp (car range))
4563                 (setq num (1+ num))
4564               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4565             (setq range (cdr range)))
4566           (setq num (- (cdr active) num))))
4567         ;; Update the number of unread articles.
4568         (setcar entry num)
4569         ;; Update the group buffer.
4570         (gnus-group-update-group group t)))))
4571
4572 (defvar gnus-newsgroup-none-id 0)
4573
4574 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4575   (let ((cur nntp-server-buffer)
4576         (dependencies
4577          (or dependencies
4578              (save-excursion (set-buffer gnus-summary-buffer)
4579                              gnus-newsgroup-dependencies)))
4580         headers id end ref
4581         (mail-parse-charset gnus-newsgroup-charset)
4582         (mail-parse-ignored-charsets
4583          (save-excursion (condition-case nil
4584                              (set-buffer gnus-summary-buffer)
4585                            (error))
4586                          gnus-newsgroup-ignored-charsets)))
4587     (save-excursion
4588       (set-buffer nntp-server-buffer)
4589       ;; Translate all TAB characters into SPACE characters.
4590       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4591       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4592       (gnus-run-hooks 'gnus-parse-headers-hook)
4593       (let ((case-fold-search t)
4594             in-reply-to header p lines chars ctype)
4595         (goto-char (point-min))
4596         ;; Search to the beginning of the next header.  Error messages
4597         ;; do not begin with 2 or 3.
4598         (while (re-search-forward "^[23][0-9]+ " nil t)
4599           (setq id nil
4600                 ref nil)
4601           ;; This implementation of this function, with nine
4602           ;; search-forwards instead of the one re-search-forward and
4603           ;; a case (which basically was the old function) is actually
4604           ;; about twice as fast, even though it looks messier.  You
4605           ;; can't have everything, I guess.  Speed and elegance
4606           ;; doesn't always go hand in hand.
4607           (setq
4608            header
4609            (make-full-mail-header
4610             ;; Number.
4611             (prog1
4612                 (read cur)
4613               (end-of-line)
4614               (setq p (point))
4615               (narrow-to-region (point)
4616                                 (or (and (search-forward "\n.\n" nil t)
4617                                          (- (point) 2))
4618                                     (point))))
4619             ;; Subject.
4620             (progn
4621               (goto-char p)
4622               (if (search-forward "\nsubject: " nil t)
4623                   (buffer-substring (match-end 0) (std11-field-end))
4624                 "(none)"))
4625             ;; From.
4626             (progn
4627               (goto-char p)
4628               (if (search-forward "\nfrom: " nil t)
4629                   (buffer-substring (match-end 0) (std11-field-end))
4630                 "(nobody)"))
4631             ;; Date.
4632             (progn
4633               (goto-char p)
4634               (if (search-forward "\ndate: " nil t)
4635                   (buffer-substring (match-end 0) (std11-field-end))
4636                 ""))
4637             ;; Message-ID.
4638             (progn
4639               (goto-char p)
4640               (setq id (if (re-search-forward
4641                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4642                            ;; We do it this way to make sure the Message-ID
4643                            ;; is (somewhat) syntactically valid.
4644                            (buffer-substring (match-beginning 1)
4645                                              (match-end 1))
4646                          ;; If there was no message-id, we just fake one
4647                          ;; to make subsequent routines simpler.
4648                          (nnheader-generate-fake-message-id))))
4649             ;; References.
4650             (progn
4651               (goto-char p)
4652               (if (search-forward "\nreferences: " nil t)
4653                   (progn
4654                     (setq end (point))
4655                     (prog1
4656                         (buffer-substring (match-end 0) (std11-field-end))
4657                       (setq ref
4658                             (buffer-substring
4659                              (progn
4660                                ;; (end-of-line)
4661                                (search-backward ">" end t)
4662                                (1+ (point)))
4663                              (progn
4664                                (search-backward "<" end t)
4665                                (point))))))
4666                 ;; Get the references from the in-reply-to header if there
4667                 ;; were no references and the in-reply-to header looks
4668                 ;; promising.
4669                 (if (and (search-forward "\nin-reply-to: " nil t)
4670                          (setq in-reply-to
4671                                (buffer-substring (match-end 0)
4672                                                  (std11-field-end)))
4673                          (string-match "<[^>]+>" in-reply-to))
4674                     (let (ref2)
4675                       (setq ref (substring in-reply-to (match-beginning 0)
4676                                            (match-end 0)))
4677                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4678                         (setq ref2 (substring in-reply-to (match-beginning 0)
4679                                               (match-end 0)))
4680                         (when (> (length ref2) (length ref))
4681                           (setq ref ref2)))
4682                       ref)
4683                   (setq ref nil))))
4684             ;; Chars.
4685             (progn
4686               (goto-char p)
4687               (if (search-forward "\nchars: " nil t)
4688                   (if (numberp (setq chars (ignore-errors (read cur))))
4689                       chars 0)
4690                 0))
4691             ;; Lines.
4692             (progn
4693               (goto-char p)
4694               (if (search-forward "\nlines: " nil t)
4695                   (if (numberp (setq lines (ignore-errors (read cur))))
4696                       lines 0)
4697                 0))
4698             ;; Xref.
4699             (progn
4700               (goto-char p)
4701               (and (search-forward "\nxref: " nil t)
4702                    (buffer-substring (match-end 0) (std11-field-end))))
4703             ;; Extra.
4704             (when gnus-extra-headers
4705               (let ((extra gnus-extra-headers)
4706                     out)
4707                 (while extra
4708                   (goto-char p)
4709                   (when (search-forward
4710                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4711                     (push (cons (car extra)
4712                                 (buffer-substring (match-end 0)
4713                                                   (std11-field-end)))
4714                           out))
4715                   (pop extra))
4716                 out))))
4717           (goto-char p)
4718           (if (and (search-forward "\ncontent-type: " nil t)
4719                    (setq ctype
4720                          (buffer-substring (match-end 0) (std11-field-end))))
4721               (mime-entity-set-content-type-internal
4722                header (mime-parse-Content-Type ctype)))
4723           (when (equal id ref)
4724             (setq ref nil))
4725
4726           (when gnus-alter-header-function
4727             (funcall gnus-alter-header-function header)
4728             (setq id (mail-header-id header)
4729                   ref (gnus-parent-id (mail-header-references header))))
4730
4731           (when (setq header
4732                       (gnus-dependencies-add-header
4733                        header dependencies force-new))
4734             (push header headers))
4735           (goto-char (point-max))
4736           (widen))
4737         (nreverse headers)))))
4738
4739 ;; Goes through the xover lines and returns a list of vectors
4740 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4741                                                   force-new dependencies
4742                                                   group also-fetch-heads)
4743   "Parse the news overview data in the server buffer, and return a
4744 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4745   ;; Get the Xref when the users reads the articles since most/some
4746   ;; NNTP servers do not include Xrefs when using XOVER.
4747   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4748   (let ((mail-parse-charset gnus-newsgroup-charset)
4749         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4750         (cur nntp-server-buffer)
4751         (dependencies (or dependencies gnus-newsgroup-dependencies))
4752         number headers header)
4753     (save-excursion
4754       (set-buffer nntp-server-buffer)
4755       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4756       ;; Allow the user to mangle the headers before parsing them.
4757       (gnus-run-hooks 'gnus-parse-headers-hook)
4758       (goto-char (point-min))
4759       (while (not (eobp))
4760         (condition-case ()
4761             (while (and sequence (not (eobp)))
4762               (setq number (read cur))
4763               (while (and sequence
4764                           (< (car sequence) number))
4765                 (setq sequence (cdr sequence)))
4766               (and sequence
4767                    (eq number (car sequence))
4768                    (progn
4769                      (setq sequence (cdr sequence))
4770                      (setq header (inline
4771                                     (gnus-nov-parse-line
4772                                      number dependencies force-new))))
4773                    (push header headers))
4774               (forward-line 1))
4775           (error
4776            (gnus-error 4 "Strange nov line (%d)"
4777                        (count-lines (point-min) (point)))))
4778         (forward-line 1))
4779       ;; A common bug in inn is that if you have posted an article and
4780       ;; then retrieves the active file, it will answer correctly --
4781       ;; the new article is included.  However, a NOV entry for the
4782       ;; article may not have been generated yet, so this may fail.
4783       ;; We work around this problem by retrieving the last few
4784       ;; headers using HEAD.
4785       (if (or (not also-fetch-heads)
4786               (not sequence))
4787           ;; We (probably) got all the headers.
4788           (nreverse headers)
4789         (let ((gnus-nov-is-evil t))
4790           (nconc
4791            (nreverse headers)
4792            (gnus-retrieve-parsed-headers sequence group)
4793            ))))))
4794
4795 (defun gnus-article-get-xrefs ()
4796   "Fill in the Xref value in `gnus-current-headers', if necessary.
4797 This is meant to be called in `gnus-article-internal-prepare-hook'."
4798   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4799                                  gnus-current-headers)))
4800     (or (not gnus-use-cross-reference)
4801         (not headers)
4802         (and (mail-header-xref headers)
4803              (not (string= (mail-header-xref headers) "")))
4804         (let ((case-fold-search t)
4805               xref)
4806           (save-restriction
4807             (nnheader-narrow-to-headers)
4808             (goto-char (point-min))
4809             (when (or (and (eq (downcase (char-after)) ?x)
4810                            (looking-at "Xref:"))
4811                       (search-forward "\nXref:" nil t))
4812               (goto-char (1+ (match-end 0)))
4813               (setq xref (buffer-substring (point)
4814                                            (progn (end-of-line) (point))))
4815               (mail-header-set-xref headers xref)))))))
4816
4817 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4818   "Find article ID and insert the summary line for that article.
4819 OLD-HEADER can either be a header or a line number to insert
4820 the subject line on."
4821   (let* ((line (and (numberp old-header) old-header))
4822          (old-header (and (vectorp old-header) old-header))
4823          (header (cond ((and old-header use-old-header)
4824                         old-header)
4825                        ((and (numberp id)
4826                              (gnus-number-to-header id))
4827                         (gnus-number-to-header id))
4828                        (t
4829                         (gnus-read-header id))))
4830          (number (and (numberp id) id))
4831          d)
4832     (when header
4833       ;; Rebuild the thread that this article is part of and go to the
4834       ;; article we have fetched.
4835       (when (and (not gnus-show-threads)
4836                  old-header)
4837         (when (and number
4838                    (setq d (gnus-data-find (mail-header-number old-header))))
4839           (goto-char (gnus-data-pos d))
4840           (gnus-data-remove
4841            number
4842            (- (gnus-point-at-bol)
4843               (prog1
4844                   (1+ (gnus-point-at-eol))
4845                 (gnus-delete-line))))))
4846       (when old-header
4847         (mail-header-set-number header (mail-header-number old-header)))
4848       (setq gnus-newsgroup-sparse
4849             (delq (setq number (mail-header-number header))
4850                   gnus-newsgroup-sparse))
4851       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4852       (push number gnus-newsgroup-limit)
4853       (gnus-rebuild-thread (mail-header-id header) line)
4854       (gnus-summary-goto-subject number nil t))
4855     (when (and (numberp number)
4856                (> number 0))
4857       ;; We have to update the boundaries even if we can't fetch the
4858       ;; article if ID is a number -- so that the next `P' or `N'
4859       ;; command will fetch the previous (or next) article even
4860       ;; if the one we tried to fetch this time has been canceled.
4861       (when (> number gnus-newsgroup-end)
4862         (setq gnus-newsgroup-end number))
4863       (when (< number gnus-newsgroup-begin)
4864         (setq gnus-newsgroup-begin number))
4865       (setq gnus-newsgroup-unselected
4866             (delq number gnus-newsgroup-unselected)))
4867     ;; Report back a success?
4868     (and header (mail-header-number header))))
4869
4870 ;;; Process/prefix in the summary buffer
4871
4872 (defun gnus-summary-work-articles (n)
4873   "Return a list of articles to be worked upon.
4874 The prefix argument, the list of process marked articles, and the
4875 current article will be taken into consideration."
4876   (save-excursion
4877     (set-buffer gnus-summary-buffer)
4878     (cond
4879      (n
4880       ;; A numerical prefix has been given.
4881       (setq n (prefix-numeric-value n))
4882       (let ((backward (< n 0))
4883             (n (abs (prefix-numeric-value n)))
4884             articles article)
4885         (save-excursion
4886           (while
4887               (and (> n 0)
4888                    (push (setq article (gnus-summary-article-number))
4889                          articles)
4890                    (if backward
4891                        (gnus-summary-find-prev nil article)
4892                      (gnus-summary-find-next nil article)))
4893             (decf n)))
4894         (nreverse articles)))
4895      ((and (gnus-region-active-p) (mark))
4896       (message "region active")
4897       ;; Work on the region between point and mark.
4898       (let ((max (max (point) (mark)))
4899             articles article)
4900         (save-excursion
4901           (goto-char (min (point) (mark)))
4902           (while
4903               (and
4904                (push (setq article (gnus-summary-article-number)) articles)
4905                (gnus-summary-find-next nil article)
4906                (< (point) max)))
4907           (nreverse articles))))
4908      (gnus-newsgroup-processable
4909       ;; There are process-marked articles present.
4910       ;; Save current state.
4911       (gnus-summary-save-process-mark)
4912       ;; Return the list.
4913       (reverse gnus-newsgroup-processable))
4914      (t
4915       ;; Just return the current article.
4916       (list (gnus-summary-article-number))))))
4917
4918 (defmacro gnus-summary-iterate (arg &rest forms)
4919   "Iterate over the process/prefixed articles and do FORMS.
4920 ARG is the interactive prefix given to the command.  FORMS will be
4921 executed with point over the summary line of the articles."
4922   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4923     `(let ((,articles (gnus-summary-work-articles ,arg)))
4924        (while ,articles
4925          (gnus-summary-goto-subject (car ,articles))
4926          ,@forms
4927          (pop ,articles)))))
4928
4929 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4930 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4931
4932 (defun gnus-summary-save-process-mark ()
4933   "Push the current set of process marked articles on the stack."
4934   (interactive)
4935   (push (copy-sequence gnus-newsgroup-processable)
4936         gnus-newsgroup-process-stack))
4937
4938 (defun gnus-summary-kill-process-mark ()
4939   "Push the current set of process marked articles on the stack and unmark."
4940   (interactive)
4941   (gnus-summary-save-process-mark)
4942   (gnus-summary-unmark-all-processable))
4943
4944 (defun gnus-summary-yank-process-mark ()
4945   "Pop the last process mark state off the stack and restore it."
4946   (interactive)
4947   (unless gnus-newsgroup-process-stack
4948     (error "Empty mark stack"))
4949   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4950
4951 (defun gnus-summary-process-mark-set (set)
4952   "Make SET into the current process marked articles."
4953   (gnus-summary-unmark-all-processable)
4954   (while set
4955     (gnus-summary-set-process-mark (pop set))))
4956
4957 ;;; Searching and stuff
4958
4959 (defun gnus-summary-search-group (&optional backward use-level)
4960   "Search for next unread newsgroup.
4961 If optional argument BACKWARD is non-nil, search backward instead."
4962   (save-excursion
4963     (set-buffer gnus-group-buffer)
4964     (when (gnus-group-search-forward
4965            backward nil (if use-level (gnus-group-group-level) nil))
4966       (gnus-group-group-name))))
4967
4968 (defun gnus-summary-best-group (&optional exclude-group)
4969   "Find the name of the best unread group.
4970 If EXCLUDE-GROUP, do not go to this group."
4971   (save-excursion
4972     (set-buffer gnus-group-buffer)
4973     (save-excursion
4974       (gnus-group-best-unread-group exclude-group))))
4975
4976 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4977   (if backward (gnus-summary-find-prev)
4978     (let* ((dummy (gnus-summary-article-intangible-p))
4979            (article (or article (gnus-summary-article-number)))
4980            (arts (gnus-data-find-list article))
4981            result)
4982       (when (and (not dummy)
4983                  (or (not gnus-summary-check-current)
4984                      (not unread)
4985                      (not (gnus-data-unread-p (car arts)))))
4986         (setq arts (cdr arts)))
4987       (when (setq result
4988                   (if unread
4989                       (progn
4990                         (while arts
4991                           (when (or (and undownloaded
4992                                          (eq gnus-undownloaded-mark
4993                                              (gnus-data-mark (car arts))))
4994                                     (gnus-data-unread-p (car arts)))
4995                             (setq result (car arts)
4996                                   arts nil))
4997                           (setq arts (cdr arts)))
4998                         result)
4999                     (car arts)))
5000         (goto-char (gnus-data-pos result))
5001         (gnus-data-number result)))))
5002
5003 (defun gnus-summary-find-prev (&optional unread article)
5004   (let* ((eobp (eobp))
5005          (article (or article (gnus-summary-article-number)))
5006          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5007          result)
5008     (when (and (not eobp)
5009                (or (not gnus-summary-check-current)
5010                    (not unread)
5011                    (not (gnus-data-unread-p (car arts)))))
5012       (setq arts (cdr arts)))
5013     (when (setq result
5014                 (if unread
5015                     (progn
5016                       (while arts
5017                         (when (gnus-data-unread-p (car arts))
5018                           (setq result (car arts)
5019                                 arts nil))
5020                         (setq arts (cdr arts)))
5021                       result)
5022                   (car arts)))
5023       (goto-char (gnus-data-pos result))
5024       (gnus-data-number result))))
5025
5026 (defun gnus-summary-find-subject (subject &optional unread backward article)
5027   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5028          (article (or article (gnus-summary-article-number)))
5029          (articles (gnus-data-list backward))
5030          (arts (gnus-data-find-list article articles))
5031          result)
5032     (when (or (not gnus-summary-check-current)
5033               (not unread)
5034               (not (gnus-data-unread-p (car arts))))
5035       (setq arts (cdr arts)))
5036     (while arts
5037       (and (or (not unread)
5038                (gnus-data-unread-p (car arts)))
5039            (vectorp (gnus-data-header (car arts)))
5040            (gnus-subject-equal
5041             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5042            (setq result (car arts)
5043                  arts nil))
5044       (setq arts (cdr arts)))
5045     (and result
5046          (goto-char (gnus-data-pos result))
5047          (gnus-data-number result))))
5048
5049 (defun gnus-summary-search-forward (&optional unread subject backward)
5050   "Search forward for an article.
5051 If UNREAD, look for unread articles.  If SUBJECT, look for
5052 articles with that subject.  If BACKWARD, search backward instead."
5053   (cond (subject (gnus-summary-find-subject subject unread backward))
5054         (backward (gnus-summary-find-prev unread))
5055         (t (gnus-summary-find-next unread))))
5056
5057 (defun gnus-recenter (&optional n)
5058   "Center point in window and redisplay frame.
5059 Also do horizontal recentering."
5060   (interactive "P")
5061   (when (and gnus-auto-center-summary
5062              (not (eq gnus-auto-center-summary 'vertical)))
5063     (gnus-horizontal-recenter))
5064   (recenter n))
5065
5066 (defun gnus-summary-recenter ()
5067   "Center point in the summary window.
5068 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5069 displayed, no centering will be performed."
5070   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5071   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5072   (let* ((top (cond ((< (window-height) 4) 0)
5073                     ((< (window-height) 7) 1)
5074                     (t (if (numberp gnus-auto-center-summary)
5075                            gnus-auto-center-summary
5076                          2))))
5077          (height (1- (window-height)))
5078          (bottom (save-excursion (goto-char (point-max))
5079                                  (forward-line (- height))
5080                                  (point)))
5081          (window (get-buffer-window (current-buffer))))
5082     ;; The user has to want it.
5083     (when gnus-auto-center-summary
5084       (when (get-buffer-window gnus-article-buffer)
5085         ;; Only do recentering when the article buffer is displayed,
5086         ;; Set the window start to either `bottom', which is the biggest
5087         ;; possible valid number, or the second line from the top,
5088         ;; whichever is the least.
5089         (set-window-start
5090          window (min bottom (save-excursion
5091                               (forward-line (- top)) (point)))))
5092       ;; Do horizontal recentering while we're at it.
5093       (when (and (get-buffer-window (current-buffer) t)
5094                  (not (eq gnus-auto-center-summary 'vertical)))
5095         (let ((selected (selected-window)))
5096           (select-window (get-buffer-window (current-buffer) t))
5097           (gnus-summary-position-point)
5098           (gnus-horizontal-recenter)
5099           (select-window selected))))))
5100
5101 (defun gnus-summary-jump-to-group (newsgroup)
5102   "Move point to NEWSGROUP in group mode buffer."
5103   ;; Keep update point of group mode buffer if visible.
5104   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5105       (save-window-excursion
5106         ;; Take care of tree window mode.
5107         (when (get-buffer-window gnus-group-buffer)
5108           (pop-to-buffer gnus-group-buffer))
5109         (gnus-group-jump-to-group newsgroup))
5110     (save-excursion
5111       ;; Take care of tree window mode.
5112       (if (get-buffer-window gnus-group-buffer)
5113           (pop-to-buffer gnus-group-buffer)
5114         (set-buffer gnus-group-buffer))
5115       (gnus-group-jump-to-group newsgroup))))
5116
5117 ;; This function returns a list of article numbers based on the
5118 ;; difference between the ranges of read articles in this group and
5119 ;; the range of active articles.
5120 (defun gnus-list-of-unread-articles (group)
5121   (let* ((read (gnus-info-read (gnus-get-info group)))
5122          (active (or (gnus-active group) (gnus-activate-group group)))
5123          (last (cdr active))
5124          first nlast unread)
5125     ;; If none are read, then all are unread.
5126     (if (not read)
5127         (setq first (car active))
5128       ;; If the range of read articles is a single range, then the
5129       ;; first unread article is the article after the last read
5130       ;; article.  Sounds logical, doesn't it?
5131       (if (not (listp (cdr read)))
5132           (setq first (max (car active) (1+ (cdr read))))
5133         ;; `read' is a list of ranges.
5134         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5135                                   (caar read)))
5136                   1)
5137           (setq first (car active)))
5138         (while read
5139           (when first
5140             (while (< first nlast)
5141               (push first unread)
5142               (setq first (1+ first))))
5143           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5144           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5145           (setq read (cdr read)))))
5146     ;; And add the last unread articles.
5147     (while (<= first last)
5148       (push first unread)
5149       (setq first (1+ first)))
5150     ;; Return the list of unread articles.
5151     (delq 0 (nreverse unread))))
5152
5153 (defun gnus-list-of-read-articles (group)
5154   "Return a list of unread, unticked and non-dormant articles."
5155   (let* ((info (gnus-get-info group))
5156          (marked (gnus-info-marks info))
5157          (active (gnus-active group)))
5158     (and info active
5159          (gnus-set-difference
5160           (gnus-sorted-complement
5161            (gnus-uncompress-range active)
5162            (gnus-list-of-unread-articles group))
5163           (append
5164            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5165            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5166
5167 ;; Various summary commands
5168
5169 (defun gnus-summary-select-article-buffer ()
5170   "Reconfigure windows to show article buffer."
5171   (interactive)
5172   (if (not (gnus-buffer-live-p gnus-article-buffer))
5173       (error "There is no article buffer for this summary buffer")
5174     (gnus-configure-windows 'article)
5175     (select-window (get-buffer-window gnus-article-buffer))))
5176
5177 (defun gnus-summary-universal-argument (arg)
5178   "Perform any operation on all articles that are process/prefixed."
5179   (interactive "P")
5180   (let ((articles (gnus-summary-work-articles arg))
5181         func article)
5182     (if (eq
5183          (setq
5184           func
5185           (key-binding
5186            (read-key-sequence
5187             (substitute-command-keys
5188              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5189              ))))
5190          'undefined)
5191         (gnus-error 1 "Undefined key")
5192       (save-excursion
5193         (while articles
5194           (gnus-summary-goto-subject (setq article (pop articles)))
5195           (let (gnus-newsgroup-processable)
5196             (command-execute func))
5197           (gnus-summary-remove-process-mark article)))))
5198   (gnus-summary-position-point))
5199
5200 (defun gnus-summary-toggle-truncation (&optional arg)
5201   "Toggle truncation of summary lines.
5202 With arg, turn line truncation on iff arg is positive."
5203   (interactive "P")
5204   (setq truncate-lines
5205         (if (null arg) (not truncate-lines)
5206           (> (prefix-numeric-value arg) 0)))
5207   (redraw-display))
5208
5209 (defun gnus-summary-reselect-current-group (&optional all rescan)
5210   "Rescan the current newsgroup, exit and then reselect it.
5211 The prefix argument ALL means to select all articles."
5212   (interactive "P")
5213   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5214     (error "Ephemeral groups can't be reselected"))
5215   (let ((current-subject (gnus-summary-article-number))
5216         (group gnus-newsgroup-name))
5217     (save-excursion
5218       (set-buffer gnus-group-buffer)
5219       ;; We have to adjust the point of group mode buffer because
5220       ;; point was moved to the next unread newsgroup by exiting.
5221       (gnus-summary-jump-to-group group)
5222       (when rescan
5223         (save-excursion
5224           (gnus-group-get-new-news-this-group 1))))
5225     (setq gnus-newsgroup-begin nil)
5226     (gnus-summary-exit)
5227     (gnus-group-read-group all t group)
5228     (gnus-summary-goto-subject current-subject nil t)))
5229
5230 (defun gnus-summary-rescan-group (&optional all)
5231   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5232   (interactive "P")
5233   (gnus-summary-reselect-current-group all t))
5234
5235 (defun gnus-summary-update-info (&optional non-destructive)
5236   (save-excursion
5237     (let ((group gnus-newsgroup-name))
5238       (when group
5239         (when gnus-newsgroup-kill-headers
5240           (setq gnus-newsgroup-killed
5241                 (gnus-compress-sequence
5242                  (nconc
5243                   (gnus-set-sorted-intersection
5244                    (gnus-uncompress-range gnus-newsgroup-killed)
5245                    (setq gnus-newsgroup-unselected
5246                          (sort gnus-newsgroup-unselected '<)))
5247                   (setq gnus-newsgroup-unreads
5248                         (sort gnus-newsgroup-unreads '<)))
5249                  t)))
5250         (unless (listp (cdr gnus-newsgroup-killed))
5251           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5252         (let ((headers gnus-newsgroup-headers))
5253           ;; Set the new ranges of read articles.
5254           (save-excursion
5255             (set-buffer gnus-group-buffer)
5256             (gnus-undo-force-boundary))
5257           (gnus-update-read-articles
5258            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5259           ;; Set the current article marks.
5260           (let ((gnus-newsgroup-scored
5261                  (if (and (not gnus-save-score)
5262                           (not non-destructive))
5263                      nil
5264                    gnus-newsgroup-scored)))
5265             (save-excursion
5266               (gnus-update-marks)))
5267           ;; Do the cross-ref thing.
5268           (when gnus-use-cross-reference
5269             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5270           ;; Do not switch windows but change the buffer to work.
5271           (set-buffer gnus-group-buffer)
5272           (unless (gnus-ephemeral-group-p group)
5273             (gnus-group-update-group group)))))))
5274
5275 (defun gnus-summary-save-newsrc (&optional force)
5276   "Save the current number of read/marked articles in the dribble buffer.
5277 The dribble buffer will then be saved.
5278 If FORCE (the prefix), also save the .newsrc file(s)."
5279   (interactive "P")
5280   (gnus-summary-update-info t)
5281   (if force
5282       (gnus-save-newsrc-file)
5283     (gnus-dribble-save)))
5284
5285 (defun gnus-summary-exit (&optional temporary)
5286   "Exit reading current newsgroup, and then return to group selection mode.
5287 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5288   (interactive)
5289   (gnus-set-global-variables)
5290   (gnus-kill-save-kill-buffer)
5291   (gnus-async-halt-prefetch)
5292   (let* ((group gnus-newsgroup-name)
5293          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5294          (mode major-mode)
5295          (group-point nil)
5296          (buf (current-buffer)))
5297     (unless quit-config
5298       ;; Do adaptive scoring, and possibly save score files.
5299       (when gnus-newsgroup-adaptive
5300         (gnus-score-adaptive))
5301       (when gnus-use-scoring
5302         (gnus-score-save)))
5303     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5304     ;; If we have several article buffers, we kill them at exit.
5305     (unless gnus-single-article-buffer
5306       (gnus-kill-buffer gnus-original-article-buffer)
5307       (setq gnus-article-current nil))
5308     (when gnus-use-cache
5309       (gnus-cache-possibly-remove-articles)
5310       (gnus-cache-save-buffers))
5311     (gnus-async-prefetch-remove-group group)
5312     (when gnus-suppress-duplicates
5313       (gnus-dup-enter-articles))
5314     (when gnus-use-trees
5315       (gnus-tree-close group))
5316     ;; Remove entries for this group.
5317     (nnmail-purge-split-history (gnus-group-real-name group))
5318     ;; Make all changes in this group permanent.
5319     (unless quit-config
5320       (gnus-run-hooks 'gnus-exit-group-hook)
5321       (gnus-summary-update-info))
5322     (gnus-close-group group)
5323     ;; Make sure where we were, and go to next newsgroup.
5324     (set-buffer gnus-group-buffer)
5325     (unless quit-config
5326       (gnus-group-jump-to-group group))
5327     (gnus-run-hooks 'gnus-summary-exit-hook)
5328     (unless (or quit-config
5329                 ;; If this group has disappeared from the summary
5330                 ;; buffer, don't skip forwards.
5331                 (not (string= group (gnus-group-group-name))))
5332       (gnus-group-next-unread-group 1))
5333     (setq group-point (point))
5334     (if temporary
5335         nil                             ;Nothing to do.
5336       ;; If we have several article buffers, we kill them at exit.
5337       (unless gnus-single-article-buffer
5338         (gnus-kill-buffer gnus-article-buffer)
5339         (gnus-kill-buffer gnus-original-article-buffer)
5340         (setq gnus-article-current nil))
5341       (set-buffer buf)
5342       (if (not gnus-kill-summary-on-exit)
5343           (gnus-deaden-summary)
5344         ;; We set all buffer-local variables to nil.  It is unclear why
5345         ;; this is needed, but if we don't, buffer-local variables are
5346         ;; not garbage-collected, it seems.  This would the lead to en
5347         ;; ever-growing Emacs.
5348         (gnus-summary-clear-local-variables)
5349         (when (get-buffer gnus-article-buffer)
5350           (bury-buffer gnus-article-buffer))
5351         ;; We clear the global counterparts of the buffer-local
5352         ;; variables as well, just to be on the safe side.
5353         (set-buffer gnus-group-buffer)
5354         (gnus-summary-clear-local-variables)
5355         ;; Return to group mode buffer.
5356         (when (eq mode 'gnus-summary-mode)
5357           (gnus-kill-buffer buf)))
5358       (setq gnus-current-select-method gnus-select-method)
5359       (pop-to-buffer gnus-group-buffer)
5360       (if (not quit-config)
5361           (progn
5362             (goto-char group-point)
5363             (gnus-configure-windows 'group 'force))
5364         (gnus-handle-ephemeral-exit quit-config))
5365       ;; Clear the current group name.
5366       (unless quit-config
5367         (setq gnus-newsgroup-name nil)))))
5368
5369 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5370 (defun gnus-summary-exit-no-update (&optional no-questions)
5371   "Quit reading current newsgroup without updating read article info."
5372   (interactive)
5373   (let* ((group gnus-newsgroup-name)
5374          (quit-config (gnus-group-quit-config group)))
5375     (when (or no-questions
5376               gnus-expert-user
5377               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5378       (gnus-async-halt-prefetch)
5379       (mapcar 'funcall
5380               (delq 'gnus-summary-expire-articles
5381                     (copy-list gnus-summary-prepare-exit-hook)))
5382       ;; If we have several article buffers, we kill them at exit.
5383       (unless gnus-single-article-buffer
5384         (gnus-kill-buffer gnus-article-buffer)
5385         (gnus-kill-buffer gnus-original-article-buffer)
5386         (setq gnus-article-current nil))
5387       (if (not gnus-kill-summary-on-exit)
5388           (gnus-deaden-summary)
5389         (gnus-close-group group)
5390         (gnus-summary-clear-local-variables)
5391         (set-buffer gnus-group-buffer)
5392         (gnus-summary-clear-local-variables)
5393         (when (get-buffer gnus-summary-buffer)
5394           (kill-buffer gnus-summary-buffer)))
5395       (unless gnus-single-article-buffer
5396         (setq gnus-article-current nil))
5397       (when gnus-use-trees
5398         (gnus-tree-close group))
5399       (gnus-async-prefetch-remove-group group)
5400       (when (get-buffer gnus-article-buffer)
5401         (bury-buffer gnus-article-buffer))
5402       ;; Return to the group buffer.
5403       (gnus-configure-windows 'group 'force)
5404       ;; Clear the current group name.
5405       (setq gnus-newsgroup-name nil)
5406       (when (equal (gnus-group-group-name) group)
5407         (gnus-group-next-unread-group 1))
5408       (when quit-config
5409         (gnus-handle-ephemeral-exit quit-config)))))
5410
5411 (defun gnus-handle-ephemeral-exit (quit-config)
5412   "Handle movement when leaving an ephemeral group.
5413 The state which existed when entering the ephemeral is reset."
5414   (if (not (buffer-name (car quit-config)))
5415       (gnus-configure-windows 'group 'force)
5416     (set-buffer (car quit-config))
5417     (cond ((eq major-mode 'gnus-summary-mode)
5418            (gnus-set-global-variables))
5419           ((eq major-mode 'gnus-article-mode)
5420            (save-excursion
5421              ;; The `gnus-summary-buffer' variable may point
5422              ;; to the old summary buffer when using a single
5423              ;; article buffer.
5424              (unless (gnus-buffer-live-p gnus-summary-buffer)
5425                (set-buffer gnus-group-buffer))
5426              (set-buffer gnus-summary-buffer)
5427              (gnus-set-global-variables))))
5428     (if (or (eq (cdr quit-config) 'article)
5429             (eq (cdr quit-config) 'pick))
5430         (progn
5431           ;; The current article may be from the ephemeral group
5432           ;; thus it is best that we reload this article
5433           (gnus-summary-show-article)
5434           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5435               (gnus-configure-windows 'pick 'force)
5436             (gnus-configure-windows (cdr quit-config) 'force)))
5437       (gnus-configure-windows (cdr quit-config) 'force))
5438     (when (eq major-mode 'gnus-summary-mode)
5439       (gnus-summary-next-subject 1 nil t)
5440       (gnus-summary-recenter)
5441       (gnus-summary-position-point))))
5442
5443 (defun gnus-summary-preview-mime-message ()
5444   "MIME decode and play this message."
5445   (interactive)
5446   (let ((gnus-break-pages nil)
5447         (gnus-show-mime t))
5448     (gnus-summary-select-article gnus-show-all-headers t))
5449   (select-window (get-buffer-window gnus-article-buffer)))
5450
5451 ;;; Dead summaries.
5452
5453 (defvar gnus-dead-summary-mode-map nil)
5454
5455 (unless gnus-dead-summary-mode-map
5456   (setq gnus-dead-summary-mode-map (make-keymap))
5457   (suppress-keymap gnus-dead-summary-mode-map)
5458   (substitute-key-definition
5459    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5460   (let ((keys '("\C-d" "\r" "\177" [delete])))
5461     (while keys
5462       (define-key gnus-dead-summary-mode-map
5463         (pop keys) 'gnus-summary-wake-up-the-dead))))
5464
5465 (defvar gnus-dead-summary-mode nil
5466   "Minor mode for Gnus summary buffers.")
5467
5468 (defun gnus-dead-summary-mode (&optional arg)
5469   "Minor mode for Gnus summary buffers."
5470   (interactive "P")
5471   (when (eq major-mode 'gnus-summary-mode)
5472     (make-local-variable 'gnus-dead-summary-mode)
5473     (setq gnus-dead-summary-mode
5474           (if (null arg) (not gnus-dead-summary-mode)
5475             (> (prefix-numeric-value arg) 0)))
5476     (when gnus-dead-summary-mode
5477       (gnus-add-minor-mode
5478        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5479
5480 (defun gnus-deaden-summary ()
5481   "Make the current summary buffer into a dead summary buffer."
5482   ;; Kill any previous dead summary buffer.
5483   (when (and gnus-dead-summary
5484              (buffer-name gnus-dead-summary))
5485     (save-excursion
5486       (set-buffer gnus-dead-summary)
5487       (when gnus-dead-summary-mode
5488         (kill-buffer (current-buffer)))))
5489   ;; Make this the current dead summary.
5490   (setq gnus-dead-summary (current-buffer))
5491   (gnus-dead-summary-mode 1)
5492   (let ((name (buffer-name)))
5493     (when (string-match "Summary" name)
5494       (rename-buffer
5495        (concat (substring name 0 (match-beginning 0)) "Dead "
5496                (substring name (match-beginning 0)))
5497        t))))
5498
5499 (defun gnus-kill-or-deaden-summary (buffer)
5500   "Kill or deaden the summary BUFFER."
5501   (save-excursion
5502     (when (and (buffer-name buffer)
5503                (not gnus-single-article-buffer))
5504       (save-excursion
5505         (set-buffer buffer)
5506         (gnus-kill-buffer gnus-article-buffer)
5507         (gnus-kill-buffer gnus-original-article-buffer)))
5508     (cond (gnus-kill-summary-on-exit
5509            (when (and gnus-use-trees
5510                       (gnus-buffer-exists-p buffer))
5511              (save-excursion
5512                (set-buffer buffer)
5513                (gnus-tree-close gnus-newsgroup-name)))
5514            (gnus-kill-buffer buffer))
5515           ((gnus-buffer-exists-p buffer)
5516            (save-excursion
5517              (set-buffer buffer)
5518              (gnus-deaden-summary))))))
5519
5520 (defun gnus-summary-wake-up-the-dead (&rest args)
5521   "Wake up the dead summary buffer."
5522   (interactive)
5523   (gnus-dead-summary-mode -1)
5524   (let ((name (buffer-name)))
5525     (when (string-match "Dead " name)
5526       (rename-buffer
5527        (concat (substring name 0 (match-beginning 0))
5528                (substring name (match-end 0)))
5529        t)))
5530   (gnus-message 3 "This dead summary is now alive again"))
5531
5532 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5533 (defun gnus-summary-fetch-faq (&optional faq-dir)
5534   "Fetch the FAQ for the current group.
5535 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5536 in."
5537   (interactive
5538    (list
5539     (when current-prefix-arg
5540       (completing-read
5541        "Faq dir: " (and (listp gnus-group-faq-directory)
5542                         (mapcar (lambda (file) (list file))
5543                                 gnus-group-faq-directory))))))
5544   (let (gnus-faq-buffer)
5545     (when (setq gnus-faq-buffer
5546                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5547       (gnus-configure-windows 'summary-faq))))
5548
5549 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5550 (defun gnus-summary-describe-group (&optional force)
5551   "Describe the current newsgroup."
5552   (interactive "P")
5553   (gnus-group-describe-group force gnus-newsgroup-name))
5554
5555 (defun gnus-summary-describe-briefly ()
5556   "Describe summary mode commands briefly."
5557   (interactive)
5558   (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")))
5559
5560 ;; Walking around group mode buffer from summary mode.
5561
5562 (defun gnus-summary-next-group (&optional no-article target-group backward)
5563   "Exit current newsgroup and then select next unread newsgroup.
5564 If prefix argument NO-ARTICLE is non-nil, no article is selected
5565 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5566 previous group instead."
5567   (interactive "P")
5568   ;; Stop pre-fetching.
5569   (gnus-async-halt-prefetch)
5570   (let ((current-group gnus-newsgroup-name)
5571         (current-buffer (current-buffer))
5572         entered)
5573     ;; First we semi-exit this group to update Xrefs and all variables.
5574     ;; We can't do a real exit, because the window conf must remain
5575     ;; the same in case the user is prompted for info, and we don't
5576     ;; want the window conf to change before that...
5577     (gnus-summary-exit t)
5578     (while (not entered)
5579       ;; Then we find what group we are supposed to enter.
5580       (set-buffer gnus-group-buffer)
5581       (gnus-group-jump-to-group current-group)
5582       (setq target-group
5583             (or target-group
5584                 (if (eq gnus-keep-same-level 'best)
5585                     (gnus-summary-best-group gnus-newsgroup-name)
5586                   (gnus-summary-search-group backward gnus-keep-same-level))))
5587       (if (not target-group)
5588           ;; There are no further groups, so we return to the group
5589           ;; buffer.
5590           (progn
5591             (gnus-message 5 "Returning to the group buffer")
5592             (setq entered t)
5593             (when (gnus-buffer-live-p current-buffer)
5594               (set-buffer current-buffer)
5595               (gnus-summary-exit))
5596             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5597         ;; We try to enter the target group.
5598         (gnus-group-jump-to-group target-group)
5599         (let ((unreads (gnus-group-group-unread)))
5600           (if (and (or (eq t unreads)
5601                        (and unreads (not (zerop unreads))))
5602                    (gnus-summary-read-group
5603                     target-group nil no-article
5604                     (and (buffer-name current-buffer) current-buffer)
5605                     nil backward))
5606               (setq entered t)
5607             (setq current-group target-group
5608                   target-group nil)))))))
5609
5610 (defun gnus-summary-prev-group (&optional no-article)
5611   "Exit current newsgroup and then select previous unread newsgroup.
5612 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5613   (interactive "P")
5614   (gnus-summary-next-group no-article nil t))
5615
5616 ;; Walking around summary lines.
5617
5618 (defun gnus-summary-first-subject (&optional unread undownloaded)
5619   "Go to the first unread subject.
5620 If UNREAD is non-nil, go to the first unread article.
5621 Returns the article selected or nil if there are no unread articles."
5622   (interactive "P")
5623   (prog1
5624       (cond
5625        ;; Empty summary.
5626        ((null gnus-newsgroup-data)
5627         (gnus-message 3 "No articles in the group")
5628         nil)
5629        ;; Pick the first article.
5630        ((not unread)
5631         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5632         (gnus-data-number (car gnus-newsgroup-data)))
5633        ;; No unread articles.
5634        ((null gnus-newsgroup-unreads)
5635         (gnus-message 3 "No more unread articles")
5636         nil)
5637        ;; Find the first unread article.
5638        (t
5639         (let ((data gnus-newsgroup-data))
5640           (while (and data
5641                       (and (not (and undownloaded
5642                                      (eq gnus-undownloaded-mark
5643                                          (gnus-data-mark (car data)))))
5644                            (not (gnus-data-unread-p (car data)))))
5645             (setq data (cdr data)))
5646           (when data
5647             (goto-char (gnus-data-pos (car data)))
5648             (gnus-data-number (car data))))))
5649     (gnus-summary-position-point)))
5650
5651 (defun gnus-summary-next-subject (n &optional unread dont-display)
5652   "Go to next N'th summary line.
5653 If N is negative, go to the previous N'th subject line.
5654 If UNREAD is non-nil, only unread articles are selected.
5655 The difference between N and the actual number of steps taken is
5656 returned."
5657   (interactive "p")
5658   (let ((backward (< n 0))
5659         (n (abs n)))
5660     (while (and (> n 0)
5661                 (if backward
5662                     (gnus-summary-find-prev unread)
5663                   (gnus-summary-find-next unread)))
5664       (gnus-summary-show-thread)
5665       (setq n (1- n)))
5666     (when (/= 0 n)
5667       (gnus-message 7 "No more%s articles"
5668                     (if unread " unread" "")))
5669     (unless dont-display
5670       (gnus-summary-recenter)
5671       (gnus-summary-position-point))
5672     n))
5673
5674 (defun gnus-summary-next-unread-subject (n)
5675   "Go to next N'th unread summary line."
5676   (interactive "p")
5677   (gnus-summary-next-subject n t))
5678
5679 (defun gnus-summary-prev-subject (n &optional unread)
5680   "Go to previous N'th summary line.
5681 If optional argument UNREAD is non-nil, only unread article is selected."
5682   (interactive "p")
5683   (gnus-summary-next-subject (- n) unread))
5684
5685 (defun gnus-summary-prev-unread-subject (n)
5686   "Go to previous N'th unread summary line."
5687   (interactive "p")
5688   (gnus-summary-next-subject (- n) t))
5689
5690 (defun gnus-summary-goto-subject (article &optional force silent)
5691   "Go the subject line of ARTICLE.
5692 If FORCE, also allow jumping to articles not currently shown."
5693   (interactive "nArticle number: ")
5694   (let ((b (point))
5695         (data (gnus-data-find article)))
5696     ;; We read in the article if we have to.
5697     (and (not data)
5698          force
5699          (gnus-summary-insert-subject
5700           article
5701           (if (or (numberp force) (vectorp force)) force)
5702           t)
5703          (setq data (gnus-data-find article)))
5704     (goto-char b)
5705     (if (not data)
5706         (progn
5707           (unless silent
5708             (gnus-message 3 "Can't find article %d" article))
5709           nil)
5710       (goto-char (gnus-data-pos data))
5711       (gnus-summary-position-point)
5712       article)))
5713
5714 ;; Walking around summary lines with displaying articles.
5715
5716 (defun gnus-summary-expand-window (&optional arg)
5717   "Make the summary buffer take up the entire Emacs frame.
5718 Given a prefix, will force an `article' buffer configuration."
5719   (interactive "P")
5720   (if arg
5721       (gnus-configure-windows 'article 'force)
5722     (gnus-configure-windows 'summary 'force)))
5723
5724 (defun gnus-summary-display-article (article &optional all-header)
5725   "Display ARTICLE in article buffer."
5726   (gnus-set-global-variables)
5727   (if (null article)
5728       nil
5729     (prog1
5730         (if gnus-summary-display-article-function
5731             (funcall gnus-summary-display-article-function article all-header)
5732           (gnus-article-prepare article all-header))
5733       (gnus-run-hooks 'gnus-select-article-hook)
5734       (when (and gnus-current-article
5735                  (not (zerop gnus-current-article)))
5736         (gnus-summary-goto-subject gnus-current-article))
5737       (gnus-summary-recenter)
5738       (when (and gnus-use-trees gnus-show-threads)
5739         (gnus-possibly-generate-tree article)
5740         (gnus-highlight-selected-tree article))
5741       ;; Successfully display article.
5742       (gnus-article-set-window-start
5743        (cdr (assq article gnus-newsgroup-bookmarks))))))
5744
5745 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5746   "Select the current article.
5747 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5748 non-nil, the article will be re-fetched even if it already present in
5749 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5750 be displayed."
5751   ;; Make sure we are in the summary buffer to work around bbdb bug.
5752   (unless (eq major-mode 'gnus-summary-mode)
5753     (set-buffer gnus-summary-buffer))
5754   (let ((article (or article (gnus-summary-article-number)))
5755         (all-headers (not (not all-headers))) ;Must be T or NIL.
5756         gnus-summary-display-article-function
5757         did)
5758     (and (not pseudo)
5759          (gnus-summary-article-pseudo-p article)
5760          (error "This is a pseudo-article"))
5761     (prog1
5762         (save-excursion
5763           (set-buffer gnus-summary-buffer)
5764           (if (or (and gnus-single-article-buffer
5765                        (or (null gnus-current-article)
5766                            (null gnus-article-current)
5767                            (null (get-buffer gnus-article-buffer))
5768                            (not (eq article (cdr gnus-article-current)))
5769                            (not (equal (car gnus-article-current)
5770                                        gnus-newsgroup-name))))
5771                   (and (not gnus-single-article-buffer)
5772                        (or (null gnus-current-article)
5773                            (not (eq gnus-current-article article))))
5774                   force)
5775               ;; The requested article is different from the current article.
5776               (prog1
5777                   (gnus-summary-display-article article all-headers)
5778                 (setq did article)
5779                 (when (or all-headers gnus-show-all-headers)
5780                   (gnus-article-show-all-headers)))
5781             (when (or all-headers gnus-show-all-headers)
5782               (gnus-article-show-all-headers))
5783             'old))
5784       (when did
5785         (gnus-article-set-window-start
5786          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5787
5788 (defun gnus-summary-set-current-mark (&optional current-mark)
5789   "Obsolete function."
5790   nil)
5791
5792 (defun gnus-summary-next-article (&optional unread subject backward push)
5793   "Select the next article.
5794 If UNREAD, only unread articles are selected.
5795 If SUBJECT, only articles with SUBJECT are selected.
5796 If BACKWARD, the previous article is selected instead of the next."
5797   (interactive "P")
5798   (cond
5799    ;; Is there such an article?
5800    ((and (gnus-summary-search-forward unread subject backward)
5801          (or (gnus-summary-display-article (gnus-summary-article-number))
5802              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5803     (gnus-summary-position-point))
5804    ;; If not, we try the first unread, if that is wanted.
5805    ((and subject
5806          gnus-auto-select-same
5807          (gnus-summary-first-unread-article))
5808     (gnus-summary-position-point)
5809     (gnus-message 6 "Wrapped"))
5810    ;; Try to get next/previous article not displayed in this group.
5811    ((and gnus-auto-extend-newsgroup
5812          (not unread) (not subject))
5813     (gnus-summary-goto-article
5814      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5815      nil (count-lines (point-min) (point))))
5816    ;; Go to next/previous group.
5817    (t
5818     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5819       (gnus-summary-jump-to-group gnus-newsgroup-name))
5820     (let ((cmd last-command-char)
5821           (point
5822            (save-excursion
5823              (set-buffer gnus-group-buffer)
5824              (point)))
5825           (group
5826            (if (eq gnus-keep-same-level 'best)
5827                (gnus-summary-best-group gnus-newsgroup-name)
5828              (gnus-summary-search-group backward gnus-keep-same-level))))
5829       ;; For some reason, the group window gets selected.  We change
5830       ;; it back.
5831       (select-window (get-buffer-window (current-buffer)))
5832       ;; Select next unread newsgroup automagically.
5833       (cond
5834        ((or (not gnus-auto-select-next)
5835             (not cmd))
5836         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5837        ((or (eq gnus-auto-select-next 'quietly)
5838             (and (eq gnus-auto-select-next 'slightly-quietly)
5839                  push)
5840             (and (eq gnus-auto-select-next 'almost-quietly)
5841                  (gnus-summary-last-article-p)))
5842         ;; Select quietly.
5843         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5844             (gnus-summary-exit)
5845           (gnus-message 7 "No more%s articles (%s)..."
5846                         (if unread " unread" "")
5847                         (if group (concat "selecting " group)
5848                           "exiting"))
5849           (gnus-summary-next-group nil group backward)))
5850        (t
5851         (when (gnus-key-press-event-p last-input-event)
5852           (gnus-summary-walk-group-buffer
5853            gnus-newsgroup-name cmd unread backward point))))))))
5854
5855 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5856   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5857                       (?\C-p (gnus-group-prev-unread-group 1))))
5858         (cursor-in-echo-area t)
5859         keve key group ended)
5860     (save-excursion
5861       (set-buffer gnus-group-buffer)
5862       (goto-char start)
5863       (setq group
5864             (if (eq gnus-keep-same-level 'best)
5865                 (gnus-summary-best-group gnus-newsgroup-name)
5866               (gnus-summary-search-group backward gnus-keep-same-level))))
5867     (while (not ended)
5868       (gnus-message
5869        5 "No more%s articles%s" (if unread " unread" "")
5870        (if (and group
5871                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5872            (format " (Type %s for %s [%s])"
5873                    (single-key-description cmd) group
5874                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5875          (format " (Type %s to exit %s)"
5876                  (single-key-description cmd)
5877                  gnus-newsgroup-name)))
5878       ;; Confirm auto selection.
5879       (setq key (car (setq keve (gnus-read-event-char))))
5880       (setq ended t)
5881       (cond
5882        ((assq key keystrokes)
5883         (let ((obuf (current-buffer)))
5884           (switch-to-buffer gnus-group-buffer)
5885           (when group
5886             (gnus-group-jump-to-group group))
5887           (eval (cadr (assq key keystrokes)))
5888           (setq group (gnus-group-group-name))
5889           (switch-to-buffer obuf))
5890         (setq ended nil))
5891        ((equal key cmd)
5892         (if (or (not group)
5893                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5894             (gnus-summary-exit)
5895           (gnus-summary-next-group nil group backward)))
5896        (t
5897         (push (cdr keve) unread-command-events))))))
5898
5899 (defun gnus-summary-next-unread-article ()
5900   "Select unread article after current one."
5901   (interactive)
5902   (gnus-summary-next-article
5903    (or (not (eq gnus-summary-goto-unread 'never))
5904        (gnus-summary-last-article-p (gnus-summary-article-number)))
5905    (and gnus-auto-select-same
5906         (gnus-summary-article-subject))))
5907
5908 (defun gnus-summary-prev-article (&optional unread subject)
5909   "Select the article after the current one.
5910 If UNREAD is non-nil, only unread articles are selected."
5911   (interactive "P")
5912   (gnus-summary-next-article unread subject t))
5913
5914 (defun gnus-summary-prev-unread-article ()
5915   "Select unread article before current one."
5916   (interactive)
5917   (gnus-summary-prev-article
5918    (or (not (eq gnus-summary-goto-unread 'never))
5919        (gnus-summary-first-article-p (gnus-summary-article-number)))
5920    (and gnus-auto-select-same
5921         (gnus-summary-article-subject))))
5922
5923 (defun gnus-summary-next-page (&optional lines circular)
5924   "Show next page of the selected article.
5925 If at the end of the current article, select the next article.
5926 LINES says how many lines should be scrolled up.
5927
5928 If CIRCULAR is non-nil, go to the start of the article instead of
5929 selecting the next article when reaching the end of the current
5930 article."
5931   (interactive "P")
5932   (setq gnus-summary-buffer (current-buffer))
5933   (gnus-set-global-variables)
5934   (let ((article (gnus-summary-article-number))
5935         (article-window (get-buffer-window gnus-article-buffer t))
5936         endp)
5937     ;; If the buffer is empty, we have no article.
5938     (unless article
5939       (error "No article to select"))
5940     (gnus-configure-windows 'article)
5941     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5942         (if (and (eq gnus-summary-goto-unread 'never)
5943                  (not (gnus-summary-last-article-p article)))
5944             (gnus-summary-next-article)
5945           (gnus-summary-next-unread-article))
5946       (if (or (null gnus-current-article)
5947               (null gnus-article-current)
5948               (/= article (cdr gnus-article-current))
5949               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5950           ;; Selected subject is different from current article's.
5951           (gnus-summary-display-article article)
5952         (when article-window
5953           (gnus-eval-in-buffer-window gnus-article-buffer
5954             (setq endp (gnus-article-next-page lines)))
5955           (when endp
5956             (cond (circular
5957                    (gnus-summary-beginning-of-article))
5958                   (lines
5959                    (gnus-message 3 "End of message"))
5960                   ((null lines)
5961                    (if (and (eq gnus-summary-goto-unread 'never)
5962                             (not (gnus-summary-last-article-p article)))
5963                        (gnus-summary-next-article)
5964                      (gnus-summary-next-unread-article))))))))
5965     (gnus-summary-recenter)
5966     (gnus-summary-position-point)))
5967
5968 (defun gnus-summary-prev-page (&optional lines move)
5969   "Show previous page of selected article.
5970 Argument LINES specifies lines to be scrolled down.
5971 If MOVE, move to the previous unread article if point is at
5972 the beginning of the buffer."
5973   (interactive "P")
5974   (let ((article (gnus-summary-article-number))
5975         (article-window (get-buffer-window gnus-article-buffer t))
5976         endp)
5977     (gnus-configure-windows 'article)
5978     (if (or (null gnus-current-article)
5979             (null gnus-article-current)
5980             (/= article (cdr gnus-article-current))
5981             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5982         ;; Selected subject is different from current article's.
5983         (gnus-summary-display-article article)
5984       (gnus-summary-recenter)
5985       (when article-window
5986         (gnus-eval-in-buffer-window gnus-article-buffer
5987           (setq endp (gnus-article-prev-page lines)))
5988         (when (and move endp)
5989           (cond (lines
5990                  (gnus-message 3 "Beginning of message"))
5991                 ((null lines)
5992                  (if (and (eq gnus-summary-goto-unread 'never)
5993                           (not (gnus-summary-first-article-p article)))
5994                      (gnus-summary-prev-article)
5995                    (gnus-summary-prev-unread-article))))))))
5996   (gnus-summary-position-point))
5997
5998 (defun gnus-summary-prev-page-or-article (&optional lines)
5999   "Show previous page of selected article.
6000 Argument LINES specifies lines to be scrolled down.
6001 If at the beginning of the article, go to the next article."
6002   (interactive "P")
6003   (gnus-summary-prev-page lines t))
6004
6005 (defun gnus-summary-scroll-up (lines)
6006   "Scroll up (or down) one line current article.
6007 Argument LINES specifies lines to be scrolled up (or down if negative)."
6008   (interactive "p")
6009   (gnus-configure-windows 'article)
6010   (gnus-summary-show-thread)
6011   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6012     (gnus-eval-in-buffer-window gnus-article-buffer
6013       (cond ((> lines 0)
6014              (when (gnus-article-next-page lines)
6015                (gnus-message 3 "End of message")))
6016             ((< lines 0)
6017              (gnus-article-prev-page (- lines))))))
6018   (gnus-summary-recenter)
6019   (gnus-summary-position-point))
6020
6021 (defun gnus-summary-scroll-down (lines)
6022   "Scroll down (or up) one line current article.
6023 Argument LINES specifies lines to be scrolled down (or up if negative)."
6024   (interactive "p")
6025   (gnus-summary-scroll-up (- lines)))
6026
6027 (defun gnus-summary-next-same-subject ()
6028   "Select next article which has the same subject as current one."
6029   (interactive)
6030   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6031
6032 (defun gnus-summary-prev-same-subject ()
6033   "Select previous article which has the same subject as current one."
6034   (interactive)
6035   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6036
6037 (defun gnus-summary-next-unread-same-subject ()
6038   "Select next unread article which has the same subject as current one."
6039   (interactive)
6040   (gnus-summary-next-article t (gnus-summary-article-subject)))
6041
6042 (defun gnus-summary-prev-unread-same-subject ()
6043   "Select previous unread article which has the same subject as current one."
6044   (interactive)
6045   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6046
6047 (defun gnus-summary-first-unread-article ()
6048   "Select the first unread article.
6049 Return nil if there are no unread articles."
6050   (interactive)
6051   (prog1
6052       (when (gnus-summary-first-subject t)
6053         (gnus-summary-show-thread)
6054         (gnus-summary-first-subject t)
6055         (gnus-summary-display-article (gnus-summary-article-number)))
6056     (gnus-summary-position-point)))
6057
6058 (defun gnus-summary-first-unread-subject ()
6059   "Place the point on the subject line of the first unread article.
6060 Return nil if there are no unread articles."
6061   (interactive)
6062   (prog1
6063       (when (gnus-summary-first-subject t)
6064         (gnus-summary-show-thread)
6065         (gnus-summary-first-subject t))
6066     (gnus-summary-position-point)))
6067
6068 (defun gnus-summary-first-article ()
6069   "Select the first article.
6070 Return nil if there are no articles."
6071   (interactive)
6072   (prog1
6073       (when (gnus-summary-first-subject)
6074         (gnus-summary-show-thread)
6075         (gnus-summary-first-subject)
6076         (gnus-summary-display-article (gnus-summary-article-number)))
6077     (gnus-summary-position-point)))
6078
6079 (defun gnus-summary-best-unread-article ()
6080   "Select the unread article with the highest score."
6081   (interactive)
6082   (let ((best -1000000)
6083         (data gnus-newsgroup-data)
6084         article score)
6085     (while data
6086       (and (gnus-data-unread-p (car data))
6087            (> (setq score
6088                     (gnus-summary-article-score (gnus-data-number (car data))))
6089               best)
6090            (setq best score
6091                  article (gnus-data-number (car data))))
6092       (setq data (cdr data)))
6093     (prog1
6094         (if article
6095             (gnus-summary-goto-article article)
6096           (error "No unread articles"))
6097       (gnus-summary-position-point))))
6098
6099 (defun gnus-summary-last-subject ()
6100   "Go to the last displayed subject line in the group."
6101   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6102     (when article
6103       (gnus-summary-goto-subject article))))
6104
6105 (defun gnus-summary-goto-article (article &optional all-headers force)
6106   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6107 If ALL-HEADERS is non-nil, no header lines are hidden.
6108 If FORCE, go to the article even if it isn't displayed.  If FORCE
6109 is a number, it is the line the article is to be displayed on."
6110   (interactive
6111    (list
6112     (completing-read
6113      "Article number or Message-ID: "
6114      (mapcar (lambda (number) (list (int-to-string number)))
6115              gnus-newsgroup-limit))
6116     current-prefix-arg
6117     t))
6118   (prog1
6119       (if (and (stringp article)
6120                (string-match "@" article))
6121           (gnus-summary-refer-article article)
6122         (when (stringp article)
6123           (setq article (string-to-number article)))
6124         (if (gnus-summary-goto-subject article force)
6125             (gnus-summary-display-article article all-headers)
6126           (gnus-message 4 "Couldn't go to article %s" article) nil))
6127     (gnus-summary-position-point)))
6128
6129 (defun gnus-summary-goto-last-article ()
6130   "Go to the previously read article."
6131   (interactive)
6132   (prog1
6133       (when gnus-last-article
6134         (gnus-summary-goto-article gnus-last-article nil t))
6135     (gnus-summary-position-point)))
6136
6137 (defun gnus-summary-pop-article (number)
6138   "Pop one article off the history and go to the previous.
6139 NUMBER articles will be popped off."
6140   (interactive "p")
6141   (let (to)
6142     (setq gnus-newsgroup-history
6143           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6144     (if to
6145         (gnus-summary-goto-article (car to) nil t)
6146       (error "Article history empty")))
6147   (gnus-summary-position-point))
6148
6149 ;; Summary commands and functions for limiting the summary buffer.
6150
6151 (defun gnus-summary-limit-to-articles (n)
6152   "Limit the summary buffer to the next N articles.
6153 If not given a prefix, use the process marked articles instead."
6154   (interactive "P")
6155   (prog1
6156       (let ((articles (gnus-summary-work-articles n)))
6157         (setq gnus-newsgroup-processable nil)
6158         (gnus-summary-limit articles))
6159     (gnus-summary-position-point)))
6160
6161 (defun gnus-summary-pop-limit (&optional total)
6162   "Restore the previous limit.
6163 If given a prefix, remove all limits."
6164   (interactive "P")
6165   (when total
6166     (setq gnus-newsgroup-limits
6167           (list (mapcar (lambda (h) (mail-header-number h))
6168                         gnus-newsgroup-headers))))
6169   (unless gnus-newsgroup-limits
6170     (error "No limit to pop"))
6171   (prog1
6172       (gnus-summary-limit nil 'pop)
6173     (gnus-summary-position-point)))
6174
6175 (defun gnus-summary-limit-to-subject (subject &optional header)
6176   "Limit the summary buffer to articles that have subjects that match a regexp."
6177   (interactive "sLimit to subject (regexp): ")
6178   (unless header
6179     (setq header "subject"))
6180   (when (not (equal "" subject))
6181     (prog1
6182         (let ((articles (gnus-summary-find-matching
6183                          (or header "subject") subject 'all)))
6184           (unless articles
6185             (error "Found no matches for \"%s\"" subject))
6186           (gnus-summary-limit articles))
6187       (gnus-summary-position-point))))
6188
6189 (defun gnus-summary-limit-to-author (from)
6190   "Limit the summary buffer to articles that have authors that match a regexp."
6191   (interactive "sLimit to author (regexp): ")
6192   (gnus-summary-limit-to-subject from "from"))
6193
6194 (defun gnus-summary-limit-to-age (age &optional younger-p)
6195   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6196 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6197 articles that are younger than AGE days."
6198   (interactive "nLimit to articles older than (in days): \nP")
6199   (prog1
6200       (let ((data gnus-newsgroup-data)
6201             (cutoff (days-to-time age))
6202             articles d date is-younger)
6203         (while (setq d (pop data))
6204           (when (and (vectorp (gnus-data-header d))
6205                      (setq date (mail-header-date (gnus-data-header d))))
6206             (setq is-younger (time-less-p
6207                               (time-since (date-to-time date))
6208                               cutoff))
6209             (when (if younger-p
6210                       is-younger
6211                     (not is-younger))
6212               (push (gnus-data-number d) articles))))
6213         (gnus-summary-limit (nreverse articles)))
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-limit-to-extra (header regexp)
6217   "Limit the summary buffer to articles that match an 'extra' header."
6218   (interactive
6219    (let ((header
6220           (intern
6221            (gnus-completing-read
6222             (symbol-name (car gnus-extra-headers))      
6223             "Limit extra header:"       
6224             (mapcar (lambda (x) 
6225                       (cons (symbol-name x) x))
6226                     gnus-extra-headers)
6227             nil                 
6228             t))))
6229      (list header
6230            (read-string (format "Limit to header %s (regexp): " header)))))
6231   (when (not (equal "" regexp))
6232     (prog1
6233         (let ((articles (gnus-summary-find-matching
6234                          (cons 'extra header) regexp 'all)))
6235           (unless articles
6236             (error "Found no matches for \"%s\"" regexp))
6237           (gnus-summary-limit articles))
6238       (gnus-summary-position-point))))
6239
6240 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6241 (make-obsolete
6242  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6243
6244 (defun gnus-summary-limit-to-unread (&optional all)
6245   "Limit the summary buffer to articles that are not marked as read.
6246 If ALL is non-nil, limit strictly to unread articles."
6247   (interactive "P")
6248   (if all
6249       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6250     (gnus-summary-limit-to-marks
6251      ;; Concat all the marks that say that an article is read and have
6252      ;; those removed.
6253      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6254            gnus-killed-mark gnus-kill-file-mark
6255            gnus-low-score-mark gnus-expirable-mark
6256            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6257            gnus-duplicate-mark gnus-souped-mark)
6258      'reverse)))
6259
6260 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6261 (make-obsolete 'gnus-summary-delete-marked-with
6262                'gnus-summary-limit-exlude-marks)
6263
6264 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6265   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6266 If REVERSE, limit the summary buffer to articles that are marked
6267 with MARKS.  MARKS can either be a string of marks or a list of marks.
6268 Returns how many articles were removed."
6269   (interactive "sMarks: ")
6270   (gnus-summary-limit-to-marks marks t))
6271
6272 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6273   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6274 If REVERSE (the prefix), limit the summary buffer to articles that are
6275 not marked with MARKS.  MARKS can either be a string of marks or a
6276 list of marks.
6277 Returns how many articles were removed."
6278   (interactive "sMarks: \nP")
6279   (prog1
6280       (let ((data gnus-newsgroup-data)
6281             (marks (if (listp marks) marks
6282                      (append marks nil))) ; Transform to list.
6283             articles)
6284         (while data
6285           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6286                   (memq (gnus-data-mark (car data)) marks))
6287             (push (gnus-data-number (car data)) articles))
6288           (setq data (cdr data)))
6289         (gnus-summary-limit articles))
6290     (gnus-summary-position-point)))
6291
6292 (defun gnus-summary-limit-to-score (&optional score)
6293   "Limit to articles with score at or above SCORE."
6294   (interactive "P")
6295   (setq score (if score
6296                   (prefix-numeric-value score)
6297                 (or gnus-summary-default-score 0)))
6298   (let ((data gnus-newsgroup-data)
6299         articles)
6300     (while data
6301       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6302                 score)
6303         (push (gnus-data-number (car data)) articles))
6304       (setq data (cdr data)))
6305     (prog1
6306         (gnus-summary-limit articles)
6307       (gnus-summary-position-point))))
6308
6309 (defun gnus-summary-limit-include-thread (id)
6310   "Display all the hidden articles that in the current thread."
6311   (interactive (list (mail-header-id (gnus-summary-article-header))))
6312   (let ((articles (gnus-articles-in-thread
6313                    (gnus-id-to-thread (gnus-root-id id)))))
6314     (prog1
6315         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6316       (gnus-summary-position-point))))
6317
6318 (defun gnus-summary-limit-include-dormant ()
6319   "Display all the hidden articles that are marked as dormant.
6320 Note that this command only works on a subset of the articles currently
6321 fetched for this group."
6322   (interactive)
6323   (unless gnus-newsgroup-dormant
6324     (error "There are no dormant articles in this group"))
6325   (prog1
6326       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6327     (gnus-summary-position-point)))
6328
6329 (defun gnus-summary-limit-exclude-dormant ()
6330   "Hide all dormant articles."
6331   (interactive)
6332   (prog1
6333       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6334     (gnus-summary-position-point)))
6335
6336 (defun gnus-summary-limit-exclude-childless-dormant ()
6337   "Hide all dormant articles that have no children."
6338   (interactive)
6339   (let ((data (gnus-data-list t))
6340         articles d children)
6341     ;; Find all articles that are either not dormant or have
6342     ;; children.
6343     (while (setq d (pop data))
6344       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6345                 (and (setq children
6346                            (gnus-article-children (gnus-data-number d)))
6347                      (let (found)
6348                        (while children
6349                          (when (memq (car children) articles)
6350                            (setq children nil
6351                                  found t))
6352                          (pop children))
6353                        found)))
6354         (push (gnus-data-number d) articles)))
6355     ;; Do the limiting.
6356     (prog1
6357         (gnus-summary-limit articles)
6358       (gnus-summary-position-point))))
6359
6360 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6361   "Mark all unread excluded articles as read.
6362 If ALL, mark even excluded ticked and dormants as read."
6363   (interactive "P")
6364   (let ((articles (gnus-sorted-complement
6365                    (sort
6366                     (mapcar (lambda (h) (mail-header-number h))
6367                             gnus-newsgroup-headers)
6368                     '<)
6369                    (sort gnus-newsgroup-limit '<)))
6370         article)
6371     (setq gnus-newsgroup-unreads
6372           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6373     (if all
6374         (setq gnus-newsgroup-dormant nil
6375               gnus-newsgroup-marked nil
6376               gnus-newsgroup-reads
6377               (nconc
6378                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6379                gnus-newsgroup-reads))
6380       (while (setq article (pop articles))
6381         (unless (or (memq article gnus-newsgroup-dormant)
6382                     (memq article gnus-newsgroup-marked))
6383           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6384
6385 (defun gnus-summary-limit (articles &optional pop)
6386   (if pop
6387       ;; We pop the previous limit off the stack and use that.
6388       (setq articles (car gnus-newsgroup-limits)
6389             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6390     ;; We use the new limit, so we push the old limit on the stack.
6391     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6392   ;; Set the limit.
6393   (setq gnus-newsgroup-limit articles)
6394   (let ((total (length gnus-newsgroup-data))
6395         (data (gnus-data-find-list (gnus-summary-article-number)))
6396         (gnus-summary-mark-below nil)   ; Inhibit this.
6397         found)
6398     ;; This will do all the work of generating the new summary buffer
6399     ;; according to the new limit.
6400     (gnus-summary-prepare)
6401     ;; Hide any threads, possibly.
6402     (and gnus-show-threads
6403          gnus-thread-hide-subtree
6404          (gnus-summary-hide-all-threads))
6405     ;; Try to return to the article you were at, or one in the
6406     ;; neighborhood.
6407     (when data
6408       ;; We try to find some article after the current one.
6409       (while data
6410         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6411           (setq data nil
6412                 found t))
6413         (setq data (cdr data))))
6414     (unless found
6415       ;; If there is no data, that means that we were after the last
6416       ;; article.  The same goes when we can't find any articles
6417       ;; after the current one.
6418       (goto-char (point-max))
6419       (gnus-summary-find-prev))
6420     (gnus-set-mode-line 'summary)
6421     ;; We return how many articles were removed from the summary
6422     ;; buffer as a result of the new limit.
6423     (- total (length gnus-newsgroup-data))))
6424
6425 (defsubst gnus-invisible-cut-children (threads)
6426   (let ((num 0))
6427     (while threads
6428       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6429         (incf num))
6430       (pop threads))
6431     (< num 2)))
6432
6433 (defsubst gnus-cut-thread (thread)
6434   "Go forwards in the thread until we find an article that we want to display."
6435   (when (or (eq gnus-fetch-old-headers 'some)
6436             (eq gnus-fetch-old-headers 'invisible)
6437             (eq gnus-build-sparse-threads 'some)
6438             (eq gnus-build-sparse-threads 'more))
6439     ;; Deal with old-fetched headers and sparse threads.
6440     (while (and
6441             thread
6442             (or
6443              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6444              (gnus-summary-article-ancient-p
6445               (mail-header-number (car thread))))
6446             (if (or (<= (length (cdr thread)) 1)
6447                     (eq gnus-fetch-old-headers 'invisible))
6448                 (setq gnus-newsgroup-limit
6449                       (delq (mail-header-number (car thread))
6450                             gnus-newsgroup-limit)
6451                       thread (cadr thread))
6452               (when (gnus-invisible-cut-children (cdr thread))
6453                 (let ((th (cdr thread)))
6454                   (while th
6455                     (if (memq (mail-header-number (caar th))
6456                               gnus-newsgroup-limit)
6457                         (setq thread (car th)
6458                               th nil)
6459                       (setq th (cdr th))))))))))
6460   thread)
6461
6462 (defun gnus-cut-threads (threads)
6463   "Cut off all uninteresting articles from the beginning of threads."
6464   (when (or (eq gnus-fetch-old-headers 'some)
6465             (eq gnus-fetch-old-headers 'invisible)
6466             (eq gnus-build-sparse-threads 'some)
6467             (eq gnus-build-sparse-threads 'more))
6468     (let ((th threads))
6469       (while th
6470         (setcar th (gnus-cut-thread (car th)))
6471         (setq th (cdr th)))))
6472   ;; Remove nixed out threads.
6473   (delq nil threads))
6474
6475 (defun gnus-summary-initial-limit (&optional show-if-empty)
6476   "Figure out what the initial limit is supposed to be on group entry.
6477 This entails weeding out unwanted dormants, low-scored articles,
6478 fetch-old-headers verbiage, and so on."
6479   ;; Most groups have nothing to remove.
6480   (if (or gnus-inhibit-limiting
6481           (and (null gnus-newsgroup-dormant)
6482                (not (eq gnus-fetch-old-headers 'some))
6483                (not (eq gnus-fetch-old-headers 'invisible))
6484                (null gnus-summary-expunge-below)
6485                (not (eq gnus-build-sparse-threads 'some))
6486                (not (eq gnus-build-sparse-threads 'more))
6487                (null gnus-thread-expunge-below)
6488                (not gnus-use-nocem)))
6489       ()                                ; Do nothing.
6490     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6491     (setq gnus-newsgroup-limit nil)
6492     (mapatoms
6493      (lambda (node)
6494        (unless (car (symbol-value node))
6495          ;; These threads have no parents -- they are roots.
6496          (let ((nodes (cdr (symbol-value node)))
6497                thread)
6498            (while nodes
6499              (if (and gnus-thread-expunge-below
6500                       (< (gnus-thread-total-score (car nodes))
6501                          gnus-thread-expunge-below))
6502                  (gnus-expunge-thread (pop nodes))
6503                (setq thread (pop nodes))
6504                (gnus-summary-limit-children thread))))))
6505      gnus-newsgroup-dependencies)
6506     ;; If this limitation resulted in an empty group, we might
6507     ;; pop the previous limit and use it instead.
6508     (when (and (not gnus-newsgroup-limit)
6509                show-if-empty)
6510       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6511     gnus-newsgroup-limit))
6512
6513 (defun gnus-summary-limit-children (thread)
6514   "Return 1 if this subthread is visible and 0 if it is not."
6515   ;; First we get the number of visible children to this thread.  This
6516   ;; is done by recursing down the thread using this function, so this
6517   ;; will really go down to a leaf article first, before slowly
6518   ;; working its way up towards the root.
6519   (when thread
6520     (let ((children
6521            (if (cdr thread)
6522                (apply '+ (mapcar 'gnus-summary-limit-children
6523                                  (cdr thread)))
6524              0))
6525           (number (mail-header-number (car thread)))
6526           score)
6527       (if (and
6528            (not (memq number gnus-newsgroup-marked))
6529            (or
6530             ;; If this article is dormant and has absolutely no visible
6531             ;; children, then this article isn't visible.
6532             (and (memq number gnus-newsgroup-dormant)
6533                  (zerop children))
6534             ;; If this is "fetch-old-headered" and there is no
6535             ;; visible children, then we don't want this article.
6536             (and (eq gnus-fetch-old-headers 'some)
6537                  (gnus-summary-article-ancient-p number)
6538                  (zerop children))
6539             ;; If this is "fetch-old-headered" and `invisible', then
6540             ;; we don't want this article.
6541             (and (eq gnus-fetch-old-headers 'invisible)
6542                  (gnus-summary-article-ancient-p number))
6543             ;; If this is a sparsely inserted article with no children,
6544             ;; we don't want it.
6545             (and (eq gnus-build-sparse-threads 'some)
6546                  (gnus-summary-article-sparse-p number)
6547                  (zerop children))
6548             ;; If we use expunging, and this article is really
6549             ;; low-scored, then we don't want this article.
6550             (when (and gnus-summary-expunge-below
6551                        (< (setq score
6552                                 (or (cdr (assq number gnus-newsgroup-scored))
6553                                     gnus-summary-default-score))
6554                           gnus-summary-expunge-below))
6555               ;; We increase the expunge-tally here, but that has
6556               ;; nothing to do with the limits, really.
6557               (incf gnus-newsgroup-expunged-tally)
6558               ;; We also mark as read here, if that's wanted.
6559               (when (and gnus-summary-mark-below
6560                          (< score gnus-summary-mark-below))
6561                 (setq gnus-newsgroup-unreads
6562                       (delq number gnus-newsgroup-unreads))
6563                 (if gnus-newsgroup-auto-expire
6564                     (push number gnus-newsgroup-expirable)
6565                   (push (cons number gnus-low-score-mark)
6566                         gnus-newsgroup-reads)))
6567               t)
6568             ;; Check NoCeM things.
6569             (if (and gnus-use-nocem
6570                      (gnus-nocem-unwanted-article-p
6571                       (mail-header-id (car thread))))
6572                 (progn
6573                   (setq gnus-newsgroup-unreads
6574                         (delq number gnus-newsgroup-unreads))
6575                   t))))
6576           ;; Nope, invisible article.
6577           0
6578         ;; Ok, this article is to be visible, so we add it to the limit
6579         ;; and return 1.
6580         (push number gnus-newsgroup-limit)
6581         1))))
6582
6583 (defun gnus-expunge-thread (thread)
6584   "Mark all articles in THREAD as read."
6585   (let* ((number (mail-header-number (car thread))))
6586     (incf gnus-newsgroup-expunged-tally)
6587     ;; We also mark as read here, if that's wanted.
6588     (setq gnus-newsgroup-unreads
6589           (delq number gnus-newsgroup-unreads))
6590     (if gnus-newsgroup-auto-expire
6591         (push number gnus-newsgroup-expirable)
6592       (push (cons number gnus-low-score-mark)
6593             gnus-newsgroup-reads)))
6594   ;; Go recursively through all subthreads.
6595   (mapcar 'gnus-expunge-thread (cdr thread)))
6596
6597 ;; Summary article oriented commands
6598
6599 (defun gnus-summary-refer-parent-article (n)
6600   "Refer parent article N times.
6601 If N is negative, go to ancestor -N instead.
6602 The difference between N and the number of articles fetched is returned."
6603   (interactive "p")
6604   (let ((skip 1)
6605         error header ref)
6606     (when (not (natnump n))
6607       (setq skip (abs n)
6608             n 1))
6609     (while (and (> n 0)
6610                 (not error))
6611       (setq header (gnus-summary-article-header))
6612       (if (and (eq (mail-header-number header)
6613                    (cdr gnus-article-current))
6614                (equal gnus-newsgroup-name
6615                       (car gnus-article-current)))
6616           ;; If we try to find the parent of the currently
6617           ;; displayed article, then we take a look at the actual
6618           ;; References header, since this is slightly more
6619           ;; reliable than the References field we got from the
6620           ;; server.
6621           (save-excursion
6622             (set-buffer gnus-original-article-buffer)
6623             (nnheader-narrow-to-headers)
6624             (unless (setq ref (message-fetch-field "references"))
6625               (setq ref (message-fetch-field "in-reply-to")))
6626             (widen))
6627         (setq ref
6628               ;; It's not the current article, so we take a bet on
6629               ;; the value we got from the server.
6630               (mail-header-references header)))
6631       (if (and ref
6632                (not (equal ref "")))
6633           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6634             (gnus-message 1 "Couldn't find parent"))
6635         (gnus-message 1 "No references in article %d"
6636                       (gnus-summary-article-number))
6637         (setq error t))
6638       (decf n))
6639     (gnus-summary-position-point)
6640     n))
6641
6642 (defun gnus-summary-refer-references ()
6643   "Fetch all articles mentioned in the References header.
6644 Return the number of articles fetched."
6645   (interactive)
6646   (let ((ref (mail-header-references (gnus-summary-article-header)))
6647         (current (gnus-summary-article-number))
6648         (n 0))
6649     (if (or (not ref)
6650             (equal ref ""))
6651         (error "No References in the current article")
6652       ;; For each Message-ID in the References header...
6653       (while (string-match "<[^>]*>" ref)
6654         (incf n)
6655         ;; ... fetch that article.
6656         (gnus-summary-refer-article
6657          (prog1 (match-string 0 ref)
6658            (setq ref (substring ref (match-end 0))))))
6659       (gnus-summary-goto-subject current)
6660       (gnus-summary-position-point)
6661       n)))
6662
6663 (defun gnus-summary-refer-thread (&optional limit)
6664   "Fetch all articles in the current thread.
6665 If LIMIT (the numerical prefix), fetch that many old headers instead
6666 of what's specified by the `gnus-refer-thread-limit' variable."
6667   (interactive "P")
6668   (let ((id (mail-header-id (gnus-summary-article-header)))
6669         (limit (if limit (prefix-numeric-value limit)
6670                  gnus-refer-thread-limit)))
6671     ;; We want to fetch LIMIT *old* headers, but we also have to
6672     ;; re-fetch all the headers in the current buffer, because many of
6673     ;; them may be undisplayed.  So we adjust LIMIT.
6674     (when (numberp limit)
6675       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6676     (unless (eq gnus-fetch-old-headers 'invisible)
6677       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6678       ;; Retrieve the headers and read them in.
6679       (if (eq (gnus-retrieve-headers
6680                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6681               'nov)
6682           (gnus-build-all-threads)
6683         (error "Can't fetch thread from backends that don't support NOV"))
6684       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6685     (gnus-summary-limit-include-thread id)))
6686
6687 (defun gnus-summary-refer-article (message-id &optional arg)
6688   "Fetch an article specified by MESSAGE-ID.
6689 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6690 or `gnus-select-method', no matter what backend the article comes from."
6691   (interactive "sMessage-ID: \nP")
6692   (when (and (stringp message-id)
6693              (not (zerop (length message-id))))
6694     ;; Construct the correct Message-ID if necessary.
6695     ;; Suggested by tale@pawl.rpi.edu.
6696     (unless (string-match "^<" message-id)
6697       (setq message-id (concat "<" message-id)))
6698     (unless (string-match ">$" message-id)
6699       (setq message-id (concat message-id ">")))
6700     (let* ((header (gnus-id-to-header message-id))
6701            (sparse (and header
6702                         (gnus-summary-article-sparse-p
6703                          (mail-header-number header))
6704                         (memq (mail-header-number header)
6705                               gnus-newsgroup-limit))))
6706       (cond
6707        ;; If the article is present in the buffer we just go to it.
6708        ((and header
6709              (or (not (gnus-summary-article-sparse-p
6710                        (mail-header-number header)))
6711                  sparse))
6712         (prog1
6713             (gnus-summary-goto-article
6714              (mail-header-number header) nil t)
6715           (when sparse
6716             (gnus-summary-update-article (mail-header-number header)))))
6717        (t
6718         ;; We fetch the article
6719         (let ((gnus-override-method
6720                (cond ((gnus-news-group-p gnus-newsgroup-name)
6721                       gnus-refer-article-method)
6722                      (arg
6723                       (or gnus-refer-article-method gnus-select-method))
6724                      (t nil)))
6725               number)
6726           ;; Start the special refer-article method, if necessary.
6727           (when (and gnus-refer-article-method
6728                      (gnus-news-group-p gnus-newsgroup-name))
6729             (gnus-check-server gnus-refer-article-method))
6730           ;; Fetch the header, and display the article.
6731           (if (setq number (gnus-summary-insert-subject message-id))
6732               (gnus-summary-select-article nil nil nil number)
6733             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6734
6735 (defun gnus-summary-edit-parameters ()
6736   "Edit the group parameters of the current group."
6737   (interactive)
6738   (gnus-group-edit-group gnus-newsgroup-name 'params))
6739
6740 (defun gnus-summary-customize-parameters ()
6741   "Customize the group parameters of the current group."
6742   (interactive)
6743   (gnus-group-customize gnus-newsgroup-name))
6744
6745 (defun gnus-summary-enter-digest-group (&optional force)
6746   "Enter an nndoc group based on the current article.
6747 If FORCE, force a digest interpretation.  If not, try
6748 to guess what the document format is."
6749   (interactive "P")
6750   (let ((conf gnus-current-window-configuration))
6751     (save-excursion
6752       (gnus-summary-select-article))
6753     (setq gnus-current-window-configuration conf)
6754     (let* ((name (format "%s-%d"
6755                          (gnus-group-prefixed-name
6756                           gnus-newsgroup-name (list 'nndoc ""))
6757                          (save-excursion
6758                            (set-buffer gnus-summary-buffer)
6759                            gnus-current-article)))
6760            (ogroup gnus-newsgroup-name)
6761            (params (append (gnus-info-params (gnus-get-info ogroup))
6762                            (list (cons 'to-group ogroup))
6763                            (list (cons 'save-article-group ogroup))))
6764            (case-fold-search t)
6765            (buf (current-buffer))
6766            dig)
6767       (save-excursion
6768         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6769         (insert-buffer-substring gnus-original-article-buffer)
6770         ;; Remove lines that may lead nndoc to misinterpret the
6771         ;; document type.
6772         (narrow-to-region
6773          (goto-char (point-min))
6774          (or (search-forward "\n\n" nil t) (point)))
6775         (goto-char (point-min))
6776         (delete-matching-lines "^Path:\\|^From ")
6777         (widen))
6778       (unwind-protect
6779           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6780                     (gnus-newsgroup-ephemeral-ignored-charsets
6781                      gnus-newsgroup-ignored-charsets))
6782                 (gnus-group-read-ephemeral-group
6783                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6784                               (nndoc-article-type
6785                                ,(if force 'digest 'guess))) t))
6786               ;; Make all postings to this group go to the parent group.
6787               (nconc (gnus-info-params (gnus-get-info name))
6788                      params)
6789             ;; Couldn't select this doc group.
6790             (switch-to-buffer buf)
6791             (gnus-set-global-variables)
6792             (gnus-configure-windows 'summary)
6793             (gnus-message 3 "Article couldn't be entered?"))
6794         (kill-buffer dig)))))
6795
6796 (defun gnus-summary-read-document (n)
6797   "Open a new group based on the current article(s).
6798 This will allow you to read digests and other similar
6799 documents as newsgroups.
6800 Obeys the standard process/prefix convention."
6801   (interactive "P")
6802   (let* ((articles (gnus-summary-work-articles n))
6803          (ogroup gnus-newsgroup-name)
6804          (params (append (gnus-info-params (gnus-get-info ogroup))
6805                          (list (cons 'to-group ogroup))))
6806          article group egroup groups vgroup)
6807     (while (setq article (pop articles))
6808       (setq group (format "%s-%d" gnus-newsgroup-name article))
6809       (gnus-summary-remove-process-mark article)
6810       (when (gnus-summary-display-article article)
6811         (save-excursion
6812           (with-temp-buffer
6813             (insert-buffer-substring gnus-original-article-buffer)
6814             ;; Remove some headers that may lead nndoc to make
6815             ;; the wrong guess.
6816             (message-narrow-to-head)
6817             (goto-char (point-min))
6818             (delete-matching-lines "^\\(Path\\):\\|^From ")
6819             (widen)
6820             (if (setq egroup
6821                       (gnus-group-read-ephemeral-group
6822                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6823                                      (nndoc-article-type guess))
6824                        t nil t))
6825                 (progn
6826                   ;; Make all postings to this group go to the parent group.
6827                   (nconc (gnus-info-params (gnus-get-info egroup))
6828                          params)
6829                   (push egroup groups))
6830               ;; Couldn't select this doc group.
6831               (gnus-error 3 "Article couldn't be entered"))))))
6832     ;; Now we have selected all the documents.
6833     (cond
6834      ((not groups)
6835       (error "None of the articles could be interpreted as documents"))
6836      ((gnus-group-read-ephemeral-group
6837        (setq vgroup (format
6838                      "nnvirtual:%s-%s" gnus-newsgroup-name
6839                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6840        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6841        t
6842        (cons (current-buffer) 'summary)))
6843      (t
6844       (error "Couldn't select virtual nndoc group")))))
6845
6846 (defun gnus-summary-isearch-article (&optional regexp-p)
6847   "Do incremental search forward on the current article.
6848 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6849   (interactive "P")
6850   (let (gnus-treat-hide-headers
6851         gnus-treat-hide-boring-headers
6852         gnus-treat-display-xface
6853         gnus-treat-display-smileys
6854         gnus-treat-display-picons
6855         gnus-treat-play-sounds)
6856     (gnus-summary-select-article nil t))
6857   (gnus-configure-windows 'article)
6858   (gnus-eval-in-buffer-window gnus-article-buffer
6859     (save-restriction
6860       (widen)
6861       (isearch-forward regexp-p))))
6862
6863 (defun gnus-summary-search-article-forward (regexp &optional backward)
6864   "Search for an article containing REGEXP forward.
6865 If BACKWARD, search backward instead."
6866   (interactive
6867    (list (read-string
6868           (format "Search article %s (regexp%s): "
6869                   (if current-prefix-arg "backward" "forward")
6870                   (if gnus-last-search-regexp
6871                       (concat ", default " gnus-last-search-regexp)
6872                     "")))
6873          current-prefix-arg))
6874   (if (string-equal regexp "")
6875       (setq regexp (or gnus-last-search-regexp ""))
6876     (setq gnus-last-search-regexp regexp))
6877   (if (gnus-summary-search-article regexp backward)
6878       (gnus-summary-show-thread)
6879     (error "Search failed: \"%s\"" regexp)))
6880
6881 (defun gnus-summary-search-article-backward (regexp)
6882   "Search for an article containing REGEXP backward."
6883   (interactive
6884    (list (read-string
6885           (format "Search article backward (regexp%s): "
6886                   (if gnus-last-search-regexp
6887                       (concat ", default " gnus-last-search-regexp)
6888                     "")))))
6889   (gnus-summary-search-article-forward regexp 'backward))
6890
6891 (defvar gnus-summary-search-article-matched-text)
6892 (eval-when-compile
6893   (defmacro gnus-summary-search-article-highlight-matched-text ()
6894     "Highlight matched text in the function `gnus-summary-search-article'."
6895     '(let ((start (match-beginning 0))
6896            (end (match-end 0))
6897            (old (if (boundp 'gnus-summary-search-article-matched-text)
6898                     gnus-summary-search-article-matched-text))
6899            (inhibit-read-only t)
6900            buffer-read-only)
6901        (when old
6902          (let ((old-start (car old))
6903                (old-end (cadr old))
6904                (old-text (caddr old)))
6905            (when (string-equal (buffer-substring old-start old-end) old-text)
6906              (goto-char old-start)
6907              (delete-region old-start old-end)
6908              (insert old-text)
6909              (goto-char end))))
6910        (set (make-local-variable 'gnus-summary-search-article-matched-text)
6911             (list start end (buffer-substring start end)))
6912        (put-text-property start end 'face
6913                           (or (find-face 'isearch) 'secondary-selection))))
6914   )
6915
6916 (defun gnus-summary-search-article (regexp &optional backward)
6917   "Search for an article containing REGEXP.
6918 Optional argument BACKWARD means do search for backward.
6919 `gnus-select-article-hook' is not called during the search."
6920   ;; We have to require this here to make sure that the following
6921   ;; dynamic binding isn't shadowed by autoloading.
6922   (require 'gnus-async)
6923   (require 'gnus-art)
6924   (let ((gnus-select-article-hook nil)  ;Disable hook.
6925         (gnus-article-display-hook nil)
6926         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6927         (gnus-use-article-prefetch nil)
6928         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6929         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6930         (sum (current-buffer))
6931         (found nil)
6932         point)
6933     (gnus-save-hidden-threads
6934       (let (gnus-treat-hide-headers
6935             gnus-treat-hide-boring-headers
6936             gnus-treat-display-xface
6937             gnus-treat-display-smileys
6938             gnus-treat-display-picons
6939             gnus-treat-play-sounds)
6940         (gnus-summary-select-article nil t))
6941       (set-buffer gnus-article-buffer)
6942       (when backward
6943         (forward-line -1))
6944       (while (not found)
6945         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6946         (if (if backward
6947                 (re-search-backward regexp nil t)
6948               (re-search-forward regexp nil t))
6949             ;; We found the regexp.
6950             (progn
6951               (gnus-summary-search-article-highlight-matched-text)
6952               (setq found 'found)
6953               (beginning-of-line)
6954               (set-window-start
6955                (get-buffer-window (current-buffer))
6956                (point))
6957               (forward-line 1)
6958               (set-buffer sum)
6959               (setq point (point)))
6960           ;; We didn't find it, so we go to the next article.
6961           (set-buffer sum)
6962           (setq found 'not)
6963           (while (eq found 'not)
6964             (if (not (if backward (gnus-summary-find-prev)
6965                        (gnus-summary-find-next)))
6966                 ;; No more articles.
6967                 (setq found t)
6968               ;; Select the next article and adjust point.
6969               (unless (gnus-summary-article-sparse-p
6970                        (gnus-summary-article-number))
6971                 (setq found nil)
6972                 (let (gnus-treat-hide-headers
6973                       gnus-treat-hide-boring-headers
6974                       gnus-treat-display-xface
6975                       gnus-treat-display-smileys
6976                       gnus-treat-display-picons
6977                       gnus-treat-play-sounds)
6978                   (gnus-summary-select-article nil t))
6979                 (set-buffer gnus-article-buffer)
6980                 (widen)
6981                 (goto-char (if backward (point-max) (point-min))))))))
6982       (gnus-message 7 ""))
6983     ;; Return whether we found the regexp.
6984     (when (eq found 'found)
6985       (goto-char point)
6986       (gnus-summary-show-thread)
6987       (gnus-summary-goto-subject gnus-current-article)
6988       (gnus-summary-position-point)
6989       t)))
6990
6991 (defun gnus-summary-find-matching (header regexp &optional backward unread
6992                                           not-case-fold)
6993   "Return a list of all articles that match REGEXP on HEADER.
6994 The search stars on the current article and goes forwards unless
6995 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6996 If UNREAD is non-nil, only unread articles will
6997 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6998 in the comparisons."
6999   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7000                 (gnus-data-find-list
7001                  (gnus-summary-article-number) (gnus-data-list backward))))
7002         (case-fold-search (not not-case-fold))
7003         articles d func)
7004     (if (consp header)
7005         (if (eq (car header) 'extra)
7006             (setq func
7007                   `(lambda (h)
7008                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7009                          "")))
7010           (error "%s is an invalid header" header))
7011       (unless (fboundp (intern (concat "mail-header-" header)))
7012         (error "%s is not a valid header" header))
7013       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7014     (while data
7015       (setq d (car data))
7016       (and (or (not unread)             ; We want all articles...
7017                (gnus-data-unread-p d))  ; Or just unreads.
7018            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7019            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7020            (push (gnus-data-number d) articles)) ; Success!
7021       (setq data (cdr data)))
7022     (nreverse articles)))
7023
7024 (defun gnus-summary-execute-command (header regexp command &optional backward)
7025   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7026 If HEADER is an empty string (or nil), the match is done on the entire
7027 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7028   (interactive
7029    (list (let ((completion-ignore-case t))
7030            (completing-read
7031             "Header name: "
7032             (mapcar (lambda (string) (list string))
7033                     '("Number" "Subject" "From" "Lines" "Date"
7034                       "Message-ID" "Xref" "References" "Body"))
7035             nil 'require-match))
7036          (read-string "Regexp: ")
7037          (read-key-sequence "Command: ")
7038          current-prefix-arg))
7039   (when (equal header "Body")
7040     (setq header ""))
7041   ;; Hidden thread subtrees must be searched as well.
7042   (gnus-summary-show-all-threads)
7043   ;; We don't want to change current point nor window configuration.
7044   (save-excursion
7045     (save-window-excursion
7046       (gnus-message 6 "Executing %s..." (key-description command))
7047       ;; We'd like to execute COMMAND interactively so as to give arguments.
7048       (gnus-execute header regexp
7049                     `(call-interactively ',(key-binding command))
7050                     backward)
7051       (gnus-message 6 "Executing %s...done" (key-description command)))))
7052
7053 (defun gnus-summary-beginning-of-article ()
7054   "Scroll the article back to the beginning."
7055   (interactive)
7056   (gnus-summary-select-article)
7057   (gnus-configure-windows 'article)
7058   (gnus-eval-in-buffer-window gnus-article-buffer
7059     (widen)
7060     (goto-char (point-min))
7061     (when gnus-page-broken
7062       (gnus-narrow-to-page))))
7063
7064 (defun gnus-summary-end-of-article ()
7065   "Scroll to the end of the article."
7066   (interactive)
7067   (gnus-summary-select-article)
7068   (gnus-configure-windows 'article)
7069   (gnus-eval-in-buffer-window gnus-article-buffer
7070     (widen)
7071     (goto-char (point-max))
7072     (recenter -3)
7073     (when gnus-page-broken
7074       (gnus-narrow-to-page))))
7075
7076 (defun gnus-summary-print-article (&optional filename n)
7077   "Generate and print a PostScript image of the N next (mail) articles.
7078
7079 If N is negative, print the N previous articles.  If N is nil and articles
7080 have been marked with the process mark, print these instead.
7081
7082 If the optional first argument FILENAME is nil, send the image to the
7083 printer.  If FILENAME is a string, save the PostScript image in a file with
7084 that name.  If FILENAME is a number, prompt the user for the name of the file
7085 to save in."
7086   (interactive (list (ps-print-preprint current-prefix-arg)
7087                      current-prefix-arg))
7088   (dolist (article (gnus-summary-work-articles n))
7089     (gnus-summary-select-article nil nil 'pseudo article)
7090     (gnus-eval-in-buffer-window gnus-article-buffer
7091       (let ((buffer (generate-new-buffer " *print*")))
7092         (unwind-protect
7093             (progn
7094               (copy-to-buffer buffer (point-min) (point-max))
7095               (set-buffer buffer)
7096               (gnus-article-delete-invisible-text)
7097               (let ((ps-left-header
7098                      (list
7099                       (concat "("
7100                               (mail-header-subject gnus-current-headers) ")")
7101                       (concat "("
7102                               (mail-header-from gnus-current-headers) ")")))
7103                     (ps-right-header
7104                      (list
7105                       "/pagenumberstring load"
7106                       (concat "("
7107                               (mail-header-date gnus-current-headers) ")"))))
7108                 (gnus-run-hooks 'gnus-ps-print-hook)
7109                 (save-excursion
7110                   (ps-print-buffer-with-faces filename))))
7111           (kill-buffer buffer))))))
7112
7113 (defun gnus-summary-show-article (&optional arg)
7114   "Force re-fetching of the current article.
7115 If ARG (the prefix) is non-nil, show the raw article without any
7116 article massaging functions being run."
7117   (interactive "P")
7118   (if (not arg)
7119       ;; Select the article the normal way.
7120       (gnus-summary-select-article nil 'force)
7121     ;; We have to require this here to make sure that the following
7122     ;; dynamic binding isn't shadowed by autoloading.
7123     (require 'gnus-async)
7124     (require 'gnus-art)
7125     ;; Bind the article treatment functions to nil.
7126     (let ((gnus-have-all-headers t)
7127           gnus-article-display-hook
7128           gnus-article-prepare-hook
7129           gnus-article-decode-hook
7130           gnus-break-pages
7131           gnus-show-mime
7132           gnus-visual)
7133       (gnus-summary-select-article nil 'force)))
7134   (gnus-summary-goto-subject gnus-current-article)
7135   (gnus-summary-position-point))
7136
7137 (defun gnus-summary-verbose-headers (&optional arg)
7138   "Toggle permanent full header display.
7139 If ARG is a positive number, turn header display on.
7140 If ARG is a negative number, turn header display off."
7141   (interactive "P")
7142   (setq gnus-show-all-headers
7143         (cond ((or (not (numberp arg))
7144                    (zerop arg))
7145                (not gnus-show-all-headers))
7146               ((natnump arg)
7147                t)))
7148   (gnus-summary-show-article))
7149
7150 (defun gnus-summary-toggle-header (&optional arg)
7151   "Show the headers if they are hidden, or hide them if they are shown.
7152 If ARG is a positive number, show the entire header.
7153 If ARG is a negative number, hide the unwanted header lines."
7154   (interactive "P")
7155   (save-excursion
7156     (set-buffer gnus-article-buffer)
7157     (save-restriction
7158       (let* ((buffer-read-only nil)
7159              (inhibit-point-motion-hooks t)
7160              hidden e)
7161         (save-restriction 
7162           (article-narrow-to-head)
7163           (setq hidden (gnus-article-hidden-text-p 'headers)))
7164         (goto-char (point-min))
7165         (when (search-forward "\n\n" nil t)
7166           (delete-region (point-min) (1- (point))))
7167         (goto-char (point-min))
7168         (save-excursion
7169           (set-buffer gnus-original-article-buffer)
7170           (goto-char (point-min))
7171           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7172         (insert-buffer-substring gnus-original-article-buffer 1 e)
7173         (save-restriction
7174           (narrow-to-region (point-min) (point))
7175           (article-decode-encoded-words)
7176           (if (or hidden
7177                   (and (numberp arg) (< arg 0)))
7178               (let ((gnus-treat-hide-headers nil)
7179                     (gnus-treat-hide-boring-headers nil))
7180                 (gnus-treat-article 'head))
7181             (gnus-treat-article 'head)))))))
7182
7183 (defun gnus-summary-show-all-headers ()
7184   "Make all header lines visible."
7185   (interactive)
7186   (gnus-article-show-all-headers))
7187
7188 (defun gnus-summary-toggle-mime (&optional arg)
7189   "Toggle MIME processing.
7190 If ARG is a positive number, turn MIME processing on."
7191   (interactive "P")
7192   (setq gnus-show-mime
7193         (if (null arg)
7194             (not gnus-show-mime)
7195           (> (prefix-numeric-value arg) 0)))
7196   (gnus-summary-select-article t 'force))
7197
7198 (defun gnus-summary-caesar-message (&optional arg)
7199   "Caesar rotate the current article by 13.
7200 The numerical prefix specifies how many places to rotate each letter
7201 forward."
7202   (interactive "P")
7203   (gnus-summary-select-article)
7204   (let ((mail-header-separator ""))
7205     (gnus-eval-in-buffer-window gnus-article-buffer
7206       (save-restriction
7207         (widen)
7208         (let ((start (window-start))
7209               buffer-read-only)
7210           (message-caesar-buffer-body arg)
7211           (set-window-start (get-buffer-window (current-buffer)) start))))))
7212
7213 (defun gnus-summary-stop-page-breaking ()
7214   "Stop page breaking in the current article."
7215   (interactive)
7216   (gnus-summary-select-article)
7217   (gnus-eval-in-buffer-window gnus-article-buffer
7218     (widen)
7219     (when (gnus-visual-p 'page-marker)
7220       (let ((buffer-read-only nil))
7221         (gnus-remove-text-with-property 'gnus-prev)
7222         (gnus-remove-text-with-property 'gnus-next))
7223       (setq gnus-page-broken nil))))
7224
7225 (defun gnus-summary-move-article (&optional n to-newsgroup
7226                                             select-method action)
7227   "Move the current article to a different newsgroup.
7228 If N is a positive number, move the N next articles.
7229 If N is a negative number, move the N previous articles.
7230 If N is nil and any articles have been marked with the process mark,
7231 move those articles instead.
7232 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7233 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7234 re-spool using this method.
7235
7236 For this function to work, both the current newsgroup and the
7237 newsgroup that you want to move to have to support the `request-move'
7238 and `request-accept' functions."
7239   (interactive "P")
7240   (unless action
7241     (setq action 'move))
7242   ;; Disable marking as read.
7243   (let (gnus-mark-article-hook)
7244     (save-window-excursion
7245       (gnus-summary-select-article)))
7246   ;; Check whether the source group supports the required functions.
7247   (cond ((and (eq action 'move)
7248               (not (gnus-check-backend-function
7249                     'request-move-article gnus-newsgroup-name)))
7250          (error "The current group does not support article moving"))
7251         ((and (eq action 'crosspost)
7252               (not (gnus-check-backend-function
7253                     'request-replace-article gnus-newsgroup-name)))
7254          (error "The current group does not support article editing")))
7255   (let ((articles (gnus-summary-work-articles n))
7256         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7257         (names '((move "Move" "Moving")
7258                  (copy "Copy" "Copying")
7259                  (crosspost "Crosspost" "Crossposting")))
7260         (copy-buf (save-excursion
7261                     (nnheader-set-temp-buffer " *copy article*")))
7262         (default-marks gnus-article-mark-lists)
7263         (no-expire-marks (delete '(expirable . expire)
7264                                  (copy-sequence gnus-article-mark-lists)))
7265         art-group to-method new-xref article to-groups)
7266     (unless (assq action names)
7267       (error "Unknown action %s" action))
7268     ;; Read the newsgroup name.
7269     (when (and (not to-newsgroup)
7270                (not select-method))
7271       (setq to-newsgroup
7272             (gnus-read-move-group-name
7273              (cadr (assq action names))
7274              (symbol-value (intern (format "gnus-current-%s-group" action)))
7275              articles prefix))
7276       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7277     (setq to-method (or select-method
7278                         (gnus-group-name-to-method to-newsgroup)))
7279     ;; Check the method we are to move this article to...
7280     (unless (gnus-check-backend-function
7281              'request-accept-article (car to-method))
7282       (error "%s does not support article copying" (car to-method)))
7283     (unless (gnus-check-server to-method)
7284       (error "Can't open server %s" (car to-method)))
7285     (gnus-message 6 "%s to %s: %s..."
7286                   (caddr (assq action names))
7287                   (or (car select-method) to-newsgroup) articles)
7288     (while articles
7289       (setq article (pop articles))
7290       (setq
7291        art-group
7292        (cond
7293         ;; Move the article.
7294         ((eq action 'move)
7295          ;; Remove this article from future suppression.
7296          (gnus-dup-unsuppress-article article)
7297          (gnus-request-move-article
7298           article                       ; Article to move
7299           gnus-newsgroup-name           ; From newsgroup
7300           (nth 1 (gnus-find-method-for-group
7301                   gnus-newsgroup-name)) ; Server
7302           (list 'gnus-request-accept-article
7303                 to-newsgroup (list 'quote select-method)
7304                 (not articles) t)               ; Accept form
7305           (not articles)))              ; Only save nov last time
7306         ;; Copy the article.
7307         ((eq action 'copy)
7308          (save-excursion
7309            (set-buffer copy-buf)
7310            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7311              (gnus-request-accept-article
7312               to-newsgroup select-method (not articles) t))))
7313         ;; Crosspost the article.
7314         ((eq action 'crosspost)
7315          (let ((xref (message-tokenize-header
7316                       (mail-header-xref (gnus-summary-article-header article))
7317                       " ")))
7318            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7319                                   ":" article))
7320            (unless xref
7321              (setq xref (list (system-name))))
7322            (setq new-xref
7323                  (concat
7324                   (mapconcat 'identity
7325                              (delete "Xref:" (delete new-xref xref))
7326                              " ")
7327                   " " new-xref))
7328            (save-excursion
7329              (set-buffer copy-buf)
7330              ;; First put the article in the destination group.
7331              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7332              (when (consp (setq art-group
7333                                 (gnus-request-accept-article
7334                                  to-newsgroup select-method (not articles))))
7335                (setq new-xref (concat new-xref " " (car art-group)
7336                                       ":" (cdr art-group)))
7337                ;; Now we have the new Xrefs header, so we insert
7338                ;; it and replace the new article.
7339                (nnheader-replace-header "Xref" new-xref)
7340                (gnus-request-replace-article
7341                 (cdr art-group) to-newsgroup (current-buffer))
7342                art-group))))))
7343       (cond
7344        ((not art-group)
7345         (gnus-message 1 "Couldn't %s article %s"
7346                       (cadr (assq action names)) article))
7347        ((and (eq art-group 'junk)
7348              (eq action 'move))
7349         (gnus-summary-mark-article article gnus-canceled-mark)
7350         (gnus-message 4 "Deleted article %s" article))
7351        (t
7352         (let* ((pto-group (gnus-group-prefixed-name
7353                            (car art-group) to-method))
7354                (entry
7355                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7356                (info (nth 2 entry))
7357                (to-group (gnus-info-group info))
7358                to-marks)
7359           ;; Update the group that has been moved to.
7360           (when (and info
7361                      (memq action '(move copy)))
7362             (unless (member to-group to-groups)
7363               (push to-group to-groups))
7364
7365             (unless (memq article gnus-newsgroup-unreads)
7366               (push 'read to-marks)
7367               (gnus-info-set-read
7368                info (gnus-add-to-range (gnus-info-read info)
7369                                        (list (cdr art-group)))))
7370
7371             ;; Copy any marks over to the new group.
7372             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7373                              default-marks
7374                            no-expire-marks))
7375                   (to-article (cdr art-group)))
7376
7377               ;; See whether the article is to be put in the cache.
7378               (when gnus-use-cache
7379                 (gnus-cache-possibly-enter-article
7380                  to-group to-article
7381                  (let ((header (copy-sequence
7382                                 (gnus-summary-article-header article))))
7383                    (mail-header-set-number header to-article)
7384                    header)
7385                  (memq article gnus-newsgroup-marked)
7386                  (memq article gnus-newsgroup-dormant)
7387                  (memq article gnus-newsgroup-unreads)))
7388
7389               (when (and (equal to-group gnus-newsgroup-name)
7390                          (not (memq article gnus-newsgroup-unreads)))
7391                 ;; Mark this article as read in this group.
7392                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7393                 (setcdr (gnus-active to-group) to-article)
7394                 (setcdr gnus-newsgroup-active to-article))
7395
7396               (while marks
7397                 (when (memq article (symbol-value
7398                                      (intern (format "gnus-newsgroup-%s"
7399                                                      (caar marks)))))
7400                   (push (cdar marks) to-marks)
7401                   ;; If the other group is the same as this group,
7402                   ;; then we have to add the mark to the list.
7403                   (when (equal to-group gnus-newsgroup-name)
7404                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7405                          (cons to-article
7406                                (symbol-value
7407                                 (intern (format "gnus-newsgroup-%s"
7408                                                 (caar marks)))))))
7409                   ;; Copy the marks to other group.
7410                   (gnus-add-marked-articles
7411                    to-group (cdar marks) (list to-article) info))
7412                 (setq marks (cdr marks)))
7413
7414               (gnus-request-set-mark to-group (list (list (list to-article)
7415                                                           'set
7416                                                           to-marks)))
7417
7418               (gnus-dribble-enter
7419                (concat "(gnus-group-set-info '"
7420                        (gnus-prin1-to-string (gnus-get-info to-group))
7421                        ")"))))
7422
7423           ;; Update the Xref header in this article to point to
7424           ;; the new crossposted article we have just created.
7425           (when (eq action 'crosspost)
7426             (save-excursion
7427               (set-buffer copy-buf)
7428               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7429               (nnheader-replace-header "Xref" new-xref)
7430               (gnus-request-replace-article
7431                article gnus-newsgroup-name (current-buffer)))))
7432
7433         ;;;!!!Why is this necessary?
7434         (set-buffer gnus-summary-buffer)
7435
7436         (gnus-summary-goto-subject article)
7437         (when (eq action 'move)
7438           (gnus-summary-mark-article article gnus-canceled-mark))))
7439       (gnus-summary-remove-process-mark article))
7440     ;; Re-activate all groups that have been moved to.
7441     (while to-groups
7442       (save-excursion
7443         (set-buffer gnus-group-buffer)
7444         (when (gnus-group-goto-group (car to-groups) t)
7445           (gnus-group-get-new-news-this-group 1 t))
7446         (pop to-groups)))
7447
7448     (gnus-kill-buffer copy-buf)
7449     (gnus-summary-position-point)
7450     (gnus-set-mode-line 'summary)))
7451
7452 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7453   "Move the current article to a different newsgroup.
7454 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7455 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7456 re-spool using this method."
7457   (interactive "P")
7458   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7459
7460 (defun gnus-summary-crosspost-article (&optional n)
7461   "Crosspost the current article to some other group."
7462   (interactive "P")
7463   (gnus-summary-move-article n nil nil 'crosspost))
7464
7465 (defcustom gnus-summary-respool-default-method nil
7466   "Default method for respooling an article.
7467 If nil, use to the current newsgroup method."
7468   :type '(choice (gnus-select-method :value (nnml ""))
7469                  (const nil))
7470   :group 'gnus-summary-mail)
7471
7472 (defun gnus-summary-respool-article (&optional n method)
7473   "Respool the current article.
7474 The article will be squeezed through the mail spooling process again,
7475 which means that it will be put in some mail newsgroup or other
7476 depending on `nnmail-split-methods'.
7477 If N is a positive number, respool the N next articles.
7478 If N is a negative number, respool the N previous articles.
7479 If N is nil and any articles have been marked with the process mark,
7480 respool those articles instead.
7481
7482 Respooling can be done both from mail groups and \"real\" newsgroups.
7483 In the former case, the articles in question will be moved from the
7484 current group into whatever groups they are destined to.  In the
7485 latter case, they will be copied into the relevant groups."
7486   (interactive
7487    (list current-prefix-arg
7488          (let* ((methods (gnus-methods-using 'respool))
7489                 (methname
7490                  (symbol-name (or gnus-summary-respool-default-method
7491                                   (car (gnus-find-method-for-group
7492                                         gnus-newsgroup-name)))))
7493                 (method
7494                  (gnus-completing-read
7495                   methname "What backend do you want to use when respooling?"
7496                   methods nil t nil 'gnus-mail-method-history))
7497                 ms)
7498            (cond
7499             ((zerop (length (setq ms (gnus-servers-using-backend
7500                                       (intern method)))))
7501              (list (intern method) ""))
7502             ((= 1 (length ms))
7503              (car ms))
7504             (t
7505              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7506                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7507                            ms-alist))))))))
7508   (unless method
7509     (error "No method given for respooling"))
7510   (if (assoc (symbol-name
7511               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7512              (gnus-methods-using 'respool))
7513       (gnus-summary-move-article n nil method)
7514     (gnus-summary-copy-article n nil method)))
7515
7516 (defun gnus-summary-import-article (file)
7517   "Import an arbitrary file into a mail newsgroup."
7518   (interactive "fImport file: ")
7519   (let ((group gnus-newsgroup-name)
7520         (now (current-time))
7521         atts lines)
7522     (unless (gnus-check-backend-function 'request-accept-article group)
7523       (error "%s does not support article importing" group))
7524     (or (file-readable-p file)
7525         (not (file-regular-p file))
7526         (error "Can't read %s" file))
7527     (save-excursion
7528       (set-buffer (gnus-get-buffer-create " *import file*"))
7529       (erase-buffer)
7530       (nnheader-insert-file-contents file)
7531       (goto-char (point-min))
7532       (unless (nnheader-article-p)
7533         ;; This doesn't look like an article, so we fudge some headers.
7534         (setq atts (file-attributes file)
7535               lines (count-lines (point-min) (point-max)))
7536         (insert "From: " (read-string "From: ") "\n"
7537                 "Subject: " (read-string "Subject: ") "\n"
7538                 "Date: " (message-make-date (nth 5 atts))
7539                 "\n"
7540                 "Message-ID: " (message-make-message-id) "\n"
7541                 "Lines: " (int-to-string lines) "\n"
7542                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7543       (gnus-request-accept-article group nil t)
7544       (kill-buffer (current-buffer)))))
7545
7546 (defun gnus-summary-article-posted-p ()
7547   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7548 This will be the case if the article has both been mailed and posted."
7549   (interactive)
7550   (let ((id (mail-header-references (gnus-summary-article-header)))
7551         (gnus-override-method
7552          (or gnus-refer-article-method gnus-select-method)))
7553     (if (gnus-request-head id "")
7554         (gnus-message 2 "The current message was found on %s"
7555                       gnus-override-method)
7556       (gnus-message 2 "The current message couldn't be found on %s"
7557                     gnus-override-method)
7558       nil)))
7559
7560 (defun gnus-summary-expire-articles (&optional now)
7561   "Expire all articles that are marked as expirable in the current group."
7562   (interactive)
7563   (when (gnus-check-backend-function
7564          'request-expire-articles gnus-newsgroup-name)
7565     ;; This backend supports expiry.
7566     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7567            (expirable (if total
7568                           (progn
7569                             ;; We need to update the info for
7570                             ;; this group for `gnus-list-of-read-articles'
7571                             ;; to give us the right answer.
7572                             (gnus-run-hooks 'gnus-exit-group-hook)
7573                             (gnus-summary-update-info)
7574                             (gnus-list-of-read-articles gnus-newsgroup-name))
7575                         (setq gnus-newsgroup-expirable
7576                               (sort gnus-newsgroup-expirable '<))))
7577            (expiry-wait (if now 'immediate
7578                           (gnus-group-find-parameter
7579                            gnus-newsgroup-name 'expiry-wait)))
7580            es)
7581       (when expirable
7582         ;; There are expirable articles in this group, so we run them
7583         ;; through the expiry process.
7584         (gnus-message 6 "Expiring articles...")
7585         (unless (gnus-check-group gnus-newsgroup-name)
7586           (error "Can't open server for %s" gnus-newsgroup-name))
7587         ;; The list of articles that weren't expired is returned.
7588         (save-excursion
7589           (if expiry-wait
7590               (let ((nnmail-expiry-wait-function nil)
7591                     (nnmail-expiry-wait expiry-wait))
7592                 (setq es (gnus-request-expire-articles
7593                           expirable gnus-newsgroup-name)))
7594             (setq es (gnus-request-expire-articles
7595                       expirable gnus-newsgroup-name))))
7596         (unless total
7597           (setq gnus-newsgroup-expirable es))
7598         ;; We go through the old list of expirable, and mark all
7599         ;; really expired articles as nonexistent.
7600         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7601           (let ((gnus-use-cache nil))
7602             (while expirable
7603               (unless (memq (car expirable) es)
7604                 (when (gnus-data-find (car expirable))
7605                   (gnus-summary-mark-article
7606                    (car expirable) gnus-canceled-mark)))
7607               (setq expirable (cdr expirable)))))
7608         (gnus-message 6 "Expiring articles...done")))))
7609
7610 (defun gnus-summary-expire-articles-now ()
7611   "Expunge all expirable articles in the current group.
7612 This means that *all* articles that are marked as expirable will be
7613 deleted forever, right now."
7614   (interactive)
7615   (or gnus-expert-user
7616       (gnus-yes-or-no-p
7617        "Are you really, really, really sure you want to delete all these messages? ")
7618       (error "Phew!"))
7619   (gnus-summary-expire-articles t))
7620
7621 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7622 (defun gnus-summary-delete-article (&optional n)
7623   "Delete the N next (mail) articles.
7624 This command actually deletes articles.  This is not a marking
7625 command.  The article will disappear forever from your life, never to
7626 return.
7627 If N is negative, delete backwards.
7628 If N is nil and articles have been marked with the process mark,
7629 delete these instead."
7630   (interactive "P")
7631   (unless (gnus-check-backend-function 'request-expire-articles
7632                                        gnus-newsgroup-name)
7633     (error "The current newsgroup does not support article deletion"))
7634   ;; Compute the list of articles to delete.
7635   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7636         not-deleted)
7637     (if (and gnus-novice-user
7638              (not (gnus-yes-or-no-p
7639                    (format "Do you really want to delete %s forever? "
7640                            (if (> (length articles) 1)
7641                                (format "these %s articles" (length articles))
7642                              "this article")))))
7643         ()
7644       ;; Delete the articles.
7645       (setq not-deleted (gnus-request-expire-articles
7646                          articles gnus-newsgroup-name 'force))
7647       (while articles
7648         (gnus-summary-remove-process-mark (car articles))
7649         ;; The backend might not have been able to delete the article
7650         ;; after all.
7651         (unless (memq (car articles) not-deleted)
7652           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7653         (setq articles (cdr articles)))
7654       (when not-deleted
7655         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7656     (gnus-summary-position-point)
7657     (gnus-set-mode-line 'summary)
7658     not-deleted))
7659
7660 (defun gnus-summary-edit-article (&optional force)
7661   "Edit the current article.
7662 This will have permanent effect only in mail groups.
7663 If FORCE is non-nil, allow editing of articles even in read-only
7664 groups."
7665   (interactive "P")
7666   (save-excursion
7667     (set-buffer gnus-summary-buffer)
7668     (let ((mail-parse-charset gnus-newsgroup-charset)
7669           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7670       (gnus-set-global-variables)
7671       (when (and (not force)
7672                  (gnus-group-read-only-p))
7673         (error "The current newsgroup does not support article editing"))
7674       (gnus-summary-show-article t)
7675       (gnus-article-edit-article
7676        'ignore
7677        `(lambda (no-highlight)
7678           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7679                 (mail-parse-ignored-charsets
7680                  ',gnus-newsgroup-ignored-charsets))
7681             (gnus-summary-edit-article-done
7682              ,(or (mail-header-references gnus-current-headers) "")
7683              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7684
7685 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7686
7687 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7688                                                  no-highlight)
7689   "Make edits to the current article permanent."
7690   (interactive)
7691   ;; Replace the article.
7692   (let ((buf (current-buffer)))
7693     (with-temp-buffer
7694       (insert-buffer-substring buf)
7695       (if (and (not read-only)
7696                (not (gnus-request-replace-article
7697                      (cdr gnus-article-current) (car gnus-article-current)
7698                      (current-buffer) t)))
7699           (error "Couldn't replace article")
7700         ;; Update the summary buffer.
7701         (if (and references
7702                  (equal (message-tokenize-header references " ")
7703                         (message-tokenize-header
7704                          (or (message-fetch-field "references") "") " ")))
7705             ;; We only have to update this line.
7706             (save-excursion
7707               (save-restriction
7708                 (message-narrow-to-head)
7709                 (let ((head (buffer-string))
7710                       header)
7711                   (with-temp-buffer
7712                     (insert (format "211 %d Article retrieved.\n"
7713                                     (cdr gnus-article-current)))
7714                     (insert head)
7715                     (insert ".\n")
7716                     (let ((nntp-server-buffer (current-buffer)))
7717                       (setq header (car (gnus-get-newsgroup-headers
7718                                          (save-excursion
7719                                            (set-buffer gnus-summary-buffer)
7720                                            gnus-newsgroup-dependencies)
7721                                          t))))
7722                     (save-excursion
7723                       (set-buffer gnus-summary-buffer)
7724                       (gnus-data-set-header
7725                        (gnus-data-find (cdr gnus-article-current))
7726                        header)
7727                       (gnus-summary-update-article-line
7728                        (cdr gnus-article-current) header))))))
7729           ;; Update threads.
7730           (set-buffer (or buffer gnus-summary-buffer))
7731           (gnus-summary-update-article (cdr gnus-article-current)))
7732         ;; Prettify the article buffer again.
7733         (unless no-highlight
7734           (save-excursion
7735             (set-buffer gnus-article-buffer)
7736             ;;;!!! Fix this -- article should be rehighlighted.
7737             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7738             (set-buffer gnus-original-article-buffer)
7739             (gnus-request-article
7740              (cdr gnus-article-current)
7741              (car gnus-article-current) (current-buffer))))
7742         ;; Prettify the summary buffer line.
7743         (when (gnus-visual-p 'summary-highlight 'highlight)
7744           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7745
7746 (defun gnus-summary-edit-wash (key)
7747   "Perform editing command KEY in the article buffer."
7748   (interactive
7749    (list
7750     (progn
7751       (message "%s" (concat (this-command-keys) "- "))
7752       (read-char))))
7753   (message "")
7754   (gnus-summary-edit-article)
7755   (execute-kbd-macro (concat (this-command-keys) key))
7756   (gnus-article-edit-done))
7757
7758 ;;; Respooling
7759
7760 (defun gnus-summary-respool-query (&optional silent trace)
7761   "Query where the respool algorithm would put this article."
7762   (interactive)
7763   (let (gnus-mark-article-hook)
7764     (gnus-summary-select-article)
7765     (save-excursion
7766       (set-buffer gnus-original-article-buffer)
7767       (save-restriction
7768         (message-narrow-to-head)
7769         (let ((groups (nnmail-article-group 'identity trace)))
7770           (unless silent
7771             (if groups
7772                 (message "This message would go to %s"
7773                          (mapconcat 'car groups ", "))
7774               (message "This message would go to no groups"))
7775             groups))))))
7776
7777 (defun gnus-summary-respool-trace ()
7778   "Trace where the respool algorithm would put this article.
7779 Display a buffer showing all fancy splitting patterns which matched."
7780   (interactive)
7781   (gnus-summary-respool-query nil t))
7782
7783 ;; Summary marking commands.
7784
7785 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7786   "Mark articles which has the same subject as read, and then select the next.
7787 If UNMARK is positive, remove any kind of mark.
7788 If UNMARK is negative, tick articles."
7789   (interactive "P")
7790   (when unmark
7791     (setq unmark (prefix-numeric-value unmark)))
7792   (let ((count
7793          (gnus-summary-mark-same-subject
7794           (gnus-summary-article-subject) unmark)))
7795     ;; Select next unread article.  If auto-select-same mode, should
7796     ;; select the first unread article.
7797     (gnus-summary-next-article t (and gnus-auto-select-same
7798                                       (gnus-summary-article-subject)))
7799     (gnus-message 7 "%d article%s marked as %s"
7800                   count (if (= count 1) " is" "s are")
7801                   (if unmark "unread" "read"))))
7802
7803 (defun gnus-summary-kill-same-subject (&optional unmark)
7804   "Mark articles which has the same subject as read.
7805 If UNMARK is positive, remove any kind of mark.
7806 If UNMARK is negative, tick articles."
7807   (interactive "P")
7808   (when unmark
7809     (setq unmark (prefix-numeric-value unmark)))
7810   (let ((count
7811          (gnus-summary-mark-same-subject
7812           (gnus-summary-article-subject) unmark)))
7813     ;; If marked as read, go to next unread subject.
7814     (when (null unmark)
7815       ;; Go to next unread subject.
7816       (gnus-summary-next-subject 1 t))
7817     (gnus-message 7 "%d articles are marked as %s"
7818                   count (if unmark "unread" "read"))))
7819
7820 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7821   "Mark articles with same SUBJECT as read, and return marked number.
7822 If optional argument UNMARK is positive, remove any kinds of marks.
7823 If optional argument UNMARK is negative, mark articles as unread instead."
7824   (let ((count 1))
7825     (save-excursion
7826       (cond
7827        ((null unmark)                   ; Mark as read.
7828         (while (and
7829                 (progn
7830                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7831                   (gnus-summary-show-thread) t)
7832                 (gnus-summary-find-subject subject))
7833           (setq count (1+ count))))
7834        ((> unmark 0)                    ; Tick.
7835         (while (and
7836                 (progn
7837                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7838                   (gnus-summary-show-thread) t)
7839                 (gnus-summary-find-subject subject))
7840           (setq count (1+ count))))
7841        (t                               ; Mark as unread.
7842         (while (and
7843                 (progn
7844                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7845                   (gnus-summary-show-thread) t)
7846                 (gnus-summary-find-subject subject))
7847           (setq count (1+ count)))))
7848       (gnus-set-mode-line 'summary)
7849       ;; Return the number of marked articles.
7850       count)))
7851
7852 (defun gnus-summary-mark-as-processable (n &optional unmark)
7853   "Set the process mark on the next N articles.
7854 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7855 the process mark instead.  The difference between N and the actual
7856 number of articles marked is returned."
7857   (interactive "p")
7858   (let ((backward (< n 0))
7859         (n (abs n)))
7860     (while (and
7861             (> n 0)
7862             (if unmark
7863                 (gnus-summary-remove-process-mark
7864                  (gnus-summary-article-number))
7865               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7866             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7867       (setq n (1- n)))
7868     (when (/= 0 n)
7869       (gnus-message 7 "No more articles"))
7870     (gnus-summary-recenter)
7871     (gnus-summary-position-point)
7872     n))
7873
7874 (defun gnus-summary-unmark-as-processable (n)
7875   "Remove the process mark from the next N articles.
7876 If N is negative, unmark backward instead.  The difference between N and
7877 the actual number of articles unmarked is returned."
7878   (interactive "p")
7879   (gnus-summary-mark-as-processable n t))
7880
7881 (defun gnus-summary-unmark-all-processable ()
7882   "Remove the process mark from all articles."
7883   (interactive)
7884   (save-excursion
7885     (while gnus-newsgroup-processable
7886       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7887   (gnus-summary-position-point))
7888
7889 (defun gnus-summary-mark-as-expirable (n)
7890   "Mark N articles forward as expirable.
7891 If N is negative, mark backward instead.  The difference between N and
7892 the actual number of articles marked is returned."
7893   (interactive "p")
7894   (gnus-summary-mark-forward n gnus-expirable-mark))
7895
7896 (defun gnus-summary-mark-article-as-replied (article)
7897   "Mark ARTICLE replied and update the summary line."
7898   (push article gnus-newsgroup-replied)
7899   (let ((buffer-read-only nil))
7900     (when (gnus-summary-goto-subject article nil t)
7901       (gnus-summary-update-secondary-mark article))))
7902
7903 (defun gnus-summary-set-bookmark (article)
7904   "Set a bookmark in current article."
7905   (interactive (list (gnus-summary-article-number)))
7906   (when (or (not (get-buffer gnus-article-buffer))
7907             (not gnus-current-article)
7908             (not gnus-article-current)
7909             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7910     (error "No current article selected"))
7911   ;; Remove old bookmark, if one exists.
7912   (let ((old (assq article gnus-newsgroup-bookmarks)))
7913     (when old
7914       (setq gnus-newsgroup-bookmarks
7915             (delq old gnus-newsgroup-bookmarks))))
7916   ;; Set the new bookmark, which is on the form
7917   ;; (article-number . line-number-in-body).
7918   (push
7919    (cons article
7920          (save-excursion
7921            (set-buffer gnus-article-buffer)
7922            (count-lines
7923             (min (point)
7924                  (save-excursion
7925                    (goto-char (point-min))
7926                    (search-forward "\n\n" nil t)
7927                    (point)))
7928             (point))))
7929    gnus-newsgroup-bookmarks)
7930   (gnus-message 6 "A bookmark has been added to the current article."))
7931
7932 (defun gnus-summary-remove-bookmark (article)
7933   "Remove the bookmark from the current article."
7934   (interactive (list (gnus-summary-article-number)))
7935   ;; Remove old bookmark, if one exists.
7936   (let ((old (assq article gnus-newsgroup-bookmarks)))
7937     (if old
7938         (progn
7939           (setq gnus-newsgroup-bookmarks
7940                 (delq old gnus-newsgroup-bookmarks))
7941           (gnus-message 6 "Removed bookmark."))
7942       (gnus-message 6 "No bookmark in current article."))))
7943
7944 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7945 (defun gnus-summary-mark-as-dormant (n)
7946   "Mark N articles forward as dormant.
7947 If N is negative, mark backward instead.  The difference between N and
7948 the actual number of articles marked is returned."
7949   (interactive "p")
7950   (gnus-summary-mark-forward n gnus-dormant-mark))
7951
7952 (defun gnus-summary-set-process-mark (article)
7953   "Set the process mark on ARTICLE and update the summary line."
7954   (setq gnus-newsgroup-processable
7955         (cons article
7956               (delq article gnus-newsgroup-processable)))
7957   (when (gnus-summary-goto-subject article)
7958     (gnus-summary-show-thread)
7959     (gnus-summary-goto-subject article)
7960     (gnus-summary-update-secondary-mark article)))
7961
7962 (defun gnus-summary-remove-process-mark (article)
7963   "Remove the process mark from ARTICLE and update the summary line."
7964   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7965   (when (gnus-summary-goto-subject article)
7966     (gnus-summary-show-thread)
7967     (gnus-summary-goto-subject article)
7968     (gnus-summary-update-secondary-mark article)))
7969
7970 (defun gnus-summary-set-saved-mark (article)
7971   "Set the process mark on ARTICLE and update the summary line."
7972   (push article gnus-newsgroup-saved)
7973   (when (gnus-summary-goto-subject article)
7974     (gnus-summary-update-secondary-mark article)))
7975
7976 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7977   "Mark N articles as read forwards.
7978 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7979 The difference between N and the actual number of articles marked is
7980 returned."
7981   (interactive "p")
7982   (gnus-summary-show-thread)
7983   (let ((backward (< n 0))
7984         (gnus-summary-goto-unread
7985          (and gnus-summary-goto-unread
7986               (not (eq gnus-summary-goto-unread 'never))
7987               (not (memq mark (list gnus-unread-mark
7988                                     gnus-ticked-mark gnus-dormant-mark)))))
7989         (n (abs n))
7990         (mark (or mark gnus-del-mark)))
7991     (while (and (> n 0)
7992                 (gnus-summary-mark-article nil mark no-expire)
7993                 (zerop (gnus-summary-next-subject
7994                         (if backward -1 1)
7995                         (and gnus-summary-goto-unread
7996                              (not (eq gnus-summary-goto-unread 'never)))
7997                         t)))
7998       (setq n (1- n)))
7999     (when (/= 0 n)
8000       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8001     (gnus-summary-recenter)
8002     (gnus-summary-position-point)
8003     (gnus-set-mode-line 'summary)
8004     n))
8005
8006 (defun gnus-summary-mark-article-as-read (mark)
8007   "Mark the current article quickly as read with MARK."
8008   (let ((article (gnus-summary-article-number)))
8009     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8010     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8011     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8012     (push (cons article mark) gnus-newsgroup-reads)
8013     ;; Possibly remove from cache, if that is used.
8014     (when gnus-use-cache
8015       (gnus-cache-enter-remove-article article))
8016     ;; Allow the backend to change the mark.
8017     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8018     ;; Check for auto-expiry.
8019     (when (and gnus-newsgroup-auto-expire
8020                (memq mark gnus-auto-expirable-marks))
8021       (setq mark gnus-expirable-mark)
8022       ;; Let the backend know about the mark change.
8023       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8024       (push article gnus-newsgroup-expirable))
8025     ;; Set the mark in the buffer.
8026     (gnus-summary-update-mark mark 'unread)
8027     t))
8028
8029 (defun gnus-summary-mark-article-as-unread (mark)
8030   "Mark the current article quickly as unread with MARK."
8031   (let* ((article (gnus-summary-article-number))
8032          (old-mark (gnus-summary-article-mark article)))
8033     ;; Allow the backend to change the mark.
8034     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8035     (if (eq mark old-mark)
8036         t
8037       (if (<= article 0)
8038           (progn
8039             (gnus-error 1 "Can't mark negative article numbers")
8040             nil)
8041         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8042         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8043         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8044         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8045         (cond ((= mark gnus-ticked-mark)
8046                (push article gnus-newsgroup-marked))
8047               ((= mark gnus-dormant-mark)
8048                (push article gnus-newsgroup-dormant))
8049               (t
8050                (push article gnus-newsgroup-unreads)))
8051         (gnus-pull article gnus-newsgroup-reads)
8052
8053         ;; See whether the article is to be put in the cache.
8054         (and gnus-use-cache
8055              (vectorp (gnus-summary-article-header article))
8056              (save-excursion
8057                (gnus-cache-possibly-enter-article
8058                 gnus-newsgroup-name article
8059                 (gnus-summary-article-header article)
8060                 (= mark gnus-ticked-mark)
8061                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8062
8063         ;; Fix the mark.
8064         (gnus-summary-update-mark mark 'unread)
8065         t))))
8066
8067 (defun gnus-summary-mark-article (&optional article mark no-expire)
8068   "Mark ARTICLE with MARK.  MARK can be any character.
8069 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8070 `??' (dormant) and `?E' (expirable).
8071 If MARK is nil, then the default character `?r' is used.
8072 If ARTICLE is nil, then the article on the current line will be
8073 marked."
8074   ;; The mark might be a string.
8075   (when (stringp mark)
8076     (setq mark (aref mark 0)))
8077   ;; If no mark is given, then we check auto-expiring.
8078   (when (null mark)
8079     (setq mark gnus-del-mark))
8080   (when (and (not no-expire)
8081              gnus-newsgroup-auto-expire
8082              (memq mark gnus-auto-expirable-marks))
8083     (setq mark gnus-expirable-mark))
8084   (let ((article (or article (gnus-summary-article-number)))
8085         (old-mark (gnus-summary-article-mark article)))
8086     ;; Allow the backend to change the mark.
8087     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8088     (if (eq mark old-mark)
8089         t
8090       (unless article
8091         (error "No article on current line"))
8092       (if (not (if (or (= mark gnus-unread-mark)
8093                        (= mark gnus-ticked-mark)
8094                        (= mark gnus-dormant-mark))
8095                    (gnus-mark-article-as-unread article mark)
8096                  (gnus-mark-article-as-read article mark)))
8097           t
8098         ;; See whether the article is to be put in the cache.
8099         (and gnus-use-cache
8100              (not (= mark gnus-canceled-mark))
8101              (vectorp (gnus-summary-article-header article))
8102              (save-excursion
8103                (gnus-cache-possibly-enter-article
8104                 gnus-newsgroup-name article
8105                 (gnus-summary-article-header article)
8106                 (= mark gnus-ticked-mark)
8107                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8108
8109         (when (gnus-summary-goto-subject article nil t)
8110           (let ((buffer-read-only nil))
8111             (gnus-summary-show-thread)
8112             ;; Fix the mark.
8113             (gnus-summary-update-mark mark 'unread)
8114             t))))))
8115
8116 (defun gnus-summary-update-secondary-mark (article)
8117   "Update the secondary (read, process, cache) mark."
8118   (gnus-summary-update-mark
8119    (cond ((memq article gnus-newsgroup-processable)
8120           gnus-process-mark)
8121          ((memq article gnus-newsgroup-cached)
8122           gnus-cached-mark)
8123          ((memq article gnus-newsgroup-replied)
8124           gnus-replied-mark)
8125          ((memq article gnus-newsgroup-saved)
8126           gnus-saved-mark)
8127          (t gnus-unread-mark))
8128    'replied)
8129   (when (gnus-visual-p 'summary-highlight 'highlight)
8130     (gnus-run-hooks 'gnus-summary-update-hook))
8131   t)
8132
8133 (defun gnus-summary-update-mark (mark type)
8134   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8135         (buffer-read-only nil))
8136     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8137     (when forward
8138       (when (looking-at "\r")
8139         (incf forward))
8140       (when (<= (+ forward (point)) (point-max))
8141         ;; Go to the right position on the line.
8142         (goto-char (+ forward (point)))
8143         ;; Replace the old mark with the new mark.
8144         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8145         ;; Optionally update the marks by some user rule.
8146         (when (eq type 'unread)
8147           (gnus-data-set-mark
8148            (gnus-data-find (gnus-summary-article-number)) mark)
8149           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8150
8151 (defun gnus-mark-article-as-read (article &optional mark)
8152   "Enter ARTICLE in the pertinent lists and remove it from others."
8153   ;; Make the article expirable.
8154   (let ((mark (or mark gnus-del-mark)))
8155     (if (= mark gnus-expirable-mark)
8156         (push article gnus-newsgroup-expirable)
8157       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8158     ;; Remove from unread and marked lists.
8159     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8160     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8161     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8162     (push (cons article mark) gnus-newsgroup-reads)
8163     ;; Possibly remove from cache, if that is used.
8164     (when gnus-use-cache
8165       (gnus-cache-enter-remove-article article))
8166     t))
8167
8168 (defun gnus-mark-article-as-unread (article &optional mark)
8169   "Enter ARTICLE in the pertinent lists and remove it from others."
8170   (let ((mark (or mark gnus-ticked-mark)))
8171     (if (<= article 0)
8172         (progn
8173           (gnus-error 1 "Can't mark negative article numbers")
8174           nil)
8175       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8176             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8177             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8178             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8179
8180       ;; Unsuppress duplicates?
8181       (when gnus-suppress-duplicates
8182         (gnus-dup-unsuppress-article article))
8183
8184       (cond ((= mark gnus-ticked-mark)
8185              (push article gnus-newsgroup-marked))
8186             ((= mark gnus-dormant-mark)
8187              (push article gnus-newsgroup-dormant))
8188             (t
8189              (push article gnus-newsgroup-unreads)))
8190       (gnus-pull article gnus-newsgroup-reads)
8191       t)))
8192
8193 (defalias 'gnus-summary-mark-as-unread-forward
8194   'gnus-summary-tick-article-forward)
8195 (make-obsolete 'gnus-summary-mark-as-unread-forward
8196                'gnus-summary-tick-article-forward)
8197 (defun gnus-summary-tick-article-forward (n)
8198   "Tick N articles forwards.
8199 If N is negative, tick backwards instead.
8200 The difference between N and the number of articles ticked is returned."
8201   (interactive "p")
8202   (gnus-summary-mark-forward n gnus-ticked-mark))
8203
8204 (defalias 'gnus-summary-mark-as-unread-backward
8205   'gnus-summary-tick-article-backward)
8206 (make-obsolete 'gnus-summary-mark-as-unread-backward
8207                'gnus-summary-tick-article-backward)
8208 (defun gnus-summary-tick-article-backward (n)
8209   "Tick N articles backwards.
8210 The difference between N and the number of articles ticked is returned."
8211   (interactive "p")
8212   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8213
8214 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8215 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8216 (defun gnus-summary-tick-article (&optional article clear-mark)
8217   "Mark current article as unread.
8218 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8219 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8220   (interactive)
8221   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8222                                        gnus-ticked-mark)))
8223
8224 (defun gnus-summary-mark-as-read-forward (n)
8225   "Mark N articles as read forwards.
8226 If N is negative, mark backwards instead.
8227 The difference between N and the actual number of articles marked is
8228 returned."
8229   (interactive "p")
8230   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8231
8232 (defun gnus-summary-mark-as-read-backward (n)
8233   "Mark the N articles as read backwards.
8234 The difference between N and the actual number of articles marked is
8235 returned."
8236   (interactive "p")
8237   (gnus-summary-mark-forward
8238    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8239
8240 (defun gnus-summary-mark-as-read (&optional article mark)
8241   "Mark current article as read.
8242 ARTICLE specifies the article to be marked as read.
8243 MARK specifies a string to be inserted at the beginning of the line."
8244   (gnus-summary-mark-article article mark))
8245
8246 (defun gnus-summary-clear-mark-forward (n)
8247   "Clear marks from N articles forward.
8248 If N is negative, clear backward instead.
8249 The difference between N and the number of marks cleared is returned."
8250   (interactive "p")
8251   (gnus-summary-mark-forward n gnus-unread-mark))
8252
8253 (defun gnus-summary-clear-mark-backward (n)
8254   "Clear marks from N articles backward.
8255 The difference between N and the number of marks cleared is returned."
8256   (interactive "p")
8257   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8258
8259 (defun gnus-summary-mark-unread-as-read ()
8260   "Intended to be used by `gnus-summary-mark-article-hook'."
8261   (when (memq gnus-current-article gnus-newsgroup-unreads)
8262     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8263
8264 (defun gnus-summary-mark-read-and-unread-as-read ()
8265   "Intended to be used by `gnus-summary-mark-article-hook'."
8266   (let ((mark (gnus-summary-article-mark)))
8267     (when (or (gnus-unread-mark-p mark)
8268               (gnus-read-mark-p mark))
8269       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8270
8271 (defun gnus-summary-mark-region-as-read (point mark all)
8272   "Mark all unread articles between point and mark as read.
8273 If given a prefix, mark all articles between point and mark as read,
8274 even ticked and dormant ones."
8275   (interactive "r\nP")
8276   (save-excursion
8277     (let (article)
8278       (goto-char point)
8279       (beginning-of-line)
8280       (while (and
8281               (< (point) mark)
8282               (progn
8283                 (when (or all
8284                           (memq (setq article (gnus-summary-article-number))
8285                                 gnus-newsgroup-unreads))
8286                   (gnus-summary-mark-article article gnus-del-mark))
8287                 t)
8288               (gnus-summary-find-next))))))
8289
8290 (defun gnus-summary-mark-below (score mark)
8291   "Mark articles with score less than SCORE with MARK."
8292   (interactive "P\ncMark: ")
8293   (setq score (if score
8294                   (prefix-numeric-value score)
8295                 (or gnus-summary-default-score 0)))
8296   (save-excursion
8297     (set-buffer gnus-summary-buffer)
8298     (goto-char (point-min))
8299     (while
8300         (progn
8301           (and (< (gnus-summary-article-score) score)
8302                (gnus-summary-mark-article nil mark))
8303           (gnus-summary-find-next)))))
8304
8305 (defun gnus-summary-kill-below (&optional score)
8306   "Mark articles with score below SCORE as read."
8307   (interactive "P")
8308   (gnus-summary-mark-below score gnus-killed-mark))
8309
8310 (defun gnus-summary-clear-above (&optional score)
8311   "Clear all marks from articles with score above SCORE."
8312   (interactive "P")
8313   (gnus-summary-mark-above score gnus-unread-mark))
8314
8315 (defun gnus-summary-tick-above (&optional score)
8316   "Tick all articles with score above SCORE."
8317   (interactive "P")
8318   (gnus-summary-mark-above score gnus-ticked-mark))
8319
8320 (defun gnus-summary-mark-above (score mark)
8321   "Mark articles with score over SCORE with MARK."
8322   (interactive "P\ncMark: ")
8323   (setq score (if score
8324                   (prefix-numeric-value score)
8325                 (or gnus-summary-default-score 0)))
8326   (save-excursion
8327     (set-buffer gnus-summary-buffer)
8328     (goto-char (point-min))
8329     (while (and (progn
8330                   (when (> (gnus-summary-article-score) score)
8331                     (gnus-summary-mark-article nil mark))
8332                   t)
8333                 (gnus-summary-find-next)))))
8334
8335 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8336 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8337 (defun gnus-summary-limit-include-expunged (&optional no-error)
8338   "Display all the hidden articles that were expunged for low scores."
8339   (interactive)
8340   (let ((buffer-read-only nil))
8341     (let ((scored gnus-newsgroup-scored)
8342           headers h)
8343       (while scored
8344         (unless (gnus-summary-goto-subject (caar scored))
8345           (and (setq h (gnus-summary-article-header (caar scored)))
8346                (< (cdar scored) gnus-summary-expunge-below)
8347                (push h headers)))
8348         (setq scored (cdr scored)))
8349       (if (not headers)
8350           (when (not no-error)
8351             (error "No expunged articles hidden"))
8352         (goto-char (point-min))
8353         (gnus-summary-prepare-unthreaded (nreverse headers))
8354         (goto-char (point-min))
8355         (gnus-summary-position-point)
8356         t))))
8357
8358 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8359   "Mark all unread articles in this newsgroup as read.
8360 If prefix argument ALL is non-nil, ticked and dormant articles will
8361 also be marked as read.
8362 If QUIETLY is non-nil, no questions will be asked.
8363 If TO-HERE is non-nil, it should be a point in the buffer.  All
8364 articles before this point will be marked as read.
8365 Note that this function will only catch up the unread article
8366 in the current summary buffer limitation.
8367 The number of articles marked as read is returned."
8368   (interactive "P")
8369   (prog1
8370       (save-excursion
8371         (when (or quietly
8372                   (not gnus-interactive-catchup) ;Without confirmation?
8373                   gnus-expert-user
8374                   (gnus-y-or-n-p
8375                    (if all
8376                        "Mark absolutely all articles as read? "
8377                      "Mark all unread articles as read? ")))
8378           (if (and not-mark
8379                    (not gnus-newsgroup-adaptive)
8380                    (not gnus-newsgroup-auto-expire)
8381                    (not gnus-suppress-duplicates)
8382                    (or (not gnus-use-cache)
8383                        (eq gnus-use-cache 'passive)))
8384               (progn
8385                 (when all
8386                   (setq gnus-newsgroup-marked nil
8387                         gnus-newsgroup-dormant nil))
8388                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8389             ;; We actually mark all articles as canceled, which we
8390             ;; have to do when using auto-expiry or adaptive scoring.
8391             (gnus-summary-show-all-threads)
8392             (when (gnus-summary-first-subject (not all) t)
8393               (while (and
8394                       (if to-here (< (point) to-here) t)
8395                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8396                       (gnus-summary-find-next (not all) nil nil t))))
8397             (gnus-set-mode-line 'summary))
8398           t))
8399     (gnus-summary-position-point)))
8400
8401 (defun gnus-summary-catchup-to-here (&optional all)
8402   "Mark all unticked articles before the current one as read.
8403 If ALL is non-nil, also mark ticked and dormant articles as read."
8404   (interactive "P")
8405   (save-excursion
8406     (gnus-save-hidden-threads
8407       (let ((beg (point)))
8408         ;; We check that there are unread articles.
8409         (when (or all (gnus-summary-find-prev))
8410           (gnus-summary-catchup all t beg)))))
8411   (gnus-summary-position-point))
8412
8413 (defun gnus-summary-catchup-all (&optional quietly)
8414   "Mark all articles in this newsgroup as read."
8415   (interactive "P")
8416   (gnus-summary-catchup t quietly))
8417
8418 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8419   "Mark all unread articles in this group as read, then exit.
8420 If prefix argument ALL is non-nil, all articles are marked as read."
8421   (interactive "P")
8422   (when (gnus-summary-catchup all quietly nil 'fast)
8423     ;; Select next newsgroup or exit.
8424     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8425              (eq gnus-auto-select-next 'quietly))
8426         (gnus-summary-next-group nil)
8427       (gnus-summary-exit))))
8428
8429 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8430   "Mark all articles in this newsgroup as read, and then exit."
8431   (interactive "P")
8432   (gnus-summary-catchup-and-exit t quietly))
8433
8434 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8435 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8436   "Mark all articles in this group as read and select the next group.
8437 If given a prefix, mark all articles, unread as well as ticked, as
8438 read."
8439   (interactive "P")
8440   (save-excursion
8441     (gnus-summary-catchup all))
8442   (gnus-summary-next-article t nil nil t))
8443
8444 ;; Thread-based commands.
8445
8446 (defun gnus-summary-articles-in-thread (&optional article)
8447   "Return a list of all articles in the current thread.
8448 If ARTICLE is non-nil, return all articles in the thread that starts
8449 with that article."
8450   (let* ((article (or article (gnus-summary-article-number)))
8451          (data (gnus-data-find-list article))
8452          (top-level (gnus-data-level (car data)))
8453          (top-subject
8454           (cond ((null gnus-thread-operation-ignore-subject)
8455                  (gnus-simplify-subject-re
8456                   (mail-header-subject (gnus-data-header (car data)))))
8457                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8458                  (gnus-simplify-subject-fuzzy
8459                   (mail-header-subject (gnus-data-header (car data)))))
8460                 (t nil)))
8461          (end-point (save-excursion
8462                       (if (gnus-summary-go-to-next-thread)
8463                           (point) (point-max))))
8464          articles)
8465     (while (and data
8466                 (< (gnus-data-pos (car data)) end-point))
8467       (when (or (not top-subject)
8468                 (string= top-subject
8469                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8470                              (gnus-simplify-subject-fuzzy
8471                               (mail-header-subject
8472                                (gnus-data-header (car data))))
8473                            (gnus-simplify-subject-re
8474                             (mail-header-subject
8475                              (gnus-data-header (car data)))))))
8476         (push (gnus-data-number (car data)) articles))
8477       (unless (and (setq data (cdr data))
8478                    (> (gnus-data-level (car data)) top-level))
8479         (setq data nil)))
8480     ;; Return the list of articles.
8481     (nreverse articles)))
8482
8483 (defun gnus-summary-rethread-current ()
8484   "Rethread the thread the current article is part of."
8485   (interactive)
8486   (let* ((gnus-show-threads t)
8487          (article (gnus-summary-article-number))
8488          (id (mail-header-id (gnus-summary-article-header)))
8489          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8490     (unless id
8491       (error "No article on the current line"))
8492     (gnus-rebuild-thread id)
8493     (gnus-summary-goto-subject article)))
8494
8495 (defun gnus-summary-reparent-thread ()
8496   "Make the current article child of the marked (or previous) article.
8497
8498 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8499 is non-nil or the Subject: of both articles are the same."
8500   (interactive)
8501   (unless (not (gnus-group-read-only-p))
8502     (error "The current newsgroup does not support article editing"))
8503   (unless (<= (length gnus-newsgroup-processable) 1)
8504     (error "No more than one article may be marked"))
8505   (save-window-excursion
8506     (let ((gnus-article-buffer " *reparent*")
8507           (current-article (gnus-summary-article-number))
8508           ;; First grab the marked article, otherwise one line up.
8509           (parent-article (if (not (null gnus-newsgroup-processable))
8510                               (car gnus-newsgroup-processable)
8511                             (save-excursion
8512                               (if (eq (forward-line -1) 0)
8513                                   (gnus-summary-article-number)
8514                                 (error "Beginning of summary buffer"))))))
8515       (unless (not (eq current-article parent-article))
8516         (error "An article may not be self-referential"))
8517       (let ((message-id (mail-header-id
8518                          (gnus-summary-article-header parent-article))))
8519         (unless (and message-id (not (equal message-id "")))
8520           (error "No message-id in desired parent"))
8521         (gnus-with-article current-article
8522           (goto-char (point-min))
8523           (if (re-search-forward "^References: " nil t)
8524               (progn
8525                 (re-search-forward "^[^ \t]" nil t)
8526                 (forward-line -1)
8527                 (end-of-line)
8528                 (insert " " message-id))
8529             (insert "References: " message-id "\n")))
8530         (set-buffer gnus-summary-buffer)
8531         (gnus-summary-unmark-all-processable)
8532         (gnus-summary-update-article current-article)
8533         (gnus-summary-rethread-current)
8534         (gnus-message 3 "Article %d is now the child of article %d"
8535                       current-article parent-article)))))
8536
8537 (defun gnus-summary-toggle-threads (&optional arg)
8538   "Toggle showing conversation threads.
8539 If ARG is positive number, turn showing conversation threads on."
8540   (interactive "P")
8541   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8542     (setq gnus-show-threads
8543           (if (null arg) (not gnus-show-threads)
8544             (> (prefix-numeric-value arg) 0)))
8545     (gnus-summary-prepare)
8546     (gnus-summary-goto-subject current)
8547     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8548     (gnus-summary-position-point)))
8549
8550 (defun gnus-summary-show-all-threads ()
8551   "Show all threads."
8552   (interactive)
8553   (save-excursion
8554     (let ((buffer-read-only nil))
8555       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8556   (gnus-summary-position-point))
8557
8558 (defun gnus-summary-show-thread ()
8559   "Show thread subtrees.
8560 Returns nil if no thread was there to be shown."
8561   (interactive)
8562   (let ((buffer-read-only nil)
8563         (orig (point))
8564         ;; first goto end then to beg, to have point at beg after let
8565         (end (progn (end-of-line) (point)))
8566         (beg (progn (beginning-of-line) (point))))
8567     (prog1
8568         ;; Any hidden lines here?
8569         (search-forward "\r" end t)
8570       (subst-char-in-region beg end ?\^M ?\n t)
8571       (goto-char orig)
8572       (gnus-summary-position-point))))
8573
8574 (defun gnus-summary-hide-all-threads ()
8575   "Hide all thread subtrees."
8576   (interactive)
8577   (save-excursion
8578     (goto-char (point-min))
8579     (gnus-summary-hide-thread)
8580     (while (zerop (gnus-summary-next-thread 1 t))
8581       (gnus-summary-hide-thread)))
8582   (gnus-summary-position-point))
8583
8584 (defun gnus-summary-hide-thread ()
8585   "Hide thread subtrees.
8586 Returns nil if no threads were there to be hidden."
8587   (interactive)
8588   (let ((buffer-read-only nil)
8589         (start (point))
8590         (article (gnus-summary-article-number)))
8591     (goto-char start)
8592     ;; Go forward until either the buffer ends or the subthread
8593     ;; ends.
8594     (when (and (not (eobp))
8595                (or (zerop (gnus-summary-next-thread 1 t))
8596                    (goto-char (point-max))))
8597       (prog1
8598           (if (and (> (point) start)
8599                    (search-backward "\n" start t))
8600               (progn
8601                 (subst-char-in-region start (point) ?\n ?\^M)
8602                 (gnus-summary-goto-subject article))
8603             (goto-char start)
8604             nil)
8605         ;;(gnus-summary-position-point)
8606         ))))
8607
8608 (defun gnus-summary-go-to-next-thread (&optional previous)
8609   "Go to the same level (or less) next thread.
8610 If PREVIOUS is non-nil, go to previous thread instead.
8611 Return the article number moved to, or nil if moving was impossible."
8612   (let ((level (gnus-summary-thread-level))
8613         (way (if previous -1 1))
8614         (beg (point)))
8615     (forward-line way)
8616     (while (and (not (eobp))
8617                 (< level (gnus-summary-thread-level)))
8618       (forward-line way))
8619     (if (eobp)
8620         (progn
8621           (goto-char beg)
8622           nil)
8623       (setq beg (point))
8624       (prog1
8625           (gnus-summary-article-number)
8626         (goto-char beg)))))
8627
8628 (defun gnus-summary-next-thread (n &optional silent)
8629   "Go to the same level next N'th thread.
8630 If N is negative, search backward instead.
8631 Returns the difference between N and the number of skips actually
8632 done.
8633
8634 If SILENT, don't output messages."
8635   (interactive "p")
8636   (let ((backward (< n 0))
8637         (n (abs n)))
8638     (while (and (> n 0)
8639                 (gnus-summary-go-to-next-thread backward))
8640       (decf n))
8641     (unless silent
8642       (gnus-summary-position-point))
8643     (when (and (not silent) (/= 0 n))
8644       (gnus-message 7 "No more threads"))
8645     n))
8646
8647 (defun gnus-summary-prev-thread (n)
8648   "Go to the same level previous N'th thread.
8649 Returns the difference between N and the number of skips actually
8650 done."
8651   (interactive "p")
8652   (gnus-summary-next-thread (- n)))
8653
8654 (defun gnus-summary-go-down-thread ()
8655   "Go down one level in the current thread."
8656   (let ((children (gnus-summary-article-children)))
8657     (when children
8658       (gnus-summary-goto-subject (car children)))))
8659
8660 (defun gnus-summary-go-up-thread ()
8661   "Go up one level in the current thread."
8662   (let ((parent (gnus-summary-article-parent)))
8663     (when parent
8664       (gnus-summary-goto-subject parent))))
8665
8666 (defun gnus-summary-down-thread (n)
8667   "Go down thread N steps.
8668 If N is negative, go up instead.
8669 Returns the difference between N and how many steps down that were
8670 taken."
8671   (interactive "p")
8672   (let ((up (< n 0))
8673         (n (abs n)))
8674     (while (and (> n 0)
8675                 (if up (gnus-summary-go-up-thread)
8676                   (gnus-summary-go-down-thread)))
8677       (setq n (1- n)))
8678     (gnus-summary-position-point)
8679     (when (/= 0 n)
8680       (gnus-message 7 "Can't go further"))
8681     n))
8682
8683 (defun gnus-summary-up-thread (n)
8684   "Go up thread N steps.
8685 If N is negative, go up instead.
8686 Returns the difference between N and how many steps down that were
8687 taken."
8688   (interactive "p")
8689   (gnus-summary-down-thread (- n)))
8690
8691 (defun gnus-summary-top-thread ()
8692   "Go to the top of the thread."
8693   (interactive)
8694   (while (gnus-summary-go-up-thread))
8695   (gnus-summary-article-number))
8696
8697 (defun gnus-summary-kill-thread (&optional unmark)
8698   "Mark articles under current thread as read.
8699 If the prefix argument is positive, remove any kinds of marks.
8700 If the prefix argument is negative, tick articles instead."
8701   (interactive "P")
8702   (when unmark
8703     (setq unmark (prefix-numeric-value unmark)))
8704   (let ((articles (gnus-summary-articles-in-thread)))
8705     (save-excursion
8706       ;; Expand the thread.
8707       (gnus-summary-show-thread)
8708       ;; Mark all the articles.
8709       (while articles
8710         (gnus-summary-goto-subject (car articles))
8711         (cond ((null unmark)
8712                (gnus-summary-mark-article-as-read gnus-killed-mark))
8713               ((> unmark 0)
8714                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8715               (t
8716                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8717         (setq articles (cdr articles))))
8718     ;; Hide killed subtrees.
8719     (and (null unmark)
8720          gnus-thread-hide-killed
8721          (gnus-summary-hide-thread))
8722     ;; If marked as read, go to next unread subject.
8723     (when (null unmark)
8724       ;; Go to next unread subject.
8725       (gnus-summary-next-subject 1 t)))
8726   (gnus-set-mode-line 'summary))
8727
8728 ;; Summary sorting commands
8729
8730 (defun gnus-summary-sort-by-number (&optional reverse)
8731   "Sort the summary buffer by article number.
8732 Argument REVERSE means reverse order."
8733   (interactive "P")
8734   (gnus-summary-sort 'number reverse))
8735
8736 (defun gnus-summary-sort-by-author (&optional reverse)
8737   "Sort the summary buffer by author name alphabetically.
8738 If case-fold-search is non-nil, case of letters is ignored.
8739 Argument REVERSE means reverse order."
8740   (interactive "P")
8741   (gnus-summary-sort 'author reverse))
8742
8743 (defun gnus-summary-sort-by-subject (&optional reverse)
8744   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8745 If case-fold-search is non-nil, case of letters is ignored.
8746 Argument REVERSE means reverse order."
8747   (interactive "P")
8748   (gnus-summary-sort 'subject reverse))
8749
8750 (defun gnus-summary-sort-by-date (&optional reverse)
8751   "Sort the summary buffer by date.
8752 Argument REVERSE means reverse order."
8753   (interactive "P")
8754   (gnus-summary-sort 'date reverse))
8755
8756 (defun gnus-summary-sort-by-score (&optional reverse)
8757   "Sort the summary buffer by score.
8758 Argument REVERSE means reverse order."
8759   (interactive "P")
8760   (gnus-summary-sort 'score reverse))
8761
8762 (defun gnus-summary-sort-by-lines (&optional reverse)
8763   "Sort the summary buffer by the number of lines.
8764 Argument REVERSE means reverse order."
8765   (interactive "P")
8766   (gnus-summary-sort 'lines reverse))
8767
8768 (defun gnus-summary-sort-by-chars (&optional reverse)
8769   "Sort the summary buffer by article length.
8770 Argument REVERSE means reverse order."
8771   (interactive "P")
8772   (gnus-summary-sort 'chars reverse))   
8773
8774 (defun gnus-summary-sort (predicate reverse)
8775   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8776   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8777          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8778          (gnus-thread-sort-functions
8779           (if (not reverse)
8780               thread
8781             `(lambda (t1 t2)
8782                (,thread t2 t1))))
8783          (gnus-article-sort-functions
8784           (if (not reverse)
8785               article
8786             `(lambda (t1 t2)
8787                (,article t2 t1))))
8788          (buffer-read-only)
8789          (gnus-summary-prepare-hook nil))
8790     ;; We do the sorting by regenerating the threads.
8791     (gnus-summary-prepare)
8792     ;; Hide subthreads if needed.
8793     (when (and gnus-show-threads gnus-thread-hide-subtree)
8794       (gnus-summary-hide-all-threads))))
8795
8796 ;; Summary saving commands.
8797
8798 (defun gnus-summary-save-article (&optional n not-saved)
8799   "Save the current article using the default saver function.
8800 If N is a positive number, save the N next articles.
8801 If N is a negative number, save the N previous articles.
8802 If N is nil and any articles have been marked with the process mark,
8803 save those articles instead.
8804 The variable `gnus-default-article-saver' specifies the saver function."
8805   (interactive "P")
8806   (let* ((articles (gnus-summary-work-articles n))
8807          (save-buffer (save-excursion
8808                         (nnheader-set-temp-buffer " *Gnus Save*")))
8809          (num (length articles))
8810          header file)
8811     (dolist (article articles)
8812       (setq header (gnus-summary-article-header article))
8813       (if (not (vectorp header))
8814           ;; This is a pseudo-article.
8815           (if (assq 'name header)
8816               (gnus-copy-file (cdr (assq 'name header)))
8817             (gnus-message 1 "Article %d is unsaveable" article))
8818         ;; This is a real article.
8819         (save-window-excursion
8820           (gnus-summary-select-article t nil nil article))
8821         (save-excursion
8822           (set-buffer save-buffer)
8823           (erase-buffer)
8824           (insert-buffer-substring gnus-original-article-buffer))
8825         (setq file (gnus-article-save save-buffer file num))
8826         (gnus-summary-remove-process-mark article)
8827         (unless not-saved
8828           (gnus-summary-set-saved-mark article))))
8829     (gnus-kill-buffer save-buffer)
8830     (gnus-summary-position-point)
8831     (gnus-set-mode-line 'summary)
8832     n))
8833
8834 (defun gnus-summary-pipe-output (&optional arg)
8835   "Pipe the current article to a subprocess.
8836 If N is a positive number, pipe the N next articles.
8837 If N is a negative number, pipe the N previous articles.
8838 If N is nil and any articles have been marked with the process mark,
8839 pipe those articles instead."
8840   (interactive "P")
8841   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8842     (gnus-summary-save-article arg t))
8843   (gnus-configure-windows 'pipe))
8844
8845 (defun gnus-summary-save-article-mail (&optional arg)
8846   "Append the current article to an mail file.
8847 If N is a positive number, save the N next articles.
8848 If N is a negative number, save the N previous articles.
8849 If N is nil and any articles have been marked with the process mark,
8850 save those articles instead."
8851   (interactive "P")
8852   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8853     (gnus-summary-save-article arg)))
8854
8855 (defun gnus-summary-save-article-rmail (&optional arg)
8856   "Append the current article to an rmail file.
8857 If N is a positive number, save the N next articles.
8858 If N is a negative number, save the N previous articles.
8859 If N is nil and any articles have been marked with the process mark,
8860 save those articles instead."
8861   (interactive "P")
8862   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8863     (gnus-summary-save-article arg)))
8864
8865 (defun gnus-summary-save-article-file (&optional arg)
8866   "Append the current article to a file.
8867 If N is a positive number, save the N next articles.
8868 If N is a negative number, save the N previous articles.
8869 If N is nil and any articles have been marked with the process mark,
8870 save those articles instead."
8871   (interactive "P")
8872   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8873     (gnus-summary-save-article arg)))
8874
8875 (defun gnus-summary-write-article-file (&optional arg)
8876   "Write the current article to a file, deleting the previous file.
8877 If N is a positive number, save the N next articles.
8878 If N is a negative number, save the N previous articles.
8879 If N is nil and any articles have been marked with the process mark,
8880 save those articles instead."
8881   (interactive "P")
8882   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8883     (gnus-summary-save-article arg)))
8884
8885 (defun gnus-summary-save-article-body-file (&optional arg)
8886   "Append the current article body to a file.
8887 If N is a positive number, save the N next articles.
8888 If N is a negative number, save the N previous articles.
8889 If N is nil and any articles have been marked with the process mark,
8890 save those articles instead."
8891   (interactive "P")
8892   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8893     (gnus-summary-save-article arg)))
8894
8895 (defun gnus-summary-pipe-message (program)
8896   "Pipe the current article through PROGRAM."
8897   (interactive "sProgram: ")
8898   (gnus-summary-select-article)
8899   (let ((mail-header-separator ""))
8900     (gnus-eval-in-buffer-window gnus-article-buffer
8901       (save-restriction
8902         (widen)
8903         (let ((start (window-start))
8904               buffer-read-only)
8905           (message-pipe-buffer-body program)
8906           (set-window-start (get-buffer-window (current-buffer)) start))))))
8907
8908 (defun gnus-get-split-value (methods)
8909   "Return a value based on the split METHODS."
8910   (let (split-name method result match)
8911     (when methods
8912       (save-excursion
8913         (set-buffer gnus-original-article-buffer)
8914         (save-restriction
8915           (nnheader-narrow-to-headers)
8916           (while methods
8917             (goto-char (point-min))
8918             (setq method (pop methods))
8919             (setq match (car method))
8920             (when (cond
8921                    ((stringp match)
8922                     ;; Regular expression.
8923                     (ignore-errors
8924                       (re-search-forward match nil t)))
8925                    ((gnus-functionp match)
8926                     ;; Function.
8927                     (save-restriction
8928                       (widen)
8929                       (setq result (funcall match gnus-newsgroup-name))))
8930                    ((consp match)
8931                     ;; Form.
8932                     (save-restriction
8933                       (widen)
8934                       (setq result (eval match)))))
8935               (setq split-name (append (cdr method) split-name))
8936               (cond ((stringp result)
8937                      (push (expand-file-name
8938                             result gnus-article-save-directory)
8939                            split-name))
8940                     ((consp result)
8941                      (setq split-name (append result split-name)))))))))
8942     (nreverse split-name)))
8943
8944 (defun gnus-valid-move-group-p (group)
8945   (and (boundp group)
8946        (symbol-name group)
8947        (symbol-value group)
8948        (gnus-get-function (gnus-find-method-for-group
8949                            (symbol-name group)) 'request-accept-article t)))
8950
8951 (defun gnus-read-move-group-name (prompt default articles prefix)
8952   "Read a group name."
8953   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8954          (minibuffer-confirm-incomplete nil) ; XEmacs
8955          (prom
8956           (format "%s %s to:"
8957                   prompt
8958                   (if (> (length articles) 1)
8959                       (format "these %d articles" (length articles))
8960                     "this article")))
8961          (to-newsgroup
8962           (cond
8963            ((null split-name)
8964             (gnus-completing-read default prom
8965                                   gnus-active-hashtb
8966                                   'gnus-valid-move-group-p
8967                                   nil prefix
8968                                   'gnus-group-history))
8969            ((= 1 (length split-name))
8970             (gnus-completing-read (car split-name) prom
8971                                   gnus-active-hashtb
8972                                   'gnus-valid-move-group-p
8973                                   nil nil
8974                                   'gnus-group-history))
8975            (t
8976             (gnus-completing-read nil prom
8977                                   (mapcar (lambda (el) (list el))
8978                                           (nreverse split-name))
8979                                   nil nil nil
8980                                   'gnus-group-history)))))
8981     (when to-newsgroup
8982       (if (or (string= to-newsgroup "")
8983               (string= to-newsgroup prefix))
8984           (setq to-newsgroup default))
8985       (unless to-newsgroup
8986         (error "No group name entered"))
8987       (or (gnus-active to-newsgroup)
8988           (gnus-activate-group to-newsgroup)
8989           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8990                                      to-newsgroup))
8991               (or (and (gnus-request-create-group
8992                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8993                        (gnus-activate-group to-newsgroup nil nil
8994                                             (gnus-group-name-to-method
8995                                              to-newsgroup)))
8996                   (error "Couldn't create group %s" to-newsgroup)))
8997           (error "No such group: %s" to-newsgroup)))
8998     to-newsgroup))
8999
9000 (defun gnus-summary-save-parts (type dir n reverse)
9001   "Save parts matching TYPE to DIR.
9002 If REVERSE, save parts that do not match TYPE."
9003   (interactive
9004    (list (read-string "Save parts of type: " "image/.*")
9005          (read-file-name "Save to directory: " t nil t)
9006          current-prefix-arg))
9007   (gnus-summary-iterate n
9008     (let ((gnus-display-mime-function nil)
9009           (gnus-inhibit-treatment t))
9010       (gnus-summary-select-article))
9011     (save-excursion
9012       (set-buffer gnus-article-buffer)
9013       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9014         (when handles
9015           (gnus-summary-save-parts-1 type dir handles reverse)
9016           (mm-destroy-parts handles))))))
9017
9018 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9019   (if (stringp (car handle))
9020       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9021               (cdr handle))
9022     (when (if reverse
9023               (not (string-match type (mm-handle-media-type handle)))
9024             (string-match type (mm-handle-media-type handle)))
9025       (let ((file (expand-file-name
9026                    (file-name-nondirectory
9027                     (or
9028                      (mail-content-type-get
9029                       (mm-handle-disposition handle) 'filename)
9030                      (concat gnus-newsgroup-name "." gnus-current-article)))
9031                    dir)))
9032         (unless (file-exists-p file)
9033           (mm-save-part-to-file handle file))))))
9034
9035 ;; Summary extract commands
9036
9037 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9038   (let ((buffer-read-only nil)
9039         (article (gnus-summary-article-number))
9040         after-article b e)
9041     (unless (gnus-summary-goto-subject article)
9042       (error "No such article: %d" article))
9043     (gnus-summary-position-point)
9044     ;; If all commands are to be bunched up on one line, we collect
9045     ;; them here.
9046     (unless gnus-view-pseudos-separately
9047       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9048             files action)
9049         (while ps
9050           (setq action (cdr (assq 'action (car ps))))
9051           (setq files (list (cdr (assq 'name (car ps)))))
9052           (while (and ps (cdr ps)
9053                       (string= (or action "1")
9054                                (or (cdr (assq 'action (cadr ps))) "2")))
9055             (push (cdr (assq 'name (cadr ps))) files)
9056             (setcdr ps (cddr ps)))
9057           (when files
9058             (when (not (string-match "%s" action))
9059               (push " " files))
9060             (push " " files)
9061             (when (assq 'execute (car ps))
9062               (setcdr (assq 'execute (car ps))
9063                       (funcall (if (string-match "%s" action)
9064                                    'format 'concat)
9065                                action
9066                                (mapconcat
9067                                 (lambda (f)
9068                                   (if (equal f " ")
9069                                       f
9070                                     (gnus-quote-arg-for-sh-or-csh f)))
9071                                 files " ")))))
9072           (setq ps (cdr ps)))))
9073     (if (and gnus-view-pseudos (not not-view))
9074         (while pslist
9075           (when (assq 'execute (car pslist))
9076             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9077                                   (eq gnus-view-pseudos 'not-confirm)))
9078           (setq pslist (cdr pslist)))
9079       (save-excursion
9080         (while pslist
9081           (setq after-article (or (cdr (assq 'article (car pslist)))
9082                                   (gnus-summary-article-number)))
9083           (gnus-summary-goto-subject after-article)
9084           (forward-line 1)
9085           (setq b (point))
9086           (insert "    " (file-name-nondirectory
9087                           (cdr (assq 'name (car pslist))))
9088                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9089           (setq e (point))
9090           (forward-line -1)             ; back to `b'
9091           (gnus-add-text-properties
9092            b (1- e) (list 'gnus-number gnus-reffed-article-number
9093                           gnus-mouse-face-prop gnus-mouse-face))
9094           (gnus-data-enter
9095            after-article gnus-reffed-article-number
9096            gnus-unread-mark b (car pslist) 0 (- e b))
9097           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9098           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9099           (setq pslist (cdr pslist)))))))
9100
9101 (defun gnus-pseudos< (p1 p2)
9102   (let ((c1 (cdr (assq 'action p1)))
9103         (c2 (cdr (assq 'action p2))))
9104     (and c1 c2 (string< c1 c2))))
9105
9106 (defun gnus-request-pseudo-article (props)
9107   (cond ((assq 'execute props)
9108          (gnus-execute-command (cdr (assq 'execute props)))))
9109   (let ((gnus-current-article (gnus-summary-article-number)))
9110     (gnus-run-hooks 'gnus-mark-article-hook)))
9111
9112 (defun gnus-execute-command (command &optional automatic)
9113   (save-excursion
9114     (gnus-article-setup-buffer)
9115     (set-buffer gnus-article-buffer)
9116     (setq buffer-read-only nil)
9117     (let ((command (if automatic command
9118                      (read-string "Command: " (cons command 0)))))
9119       (erase-buffer)
9120       (insert "$ " command "\n\n")
9121       (if gnus-view-pseudo-asynchronously
9122           (start-process "gnus-execute" (current-buffer) shell-file-name
9123                          shell-command-switch command)
9124         (call-process shell-file-name nil t nil
9125                       shell-command-switch command)))))
9126
9127 ;; Summary kill commands.
9128
9129 (defun gnus-summary-edit-global-kill (article)
9130   "Edit the \"global\" kill file."
9131   (interactive (list (gnus-summary-article-number)))
9132   (gnus-group-edit-global-kill article))
9133
9134 (defun gnus-summary-edit-local-kill ()
9135   "Edit a local kill file applied to the current newsgroup."
9136   (interactive)
9137   (setq gnus-current-headers (gnus-summary-article-header))
9138   (gnus-group-edit-local-kill
9139    (gnus-summary-article-number) gnus-newsgroup-name))
9140
9141 ;;; Header reading.
9142
9143 (defun gnus-read-header (id &optional header)
9144   "Read the headers of article ID and enter them into the Gnus system."
9145   (let ((group gnus-newsgroup-name)
9146         (gnus-override-method
9147          (and (gnus-news-group-p gnus-newsgroup-name)
9148               gnus-refer-article-method))
9149         where)
9150     ;; First we check to see whether the header in question is already
9151     ;; fetched.
9152     (if (stringp id)
9153         ;; This is a Message-ID.
9154         (setq header (or header (gnus-id-to-header id)))
9155       ;; This is an article number.
9156       (setq header (or header (gnus-summary-article-header id))))
9157     (if (and header
9158              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9159         ;; We have found the header.
9160         header
9161       ;; If this is a sparse article, we have to nix out its
9162       ;; previous entry in the thread hashtb.
9163       (when (and header
9164                  (gnus-summary-article-sparse-p (mail-header-number header)))
9165         (let* ((parent (gnus-parent-id (mail-header-references header)))
9166                (thread (and parent (gnus-id-to-thread parent))))
9167           (when thread
9168             (delq (assq header thread) thread))))
9169       ;; We have to really fetch the header to this article.
9170       (save-excursion
9171         (set-buffer nntp-server-buffer)
9172         (when (setq where (gnus-request-head id group))
9173           (nnheader-fold-continuation-lines)
9174           (goto-char (point-max))
9175           (insert ".\n")
9176           (goto-char (point-min))
9177           (insert "211 ")
9178           (princ (cond
9179                   ((numberp id) id)
9180                   ((cdr where) (cdr where))
9181                   (header (mail-header-number header))
9182                   (t gnus-reffed-article-number))
9183                  (current-buffer))
9184           (insert " Article retrieved.\n"))
9185         (if (or (not where)
9186                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9187             ()                          ; Malformed head.
9188           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9189             (when (and (stringp id)
9190                        (not (string= (gnus-group-real-name group)
9191                                      (car where))))
9192               ;; If we fetched by Message-ID and the article came
9193               ;; from a different group, we fudge some bogus article
9194               ;; numbers for this article.
9195               (mail-header-set-number header gnus-reffed-article-number))
9196             (save-excursion
9197               (set-buffer gnus-summary-buffer)
9198               (decf gnus-reffed-article-number)
9199               (gnus-remove-header (mail-header-number header))
9200               (push header gnus-newsgroup-headers)
9201               (setq gnus-current-headers header)
9202               (push (mail-header-number header) gnus-newsgroup-limit)))
9203           header)))))
9204
9205 (defun gnus-remove-header (number)
9206   "Remove header NUMBER from `gnus-newsgroup-headers'."
9207   (if (and gnus-newsgroup-headers
9208            (= number (mail-header-number (car gnus-newsgroup-headers))))
9209       (pop gnus-newsgroup-headers)
9210     (let ((headers gnus-newsgroup-headers))
9211       (while (and (cdr headers)
9212                   (not (= number (mail-header-number (cadr headers)))))
9213         (pop headers))
9214       (when (cdr headers)
9215         (setcdr headers (cddr headers))))))
9216
9217 ;;;
9218 ;;; summary highlights
9219 ;;;
9220
9221 (defun gnus-highlight-selected-summary ()
9222   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9223   ;; Highlight selected article in summary buffer
9224   (when gnus-summary-selected-face
9225     (save-excursion
9226       (let* ((beg (progn (beginning-of-line) (point)))
9227              (end (progn (end-of-line) (point)))
9228              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9229              (from (if (get-text-property beg gnus-mouse-face-prop)
9230                        beg
9231                      (or (next-single-property-change
9232                           beg gnus-mouse-face-prop nil end)
9233                          beg)))
9234              (to
9235               (if (= from end)
9236                   (- from 2)
9237                 (or (next-single-property-change
9238                      from gnus-mouse-face-prop nil end)
9239                     end))))
9240         ;; If no mouse-face prop on line we will have to = from = end,
9241         ;; so we highlight the entire line instead.
9242         (when (= (+ to 2) from)
9243           (setq from beg)
9244           (setq to end))
9245         (if gnus-newsgroup-selected-overlay
9246             ;; Move old overlay.
9247             (gnus-move-overlay
9248              gnus-newsgroup-selected-overlay from to (current-buffer))
9249           ;; Create new overlay.
9250           (gnus-overlay-put
9251            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9252            'face gnus-summary-selected-face))))))
9253
9254 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9255 (defun gnus-summary-highlight-line ()
9256   "Highlight current line according to `gnus-summary-highlight'."
9257   (let* ((list gnus-summary-highlight)
9258          (p (point))
9259          (end (progn (end-of-line) (point)))
9260          ;; now find out where the line starts and leave point there.
9261          (beg (progn (beginning-of-line) (point)))
9262          (article (gnus-summary-article-number))
9263          (score (or (cdr (assq (or article gnus-current-article)
9264                                gnus-newsgroup-scored))
9265                     gnus-summary-default-score 0))
9266          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9267          (inhibit-read-only t))
9268     ;; Eval the cars of the lists until we find a match.
9269     (let ((default gnus-summary-default-score))
9270       (while (and list
9271                   (not (eval (caar list))))
9272         (setq list (cdr list))))
9273     (let ((face (cdar list)))
9274       (unless (eq face (get-text-property beg 'face))
9275         (gnus-put-text-property-excluding-characters-with-faces
9276          beg end 'face
9277          (setq face (if (boundp face) (symbol-value face) face)))
9278         (when gnus-summary-highlight-line-function
9279           (funcall gnus-summary-highlight-line-function article face))))
9280     (goto-char p)))
9281
9282 (defun gnus-update-read-articles (group unread &optional compute)
9283   "Update the list of read articles in GROUP."
9284   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9285          (entry (gnus-gethash group gnus-newsrc-hashtb))
9286          (info (nth 2 entry))
9287          (prev 1)
9288          (unread (sort (copy-sequence unread) '<))
9289          read)
9290     (if (or (not info) (not active))
9291         ;; There is no info on this group if it was, in fact,
9292         ;; killed.  Gnus stores no information on killed groups, so
9293         ;; there's nothing to be done.
9294         ;; One could store the information somewhere temporarily,
9295         ;; perhaps...  Hmmm...
9296         ()
9297       ;; Remove any negative articles numbers.
9298       (while (and unread (< (car unread) 0))
9299         (setq unread (cdr unread)))
9300       ;; Remove any expired article numbers
9301       (while (and unread (< (car unread) (car active)))
9302         (setq unread (cdr unread)))
9303       ;; Compute the ranges of read articles by looking at the list of
9304       ;; unread articles.
9305       (while unread
9306         (when (/= (car unread) prev)
9307           (push (if (= prev (1- (car unread))) prev
9308                   (cons prev (1- (car unread))))
9309                 read))
9310         (setq prev (1+ (car unread)))
9311         (setq unread (cdr unread)))
9312       (when (<= prev (cdr active))
9313         (push (cons prev (cdr active)) read))
9314       (setq read (if (> (length read) 1) (nreverse read) read))
9315       (if compute
9316           read
9317         (save-excursion
9318           (set-buffer gnus-group-buffer)
9319           (gnus-undo-register
9320             `(progn
9321                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9322                (gnus-info-set-read ',info ',(gnus-info-read info))
9323                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9324                (gnus-group-update-group ,group t))))
9325        ;; Propagate the read marks to the backend.
9326        (if (gnus-check-backend-function 'request-set-mark group)
9327            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9328                  (add (gnus-remove-from-range read (gnus-info-read info))))
9329              (when (or add del)
9330                (unless (gnus-check-group group)
9331                  (error "Can't open server for %s" group))
9332                (gnus-request-set-mark
9333                 group (delq nil (list (if add (list add 'add '(read)))
9334                                       (if del (list del 'del '(read)))))))))
9335         ;; Enter this list into the group info.
9336         (gnus-info-set-read info read)
9337         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9338         (gnus-get-unread-articles-in-group info (gnus-active group))
9339         t))))
9340
9341 (defun gnus-offer-save-summaries ()
9342   "Offer to save all active summary buffers."
9343   (save-excursion
9344     (let ((buflist (buffer-list))
9345           buffers bufname)
9346       ;; Go through all buffers and find all summaries.
9347       (while buflist
9348         (and (setq bufname (buffer-name (car buflist)))
9349              (string-match "Summary" bufname)
9350              (save-excursion
9351                (set-buffer bufname)
9352                ;; We check that this is, indeed, a summary buffer.
9353                (and (eq major-mode 'gnus-summary-mode)
9354                     ;; Also make sure this isn't bogus.
9355                     gnus-newsgroup-prepared
9356                     ;; Also make sure that this isn't a dead summary buffer.
9357                     (not gnus-dead-summary-mode)))
9358              (push bufname buffers))
9359         (setq buflist (cdr buflist)))
9360       ;; Go through all these summary buffers and offer to save them.
9361       (when buffers
9362         (map-y-or-n-p
9363          "Update summary buffer %s? "
9364          (lambda (buf)
9365            (switch-to-buffer buf)
9366            (gnus-summary-exit))
9367          buffers)))))
9368
9369
9370 ;;; @ for mime-partial
9371 ;;;
9372
9373 (defun gnus-request-partial-message ()
9374   (save-excursion
9375     (let ((number (gnus-summary-article-number))
9376           (group gnus-newsgroup-name)
9377           (mother gnus-article-buffer))
9378       (set-buffer (get-buffer-create " *Partial Article*"))
9379       (erase-buffer)
9380       (setq mime-preview-buffer mother)
9381       (gnus-request-article-this-buffer number group)
9382       (mime-parse-buffer)
9383       )))
9384
9385 (autoload 'mime-combine-message/partial-pieces-automatically
9386   "mime-partial"
9387   "Internal method to combine message/partial messages automatically.")
9388
9389 (mime-add-condition
9390  'action '((type . message)(subtype . partial)
9391            (major-mode . gnus-original-article-mode)
9392            (method . mime-combine-message/partial-pieces-automatically)
9393            (summary-buffer-exp . gnus-summary-buffer)
9394            (request-partial-message-method . gnus-request-partial-message)
9395            ))
9396
9397
9398 ;;; @ for message/rfc822
9399 ;;;
9400
9401 (defun gnus-mime-extract-message/rfc822 (entity situation)
9402   (let (group article num cwin swin cur)
9403     (with-current-buffer (mime-entity-buffer entity)
9404       (save-restriction
9405         (narrow-to-region (mime-entity-body-start entity)
9406                           (mime-entity-body-end entity))
9407         (setq group (or (cdr (assq 'group situation))
9408                         (completing-read "Group: "
9409                                          gnus-active-hashtb
9410                                          nil
9411                                          (gnus-read-active-file-p)
9412                                          gnus-newsgroup-name))
9413               article (gnus-request-accept-article group)
9414               )
9415         ))
9416     (when (and (consp article)
9417                (numberp (setq article (cdr article))))
9418       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9419             cwin (get-buffer-window (current-buffer) t)
9420             )
9421       (save-window-excursion
9422         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9423             (select-window swin)
9424           (set-buffer gnus-summary-buffer)
9425           )
9426         (setq cur gnus-current-article)
9427         (forward-line num)
9428         (let (gnus-show-threads)
9429           (gnus-summary-goto-subject article t)
9430           )
9431         (gnus-summary-clear-mark-forward 1)
9432         (gnus-summary-goto-subject cur)
9433         )
9434       (when (and cwin (window-frame cwin))
9435         (select-frame (window-frame cwin))
9436         )
9437       (when (boundp 'mime-acting-situation-to-override)
9438         (set-alist 'mime-acting-situation-to-override
9439                    'group
9440                    group)
9441         (set-alist 'mime-acting-situation-to-override
9442                    'after-method
9443                    `(progn
9444                       (save-current-buffer
9445                         (set-buffer gnus-group-buffer)
9446                         (gnus-activate-group ,group)
9447                         )
9448                       (gnus-summary-goto-article ,cur
9449                                                  gnus-show-all-headers)
9450                       ))
9451         (set-alist 'mime-acting-situation-to-override
9452                    'number num)
9453         )
9454       )))
9455
9456 (mime-add-condition
9457  'action '((type . message)(subtype . rfc822)
9458            (major-mode . gnus-original-article-mode)
9459            (method . gnus-mime-extract-message/rfc822)
9460            (mode . "extract")
9461            ))
9462
9463 (mime-add-condition
9464  'action '((type . message)(subtype . news)
9465            (major-mode . gnus-original-article-mode)
9466            (method . gnus-mime-extract-message/rfc822)
9467            (mode . "extract")
9468            ))
9469
9470 (defun gnus-mime-extract-multipart (entity situation)
9471   (let ((children (mime-entity-children entity))
9472         mime-acting-situation-to-override
9473         f)
9474     (while children
9475       (mime-play-entity (car children)
9476                         (cons (assq 'mode situation)
9477                               mime-acting-situation-to-override))
9478       (setq children (cdr children)))
9479     (if (setq f (cdr (assq 'after-method
9480                            mime-acting-situation-to-override)))
9481         (eval f)
9482       )))  
9483
9484 (mime-add-condition
9485  'action '((type . multipart)
9486            (method . gnus-mime-extract-multipart)
9487            (mode . "extract")
9488            )
9489  'with-default)
9490
9491
9492 ;;; @ end
9493 ;;;
9494
9495 (defun gnus-summary-setup-default-charset ()
9496   "Setup newsgroup default charset."
9497   (if (equal gnus-newsgroup-name "nndraft:drafts")
9498       (setq gnus-newsgroup-charset nil)
9499   (let* ((name (and gnus-newsgroup-name
9500                    (gnus-group-real-name gnus-newsgroup-name)))
9501          (ignored-charsets 
9502           (or gnus-newsgroup-ephemeral-ignored-charsets
9503               (append
9504                (and gnus-newsgroup-name
9505                     (or (gnus-group-find-parameter gnus-newsgroup-name
9506                                                    'ignored-charsets t)
9507                         (let ((alist gnus-group-ignored-charsets-alist)
9508                            elem (charsets nil))
9509                           (while (setq elem (pop alist))
9510                             (when (and name
9511                                        (string-match (car elem) name))
9512                               (setq alist nil
9513                                     charsets (cdr elem))))
9514                           charsets))))
9515               gnus-newsgroup-ignored-charsets)))
9516     (setq gnus-newsgroup-charset
9517           (or gnus-newsgroup-ephemeral-charset
9518               (and gnus-newsgroup-name
9519                    (or (gnus-group-find-parameter gnus-newsgroup-name
9520                                                   'charset)
9521                        (let ((alist gnus-group-charset-alist)
9522                              elem (charset nil))
9523                          (while (setq elem (pop alist))
9524                            (when (and name
9525                                       (string-match (car elem) name))
9526                              (setq alist nil
9527                                    charset (cadr elem))))
9528                          charset)))
9529               gnus-default-charset))
9530     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9531          ignored-charsets))))
9532
9533 ;;;
9534 ;;; Mime Commands
9535 ;;;
9536
9537 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9538   "Display the current article buffer fully MIME-buttonized.
9539 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9540 treated as multipart/mixed."
9541   (interactive "P")
9542   (require 'gnus-art)
9543   (let ((gnus-unbuttonized-mime-types nil)
9544         (gnus-mime-display-multipart-as-mixed show-all-parts))
9545     (gnus-summary-show-article)))
9546
9547 (defun gnus-summary-repair-multipart (article)
9548   "Add a Content-Type header to a multipart article without one."
9549   (interactive (list (gnus-summary-article-number)))
9550   (gnus-with-article article
9551     (message-narrow-to-head)
9552     (goto-char (point-max))
9553     (widen)
9554     (when (search-forward "\n--" nil t)
9555       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9556         (message-narrow-to-head)
9557         (message-remove-header "Mime-Version")
9558         (message-remove-header "Content-Type")
9559         (goto-char (point-max))
9560         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9561                         separator))
9562         (insert "Mime-Version: 1.0\n")
9563         (widen))))
9564   (let (gnus-mark-article-hook)
9565     (gnus-summary-select-article t t nil article)))
9566
9567 (defun gnus-summary-toggle-display-buttonized ()
9568   "Toggle the buttonizing of the article buffer."
9569   (interactive)
9570   (require 'gnus-art)
9571   (if (setq gnus-inhibit-mime-unbuttonizing
9572             (not gnus-inhibit-mime-unbuttonizing))
9573       (let ((gnus-unbuttonized-mime-types nil))
9574         (gnus-summary-show-article))
9575     (gnus-summary-show-article)))
9576
9577 ;;;
9578 ;;; Intelli-mouse commmands
9579 ;;;
9580
9581 (defun gnus-wheel-summary-scroll (event)
9582   (interactive "e")
9583   (let ((amount (if (memq 'shift (event-modifiers event))
9584                     (car gnus-wheel-scroll-amount)
9585                   (cdr gnus-wheel-scroll-amount)))
9586         (direction (- (* (static-if (featurep 'xemacs)
9587                              (event-button event)
9588                            (cond ((eq 'mouse-4 (event-basic-type event))
9589                                   4)
9590                                  ((eq 'mouse-5 (event-basic-type event))
9591                                   5)))
9592                          2) 9))
9593         edge)
9594     (gnus-summary-scroll-up (* amount direction))
9595     (when (gnus-eval-in-buffer-window gnus-article-buffer
9596             (save-restriction
9597               (widen)
9598               (and (if (< 0 direction)
9599                        (gnus-article-next-page 0)
9600                      (gnus-article-prev-page 0)
9601                      (bobp))
9602                    (if (setq edge (get-text-property
9603                                    (point-min) 'gnus-wheel-edge))
9604                        (setq edge (* edge direction))
9605                      (setq edge -1))
9606                    (or (plusp edge)
9607                        (let ((buffer-read-only nil)
9608                              (inhibit-read-only t))
9609                          (put-text-property (point-min) (point-max)
9610                                             'gnus-wheel-edge direction)
9611                          nil))
9612                    (or (> edge gnus-wheel-edge-resistance)
9613                        (let ((buffer-read-only nil)
9614                              (inhibit-read-only t))
9615                          (put-text-property (point-min) (point-max)
9616                                             'gnus-wheel-edge
9617                                             (* (1+ edge) direction))
9618                          nil))
9619                    (eq last-command 'gnus-wheel-summary-scroll))
9620               ))
9621       (gnus-summary-next-article nil nil (minusp direction)))
9622     ))
9623
9624 (defun gnus-wheel-install ()
9625   "Enable mouse wheel support on summary window."
9626   (when gnus-use-wheel
9627     (let ((keys 
9628            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9629       (dolist (key keys)
9630         (define-key gnus-summary-mode-map key
9631           'gnus-wheel-summary-scroll)))))
9632
9633 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9634
9635 ;;;
9636 ;;; with article
9637 ;;;
9638
9639 (defmacro gnus-with-article (article &rest forms)
9640   "Select ARTICLE and perform FORMS in the original article buffer.
9641 Then replace the article with the result."
9642   `(progn
9643      ;; We don't want the article to be marked as read.
9644      (let (gnus-mark-article-hook)
9645        (gnus-summary-select-article t t nil ,article))
9646      (set-buffer gnus-original-article-buffer)
9647      ,@forms
9648      (if (not (gnus-check-backend-function
9649                'request-replace-article (car gnus-article-current)))
9650          (gnus-message 5 "Read-only group; not replacing")
9651        (unless (gnus-request-replace-article
9652                 ,article (car gnus-article-current)
9653                 (current-buffer) t)
9654          (error "Couldn't replace article")))
9655      ;; The cache and backlog have to be flushed somewhat.
9656      (when gnus-keep-backlog
9657        (gnus-backlog-remove-article
9658         (car gnus-article-current) (cdr gnus-article-current)))
9659      (when gnus-use-cache
9660        (gnus-cache-update-article
9661         (car gnus-article-current) (cdr gnus-article-current)))))
9662
9663 (put 'gnus-with-article 'lisp-indent-function 1)
9664 (put 'gnus-with-article 'edebug-form-spec '(form body))
9665
9666 ;;;
9667 ;;; Generic summary marking commands
9668 ;;;
9669
9670 (defvar gnus-summary-marking-alist
9671   '((read gnus-del-mark "d")
9672     (unread gnus-unread-mark "u")
9673     (ticked gnus-ticked-mark "!")
9674     (dormant gnus-dormant-mark "?")
9675     (expirable gnus-expirable-mark "e"))
9676   "An alist of names/marks/keystrokes.")
9677
9678 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9679 (defvar gnus-summary-mark-map)
9680
9681 (defun gnus-summary-make-all-marking-commands ()
9682   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9683   (dolist (elem gnus-summary-marking-alist)
9684     (apply 'gnus-summary-make-marking-command elem)))
9685
9686 (defun gnus-summary-make-marking-command (name mark keystroke)
9687   (let ((map (make-sparse-keymap)))
9688     (define-key gnus-summary-generic-mark-map keystroke map)
9689     (dolist (lway `((next "next" next nil "n")
9690                     (next-unread "next unread" next t "N")
9691                     (prev "previous" prev nil "p")
9692                     (prev-unread "previous unread" prev t "P")
9693                     (nomove "" nil nil ,keystroke)))
9694       (let ((func (gnus-summary-make-marking-command-1
9695                    mark (car lway) lway name)))
9696         (setq func (eval func))
9697         (define-key map (nth 4 lway) func)))))
9698       
9699 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9700   `(defun ,(intern
9701             (format "gnus-summary-put-mark-as-%s%s"
9702                     name (if (eq way 'nomove)
9703                              ""
9704                            (concat "-" (symbol-name way)))))
9705      (n)
9706      ,(format
9707        "Mark the current article as %s%s.
9708 If N, the prefix, then repeat N times.
9709 If N is negative, move in reverse order.
9710 The difference between N and the actual number of articles marked is
9711 returned."
9712        name (cadr lway))
9713      (interactive "p")
9714      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9715     
9716 (defun gnus-summary-generic-mark (n mark move unread)
9717   "Mark N articles with MARK."
9718   (unless (eq major-mode 'gnus-summary-mode)
9719     (error "This command can only be used in the summary buffer"))
9720   (gnus-summary-show-thread)
9721   (let ((nummove
9722          (cond
9723           ((eq move 'next) 1)
9724           ((eq move 'prev) -1)
9725           (t 0))))
9726     (if (zerop nummove)
9727         (setq n 1)
9728       (when (< n 0)
9729         (setq n (abs n)
9730               nummove (* -1 nummove))))
9731     (while (and (> n 0)
9732                 (gnus-summary-mark-article nil mark)
9733                 (zerop (gnus-summary-next-subject nummove unread t)))
9734       (setq n (1- n)))
9735     (when (/= 0 n)
9736       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9737     (gnus-summary-recenter)
9738     (gnus-summary-position-point)
9739     (gnus-set-mode-line 'summary)
9740     n))
9741
9742 (gnus-summary-make-all-marking-commands)
9743
9744 (gnus-ems-redefine)
9745
9746 (provide 'gnus-sum)
9747
9748 (run-hooks 'gnus-sum-load-hook)
9749
9750 ;;; gnus-sum.el ends here