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