(gnus-summary-reselect-current-group): Truncate lines in the imitation buffer;
[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, many commands will go to the next unread article.
183 This applies to marking commands as well as other commands that
184 \"naturally\" select the next article, like, for instance, `SPC' at
185 the end of an article.
186 If nil, only the marking commands will go to the next (un)read article.
187 If `never', commands that usually go to the next unread article, will
188 go to the next article, whether it is read or not."
189   :group 'gnus-summary-marks
190   :link '(custom-manual "(gnus)Setting Marks")
191   :type '(choice (const :tag "off" nil)
192                  (const never)
193                  (sexp :menu-tag "on" t)))
194
195 (defcustom gnus-summary-default-score 0
196   "*Default article score level.
197 All scores generated by the score files will be added to this score.
198 If this variable is nil, scoring will be disabled."
199   :group 'gnus-score-default
200   :type '(choice (const :tag "disable")
201                  integer))
202
203 (defcustom gnus-summary-zcore-fuzz 0
204   "*Fuzziness factor for the zcore in the summary buffer.
205 Articles with scores closer than this to `gnus-summary-default-score'
206 will not be marked."
207   :group 'gnus-summary-format
208   :type 'integer)
209
210 (defcustom gnus-simplify-subject-fuzzy-regexp nil
211   "*Strings to be removed when doing fuzzy matches.
212 This can either be a regular expression or list of regular expressions
213 that will be removed from subject strings if fuzzy subject
214 simplification is selected."
215   :group 'gnus-thread
216   :type '(repeat regexp))
217
218 (defcustom gnus-show-threads t
219   "*If non-nil, display threads in summary mode."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-subtree nil
224   "*If non-nil, hide all threads initially.
225 If threads are hidden, you have to run the command
226 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
227 to expose hidden threads."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-hide-killed t
232   "*If non-nil, hide killed threads automatically."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-ignore-subject t
237   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
238 If nil, articles that have different subjects from their parents will
239 start separate threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-operation-ignore-subject t
244   "*If non-nil, subjects will be ignored when doing thread commands.
245 This affects commands like `gnus-summary-kill-thread' and
246 `gnus-summary-lower-thread'.
247
248 If this variable is nil, articles in the same thread with different
249 subjects will not be included in the operation in question.  If this
250 variable is `fuzzy', only articles that have subjects that are fuzzily
251 equal will be included."
252   :group 'gnus-thread
253   :type '(choice (const :tag "off" nil)
254                  (const fuzzy)
255                  (sexp :tag "on" t)))
256
257 (defcustom gnus-thread-indent-level 4
258   "*Number that says how much each sub-thread should be indented."
259   :group 'gnus-thread
260   :type 'integer)
261
262 (defcustom gnus-auto-extend-newsgroup t
263   "*If non-nil, extend newsgroup forward and backward when requested."
264   :group 'gnus-summary-choose
265   :type 'boolean)
266
267 (defcustom gnus-auto-select-first t
268   "*If nil, don't select the first unread article when entering a group.
269 If this variable is `best', select the highest-scored unread article
270 in the group.  If t, select the first unread article.
271
272 This variable can also be a function to place point on a likely
273 subject line.  Useful values include `gnus-summary-first-unread-subject',
274 `gnus-summary-first-unread-article' and
275 `gnus-summary-best-unread-article'.
276
277 If you want to prevent automatic selection of the first unread article
278 in some newsgroups, set the variable to nil in
279 `gnus-select-group-hook'."
280   :group 'gnus-group-select
281   :type '(choice (const :tag "none" nil)
282                  (const best)
283                  (sexp :menu-tag "first" t)
284                  (function-item gnus-summary-first-unread-subject)
285                  (function-item gnus-summary-first-unread-article)
286                  (function-item gnus-summary-best-unread-article)))
287
288 (defcustom gnus-dont-select-after-jump-to-other-group nil
289   "If non-nil, don't select the first unread article after entering the
290 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
291 it is depend on the value of `gnus-auto-select-first' whether to select
292 or not."
293   :group 'gnus-group-select
294   :type 'boolean)
295
296 (defcustom gnus-auto-select-next t
297   "*If non-nil, offer to go to the next group from the end of the previous.
298 If the value is t and the next newsgroup is empty, Gnus will exit
299 summary mode and go back to group mode.  If the value is neither nil
300 nor t, Gnus will select the following unread newsgroup.  In
301 particular, if the value is the symbol `quietly', the next unread
302 newsgroup will be selected without any confirmation, and if it is
303 `almost-quietly', the next group will be selected without any
304 confirmation if you are located on the last article in the group.
305 Finally, if this variable is `slightly-quietly', the `Z n' command
306 will go to the next group without confirmation."
307   :group 'gnus-summary-maneuvering
308   :type '(choice (const :tag "off" nil)
309                  (const quietly)
310                  (const almost-quietly)
311                  (const slightly-quietly)
312                  (sexp :menu-tag "on" t)))
313
314 (defcustom gnus-auto-select-same nil
315   "*If non-nil, select the next article with the same subject.
316 If there are no more articles with the same subject, go to
317 the first unread article."
318   :group 'gnus-summary-maneuvering
319   :type 'boolean)
320
321 (defcustom gnus-summary-check-current nil
322   "*If non-nil, consider the current article when moving.
323 The \"unread\" movement commands will stay on the same line if the
324 current article is unread."
325   :group 'gnus-summary-maneuvering
326   :type 'boolean)
327
328 (defcustom gnus-auto-center-summary t
329   "*If non-nil, always center the current summary buffer.
330 In particular, if `vertical' do only vertical recentering.  If non-nil
331 and non-`vertical', do both horizontal and vertical recentering."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "none" nil)
334                  (const vertical)
335                  (integer :tag "height")
336                  (sexp :menu-tag "both" t)))
337
338 (defcustom gnus-show-all-headers nil
339   "*If non-nil, don't hide any headers."
340   :group 'gnus-article-hiding
341   :group 'gnus-article-headers
342   :type 'boolean)
343
344 (defcustom gnus-summary-ignore-duplicates nil
345   "*If non-nil, ignore articles with identical Message-ID headers."
346   :group 'gnus-summary
347   :type 'boolean)
348
349 (defcustom gnus-single-article-buffer t
350   "*If non-nil, display all articles in the same buffer.
351 If nil, each group will get its own article buffer."
352   :group 'gnus-article-various
353   :type 'boolean)
354
355 (defcustom gnus-break-pages t
356   "*If non-nil, do page breaking on articles.
357 The page delimiter is specified by the `gnus-page-delimiter'
358 variable."
359   :group 'gnus-article-various
360   :type 'boolean)
361
362 (defcustom gnus-show-mime t
363   "*If non-nil, do mime processing of articles.
364 The articles will simply be fed to the function given by
365 `gnus-article-display-method-for-mime'."
366   :group 'gnus-article-mime
367   :type 'boolean)
368
369 (defcustom gnus-move-split-methods nil
370   "*Variable used to suggest where articles are to be moved to.
371 It uses the same syntax as the `gnus-split-methods' variable."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ?  ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for killed articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-cached-mark ?*
438   "*Mark used for articles that are in the cache."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-saved-mark ?S
443   "*Mark used for articles that have been saved to."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-ancient-mark ?O
448   "*Mark used for ancient articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-sparse-mark ?Q
453   "*Mark used for sparsely reffed articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-canceled-mark ?G
458   "*Mark used for canceled articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-duplicate-mark ?M
463   "*Mark used for duplicate articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-undownloaded-mark ?@
468   "*Mark used for articles that weren't downloaded."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-downloadable-mark ?%
473   "*Mark used for articles that are to be downloaded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-unsendable-mark ?=
478   "*Mark used for articles that won't be sent."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-score-over-mark ?+
483   "*Score mark used for articles with high scores."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-score-below-mark ?-
488   "*Score mark used for articles with low scores."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-empty-thread-mark ?  ;Whitespace
493   "*There is no thread under the article."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-not-empty-thread-mark ?=
498   "*There is a thread under the article."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-view-pseudo-asynchronously nil
503   "*If non-nil, Gnus will view pseudo-articles asynchronously."
504   :group 'gnus-extract-view
505   :type 'boolean)
506
507 (defcustom gnus-auto-expirable-marks
508   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
509         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
510         gnus-souped-mark gnus-duplicate-mark)
511   "*The list of marks converted into expiration if a group is auto-expirable."
512   :group 'gnus-summary
513   :type '(repeat character))
514
515 (defcustom gnus-inhibit-user-auto-expire t
516   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
517   :group 'gnus-summary
518   :type 'boolean)
519
520 (defcustom gnus-view-pseudos nil
521   "*If `automatic', pseudo-articles will be viewed automatically.
522 If `not-confirm', pseudos will be viewed automatically, and the user
523 will not be asked to confirm the command."
524   :group 'gnus-extract-view
525   :type '(choice (const :tag "off" nil)
526                  (const automatic)
527                  (const not-confirm)))
528
529 (defcustom gnus-view-pseudos-separately t
530   "*If non-nil, one pseudo-article will be created for each file to be viewed.
531 If nil, all files that use the same viewing command will be given as a
532 list of parameters to that command."
533   :group 'gnus-extract-view
534   :type 'boolean)
535
536 (defcustom gnus-insert-pseudo-articles t
537   "*If non-nil, insert pseudo-articles when decoding articles."
538   :group 'gnus-extract-view
539   :type 'boolean)
540
541 (defcustom gnus-summary-dummy-line-format
542   "  %(:                          :%) %S\n"
543   "*The format specification for the dummy roots in the summary buffer.
544 It works along the same lines as a normal formatting string,
545 with some simple extensions.
546
547 %S  The subject"
548   :group 'gnus-threading
549   :type 'string)
550
551 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
552   "*The format specification for the summary mode line.
553 It works along the same lines as a normal formatting string,
554 with some simple extensions:
555
556 %G  Group name
557 %p  Unprefixed group name
558 %A  Current article number
559 %z  Current article score
560 %V  Gnus version
561 %U  Number of unread articles in the group
562 %e  Number of unselected articles in the group
563 %Z  A string with unread/unselected article counts
564 %g  Shortish group name
565 %S  Subject of the current article
566 %u  User-defined spec
567 %s  Current score file name
568 %d  Number of dormant articles
569 %r  Number of articles that have been marked as read in this session
570 %E  Number of articles expunged by the score files"
571   :group 'gnus-summary-format
572   :type 'string)
573
574 (defcustom gnus-list-identifiers nil
575   "Regexp that matches list identifiers to be removed from subject.
576 This can also be a list of regexps."
577   :group 'gnus-summary-format
578   :group 'gnus-article-hiding
579   :type '(choice (const :tag "none" nil)
580                  (regexp :value ".*")
581                  (repeat :value (".*") regexp)))
582
583 (defcustom gnus-summary-mark-below 0
584   "*Mark all articles with a score below this variable as read.
585 This variable is local to each summary buffer and usually set by the
586 score file."
587   :group 'gnus-score-default
588   :type 'integer)
589
590 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
591   "*List of functions used for sorting articles in the summary buffer.
592 This variable is only used when not using a threaded display."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-article-sort-by-number)
595                          (function-item gnus-article-sort-by-author)
596                          (function-item gnus-article-sort-by-subject)
597                          (function-item gnus-article-sort-by-date)
598                          (function-item gnus-article-sort-by-score)
599                          (function :tag "other"))))
600
601 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
602   "*List of functions used for sorting threads in the summary buffer.
603 By default, threads are sorted by article number.
604
605 Each function takes two threads and return non-nil if the first thread
606 should be sorted before the other.  If you use more than one function,
607 the primary sort function should be the last.  You should probably
608 always include `gnus-thread-sort-by-number' in the list of sorting
609 functions -- preferably first.
610
611 Ready-made functions include `gnus-thread-sort-by-number',
612 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
613 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
614 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
615   :group 'gnus-summary-sort
616   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
617                          (function-item gnus-thread-sort-by-author)
618                          (function-item gnus-thread-sort-by-subject)
619                          (function-item gnus-thread-sort-by-date)
620                          (function-item gnus-thread-sort-by-score)
621                          (function-item gnus-thread-sort-by-total-score)
622                          (function :tag "other"))))
623
624 (defcustom gnus-thread-score-function '+
625   "*Function used for calculating the total score of a thread.
626
627 The function is called with the scores of the article and each
628 subthread and should then return the score of the thread.
629
630 Some functions you can use are `+', `max', or `min'."
631   :group 'gnus-summary-sort
632   :type 'function)
633
634 (defcustom gnus-summary-expunge-below nil
635   "All articles that have a score less than this variable will be expunged.
636 This variable is local to the summary buffers."
637   :group 'gnus-score-default
638   :type '(choice (const :tag "off" nil)
639                  integer))
640
641 (defcustom gnus-thread-expunge-below nil
642   "All threads that have a total score less than this variable will be expunged.
643 See `gnus-thread-score-function' for en explanation of what a
644 \"thread score\" is.
645
646 This variable is local to the summary buffers."
647   :group 'gnus-threading
648   :group 'gnus-score-default
649   :type '(choice (const :tag "off" nil)
650                  integer))
651
652 (defcustom gnus-summary-mode-hook nil
653   "*A hook for Gnus summary mode.
654 This hook is run before any variables are set in the summary buffer."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-menu-hook nil
659   "*Hook run after the creation of the summary mode menu."
660   :group 'gnus-summary-visual
661   :type 'hook)
662
663 (defcustom gnus-summary-exit-hook nil
664   "*A hook called on exit from the summary buffer.
665 It will be called with point in the group buffer."
666   :group 'gnus-summary-exit
667   :type 'hook)
668
669 (defcustom gnus-summary-prepare-hook nil
670   "*A hook called after the summary buffer has been generated.
671 If you want to modify the summary buffer, you can use this hook."
672   :group 'gnus-summary-various
673   :type 'hook)
674
675 (defcustom gnus-summary-prepared-hook nil
676   "*A hook called as the last thing after the summary buffer has been generated."
677   :group 'gnus-summary-various
678   :type 'hook)
679
680 (defcustom gnus-summary-generate-hook nil
681   "*A hook run just before generating the summary buffer.
682 This hook is commonly used to customize threading variables and the
683 like."
684   :group 'gnus-summary-various
685   :type 'hook)
686
687 (defcustom gnus-select-group-hook nil
688   "*A hook called when a newsgroup is selected.
689
690 If you'd like to simplify subjects like the
691 `gnus-summary-next-same-subject' command does, you can use the
692 following hook:
693
694  (setq gnus-select-group-hook
695       (list
696         (lambda ()
697           (mapcar (lambda (header)
698                      (mail-header-set-subject
699                       header
700                       (gnus-simplify-subject
701                        (mail-header-subject header) 're-only)))
702                   gnus-newsgroup-headers))))"
703   :group 'gnus-group-select
704   :type 'hook)
705
706 (defcustom gnus-select-article-hook nil
707   "*A hook called when an article is selected."
708   :group 'gnus-summary-choose
709   :type 'hook)
710
711 (defcustom gnus-visual-mark-article-hook
712   (list 'gnus-highlight-selected-summary)
713   "*Hook run after selecting an article in the summary buffer.
714 It is meant to be used for highlighting the article in some way.  It
715 is not run if `gnus-visual' is nil."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
720   "*A hook called before parsing the headers."
721   :group 'gnus-various
722   :type 'hook)
723
724 (defcustom gnus-exit-group-hook nil
725   "*A hook called when exiting (not quitting) summary mode."
726   :group 'gnus-various
727   :type 'hook)
728
729 (defcustom gnus-summary-update-hook
730   (list 'gnus-summary-highlight-line)
731   "*A hook called when a summary line is changed.
732 The hook will not be called if `gnus-visual' is nil.
733
734 The default function `gnus-summary-highlight-line' will
735 highlight the line according to the `gnus-summary-highlight'
736 variable."
737   :group 'gnus-summary-visual
738   :type 'hook)
739
740 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
741   "*A hook called when an article is selected for the first time.
742 The hook is intended to mark an article as read (or unread)
743 automatically when it is selected."
744   :group 'gnus-summary-choose
745   :type 'hook)
746
747 (defcustom gnus-group-no-more-groups-hook nil
748   "*A hook run when returning to group mode having no more (unread) groups."
749   :group 'gnus-group-select
750   :type 'hook)
751
752 (defcustom gnus-ps-print-hook nil
753   "*A hook run before ps-printing something from Gnus."
754   :group 'gnus-summary
755   :type 'hook)
756
757 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
758   "Face used for highlighting the current article in the summary buffer."
759   :group 'gnus-summary-visual
760   :type 'face)
761
762 (defcustom gnus-summary-highlight
763   '(((= mark gnus-canceled-mark)
764      . gnus-summary-cancelled-face)
765     ((and (> score default)
766           (or (= mark gnus-dormant-mark)
767               (= mark gnus-ticked-mark)))
768      . gnus-summary-high-ticked-face)
769     ((and (< score default)
770           (or (= mark gnus-dormant-mark)
771               (= mark gnus-ticked-mark)))
772      . gnus-summary-low-ticked-face)
773     ((or (= mark gnus-dormant-mark)
774          (= mark gnus-ticked-mark))
775      . gnus-summary-normal-ticked-face)
776     ((and (> score default) (= mark gnus-ancient-mark))
777      . gnus-summary-high-ancient-face)
778     ((and (< score default) (= mark gnus-ancient-mark))
779      . gnus-summary-low-ancient-face)
780     ((= mark gnus-ancient-mark)
781      . gnus-summary-normal-ancient-face)
782     ((and (> score default) (= mark gnus-unread-mark))
783      . gnus-summary-high-unread-face)
784     ((and (< score default) (= mark gnus-unread-mark))
785      . gnus-summary-low-unread-face)
786     ((and (memq article gnus-newsgroup-incorporated) 
787           (= mark gnus-unread-mark))
788      . gnus-summary-incorporated-face)
789     ((= mark gnus-unread-mark)
790      . gnus-summary-normal-unread-face)
791     ((and (> score default) (memq mark (list gnus-downloadable-mark
792                                              gnus-undownloaded-mark)))
793      . gnus-summary-high-unread-face)
794     ((and (< score default) (memq mark (list gnus-downloadable-mark
795                                              gnus-undownloaded-mark)))
796      . gnus-summary-low-unread-face)
797     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
798      . gnus-summary-normal-unread-face)
799     ((> score default)
800      . gnus-summary-high-read-face)
801     ((< score default)
802      . gnus-summary-low-read-face)
803     (t
804      . gnus-summary-normal-read-face))
805   "*Controls the highlighting of summary buffer lines.
806
807 A list of (FORM . FACE) pairs.  When deciding how a a particular
808 summary line should be displayed, each form is evaluated.  The content
809 of the face field after the first true form is used.  You can change
810 how those summary lines are displayed, by editing the face field.
811
812 You can use the following variables in the FORM field.
813
814 score:   The articles score
815 default: The default article score.
816 below:   The score below which articles are automatically marked as read.
817 mark:    The articles mark."
818   :group 'gnus-summary-visual
819   :type '(repeat (cons (sexp :tag "Form" nil)
820                        face)))
821
822 (defcustom gnus-alter-header-function nil
823   "Function called to allow alteration of article header structures.
824 The function is called with one parameter, the article header vector,
825 which it may alter in any way.")
826
827 (defvar gnus-decode-encoded-word-function
828   (mime-find-field-decoder 'From 'nov)
829   "Variable that says which function should be used to decode a string with encoded words.")
830
831 (defcustom gnus-extra-headers nil
832   "*Extra headers to parse."
833   :group 'gnus-summary
834   :type '(repeat symbol))
835
836 (defcustom gnus-ignored-from-addresses
837   (and user-mail-address (regexp-quote user-mail-address))
838   "*Regexp of From headers that may be suppressed in favor of To headers."
839   :group 'gnus-summary
840   :type 'regexp)
841
842 (defcustom gnus-group-charset-alist
843   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
844     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
845     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
846     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
847     ("^relcom\\>" koi8-r)
848     ("^fido7\\>" koi8-r)
849     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
850     ("^israel\\>" iso-8859-1)
851     ("^han\\>" euc-kr)
852     ("^alt.chinese.text.big5\\>" chinese-big5)
853     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
854     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
855     (".*" iso-8859-1))
856   "Alist of regexps (to match group names) and default charsets to be used when reading."
857   :type '(repeat (list (regexp :tag "Group")
858                        (symbol :tag "Charset")))
859   :group 'gnus-charset)
860
861 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
862   "List of charsets that should be ignored.
863 When these charsets are used in the \"charset\" parameter, the
864 default charset will be used instead."
865   :type '(repeat symbol)
866   :group 'gnus-charset)
867
868 (defcustom gnus-group-ignored-charsets-alist 
869   '(("alt\\.chinese\\.text" iso-8859-1))
870   "Alist of regexps (to match group names) and charsets that should be ignored.
871 When these charsets are used in the \"charset\" parameter, the
872 default charset will be used instead."
873   :type '(repeat (cons (regexp :tag "Group")
874                        (repeat symbol)))
875   :group 'gnus-charset)
876
877 (defcustom gnus-group-highlight-words-alist nil
878   "Alist of group regexps and highlight regexps.
879 This variable uses the same syntax as `gnus-emphasis-alist'."
880   :type '(repeat (cons (regexp :tag "Group")
881                        (repeat (list (regexp :tag "Highlight regexp")
882                                      (number :tag "Group for entire word" 0)
883                                      (number :tag "Group for displayed part" 0)
884                                      (symbol :tag "Face" 
885                                              gnus-emphasis-highlight-words)))))
886   :group 'gnus-summary-visual)
887
888 (defcustom gnus-use-wheel nil
889   "Use Intelli-mouse on summary movement"
890   :type 'boolean
891   :group 'gnus-summary-maneuvering)
892
893 (defcustom gnus-wheel-scroll-amount '(5 . 1)
894   "Amount to scroll messages by spinning the mouse wheel.
895 This is actually a cons cell, where the first item is the amount to scroll
896 on a normal wheel event, and the second is the amount to scroll when the
897 wheel is moved with the shift key depressed."
898   :type '(cons (integer :tag "Shift") integer)
899   :group 'gnus-summary-maneuvering)
900
901 (defcustom gnus-wheel-edge-resistance 2
902   "How hard it should be to change the current article
903 by moving the mouse over the edge of the article window."
904   :type 'integer
905   :group 'gnus-summary-maneuvering)
906
907 (defcustom gnus-summary-show-article-charset-alist
908   nil
909   "Alist of number and charset.
910 The article will be shown with the charset corresponding to the
911 numbered argument.
912 For example: ((1 . cn-gb-2312) (2 . big5))."
913   :type '(repeat (cons (number :tag "Argument" 1)
914                        (symbol :tag "Charset")))
915   :group 'gnus-charset)
916
917 (defcustom gnus-preserve-marks t
918   "Whether marks are preserved when moving, copying and respooling messages."
919   :type 'boolean
920   :group 'gnus-summary-marks)
921
922 ;;; Internal variables
923
924 (defvar gnus-scores-exclude-files nil)
925 (defvar gnus-page-broken nil)
926 (defvar gnus-inhibit-mime-unbuttonizing nil)
927
928 (defvar gnus-original-article nil)
929 (defvar gnus-article-internal-prepare-hook nil)
930 (defvar gnus-newsgroup-process-stack nil)
931
932 (defvar gnus-thread-indent-array nil)
933 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
934 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
935   "Function called to sort the articles within a thread after it has been gathered together.")
936
937 ;; Avoid highlighting in kill files.
938 (defvar gnus-summary-inhibit-highlight nil)
939 (defvar gnus-newsgroup-selected-overlay nil)
940 (defvar gnus-inhibit-limiting nil)
941 (defvar gnus-newsgroup-adaptive-score-file nil)
942 (defvar gnus-current-score-file nil)
943 (defvar gnus-current-move-group nil)
944 (defvar gnus-current-copy-group nil)
945 (defvar gnus-current-crosspost-group nil)
946
947 (defvar gnus-newsgroup-dependencies nil)
948 (defvar gnus-newsgroup-adaptive nil)
949 (defvar gnus-summary-display-article-function nil)
950 (defvar gnus-summary-highlight-line-function nil
951   "Function called after highlighting a summary line.")
952
953 (defvar gnus-summary-line-format-alist
954   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
955     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
956     (?s gnus-tmp-subject-or-nil ?s)
957     (?n gnus-tmp-name ?s)
958     (?A (std11-address-string
959          (car (mime-read-field 'From gnus-tmp-header))) ?s)
960     (?a (or (std11-full-name-string
961              (car (mime-read-field 'From gnus-tmp-header)))
962             gnus-tmp-from) ?s)
963     (?F gnus-tmp-from ?s)
964     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
965     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
966     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
967     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
968     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
969     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
970     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
971     (?L gnus-tmp-lines ?d)
972     (?I gnus-tmp-indentation ?s)
973     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
974     (?R gnus-tmp-replied ?c)
975     (?\[ gnus-tmp-opening-bracket ?c)
976     (?\] gnus-tmp-closing-bracket ?c)
977     (?\> (make-string gnus-tmp-level ? ) ?s)
978     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
979     (?i gnus-tmp-score ?d)
980     (?z gnus-tmp-score-char ?c)
981     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
982     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
983     (?U gnus-tmp-unread ?c)
984     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
985     (?t (gnus-summary-number-of-articles-in-thread
986          (and (boundp 'thread) (car thread)) gnus-tmp-level)
987         ?d)
988     (?e (gnus-summary-number-of-articles-in-thread
989          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
990         ?c)
991     (?u gnus-tmp-user-defined ?s)
992     (?P (gnus-pick-line-number) ?d))
993   "An alist of format specifications that can appear in summary lines,
994 and what variables they correspond with, along with the type of the
995 variable (string, integer, character, etc).")
996
997 (defvar gnus-summary-dummy-line-format-alist
998   `((?S gnus-tmp-subject ?s)
999     (?N gnus-tmp-number ?d)
1000     (?u gnus-tmp-user-defined ?s)))
1001
1002 (defvar gnus-summary-mode-line-format-alist
1003   `((?G gnus-tmp-group-name ?s)
1004     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1005     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1006     (?A gnus-tmp-article-number ?d)
1007     (?Z gnus-tmp-unread-and-unselected ?s)
1008     (?V gnus-version ?s)
1009     (?U gnus-tmp-unread-and-unticked ?d)
1010     (?S gnus-tmp-subject ?s)
1011     (?e gnus-tmp-unselected ?d)
1012     (?u gnus-tmp-user-defined ?s)
1013     (?d (length gnus-newsgroup-dormant) ?d)
1014     (?t (length gnus-newsgroup-marked) ?d)
1015     (?r (length gnus-newsgroup-reads) ?d)
1016     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1017     (?E gnus-newsgroup-expunged-tally ?d)
1018     (?s (gnus-current-score-file-nondirectory) ?s)))
1019
1020 (defvar gnus-last-search-regexp nil
1021   "Default regexp for article search command.")
1022
1023 (defvar gnus-summary-search-article-matched-data nil
1024   "Last matched data of article search command.  It is the local variable
1025 in `gnus-article-buffer' which consists of the list of start position,
1026 end position and text.")
1027
1028 (defvar gnus-last-shell-command nil
1029   "Default shell command on article.")
1030
1031 (defvar gnus-newsgroup-begin nil)
1032 (defvar gnus-newsgroup-end nil)
1033 (defvar gnus-newsgroup-last-rmail nil)
1034 (defvar gnus-newsgroup-last-mail nil)
1035 (defvar gnus-newsgroup-last-folder nil)
1036 (defvar gnus-newsgroup-last-file nil)
1037 (defvar gnus-newsgroup-auto-expire nil)
1038 (defvar gnus-newsgroup-active nil)
1039
1040 (defvar gnus-newsgroup-data nil)
1041 (defvar gnus-newsgroup-data-reverse nil)
1042 (defvar gnus-newsgroup-limit nil)
1043 (defvar gnus-newsgroup-limits nil)
1044
1045 (defvar gnus-newsgroup-unreads nil
1046   "List of unread articles in the current newsgroup.")
1047
1048 (defvar gnus-newsgroup-unselected nil
1049   "List of unselected unread articles in the current newsgroup.")
1050
1051 (defvar gnus-newsgroup-reads nil
1052   "Alist of read articles and article marks in the current newsgroup.")
1053
1054 (defvar gnus-newsgroup-expunged-tally nil)
1055
1056 (defvar gnus-newsgroup-marked nil
1057   "List of ticked articles in the current newsgroup (a subset of unread art).")
1058
1059 (defvar gnus-newsgroup-killed nil
1060   "List of ranges of articles that have been through the scoring process.")
1061
1062 (defvar gnus-newsgroup-cached nil
1063   "List of articles that come from the article cache.")
1064
1065 (defvar gnus-newsgroup-saved nil
1066   "List of articles that have been saved.")
1067
1068 (defvar gnus-newsgroup-kill-headers nil)
1069
1070 (defvar gnus-newsgroup-replied nil
1071   "List of articles that have been replied to in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-expirable nil
1074   "List of articles in the current newsgroup that can be expired.")
1075
1076 (defvar gnus-newsgroup-processable nil
1077   "List of articles in the current newsgroup that can be processed.")
1078
1079 (defvar gnus-newsgroup-downloadable nil
1080   "List of articles in the current newsgroup that can be processed.")
1081
1082 (defvar gnus-newsgroup-undownloaded nil
1083   "List of articles in the current newsgroup that haven't been downloaded..")
1084
1085 (defvar gnus-newsgroup-unsendable nil
1086   "List of articles in the current newsgroup that won't be sent.")
1087
1088 (defvar gnus-newsgroup-bookmarks nil
1089   "List of articles in the current newsgroup that have bookmarks.")
1090
1091 (defvar gnus-newsgroup-dormant nil
1092   "List of dormant articles in the current newsgroup.")
1093
1094 (defvar gnus-newsgroup-scored nil
1095   "List of scored articles in the current newsgroup.")
1096
1097 (defvar gnus-newsgroup-incorporated nil
1098   "List of incorporated articles in the current newsgroup.")
1099
1100 (defvar gnus-newsgroup-headers nil
1101   "List of article headers in the current newsgroup.")
1102
1103 (defvar gnus-newsgroup-threads nil)
1104
1105 (defvar gnus-newsgroup-prepared nil
1106   "Whether the current group has been prepared properly.")
1107
1108 (defvar gnus-newsgroup-ancient nil
1109   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1110
1111 (defvar gnus-newsgroup-sparse nil)
1112
1113 (defvar gnus-current-article nil)
1114 (defvar gnus-article-current nil)
1115 (defvar gnus-current-headers nil)
1116 (defvar gnus-have-all-headers nil)
1117 (defvar gnus-last-article nil)
1118 (defvar gnus-newsgroup-history nil)
1119 (defvar gnus-newsgroup-charset nil)
1120 (defvar gnus-newsgroup-ephemeral-charset nil)
1121 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1122
1123 (defconst gnus-summary-local-variables
1124   '(gnus-newsgroup-name
1125     gnus-newsgroup-begin gnus-newsgroup-end
1126     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1127     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1128     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1129     gnus-newsgroup-unselected gnus-newsgroup-marked
1130     gnus-newsgroup-reads gnus-newsgroup-saved
1131     gnus-newsgroup-replied gnus-newsgroup-expirable
1132     gnus-newsgroup-processable gnus-newsgroup-killed
1133     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1134     gnus-newsgroup-unsendable
1135     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1136     gnus-newsgroup-headers gnus-newsgroup-threads
1137     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1138     gnus-current-article gnus-current-headers gnus-have-all-headers
1139     gnus-last-article gnus-article-internal-prepare-hook
1140     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1141     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1142     gnus-thread-expunge-below
1143     gnus-score-alist gnus-current-score-file
1144     (gnus-summary-expunge-below . global)
1145     (gnus-summary-mark-below . global)
1146     gnus-newsgroup-active gnus-scores-exclude-files
1147     gnus-newsgroup-history gnus-newsgroup-ancient
1148     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1149     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1150     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1151     (gnus-newsgroup-expunged-tally . 0)
1152     gnus-cache-removable-articles gnus-newsgroup-cached
1153     gnus-newsgroup-data gnus-newsgroup-data-reverse
1154     gnus-newsgroup-limit gnus-newsgroup-limits
1155     gnus-newsgroup-charset
1156     gnus-newsgroup-incorporated)
1157   "Variables that are buffer-local to the summary buffers.")
1158
1159 ;; Byte-compiler warning.
1160 (defvar gnus-article-mode-map)
1161
1162 ;; Subject simplification.
1163
1164 (defun gnus-simplify-whitespace (str)
1165   "Remove excessive whitespace."
1166   (let ((mystr str))
1167     ;; Multiple spaces.
1168     (while (string-match "[ \t][ \t]+" mystr)
1169       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1170                           " "
1171                           (substring mystr (match-end 0)))))
1172     ;; Leading spaces.
1173     (when (string-match "^[ \t]+" mystr)
1174       (setq mystr (substring mystr (match-end 0))))
1175     ;; Trailing spaces.
1176     (when (string-match "[ \t]+$" mystr)
1177       (setq mystr (substring mystr 0 (match-beginning 0))))
1178     mystr))
1179
1180 (defsubst gnus-simplify-subject-re (subject)
1181   "Remove \"Re:\" from subject lines."
1182   (if (string-match "^[Rr][Ee]: *" subject)
1183       (substring subject (match-end 0))
1184     subject))
1185
1186 (defun gnus-simplify-subject (subject &optional re-only)
1187   "Remove `Re:' and words in parentheses.
1188 If RE-ONLY is non-nil, strip leading `Re:'s only."
1189   (let ((case-fold-search t))           ;Ignore case.
1190     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1191     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1192       (setq subject (substring subject (match-end 0))))
1193     ;; Remove uninteresting prefixes.
1194     (when (and (not re-only)
1195                gnus-simplify-ignored-prefixes
1196                (string-match gnus-simplify-ignored-prefixes subject))
1197       (setq subject (substring subject (match-end 0))))
1198     ;; Remove words in parentheses from end.
1199     (unless re-only
1200       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1201         (setq subject (substring subject 0 (match-beginning 0)))))
1202     ;; Return subject string.
1203     subject))
1204
1205 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1206 ;; all whitespace.
1207 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1208   (goto-char (point-min))
1209   (while (re-search-forward regexp nil t)
1210     (replace-match (or newtext ""))))
1211
1212 (defun gnus-simplify-buffer-fuzzy ()
1213   "Simplify string in the buffer fuzzily.
1214 The string in the accessible portion of the current buffer is simplified.
1215 It is assumed to be a single-line subject.
1216 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1217 matter is removed.  Additional things can be deleted by setting
1218 gnus-simplify-subject-fuzzy-regexp."
1219   (let ((case-fold-search t)
1220         (modified-tick))
1221     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1222
1223     (while (not (eq modified-tick (buffer-modified-tick)))
1224       (setq modified-tick (buffer-modified-tick))
1225       (cond
1226        ((listp gnus-simplify-subject-fuzzy-regexp)
1227         (mapcar 'gnus-simplify-buffer-fuzzy-step
1228                 gnus-simplify-subject-fuzzy-regexp))
1229        (gnus-simplify-subject-fuzzy-regexp
1230         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1231       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1232       (gnus-simplify-buffer-fuzzy-step
1233        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1234       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1235
1236     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1237     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1238     (gnus-simplify-buffer-fuzzy-step " $")
1239     (gnus-simplify-buffer-fuzzy-step "^ +")))
1240
1241 (defun gnus-simplify-subject-fuzzy (subject)
1242   "Simplify a subject string fuzzily.
1243 See `gnus-simplify-buffer-fuzzy' for details."
1244   (save-excursion
1245     (gnus-set-work-buffer)
1246     (let ((case-fold-search t))
1247       ;; Remove uninteresting prefixes.
1248       (when (and gnus-simplify-ignored-prefixes
1249                  (string-match gnus-simplify-ignored-prefixes subject))
1250         (setq subject (substring subject (match-end 0))))
1251       (insert subject)
1252       (inline (gnus-simplify-buffer-fuzzy))
1253       (buffer-string))))
1254
1255 (defsubst gnus-simplify-subject-fully (subject)
1256   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1257   (cond
1258    (gnus-simplify-subject-functions
1259     (gnus-map-function gnus-simplify-subject-functions subject))
1260    ((null gnus-summary-gather-subject-limit)
1261     (gnus-simplify-subject-re subject))
1262    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1263     (gnus-simplify-subject-fuzzy subject))
1264    ((numberp gnus-summary-gather-subject-limit)
1265     (gnus-limit-string (gnus-simplify-subject-re subject)
1266                        gnus-summary-gather-subject-limit))
1267    (t
1268     subject)))
1269
1270 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1271   "Check whether two subjects are equal.
1272 If optional argument simple-first is t, first argument is already
1273 simplified."
1274   (cond
1275    ((null simple-first)
1276     (equal (gnus-simplify-subject-fully s1)
1277            (gnus-simplify-subject-fully s2)))
1278    (t
1279     (equal s1
1280            (gnus-simplify-subject-fully s2)))))
1281
1282 (defun gnus-summary-bubble-group ()
1283   "Increase the score of the current group.
1284 This is a handy function to add to `gnus-summary-exit-hook' to
1285 increase the score of each group you read."
1286   (gnus-group-add-score gnus-newsgroup-name))
1287
1288 \f
1289 ;;;
1290 ;;; Gnus summary mode
1291 ;;;
1292
1293 (put 'gnus-summary-mode 'mode-class 'special)
1294
1295 (when t
1296   ;; Non-orthogonal keys
1297
1298   (gnus-define-keys gnus-summary-mode-map
1299     " " gnus-summary-next-page
1300     "\177" gnus-summary-prev-page
1301     [delete] gnus-summary-prev-page
1302     [backspace] gnus-summary-prev-page
1303     "\r" gnus-summary-scroll-up
1304     "\M-\r" gnus-summary-scroll-down
1305     "n" gnus-summary-next-unread-article
1306     "p" gnus-summary-prev-unread-article
1307     "N" gnus-summary-next-article
1308     "P" gnus-summary-prev-article
1309     "\M-\C-n" gnus-summary-next-same-subject
1310     "\M-\C-p" gnus-summary-prev-same-subject
1311     "\M-n" gnus-summary-next-unread-subject
1312     "\M-p" gnus-summary-prev-unread-subject
1313     "." gnus-summary-first-unread-article
1314     "," gnus-summary-best-unread-article
1315     "\M-s" gnus-summary-search-article-forward
1316     "\M-r" gnus-summary-search-article-backward
1317     "<" gnus-summary-beginning-of-article
1318     ">" gnus-summary-end-of-article
1319     "j" gnus-summary-goto-article
1320     "^" gnus-summary-refer-parent-article
1321     "\M-^" gnus-summary-refer-article
1322     "u" gnus-summary-tick-article-forward
1323     "!" gnus-summary-tick-article-forward
1324     "U" gnus-summary-tick-article-backward
1325     "d" gnus-summary-mark-as-read-forward
1326     "D" gnus-summary-mark-as-read-backward
1327     "E" gnus-summary-mark-as-expirable
1328     "\M-u" gnus-summary-clear-mark-forward
1329     "\M-U" gnus-summary-clear-mark-backward
1330     "k" gnus-summary-kill-same-subject-and-select
1331     "\C-k" gnus-summary-kill-same-subject
1332     "\M-\C-k" gnus-summary-kill-thread
1333     "\M-\C-l" gnus-summary-lower-thread
1334     "e" gnus-summary-edit-article
1335     "#" gnus-summary-mark-as-processable
1336     "\M-#" gnus-summary-unmark-as-processable
1337     "\M-\C-t" gnus-summary-toggle-threads
1338     "\M-\C-s" gnus-summary-show-thread
1339     "\M-\C-h" gnus-summary-hide-thread
1340     "\M-\C-f" gnus-summary-next-thread
1341     "\M-\C-b" gnus-summary-prev-thread
1342     "\M-\C-u" gnus-summary-up-thread
1343     "\M-\C-d" gnus-summary-down-thread
1344     "&" gnus-summary-execute-command
1345     "c" gnus-summary-catchup-and-exit
1346     "\C-w" gnus-summary-mark-region-as-read
1347     "\C-t" gnus-summary-toggle-truncation
1348     "?" gnus-summary-mark-as-dormant
1349     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1350     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1351     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1352     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1353     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1354     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1355     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1356     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1357     "=" gnus-summary-expand-window
1358     "\C-x\C-s" gnus-summary-reselect-current-group
1359     "\M-g" gnus-summary-rescan-group
1360     "w" gnus-summary-stop-page-breaking
1361     "\C-c\C-r" gnus-summary-caesar-message
1362     "\M-t" gnus-summary-toggle-mime
1363     "f" gnus-summary-followup
1364     "F" gnus-summary-followup-with-original
1365     "C" gnus-summary-cancel-article
1366     "r" gnus-summary-reply
1367     "R" gnus-summary-reply-with-original
1368     "\C-c\C-f" gnus-summary-mail-forward
1369     "o" gnus-summary-save-article
1370     "\C-o" gnus-summary-save-article-mail
1371     "|" gnus-summary-pipe-output
1372     "\M-k" gnus-summary-edit-local-kill
1373     "\M-K" gnus-summary-edit-global-kill
1374     ;; "V" gnus-version
1375     "\C-c\C-d" gnus-summary-describe-group
1376     "q" gnus-summary-exit
1377     "Q" gnus-summary-exit-no-update
1378     "\C-c\C-i" gnus-info-find-node
1379     gnus-mouse-2 gnus-mouse-pick-article
1380     "m" gnus-summary-mail-other-window
1381     "a" gnus-summary-post-news
1382     "x" gnus-summary-limit-to-unread
1383     "s" gnus-summary-isearch-article
1384     "t" gnus-article-toggle-headers
1385     "g" gnus-summary-show-article
1386     "l" gnus-summary-goto-last-article
1387     "v" gnus-summary-preview-mime-message
1388     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1389     "\C-d" gnus-summary-enter-digest-group
1390     "\M-\C-d" gnus-summary-read-document
1391     "\M-\C-e" gnus-summary-edit-parameters
1392     "\M-\C-a" gnus-summary-customize-parameters
1393     "\C-c\C-b" gnus-bug
1394     "*" gnus-cache-enter-article
1395     "\M-*" gnus-cache-remove-article
1396     "\M-&" gnus-summary-universal-argument
1397     "\C-l" gnus-recenter
1398     "I" gnus-summary-increase-score
1399     "L" gnus-summary-lower-score
1400     "\M-i" gnus-symbolic-argument
1401     "h" gnus-summary-select-article-buffer
1402
1403     "V" gnus-summary-score-map
1404     "X" gnus-uu-extract-map
1405     "S" gnus-summary-send-map)
1406
1407   ;; Sort of orthogonal keymap
1408   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1409     "t" gnus-summary-tick-article-forward
1410     "!" gnus-summary-tick-article-forward
1411     "d" gnus-summary-mark-as-read-forward
1412     "r" gnus-summary-mark-as-read-forward
1413     "c" gnus-summary-clear-mark-forward
1414     " " gnus-summary-clear-mark-forward
1415     "e" gnus-summary-mark-as-expirable
1416     "x" gnus-summary-mark-as-expirable
1417     "?" gnus-summary-mark-as-dormant
1418     "b" gnus-summary-set-bookmark
1419     "B" gnus-summary-remove-bookmark
1420     "#" gnus-summary-mark-as-processable
1421     "\M-#" gnus-summary-unmark-as-processable
1422     "S" gnus-summary-limit-include-expunged
1423     "C" gnus-summary-catchup
1424     "H" gnus-summary-catchup-to-here
1425     "\C-c" gnus-summary-catchup-all
1426     "k" gnus-summary-kill-same-subject-and-select
1427     "K" gnus-summary-kill-same-subject
1428     "P" gnus-uu-mark-map)
1429
1430   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1431     "c" gnus-summary-clear-above
1432     "u" gnus-summary-tick-above
1433     "m" gnus-summary-mark-above
1434     "k" gnus-summary-kill-below)
1435
1436   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1437     "/" gnus-summary-limit-to-subject
1438     "n" gnus-summary-limit-to-articles
1439     "w" gnus-summary-pop-limit
1440     "s" gnus-summary-limit-to-subject
1441     "a" gnus-summary-limit-to-author
1442     "u" gnus-summary-limit-to-unread
1443     "m" gnus-summary-limit-to-marks
1444     "M" gnus-summary-limit-exclude-marks
1445     "v" gnus-summary-limit-to-score
1446     "*" gnus-summary-limit-include-cached
1447     "D" gnus-summary-limit-include-dormant
1448     "T" gnus-summary-limit-include-thread
1449     "d" gnus-summary-limit-exclude-dormant
1450     "t" gnus-summary-limit-to-age
1451     "x" gnus-summary-limit-to-extra 
1452     "E" gnus-summary-limit-include-expunged
1453     "c" gnus-summary-limit-exclude-childless-dormant
1454     "C" gnus-summary-limit-mark-excluded-as-read)
1455
1456   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1457     "n" gnus-summary-next-unread-article
1458     "p" gnus-summary-prev-unread-article
1459     "N" gnus-summary-next-article
1460     "P" gnus-summary-prev-article
1461     "\C-n" gnus-summary-next-same-subject
1462     "\C-p" gnus-summary-prev-same-subject
1463     "\M-n" gnus-summary-next-unread-subject
1464     "\M-p" gnus-summary-prev-unread-subject
1465     "f" gnus-summary-first-unread-article
1466     "b" gnus-summary-best-unread-article
1467     "j" gnus-summary-goto-article
1468     "g" gnus-summary-goto-subject
1469     "l" gnus-summary-goto-last-article
1470     "o" gnus-summary-pop-article)
1471
1472   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1473     "k" gnus-summary-kill-thread
1474     "l" gnus-summary-lower-thread
1475     "i" gnus-summary-raise-thread
1476     "T" gnus-summary-toggle-threads
1477     "t" gnus-summary-rethread-current
1478     "^" gnus-summary-reparent-thread
1479     "s" gnus-summary-show-thread
1480     "S" gnus-summary-show-all-threads
1481     "h" gnus-summary-hide-thread
1482     "H" gnus-summary-hide-all-threads
1483     "n" gnus-summary-next-thread
1484     "p" gnus-summary-prev-thread
1485     "u" gnus-summary-up-thread
1486     "o" gnus-summary-top-thread
1487     "d" gnus-summary-down-thread
1488     "#" gnus-uu-mark-thread
1489     "\M-#" gnus-uu-unmark-thread)
1490
1491   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1492     "g" gnus-summary-prepare
1493     "c" gnus-summary-insert-cached-articles)
1494
1495   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1496     "c" gnus-summary-catchup-and-exit
1497     "C" gnus-summary-catchup-all-and-exit
1498     "E" gnus-summary-exit-no-update
1499     "J" gnus-summary-jump-to-other-group
1500     "Q" gnus-summary-exit
1501     "Z" gnus-summary-exit
1502     "n" gnus-summary-catchup-and-goto-next-group
1503     "R" gnus-summary-reselect-current-group
1504     "G" gnus-summary-rescan-group
1505     "N" gnus-summary-next-group
1506     "s" gnus-summary-save-newsrc
1507     "P" gnus-summary-prev-group)
1508
1509   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1510     " " gnus-summary-next-page
1511     "n" gnus-summary-next-page
1512     "\177" gnus-summary-prev-page
1513     [delete] gnus-summary-prev-page
1514     "p" gnus-summary-prev-page
1515     "\r" gnus-summary-scroll-up
1516     "\M-\r" gnus-summary-scroll-down
1517     "<" gnus-summary-beginning-of-article
1518     ">" gnus-summary-end-of-article
1519     "b" gnus-summary-beginning-of-article
1520     "e" gnus-summary-end-of-article
1521     "^" gnus-summary-refer-parent-article
1522     "r" gnus-summary-refer-parent-article
1523     "D" gnus-summary-enter-digest-group
1524     "R" gnus-summary-refer-references
1525     "T" gnus-summary-refer-thread
1526     "g" gnus-summary-show-article
1527     "s" gnus-summary-isearch-article
1528     "P" gnus-summary-print-article
1529     "t" gnus-article-babel)
1530
1531   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1532     "b" gnus-article-add-buttons
1533     "B" gnus-article-add-buttons-to-head
1534     "o" gnus-article-treat-overstrike
1535     "e" gnus-article-emphasize
1536     "w" gnus-article-fill-cited-article
1537     "Q" gnus-article-fill-long-lines
1538     "C" gnus-article-capitalize-sentences
1539     "c" gnus-article-remove-cr
1540     "f" gnus-article-display-x-face
1541     "l" gnus-summary-stop-page-breaking
1542     "r" gnus-summary-caesar-message
1543     "t" gnus-article-toggle-headers
1544     "v" gnus-summary-verbose-headers
1545     "m" gnus-summary-toggle-mime
1546     "H" gnus-article-strip-headers-in-body
1547     "d" gnus-article-treat-dumbquotes
1548     "s" gnus-smiley-display)
1549
1550   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1551     "a" gnus-article-hide
1552     "h" gnus-article-toggle-headers
1553     "b" gnus-article-hide-boring-headers
1554     "s" gnus-article-hide-signature
1555     "c" gnus-article-hide-citation
1556     "C" gnus-article-hide-citation-in-followups
1557     "l" gnus-article-hide-list-identifiers
1558     "p" gnus-article-hide-pgp
1559     "B" gnus-article-strip-banner
1560     "P" gnus-article-hide-pem
1561     "\C-c" gnus-article-hide-citation-maybe)
1562
1563   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1564     "a" gnus-article-highlight
1565     "h" gnus-article-highlight-headers
1566     "c" gnus-article-highlight-citation
1567     "s" gnus-article-highlight-signature)
1568
1569   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1570     "z" gnus-article-date-ut
1571     "u" gnus-article-date-ut
1572     "l" gnus-article-date-local
1573     "e" gnus-article-date-lapsed
1574     "o" gnus-article-date-original
1575     "i" gnus-article-date-iso8601
1576     "s" gnus-article-date-user)
1577
1578   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1579     "t" gnus-article-remove-trailing-blank-lines
1580     "l" gnus-article-strip-leading-blank-lines
1581     "m" gnus-article-strip-multiple-blank-lines
1582     "a" gnus-article-strip-blank-lines
1583     "A" gnus-article-strip-all-blank-lines
1584     "s" gnus-article-strip-leading-space
1585     "e" gnus-article-strip-trailing-space)
1586
1587   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1588     "v" gnus-version
1589     "f" gnus-summary-fetch-faq
1590     "d" gnus-summary-describe-group
1591     "h" gnus-summary-describe-briefly
1592     "i" gnus-info-find-node)
1593
1594   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1595     "e" gnus-summary-expire-articles
1596     "\M-\C-e" gnus-summary-expire-articles-now
1597     "\177" gnus-summary-delete-article
1598     [delete] gnus-summary-delete-article
1599     [backspace] gnus-summary-delete-article
1600     "m" gnus-summary-move-article
1601     "r" gnus-summary-respool-article
1602     "w" gnus-summary-edit-article
1603     "c" gnus-summary-copy-article
1604     "B" gnus-summary-crosspost-article
1605     "q" gnus-summary-respool-query
1606     "t" gnus-summary-respool-trace
1607     "i" gnus-summary-import-article
1608     "p" gnus-summary-article-posted-p)
1609
1610   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1611     "o" gnus-summary-save-article
1612     "m" gnus-summary-save-article-mail
1613     "F" gnus-summary-write-article-file
1614     "r" gnus-summary-save-article-rmail
1615     "f" gnus-summary-save-article-file
1616     "b" gnus-summary-save-article-body-file
1617     "h" gnus-summary-save-article-folder
1618     "v" gnus-summary-save-article-vm
1619     "p" gnus-summary-pipe-output
1620     "s" gnus-soup-add-article)
1621
1622   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1623     "b" gnus-summary-display-buttonized
1624     "m" gnus-summary-repair-multipart
1625     "v" gnus-article-view-part
1626     "o" gnus-article-save-part
1627     "c" gnus-article-copy-part
1628     "e" gnus-article-externalize-part
1629     "i" gnus-article-inline-part
1630     "|" gnus-article-pipe-part))
1631
1632 (defun gnus-summary-make-menu-bar ()
1633   (gnus-turn-off-edit-menu 'summary)
1634
1635   (unless (boundp 'gnus-summary-misc-menu)
1636
1637     (easy-menu-define
1638      gnus-summary-kill-menu gnus-summary-mode-map ""
1639      (cons
1640       "Score"
1641       (nconc
1642        (list
1643         ["Enter score..." gnus-summary-score-entry t]
1644         ["Customize" gnus-score-customize t])
1645        (gnus-make-score-map 'increase)
1646        (gnus-make-score-map 'lower)
1647        '(("Mark"
1648           ["Kill below" gnus-summary-kill-below t]
1649           ["Mark above" gnus-summary-mark-above t]
1650           ["Tick above" gnus-summary-tick-above t]
1651           ["Clear above" gnus-summary-clear-above t])
1652          ["Current score" gnus-summary-current-score t]
1653          ["Set score" gnus-summary-set-score t]
1654          ["Switch current score file..." gnus-score-change-score-file t]
1655          ["Set mark below..." gnus-score-set-mark-below t]
1656          ["Set expunge below..." gnus-score-set-expunge-below t]
1657          ["Edit current score file" gnus-score-edit-current-scores t]
1658          ["Edit score file" gnus-score-edit-file t]
1659          ["Trace score" gnus-score-find-trace t]
1660          ["Find words" gnus-score-find-favourite-words t]
1661          ["Rescore buffer" gnus-summary-rescore t]
1662          ["Increase score..." gnus-summary-increase-score t]
1663          ["Lower score..." gnus-summary-lower-score t]))))
1664
1665     ;; Define both the Article menu in the summary buffer and the equivalent
1666     ;; Commands menu in the article buffer here for consistency.
1667     (let ((innards
1668            '(("Hide"
1669               ["All" gnus-article-hide t]
1670               ["Headers" gnus-article-toggle-headers t]
1671               ["Signature" gnus-article-hide-signature t]
1672               ["Citation" gnus-article-hide-citation t]
1673               ["List identifiers" gnus-article-hide-list-identifiers t]
1674               ["PGP" gnus-article-hide-pgp t]
1675               ["Banner" gnus-article-strip-banner t]
1676               ["Boring headers" gnus-article-hide-boring-headers t])
1677              ("Highlight"
1678               ["All" gnus-article-highlight t]
1679               ["Headers" gnus-article-highlight-headers t]
1680               ["Signature" gnus-article-highlight-signature t]
1681               ["Citation" gnus-article-highlight-citation t])
1682              ("Date"
1683               ["Local" gnus-article-date-local t]
1684               ["ISO8601" gnus-article-date-iso8601 t]
1685               ["UT" gnus-article-date-ut t]
1686               ["Original" gnus-article-date-original t]
1687               ["Lapsed" gnus-article-date-lapsed t]
1688               ["User-defined" gnus-article-date-user t])
1689              ("Washing"
1690               ("Remove Blanks"
1691                ["Leading" gnus-article-strip-leading-blank-lines t]
1692                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1693                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1694                ["All of the above" gnus-article-strip-blank-lines t]
1695                ["All" gnus-article-strip-all-blank-lines t]
1696                ["Leading space" gnus-article-strip-leading-space t]
1697                ["Trailing space" gnus-article-strip-trailing-space t])
1698               ["Overstrike" gnus-article-treat-overstrike t]
1699               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1700               ["Emphasis" gnus-article-emphasize t]
1701               ["Word wrap" gnus-article-fill-cited-article t]
1702               ["Fill long lines" gnus-article-fill-long-lines t]
1703               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1704               ["CR" gnus-article-remove-cr t]
1705               ["Show X-Face" gnus-article-display-x-face t]
1706               ["Rot 13" gnus-summary-caesar-message t]
1707               ["Unix pipe" gnus-summary-pipe-message t]
1708               ["Add buttons" gnus-article-add-buttons t]
1709               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1710               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1711               ["Toggle MIME" gnus-summary-toggle-mime t]
1712               ["Verbose header" gnus-summary-verbose-headers t]
1713               ["Toggle header" gnus-summary-toggle-header t]
1714               ["Toggle smileys" gnus-smiley-display t]
1715               ["HZ" gnus-article-decode-HZ t])
1716              ("Output"
1717               ["Save in default format" gnus-summary-save-article t]
1718               ["Save in file" gnus-summary-save-article-file t]
1719               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1720               ["Save in MH folder" gnus-summary-save-article-folder t]
1721               ["Save in VM folder" gnus-summary-save-article-vm t]
1722               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1723               ["Save body in file" gnus-summary-save-article-body-file t]
1724               ["Pipe through a filter" gnus-summary-pipe-output t]
1725               ["Add to SOUP packet" gnus-soup-add-article t]
1726               ["Print" gnus-summary-print-article t])
1727              ("Backend"
1728               ["Respool article..." gnus-summary-respool-article t]
1729               ["Move article..." gnus-summary-move-article
1730                (gnus-check-backend-function
1731                 'request-move-article gnus-newsgroup-name)]
1732               ["Copy article..." gnus-summary-copy-article t]
1733               ["Crosspost article..." gnus-summary-crosspost-article
1734                (gnus-check-backend-function
1735                 'request-replace-article gnus-newsgroup-name)]
1736               ["Import file..." gnus-summary-import-article t]
1737               ["Check if posted" gnus-summary-article-posted-p t]
1738               ["Edit article" gnus-summary-edit-article
1739                (not (gnus-group-read-only-p))]
1740               ["Delete article" gnus-summary-delete-article
1741                (gnus-check-backend-function
1742                 'request-expire-articles gnus-newsgroup-name)]
1743               ["Query respool" gnus-summary-respool-query t]
1744               ["Trace respool" gnus-summary-respool-trace t]
1745               ["Delete expirable articles" gnus-summary-expire-articles-now
1746                (gnus-check-backend-function
1747                 'request-expire-articles gnus-newsgroup-name)])
1748              ("Extract"
1749               ["Uudecode" gnus-uu-decode-uu t]
1750               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1751               ["Unshar" gnus-uu-decode-unshar t]
1752               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1753               ["Save" gnus-uu-decode-save t]
1754               ["Binhex" gnus-uu-decode-binhex t]
1755               ["Postscript" gnus-uu-decode-postscript t])
1756              ("Cache"
1757               ["Enter article" gnus-cache-enter-article t]
1758               ["Remove article" gnus-cache-remove-article t])
1759              ["Translate" gnus-article-babel t]
1760              ["Select article buffer" gnus-summary-select-article-buffer t]
1761              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1762              ["Isearch article..." gnus-summary-isearch-article t]
1763              ["Beginning of the article" gnus-summary-beginning-of-article t]
1764              ["End of the article" gnus-summary-end-of-article t]
1765              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1766              ["Fetch referenced articles" gnus-summary-refer-references t]
1767              ["Fetch current thread" gnus-summary-refer-thread t]
1768              ["Fetch article with id..." gnus-summary-refer-article t]
1769              ["Redisplay" gnus-summary-show-article t])))
1770       (easy-menu-define
1771        gnus-summary-article-menu gnus-summary-mode-map ""
1772        (cons "Article" innards))
1773
1774       (easy-menu-define
1775        gnus-article-commands-menu gnus-article-mode-map ""
1776        (cons "Commands" innards)))
1777
1778     (easy-menu-define
1779      gnus-summary-thread-menu gnus-summary-mode-map ""
1780      '("Threads"
1781        ["Toggle threading" gnus-summary-toggle-threads t]
1782        ["Hide threads" gnus-summary-hide-all-threads t]
1783        ["Show threads" gnus-summary-show-all-threads t]
1784        ["Hide thread" gnus-summary-hide-thread t]
1785        ["Show thread" gnus-summary-show-thread t]
1786        ["Go to next thread" gnus-summary-next-thread t]
1787        ["Go to previous thread" gnus-summary-prev-thread t]
1788        ["Go down thread" gnus-summary-down-thread t]
1789        ["Go up thread" gnus-summary-up-thread t]
1790        ["Top of thread" gnus-summary-top-thread t]
1791        ["Mark thread as read" gnus-summary-kill-thread t]
1792        ["Lower thread score" gnus-summary-lower-thread t]
1793        ["Raise thread score" gnus-summary-raise-thread t]
1794        ["Rethread current" gnus-summary-rethread-current t]))
1795
1796     (easy-menu-define
1797      gnus-summary-post-menu gnus-summary-mode-map ""
1798      '("Post"
1799        ["Post an article" gnus-summary-post-news t]
1800        ["Followup" gnus-summary-followup t]
1801        ["Followup and yank" gnus-summary-followup-with-original t]
1802        ["Supersede article" gnus-summary-supersede-article t]
1803        ["Cancel article" gnus-summary-cancel-article t]
1804        ["Reply" gnus-summary-reply t]
1805        ["Reply and yank" gnus-summary-reply-with-original t]
1806        ["Wide reply" gnus-summary-wide-reply t]
1807        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1808        ["Mail forward" gnus-summary-mail-forward t]
1809        ["Post forward" gnus-summary-post-forward t]
1810        ["Digest and mail" gnus-summary-mail-digest t]
1811        ["Digest and post" gnus-summary-post-digest t]
1812        ["Resend message" gnus-summary-resend-message t]
1813        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1814        ["Send a mail" gnus-summary-mail-other-window t]
1815        ["Uuencode and post" gnus-uu-post-news t]
1816        ["Followup via news" gnus-summary-followup-to-mail t]
1817        ["Followup via news and yank"
1818         gnus-summary-followup-to-mail-with-original t]
1819        ;;("Draft"
1820        ;;["Send" gnus-summary-send-draft t]
1821        ;;["Send bounced" gnus-resend-bounced-mail t])
1822        ))
1823
1824     (easy-menu-define
1825      gnus-summary-misc-menu gnus-summary-mode-map ""
1826      '("Misc"
1827        ("Mark Read"
1828         ["Mark as read" gnus-summary-mark-as-read-forward t]
1829         ["Mark same subject and select"
1830          gnus-summary-kill-same-subject-and-select t]
1831         ["Mark same subject" gnus-summary-kill-same-subject t]
1832         ["Catchup" gnus-summary-catchup t]
1833         ["Catchup all" gnus-summary-catchup-all t]
1834         ["Catchup to here" gnus-summary-catchup-to-here t]
1835         ["Catchup region" gnus-summary-mark-region-as-read t]
1836         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1837        ("Mark Various"
1838         ["Tick" gnus-summary-tick-article-forward t]
1839         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1840         ["Remove marks" gnus-summary-clear-mark-forward t]
1841         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1842         ["Set bookmark" gnus-summary-set-bookmark t]
1843         ["Remove bookmark" gnus-summary-remove-bookmark t])
1844        ("Mark Limit"
1845         ["Marks..." gnus-summary-limit-to-marks t]
1846         ["Subject..." gnus-summary-limit-to-subject t]
1847         ["Author..." gnus-summary-limit-to-author t]
1848         ["Age..." gnus-summary-limit-to-age t]
1849         ["Extra..." gnus-summary-limit-to-extra t]
1850         ["Score" gnus-summary-limit-to-score t]
1851         ["Unread" gnus-summary-limit-to-unread t]
1852         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1853         ["Articles" gnus-summary-limit-to-articles t]
1854         ["Pop limit" gnus-summary-pop-limit t]
1855         ["Show dormant" gnus-summary-limit-include-dormant t]
1856         ["Hide childless dormant"
1857          gnus-summary-limit-exclude-childless-dormant t]
1858         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1859         ["Hide marked" gnus-summary-limit-exclude-marks t]
1860         ["Show expunged" gnus-summary-show-all-expunged t])
1861        ("Process Mark"
1862         ["Set mark" gnus-summary-mark-as-processable t]
1863         ["Remove mark" gnus-summary-unmark-as-processable t]
1864         ["Remove all marks" gnus-summary-unmark-all-processable t]
1865         ["Mark above" gnus-uu-mark-over t]
1866         ["Mark series" gnus-uu-mark-series t]
1867         ["Mark region" gnus-uu-mark-region t]
1868         ["Unmark region" gnus-uu-unmark-region t]
1869         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1870         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1871         ["Mark all" gnus-uu-mark-all t]
1872         ["Mark buffer" gnus-uu-mark-buffer t]
1873         ["Mark sparse" gnus-uu-mark-sparse t]
1874         ["Mark thread" gnus-uu-mark-thread t]
1875         ["Unmark thread" gnus-uu-unmark-thread t]
1876         ("Process Mark Sets"
1877          ["Kill" gnus-summary-kill-process-mark t]
1878          ["Yank" gnus-summary-yank-process-mark
1879           gnus-newsgroup-process-stack]
1880          ["Save" gnus-summary-save-process-mark t]))
1881        ("Scroll article"
1882         ["Page forward" gnus-summary-next-page t]
1883         ["Page backward" gnus-summary-prev-page t]
1884         ["Line forward" gnus-summary-scroll-up t])
1885        ("Move"
1886         ["Next unread article" gnus-summary-next-unread-article t]
1887         ["Previous unread article" gnus-summary-prev-unread-article t]
1888         ["Next article" gnus-summary-next-article t]
1889         ["Previous article" gnus-summary-prev-article t]
1890         ["Next unread subject" gnus-summary-next-unread-subject t]
1891         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1892         ["Next article same subject" gnus-summary-next-same-subject t]
1893         ["Previous article same subject" gnus-summary-prev-same-subject t]
1894         ["First unread article" gnus-summary-first-unread-article t]
1895         ["Best unread article" gnus-summary-best-unread-article t]
1896         ["Go to subject number..." gnus-summary-goto-subject t]
1897         ["Go to article number..." gnus-summary-goto-article t]
1898         ["Go to the last article" gnus-summary-goto-last-article t]
1899         ["Pop article off history" gnus-summary-pop-article t])
1900        ("Sort"
1901         ["Sort by number" gnus-summary-sort-by-number t]
1902         ["Sort by author" gnus-summary-sort-by-author t]
1903         ["Sort by subject" gnus-summary-sort-by-subject t]
1904         ["Sort by date" gnus-summary-sort-by-date t]
1905         ["Sort by score" gnus-summary-sort-by-score t]
1906         ["Sort by lines" gnus-summary-sort-by-lines t]
1907         ["Sort by characters" gnus-summary-sort-by-chars t])
1908        ("Help"
1909         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1910         ["Describe group" gnus-summary-describe-group t]
1911         ["Read manual" gnus-info-find-node t])
1912        ("Modes"
1913         ["Pick and read" gnus-pick-mode t]
1914         ["Binary" gnus-binary-mode t])
1915        ("Regeneration"
1916         ["Regenerate" gnus-summary-prepare t]
1917         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1918         ["Toggle threading" gnus-summary-toggle-threads t])
1919        ["Filter articles..." gnus-summary-execute-command t]
1920        ["Run command on subjects..." gnus-summary-universal-argument t]
1921        ["Search articles forward..." gnus-summary-search-article-forward t]
1922        ["Search articles backward..." gnus-summary-search-article-backward t]
1923        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1924        ["Expand window" gnus-summary-expand-window t]
1925        ["Expire expirable articles" gnus-summary-expire-articles
1926         (gnus-check-backend-function
1927          'request-expire-articles gnus-newsgroup-name)]
1928        ["Edit local kill file" gnus-summary-edit-local-kill t]
1929        ["Edit main kill file" gnus-summary-edit-global-kill t]
1930        ["Edit group parameters" gnus-summary-edit-parameters t]
1931        ["Customize group parameters" gnus-summary-customize-parameters t]
1932        ["Send a bug report" gnus-bug t]
1933        ("Exit"
1934         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1935         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1936         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1937         ["Exit group" gnus-summary-exit t]
1938         ["Exit group without updating" gnus-summary-exit-no-update t]
1939         ["Exit and goto next group" gnus-summary-next-group t]
1940         ["Exit and goto prev group" gnus-summary-prev-group t]
1941         ["Reselect group" gnus-summary-reselect-current-group t]
1942         ["Rescan group" gnus-summary-rescan-group t]
1943         ["Update dribble" gnus-summary-save-newsrc t])))
1944
1945     (gnus-run-hooks 'gnus-summary-menu-hook)))
1946
1947 (defun gnus-score-set-default (var value)
1948   "A version of set that updates the GNU Emacs menu-bar."
1949   (set var value)
1950   ;; It is the message that forces the active status to be updated.
1951   (message ""))
1952
1953 (defun gnus-make-score-map (type)
1954   "Make a summary score map of type TYPE."
1955   (if t
1956       nil
1957     (let ((headers '(("author" "from" string)
1958                      ("subject" "subject" string)
1959                      ("article body" "body" string)
1960                      ("article head" "head" string)
1961                      ("xref" "xref" string)
1962                      ("extra header" "extra" string)
1963                      ("lines" "lines" number)
1964                      ("followups to author" "followup" string)))
1965           (types '((number ("less than" <)
1966                            ("greater than" >)
1967                            ("equal" =))
1968                    (string ("substring" s)
1969                            ("exact string" e)
1970                            ("fuzzy string" f)
1971                            ("regexp" r))))
1972           (perms '(("temporary" (current-time-string))
1973                    ("permanent" nil)
1974                    ("immediate" now)))
1975           header)
1976       (list
1977        (apply
1978         'nconc
1979         (list
1980          (if (eq type 'lower)
1981              "Lower score"
1982            "Increase score"))
1983         (let (outh)
1984           (while headers
1985             (setq header (car headers))
1986             (setq outh
1987                   (cons
1988                    (apply
1989                     'nconc
1990                     (list (car header))
1991                     (let ((ts (cdr (assoc (nth 2 header) types)))
1992                           outt)
1993                       (while ts
1994                         (setq outt
1995                               (cons
1996                                (apply
1997                                 'nconc
1998                                 (list (caar ts))
1999                                 (let ((ps perms)
2000                                       outp)
2001                                   (while ps
2002                                     (setq outp
2003                                           (cons
2004                                            (vector
2005                                             (caar ps)
2006                                             (list
2007                                              'gnus-summary-score-entry
2008                                              (nth 1 header)
2009                                              (if (or (string= (nth 1 header)
2010                                                               "head")
2011                                                      (string= (nth 1 header)
2012                                                               "body"))
2013                                                  ""
2014                                                (list 'gnus-summary-header
2015                                                      (nth 1 header)))
2016                                              (list 'quote (nth 1 (car ts)))
2017                                              (list 'gnus-score-delta-default
2018                                                    nil)
2019                                              (nth 1 (car ps))
2020                                              t)
2021                                             t)
2022                                            outp))
2023                                     (setq ps (cdr ps)))
2024                                   (list (nreverse outp))))
2025                                outt))
2026                         (setq ts (cdr ts)))
2027                       (list (nreverse outt))))
2028                    outh))
2029             (setq headers (cdr headers)))
2030           (list (nreverse outh))))))))
2031
2032 \f
2033
2034 (defun gnus-summary-mode (&optional group)
2035   "Major mode for reading articles.
2036
2037 All normal editing commands are switched off.
2038 \\<gnus-summary-mode-map>
2039 Each line in this buffer represents one article.  To read an
2040 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2041 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2042 respectively.
2043
2044 You can also post articles and send mail from this buffer.  To
2045 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2046 of an article, type `\\[gnus-summary-reply]'.
2047
2048 There are approx. one gazillion commands you can execute in this
2049 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2050
2051 The following commands are available:
2052
2053 \\{gnus-summary-mode-map}"
2054   (interactive)
2055   (when (gnus-visual-p 'summary-menu 'menu)
2056     (gnus-summary-make-menu-bar))
2057   (kill-all-local-variables)
2058   (gnus-summary-make-local-variables)
2059   (gnus-make-thread-indent-array)
2060   (gnus-simplify-mode-line)
2061   (setq major-mode 'gnus-summary-mode)
2062   (setq mode-name "Summary")
2063   (make-local-variable 'minor-mode-alist)
2064   (use-local-map gnus-summary-mode-map)
2065   (buffer-disable-undo)
2066   (setq buffer-read-only t)             ;Disable modification
2067   (setq truncate-lines t)
2068   (setq selective-display t)
2069   (setq selective-display-ellipses t)   ;Display `...'
2070   (gnus-summary-set-display-table)
2071   (gnus-set-default-directory)
2072   (setq gnus-newsgroup-name group)
2073   (unless (gnus-news-group-p group)
2074     (setq gnus-newsgroup-incorporated
2075           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2076   (make-local-variable 'gnus-summary-line-format)
2077   (make-local-variable 'gnus-summary-line-format-spec)
2078   (make-local-variable 'gnus-summary-dummy-line-format)
2079   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2080   (make-local-variable 'gnus-summary-mark-positions)
2081   (make-local-hook 'pre-command-hook)
2082   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2083   (gnus-run-hooks 'gnus-summary-mode-hook)
2084   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2085   (gnus-update-summary-mark-positions))
2086
2087 (defun gnus-summary-make-local-variables ()
2088   "Make all the local summary buffer variables."
2089   (let (global)
2090     (dolist (local gnus-summary-local-variables)
2091       (if (consp local)
2092           (progn
2093             (if (eq (cdr local) 'global)
2094                 ;; Copy the global value of the variable.
2095                 (setq global (symbol-value (car local)))
2096               ;; Use the value from the list.
2097               (setq global (eval (cdr local))))
2098             (set (make-local-variable (car local)) global))
2099         ;; Simple nil-valued local variable.
2100         (set (make-local-variable local) nil)))))
2101
2102 (defun gnus-summary-clear-local-variables ()
2103   (let ((locals gnus-summary-local-variables))
2104     (while locals
2105       (if (consp (car locals))
2106           (and (vectorp (caar locals))
2107                (set (caar locals) nil))
2108         (and (vectorp (car locals))
2109              (set (car locals) nil)))
2110       (setq locals (cdr locals)))))
2111
2112 ;; Summary data functions.
2113
2114 (defmacro gnus-data-number (data)
2115   `(car ,data))
2116
2117 (defmacro gnus-data-set-number (data number)
2118   `(setcar ,data ,number))
2119
2120 (defmacro gnus-data-mark (data)
2121   `(nth 1 ,data))
2122
2123 (defmacro gnus-data-set-mark (data mark)
2124   `(setcar (nthcdr 1 ,data) ,mark))
2125
2126 (defmacro gnus-data-pos (data)
2127   `(nth 2 ,data))
2128
2129 (defmacro gnus-data-set-pos (data pos)
2130   `(setcar (nthcdr 2 ,data) ,pos))
2131
2132 (defmacro gnus-data-header (data)
2133   `(nth 3 ,data))
2134
2135 (defmacro gnus-data-set-header (data header)
2136   `(setcar (nthcdr 3 ,data) ,header))
2137
2138 (defmacro gnus-data-level (data)
2139   `(nth 4 ,data))
2140
2141 (defmacro gnus-data-unread-p (data)
2142   `(= (nth 1 ,data) gnus-unread-mark))
2143
2144 (defmacro gnus-data-read-p (data)
2145   `(/= (nth 1 ,data) gnus-unread-mark))
2146
2147 (defmacro gnus-data-pseudo-p (data)
2148   `(consp (nth 3 ,data)))
2149
2150 (defmacro gnus-data-find (number)
2151   `(assq ,number gnus-newsgroup-data))
2152
2153 (defmacro gnus-data-find-list (number &optional data)
2154   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2155      (memq (assq ,number bdata)
2156            bdata)))
2157
2158 (defmacro gnus-data-make (number mark pos header level)
2159   `(list ,number ,mark ,pos ,header ,level))
2160
2161 (defun gnus-data-enter (after-article number mark pos header level offset)
2162   (let ((data (gnus-data-find-list after-article)))
2163     (unless data
2164       (error "No such article: %d" after-article))
2165     (setcdr data (cons (gnus-data-make number mark pos header level)
2166                        (cdr data)))
2167     (setq gnus-newsgroup-data-reverse nil)
2168     (gnus-data-update-list (cddr data) offset)))
2169
2170 (defun gnus-data-enter-list (after-article list &optional offset)
2171   (when list
2172     (let ((data (and after-article (gnus-data-find-list after-article)))
2173           (ilist list))
2174       (if (not (or data
2175                    after-article))
2176           (let ((odata gnus-newsgroup-data))
2177             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2178             (when offset
2179               (gnus-data-update-list odata offset)))
2180         ;; Find the last element in the list to be spliced into the main
2181         ;; list.
2182         (while (cdr list)
2183           (setq list (cdr list)))
2184         (if (not data)
2185             (progn
2186               (setcdr list gnus-newsgroup-data)
2187               (setq gnus-newsgroup-data ilist)
2188               (when offset
2189                 (gnus-data-update-list (cdr list) offset)))
2190           (setcdr list (cdr data))
2191           (setcdr data ilist)
2192           (when offset
2193             (gnus-data-update-list (cdr list) offset))))
2194       (setq gnus-newsgroup-data-reverse nil))))
2195
2196 (defun gnus-data-remove (article &optional offset)
2197   (let ((data gnus-newsgroup-data))
2198     (if (= (gnus-data-number (car data)) article)
2199         (progn
2200           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2201                 gnus-newsgroup-data-reverse nil)
2202           (when offset
2203             (gnus-data-update-list gnus-newsgroup-data offset)))
2204       (while (cdr data)
2205         (when (= (gnus-data-number (cadr data)) article)
2206           (setcdr data (cddr data))
2207           (when offset
2208             (gnus-data-update-list (cdr data) offset))
2209           (setq data nil
2210                 gnus-newsgroup-data-reverse nil))
2211         (setq data (cdr data))))))
2212
2213 (defmacro gnus-data-list (backward)
2214   `(if ,backward
2215        (or gnus-newsgroup-data-reverse
2216            (setq gnus-newsgroup-data-reverse
2217                  (reverse gnus-newsgroup-data)))
2218      gnus-newsgroup-data))
2219
2220 (defun gnus-data-update-list (data offset)
2221   "Add OFFSET to the POS of all data entries in DATA."
2222   (setq gnus-newsgroup-data-reverse nil)
2223   (while data
2224     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2225     (setq data (cdr data))))
2226
2227 (defun gnus-summary-article-pseudo-p (article)
2228   "Say whether this article is a pseudo article or not."
2229   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2230
2231 (defmacro gnus-summary-article-sparse-p (article)
2232   "Say whether this article is a sparse article or not."
2233   `(memq ,article gnus-newsgroup-sparse))
2234
2235 (defmacro gnus-summary-article-ancient-p (article)
2236   "Say whether this article is a sparse article or not."
2237   `(memq ,article gnus-newsgroup-ancient))
2238
2239 (defun gnus-article-parent-p (number)
2240   "Say whether this article is a parent or not."
2241   (let ((data (gnus-data-find-list number)))
2242     (and (cdr data)                     ; There has to be an article after...
2243          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2244             (gnus-data-level (nth 1 data))))))
2245
2246 (defun gnus-article-children (number)
2247   "Return a list of all children to NUMBER."
2248   (let* ((data (gnus-data-find-list number))
2249          (level (gnus-data-level (car data)))
2250          children)
2251     (setq data (cdr data))
2252     (while (and data
2253                 (= (gnus-data-level (car data)) (1+ level)))
2254       (push (gnus-data-number (car data)) children)
2255       (setq data (cdr data)))
2256     children))
2257
2258 (defmacro gnus-summary-skip-intangible ()
2259   "If the current article is intangible, then jump to a different article."
2260   '(let ((to (get-text-property (point) 'gnus-intangible)))
2261      (and to (gnus-summary-goto-subject to))))
2262
2263 (defmacro gnus-summary-article-intangible-p ()
2264   "Say whether this article is intangible or not."
2265   '(get-text-property (point) 'gnus-intangible))
2266
2267 (defun gnus-article-read-p (article)
2268   "Say whether ARTICLE is read or not."
2269   (not (or (memq article gnus-newsgroup-marked)
2270            (memq article gnus-newsgroup-unreads)
2271            (memq article gnus-newsgroup-unselected)
2272            (memq article gnus-newsgroup-dormant))))
2273
2274 ;; Some summary mode macros.
2275
2276 (defmacro gnus-summary-article-number ()
2277   "The article number of the article on the current line.
2278 If there isn's an article number here, then we return the current
2279 article number."
2280   '(progn
2281      (gnus-summary-skip-intangible)
2282      (or (get-text-property (point) 'gnus-number)
2283          (gnus-summary-last-subject))))
2284
2285 (defmacro gnus-summary-article-header (&optional number)
2286   "Return the header of article NUMBER."
2287   `(gnus-data-header (gnus-data-find
2288                       ,(or number '(gnus-summary-article-number)))))
2289
2290 (defmacro gnus-summary-thread-level (&optional number)
2291   "Return the level of thread that starts with article NUMBER."
2292   `(if (and (eq gnus-summary-make-false-root 'dummy)
2293             (get-text-property (point) 'gnus-intangible))
2294        0
2295      (gnus-data-level (gnus-data-find
2296                        ,(or number '(gnus-summary-article-number))))))
2297
2298 (defmacro gnus-summary-article-mark (&optional number)
2299   "Return the mark of article NUMBER."
2300   `(gnus-data-mark (gnus-data-find
2301                     ,(or number '(gnus-summary-article-number)))))
2302
2303 (defmacro gnus-summary-article-pos (&optional number)
2304   "Return the position of the line of article NUMBER."
2305   `(gnus-data-pos (gnus-data-find
2306                    ,(or number '(gnus-summary-article-number)))))
2307
2308 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2309 (defmacro gnus-summary-article-subject (&optional number)
2310   "Return current subject string or nil if nothing."
2311   `(let ((headers
2312           ,(if number
2313                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2314              '(gnus-data-header (assq (gnus-summary-article-number)
2315                                       gnus-newsgroup-data)))))
2316      (and headers
2317           (vectorp headers)
2318           (mail-header-subject headers))))
2319
2320 (defmacro gnus-summary-article-score (&optional number)
2321   "Return current article score."
2322   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2323                   gnus-newsgroup-scored))
2324        gnus-summary-default-score 0))
2325
2326 (defun gnus-summary-article-children (&optional number)
2327   "Return a list of article numbers that are children of article NUMBER."
2328   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2329          (level (gnus-data-level (car data)))
2330          l children)
2331     (while (and (setq data (cdr data))
2332                 (> (setq l (gnus-data-level (car data))) level))
2333       (and (= (1+ level) l)
2334            (push (gnus-data-number (car data))
2335                  children)))
2336     (nreverse children)))
2337
2338 (defun gnus-summary-article-parent (&optional number)
2339   "Return the article number of the parent of article NUMBER."
2340   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2341                                     (gnus-data-list t)))
2342          (level (gnus-data-level (car data))))
2343     (if (zerop level)
2344         ()                              ; This is a root.
2345       ;; We search until we find an article with a level less than
2346       ;; this one.  That function has to be the parent.
2347       (while (and (setq data (cdr data))
2348                   (not (< (gnus-data-level (car data)) level))))
2349       (and data (gnus-data-number (car data))))))
2350
2351 (defun gnus-unread-mark-p (mark)
2352   "Say whether MARK is the unread mark."
2353   (= mark gnus-unread-mark))
2354
2355 (defun gnus-read-mark-p (mark)
2356   "Say whether MARK is one of the marks that mark as read.
2357 This is all marks except unread, ticked, dormant, and expirable."
2358   (not (or (= mark gnus-unread-mark)
2359            (= mark gnus-ticked-mark)
2360            (= mark gnus-dormant-mark)
2361            (= mark gnus-expirable-mark))))
2362
2363 (defmacro gnus-article-mark (number)
2364   "Return the MARK of article NUMBER.
2365 This macro should only be used when computing the mark the \"first\"
2366 time; i.e., when generating the summary lines.  After that,
2367 `gnus-summary-article-mark' should be used to examine the
2368 marks of articles."
2369   `(cond
2370     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2371     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2372     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2373     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2374     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2375     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2376     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2377     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2378            gnus-ancient-mark))))
2379
2380 ;; Saving hidden threads.
2381
2382 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2383 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2384
2385 (defmacro gnus-save-hidden-threads (&rest forms)
2386   "Save hidden threads, eval FORMS, and restore the hidden threads."
2387   (let ((config (make-symbol "config")))
2388     `(let ((,config (gnus-hidden-threads-configuration)))
2389        (unwind-protect
2390            (save-excursion
2391              ,@forms)
2392          (gnus-restore-hidden-threads-configuration ,config)))))
2393
2394 (defun gnus-data-compute-positions ()
2395   "Compute the positions of all articles."
2396   (setq gnus-newsgroup-data-reverse nil)
2397   (let ((data gnus-newsgroup-data))
2398     (save-excursion
2399       (gnus-save-hidden-threads
2400         (gnus-summary-show-all-threads)
2401         (goto-char (point-min))
2402         (while data
2403           (while (get-text-property (point) 'gnus-intangible)
2404             (forward-line 1))
2405           (gnus-data-set-pos (car data) (+ (point) 3))
2406           (setq data (cdr data))
2407           (forward-line 1))))))
2408
2409 (defun gnus-hidden-threads-configuration ()
2410   "Return the current hidden threads configuration."
2411   (save-excursion
2412     (let (config)
2413       (goto-char (point-min))
2414       (while (search-forward "\r" nil t)
2415         (push (1- (point)) config))
2416       config)))
2417
2418 (defun gnus-restore-hidden-threads-configuration (config)
2419   "Restore hidden threads configuration from CONFIG."
2420   (let (point buffer-read-only)
2421     (while (setq point (pop config))
2422       (when (and (< point (point-max))
2423                  (goto-char point)
2424                  (eq (char-after) ?\n))
2425         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2426
2427 ;; Various summary mode internalish functions.
2428
2429 (defun gnus-mouse-pick-article (e)
2430   (interactive "e")
2431   (mouse-set-point e)
2432   (gnus-summary-next-page nil t))
2433
2434 (defun gnus-summary-set-display-table ()
2435   ;; Change the display table.  Odd characters have a tendency to mess
2436   ;; up nicely formatted displays - we make all possible glyphs
2437   ;; display only a single character.
2438
2439   ;; We start from the standard display table, if any.
2440   (let ((table (or (copy-sequence standard-display-table)
2441                    (make-display-table)))
2442         (i 32))
2443     ;; Nix out all the control chars...
2444     (while (>= (setq i (1- i)) 0)
2445       (aset table i [??]))
2446     ;; ... but not newline and cr, of course.  (cr is necessary for the
2447     ;; selective display).
2448     (aset table ?\n nil)
2449     (aset table ?\r nil)
2450     ;; We keep TAB as well.
2451     (aset table ?\t nil)
2452     ;; We nix out any glyphs over 126 that are not set already.
2453     (let ((i 256))
2454       (while (>= (setq i (1- i)) 127)
2455         ;; Only modify if the entry is nil.
2456         (unless (aref table i)
2457           (aset table i [??]))))
2458     (setq buffer-display-table table)))
2459
2460 (defun gnus-summary-setup-buffer (group)
2461   "Initialize summary buffer."
2462   (let ((buffer (concat "*Summary " group "*")))
2463     (if (get-buffer buffer)
2464         (progn
2465           (set-buffer buffer)
2466           (setq gnus-summary-buffer (current-buffer))
2467           (not gnus-newsgroup-prepared))
2468       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2469       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2470       (gnus-summary-mode group)
2471       (when gnus-carpal
2472         (gnus-carpal-setup-buffer 'summary))
2473       (unless gnus-single-article-buffer
2474         (make-local-variable 'gnus-article-buffer)
2475         (make-local-variable 'gnus-article-current)
2476         (make-local-variable 'gnus-original-article-buffer))
2477       (setq gnus-newsgroup-name group)
2478       t)))
2479
2480 (defun gnus-set-global-variables ()
2481   ;; Set the global equivalents of the summary buffer-local variables
2482   ;; to the latest values they had.  These reflect the summary buffer
2483   ;; that was in action when the last article was fetched.
2484   (when (eq major-mode 'gnus-summary-mode)
2485     (setq gnus-summary-buffer (current-buffer))
2486     (let ((name gnus-newsgroup-name)
2487           (marked gnus-newsgroup-marked)
2488           (unread gnus-newsgroup-unreads)
2489           (headers gnus-current-headers)
2490           (data gnus-newsgroup-data)
2491           (summary gnus-summary-buffer)
2492           (article-buffer gnus-article-buffer)
2493           (original gnus-original-article-buffer)
2494           (gac gnus-article-current)
2495           (reffed gnus-reffed-article-number)
2496           (score-file gnus-current-score-file)
2497           (default-charset gnus-newsgroup-charset))
2498       (save-excursion
2499         (set-buffer gnus-group-buffer)
2500         (setq gnus-newsgroup-name name
2501               gnus-newsgroup-marked marked
2502               gnus-newsgroup-unreads unread
2503               gnus-current-headers headers
2504               gnus-newsgroup-data data
2505               gnus-article-current gac
2506               gnus-summary-buffer summary
2507               gnus-article-buffer article-buffer
2508               gnus-original-article-buffer original
2509               gnus-reffed-article-number reffed
2510               gnus-current-score-file score-file
2511               gnus-newsgroup-charset default-charset)
2512         ;; The article buffer also has local variables.
2513         (when (gnus-buffer-live-p gnus-article-buffer)
2514           (set-buffer gnus-article-buffer)
2515           (setq gnus-summary-buffer summary))))))
2516
2517 (defun gnus-summary-article-unread-p (article)
2518   "Say whether ARTICLE is unread or not."
2519   (memq article gnus-newsgroup-unreads))
2520
2521 (defun gnus-summary-first-article-p (&optional article)
2522   "Return whether ARTICLE is the first article in the buffer."
2523   (if (not (setq article (or article (gnus-summary-article-number))))
2524       nil
2525     (eq article (caar gnus-newsgroup-data))))
2526
2527 (defun gnus-summary-last-article-p (&optional article)
2528   "Return whether ARTICLE is the last article in the buffer."
2529   (if (not (setq article (or article (gnus-summary-article-number))))
2530       ;; All non-existent numbers are the last article.  :-)
2531       t
2532     (not (cdr (gnus-data-find-list article)))))
2533
2534 (defun gnus-make-thread-indent-array ()
2535   (let ((n 200))
2536     (unless (and gnus-thread-indent-array
2537                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2538       (setq gnus-thread-indent-array (make-vector 201 "")
2539             gnus-thread-indent-array-level gnus-thread-indent-level)
2540       (while (>= n 0)
2541         (aset gnus-thread-indent-array n
2542               (make-string (* n gnus-thread-indent-level) ? ))
2543         (setq n (1- n))))))
2544
2545 (defun gnus-update-summary-mark-positions ()
2546   "Compute where the summary marks are to go."
2547   (save-excursion
2548     (when (gnus-buffer-exists-p gnus-summary-buffer)
2549       (set-buffer gnus-summary-buffer))
2550     (let ((gnus-replied-mark 129)
2551           (gnus-score-below-mark 130)
2552           (gnus-score-over-mark 130)
2553           (gnus-download-mark 131)
2554           (spec gnus-summary-line-format-spec)
2555           gnus-visual pos)
2556       (save-excursion
2557         (gnus-set-work-buffer)
2558         (let ((gnus-summary-line-format-spec spec)
2559               (gnus-newsgroup-downloadable '((0 . t))))
2560           (gnus-summary-insert-line
2561            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2562            0 nil 128 t nil "" nil 1)
2563           (goto-char (point-min))
2564           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2565                                              (- (point) 2)))))
2566           (goto-char (point-min))
2567           (push (cons 'replied (and (search-forward "\201" nil t)
2568                                     (- (point) 2)))
2569                 pos)
2570           (goto-char (point-min))
2571           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2572                 pos)
2573           (goto-char (point-min))
2574           (push (cons 'download
2575                       (and (search-forward "\203" nil t) (- (point) 2)))
2576                 pos)))
2577       (setq gnus-summary-mark-positions pos))))
2578
2579 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2580   "Insert a dummy root in the summary buffer."
2581   (beginning-of-line)
2582   (gnus-add-text-properties
2583    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2584    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2585
2586 (defun gnus-summary-from-or-to-or-newsgroups (header)
2587   (let ((to (cdr (assq 'To (mail-header-extra header))))
2588         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2589         (default-mime-charset (with-current-buffer gnus-summary-buffer
2590                                 default-mime-charset)))
2591     (cond
2592      ((and to
2593            gnus-ignored-from-addresses
2594            (string-match gnus-ignored-from-addresses
2595                          (mail-header-from header)))
2596       (concat "-> "
2597               (or (car (funcall gnus-extract-address-components
2598                                 (funcall
2599                                  gnus-decode-encoded-word-function to)))
2600                   (funcall gnus-decode-encoded-word-function to))))
2601      ((and newsgroups
2602            gnus-ignored-from-addresses
2603            (string-match gnus-ignored-from-addresses
2604                          (mail-header-from header)))
2605       (concat "=> " newsgroups))
2606      (t
2607       (or (car (funcall gnus-extract-address-components
2608                         (mail-header-from header)))
2609           (mail-header-from header))))))
2610
2611 (defun gnus-summary-insert-line (gnus-tmp-header
2612                                  gnus-tmp-level gnus-tmp-current
2613                                  gnus-tmp-unread gnus-tmp-replied
2614                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2615                                  &optional gnus-tmp-dummy gnus-tmp-score
2616                                  gnus-tmp-process)
2617   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2618          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2619          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2620          (gnus-tmp-score-char
2621           (if (or (null gnus-summary-default-score)
2622                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2623                       gnus-summary-zcore-fuzz))
2624               ?  ;Whitespace
2625             (if (< gnus-tmp-score gnus-summary-default-score)
2626                 gnus-score-below-mark gnus-score-over-mark)))
2627          (gnus-tmp-replied
2628           (cond (gnus-tmp-process gnus-process-mark)
2629                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2630                  gnus-cached-mark)
2631                 (gnus-tmp-replied gnus-replied-mark)
2632                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2633                  gnus-saved-mark)
2634                 (t gnus-unread-mark)))
2635          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2636          (gnus-tmp-name
2637           (cond
2638            ((string-match "<[^>]+> *$" gnus-tmp-from)
2639             (let ((beg (match-beginning 0)))
2640               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2641                        (substring gnus-tmp-from (1+ (match-beginning 0))
2642                                   (1- (match-end 0))))
2643                   (substring gnus-tmp-from 0 beg))))
2644            ((string-match "(.+)" gnus-tmp-from)
2645             (substring gnus-tmp-from
2646                        (1+ (match-beginning 0)) (1- (match-end 0))))
2647            (t gnus-tmp-from)))
2648          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2649          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2650          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2651          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2652          (buffer-read-only nil))
2653     (when (string= gnus-tmp-name "")
2654       (setq gnus-tmp-name gnus-tmp-from))
2655     (unless (numberp gnus-tmp-lines)
2656       (setq gnus-tmp-lines 0))
2657     (gnus-put-text-property-excluding-characters-with-faces
2658      (point)
2659      (progn (eval gnus-summary-line-format-spec) (point))
2660      'gnus-number gnus-tmp-number)
2661     (when (gnus-visual-p 'summary-highlight 'highlight)
2662       (forward-line -1)
2663       (gnus-run-hooks 'gnus-summary-update-hook)
2664       (forward-line 1))))
2665
2666 (defun gnus-summary-update-line (&optional dont-update)
2667   ;; Update summary line after change.
2668   (when (and gnus-summary-default-score
2669              (not gnus-summary-inhibit-highlight))
2670     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2671            (article (gnus-summary-article-number))
2672            (score (gnus-summary-article-score article)))
2673       (unless dont-update
2674         (if (and gnus-summary-mark-below
2675                  (< (gnus-summary-article-score)
2676                     gnus-summary-mark-below))
2677             ;; This article has a low score, so we mark it as read.
2678             (when (memq article gnus-newsgroup-unreads)
2679               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2680           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2681             ;; This article was previously marked as read on account
2682             ;; of a low score, but now it has risen, so we mark it as
2683             ;; unread.
2684             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2685         (gnus-summary-update-mark
2686          (if (or (null gnus-summary-default-score)
2687                  (<= (abs (- score gnus-summary-default-score))
2688                      gnus-summary-zcore-fuzz))
2689              ?  ;Whitespace
2690            (if (< score gnus-summary-default-score)
2691                gnus-score-below-mark gnus-score-over-mark))
2692          'score))
2693       ;; Do visual highlighting.
2694       (when (gnus-visual-p 'summary-highlight 'highlight)
2695         (gnus-run-hooks 'gnus-summary-update-hook)))))
2696
2697 (defvar gnus-tmp-new-adopts nil)
2698
2699 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2700   "Return the number of articles in THREAD.
2701 This may be 0 in some cases -- if none of the articles in
2702 the thread are to be displayed."
2703   (let* ((number
2704           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2705           (cond
2706            ((not (listp thread))
2707             1)
2708            ((and (consp thread) (cdr thread))
2709             (apply
2710              '+ 1 (mapcar
2711                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2712            ((null thread)
2713             1)
2714            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2715             1)
2716            (t 0))))
2717     (when (and level (zerop level) gnus-tmp-new-adopts)
2718       (incf number
2719             (apply '+ (mapcar
2720                        'gnus-summary-number-of-articles-in-thread
2721                        gnus-tmp-new-adopts))))
2722     (if char
2723         (if (> number 1) gnus-not-empty-thread-mark
2724           gnus-empty-thread-mark)
2725       number)))
2726
2727 (defun gnus-summary-set-local-parameters (group)
2728   "Go through the local params of GROUP and set all variable specs in that list."
2729   (let ((params (gnus-group-find-parameter group))
2730         elem)
2731     (while params
2732       (setq elem (car params)
2733             params (cdr params))
2734       (and (consp elem)                 ; Has to be a cons.
2735            (consp (cdr elem))           ; The cdr has to be a list.
2736            (symbolp (car elem))         ; Has to be a symbol in there.
2737            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2738            (ignore-errors               ; So we set it.
2739              (make-local-variable (car elem))
2740              (set (car elem) (eval (nth 1 elem))))))))
2741
2742 (defun gnus-summary-read-group (group &optional show-all no-article
2743                                       kill-buffer no-display backward
2744                                       select-articles)
2745   "Start reading news in newsgroup GROUP.
2746 If SHOW-ALL is non-nil, already read articles are also listed.
2747 If NO-ARTICLE is non-nil, no article is selected initially.
2748 If NO-DISPLAY, don't generate a summary buffer."
2749   (let (result)
2750     (while (and group
2751                 (null (setq result
2752                             (let ((gnus-auto-select-next nil))
2753                               (or (gnus-summary-read-group-1
2754                                    group show-all no-article
2755                                    kill-buffer no-display
2756                                    select-articles)
2757                                   (setq show-all nil
2758                                         select-articles nil)))))
2759                 (eq gnus-auto-select-next 'quietly))
2760       (set-buffer gnus-group-buffer)
2761       ;; The entry function called above goes to the next
2762       ;; group automatically, so we go two groups back
2763       ;; if we are searching for the previous group.
2764       (when backward
2765         (gnus-group-prev-unread-group 2))
2766       (if (not (equal group (gnus-group-group-name)))
2767           (setq group (gnus-group-group-name))
2768         (setq group nil)))
2769     result))
2770
2771 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2772   "Directly jump to the other GROUP from summary buffer.
2773 If SHOW-ALL is non-nil, already read articles are also listed."
2774   (interactive
2775    (if (eq gnus-summary-buffer (current-buffer))
2776        (list (completing-read
2777               "Group: " gnus-active-hashtb nil t
2778               (when (and gnus-newsgroup-name
2779                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2780                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2781               'gnus-group-history)
2782              current-prefix-arg)
2783      (error "%s must be invoked from a gnus summary buffer." this-command)))
2784   (unless (or (zerop (length group))
2785               (and gnus-newsgroup-name
2786                    (string-equal gnus-newsgroup-name group)))
2787     (gnus-summary-exit)
2788     (gnus-summary-read-group group show-all
2789                              gnus-dont-select-after-jump-to-other-group)))
2790
2791 (defun gnus-summary-read-group-1 (group show-all no-article
2792                                         kill-buffer no-display
2793                                         &optional select-articles)
2794   ;; Killed foreign groups can't be entered.
2795   (when (and (not (gnus-group-native-p group))
2796              (not (gnus-gethash group gnus-newsrc-hashtb)))
2797     (error "Dead non-native groups can't be entered"))
2798   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2799   (let* ((new-group (gnus-summary-setup-buffer group))
2800          (quit-config (gnus-group-quit-config group))
2801          (did-select (and new-group (gnus-select-newsgroup
2802                                      group show-all select-articles))))
2803     (cond
2804      ;; This summary buffer exists already, so we just select it.
2805      ((not new-group)
2806       (gnus-set-global-variables)
2807       (when kill-buffer
2808         (gnus-kill-or-deaden-summary kill-buffer))
2809       (gnus-configure-windows 'summary 'force)
2810       (gnus-set-mode-line 'summary)
2811       (gnus-summary-position-point)
2812       (message "")
2813       t)
2814      ;; We couldn't select this group.
2815      ((null did-select)
2816       (when (and (eq major-mode 'gnus-summary-mode)
2817                  (not (equal (current-buffer) kill-buffer)))
2818         (kill-buffer (current-buffer))
2819         (if (not quit-config)
2820             (progn
2821               ;; Update the info -- marks might need to be removed,
2822               ;; for instance.
2823               (gnus-summary-update-info)
2824               (set-buffer gnus-group-buffer)
2825               (gnus-group-jump-to-group group)
2826               (gnus-group-next-unread-group 1))
2827           (gnus-handle-ephemeral-exit quit-config)))
2828       (gnus-message 3 "Can't select group")
2829       nil)
2830      ;; The user did a `C-g' while prompting for number of articles,
2831      ;; so we exit this group.
2832      ((eq did-select 'quit)
2833       (and (eq major-mode 'gnus-summary-mode)
2834            (not (equal (current-buffer) kill-buffer))
2835            (kill-buffer (current-buffer)))
2836       (when kill-buffer
2837         (gnus-kill-or-deaden-summary kill-buffer))
2838       (if (not quit-config)
2839           (progn
2840             (set-buffer gnus-group-buffer)
2841             (gnus-group-jump-to-group group)
2842             (gnus-group-next-unread-group 1)
2843             (gnus-configure-windows 'group 'force))
2844         (gnus-handle-ephemeral-exit quit-config))
2845       ;; Finally signal the quit.
2846       (signal 'quit nil))
2847      ;; The group was successfully selected.
2848      (t
2849       (gnus-set-global-variables)
2850       ;; Save the active value in effect when the group was entered.
2851       (setq gnus-newsgroup-active
2852             (gnus-copy-sequence
2853              (gnus-active gnus-newsgroup-name)))
2854       ;; You can change the summary buffer in some way with this hook.
2855       (gnus-run-hooks 'gnus-select-group-hook)
2856       ;; Set any local variables in the group parameters.
2857       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2858       (gnus-update-format-specifications
2859        nil 'summary 'summary-mode 'summary-dummy)
2860       (gnus-update-summary-mark-positions)
2861       ;; Do score processing.
2862       (when gnus-use-scoring
2863         (gnus-possibly-score-headers))
2864       ;; Check whether to fill in the gaps in the threads.
2865       (when gnus-build-sparse-threads
2866         (gnus-build-sparse-threads))
2867       ;; Find the initial limit.
2868       (if gnus-show-threads
2869           (if show-all
2870               (let ((gnus-newsgroup-dormant nil))
2871                 (gnus-summary-initial-limit show-all))
2872             (gnus-summary-initial-limit show-all))
2873         ;; When untreaded, all articles are always shown.
2874         (setq gnus-newsgroup-limit
2875               (mapcar
2876                (lambda (header) (mail-header-number header))
2877                gnus-newsgroup-headers)))
2878       ;; Generate the summary buffer.
2879       (unless no-display
2880         (gnus-summary-prepare))
2881       (when gnus-use-trees
2882         (gnus-tree-open group)
2883         (setq gnus-summary-highlight-line-function
2884               'gnus-tree-highlight-article))
2885       ;; If the summary buffer is empty, but there are some low-scored
2886       ;; articles or some excluded dormants, we include these in the
2887       ;; buffer.
2888       (when (and (zerop (buffer-size))
2889                  (not no-display))
2890         (cond (gnus-newsgroup-dormant
2891                (gnus-summary-limit-include-dormant))
2892               ((and gnus-newsgroup-scored show-all)
2893                (gnus-summary-limit-include-expunged t))))
2894       ;; Function `gnus-apply-kill-file' must be called in this hook.
2895       (gnus-run-hooks 'gnus-apply-kill-hook)
2896       (if (and (zerop (buffer-size))
2897                (not no-display))
2898           (progn
2899             ;; This newsgroup is empty.
2900             (gnus-summary-catchup-and-exit nil t)
2901             (gnus-message 6 "No unread news")
2902             (when kill-buffer
2903               (gnus-kill-or-deaden-summary kill-buffer))
2904             ;; Return nil from this function.
2905             nil)
2906         ;; Hide conversation thread subtrees.  We cannot do this in
2907         ;; gnus-summary-prepare-hook since kill processing may not
2908         ;; work with hidden articles.
2909         (and gnus-show-threads
2910              gnus-thread-hide-subtree
2911              (gnus-summary-hide-all-threads))
2912         (when kill-buffer
2913           (gnus-kill-or-deaden-summary kill-buffer))
2914         ;; Show first unread article if requested.
2915         (if (and (not no-article)
2916                  (not no-display)
2917                  gnus-newsgroup-unreads
2918                  gnus-auto-select-first)
2919             (progn
2920               (gnus-configure-windows 'summary)
2921               (cond
2922                ((eq gnus-auto-select-first 'best)
2923                 (gnus-summary-best-unread-article))
2924                ((eq gnus-auto-select-first t)
2925                 (gnus-summary-first-unread-article))
2926                ((gnus-functionp gnus-auto-select-first)
2927                 (funcall gnus-auto-select-first))))
2928           ;; Don't select any articles, just move point to the first
2929           ;; article in the group.
2930           (goto-char (point-min))
2931           (gnus-summary-position-point)
2932           (gnus-configure-windows 'summary 'force)
2933           (gnus-set-mode-line 'summary))
2934         (when (get-buffer-window gnus-group-buffer t)
2935           ;; Gotta use windows, because recenter does weird stuff if
2936           ;; the current buffer ain't the displayed window.
2937           (let ((owin (selected-window)))
2938             (select-window (get-buffer-window gnus-group-buffer t))
2939             (when (gnus-group-goto-group group)
2940               (recenter))
2941             (select-window owin)))
2942         ;; Mark this buffer as "prepared".
2943         (setq gnus-newsgroup-prepared t)
2944         (gnus-run-hooks 'gnus-summary-prepared-hook)
2945         t)))))
2946
2947 (defun gnus-summary-prepare ()
2948   "Generate the summary buffer."
2949   (interactive)
2950   (let ((buffer-read-only nil))
2951     (erase-buffer)
2952     (setq gnus-newsgroup-data nil
2953           gnus-newsgroup-data-reverse nil)
2954     (gnus-run-hooks 'gnus-summary-generate-hook)
2955     ;; Generate the buffer, either with threads or without.
2956     (when gnus-newsgroup-headers
2957       (gnus-summary-prepare-threads
2958        (if gnus-show-threads
2959            (gnus-sort-gathered-threads
2960             (funcall gnus-summary-thread-gathering-function
2961                      (gnus-sort-threads
2962                       (gnus-cut-threads (gnus-make-threads)))))
2963          ;; Unthreaded display.
2964          (gnus-sort-articles gnus-newsgroup-headers))))
2965     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2966     ;; Call hooks for modifying summary buffer.
2967     (goto-char (point-min))
2968     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2969
2970 (defsubst gnus-general-simplify-subject (subject)
2971   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2972   (setq subject
2973         (cond
2974          ;; Truncate the subject.
2975          (gnus-simplify-subject-functions
2976           (gnus-map-function gnus-simplify-subject-functions subject))
2977          ((numberp gnus-summary-gather-subject-limit)
2978           (setq subject (gnus-simplify-subject-re subject))
2979           (if (> (length subject) gnus-summary-gather-subject-limit)
2980               (substring subject 0 gnus-summary-gather-subject-limit)
2981             subject))
2982          ;; Fuzzily simplify it.
2983          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2984           (gnus-simplify-subject-fuzzy subject))
2985          ;; Just remove the leading "Re:".
2986          (t
2987           (gnus-simplify-subject-re subject))))
2988
2989   (if (and gnus-summary-gather-exclude-subject
2990            (string-match gnus-summary-gather-exclude-subject subject))
2991       nil                               ; This article shouldn't be gathered
2992     subject))
2993
2994 (defun gnus-summary-simplify-subject-query ()
2995   "Query where the respool algorithm would put this article."
2996   (interactive)
2997   (gnus-summary-select-article)
2998   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2999
3000 (defun gnus-gather-threads-by-subject (threads)
3001   "Gather threads by looking at Subject headers."
3002   (if (not gnus-summary-make-false-root)
3003       threads
3004     (let ((hashtb (gnus-make-hashtable 1024))
3005           (prev threads)
3006           (result threads)
3007           subject hthread whole-subject)
3008       (while threads
3009         (setq subject (gnus-general-simplify-subject
3010                        (setq whole-subject (mail-header-subject
3011                                             (caar threads)))))
3012         (when subject
3013           (if (setq hthread (gnus-gethash subject hashtb))
3014               (progn
3015                 ;; We enter a dummy root into the thread, if we
3016                 ;; haven't done that already.
3017                 (unless (stringp (caar hthread))
3018                   (setcar hthread (list whole-subject (car hthread))))
3019                 ;; We add this new gathered thread to this gathered
3020                 ;; thread.
3021                 (setcdr (car hthread)
3022                         (nconc (cdar hthread) (list (car threads))))
3023                 ;; Remove it from the list of threads.
3024                 (setcdr prev (cdr threads))
3025                 (setq threads prev))
3026             ;; Enter this thread into the hash table.
3027             (gnus-sethash subject threads hashtb)))
3028         (setq prev threads)
3029         (setq threads (cdr threads)))
3030       result)))
3031
3032 (defun gnus-gather-threads-by-references (threads)
3033   "Gather threads by looking at References headers."
3034   (let ((idhashtb (gnus-make-hashtable 1024))
3035         (thhashtb (gnus-make-hashtable 1024))
3036         (prev threads)
3037         (result threads)
3038         ids references id gthread gid entered ref)
3039     (while threads
3040       (when (setq references (mail-header-references (caar threads)))
3041         (setq id (mail-header-id (caar threads))
3042               ids (gnus-split-references references)
3043               entered nil)
3044         (while (setq ref (pop ids))
3045           (setq ids (delete ref ids))
3046           (if (not (setq gid (gnus-gethash ref idhashtb)))
3047               (progn
3048                 (gnus-sethash ref id idhashtb)
3049                 (gnus-sethash id threads thhashtb))
3050             (setq gthread (gnus-gethash gid thhashtb))
3051             (unless entered
3052               ;; We enter a dummy root into the thread, if we
3053               ;; haven't done that already.
3054               (unless (stringp (caar gthread))
3055                 (setcar gthread (list (mail-header-subject (caar gthread))
3056                                       (car gthread))))
3057               ;; We add this new gathered thread to this gathered
3058               ;; thread.
3059               (setcdr (car gthread)
3060                       (nconc (cdar gthread) (list (car threads)))))
3061             ;; Add it into the thread hash table.
3062             (gnus-sethash id gthread thhashtb)
3063             (setq entered t)
3064             ;; Remove it from the list of threads.
3065             (setcdr prev (cdr threads))
3066             (setq threads prev))))
3067       (setq prev threads)
3068       (setq threads (cdr threads)))
3069     result))
3070
3071 (defun gnus-sort-gathered-threads (threads)
3072   "Sort subtreads inside each gathered thread by article number."
3073   (let ((result threads))
3074     (while threads
3075       (when (stringp (caar threads))
3076         (setcdr (car threads)
3077                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3078       (setq threads (cdr threads)))
3079     result))
3080
3081 (defun gnus-thread-loop-p (root thread)
3082   "Say whether ROOT is in THREAD."
3083   (let ((stack (list thread))
3084         (infloop 0)
3085         th)
3086     (while (setq thread (pop stack))
3087       (setq th (cdr thread))
3088       (while (and th
3089                   (not (eq (caar th) root)))
3090         (pop th))
3091       (if th
3092           ;; We have found a loop.
3093           (let (ref-dep)
3094             (setcdr thread (delq (car th) (cdr thread)))
3095             (if (boundp (setq ref-dep (intern "none"
3096                                               gnus-newsgroup-dependencies)))
3097                 (setcdr (symbol-value ref-dep)
3098                         (nconc (cdr (symbol-value ref-dep))
3099                                (list (car th))))
3100               (set ref-dep (list nil (car th))))
3101             (setq infloop 1
3102                   stack nil))
3103         ;; Push all the subthreads onto the stack.
3104         (push (cdr thread) stack)))
3105     infloop))
3106
3107 (defun gnus-make-threads ()
3108   "Go through the dependency hashtb and find the roots.  Return all threads."
3109   (let (threads)
3110     (while (catch 'infloop
3111              (mapatoms
3112               (lambda (refs)
3113                 ;; Deal with self-referencing References loops.
3114                 (when (and (car (symbol-value refs))
3115                            (not (zerop
3116                                  (apply
3117                                   '+
3118                                   (mapcar
3119                                    (lambda (thread)
3120                                      (gnus-thread-loop-p
3121                                       (car (symbol-value refs)) thread))
3122                                    (cdr (symbol-value refs)))))))
3123                   (setq threads nil)
3124                   (throw 'infloop t))
3125                 (unless (car (symbol-value refs))
3126                   ;; These threads do not refer back to any other articles,
3127                   ;; so they're roots.
3128                   (setq threads (append (cdr (symbol-value refs)) threads))))
3129               gnus-newsgroup-dependencies)))
3130     threads))
3131
3132 ;; Build the thread tree.
3133 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3134   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3135
3136 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3137 if it was already present.
3138
3139 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3140 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3141 Message-IDs will be renamed be renamed to a unique Message-ID before
3142 being entered.
3143
3144 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3145   (let* ((id (mail-header-id header))
3146          (id-dep (and id (intern id dependencies)))
3147          ref ref-dep ref-header)
3148     ;; Enter this `header' in the `dependencies' table.
3149     (cond
3150      ((not id-dep)
3151       (setq header nil))
3152      ;; The first two cases do the normal part: enter a new `header'
3153      ;; in the `dependencies' table.
3154      ((not (boundp id-dep))
3155       (set id-dep (list header)))
3156      ((null (car (symbol-value id-dep)))
3157       (setcar (symbol-value id-dep) header))
3158
3159      ;; From here the `header' was already present in the
3160      ;; `dependencies' table.
3161      (force-new
3162       ;; Overrides an existing entry;
3163       ;; just set the header part of the entry.
3164       (setcar (symbol-value id-dep) header))
3165
3166      ;; Renames the existing `header' to a unique Message-ID.
3167      ((not gnus-summary-ignore-duplicates)
3168       ;; An article with this Message-ID has already been seen.
3169       ;; We rename the Message-ID.
3170       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3171            (list header))
3172       (mail-header-set-id header id))
3173
3174      ;; The last case ignores an existing entry, except it adds any
3175      ;; additional Xrefs (in case the two articles came from different
3176      ;; servers.
3177      ;; Also sets `header' to `nil' meaning that the `dependencies'
3178      ;; table was *not* modified.
3179      (t
3180       (mail-header-set-xref
3181        (car (symbol-value id-dep))
3182        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3183                    "")
3184                (or (mail-header-xref header) "")))
3185       (setq header nil)))
3186
3187     (when header
3188       ;; First check if that we are not creating a References loop.
3189       (setq ref (gnus-parent-id (mail-header-references header)))
3190       (while (and ref
3191                   (setq ref-dep (intern-soft ref dependencies))
3192                   (boundp ref-dep)
3193                   (setq ref-header (car (symbol-value ref-dep))))
3194         (if (string= id ref)
3195             ;; Yuk!  This is a reference loop.  Make the article be a
3196             ;; root article.
3197             (progn
3198               (mail-header-set-references (car (symbol-value id-dep)) "none")
3199               (setq ref nil))
3200           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3201       (setq ref (gnus-parent-id (mail-header-references header)))
3202       (setq ref-dep (intern (or ref "none") dependencies))
3203       (if (boundp ref-dep)
3204           (setcdr (symbol-value ref-dep)
3205                   (nconc (cdr (symbol-value ref-dep))
3206                          (list (symbol-value id-dep))))
3207         (set ref-dep (list nil (symbol-value id-dep)))))
3208     header))
3209
3210 (defun gnus-build-sparse-threads ()
3211   (let ((headers gnus-newsgroup-headers)
3212         (mail-parse-charset gnus-newsgroup-charset)
3213         (gnus-summary-ignore-duplicates t)
3214         header references generation relations
3215         subject child end new-child date)
3216     ;; First we create an alist of generations/relations, where
3217     ;; generations is how much we trust the relation, and the relation
3218     ;; is parent/child.
3219     (gnus-message 7 "Making sparse threads...")
3220     (save-excursion
3221       (nnheader-set-temp-buffer " *gnus sparse threads*")
3222       (while (setq header (pop headers))
3223         (when (and (setq references (mail-header-references header))
3224                    (not (string= references "")))
3225           (insert references)
3226           (setq child (mail-header-id header)
3227                 subject (mail-header-subject header)
3228                 date (mail-header-date header)
3229                 generation 0)
3230           (while (search-backward ">" nil t)
3231             (setq end (1+ (point)))
3232             (when (search-backward "<" nil t)
3233               (setq new-child (buffer-substring (point) end))
3234               (push (list (incf generation)
3235                           child (setq child new-child)
3236                           subject date)
3237                     relations)))
3238           (when child
3239             (push (list (1+ generation) child nil subject) relations))
3240           (erase-buffer)))
3241       (kill-buffer (current-buffer)))
3242     ;; Sort over trustworthiness.
3243     (mapcar
3244      (lambda (relation)
3245        (when (gnus-dependencies-add-header
3246               (make-full-mail-header
3247                gnus-reffed-article-number
3248                (nth 3 relation) "" (or (nth 4 relation) "")
3249                (nth 1 relation)
3250                (or (nth 2 relation) "") 0 0 "")
3251               gnus-newsgroup-dependencies nil)
3252          (push gnus-reffed-article-number gnus-newsgroup-limit)
3253          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3254          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3255                gnus-newsgroup-reads)
3256          (decf gnus-reffed-article-number)))
3257      (sort relations 'car-less-than-car))
3258     (gnus-message 7 "Making sparse threads...done")))
3259
3260 (defun gnus-build-old-threads ()
3261   ;; Look at all the articles that refer back to old articles, and
3262   ;; fetch the headers for the articles that aren't there.  This will
3263   ;; build complete threads - if the roots haven't been expired by the
3264   ;; server, that is.
3265   (let ((mail-parse-charset gnus-newsgroup-charset)
3266         id heads)
3267     (mapatoms
3268      (lambda (refs)
3269        (when (not (car (symbol-value refs)))
3270          (setq heads (cdr (symbol-value refs)))
3271          (while heads
3272            (if (memq (mail-header-number (caar heads))
3273                      gnus-newsgroup-dormant)
3274                (setq heads (cdr heads))
3275              (setq id (symbol-name refs))
3276              (while (and (setq id (gnus-build-get-header id))
3277                          (not (car (gnus-id-to-thread id)))))
3278              (setq heads nil)))))
3279      gnus-newsgroup-dependencies)))
3280
3281 ;; This function has to be called with point after the article number
3282 ;; on the beginning of the line.
3283 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3284   (let ((eol (gnus-point-at-eol))
3285         (buffer (current-buffer))
3286         header)
3287
3288     ;; overview: [num subject from date id refs chars lines misc]
3289     (unless (eobp)
3290       (forward-char))
3291
3292     (setq header
3293           (make-full-mail-header
3294            number                               ; number
3295            (nnheader-nov-field)                 ; subject
3296            (nnheader-nov-field)                 ; from
3297            (nnheader-nov-field)                 ; date
3298            (nnheader-nov-read-message-id)       ; id
3299            (nnheader-nov-field)                 ; refs
3300            (nnheader-nov-read-integer)          ; chars
3301            (nnheader-nov-read-integer)          ; lines
3302            (unless (eobp)
3303              (nnheader-nov-field))              ; misc
3304            (nnheader-nov-parse-extra)))         ; extra
3305
3306     (when gnus-alter-header-function
3307       (funcall gnus-alter-header-function header))
3308     (gnus-dependencies-add-header header dependencies force-new)))
3309
3310 (defun gnus-build-get-header (id)
3311   ;; Look through the buffer of NOV lines and find the header to
3312   ;; ID.  Enter this line into the dependencies hash table, and return
3313   ;; the id of the parent article (if any).
3314   (let ((deps gnus-newsgroup-dependencies)
3315         found header)
3316     (prog1
3317         (save-excursion
3318           (set-buffer nntp-server-buffer)
3319           (let ((case-fold-search nil))
3320             (goto-char (point-min))
3321             (while (and (not found)
3322                         (search-forward id nil t))
3323               (beginning-of-line)
3324               (setq found (looking-at
3325                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3326                                    (regexp-quote id))))
3327               (or found (beginning-of-line 2)))
3328             (when found
3329               (beginning-of-line)
3330               (and
3331                (setq header (gnus-nov-parse-line
3332                              (read (current-buffer)) deps))
3333                (gnus-parent-id (mail-header-references header))))))
3334       (when header
3335         (let ((number (mail-header-number header)))
3336           (push number gnus-newsgroup-limit)
3337           (push header gnus-newsgroup-headers)
3338           (if (memq number gnus-newsgroup-unselected)
3339               (progn
3340                 (push number gnus-newsgroup-unreads)
3341                 (setq gnus-newsgroup-unselected
3342                       (delq number gnus-newsgroup-unselected)))
3343             (push number gnus-newsgroup-ancient)))))))
3344
3345 (defun gnus-build-all-threads ()
3346   "Read all the headers."
3347   (let ((gnus-summary-ignore-duplicates t)
3348         (mail-parse-charset gnus-newsgroup-charset)
3349         (dependencies gnus-newsgroup-dependencies)
3350         header article)
3351     (save-excursion
3352       (set-buffer nntp-server-buffer)
3353       (let ((case-fold-search nil))
3354         (goto-char (point-min))
3355         (while (not (eobp))
3356           (ignore-errors
3357             (setq article (read (current-buffer))
3358                   header (gnus-nov-parse-line article dependencies)))
3359           (when header
3360             (save-excursion
3361               (set-buffer gnus-summary-buffer)
3362               (push header gnus-newsgroup-headers)
3363               (if (memq (setq article (mail-header-number header))
3364                         gnus-newsgroup-unselected)
3365                   (progn
3366                     (push article gnus-newsgroup-unreads)
3367                     (setq gnus-newsgroup-unselected
3368                           (delq article gnus-newsgroup-unselected)))
3369                 (push article gnus-newsgroup-ancient)))
3370             (forward-line 1)))))))
3371
3372 (defun gnus-summary-update-article-line (article header)
3373   "Update the line for ARTICLE using HEADERS."
3374   (let* ((id (mail-header-id header))
3375          (thread (gnus-id-to-thread id)))
3376     (unless thread
3377       (error "Article in no thread"))
3378     ;; Update the thread.
3379     (setcar thread header)
3380     (gnus-summary-goto-subject article)
3381     (let* ((datal (gnus-data-find-list article))
3382            (data (car datal))
3383            (length (when (cdr datal)
3384                      (- (gnus-data-pos data)
3385                         (gnus-data-pos (cadr datal)))))
3386            (buffer-read-only nil)
3387            (level (gnus-summary-thread-level)))
3388       (gnus-delete-line)
3389       (gnus-summary-insert-line
3390        header level nil (gnus-article-mark article)
3391        (memq article gnus-newsgroup-replied)
3392        (memq article gnus-newsgroup-expirable)
3393        ;; Only insert the Subject string when it's different
3394        ;; from the previous Subject string.
3395        (if (gnus-subject-equal
3396             (condition-case ()
3397                 (mail-header-subject
3398                  (gnus-data-header
3399                   (cadr
3400                    (gnus-data-find-list
3401                     article
3402                     (gnus-data-list t)))))
3403               ;; Error on the side of excessive subjects.
3404               (error ""))
3405             (mail-header-subject header))
3406            ""
3407          (mail-header-subject header))
3408        nil (cdr (assq article gnus-newsgroup-scored))
3409        (memq article gnus-newsgroup-processable))
3410       (when length
3411         (gnus-data-update-list
3412          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3413
3414 (defun gnus-summary-update-article (article &optional iheader)
3415   "Update ARTICLE in the summary buffer."
3416   (set-buffer gnus-summary-buffer)
3417   (let* ((header (gnus-summary-article-header article))
3418          (id (mail-header-id header))
3419          (data (gnus-data-find article))
3420          (thread (gnus-id-to-thread id))
3421          (references (mail-header-references header))
3422          (parent
3423           (gnus-id-to-thread
3424            (or (gnus-parent-id
3425                 (when (and references
3426                            (not (equal "" references)))
3427                   references))
3428                "none")))
3429          (buffer-read-only nil)
3430          (old (car thread)))
3431     (when thread
3432       (unless iheader
3433         (setcar thread nil)
3434         (when parent
3435           (delq thread parent)))
3436       (if (gnus-summary-insert-subject id header)
3437           ;; Set the (possibly) new article number in the data structure.
3438           (gnus-data-set-number data (gnus-id-to-article id))
3439         (setcar thread old)
3440         nil))))
3441
3442 (defun gnus-rebuild-thread (id &optional line)
3443   "Rebuild the thread containing ID.
3444 If LINE, insert the rebuilt thread starting on line LINE."
3445   (let ((buffer-read-only nil)
3446         old-pos current thread data)
3447     (if (not gnus-show-threads)
3448         (setq thread (list (car (gnus-id-to-thread id))))
3449       ;; Get the thread this article is part of.
3450       (setq thread (gnus-remove-thread id)))
3451     (setq old-pos (gnus-point-at-bol))
3452     (setq current (save-excursion
3453                     (and (zerop (forward-line -1))
3454                          (gnus-summary-article-number))))
3455     ;; If this is a gathered thread, we have to go some re-gathering.
3456     (when (stringp (car thread))
3457       (let ((subject (car thread))
3458             roots thr)
3459         (setq thread (cdr thread))
3460         (while thread
3461           (unless (memq (setq thr (gnus-id-to-thread
3462                                    (gnus-root-id
3463                                     (mail-header-id (caar thread)))))
3464                         roots)
3465             (push thr roots))
3466           (setq thread (cdr thread)))
3467         ;; We now have all (unique) roots.
3468         (if (= (length roots) 1)
3469             ;; All the loose roots are now one solid root.
3470             (setq thread (car roots))
3471           (setq thread (cons subject (gnus-sort-threads roots))))))
3472     (let (threads)
3473       ;; We then insert this thread into the summary buffer.
3474       (when line
3475         (goto-char (point-min))
3476         (forward-line (1- line)))
3477       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3478         (if gnus-show-threads
3479             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3480           (gnus-summary-prepare-unthreaded thread))
3481         (setq data (nreverse gnus-newsgroup-data))
3482         (setq threads gnus-newsgroup-threads))
3483       ;; We splice the new data into the data structure.
3484       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3485       ;;!!! then we want to insert at the beginning of the buffer.
3486       ;;!!! That happens to be true with Gnus now, but that may
3487       ;;!!! change in the future.  Perhaps.
3488       (gnus-data-enter-list
3489        (if line nil current) data (- (point) old-pos))
3490       (setq gnus-newsgroup-threads
3491             (nconc threads gnus-newsgroup-threads))
3492       (gnus-data-compute-positions))))
3493
3494 (defun gnus-number-to-header (number)
3495   "Return the header for article NUMBER."
3496   (let ((headers gnus-newsgroup-headers))
3497     (while (and headers
3498                 (not (= number (mail-header-number (car headers)))))
3499       (pop headers))
3500     (when headers
3501       (car headers))))
3502
3503 (defun gnus-parent-headers (in-headers &optional generation)
3504   "Return the headers of the GENERATIONeth parent of HEADERS."
3505   (unless generation
3506     (setq generation 1))
3507   (let ((parent t)
3508         (headers in-headers)
3509         references)
3510     (while (and parent
3511                 (not (zerop generation))
3512                 (setq references (mail-header-references headers)))
3513       (setq headers (if (and references
3514                              (setq parent (gnus-parent-id references)))
3515                         (car (gnus-id-to-thread parent))
3516                       nil))
3517       (decf generation))
3518     (and (not (eq headers in-headers))
3519          headers)))
3520
3521 (defun gnus-id-to-thread (id)
3522   "Return the (sub-)thread where ID appears."
3523   (gnus-gethash id gnus-newsgroup-dependencies))
3524
3525 (defun gnus-id-to-article (id)
3526   "Return the article number of ID."
3527   (let ((thread (gnus-id-to-thread id)))
3528     (when (and thread
3529                (car thread))
3530       (mail-header-number (car thread)))))
3531
3532 (defun gnus-id-to-header (id)
3533   "Return the article headers of ID."
3534   (car (gnus-id-to-thread id)))
3535
3536 (defun gnus-article-displayed-root-p (article)
3537   "Say whether ARTICLE is a root(ish) article."
3538   (let ((level (gnus-summary-thread-level article))
3539         (refs (mail-header-references  (gnus-summary-article-header article)))
3540         particle)
3541     (cond
3542      ((null level) nil)
3543      ((zerop level) t)
3544      ((null refs) t)
3545      ((null (gnus-parent-id refs)) t)
3546      ((and (= 1 level)
3547            (null (setq particle (gnus-id-to-article
3548                                  (gnus-parent-id refs))))
3549            (null (gnus-summary-thread-level particle)))))))
3550
3551 (defun gnus-root-id (id)
3552   "Return the id of the root of the thread where ID appears."
3553   (let (last-id prev)
3554     (while (and id (setq prev (car (gnus-id-to-thread id))))
3555       (setq last-id id
3556             id (gnus-parent-id (mail-header-references prev))))
3557     last-id))
3558
3559 (defun gnus-articles-in-thread (thread)
3560   "Return the list of articles in THREAD."
3561   (cons (mail-header-number (car thread))
3562         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3563
3564 (defun gnus-remove-thread (id &optional dont-remove)
3565   "Remove the thread that has ID in it."
3566   (let (headers thread last-id)
3567     ;; First go up in this thread until we find the root.
3568     (setq last-id (gnus-root-id id)
3569           headers (message-flatten-list (gnus-id-to-thread last-id)))
3570     ;; We have now found the real root of this thread.  It might have
3571     ;; been gathered into some loose thread, so we have to search
3572     ;; through the threads to find the thread we wanted.
3573     (let ((threads gnus-newsgroup-threads)
3574           sub)
3575       (while threads
3576         (setq sub (car threads))
3577         (if (stringp (car sub))
3578             ;; This is a gathered thread, so we look at the roots
3579             ;; below it to find whether this article is in this
3580             ;; gathered root.
3581             (progn
3582               (setq sub (cdr sub))
3583               (while sub
3584                 (when (member (caar sub) headers)
3585                   (setq thread (car threads)
3586                         threads nil
3587                         sub nil))
3588                 (setq sub (cdr sub))))
3589           ;; It's an ordinary thread, so we check it.
3590           (when (eq (car sub) (car headers))
3591             (setq thread sub
3592                   threads nil)))
3593         (setq threads (cdr threads)))
3594       ;; If this article is in no thread, then it's a root.
3595       (if thread
3596           (unless dont-remove
3597             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3598         (setq thread (gnus-id-to-thread last-id)))
3599       (when thread
3600         (prog1
3601             thread                      ; We return this thread.
3602           (unless dont-remove
3603             (if (stringp (car thread))
3604                 (progn
3605                   ;; If we use dummy roots, then we have to remove the
3606                   ;; dummy root as well.
3607                   (when (eq gnus-summary-make-false-root 'dummy)
3608                     ;; We go to the dummy root by going to
3609                     ;; the first sub-"thread", and then one line up.
3610                     (gnus-summary-goto-article
3611                      (mail-header-number (caadr thread)))
3612                     (forward-line -1)
3613                     (gnus-delete-line)
3614                     (gnus-data-compute-positions))
3615                   (setq thread (cdr thread))
3616                   (while thread
3617                     (gnus-remove-thread-1 (car thread))
3618                     (setq thread (cdr thread))))
3619               (gnus-summary-show-all-threads)
3620               (gnus-remove-thread-1 thread))))))))
3621
3622 (defun gnus-remove-thread-1 (thread)
3623   "Remove the thread THREAD recursively."
3624   (let ((number (mail-header-number (pop thread)))
3625         d)
3626     (setq thread (reverse thread))
3627     (while thread
3628       (gnus-remove-thread-1 (pop thread)))
3629     (when (setq d (gnus-data-find number))
3630       (goto-char (gnus-data-pos d))
3631       (gnus-data-remove
3632        number
3633        (- (gnus-point-at-bol)
3634           (prog1
3635               (1+ (gnus-point-at-eol))
3636             (gnus-delete-line)))))))
3637
3638 (defun gnus-sort-threads (threads)
3639   "Sort THREADS."
3640   (if (not gnus-thread-sort-functions)
3641       threads
3642     (gnus-message 8 "Sorting threads...")
3643     (prog1
3644         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3645       (gnus-message 8 "Sorting threads...done"))))
3646
3647 (defun gnus-sort-articles (articles)
3648   "Sort ARTICLES."
3649   (when gnus-article-sort-functions
3650     (gnus-message 7 "Sorting articles...")
3651     (prog1
3652         (setq gnus-newsgroup-headers
3653               (sort articles (gnus-make-sort-function
3654                               gnus-article-sort-functions)))
3655       (gnus-message 7 "Sorting articles...done"))))
3656
3657 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3658 (defmacro gnus-thread-header (thread)
3659   ;; Return header of first article in THREAD.
3660   ;; Note that THREAD must never, ever be anything else than a variable -
3661   ;; using some other form will lead to serious barfage.
3662   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3663   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3664   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3665         (vector thread) 2))
3666
3667 (defsubst gnus-article-sort-by-number (h1 h2)
3668   "Sort articles by article number."
3669   (< (mail-header-number h1)
3670      (mail-header-number h2)))
3671
3672 (defun gnus-thread-sort-by-number (h1 h2)
3673   "Sort threads by root article number."
3674   (gnus-article-sort-by-number
3675    (gnus-thread-header h1) (gnus-thread-header h2)))
3676
3677 (defsubst gnus-article-sort-by-lines (h1 h2)
3678   "Sort articles by article Lines header."
3679   (< (mail-header-lines h1)
3680      (mail-header-lines h2)))
3681
3682 (defun gnus-thread-sort-by-lines (h1 h2)
3683   "Sort threads by root article Lines header."
3684   (gnus-article-sort-by-lines
3685    (gnus-thread-header h1) (gnus-thread-header h2)))
3686
3687 (defsubst gnus-article-sort-by-chars (h1 h2)
3688   "Sort articles by octet length."
3689   (< (mail-header-chars h1)
3690      (mail-header-chars h2)))
3691
3692 (defun gnus-thread-sort-by-chars (h1 h2)
3693   "Sort threads by root article octet length."
3694   (gnus-article-sort-by-chars
3695    (gnus-thread-header h1) (gnus-thread-header h2)))
3696
3697 (defsubst gnus-article-sort-by-author (h1 h2)
3698   "Sort articles by root author."
3699   (string-lessp
3700    (let ((addr (car (mime-read-field 'From h1))))
3701      (or (std11-full-name-string addr)
3702          (std11-address-string addr)
3703          ""))
3704    (let ((addr (car (mime-read-field 'From h2))))
3705      (or (std11-full-name-string addr)
3706          (std11-address-string addr)
3707          ""))
3708    ))
3709
3710 (defun gnus-thread-sort-by-author (h1 h2)
3711   "Sort threads by root author."
3712   (gnus-article-sort-by-author
3713    (gnus-thread-header h1)  (gnus-thread-header h2)))
3714
3715 (defsubst gnus-article-sort-by-subject (h1 h2)
3716   "Sort articles by root subject."
3717   (string-lessp
3718    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3719    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3720
3721 (defun gnus-thread-sort-by-subject (h1 h2)
3722   "Sort threads by root subject."
3723   (gnus-article-sort-by-subject
3724    (gnus-thread-header h1) (gnus-thread-header h2)))
3725
3726 (defsubst gnus-article-sort-by-date (h1 h2)
3727   "Sort articles by root article date."
3728   (time-less-p
3729    (gnus-date-get-time (mail-header-date h1))
3730    (gnus-date-get-time (mail-header-date h2))))
3731
3732 (defun gnus-thread-sort-by-date (h1 h2)
3733   "Sort threads by root article date."
3734   (gnus-article-sort-by-date
3735    (gnus-thread-header h1) (gnus-thread-header h2)))
3736
3737 (defsubst gnus-article-sort-by-score (h1 h2)
3738   "Sort articles by root article score.
3739 Unscored articles will be counted as having a score of zero."
3740   (> (or (cdr (assq (mail-header-number h1)
3741                     gnus-newsgroup-scored))
3742          gnus-summary-default-score 0)
3743      (or (cdr (assq (mail-header-number h2)
3744                     gnus-newsgroup-scored))
3745          gnus-summary-default-score 0)))
3746
3747 (defun gnus-thread-sort-by-score (h1 h2)
3748   "Sort threads by root article score."
3749   (gnus-article-sort-by-score
3750    (gnus-thread-header h1) (gnus-thread-header h2)))
3751
3752 (defun gnus-thread-sort-by-total-score (h1 h2)
3753   "Sort threads by the sum of all scores in the thread.
3754 Unscored articles will be counted as having a score of zero."
3755   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3756
3757 (defun gnus-thread-total-score (thread)
3758   ;; This function find the total score of THREAD.
3759   (cond ((null thread)
3760          0)
3761         ((consp thread)
3762          (if (stringp (car thread))
3763              (apply gnus-thread-score-function 0
3764                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3765            (gnus-thread-total-score-1 thread)))
3766         (t
3767          (gnus-thread-total-score-1 (list thread)))))
3768
3769 (defun gnus-thread-total-score-1 (root)
3770   ;; This function find the total score of the thread below ROOT.
3771   (setq root (car root))
3772   (apply gnus-thread-score-function
3773          (or (append
3774               (mapcar 'gnus-thread-total-score
3775                       (cdr (gnus-id-to-thread (mail-header-id root))))
3776               (when (> (mail-header-number root) 0)
3777                 (list (or (cdr (assq (mail-header-number root)
3778                                      gnus-newsgroup-scored))
3779                           gnus-summary-default-score 0))))
3780              (list gnus-summary-default-score)
3781              '(0))))
3782
3783 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3784 (defvar gnus-tmp-prev-subject nil)
3785 (defvar gnus-tmp-false-parent nil)
3786 (defvar gnus-tmp-root-expunged nil)
3787 (defvar gnus-tmp-dummy-line nil)
3788
3789 (defvar gnus-tmp-header)
3790 (defun gnus-extra-header (type &optional header)
3791   "Return the extra header of TYPE."
3792   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3793       ""))
3794
3795 (defun gnus-summary-prepare-threads (threads)
3796   "Prepare summary buffer from THREADS and indentation LEVEL.
3797 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3798 or a straight list of headers."
3799   (gnus-message 7 "Generating summary...")
3800
3801   (setq gnus-newsgroup-threads threads)
3802   (beginning-of-line)
3803
3804   (let ((gnus-tmp-level 0)
3805         (default-score (or gnus-summary-default-score 0))
3806         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3807         thread number subject stack state gnus-tmp-gathered beg-match
3808         new-roots gnus-tmp-new-adopts thread-end
3809         gnus-tmp-header gnus-tmp-unread
3810         gnus-tmp-replied gnus-tmp-subject-or-nil
3811         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3812         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3813         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3814
3815     (setq gnus-tmp-prev-subject nil)
3816
3817     (if (vectorp (car threads))
3818         ;; If this is a straight (sic) list of headers, then a
3819         ;; threaded summary display isn't required, so we just create
3820         ;; an unthreaded one.
3821         (gnus-summary-prepare-unthreaded threads)
3822
3823       ;; Do the threaded display.
3824
3825       (while (or threads stack gnus-tmp-new-adopts new-roots)
3826
3827         (if (and (= gnus-tmp-level 0)
3828                  (or (not stack)
3829                      (= (caar stack) 0))
3830                  (not gnus-tmp-false-parent)
3831                  (or gnus-tmp-new-adopts new-roots))
3832             (if gnus-tmp-new-adopts
3833                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3834                       thread (list (car gnus-tmp-new-adopts))
3835                       gnus-tmp-header (caar thread)
3836                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3837               (when new-roots
3838                 (setq thread (list (car new-roots))
3839                       gnus-tmp-header (caar thread)
3840                       new-roots (cdr new-roots))))
3841
3842           (if threads
3843               ;; If there are some threads, we do them before the
3844               ;; threads on the stack.
3845               (setq thread threads
3846                     gnus-tmp-header (caar thread))
3847             ;; There were no current threads, so we pop something off
3848             ;; the stack.
3849             (setq state (car stack)
3850                   gnus-tmp-level (car state)
3851                   thread (cdr state)
3852                   stack (cdr stack)
3853                   gnus-tmp-header (caar thread))))
3854
3855         (setq gnus-tmp-false-parent nil)
3856         (setq gnus-tmp-root-expunged nil)
3857         (setq thread-end nil)
3858
3859         (if (stringp gnus-tmp-header)
3860             ;; The header is a dummy root.
3861             (cond
3862              ((eq gnus-summary-make-false-root 'adopt)
3863               ;; We let the first article adopt the rest.
3864               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3865                                                (cddar thread)))
3866               (setq gnus-tmp-gathered
3867                     (nconc (mapcar
3868                             (lambda (h) (mail-header-number (car h)))
3869                             (cddar thread))
3870                            gnus-tmp-gathered))
3871               (setq thread (cons (list (caar thread)
3872                                        (cadar thread))
3873                                  (cdr thread)))
3874               (setq gnus-tmp-level -1
3875                     gnus-tmp-false-parent t))
3876              ((eq gnus-summary-make-false-root 'empty)
3877               ;; We print adopted articles with empty subject fields.
3878               (setq gnus-tmp-gathered
3879                     (nconc (mapcar
3880                             (lambda (h) (mail-header-number (car h)))
3881                             (cddar thread))
3882                            gnus-tmp-gathered))
3883               (setq gnus-tmp-level -1))
3884              ((eq gnus-summary-make-false-root 'dummy)
3885               ;; We remember that we probably want to output a dummy
3886               ;; root.
3887               (setq gnus-tmp-dummy-line gnus-tmp-header)
3888               (setq gnus-tmp-prev-subject gnus-tmp-header))
3889              (t
3890               ;; We do not make a root for the gathered
3891               ;; sub-threads at all.
3892               (setq gnus-tmp-level -1)))
3893
3894           (setq number (mail-header-number gnus-tmp-header)
3895                 subject (mail-header-subject gnus-tmp-header))
3896
3897           (cond
3898            ;; If the thread has changed subject, we might want to make
3899            ;; this subthread into a root.
3900            ((and (null gnus-thread-ignore-subject)
3901                  (not (zerop gnus-tmp-level))
3902                  gnus-tmp-prev-subject
3903                  (not (inline
3904                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3905             (setq new-roots (nconc new-roots (list (car thread)))
3906                   thread-end t
3907                   gnus-tmp-header nil))
3908            ;; If the article lies outside the current limit,
3909            ;; then we do not display it.
3910            ((not (memq number gnus-newsgroup-limit))
3911             (setq gnus-tmp-gathered
3912                   (nconc (mapcar
3913                           (lambda (h) (mail-header-number (car h)))
3914                           (cdar thread))
3915                          gnus-tmp-gathered))
3916             (setq gnus-tmp-new-adopts (if (cdar thread)
3917                                           (append gnus-tmp-new-adopts
3918                                                   (cdar thread))
3919                                         gnus-tmp-new-adopts)
3920                   thread-end t
3921                   gnus-tmp-header nil)
3922             (when (zerop gnus-tmp-level)
3923               (setq gnus-tmp-root-expunged t)))
3924            ;; Perhaps this article is to be marked as read?
3925            ((and gnus-summary-mark-below
3926                  (< (or (cdr (assq number gnus-newsgroup-scored))
3927                         default-score)
3928                     gnus-summary-mark-below)
3929                  ;; Don't touch sparse articles.
3930                  (not (gnus-summary-article-sparse-p number))
3931                  (not (gnus-summary-article-ancient-p number)))
3932             (setq gnus-newsgroup-unreads
3933                   (delq number gnus-newsgroup-unreads))
3934             (if gnus-newsgroup-auto-expire
3935                 (push number gnus-newsgroup-expirable)
3936               (push (cons number gnus-low-score-mark)
3937                     gnus-newsgroup-reads))))
3938
3939           (when gnus-tmp-header
3940             ;; We may have an old dummy line to output before this
3941             ;; article.
3942             (when (and gnus-tmp-dummy-line
3943                        (gnus-subject-equal
3944                         gnus-tmp-dummy-line
3945                         (mail-header-subject gnus-tmp-header)))
3946               (gnus-summary-insert-dummy-line
3947                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3948               (setq gnus-tmp-dummy-line nil))
3949
3950             ;; Compute the mark.
3951             (setq gnus-tmp-unread (gnus-article-mark number))
3952
3953             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3954                                   gnus-tmp-header gnus-tmp-level)
3955                   gnus-newsgroup-data)
3956
3957             ;; Actually insert the line.
3958             (setq
3959              gnus-tmp-subject-or-nil
3960              (cond
3961               ((and gnus-thread-ignore-subject
3962                     gnus-tmp-prev-subject
3963                     (not (inline (gnus-subject-equal
3964                                   gnus-tmp-prev-subject subject))))
3965                subject)
3966               ((zerop gnus-tmp-level)
3967                (if (and (eq gnus-summary-make-false-root 'empty)
3968                         (memq number gnus-tmp-gathered)
3969                         gnus-tmp-prev-subject
3970                         (inline (gnus-subject-equal
3971                                  gnus-tmp-prev-subject subject)))
3972                    gnus-summary-same-subject
3973                  subject))
3974               (t gnus-summary-same-subject)))
3975             (if (and (eq gnus-summary-make-false-root 'adopt)
3976                      (= gnus-tmp-level 1)
3977                      (memq number gnus-tmp-gathered))
3978                 (setq gnus-tmp-opening-bracket ?\<
3979                       gnus-tmp-closing-bracket ?\>)
3980               (setq gnus-tmp-opening-bracket ?\[
3981                     gnus-tmp-closing-bracket ?\]))
3982             (setq
3983              gnus-tmp-indentation
3984              (aref gnus-thread-indent-array gnus-tmp-level)
3985              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3986              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3987                                 gnus-summary-default-score 0)
3988              gnus-tmp-score-char
3989              (if (or (null gnus-summary-default-score)
3990                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3991                          gnus-summary-zcore-fuzz))
3992                  ?  ;Whitespace
3993                (if (< gnus-tmp-score gnus-summary-default-score)
3994                    gnus-score-below-mark gnus-score-over-mark))
3995              gnus-tmp-replied
3996              (cond ((memq number gnus-newsgroup-processable)
3997                     gnus-process-mark)
3998                    ((memq number gnus-newsgroup-cached)
3999                     gnus-cached-mark)
4000                    ((memq number gnus-newsgroup-replied)
4001                     gnus-replied-mark)
4002                    ((memq number gnus-newsgroup-saved)
4003                     gnus-saved-mark)
4004                    (t gnus-unread-mark))
4005              gnus-tmp-from (mail-header-from gnus-tmp-header)
4006              gnus-tmp-name
4007              (cond
4008               ((string-match "<[^>]+> *$" gnus-tmp-from)
4009                (setq beg-match (match-beginning 0))
4010                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4011                         (substring gnus-tmp-from (1+ (match-beginning 0))
4012                                    (1- (match-end 0))))
4013                    (substring gnus-tmp-from 0 beg-match)))
4014               ((string-match "(.+)" gnus-tmp-from)
4015                (substring gnus-tmp-from
4016                           (1+ (match-beginning 0)) (1- (match-end 0))))
4017               (t gnus-tmp-from)))
4018             (when (string= gnus-tmp-name "")
4019               (setq gnus-tmp-name gnus-tmp-from))
4020             (unless (numberp gnus-tmp-lines)
4021               (setq gnus-tmp-lines 0))
4022             (gnus-put-text-property
4023              (point)
4024              (progn (eval gnus-summary-line-format-spec) (point))
4025              'gnus-number number)
4026             (when gnus-visual-p
4027               (forward-line -1)
4028               (gnus-run-hooks 'gnus-summary-update-hook)
4029               (forward-line 1))
4030
4031             (setq gnus-tmp-prev-subject subject)))
4032
4033         (when (nth 1 thread)
4034           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4035         (incf gnus-tmp-level)
4036         (setq threads (if thread-end nil (cdar thread)))
4037         (unless threads
4038           (setq gnus-tmp-level 0)))))
4039   (gnus-message 7 "Generating summary...done"))
4040
4041 (defun gnus-summary-prepare-unthreaded (headers)
4042   "Generate an unthreaded summary buffer based on HEADERS."
4043   (let (header number mark)
4044
4045     (beginning-of-line)
4046
4047     (while headers
4048       ;; We may have to root out some bad articles...
4049       (when (memq (setq number (mail-header-number
4050                                 (setq header (pop headers))))
4051                   gnus-newsgroup-limit)
4052         ;; Mark article as read when it has a low score.
4053         (when (and gnus-summary-mark-below
4054                    (< (or (cdr (assq number gnus-newsgroup-scored))
4055                           gnus-summary-default-score 0)
4056                       gnus-summary-mark-below)
4057                    (not (gnus-summary-article-ancient-p number)))
4058           (setq gnus-newsgroup-unreads
4059                 (delq number gnus-newsgroup-unreads))
4060           (if gnus-newsgroup-auto-expire
4061               (push number gnus-newsgroup-expirable)
4062             (push (cons number gnus-low-score-mark)
4063                   gnus-newsgroup-reads)))
4064
4065         (setq mark (gnus-article-mark number))
4066         (push (gnus-data-make number mark (1+ (point)) header 0)
4067               gnus-newsgroup-data)
4068         (gnus-summary-insert-line
4069          header 0 number
4070          mark (memq number gnus-newsgroup-replied)
4071          (memq number gnus-newsgroup-expirable)
4072          (mail-header-subject header) nil
4073          (cdr (assq number gnus-newsgroup-scored))
4074          (memq number gnus-newsgroup-processable))))))
4075
4076 (defun gnus-summary-remove-list-identifiers ()
4077   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4078   (let ((regexp (if (stringp gnus-list-identifiers)
4079                     gnus-list-identifiers
4080                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4081     (dolist (header gnus-newsgroup-headers)
4082       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4083                           (mail-header-subject header))
4084         (mail-header-set-subject
4085          header (concat (substring (mail-header-subject header)
4086                                    0 (match-beginning 2))
4087                         (substring (mail-header-subject header)
4088                                    (match-end 2))))))))
4089
4090 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4091   "Select newsgroup GROUP.
4092 If READ-ALL is non-nil, all articles in the group are selected.
4093 If SELECT-ARTICLES, only select those articles from GROUP."
4094   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4095          ;;!!! Dirty hack; should be removed.
4096          (gnus-summary-ignore-duplicates
4097           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4098               t
4099             gnus-summary-ignore-duplicates))
4100          (info (nth 2 entry))
4101          articles fetched-articles cached)
4102
4103     (unless (gnus-check-server
4104              (setq gnus-current-select-method
4105                    (gnus-find-method-for-group group)))
4106       (error "Couldn't open server"))
4107
4108     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4109         (gnus-activate-group group)     ; Or we can activate it...
4110         (progn                          ; Or we bug out.
4111           (when (equal major-mode 'gnus-summary-mode)
4112             (kill-buffer (current-buffer)))
4113           (error "Couldn't request group %s: %s"
4114                  group (gnus-status-message group))))
4115
4116     (unless (gnus-request-group group t)
4117       (when (equal major-mode 'gnus-summary-mode)
4118         (kill-buffer (current-buffer)))
4119       (error "Couldn't request group %s: %s"
4120              group (gnus-status-message group)))
4121
4122     (setq gnus-newsgroup-name group)
4123     (setq gnus-newsgroup-unselected nil)
4124     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4125     (gnus-summary-setup-default-charset)
4126
4127     ;; Adjust and set lists of article marks.
4128     (when info
4129       (gnus-adjust-marked-articles info))
4130
4131     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4132     (setq cached
4133           (if (gnus-virtual-group-p group)
4134               gnus-newsgroup-cached
4135             (gnus-cache-articles-in-group group)))
4136
4137     (setq gnus-newsgroup-unreads
4138           (gnus-set-difference
4139            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4140            gnus-newsgroup-dormant))
4141
4142     (setq gnus-newsgroup-processable nil)
4143
4144     (gnus-update-read-articles group gnus-newsgroup-unreads)
4145
4146     (if (setq articles select-articles)
4147         (setq gnus-newsgroup-unselected
4148               (gnus-sorted-intersection
4149                gnus-newsgroup-unreads
4150                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4151       (setq articles (gnus-articles-to-read group read-all)))
4152
4153     (cond
4154      ((null articles)
4155       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4156       'quit)
4157      ((eq articles 0) nil)
4158      (t
4159       ;; Init the dependencies hash table.
4160       (setq gnus-newsgroup-dependencies
4161             (gnus-make-hashtable (length articles)))
4162       (gnus-set-global-variables)
4163       ;; Retrieve the headers and read them in.
4164       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4165       (setq gnus-newsgroup-headers
4166             (gnus-retrieve-parsed-headers
4167              articles gnus-newsgroup-name
4168              ;; We might want to fetch old headers, but
4169              ;; not if there is only 1 article.
4170              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4171                            (not (numberp gnus-fetch-old-headers)))
4172                       (> (length articles) 1))
4173                   gnus-fetch-old-headers)))
4174       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4175
4176       ;; Suppress duplicates?
4177       (when gnus-suppress-duplicates
4178         (gnus-dup-suppress-articles))
4179
4180       ;; Set the initial limit.
4181       (setq gnus-newsgroup-limit (copy-sequence articles))
4182       ;; Remove canceled articles from the list of unread articles.
4183       (setq gnus-newsgroup-unreads
4184             (gnus-set-sorted-intersection
4185              gnus-newsgroup-unreads
4186              (setq fetched-articles
4187                    (mapcar (lambda (headers) (mail-header-number headers))
4188                            gnus-newsgroup-headers))))
4189       ;; Removed marked articles that do not exist.
4190       (gnus-update-missing-marks
4191        (gnus-sorted-complement fetched-articles articles))
4192
4193       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4194       (when cached
4195         (setq gnus-newsgroup-cached cached))
4196
4197       ;; We might want to build some more threads first.
4198       (when (and gnus-fetch-old-headers
4199                  (eq gnus-headers-retrieved-by 'nov))
4200         (if (eq gnus-fetch-old-headers 'invisible)
4201             (gnus-build-all-threads)
4202           (gnus-build-old-threads)))
4203       ;; Let the Gnus agent mark articles as read.
4204       (when gnus-agent
4205         (gnus-agent-get-undownloaded-list))
4206       ;; Remove list identifiers from subject
4207       (when gnus-list-identifiers
4208         (gnus-summary-remove-list-identifiers))
4209       ;; Check whether auto-expire is to be done in this group.
4210       (setq gnus-newsgroup-auto-expire
4211             (gnus-group-auto-expirable-p group))
4212       ;; Set up the article buffer now, if necessary.
4213       (unless gnus-single-article-buffer
4214         (gnus-article-setup-buffer))
4215       ;; First and last article in this newsgroup.
4216       (when gnus-newsgroup-headers
4217         (setq gnus-newsgroup-begin
4218               (mail-header-number (car gnus-newsgroup-headers))
4219               gnus-newsgroup-end
4220               (mail-header-number
4221                (gnus-last-element gnus-newsgroup-headers))))
4222       ;; GROUP is successfully selected.
4223       (or gnus-newsgroup-headers t)))))
4224
4225 (defun gnus-articles-to-read (group &optional read-all)
4226   ;; Find out what articles the user wants to read.
4227   (let* ((articles
4228           ;; Select all articles if `read-all' is non-nil, or if there
4229           ;; are no unread articles.
4230           (if (or read-all
4231                   (and (zerop (length gnus-newsgroup-marked))
4232                        (zerop (length gnus-newsgroup-unreads)))
4233                   (eq (gnus-group-find-parameter group 'display)
4234                       'all))
4235               (or
4236                (gnus-uncompress-range (gnus-active group))
4237                (gnus-cache-articles-in-group group))
4238             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4239                           (copy-sequence gnus-newsgroup-unreads))
4240                   '<)))
4241          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4242          (scored (length scored-list))
4243          (number (length articles))
4244          (marked (+ (length gnus-newsgroup-marked)
4245                     (length gnus-newsgroup-dormant)))
4246          (select
4247           (cond
4248            ((numberp read-all)
4249             read-all)
4250            (t
4251             (condition-case ()
4252                 (cond
4253                  ((and (or (<= scored marked) (= scored number))
4254                        (natnump gnus-large-newsgroup)
4255                        (> number gnus-large-newsgroup))
4256                   (let* ((cursor-in-echo-area nil)
4257                          (input (read-from-minibuffer
4258                                  (format
4259                                   "How many articles from %s (max %d): "
4260                                   (gnus-limit-string gnus-newsgroup-name 35)
4261                                   number)
4262                                  (cons (number-to-string gnus-large-newsgroup)
4263                                        0))))
4264                     (if (string-match "^[ \t]*$" input)
4265                         number
4266                       input)))
4267                  ((and (> scored marked) (< scored number)
4268                        (> (- scored number) 20))
4269                   (let ((input
4270                          (read-string
4271                           (format "%s %s (%d scored, %d total): "
4272                                   "How many articles from"
4273                                   group scored number))))
4274                     (if (string-match "^[ \t]*$" input)
4275                         number input)))
4276                  (t number))
4277               (quit nil))))))
4278     (setq select (if (stringp select) (string-to-number select) select))
4279     (if (or (null select) (zerop select))
4280         select
4281       (if (and (not (zerop scored)) (<= (abs select) scored))
4282           (progn
4283             (setq articles (sort scored-list '<))
4284             (setq number (length articles)))
4285         (setq articles (copy-sequence articles)))
4286
4287       (when (< (abs select) number)
4288         (if (< select 0)
4289             ;; Select the N oldest articles.
4290             (setcdr (nthcdr (1- (abs select)) articles) nil)
4291           ;; Select the N most recent articles.
4292           (setq articles (nthcdr (- number select) articles))))
4293       (setq gnus-newsgroup-unselected
4294             (gnus-sorted-intersection
4295              gnus-newsgroup-unreads
4296              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4297       articles)))
4298
4299 (defun gnus-killed-articles (killed articles)
4300   (let (out)
4301     (while articles
4302       (when (inline (gnus-member-of-range (car articles) killed))
4303         (push (car articles) out))
4304       (setq articles (cdr articles)))
4305     out))
4306
4307 (defun gnus-uncompress-marks (marks)
4308   "Uncompress the mark ranges in MARKS."
4309   (let ((uncompressed '(score bookmark))
4310         out)
4311     (while marks
4312       (if (memq (caar marks) uncompressed)
4313           (push (car marks) out)
4314         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4315       (setq marks (cdr marks)))
4316     out))
4317
4318 (defun gnus-adjust-marked-articles (info)
4319   "Set all article lists and remove all marks that are no longer valid."
4320   (let* ((marked-lists (gnus-info-marks info))
4321          (active (gnus-active (gnus-info-group info)))
4322          (min (car active))
4323          (max (cdr active))
4324          (types gnus-article-mark-lists)
4325          (uncompressed '(score bookmark killed))
4326          marks var articles article mark)
4327
4328     (while marked-lists
4329       (setq marks (pop marked-lists))
4330       (set (setq var (intern (format "gnus-newsgroup-%s"
4331                                      (car (rassq (setq mark (car marks))
4332                                                  types)))))
4333            (if (memq (car marks) uncompressed) (cdr marks)
4334              (gnus-uncompress-range (cdr marks))))
4335
4336       (setq articles (symbol-value var))
4337
4338       ;; All articles have to be subsets of the active articles.
4339       (cond
4340        ;; Adjust "simple" lists.
4341        ((memq mark '(tick dormant expire reply save))
4342         (while articles
4343           (when (or (< (setq article (pop articles)) min) (> article max))
4344             (set var (delq article (symbol-value var))))))
4345        ;; Adjust assocs.
4346        ((memq mark uncompressed)
4347         (when (not (listp (cdr (symbol-value var))))
4348           (set var (list (symbol-value var))))
4349         (when (not (listp (cdr articles)))
4350           (setq articles (list articles)))
4351         (while articles
4352           (when (or (not (consp (setq article (pop articles))))
4353                     (< (car article) min)
4354                     (> (car article) max))
4355             (set var (delq article (symbol-value var))))))))))
4356
4357 (defun gnus-update-missing-marks (missing)
4358   "Go through the list of MISSING articles and remove them from the mark lists."
4359   (when missing
4360     (let ((types gnus-article-mark-lists)
4361           var m)
4362       ;; Go through all types.
4363       (while types
4364         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4365         (when (symbol-value var)
4366           ;; This list has articles.  So we delete all missing articles
4367           ;; from it.
4368           (setq m missing)
4369           (while m
4370             (set var (delq (pop m) (symbol-value var)))))))))
4371
4372 (defun gnus-update-marks ()
4373   "Enter the various lists of marked articles into the newsgroup info list."
4374   (let ((types gnus-article-mark-lists)
4375         (info (gnus-get-info gnus-newsgroup-name))
4376         (uncompressed '(score bookmark killed))
4377         type list newmarked symbol delta-marks)
4378     (when info
4379       ;; Add all marks lists to the list of marks lists.
4380       (while (setq type (pop types))
4381         (setq list (symbol-value
4382                     (setq symbol
4383                           (intern (format "gnus-newsgroup-%s"
4384                                           (car type))))))
4385
4386         (when list
4387           ;; Get rid of the entries of the articles that have the
4388           ;; default score.
4389           (when (and (eq (cdr type) 'score)
4390                      gnus-save-score
4391                      list)
4392             (let* ((arts list)
4393                    (prev (cons nil list))
4394                    (all prev))
4395               (while arts
4396                 (if (or (not (consp (car arts)))
4397                         (= (cdar arts) gnus-summary-default-score))
4398                     (setcdr prev (cdr arts))
4399                   (setq prev arts))
4400                 (setq arts (cdr arts)))
4401               (setq list (cdr all)))))
4402
4403         (unless (memq (cdr type) uncompressed)
4404           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4405        
4406         (when (gnus-check-backend-function
4407                'request-set-mark gnus-newsgroup-name)
4408           ;; uncompressed:s are not proper flags (they are cons cells)
4409           ;; cache is a internal gnus flag
4410           (unless (memq (cdr type) (cons 'cache uncompressed))
4411             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4412                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4413                    (add (gnus-remove-from-range
4414                          (gnus-copy-sequence list) old)))
4415               (when add
4416                 (push (list add 'add (list (cdr type))) delta-marks))
4417               (when del
4418                 (push (list del 'del (list (cdr type))) delta-marks)))))
4419           
4420         (when list
4421           (push (cons (cdr type) list) newmarked)))
4422
4423       (when delta-marks
4424         (unless (gnus-check-group gnus-newsgroup-name)
4425           (error "Can't open server for %s" gnus-newsgroup-name))
4426         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4427           
4428       ;; Enter these new marks into the info of the group.
4429       (if (nthcdr 3 info)
4430           (setcar (nthcdr 3 info) newmarked)
4431         ;; Add the marks lists to the end of the info.
4432         (when newmarked
4433           (setcdr (nthcdr 2 info) (list newmarked))))
4434
4435       ;; Cut off the end of the info if there's nothing else there.
4436       (let ((i 5))
4437         (while (and (> i 2)
4438                     (not (nth i info)))
4439           (when (nthcdr (decf i) info)
4440             (setcdr (nthcdr i info) nil)))))))
4441
4442 (defun gnus-set-mode-line (where)
4443   "This function sets the mode line of the article or summary buffers.
4444 If WHERE is `summary', the summary mode line format will be used."
4445   ;; Is this mode line one we keep updated?
4446   (when (and (memq where gnus-updated-mode-lines)
4447              (symbol-value
4448               (intern (format "gnus-%s-mode-line-format-spec" where))))
4449     (let (mode-string)
4450       (save-excursion
4451         ;; We evaluate this in the summary buffer since these
4452         ;; variables are buffer-local to that buffer.
4453         (set-buffer gnus-summary-buffer)
4454         ;; We bind all these variables that are used in the `eval' form
4455         ;; below.
4456         (let* ((mformat (symbol-value
4457                          (intern
4458                           (format "gnus-%s-mode-line-format-spec" where))))
4459                (gnus-tmp-group-name gnus-newsgroup-name)
4460                (gnus-tmp-article-number (or gnus-current-article 0))
4461                (gnus-tmp-unread gnus-newsgroup-unreads)
4462                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4463                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4464                (gnus-tmp-unread-and-unselected
4465                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4466                             (zerop gnus-tmp-unselected))
4467                        "")
4468                       ((zerop gnus-tmp-unselected)
4469                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4470                       (t (format "{%d(+%d) more}"
4471                                  gnus-tmp-unread-and-unticked
4472                                  gnus-tmp-unselected))))
4473                (gnus-tmp-subject
4474                 (if (and gnus-current-headers
4475                          (vectorp gnus-current-headers))
4476                     (gnus-mode-string-quote
4477                      (mail-header-subject gnus-current-headers))
4478                   ""))
4479                bufname-length max-len
4480                gnus-tmp-header);; passed as argument to any user-format-funcs
4481           (setq mode-string (eval mformat))
4482           (setq bufname-length (if (string-match "%b" mode-string)
4483                                    (- (length
4484                                        (buffer-name
4485                                         (if (eq where 'summary)
4486                                             nil
4487                                           (get-buffer gnus-article-buffer))))
4488                                       2)
4489                                  0))
4490           (setq max-len (max 4 (if gnus-mode-non-string-length
4491                                    (- (window-width)
4492                                       gnus-mode-non-string-length
4493                                       bufname-length)
4494                                  (length mode-string))))
4495           ;; We might have to chop a bit of the string off...
4496           (when (> (length mode-string) max-len)
4497             (setq mode-string
4498                   (concat (gnus-truncate-string mode-string (- max-len 3))
4499                           "...")))
4500           ;; Pad the mode string a bit.
4501           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4502       ;; Update the mode line.
4503       (setq mode-line-buffer-identification
4504             (gnus-mode-line-buffer-identification (list mode-string)))
4505       (set-buffer-modified-p t))))
4506
4507 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4508   "Go through the HEADERS list and add all Xrefs to a hash table.
4509 The resulting hash table is returned, or nil if no Xrefs were found."
4510   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4511          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4512          (xref-hashtb (gnus-make-hashtable))
4513          start group entry number xrefs header)
4514     (while headers
4515       (setq header (pop headers))
4516       (when (and (setq xrefs (mail-header-xref header))
4517                  (not (memq (setq number (mail-header-number header))
4518                             unreads)))
4519         (setq start 0)
4520         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4521           (setq start (match-end 0))
4522           (setq group (if prefix
4523                           (concat prefix (substring xrefs (match-beginning 1)
4524                                                     (match-end 1)))
4525                         (substring xrefs (match-beginning 1) (match-end 1))))
4526           (setq number
4527                 (string-to-int (substring xrefs (match-beginning 2)
4528                                           (match-end 2))))
4529           (if (setq entry (gnus-gethash group xref-hashtb))
4530               (setcdr entry (cons number (cdr entry)))
4531             (gnus-sethash group (cons number nil) xref-hashtb)))))
4532     (and start xref-hashtb)))
4533
4534 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4535   "Look through all the headers and mark the Xrefs as read."
4536   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4537         name entry info xref-hashtb idlist method nth4)
4538     (save-excursion
4539       (set-buffer gnus-group-buffer)
4540       (when (setq xref-hashtb
4541                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4542         (mapatoms
4543          (lambda (group)
4544            (unless (string= from-newsgroup (setq name (symbol-name group)))
4545              (setq idlist (symbol-value group))
4546              ;; Dead groups are not updated.
4547              (and (prog1
4548                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4549                             info (nth 2 entry))
4550                     (when (stringp (setq nth4 (gnus-info-method info)))
4551                       (setq nth4 (gnus-server-to-method nth4))))
4552                   ;; Only do the xrefs if the group has the same
4553                   ;; select method as the group we have just read.
4554                   (or (gnus-methods-equal-p
4555                        nth4 (gnus-find-method-for-group from-newsgroup))
4556                       virtual
4557                       (equal nth4 (setq method (gnus-find-method-for-group
4558                                                 from-newsgroup)))
4559                       (and (equal (car nth4) (car method))
4560                            (equal (nth 1 nth4) (nth 1 method))))
4561                   gnus-use-cross-reference
4562                   (or (not (eq gnus-use-cross-reference t))
4563                       virtual
4564                       ;; Only do cross-references on subscribed
4565                       ;; groups, if that is what is wanted.
4566                       (<= (gnus-info-level info) gnus-level-subscribed))
4567                   (gnus-group-make-articles-read name idlist))))
4568          xref-hashtb)))))
4569
4570 (defun gnus-compute-read-articles (group articles)
4571   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4572          (info (nth 2 entry))
4573          (active (gnus-active group))
4574          ninfo)
4575     (when entry
4576       ;; First peel off all invalid article numbers.
4577       (when active
4578         (let ((ids articles)
4579               id first)
4580           (while (setq id (pop ids))
4581             (when (and first (> id (cdr active)))
4582               ;; We'll end up in this situation in one particular
4583               ;; obscure situation.  If you re-scan a group and get
4584               ;; a new article that is cross-posted to a different
4585               ;; group that has not been re-scanned, you might get
4586               ;; crossposted article that has a higher number than
4587               ;; Gnus believes possible.  So we re-activate this
4588               ;; group as well.  This might mean doing the
4589               ;; crossposting thingy will *increase* the number
4590               ;; of articles in some groups.  Tsk, tsk.
4591               (setq active (or (gnus-activate-group group) active)))
4592             (when (or (> id (cdr active))
4593                       (< id (car active)))
4594               (setq articles (delq id articles))))))
4595       ;; If the read list is nil, we init it.
4596       (if (and active
4597                (null (gnus-info-read info))
4598                (> (car active) 1))
4599           (setq ninfo (cons 1 (1- (car active))))
4600         (setq ninfo (gnus-info-read info)))
4601       ;; Then we add the read articles to the range.
4602       (gnus-add-to-range
4603        ninfo (setq articles (sort articles '<))))))
4604
4605 (defun gnus-group-make-articles-read (group articles)
4606   "Update the info of GROUP to say that ARTICLES are read."
4607   (let* ((num 0)
4608          (entry (gnus-gethash group gnus-newsrc-hashtb))
4609          (info (nth 2 entry))
4610          (active (gnus-active group))
4611          range)
4612     (when entry
4613       (setq range (gnus-compute-read-articles group articles))
4614       (save-excursion
4615         (set-buffer gnus-group-buffer)
4616         (gnus-undo-register
4617           `(progn
4618              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4619              (gnus-info-set-read ',info ',(gnus-info-read info))
4620              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4621              (gnus-group-update-group ,group t))))
4622       ;; Add the read articles to the range.
4623       (gnus-info-set-read info range)
4624       ;; Then we have to re-compute how many unread
4625       ;; articles there are in this group.
4626       (when active
4627         (cond
4628          ((not range)
4629           (setq num (- (1+ (cdr active)) (car active))))
4630          ((not (listp (cdr range)))
4631           (setq num (- (cdr active) (- (1+ (cdr range))
4632                                        (car range)))))
4633          (t
4634           (while range
4635             (if (numberp (car range))
4636                 (setq num (1+ num))
4637               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4638             (setq range (cdr range)))
4639           (setq num (- (cdr active) num))))
4640         ;; Update the number of unread articles.
4641         (setcar entry num)
4642         ;; Update the group buffer.
4643         (gnus-group-update-group group t)))))
4644
4645 (defvar gnus-newsgroup-none-id 0)
4646
4647 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4648   (let ((cur nntp-server-buffer)
4649         (dependencies
4650          (or dependencies
4651              (save-excursion (set-buffer gnus-summary-buffer)
4652                              gnus-newsgroup-dependencies)))
4653         headers id end ref
4654         (mail-parse-charset gnus-newsgroup-charset)
4655         (mail-parse-ignored-charsets
4656          (save-excursion (condition-case nil
4657                              (set-buffer gnus-summary-buffer)
4658                            (error))
4659                          gnus-newsgroup-ignored-charsets)))
4660     (save-excursion
4661       (set-buffer nntp-server-buffer)
4662       ;; Translate all TAB characters into SPACE characters.
4663       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4664       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4665       (gnus-run-hooks 'gnus-parse-headers-hook)
4666       (let ((case-fold-search t)
4667             in-reply-to header p lines chars ctype)
4668         (goto-char (point-min))
4669         ;; Search to the beginning of the next header.  Error messages
4670         ;; do not begin with 2 or 3.
4671         (while (re-search-forward "^[23][0-9]+ " nil t)
4672           (setq id nil
4673                 ref nil)
4674           ;; This implementation of this function, with nine
4675           ;; search-forwards instead of the one re-search-forward and
4676           ;; a case (which basically was the old function) is actually
4677           ;; about twice as fast, even though it looks messier.  You
4678           ;; can't have everything, I guess.  Speed and elegance
4679           ;; doesn't always go hand in hand.
4680           (setq
4681            header
4682            (make-full-mail-header
4683             ;; Number.
4684             (prog1
4685                 (read cur)
4686               (end-of-line)
4687               (setq p (point))
4688               (narrow-to-region (point)
4689                                 (or (and (search-forward "\n.\n" nil t)
4690                                          (- (point) 2))
4691                                     (point))))
4692             ;; Subject.
4693             (progn
4694               (goto-char p)
4695               (if (search-forward "\nsubject: " nil t)
4696                   (buffer-substring (match-end 0) (std11-field-end))
4697                 "(none)"))
4698             ;; From.
4699             (progn
4700               (goto-char p)
4701               (if (search-forward "\nfrom: " nil t)
4702                   (buffer-substring (match-end 0) (std11-field-end))
4703                 "(nobody)"))
4704             ;; Date.
4705             (progn
4706               (goto-char p)
4707               (if (search-forward "\ndate: " nil t)
4708                   (buffer-substring (match-end 0) (std11-field-end))
4709                 ""))
4710             ;; Message-ID.
4711             (progn
4712               (goto-char p)
4713               (setq id (if (re-search-forward
4714                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4715                            ;; We do it this way to make sure the Message-ID
4716                            ;; is (somewhat) syntactically valid.
4717                            (buffer-substring (match-beginning 1)
4718                                              (match-end 1))
4719                          ;; If there was no message-id, we just fake one
4720                          ;; to make subsequent routines simpler.
4721                          (nnheader-generate-fake-message-id))))
4722             ;; References.
4723             (progn
4724               (goto-char p)
4725               (if (search-forward "\nreferences: " nil t)
4726                   (progn
4727                     (setq end (point))
4728                     (prog1
4729                         (buffer-substring (match-end 0) (std11-field-end))
4730                       (setq ref
4731                             (buffer-substring
4732                              (progn
4733                                ;; (end-of-line)
4734                                (search-backward ">" end t)
4735                                (1+ (point)))
4736                              (progn
4737                                (search-backward "<" end t)
4738                                (point))))))
4739                 ;; Get the references from the in-reply-to header if there
4740                 ;; were no references and the in-reply-to header looks
4741                 ;; promising.
4742                 (if (and (search-forward "\nin-reply-to: " nil t)
4743                          (setq in-reply-to
4744                                (buffer-substring (match-end 0)
4745                                                  (std11-field-end)))
4746                          (string-match "<[^>]+>" in-reply-to))
4747                     (let (ref2)
4748                       (setq ref (substring in-reply-to (match-beginning 0)
4749                                            (match-end 0)))
4750                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4751                         (setq ref2 (substring in-reply-to (match-beginning 0)
4752                                               (match-end 0)))
4753                         (when (> (length ref2) (length ref))
4754                           (setq ref ref2)))
4755                       ref)
4756                   (setq ref nil))))
4757             ;; Chars.
4758             (progn
4759               (goto-char p)
4760               (if (search-forward "\nchars: " nil t)
4761                   (if (numberp (setq chars (ignore-errors (read cur))))
4762                       chars 0)
4763                 0))
4764             ;; Lines.
4765             (progn
4766               (goto-char p)
4767               (if (search-forward "\nlines: " nil t)
4768                   (if (numberp (setq lines (ignore-errors (read cur))))
4769                       lines 0)
4770                 0))
4771             ;; Xref.
4772             (progn
4773               (goto-char p)
4774               (and (search-forward "\nxref: " nil t)
4775                    (buffer-substring (match-end 0) (std11-field-end))))
4776             ;; Extra.
4777             (when gnus-extra-headers
4778               (let ((extra gnus-extra-headers)
4779                     out)
4780                 (while extra
4781                   (goto-char p)
4782                   (when (search-forward
4783                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4784                     (push (cons (car extra)
4785                                 (buffer-substring (match-end 0)
4786                                                   (std11-field-end)))
4787                           out))
4788                   (pop extra))
4789                 out))))
4790           (goto-char p)
4791           (if (and (search-forward "\ncontent-type: " nil t)
4792                    (setq ctype
4793                          (buffer-substring (match-end 0) (std11-field-end))))
4794               (mime-entity-set-content-type-internal
4795                header (mime-parse-Content-Type ctype)))
4796           (when (equal id ref)
4797             (setq ref nil))
4798
4799           (when gnus-alter-header-function
4800             (funcall gnus-alter-header-function header)
4801             (setq id (mail-header-id header)
4802                   ref (gnus-parent-id (mail-header-references header))))
4803
4804           (when (setq header
4805                       (gnus-dependencies-add-header
4806                        header dependencies force-new))
4807             (push header headers))
4808           (goto-char (point-max))
4809           (widen))
4810         (nreverse headers)))))
4811
4812 ;; Goes through the xover lines and returns a list of vectors
4813 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4814                                                   force-new dependencies
4815                                                   group also-fetch-heads)
4816   "Parse the news overview data in the server buffer, and return a
4817 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4818   ;; Get the Xref when the users reads the articles since most/some
4819   ;; NNTP servers do not include Xrefs when using XOVER.
4820   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4821   (let ((mail-parse-charset gnus-newsgroup-charset)
4822         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4823         (cur nntp-server-buffer)
4824         (dependencies (or dependencies gnus-newsgroup-dependencies))
4825         number headers header)
4826     (save-excursion
4827       (set-buffer nntp-server-buffer)
4828       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4829       ;; Allow the user to mangle the headers before parsing them.
4830       (gnus-run-hooks 'gnus-parse-headers-hook)
4831       (goto-char (point-min))
4832       (while (not (eobp))
4833         (condition-case ()
4834             (while (and sequence (not (eobp)))
4835               (setq number (read cur))
4836               (while (and sequence
4837                           (< (car sequence) number))
4838                 (setq sequence (cdr sequence)))
4839               (and sequence
4840                    (eq number (car sequence))
4841                    (progn
4842                      (setq sequence (cdr sequence))
4843                      (setq header (inline
4844                                     (gnus-nov-parse-line
4845                                      number dependencies force-new))))
4846                    (push header headers))
4847               (forward-line 1))
4848           (error
4849            (gnus-error 4 "Strange nov line (%d)"
4850                        (count-lines (point-min) (point)))))
4851         (forward-line 1))
4852       ;; A common bug in inn is that if you have posted an article and
4853       ;; then retrieves the active file, it will answer correctly --
4854       ;; the new article is included.  However, a NOV entry for the
4855       ;; article may not have been generated yet, so this may fail.
4856       ;; We work around this problem by retrieving the last few
4857       ;; headers using HEAD.
4858       (if (or (not also-fetch-heads)
4859               (not sequence))
4860           ;; We (probably) got all the headers.
4861           (nreverse headers)
4862         (let ((gnus-nov-is-evil t))
4863           (nconc
4864            (nreverse headers)
4865            (gnus-retrieve-parsed-headers sequence group)
4866            ))))))
4867
4868 (defun gnus-article-get-xrefs ()
4869   "Fill in the Xref value in `gnus-current-headers', if necessary.
4870 This is meant to be called in `gnus-article-internal-prepare-hook'."
4871   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4872                                  gnus-current-headers)))
4873     (or (not gnus-use-cross-reference)
4874         (not headers)
4875         (and (mail-header-xref headers)
4876              (not (string= (mail-header-xref headers) "")))
4877         (let ((case-fold-search t)
4878               xref)
4879           (save-restriction
4880             (nnheader-narrow-to-headers)
4881             (goto-char (point-min))
4882             (when (or (and (not (eobp))
4883                            (eq (downcase (char-after)) ?x)
4884                            (looking-at "Xref:"))
4885                       (search-forward "\nXref:" nil t))
4886               (goto-char (1+ (match-end 0)))
4887               (setq xref (buffer-substring (point)
4888                                            (progn (end-of-line) (point))))
4889               (mail-header-set-xref headers xref)))))))
4890
4891 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4892   "Find article ID and insert the summary line for that article.
4893 OLD-HEADER can either be a header or a line number to insert
4894 the subject line on."
4895   (let* ((line (and (numberp old-header) old-header))
4896          (old-header (and (vectorp old-header) old-header))
4897          (header (cond ((and old-header use-old-header)
4898                         old-header)
4899                        ((and (numberp id)
4900                              (gnus-number-to-header id))
4901                         (gnus-number-to-header id))
4902                        (t
4903                         (gnus-read-header id))))
4904          (number (and (numberp id) id))
4905          d)
4906     (when header
4907       ;; Rebuild the thread that this article is part of and go to the
4908       ;; article we have fetched.
4909       (when (and (not gnus-show-threads)
4910                  old-header)
4911         (when (and number
4912                    (setq d (gnus-data-find (mail-header-number old-header))))
4913           (goto-char (gnus-data-pos d))
4914           (gnus-data-remove
4915            number
4916            (- (gnus-point-at-bol)
4917               (prog1
4918                   (1+ (gnus-point-at-eol))
4919                 (gnus-delete-line))))))
4920       (when old-header
4921         (mail-header-set-number header (mail-header-number old-header)))
4922       (setq gnus-newsgroup-sparse
4923             (delq (setq number (mail-header-number header))
4924                   gnus-newsgroup-sparse))
4925       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4926       (push number gnus-newsgroup-limit)
4927       (gnus-rebuild-thread (mail-header-id header) line)
4928       (gnus-summary-goto-subject number nil t))
4929     (when (and (numberp number)
4930                (> number 0))
4931       ;; We have to update the boundaries even if we can't fetch the
4932       ;; article if ID is a number -- so that the next `P' or `N'
4933       ;; command will fetch the previous (or next) article even
4934       ;; if the one we tried to fetch this time has been canceled.
4935       (when (> number gnus-newsgroup-end)
4936         (setq gnus-newsgroup-end number))
4937       (when (< number gnus-newsgroup-begin)
4938         (setq gnus-newsgroup-begin number))
4939       (setq gnus-newsgroup-unselected
4940             (delq number gnus-newsgroup-unselected)))
4941     ;; Report back a success?
4942     (and header (mail-header-number header))))
4943
4944 ;;; Process/prefix in the summary buffer
4945
4946 (defun gnus-summary-work-articles (n)
4947   "Return a list of articles to be worked upon.
4948 The prefix argument, the list of process marked articles, and the
4949 current article will be taken into consideration."
4950   (save-excursion
4951     (set-buffer gnus-summary-buffer)
4952     (cond
4953      (n
4954       ;; A numerical prefix has been given.
4955       (setq n (prefix-numeric-value n))
4956       (let ((backward (< n 0))
4957             (n (abs (prefix-numeric-value n)))
4958             articles article)
4959         (save-excursion
4960           (while
4961               (and (> n 0)
4962                    (push (setq article (gnus-summary-article-number))
4963                          articles)
4964                    (if backward
4965                        (gnus-summary-find-prev nil article)
4966                      (gnus-summary-find-next nil article)))
4967             (decf n)))
4968         (nreverse articles)))
4969      ((and (gnus-region-active-p) (mark))
4970       (message "region active")
4971       ;; Work on the region between point and mark.
4972       (let ((max (max (point) (mark)))
4973             articles article)
4974         (save-excursion
4975           (goto-char (min (point) (mark)))
4976           (while
4977               (and
4978                (push (setq article (gnus-summary-article-number)) articles)
4979                (gnus-summary-find-next nil article)
4980                (< (point) max)))
4981           (nreverse articles))))
4982      (gnus-newsgroup-processable
4983       ;; There are process-marked articles present.
4984       ;; Save current state.
4985       (gnus-summary-save-process-mark)
4986       ;; Return the list.
4987       (reverse gnus-newsgroup-processable))
4988      (t
4989       ;; Just return the current article.
4990       (list (gnus-summary-article-number))))))
4991
4992 (defmacro gnus-summary-iterate (arg &rest forms)
4993   "Iterate over the process/prefixed articles and do FORMS.
4994 ARG is the interactive prefix given to the command.  FORMS will be
4995 executed with point over the summary line of the articles."
4996   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4997     `(let ((,articles (gnus-summary-work-articles ,arg)))
4998        (while ,articles
4999          (gnus-summary-goto-subject (car ,articles))
5000          ,@forms
5001          (pop ,articles)))))
5002
5003 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5004 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5005
5006 (defun gnus-summary-save-process-mark ()
5007   "Push the current set of process marked articles on the stack."
5008   (interactive)
5009   (push (copy-sequence gnus-newsgroup-processable)
5010         gnus-newsgroup-process-stack))
5011
5012 (defun gnus-summary-kill-process-mark ()
5013   "Push the current set of process marked articles on the stack and unmark."
5014   (interactive)
5015   (gnus-summary-save-process-mark)
5016   (gnus-summary-unmark-all-processable))
5017
5018 (defun gnus-summary-yank-process-mark ()
5019   "Pop the last process mark state off the stack and restore it."
5020   (interactive)
5021   (unless gnus-newsgroup-process-stack
5022     (error "Empty mark stack"))
5023   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5024
5025 (defun gnus-summary-process-mark-set (set)
5026   "Make SET into the current process marked articles."
5027   (gnus-summary-unmark-all-processable)
5028   (while set
5029     (gnus-summary-set-process-mark (pop set))))
5030
5031 ;;; Searching and stuff
5032
5033 (defun gnus-summary-search-group (&optional backward use-level)
5034   "Search for next unread newsgroup.
5035 If optional argument BACKWARD is non-nil, search backward instead."
5036   (save-excursion
5037     (set-buffer gnus-group-buffer)
5038     (when (gnus-group-search-forward
5039            backward nil (if use-level (gnus-group-group-level) nil))
5040       (gnus-group-group-name))))
5041
5042 (defun gnus-summary-best-group (&optional exclude-group)
5043   "Find the name of the best unread group.
5044 If EXCLUDE-GROUP, do not go to this group."
5045   (save-excursion
5046     (set-buffer gnus-group-buffer)
5047     (save-excursion
5048       (gnus-group-best-unread-group exclude-group))))
5049
5050 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5051   (if backward (gnus-summary-find-prev)
5052     (let* ((dummy (gnus-summary-article-intangible-p))
5053            (article (or article (gnus-summary-article-number)))
5054            (arts (gnus-data-find-list article))
5055            result)
5056       (when (and (not dummy)
5057                  (or (not gnus-summary-check-current)
5058                      (not unread)
5059                      (not (gnus-data-unread-p (car arts)))))
5060         (setq arts (cdr arts)))
5061       (when (setq result
5062                   (if unread
5063                       (progn
5064                         (while arts
5065                           (when (or (and undownloaded
5066                                          (eq gnus-undownloaded-mark
5067                                              (gnus-data-mark (car arts))))
5068                                     (gnus-data-unread-p (car arts)))
5069                             (setq result (car arts)
5070                                   arts nil))
5071                           (setq arts (cdr arts)))
5072                         result)
5073                     (car arts)))
5074         (goto-char (gnus-data-pos result))
5075         (gnus-data-number result)))))
5076
5077 (defun gnus-summary-find-prev (&optional unread article)
5078   (let* ((eobp (eobp))
5079          (article (or article (gnus-summary-article-number)))
5080          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5081          result)
5082     (when (and (not eobp)
5083                (or (not gnus-summary-check-current)
5084                    (not unread)
5085                    (not (gnus-data-unread-p (car arts)))))
5086       (setq arts (cdr arts)))
5087     (when (setq result
5088                 (if unread
5089                     (progn
5090                       (while arts
5091                         (when (gnus-data-unread-p (car arts))
5092                           (setq result (car arts)
5093                                 arts nil))
5094                         (setq arts (cdr arts)))
5095                       result)
5096                   (car arts)))
5097       (goto-char (gnus-data-pos result))
5098       (gnus-data-number result))))
5099
5100 (defun gnus-summary-find-subject (subject &optional unread backward article)
5101   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5102          (article (or article (gnus-summary-article-number)))
5103          (articles (gnus-data-list backward))
5104          (arts (gnus-data-find-list article articles))
5105          result)
5106     (when (or (not gnus-summary-check-current)
5107               (not unread)
5108               (not (gnus-data-unread-p (car arts))))
5109       (setq arts (cdr arts)))
5110     (while arts
5111       (and (or (not unread)
5112                (gnus-data-unread-p (car arts)))
5113            (vectorp (gnus-data-header (car arts)))
5114            (gnus-subject-equal
5115             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5116            (setq result (car arts)
5117                  arts nil))
5118       (setq arts (cdr arts)))
5119     (and result
5120          (goto-char (gnus-data-pos result))
5121          (gnus-data-number result))))
5122
5123 (defun gnus-summary-search-forward (&optional unread subject backward)
5124   "Search forward for an article.
5125 If UNREAD, look for unread articles.  If SUBJECT, look for
5126 articles with that subject.  If BACKWARD, search backward instead."
5127   (cond (subject (gnus-summary-find-subject subject unread backward))
5128         (backward (gnus-summary-find-prev unread))
5129         (t (gnus-summary-find-next unread))))
5130
5131 (defun gnus-recenter (&optional n)
5132   "Center point in window and redisplay frame.
5133 Also do horizontal recentering."
5134   (interactive "P")
5135   (when (and gnus-auto-center-summary
5136              (not (eq gnus-auto-center-summary 'vertical)))
5137     (gnus-horizontal-recenter))
5138   (recenter n))
5139
5140 (defun gnus-summary-recenter ()
5141   "Center point in the summary window.
5142 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5143 displayed, no centering will be performed."
5144   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5145   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5146   (let* ((top (cond ((< (window-height) 4) 0)
5147                     ((< (window-height) 7) 1)
5148                     (t (if (numberp gnus-auto-center-summary)
5149                            gnus-auto-center-summary
5150                          2))))
5151          (height (1- (window-height)))
5152          (bottom (save-excursion (goto-char (point-max))
5153                                  (forward-line (- height))
5154                                  (point)))
5155          (window (get-buffer-window (current-buffer))))
5156     ;; The user has to want it.
5157     (when gnus-auto-center-summary
5158       (when (get-buffer-window gnus-article-buffer)
5159         ;; Only do recentering when the article buffer is displayed,
5160         ;; Set the window start to either `bottom', which is the biggest
5161         ;; possible valid number, or the second line from the top,
5162         ;; whichever is the least.
5163         (set-window-start
5164          window (min bottom (save-excursion
5165                               (forward-line (- top)) (point)))
5166          t))
5167       ;; Do horizontal recentering while we're at it.
5168       (when (and (get-buffer-window (current-buffer) t)
5169                  (not (eq gnus-auto-center-summary 'vertical)))
5170         (let ((selected (selected-window)))
5171           (select-window (get-buffer-window (current-buffer) t))
5172           (gnus-summary-position-point)
5173           (gnus-horizontal-recenter)
5174           (select-window selected))))))
5175
5176 (defun gnus-summary-jump-to-group (newsgroup)
5177   "Move point to NEWSGROUP in group mode buffer."
5178   ;; Keep update point of group mode buffer if visible.
5179   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5180       (save-window-excursion
5181         ;; Take care of tree window mode.
5182         (when (get-buffer-window gnus-group-buffer)
5183           (pop-to-buffer gnus-group-buffer))
5184         (gnus-group-jump-to-group newsgroup))
5185     (save-excursion
5186       ;; Take care of tree window mode.
5187       (if (get-buffer-window gnus-group-buffer)
5188           (pop-to-buffer gnus-group-buffer)
5189         (set-buffer gnus-group-buffer))
5190       (gnus-group-jump-to-group newsgroup))))
5191
5192 ;; This function returns a list of article numbers based on the
5193 ;; difference between the ranges of read articles in this group and
5194 ;; the range of active articles.
5195 (defun gnus-list-of-unread-articles (group)
5196   (let* ((read (gnus-info-read (gnus-get-info group)))
5197          (active (or (gnus-active group) (gnus-activate-group group)))
5198          (last (cdr active))
5199          first nlast unread)
5200     ;; If none are read, then all are unread.
5201     (if (not read)
5202         (setq first (car active))
5203       ;; If the range of read articles is a single range, then the
5204       ;; first unread article is the article after the last read
5205       ;; article.  Sounds logical, doesn't it?
5206       (if (and (not (listp (cdr read)))
5207                (or (< (car read) (car active))
5208                    (progn (setq read (list read))
5209                           nil)))
5210           (setq first (max (car active) (1+ (cdr read))))
5211         ;; `read' is a list of ranges.
5212         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5213                                   (caar read)))
5214                   1)
5215           (setq first (car active)))
5216         (while read
5217           (when first
5218             (while (< first nlast)
5219               (push first unread)
5220               (setq first (1+ first))))
5221           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5222           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5223           (setq read (cdr read)))))
5224     ;; And add the last unread articles.
5225     (while (<= first last)
5226       (push first unread)
5227       (setq first (1+ first)))
5228     ;; Return the list of unread articles.
5229     (delq 0 (nreverse unread))))
5230
5231 (defun gnus-list-of-read-articles (group)
5232   "Return a list of unread, unticked and non-dormant articles."
5233   (let* ((info (gnus-get-info group))
5234          (marked (gnus-info-marks info))
5235          (active (gnus-active group)))
5236     (and info active
5237          (gnus-set-difference
5238           (gnus-sorted-complement
5239            (gnus-uncompress-range active)
5240            (gnus-list-of-unread-articles group))
5241           (append
5242            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5243            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5244
5245 ;; Various summary commands
5246
5247 (defun gnus-summary-select-article-buffer ()
5248   "Reconfigure windows to show article buffer."
5249   (interactive)
5250   (if (not (gnus-buffer-live-p gnus-article-buffer))
5251       (error "There is no article buffer for this summary buffer")
5252     (gnus-configure-windows 'article)
5253     (select-window (get-buffer-window gnus-article-buffer))))
5254
5255 (defun gnus-summary-universal-argument (arg)
5256   "Perform any operation on all articles that are process/prefixed."
5257   (interactive "P")
5258   (let ((articles (gnus-summary-work-articles arg))
5259         func article)
5260     (if (eq
5261          (setq
5262           func
5263           (key-binding
5264            (read-key-sequence
5265             (substitute-command-keys
5266              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5267          'undefined)
5268         (gnus-error 1 "Undefined key")
5269       (save-excursion
5270         (while articles
5271           (gnus-summary-goto-subject (setq article (pop articles)))
5272           (let (gnus-newsgroup-processable)
5273             (command-execute func))
5274           (gnus-summary-remove-process-mark article)))))
5275   (gnus-summary-position-point))
5276
5277 (defun gnus-summary-toggle-truncation (&optional arg)
5278   "Toggle truncation of summary lines.
5279 With arg, turn line truncation on iff arg is positive."
5280   (interactive "P")
5281   (setq truncate-lines
5282         (if (null arg) (not truncate-lines)
5283           (> (prefix-numeric-value arg) 0)))
5284   (redraw-display))
5285
5286 (defun gnus-summary-reselect-current-group (&optional all rescan)
5287   "Exit and then reselect the current newsgroup.
5288 The prefix argument ALL means to select all articles."
5289   (interactive "P")
5290   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5291     (error "Ephemeral groups can't be reselected"))
5292   (let ((current-subject (gnus-summary-article-number))
5293         (group gnus-newsgroup-name)
5294         masquerade)
5295     (when rescan
5296       (setq masquerade (get-buffer-create " *masquerade-summary*"))
5297       (let ((pos (- (point) (window-start)))
5298             (id mode-line-buffer-identification))
5299         (copy-to-buffer masquerade (window-start) (point-max))
5300         (with-current-buffer masquerade
5301           (set-buffer-modified-p nil)
5302           (goto-char (1+ (max 0 pos)))
5303           (setq mode-line-buffer-identification id
5304                 mode-name "Masquerade"
5305                 truncate-lines t))))
5306     (setq gnus-newsgroup-begin nil)
5307     (gnus-summary-exit)
5308     (if rescan
5309         (progn
5310           (switch-to-buffer masquerade)
5311           (set-window-start (selected-window) (point-min))
5312           (static-when (featurep 'xemacs)
5313             (gnus-xmas-switch-horizontal-scrollbar-off)
5314             (redraw-frame))
5315           (set-buffer gnus-group-buffer)
5316           (gnus-group-jump-to-group group)
5317           (unwind-protect
5318               (save-excursion
5319                 (gnus-group-get-new-news-this-group 1))
5320             (kill-buffer masquerade)))
5321       ;; We have to adjust the point of group mode buffer because
5322       ;; point was moved to the next unread newsgroup by exiting.
5323       (gnus-summary-jump-to-group group))
5324     (gnus-group-read-group all t)
5325     (gnus-summary-goto-subject current-subject nil t)))
5326
5327 (defun gnus-summary-rescan-group (&optional all)
5328   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5329   (interactive "P")
5330   (gnus-summary-reselect-current-group all t))
5331
5332 (defun gnus-summary-update-info (&optional non-destructive)
5333   (save-excursion
5334     (let ((group gnus-newsgroup-name))
5335       (when group
5336         (when gnus-newsgroup-kill-headers
5337           (setq gnus-newsgroup-killed
5338                 (gnus-compress-sequence
5339                  (nconc
5340                   (gnus-set-sorted-intersection
5341                    (gnus-uncompress-range gnus-newsgroup-killed)
5342                    (setq gnus-newsgroup-unselected
5343                          (sort gnus-newsgroup-unselected '<)))
5344                   (setq gnus-newsgroup-unreads
5345                         (sort gnus-newsgroup-unreads '<)))
5346                  t)))
5347         (unless (listp (cdr gnus-newsgroup-killed))
5348           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5349         (let ((headers gnus-newsgroup-headers))
5350           ;; Set the new ranges of read articles.
5351           (save-excursion
5352             (set-buffer gnus-group-buffer)
5353             (gnus-undo-force-boundary))
5354           (gnus-update-read-articles
5355            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5356           ;; Set the current article marks.
5357           (let ((gnus-newsgroup-scored
5358                  (if (and (not gnus-save-score)
5359                           (not non-destructive))
5360                      nil
5361                    gnus-newsgroup-scored)))
5362             (save-excursion
5363               (gnus-update-marks)))
5364           ;; Do the cross-ref thing.
5365           (when gnus-use-cross-reference
5366             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5367           ;; Do not switch windows but change the buffer to work.
5368           (set-buffer gnus-group-buffer)
5369           (unless (gnus-ephemeral-group-p group)
5370             (gnus-group-update-group group)))))))
5371
5372 (defun gnus-summary-save-newsrc (&optional force)
5373   "Save the current number of read/marked articles in the dribble buffer.
5374 The dribble buffer will then be saved.
5375 If FORCE (the prefix), also save the .newsrc file(s)."
5376   (interactive "P")
5377   (gnus-summary-update-info t)
5378   (if force
5379       (gnus-save-newsrc-file)
5380     (gnus-dribble-save)))
5381
5382 (defun gnus-summary-exit (&optional temporary)
5383   "Exit reading current newsgroup, and then return to group selection mode.
5384 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5385   (interactive)
5386   (gnus-set-global-variables)
5387   (gnus-kill-save-kill-buffer)
5388   (gnus-async-halt-prefetch)
5389   (let* ((group gnus-newsgroup-name)
5390          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5391          (mode major-mode)
5392          (group-point nil)
5393          (buf (current-buffer)))
5394     (unless quit-config
5395       ;; Do adaptive scoring, and possibly save score files.
5396       (when gnus-newsgroup-adaptive
5397         (gnus-score-adaptive))
5398       (when gnus-use-scoring
5399         (gnus-score-save)))
5400     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5401     ;; If we have several article buffers, we kill them at exit.
5402     (unless gnus-single-article-buffer
5403       (gnus-kill-buffer gnus-original-article-buffer)
5404       (setq gnus-article-current nil))
5405     (when gnus-use-cache
5406       (gnus-cache-possibly-remove-articles)
5407       (gnus-cache-save-buffers))
5408     (gnus-async-prefetch-remove-group group)
5409     (when gnus-suppress-duplicates
5410       (gnus-dup-enter-articles))
5411     (when gnus-use-trees
5412       (gnus-tree-close group))
5413     (when gnus-use-cache
5414       (gnus-cache-write-active))
5415     ;; Remove entries for this group.
5416     (nnmail-purge-split-history (gnus-group-real-name group))
5417     ;; Make all changes in this group permanent.
5418     (unless quit-config
5419       (gnus-run-hooks 'gnus-exit-group-hook)
5420       (gnus-summary-update-info))
5421     (gnus-close-group group)
5422     ;; Make sure where we were, and go to next newsgroup.
5423     (set-buffer gnus-group-buffer)
5424     (unless quit-config
5425       (gnus-group-jump-to-group group))
5426     (gnus-run-hooks 'gnus-summary-exit-hook)
5427     (unless (or quit-config
5428                 ;; If this group has disappeared from the summary
5429                 ;; buffer, don't skip forwards.
5430                 (not (string= group (gnus-group-group-name))))
5431       (gnus-group-next-unread-group 1))
5432     (setq group-point (point))
5433     (if temporary
5434         nil                             ;Nothing to do.
5435       ;; If we have several article buffers, we kill them at exit.
5436       (unless gnus-single-article-buffer
5437         (gnus-kill-buffer gnus-article-buffer)
5438         (gnus-kill-buffer gnus-original-article-buffer)
5439         (setq gnus-article-current nil))
5440       (set-buffer buf)
5441       (if (not gnus-kill-summary-on-exit)
5442           (gnus-deaden-summary)
5443         ;; We set all buffer-local variables to nil.  It is unclear why
5444         ;; this is needed, but if we don't, buffer-local variables are
5445         ;; not garbage-collected, it seems.  This would the lead to en
5446         ;; ever-growing Emacs.
5447         (gnus-summary-clear-local-variables)
5448         (when (get-buffer gnus-article-buffer)
5449           (bury-buffer gnus-article-buffer))
5450         ;; We clear the global counterparts of the buffer-local
5451         ;; variables as well, just to be on the safe side.
5452         (set-buffer gnus-group-buffer)
5453         (gnus-summary-clear-local-variables)
5454         ;; Return to group mode buffer.
5455         (when (eq mode 'gnus-summary-mode)
5456           (gnus-kill-buffer buf)))
5457       (setq gnus-current-select-method gnus-select-method)
5458       (pop-to-buffer gnus-group-buffer)
5459       (if (not quit-config)
5460           (progn
5461             (goto-char group-point)
5462             (gnus-configure-windows 'group 'force))
5463         (gnus-handle-ephemeral-exit quit-config))
5464       ;; Clear the current group name.
5465       (unless quit-config
5466         (setq gnus-newsgroup-name nil)))))
5467
5468 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5469 (defun gnus-summary-exit-no-update (&optional no-questions)
5470   "Quit reading current newsgroup without updating read article info."
5471   (interactive)
5472   (let* ((group gnus-newsgroup-name)
5473          (quit-config (gnus-group-quit-config group)))
5474     (when (or no-questions
5475               gnus-expert-user
5476               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5477       (gnus-async-halt-prefetch)
5478       (mapcar 'funcall
5479               (delq 'gnus-summary-expire-articles
5480                     (copy-sequence gnus-summary-prepare-exit-hook)))
5481       ;; If we have several article buffers, we kill them at exit.
5482       (unless gnus-single-article-buffer
5483         (gnus-kill-buffer gnus-article-buffer)
5484         (gnus-kill-buffer gnus-original-article-buffer)
5485         (setq gnus-article-current nil))
5486       (if (not gnus-kill-summary-on-exit)
5487           (gnus-deaden-summary)
5488         (gnus-close-group group)
5489         (gnus-summary-clear-local-variables)
5490         (set-buffer gnus-group-buffer)
5491         (gnus-summary-clear-local-variables)
5492         (when (get-buffer gnus-summary-buffer)
5493           (kill-buffer gnus-summary-buffer)))
5494       (unless gnus-single-article-buffer
5495         (setq gnus-article-current nil))
5496       (when gnus-use-trees
5497         (gnus-tree-close group))
5498       (gnus-async-prefetch-remove-group group)
5499       (when (get-buffer gnus-article-buffer)
5500         (bury-buffer gnus-article-buffer))
5501       ;; Return to the group buffer.
5502       (gnus-configure-windows 'group 'force)
5503       ;; Clear the current group name.
5504       (setq gnus-newsgroup-name nil)
5505       (when (equal (gnus-group-group-name) group)
5506         (gnus-group-next-unread-group 1))
5507       (when quit-config
5508         (gnus-handle-ephemeral-exit quit-config)))))
5509
5510 (defun gnus-handle-ephemeral-exit (quit-config)
5511   "Handle movement when leaving an ephemeral group.
5512 The state which existed when entering the ephemeral is reset."
5513   (if (not (buffer-name (car quit-config)))
5514       (gnus-configure-windows 'group 'force)
5515     (set-buffer (car quit-config))
5516     (cond ((eq major-mode 'gnus-summary-mode)
5517            (gnus-set-global-variables))
5518           ((eq major-mode 'gnus-article-mode)
5519            (save-excursion
5520              ;; The `gnus-summary-buffer' variable may point
5521              ;; to the old summary buffer when using a single
5522              ;; article buffer.
5523              (unless (gnus-buffer-live-p gnus-summary-buffer)
5524                (set-buffer gnus-group-buffer))
5525              (set-buffer gnus-summary-buffer)
5526              (gnus-set-global-variables))))
5527     (if (or (eq (cdr quit-config) 'article)
5528             (eq (cdr quit-config) 'pick))
5529         (progn
5530           ;; The current article may be from the ephemeral group
5531           ;; thus it is best that we reload this article
5532           (gnus-summary-show-article)
5533           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5534               (gnus-configure-windows 'pick 'force)
5535             (gnus-configure-windows (cdr quit-config) 'force)))
5536       (gnus-configure-windows (cdr quit-config) 'force))
5537     (when (eq major-mode 'gnus-summary-mode)
5538       (gnus-summary-next-subject 1 nil t)
5539       (gnus-summary-recenter)
5540       (gnus-summary-position-point))))
5541
5542 (defun gnus-summary-preview-mime-message ()
5543   "MIME decode and play this message."
5544   (interactive)
5545   (let ((gnus-break-pages nil)
5546         (gnus-show-mime t))
5547     (gnus-summary-select-article gnus-show-all-headers t))
5548   (select-window (get-buffer-window gnus-article-buffer)))
5549
5550 ;;; Dead summaries.
5551
5552 (defvar gnus-dead-summary-mode-map nil)
5553
5554 (unless gnus-dead-summary-mode-map
5555   (setq gnus-dead-summary-mode-map (make-keymap))
5556   (suppress-keymap gnus-dead-summary-mode-map)
5557   (substitute-key-definition
5558    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5559   (let ((keys '("\C-d" "\r" "\177" [delete])))
5560     (while keys
5561       (define-key gnus-dead-summary-mode-map
5562         (pop keys) 'gnus-summary-wake-up-the-dead))))
5563
5564 (defvar gnus-dead-summary-mode nil
5565   "Minor mode for Gnus summary buffers.")
5566
5567 (defun gnus-dead-summary-mode (&optional arg)
5568   "Minor mode for Gnus summary buffers."
5569   (interactive "P")
5570   (when (eq major-mode 'gnus-summary-mode)
5571     (make-local-variable 'gnus-dead-summary-mode)
5572     (setq gnus-dead-summary-mode
5573           (if (null arg) (not gnus-dead-summary-mode)
5574             (> (prefix-numeric-value arg) 0)))
5575     (when gnus-dead-summary-mode
5576       (gnus-add-minor-mode
5577        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5578
5579 (defun gnus-deaden-summary ()
5580   "Make the current summary buffer into a dead summary buffer."
5581   ;; Kill any previous dead summary buffer.
5582   (when (and gnus-dead-summary
5583              (buffer-name gnus-dead-summary))
5584     (save-excursion
5585       (set-buffer gnus-dead-summary)
5586       (when gnus-dead-summary-mode
5587         (kill-buffer (current-buffer)))))
5588   ;; Make this the current dead summary.
5589   (setq gnus-dead-summary (current-buffer))
5590   (gnus-dead-summary-mode 1)
5591   (let ((name (buffer-name)))
5592     (when (string-match "Summary" name)
5593       (rename-buffer
5594        (concat (substring name 0 (match-beginning 0)) "Dead "
5595                (substring name (match-beginning 0)))
5596        t))))
5597
5598 (defun gnus-kill-or-deaden-summary (buffer)
5599   "Kill or deaden the summary BUFFER."
5600   (save-excursion
5601     (when (and (buffer-name buffer)
5602                (not gnus-single-article-buffer))
5603       (save-excursion
5604         (set-buffer buffer)
5605         (gnus-kill-buffer gnus-article-buffer)
5606         (gnus-kill-buffer gnus-original-article-buffer)))
5607     (cond (gnus-kill-summary-on-exit
5608            (when (and gnus-use-trees
5609                       (gnus-buffer-exists-p buffer))
5610              (save-excursion
5611                (set-buffer buffer)
5612                (gnus-tree-close gnus-newsgroup-name)))
5613            (gnus-kill-buffer buffer))
5614           ((gnus-buffer-exists-p buffer)
5615            (save-excursion
5616              (set-buffer buffer)
5617              (gnus-deaden-summary))))))
5618
5619 (defun gnus-summary-wake-up-the-dead (&rest args)
5620   "Wake up the dead summary buffer."
5621   (interactive)
5622   (gnus-dead-summary-mode -1)
5623   (let ((name (buffer-name)))
5624     (when (string-match "Dead " name)
5625       (rename-buffer
5626        (concat (substring name 0 (match-beginning 0))
5627                (substring name (match-end 0)))
5628        t)))
5629   (gnus-message 3 "This dead summary is now alive again"))
5630
5631 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5632 (defun gnus-summary-fetch-faq (&optional faq-dir)
5633   "Fetch the FAQ for the current group.
5634 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5635 in."
5636   (interactive
5637    (list
5638     (when current-prefix-arg
5639       (completing-read
5640        "Faq dir: " (and (listp gnus-group-faq-directory)
5641                         (mapcar (lambda (file) (list file))
5642                                 gnus-group-faq-directory))))))
5643   (let (gnus-faq-buffer)
5644     (when (setq gnus-faq-buffer
5645                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5646       (gnus-configure-windows 'summary-faq))))
5647
5648 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5649 (defun gnus-summary-describe-group (&optional force)
5650   "Describe the current newsgroup."
5651   (interactive "P")
5652   (gnus-group-describe-group force gnus-newsgroup-name))
5653
5654 (defun gnus-summary-describe-briefly ()
5655   "Describe summary mode commands briefly."
5656   (interactive)
5657   (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")))
5658
5659 ;; Walking around group mode buffer from summary mode.
5660
5661 (defun gnus-summary-next-group (&optional no-article target-group backward)
5662   "Exit current newsgroup and then select next unread newsgroup.
5663 If prefix argument NO-ARTICLE is non-nil, no article is selected
5664 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5665 previous group instead."
5666   (interactive "P")
5667   ;; Stop pre-fetching.
5668   (gnus-async-halt-prefetch)
5669   (let ((current-group gnus-newsgroup-name)
5670         (current-buffer (current-buffer))
5671         entered)
5672     ;; First we semi-exit this group to update Xrefs and all variables.
5673     ;; We can't do a real exit, because the window conf must remain
5674     ;; the same in case the user is prompted for info, and we don't
5675     ;; want the window conf to change before that...
5676     (gnus-summary-exit t)
5677     (while (not entered)
5678       ;; Then we find what group we are supposed to enter.
5679       (set-buffer gnus-group-buffer)
5680       (gnus-group-jump-to-group current-group)
5681       (setq target-group
5682             (or target-group
5683                 (if (eq gnus-keep-same-level 'best)
5684                     (gnus-summary-best-group gnus-newsgroup-name)
5685                   (gnus-summary-search-group backward gnus-keep-same-level))))
5686       (if (not target-group)
5687           ;; There are no further groups, so we return to the group
5688           ;; buffer.
5689           (progn
5690             (gnus-message 5 "Returning to the group buffer")
5691             (setq entered t)
5692             (when (gnus-buffer-live-p current-buffer)
5693               (set-buffer current-buffer)
5694               (gnus-summary-exit))
5695             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5696         ;; We try to enter the target group.
5697         (gnus-group-jump-to-group target-group)
5698         (let ((unreads (gnus-group-group-unread)))
5699           (if (and (or (eq t unreads)
5700                        (and unreads (not (zerop unreads))))
5701                    (gnus-summary-read-group
5702                     target-group nil no-article
5703                     (and (buffer-name current-buffer) current-buffer)
5704                     nil backward))
5705               (setq entered t)
5706             (setq current-group target-group
5707                   target-group nil)))))))
5708
5709 (defun gnus-summary-prev-group (&optional no-article)
5710   "Exit current newsgroup and then select previous unread newsgroup.
5711 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5712   (interactive "P")
5713   (gnus-summary-next-group no-article nil t))
5714
5715 ;; Walking around summary lines.
5716
5717 (defun gnus-summary-first-subject (&optional unread undownloaded)
5718   "Go to the first unread subject.
5719 If UNREAD is non-nil, go to the first unread article.
5720 Returns the article selected or nil if there are no unread articles."
5721   (interactive "P")
5722   (prog1
5723       (cond
5724        ;; Empty summary.
5725        ((null gnus-newsgroup-data)
5726         (gnus-message 3 "No articles in the group")
5727         nil)
5728        ;; Pick the first article.
5729        ((not unread)
5730         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5731         (gnus-data-number (car gnus-newsgroup-data)))
5732        ;; No unread articles.
5733        ((null gnus-newsgroup-unreads)
5734         (gnus-message 3 "No more unread articles")
5735         nil)
5736        ;; Find the first unread article.
5737        (t
5738         (let ((data gnus-newsgroup-data))
5739           (while (and data
5740                       (and (not (and undownloaded
5741                                      (eq gnus-undownloaded-mark
5742                                          (gnus-data-mark (car data)))))
5743                            (not (gnus-data-unread-p (car data)))))
5744             (setq data (cdr data)))
5745           (when data
5746             (goto-char (gnus-data-pos (car data)))
5747             (gnus-data-number (car data))))))
5748     (gnus-summary-position-point)))
5749
5750 (defun gnus-summary-next-subject (n &optional unread dont-display)
5751   "Go to next N'th summary line.
5752 If N is negative, go to the previous N'th subject line.
5753 If UNREAD is non-nil, only unread articles are selected.
5754 The difference between N and the actual number of steps taken is
5755 returned."
5756   (interactive "p")
5757   (let ((backward (< n 0))
5758         (n (abs n)))
5759     (while (and (> n 0)
5760                 (if backward
5761                     (gnus-summary-find-prev unread)
5762                   (gnus-summary-find-next unread)))
5763       (unless (zerop (setq n (1- n)))
5764         (gnus-summary-show-thread)))
5765     (when (/= 0 n)
5766       (gnus-message 7 "No more%s articles"
5767                     (if unread " unread" "")))
5768     (unless dont-display
5769       (gnus-summary-recenter)
5770       (gnus-summary-position-point))
5771     n))
5772
5773 (defun gnus-summary-next-unread-subject (n)
5774   "Go to next N'th unread summary line."
5775   (interactive "p")
5776   (gnus-summary-next-subject n t))
5777
5778 (defun gnus-summary-prev-subject (n &optional unread)
5779   "Go to previous N'th summary line.
5780 If optional argument UNREAD is non-nil, only unread article is selected."
5781   (interactive "p")
5782   (gnus-summary-next-subject (- n) unread))
5783
5784 (defun gnus-summary-prev-unread-subject (n)
5785   "Go to previous N'th unread summary line."
5786   (interactive "p")
5787   (gnus-summary-next-subject (- n) t))
5788
5789 (defun gnus-summary-goto-subject (article &optional force silent)
5790   "Go the subject line of ARTICLE.
5791 If FORCE, also allow jumping to articles not currently shown."
5792   (interactive "nArticle number: ")
5793   (let ((b (point))
5794         (data (gnus-data-find article)))
5795     ;; We read in the article if we have to.
5796     (and (not data)
5797          force
5798          (gnus-summary-insert-subject
5799           article
5800           (if (or (numberp force) (vectorp force)) force)
5801           t)
5802          (setq data (gnus-data-find article)))
5803     (goto-char b)
5804     (if (not data)
5805         (progn
5806           (unless silent
5807             (gnus-message 3 "Can't find article %d" article))
5808           nil)
5809       (goto-char (gnus-data-pos data))
5810       (gnus-summary-position-point)
5811       article)))
5812
5813 ;; Walking around summary lines with displaying articles.
5814
5815 (defun gnus-summary-expand-window (&optional arg)
5816   "Make the summary buffer take up the entire Emacs frame.
5817 Given a prefix, will force an `article' buffer configuration."
5818   (interactive "P")
5819   (if arg
5820       (gnus-configure-windows 'article 'force)
5821     (gnus-configure-windows 'summary 'force)))
5822
5823 (defun gnus-summary-display-article (article &optional all-header)
5824   "Display ARTICLE in article buffer."
5825   (gnus-set-global-variables)
5826   (if (null article)
5827       nil
5828     (prog1
5829         (if gnus-summary-display-article-function
5830             (funcall gnus-summary-display-article-function article all-header)
5831           (gnus-article-prepare article all-header))
5832       (with-current-buffer gnus-article-buffer
5833         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5834              nil))
5835       (gnus-run-hooks 'gnus-select-article-hook)
5836       (when (and gnus-current-article
5837                  (not (zerop gnus-current-article)))
5838         (gnus-summary-goto-subject gnus-current-article))
5839       (gnus-summary-recenter)
5840       (when (and gnus-use-trees gnus-show-threads)
5841         (gnus-possibly-generate-tree article)
5842         (gnus-highlight-selected-tree article))
5843       ;; Successfully display article.
5844       (gnus-article-set-window-start
5845        (cdr (assq article gnus-newsgroup-bookmarks))))))
5846
5847 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5848   "Select the current article.
5849 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5850 non-nil, the article will be re-fetched even if it already present in
5851 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5852 be displayed."
5853   ;; Make sure we are in the summary buffer to work around bbdb bug.
5854   (unless (eq major-mode 'gnus-summary-mode)
5855     (set-buffer gnus-summary-buffer))
5856   (let ((article (or article (gnus-summary-article-number)))
5857         (all-headers (not (not all-headers))) ;Must be T or NIL.
5858         gnus-summary-display-article-function
5859         did)
5860     (and (not pseudo)
5861          (gnus-summary-article-pseudo-p article)
5862          (error "This is a pseudo-article"))
5863     (prog1
5864         (save-excursion
5865           (set-buffer gnus-summary-buffer)
5866           (if (or (and gnus-single-article-buffer
5867                        (or (null gnus-current-article)
5868                            (null gnus-article-current)
5869                            (null (get-buffer gnus-article-buffer))
5870                            (not (eq article (cdr gnus-article-current)))
5871                            (not (equal (car gnus-article-current)
5872                                        gnus-newsgroup-name))))
5873                   (and (not gnus-single-article-buffer)
5874                        (or (null gnus-current-article)
5875                            (not (eq gnus-current-article article))))
5876                   force)
5877               ;; The requested article is different from the current article.
5878               (prog1
5879                   (gnus-summary-display-article article all-headers)
5880                 (setq did article)
5881                 (when (or all-headers gnus-show-all-headers)
5882                   (if (eq 'gnus-summary-toggle-mime this-command)
5883                       (gnus-article-show-all)
5884                     (gnus-article-show-all-headers))))
5885             (when (or all-headers gnus-show-all-headers)
5886               (gnus-article-show-all-headers))
5887             'old))
5888       (when did
5889         (gnus-article-set-window-start
5890          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5891
5892 (defun gnus-summary-set-current-mark (&optional current-mark)
5893   "Obsolete function."
5894   nil)
5895
5896 (defun gnus-summary-next-article (&optional unread subject backward push)
5897   "Select the next article.
5898 If UNREAD, only unread articles are selected.
5899 If SUBJECT, only articles with SUBJECT are selected.
5900 If BACKWARD, the previous article is selected instead of the next."
5901   (interactive "P")
5902   (cond
5903    ;; Is there such an article?
5904    ((and (gnus-summary-search-forward unread subject backward)
5905          (or (gnus-summary-display-article (gnus-summary-article-number))
5906              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5907     (gnus-summary-position-point))
5908    ;; If not, we try the first unread, if that is wanted.
5909    ((and subject
5910          gnus-auto-select-same
5911          (gnus-summary-first-unread-article))
5912     (gnus-summary-position-point)
5913     (gnus-message 6 "Wrapped"))
5914    ;; Try to get next/previous article not displayed in this group.
5915    ((and gnus-auto-extend-newsgroup
5916          (not unread) (not subject))
5917     (gnus-summary-goto-article
5918      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5919      nil (count-lines (point-min) (point))))
5920    ;; Go to next/previous group.
5921    (t
5922     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5923       (gnus-summary-jump-to-group gnus-newsgroup-name))
5924     (let ((cmd last-command-char)
5925           (point
5926            (save-excursion
5927              (set-buffer gnus-group-buffer)
5928              (point)))
5929           (group
5930            (if (eq gnus-keep-same-level 'best)
5931                (gnus-summary-best-group gnus-newsgroup-name)
5932              (gnus-summary-search-group backward gnus-keep-same-level))))
5933       ;; For some reason, the group window gets selected.  We change
5934       ;; it back.
5935       (select-window (get-buffer-window (current-buffer)))
5936       ;; Select next unread newsgroup automagically.
5937       (cond
5938        ((or (not gnus-auto-select-next)
5939             (not cmd))
5940         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5941        ((or (eq gnus-auto-select-next 'quietly)
5942             (and (eq gnus-auto-select-next 'slightly-quietly)
5943                  push)
5944             (and (eq gnus-auto-select-next 'almost-quietly)
5945                  (gnus-summary-last-article-p)))
5946         ;; Select quietly.
5947         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5948             (gnus-summary-exit)
5949           (gnus-message 7 "No more%s articles (%s)..."
5950                         (if unread " unread" "")
5951                         (if group (concat "selecting " group)
5952                           "exiting"))
5953           (gnus-summary-next-group nil group backward)))
5954        (t
5955         (when (gnus-key-press-event-p last-input-event)
5956           (gnus-summary-walk-group-buffer
5957            gnus-newsgroup-name cmd unread backward point))))))))
5958
5959 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5960   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5961                       (?\C-p (gnus-group-prev-unread-group 1))))
5962         (cursor-in-echo-area t)
5963         keve key group ended)
5964     (save-excursion
5965       (set-buffer gnus-group-buffer)
5966       (goto-char start)
5967       (setq group
5968             (if (eq gnus-keep-same-level 'best)
5969                 (gnus-summary-best-group gnus-newsgroup-name)
5970               (gnus-summary-search-group backward gnus-keep-same-level))))
5971     (while (not ended)
5972       (gnus-message
5973        5 "No more%s articles%s" (if unread " unread" "")
5974        (if (and group
5975                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5976            (format " (Type %s for %s [%s])"
5977                    (single-key-description cmd) group
5978                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5979          (format " (Type %s to exit %s)"
5980                  (single-key-description cmd)
5981                  gnus-newsgroup-name)))
5982       ;; Confirm auto selection.
5983       (setq key (car (setq keve (gnus-read-event-char))))
5984       (setq ended t)
5985       (cond
5986        ((assq key keystrokes)
5987         (let ((obuf (current-buffer)))
5988           (switch-to-buffer gnus-group-buffer)
5989           (when group
5990             (gnus-group-jump-to-group group))
5991           (eval (cadr (assq key keystrokes)))
5992           (setq group (gnus-group-group-name))
5993           (switch-to-buffer obuf))
5994         (setq ended nil))
5995        ((equal key cmd)
5996         (if (or (not group)
5997                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5998             (gnus-summary-exit)
5999           (gnus-summary-next-group nil group backward)))
6000        (t
6001         (push (cdr keve) unread-command-events))))))
6002
6003 (defun gnus-summary-next-unread-article ()
6004   "Select unread article after current one."
6005   (interactive)
6006   (gnus-summary-next-article
6007    (or (not (eq gnus-summary-goto-unread 'never))
6008        (gnus-summary-last-article-p (gnus-summary-article-number)))
6009    (and gnus-auto-select-same
6010         (gnus-summary-article-subject))))
6011
6012 (defun gnus-summary-prev-article (&optional unread subject)
6013   "Select the article after the current one.
6014 If UNREAD is non-nil, only unread articles are selected."
6015   (interactive "P")
6016   (gnus-summary-next-article unread subject t))
6017
6018 (defun gnus-summary-prev-unread-article ()
6019   "Select unread article before current one."
6020   (interactive)
6021   (gnus-summary-prev-article
6022    (or (not (eq gnus-summary-goto-unread 'never))
6023        (gnus-summary-first-article-p (gnus-summary-article-number)))
6024    (and gnus-auto-select-same
6025         (gnus-summary-article-subject))))
6026
6027 (defun gnus-summary-next-page (&optional lines circular)
6028   "Show next page of the selected article.
6029 If at the end of the current article, select the next article.
6030 LINES says how many lines should be scrolled up.
6031
6032 If CIRCULAR is non-nil, go to the start of the article instead of
6033 selecting the next article when reaching the end of the current
6034 article."
6035   (interactive "P")
6036   (setq gnus-summary-buffer (current-buffer))
6037   (gnus-set-global-variables)
6038   (let ((article (gnus-summary-article-number))
6039         (article-window (get-buffer-window gnus-article-buffer t))
6040         endp)
6041     ;; If the buffer is empty, we have no article.
6042     (unless article
6043       (error "No article to select"))
6044     (gnus-configure-windows 'article)
6045     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6046         (if (and (eq gnus-summary-goto-unread 'never)
6047                  (not (gnus-summary-last-article-p article)))
6048             (gnus-summary-next-article)
6049           (gnus-summary-next-unread-article))
6050       (if (or (null gnus-current-article)
6051               (null gnus-article-current)
6052               (/= article (cdr gnus-article-current))
6053               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6054           ;; Selected subject is different from current article's.
6055           (gnus-summary-display-article article)
6056         (when article-window
6057           (gnus-eval-in-buffer-window gnus-article-buffer
6058             (setq endp (gnus-article-next-page lines)))
6059           (when endp
6060             (cond (circular
6061                    (gnus-summary-beginning-of-article))
6062                   (lines
6063                    (gnus-message 3 "End of message"))
6064                   ((null lines)
6065                    (if (and (eq gnus-summary-goto-unread 'never)
6066                             (not (gnus-summary-last-article-p article)))
6067                        (gnus-summary-next-article)
6068                      (gnus-summary-next-unread-article))))))))
6069     (gnus-summary-recenter)
6070     (gnus-summary-position-point)))
6071
6072 (defun gnus-summary-prev-page (&optional lines move)
6073   "Show previous page of selected article.
6074 Argument LINES specifies lines to be scrolled down.
6075 If MOVE, move to the previous unread article if point is at
6076 the beginning of the buffer."
6077   (interactive "P")
6078   (let ((article (gnus-summary-article-number))
6079         (article-window (get-buffer-window gnus-article-buffer t))
6080         endp)
6081     (gnus-configure-windows 'article)
6082     (if (or (null gnus-current-article)
6083             (null gnus-article-current)
6084             (/= article (cdr gnus-article-current))
6085             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6086         ;; Selected subject is different from current article's.
6087         (gnus-summary-display-article article)
6088       (gnus-summary-recenter)
6089       (when article-window
6090         (gnus-eval-in-buffer-window gnus-article-buffer
6091           (setq endp (gnus-article-prev-page lines)))
6092         (when (and move endp)
6093           (cond (lines
6094                  (gnus-message 3 "Beginning of message"))
6095                 ((null lines)
6096                  (if (and (eq gnus-summary-goto-unread 'never)
6097                           (not (gnus-summary-first-article-p article)))
6098                      (gnus-summary-prev-article)
6099                    (gnus-summary-prev-unread-article))))))))
6100   (gnus-summary-position-point))
6101
6102 (defun gnus-summary-prev-page-or-article (&optional lines)
6103   "Show previous page of selected article.
6104 Argument LINES specifies lines to be scrolled down.
6105 If at the beginning of the article, go to the next article."
6106   (interactive "P")
6107   (gnus-summary-prev-page lines t))
6108
6109 (defun gnus-summary-scroll-up (lines)
6110   "Scroll up (or down) one line current article.
6111 Argument LINES specifies lines to be scrolled up (or down if negative)."
6112   (interactive "p")
6113   (gnus-configure-windows 'article)
6114   (gnus-summary-show-thread)
6115   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6116     (gnus-eval-in-buffer-window gnus-article-buffer
6117       (cond ((> lines 0)
6118              (when (gnus-article-next-page lines)
6119                (gnus-message 3 "End of message")))
6120             ((< lines 0)
6121              (gnus-article-prev-page (- lines))))))
6122   (gnus-summary-recenter)
6123   (gnus-summary-position-point))
6124
6125 (defun gnus-summary-scroll-down (lines)
6126   "Scroll down (or up) one line current article.
6127 Argument LINES specifies lines to be scrolled down (or up if negative)."
6128   (interactive "p")
6129   (gnus-summary-scroll-up (- lines)))
6130
6131 (defun gnus-summary-next-same-subject ()
6132   "Select next article which has the same subject as current one."
6133   (interactive)
6134   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6135
6136 (defun gnus-summary-prev-same-subject ()
6137   "Select previous article which has the same subject as current one."
6138   (interactive)
6139   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6140
6141 (defun gnus-summary-next-unread-same-subject ()
6142   "Select next unread article which has the same subject as current one."
6143   (interactive)
6144   (gnus-summary-next-article t (gnus-summary-article-subject)))
6145
6146 (defun gnus-summary-prev-unread-same-subject ()
6147   "Select previous unread article which has the same subject as current one."
6148   (interactive)
6149   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6150
6151 (defun gnus-summary-first-unread-article ()
6152   "Select the first unread article.
6153 Return nil if there are no unread articles."
6154   (interactive)
6155   (prog1
6156       (when (gnus-summary-first-subject t)
6157         (gnus-summary-show-thread)
6158         (gnus-summary-first-subject t)
6159         (gnus-summary-display-article (gnus-summary-article-number)))
6160     (gnus-summary-position-point)))
6161
6162 (defun gnus-summary-first-unread-subject ()
6163   "Place the point on the subject line of the first unread article.
6164 Return nil if there are no unread articles."
6165   (interactive)
6166   (prog1
6167       (when (gnus-summary-first-subject t)
6168         (gnus-summary-show-thread)
6169         (gnus-summary-first-subject t))
6170     (gnus-summary-position-point)))
6171
6172 (defun gnus-summary-first-article ()
6173   "Select the first article.
6174 Return nil if there are no articles."
6175   (interactive)
6176   (prog1
6177       (when (gnus-summary-first-subject)
6178         (gnus-summary-show-thread)
6179         (gnus-summary-first-subject)
6180         (gnus-summary-display-article (gnus-summary-article-number)))
6181     (gnus-summary-position-point)))
6182
6183 (defun gnus-summary-best-unread-article ()
6184   "Select the unread article with the highest score."
6185   (interactive)
6186   (let ((best -1000000)
6187         (data gnus-newsgroup-data)
6188         article score)
6189     (while data
6190       (and (gnus-data-unread-p (car data))
6191            (> (setq score
6192                     (gnus-summary-article-score (gnus-data-number (car data))))
6193               best)
6194            (setq best score
6195                  article (gnus-data-number (car data))))
6196       (setq data (cdr data)))
6197     (prog1
6198         (if article
6199             (gnus-summary-goto-article article)
6200           (error "No unread articles"))
6201       (gnus-summary-position-point))))
6202
6203 (defun gnus-summary-last-subject ()
6204   "Go to the last displayed subject line in the group."
6205   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6206     (when article
6207       (gnus-summary-goto-subject article))))
6208
6209 (defun gnus-summary-goto-article (article &optional all-headers force)
6210   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6211 If ALL-HEADERS is non-nil, no header lines are hidden.
6212 If FORCE, go to the article even if it isn't displayed.  If FORCE
6213 is a number, it is the line the article is to be displayed on."
6214   (interactive
6215    (list
6216     (completing-read
6217      "Article number or Message-ID: "
6218      (mapcar (lambda (number) (list (int-to-string number)))
6219              gnus-newsgroup-limit))
6220     current-prefix-arg
6221     t))
6222   (prog1
6223       (if (and (stringp article)
6224                (string-match "@" article))
6225           (gnus-summary-refer-article article)
6226         (when (stringp article)
6227           (setq article (string-to-number article)))
6228         (if (gnus-summary-goto-subject article force)
6229             (gnus-summary-display-article article all-headers)
6230           (gnus-message 4 "Couldn't go to article %s" article) nil))
6231     (gnus-summary-position-point)))
6232
6233 (defun gnus-summary-goto-last-article ()
6234   "Go to the previously read article."
6235   (interactive)
6236   (prog1
6237       (when gnus-last-article
6238         (gnus-summary-goto-article gnus-last-article nil t))
6239     (gnus-summary-position-point)))
6240
6241 (defun gnus-summary-pop-article (number)
6242   "Pop one article off the history and go to the previous.
6243 NUMBER articles will be popped off."
6244   (interactive "p")
6245   (let (to)
6246     (setq gnus-newsgroup-history
6247           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6248     (if to
6249         (gnus-summary-goto-article (car to) nil t)
6250       (error "Article history empty")))
6251   (gnus-summary-position-point))
6252
6253 ;; Summary commands and functions for limiting the summary buffer.
6254
6255 (defun gnus-summary-limit-to-articles (n)
6256   "Limit the summary buffer to the next N articles.
6257 If not given a prefix, use the process marked articles instead."
6258   (interactive "P")
6259   (prog1
6260       (let ((articles (gnus-summary-work-articles n)))
6261         (setq gnus-newsgroup-processable nil)
6262         (gnus-summary-limit articles))
6263     (gnus-summary-position-point)))
6264
6265 (defun gnus-summary-pop-limit (&optional total)
6266   "Restore the previous limit.
6267 If given a prefix, remove all limits."
6268   (interactive "P")
6269   (when total
6270     (setq gnus-newsgroup-limits
6271           (list (mapcar (lambda (h) (mail-header-number h))
6272                         gnus-newsgroup-headers))))
6273   (unless gnus-newsgroup-limits
6274     (error "No limit to pop"))
6275   (prog1
6276       (gnus-summary-limit nil 'pop)
6277     (gnus-summary-position-point)))
6278
6279 (defun gnus-summary-limit-to-subject (subject &optional header)
6280   "Limit the summary buffer to articles that have subjects that match a regexp."
6281   (interactive "sLimit to subject (regexp): ")
6282   (unless header
6283     (setq header "subject"))
6284   (when (not (equal "" subject))
6285     (prog1
6286         (let ((articles (gnus-summary-find-matching
6287                          (or header "subject") subject 'all)))
6288           (unless articles
6289             (error "Found no matches for \"%s\"" subject))
6290           (gnus-summary-limit articles))
6291       (gnus-summary-position-point))))
6292
6293 (defun gnus-summary-limit-to-author (from)
6294   "Limit the summary buffer to articles that have authors that match a regexp."
6295   (interactive "sLimit to author (regexp): ")
6296   (gnus-summary-limit-to-subject from "from"))
6297
6298 (defun gnus-summary-limit-to-age (age &optional younger-p)
6299   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6300 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6301 articles that are younger than AGE days."
6302   (interactive
6303    (let ((younger current-prefix-arg)
6304          (days-got nil)
6305          days)
6306      (while (not days-got)
6307        (setq days (if younger
6308                       (read-string "Limit to articles within (in days): ")
6309                     (read-string "Limit to articles old than (in days): ")))
6310        (when (> (length days) 0)
6311          (setq days (read days)))
6312        (if (numberp days)
6313            (setq days-got t)
6314          (message "Please enter a number.")
6315          (sleep-for 1)))
6316      (list days younger)))
6317   (prog1
6318       (let ((data gnus-newsgroup-data)
6319             (cutoff (days-to-time age))
6320             articles d date is-younger)
6321         (while (setq d (pop data))
6322           (when (and (vectorp (gnus-data-header d))
6323                      (setq date (mail-header-date (gnus-data-header d))))
6324             (setq is-younger (time-less-p
6325                               (time-since (condition-case ()
6326                                               (date-to-time date)
6327                                             (error '(0 0))))
6328                               cutoff))
6329             (when (if younger-p
6330                       is-younger
6331                     (not is-younger))
6332               (push (gnus-data-number d) articles))))
6333         (gnus-summary-limit (nreverse articles)))
6334     (gnus-summary-position-point)))
6335
6336 (defun gnus-summary-limit-to-extra (header regexp)
6337   "Limit the summary buffer to articles that match an 'extra' header."
6338   (interactive
6339    (let ((header
6340           (intern
6341            (gnus-completing-read
6342             (symbol-name (car gnus-extra-headers))      
6343             "Limit extra header:"       
6344             (mapcar (lambda (x) 
6345                       (cons (symbol-name x) x))
6346                     gnus-extra-headers)
6347             nil                 
6348             t))))
6349      (list header
6350            (read-string (format "Limit to header %s (regexp): " header)))))
6351   (when (not (equal "" regexp))
6352     (prog1
6353         (let ((articles (gnus-summary-find-matching
6354                          (cons 'extra header) regexp 'all)))
6355           (unless articles
6356             (error "Found no matches for \"%s\"" regexp))
6357           (gnus-summary-limit articles))
6358       (gnus-summary-position-point))))
6359
6360 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6361 (make-obsolete
6362  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6363
6364 (defun gnus-summary-limit-to-unread (&optional all)
6365   "Limit the summary buffer to articles that are not marked as read.
6366 If ALL is non-nil, limit strictly to unread articles."
6367   (interactive "P")
6368   (if all
6369       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6370     (gnus-summary-limit-to-marks
6371      ;; Concat all the marks that say that an article is read and have
6372      ;; those removed.
6373      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6374            gnus-killed-mark gnus-kill-file-mark
6375            gnus-low-score-mark gnus-expirable-mark
6376            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6377            gnus-duplicate-mark gnus-souped-mark)
6378      'reverse)))
6379
6380 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6381 (make-obsolete 'gnus-summary-delete-marked-with
6382                'gnus-summary-limit-exlude-marks)
6383
6384 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6385   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6386 If REVERSE, limit the summary buffer to articles that are marked
6387 with MARKS.  MARKS can either be a string of marks or a list of marks.
6388 Returns how many articles were removed."
6389   (interactive "sMarks: ")
6390   (gnus-summary-limit-to-marks marks t))
6391
6392 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6393   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6394 If REVERSE (the prefix), limit the summary buffer to articles that are
6395 not marked with MARKS.  MARKS can either be a string of marks or a
6396 list of marks.
6397 Returns how many articles were removed."
6398   (interactive "sMarks: \nP")
6399   (prog1
6400       (let ((data gnus-newsgroup-data)
6401             (marks (if (listp marks) marks
6402                      (append marks nil))) ; Transform to list.
6403             articles)
6404         (while data
6405           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6406                   (memq (gnus-data-mark (car data)) marks))
6407             (push (gnus-data-number (car data)) articles))
6408           (setq data (cdr data)))
6409         (gnus-summary-limit articles))
6410     (gnus-summary-position-point)))
6411
6412 (defun gnus-summary-limit-to-score (&optional score)
6413   "Limit to articles with score at or above SCORE."
6414   (interactive "P")
6415   (setq score (if score
6416                   (prefix-numeric-value score)
6417                 (or gnus-summary-default-score 0)))
6418   (let ((data gnus-newsgroup-data)
6419         articles)
6420     (while data
6421       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6422                 score)
6423         (push (gnus-data-number (car data)) articles))
6424       (setq data (cdr data)))
6425     (prog1
6426         (gnus-summary-limit articles)
6427       (gnus-summary-position-point))))
6428
6429 (defun gnus-summary-limit-include-thread (id)
6430   "Display all the hidden articles that in the current thread."
6431   (interactive (list (mail-header-id (gnus-summary-article-header))))
6432   (let ((articles (gnus-articles-in-thread
6433                    (gnus-id-to-thread (gnus-root-id id)))))
6434     (prog1
6435         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6436       (gnus-summary-position-point))))
6437
6438 (defun gnus-summary-limit-include-dormant ()
6439   "Display all the hidden articles that are marked as dormant.
6440 Note that this command only works on a subset of the articles currently
6441 fetched for this group."
6442   (interactive)
6443   (unless gnus-newsgroup-dormant
6444     (error "There are no dormant articles in this group"))
6445   (prog1
6446       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6447     (gnus-summary-position-point)))
6448
6449 (defun gnus-summary-limit-exclude-dormant ()
6450   "Hide all dormant articles."
6451   (interactive)
6452   (prog1
6453       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6454     (gnus-summary-position-point)))
6455
6456 (defun gnus-summary-limit-exclude-childless-dormant ()
6457   "Hide all dormant articles that have no children."
6458   (interactive)
6459   (let ((data (gnus-data-list t))
6460         articles d children)
6461     ;; Find all articles that are either not dormant or have
6462     ;; children.
6463     (while (setq d (pop data))
6464       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6465                 (and (setq children
6466                            (gnus-article-children (gnus-data-number d)))
6467                      (let (found)
6468                        (while children
6469                          (when (memq (car children) articles)
6470                            (setq children nil
6471                                  found t))
6472                          (pop children))
6473                        found)))
6474         (push (gnus-data-number d) articles)))
6475     ;; Do the limiting.
6476     (prog1
6477         (gnus-summary-limit articles)
6478       (gnus-summary-position-point))))
6479
6480 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6481   "Mark all unread excluded articles as read.
6482 If ALL, mark even excluded ticked and dormants as read."
6483   (interactive "P")
6484   (let ((articles (gnus-sorted-complement
6485                    (sort
6486                     (mapcar (lambda (h) (mail-header-number h))
6487                             gnus-newsgroup-headers)
6488                     '<)
6489                    (sort gnus-newsgroup-limit '<)))
6490         article)
6491     (setq gnus-newsgroup-unreads
6492           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6493     (if all
6494         (setq gnus-newsgroup-dormant nil
6495               gnus-newsgroup-marked nil
6496               gnus-newsgroup-reads
6497               (nconc
6498                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6499                gnus-newsgroup-reads))
6500       (while (setq article (pop articles))
6501         (unless (or (memq article gnus-newsgroup-dormant)
6502                     (memq article gnus-newsgroup-marked))
6503           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6504
6505 (defun gnus-summary-limit (articles &optional pop)
6506   (if pop
6507       ;; We pop the previous limit off the stack and use that.
6508       (setq articles (car gnus-newsgroup-limits)
6509             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6510     ;; We use the new limit, so we push the old limit on the stack.
6511     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6512   ;; Set the limit.
6513   (setq gnus-newsgroup-limit articles)
6514   (let ((total (length gnus-newsgroup-data))
6515         (data (gnus-data-find-list (gnus-summary-article-number)))
6516         (gnus-summary-mark-below nil)   ; Inhibit this.
6517         found)
6518     ;; This will do all the work of generating the new summary buffer
6519     ;; according to the new limit.
6520     (gnus-summary-prepare)
6521     ;; Hide any threads, possibly.
6522     (and gnus-show-threads
6523          gnus-thread-hide-subtree
6524          (gnus-summary-hide-all-threads))
6525     ;; Try to return to the article you were at, or one in the
6526     ;; neighborhood.
6527     (when data
6528       ;; We try to find some article after the current one.
6529       (while data
6530         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6531           (setq data nil
6532                 found t))
6533         (setq data (cdr data))))
6534     (unless found
6535       ;; If there is no data, that means that we were after the last
6536       ;; article.  The same goes when we can't find any articles
6537       ;; after the current one.
6538       (goto-char (point-max))
6539       (gnus-summary-find-prev))
6540     (gnus-set-mode-line 'summary)
6541     ;; We return how many articles were removed from the summary
6542     ;; buffer as a result of the new limit.
6543     (- total (length gnus-newsgroup-data))))
6544
6545 (defsubst gnus-invisible-cut-children (threads)
6546   (let ((num 0))
6547     (while threads
6548       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6549         (incf num))
6550       (pop threads))
6551     (< num 2)))
6552
6553 (defsubst gnus-cut-thread (thread)
6554   "Go forwards in the thread until we find an article that we want to display."
6555   (when (or (eq gnus-fetch-old-headers 'some)
6556             (eq gnus-fetch-old-headers 'invisible)
6557             (numberp gnus-fetch-old-headers)
6558             (eq gnus-build-sparse-threads 'some)
6559             (eq gnus-build-sparse-threads 'more))
6560     ;; Deal with old-fetched headers and sparse threads.
6561     (while (and
6562             thread
6563             (or
6564              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6565              (gnus-summary-article-ancient-p
6566               (mail-header-number (car thread))))
6567             (if (or (<= (length (cdr thread)) 1)
6568                     (eq gnus-fetch-old-headers 'invisible))
6569                 (setq gnus-newsgroup-limit
6570                       (delq (mail-header-number (car thread))
6571                             gnus-newsgroup-limit)
6572                       thread (cadr thread))
6573               (when (gnus-invisible-cut-children (cdr thread))
6574                 (let ((th (cdr thread)))
6575                   (while th
6576                     (if (memq (mail-header-number (caar th))
6577                               gnus-newsgroup-limit)
6578                         (setq thread (car th)
6579                               th nil)
6580                       (setq th (cdr th))))))))))
6581   thread)
6582
6583 (defun gnus-cut-threads (threads)
6584   "Cut off all uninteresting articles from the beginning of threads."
6585   (when (or (eq gnus-fetch-old-headers 'some)
6586             (eq gnus-fetch-old-headers 'invisible)
6587             (numberp gnus-fetch-old-headers)
6588             (eq gnus-build-sparse-threads 'some)
6589             (eq gnus-build-sparse-threads 'more))
6590     (let ((th threads))
6591       (while th
6592         (setcar th (gnus-cut-thread (car th)))
6593         (setq th (cdr th)))))
6594   ;; Remove nixed out threads.
6595   (delq nil threads))
6596
6597 (defun gnus-summary-initial-limit (&optional show-if-empty)
6598   "Figure out what the initial limit is supposed to be on group entry.
6599 This entails weeding out unwanted dormants, low-scored articles,
6600 fetch-old-headers verbiage, and so on."
6601   ;; Most groups have nothing to remove.
6602   (if (or gnus-inhibit-limiting
6603           (and (null gnus-newsgroup-dormant)
6604                (not (eq gnus-fetch-old-headers 'some))
6605                (not (numberp gnus-fetch-old-headers))
6606                (not (eq gnus-fetch-old-headers 'invisible))
6607                (null gnus-summary-expunge-below)
6608                (not (eq gnus-build-sparse-threads 'some))
6609                (not (eq gnus-build-sparse-threads 'more))
6610                (null gnus-thread-expunge-below)
6611                (not gnus-use-nocem)))
6612       ()                                ; Do nothing.
6613     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6614     (setq gnus-newsgroup-limit nil)
6615     (mapatoms
6616      (lambda (node)
6617        (unless (car (symbol-value node))
6618          ;; These threads have no parents -- they are roots.
6619          (let ((nodes (cdr (symbol-value node)))
6620                thread)
6621            (while nodes
6622              (if (and gnus-thread-expunge-below
6623                       (< (gnus-thread-total-score (car nodes))
6624                          gnus-thread-expunge-below))
6625                  (gnus-expunge-thread (pop nodes))
6626                (setq thread (pop nodes))
6627                (gnus-summary-limit-children thread))))))
6628      gnus-newsgroup-dependencies)
6629     ;; If this limitation resulted in an empty group, we might
6630     ;; pop the previous limit and use it instead.
6631     (when (and (not gnus-newsgroup-limit)
6632                show-if-empty)
6633       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6634     gnus-newsgroup-limit))
6635
6636 (defun gnus-summary-limit-children (thread)
6637   "Return 1 if this subthread is visible and 0 if it is not."
6638   ;; First we get the number of visible children to this thread.  This
6639   ;; is done by recursing down the thread using this function, so this
6640   ;; will really go down to a leaf article first, before slowly
6641   ;; working its way up towards the root.
6642   (when thread
6643     (let ((children
6644            (if (cdr thread)
6645                (apply '+ (mapcar 'gnus-summary-limit-children
6646                                  (cdr thread)))
6647              0))
6648           (number (mail-header-number (car thread)))
6649           score)
6650       (if (and
6651            (not (memq number gnus-newsgroup-marked))
6652            (or
6653             ;; If this article is dormant and has absolutely no visible
6654             ;; children, then this article isn't visible.
6655             (and (memq number gnus-newsgroup-dormant)
6656                  (zerop children))
6657             ;; If this is "fetch-old-headered" and there is no
6658             ;; visible children, then we don't want this article.
6659             (and (or (eq gnus-fetch-old-headers 'some)
6660                      (numberp gnus-fetch-old-headers))
6661                  (gnus-summary-article-ancient-p number)
6662                  (zerop children))
6663             ;; If this is "fetch-old-headered" and `invisible', then
6664             ;; we don't want this article.
6665             (and (eq gnus-fetch-old-headers 'invisible)
6666                  (gnus-summary-article-ancient-p number))
6667             ;; If this is a sparsely inserted article with no children,
6668             ;; we don't want it.
6669             (and (eq gnus-build-sparse-threads 'some)
6670                  (gnus-summary-article-sparse-p number)
6671                  (zerop children))
6672             ;; If we use expunging, and this article is really
6673             ;; low-scored, then we don't want this article.
6674             (when (and gnus-summary-expunge-below
6675                        (< (setq score
6676                                 (or (cdr (assq number gnus-newsgroup-scored))
6677                                     gnus-summary-default-score))
6678                           gnus-summary-expunge-below))
6679               ;; We increase the expunge-tally here, but that has
6680               ;; nothing to do with the limits, really.
6681               (incf gnus-newsgroup-expunged-tally)
6682               ;; We also mark as read here, if that's wanted.
6683               (when (and gnus-summary-mark-below
6684                          (< score gnus-summary-mark-below))
6685                 (setq gnus-newsgroup-unreads
6686                       (delq number gnus-newsgroup-unreads))
6687                 (if gnus-newsgroup-auto-expire
6688                     (push number gnus-newsgroup-expirable)
6689                   (push (cons number gnus-low-score-mark)
6690                         gnus-newsgroup-reads)))
6691               t)
6692             ;; Check NoCeM things.
6693             (if (and gnus-use-nocem
6694                      (gnus-nocem-unwanted-article-p
6695                       (mail-header-id (car thread))))
6696                 (progn
6697                   (setq gnus-newsgroup-unreads
6698                         (delq number gnus-newsgroup-unreads))
6699                   t))))
6700           ;; Nope, invisible article.
6701           0
6702         ;; Ok, this article is to be visible, so we add it to the limit
6703         ;; and return 1.
6704         (push number gnus-newsgroup-limit)
6705         1))))
6706
6707 (defun gnus-expunge-thread (thread)
6708   "Mark all articles in THREAD as read."
6709   (let* ((number (mail-header-number (car thread))))
6710     (incf gnus-newsgroup-expunged-tally)
6711     ;; We also mark as read here, if that's wanted.
6712     (setq gnus-newsgroup-unreads
6713           (delq number gnus-newsgroup-unreads))
6714     (if gnus-newsgroup-auto-expire
6715         (push number gnus-newsgroup-expirable)
6716       (push (cons number gnus-low-score-mark)
6717             gnus-newsgroup-reads)))
6718   ;; Go recursively through all subthreads.
6719   (mapcar 'gnus-expunge-thread (cdr thread)))
6720
6721 ;; Summary article oriented commands
6722
6723 (defun gnus-summary-refer-parent-article (n)
6724   "Refer parent article N times.
6725 If N is negative, go to ancestor -N instead.
6726 The difference between N and the number of articles fetched is returned."
6727   (interactive "p")
6728   (let ((skip 1)
6729         error header ref)
6730     (when (not (natnump n))
6731       (setq skip (abs n)
6732             n 1))
6733     (while (and (> n 0)
6734                 (not error))
6735       (setq header (gnus-summary-article-header))
6736       (if (and (eq (mail-header-number header)
6737                    (cdr gnus-article-current))
6738                (equal gnus-newsgroup-name
6739                       (car gnus-article-current)))
6740           ;; If we try to find the parent of the currently
6741           ;; displayed article, then we take a look at the actual
6742           ;; References header, since this is slightly more
6743           ;; reliable than the References field we got from the
6744           ;; server.
6745           (save-excursion
6746             (set-buffer gnus-original-article-buffer)
6747             (nnheader-narrow-to-headers)
6748             (unless (setq ref (message-fetch-field "references"))
6749               (setq ref (message-fetch-field "in-reply-to")))
6750             (widen))
6751         (setq ref
6752               ;; It's not the current article, so we take a bet on
6753               ;; the value we got from the server.
6754               (mail-header-references header)))
6755       (if (and ref
6756                (not (equal ref "")))
6757           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6758             (gnus-message 1 "Couldn't find parent"))
6759         (gnus-message 1 "No references in article %d"
6760                       (gnus-summary-article-number))
6761         (setq error t))
6762       (decf n))
6763     (gnus-summary-position-point)
6764     n))
6765
6766 (defun gnus-summary-refer-references ()
6767   "Fetch all articles mentioned in the References header.
6768 Return the number of articles fetched."
6769   (interactive)
6770   (let ((ref (mail-header-references (gnus-summary-article-header)))
6771         (current (gnus-summary-article-number))
6772         (n 0))
6773     (if (or (not ref)
6774             (equal ref ""))
6775         (error "No References in the current article")
6776       ;; For each Message-ID in the References header...
6777       (while (string-match "<[^>]*>" ref)
6778         (incf n)
6779         ;; ... fetch that article.
6780         (gnus-summary-refer-article
6781          (prog1 (match-string 0 ref)
6782            (setq ref (substring ref (match-end 0))))))
6783       (gnus-summary-goto-subject current)
6784       (gnus-summary-position-point)
6785       n)))
6786
6787 (defun gnus-summary-refer-thread (&optional limit)
6788   "Fetch all articles in the current thread.
6789 If LIMIT (the numerical prefix), fetch that many old headers instead
6790 of what's specified by the `gnus-refer-thread-limit' variable."
6791   (interactive "P")
6792   (let ((id (mail-header-id (gnus-summary-article-header)))
6793         (limit (if limit (prefix-numeric-value limit)
6794                  gnus-refer-thread-limit)))
6795     ;; We want to fetch LIMIT *old* headers, but we also have to
6796     ;; re-fetch all the headers in the current buffer, because many of
6797     ;; them may be undisplayed.  So we adjust LIMIT.
6798     (when (numberp limit)
6799       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6800     (unless (eq gnus-fetch-old-headers 'invisible)
6801       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6802       ;; Retrieve the headers and read them in.
6803       (if (eq (gnus-retrieve-headers
6804                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6805               'nov)
6806           (gnus-build-all-threads)
6807         (error "Can't fetch thread from backends that don't support NOV"))
6808       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6809     (gnus-summary-limit-include-thread id)))
6810
6811 (defun gnus-summary-refer-article (message-id)
6812   "Fetch an article specified by MESSAGE-ID."
6813   (interactive "sMessage-ID: ")
6814   (when (and (stringp message-id)
6815              (not (zerop (length message-id))))
6816     ;; Construct the correct Message-ID if necessary.
6817     ;; Suggested by tale@pawl.rpi.edu.
6818     (unless (string-match "^<" message-id)
6819       (setq message-id (concat "<" message-id)))
6820     (unless (string-match ">$" message-id)
6821       (setq message-id (concat message-id ">")))
6822     (let* ((header (gnus-id-to-header message-id))
6823            (sparse (and header
6824                         (gnus-summary-article-sparse-p
6825                          (mail-header-number header))
6826                         (memq (mail-header-number header)
6827                               gnus-newsgroup-limit)))
6828            number)
6829       (cond
6830        ;; If the article is present in the buffer we just go to it.
6831        ((and header
6832              (or (not (gnus-summary-article-sparse-p
6833                        (mail-header-number header)))
6834                  sparse))
6835         (prog1
6836             (gnus-summary-goto-article
6837              (mail-header-number header) nil t)
6838           (when sparse
6839             (gnus-summary-update-article (mail-header-number header)))))
6840        (t
6841         ;; We fetch the article.
6842         (catch 'found
6843           (dolist (gnus-override-method (gnus-refer-article-methods))
6844             (gnus-check-server gnus-override-method)
6845             ;; Fetch the header, and display the article.
6846             (when (setq number (gnus-summary-insert-subject message-id))
6847               (gnus-summary-select-article nil nil nil number)
6848               (throw 'found t)))
6849           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6850
6851 (defun gnus-refer-article-methods ()
6852   "Return a list of referrable methods."
6853   (cond
6854    ;; No method, so we default to current and native.
6855    ((null gnus-refer-article-method)
6856     (list gnus-current-select-method gnus-select-method))
6857    ;; Current.
6858    ((eq 'current gnus-refer-article-method)
6859     (list gnus-current-select-method))
6860    ;; List of select methods.
6861    ((not (stringp (cadr gnus-refer-article-method)))
6862     (let (out)
6863       (dolist (method gnus-refer-article-method)
6864         (push (if (eq 'current method)
6865                   gnus-current-select-method
6866                 method)
6867               out))
6868       (nreverse out)))
6869    ;; One single select method.
6870    (t
6871     (list gnus-refer-article-method))))
6872
6873 (defun gnus-summary-edit-parameters ()
6874   "Edit the group parameters of the current group."
6875   (interactive)
6876   (gnus-group-edit-group gnus-newsgroup-name 'params))
6877
6878 (defun gnus-summary-customize-parameters ()
6879   "Customize the group parameters of the current group."
6880   (interactive)
6881   (gnus-group-customize gnus-newsgroup-name))
6882
6883 (defun gnus-summary-enter-digest-group (&optional force)
6884   "Enter an nndoc group based on the current article.
6885 If FORCE, force a digest interpretation.  If not, try
6886 to guess what the document format is."
6887   (interactive "P")
6888   (let ((conf gnus-current-window-configuration))
6889     (save-excursion
6890       (gnus-summary-select-article))
6891     (setq gnus-current-window-configuration conf)
6892     (let* ((name (format "%s-%d"
6893                          (gnus-group-prefixed-name
6894                           gnus-newsgroup-name (list 'nndoc ""))
6895                          (save-excursion
6896                            (set-buffer gnus-summary-buffer)
6897                            gnus-current-article)))
6898            (ogroup gnus-newsgroup-name)
6899            (params (append (gnus-info-params (gnus-get-info ogroup))
6900                            (list (cons 'to-group ogroup))
6901                            (list (cons 'save-article-group ogroup))))
6902            (case-fold-search t)
6903            (buf (current-buffer))
6904            dig to-address)
6905       (save-excursion
6906         (set-buffer gnus-original-article-buffer)
6907         ;; Have the digest group inherit the main mail address of
6908         ;; the parent article.
6909         (when (setq to-address (or (message-fetch-field "reply-to")
6910                                    (message-fetch-field "from")))
6911           (setq params (append (list (cons 'to-address to-address)))))
6912         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6913         (insert-buffer-substring gnus-original-article-buffer)
6914         ;; Remove lines that may lead nndoc to misinterpret the
6915         ;; document type.
6916         (narrow-to-region
6917          (goto-char (point-min))
6918          (or (search-forward "\n\n" nil t) (point)))
6919         (goto-char (point-min))
6920         (delete-matching-lines "^Path:\\|^From ")
6921         (widen))
6922       (unwind-protect
6923           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6924                     (gnus-newsgroup-ephemeral-ignored-charsets
6925                      gnus-newsgroup-ignored-charsets))
6926                 (gnus-group-read-ephemeral-group
6927                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6928                               (nndoc-article-type
6929                                ,(if force 'mbox 'guess))) t))
6930               ;; Make all postings to this group go to the parent group.
6931               (nconc (gnus-info-params (gnus-get-info name))
6932                      params)
6933             ;; Couldn't select this doc group.
6934             (switch-to-buffer buf)
6935             (gnus-set-global-variables)
6936             (gnus-configure-windows 'summary)
6937             (gnus-message 3 "Article couldn't be entered?"))
6938         (kill-buffer dig)))))
6939
6940 (defun gnus-summary-read-document (n)
6941   "Open a new group based on the current article(s).
6942 This will allow you to read digests and other similar
6943 documents as newsgroups.
6944 Obeys the standard process/prefix convention."
6945   (interactive "P")
6946   (let* ((articles (gnus-summary-work-articles n))
6947          (ogroup gnus-newsgroup-name)
6948          (params (append (gnus-info-params (gnus-get-info ogroup))
6949                          (list (cons 'to-group ogroup))))
6950          article group egroup groups vgroup)
6951     (while (setq article (pop articles))
6952       (setq group (format "%s-%d" gnus-newsgroup-name article))
6953       (gnus-summary-remove-process-mark article)
6954       (when (gnus-summary-display-article article)
6955         (save-excursion
6956           (with-temp-buffer
6957             (insert-buffer-substring gnus-original-article-buffer)
6958             ;; Remove some headers that may lead nndoc to make
6959             ;; the wrong guess.
6960             (message-narrow-to-head)
6961             (goto-char (point-min))
6962             (delete-matching-lines "^\\(Path\\):\\|^From ")
6963             (widen)
6964             (if (setq egroup
6965                       (gnus-group-read-ephemeral-group
6966                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6967                                      (nndoc-article-type guess))
6968                        t nil t))
6969                 (progn
6970                   ;; Make all postings to this group go to the parent group.
6971                   (nconc (gnus-info-params (gnus-get-info egroup))
6972                          params)
6973                   (push egroup groups))
6974               ;; Couldn't select this doc group.
6975               (gnus-error 3 "Article couldn't be entered"))))))
6976     ;; Now we have selected all the documents.
6977     (cond
6978      ((not groups)
6979       (error "None of the articles could be interpreted as documents"))
6980      ((gnus-group-read-ephemeral-group
6981        (setq vgroup (format
6982                      "nnvirtual:%s-%s" gnus-newsgroup-name
6983                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6984        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6985        t
6986        (cons (current-buffer) 'summary)))
6987      (t
6988       (error "Couldn't select virtual nndoc group")))))
6989
6990 (defun gnus-summary-isearch-article (&optional regexp-p)
6991   "Do incremental search forward on the current article.
6992 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6993   (interactive "P")
6994   (let* ((gnus-inhibit-treatment t)
6995          (old (gnus-summary-select-article)))
6996     (gnus-configure-windows 'article)
6997     (gnus-eval-in-buffer-window gnus-article-buffer
6998       (save-restriction
6999         (widen)
7000         (when (eq 'old old)
7001           (gnus-article-show-all-headers))
7002         (goto-char (point-min))
7003         (isearch-forward regexp-p)))))
7004
7005 (defun gnus-summary-search-article-forward (regexp &optional backward)
7006   "Search for an article containing REGEXP forward.
7007 If BACKWARD, search backward instead."
7008   (interactive
7009    (list (read-string
7010           (format "Search article %s (regexp%s): "
7011                   (if current-prefix-arg "backward" "forward")
7012                   (if gnus-last-search-regexp
7013                       (concat ", default " gnus-last-search-regexp)
7014                     "")))
7015          current-prefix-arg))
7016   (if (string-equal regexp "")
7017       (setq regexp (or gnus-last-search-regexp ""))
7018     (setq gnus-last-search-regexp regexp))
7019   (if (gnus-summary-search-article regexp backward)
7020       (gnus-summary-show-thread)
7021     (error "Search failed: \"%s\"" regexp)))
7022
7023 (defun gnus-summary-search-article-backward (regexp)
7024   "Search for an article containing REGEXP backward."
7025   (interactive
7026    (list (read-string
7027           (format "Search article backward (regexp%s): "
7028                   (if gnus-last-search-regexp
7029                       (concat ", default " gnus-last-search-regexp)
7030                     "")))))
7031   (gnus-summary-search-article-forward regexp 'backward))
7032
7033 (eval-when-compile
7034   (defmacro gnus-summary-search-article-position-point (regexp backward)
7035     "Dehighlight the last matched text and goto the beginning position."
7036     (` (if (and gnus-summary-search-article-matched-data
7037                 (let ((text (caddr gnus-summary-search-article-matched-data))
7038                       (inhibit-read-only t)
7039                       buffer-read-only)
7040                   (delete-region
7041                    (goto-char (car gnus-summary-search-article-matched-data))
7042                    (cadr gnus-summary-search-article-matched-data))
7043                   (insert text)
7044                   (string-match (, regexp) text)))
7045            (if (, backward) (beginning-of-line) (end-of-line))
7046          (goto-char (if (, backward) (point-max) (point-min))))))
7047
7048   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7049     "Place point where X-Face image is displayed."
7050     (if (featurep 'xemacs)
7051         (` (let ((end (if (search-forward "\n\n" nil t)
7052                           (goto-char (1- (point)))
7053                         (point-min)))
7054                  extent)
7055              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7056              (unless (and (re-search-forward "^From:" end t)
7057                           (setq extent (extent-at (point)))
7058                           (extent-begin-glyph extent))
7059                (goto-char (, opoint)))))
7060       (` (let ((end (if (search-forward "\n\n" nil t)
7061                         (goto-char (1- (point)))
7062                       (point-min))))
7063            (goto-char
7064             (or (text-property-any (or (search-backward "\n\n" nil t)
7065                                        (point-min))
7066                                    end 'x-face-mule-bitmap-image t)
7067                 (, opoint)))))))
7068
7069   (defmacro gnus-summary-search-article-highlight-matched-text
7070     (backward treated x-face)
7071     "Highlight matched text in the function `gnus-summary-search-article'."
7072     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7073              (end (set-marker (make-marker) (match-end 0)))
7074              (inhibit-read-only t)
7075              buffer-read-only)
7076          (unless treated
7077            (let ((,@
7078                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7079                     (mapcar
7080                      (lambda (item) (setq items (delq item items)))
7081                      '(gnus-treat-buttonize
7082                        gnus-treat-fill-article
7083                        gnus-treat-fill-long-lines
7084                        gnus-treat-emphasize
7085                        gnus-treat-highlight-headers
7086                        gnus-treat-highlight-citation
7087                        gnus-treat-highlight-signature
7088                        gnus-treat-overstrike
7089                        gnus-treat-display-xface
7090                        gnus-treat-buttonize-head
7091                        gnus-treat-decode-article-as-default-mime-charset))
7092                     (static-if (featurep 'xemacs)
7093                         items
7094                       (cons '(x-face-mule-delete-x-face-field
7095                               (quote never))
7096                             items))))
7097                  (gnus-treat-display-xface
7098                   (when (, x-face) gnus-treat-display-xface)))
7099              (gnus-article-prepare-mime-display)))
7100          (goto-char (if (, backward) start end))
7101          (when (, x-face)
7102            (gnus-summary-search-article-highlight-goto-x-face (point)))
7103          (setq gnus-summary-search-article-matched-data
7104                (list start end (buffer-substring start end)))
7105          (unless (eq start end);; matched text has been deleted. :-<
7106            (put-text-property start end 'face
7107                               (or (find-face 'isearch)
7108                                   'secondary-selection))))))
7109   )
7110
7111 (defun gnus-summary-search-article (regexp &optional backward)
7112   "Search for an article containing REGEXP.
7113 Optional argument BACKWARD means do search for backward.
7114 `gnus-select-article-hook' is not called during the search."
7115   ;; We have to require this here to make sure that the following
7116   ;; dynamic binding isn't shadowed by autoloading.
7117   (require 'gnus-async)
7118   (require 'gnus-art)
7119   (let ((gnus-select-article-hook nil)  ;Disable hook.
7120         (gnus-article-display-hook nil)
7121         (gnus-article-prepare-hook nil)
7122         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7123         (gnus-use-article-prefetch nil)
7124         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7125         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7126         (sum (current-buffer))
7127         (found nil)
7128         point treated)
7129     (gnus-save-hidden-threads
7130       (static-if (featurep 'xemacs)
7131           (let ((gnus-inhibit-treatment t))
7132             (setq treated (eq 'old (gnus-summary-select-article)))
7133             (when (and treated
7134                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7135                                  (window-live-p (get-buffer-window
7136                                                  gnus-article-buffer t)))))
7137               (gnus-summary-select-article nil t)
7138               (setq treated nil)))
7139         (let ((gnus-inhibit-treatment t)
7140               (x-face-mule-delete-x-face-field 'never))
7141           (setq treated (eq 'old (gnus-summary-select-article)))
7142           (when (and treated
7143                      (not
7144                       (and (gnus-buffer-live-p gnus-article-buffer)
7145                            (window-live-p (get-buffer-window
7146                                            gnus-article-buffer t))
7147                            (or (not (string-match "^\\^X-Face:" regexp))
7148                                (with-current-buffer gnus-article-buffer
7149                                  gnus-summary-search-article-matched-data)))))
7150             (gnus-summary-select-article nil t)
7151             (setq treated nil))))
7152       (set-buffer gnus-article-buffer)
7153       (widen)
7154       (if treated
7155           (progn
7156             (gnus-article-show-all-headers)
7157             (gnus-summary-search-article-position-point regexp backward))
7158         (goto-char (if backward (point-max) (point-min))))
7159       (while (not found)
7160         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7161         (if (if backward
7162                 (re-search-backward regexp nil t)
7163               (re-search-forward regexp nil t))
7164             ;; We found the regexp.
7165             (progn
7166               (gnus-summary-search-article-highlight-matched-text
7167                backward treated (string-match "^\\^X-Face:" regexp))
7168               (setq found 'found)
7169               (forward-line
7170                (/ (- 2 (window-height
7171                         (get-buffer-window gnus-article-buffer t)))
7172                   2))
7173               (set-window-start
7174                (get-buffer-window (current-buffer))
7175                (point))
7176               (set-buffer sum)
7177               (setq point (point)))
7178           ;; We didn't find it, so we go to the next article.
7179           (set-buffer sum)
7180           (setq found 'not)
7181           (while (eq found 'not)
7182             (if (not (if backward (gnus-summary-find-prev)
7183                        (gnus-summary-find-next)))
7184                 ;; No more articles.
7185                 (setq found t)
7186               ;; Select the next article and adjust point.
7187               (unless (gnus-summary-article-sparse-p
7188                        (gnus-summary-article-number))
7189                 (setq found nil)
7190                 (let ((gnus-inhibit-treatment t))
7191                   (gnus-summary-select-article))
7192                 (setq treated nil)
7193                 (set-buffer gnus-article-buffer)
7194                 (widen)
7195                 (goto-char (if backward (point-max) (point-min))))))))
7196       (gnus-message 7 ""))
7197     ;; Return whether we found the regexp.
7198     (when (eq found 'found)
7199       (goto-char point)
7200       (gnus-summary-show-thread)
7201       (gnus-summary-goto-subject gnus-current-article)
7202       (gnus-summary-position-point)
7203       t)))
7204
7205 (defun gnus-summary-find-matching (header regexp &optional backward unread
7206                                           not-case-fold)
7207   "Return a list of all articles that match REGEXP on HEADER.
7208 The search stars on the current article and goes forwards unless
7209 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7210 If UNREAD is non-nil, only unread articles will
7211 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7212 in the comparisons."
7213   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7214                 (gnus-data-find-list
7215                  (gnus-summary-article-number) (gnus-data-list backward))))
7216         (case-fold-search (not not-case-fold))
7217         articles d func)
7218     (if (consp header)
7219         (if (eq (car header) 'extra)
7220             (setq func
7221                   `(lambda (h)
7222                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7223                          "")))
7224           (error "%s is an invalid header" header))
7225       (unless (fboundp (intern (concat "mail-header-" header)))
7226         (error "%s is not a valid header" header))
7227       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7228     (while data
7229       (setq d (car data))
7230       (and (or (not unread)             ; We want all articles...
7231                (gnus-data-unread-p d))  ; Or just unreads.
7232            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7233            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7234            (push (gnus-data-number d) articles)) ; Success!
7235       (setq data (cdr data)))
7236     (nreverse articles)))
7237
7238 (defun gnus-summary-execute-command (header regexp command &optional backward)
7239   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7240 If HEADER is an empty string (or nil), the match is done on the entire
7241 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7242   (interactive
7243    (list (let ((completion-ignore-case t))
7244            (completing-read
7245             "Header name: "
7246             (mapcar (lambda (string) (list string))
7247                     '("Number" "Subject" "From" "Lines" "Date"
7248                       "Message-ID" "Xref" "References" "Body"))
7249             nil 'require-match))
7250          (read-string "Regexp: ")
7251          (read-key-sequence "Command: ")
7252          current-prefix-arg))
7253   (when (equal header "Body")
7254     (setq header ""))
7255   ;; Hidden thread subtrees must be searched as well.
7256   (gnus-summary-show-all-threads)
7257   ;; We don't want to change current point nor window configuration.
7258   (save-excursion
7259     (save-window-excursion
7260       (gnus-message 6 "Executing %s..." (key-description command))
7261       ;; We'd like to execute COMMAND interactively so as to give arguments.
7262       (gnus-execute header regexp
7263                     `(call-interactively ',(key-binding command))
7264                     backward)
7265       (gnus-message 6 "Executing %s...done" (key-description command)))))
7266
7267 (defun gnus-summary-beginning-of-article ()
7268   "Scroll the article back to the beginning."
7269   (interactive)
7270   (gnus-summary-select-article)
7271   (gnus-configure-windows 'article)
7272   (gnus-eval-in-buffer-window gnus-article-buffer
7273     (widen)
7274     (goto-char (point-min))
7275     (when gnus-page-broken
7276       (gnus-narrow-to-page))))
7277
7278 (defun gnus-summary-end-of-article ()
7279   "Scroll to the end of the article."
7280   (interactive)
7281   (gnus-summary-select-article)
7282   (gnus-configure-windows 'article)
7283   (gnus-eval-in-buffer-window gnus-article-buffer
7284     (widen)
7285     (goto-char (point-max))
7286     (recenter -3)
7287     (when gnus-page-broken
7288       (gnus-narrow-to-page))))
7289
7290 (defun gnus-summary-print-article (&optional filename n)
7291   "Generate and print a PostScript image of the N next (mail) articles.
7292
7293 If N is negative, print the N previous articles.  If N is nil and articles
7294 have been marked with the process mark, print these instead.
7295
7296 If the optional first argument FILENAME is nil, send the image to the
7297 printer.  If FILENAME is a string, save the PostScript image in a file with
7298 that name.  If FILENAME is a number, prompt the user for the name of the file
7299 to save in."
7300   (interactive (list (ps-print-preprint current-prefix-arg)
7301                      current-prefix-arg))
7302   (dolist (article (gnus-summary-work-articles n))
7303     (gnus-summary-select-article nil nil 'pseudo article)
7304     (gnus-eval-in-buffer-window gnus-article-buffer
7305       (let ((buffer (generate-new-buffer " *print*")))
7306         (unwind-protect
7307             (progn
7308               (copy-to-buffer buffer (point-min) (point-max))
7309               (set-buffer buffer)
7310               (gnus-article-delete-invisible-text)
7311               (let ((ps-left-header
7312                      (list
7313                       (concat "("
7314                               (mail-header-subject gnus-current-headers) ")")
7315                       (concat "("
7316                               (mail-header-from gnus-current-headers) ")")))
7317                     (ps-right-header
7318                      (list
7319                       "/pagenumberstring load"
7320                       (concat "("
7321                               (mail-header-date gnus-current-headers) ")"))))
7322                 (gnus-run-hooks 'gnus-ps-print-hook)
7323                 (save-excursion
7324                   (ps-print-buffer-with-faces filename))))
7325           (kill-buffer buffer))))))
7326
7327 (defun gnus-summary-show-article (&optional arg)
7328   "Force re-fetching of the current article.
7329 If ARG (the prefix) is a number, show the article with the charset 
7330 defined in `gnus-summary-show-article-charset-alist', or the charset
7331 inputed.
7332 If ARG (the prefix) is non-nil and not a number, show the raw article 
7333 without any article massaging functions being run."
7334   (interactive "P")
7335   (cond 
7336    ((numberp arg)
7337     (let ((gnus-newsgroup-charset 
7338            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7339                (read-coding-system "Charset: ")))
7340           (gnus-newsgroup-ignored-charsets 'gnus-all))
7341       (gnus-summary-select-article nil 'force)))
7342    ((not arg)
7343     ;; Select the article the normal way.
7344     (gnus-summary-select-article nil 'force))
7345    (t
7346     ;; We have to require this here to make sure that the following
7347     ;; dynamic binding isn't shadowed by autoloading.
7348     (require 'gnus-async)
7349     (require 'gnus-art)
7350     ;; Bind the article treatment functions to nil.
7351     (let ((gnus-have-all-headers t)
7352           gnus-article-display-hook
7353           gnus-article-prepare-hook
7354           gnus-article-decode-hook
7355           gnus-break-pages
7356           gnus-show-mime)
7357       (gnus-summary-select-article nil 'force))))
7358   (gnus-summary-goto-subject gnus-current-article)
7359   (gnus-summary-position-point))
7360
7361 (defun gnus-summary-verbose-headers (&optional arg)
7362   "Toggle permanent full header display.
7363 If ARG is a positive number, turn header display on.
7364 If ARG is a negative number, turn header display off."
7365   (interactive "P")
7366   (setq gnus-show-all-headers
7367         (cond ((or (not (numberp arg))
7368                    (zerop arg))
7369                (not gnus-show-all-headers))
7370               ((natnump arg)
7371                t)))
7372   (gnus-summary-show-article))
7373
7374 (defun gnus-summary-toggle-header (&optional arg)
7375   "Show the headers if they are hidden, or hide them if they are shown.
7376 If ARG is a positive number, show the entire header.
7377 If ARG is a negative number, hide the unwanted header lines."
7378   (interactive "P")
7379   (save-excursion
7380     (set-buffer gnus-article-buffer)
7381     (save-restriction
7382       (let* ((buffer-read-only nil)
7383              (inhibit-point-motion-hooks t)
7384              hidden e)
7385         (setq hidden
7386               (if (numberp arg)
7387                   (>= arg 0)
7388                 (save-restriction 
7389                   (article-narrow-to-head)
7390                   (gnus-article-hidden-text-p 'headers))))
7391         (goto-char (point-min))
7392         (when (search-forward "\n\n" nil t)
7393           (delete-region (point-min) (1- (point))))
7394         (goto-char (point-min))
7395         (save-excursion
7396           (set-buffer gnus-original-article-buffer)
7397           (goto-char (point-min))
7398           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7399         (insert-buffer-substring gnus-original-article-buffer 1 e)
7400         (save-restriction
7401           (narrow-to-region (point-min) (point))
7402           (article-decode-encoded-words)
7403           (if  hidden
7404               (let ((gnus-treat-hide-headers nil)
7405                     (gnus-treat-hide-boring-headers nil))
7406                 (gnus-treat-article 'head))
7407             (gnus-treat-article 'head)))))))
7408
7409 (defun gnus-summary-show-all-headers ()
7410   "Make all header lines visible."
7411   (interactive)
7412   (gnus-article-show-all-headers))
7413
7414 (defun gnus-summary-toggle-mime (&optional arg)
7415   "Toggle MIME processing.
7416 If ARG is a positive number, turn MIME processing on."
7417   (interactive "P")
7418   (setq gnus-show-mime
7419         (if (null arg)
7420             (not gnus-show-mime)
7421           (> (prefix-numeric-value arg) 0)))
7422   (gnus-summary-select-article t 'force))
7423
7424 (defun gnus-summary-caesar-message (&optional arg)
7425   "Caesar rotate the current article by 13.
7426 The numerical prefix specifies how many places to rotate each letter
7427 forward."
7428   (interactive "P")
7429   (gnus-summary-select-article)
7430   (let ((mail-header-separator ""))
7431     (gnus-eval-in-buffer-window gnus-article-buffer
7432       (save-restriction
7433         (widen)
7434         (let ((start (window-start))
7435               buffer-read-only)
7436           (message-caesar-buffer-body arg)
7437           (set-window-start (get-buffer-window (current-buffer)) start))))))
7438
7439 (defun gnus-summary-stop-page-breaking ()
7440   "Stop page breaking in the current article."
7441   (interactive)
7442   (gnus-summary-select-article)
7443   (gnus-eval-in-buffer-window gnus-article-buffer
7444     (widen)
7445     (when (gnus-visual-p 'page-marker)
7446       (let ((buffer-read-only nil))
7447         (gnus-remove-text-with-property 'gnus-prev)
7448         (gnus-remove-text-with-property 'gnus-next))
7449       (setq gnus-page-broken nil))))
7450
7451 (defun gnus-summary-move-article (&optional n to-newsgroup
7452                                             select-method action)
7453   "Move the current article to a different newsgroup.
7454 If N is a positive number, move the N next articles.
7455 If N is a negative number, move the N previous articles.
7456 If N is nil and any articles have been marked with the process mark,
7457 move those articles instead.
7458 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7459 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7460 re-spool using this method.
7461
7462 For this function to work, both the current newsgroup and the
7463 newsgroup that you want to move to have to support the `request-move'
7464 and `request-accept' functions.
7465
7466 ACTION can be either `move' (the default), `crosspost' or `copy'."
7467   (interactive "P")
7468   (unless action
7469     (setq action 'move))
7470   ;; Disable marking as read.
7471   (let (gnus-mark-article-hook)
7472     (save-window-excursion
7473       (gnus-summary-select-article)))
7474   ;; Check whether the source group supports the required functions.
7475   (cond ((and (eq action 'move)
7476               (not (gnus-check-backend-function
7477                     'request-move-article gnus-newsgroup-name)))
7478          (error "The current group does not support article moving"))
7479         ((and (eq action 'crosspost)
7480               (not (gnus-check-backend-function
7481                     'request-replace-article gnus-newsgroup-name)))
7482          (error "The current group does not support article editing")))
7483   (let ((articles (gnus-summary-work-articles n))
7484         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7485         (names '((move "Move" "Moving")
7486                  (copy "Copy" "Copying")
7487                  (crosspost "Crosspost" "Crossposting")))
7488         (copy-buf (save-excursion
7489                     (nnheader-set-temp-buffer " *copy article*")))
7490         (default-marks gnus-article-mark-lists)
7491         (no-expire-marks (delete '(expirable . expire)
7492                                  (copy-sequence gnus-article-mark-lists)))
7493         art-group to-method new-xref article to-groups)
7494     (unless (assq action names)
7495       (error "Unknown action %s" action))
7496     ;; Read the newsgroup name.
7497     (when (and (not to-newsgroup)
7498                (not select-method))
7499       (setq to-newsgroup
7500             (gnus-read-move-group-name
7501              (cadr (assq action names))
7502              (symbol-value (intern (format "gnus-current-%s-group" action)))
7503              articles prefix))
7504       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7505     (setq to-method (or select-method
7506                         (gnus-group-name-to-method to-newsgroup)))
7507     ;; Check the method we are to move this article to...
7508     (unless (gnus-check-backend-function
7509              'request-accept-article (car to-method))
7510       (error "%s does not support article copying" (car to-method)))
7511     (unless (gnus-check-server to-method)
7512       (error "Can't open server %s" (car to-method)))
7513     (gnus-message 6 "%s to %s: %s..."
7514                   (caddr (assq action names))
7515                   (or (car select-method) to-newsgroup) articles)
7516     (while articles
7517       (setq article (pop articles))
7518       (setq
7519        art-group
7520        (cond
7521         ;; Move the article.
7522         ((eq action 'move)
7523          ;; Remove this article from future suppression.
7524          (gnus-dup-unsuppress-article article)
7525          (gnus-request-move-article
7526           article                       ; Article to move
7527           gnus-newsgroup-name           ; From newsgroup
7528           (nth 1 (gnus-find-method-for-group
7529                   gnus-newsgroup-name)) ; Server
7530           (list 'gnus-request-accept-article
7531                 to-newsgroup (list 'quote select-method)
7532                 (not articles) t)       ; Accept form
7533           (not articles)))              ; Only save nov last time
7534         ;; Copy the article.
7535         ((eq action 'copy)
7536          (save-excursion
7537            (set-buffer copy-buf)
7538            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7539              (gnus-request-accept-article
7540               to-newsgroup select-method (not articles) t))))
7541         ;; Crosspost the article.
7542         ((eq action 'crosspost)
7543          (let ((xref (message-tokenize-header
7544                       (mail-header-xref (gnus-summary-article-header article))
7545                       " ")))
7546            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7547                                   ":" article))
7548            (unless xref
7549              (setq xref (list (system-name))))
7550            (setq new-xref
7551                  (concat
7552                   (mapconcat 'identity
7553                              (delete "Xref:" (delete new-xref xref))
7554                              " ")
7555                   " " new-xref))
7556            (save-excursion
7557              (set-buffer copy-buf)
7558              ;; First put the article in the destination group.
7559              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7560              (when (consp (setq art-group
7561                                 (gnus-request-accept-article
7562                                  to-newsgroup select-method (not articles))))
7563                (setq new-xref (concat new-xref " " (car art-group)
7564                                       ":" (cdr art-group)))
7565                ;; Now we have the new Xrefs header, so we insert
7566                ;; it and replace the new article.
7567                (nnheader-replace-header "Xref" new-xref)
7568                (gnus-request-replace-article
7569                 (cdr art-group) to-newsgroup (current-buffer))
7570                art-group))))))
7571       (cond
7572        ((not art-group)
7573         (gnus-message 1 "Couldn't %s article %s: %s"
7574                       (cadr (assq action names)) article
7575                       (nnheader-get-report (car to-method))))
7576        ((and (eq art-group 'junk)
7577              (eq action 'move))
7578         (gnus-summary-mark-article article gnus-canceled-mark)
7579         (gnus-message 4 "Deleted article %s" article))
7580        (t
7581         (let* ((pto-group (gnus-group-prefixed-name
7582                            (car art-group) to-method))
7583                (entry
7584                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7585                (info (nth 2 entry))
7586                (to-group (gnus-info-group info))
7587                to-marks)
7588           ;; Update the group that has been moved to.
7589           (when (and info
7590                      (memq action '(move copy)))
7591             (unless (member to-group to-groups)
7592               (push to-group to-groups))
7593
7594             (unless (memq article gnus-newsgroup-unreads)
7595               (push 'read to-marks)
7596               (gnus-info-set-read
7597                info (gnus-add-to-range (gnus-info-read info)
7598                                        (list (cdr art-group)))))
7599
7600             ;; See whether the article is to be put in the cache.
7601             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7602                              default-marks
7603                            no-expire-marks))
7604                   (to-article (cdr art-group)))
7605
7606               ;; Enter the article into the cache in the new group,
7607               ;; if that is required.
7608               (when gnus-use-cache
7609                 (gnus-cache-possibly-enter-article
7610                  to-group to-article
7611                  (let ((header (copy-sequence
7612                                 (gnus-summary-article-header article))))
7613                    (mail-header-set-number header to-article)
7614                    header)
7615                  (memq article gnus-newsgroup-marked)
7616                  (memq article gnus-newsgroup-dormant)
7617                  (memq article gnus-newsgroup-unreads)))
7618
7619               (when gnus-preserve-marks 
7620                 ;; Copy any marks over to the new group.
7621                 (when (and (equal to-group gnus-newsgroup-name)
7622                            (not (memq article gnus-newsgroup-unreads)))
7623                   ;; Mark this article as read in this group.
7624                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7625                   (setcdr (gnus-active to-group) to-article)
7626                   (setcdr gnus-newsgroup-active to-article))
7627
7628                 (while marks
7629                   (when (memq article (symbol-value
7630                                        (intern (format "gnus-newsgroup-%s"
7631                                                        (caar marks)))))
7632                     (push (cdar marks) to-marks)
7633                     ;; If the other group is the same as this group,
7634                     ;; then we have to add the mark to the list.
7635                     (when (equal to-group gnus-newsgroup-name)
7636                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7637                            (cons to-article
7638                                  (symbol-value
7639                                   (intern (format "gnus-newsgroup-%s"
7640                                                   (caar marks)))))))
7641                     ;; Copy the marks to other group.
7642                     (gnus-add-marked-articles
7643                      to-group (cdar marks) (list to-article) info))
7644                   (setq marks (cdr marks)))
7645
7646                 (gnus-request-set-mark to-group (list (list (list to-article)
7647                                                             'set
7648                                                             to-marks))))
7649
7650               (gnus-dribble-enter
7651                (concat "(gnus-group-set-info '"
7652                        (gnus-prin1-to-string (gnus-get-info to-group))
7653                        ")"))))
7654
7655           ;; Update the Xref header in this article to point to
7656           ;; the new crossposted article we have just created.
7657           (when (eq action 'crosspost)
7658             (save-excursion
7659               (set-buffer copy-buf)
7660               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7661               (nnheader-replace-header "Xref" new-xref)
7662               (gnus-request-replace-article
7663                article gnus-newsgroup-name (current-buffer)))))
7664
7665         ;;;!!!Why is this necessary?
7666         (set-buffer gnus-summary-buffer)
7667
7668         (gnus-summary-goto-subject article)
7669         (when (eq action 'move)
7670           (gnus-summary-mark-article article gnus-canceled-mark))))
7671       (gnus-summary-remove-process-mark article))
7672     ;; Re-activate all groups that have been moved to.
7673     (while to-groups
7674       (save-excursion
7675         (set-buffer gnus-group-buffer)
7676         (when (gnus-group-goto-group (car to-groups) t)
7677           (gnus-group-get-new-news-this-group 1 t))
7678         (pop to-groups)))
7679
7680     (gnus-kill-buffer copy-buf)
7681     (gnus-summary-position-point)
7682     (gnus-set-mode-line 'summary)))
7683
7684 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7685   "Move the current article to a different newsgroup.
7686 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7687 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7688 re-spool using this method."
7689   (interactive "P")
7690   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7691
7692 (defun gnus-summary-crosspost-article (&optional n)
7693   "Crosspost the current article to some other group."
7694   (interactive "P")
7695   (gnus-summary-move-article n nil nil 'crosspost))
7696
7697 (defcustom gnus-summary-respool-default-method nil
7698   "Default method for respooling an article.
7699 If nil, use to the current newsgroup method."
7700   :type '(choice (gnus-select-method :value (nnml ""))
7701                  (const nil))
7702   :group 'gnus-summary-mail)
7703
7704 (defun gnus-summary-respool-article (&optional n method)
7705   "Respool the current article.
7706 The article will be squeezed through the mail spooling process again,
7707 which means that it will be put in some mail newsgroup or other
7708 depending on `nnmail-split-methods'.
7709 If N is a positive number, respool the N next articles.
7710 If N is a negative number, respool the N previous articles.
7711 If N is nil and any articles have been marked with the process mark,
7712 respool those articles instead.
7713
7714 Respooling can be done both from mail groups and \"real\" newsgroups.
7715 In the former case, the articles in question will be moved from the
7716 current group into whatever groups they are destined to.  In the
7717 latter case, they will be copied into the relevant groups."
7718   (interactive
7719    (list current-prefix-arg
7720          (let* ((methods (gnus-methods-using 'respool))
7721                 (methname
7722                  (symbol-name (or gnus-summary-respool-default-method
7723                                   (car (gnus-find-method-for-group
7724                                         gnus-newsgroup-name)))))
7725                 (method
7726                  (gnus-completing-read
7727                   methname "What backend do you want to use when respooling?"
7728                   methods nil t nil 'gnus-mail-method-history))
7729                 ms)
7730            (cond
7731             ((zerop (length (setq ms (gnus-servers-using-backend
7732                                       (intern method)))))
7733              (list (intern method) ""))
7734             ((= 1 (length ms))
7735              (car ms))
7736             (t
7737              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7738                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7739                            ms-alist))))))))
7740   (unless method
7741     (error "No method given for respooling"))
7742   (if (assoc (symbol-name
7743               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7744              (gnus-methods-using 'respool))
7745       (gnus-summary-move-article n nil method)
7746     (gnus-summary-copy-article n nil method)))
7747
7748 (defun gnus-summary-import-article (file)
7749   "Import an arbitrary file into a mail newsgroup."
7750   (interactive "fImport file: ")
7751   (let ((group gnus-newsgroup-name)
7752         (now (current-time))
7753         atts lines)
7754     (unless (gnus-check-backend-function 'request-accept-article group)
7755       (error "%s does not support article importing" group))
7756     (or (file-readable-p file)
7757         (not (file-regular-p file))
7758         (error "Can't read %s" file))
7759     (save-excursion
7760       (set-buffer (gnus-get-buffer-create " *import file*"))
7761       (erase-buffer)
7762       (nnheader-insert-file-contents file)
7763       (goto-char (point-min))
7764       (unless (nnheader-article-p)
7765         ;; This doesn't look like an article, so we fudge some headers.
7766         (setq atts (file-attributes file)
7767               lines (count-lines (point-min) (point-max)))
7768         (insert "From: " (read-string "From: ") "\n"
7769                 "Subject: " (read-string "Subject: ") "\n"
7770                 "Date: " (message-make-date (nth 5 atts))
7771                 "\n"
7772                 "Message-ID: " (message-make-message-id) "\n"
7773                 "Lines: " (int-to-string lines) "\n"
7774                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7775       (gnus-request-accept-article group nil t)
7776       (kill-buffer (current-buffer)))))
7777
7778 (defun gnus-summary-article-posted-p ()
7779   "Say whether the current (mail) article is available from news as well.
7780 This will be the case if the article has both been mailed and posted."
7781   (interactive)
7782   (let ((id (mail-header-references (gnus-summary-article-header)))
7783         (gnus-override-method (car (gnus-refer-article-methods))))
7784     (if (gnus-request-head id "")
7785         (gnus-message 2 "The current message was found on %s"
7786                       gnus-override-method)
7787       (gnus-message 2 "The current message couldn't be found on %s"
7788                     gnus-override-method)
7789       nil)))
7790
7791 (defun gnus-summary-expire-articles (&optional now)
7792   "Expire all articles that are marked as expirable in the current group."
7793   (interactive)
7794   (when (gnus-check-backend-function
7795          'request-expire-articles gnus-newsgroup-name)
7796     ;; This backend supports expiry.
7797     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7798            (expirable (if total
7799                           (progn
7800                             ;; We need to update the info for
7801                             ;; this group for `gnus-list-of-read-articles'
7802                             ;; to give us the right answer.
7803                             (gnus-run-hooks 'gnus-exit-group-hook)
7804                             (gnus-summary-update-info)
7805                             (gnus-list-of-read-articles gnus-newsgroup-name))
7806                         (setq gnus-newsgroup-expirable
7807                               (sort gnus-newsgroup-expirable '<))))
7808            (expiry-wait (if now 'immediate
7809                           (gnus-group-find-parameter
7810                            gnus-newsgroup-name 'expiry-wait)))
7811            es)
7812       (when expirable
7813         ;; There are expirable articles in this group, so we run them
7814         ;; through the expiry process.
7815         (gnus-message 6 "Expiring articles...")
7816         (unless (gnus-check-group gnus-newsgroup-name)
7817           (error "Can't open server for %s" gnus-newsgroup-name))
7818         ;; The list of articles that weren't expired is returned.
7819         (save-excursion
7820           (if expiry-wait
7821               (let ((nnmail-expiry-wait-function nil)
7822                     (nnmail-expiry-wait expiry-wait))
7823                 (setq es (gnus-request-expire-articles
7824                           expirable gnus-newsgroup-name)))
7825             (setq es (gnus-request-expire-articles
7826                       expirable gnus-newsgroup-name))))
7827         (unless total
7828           (setq gnus-newsgroup-expirable es))
7829         ;; We go through the old list of expirable, and mark all
7830         ;; really expired articles as nonexistent.
7831         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7832           (let ((gnus-use-cache nil))
7833             (while expirable
7834               (unless (memq (car expirable) es)
7835                 (when (gnus-data-find (car expirable))
7836                   (gnus-summary-mark-article
7837                    (car expirable) gnus-canceled-mark)))
7838               (setq expirable (cdr expirable)))))
7839         (gnus-message 6 "Expiring articles...done")))))
7840
7841 (defun gnus-summary-expire-articles-now ()
7842   "Expunge all expirable articles in the current group.
7843 This means that *all* articles that are marked as expirable will be
7844 deleted forever, right now."
7845   (interactive)
7846   (or gnus-expert-user
7847       (gnus-yes-or-no-p
7848        "Are you really, really, really sure you want to delete all these messages? ")
7849       (error "Phew!"))
7850   (gnus-summary-expire-articles t))
7851
7852 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7853 (defun gnus-summary-delete-article (&optional n)
7854   "Delete the N next (mail) articles.
7855 This command actually deletes articles.  This is not a marking
7856 command.  The article will disappear forever from your life, never to
7857 return.
7858 If N is negative, delete backwards.
7859 If N is nil and articles have been marked with the process mark,
7860 delete these instead."
7861   (interactive "P")
7862   (unless (gnus-check-backend-function 'request-expire-articles
7863                                        gnus-newsgroup-name)
7864     (error "The current newsgroup does not support article deletion"))
7865   ;; Compute the list of articles to delete.
7866   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7867         not-deleted)
7868     (if (and gnus-novice-user
7869              (not (gnus-yes-or-no-p
7870                    (format "Do you really want to delete %s forever? "
7871                            (if (> (length articles) 1)
7872                                (format "these %s articles" (length articles))
7873                              "this article")))))
7874         ()
7875       ;; Delete the articles.
7876       (setq not-deleted (gnus-request-expire-articles
7877                          articles gnus-newsgroup-name 'force))
7878       (while articles
7879         (gnus-summary-remove-process-mark (car articles))
7880         ;; The backend might not have been able to delete the article
7881         ;; after all.
7882         (unless (memq (car articles) not-deleted)
7883           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7884         (setq articles (cdr articles)))
7885       (when not-deleted
7886         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7887     (gnus-summary-position-point)
7888     (gnus-set-mode-line 'summary)
7889     not-deleted))
7890
7891 (defun gnus-summary-edit-article (&optional force)
7892   "Edit the current article.
7893 This will have permanent effect only in mail groups.
7894 If FORCE is non-nil, allow editing of articles even in read-only
7895 groups."
7896   (interactive "P")
7897   (save-excursion
7898     (set-buffer gnus-summary-buffer)
7899     (let ((mail-parse-charset gnus-newsgroup-charset)
7900           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7901       (gnus-set-global-variables)
7902       (when (and (not force)
7903                  (gnus-group-read-only-p))
7904         (error "The current newsgroup does not support article editing"))
7905       (gnus-summary-show-article t)
7906       (gnus-article-edit-article
7907        'ignore
7908        `(lambda (no-highlight)
7909           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7910                 (mail-parse-ignored-charsets
7911                  ',gnus-newsgroup-ignored-charsets))
7912             (gnus-summary-edit-article-done
7913              ,(or (mail-header-references gnus-current-headers) "")
7914              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7915
7916 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7917
7918 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7919                                                  no-highlight)
7920   "Make edits to the current article permanent."
7921   (interactive)
7922   ;; Replace the article.
7923   (let ((buf (current-buffer)))
7924     (with-temp-buffer
7925       (insert-buffer-substring buf)
7926       (if (and (not read-only)
7927                (not (gnus-request-replace-article
7928                      (cdr gnus-article-current) (car gnus-article-current)
7929                      (current-buffer) t)))
7930           (error "Couldn't replace article")
7931         ;; Update the summary buffer.
7932         (if (and references
7933                  (equal (message-tokenize-header references " ")
7934                         (message-tokenize-header
7935                          (or (message-fetch-field "references") "") " ")))
7936             ;; We only have to update this line.
7937             (save-excursion
7938               (save-restriction
7939                 (message-narrow-to-head)
7940                 (let ((head (buffer-string))
7941                       header)
7942                   (with-temp-buffer
7943                     (insert (format "211 %d Article retrieved.\n"
7944                                     (cdr gnus-article-current)))
7945                     (insert head)
7946                     (insert ".\n")
7947                     (let ((nntp-server-buffer (current-buffer)))
7948                       (setq header (car (gnus-get-newsgroup-headers
7949                                          (save-excursion
7950                                            (set-buffer gnus-summary-buffer)
7951                                            gnus-newsgroup-dependencies)
7952                                          t))))
7953                     (save-excursion
7954                       (set-buffer gnus-summary-buffer)
7955                       (gnus-data-set-header
7956                        (gnus-data-find (cdr gnus-article-current))
7957                        header)
7958                       (gnus-summary-update-article-line
7959                        (cdr gnus-article-current) header))))))
7960           ;; Update threads.
7961           (set-buffer (or buffer gnus-summary-buffer))
7962           (gnus-summary-update-article (cdr gnus-article-current)))
7963         ;; Prettify the article buffer again.
7964         (unless no-highlight
7965           (save-excursion
7966             (set-buffer gnus-article-buffer)
7967             ;;;!!! Fix this -- article should be rehighlighted.
7968             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7969             (set-buffer gnus-original-article-buffer)
7970             (gnus-request-article
7971              (cdr gnus-article-current)
7972              (car gnus-article-current) (current-buffer))))
7973         ;; Prettify the summary buffer line.
7974         (when (gnus-visual-p 'summary-highlight 'highlight)
7975           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7976
7977 (defun gnus-summary-edit-wash (key)
7978   "Perform editing command KEY in the article buffer."
7979   (interactive
7980    (list
7981     (progn
7982       (message "%s" (concat (this-command-keys) "- "))
7983       (read-char))))
7984   (message "")
7985   (gnus-summary-edit-article)
7986   (execute-kbd-macro (concat (this-command-keys) key))
7987   (gnus-article-edit-done))
7988
7989 ;;; Respooling
7990
7991 (defun gnus-summary-respool-query (&optional silent trace)
7992   "Query where the respool algorithm would put this article."
7993   (interactive)
7994   (let (gnus-mark-article-hook)
7995     (gnus-summary-select-article)
7996     (save-excursion
7997       (set-buffer gnus-original-article-buffer)
7998       (save-restriction
7999         (message-narrow-to-head)
8000         (let ((groups (nnmail-article-group 'identity trace)))
8001           (unless silent
8002             (if groups
8003                 (message "This message would go to %s"
8004                          (mapconcat 'car groups ", "))
8005               (message "This message would go to no groups"))
8006             groups))))))
8007
8008 (defun gnus-summary-respool-trace ()
8009   "Trace where the respool algorithm would put this article.
8010 Display a buffer showing all fancy splitting patterns which matched."
8011   (interactive)
8012   (gnus-summary-respool-query nil t))
8013
8014 ;; Summary marking commands.
8015
8016 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8017   "Mark articles which has the same subject as read, and then select the next.
8018 If UNMARK is positive, remove any kind of mark.
8019 If UNMARK is negative, tick articles."
8020   (interactive "P")
8021   (when unmark
8022     (setq unmark (prefix-numeric-value unmark)))
8023   (let ((count
8024          (gnus-summary-mark-same-subject
8025           (gnus-summary-article-subject) unmark)))
8026     ;; Select next unread article.  If auto-select-same mode, should
8027     ;; select the first unread article.
8028     (gnus-summary-next-article t (and gnus-auto-select-same
8029                                       (gnus-summary-article-subject)))
8030     (gnus-message 7 "%d article%s marked as %s"
8031                   count (if (= count 1) " is" "s are")
8032                   (if unmark "unread" "read"))))
8033
8034 (defun gnus-summary-kill-same-subject (&optional unmark)
8035   "Mark articles which has the same subject as read.
8036 If UNMARK is positive, remove any kind of mark.
8037 If UNMARK is negative, tick articles."
8038   (interactive "P")
8039   (when unmark
8040     (setq unmark (prefix-numeric-value unmark)))
8041   (let ((count
8042          (gnus-summary-mark-same-subject
8043           (gnus-summary-article-subject) unmark)))
8044     ;; If marked as read, go to next unread subject.
8045     (when (null unmark)
8046       ;; Go to next unread subject.
8047       (gnus-summary-next-subject 1 t))
8048     (gnus-message 7 "%d articles are marked as %s"
8049                   count (if unmark "unread" "read"))))
8050
8051 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8052   "Mark articles with same SUBJECT as read, and return marked number.
8053 If optional argument UNMARK is positive, remove any kinds of marks.
8054 If optional argument UNMARK is negative, mark articles as unread instead."
8055   (let ((count 1))
8056     (save-excursion
8057       (cond
8058        ((null unmark)                   ; Mark as read.
8059         (while (and
8060                 (progn
8061                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8062                   (gnus-summary-show-thread) t)
8063                 (gnus-summary-find-subject subject))
8064           (setq count (1+ count))))
8065        ((> unmark 0)                    ; Tick.
8066         (while (and
8067                 (progn
8068                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8069                   (gnus-summary-show-thread) t)
8070                 (gnus-summary-find-subject subject))
8071           (setq count (1+ count))))
8072        (t                               ; Mark as unread.
8073         (while (and
8074                 (progn
8075                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8076                   (gnus-summary-show-thread) t)
8077                 (gnus-summary-find-subject subject))
8078           (setq count (1+ count)))))
8079       (gnus-set-mode-line 'summary)
8080       ;; Return the number of marked articles.
8081       count)))
8082
8083 (defun gnus-summary-mark-as-processable (n &optional unmark)
8084   "Set the process mark on the next N articles.
8085 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8086 the process mark instead.  The difference between N and the actual
8087 number of articles marked is returned."
8088   (interactive "p")
8089   (let ((backward (< n 0))
8090         (n (abs n)))
8091     (while (and
8092             (> n 0)
8093             (if unmark
8094                 (gnus-summary-remove-process-mark
8095                  (gnus-summary-article-number))
8096               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8097             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8098       (setq n (1- n)))
8099     (when (/= 0 n)
8100       (gnus-message 7 "No more articles"))
8101     (gnus-summary-recenter)
8102     (gnus-summary-position-point)
8103     n))
8104
8105 (defun gnus-summary-unmark-as-processable (n)
8106   "Remove the process mark from the next N articles.
8107 If N is negative, unmark backward instead.  The difference between N and
8108 the actual number of articles unmarked is returned."
8109   (interactive "p")
8110   (gnus-summary-mark-as-processable n t))
8111
8112 (defun gnus-summary-unmark-all-processable ()
8113   "Remove the process mark from all articles."
8114   (interactive)
8115   (save-excursion
8116     (while gnus-newsgroup-processable
8117       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8118   (gnus-summary-position-point))
8119
8120 (defun gnus-summary-mark-as-expirable (n)
8121   "Mark N articles forward as expirable.
8122 If N is negative, mark backward instead.  The difference between N and
8123 the actual number of articles marked is returned."
8124   (interactive "p")
8125   (gnus-summary-mark-forward n gnus-expirable-mark))
8126
8127 (defun gnus-summary-mark-article-as-replied (article)
8128   "Mark ARTICLE replied and update the summary line."
8129   (push article gnus-newsgroup-replied)
8130   (let ((buffer-read-only nil))
8131     (when (gnus-summary-goto-subject article nil t)
8132       (gnus-summary-update-secondary-mark article))))
8133
8134 (defun gnus-summary-set-bookmark (article)
8135   "Set a bookmark in current article."
8136   (interactive (list (gnus-summary-article-number)))
8137   (when (or (not (get-buffer gnus-article-buffer))
8138             (not gnus-current-article)
8139             (not gnus-article-current)
8140             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8141     (error "No current article selected"))
8142   ;; Remove old bookmark, if one exists.
8143   (let ((old (assq article gnus-newsgroup-bookmarks)))
8144     (when old
8145       (setq gnus-newsgroup-bookmarks
8146             (delq old gnus-newsgroup-bookmarks))))
8147   ;; Set the new bookmark, which is on the form
8148   ;; (article-number . line-number-in-body).
8149   (push
8150    (cons article
8151          (save-excursion
8152            (set-buffer gnus-article-buffer)
8153            (count-lines
8154             (min (point)
8155                  (save-excursion
8156                    (goto-char (point-min))
8157                    (search-forward "\n\n" nil t)
8158                    (point)))
8159             (point))))
8160    gnus-newsgroup-bookmarks)
8161   (gnus-message 6 "A bookmark has been added to the current article."))
8162
8163 (defun gnus-summary-remove-bookmark (article)
8164   "Remove the bookmark from the current article."
8165   (interactive (list (gnus-summary-article-number)))
8166   ;; Remove old bookmark, if one exists.
8167   (let ((old (assq article gnus-newsgroup-bookmarks)))
8168     (if old
8169         (progn
8170           (setq gnus-newsgroup-bookmarks
8171                 (delq old gnus-newsgroup-bookmarks))
8172           (gnus-message 6 "Removed bookmark."))
8173       (gnus-message 6 "No bookmark in current article."))))
8174
8175 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8176 (defun gnus-summary-mark-as-dormant (n)
8177   "Mark N articles forward as dormant.
8178 If N is negative, mark backward instead.  The difference between N and
8179 the actual number of articles marked is returned."
8180   (interactive "p")
8181   (gnus-summary-mark-forward n gnus-dormant-mark))
8182
8183 (defun gnus-summary-set-process-mark (article)
8184   "Set the process mark on ARTICLE and update the summary line."
8185   (setq gnus-newsgroup-processable
8186         (cons article
8187               (delq article gnus-newsgroup-processable)))
8188   (when (gnus-summary-goto-subject article)
8189     (gnus-summary-show-thread)
8190     (gnus-summary-goto-subject article)
8191     (gnus-summary-update-secondary-mark article)))
8192
8193 (defun gnus-summary-remove-process-mark (article)
8194   "Remove the process mark from ARTICLE and update the summary line."
8195   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8196   (when (gnus-summary-goto-subject article)
8197     (gnus-summary-show-thread)
8198     (gnus-summary-goto-subject article)
8199     (gnus-summary-update-secondary-mark article)))
8200
8201 (defun gnus-summary-set-saved-mark (article)
8202   "Set the process mark on ARTICLE and update the summary line."
8203   (push article gnus-newsgroup-saved)
8204   (when (gnus-summary-goto-subject article)
8205     (gnus-summary-update-secondary-mark article)))
8206
8207 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8208   "Mark N articles as read forwards.
8209 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8210 The difference between N and the actual number of articles marked is
8211 returned.
8212 Iff NO-EXPIRE, auto-expiry will be inhibited."
8213   (interactive "p")
8214   (gnus-summary-show-thread)
8215   (let ((backward (< n 0))
8216         (gnus-summary-goto-unread
8217          (and gnus-summary-goto-unread
8218               (not (eq gnus-summary-goto-unread 'never))
8219               (not (memq mark (list gnus-unread-mark
8220                                     gnus-ticked-mark gnus-dormant-mark)))))
8221         (n (abs n))
8222         (mark (or mark gnus-del-mark)))
8223     (while (and (> n 0)
8224                 (gnus-summary-mark-article nil mark no-expire)
8225                 (zerop (gnus-summary-next-subject
8226                         (if backward -1 1)
8227                         (and gnus-summary-goto-unread
8228                              (not (eq gnus-summary-goto-unread 'never)))
8229                         t)))
8230       (setq n (1- n)))
8231     (when (/= 0 n)
8232       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8233     (gnus-summary-recenter)
8234     (gnus-summary-position-point)
8235     (gnus-set-mode-line 'summary)
8236     n))
8237
8238 (defun gnus-summary-mark-article-as-read (mark)
8239   "Mark the current article quickly as read with MARK."
8240   (let ((article (gnus-summary-article-number)))
8241     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8242     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8243     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8244     (push (cons article mark) gnus-newsgroup-reads)
8245     ;; Possibly remove from cache, if that is used.
8246     (when gnus-use-cache
8247       (gnus-cache-enter-remove-article article))
8248     ;; Allow the backend to change the mark.
8249     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8250     ;; Check for auto-expiry.
8251     (when (and gnus-newsgroup-auto-expire
8252                (memq mark gnus-auto-expirable-marks))
8253       (setq mark gnus-expirable-mark)
8254       ;; Let the backend know about the mark change.
8255       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8256       (push article gnus-newsgroup-expirable))
8257     ;; Set the mark in the buffer.
8258     (gnus-summary-update-mark mark 'unread)
8259     t))
8260
8261 (defun gnus-summary-mark-article-as-unread (mark)
8262   "Mark the current article quickly as unread with MARK."
8263   (let* ((article (gnus-summary-article-number))
8264          (old-mark (gnus-summary-article-mark article)))
8265     ;; Allow the backend to change the mark.
8266     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8267     (if (eq mark old-mark)
8268         t
8269       (if (<= article 0)
8270           (progn
8271             (gnus-error 1 "Can't mark negative article numbers")
8272             nil)
8273         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8274         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8275         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8276         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8277         (cond ((= mark gnus-ticked-mark)
8278                (push article gnus-newsgroup-marked))
8279               ((= mark gnus-dormant-mark)
8280                (push article gnus-newsgroup-dormant))
8281               (t
8282                (push article gnus-newsgroup-unreads)))
8283         (gnus-pull article gnus-newsgroup-reads)
8284
8285         ;; See whether the article is to be put in the cache.
8286         (and gnus-use-cache
8287              (vectorp (gnus-summary-article-header article))
8288              (save-excursion
8289                (gnus-cache-possibly-enter-article
8290                 gnus-newsgroup-name article
8291                 (gnus-summary-article-header article)
8292                 (= mark gnus-ticked-mark)
8293                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8294
8295         ;; Fix the mark.
8296         (gnus-summary-update-mark mark 'unread)
8297         t))))
8298
8299 (defun gnus-summary-mark-article (&optional article mark no-expire)
8300   "Mark ARTICLE with MARK.  MARK can be any character.
8301 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8302 `??' (dormant) and `?E' (expirable).
8303 If MARK is nil, then the default character `?r' is used.
8304 If ARTICLE is nil, then the article on the current line will be
8305 marked.
8306 Iff NO-EXPIRE, auto-expiry will be inhibited."
8307   ;; The mark might be a string.
8308   (when (stringp mark)
8309     (setq mark (aref mark 0)))
8310   ;; If no mark is given, then we check auto-expiring.
8311   (when (null mark)
8312     (setq mark gnus-del-mark))
8313   (when (and (not no-expire)
8314              gnus-newsgroup-auto-expire
8315              (memq mark gnus-auto-expirable-marks))
8316     (setq mark gnus-expirable-mark))
8317   (let ((article (or article (gnus-summary-article-number)))
8318         (old-mark (gnus-summary-article-mark article)))
8319     ;; Allow the backend to change the mark.
8320     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8321     (if (eq mark old-mark)
8322         t
8323       (unless article
8324         (error "No article on current line"))
8325       (if (not (if (or (= mark gnus-unread-mark)
8326                        (= mark gnus-ticked-mark)
8327                        (= mark gnus-dormant-mark))
8328                    (gnus-mark-article-as-unread article mark)
8329                  (gnus-mark-article-as-read article mark)))
8330           t
8331         ;; See whether the article is to be put in the cache.
8332         (and gnus-use-cache
8333              (not (= mark gnus-canceled-mark))
8334              (vectorp (gnus-summary-article-header article))
8335              (save-excursion
8336                (gnus-cache-possibly-enter-article
8337                 gnus-newsgroup-name article
8338                 (gnus-summary-article-header article)
8339                 (= mark gnus-ticked-mark)
8340                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8341
8342         (when (gnus-summary-goto-subject article nil t)
8343           (let ((buffer-read-only nil))
8344             (gnus-summary-show-thread)
8345             ;; Fix the mark.
8346             (gnus-summary-update-mark mark 'unread)
8347             t))))))
8348
8349 (defun gnus-summary-update-secondary-mark (article)
8350   "Update the secondary (read, process, cache) mark."
8351   (gnus-summary-update-mark
8352    (cond ((memq article gnus-newsgroup-processable)
8353           gnus-process-mark)
8354          ((memq article gnus-newsgroup-cached)
8355           gnus-cached-mark)
8356          ((memq article gnus-newsgroup-replied)
8357           gnus-replied-mark)
8358          ((memq article gnus-newsgroup-saved)
8359           gnus-saved-mark)
8360          (t gnus-unread-mark))
8361    'replied)
8362   (when (gnus-visual-p 'summary-highlight 'highlight)
8363     (gnus-run-hooks 'gnus-summary-update-hook))
8364   t)
8365
8366 (defun gnus-summary-update-mark (mark type)
8367   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8368         (buffer-read-only nil))
8369     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8370     (when forward
8371       (when (looking-at "\r")
8372         (incf forward))
8373       (when (<= (+ forward (point)) (point-max))
8374         ;; Go to the right position on the line.
8375         (goto-char (+ forward (point)))
8376         ;; Replace the old mark with the new mark.
8377         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8378         ;; Optionally update the marks by some user rule.
8379         (when (eq type 'unread)
8380           (gnus-data-set-mark
8381            (gnus-data-find (gnus-summary-article-number)) mark)
8382           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8383
8384 (defun gnus-mark-article-as-read (article &optional mark)
8385   "Enter ARTICLE in the pertinent lists and remove it from others."
8386   ;; Make the article expirable.
8387   (let ((mark (or mark gnus-del-mark)))
8388     (if (= mark gnus-expirable-mark)
8389         (push article gnus-newsgroup-expirable)
8390       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8391     ;; Remove from unread and marked lists.
8392     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8393     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8394     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8395     (push (cons article mark) gnus-newsgroup-reads)
8396     ;; Possibly remove from cache, if that is used.
8397     (when gnus-use-cache
8398       (gnus-cache-enter-remove-article article))
8399     t))
8400
8401 (defun gnus-mark-article-as-unread (article &optional mark)
8402   "Enter ARTICLE in the pertinent lists and remove it from others."
8403   (let ((mark (or mark gnus-ticked-mark)))
8404     (if (<= article 0)
8405         (progn
8406           (gnus-error 1 "Can't mark negative article numbers")
8407           nil)
8408       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8409             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8410             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8411             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8412
8413       ;; Unsuppress duplicates?
8414       (when gnus-suppress-duplicates
8415         (gnus-dup-unsuppress-article article))
8416
8417       (cond ((= mark gnus-ticked-mark)
8418              (push article gnus-newsgroup-marked))
8419             ((= mark gnus-dormant-mark)
8420              (push article gnus-newsgroup-dormant))
8421             (t
8422              (push article gnus-newsgroup-unreads)))
8423       (gnus-pull article gnus-newsgroup-reads)
8424       t)))
8425
8426 (defalias 'gnus-summary-mark-as-unread-forward
8427   'gnus-summary-tick-article-forward)
8428 (make-obsolete 'gnus-summary-mark-as-unread-forward
8429                'gnus-summary-tick-article-forward)
8430 (defun gnus-summary-tick-article-forward (n)
8431   "Tick N articles forwards.
8432 If N is negative, tick backwards instead.
8433 The difference between N and the number of articles ticked is returned."
8434   (interactive "p")
8435   (gnus-summary-mark-forward n gnus-ticked-mark))
8436
8437 (defalias 'gnus-summary-mark-as-unread-backward
8438   'gnus-summary-tick-article-backward)
8439 (make-obsolete 'gnus-summary-mark-as-unread-backward
8440                'gnus-summary-tick-article-backward)
8441 (defun gnus-summary-tick-article-backward (n)
8442   "Tick N articles backwards.
8443 The difference between N and the number of articles ticked is returned."
8444   (interactive "p")
8445   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8446
8447 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8448 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8449 (defun gnus-summary-tick-article (&optional article clear-mark)
8450   "Mark current article as unread.
8451 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8452 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8453   (interactive)
8454   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8455                                        gnus-ticked-mark)))
8456
8457 (defun gnus-summary-mark-as-read-forward (n)
8458   "Mark N articles as read forwards.
8459 If N is negative, mark backwards instead.
8460 The difference between N and the actual number of articles marked is
8461 returned."
8462   (interactive "p")
8463   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8464
8465 (defun gnus-summary-mark-as-read-backward (n)
8466   "Mark the N articles as read backwards.
8467 The difference between N and the actual number of articles marked is
8468 returned."
8469   (interactive "p")
8470   (gnus-summary-mark-forward
8471    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8472
8473 (defun gnus-summary-mark-as-read (&optional article mark)
8474   "Mark current article as read.
8475 ARTICLE specifies the article to be marked as read.
8476 MARK specifies a string to be inserted at the beginning of the line."
8477   (gnus-summary-mark-article article mark))
8478
8479 (defun gnus-summary-clear-mark-forward (n)
8480   "Clear marks from N articles forward.
8481 If N is negative, clear backward instead.
8482 The difference between N and the number of marks cleared is returned."
8483   (interactive "p")
8484   (gnus-summary-mark-forward n gnus-unread-mark))
8485
8486 (defun gnus-summary-clear-mark-backward (n)
8487   "Clear marks from N articles backward.
8488 The difference between N and the number of marks cleared is returned."
8489   (interactive "p")
8490   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8491
8492 (defun gnus-summary-mark-unread-as-read ()
8493   "Intended to be used by `gnus-summary-mark-article-hook'."
8494   (when (memq gnus-current-article gnus-newsgroup-unreads)
8495     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8496
8497 (defun gnus-summary-mark-read-and-unread-as-read ()
8498   "Intended to be used by `gnus-summary-mark-article-hook'."
8499   (let ((mark (gnus-summary-article-mark)))
8500     (when (or (gnus-unread-mark-p mark)
8501               (gnus-read-mark-p mark))
8502       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8503
8504 (defun gnus-summary-mark-region-as-read (point mark all)
8505   "Mark all unread articles between point and mark as read.
8506 If given a prefix, mark all articles between point and mark as read,
8507 even ticked and dormant ones."
8508   (interactive "r\nP")
8509   (save-excursion
8510     (let (article)
8511       (goto-char point)
8512       (beginning-of-line)
8513       (while (and
8514               (< (point) mark)
8515               (progn
8516                 (when (or all
8517                           (memq (setq article (gnus-summary-article-number))
8518                                 gnus-newsgroup-unreads))
8519                   (gnus-summary-mark-article article gnus-del-mark))
8520                 t)
8521               (gnus-summary-find-next))))))
8522
8523 (defun gnus-summary-mark-below (score mark)
8524   "Mark articles with score less than SCORE with MARK."
8525   (interactive "P\ncMark: ")
8526   (setq score (if score
8527                   (prefix-numeric-value score)
8528                 (or gnus-summary-default-score 0)))
8529   (save-excursion
8530     (set-buffer gnus-summary-buffer)
8531     (goto-char (point-min))
8532     (while
8533         (progn
8534           (and (< (gnus-summary-article-score) score)
8535                (gnus-summary-mark-article nil mark))
8536           (gnus-summary-find-next)))))
8537
8538 (defun gnus-summary-kill-below (&optional score)
8539   "Mark articles with score below SCORE as read."
8540   (interactive "P")
8541   (gnus-summary-mark-below score gnus-killed-mark))
8542
8543 (defun gnus-summary-clear-above (&optional score)
8544   "Clear all marks from articles with score above SCORE."
8545   (interactive "P")
8546   (gnus-summary-mark-above score gnus-unread-mark))
8547
8548 (defun gnus-summary-tick-above (&optional score)
8549   "Tick all articles with score above SCORE."
8550   (interactive "P")
8551   (gnus-summary-mark-above score gnus-ticked-mark))
8552
8553 (defun gnus-summary-mark-above (score mark)
8554   "Mark articles with score over SCORE with MARK."
8555   (interactive "P\ncMark: ")
8556   (setq score (if score
8557                   (prefix-numeric-value score)
8558                 (or gnus-summary-default-score 0)))
8559   (save-excursion
8560     (set-buffer gnus-summary-buffer)
8561     (goto-char (point-min))
8562     (while (and (progn
8563                   (when (> (gnus-summary-article-score) score)
8564                     (gnus-summary-mark-article nil mark))
8565                   t)
8566                 (gnus-summary-find-next)))))
8567
8568 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8569 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8570 (defun gnus-summary-limit-include-expunged (&optional no-error)
8571   "Display all the hidden articles that were expunged for low scores."
8572   (interactive)
8573   (let ((buffer-read-only nil))
8574     (let ((scored gnus-newsgroup-scored)
8575           headers h)
8576       (while scored
8577         (unless (gnus-summary-goto-subject (caar scored))
8578           (and (setq h (gnus-summary-article-header (caar scored)))
8579                (< (cdar scored) gnus-summary-expunge-below)
8580                (push h headers)))
8581         (setq scored (cdr scored)))
8582       (if (not headers)
8583           (when (not no-error)
8584             (error "No expunged articles hidden"))
8585         (goto-char (point-min))
8586         (gnus-summary-prepare-unthreaded (nreverse headers))
8587         (goto-char (point-min))
8588         (gnus-summary-position-point)
8589         t))))
8590
8591 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8592   "Mark all unread articles in this newsgroup as read.
8593 If prefix argument ALL is non-nil, ticked and dormant articles will
8594 also be marked as read.
8595 If QUIETLY is non-nil, no questions will be asked.
8596 If TO-HERE is non-nil, it should be a point in the buffer.  All
8597 articles before this point will be marked as read.
8598 Note that this function will only catch up the unread article
8599 in the current summary buffer limitation.
8600 The number of articles marked as read is returned."
8601   (interactive "P")
8602   (prog1
8603       (save-excursion
8604         (when (or quietly
8605                   (not gnus-interactive-catchup) ;Without confirmation?
8606                   gnus-expert-user
8607                   (gnus-y-or-n-p
8608                    (if all
8609                        "Mark absolutely all articles as read? "
8610                      "Mark all unread articles as read? ")))
8611           (if (and not-mark
8612                    (not gnus-newsgroup-adaptive)
8613                    (not gnus-newsgroup-auto-expire)
8614                    (not gnus-suppress-duplicates)
8615                    (or (not gnus-use-cache)
8616                        (eq gnus-use-cache 'passive)))
8617               (progn
8618                 (when all
8619                   (setq gnus-newsgroup-marked nil
8620                         gnus-newsgroup-dormant nil))
8621                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8622             ;; We actually mark all articles as canceled, which we
8623             ;; have to do when using auto-expiry or adaptive scoring.
8624             (gnus-summary-show-all-threads)
8625             (when (gnus-summary-first-subject (not all) t)
8626               (while (and
8627                       (if to-here (< (point) to-here) t)
8628                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8629                       (gnus-summary-find-next (not all) nil nil t))))
8630             (gnus-set-mode-line 'summary))
8631           t))
8632     (gnus-summary-position-point)))
8633
8634 (defun gnus-summary-catchup-to-here (&optional all)
8635   "Mark all unticked articles before the current one as read.
8636 If ALL is non-nil, also mark ticked and dormant articles as read."
8637   (interactive "P")
8638   (save-excursion
8639     (gnus-save-hidden-threads
8640       (let ((beg (point)))
8641         ;; We check that there are unread articles.
8642         (when (or all (gnus-summary-find-prev))
8643           (gnus-summary-catchup all t beg)))))
8644   (gnus-summary-position-point))
8645
8646 (defun gnus-summary-catchup-all (&optional quietly)
8647   "Mark all articles in this newsgroup as read."
8648   (interactive "P")
8649   (gnus-summary-catchup t quietly))
8650
8651 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8652   "Mark all unread articles in this group as read, then exit.
8653 If prefix argument ALL is non-nil, all articles are marked as read."
8654   (interactive "P")
8655   (when (gnus-summary-catchup all quietly nil 'fast)
8656     ;; Select next newsgroup or exit.
8657     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8658              (eq gnus-auto-select-next 'quietly))
8659         (gnus-summary-next-group nil)
8660       (gnus-summary-exit))))
8661
8662 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8663   "Mark all articles in this newsgroup as read, and then exit."
8664   (interactive "P")
8665   (gnus-summary-catchup-and-exit t quietly))
8666
8667 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8668   "Mark all articles in this group as read and select the next group.
8669 If given a prefix, mark all articles, unread as well as ticked, as
8670 read."
8671   (interactive "P")
8672   (save-excursion
8673     (gnus-summary-catchup all))
8674   (gnus-summary-next-group))
8675
8676 ;; Thread-based commands.
8677
8678 (defun gnus-summary-articles-in-thread (&optional article)
8679   "Return a list of all articles in the current thread.
8680 If ARTICLE is non-nil, return all articles in the thread that starts
8681 with that article."
8682   (let* ((article (or article (gnus-summary-article-number)))
8683          (data (gnus-data-find-list article))
8684          (top-level (gnus-data-level (car data)))
8685          (top-subject
8686           (cond ((null gnus-thread-operation-ignore-subject)
8687                  (gnus-simplify-subject-re
8688                   (mail-header-subject (gnus-data-header (car data)))))
8689                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8690                  (gnus-simplify-subject-fuzzy
8691                   (mail-header-subject (gnus-data-header (car data)))))
8692                 (t nil)))
8693          (end-point (save-excursion
8694                       (if (gnus-summary-go-to-next-thread)
8695                           (point) (point-max))))
8696          articles)
8697     (while (and data
8698                 (< (gnus-data-pos (car data)) end-point))
8699       (when (or (not top-subject)
8700                 (string= top-subject
8701                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8702                              (gnus-simplify-subject-fuzzy
8703                               (mail-header-subject
8704                                (gnus-data-header (car data))))
8705                            (gnus-simplify-subject-re
8706                             (mail-header-subject
8707                              (gnus-data-header (car data)))))))
8708         (push (gnus-data-number (car data)) articles))
8709       (unless (and (setq data (cdr data))
8710                    (> (gnus-data-level (car data)) top-level))
8711         (setq data nil)))
8712     ;; Return the list of articles.
8713     (nreverse articles)))
8714
8715 (defun gnus-summary-rethread-current ()
8716   "Rethread the thread the current article is part of."
8717   (interactive)
8718   (let* ((gnus-show-threads t)
8719          (article (gnus-summary-article-number))
8720          (id (mail-header-id (gnus-summary-article-header)))
8721          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8722     (unless id
8723       (error "No article on the current line"))
8724     (gnus-rebuild-thread id)
8725     (gnus-summary-goto-subject article)))
8726
8727 (defun gnus-summary-reparent-thread ()
8728   "Make the current article child of the marked (or previous) article.
8729
8730 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8731 is non-nil or the Subject: of both articles are the same."
8732   (interactive)
8733   (unless (not (gnus-group-read-only-p))
8734     (error "The current newsgroup does not support article editing"))
8735   (unless (<= (length gnus-newsgroup-processable) 1)
8736     (error "No more than one article may be marked"))
8737   (save-window-excursion
8738     (let ((gnus-article-buffer " *reparent*")
8739           (current-article (gnus-summary-article-number))
8740           ;; First grab the marked article, otherwise one line up.
8741           (parent-article (if (not (null gnus-newsgroup-processable))
8742                               (car gnus-newsgroup-processable)
8743                             (save-excursion
8744                               (if (eq (forward-line -1) 0)
8745                                   (gnus-summary-article-number)
8746                                 (error "Beginning of summary buffer"))))))
8747       (unless (not (eq current-article parent-article))
8748         (error "An article may not be self-referential"))
8749       (let ((message-id (mail-header-id
8750                          (gnus-summary-article-header parent-article))))
8751         (unless (and message-id (not (equal message-id "")))
8752           (error "No message-id in desired parent"))
8753         (gnus-with-article current-article
8754           (goto-char (point-min))
8755           (if (re-search-forward "^References: " nil t)
8756               (progn
8757                 (re-search-forward "^[^ \t]" nil t)
8758                 (forward-line -1)
8759                 (end-of-line)
8760                 (insert " " message-id))
8761             (insert "References: " message-id "\n")))
8762         (set-buffer gnus-summary-buffer)
8763         (gnus-summary-unmark-all-processable)
8764         (gnus-summary-update-article current-article)
8765         (gnus-summary-rethread-current)
8766         (gnus-message 3 "Article %d is now the child of article %d"
8767                       current-article parent-article)))))
8768
8769 (defun gnus-summary-toggle-threads (&optional arg)
8770   "Toggle showing conversation threads.
8771 If ARG is positive number, turn showing conversation threads on."
8772   (interactive "P")
8773   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8774     (setq gnus-show-threads
8775           (if (null arg) (not gnus-show-threads)
8776             (> (prefix-numeric-value arg) 0)))
8777     (gnus-summary-prepare)
8778     (gnus-summary-goto-subject current)
8779     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8780     (gnus-summary-position-point)))
8781
8782 (defun gnus-summary-show-all-threads ()
8783   "Show all threads."
8784   (interactive)
8785   (save-excursion
8786     (let ((buffer-read-only nil))
8787       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8788   (gnus-summary-position-point))
8789
8790 (defun gnus-summary-show-thread ()
8791   "Show thread subtrees.
8792 Returns nil if no thread was there to be shown."
8793   (interactive)
8794   (let ((buffer-read-only nil)
8795         (orig (point))
8796         ;; first goto end then to beg, to have point at beg after let
8797         (end (progn (end-of-line) (point)))
8798         (beg (progn (beginning-of-line) (point))))
8799     (prog1
8800         ;; Any hidden lines here?
8801         (search-forward "\r" end t)
8802       (subst-char-in-region beg end ?\^M ?\n t)
8803       (goto-char orig)
8804       (gnus-summary-position-point))))
8805
8806 (defun gnus-summary-hide-all-threads ()
8807   "Hide all thread subtrees."
8808   (interactive)
8809   (save-excursion
8810     (goto-char (point-min))
8811     (gnus-summary-hide-thread)
8812     (while (zerop (gnus-summary-next-thread 1 t))
8813       (gnus-summary-hide-thread)))
8814   (gnus-summary-position-point))
8815
8816 (defun gnus-summary-hide-thread ()
8817   "Hide thread subtrees.
8818 Returns nil if no threads were there to be hidden."
8819   (interactive)
8820   (let ((buffer-read-only nil)
8821         (start (point))
8822         (article (gnus-summary-article-number)))
8823     (goto-char start)
8824     ;; Go forward until either the buffer ends or the subthread
8825     ;; ends.
8826     (when (and (not (eobp))
8827                (or (zerop (gnus-summary-next-thread 1 t))
8828                    (goto-char (point-max))))
8829       (prog1
8830           (if (and (> (point) start)
8831                    (search-backward "\n" start t))
8832               (progn
8833                 (subst-char-in-region start (point) ?\n ?\^M)
8834                 (gnus-summary-goto-subject article))
8835             (goto-char start)
8836             nil)))))
8837
8838 (defun gnus-summary-go-to-next-thread (&optional previous)
8839   "Go to the same level (or less) next thread.
8840 If PREVIOUS is non-nil, go to previous thread instead.
8841 Return the article number moved to, or nil if moving was impossible."
8842   (let ((level (gnus-summary-thread-level))
8843         (way (if previous -1 1))
8844         (beg (point)))
8845     (forward-line way)
8846     (while (and (not (eobp))
8847                 (< level (gnus-summary-thread-level)))
8848       (forward-line way))
8849     (if (eobp)
8850         (progn
8851           (goto-char beg)
8852           nil)
8853       (setq beg (point))
8854       (prog1
8855           (gnus-summary-article-number)
8856         (goto-char beg)))))
8857
8858 (defun gnus-summary-next-thread (n &optional silent)
8859   "Go to the same level next N'th thread.
8860 If N is negative, search backward instead.
8861 Returns the difference between N and the number of skips actually
8862 done.
8863
8864 If SILENT, don't output messages."
8865   (interactive "p")
8866   (let ((backward (< n 0))
8867         (n (abs n)))
8868     (while (and (> n 0)
8869                 (gnus-summary-go-to-next-thread backward))
8870       (decf n))
8871     (unless silent
8872       (gnus-summary-position-point))
8873     (when (and (not silent) (/= 0 n))
8874       (gnus-message 7 "No more threads"))
8875     n))
8876
8877 (defun gnus-summary-prev-thread (n)
8878   "Go to the same level previous N'th thread.
8879 Returns the difference between N and the number of skips actually
8880 done."
8881   (interactive "p")
8882   (gnus-summary-next-thread (- n)))
8883
8884 (defun gnus-summary-go-down-thread ()
8885   "Go down one level in the current thread."
8886   (let ((children (gnus-summary-article-children)))
8887     (when children
8888       (gnus-summary-goto-subject (car children)))))
8889
8890 (defun gnus-summary-go-up-thread ()
8891   "Go up one level in the current thread."
8892   (let ((parent (gnus-summary-article-parent)))
8893     (when parent
8894       (gnus-summary-goto-subject parent))))
8895
8896 (defun gnus-summary-down-thread (n)
8897   "Go down thread N steps.
8898 If N is negative, go up instead.
8899 Returns the difference between N and how many steps down that were
8900 taken."
8901   (interactive "p")
8902   (let ((up (< n 0))
8903         (n (abs n)))
8904     (while (and (> n 0)
8905                 (if up (gnus-summary-go-up-thread)
8906                   (gnus-summary-go-down-thread)))
8907       (setq n (1- n)))
8908     (gnus-summary-position-point)
8909     (when (/= 0 n)
8910       (gnus-message 7 "Can't go further"))
8911     n))
8912
8913 (defun gnus-summary-up-thread (n)
8914   "Go up thread N steps.
8915 If N is negative, go up instead.
8916 Returns the difference between N and how many steps down that were
8917 taken."
8918   (interactive "p")
8919   (gnus-summary-down-thread (- n)))
8920
8921 (defun gnus-summary-top-thread ()
8922   "Go to the top of the thread."
8923   (interactive)
8924   (while (gnus-summary-go-up-thread))
8925   (gnus-summary-article-number))
8926
8927 (defun gnus-summary-kill-thread (&optional unmark)
8928   "Mark articles under current thread as read.
8929 If the prefix argument is positive, remove any kinds of marks.
8930 If the prefix argument is negative, tick articles instead."
8931   (interactive "P")
8932   (when unmark
8933     (setq unmark (prefix-numeric-value unmark)))
8934   (let ((articles (gnus-summary-articles-in-thread)))
8935     (save-excursion
8936       ;; Expand the thread.
8937       (gnus-summary-show-thread)
8938       ;; Mark all the articles.
8939       (while articles
8940         (gnus-summary-goto-subject (car articles))
8941         (cond ((null unmark)
8942                (gnus-summary-mark-article-as-read gnus-killed-mark))
8943               ((> unmark 0)
8944                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8945               (t
8946                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8947         (setq articles (cdr articles))))
8948     ;; Hide killed subtrees.
8949     (and (null unmark)
8950          gnus-thread-hide-killed
8951          (gnus-summary-hide-thread))
8952     ;; If marked as read, go to next unread subject.
8953     (when (null unmark)
8954       ;; Go to next unread subject.
8955       (gnus-summary-next-subject 1 t)))
8956   (gnus-set-mode-line 'summary))
8957
8958 ;; Summary sorting commands
8959
8960 (defun gnus-summary-sort-by-number (&optional reverse)
8961   "Sort the summary buffer by article number.
8962 Argument REVERSE means reverse order."
8963   (interactive "P")
8964   (gnus-summary-sort 'number reverse))
8965
8966 (defun gnus-summary-sort-by-author (&optional reverse)
8967   "Sort the summary buffer by author name alphabetically.
8968 If case-fold-search is non-nil, case of letters is ignored.
8969 Argument REVERSE means reverse order."
8970   (interactive "P")
8971   (gnus-summary-sort 'author reverse))
8972
8973 (defun gnus-summary-sort-by-subject (&optional reverse)
8974   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8975 If case-fold-search is non-nil, case of letters is ignored.
8976 Argument REVERSE means reverse order."
8977   (interactive "P")
8978   (gnus-summary-sort 'subject reverse))
8979
8980 (defun gnus-summary-sort-by-date (&optional reverse)
8981   "Sort the summary buffer by date.
8982 Argument REVERSE means reverse order."
8983   (interactive "P")
8984   (gnus-summary-sort 'date reverse))
8985
8986 (defun gnus-summary-sort-by-score (&optional reverse)
8987   "Sort the summary buffer by score.
8988 Argument REVERSE means reverse order."
8989   (interactive "P")
8990   (gnus-summary-sort 'score reverse))
8991
8992 (defun gnus-summary-sort-by-lines (&optional reverse)
8993   "Sort the summary buffer by the number of lines.
8994 Argument REVERSE means reverse order."
8995   (interactive "P")
8996   (gnus-summary-sort 'lines reverse))
8997
8998 (defun gnus-summary-sort-by-chars (&optional reverse)
8999   "Sort the summary buffer by article length.
9000 Argument REVERSE means reverse order."
9001   (interactive "P")
9002   (gnus-summary-sort 'chars reverse))   
9003
9004 (defun gnus-summary-sort (predicate reverse)
9005   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9006   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9007          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9008          (gnus-thread-sort-functions
9009           (if (not reverse)
9010               thread
9011             `(lambda (t1 t2)
9012                (,thread t2 t1))))
9013          (gnus-article-sort-functions
9014           (if (not reverse)
9015               article
9016             `(lambda (t1 t2)
9017                (,article t2 t1))))
9018          (buffer-read-only)
9019          (gnus-summary-prepare-hook nil))
9020     ;; We do the sorting by regenerating the threads.
9021     (gnus-summary-prepare)
9022     ;; Hide subthreads if needed.
9023     (when (and gnus-show-threads gnus-thread-hide-subtree)
9024       (gnus-summary-hide-all-threads))))
9025
9026 ;; Summary saving commands.
9027
9028 (defun gnus-summary-save-article (&optional n not-saved)
9029   "Save the current article using the default saver function.
9030 If N is a positive number, save the N next articles.
9031 If N is a negative number, save the N previous articles.
9032 If N is nil and any articles have been marked with the process mark,
9033 save those articles instead.
9034 The variable `gnus-default-article-saver' specifies the saver function."
9035   (interactive "P")
9036   (let* ((articles (gnus-summary-work-articles n))
9037          (save-buffer (save-excursion
9038                         (nnheader-set-temp-buffer " *Gnus Save*")))
9039          (num (length articles))
9040          header file)
9041     (dolist (article articles)
9042       (setq header (gnus-summary-article-header article))
9043       (if (not (vectorp header))
9044           ;; This is a pseudo-article.
9045           (if (assq 'name header)
9046               (gnus-copy-file (cdr (assq 'name header)))
9047             (gnus-message 1 "Article %d is unsaveable" article))
9048         ;; This is a real article.
9049         (save-window-excursion
9050           (gnus-summary-select-article t nil nil article))
9051         (save-excursion
9052           (set-buffer save-buffer)
9053           (erase-buffer)
9054           (insert-buffer-substring gnus-original-article-buffer))
9055         (setq file (gnus-article-save save-buffer file num))
9056         (gnus-summary-remove-process-mark article)
9057         (unless not-saved
9058           (gnus-summary-set-saved-mark article))))
9059     (gnus-kill-buffer save-buffer)
9060     (gnus-summary-position-point)
9061     (gnus-set-mode-line 'summary)
9062     n))
9063
9064 (defun gnus-summary-pipe-output (&optional arg)
9065   "Pipe the current article to a subprocess.
9066 If N is a positive number, pipe the N next articles.
9067 If N is a negative number, pipe the N previous articles.
9068 If N is nil and any articles have been marked with the process mark,
9069 pipe those articles instead."
9070   (interactive "P")
9071   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9072     (gnus-summary-save-article arg t))
9073   (gnus-configure-windows 'pipe))
9074
9075 (defun gnus-summary-save-article-mail (&optional arg)
9076   "Append the current article to an mail file.
9077 If N is a positive number, save the N next articles.
9078 If N is a negative number, save the N previous articles.
9079 If N is nil and any articles have been marked with the process mark,
9080 save those articles instead."
9081   (interactive "P")
9082   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9083     (gnus-summary-save-article arg)))
9084
9085 (defun gnus-summary-save-article-rmail (&optional arg)
9086   "Append the current article to an rmail file.
9087 If N is a positive number, save the N next articles.
9088 If N is a negative number, save the N previous articles.
9089 If N is nil and any articles have been marked with the process mark,
9090 save those articles instead."
9091   (interactive "P")
9092   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9093     (gnus-summary-save-article arg)))
9094
9095 (defun gnus-summary-save-article-file (&optional arg)
9096   "Append the current article to a file.
9097 If N is a positive number, save the N next articles.
9098 If N is a negative number, save the N previous articles.
9099 If N is nil and any articles have been marked with the process mark,
9100 save those articles instead."
9101   (interactive "P")
9102   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9103     (gnus-summary-save-article arg)))
9104
9105 (defun gnus-summary-write-article-file (&optional arg)
9106   "Write the current article to a file, deleting the previous file.
9107 If N is a positive number, save the N next articles.
9108 If N is a negative number, save the N previous articles.
9109 If N is nil and any articles have been marked with the process mark,
9110 save those articles instead."
9111   (interactive "P")
9112   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9113     (gnus-summary-save-article arg)))
9114
9115 (defun gnus-summary-save-article-body-file (&optional arg)
9116   "Append the current article body to a file.
9117 If N is a positive number, save the N next articles.
9118 If N is a negative number, save the N previous articles.
9119 If N is nil and any articles have been marked with the process mark,
9120 save those articles instead."
9121   (interactive "P")
9122   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9123     (gnus-summary-save-article arg)))
9124
9125 (defun gnus-summary-pipe-message (program)
9126   "Pipe the current article through PROGRAM."
9127   (interactive "sProgram: ")
9128   (gnus-summary-select-article)
9129   (let ((mail-header-separator ""))
9130     (gnus-eval-in-buffer-window gnus-article-buffer
9131       (save-restriction
9132         (widen)
9133         (let ((start (window-start))
9134               buffer-read-only)
9135           (message-pipe-buffer-body program)
9136           (set-window-start (get-buffer-window (current-buffer)) start))))))
9137
9138 (defun gnus-get-split-value (methods)
9139   "Return a value based on the split METHODS."
9140   (let (split-name method result match)
9141     (when methods
9142       (save-excursion
9143         (set-buffer gnus-original-article-buffer)
9144         (save-restriction
9145           (nnheader-narrow-to-headers)
9146           (while methods
9147             (goto-char (point-min))
9148             (setq method (pop methods))
9149             (setq match (car method))
9150             (when (cond
9151                    ((stringp match)
9152                     ;; Regular expression.
9153                     (ignore-errors
9154                       (re-search-forward match nil t)))
9155                    ((gnus-functionp match)
9156                     ;; Function.
9157                     (save-restriction
9158                       (widen)
9159                       (setq result (funcall match gnus-newsgroup-name))))
9160                    ((consp match)
9161                     ;; Form.
9162                     (save-restriction
9163                       (widen)
9164                       (setq result (eval match)))))
9165               (setq split-name (append (cdr method) split-name))
9166               (cond ((stringp result)
9167                      (push (expand-file-name
9168                             result gnus-article-save-directory)
9169                            split-name))
9170                     ((consp result)
9171                      (setq split-name (append result split-name)))))))))
9172     (nreverse split-name)))
9173
9174 (defun gnus-valid-move-group-p (group)
9175   (and (boundp group)
9176        (symbol-name group)
9177        (symbol-value group)
9178        (gnus-get-function (gnus-find-method-for-group
9179                            (symbol-name group)) 'request-accept-article t)))
9180
9181 (defun gnus-read-move-group-name (prompt default articles prefix)
9182   "Read a group name."
9183   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9184          (minibuffer-confirm-incomplete nil) ; XEmacs
9185          (prom
9186           (format "%s %s to:"
9187                   prompt
9188                   (if (> (length articles) 1)
9189                       (format "these %d articles" (length articles))
9190                     "this article")))
9191          (to-newsgroup
9192           (cond
9193            ((null split-name)
9194             (gnus-completing-read default prom
9195                                   gnus-active-hashtb
9196                                   'gnus-valid-move-group-p
9197                                   nil prefix
9198                                   'gnus-group-history))
9199            ((= 1 (length split-name))
9200             (gnus-completing-read (car split-name) prom
9201                                   gnus-active-hashtb
9202                                   'gnus-valid-move-group-p
9203                                   nil nil
9204                                   'gnus-group-history))
9205            (t
9206             (gnus-completing-read nil prom
9207                                   (mapcar (lambda (el) (list el))
9208                                           (nreverse split-name))
9209                                   nil nil nil
9210                                   'gnus-group-history)))))
9211     (when to-newsgroup
9212       (if (or (string= to-newsgroup "")
9213               (string= to-newsgroup prefix))
9214           (setq to-newsgroup default))
9215       (unless to-newsgroup
9216         (error "No group name entered"))
9217       (or (gnus-active to-newsgroup)
9218           (gnus-activate-group to-newsgroup)
9219           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9220                                      to-newsgroup))
9221               (or (and (gnus-request-create-group
9222                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9223                        (gnus-activate-group
9224                         to-newsgroup nil nil
9225                         (gnus-group-name-to-method to-newsgroup))
9226                        (gnus-subscribe-group to-newsgroup))
9227                   (error "Couldn't create group %s" to-newsgroup)))
9228           (error "No such group: %s" to-newsgroup)))
9229     to-newsgroup))
9230
9231 (defun gnus-summary-save-parts (type dir n reverse)
9232   "Save parts matching TYPE to DIR.
9233 If REVERSE, save parts that do not match TYPE."
9234   (interactive
9235    (list (read-string "Save parts of type: " "image/.*")
9236          (read-file-name "Save to directory: " t nil t)
9237          current-prefix-arg))
9238   (gnus-summary-iterate n
9239     (let ((gnus-display-mime-function nil)
9240           (gnus-inhibit-treatment t))
9241       (gnus-summary-select-article))
9242     (save-excursion
9243       (set-buffer gnus-article-buffer)
9244       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9245         (when handles
9246           (gnus-summary-save-parts-1 type dir handles reverse)
9247           (mm-destroy-parts handles))))))
9248
9249 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9250   (if (stringp (car handle))
9251       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9252               (cdr handle))
9253     (when (if reverse
9254               (not (string-match type (mm-handle-media-type handle)))
9255             (string-match type (mm-handle-media-type handle)))
9256       (let ((file (expand-file-name
9257                    (file-name-nondirectory
9258                     (or
9259                      (mail-content-type-get
9260                       (mm-handle-disposition handle) 'filename)
9261                      (concat gnus-newsgroup-name "." gnus-current-article)))
9262                    dir)))
9263         (unless (file-exists-p file)
9264           (mm-save-part-to-file handle file))))))
9265
9266 ;; Summary extract commands
9267
9268 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9269   (let ((buffer-read-only nil)
9270         (article (gnus-summary-article-number))
9271         after-article b e)
9272     (unless (gnus-summary-goto-subject article)
9273       (error "No such article: %d" article))
9274     (gnus-summary-position-point)
9275     ;; If all commands are to be bunched up on one line, we collect
9276     ;; them here.
9277     (unless gnus-view-pseudos-separately
9278       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9279             files action)
9280         (while ps
9281           (setq action (cdr (assq 'action (car ps))))
9282           (setq files (list (cdr (assq 'name (car ps)))))
9283           (while (and ps (cdr ps)
9284                       (string= (or action "1")
9285                                (or (cdr (assq 'action (cadr ps))) "2")))
9286             (push (cdr (assq 'name (cadr ps))) files)
9287             (setcdr ps (cddr ps)))
9288           (when files
9289             (when (not (string-match "%s" action))
9290               (push " " files))
9291             (push " " files)
9292             (when (assq 'execute (car ps))
9293               (setcdr (assq 'execute (car ps))
9294                       (funcall (if (string-match "%s" action)
9295                                    'format 'concat)
9296                                action
9297                                (mapconcat
9298                                 (lambda (f)
9299                                   (if (equal f " ")
9300                                       f
9301                                     (gnus-quote-arg-for-sh-or-csh f)))
9302                                 files " ")))))
9303           (setq ps (cdr ps)))))
9304     (if (and gnus-view-pseudos (not not-view))
9305         (while pslist
9306           (when (assq 'execute (car pslist))
9307             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9308                                   (eq gnus-view-pseudos 'not-confirm)))
9309           (setq pslist (cdr pslist)))
9310       (save-excursion
9311         (while pslist
9312           (setq after-article (or (cdr (assq 'article (car pslist)))
9313                                   (gnus-summary-article-number)))
9314           (gnus-summary-goto-subject after-article)
9315           (forward-line 1)
9316           (setq b (point))
9317           (insert "    " (file-name-nondirectory
9318                           (cdr (assq 'name (car pslist))))
9319                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9320           (setq e (point))
9321           (forward-line -1)             ; back to `b'
9322           (gnus-add-text-properties
9323            b (1- e) (list 'gnus-number gnus-reffed-article-number
9324                           gnus-mouse-face-prop gnus-mouse-face))
9325           (gnus-data-enter
9326            after-article gnus-reffed-article-number
9327            gnus-unread-mark b (car pslist) 0 (- e b))
9328           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9329           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9330           (setq pslist (cdr pslist)))))))
9331
9332 (defun gnus-pseudos< (p1 p2)
9333   (let ((c1 (cdr (assq 'action p1)))
9334         (c2 (cdr (assq 'action p2))))
9335     (and c1 c2 (string< c1 c2))))
9336
9337 (defun gnus-request-pseudo-article (props)
9338   (cond ((assq 'execute props)
9339          (gnus-execute-command (cdr (assq 'execute props)))))
9340   (let ((gnus-current-article (gnus-summary-article-number)))
9341     (gnus-run-hooks 'gnus-mark-article-hook)))
9342
9343 (defun gnus-execute-command (command &optional automatic)
9344   (save-excursion
9345     (gnus-article-setup-buffer)
9346     (set-buffer gnus-article-buffer)
9347     (setq buffer-read-only nil)
9348     (let ((command (if automatic command
9349                      (read-string "Command: " (cons command 0)))))
9350       (erase-buffer)
9351       (insert "$ " command "\n\n")
9352       (if gnus-view-pseudo-asynchronously
9353           (start-process "gnus-execute" (current-buffer) shell-file-name
9354                          shell-command-switch command)
9355         (call-process shell-file-name nil t nil
9356                       shell-command-switch command)))))
9357
9358 ;; Summary kill commands.
9359
9360 (defun gnus-summary-edit-global-kill (article)
9361   "Edit the \"global\" kill file."
9362   (interactive (list (gnus-summary-article-number)))
9363   (gnus-group-edit-global-kill article))
9364
9365 (defun gnus-summary-edit-local-kill ()
9366   "Edit a local kill file applied to the current newsgroup."
9367   (interactive)
9368   (setq gnus-current-headers (gnus-summary-article-header))
9369   (gnus-group-edit-local-kill
9370    (gnus-summary-article-number) gnus-newsgroup-name))
9371
9372 ;;; Header reading.
9373
9374 (defun gnus-read-header (id &optional header)
9375   "Read the headers of article ID and enter them into the Gnus system."
9376   (let ((group gnus-newsgroup-name)
9377         (gnus-override-method
9378          (or
9379           gnus-override-method
9380           (and (gnus-news-group-p gnus-newsgroup-name)
9381                (car (gnus-refer-article-methods)))))
9382         where)
9383     ;; First we check to see whether the header in question is already
9384     ;; fetched.
9385     (if (stringp id)
9386         ;; This is a Message-ID.
9387         (setq header (or header (gnus-id-to-header id)))
9388       ;; This is an article number.
9389       (setq header (or header (gnus-summary-article-header id))))
9390     (if (and header
9391              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9392         ;; We have found the header.
9393         header
9394       ;; If this is a sparse article, we have to nix out its
9395       ;; previous entry in the thread hashtb.
9396       (when (and header
9397                  (gnus-summary-article-sparse-p (mail-header-number header)))
9398         (let* ((parent (gnus-parent-id (mail-header-references header)))
9399                (thread (and parent (gnus-id-to-thread parent))))
9400           (when thread
9401             (delq (assq header thread) thread))))
9402       ;; We have to really fetch the header to this article.
9403       (save-excursion
9404         (set-buffer nntp-server-buffer)
9405         (when (setq where (gnus-request-head id group))
9406           (nnheader-fold-continuation-lines)
9407           (goto-char (point-max))
9408           (insert ".\n")
9409           (goto-char (point-min))
9410           (insert "211 ")
9411           (princ (cond
9412                   ((numberp id) id)
9413                   ((cdr where) (cdr where))
9414                   (header (mail-header-number header))
9415                   (t gnus-reffed-article-number))
9416                  (current-buffer))
9417           (insert " Article retrieved.\n"))
9418         (if (or (not where)
9419                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9420             ()                          ; Malformed head.
9421           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9422             (when (and (stringp id)
9423                        (not (string= (gnus-group-real-name group)
9424                                      (car where))))
9425               ;; If we fetched by Message-ID and the article came
9426               ;; from a different group, we fudge some bogus article
9427               ;; numbers for this article.
9428               (mail-header-set-number header gnus-reffed-article-number))
9429             (save-excursion
9430               (set-buffer gnus-summary-buffer)
9431               (decf gnus-reffed-article-number)
9432               (gnus-remove-header (mail-header-number header))
9433               (push header gnus-newsgroup-headers)
9434               (setq gnus-current-headers header)
9435               (push (mail-header-number header) gnus-newsgroup-limit)))
9436           header)))))
9437
9438 (defun gnus-remove-header (number)
9439   "Remove header NUMBER from `gnus-newsgroup-headers'."
9440   (if (and gnus-newsgroup-headers
9441            (= number (mail-header-number (car gnus-newsgroup-headers))))
9442       (pop gnus-newsgroup-headers)
9443     (let ((headers gnus-newsgroup-headers))
9444       (while (and (cdr headers)
9445                   (not (= number (mail-header-number (cadr headers)))))
9446         (pop headers))
9447       (when (cdr headers)
9448         (setcdr headers (cddr headers))))))
9449
9450 ;;;
9451 ;;; summary highlights
9452 ;;;
9453
9454 (defun gnus-highlight-selected-summary ()
9455   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9456   ;; Highlight selected article in summary buffer
9457   (when gnus-summary-selected-face
9458     (save-excursion
9459       (let* ((beg (progn (beginning-of-line) (point)))
9460              (end (progn (end-of-line) (point)))
9461              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9462              (from (if (get-text-property beg gnus-mouse-face-prop)
9463                        beg
9464                      (or (next-single-property-change
9465                           beg gnus-mouse-face-prop nil end)
9466                          beg)))
9467              (to
9468               (if (= from end)
9469                   (- from 2)
9470                 (or (next-single-property-change
9471                      from gnus-mouse-face-prop nil end)
9472                     end))))
9473         ;; If no mouse-face prop on line we will have to = from = end,
9474         ;; so we highlight the entire line instead.
9475         (when (= (+ to 2) from)
9476           (setq from beg)
9477           (setq to end))
9478         (if gnus-newsgroup-selected-overlay
9479             ;; Move old overlay.
9480             (gnus-move-overlay
9481              gnus-newsgroup-selected-overlay from to (current-buffer))
9482           ;; Create new overlay.
9483           (gnus-overlay-put
9484            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9485            'face gnus-summary-selected-face))))))
9486
9487 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9488 (defun gnus-summary-highlight-line ()
9489   "Highlight current line according to `gnus-summary-highlight'."
9490   (let* ((list gnus-summary-highlight)
9491          (p (point))
9492          (end (progn (end-of-line) (point)))
9493          ;; now find out where the line starts and leave point there.
9494          (beg (progn (beginning-of-line) (point)))
9495          (article (gnus-summary-article-number))
9496          (score (or (cdr (assq (or article gnus-current-article)
9497                                gnus-newsgroup-scored))
9498                     gnus-summary-default-score 0))
9499          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9500          (inhibit-read-only t))
9501     ;; Eval the cars of the lists until we find a match.
9502     (let ((default gnus-summary-default-score))
9503       (while (and list
9504                   (not (eval (caar list))))
9505         (setq list (cdr list))))
9506     (let ((face (cdar list)))
9507       (unless (eq face (get-text-property beg 'face))
9508         (gnus-put-text-property-excluding-characters-with-faces
9509          beg end 'face
9510          (setq face (if (boundp face) (symbol-value face) face)))
9511         (when gnus-summary-highlight-line-function
9512           (funcall gnus-summary-highlight-line-function article face))))
9513     (goto-char p)))
9514
9515 (defun gnus-update-read-articles (group unread &optional compute)
9516   "Update the list of read articles in GROUP."
9517   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9518          (entry (gnus-gethash group gnus-newsrc-hashtb))
9519          (info (nth 2 entry))
9520          (prev 1)
9521          (unread (sort (copy-sequence unread) '<))
9522          read)
9523     (if (or (not info) (not active))
9524         ;; There is no info on this group if it was, in fact,
9525         ;; killed.  Gnus stores no information on killed groups, so
9526         ;; there's nothing to be done.
9527         ;; One could store the information somewhere temporarily,
9528         ;; perhaps...  Hmmm...
9529         ()
9530       ;; Remove any negative articles numbers.
9531       (while (and unread (< (car unread) 0))
9532         (setq unread (cdr unread)))
9533       ;; Remove any expired article numbers
9534       (while (and unread (< (car unread) (car active)))
9535         (setq unread (cdr unread)))
9536       ;; Compute the ranges of read articles by looking at the list of
9537       ;; unread articles.
9538       (while unread
9539         (when (/= (car unread) prev)
9540           (push (if (= prev (1- (car unread))) prev
9541                   (cons prev (1- (car unread))))
9542                 read))
9543         (setq prev (1+ (car unread)))
9544         (setq unread (cdr unread)))
9545       (when (<= prev (cdr active))
9546         (push (cons prev (cdr active)) read))
9547       (setq read (if (> (length read) 1) (nreverse read) read))
9548       (if compute
9549           read
9550         (save-excursion
9551           (set-buffer gnus-group-buffer)
9552           (gnus-undo-register
9553             `(progn
9554                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9555                (gnus-info-set-read ',info ',(gnus-info-read info))
9556                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9557                (gnus-group-update-group ,group t))))
9558         ;; Propagate the read marks to the backend.
9559         (if (gnus-check-backend-function 'request-set-mark group)
9560             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9561                   (add (gnus-remove-from-range read (gnus-info-read info))))
9562               (when (or add del)
9563                 (unless (gnus-check-group group)
9564                   (error "Can't open server for %s" group))
9565                 (gnus-request-set-mark
9566                  group (delq nil (list (if add (list add 'add '(read)))
9567                                        (if del (list del 'del '(read)))))))))
9568         ;; Enter this list into the group info.
9569         (gnus-info-set-read info read)
9570         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9571         (gnus-get-unread-articles-in-group info (gnus-active group))
9572         t))))
9573
9574 (defun gnus-offer-save-summaries ()
9575   "Offer to save all active summary buffers."
9576   (save-excursion
9577     (let ((buflist (buffer-list))
9578           buffers bufname)
9579       ;; Go through all buffers and find all summaries.
9580       (while buflist
9581         (and (setq bufname (buffer-name (car buflist)))
9582              (string-match "Summary" bufname)
9583              (save-excursion
9584                (set-buffer bufname)
9585                ;; We check that this is, indeed, a summary buffer.
9586                (and (eq major-mode 'gnus-summary-mode)
9587                     ;; Also make sure this isn't bogus.
9588                     gnus-newsgroup-prepared
9589                     ;; Also make sure that this isn't a dead summary buffer.
9590                     (not gnus-dead-summary-mode)))
9591              (push bufname buffers))
9592         (setq buflist (cdr buflist)))
9593       ;; Go through all these summary buffers and offer to save them.
9594       (when buffers
9595         (map-y-or-n-p
9596          "Update summary buffer %s? "
9597          (lambda (buf)
9598            (switch-to-buffer buf)
9599            (gnus-summary-exit))
9600          buffers)))))
9601
9602
9603 ;;; @ for mime-partial
9604 ;;;
9605
9606 (defun gnus-request-partial-message ()
9607   (save-excursion
9608     (let ((number (gnus-summary-article-number))
9609           (group gnus-newsgroup-name)
9610           (mother gnus-article-buffer))
9611       (set-buffer (get-buffer-create " *Partial Article*"))
9612       (erase-buffer)
9613       (setq mime-preview-buffer mother)
9614       (gnus-request-article-this-buffer number group)
9615       (mime-parse-buffer)
9616       )))
9617
9618 (autoload 'mime-combine-message/partial-pieces-automatically
9619   "mime-partial"
9620   "Internal method to combine message/partial messages automatically.")
9621
9622 (mime-add-condition
9623  'action '((type . message)(subtype . partial)
9624            (major-mode . gnus-original-article-mode)
9625            (method . mime-combine-message/partial-pieces-automatically)
9626            (summary-buffer-exp . gnus-summary-buffer)
9627            (request-partial-message-method . gnus-request-partial-message)
9628            ))
9629
9630
9631 ;;; @ for message/rfc822
9632 ;;;
9633
9634 (defun gnus-mime-extract-message/rfc822 (entity situation)
9635   (let (group article num cwin swin cur)
9636     (with-current-buffer (mime-entity-buffer entity)
9637       (save-restriction
9638         (narrow-to-region (mime-entity-body-start entity)
9639                           (mime-entity-body-end entity))
9640         (setq group (or (cdr (assq 'group situation))
9641                         (completing-read "Group: "
9642                                          gnus-active-hashtb
9643                                          nil
9644                                          (gnus-read-active-file-p)
9645                                          gnus-newsgroup-name))
9646               article (gnus-request-accept-article group)
9647               )
9648         ))
9649     (when (and (consp article)
9650                (numberp (setq article (cdr article))))
9651       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9652             cwin (get-buffer-window (current-buffer) t)
9653             )
9654       (save-window-excursion
9655         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9656             (select-window swin)
9657           (set-buffer gnus-summary-buffer)
9658           )
9659         (setq cur gnus-current-article)
9660         (forward-line num)
9661         (let (gnus-show-threads)
9662           (gnus-summary-goto-subject article t)
9663           )
9664         (gnus-summary-clear-mark-forward 1)
9665         (gnus-summary-goto-subject cur)
9666         )
9667       (when (and cwin (window-frame cwin))
9668         (select-frame (window-frame cwin))
9669         )
9670       (when (boundp 'mime-acting-situation-to-override)
9671         (set-alist 'mime-acting-situation-to-override
9672                    'group
9673                    group)
9674         (set-alist 'mime-acting-situation-to-override
9675                    'after-method
9676                    `(progn
9677                       (save-current-buffer
9678                         (set-buffer gnus-group-buffer)
9679                         (gnus-activate-group ,group)
9680                         )
9681                       (gnus-summary-goto-article ,cur
9682                                                  gnus-show-all-headers)
9683                       ))
9684         (set-alist 'mime-acting-situation-to-override
9685                    'number num)
9686         )
9687       )))
9688
9689 (mime-add-condition
9690  'action '((type . message)(subtype . rfc822)
9691            (major-mode . gnus-original-article-mode)
9692            (method . gnus-mime-extract-message/rfc822)
9693            (mode . "extract")
9694            ))
9695
9696 (mime-add-condition
9697  'action '((type . message)(subtype . news)
9698            (major-mode . gnus-original-article-mode)
9699            (method . gnus-mime-extract-message/rfc822)
9700            (mode . "extract")
9701            ))
9702
9703 (defun gnus-mime-extract-multipart (entity situation)
9704   (let ((children (mime-entity-children entity))
9705         mime-acting-situation-to-override
9706         f)
9707     (while children
9708       (mime-play-entity (car children)
9709                         (cons (assq 'mode situation)
9710                               mime-acting-situation-to-override))
9711       (setq children (cdr children)))
9712     (if (setq f (cdr (assq 'after-method
9713                            mime-acting-situation-to-override)))
9714         (eval f)
9715       )))  
9716
9717 (mime-add-condition
9718  'action '((type . multipart)
9719            (method . gnus-mime-extract-multipart)
9720            (mode . "extract")
9721            )
9722  'with-default)
9723
9724
9725 ;;; @ end
9726 ;;;
9727
9728 (defun gnus-summary-setup-default-charset ()
9729   "Setup newsgroup default charset."
9730   (if (equal gnus-newsgroup-name "nndraft:drafts")
9731       (setq gnus-newsgroup-charset nil)
9732     (let* ((name (and gnus-newsgroup-name
9733                       (gnus-group-real-name gnus-newsgroup-name)))
9734            (ignored-charsets 
9735             (or gnus-newsgroup-ephemeral-ignored-charsets
9736                 (append
9737                  (and gnus-newsgroup-name
9738                       (or (gnus-group-find-parameter gnus-newsgroup-name
9739                                                      'ignored-charsets t)
9740                           (let ((alist gnus-group-ignored-charsets-alist)
9741                                 elem (charsets nil))
9742                             (while (setq elem (pop alist))
9743                               (when (and name
9744                                          (string-match (car elem) name))
9745                                 (setq alist nil
9746                                       charsets (cdr elem))))
9747                             charsets)))
9748                  gnus-newsgroup-ignored-charsets))))
9749       (setq gnus-newsgroup-charset
9750             (or gnus-newsgroup-ephemeral-charset
9751                 (and gnus-newsgroup-name
9752                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9753                          (let ((alist gnus-group-charset-alist)
9754                                elem charset)
9755                            (while (setq elem (pop alist))
9756                              (when (and name
9757                                         (string-match (car elem) name))
9758                                (setq alist nil
9759                                      charset (cadr elem))))
9760                            charset)))
9761                 gnus-default-charset))
9762       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9763            ignored-charsets))))
9764
9765 ;;;
9766 ;;; Mime Commands
9767 ;;;
9768
9769 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9770   "Display the current article buffer fully MIME-buttonized.
9771 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9772 treated as multipart/mixed."
9773   (interactive "P")
9774   (require 'gnus-art)
9775   (let ((gnus-unbuttonized-mime-types nil)
9776         (gnus-mime-display-multipart-as-mixed show-all-parts))
9777     (gnus-summary-show-article)))
9778
9779 (defun gnus-summary-repair-multipart (article)
9780   "Add a Content-Type header to a multipart article without one."
9781   (interactive (list (gnus-summary-article-number)))
9782   (gnus-with-article article
9783     (message-narrow-to-head)
9784     (goto-char (point-max))
9785     (widen)
9786     (when (search-forward "\n--" nil t)
9787       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9788         (message-narrow-to-head)
9789         (message-remove-header "Mime-Version")
9790         (message-remove-header "Content-Type")
9791         (goto-char (point-max))
9792         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9793                         separator))
9794         (insert "Mime-Version: 1.0\n")
9795         (widen))))
9796   (let (gnus-mark-article-hook)
9797     (gnus-summary-select-article t t nil article)))
9798
9799 (defun gnus-summary-toggle-display-buttonized ()
9800   "Toggle the buttonizing of the article buffer."
9801   (interactive)
9802   (require 'gnus-art)
9803   (if (setq gnus-inhibit-mime-unbuttonizing
9804             (not gnus-inhibit-mime-unbuttonizing))
9805       (let ((gnus-unbuttonized-mime-types nil))
9806         (gnus-summary-show-article))
9807     (gnus-summary-show-article)))
9808
9809 ;;;
9810 ;;; Intelli-mouse commmands
9811 ;;;
9812
9813 (defun gnus-wheel-summary-scroll (event)
9814   (interactive "e")
9815   (let ((amount (if (memq 'shift (event-modifiers event))
9816                     (car gnus-wheel-scroll-amount)
9817                   (cdr gnus-wheel-scroll-amount)))
9818         (direction (- (* (static-if (featurep 'xemacs)
9819                              (event-button event)
9820                            (cond ((eq 'mouse-4 (event-basic-type event))
9821                                   4)
9822                                  ((eq 'mouse-5 (event-basic-type event))
9823                                   5)))
9824                          2) 9))
9825         edge)
9826     (gnus-summary-scroll-up (* amount direction))
9827     (when (gnus-eval-in-buffer-window gnus-article-buffer
9828             (save-restriction
9829               (widen)
9830               (and (if (< 0 direction)
9831                        (gnus-article-next-page 0)
9832                      (gnus-article-prev-page 0)
9833                      (bobp))
9834                    (if (setq edge (get-text-property
9835                                    (point-min) 'gnus-wheel-edge))
9836                        (setq edge (* edge direction))
9837                      (setq edge -1))
9838                    (or (plusp edge)
9839                        (let ((buffer-read-only nil)
9840                              (inhibit-read-only t))
9841                          (put-text-property (point-min) (point-max)
9842                                             'gnus-wheel-edge direction)
9843                          nil))
9844                    (or (> edge gnus-wheel-edge-resistance)
9845                        (let ((buffer-read-only nil)
9846                              (inhibit-read-only t))
9847                          (put-text-property (point-min) (point-max)
9848                                             'gnus-wheel-edge
9849                                             (* (1+ edge) direction))
9850                          nil))
9851                    (eq last-command 'gnus-wheel-summary-scroll))
9852               ))
9853       (gnus-summary-next-article nil nil (minusp direction)))
9854     ))
9855
9856 (defun gnus-wheel-install ()
9857   "Enable mouse wheel support on summary window."
9858   (when gnus-use-wheel
9859     (let ((keys 
9860            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9861       (dolist (key keys)
9862         (define-key gnus-summary-mode-map key
9863           'gnus-wheel-summary-scroll)))))
9864
9865 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9866
9867 ;;;
9868 ;;; with article
9869 ;;;
9870
9871 (defmacro gnus-with-article (article &rest forms)
9872   "Select ARTICLE and perform FORMS in the original article buffer.
9873 Then replace the article with the result."
9874   `(progn
9875      ;; We don't want the article to be marked as read.
9876      (let (gnus-mark-article-hook)
9877        (gnus-summary-select-article t t nil ,article))
9878      (set-buffer gnus-original-article-buffer)
9879      ,@forms
9880      (if (not (gnus-check-backend-function
9881                'request-replace-article (car gnus-article-current)))
9882          (gnus-message 5 "Read-only group; not replacing")
9883        (unless (gnus-request-replace-article
9884                 ,article (car gnus-article-current)
9885                 (current-buffer) t)
9886          (error "Couldn't replace article")))
9887      ;; The cache and backlog have to be flushed somewhat.
9888      (when gnus-keep-backlog
9889        (gnus-backlog-remove-article
9890         (car gnus-article-current) (cdr gnus-article-current)))
9891      (when gnus-use-cache
9892        (gnus-cache-update-article
9893         (car gnus-article-current) (cdr gnus-article-current)))))
9894
9895 (put 'gnus-with-article 'lisp-indent-function 1)
9896 (put 'gnus-with-article 'edebug-form-spec '(form body))
9897
9898 ;;;
9899 ;;; Generic summary marking commands
9900 ;;;
9901
9902 (defvar gnus-summary-marking-alist
9903   '((read gnus-del-mark "d")
9904     (unread gnus-unread-mark "u")
9905     (ticked gnus-ticked-mark "!")
9906     (dormant gnus-dormant-mark "?")
9907     (expirable gnus-expirable-mark "e"))
9908   "An alist of names/marks/keystrokes.")
9909
9910 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9911 (defvar gnus-summary-mark-map)
9912
9913 (defun gnus-summary-make-all-marking-commands ()
9914   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9915   (dolist (elem gnus-summary-marking-alist)
9916     (apply 'gnus-summary-make-marking-command elem)))
9917
9918 (defun gnus-summary-make-marking-command (name mark keystroke)
9919   (let ((map (make-sparse-keymap)))
9920     (define-key gnus-summary-generic-mark-map keystroke map)
9921     (dolist (lway `((next "next" next nil "n")
9922                     (next-unread "next unread" next t "N")
9923                     (prev "previous" prev nil "p")
9924                     (prev-unread "previous unread" prev t "P")
9925                     (nomove "" nil nil ,keystroke)))
9926       (let ((func (gnus-summary-make-marking-command-1
9927                    mark (car lway) lway name)))
9928         (setq func (eval func))
9929         (define-key map (nth 4 lway) func)))))
9930       
9931 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9932   `(defun ,(intern
9933             (format "gnus-summary-put-mark-as-%s%s"
9934                     name (if (eq way 'nomove)
9935                              ""
9936                            (concat "-" (symbol-name way)))))
9937      (n)
9938      ,(format
9939        "Mark the current article as %s%s.
9940 If N, the prefix, then repeat N times.
9941 If N is negative, move in reverse order.
9942 The difference between N and the actual number of articles marked is
9943 returned."
9944        name (car (cdr lway)))
9945      (interactive "p")
9946      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9947     
9948 (defun gnus-summary-generic-mark (n mark move unread)
9949   "Mark N articles with MARK."
9950   (unless (eq major-mode 'gnus-summary-mode)
9951     (error "This command can only be used in the summary buffer"))
9952   (gnus-summary-show-thread)
9953   (let ((nummove
9954          (cond
9955           ((eq move 'next) 1)
9956           ((eq move 'prev) -1)
9957           (t 0))))
9958     (if (zerop nummove)
9959         (setq n 1)
9960       (when (< n 0)
9961         (setq n (abs n)
9962               nummove (* -1 nummove))))
9963     (while (and (> n 0)
9964                 (gnus-summary-mark-article nil mark)
9965                 (zerop (gnus-summary-next-subject nummove unread t)))
9966       (setq n (1- n)))
9967     (when (/= 0 n)
9968       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9969     (gnus-summary-recenter)
9970     (gnus-summary-position-point)
9971     (gnus-set-mode-line 'summary)
9972     n))
9973
9974 (gnus-summary-make-all-marking-commands)
9975
9976 (gnus-ems-redefine)
9977
9978 (provide 'gnus-sum)
9979
9980 (run-hooks 'gnus-sum-load-hook)
9981
9982 ;;; gnus-sum.el ends here