(mail-source-fetch-imap): Don't create multiple temporary buffers, and
[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 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mime-view)
39
40 (eval-when-compile
41   (require 'mime-play)
42   (require 'static))
43
44 (eval-and-compile
45   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
46
47 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
48 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
113   "*A regexp to match subjects to be excluded from loose thread gathering.
114 As loose thread gathering is done on subjects only, that means that
115 there can be many false gatherings performed.  By rooting out certain
116 common subjects, gathering might become saner."
117   :group 'gnus-thread
118   :type 'regexp)
119
120 (defcustom gnus-summary-gather-subject-limit nil
121   "*Maximum length of subject comparisons when gathering loose threads.
122 Use nil to compare full subjects.  Setting this variable to a low
123 number will help gather threads that have been corrupted by
124 newsreaders chopping off subject lines, but it might also mean that
125 unrelated articles that have subject that happen to begin with the
126 same few characters will be incorrectly gathered.
127
128 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
129 comparing subjects."
130   :group 'gnus-thread
131   :type '(choice (const :tag "off" nil)
132                  (const fuzzy)
133                  (sexp :menu-tag "on" t)))
134
135 (defcustom gnus-simplify-subject-functions nil
136   "List of functions taking a string argument that simplify subjects.
137 The functions are applied recursively.
138
139 Useful functions to put in this list include: `gnus-simplify-subject-re',
140 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
141   :group 'gnus-thread
142   :type '(repeat function))
143
144 (defcustom gnus-simplify-ignored-prefixes nil
145   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
146   :group 'gnus-thread
147   :type '(choice (const :tag "off" nil)
148                  regexp))
149
150 (defcustom gnus-build-sparse-threads nil
151   "*If non-nil, fill in the gaps in threads.
152 If `some', only fill in the gaps that are needed to tie loose threads
153 together.  If `more', fill in all leaf nodes that Gnus can find.  If
154 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  (const some)
158                  (const more)
159                  (sexp :menu-tag "all" t)))
160
161 (defcustom gnus-summary-thread-gathering-function
162   'gnus-gather-threads-by-subject
163   "*Function used for gathering loose threads.
164 There are two pre-defined functions: `gnus-gather-threads-by-subject',
165 which only takes Subjects into consideration; and
166 `gnus-gather-threads-by-references', which compared the References
167 headers of the articles to find matches."
168   :group 'gnus-thread
169   :type '(radio (function-item gnus-gather-threads-by-subject)
170                 (function-item gnus-gather-threads-by-references)
171                 (function :tag "other")))
172
173 (defcustom gnus-summary-same-subject ""
174   "*String indicating that the current article has the same subject as the previous.
175 This variable will only be used if the value of
176 `gnus-summary-make-false-root' is `empty'."
177   :group 'gnus-summary-format
178   :type 'string)
179
180 (defcustom gnus-summary-goto-unread t
181   "*If t, marking commands will go to the next unread article.
182 If `never', commands that usually go to the next unread article, will
183 go to the next article, whether it is read or not.
184 If nil, only the marking commands will go to the next (un)read article."
185   :group 'gnus-summary-marks
186   :link '(custom-manual "(gnus)Setting Marks")
187   :type '(choice (const :tag "off" nil)
188                  (const never)
189                  (sexp :menu-tag "on" t)))
190
191 (defcustom gnus-summary-default-score 0
192   "*Default article score level.
193 All scores generated by the score files will be added to this score.
194 If this variable is nil, scoring will be disabled."
195   :group 'gnus-score-default
196   :type '(choice (const :tag "disable")
197                  integer))
198
199 (defcustom gnus-summary-zcore-fuzz 0
200   "*Fuzziness factor for the zcore in the summary buffer.
201 Articles with scores closer than this to `gnus-summary-default-score'
202 will not be marked."
203   :group 'gnus-summary-format
204   :type 'integer)
205
206 (defcustom gnus-simplify-subject-fuzzy-regexp nil
207   "*Strings to be removed when doing fuzzy matches.
208 This can either be a regular expression or list of regular expressions
209 that will be removed from subject strings if fuzzy subject
210 simplification is selected."
211   :group 'gnus-thread
212   :type '(repeat regexp))
213
214 (defcustom gnus-show-threads t
215   "*If non-nil, display threads in summary mode."
216   :group 'gnus-thread
217   :type 'boolean)
218
219 (defcustom gnus-thread-hide-subtree nil
220   "*If non-nil, hide all threads initially.
221 If threads are hidden, you have to run the command
222 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
223 to expose hidden threads."
224   :group 'gnus-thread
225   :type 'boolean)
226
227 (defcustom gnus-thread-hide-killed t
228   "*If non-nil, hide killed threads automatically."
229   :group 'gnus-thread
230   :type 'boolean)
231
232 (defcustom gnus-thread-ignore-subject t
233   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
234 If nil, articles that have different subjects from their parents will
235 start separate threads."
236   :group 'gnus-thread
237   :type 'boolean)
238
239 (defcustom gnus-thread-operation-ignore-subject t
240   "*If non-nil, subjects will be ignored when doing thread commands.
241 This affects commands like `gnus-summary-kill-thread' and
242 `gnus-summary-lower-thread'.
243
244 If this variable is nil, articles in the same thread with different
245 subjects will not be included in the operation in question.  If this
246 variable is `fuzzy', only articles that have subjects that are fuzzily
247 equal will be included."
248   :group 'gnus-thread
249   :type '(choice (const :tag "off" nil)
250                  (const fuzzy)
251                  (sexp :tag "on" t)))
252
253 (defcustom gnus-thread-indent-level 4
254   "*Number that says how much each sub-thread should be indented."
255   :group 'gnus-thread
256   :type 'integer)
257
258 (defcustom gnus-auto-extend-newsgroup t
259   "*If non-nil, extend newsgroup forward and backward when requested."
260   :group 'gnus-summary-choose
261   :type 'boolean)
262
263 (defcustom gnus-auto-select-first t
264   "*If nil, don't select the first unread article when entering a group.
265 If this variable is `best', select the highest-scored unread article
266 in the group.  If t, select the first unread article.
267
268 This variable can also be a function to place point on a likely
269 subject line.  Useful values include `gnus-summary-first-unread-subject',
270 `gnus-summary-first-unread-article' and
271 `gnus-summary-best-unread-article'.
272
273 If you want to prevent automatic selection of the first unread article
274 in some newsgroups, set the variable to nil in
275 `gnus-select-group-hook'."
276   :group 'gnus-group-select
277   :type '(choice (const :tag "none" nil)
278                  (const best)
279                  (sexp :menu-tag "first" t)
280                  (function-item gnus-summary-first-unread-subject)
281                  (function-item gnus-summary-first-unread-article)
282                  (function-item gnus-summary-best-unread-article)))
283
284 (defcustom gnus-dont-select-after-jump-to-other-group nil
285   "If non-nil, don't select the first unread article after entering the
286 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
287 it is depend on the value of `gnus-auto-select-first' whether to select
288 or not."
289   :group 'gnus-group-select
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-next t
293   "*If non-nil, offer to go to the next group from the end of the previous.
294 If the value is t and the next newsgroup is empty, Gnus will exit
295 summary mode and go back to group mode.  If the value is neither nil
296 nor t, Gnus will select the following unread newsgroup.  In
297 particular, if the value is the symbol `quietly', the next unread
298 newsgroup will be selected without any confirmation, and if it is
299 `almost-quietly', the next group will be selected without any
300 confirmation if you are located on the last article in the group.
301 Finally, if this variable is `slightly-quietly', the `Z n' command
302 will go to the next group without confirmation."
303   :group 'gnus-summary-maneuvering
304   :type '(choice (const :tag "off" nil)
305                  (const quietly)
306                  (const almost-quietly)
307                  (const slightly-quietly)
308                  (sexp :menu-tag "on" t)))
309
310 (defcustom gnus-auto-select-same nil
311   "*If non-nil, select the next article with the same subject.
312 If there are no more articles with the same subject, go to
313 the first unread article."
314   :group 'gnus-summary-maneuvering
315   :type 'boolean)
316
317 (defcustom gnus-summary-check-current nil
318   "*If non-nil, consider the current article when moving.
319 The \"unread\" movement commands will stay on the same line if the
320 current article is unread."
321   :group 'gnus-summary-maneuvering
322   :type 'boolean)
323
324 (defcustom gnus-auto-center-summary t
325   "*If non-nil, always center the current summary buffer.
326 In particular, if `vertical' do only vertical recentering.  If non-nil
327 and non-`vertical', do both horizontal and vertical recentering."
328   :group 'gnus-summary-maneuvering
329   :type '(choice (const :tag "none" nil)
330                  (const vertical)
331                  (integer :tag "height")
332                  (sexp :menu-tag "both" t)))
333
334 (defcustom gnus-show-all-headers nil
335   "*If non-nil, don't hide any headers."
336   :group 'gnus-article-hiding
337   :group 'gnus-article-headers
338   :type 'boolean)
339
340 (defcustom gnus-summary-ignore-duplicates nil
341   "*If non-nil, ignore articles with identical Message-ID headers."
342   :group 'gnus-summary
343   :type 'boolean)
344
345 (defcustom gnus-single-article-buffer t
346   "*If non-nil, display all articles in the same buffer.
347 If nil, each group will get its own article buffer."
348   :group 'gnus-article-various
349   :type 'boolean)
350
351 (defcustom gnus-break-pages t
352   "*If non-nil, do page breaking on articles.
353 The page delimiter is specified by the `gnus-page-delimiter'
354 variable."
355   :group 'gnus-article-various
356   :type 'boolean)
357
358 (defcustom gnus-show-mime t
359   "*If non-nil, do mime processing of articles.
360 The articles will simply be fed to the function given by
361 `gnus-article-display-method-for-mime'."
362   :group 'gnus-article-mime
363   :type 'boolean)
364
365 (defcustom gnus-move-split-methods nil
366   "*Variable used to suggest where articles are to be moved to.
367 It uses the same syntax as the `gnus-split-methods' variable."
368   :group 'gnus-summary-mail
369   :type '(repeat (choice (list :value (fun) function)
370                          (cons :value ("" "") regexp (repeat string))
371                          (sexp :value nil))))
372
373 (defcustom gnus-unread-mark ? ;Whitespace
374   "*Mark used for unread articles."
375   :group 'gnus-summary-marks
376   :type 'character)
377
378 (defcustom gnus-ticked-mark ?!
379   "*Mark used for ticked articles."
380   :group 'gnus-summary-marks
381   :type 'character)
382
383 (defcustom gnus-dormant-mark ??
384   "*Mark used for dormant articles."
385   :group 'gnus-summary-marks
386   :type 'character)
387
388 (defcustom gnus-del-mark ?r
389   "*Mark used for del'd articles."
390   :group 'gnus-summary-marks
391   :type 'character)
392
393 (defcustom gnus-read-mark ?R
394   "*Mark used for read articles."
395   :group 'gnus-summary-marks
396   :type 'character)
397
398 (defcustom gnus-expirable-mark ?E
399   "*Mark used for expirable articles."
400   :group 'gnus-summary-marks
401   :type 'character)
402
403 (defcustom gnus-killed-mark ?K
404   "*Mark used for killed articles."
405   :group 'gnus-summary-marks
406   :type 'character)
407
408 (defcustom gnus-souped-mark ?F
409   "*Mark used for killed articles."
410   :group 'gnus-summary-marks
411   :type 'character)
412
413 (defcustom gnus-kill-file-mark ?X
414   "*Mark used for articles killed by kill files."
415   :group 'gnus-summary-marks
416   :type 'character)
417
418 (defcustom gnus-low-score-mark ?Y
419   "*Mark used for articles with a low score."
420   :group 'gnus-summary-marks
421   :type 'character)
422
423 (defcustom gnus-catchup-mark ?C
424   "*Mark used for articles that are caught up."
425   :group 'gnus-summary-marks
426   :type 'character)
427
428 (defcustom gnus-replied-mark ?A
429   "*Mark used for articles that have been replied to."
430   :group 'gnus-summary-marks
431   :type 'character)
432
433 (defcustom gnus-cached-mark ?*
434   "*Mark used for articles that are in the cache."
435   :group 'gnus-summary-marks
436   :type 'character)
437
438 (defcustom gnus-saved-mark ?S
439   "*Mark used for articles that have been saved to."
440   :group 'gnus-summary-marks
441   :type 'character)
442
443 (defcustom gnus-ancient-mark ?O
444   "*Mark used for ancient articles."
445   :group 'gnus-summary-marks
446   :type 'character)
447
448 (defcustom gnus-sparse-mark ?Q
449   "*Mark used for sparsely reffed articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-canceled-mark ?G
454   "*Mark used for canceled articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-duplicate-mark ?M
459   "*Mark used for duplicate articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-undownloaded-mark ?@
464   "*Mark used for articles that weren't downloaded."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-downloadable-mark ?%
469   "*Mark used for articles that are to be downloaded."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-unsendable-mark ?=
474   "*Mark used for articles that won't be sent."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-score-over-mark ?+
479   "*Score mark used for articles with high scores."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-score-below-mark ?-
484   "*Score mark used for articles with low scores."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-empty-thread-mark ? ;Whitespace
489   "*There is no thread under the article."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-not-empty-thread-mark ?=
494   "*There is a thread under the article."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-view-pseudo-asynchronously nil
499   "*If non-nil, Gnus will view pseudo-articles asynchronously."
500   :group 'gnus-extract-view
501   :type 'boolean)
502
503 (defcustom gnus-auto-expirable-marks
504   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
505         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
506         gnus-souped-mark gnus-duplicate-mark)
507   "*The list of marks converted into expiration if a group is auto-expirable."
508   :group 'gnus-summary
509   :type '(repeat character))
510
511 (defcustom gnus-inhibit-user-auto-expire t
512   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
513   :group 'gnus-summary
514   :type 'boolean)
515
516 (defcustom gnus-view-pseudos nil
517   "*If `automatic', pseudo-articles will be viewed automatically.
518 If `not-confirm', pseudos will be viewed automatically, and the user
519 will not be asked to confirm the command."
520   :group 'gnus-extract-view
521   :type '(choice (const :tag "off" nil)
522                  (const automatic)
523                  (const not-confirm)))
524
525 (defcustom gnus-view-pseudos-separately t
526   "*If non-nil, one pseudo-article will be created for each file to be viewed.
527 If nil, all files that use the same viewing command will be given as a
528 list of parameters to that command."
529   :group 'gnus-extract-view
530   :type 'boolean)
531
532 (defcustom gnus-insert-pseudo-articles t
533   "*If non-nil, insert pseudo-articles when decoding articles."
534   :group 'gnus-extract-view
535   :type 'boolean)
536
537 (defcustom gnus-summary-dummy-line-format
538   "  %(:                          :%) %S\n"
539   "*The format specification for the dummy roots in the summary buffer.
540 It works along the same lines as a normal formatting string,
541 with some simple extensions.
542
543 %S  The subject"
544   :group 'gnus-threading
545   :type 'string)
546
547 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
548   "*The format specification for the summary mode line.
549 It works along the same lines as a normal formatting string,
550 with some simple extensions:
551
552 %G  Group name
553 %p  Unprefixed group name
554 %A  Current article number
555 %z  Current article score
556 %V  Gnus version
557 %U  Number of unread articles in the group
558 %e  Number of unselected articles in the group
559 %Z  A string with unread/unselected article counts
560 %g  Shortish group name
561 %S  Subject of the current article
562 %u  User-defined spec
563 %s  Current score file name
564 %d  Number of dormant articles
565 %r  Number of articles that have been marked as read in this session
566 %E  Number of articles expunged by the score files"
567   :group 'gnus-summary-format
568   :type 'string)
569
570 (defcustom gnus-list-identifiers nil
571   "Regexp that matches list identifiers to be removed from subject.
572 This can also be a list of regexps."
573   :group 'gnus-summary-format
574   :group 'gnus-article-hiding
575   :type '(choice (const :tag "none" nil)
576                  (regexp :value ".*")
577                  (repeat :value (".*") regexp)))
578
579 (defcustom gnus-summary-mark-below 0
580   "*Mark all articles with a score below this variable as read.
581 This variable is local to each summary buffer and usually set by the
582 score file."
583   :group 'gnus-score-default
584   :type 'integer)
585
586 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
587   "*List of functions used for sorting articles in the summary buffer.
588 This variable is only used when not using a threaded display."
589   :group 'gnus-summary-sort
590   :type '(repeat (choice (function-item gnus-article-sort-by-number)
591                          (function-item gnus-article-sort-by-author)
592                          (function-item gnus-article-sort-by-subject)
593                          (function-item gnus-article-sort-by-date)
594                          (function-item gnus-article-sort-by-score)
595                          (function :tag "other"))))
596
597 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
598   "*List of functions used for sorting threads in the summary buffer.
599 By default, threads are sorted by article number.
600
601 Each function takes two threads and return non-nil if the first thread
602 should be sorted before the other.  If you use more than one function,
603 the primary sort function should be the last.  You should probably
604 always include `gnus-thread-sort-by-number' in the list of sorting
605 functions -- preferably first.
606
607 Ready-made functions include `gnus-thread-sort-by-number',
608 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
609 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
610 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
611   :group 'gnus-summary-sort
612   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
613                          (function-item gnus-thread-sort-by-author)
614                          (function-item gnus-thread-sort-by-subject)
615                          (function-item gnus-thread-sort-by-date)
616                          (function-item gnus-thread-sort-by-score)
617                          (function-item gnus-thread-sort-by-total-score)
618                          (function :tag "other"))))
619
620 (defcustom gnus-thread-score-function '+
621   "*Function used for calculating the total score of a thread.
622
623 The function is called with the scores of the article and each
624 subthread and should then return the score of the thread.
625
626 Some functions you can use are `+', `max', or `min'."
627   :group 'gnus-summary-sort
628   :type 'function)
629
630 (defcustom gnus-summary-expunge-below nil
631   "All articles that have a score less than this variable will be expunged.
632 This variable is local to the summary buffers."
633   :group 'gnus-score-default
634   :type '(choice (const :tag "off" nil)
635                  integer))
636
637 (defcustom gnus-thread-expunge-below nil
638   "All threads that have a total score less than this variable will be expunged.
639 See `gnus-thread-score-function' for en explanation of what a
640 \"thread score\" is.
641
642 This variable is local to the summary buffers."
643   :group 'gnus-threading
644   :group 'gnus-score-default
645   :type '(choice (const :tag "off" nil)
646                  integer))
647
648 (defcustom gnus-summary-mode-hook nil
649   "*A hook for Gnus summary mode.
650 This hook is run before any variables are set in the summary buffer."
651   :group 'gnus-summary-various
652   :type 'hook)
653
654 (defcustom gnus-summary-menu-hook nil
655   "*Hook run after the creation of the summary mode menu."
656   :group 'gnus-summary-visual
657   :type 'hook)
658
659 (defcustom gnus-summary-exit-hook nil
660   "*A hook called on exit from the summary buffer.
661 It will be called with point in the group buffer."
662   :group 'gnus-summary-exit
663   :type 'hook)
664
665 (defcustom gnus-summary-prepare-hook nil
666   "*A hook called after the summary buffer has been generated.
667 If you want to modify the summary buffer, you can use this hook."
668   :group 'gnus-summary-various
669   :type 'hook)
670
671 (defcustom gnus-summary-prepared-hook nil
672   "*A hook called as the last thing after the summary buffer has been generated."
673   :group 'gnus-summary-various
674   :type 'hook)
675
676 (defcustom gnus-summary-generate-hook nil
677   "*A hook run just before generating the summary buffer.
678 This hook is commonly used to customize threading variables and the
679 like."
680   :group 'gnus-summary-various
681   :type 'hook)
682
683 (defcustom gnus-select-group-hook nil
684   "*A hook called when a newsgroup is selected.
685
686 If you'd like to simplify subjects like the
687 `gnus-summary-next-same-subject' command does, you can use the
688 following hook:
689
690  (setq gnus-select-group-hook
691       (list
692         (lambda ()
693           (mapcar (lambda (header)
694                      (mail-header-set-subject
695                       header
696                       (gnus-simplify-subject
697                        (mail-header-subject header) 're-only)))
698                   gnus-newsgroup-headers))))"
699   :group 'gnus-group-select
700   :type 'hook)
701
702 (defcustom gnus-select-article-hook nil
703   "*A hook called when an article is selected."
704   :group 'gnus-summary-choose
705   :type 'hook)
706
707 (defcustom gnus-visual-mark-article-hook
708   (list 'gnus-highlight-selected-summary)
709   "*Hook run after selecting an article in the summary buffer.
710 It is meant to be used for highlighting the article in some way.  It
711 is not run if `gnus-visual' is nil."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
716   "*A hook called before parsing the headers."
717   :group 'gnus-various
718   :type 'hook)
719
720 (defcustom gnus-exit-group-hook nil
721   "*A hook called when exiting (not quitting) summary mode."
722   :group 'gnus-various
723   :type 'hook)
724
725 (defcustom gnus-summary-update-hook
726   (list 'gnus-summary-highlight-line)
727   "*A hook called when a summary line is changed.
728 The hook will not be called if `gnus-visual' is nil.
729
730 The default function `gnus-summary-highlight-line' will
731 highlight the line according to the `gnus-summary-highlight'
732 variable."
733   :group 'gnus-summary-visual
734   :type 'hook)
735
736 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
737   "*A hook called when an article is selected for the first time.
738 The hook is intended to mark an article as read (or unread)
739 automatically when it is selected."
740   :group 'gnus-summary-choose
741   :type 'hook)
742
743 (defcustom gnus-group-no-more-groups-hook nil
744   "*A hook run when returning to group mode having no more (unread) groups."
745   :group 'gnus-group-select
746   :type 'hook)
747
748 (defcustom gnus-ps-print-hook nil
749   "*A hook run before ps-printing something from Gnus."
750   :group 'gnus-summary
751   :type 'hook)
752
753 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
754   "Face used for highlighting the current article in the summary buffer."
755   :group 'gnus-summary-visual
756   :type 'face)
757
758 (defcustom gnus-summary-highlight
759   '(((= mark gnus-canceled-mark)
760      . gnus-summary-cancelled-face)
761     ((and (> score default)
762           (or (= mark gnus-dormant-mark)
763               (= mark gnus-ticked-mark)))
764      . gnus-summary-high-ticked-face)
765     ((and (< score default)
766           (or (= mark gnus-dormant-mark)
767               (= mark gnus-ticked-mark)))
768      . gnus-summary-low-ticked-face)
769     ((or (= mark gnus-dormant-mark)
770          (= mark gnus-ticked-mark))
771      . gnus-summary-normal-ticked-face)
772     ((and (> score default) (= mark gnus-ancient-mark))
773      . gnus-summary-high-ancient-face)
774     ((and (< score default) (= mark gnus-ancient-mark))
775      . gnus-summary-low-ancient-face)
776     ((= mark gnus-ancient-mark)
777      . gnus-summary-normal-ancient-face)
778     ((and (> score default) (= mark gnus-unread-mark))
779      . gnus-summary-high-unread-face)
780     ((and (< score default) (= mark gnus-unread-mark))
781      . gnus-summary-low-unread-face)
782     ((and (memq article gnus-newsgroup-incorporated) 
783           (= mark gnus-unread-mark))
784      . gnus-summary-incorporated-face)
785     ((= mark gnus-unread-mark)
786      . gnus-summary-normal-unread-face)
787     ((and (> score default) (memq mark (list gnus-downloadable-mark
788                                              gnus-undownloaded-mark)))
789      . gnus-summary-high-unread-face)
790     ((and (< score default) (memq mark (list gnus-downloadable-mark
791                                              gnus-undownloaded-mark)))
792      . gnus-summary-low-unread-face)
793     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
794      . gnus-summary-normal-unread-face)
795     ((> score default)
796      . gnus-summary-high-read-face)
797     ((< score default)
798      . gnus-summary-low-read-face)
799     (t
800      . gnus-summary-normal-read-face))
801   "*Controls the highlighting of summary buffer lines.
802
803 A list of (FORM . FACE) pairs.  When deciding how a a particular
804 summary line should be displayed, each form is evaluated.  The content
805 of the face field after the first true form is used.  You can change
806 how those summary lines are displayed, by editing the face field.
807
808 You can use the following variables in the FORM field.
809
810 score:   The articles score
811 default: The default article score.
812 below:   The score below which articles are automatically marked as read.
813 mark:    The articles mark."
814   :group 'gnus-summary-visual
815   :type '(repeat (cons (sexp :tag "Form" nil)
816                        face)))
817
818 (defcustom gnus-alter-header-function nil
819   "Function called to allow alteration of article header structures.
820 The function is called with one parameter, the article header vector,
821 which it may alter in any way.")
822
823 (defvar gnus-decode-encoded-word-function
824   (mime-find-field-decoder 'From 'nov)
825   "Variable that says which function should be used to decode a string with encoded words.")
826
827 (defcustom gnus-extra-headers nil
828   "*Extra headers to parse."
829   :group 'gnus-summary
830   :type '(repeat symbol))
831
832 (defcustom gnus-ignored-from-addresses
833   (and user-mail-address (regexp-quote user-mail-address))
834   "*Regexp of From headers that may be suppressed in favor of To headers."
835   :group 'gnus-summary
836   :type 'regexp)
837
838 (defcustom gnus-group-charset-alist
839   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
840     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
841     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
842     ("^relcom\\>" koi8-r)
843     ("^fido7\\>" koi8-r)
844     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
845     ("^israel\\>" iso-8859-1)
846     ("^han\\>" euc-kr)
847     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
848     (".*" iso-8859-1))
849   "Alist of regexps (to match group names) and default charsets to be used when reading."
850   :type '(repeat (list (regexp :tag "Group")
851                        (symbol :tag "Charset")))
852   :group 'gnus-charset)
853
854 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
855   "List of charsets that should be ignored.
856 When these charsets are used in the \"charset\" parameter, the
857 default charset will be used instead."
858   :type '(repeat symbol)
859   :group 'gnus-charset)
860
861 (defcustom gnus-group-ignored-charsets-alist 
862   '(("alt\\.chinese\\.text" iso-8859-1))
863   "Alist of regexps (to match group names) and charsets that should be ignored.
864 When these charsets are used in the \"charset\" parameter, the
865 default charset will be used instead."
866   :type '(repeat (cons (regexp :tag "Group")
867                        (repeat symbol)))
868   :group 'gnus-charset)
869
870 (defcustom gnus-group-highlight-words-alist nil
871   "Alist of group regexps and highlight regexps.
872 This variable uses the same syntax as `gnus-emphasis-alist'."
873   :type '(repeat (cons (regexp :tag "Group")
874                        (repeat (list (regexp :tag "Highlight regexp")
875                                      (number :tag "Group for entire word" 0)
876                                      (number :tag "Group for displayed part" 0)
877                                      (symbol :tag "Face" 
878                                              gnus-emphasis-highlight-words)))))
879   :group 'gnus-summary-visual)
880
881 (defcustom gnus-use-wheel nil
882   "Use Intelli-mouse on summary movement"
883   :type 'boolean
884   :group 'gnus-summary-maneuvering)
885
886 (defcustom gnus-wheel-scroll-amount '(5 . 1)
887   "Amount to scroll messages by spinning the mouse wheel.
888 This is actually a cons cell, where the first item is the amount to scroll
889 on a normal wheel event, and the second is the amount to scroll when the
890 wheel is moved with the shift key depressed."
891   :type '(cons (integer :tag "Shift") integer)
892   :group 'gnus-summary-maneuvering)
893
894 (defcustom gnus-wheel-edge-resistance 2
895   "How hard it should be to change the current article
896 by moving the mouse over the edge of the article window."
897   :type 'integer
898   :group 'gnus-summary-maneuvering)
899
900 (defcustom gnus-summary-show-article-charset-alist
901   nil
902   "Alist of number and charset.
903 The article will be shown with the charset corresponding to the
904 numbered argument.
905 For example: ((1 . cn-gb-2312) (2 . big5))."
906   :type '(repeat (cons (number :tag "Argument" 1)
907                        (symbol :tag "Charset")))
908   :group 'gnus-charset)
909
910
911 ;;; Internal variables
912
913 (defvar gnus-scores-exclude-files nil)
914 (defvar gnus-page-broken nil)
915 (defvar gnus-inhibit-mime-unbuttonizing nil)
916
917 (defvar gnus-original-article nil)
918 (defvar gnus-article-internal-prepare-hook nil)
919 (defvar gnus-newsgroup-process-stack nil)
920
921 (defvar gnus-thread-indent-array nil)
922 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
923 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
924   "Function called to sort the articles within a thread after it has 
925 been gathered together.")
926
927 ;; Avoid highlighting in kill files.
928 (defvar gnus-summary-inhibit-highlight nil)
929 (defvar gnus-newsgroup-selected-overlay nil)
930 (defvar gnus-inhibit-limiting nil)
931 (defvar gnus-newsgroup-adaptive-score-file nil)
932 (defvar gnus-current-score-file nil)
933 (defvar gnus-current-move-group nil)
934 (defvar gnus-current-copy-group nil)
935 (defvar gnus-current-crosspost-group nil)
936
937 (defvar gnus-newsgroup-dependencies nil)
938 (defvar gnus-newsgroup-adaptive nil)
939 (defvar gnus-summary-display-article-function nil)
940 (defvar gnus-summary-highlight-line-function nil
941   "Function called after highlighting a summary line.")
942
943 (defvar gnus-summary-line-format-alist
944   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
945     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
946     (?s gnus-tmp-subject-or-nil ?s)
947     (?n gnus-tmp-name ?s)
948     (?A (std11-address-string
949          (car (mime-read-field 'From gnus-tmp-header))) ?s)
950     (?a (or (std11-full-name-string
951              (car (mime-read-field 'From gnus-tmp-header)))
952             gnus-tmp-from) ?s)
953     (?F gnus-tmp-from ?s)
954     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
955     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
956     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
957     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
958     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
959     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
960     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
961     (?L gnus-tmp-lines ?d)
962     (?I gnus-tmp-indentation ?s)
963     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
964     (?R gnus-tmp-replied ?c)
965     (?\[ gnus-tmp-opening-bracket ?c)
966     (?\] gnus-tmp-closing-bracket ?c)
967     (?\> (make-string gnus-tmp-level ? ) ?s)
968     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
969     (?i gnus-tmp-score ?d)
970     (?z gnus-tmp-score-char ?c)
971     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
972     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
973     (?U gnus-tmp-unread ?c)
974     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
975     (?t (gnus-summary-number-of-articles-in-thread
976          (and (boundp 'thread) (car thread)) gnus-tmp-level)
977         ?d)
978     (?e (gnus-summary-number-of-articles-in-thread
979          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
980         ?c)
981     (?u gnus-tmp-user-defined ?s)
982     (?P (gnus-pick-line-number) ?d))
983   "An alist of format specifications that can appear in summary lines,
984 and what variables they correspond with, along with the type of the
985 variable (string, integer, character, etc).")
986
987 (defvar gnus-summary-dummy-line-format-alist
988   `((?S gnus-tmp-subject ?s)
989     (?N gnus-tmp-number ?d)
990     (?u gnus-tmp-user-defined ?s)))
991
992 (defvar gnus-summary-mode-line-format-alist
993   `((?G gnus-tmp-group-name ?s)
994     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
995     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
996     (?A gnus-tmp-article-number ?d)
997     (?Z gnus-tmp-unread-and-unselected ?s)
998     (?V gnus-version ?s)
999     (?U gnus-tmp-unread-and-unticked ?d)
1000     (?S gnus-tmp-subject ?s)
1001     (?e gnus-tmp-unselected ?d)
1002     (?u gnus-tmp-user-defined ?s)
1003     (?d (length gnus-newsgroup-dormant) ?d)
1004     (?t (length gnus-newsgroup-marked) ?d)
1005     (?r (length gnus-newsgroup-reads) ?d)
1006     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1007     (?E gnus-newsgroup-expunged-tally ?d)
1008     (?s (gnus-current-score-file-nondirectory) ?s)))
1009
1010 (defvar gnus-last-search-regexp nil
1011   "Default regexp for article search command.")
1012
1013 (defvar gnus-summary-search-article-matched-data nil
1014   "Last matched data of article search command.  It is the local variable
1015 in `gnus-article-buffer' which consists of the list of start position,
1016 end position and text.")
1017
1018 (defvar gnus-last-shell-command nil
1019   "Default shell command on article.")
1020
1021 (defvar gnus-newsgroup-begin nil)
1022 (defvar gnus-newsgroup-end nil)
1023 (defvar gnus-newsgroup-last-rmail nil)
1024 (defvar gnus-newsgroup-last-mail nil)
1025 (defvar gnus-newsgroup-last-folder nil)
1026 (defvar gnus-newsgroup-last-file nil)
1027 (defvar gnus-newsgroup-auto-expire nil)
1028 (defvar gnus-newsgroup-active nil)
1029
1030 (defvar gnus-newsgroup-data nil)
1031 (defvar gnus-newsgroup-data-reverse nil)
1032 (defvar gnus-newsgroup-limit nil)
1033 (defvar gnus-newsgroup-limits nil)
1034
1035 (defvar gnus-newsgroup-unreads nil
1036   "List of unread articles in the current newsgroup.")
1037
1038 (defvar gnus-newsgroup-unselected nil
1039   "List of unselected unread articles in the current newsgroup.")
1040
1041 (defvar gnus-newsgroup-reads nil
1042   "Alist of read articles and article marks in the current newsgroup.")
1043
1044 (defvar gnus-newsgroup-expunged-tally nil)
1045
1046 (defvar gnus-newsgroup-marked nil
1047   "List of ticked articles in the current newsgroup (a subset of unread art).")
1048
1049 (defvar gnus-newsgroup-killed nil
1050   "List of ranges of articles that have been through the scoring process.")
1051
1052 (defvar gnus-newsgroup-cached nil
1053   "List of articles that come from the article cache.")
1054
1055 (defvar gnus-newsgroup-saved nil
1056   "List of articles that have been saved.")
1057
1058 (defvar gnus-newsgroup-kill-headers nil)
1059
1060 (defvar gnus-newsgroup-replied nil
1061   "List of articles that have been replied to in the current newsgroup.")
1062
1063 (defvar gnus-newsgroup-expirable nil
1064   "List of articles in the current newsgroup that can be expired.")
1065
1066 (defvar gnus-newsgroup-processable nil
1067   "List of articles in the current newsgroup that can be processed.")
1068
1069 (defvar gnus-newsgroup-downloadable nil
1070   "List of articles in the current newsgroup that can be processed.")
1071
1072 (defvar gnus-newsgroup-undownloaded nil
1073   "List of articles in the current newsgroup that haven't been downloaded..")
1074
1075 (defvar gnus-newsgroup-unsendable nil
1076   "List of articles in the current newsgroup that won't be sent.")
1077
1078 (defvar gnus-newsgroup-bookmarks nil
1079   "List of articles in the current newsgroup that have bookmarks.")
1080
1081 (defvar gnus-newsgroup-dormant nil
1082   "List of dormant articles in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-scored nil
1085   "List of scored articles in the current newsgroup.")
1086
1087 (defvar gnus-newsgroup-incorporated nil
1088   "List of incorporated articles in the current newsgroup.")
1089
1090 (defvar gnus-newsgroup-headers nil
1091   "List of article headers in the current newsgroup.")
1092
1093 (defvar gnus-newsgroup-threads nil)
1094
1095 (defvar gnus-newsgroup-prepared nil
1096   "Whether the current group has been prepared properly.")
1097
1098 (defvar gnus-newsgroup-ancient nil
1099   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1100
1101 (defvar gnus-newsgroup-sparse nil)
1102
1103 (defvar gnus-current-article nil)
1104 (defvar gnus-article-current nil)
1105 (defvar gnus-current-headers nil)
1106 (defvar gnus-have-all-headers nil)
1107 (defvar gnus-last-article nil)
1108 (defvar gnus-newsgroup-history nil)
1109 (defvar gnus-newsgroup-charset nil)
1110 (defvar gnus-newsgroup-ephemeral-charset nil)
1111 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1112
1113 (defconst gnus-summary-local-variables
1114   '(gnus-newsgroup-name
1115     gnus-newsgroup-begin gnus-newsgroup-end
1116     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1117     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1118     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1119     gnus-newsgroup-unselected gnus-newsgroup-marked
1120     gnus-newsgroup-reads gnus-newsgroup-saved
1121     gnus-newsgroup-replied gnus-newsgroup-expirable
1122     gnus-newsgroup-processable gnus-newsgroup-killed
1123     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1124     gnus-newsgroup-unsendable
1125     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1126     gnus-newsgroup-headers gnus-newsgroup-threads
1127     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1128     gnus-current-article gnus-current-headers gnus-have-all-headers
1129     gnus-last-article gnus-article-internal-prepare-hook
1130     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1131     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1132     gnus-thread-expunge-below
1133     gnus-score-alist gnus-current-score-file
1134     (gnus-summary-expunge-below . global)
1135     (gnus-summary-mark-below . global)
1136     gnus-newsgroup-active gnus-scores-exclude-files
1137     gnus-newsgroup-history gnus-newsgroup-ancient
1138     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1139     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1140     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1141     (gnus-newsgroup-expunged-tally . 0)
1142     gnus-cache-removable-articles gnus-newsgroup-cached
1143     gnus-newsgroup-data gnus-newsgroup-data-reverse
1144     gnus-newsgroup-limit gnus-newsgroup-limits
1145     gnus-newsgroup-charset
1146     gnus-newsgroup-incorporated)
1147   "Variables that are buffer-local to the summary buffers.")
1148
1149 ;; Byte-compiler warning.
1150 (defvar gnus-article-mode-map)
1151
1152 ;; Subject simplification.
1153
1154 (defun gnus-simplify-whitespace (str)
1155   "Remove excessive whitespace."
1156   (let ((mystr str))
1157     ;; Multiple spaces.
1158     (while (string-match "[ \t][ \t]+" mystr)
1159       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1160                           " "
1161                           (substring mystr (match-end 0)))))
1162     ;; Leading spaces.
1163     (when (string-match "^[ \t]+" mystr)
1164       (setq mystr (substring mystr (match-end 0))))
1165     ;; Trailing spaces.
1166     (when (string-match "[ \t]+$" mystr)
1167       (setq mystr (substring mystr 0 (match-beginning 0))))
1168     mystr))
1169
1170 (defsubst gnus-simplify-subject-re (subject)
1171   "Remove \"Re:\" from subject lines."
1172   (if (string-match "^[Rr][Ee]: *" subject)
1173       (substring subject (match-end 0))
1174     subject))
1175
1176 (defun gnus-simplify-subject (subject &optional re-only)
1177   "Remove `Re:' and words in parentheses.
1178 If RE-ONLY is non-nil, strip leading `Re:'s only."
1179   (let ((case-fold-search t))           ;Ignore case.
1180     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1181     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1182       (setq subject (substring subject (match-end 0))))
1183     ;; Remove uninteresting prefixes.
1184     (when (and (not re-only)
1185                gnus-simplify-ignored-prefixes
1186                (string-match gnus-simplify-ignored-prefixes subject))
1187       (setq subject (substring subject (match-end 0))))
1188     ;; Remove words in parentheses from end.
1189     (unless re-only
1190       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1191         (setq subject (substring subject 0 (match-beginning 0)))))
1192     ;; Return subject string.
1193     subject))
1194
1195 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1196 ;; all whitespace.
1197 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1198   (goto-char (point-min))
1199   (while (re-search-forward regexp nil t)
1200       (replace-match (or newtext ""))))
1201
1202 (defun gnus-simplify-buffer-fuzzy ()
1203   "Simplify string in the buffer fuzzily.
1204 The string in the accessible portion of the current buffer is simplified.
1205 It is assumed to be a single-line subject.
1206 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1207 matter is removed.  Additional things can be deleted by setting
1208 gnus-simplify-subject-fuzzy-regexp."
1209   (let ((case-fold-search t)
1210         (modified-tick))
1211     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1212
1213     (while (not (eq modified-tick (buffer-modified-tick)))
1214       (setq modified-tick (buffer-modified-tick))
1215       (cond
1216        ((listp gnus-simplify-subject-fuzzy-regexp)
1217         (mapcar 'gnus-simplify-buffer-fuzzy-step
1218                 gnus-simplify-subject-fuzzy-regexp))
1219        (gnus-simplify-subject-fuzzy-regexp
1220         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1221       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1222       (gnus-simplify-buffer-fuzzy-step
1223        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1224       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1225
1226     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1227     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1228     (gnus-simplify-buffer-fuzzy-step " $")
1229     (gnus-simplify-buffer-fuzzy-step "^ +")))
1230
1231 (defun gnus-simplify-subject-fuzzy (subject)
1232   "Simplify a subject string fuzzily.
1233 See `gnus-simplify-buffer-fuzzy' for details."
1234   (save-excursion
1235     (gnus-set-work-buffer)
1236     (let ((case-fold-search t))
1237       ;; Remove uninteresting prefixes.
1238       (when (and gnus-simplify-ignored-prefixes
1239                  (string-match gnus-simplify-ignored-prefixes subject))
1240         (setq subject (substring subject (match-end 0))))
1241       (insert subject)
1242       (inline (gnus-simplify-buffer-fuzzy))
1243       (buffer-string))))
1244
1245 (defsubst gnus-simplify-subject-fully (subject)
1246   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1247   (cond
1248    (gnus-simplify-subject-functions
1249     (gnus-map-function gnus-simplify-subject-functions subject))
1250    ((null gnus-summary-gather-subject-limit)
1251     (gnus-simplify-subject-re subject))
1252    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1253     (gnus-simplify-subject-fuzzy subject))
1254    ((numberp gnus-summary-gather-subject-limit)
1255     (gnus-limit-string (gnus-simplify-subject-re subject)
1256                        gnus-summary-gather-subject-limit))
1257    (t
1258     subject)))
1259
1260 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1261   "Check whether two subjects are equal.
1262 If optional argument simple-first is t, first argument is already
1263 simplified."
1264   (cond
1265    ((null simple-first)
1266     (equal (gnus-simplify-subject-fully s1)
1267            (gnus-simplify-subject-fully s2)))
1268    (t
1269     (equal s1
1270            (gnus-simplify-subject-fully s2)))))
1271
1272 (defun gnus-summary-bubble-group ()
1273   "Increase the score of the current group.
1274 This is a handy function to add to `gnus-summary-exit-hook' to
1275 increase the score of each group you read."
1276   (gnus-group-add-score gnus-newsgroup-name))
1277
1278 \f
1279 ;;;
1280 ;;; Gnus summary mode
1281 ;;;
1282
1283 (put 'gnus-summary-mode 'mode-class 'special)
1284
1285 (when t
1286   ;; Non-orthogonal keys
1287
1288   (gnus-define-keys gnus-summary-mode-map
1289     " " gnus-summary-next-page
1290     "\177" gnus-summary-prev-page
1291     [delete] gnus-summary-prev-page
1292     [backspace] gnus-summary-prev-page
1293     "\r" gnus-summary-scroll-up
1294     "\M-\r" gnus-summary-scroll-down
1295     "n" gnus-summary-next-unread-article
1296     "p" gnus-summary-prev-unread-article
1297     "N" gnus-summary-next-article
1298     "P" gnus-summary-prev-article
1299     "\M-\C-n" gnus-summary-next-same-subject
1300     "\M-\C-p" gnus-summary-prev-same-subject
1301     "\M-n" gnus-summary-next-unread-subject
1302     "\M-p" gnus-summary-prev-unread-subject
1303     "." gnus-summary-first-unread-article
1304     "," gnus-summary-best-unread-article
1305     "\M-s" gnus-summary-search-article-forward
1306     "\M-r" gnus-summary-search-article-backward
1307     "<" gnus-summary-beginning-of-article
1308     ">" gnus-summary-end-of-article
1309     "j" gnus-summary-goto-article
1310     "^" gnus-summary-refer-parent-article
1311     "\M-^" gnus-summary-refer-article
1312     "u" gnus-summary-tick-article-forward
1313     "!" gnus-summary-tick-article-forward
1314     "U" gnus-summary-tick-article-backward
1315     "d" gnus-summary-mark-as-read-forward
1316     "D" gnus-summary-mark-as-read-backward
1317     "E" gnus-summary-mark-as-expirable
1318     "\M-u" gnus-summary-clear-mark-forward
1319     "\M-U" gnus-summary-clear-mark-backward
1320     "k" gnus-summary-kill-same-subject-and-select
1321     "\C-k" gnus-summary-kill-same-subject
1322     "\M-\C-k" gnus-summary-kill-thread
1323     "\M-\C-l" gnus-summary-lower-thread
1324     "e" gnus-summary-edit-article
1325     "#" gnus-summary-mark-as-processable
1326     "\M-#" gnus-summary-unmark-as-processable
1327     "\M-\C-t" gnus-summary-toggle-threads
1328     "\M-\C-s" gnus-summary-show-thread
1329     "\M-\C-h" gnus-summary-hide-thread
1330     "\M-\C-f" gnus-summary-next-thread
1331     "\M-\C-b" gnus-summary-prev-thread
1332     "\M-\C-u" gnus-summary-up-thread
1333     "\M-\C-d" gnus-summary-down-thread
1334     "&" gnus-summary-execute-command
1335     "c" gnus-summary-catchup-and-exit
1336     "\C-w" gnus-summary-mark-region-as-read
1337     "\C-t" gnus-summary-toggle-truncation
1338     "?" gnus-summary-mark-as-dormant
1339     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1340     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1341     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1342     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1343     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1344     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1345     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1346     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1347     "=" gnus-summary-expand-window
1348     "\C-x\C-s" gnus-summary-reselect-current-group
1349     "\M-g" gnus-summary-rescan-group
1350     "w" gnus-summary-stop-page-breaking
1351     "\C-c\C-r" gnus-summary-caesar-message
1352     "\M-t" gnus-summary-toggle-mime
1353     "f" gnus-summary-followup
1354     "F" gnus-summary-followup-with-original
1355     "C" gnus-summary-cancel-article
1356     "r" gnus-summary-reply
1357     "R" gnus-summary-reply-with-original
1358     "\C-c\C-f" gnus-summary-mail-forward
1359     "o" gnus-summary-save-article
1360     "\C-o" gnus-summary-save-article-mail
1361     "|" gnus-summary-pipe-output
1362     "\M-k" gnus-summary-edit-local-kill
1363     "\M-K" gnus-summary-edit-global-kill
1364     ;; "V" gnus-version
1365     "\C-c\C-d" gnus-summary-describe-group
1366     "q" gnus-summary-exit
1367     "Q" gnus-summary-exit-no-update
1368     "\C-c\C-i" gnus-info-find-node
1369     gnus-mouse-2 gnus-mouse-pick-article
1370     "m" gnus-summary-mail-other-window
1371     "a" gnus-summary-post-news
1372     "x" gnus-summary-limit-to-unread
1373     "s" gnus-summary-isearch-article
1374     "t" gnus-article-toggle-headers
1375     "g" gnus-summary-show-article
1376     "l" gnus-summary-goto-last-article
1377     "v" gnus-summary-preview-mime-message
1378     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1379     "\C-d" gnus-summary-enter-digest-group
1380     "\M-\C-d" gnus-summary-read-document
1381     "\M-\C-e" gnus-summary-edit-parameters
1382     "\M-\C-g" gnus-summary-customize-parameters
1383     "\C-c\C-b" gnus-bug
1384     "*" gnus-cache-enter-article
1385     "\M-*" gnus-cache-remove-article
1386     "\M-&" gnus-summary-universal-argument
1387     "\C-l" gnus-recenter
1388     "I" gnus-summary-increase-score
1389     "L" gnus-summary-lower-score
1390     "\M-i" gnus-symbolic-argument
1391     "h" gnus-summary-select-article-buffer
1392
1393     "V" gnus-summary-score-map
1394     "X" gnus-uu-extract-map
1395     "S" gnus-summary-send-map)
1396
1397   ;; Sort of orthogonal keymap
1398   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1399     "t" gnus-summary-tick-article-forward
1400     "!" gnus-summary-tick-article-forward
1401     "d" gnus-summary-mark-as-read-forward
1402     "r" gnus-summary-mark-as-read-forward
1403     "c" gnus-summary-clear-mark-forward
1404     " " gnus-summary-clear-mark-forward
1405     "e" gnus-summary-mark-as-expirable
1406     "x" gnus-summary-mark-as-expirable
1407     "?" gnus-summary-mark-as-dormant
1408     "b" gnus-summary-set-bookmark
1409     "B" gnus-summary-remove-bookmark
1410     "#" gnus-summary-mark-as-processable
1411     "\M-#" gnus-summary-unmark-as-processable
1412     "S" gnus-summary-limit-include-expunged
1413     "C" gnus-summary-catchup
1414     "H" gnus-summary-catchup-to-here
1415     "\C-c" gnus-summary-catchup-all
1416     "k" gnus-summary-kill-same-subject-and-select
1417     "K" gnus-summary-kill-same-subject
1418     "P" gnus-uu-mark-map)
1419
1420   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1421     "c" gnus-summary-clear-above
1422     "u" gnus-summary-tick-above
1423     "m" gnus-summary-mark-above
1424     "k" gnus-summary-kill-below)
1425
1426   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1427     "/" gnus-summary-limit-to-subject
1428     "n" gnus-summary-limit-to-articles
1429     "w" gnus-summary-pop-limit
1430     "s" gnus-summary-limit-to-subject
1431     "a" gnus-summary-limit-to-author
1432     "u" gnus-summary-limit-to-unread
1433     "m" gnus-summary-limit-to-marks
1434     "M" gnus-summary-limit-exclude-marks
1435     "v" gnus-summary-limit-to-score
1436     "*" gnus-summary-limit-include-cached
1437     "D" gnus-summary-limit-include-dormant
1438     "T" gnus-summary-limit-include-thread
1439     "d" gnus-summary-limit-exclude-dormant
1440     "t" gnus-summary-limit-to-age
1441     "x" gnus-summary-limit-to-extra 
1442     "E" gnus-summary-limit-include-expunged
1443     "c" gnus-summary-limit-exclude-childless-dormant
1444     "C" gnus-summary-limit-mark-excluded-as-read)
1445
1446   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1447     "n" gnus-summary-next-unread-article
1448     "p" gnus-summary-prev-unread-article
1449     "N" gnus-summary-next-article
1450     "P" gnus-summary-prev-article
1451     "\C-n" gnus-summary-next-same-subject
1452     "\C-p" gnus-summary-prev-same-subject
1453     "\M-n" gnus-summary-next-unread-subject
1454     "\M-p" gnus-summary-prev-unread-subject
1455     "f" gnus-summary-first-unread-article
1456     "b" gnus-summary-best-unread-article
1457     "j" gnus-summary-goto-article
1458     "g" gnus-summary-goto-subject
1459     "l" gnus-summary-goto-last-article
1460     "o" gnus-summary-pop-article)
1461
1462   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1463     "k" gnus-summary-kill-thread
1464     "l" gnus-summary-lower-thread
1465     "i" gnus-summary-raise-thread
1466     "T" gnus-summary-toggle-threads
1467     "t" gnus-summary-rethread-current
1468     "^" gnus-summary-reparent-thread
1469     "s" gnus-summary-show-thread
1470     "S" gnus-summary-show-all-threads
1471     "h" gnus-summary-hide-thread
1472     "H" gnus-summary-hide-all-threads
1473     "n" gnus-summary-next-thread
1474     "p" gnus-summary-prev-thread
1475     "u" gnus-summary-up-thread
1476     "o" gnus-summary-top-thread
1477     "d" gnus-summary-down-thread
1478     "#" gnus-uu-mark-thread
1479     "\M-#" gnus-uu-unmark-thread)
1480
1481   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1482     "g" gnus-summary-prepare
1483     "c" gnus-summary-insert-cached-articles)
1484
1485   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1486     "c" gnus-summary-catchup-and-exit
1487     "C" gnus-summary-catchup-all-and-exit
1488     "E" gnus-summary-exit-no-update
1489     "J" gnus-summary-jump-to-other-group
1490     "Q" gnus-summary-exit
1491     "Z" gnus-summary-exit
1492     "n" gnus-summary-catchup-and-goto-next-group
1493     "R" gnus-summary-reselect-current-group
1494     "G" gnus-summary-rescan-group
1495     "N" gnus-summary-next-group
1496     "s" gnus-summary-save-newsrc
1497     "P" gnus-summary-prev-group)
1498
1499   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1500     " " gnus-summary-next-page
1501     "n" gnus-summary-next-page
1502     "\177" gnus-summary-prev-page
1503     [delete] gnus-summary-prev-page
1504     "p" gnus-summary-prev-page
1505     "\r" gnus-summary-scroll-up
1506     "\M-\r" gnus-summary-scroll-down
1507     "<" gnus-summary-beginning-of-article
1508     ">" gnus-summary-end-of-article
1509     "b" gnus-summary-beginning-of-article
1510     "e" gnus-summary-end-of-article
1511     "^" gnus-summary-refer-parent-article
1512     "r" gnus-summary-refer-parent-article
1513     "D" gnus-summary-enter-digest-group
1514     "R" gnus-summary-refer-references
1515     "T" gnus-summary-refer-thread
1516     "g" gnus-summary-show-article
1517     "s" gnus-summary-isearch-article
1518     "P" gnus-summary-print-article
1519     "t" gnus-article-babel)
1520
1521   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1522     "b" gnus-article-add-buttons
1523     "B" gnus-article-add-buttons-to-head
1524     "o" gnus-article-treat-overstrike
1525     "e" gnus-article-emphasize
1526     "w" gnus-article-fill-cited-article
1527     "Q" gnus-article-fill-long-lines
1528     "C" gnus-article-capitalize-sentences
1529     "c" gnus-article-remove-cr
1530     "f" gnus-article-display-x-face
1531     "l" gnus-summary-stop-page-breaking
1532     "r" gnus-summary-caesar-message
1533     "t" gnus-article-toggle-headers
1534     "v" gnus-summary-verbose-headers
1535     "m" gnus-summary-toggle-mime
1536     "H" gnus-article-strip-headers-in-body
1537     "d" gnus-article-treat-dumbquotes
1538     "s" gnus-smiley-display)
1539
1540   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1541     "a" gnus-article-hide
1542     "h" gnus-article-toggle-headers
1543     "b" gnus-article-hide-boring-headers
1544     "s" gnus-article-hide-signature
1545     "c" gnus-article-hide-citation
1546     "C" gnus-article-hide-citation-in-followups
1547     "l" gnus-article-hide-list-identifiers
1548     "p" gnus-article-hide-pgp
1549     "B" gnus-article-strip-banner
1550     "P" gnus-article-hide-pem
1551     "\C-c" gnus-article-hide-citation-maybe)
1552
1553   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1554     "a" gnus-article-highlight
1555     "h" gnus-article-highlight-headers
1556     "c" gnus-article-highlight-citation
1557     "s" gnus-article-highlight-signature)
1558
1559   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1560     "z" gnus-article-date-ut
1561     "u" gnus-article-date-ut
1562     "l" gnus-article-date-local
1563     "e" gnus-article-date-lapsed
1564     "o" gnus-article-date-original
1565     "i" gnus-article-date-iso8601
1566     "s" gnus-article-date-user)
1567
1568   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1569     "t" gnus-article-remove-trailing-blank-lines
1570     "l" gnus-article-strip-leading-blank-lines
1571     "m" gnus-article-strip-multiple-blank-lines
1572     "a" gnus-article-strip-blank-lines
1573     "A" gnus-article-strip-all-blank-lines
1574     "s" gnus-article-strip-leading-space
1575     "e" gnus-article-strip-trailing-space)
1576
1577   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1578     "v" gnus-version
1579     "f" gnus-summary-fetch-faq
1580     "d" gnus-summary-describe-group
1581     "h" gnus-summary-describe-briefly
1582     "i" gnus-info-find-node)
1583
1584   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1585     "e" gnus-summary-expire-articles
1586     "\M-\C-e" gnus-summary-expire-articles-now
1587     "\177" gnus-summary-delete-article
1588     [delete] gnus-summary-delete-article
1589     [backspace] gnus-summary-delete-article
1590     "m" gnus-summary-move-article
1591     "r" gnus-summary-respool-article
1592     "w" gnus-summary-edit-article
1593     "c" gnus-summary-copy-article
1594     "B" gnus-summary-crosspost-article
1595     "q" gnus-summary-respool-query
1596     "t" gnus-summary-respool-trace
1597     "i" gnus-summary-import-article
1598     "p" gnus-summary-article-posted-p)
1599
1600   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1601     "o" gnus-summary-save-article
1602     "m" gnus-summary-save-article-mail
1603     "F" gnus-summary-write-article-file
1604     "r" gnus-summary-save-article-rmail
1605     "f" gnus-summary-save-article-file
1606     "b" gnus-summary-save-article-body-file
1607     "h" gnus-summary-save-article-folder
1608     "v" gnus-summary-save-article-vm
1609     "p" gnus-summary-pipe-output
1610     "s" gnus-soup-add-article)
1611
1612   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1613     "b" gnus-summary-display-buttonized
1614     "m" gnus-summary-repair-multipart
1615     "v" gnus-article-view-part
1616     "o" gnus-article-save-part
1617     "c" gnus-article-copy-part
1618     "e" gnus-article-externalize-part
1619     "i" gnus-article-inline-part
1620     "|" gnus-article-pipe-part)
1621   )
1622
1623 (defun gnus-summary-make-menu-bar ()
1624   (gnus-turn-off-edit-menu 'summary)
1625
1626   (unless (boundp 'gnus-summary-misc-menu)
1627
1628     (easy-menu-define
1629      gnus-summary-kill-menu gnus-summary-mode-map ""
1630      (cons
1631       "Score"
1632       (nconc
1633        (list
1634         ["Enter score..." gnus-summary-score-entry t]
1635         ["Customize" gnus-score-customize t])
1636        (gnus-make-score-map 'increase)
1637        (gnus-make-score-map 'lower)
1638        '(("Mark"
1639           ["Kill below" gnus-summary-kill-below t]
1640           ["Mark above" gnus-summary-mark-above t]
1641           ["Tick above" gnus-summary-tick-above t]
1642           ["Clear above" gnus-summary-clear-above t])
1643          ["Current score" gnus-summary-current-score t]
1644          ["Set score" gnus-summary-set-score t]
1645          ["Switch current score file..." gnus-score-change-score-file t]
1646          ["Set mark below..." gnus-score-set-mark-below t]
1647          ["Set expunge below..." gnus-score-set-expunge-below t]
1648          ["Edit current score file" gnus-score-edit-current-scores t]
1649          ["Edit score file" gnus-score-edit-file t]
1650          ["Trace score" gnus-score-find-trace t]
1651          ["Find words" gnus-score-find-favourite-words t]
1652          ["Rescore buffer" gnus-summary-rescore t]
1653          ["Increase score..." gnus-summary-increase-score t]
1654          ["Lower score..." gnus-summary-lower-score t]))))
1655
1656     ;; Define both the Article menu in the summary buffer and the equivalent
1657     ;; Commands menu in the article buffer here for consistency.
1658     (let ((innards
1659            '(("Hide"
1660               ["All" gnus-article-hide t]
1661               ["Headers" gnus-article-toggle-headers t]
1662               ["Signature" gnus-article-hide-signature t]
1663               ["Citation" gnus-article-hide-citation t]
1664               ["List identifiers" gnus-article-hide-list-identifiers t]
1665               ["PGP" gnus-article-hide-pgp t]
1666               ["Banner" gnus-article-strip-banner t]
1667               ["Boring headers" gnus-article-hide-boring-headers t])
1668              ("Highlight"
1669               ["All" gnus-article-highlight t]
1670               ["Headers" gnus-article-highlight-headers t]
1671               ["Signature" gnus-article-highlight-signature t]
1672               ["Citation" gnus-article-highlight-citation t])
1673              ("Date"
1674               ["Local" gnus-article-date-local t]
1675               ["ISO8601" gnus-article-date-iso8601 t]
1676               ["UT" gnus-article-date-ut t]
1677               ["Original" gnus-article-date-original t]
1678               ["Lapsed" gnus-article-date-lapsed t]
1679               ["User-defined" gnus-article-date-user t])
1680              ("Washing"
1681               ("Remove Blanks"
1682                ["Leading" gnus-article-strip-leading-blank-lines t]
1683                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1684                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1685                ["All of the above" gnus-article-strip-blank-lines t]
1686                ["All" gnus-article-strip-all-blank-lines t]
1687                ["Leading space" gnus-article-strip-leading-space t]
1688                ["Trailing space" gnus-article-strip-trailing-space t])
1689               ["Overstrike" gnus-article-treat-overstrike t]
1690               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1691               ["Emphasis" gnus-article-emphasize t]
1692               ["Word wrap" gnus-article-fill-cited-article t]
1693               ["Fill long lines" gnus-article-fill-long-lines t]
1694               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1695               ["CR" gnus-article-remove-cr t]
1696               ["Show X-Face" gnus-article-display-x-face t]
1697               ["Rot 13" gnus-summary-caesar-message t]
1698               ["Unix pipe" gnus-summary-pipe-message t]
1699               ["Add buttons" gnus-article-add-buttons t]
1700               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1701               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1702               ["Toggle MIME" gnus-summary-toggle-mime t]
1703               ["Verbose header" gnus-summary-verbose-headers t]
1704               ["Toggle header" gnus-summary-toggle-header t]
1705               ["Toggle smileys" gnus-smiley-display t])
1706              ("Output"
1707               ["Save in default format" gnus-summary-save-article t]
1708               ["Save in file" gnus-summary-save-article-file t]
1709               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1710               ["Save in MH folder" gnus-summary-save-article-folder t]
1711               ["Save in VM folder" gnus-summary-save-article-vm t]
1712               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1713               ["Save body in file" gnus-summary-save-article-body-file t]
1714               ["Pipe through a filter" gnus-summary-pipe-output t]
1715               ["Add to SOUP packet" gnus-soup-add-article t]
1716               ["Print" gnus-summary-print-article t])
1717              ("Backend"
1718               ["Respool article..." gnus-summary-respool-article t]
1719               ["Move article..." gnus-summary-move-article
1720                (gnus-check-backend-function
1721                 'request-move-article gnus-newsgroup-name)]
1722               ["Copy article..." gnus-summary-copy-article t]
1723               ["Crosspost article..." gnus-summary-crosspost-article
1724                (gnus-check-backend-function
1725                 'request-replace-article gnus-newsgroup-name)]
1726               ["Import file..." gnus-summary-import-article t]
1727               ["Check if posted" gnus-summary-article-posted-p t]
1728               ["Edit article" gnus-summary-edit-article
1729                (not (gnus-group-read-only-p))]
1730               ["Delete article" gnus-summary-delete-article
1731                (gnus-check-backend-function
1732                 'request-expire-articles gnus-newsgroup-name)]
1733               ["Query respool" gnus-summary-respool-query t]
1734               ["Trace respool" gnus-summary-respool-trace t]
1735               ["Delete expirable articles" gnus-summary-expire-articles-now
1736                (gnus-check-backend-function
1737                 'request-expire-articles gnus-newsgroup-name)])
1738              ("Extract"
1739               ["Uudecode" gnus-uu-decode-uu t]
1740               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1741               ["Unshar" gnus-uu-decode-unshar t]
1742               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1743               ["Save" gnus-uu-decode-save t]
1744               ["Binhex" gnus-uu-decode-binhex t]
1745               ["Postscript" gnus-uu-decode-postscript t])
1746              ("Cache"
1747               ["Enter article" gnus-cache-enter-article t]
1748               ["Remove article" gnus-cache-remove-article t])
1749              ["Translate" gnus-article-babel t]
1750              ["Select article buffer" gnus-summary-select-article-buffer t]
1751              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1752              ["Isearch article..." gnus-summary-isearch-article t]
1753              ["Beginning of the article" gnus-summary-beginning-of-article t]
1754              ["End of the article" gnus-summary-end-of-article t]
1755              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1756              ["Fetch referenced articles" gnus-summary-refer-references t]
1757              ["Fetch current thread" gnus-summary-refer-thread t]
1758              ["Fetch article with id..." gnus-summary-refer-article t]
1759              ["Redisplay" gnus-summary-show-article t])))
1760       (easy-menu-define
1761        gnus-summary-article-menu gnus-summary-mode-map ""
1762        (cons "Article" innards))
1763
1764       (easy-menu-define
1765        gnus-article-commands-menu gnus-article-mode-map ""
1766        (cons "Commands" innards)))
1767
1768     (easy-menu-define
1769      gnus-summary-thread-menu gnus-summary-mode-map ""
1770      '("Threads"
1771        ["Toggle threading" gnus-summary-toggle-threads t]
1772        ["Hide threads" gnus-summary-hide-all-threads t]
1773        ["Show threads" gnus-summary-show-all-threads t]
1774        ["Hide thread" gnus-summary-hide-thread t]
1775        ["Show thread" gnus-summary-show-thread t]
1776        ["Go to next thread" gnus-summary-next-thread t]
1777        ["Go to previous thread" gnus-summary-prev-thread t]
1778        ["Go down thread" gnus-summary-down-thread t]
1779        ["Go up thread" gnus-summary-up-thread t]
1780        ["Top of thread" gnus-summary-top-thread t]
1781        ["Mark thread as read" gnus-summary-kill-thread t]
1782        ["Lower thread score" gnus-summary-lower-thread t]
1783        ["Raise thread score" gnus-summary-raise-thread t]
1784        ["Rethread current" gnus-summary-rethread-current t]
1785        ))
1786
1787     (easy-menu-define
1788      gnus-summary-post-menu gnus-summary-mode-map ""
1789      '("Post"
1790        ["Post an article" gnus-summary-post-news t]
1791        ["Followup" gnus-summary-followup t]
1792        ["Followup and yank" gnus-summary-followup-with-original t]
1793        ["Supersede article" gnus-summary-supersede-article t]
1794        ["Cancel article" gnus-summary-cancel-article t]
1795        ["Reply" gnus-summary-reply t]
1796        ["Reply and yank" gnus-summary-reply-with-original t]
1797        ["Wide reply" gnus-summary-wide-reply t]
1798        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1799        ["Mail forward" gnus-summary-mail-forward t]
1800        ["Post forward" gnus-summary-post-forward t]
1801        ["Digest and mail" gnus-summary-mail-digest t]
1802        ["Digest and post" gnus-summary-post-digest t]
1803        ["Resend message" gnus-summary-resend-message t]
1804        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1805        ["Send a mail" gnus-summary-mail-other-window t]
1806        ["Uuencode and post" gnus-uu-post-news t]
1807        ["Followup via news" gnus-summary-followup-to-mail t]
1808        ["Followup via news and yank"
1809         gnus-summary-followup-to-mail-with-original t]
1810        ;;("Draft"
1811        ;;["Send" gnus-summary-send-draft t]
1812        ;;["Send bounced" gnus-resend-bounced-mail t])
1813        ))
1814
1815     (easy-menu-define
1816      gnus-summary-misc-menu gnus-summary-mode-map ""
1817      '("Misc"
1818        ("Mark Read"
1819         ["Mark as read" gnus-summary-mark-as-read-forward t]
1820         ["Mark same subject and select"
1821          gnus-summary-kill-same-subject-and-select t]
1822         ["Mark same subject" gnus-summary-kill-same-subject t]
1823         ["Catchup" gnus-summary-catchup t]
1824         ["Catchup all" gnus-summary-catchup-all t]
1825         ["Catchup to here" gnus-summary-catchup-to-here t]
1826         ["Catchup region" gnus-summary-mark-region-as-read t]
1827         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1828        ("Mark Various"
1829         ["Tick" gnus-summary-tick-article-forward t]
1830         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1831         ["Remove marks" gnus-summary-clear-mark-forward t]
1832         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1833         ["Set bookmark" gnus-summary-set-bookmark t]
1834         ["Remove bookmark" gnus-summary-remove-bookmark t])
1835        ("Mark Limit"
1836         ["Marks..." gnus-summary-limit-to-marks t]
1837         ["Subject..." gnus-summary-limit-to-subject t]
1838         ["Author..." gnus-summary-limit-to-author t]
1839         ["Age..." gnus-summary-limit-to-age t]
1840         ["Extra..." gnus-summary-limit-to-extra t]
1841         ["Score" gnus-summary-limit-to-score t]
1842         ["Unread" gnus-summary-limit-to-unread t]
1843         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1844         ["Articles" gnus-summary-limit-to-articles t]
1845         ["Pop limit" gnus-summary-pop-limit t]
1846         ["Show dormant" gnus-summary-limit-include-dormant t]
1847         ["Hide childless dormant"
1848          gnus-summary-limit-exclude-childless-dormant t]
1849         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1850         ["Hide marked" gnus-summary-limit-exclude-marks t]
1851         ["Show expunged" gnus-summary-show-all-expunged t])
1852        ("Process Mark"
1853         ["Set mark" gnus-summary-mark-as-processable t]
1854         ["Remove mark" gnus-summary-unmark-as-processable t]
1855         ["Remove all marks" gnus-summary-unmark-all-processable t]
1856         ["Mark above" gnus-uu-mark-over t]
1857         ["Mark series" gnus-uu-mark-series t]
1858         ["Mark region" gnus-uu-mark-region t]
1859         ["Unmark region" gnus-uu-unmark-region t]
1860         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1861         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1862         ["Mark all" gnus-uu-mark-all t]
1863         ["Mark buffer" gnus-uu-mark-buffer t]
1864         ["Mark sparse" gnus-uu-mark-sparse t]
1865         ["Mark thread" gnus-uu-mark-thread t]
1866         ["Unmark thread" gnus-uu-unmark-thread t]
1867         ("Process Mark Sets"
1868          ["Kill" gnus-summary-kill-process-mark t]
1869          ["Yank" gnus-summary-yank-process-mark
1870           gnus-newsgroup-process-stack]
1871          ["Save" gnus-summary-save-process-mark t]))
1872        ("Scroll article"
1873         ["Page forward" gnus-summary-next-page t]
1874         ["Page backward" gnus-summary-prev-page t]
1875         ["Line forward" gnus-summary-scroll-up t])
1876        ("Move"
1877         ["Next unread article" gnus-summary-next-unread-article t]
1878         ["Previous unread article" gnus-summary-prev-unread-article t]
1879         ["Next article" gnus-summary-next-article t]
1880         ["Previous article" gnus-summary-prev-article t]
1881         ["Next unread subject" gnus-summary-next-unread-subject t]
1882         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1883         ["Next article same subject" gnus-summary-next-same-subject t]
1884         ["Previous article same subject" gnus-summary-prev-same-subject t]
1885         ["First unread article" gnus-summary-first-unread-article t]
1886         ["Best unread article" gnus-summary-best-unread-article t]
1887         ["Go to subject number..." gnus-summary-goto-subject t]
1888         ["Go to article number..." gnus-summary-goto-article t]
1889         ["Go to the last article" gnus-summary-goto-last-article t]
1890         ["Pop article off history" gnus-summary-pop-article t])
1891        ("Sort"
1892         ["Sort by number" gnus-summary-sort-by-number t]
1893         ["Sort by author" gnus-summary-sort-by-author t]
1894         ["Sort by subject" gnus-summary-sort-by-subject t]
1895         ["Sort by date" gnus-summary-sort-by-date t]
1896         ["Sort by score" gnus-summary-sort-by-score t]
1897         ["Sort by lines" gnus-summary-sort-by-lines t]
1898         ["Sort by characters" gnus-summary-sort-by-chars t])
1899        ("Help"
1900         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1901         ["Describe group" gnus-summary-describe-group t]
1902         ["Read manual" gnus-info-find-node t])
1903        ("Modes"
1904         ["Pick and read" gnus-pick-mode t]
1905         ["Binary" gnus-binary-mode t])
1906        ("Regeneration"
1907         ["Regenerate" gnus-summary-prepare t]
1908         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1909         ["Toggle threading" gnus-summary-toggle-threads t])
1910        ["Filter articles..." gnus-summary-execute-command t]
1911        ["Run command on subjects..." gnus-summary-universal-argument t]
1912        ["Search articles forward..." gnus-summary-search-article-forward t]
1913        ["Search articles backward..." gnus-summary-search-article-backward t]
1914        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1915        ["Expand window" gnus-summary-expand-window t]
1916        ["Expire expirable articles" gnus-summary-expire-articles
1917         (gnus-check-backend-function
1918          'request-expire-articles gnus-newsgroup-name)]
1919        ["Edit local kill file" gnus-summary-edit-local-kill t]
1920        ["Edit main kill file" gnus-summary-edit-global-kill t]
1921        ["Edit group parameters" gnus-summary-edit-parameters t]
1922        ["Customize group parameters" gnus-summary-customize-parameters t]
1923        ["Send a bug report" gnus-bug t]
1924        ("Exit"
1925         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1926         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1927         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1928         ["Exit group" gnus-summary-exit t]
1929         ["Exit group without updating" gnus-summary-exit-no-update t]
1930         ["Exit and goto next group" gnus-summary-next-group t]
1931         ["Exit and goto prev group" gnus-summary-prev-group t]
1932         ["Reselect group" gnus-summary-reselect-current-group t]
1933         ["Rescan group" gnus-summary-rescan-group t]
1934         ["Update dribble" gnus-summary-save-newsrc t])))
1935
1936     (gnus-run-hooks 'gnus-summary-menu-hook)))
1937
1938 (defun gnus-score-set-default (var value)
1939   "A version of set that updates the GNU Emacs menu-bar."
1940   (set var value)
1941   ;; It is the message that forces the active status to be updated.
1942   (message ""))
1943
1944 (defun gnus-make-score-map (type)
1945   "Make a summary score map of type TYPE."
1946   (if t
1947       nil
1948     (let ((headers '(("author" "from" string)
1949                      ("subject" "subject" string)
1950                      ("article body" "body" string)
1951                      ("article head" "head" string)
1952                      ("xref" "xref" string)
1953                      ("extra header" "extra" string)
1954                      ("lines" "lines" number)
1955                      ("followups to author" "followup" string)))
1956           (types '((number ("less than" <)
1957                            ("greater than" >)
1958                            ("equal" =))
1959                    (string ("substring" s)
1960                            ("exact string" e)
1961                            ("fuzzy string" f)
1962                            ("regexp" r))))
1963           (perms '(("temporary" (current-time-string))
1964                    ("permanent" nil)
1965                    ("immediate" now)))
1966           header)
1967       (list
1968        (apply
1969         'nconc
1970         (list
1971          (if (eq type 'lower)
1972              "Lower score"
1973            "Increase score"))
1974         (let (outh)
1975           (while headers
1976             (setq header (car headers))
1977             (setq outh
1978                   (cons
1979                    (apply
1980                     'nconc
1981                     (list (car header))
1982                     (let ((ts (cdr (assoc (nth 2 header) types)))
1983                           outt)
1984                       (while ts
1985                         (setq outt
1986                               (cons
1987                                (apply
1988                                 'nconc
1989                                 (list (caar ts))
1990                                 (let ((ps perms)
1991                                       outp)
1992                                   (while ps
1993                                     (setq outp
1994                                           (cons
1995                                            (vector
1996                                             (caar ps)
1997                                             (list
1998                                              'gnus-summary-score-entry
1999                                              (nth 1 header)
2000                                              (if (or (string= (nth 1 header)
2001                                                               "head")
2002                                                      (string= (nth 1 header)
2003                                                               "body"))
2004                                                  ""
2005                                                (list 'gnus-summary-header
2006                                                      (nth 1 header)))
2007                                              (list 'quote (nth 1 (car ts)))
2008                                              (list 'gnus-score-default nil)
2009                                              (nth 1 (car ps))
2010                                              t)
2011                                             t)
2012                                            outp))
2013                                     (setq ps (cdr ps)))
2014                                   (list (nreverse outp))))
2015                                outt))
2016                         (setq ts (cdr ts)))
2017                       (list (nreverse outt))))
2018                    outh))
2019             (setq headers (cdr headers)))
2020           (list (nreverse outh))))))))
2021
2022 \f
2023
2024 (defun gnus-summary-mode (&optional group)
2025   "Major mode for reading articles.
2026
2027 All normal editing commands are switched off.
2028 \\<gnus-summary-mode-map>
2029 Each line in this buffer represents one article.  To read an
2030 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2031 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2032 respectively.
2033
2034 You can also post articles and send mail from this buffer.  To
2035 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2036 of an article, type `\\[gnus-summary-reply]'.
2037
2038 There are approx. one gazillion commands you can execute in this
2039 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2040
2041 The following commands are available:
2042
2043 \\{gnus-summary-mode-map}"
2044   (interactive)
2045   (when (gnus-visual-p 'summary-menu 'menu)
2046     (gnus-summary-make-menu-bar))
2047   (kill-all-local-variables)
2048   (gnus-summary-make-local-variables)
2049   (gnus-make-thread-indent-array)
2050   (gnus-simplify-mode-line)
2051   (setq major-mode 'gnus-summary-mode)
2052   (setq mode-name "Summary")
2053   (make-local-variable 'minor-mode-alist)
2054   (use-local-map gnus-summary-mode-map)
2055   (buffer-disable-undo)
2056   (setq buffer-read-only t)             ;Disable modification
2057   (setq truncate-lines t)
2058   (setq selective-display t)
2059   (setq selective-display-ellipses t)   ;Display `...'
2060   (gnus-summary-set-display-table)
2061   (gnus-set-default-directory)
2062   (setq gnus-newsgroup-name group)
2063   (unless (gnus-news-group-p group)
2064     (setq gnus-newsgroup-incorporated
2065           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2066   (make-local-variable 'gnus-summary-line-format)
2067   (make-local-variable 'gnus-summary-line-format-spec)
2068   (make-local-variable 'gnus-summary-dummy-line-format)
2069   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2070   (make-local-variable 'gnus-summary-mark-positions)
2071   (make-local-hook 'pre-command-hook)
2072   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2073   (gnus-run-hooks 'gnus-summary-mode-hook)
2074   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2075   (gnus-update-summary-mark-positions))
2076
2077 (defun gnus-summary-make-local-variables ()
2078   "Make all the local summary buffer variables."
2079   (let (global)
2080     (dolist (local gnus-summary-local-variables)
2081       (if (consp local)
2082           (progn
2083             (if (eq (cdr local) 'global)
2084                 ;; Copy the global value of the variable.
2085                 (setq global (symbol-value (car local)))
2086               ;; Use the value from the list.
2087               (setq global (eval (cdr local))))
2088             (set (make-local-variable (car local)) global))
2089         ;; Simple nil-valued local variable.
2090         (set (make-local-variable local) nil)))))
2091
2092 (defun gnus-summary-clear-local-variables ()
2093   (let ((locals gnus-summary-local-variables))
2094     (while locals
2095       (if (consp (car locals))
2096           (and (vectorp (caar locals))
2097                (set (caar locals) nil))
2098         (and (vectorp (car locals))
2099              (set (car locals) nil)))
2100       (setq locals (cdr locals)))))
2101
2102 ;; Summary data functions.
2103
2104 (defmacro gnus-data-number (data)
2105   `(car ,data))
2106
2107 (defmacro gnus-data-set-number (data number)
2108   `(setcar ,data ,number))
2109
2110 (defmacro gnus-data-mark (data)
2111   `(nth 1 ,data))
2112
2113 (defmacro gnus-data-set-mark (data mark)
2114   `(setcar (nthcdr 1 ,data) ,mark))
2115
2116 (defmacro gnus-data-pos (data)
2117   `(nth 2 ,data))
2118
2119 (defmacro gnus-data-set-pos (data pos)
2120   `(setcar (nthcdr 2 ,data) ,pos))
2121
2122 (defmacro gnus-data-header (data)
2123   `(nth 3 ,data))
2124
2125 (defmacro gnus-data-set-header (data header)
2126   `(setcar (nthcdr 3 ,data) ,header))
2127
2128 (defmacro gnus-data-level (data)
2129   `(nth 4 ,data))
2130
2131 (defmacro gnus-data-unread-p (data)
2132   `(= (nth 1 ,data) gnus-unread-mark))
2133
2134 (defmacro gnus-data-read-p (data)
2135   `(/= (nth 1 ,data) gnus-unread-mark))
2136
2137 (defmacro gnus-data-pseudo-p (data)
2138   `(consp (nth 3 ,data)))
2139
2140 (defmacro gnus-data-find (number)
2141   `(assq ,number gnus-newsgroup-data))
2142
2143 (defmacro gnus-data-find-list (number &optional data)
2144   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2145      (memq (assq ,number bdata)
2146            bdata)))
2147
2148 (defmacro gnus-data-make (number mark pos header level)
2149   `(list ,number ,mark ,pos ,header ,level))
2150
2151 (defun gnus-data-enter (after-article number mark pos header level offset)
2152   (let ((data (gnus-data-find-list after-article)))
2153     (unless data
2154       (error "No such article: %d" after-article))
2155     (setcdr data (cons (gnus-data-make number mark pos header level)
2156                        (cdr data)))
2157     (setq gnus-newsgroup-data-reverse nil)
2158     (gnus-data-update-list (cddr data) offset)))
2159
2160 (defun gnus-data-enter-list (after-article list &optional offset)
2161   (when list
2162     (let ((data (and after-article (gnus-data-find-list after-article)))
2163           (ilist list))
2164       (if (not (or data
2165                    after-article))
2166           (let ((odata gnus-newsgroup-data))
2167             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2168             (when offset
2169               (gnus-data-update-list odata offset)))
2170         ;; Find the last element in the list to be spliced into the main
2171         ;; list.
2172         (while (cdr list)
2173           (setq list (cdr list)))
2174         (if (not data)
2175             (progn
2176               (setcdr list gnus-newsgroup-data)
2177               (setq gnus-newsgroup-data ilist)
2178               (when offset
2179                 (gnus-data-update-list (cdr list) offset)))
2180           (setcdr list (cdr data))
2181           (setcdr data ilist)
2182           (when offset
2183             (gnus-data-update-list (cdr list) offset))))
2184       (setq gnus-newsgroup-data-reverse nil))))
2185
2186 (defun gnus-data-remove (article &optional offset)
2187   (let ((data gnus-newsgroup-data))
2188     (if (= (gnus-data-number (car data)) article)
2189         (progn
2190           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2191                 gnus-newsgroup-data-reverse nil)
2192           (when offset
2193             (gnus-data-update-list gnus-newsgroup-data offset)))
2194       (while (cdr data)
2195         (when (= (gnus-data-number (cadr data)) article)
2196           (setcdr data (cddr data))
2197           (when offset
2198             (gnus-data-update-list (cdr data) offset))
2199           (setq data nil
2200                 gnus-newsgroup-data-reverse nil))
2201         (setq data (cdr data))))))
2202
2203 (defmacro gnus-data-list (backward)
2204   `(if ,backward
2205        (or gnus-newsgroup-data-reverse
2206            (setq gnus-newsgroup-data-reverse
2207                  (reverse gnus-newsgroup-data)))
2208      gnus-newsgroup-data))
2209
2210 (defun gnus-data-update-list (data offset)
2211   "Add OFFSET to the POS of all data entries in DATA."
2212   (setq gnus-newsgroup-data-reverse nil)
2213   (while data
2214     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2215     (setq data (cdr data))))
2216
2217 (defun gnus-summary-article-pseudo-p (article)
2218   "Say whether this article is a pseudo article or not."
2219   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2220
2221 (defmacro gnus-summary-article-sparse-p (article)
2222   "Say whether this article is a sparse article or not."
2223   `(memq ,article gnus-newsgroup-sparse))
2224
2225 (defmacro gnus-summary-article-ancient-p (article)
2226   "Say whether this article is a sparse article or not."
2227   `(memq ,article gnus-newsgroup-ancient))
2228
2229 (defun gnus-article-parent-p (number)
2230   "Say whether this article is a parent or not."
2231   (let ((data (gnus-data-find-list number)))
2232     (and (cdr data)                     ; There has to be an article after...
2233          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2234             (gnus-data-level (nth 1 data))))))
2235
2236 (defun gnus-article-children (number)
2237   "Return a list of all children to NUMBER."
2238   (let* ((data (gnus-data-find-list number))
2239          (level (gnus-data-level (car data)))
2240          children)
2241     (setq data (cdr data))
2242     (while (and data
2243                 (= (gnus-data-level (car data)) (1+ level)))
2244       (push (gnus-data-number (car data)) children)
2245       (setq data (cdr data)))
2246     children))
2247
2248 (defmacro gnus-summary-skip-intangible ()
2249   "If the current article is intangible, then jump to a different article."
2250   '(let ((to (get-text-property (point) 'gnus-intangible)))
2251      (and to (gnus-summary-goto-subject to))))
2252
2253 (defmacro gnus-summary-article-intangible-p ()
2254   "Say whether this article is intangible or not."
2255   '(get-text-property (point) 'gnus-intangible))
2256
2257 (defun gnus-article-read-p (article)
2258   "Say whether ARTICLE is read or not."
2259   (not (or (memq article gnus-newsgroup-marked)
2260            (memq article gnus-newsgroup-unreads)
2261            (memq article gnus-newsgroup-unselected)
2262            (memq article gnus-newsgroup-dormant))))
2263
2264 ;; Some summary mode macros.
2265
2266 (defmacro gnus-summary-article-number ()
2267   "The article number of the article on the current line.
2268 If there isn's an article number here, then we return the current
2269 article number."
2270   '(progn
2271      (gnus-summary-skip-intangible)
2272      (or (get-text-property (point) 'gnus-number)
2273          (gnus-summary-last-subject))))
2274
2275 (defmacro gnus-summary-article-header (&optional number)
2276   "Return the header of article NUMBER."
2277   `(gnus-data-header (gnus-data-find
2278                       ,(or number '(gnus-summary-article-number)))))
2279
2280 (defmacro gnus-summary-thread-level (&optional number)
2281   "Return the level of thread that starts with article NUMBER."
2282   `(if (and (eq gnus-summary-make-false-root 'dummy)
2283             (get-text-property (point) 'gnus-intangible))
2284        0
2285      (gnus-data-level (gnus-data-find
2286                        ,(or number '(gnus-summary-article-number))))))
2287
2288 (defmacro gnus-summary-article-mark (&optional number)
2289   "Return the mark of article NUMBER."
2290   `(gnus-data-mark (gnus-data-find
2291                     ,(or number '(gnus-summary-article-number)))))
2292
2293 (defmacro gnus-summary-article-pos (&optional number)
2294   "Return the position of the line of article NUMBER."
2295   `(gnus-data-pos (gnus-data-find
2296                    ,(or number '(gnus-summary-article-number)))))
2297
2298 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2299 (defmacro gnus-summary-article-subject (&optional number)
2300   "Return current subject string or nil if nothing."
2301   `(let ((headers
2302           ,(if number
2303                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2304              '(gnus-data-header (assq (gnus-summary-article-number)
2305                                       gnus-newsgroup-data)))))
2306      (and headers
2307           (vectorp headers)
2308           (mail-header-subject headers))))
2309
2310 (defmacro gnus-summary-article-score (&optional number)
2311   "Return current article score."
2312   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2313                   gnus-newsgroup-scored))
2314        gnus-summary-default-score 0))
2315
2316 (defun gnus-summary-article-children (&optional number)
2317   "Return a list of article numbers that are children of article NUMBER."
2318   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2319          (level (gnus-data-level (car data)))
2320          l children)
2321     (while (and (setq data (cdr data))
2322                 (> (setq l (gnus-data-level (car data))) level))
2323       (and (= (1+ level) l)
2324            (push (gnus-data-number (car data))
2325                  children)))
2326     (nreverse children)))
2327
2328 (defun gnus-summary-article-parent (&optional number)
2329   "Return the article number of the parent of article NUMBER."
2330   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2331                                     (gnus-data-list t)))
2332          (level (gnus-data-level (car data))))
2333     (if (zerop level)
2334         ()                              ; This is a root.
2335       ;; We search until we find an article with a level less than
2336       ;; this one.  That function has to be the parent.
2337       (while (and (setq data (cdr data))
2338                   (not (< (gnus-data-level (car data)) level))))
2339       (and data (gnus-data-number (car data))))))
2340
2341 (defun gnus-unread-mark-p (mark)
2342   "Say whether MARK is the unread mark."
2343   (= mark gnus-unread-mark))
2344
2345 (defun gnus-read-mark-p (mark)
2346   "Say whether MARK is one of the marks that mark as read.
2347 This is all marks except unread, ticked, dormant, and expirable."
2348   (not (or (= mark gnus-unread-mark)
2349            (= mark gnus-ticked-mark)
2350            (= mark gnus-dormant-mark)
2351            (= mark gnus-expirable-mark))))
2352
2353 (defmacro gnus-article-mark (number)
2354   "Return the MARK of article NUMBER.
2355 This macro should only be used when computing the mark the \"first\"
2356 time; i.e., when generating the summary lines.  After that,
2357 `gnus-summary-article-mark' should be used to examine the
2358 marks of articles."
2359   `(cond
2360     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2361     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2362     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2363     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2364     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2365     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2366     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2367     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2368            gnus-ancient-mark))))
2369
2370 ;; Saving hidden threads.
2371
2372 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2373 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2374
2375 (defmacro gnus-save-hidden-threads (&rest forms)
2376   "Save hidden threads, eval FORMS, and restore the hidden threads."
2377   (let ((config (make-symbol "config")))
2378     `(let ((,config (gnus-hidden-threads-configuration)))
2379        (unwind-protect
2380            (save-excursion
2381              ,@forms)
2382          (gnus-restore-hidden-threads-configuration ,config)))))
2383
2384 (defun gnus-data-compute-positions ()
2385   "Compute the positions of all articles."
2386   (setq gnus-newsgroup-data-reverse nil)
2387   (let ((data gnus-newsgroup-data))
2388     (save-excursion
2389       (gnus-save-hidden-threads
2390         (gnus-summary-show-all-threads)
2391         (goto-char (point-min))
2392         (while data
2393           (while (get-text-property (point) 'gnus-intangible)
2394             (forward-line 1))
2395           (gnus-data-set-pos (car data) (+ (point) 3))
2396           (setq data (cdr data))
2397           (forward-line 1))))))
2398
2399 (defun gnus-hidden-threads-configuration ()
2400   "Return the current hidden threads configuration."
2401   (save-excursion
2402     (let (config)
2403       (goto-char (point-min))
2404       (while (search-forward "\r" nil t)
2405         (push (1- (point)) config))
2406       config)))
2407
2408 (defun gnus-restore-hidden-threads-configuration (config)
2409   "Restore hidden threads configuration from CONFIG."
2410   (let (point buffer-read-only)
2411     (while (setq point (pop config))
2412       (when (and (< point (point-max))
2413                  (goto-char point)
2414                  (eq (char-after) ?\n))
2415         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2416
2417 ;; Various summary mode internalish functions.
2418
2419 (defun gnus-mouse-pick-article (e)
2420   (interactive "e")
2421   (mouse-set-point e)
2422   (gnus-summary-next-page nil t))
2423
2424 (defun gnus-summary-set-display-table ()
2425   ;; Change the display table.  Odd characters have a tendency to mess
2426   ;; up nicely formatted displays - we make all possible glyphs
2427   ;; display only a single character.
2428
2429   ;; We start from the standard display table, if any.
2430   (let ((table (or (copy-sequence standard-display-table)
2431                    (make-display-table)))
2432         (i 32))
2433     ;; Nix out all the control chars...
2434     (while (>= (setq i (1- i)) 0)
2435       (aset table i [??]))
2436     ;; ... but not newline and cr, of course.  (cr is necessary for the
2437     ;; selective display).
2438     (aset table ?\n nil)
2439     (aset table ?\r nil)
2440     ;; We keep TAB as well.
2441     (aset table ?\t nil)
2442     ;; We nix out any glyphs over 126 that are not set already.
2443     (let ((i 256))
2444       (while (>= (setq i (1- i)) 127)
2445         ;; Only modify if the entry is nil.
2446         (unless (aref table i)
2447           (aset table i [??]))))
2448     (setq buffer-display-table table)))
2449
2450 (defun gnus-summary-setup-buffer (group)
2451   "Initialize summary buffer."
2452   (let ((buffer (concat "*Summary " group "*")))
2453     (if (get-buffer buffer)
2454         (progn
2455           (set-buffer buffer)
2456           (setq gnus-summary-buffer (current-buffer))
2457           (not gnus-newsgroup-prepared))
2458       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2459       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2460       (gnus-summary-mode group)
2461       (when gnus-carpal
2462         (gnus-carpal-setup-buffer 'summary))
2463       (unless gnus-single-article-buffer
2464         (make-local-variable 'gnus-article-buffer)
2465         (make-local-variable 'gnus-article-current)
2466         (make-local-variable 'gnus-original-article-buffer))
2467       (setq gnus-newsgroup-name group)
2468       t)))
2469
2470 (defun gnus-set-global-variables ()
2471   ;; Set the global equivalents of the summary buffer-local variables
2472   ;; to the latest values they had.  These reflect the summary buffer
2473   ;; that was in action when the last article was fetched.
2474   (when (eq major-mode 'gnus-summary-mode)
2475     (setq gnus-summary-buffer (current-buffer))
2476     (let ((name gnus-newsgroup-name)
2477           (marked gnus-newsgroup-marked)
2478           (unread gnus-newsgroup-unreads)
2479           (headers gnus-current-headers)
2480           (data gnus-newsgroup-data)
2481           (summary gnus-summary-buffer)
2482           (article-buffer gnus-article-buffer)
2483           (original gnus-original-article-buffer)
2484           (gac gnus-article-current)
2485           (reffed gnus-reffed-article-number)
2486           (score-file gnus-current-score-file)
2487           (default-charset gnus-newsgroup-charset))
2488       (save-excursion
2489         (set-buffer gnus-group-buffer)
2490         (setq gnus-newsgroup-name name
2491               gnus-newsgroup-marked marked
2492               gnus-newsgroup-unreads unread
2493               gnus-current-headers headers
2494               gnus-newsgroup-data data
2495               gnus-article-current gac
2496               gnus-summary-buffer summary
2497               gnus-article-buffer article-buffer
2498               gnus-original-article-buffer original
2499               gnus-reffed-article-number reffed
2500               gnus-current-score-file score-file
2501               gnus-newsgroup-charset default-charset)
2502         ;; The article buffer also has local variables.
2503         (when (gnus-buffer-live-p gnus-article-buffer)
2504           (set-buffer gnus-article-buffer)
2505           (setq gnus-summary-buffer summary))))))
2506
2507 (defun gnus-summary-article-unread-p (article)
2508   "Say whether ARTICLE is unread or not."
2509   (memq article gnus-newsgroup-unreads))
2510
2511 (defun gnus-summary-first-article-p (&optional article)
2512   "Return whether ARTICLE is the first article in the buffer."
2513   (if (not (setq article (or article (gnus-summary-article-number))))
2514       nil
2515     (eq article (caar gnus-newsgroup-data))))
2516
2517 (defun gnus-summary-last-article-p (&optional article)
2518   "Return whether ARTICLE is the last article in the buffer."
2519   (if (not (setq article (or article (gnus-summary-article-number))))
2520       ;; All non-existent numbers are the last article.  :-)
2521       t
2522     (not (cdr (gnus-data-find-list article)))))
2523
2524 (defun gnus-make-thread-indent-array ()
2525   (let ((n 200))
2526     (unless (and gnus-thread-indent-array
2527                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2528       (setq gnus-thread-indent-array (make-vector 201 "")
2529             gnus-thread-indent-array-level gnus-thread-indent-level)
2530       (while (>= n 0)
2531         (aset gnus-thread-indent-array n
2532               (make-string (* n gnus-thread-indent-level) ? ))
2533         (setq n (1- n))))))
2534
2535 (defun gnus-update-summary-mark-positions ()
2536   "Compute where the summary marks are to go."
2537   (save-excursion
2538     (when (gnus-buffer-exists-p gnus-summary-buffer)
2539       (set-buffer gnus-summary-buffer))
2540     (let ((gnus-replied-mark 129)
2541           (gnus-score-below-mark 130)
2542           (gnus-score-over-mark 130)
2543           (gnus-download-mark 131)
2544           (spec gnus-summary-line-format-spec)
2545           gnus-visual pos)
2546       (save-excursion
2547         (gnus-set-work-buffer)
2548         (let ((gnus-summary-line-format-spec spec)
2549               (gnus-newsgroup-downloadable '((0 . t))))
2550           (gnus-summary-insert-line
2551            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2552            0 nil 128 t nil "" nil 1)
2553           (goto-char (point-min))
2554           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2555                                              (- (point) 2)))))
2556           (goto-char (point-min))
2557           (push (cons 'replied (and (search-forward "\201" nil t)
2558                                     (- (point) 2)))
2559                 pos)
2560           (goto-char (point-min))
2561           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2562                 pos)
2563           (goto-char (point-min))
2564           (push (cons 'download
2565                       (and (search-forward "\203" nil t) (- (point) 2)))
2566                 pos)))
2567       (setq gnus-summary-mark-positions pos))))
2568
2569 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2570   "Insert a dummy root in the summary buffer."
2571   (beginning-of-line)
2572   (gnus-add-text-properties
2573    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2574    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2575
2576 (defun gnus-summary-from-or-to-or-newsgroups (header)
2577   (let ((to (cdr (assq 'To (mail-header-extra header))))
2578         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2579         (default-mime-charset (with-current-buffer gnus-summary-buffer
2580                                 default-mime-charset)))
2581     (cond
2582      ((and to
2583            gnus-ignored-from-addresses
2584            (string-match gnus-ignored-from-addresses
2585                          (mail-header-from header)))
2586       (concat "-> "
2587               (or (car (funcall gnus-extract-address-components
2588                                 (funcall
2589                                  gnus-decode-encoded-word-function to)))
2590                   (funcall gnus-decode-encoded-word-function to))))
2591      ((and newsgroups
2592            gnus-ignored-from-addresses
2593            (string-match gnus-ignored-from-addresses
2594                          (mail-header-from header)))
2595       (concat "=> " newsgroups))
2596      (t
2597       (or (car (funcall gnus-extract-address-components
2598                         (mail-header-from header)))
2599           (mail-header-from header))))))
2600
2601 (defun gnus-summary-insert-line (gnus-tmp-header
2602                                  gnus-tmp-level gnus-tmp-current
2603                                  gnus-tmp-unread gnus-tmp-replied
2604                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2605                                  &optional gnus-tmp-dummy gnus-tmp-score
2606                                  gnus-tmp-process)
2607   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2608          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2609          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2610          (gnus-tmp-score-char
2611           (if (or (null gnus-summary-default-score)
2612                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2613                       gnus-summary-zcore-fuzz))
2614               ? ;Whitespace
2615             (if (< gnus-tmp-score gnus-summary-default-score)
2616                 gnus-score-below-mark gnus-score-over-mark)))
2617          (gnus-tmp-replied
2618           (cond (gnus-tmp-process gnus-process-mark)
2619                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2620                  gnus-cached-mark)
2621                 (gnus-tmp-replied gnus-replied-mark)
2622                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2623                  gnus-saved-mark)
2624                 (t gnus-unread-mark)))
2625          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2626          (gnus-tmp-name
2627           (cond
2628            ((string-match "<[^>]+> *$" gnus-tmp-from)
2629             (let ((beg (match-beginning 0)))
2630               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2631                        (substring gnus-tmp-from (1+ (match-beginning 0))
2632                                   (1- (match-end 0))))
2633                   (substring gnus-tmp-from 0 beg))))
2634            ((string-match "(.+)" gnus-tmp-from)
2635             (substring gnus-tmp-from
2636                        (1+ (match-beginning 0)) (1- (match-end 0))))
2637            (t gnus-tmp-from)))
2638          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2639          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2640          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2641          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2642          (buffer-read-only nil))
2643     (when (string= gnus-tmp-name "")
2644       (setq gnus-tmp-name gnus-tmp-from))
2645     (unless (numberp gnus-tmp-lines)
2646       (setq gnus-tmp-lines 0))
2647     (gnus-put-text-property-excluding-characters-with-faces
2648      (point)
2649      (progn (eval gnus-summary-line-format-spec) (point))
2650      'gnus-number gnus-tmp-number)
2651     (when (gnus-visual-p 'summary-highlight 'highlight)
2652       (forward-line -1)
2653       (gnus-run-hooks 'gnus-summary-update-hook)
2654       (forward-line 1))))
2655
2656 (defun gnus-summary-update-line (&optional dont-update)
2657   ;; Update summary line after change.
2658   (when (and gnus-summary-default-score
2659              (not gnus-summary-inhibit-highlight))
2660     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2661            (article (gnus-summary-article-number))
2662            (score (gnus-summary-article-score article)))
2663       (unless dont-update
2664         (if (and gnus-summary-mark-below
2665                  (< (gnus-summary-article-score)
2666                     gnus-summary-mark-below))
2667             ;; This article has a low score, so we mark it as read.
2668             (when (memq article gnus-newsgroup-unreads)
2669               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2670           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2671             ;; This article was previously marked as read on account
2672             ;; of a low score, but now it has risen, so we mark it as
2673             ;; unread.
2674             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2675         (gnus-summary-update-mark
2676          (if (or (null gnus-summary-default-score)
2677                  (<= (abs (- score gnus-summary-default-score))
2678                      gnus-summary-zcore-fuzz))
2679              ? ;Whitespace
2680            (if (< score gnus-summary-default-score)
2681                gnus-score-below-mark gnus-score-over-mark))
2682          'score))
2683       ;; Do visual highlighting.
2684       (when (gnus-visual-p 'summary-highlight 'highlight)
2685         (gnus-run-hooks 'gnus-summary-update-hook)))))
2686
2687 (defvar gnus-tmp-new-adopts nil)
2688
2689 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2690   "Return the number of articles in THREAD.
2691 This may be 0 in some cases -- if none of the articles in
2692 the thread are to be displayed."
2693   (let* ((number
2694           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2695           (cond
2696            ((not (listp thread))
2697             1)
2698            ((and (consp thread) (cdr thread))
2699             (apply
2700              '+ 1 (mapcar
2701                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2702            ((null thread)
2703             1)
2704            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2705             1)
2706            (t 0))))
2707     (when (and level (zerop level) gnus-tmp-new-adopts)
2708       (incf number
2709             (apply '+ (mapcar
2710                        'gnus-summary-number-of-articles-in-thread
2711                        gnus-tmp-new-adopts))))
2712     (if char
2713         (if (> number 1) gnus-not-empty-thread-mark
2714           gnus-empty-thread-mark)
2715       number)))
2716
2717 (defun gnus-summary-set-local-parameters (group)
2718   "Go through the local params of GROUP and set all variable specs in that list."
2719   (let ((params (gnus-group-find-parameter group))
2720         elem)
2721     (while params
2722       (setq elem (car params)
2723             params (cdr params))
2724       (and (consp elem)                 ; Has to be a cons.
2725            (consp (cdr elem))           ; The cdr has to be a list.
2726            (symbolp (car elem))         ; Has to be a symbol in there.
2727            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2728            (ignore-errors               ; So we set it.
2729              (make-local-variable (car elem))
2730              (set (car elem) (eval (nth 1 elem))))))))
2731
2732 (defun gnus-summary-read-group (group &optional show-all no-article
2733                                       kill-buffer no-display backward
2734                                       select-articles)
2735   "Start reading news in newsgroup GROUP.
2736 If SHOW-ALL is non-nil, already read articles are also listed.
2737 If NO-ARTICLE is non-nil, no article is selected initially.
2738 If NO-DISPLAY, don't generate a summary buffer."
2739   (let (result)
2740     (while (and group
2741                 (null (setq result
2742                             (let ((gnus-auto-select-next nil))
2743                               (or (gnus-summary-read-group-1
2744                                    group show-all no-article
2745                                    kill-buffer no-display
2746                                    select-articles)
2747                                   (setq show-all nil
2748                                         select-articles nil)))))
2749                 (eq gnus-auto-select-next 'quietly))
2750       (set-buffer gnus-group-buffer)
2751       ;; The entry function called above goes to the next
2752       ;; group automatically, so we go two groups back
2753       ;; if we are searching for the previous group.
2754       (when backward
2755         (gnus-group-prev-unread-group 2))
2756       (if (not (equal group (gnus-group-group-name)))
2757           (setq group (gnus-group-group-name))
2758         (setq group nil)))
2759     result))
2760
2761 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2762   "Directly jump to the other GROUP from summary buffer.
2763 If SHOW-ALL is non-nil, already read articles are also listed."
2764   (interactive
2765    (if (eq gnus-summary-buffer (current-buffer))
2766        (list (completing-read
2767               "Group: " gnus-active-hashtb nil t
2768               (when (and gnus-newsgroup-name
2769                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2770                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2771               'gnus-group-history)
2772              current-prefix-arg)
2773      (error "%s must be invoked from a gnus summary buffer." this-command)))
2774   (unless (or (zerop (length group))
2775               (and gnus-newsgroup-name
2776                    (string-equal gnus-newsgroup-name group)))
2777     (gnus-summary-exit)
2778     (gnus-summary-read-group group show-all
2779                              gnus-dont-select-after-jump-to-other-group)))
2780
2781 (defun gnus-summary-read-group-1 (group show-all no-article
2782                                         kill-buffer no-display
2783                                         &optional select-articles)
2784   ;; Killed foreign groups can't be entered.
2785   (when (and (not (gnus-group-native-p group))
2786              (not (gnus-gethash group gnus-newsrc-hashtb)))
2787     (error "Dead non-native groups can't be entered"))
2788   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2789   (let* ((new-group (gnus-summary-setup-buffer group))
2790          (quit-config (gnus-group-quit-config group))
2791          (did-select (and new-group (gnus-select-newsgroup
2792                                      group show-all select-articles))))
2793     (cond
2794      ;; This summary buffer exists already, so we just select it.
2795      ((not new-group)
2796       (gnus-set-global-variables)
2797       (when kill-buffer
2798         (gnus-kill-or-deaden-summary kill-buffer))
2799       (gnus-configure-windows 'summary 'force)
2800       (gnus-set-mode-line 'summary)
2801       (gnus-summary-position-point)
2802       (message "")
2803       t)
2804      ;; We couldn't select this group.
2805      ((null did-select)
2806       (when (and (eq major-mode 'gnus-summary-mode)
2807                  (not (equal (current-buffer) kill-buffer)))
2808         (kill-buffer (current-buffer))
2809         (if (not quit-config)
2810             (progn
2811               ;; Update the info -- marks might need to be removed,
2812               ;; for instance.
2813               (gnus-summary-update-info)
2814               (set-buffer gnus-group-buffer)
2815               (gnus-group-jump-to-group group)
2816               (gnus-group-next-unread-group 1))
2817           (gnus-handle-ephemeral-exit quit-config)))
2818       (gnus-message 3 "Can't select group")
2819       nil)
2820      ;; The user did a `C-g' while prompting for number of articles,
2821      ;; so we exit this group.
2822      ((eq did-select 'quit)
2823       (and (eq major-mode 'gnus-summary-mode)
2824            (not (equal (current-buffer) kill-buffer))
2825            (kill-buffer (current-buffer)))
2826       (when kill-buffer
2827         (gnus-kill-or-deaden-summary kill-buffer))
2828       (if (not quit-config)
2829           (progn
2830             (set-buffer gnus-group-buffer)
2831             (gnus-group-jump-to-group group)
2832             (gnus-group-next-unread-group 1)
2833             (gnus-configure-windows 'group 'force))
2834         (gnus-handle-ephemeral-exit quit-config))
2835       ;; Finally signal the quit.
2836       (signal 'quit nil))
2837      ;; The group was successfully selected.
2838      (t
2839       (gnus-set-global-variables)
2840       ;; Save the active value in effect when the group was entered.
2841       (setq gnus-newsgroup-active
2842             (gnus-copy-sequence
2843              (gnus-active gnus-newsgroup-name)))
2844       ;; You can change the summary buffer in some way with this hook.
2845       (gnus-run-hooks 'gnus-select-group-hook)
2846       ;; Set any local variables in the group parameters.
2847       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2848       (gnus-update-format-specifications
2849        nil 'summary 'summary-mode 'summary-dummy)
2850       (gnus-update-summary-mark-positions)
2851       ;; Do score processing.
2852       (when gnus-use-scoring
2853         (gnus-possibly-score-headers))
2854       ;; Check whether to fill in the gaps in the threads.
2855       (when gnus-build-sparse-threads
2856         (gnus-build-sparse-threads))
2857       ;; Find the initial limit.
2858       (if gnus-show-threads
2859           (if show-all
2860               (let ((gnus-newsgroup-dormant nil))
2861                 (gnus-summary-initial-limit show-all))
2862             (gnus-summary-initial-limit show-all))
2863         ;; When untreaded, all articles are always shown.
2864         (setq gnus-newsgroup-limit
2865               (mapcar
2866                (lambda (header) (mail-header-number header))
2867                gnus-newsgroup-headers)))
2868       ;; Generate the summary buffer.
2869       (unless no-display
2870         (gnus-summary-prepare))
2871       (when gnus-use-trees
2872         (gnus-tree-open group)
2873         (setq gnus-summary-highlight-line-function
2874               'gnus-tree-highlight-article))
2875       ;; If the summary buffer is empty, but there are some low-scored
2876       ;; articles or some excluded dormants, we include these in the
2877       ;; buffer.
2878       (when (and (zerop (buffer-size))
2879                  (not no-display))
2880         (cond (gnus-newsgroup-dormant
2881                (gnus-summary-limit-include-dormant))
2882               ((and gnus-newsgroup-scored show-all)
2883                (gnus-summary-limit-include-expunged t))))
2884       ;; Function `gnus-apply-kill-file' must be called in this hook.
2885       (gnus-run-hooks 'gnus-apply-kill-hook)
2886       (if (and (zerop (buffer-size))
2887                (not no-display))
2888           (progn
2889             ;; This newsgroup is empty.
2890             (gnus-summary-catchup-and-exit nil t)
2891             (gnus-message 6 "No unread news")
2892             (when kill-buffer
2893               (gnus-kill-or-deaden-summary kill-buffer))
2894             ;; Return nil from this function.
2895             nil)
2896         ;; Hide conversation thread subtrees.  We cannot do this in
2897         ;; gnus-summary-prepare-hook since kill processing may not
2898         ;; work with hidden articles.
2899         (and gnus-show-threads
2900              gnus-thread-hide-subtree
2901              (gnus-summary-hide-all-threads))
2902         (when kill-buffer
2903           (gnus-kill-or-deaden-summary kill-buffer))
2904         ;; Show first unread article if requested.
2905         (if (and (not no-article)
2906                  (not no-display)
2907                  gnus-newsgroup-unreads
2908                  gnus-auto-select-first)
2909             (progn
2910               (gnus-configure-windows 'summary)
2911               (cond
2912                ((eq gnus-auto-select-first 'best)
2913                 (gnus-summary-best-unread-article))
2914                ((eq gnus-auto-select-first t)
2915                 (gnus-summary-first-unread-article))
2916                ((gnus-functionp gnus-auto-select-first)
2917                 (funcall gnus-auto-select-first))))
2918           ;; Don't select any articles, just move point to the first
2919           ;; article in the group.
2920           (goto-char (point-min))
2921           (gnus-summary-position-point)
2922           (gnus-configure-windows 'summary 'force)
2923           (gnus-set-mode-line 'summary))
2924         (when (get-buffer-window gnus-group-buffer t)
2925           ;; Gotta use windows, because recenter does weird stuff if
2926           ;; the current buffer ain't the displayed window.
2927           (let ((owin (selected-window)))
2928             (select-window (get-buffer-window gnus-group-buffer t))
2929             (when (gnus-group-goto-group group)
2930               (recenter))
2931             (select-window owin)))
2932         ;; Mark this buffer as "prepared".
2933         (setq gnus-newsgroup-prepared t)
2934         (gnus-run-hooks 'gnus-summary-prepared-hook)
2935         t)))))
2936
2937 (defun gnus-summary-prepare ()
2938   "Generate the summary buffer."
2939   (interactive)
2940   (let ((buffer-read-only nil))
2941     (erase-buffer)
2942     (setq gnus-newsgroup-data nil
2943           gnus-newsgroup-data-reverse nil)
2944     (gnus-run-hooks 'gnus-summary-generate-hook)
2945     ;; Generate the buffer, either with threads or without.
2946     (when gnus-newsgroup-headers
2947       (gnus-summary-prepare-threads
2948        (if gnus-show-threads
2949            (gnus-sort-gathered-threads
2950             (funcall gnus-summary-thread-gathering-function
2951                      (gnus-sort-threads
2952                       (gnus-cut-threads (gnus-make-threads)))))
2953          ;; Unthreaded display.
2954          (gnus-sort-articles gnus-newsgroup-headers))))
2955     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2956     ;; Call hooks for modifying summary buffer.
2957     (goto-char (point-min))
2958     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2959
2960 (defsubst gnus-general-simplify-subject (subject)
2961   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2962   (setq subject
2963         (cond
2964          ;; Truncate the subject.
2965          (gnus-simplify-subject-functions
2966           (gnus-map-function gnus-simplify-subject-functions subject))
2967          ((numberp gnus-summary-gather-subject-limit)
2968           (setq subject (gnus-simplify-subject-re subject))
2969           (if (> (length subject) gnus-summary-gather-subject-limit)
2970               (substring subject 0 gnus-summary-gather-subject-limit)
2971             subject))
2972          ;; Fuzzily simplify it.
2973          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2974           (gnus-simplify-subject-fuzzy subject))
2975          ;; Just remove the leading "Re:".
2976          (t
2977           (gnus-simplify-subject-re subject))))
2978
2979   (if (and gnus-summary-gather-exclude-subject
2980            (string-match gnus-summary-gather-exclude-subject subject))
2981       nil                               ; This article shouldn't be gathered
2982     subject))
2983
2984 (defun gnus-summary-simplify-subject-query ()
2985   "Query where the respool algorithm would put this article."
2986   (interactive)
2987   (gnus-summary-select-article)
2988   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2989
2990 (defun gnus-gather-threads-by-subject (threads)
2991   "Gather threads by looking at Subject headers."
2992   (if (not gnus-summary-make-false-root)
2993       threads
2994     (let ((hashtb (gnus-make-hashtable 1024))
2995           (prev threads)
2996           (result threads)
2997           subject hthread whole-subject)
2998       (while threads
2999         (setq subject (gnus-general-simplify-subject
3000                        (setq whole-subject (mail-header-subject
3001                                             (caar threads)))))
3002         (when subject
3003           (if (setq hthread (gnus-gethash subject hashtb))
3004               (progn
3005                 ;; We enter a dummy root into the thread, if we
3006                 ;; haven't done that already.
3007                 (unless (stringp (caar hthread))
3008                   (setcar hthread (list whole-subject (car hthread))))
3009                 ;; We add this new gathered thread to this gathered
3010                 ;; thread.
3011                 (setcdr (car hthread)
3012                         (nconc (cdar hthread) (list (car threads))))
3013                 ;; Remove it from the list of threads.
3014                 (setcdr prev (cdr threads))
3015                 (setq threads prev))
3016             ;; Enter this thread into the hash table.
3017             (gnus-sethash subject threads hashtb)))
3018         (setq prev threads)
3019         (setq threads (cdr threads)))
3020       result)))
3021
3022 (defun gnus-gather-threads-by-references (threads)
3023   "Gather threads by looking at References headers."
3024   (let ((idhashtb (gnus-make-hashtable 1024))
3025         (thhashtb (gnus-make-hashtable 1024))
3026         (prev threads)
3027         (result threads)
3028         ids references id gthread gid entered ref)
3029     (while threads
3030       (when (setq references (mail-header-references (caar threads)))
3031         (setq id (mail-header-id (caar threads))
3032               ids (gnus-split-references references)
3033               entered nil)
3034         (while (setq ref (pop ids))
3035           (setq ids (delete ref ids))
3036           (if (not (setq gid (gnus-gethash ref idhashtb)))
3037               (progn
3038                 (gnus-sethash ref id idhashtb)
3039                 (gnus-sethash id threads thhashtb))
3040             (setq gthread (gnus-gethash gid thhashtb))
3041             (unless entered
3042               ;; We enter a dummy root into the thread, if we
3043               ;; haven't done that already.
3044               (unless (stringp (caar gthread))
3045                 (setcar gthread (list (mail-header-subject (caar gthread))
3046                                       (car gthread))))
3047               ;; We add this new gathered thread to this gathered
3048               ;; thread.
3049               (setcdr (car gthread)
3050                       (nconc (cdar gthread) (list (car threads)))))
3051             ;; Add it into the thread hash table.
3052             (gnus-sethash id gthread thhashtb)
3053             (setq entered t)
3054             ;; Remove it from the list of threads.
3055             (setcdr prev (cdr threads))
3056             (setq threads prev))))
3057       (setq prev threads)
3058       (setq threads (cdr threads)))
3059     result))
3060
3061 (defun gnus-sort-gathered-threads (threads)
3062   "Sort subtreads inside each gathered thread by article number."
3063   (let ((result threads))
3064     (while threads
3065       (when (stringp (caar threads))
3066         (setcdr (car threads)
3067                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3068       (setq threads (cdr threads)))
3069     result))
3070
3071 (defun gnus-thread-loop-p (root thread)
3072   "Say whether ROOT is in THREAD."
3073   (let ((stack (list thread))
3074         (infloop 0)
3075         th)
3076     (while (setq thread (pop stack))
3077       (setq th (cdr thread))
3078       (while (and th
3079                   (not (eq (caar th) root)))
3080         (pop th))
3081       (if th
3082           ;; We have found a loop.
3083           (let (ref-dep)
3084             (setcdr thread (delq (car th) (cdr thread)))
3085             (if (boundp (setq ref-dep (intern "none"
3086                                               gnus-newsgroup-dependencies)))
3087                 (setcdr (symbol-value ref-dep)
3088                         (nconc (cdr (symbol-value ref-dep))
3089                                (list (car th))))
3090               (set ref-dep (list nil (car th))))
3091             (setq infloop 1
3092                   stack nil))
3093         ;; Push all the subthreads onto the stack.
3094         (push (cdr thread) stack)))
3095     infloop))
3096
3097 (defun gnus-make-threads ()
3098   "Go through the dependency hashtb and find the roots.  Return all threads."
3099   (let (threads)
3100     (while (catch 'infloop
3101              (mapatoms
3102               (lambda (refs)
3103                 ;; Deal with self-referencing References loops.
3104                 (when (and (car (symbol-value refs))
3105                            (not (zerop
3106                                  (apply
3107                                   '+
3108                                   (mapcar
3109                                    (lambda (thread)
3110                                      (gnus-thread-loop-p
3111                                       (car (symbol-value refs)) thread))
3112                                    (cdr (symbol-value refs)))))))
3113                   (setq threads nil)
3114                   (throw 'infloop t))
3115                 (unless (car (symbol-value refs))
3116                   ;; These threads do not refer back to any other articles,
3117                   ;; so they're roots.
3118                   (setq threads (append (cdr (symbol-value refs)) threads))))
3119               gnus-newsgroup-dependencies)))
3120     threads))
3121
3122 ;; Build the thread tree.
3123 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3124   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3125
3126 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3127 if it was already present.
3128
3129 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3130 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3131 Message-IDs will be renamed be renamed to a unique Message-ID before
3132 being entered.
3133
3134 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3135   (let* ((id (mail-header-id header))
3136          (id-dep (and id (intern id dependencies)))
3137          ref ref-dep ref-header)
3138     ;; Enter this `header' in the `dependencies' table.
3139     (cond
3140      ((not id-dep)
3141       (setq header nil))
3142      ;; The first two cases do the normal part: enter a new `header'
3143      ;; in the `dependencies' table.
3144      ((not (boundp id-dep))
3145       (set id-dep (list header)))
3146      ((null (car (symbol-value id-dep)))
3147       (setcar (symbol-value id-dep) header))
3148
3149      ;; From here the `header' was already present in the
3150      ;; `dependencies' table.
3151      (force-new
3152       ;; Overrides an existing entry;
3153       ;; just set the header part of the entry.
3154       (setcar (symbol-value id-dep) header))
3155
3156      ;; Renames the existing `header' to a unique Message-ID.
3157      ((not gnus-summary-ignore-duplicates)
3158       ;; An article with this Message-ID has already been seen.
3159       ;; We rename the Message-ID.
3160       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3161            (list header))
3162       (mail-header-set-id header id))
3163
3164      ;; The last case ignores an existing entry, except it adds any
3165      ;; additional Xrefs (in case the two articles came from different
3166      ;; servers.
3167      ;; Also sets `header' to `nil' meaning that the `dependencies'
3168      ;; table was *not* modified.
3169      (t
3170       (mail-header-set-xref
3171        (car (symbol-value id-dep))
3172        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3173                    "")
3174                (or (mail-header-xref header) "")))
3175       (setq header nil)))
3176
3177     (when header
3178       ;; First check if that we are not creating a References loop.
3179       (setq ref (gnus-parent-id (mail-header-references header)))
3180       (while (and ref
3181                   (setq ref-dep (intern-soft ref dependencies))
3182                   (boundp ref-dep)
3183                   (setq ref-header (car (symbol-value ref-dep))))
3184         (if (string= id ref)
3185             ;; Yuk!  This is a reference loop.  Make the article be a
3186             ;; root article.
3187             (progn
3188               (mail-header-set-references (car (symbol-value id-dep)) "none")
3189               (setq ref nil))
3190           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3191       (setq ref (gnus-parent-id (mail-header-references header)))
3192       (setq ref-dep (intern (or ref "none") dependencies))
3193       (if (boundp ref-dep)
3194           (setcdr (symbol-value ref-dep)
3195                   (nconc (cdr (symbol-value ref-dep))
3196                          (list (symbol-value id-dep))))
3197         (set ref-dep (list nil (symbol-value id-dep)))))
3198     header))
3199
3200 (defun gnus-build-sparse-threads ()
3201   (let ((headers gnus-newsgroup-headers)
3202         (mail-parse-charset gnus-newsgroup-charset)
3203         (gnus-summary-ignore-duplicates t)
3204         header references generation relations
3205         subject child end new-child date)
3206     ;; First we create an alist of generations/relations, where
3207     ;; generations is how much we trust the relation, and the relation
3208     ;; is parent/child.
3209     (gnus-message 7 "Making sparse threads...")
3210     (save-excursion
3211       (nnheader-set-temp-buffer " *gnus sparse threads*")
3212       (while (setq header (pop headers))
3213         (when (and (setq references (mail-header-references header))
3214                    (not (string= references "")))
3215           (insert references)
3216           (setq child (mail-header-id header)
3217                 subject (mail-header-subject header)
3218                 date (mail-header-date header)
3219                 generation 0)
3220           (while (search-backward ">" nil t)
3221             (setq end (1+ (point)))
3222             (when (search-backward "<" nil t)
3223               (setq new-child (buffer-substring (point) end))
3224               (push (list (incf generation)
3225                           child (setq child new-child)
3226                           subject date)
3227                     relations)))
3228           (when child
3229             (push (list (1+ generation) child nil subject) relations))
3230           (erase-buffer)))
3231       (kill-buffer (current-buffer)))
3232     ;; Sort over trustworthiness.
3233     (mapcar
3234      (lambda (relation)
3235        (when (gnus-dependencies-add-header
3236               (make-full-mail-header
3237                gnus-reffed-article-number
3238                (nth 3 relation) "" (or (nth 4 relation) "")
3239                (nth 1 relation)
3240                (or (nth 2 relation) "") 0 0 "")
3241               gnus-newsgroup-dependencies nil)
3242          (push gnus-reffed-article-number gnus-newsgroup-limit)
3243          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3244          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3245                gnus-newsgroup-reads)
3246          (decf gnus-reffed-article-number)))
3247      (sort relations 'car-less-than-car))
3248     (gnus-message 7 "Making sparse threads...done")))
3249
3250 (defun gnus-build-old-threads ()
3251   ;; Look at all the articles that refer back to old articles, and
3252   ;; fetch the headers for the articles that aren't there.  This will
3253   ;; build complete threads - if the roots haven't been expired by the
3254   ;; server, that is.
3255   (let ((mail-parse-charset gnus-newsgroup-charset)
3256         id heads)
3257     (mapatoms
3258      (lambda (refs)
3259        (when (not (car (symbol-value refs)))
3260          (setq heads (cdr (symbol-value refs)))
3261          (while heads
3262            (if (memq (mail-header-number (caar heads))
3263                      gnus-newsgroup-dormant)
3264                (setq heads (cdr heads))
3265              (setq id (symbol-name refs))
3266              (while (and (setq id (gnus-build-get-header id))
3267                          (not (car (gnus-id-to-thread id)))))
3268              (setq heads nil)))))
3269      gnus-newsgroup-dependencies)))
3270
3271 ;; This function has to be called with point after the article number
3272 ;; on the beginning of the line.
3273 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3274   (let ((eol (gnus-point-at-eol))
3275         (buffer (current-buffer))
3276         header)
3277
3278     ;; overview: [num subject from date id refs chars lines misc]
3279     (unless (eobp)
3280       (forward-char))
3281
3282     (setq header
3283           (make-full-mail-header
3284            number                               ; number
3285            (nnheader-nov-field)                 ; subject
3286            (nnheader-nov-field)                 ; from
3287            (nnheader-nov-field)                 ; date
3288            (nnheader-nov-read-message-id)       ; id
3289            (nnheader-nov-field)                 ; refs
3290            (nnheader-nov-read-integer)          ; chars
3291            (nnheader-nov-read-integer)          ; lines
3292            (unless (eobp)
3293              (nnheader-nov-field))              ; misc
3294            (nnheader-nov-parse-extra)))         ; extra
3295
3296     (when gnus-alter-header-function
3297       (funcall gnus-alter-header-function header))
3298     (gnus-dependencies-add-header header dependencies force-new)))
3299
3300 (defun gnus-build-get-header (id)
3301   ;; Look through the buffer of NOV lines and find the header to
3302   ;; ID.  Enter this line into the dependencies hash table, and return
3303   ;; the id of the parent article (if any).
3304   (let ((deps gnus-newsgroup-dependencies)
3305         found header)
3306     (prog1
3307         (save-excursion
3308           (set-buffer nntp-server-buffer)
3309           (let ((case-fold-search nil))
3310             (goto-char (point-min))
3311             (while (and (not found)
3312                         (search-forward id nil t))
3313               (beginning-of-line)
3314               (setq found (looking-at
3315                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3316                                    (regexp-quote id))))
3317               (or found (beginning-of-line 2)))
3318             (when found
3319               (beginning-of-line)
3320               (and
3321                (setq header (gnus-nov-parse-line
3322                              (read (current-buffer)) deps))
3323                (gnus-parent-id (mail-header-references header))))))
3324       (when header
3325         (let ((number (mail-header-number header)))
3326           (push number gnus-newsgroup-limit)
3327           (push header gnus-newsgroup-headers)
3328           (if (memq number gnus-newsgroup-unselected)
3329               (progn
3330                 (push number gnus-newsgroup-unreads)
3331                 (setq gnus-newsgroup-unselected
3332                       (delq number gnus-newsgroup-unselected)))
3333             (push number gnus-newsgroup-ancient)))))))
3334
3335 (defun gnus-build-all-threads ()
3336   "Read all the headers."
3337   (let ((gnus-summary-ignore-duplicates t)
3338         (mail-parse-charset gnus-newsgroup-charset)
3339         (dependencies gnus-newsgroup-dependencies)
3340         header article)
3341     (save-excursion
3342       (set-buffer nntp-server-buffer)
3343       (let ((case-fold-search nil))
3344         (goto-char (point-min))
3345         (while (not (eobp))
3346           (ignore-errors
3347             (setq article (read (current-buffer))
3348                   header (gnus-nov-parse-line article dependencies)))
3349           (when header
3350             (save-excursion
3351               (set-buffer gnus-summary-buffer)
3352               (push header gnus-newsgroup-headers)
3353               (if (memq (setq article (mail-header-number header))
3354                         gnus-newsgroup-unselected)
3355                   (progn
3356                     (push article gnus-newsgroup-unreads)
3357                     (setq gnus-newsgroup-unselected
3358                           (delq article gnus-newsgroup-unselected)))
3359                 (push article gnus-newsgroup-ancient)))
3360             (forward-line 1)))))))
3361
3362 (defun gnus-summary-update-article-line (article header)
3363   "Update the line for ARTICLE using HEADERS."
3364   (let* ((id (mail-header-id header))
3365          (thread (gnus-id-to-thread id)))
3366     (unless thread
3367       (error "Article in no thread"))
3368     ;; Update the thread.
3369     (setcar thread header)
3370     (gnus-summary-goto-subject article)
3371     (let* ((datal (gnus-data-find-list article))
3372            (data (car datal))
3373            (length (when (cdr datal)
3374                      (- (gnus-data-pos data)
3375                         (gnus-data-pos (cadr datal)))))
3376            (buffer-read-only nil)
3377            (level (gnus-summary-thread-level)))
3378       (gnus-delete-line)
3379       (gnus-summary-insert-line
3380        header level nil (gnus-article-mark article)
3381        (memq article gnus-newsgroup-replied)
3382        (memq article gnus-newsgroup-expirable)
3383        ;; Only insert the Subject string when it's different
3384        ;; from the previous Subject string.
3385        (if (gnus-subject-equal
3386             (condition-case ()
3387                 (mail-header-subject
3388                  (gnus-data-header
3389                   (cadr
3390                    (gnus-data-find-list
3391                     article
3392                     (gnus-data-list t)))))
3393               ;; Error on the side of excessive subjects.
3394               (error ""))
3395             (mail-header-subject header))
3396            ""
3397          (mail-header-subject header))
3398        nil (cdr (assq article gnus-newsgroup-scored))
3399        (memq article gnus-newsgroup-processable))
3400       (when length
3401         (gnus-data-update-list
3402          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3403
3404 (defun gnus-summary-update-article (article &optional iheader)
3405   "Update ARTICLE in the summary buffer."
3406   (set-buffer gnus-summary-buffer)
3407   (let* ((header (gnus-summary-article-header article))
3408          (id (mail-header-id header))
3409          (data (gnus-data-find article))
3410          (thread (gnus-id-to-thread id))
3411          (references (mail-header-references header))
3412          (parent
3413           (gnus-id-to-thread
3414            (or (gnus-parent-id
3415                 (when (and references
3416                            (not (equal "" references)))
3417                   references))
3418                "none")))
3419          (buffer-read-only nil)
3420          (old (car thread)))
3421     (when thread
3422       (unless iheader
3423         (setcar thread nil)
3424         (when parent
3425           (delq thread parent)))
3426       (if (gnus-summary-insert-subject id header)
3427           ;; Set the (possibly) new article number in the data structure.
3428           (gnus-data-set-number data (gnus-id-to-article id))
3429         (setcar thread old)
3430         nil))))
3431
3432 (defun gnus-rebuild-thread (id &optional line)
3433   "Rebuild the thread containing ID.
3434 If LINE, insert the rebuilt thread starting on line LINE."
3435   (let ((buffer-read-only nil)
3436         old-pos current thread data)
3437     (if (not gnus-show-threads)
3438         (setq thread (list (car (gnus-id-to-thread id))))
3439       ;; Get the thread this article is part of.
3440       (setq thread (gnus-remove-thread id)))
3441     (setq old-pos (gnus-point-at-bol))
3442     (setq current (save-excursion
3443                     (and (zerop (forward-line -1))
3444                          (gnus-summary-article-number))))
3445     ;; If this is a gathered thread, we have to go some re-gathering.
3446     (when (stringp (car thread))
3447       (let ((subject (car thread))
3448             roots thr)
3449         (setq thread (cdr thread))
3450         (while thread
3451           (unless (memq (setq thr (gnus-id-to-thread
3452                                    (gnus-root-id
3453                                     (mail-header-id (caar thread)))))
3454                         roots)
3455             (push thr roots))
3456           (setq thread (cdr thread)))
3457         ;; We now have all (unique) roots.
3458         (if (= (length roots) 1)
3459             ;; All the loose roots are now one solid root.
3460             (setq thread (car roots))
3461           (setq thread (cons subject (gnus-sort-threads roots))))))
3462     (let (threads)
3463       ;; We then insert this thread into the summary buffer.
3464       (when line
3465         (goto-char (point-min))
3466         (forward-line (1- line)))
3467       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3468         (if gnus-show-threads
3469             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3470           (gnus-summary-prepare-unthreaded thread))
3471         (setq data (nreverse gnus-newsgroup-data))
3472         (setq threads gnus-newsgroup-threads))
3473       ;; We splice the new data into the data structure.
3474       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3475       ;;!!! then we want to insert at the beginning of the buffer.
3476       ;;!!! That happens to be true with Gnus now, but that may
3477       ;;!!! change in the future.  Perhaps.
3478       (gnus-data-enter-list
3479        (if line nil current) data (- (point) old-pos))
3480       (setq gnus-newsgroup-threads
3481             (nconc threads gnus-newsgroup-threads))
3482       (gnus-data-compute-positions))))
3483
3484 (defun gnus-number-to-header (number)
3485   "Return the header for article NUMBER."
3486   (let ((headers gnus-newsgroup-headers))
3487     (while (and headers
3488                 (not (= number (mail-header-number (car headers)))))
3489       (pop headers))
3490     (when headers
3491       (car headers))))
3492
3493 (defun gnus-parent-headers (in-headers &optional generation)
3494   "Return the headers of the GENERATIONeth parent of HEADERS."
3495   (unless generation
3496     (setq generation 1))
3497   (let ((parent t)
3498         (headers in-headers)
3499         references)
3500     (while (and parent
3501                 (not (zerop generation))
3502                 (setq references (mail-header-references headers)))
3503       (setq headers (if (and references
3504                              (setq parent (gnus-parent-id references)))
3505                         (car (gnus-id-to-thread parent))
3506                       nil))
3507       (decf generation))
3508     (and (not (eq headers in-headers))
3509          headers)))
3510
3511 (defun gnus-id-to-thread (id)
3512   "Return the (sub-)thread where ID appears."
3513   (gnus-gethash id gnus-newsgroup-dependencies))
3514
3515 (defun gnus-id-to-article (id)
3516   "Return the article number of ID."
3517   (let ((thread (gnus-id-to-thread id)))
3518     (when (and thread
3519                (car thread))
3520       (mail-header-number (car thread)))))
3521
3522 (defun gnus-id-to-header (id)
3523   "Return the article headers of ID."
3524   (car (gnus-id-to-thread id)))
3525
3526 (defun gnus-article-displayed-root-p (article)
3527   "Say whether ARTICLE is a root(ish) article."
3528   (let ((level (gnus-summary-thread-level article))
3529         (refs (mail-header-references  (gnus-summary-article-header article)))
3530         particle)
3531     (cond
3532      ((null level) nil)
3533      ((zerop level) t)
3534      ((null refs) t)
3535      ((null (gnus-parent-id refs)) t)
3536      ((and (= 1 level)
3537            (null (setq particle (gnus-id-to-article
3538                                  (gnus-parent-id refs))))
3539            (null (gnus-summary-thread-level particle)))))))
3540
3541 (defun gnus-root-id (id)
3542   "Return the id of the root of the thread where ID appears."
3543   (let (last-id prev)
3544     (while (and id (setq prev (car (gnus-id-to-thread id))))
3545       (setq last-id id
3546             id (gnus-parent-id (mail-header-references prev))))
3547     last-id))
3548
3549 (defun gnus-articles-in-thread (thread)
3550   "Return the list of articles in THREAD."
3551   (cons (mail-header-number (car thread))
3552         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3553
3554 (defun gnus-remove-thread (id &optional dont-remove)
3555   "Remove the thread that has ID in it."
3556   (let (headers thread last-id)
3557     ;; First go up in this thread until we find the root.
3558     (setq last-id (gnus-root-id id)
3559           headers (message-flatten-list (gnus-id-to-thread last-id)))
3560     ;; We have now found the real root of this thread.  It might have
3561     ;; been gathered into some loose thread, so we have to search
3562     ;; through the threads to find the thread we wanted.
3563     (let ((threads gnus-newsgroup-threads)
3564           sub)
3565       (while threads
3566         (setq sub (car threads))
3567         (if (stringp (car sub))
3568             ;; This is a gathered thread, so we look at the roots
3569             ;; below it to find whether this article is in this
3570             ;; gathered root.
3571             (progn
3572               (setq sub (cdr sub))
3573               (while sub
3574                 (when (member (caar sub) headers)
3575                   (setq thread (car threads)
3576                         threads nil
3577                         sub nil))
3578                 (setq sub (cdr sub))))
3579           ;; It's an ordinary thread, so we check it.
3580           (when (eq (car sub) (car headers))
3581             (setq thread sub
3582                   threads nil)))
3583         (setq threads (cdr threads)))
3584       ;; If this article is in no thread, then it's a root.
3585       (if thread
3586           (unless dont-remove
3587             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3588         (setq thread (gnus-id-to-thread last-id)))
3589       (when thread
3590         (prog1
3591             thread                      ; We return this thread.
3592           (unless dont-remove
3593             (if (stringp (car thread))
3594                 (progn
3595                   ;; If we use dummy roots, then we have to remove the
3596                   ;; dummy root as well.
3597                   (when (eq gnus-summary-make-false-root 'dummy)
3598                     ;; We go to the dummy root by going to
3599                     ;; the first sub-"thread", and then one line up.
3600                     (gnus-summary-goto-article
3601                      (mail-header-number (caadr thread)))
3602                     (forward-line -1)
3603                     (gnus-delete-line)
3604                     (gnus-data-compute-positions))
3605                   (setq thread (cdr thread))
3606                   (while thread
3607                     (gnus-remove-thread-1 (car thread))
3608                     (setq thread (cdr thread))))
3609               (gnus-summary-show-all-threads)
3610               (gnus-remove-thread-1 thread))))))))
3611
3612 (defun gnus-remove-thread-1 (thread)
3613   "Remove the thread THREAD recursively."
3614   (let ((number (mail-header-number (pop thread)))
3615         d)
3616     (setq thread (reverse thread))
3617     (while thread
3618       (gnus-remove-thread-1 (pop thread)))
3619     (when (setq d (gnus-data-find number))
3620       (goto-char (gnus-data-pos d))
3621       (gnus-data-remove
3622        number
3623        (- (gnus-point-at-bol)
3624           (prog1
3625               (1+ (gnus-point-at-eol))
3626             (gnus-delete-line)))))))
3627
3628 (defun gnus-sort-threads (threads)
3629   "Sort THREADS."
3630   (if (not gnus-thread-sort-functions)
3631       threads
3632     (gnus-message 8 "Sorting threads...")
3633     (prog1
3634         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3635       (gnus-message 8 "Sorting threads...done"))))
3636
3637 (defun gnus-sort-articles (articles)
3638   "Sort ARTICLES."
3639   (when gnus-article-sort-functions
3640     (gnus-message 7 "Sorting articles...")
3641     (prog1
3642         (setq gnus-newsgroup-headers
3643               (sort articles (gnus-make-sort-function
3644                               gnus-article-sort-functions)))
3645       (gnus-message 7 "Sorting articles...done"))))
3646
3647 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3648 (defmacro gnus-thread-header (thread)
3649   ;; Return header of first article in THREAD.
3650   ;; Note that THREAD must never, ever be anything else than a variable -
3651   ;; using some other form will lead to serious barfage.
3652   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3653   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3654   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3655         (vector thread) 2))
3656
3657 (defsubst gnus-article-sort-by-number (h1 h2)
3658   "Sort articles by article number."
3659   (< (mail-header-number h1)
3660      (mail-header-number h2)))
3661
3662 (defun gnus-thread-sort-by-number (h1 h2)
3663   "Sort threads by root article number."
3664   (gnus-article-sort-by-number
3665    (gnus-thread-header h1) (gnus-thread-header h2)))
3666
3667 (defsubst gnus-article-sort-by-lines (h1 h2)
3668   "Sort articles by article Lines header."
3669   (< (mail-header-lines h1)
3670      (mail-header-lines h2)))
3671
3672 (defun gnus-thread-sort-by-lines (h1 h2)
3673   "Sort threads by root article Lines header."
3674   (gnus-article-sort-by-lines
3675    (gnus-thread-header h1) (gnus-thread-header h2)))
3676
3677 (defsubst gnus-article-sort-by-chars (h1 h2)
3678   "Sort articles by octet length."
3679   (< (mail-header-chars h1)
3680      (mail-header-chars h2)))
3681
3682 (defun gnus-thread-sort-by-chars (h1 h2)
3683   "Sort threads by root article octet length."
3684   (gnus-article-sort-by-chars
3685    (gnus-thread-header h1) (gnus-thread-header h2)))
3686
3687 (defsubst gnus-article-sort-by-author (h1 h2)
3688   "Sort articles by root author."
3689   (string-lessp
3690    (let ((addr (car (mime-read-field 'From h1))))
3691      (or (std11-full-name-string addr)
3692          (std11-address-string addr)
3693          ""))
3694    (let ((addr (car (mime-read-field 'From h2))))
3695      (or (std11-full-name-string addr)
3696          (std11-address-string addr)
3697          ""))
3698    ))
3699
3700 (defun gnus-thread-sort-by-author (h1 h2)
3701   "Sort threads by root author."
3702   (gnus-article-sort-by-author
3703    (gnus-thread-header h1)  (gnus-thread-header h2)))
3704
3705 (defsubst gnus-article-sort-by-subject (h1 h2)
3706   "Sort articles by root subject."
3707   (string-lessp
3708    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3709    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3710
3711 (defun gnus-thread-sort-by-subject (h1 h2)
3712   "Sort threads by root subject."
3713   (gnus-article-sort-by-subject
3714    (gnus-thread-header h1) (gnus-thread-header h2)))
3715
3716 (defsubst gnus-article-sort-by-date (h1 h2)
3717   "Sort articles by root article date."
3718   (time-less-p
3719    (gnus-date-get-time (mail-header-date h1))
3720    (gnus-date-get-time (mail-header-date h2))))
3721
3722 (defun gnus-thread-sort-by-date (h1 h2)
3723   "Sort threads by root article date."
3724   (gnus-article-sort-by-date
3725    (gnus-thread-header h1) (gnus-thread-header h2)))
3726
3727 (defsubst gnus-article-sort-by-score (h1 h2)
3728   "Sort articles by root article score.
3729 Unscored articles will be counted as having a score of zero."
3730   (> (or (cdr (assq (mail-header-number h1)
3731                     gnus-newsgroup-scored))
3732          gnus-summary-default-score 0)
3733      (or (cdr (assq (mail-header-number h2)
3734                     gnus-newsgroup-scored))
3735          gnus-summary-default-score 0)))
3736
3737 (defun gnus-thread-sort-by-score (h1 h2)
3738   "Sort threads by root article score."
3739   (gnus-article-sort-by-score
3740    (gnus-thread-header h1) (gnus-thread-header h2)))
3741
3742 (defun gnus-thread-sort-by-total-score (h1 h2)
3743   "Sort threads by the sum of all scores in the thread.
3744 Unscored articles will be counted as having a score of zero."
3745   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3746
3747 (defun gnus-thread-total-score (thread)
3748   ;; This function find the total score of THREAD.
3749   (cond ((null thread)
3750          0)
3751         ((consp thread)
3752          (if (stringp (car thread))
3753              (apply gnus-thread-score-function 0
3754                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3755            (gnus-thread-total-score-1 thread)))
3756         (t
3757          (gnus-thread-total-score-1 (list thread)))))
3758
3759 (defun gnus-thread-total-score-1 (root)
3760   ;; This function find the total score of the thread below ROOT.
3761   (setq root (car root))
3762   (apply gnus-thread-score-function
3763          (or (append
3764               (mapcar 'gnus-thread-total-score
3765                       (cdr (gnus-id-to-thread (mail-header-id root))))
3766               (when (> (mail-header-number root) 0)
3767                 (list (or (cdr (assq (mail-header-number root)
3768                                      gnus-newsgroup-scored))
3769                           gnus-summary-default-score 0))))
3770              (list gnus-summary-default-score)
3771              '(0))))
3772
3773 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3774 (defvar gnus-tmp-prev-subject nil)
3775 (defvar gnus-tmp-false-parent nil)
3776 (defvar gnus-tmp-root-expunged nil)
3777 (defvar gnus-tmp-dummy-line nil)
3778
3779 (defvar gnus-tmp-header)
3780 (defun gnus-extra-header (type &optional header)
3781   "Return the extra header of TYPE."
3782   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3783       ""))
3784
3785 (defun gnus-summary-prepare-threads (threads)
3786   "Prepare summary buffer from THREADS and indentation LEVEL.
3787 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3788 or a straight list of headers."
3789   (gnus-message 7 "Generating summary...")
3790
3791   (setq gnus-newsgroup-threads threads)
3792   (beginning-of-line)
3793
3794   (let ((gnus-tmp-level 0)
3795         (default-score (or gnus-summary-default-score 0))
3796         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3797         thread number subject stack state gnus-tmp-gathered beg-match
3798         new-roots gnus-tmp-new-adopts thread-end
3799         gnus-tmp-header gnus-tmp-unread
3800         gnus-tmp-replied gnus-tmp-subject-or-nil
3801         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3802         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3803         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3804
3805     (setq gnus-tmp-prev-subject nil)
3806
3807     (if (vectorp (car threads))
3808         ;; If this is a straight (sic) list of headers, then a
3809         ;; threaded summary display isn't required, so we just create
3810         ;; an unthreaded one.
3811         (gnus-summary-prepare-unthreaded threads)
3812
3813       ;; Do the threaded display.
3814
3815       (while (or threads stack gnus-tmp-new-adopts new-roots)
3816
3817         (if (and (= gnus-tmp-level 0)
3818                  (or (not stack)
3819                      (= (caar stack) 0))
3820                  (not gnus-tmp-false-parent)
3821                  (or gnus-tmp-new-adopts new-roots))
3822             (if gnus-tmp-new-adopts
3823                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3824                       thread (list (car gnus-tmp-new-adopts))
3825                       gnus-tmp-header (caar thread)
3826                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3827               (when new-roots
3828                 (setq thread (list (car new-roots))
3829                       gnus-tmp-header (caar thread)
3830                       new-roots (cdr new-roots))))
3831
3832           (if threads
3833               ;; If there are some threads, we do them before the
3834               ;; threads on the stack.
3835               (setq thread threads
3836                     gnus-tmp-header (caar thread))
3837             ;; There were no current threads, so we pop something off
3838             ;; the stack.
3839             (setq state (car stack)
3840                   gnus-tmp-level (car state)
3841                   thread (cdr state)
3842                   stack (cdr stack)
3843                   gnus-tmp-header (caar thread))))
3844
3845         (setq gnus-tmp-false-parent nil)
3846         (setq gnus-tmp-root-expunged nil)
3847         (setq thread-end nil)
3848
3849         (if (stringp gnus-tmp-header)
3850             ;; The header is a dummy root.
3851             (cond
3852              ((eq gnus-summary-make-false-root 'adopt)
3853               ;; We let the first article adopt the rest.
3854               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3855                                                (cddar thread)))
3856               (setq gnus-tmp-gathered
3857                     (nconc (mapcar
3858                             (lambda (h) (mail-header-number (car h)))
3859                             (cddar thread))
3860                            gnus-tmp-gathered))
3861               (setq thread (cons (list (caar thread)
3862                                        (cadar thread))
3863                                  (cdr thread)))
3864               (setq gnus-tmp-level -1
3865                     gnus-tmp-false-parent t))
3866              ((eq gnus-summary-make-false-root 'empty)
3867               ;; We print adopted articles with empty subject fields.
3868               (setq gnus-tmp-gathered
3869                     (nconc (mapcar
3870                             (lambda (h) (mail-header-number (car h)))
3871                             (cddar thread))
3872                            gnus-tmp-gathered))
3873               (setq gnus-tmp-level -1))
3874              ((eq gnus-summary-make-false-root 'dummy)
3875               ;; We remember that we probably want to output a dummy
3876               ;; root.
3877               (setq gnus-tmp-dummy-line gnus-tmp-header)
3878               (setq gnus-tmp-prev-subject gnus-tmp-header))
3879              (t
3880               ;; We do not make a root for the gathered
3881               ;; sub-threads at all.
3882               (setq gnus-tmp-level -1)))
3883
3884           (setq number (mail-header-number gnus-tmp-header)
3885                 subject (mail-header-subject gnus-tmp-header))
3886
3887           (cond
3888            ;; If the thread has changed subject, we might want to make
3889            ;; this subthread into a root.
3890            ((and (null gnus-thread-ignore-subject)
3891                  (not (zerop gnus-tmp-level))
3892                  gnus-tmp-prev-subject
3893                  (not (inline
3894                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3895             (setq new-roots (nconc new-roots (list (car thread)))
3896                   thread-end t
3897                   gnus-tmp-header nil))
3898            ;; If the article lies outside the current limit,
3899            ;; then we do not display it.
3900            ((not (memq number gnus-newsgroup-limit))
3901             (setq gnus-tmp-gathered
3902                   (nconc (mapcar
3903                           (lambda (h) (mail-header-number (car h)))
3904                           (cdar thread))
3905                          gnus-tmp-gathered))
3906             (setq gnus-tmp-new-adopts (if (cdar thread)
3907                                           (append gnus-tmp-new-adopts
3908                                                   (cdar thread))
3909                                         gnus-tmp-new-adopts)
3910                   thread-end t
3911                   gnus-tmp-header nil)
3912             (when (zerop gnus-tmp-level)
3913               (setq gnus-tmp-root-expunged t)))
3914            ;; Perhaps this article is to be marked as read?
3915            ((and gnus-summary-mark-below
3916                  (< (or (cdr (assq number gnus-newsgroup-scored))
3917                         default-score)
3918                     gnus-summary-mark-below)
3919                  ;; Don't touch sparse articles.
3920                  (not (gnus-summary-article-sparse-p number))
3921                  (not (gnus-summary-article-ancient-p number)))
3922             (setq gnus-newsgroup-unreads
3923                   (delq number gnus-newsgroup-unreads))
3924             (if gnus-newsgroup-auto-expire
3925                 (push number gnus-newsgroup-expirable)
3926               (push (cons number gnus-low-score-mark)
3927                     gnus-newsgroup-reads))))
3928
3929           (when gnus-tmp-header
3930             ;; We may have an old dummy line to output before this
3931             ;; article.
3932             (when (and gnus-tmp-dummy-line
3933                        (gnus-subject-equal
3934                         gnus-tmp-dummy-line
3935                         (mail-header-subject gnus-tmp-header)))
3936               (gnus-summary-insert-dummy-line
3937                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3938               (setq gnus-tmp-dummy-line nil))
3939
3940             ;; Compute the mark.
3941             (setq gnus-tmp-unread (gnus-article-mark number))
3942
3943             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3944                                   gnus-tmp-header gnus-tmp-level)
3945                   gnus-newsgroup-data)
3946
3947             ;; Actually insert the line.
3948             (setq
3949              gnus-tmp-subject-or-nil
3950              (cond
3951               ((and gnus-thread-ignore-subject
3952                     gnus-tmp-prev-subject
3953                     (not (inline (gnus-subject-equal
3954                                   gnus-tmp-prev-subject subject))))
3955                subject)
3956               ((zerop gnus-tmp-level)
3957                (if (and (eq gnus-summary-make-false-root 'empty)
3958                         (memq number gnus-tmp-gathered)
3959                         gnus-tmp-prev-subject
3960                         (inline (gnus-subject-equal
3961                                  gnus-tmp-prev-subject subject)))
3962                    gnus-summary-same-subject
3963                  subject))
3964               (t gnus-summary-same-subject)))
3965             (if (and (eq gnus-summary-make-false-root 'adopt)
3966                      (= gnus-tmp-level 1)
3967                      (memq number gnus-tmp-gathered))
3968                 (setq gnus-tmp-opening-bracket ?\<
3969                       gnus-tmp-closing-bracket ?\>)
3970               (setq gnus-tmp-opening-bracket ?\[
3971                     gnus-tmp-closing-bracket ?\]))
3972             (setq
3973              gnus-tmp-indentation
3974              (aref gnus-thread-indent-array gnus-tmp-level)
3975              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3976              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3977                                 gnus-summary-default-score 0)
3978              gnus-tmp-score-char
3979              (if (or (null gnus-summary-default-score)
3980                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3981                          gnus-summary-zcore-fuzz))
3982                  ? ;Whitespace
3983                (if (< gnus-tmp-score gnus-summary-default-score)
3984                    gnus-score-below-mark gnus-score-over-mark))
3985              gnus-tmp-replied
3986              (cond ((memq number gnus-newsgroup-processable)
3987                     gnus-process-mark)
3988                    ((memq number gnus-newsgroup-cached)
3989                     gnus-cached-mark)
3990                    ((memq number gnus-newsgroup-replied)
3991                     gnus-replied-mark)
3992                    ((memq number gnus-newsgroup-saved)
3993                     gnus-saved-mark)
3994                    (t gnus-unread-mark))
3995              gnus-tmp-from (mail-header-from gnus-tmp-header)
3996              gnus-tmp-name
3997              (cond
3998               ((string-match "<[^>]+> *$" gnus-tmp-from)
3999                (setq beg-match (match-beginning 0))
4000                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4001                         (substring gnus-tmp-from (1+ (match-beginning 0))
4002                                    (1- (match-end 0))))
4003                    (substring gnus-tmp-from 0 beg-match)))
4004               ((string-match "(.+)" gnus-tmp-from)
4005                (substring gnus-tmp-from
4006                           (1+ (match-beginning 0)) (1- (match-end 0))))
4007               (t gnus-tmp-from)))
4008             (when (string= gnus-tmp-name "")
4009               (setq gnus-tmp-name gnus-tmp-from))
4010             (unless (numberp gnus-tmp-lines)
4011               (setq gnus-tmp-lines 0))
4012             (gnus-put-text-property
4013              (point)
4014              (progn (eval gnus-summary-line-format-spec) (point))
4015              'gnus-number number)
4016             (when gnus-visual-p
4017               (forward-line -1)
4018               (gnus-run-hooks 'gnus-summary-update-hook)
4019               (forward-line 1))
4020
4021             (setq gnus-tmp-prev-subject subject)))
4022
4023         (when (nth 1 thread)
4024           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4025         (incf gnus-tmp-level)
4026         (setq threads (if thread-end nil (cdar thread)))
4027         (unless threads
4028           (setq gnus-tmp-level 0)))))
4029   (gnus-message 7 "Generating summary...done"))
4030
4031 (defun gnus-summary-prepare-unthreaded (headers)
4032   "Generate an unthreaded summary buffer based on HEADERS."
4033   (let (header number mark)
4034
4035     (beginning-of-line)
4036
4037     (while headers
4038       ;; We may have to root out some bad articles...
4039       (when (memq (setq number (mail-header-number
4040                                 (setq header (pop headers))))
4041                   gnus-newsgroup-limit)
4042         ;; Mark article as read when it has a low score.
4043         (when (and gnus-summary-mark-below
4044                    (< (or (cdr (assq number gnus-newsgroup-scored))
4045                           gnus-summary-default-score 0)
4046                       gnus-summary-mark-below)
4047                    (not (gnus-summary-article-ancient-p number)))
4048           (setq gnus-newsgroup-unreads
4049                 (delq number gnus-newsgroup-unreads))
4050           (if gnus-newsgroup-auto-expire
4051               (push number gnus-newsgroup-expirable)
4052             (push (cons number gnus-low-score-mark)
4053                   gnus-newsgroup-reads)))
4054
4055         (setq mark (gnus-article-mark number))
4056         (push (gnus-data-make number mark (1+ (point)) header 0)
4057               gnus-newsgroup-data)
4058         (gnus-summary-insert-line
4059          header 0 number
4060          mark (memq number gnus-newsgroup-replied)
4061          (memq number gnus-newsgroup-expirable)
4062          (mail-header-subject header) nil
4063          (cdr (assq number gnus-newsgroup-scored))
4064          (memq number gnus-newsgroup-processable))))))
4065
4066 (defun gnus-summary-remove-list-identifiers ()
4067   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4068   (let ((regexp (if (stringp gnus-list-identifiers)
4069                     gnus-list-identifiers
4070                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4071     (dolist (header gnus-newsgroup-headers)
4072       (when (string-match (concat "\\(Re: +\\)?\\(" regexp " *\\)")
4073                           (mail-header-subject header))
4074         (mail-header-set-subject
4075          header (concat (substring (mail-header-subject header)
4076                                    0 (match-beginning 2))
4077                         (substring (mail-header-subject header)
4078                                    (match-end 2))))))))
4079
4080 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4081   "Select newsgroup GROUP.
4082 If READ-ALL is non-nil, all articles in the group are selected.
4083 If SELECT-ARTICLES, only select those articles from GROUP."
4084   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4085          ;;!!! Dirty hack; should be removed.
4086          (gnus-summary-ignore-duplicates
4087           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4088               t
4089             gnus-summary-ignore-duplicates))
4090          (info (nth 2 entry))
4091          articles fetched-articles cached)
4092
4093     (unless (gnus-check-server
4094              (setq gnus-current-select-method
4095                    (gnus-find-method-for-group group)))
4096       (error "Couldn't open server"))
4097
4098     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4099         (gnus-activate-group group)     ; Or we can activate it...
4100         (progn                          ; Or we bug out.
4101           (when (equal major-mode 'gnus-summary-mode)
4102             (kill-buffer (current-buffer)))
4103           (error "Couldn't request group %s: %s"
4104                  group (gnus-status-message group))))
4105
4106     (unless (gnus-request-group group t)
4107       (when (equal major-mode 'gnus-summary-mode)
4108         (kill-buffer (current-buffer)))
4109       (error "Couldn't request group %s: %s"
4110              group (gnus-status-message group)))
4111
4112     (setq gnus-newsgroup-name group)
4113     (setq gnus-newsgroup-unselected nil)
4114     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4115     (gnus-summary-setup-default-charset)
4116
4117     ;; Adjust and set lists of article marks.
4118     (when info
4119       (gnus-adjust-marked-articles info))
4120
4121     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4122     (setq cached
4123           (if (gnus-virtual-group-p group)
4124               gnus-newsgroup-cached
4125             (gnus-cache-articles-in-group group)))
4126
4127     (setq gnus-newsgroup-unreads
4128           (gnus-set-difference
4129            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4130            gnus-newsgroup-dormant))
4131
4132     (setq gnus-newsgroup-processable nil)
4133
4134     (gnus-update-read-articles group gnus-newsgroup-unreads)
4135
4136     (if (setq articles select-articles)
4137         (setq gnus-newsgroup-unselected
4138               (gnus-sorted-intersection
4139                gnus-newsgroup-unreads
4140                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4141       (setq articles (gnus-articles-to-read group read-all)))
4142
4143     (cond
4144      ((null articles)
4145       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4146       'quit)
4147      ((eq articles 0) nil)
4148      (t
4149       ;; Init the dependencies hash table.
4150       (setq gnus-newsgroup-dependencies
4151             (gnus-make-hashtable (length articles)))
4152       (gnus-set-global-variables)
4153       ;; Retrieve the headers and read them in.
4154       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4155       (setq gnus-newsgroup-headers
4156             (gnus-retrieve-parsed-headers
4157              articles gnus-newsgroup-name
4158              ;; We might want to fetch old headers, but
4159              ;; not if there is only 1 article.
4160              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4161                            (not (numberp gnus-fetch-old-headers)))
4162                       (> (length articles) 1))
4163                   gnus-fetch-old-headers)))
4164       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4165
4166       ;; Suppress duplicates?
4167       (when gnus-suppress-duplicates
4168         (gnus-dup-suppress-articles))
4169
4170       ;; Set the initial limit.
4171       (setq gnus-newsgroup-limit (copy-sequence articles))
4172       ;; Remove canceled articles from the list of unread articles.
4173       (setq gnus-newsgroup-unreads
4174             (gnus-set-sorted-intersection
4175              gnus-newsgroup-unreads
4176              (setq fetched-articles
4177                    (mapcar (lambda (headers) (mail-header-number headers))
4178                            gnus-newsgroup-headers))))
4179       ;; Removed marked articles that do not exist.
4180       (gnus-update-missing-marks
4181        (gnus-sorted-complement fetched-articles articles))
4182
4183       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4184       (when cached
4185         (setq gnus-newsgroup-cached cached))
4186
4187       ;; We might want to build some more threads first.
4188       (when (and gnus-fetch-old-headers
4189                  (eq gnus-headers-retrieved-by 'nov))
4190         (if (eq gnus-fetch-old-headers 'invisible)
4191             (gnus-build-all-threads)
4192           (gnus-build-old-threads)))
4193       ;; Let the Gnus agent mark articles as read.
4194       (when gnus-agent
4195         (gnus-agent-get-undownloaded-list))
4196       ;; Remove list identifiers from subject
4197       (when gnus-list-identifiers
4198         (gnus-summary-remove-list-identifiers))
4199       ;; Check whether auto-expire is to be done in this group.
4200       (setq gnus-newsgroup-auto-expire
4201             (gnus-group-auto-expirable-p group))
4202       ;; Set up the article buffer now, if necessary.
4203       (unless gnus-single-article-buffer
4204         (gnus-article-setup-buffer))
4205       ;; First and last article in this newsgroup.
4206       (when gnus-newsgroup-headers
4207         (setq gnus-newsgroup-begin
4208               (mail-header-number (car gnus-newsgroup-headers))
4209               gnus-newsgroup-end
4210               (mail-header-number
4211                (gnus-last-element gnus-newsgroup-headers))))
4212       ;; GROUP is successfully selected.
4213       (or gnus-newsgroup-headers t)))))
4214
4215 (defun gnus-articles-to-read (group &optional read-all)
4216   ;; Find out what articles the user wants to read.
4217   (let* ((articles
4218           ;; Select all articles if `read-all' is non-nil, or if there
4219           ;; are no unread articles.
4220           (if (or read-all
4221                   (and (zerop (length gnus-newsgroup-marked))
4222                        (zerop (length gnus-newsgroup-unreads)))
4223                   (eq (gnus-group-find-parameter group 'display)
4224                       'all))
4225               (or
4226                (gnus-uncompress-range (gnus-active group))
4227                (gnus-cache-articles-in-group group))
4228             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4229                           (copy-sequence gnus-newsgroup-unreads))
4230                   '<)))
4231          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4232          (scored (length scored-list))
4233          (number (length articles))
4234          (marked (+ (length gnus-newsgroup-marked)
4235                     (length gnus-newsgroup-dormant)))
4236          (select
4237           (cond
4238            ((numberp read-all)
4239             read-all)
4240            (t
4241             (condition-case ()
4242                 (cond
4243                  ((and (or (<= scored marked) (= scored number))
4244                        (natnump gnus-large-newsgroup)
4245                        (> number gnus-large-newsgroup))
4246                   (let ((input (read-from-minibuffer
4247                                 (format
4248                                  "How many articles from %s (max %d): "
4249                                  (gnus-limit-string gnus-newsgroup-name 35)
4250                                  number)
4251                                 (static-if (< emacs-major-version 20)
4252                                     (number-to-string gnus-large-newsgroup)
4253                                   (cons
4254                                    (number-to-string gnus-large-newsgroup)
4255                                    0)))))
4256                     (if (string-match "^[ \t]*$" input)
4257                         number
4258                       input)))
4259                  ((and (> scored marked) (< scored number)
4260                        (> (- scored number) 20))
4261                   (let ((input
4262                          (read-string
4263                           (format "%s %s (%d scored, %d total): "
4264                                   "How many articles from"
4265                                   group scored number))))
4266                     (if (string-match "^[ \t]*$" input)
4267                         number input)))
4268                  (t number))
4269               (quit nil))))))
4270     (setq select (if (stringp select) (string-to-number select) select))
4271     (if (or (null select) (zerop select))
4272         select
4273       (if (and (not (zerop scored)) (<= (abs select) scored))
4274           (progn
4275             (setq articles (sort scored-list '<))
4276             (setq number (length articles)))
4277         (setq articles (copy-sequence articles)))
4278
4279       (when (< (abs select) number)
4280         (if (< select 0)
4281             ;; Select the N oldest articles.
4282             (setcdr (nthcdr (1- (abs select)) articles) nil)
4283           ;; Select the N most recent articles.
4284           (setq articles (nthcdr (- number select) articles))))
4285       (setq gnus-newsgroup-unselected
4286             (gnus-sorted-intersection
4287              gnus-newsgroup-unreads
4288              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4289       articles)))
4290
4291 (defun gnus-killed-articles (killed articles)
4292   (let (out)
4293     (while articles
4294       (when (inline (gnus-member-of-range (car articles) killed))
4295         (push (car articles) out))
4296       (setq articles (cdr articles)))
4297     out))
4298
4299 (defun gnus-uncompress-marks (marks)
4300   "Uncompress the mark ranges in MARKS."
4301   (let ((uncompressed '(score bookmark))
4302         out)
4303     (while marks
4304       (if (memq (caar marks) uncompressed)
4305           (push (car marks) out)
4306         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4307       (setq marks (cdr marks)))
4308     out))
4309
4310 (defun gnus-adjust-marked-articles (info)
4311   "Set all article lists and remove all marks that are no longer valid."
4312   (let* ((marked-lists (gnus-info-marks info))
4313          (active (gnus-active (gnus-info-group info)))
4314          (min (car active))
4315          (max (cdr active))
4316          (types gnus-article-mark-lists)
4317          (uncompressed '(score bookmark killed))
4318          marks var articles article mark)
4319
4320     (while marked-lists
4321       (setq marks (pop marked-lists))
4322       (set (setq var (intern (format "gnus-newsgroup-%s"
4323                                      (car (rassq (setq mark (car marks))
4324                                                  types)))))
4325            (if (memq (car marks) uncompressed) (cdr marks)
4326              (gnus-uncompress-range (cdr marks))))
4327
4328       (setq articles (symbol-value var))
4329
4330       ;; All articles have to be subsets of the active articles.
4331       (cond
4332        ;; Adjust "simple" lists.
4333        ((memq mark '(tick dormant expire reply save))
4334         (while articles
4335           (when (or (< (setq article (pop articles)) min) (> article max))
4336             (set var (delq article (symbol-value var))))))
4337        ;; Adjust assocs.
4338        ((memq mark uncompressed)
4339         (when (not (listp (cdr (symbol-value var))))
4340           (set var (list (symbol-value var))))
4341         (when (not (listp (cdr articles)))
4342           (setq articles (list articles)))
4343         (while articles
4344           (when (or (not (consp (setq article (pop articles))))
4345                     (< (car article) min)
4346                     (> (car article) max))
4347             (set var (delq article (symbol-value var))))))))))
4348
4349 (defun gnus-update-missing-marks (missing)
4350   "Go through the list of MISSING articles and remove them from the mark lists."
4351   (when missing
4352     (let ((types gnus-article-mark-lists)
4353           var m)
4354       ;; Go through all types.
4355       (while types
4356         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4357         (when (symbol-value var)
4358           ;; This list has articles.  So we delete all missing articles
4359           ;; from it.
4360           (setq m missing)
4361           (while m
4362             (set var (delq (pop m) (symbol-value var)))))))))
4363
4364 (defun gnus-update-marks ()
4365   "Enter the various lists of marked articles into the newsgroup info list."
4366   (let ((types gnus-article-mark-lists)
4367         (info (gnus-get-info gnus-newsgroup-name))
4368         (uncompressed '(score bookmark killed))
4369         type list newmarked symbol delta-marks)
4370     (when info
4371       ;; Add all marks lists to the list of marks lists.
4372       (while (setq type (pop types))
4373         (setq list (symbol-value
4374                           (setq symbol
4375                                 (intern (format "gnus-newsgroup-%s"
4376                                                 (car type))))))
4377
4378         (when list
4379           ;; Get rid of the entries of the articles that have the
4380           ;; default score.
4381           (when (and (eq (cdr type) 'score)
4382                      gnus-save-score
4383                      list)
4384             (let* ((arts list)
4385                    (prev (cons nil list))
4386                    (all prev))
4387               (while arts
4388                 (if (or (not (consp (car arts)))
4389                         (= (cdar arts) gnus-summary-default-score))
4390                     (setcdr prev (cdr arts))
4391                   (setq prev arts))
4392                 (setq arts (cdr arts)))
4393               (setq list (cdr all)))))
4394
4395        (or (memq (cdr type) uncompressed)
4396            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4397        
4398        (when (gnus-check-backend-function 'request-set-mark
4399                                           gnus-newsgroup-name)
4400          ;; uncompressed:s are not proper flags (they are cons cells)
4401          ;; cache is a internal gnus flag
4402          (unless (memq (cdr type) (cons 'cache uncompressed))
4403            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4404                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4405                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4406              (if add
4407                  (push (list add 'add (list (cdr type))) delta-marks))
4408              (if del
4409                  (push (list del 'del (list (cdr type))) delta-marks)))))
4410           
4411         (when list
4412          (push (cons (cdr type) list) newmarked)))
4413
4414       (when delta-marks
4415         (unless (gnus-check-group gnus-newsgroup-name)
4416           (error "Can't open server for %s" gnus-newsgroup-name))
4417         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4418           
4419       ;; Enter these new marks into the info of the group.
4420       (if (nthcdr 3 info)
4421           (setcar (nthcdr 3 info) newmarked)
4422         ;; Add the marks lists to the end of the info.
4423         (when newmarked
4424           (setcdr (nthcdr 2 info) (list newmarked))))
4425
4426       ;; Cut off the end of the info if there's nothing else there.
4427       (let ((i 5))
4428         (while (and (> i 2)
4429                     (not (nth i info)))
4430           (when (nthcdr (decf i) info)
4431             (setcdr (nthcdr i info) nil)))))))
4432
4433 (defun gnus-set-mode-line (where)
4434   "This function sets the mode line of the article or summary buffers.
4435 If WHERE is `summary', the summary mode line format will be used."
4436   ;; Is this mode line one we keep updated?
4437   (when (and (memq where gnus-updated-mode-lines)
4438              (symbol-value
4439               (intern (format "gnus-%s-mode-line-format-spec" where))))
4440     (let (mode-string)
4441       (save-excursion
4442         ;; We evaluate this in the summary buffer since these
4443         ;; variables are buffer-local to that buffer.
4444         (set-buffer gnus-summary-buffer)
4445         ;; We bind all these variables that are used in the `eval' form
4446         ;; below.
4447         (let* ((mformat (symbol-value
4448                          (intern
4449                           (format "gnus-%s-mode-line-format-spec" where))))
4450                (gnus-tmp-group-name gnus-newsgroup-name)
4451                (gnus-tmp-article-number (or gnus-current-article 0))
4452                (gnus-tmp-unread gnus-newsgroup-unreads)
4453                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4454                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4455                (gnus-tmp-unread-and-unselected
4456                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4457                             (zerop gnus-tmp-unselected))
4458                        "")
4459                       ((zerop gnus-tmp-unselected)
4460                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4461                       (t (format "{%d(+%d) more}"
4462                                  gnus-tmp-unread-and-unticked
4463                                  gnus-tmp-unselected))))
4464                (gnus-tmp-subject
4465                 (if (and gnus-current-headers
4466                          (vectorp gnus-current-headers))
4467                     (gnus-mode-string-quote
4468                      (mail-header-subject gnus-current-headers))
4469                   ""))
4470                bufname-length max-len
4471                gnus-tmp-header);; passed as argument to any user-format-funcs
4472           (setq mode-string (eval mformat))
4473           (setq bufname-length (if (string-match "%b" mode-string)
4474                                    (- (length
4475                                        (buffer-name
4476                                         (if (eq where 'summary)
4477                                             nil
4478                                           (get-buffer gnus-article-buffer))))
4479                                       2)
4480                                  0))
4481           (setq max-len (max 4 (if gnus-mode-non-string-length
4482                                    (- (window-width)
4483                                       gnus-mode-non-string-length
4484                                       bufname-length)
4485                                  (length mode-string))))
4486           ;; We might have to chop a bit of the string off...
4487           (when (> (length mode-string) max-len)
4488             (setq mode-string
4489                   (concat (gnus-truncate-string mode-string (- max-len 3))
4490                           "...")))
4491           ;; Pad the mode string a bit.
4492           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4493       ;; Update the mode line.
4494       (setq mode-line-buffer-identification
4495             (gnus-mode-line-buffer-identification (list mode-string)))
4496       (set-buffer-modified-p t))))
4497
4498 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4499   "Go through the HEADERS list and add all Xrefs to a hash table.
4500 The resulting hash table is returned, or nil if no Xrefs were found."
4501   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4502          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4503          (xref-hashtb (gnus-make-hashtable))
4504          start group entry number xrefs header)
4505     (while headers
4506       (setq header (pop headers))
4507       (when (and (setq xrefs (mail-header-xref header))
4508                  (not (memq (setq number (mail-header-number header))
4509                             unreads)))
4510         (setq start 0)
4511         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4512           (setq start (match-end 0))
4513           (setq group (if prefix
4514                           (concat prefix (substring xrefs (match-beginning 1)
4515                                                     (match-end 1)))
4516                         (substring xrefs (match-beginning 1) (match-end 1))))
4517           (setq number
4518                 (string-to-int (substring xrefs (match-beginning 2)
4519                                           (match-end 2))))
4520           (if (setq entry (gnus-gethash group xref-hashtb))
4521               (setcdr entry (cons number (cdr entry)))
4522             (gnus-sethash group (cons number nil) xref-hashtb)))))
4523     (and start xref-hashtb)))
4524
4525 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4526   "Look through all the headers and mark the Xrefs as read."
4527   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4528         name entry info xref-hashtb idlist method nth4)
4529     (save-excursion
4530       (set-buffer gnus-group-buffer)
4531       (when (setq xref-hashtb
4532                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4533         (mapatoms
4534          (lambda (group)
4535            (unless (string= from-newsgroup (setq name (symbol-name group)))
4536              (setq idlist (symbol-value group))
4537              ;; Dead groups are not updated.
4538              (and (prog1
4539                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4540                             info (nth 2 entry))
4541                     (when (stringp (setq nth4 (gnus-info-method info)))
4542                       (setq nth4 (gnus-server-to-method nth4))))
4543                   ;; Only do the xrefs if the group has the same
4544                   ;; select method as the group we have just read.
4545                   (or (gnus-methods-equal-p
4546                        nth4 (gnus-find-method-for-group from-newsgroup))
4547                       virtual
4548                       (equal nth4 (setq method (gnus-find-method-for-group
4549                                                 from-newsgroup)))
4550                       (and (equal (car nth4) (car method))
4551                            (equal (nth 1 nth4) (nth 1 method))))
4552                   gnus-use-cross-reference
4553                   (or (not (eq gnus-use-cross-reference t))
4554                       virtual
4555                       ;; Only do cross-references on subscribed
4556                       ;; groups, if that is what is wanted.
4557                       (<= (gnus-info-level info) gnus-level-subscribed))
4558                   (gnus-group-make-articles-read name idlist))))
4559          xref-hashtb)))))
4560
4561 (defun gnus-compute-read-articles (group articles)
4562   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4563          (info (nth 2 entry))
4564          (active (gnus-active group))
4565          ninfo)
4566     (when entry
4567       ;; First peel off all invalid article numbers.
4568       (when active
4569         (let ((ids articles)
4570               id first)
4571           (while (setq id (pop ids))
4572             (when (and first (> id (cdr active)))
4573               ;; We'll end up in this situation in one particular
4574               ;; obscure situation.  If you re-scan a group and get
4575               ;; a new article that is cross-posted to a different
4576               ;; group that has not been re-scanned, you might get
4577               ;; crossposted article that has a higher number than
4578               ;; Gnus believes possible.  So we re-activate this
4579               ;; group as well.  This might mean doing the
4580               ;; crossposting thingy will *increase* the number
4581               ;; of articles in some groups.  Tsk, tsk.
4582               (setq active (or (gnus-activate-group group) active)))
4583             (when (or (> id (cdr active))
4584                       (< id (car active)))
4585               (setq articles (delq id articles))))))
4586       ;; If the read list is nil, we init it.
4587       (if (and active
4588                (null (gnus-info-read info))
4589                (> (car active) 1))
4590           (setq ninfo (cons 1 (1- (car active))))
4591         (setq ninfo (gnus-info-read info)))
4592       ;; Then we add the read articles to the range.
4593       (gnus-add-to-range
4594        ninfo (setq articles (sort articles '<))))))
4595
4596 (defun gnus-group-make-articles-read (group articles)
4597   "Update the info of GROUP to say that ARTICLES are read."
4598   (let* ((num 0)
4599          (entry (gnus-gethash group gnus-newsrc-hashtb))
4600          (info (nth 2 entry))
4601          (active (gnus-active group))
4602          range)
4603     (when entry
4604       (setq range (gnus-compute-read-articles group articles))
4605       (save-excursion
4606         (set-buffer gnus-group-buffer)
4607         (gnus-undo-register
4608           `(progn
4609              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4610              (gnus-info-set-read ',info ',(gnus-info-read info))
4611              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4612              (gnus-group-update-group ,group t))))
4613       ;; Add the read articles to the range.
4614       (gnus-info-set-read info range)
4615       ;; Then we have to re-compute how many unread
4616       ;; articles there are in this group.
4617       (when active
4618         (cond
4619          ((not range)
4620           (setq num (- (1+ (cdr active)) (car active))))
4621          ((not (listp (cdr range)))
4622           (setq num (- (cdr active) (- (1+ (cdr range))
4623                                        (car range)))))
4624          (t
4625           (while range
4626             (if (numberp (car range))
4627                 (setq num (1+ num))
4628               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4629             (setq range (cdr range)))
4630           (setq num (- (cdr active) num))))
4631         ;; Update the number of unread articles.
4632         (setcar entry num)
4633         ;; Update the group buffer.
4634         (gnus-group-update-group group t)))))
4635
4636 (defvar gnus-newsgroup-none-id 0)
4637
4638 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4639   (let ((cur nntp-server-buffer)
4640         (dependencies
4641          (or dependencies
4642              (save-excursion (set-buffer gnus-summary-buffer)
4643                              gnus-newsgroup-dependencies)))
4644         headers id end ref
4645         (mail-parse-charset gnus-newsgroup-charset)
4646         (mail-parse-ignored-charsets
4647          (save-excursion (condition-case nil
4648                              (set-buffer gnus-summary-buffer)
4649                            (error))
4650                          gnus-newsgroup-ignored-charsets)))
4651     (save-excursion
4652       (set-buffer nntp-server-buffer)
4653       ;; Translate all TAB characters into SPACE characters.
4654       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4655       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4656       (gnus-run-hooks 'gnus-parse-headers-hook)
4657       (let ((case-fold-search t)
4658             in-reply-to header p lines chars ctype)
4659         (goto-char (point-min))
4660         ;; Search to the beginning of the next header.  Error messages
4661         ;; do not begin with 2 or 3.
4662         (while (re-search-forward "^[23][0-9]+ " nil t)
4663           (setq id nil
4664                 ref nil)
4665           ;; This implementation of this function, with nine
4666           ;; search-forwards instead of the one re-search-forward and
4667           ;; a case (which basically was the old function) is actually
4668           ;; about twice as fast, even though it looks messier.  You
4669           ;; can't have everything, I guess.  Speed and elegance
4670           ;; doesn't always go hand in hand.
4671           (setq
4672            header
4673            (make-full-mail-header
4674             ;; Number.
4675             (prog1
4676                 (read cur)
4677               (end-of-line)
4678               (setq p (point))
4679               (narrow-to-region (point)
4680                                 (or (and (search-forward "\n.\n" nil t)
4681                                          (- (point) 2))
4682                                     (point))))
4683             ;; Subject.
4684             (progn
4685               (goto-char p)
4686               (if (search-forward "\nsubject: " nil t)
4687                   (buffer-substring (match-end 0) (std11-field-end))
4688                 "(none)"))
4689             ;; From.
4690             (progn
4691               (goto-char p)
4692               (if (search-forward "\nfrom: " nil t)
4693                   (buffer-substring (match-end 0) (std11-field-end))
4694                 "(nobody)"))
4695             ;; Date.
4696             (progn
4697               (goto-char p)
4698               (if (search-forward "\ndate: " nil t)
4699                   (buffer-substring (match-end 0) (std11-field-end))
4700                 ""))
4701             ;; Message-ID.
4702             (progn
4703               (goto-char p)
4704               (setq id (if (re-search-forward
4705                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4706                            ;; We do it this way to make sure the Message-ID
4707                            ;; is (somewhat) syntactically valid.
4708                            (buffer-substring (match-beginning 1)
4709                                              (match-end 1))
4710                          ;; If there was no message-id, we just fake one
4711                          ;; to make subsequent routines simpler.
4712                          (nnheader-generate-fake-message-id))))
4713             ;; References.
4714             (progn
4715               (goto-char p)
4716               (if (search-forward "\nreferences: " nil t)
4717                   (progn
4718                     (setq end (point))
4719                     (prog1
4720                         (buffer-substring (match-end 0) (std11-field-end))
4721                       (setq ref
4722                             (buffer-substring
4723                              (progn
4724                                ;; (end-of-line)
4725                                (search-backward ">" end t)
4726                                (1+ (point)))
4727                              (progn
4728                                (search-backward "<" end t)
4729                                (point))))))
4730                 ;; Get the references from the in-reply-to header if there
4731                 ;; were no references and the in-reply-to header looks
4732                 ;; promising.
4733                 (if (and (search-forward "\nin-reply-to: " nil t)
4734                          (setq in-reply-to
4735                                (buffer-substring (match-end 0)
4736                                                  (std11-field-end)))
4737                          (string-match "<[^>]+>" in-reply-to))
4738                     (let (ref2)
4739                       (setq ref (substring in-reply-to (match-beginning 0)
4740                                            (match-end 0)))
4741                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4742                         (setq ref2 (substring in-reply-to (match-beginning 0)
4743                                               (match-end 0)))
4744                         (when (> (length ref2) (length ref))
4745                           (setq ref ref2)))
4746                       ref)
4747                   (setq ref nil))))
4748             ;; Chars.
4749             (progn
4750               (goto-char p)
4751               (if (search-forward "\nchars: " nil t)
4752                   (if (numberp (setq chars (ignore-errors (read cur))))
4753                       chars 0)
4754                 0))
4755             ;; Lines.
4756             (progn
4757               (goto-char p)
4758               (if (search-forward "\nlines: " nil t)
4759                   (if (numberp (setq lines (ignore-errors (read cur))))
4760                       lines 0)
4761                 0))
4762             ;; Xref.
4763             (progn
4764               (goto-char p)
4765               (and (search-forward "\nxref: " nil t)
4766                    (buffer-substring (match-end 0) (std11-field-end))))
4767             ;; Extra.
4768             (when gnus-extra-headers
4769               (let ((extra gnus-extra-headers)
4770                     out)
4771                 (while extra
4772                   (goto-char p)
4773                   (when (search-forward
4774                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4775                     (push (cons (car extra)
4776                                 (buffer-substring (match-end 0)
4777                                                   (std11-field-end)))
4778                           out))
4779                   (pop extra))
4780                 out))))
4781           (goto-char p)
4782           (if (and (search-forward "\ncontent-type: " nil t)
4783                    (setq ctype
4784                          (buffer-substring (match-end 0) (std11-field-end))))
4785               (mime-entity-set-content-type-internal
4786                header (mime-parse-Content-Type ctype)))
4787           (when (equal id ref)
4788             (setq ref nil))
4789
4790           (when gnus-alter-header-function
4791             (funcall gnus-alter-header-function header)
4792             (setq id (mail-header-id header)
4793                   ref (gnus-parent-id (mail-header-references header))))
4794
4795           (when (setq header
4796                       (gnus-dependencies-add-header
4797                        header dependencies force-new))
4798             (push header headers))
4799           (goto-char (point-max))
4800           (widen))
4801         (nreverse headers)))))
4802
4803 ;; Goes through the xover lines and returns a list of vectors
4804 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4805                                                   force-new dependencies
4806                                                   group also-fetch-heads)
4807   "Parse the news overview data in the server buffer, and return a
4808 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4809   ;; Get the Xref when the users reads the articles since most/some
4810   ;; NNTP servers do not include Xrefs when using XOVER.
4811   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4812   (let ((mail-parse-charset gnus-newsgroup-charset)
4813         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4814         (cur nntp-server-buffer)
4815         (dependencies (or dependencies gnus-newsgroup-dependencies))
4816         number headers header)
4817     (save-excursion
4818       (set-buffer nntp-server-buffer)
4819       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4820       ;; Allow the user to mangle the headers before parsing them.
4821       (gnus-run-hooks 'gnus-parse-headers-hook)
4822       (goto-char (point-min))
4823       (while (not (eobp))
4824         (condition-case ()
4825             (while (and sequence (not (eobp)))
4826               (setq number (read cur))
4827               (while (and sequence
4828                           (< (car sequence) number))
4829                 (setq sequence (cdr sequence)))
4830               (and sequence
4831                    (eq number (car sequence))
4832                    (progn
4833                      (setq sequence (cdr sequence))
4834                      (setq header (inline
4835                                     (gnus-nov-parse-line
4836                                      number dependencies force-new))))
4837                    (push header headers))
4838               (forward-line 1))
4839           (error
4840            (gnus-error 4 "Strange nov line (%d)"
4841                        (count-lines (point-min) (point)))))
4842         (forward-line 1))
4843       ;; A common bug in inn is that if you have posted an article and
4844       ;; then retrieves the active file, it will answer correctly --
4845       ;; the new article is included.  However, a NOV entry for the
4846       ;; article may not have been generated yet, so this may fail.
4847       ;; We work around this problem by retrieving the last few
4848       ;; headers using HEAD.
4849       (if (or (not also-fetch-heads)
4850               (not sequence))
4851           ;; We (probably) got all the headers.
4852           (nreverse headers)
4853         (let ((gnus-nov-is-evil t))
4854           (nconc
4855            (nreverse headers)
4856            (gnus-retrieve-parsed-headers sequence group)
4857            ))))))
4858
4859 (defun gnus-article-get-xrefs ()
4860   "Fill in the Xref value in `gnus-current-headers', if necessary.
4861 This is meant to be called in `gnus-article-internal-prepare-hook'."
4862   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4863                                  gnus-current-headers)))
4864     (or (not gnus-use-cross-reference)
4865         (not headers)
4866         (and (mail-header-xref headers)
4867              (not (string= (mail-header-xref headers) "")))
4868         (let ((case-fold-search t)
4869               xref)
4870           (save-restriction
4871             (nnheader-narrow-to-headers)
4872             (goto-char (point-min))
4873             (when (or (and (not (eobp))
4874                            (eq (downcase (char-after)) ?x)
4875                            (looking-at "Xref:"))
4876                       (search-forward "\nXref:" nil t))
4877               (goto-char (1+ (match-end 0)))
4878               (setq xref (buffer-substring (point)
4879                                            (progn (end-of-line) (point))))
4880               (mail-header-set-xref headers xref)))))))
4881
4882 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4883   "Find article ID and insert the summary line for that article.
4884 OLD-HEADER can either be a header or a line number to insert
4885 the subject line on."
4886   (let* ((line (and (numberp old-header) old-header))
4887          (old-header (and (vectorp old-header) old-header))
4888          (header (cond ((and old-header use-old-header)
4889                         old-header)
4890                        ((and (numberp id)
4891                              (gnus-number-to-header id))
4892                         (gnus-number-to-header id))
4893                        (t
4894                         (gnus-read-header id))))
4895          (number (and (numberp id) id))
4896          d)
4897     (when header
4898       ;; Rebuild the thread that this article is part of and go to the
4899       ;; article we have fetched.
4900       (when (and (not gnus-show-threads)
4901                  old-header)
4902         (when (and number
4903                    (setq d (gnus-data-find (mail-header-number old-header))))
4904           (goto-char (gnus-data-pos d))
4905           (gnus-data-remove
4906            number
4907            (- (gnus-point-at-bol)
4908               (prog1
4909                   (1+ (gnus-point-at-eol))
4910                 (gnus-delete-line))))))
4911       (when old-header
4912         (mail-header-set-number header (mail-header-number old-header)))
4913       (setq gnus-newsgroup-sparse
4914             (delq (setq number (mail-header-number header))
4915                   gnus-newsgroup-sparse))
4916       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4917       (push number gnus-newsgroup-limit)
4918       (gnus-rebuild-thread (mail-header-id header) line)
4919       (gnus-summary-goto-subject number nil t))
4920     (when (and (numberp number)
4921                (> number 0))
4922       ;; We have to update the boundaries even if we can't fetch the
4923       ;; article if ID is a number -- so that the next `P' or `N'
4924       ;; command will fetch the previous (or next) article even
4925       ;; if the one we tried to fetch this time has been canceled.
4926       (when (> number gnus-newsgroup-end)
4927         (setq gnus-newsgroup-end number))
4928       (when (< number gnus-newsgroup-begin)
4929         (setq gnus-newsgroup-begin number))
4930       (setq gnus-newsgroup-unselected
4931             (delq number gnus-newsgroup-unselected)))
4932     ;; Report back a success?
4933     (and header (mail-header-number header))))
4934
4935 ;;; Process/prefix in the summary buffer
4936
4937 (defun gnus-summary-work-articles (n)
4938   "Return a list of articles to be worked upon.
4939 The prefix argument, the list of process marked articles, and the
4940 current article will be taken into consideration."
4941   (save-excursion
4942     (set-buffer gnus-summary-buffer)
4943     (cond
4944      (n
4945       ;; A numerical prefix has been given.
4946       (setq n (prefix-numeric-value n))
4947       (let ((backward (< n 0))
4948             (n (abs (prefix-numeric-value n)))
4949             articles article)
4950         (save-excursion
4951           (while
4952               (and (> n 0)
4953                    (push (setq article (gnus-summary-article-number))
4954                          articles)
4955                    (if backward
4956                        (gnus-summary-find-prev nil article)
4957                      (gnus-summary-find-next nil article)))
4958             (decf n)))
4959         (nreverse articles)))
4960      ((and (gnus-region-active-p) (mark))
4961       (message "region active")
4962       ;; Work on the region between point and mark.
4963       (let ((max (max (point) (mark)))
4964             articles article)
4965         (save-excursion
4966           (goto-char (min (point) (mark)))
4967           (while
4968               (and
4969                (push (setq article (gnus-summary-article-number)) articles)
4970                (gnus-summary-find-next nil article)
4971                (< (point) max)))
4972           (nreverse articles))))
4973      (gnus-newsgroup-processable
4974       ;; There are process-marked articles present.
4975       ;; Save current state.
4976       (gnus-summary-save-process-mark)
4977       ;; Return the list.
4978       (reverse gnus-newsgroup-processable))
4979      (t
4980       ;; Just return the current article.
4981       (list (gnus-summary-article-number))))))
4982
4983 (defmacro gnus-summary-iterate (arg &rest forms)
4984   "Iterate over the process/prefixed articles and do FORMS.
4985 ARG is the interactive prefix given to the command.  FORMS will be
4986 executed with point over the summary line of the articles."
4987   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4988     `(let ((,articles (gnus-summary-work-articles ,arg)))
4989        (while ,articles
4990          (gnus-summary-goto-subject (car ,articles))
4991          ,@forms
4992          (pop ,articles)))))
4993
4994 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4995 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4996
4997 (defun gnus-summary-save-process-mark ()
4998   "Push the current set of process marked articles on the stack."
4999   (interactive)
5000   (push (copy-sequence gnus-newsgroup-processable)
5001         gnus-newsgroup-process-stack))
5002
5003 (defun gnus-summary-kill-process-mark ()
5004   "Push the current set of process marked articles on the stack and unmark."
5005   (interactive)
5006   (gnus-summary-save-process-mark)
5007   (gnus-summary-unmark-all-processable))
5008
5009 (defun gnus-summary-yank-process-mark ()
5010   "Pop the last process mark state off the stack and restore it."
5011   (interactive)
5012   (unless gnus-newsgroup-process-stack
5013     (error "Empty mark stack"))
5014   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5015
5016 (defun gnus-summary-process-mark-set (set)
5017   "Make SET into the current process marked articles."
5018   (gnus-summary-unmark-all-processable)
5019   (while set
5020     (gnus-summary-set-process-mark (pop set))))
5021
5022 ;;; Searching and stuff
5023
5024 (defun gnus-summary-search-group (&optional backward use-level)
5025   "Search for next unread newsgroup.
5026 If optional argument BACKWARD is non-nil, search backward instead."
5027   (save-excursion
5028     (set-buffer gnus-group-buffer)
5029     (when (gnus-group-search-forward
5030            backward nil (if use-level (gnus-group-group-level) nil))
5031       (gnus-group-group-name))))
5032
5033 (defun gnus-summary-best-group (&optional exclude-group)
5034   "Find the name of the best unread group.
5035 If EXCLUDE-GROUP, do not go to this group."
5036   (save-excursion
5037     (set-buffer gnus-group-buffer)
5038     (save-excursion
5039       (gnus-group-best-unread-group exclude-group))))
5040
5041 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5042   (if backward (gnus-summary-find-prev)
5043     (let* ((dummy (gnus-summary-article-intangible-p))
5044            (article (or article (gnus-summary-article-number)))
5045            (arts (gnus-data-find-list article))
5046            result)
5047       (when (and (not dummy)
5048                  (or (not gnus-summary-check-current)
5049                      (not unread)
5050                      (not (gnus-data-unread-p (car arts)))))
5051         (setq arts (cdr arts)))
5052       (when (setq result
5053                   (if unread
5054                       (progn
5055                         (while arts
5056                           (when (or (and undownloaded
5057                                          (eq gnus-undownloaded-mark
5058                                              (gnus-data-mark (car arts))))
5059                                     (gnus-data-unread-p (car arts)))
5060                             (setq result (car arts)
5061                                   arts nil))
5062                           (setq arts (cdr arts)))
5063                         result)
5064                     (car arts)))
5065         (goto-char (gnus-data-pos result))
5066         (gnus-data-number result)))))
5067
5068 (defun gnus-summary-find-prev (&optional unread article)
5069   (let* ((eobp (eobp))
5070          (article (or article (gnus-summary-article-number)))
5071          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5072          result)
5073     (when (and (not eobp)
5074                (or (not gnus-summary-check-current)
5075                    (not unread)
5076                    (not (gnus-data-unread-p (car arts)))))
5077       (setq arts (cdr arts)))
5078     (when (setq result
5079                 (if unread
5080                     (progn
5081                       (while arts
5082                         (when (gnus-data-unread-p (car arts))
5083                           (setq result (car arts)
5084                                 arts nil))
5085                         (setq arts (cdr arts)))
5086                       result)
5087                   (car arts)))
5088       (goto-char (gnus-data-pos result))
5089       (gnus-data-number result))))
5090
5091 (defun gnus-summary-find-subject (subject &optional unread backward article)
5092   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5093          (article (or article (gnus-summary-article-number)))
5094          (articles (gnus-data-list backward))
5095          (arts (gnus-data-find-list article articles))
5096          result)
5097     (when (or (not gnus-summary-check-current)
5098               (not unread)
5099               (not (gnus-data-unread-p (car arts))))
5100       (setq arts (cdr arts)))
5101     (while arts
5102       (and (or (not unread)
5103                (gnus-data-unread-p (car arts)))
5104            (vectorp (gnus-data-header (car arts)))
5105            (gnus-subject-equal
5106             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5107            (setq result (car arts)
5108                  arts nil))
5109       (setq arts (cdr arts)))
5110     (and result
5111          (goto-char (gnus-data-pos result))
5112          (gnus-data-number result))))
5113
5114 (defun gnus-summary-search-forward (&optional unread subject backward)
5115   "Search forward for an article.
5116 If UNREAD, look for unread articles.  If SUBJECT, look for
5117 articles with that subject.  If BACKWARD, search backward instead."
5118   (cond (subject (gnus-summary-find-subject subject unread backward))
5119         (backward (gnus-summary-find-prev unread))
5120         (t (gnus-summary-find-next unread))))
5121
5122 (defun gnus-recenter (&optional n)
5123   "Center point in window and redisplay frame.
5124 Also do horizontal recentering."
5125   (interactive "P")
5126   (when (and gnus-auto-center-summary
5127              (not (eq gnus-auto-center-summary 'vertical)))
5128     (gnus-horizontal-recenter))
5129   (recenter n))
5130
5131 (defun gnus-summary-recenter ()
5132   "Center point in the summary window.
5133 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5134 displayed, no centering will be performed."
5135   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5136   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5137   (let* ((top (cond ((< (window-height) 4) 0)
5138                     ((< (window-height) 7) 1)
5139                     (t (if (numberp gnus-auto-center-summary)
5140                            gnus-auto-center-summary
5141                          2))))
5142          (height (1- (window-height)))
5143          (bottom (save-excursion (goto-char (point-max))
5144                                  (forward-line (- height))
5145                                  (point)))
5146          (window (get-buffer-window (current-buffer))))
5147     ;; The user has to want it.
5148     (when gnus-auto-center-summary
5149       (when (get-buffer-window gnus-article-buffer)
5150         ;; Only do recentering when the article buffer is displayed,
5151         ;; Set the window start to either `bottom', which is the biggest
5152         ;; possible valid number, or the second line from the top,
5153         ;; whichever is the least.
5154         (set-window-start
5155          window (min bottom (save-excursion
5156                               (forward-line (- top)) (point)))
5157          t))
5158       ;; Do horizontal recentering while we're at it.
5159       (when (and (get-buffer-window (current-buffer) t)
5160                  (not (eq gnus-auto-center-summary 'vertical)))
5161         (let ((selected (selected-window)))
5162           (select-window (get-buffer-window (current-buffer) t))
5163           (gnus-summary-position-point)
5164           (gnus-horizontal-recenter)
5165           (select-window selected))))))
5166
5167 (defun gnus-summary-jump-to-group (newsgroup)
5168   "Move point to NEWSGROUP in group mode buffer."
5169   ;; Keep update point of group mode buffer if visible.
5170   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5171       (save-window-excursion
5172         ;; Take care of tree window mode.
5173         (when (get-buffer-window gnus-group-buffer)
5174           (pop-to-buffer gnus-group-buffer))
5175         (gnus-group-jump-to-group newsgroup))
5176     (save-excursion
5177       ;; Take care of tree window mode.
5178       (if (get-buffer-window gnus-group-buffer)
5179           (pop-to-buffer gnus-group-buffer)
5180         (set-buffer gnus-group-buffer))
5181       (gnus-group-jump-to-group newsgroup))))
5182
5183 ;; This function returns a list of article numbers based on the
5184 ;; difference between the ranges of read articles in this group and
5185 ;; the range of active articles.
5186 (defun gnus-list-of-unread-articles (group)
5187   (let* ((read (gnus-info-read (gnus-get-info group)))
5188          (active (or (gnus-active group) (gnus-activate-group group)))
5189          (last (cdr active))
5190          first nlast unread)
5191     ;; If none are read, then all are unread.
5192     (if (not read)
5193         (setq first (car active))
5194       ;; If the range of read articles is a single range, then the
5195       ;; first unread article is the article after the last read
5196       ;; article.  Sounds logical, doesn't it?
5197       (if (not (listp (cdr read)))
5198           (setq first (max (car active) (1+ (cdr read))))
5199         ;; `read' is a list of ranges.
5200         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5201                                   (caar read)))
5202                   1)
5203           (setq first (car active)))
5204         (while read
5205           (when first
5206             (while (< first nlast)
5207               (push first unread)
5208               (setq first (1+ first))))
5209           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5210           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5211           (setq read (cdr read)))))
5212     ;; And add the last unread articles.
5213     (while (<= first last)
5214       (push first unread)
5215       (setq first (1+ first)))
5216     ;; Return the list of unread articles.
5217     (delq 0 (nreverse unread))))
5218
5219 (defun gnus-list-of-read-articles (group)
5220   "Return a list of unread, unticked and non-dormant articles."
5221   (let* ((info (gnus-get-info group))
5222          (marked (gnus-info-marks info))
5223          (active (gnus-active group)))
5224     (and info active
5225          (gnus-set-difference
5226           (gnus-sorted-complement
5227            (gnus-uncompress-range active)
5228            (gnus-list-of-unread-articles group))
5229           (append
5230            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5231            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5232
5233 ;; Various summary commands
5234
5235 (defun gnus-summary-select-article-buffer ()
5236   "Reconfigure windows to show article buffer."
5237   (interactive)
5238   (if (not (gnus-buffer-live-p gnus-article-buffer))
5239       (error "There is no article buffer for this summary buffer")
5240     (gnus-configure-windows 'article)
5241     (select-window (get-buffer-window gnus-article-buffer))))
5242
5243 (defun gnus-summary-universal-argument (arg)
5244   "Perform any operation on all articles that are process/prefixed."
5245   (interactive "P")
5246   (let ((articles (gnus-summary-work-articles arg))
5247         func article)
5248     (if (eq
5249          (setq
5250           func
5251           (key-binding
5252            (read-key-sequence
5253             (substitute-command-keys
5254              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5255              ))))
5256          'undefined)
5257         (gnus-error 1 "Undefined key")
5258       (save-excursion
5259         (while articles
5260           (gnus-summary-goto-subject (setq article (pop articles)))
5261           (let (gnus-newsgroup-processable)
5262             (command-execute func))
5263           (gnus-summary-remove-process-mark article)))))
5264   (gnus-summary-position-point))
5265
5266 (defun gnus-summary-toggle-truncation (&optional arg)
5267   "Toggle truncation of summary lines.
5268 With arg, turn line truncation on iff arg is positive."
5269   (interactive "P")
5270   (setq truncate-lines
5271         (if (null arg) (not truncate-lines)
5272           (> (prefix-numeric-value arg) 0)))
5273   (redraw-display))
5274
5275 (defun gnus-summary-reselect-current-group (&optional all rescan)
5276   "Rescan the current newsgroup, exit and then reselect it.
5277 The prefix argument ALL means to select all articles."
5278   (interactive "P")
5279   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5280     (error "Ephemeral groups can't be reselected"))
5281   (let ((current-subject (gnus-summary-article-number))
5282         (group gnus-newsgroup-name))
5283     (save-excursion
5284       (set-buffer gnus-group-buffer)
5285       ;; We have to adjust the point of group mode buffer because
5286       ;; point was moved to the next unread newsgroup by exiting.
5287       (gnus-summary-jump-to-group group)
5288       (when rescan
5289         (save-excursion
5290           (gnus-group-get-new-news-this-group 1))))
5291     (setq gnus-newsgroup-begin nil)
5292     (gnus-summary-exit)
5293     (gnus-group-read-group all t group)
5294     (gnus-summary-goto-subject current-subject nil t)))
5295
5296 (defun gnus-summary-rescan-group (&optional all)
5297   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5298   (interactive "P")
5299   (gnus-summary-reselect-current-group all t))
5300
5301 (defun gnus-summary-update-info (&optional non-destructive)
5302   (save-excursion
5303     (let ((group gnus-newsgroup-name))
5304       (when group
5305         (when gnus-newsgroup-kill-headers
5306           (setq gnus-newsgroup-killed
5307                 (gnus-compress-sequence
5308                  (nconc
5309                   (gnus-set-sorted-intersection
5310                    (gnus-uncompress-range gnus-newsgroup-killed)
5311                    (setq gnus-newsgroup-unselected
5312                          (sort gnus-newsgroup-unselected '<)))
5313                   (setq gnus-newsgroup-unreads
5314                         (sort gnus-newsgroup-unreads '<)))
5315                  t)))
5316         (unless (listp (cdr gnus-newsgroup-killed))
5317           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5318         (let ((headers gnus-newsgroup-headers))
5319           ;; Set the new ranges of read articles.
5320           (save-excursion
5321             (set-buffer gnus-group-buffer)
5322             (gnus-undo-force-boundary))
5323           (gnus-update-read-articles
5324            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5325           ;; Set the current article marks.
5326           (let ((gnus-newsgroup-scored
5327                  (if (and (not gnus-save-score)
5328                           (not non-destructive))
5329                      nil
5330                    gnus-newsgroup-scored)))
5331             (save-excursion
5332               (gnus-update-marks)))
5333           ;; Do the cross-ref thing.
5334           (when gnus-use-cross-reference
5335             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5336           ;; Do not switch windows but change the buffer to work.
5337           (set-buffer gnus-group-buffer)
5338           (unless (gnus-ephemeral-group-p group)
5339             (gnus-group-update-group group)))))))
5340
5341 (defun gnus-summary-save-newsrc (&optional force)
5342   "Save the current number of read/marked articles in the dribble buffer.
5343 The dribble buffer will then be saved.
5344 If FORCE (the prefix), also save the .newsrc file(s)."
5345   (interactive "P")
5346   (gnus-summary-update-info t)
5347   (if force
5348       (gnus-save-newsrc-file)
5349     (gnus-dribble-save)))
5350
5351 (defun gnus-summary-exit (&optional temporary)
5352   "Exit reading current newsgroup, and then return to group selection mode.
5353 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5354   (interactive)
5355   (gnus-set-global-variables)
5356   (gnus-kill-save-kill-buffer)
5357   (gnus-async-halt-prefetch)
5358   (let* ((group gnus-newsgroup-name)
5359          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5360          (mode major-mode)
5361          (group-point nil)
5362          (buf (current-buffer)))
5363     (unless quit-config
5364       ;; Do adaptive scoring, and possibly save score files.
5365       (when gnus-newsgroup-adaptive
5366         (gnus-score-adaptive))
5367       (when gnus-use-scoring
5368         (gnus-score-save)))
5369     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5370     ;; If we have several article buffers, we kill them at exit.
5371     (unless gnus-single-article-buffer
5372       (gnus-kill-buffer gnus-original-article-buffer)
5373       (setq gnus-article-current nil))
5374     (when gnus-use-cache
5375       (gnus-cache-possibly-remove-articles)
5376       (gnus-cache-save-buffers))
5377     (gnus-async-prefetch-remove-group group)
5378     (when gnus-suppress-duplicates
5379       (gnus-dup-enter-articles))
5380     (when gnus-use-trees
5381       (gnus-tree-close group))
5382     ;; Remove entries for this group.
5383     (nnmail-purge-split-history (gnus-group-real-name group))
5384     ;; Make all changes in this group permanent.
5385     (unless quit-config
5386       (gnus-run-hooks 'gnus-exit-group-hook)
5387       (gnus-summary-update-info))
5388     (gnus-close-group group)
5389     ;; Make sure where we were, and go to next newsgroup.
5390     (set-buffer gnus-group-buffer)
5391     (unless quit-config
5392       (gnus-group-jump-to-group group))
5393     (gnus-run-hooks 'gnus-summary-exit-hook)
5394     (unless (or quit-config
5395                 ;; If this group has disappeared from the summary
5396                 ;; buffer, don't skip forwards.
5397                 (not (string= group (gnus-group-group-name))))
5398       (gnus-group-next-unread-group 1))
5399     (setq group-point (point))
5400     (if temporary
5401         nil                             ;Nothing to do.
5402       ;; If we have several article buffers, we kill them at exit.
5403       (unless gnus-single-article-buffer
5404         (gnus-kill-buffer gnus-article-buffer)
5405         (gnus-kill-buffer gnus-original-article-buffer)
5406         (setq gnus-article-current nil))
5407       (set-buffer buf)
5408       (if (not gnus-kill-summary-on-exit)
5409           (gnus-deaden-summary)
5410         ;; We set all buffer-local variables to nil.  It is unclear why
5411         ;; this is needed, but if we don't, buffer-local variables are
5412         ;; not garbage-collected, it seems.  This would the lead to en
5413         ;; ever-growing Emacs.
5414         (gnus-summary-clear-local-variables)
5415         (when (get-buffer gnus-article-buffer)
5416           (bury-buffer gnus-article-buffer))
5417         ;; We clear the global counterparts of the buffer-local
5418         ;; variables as well, just to be on the safe side.
5419         (set-buffer gnus-group-buffer)
5420         (gnus-summary-clear-local-variables)
5421         ;; Return to group mode buffer.
5422         (when (eq mode 'gnus-summary-mode)
5423           (gnus-kill-buffer buf)))
5424       (setq gnus-current-select-method gnus-select-method)
5425       (pop-to-buffer gnus-group-buffer)
5426       (if (not quit-config)
5427           (progn
5428             (goto-char group-point)
5429             (gnus-configure-windows 'group 'force))
5430         (gnus-handle-ephemeral-exit quit-config))
5431       ;; Clear the current group name.
5432       (unless quit-config
5433         (setq gnus-newsgroup-name nil)))))
5434
5435 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5436 (defun gnus-summary-exit-no-update (&optional no-questions)
5437   "Quit reading current newsgroup without updating read article info."
5438   (interactive)
5439   (let* ((group gnus-newsgroup-name)
5440          (quit-config (gnus-group-quit-config group)))
5441     (when (or no-questions
5442               gnus-expert-user
5443               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5444       (gnus-async-halt-prefetch)
5445       (mapcar 'funcall
5446               (delq 'gnus-summary-expire-articles
5447                     (copy-sequence gnus-summary-prepare-exit-hook)))
5448       ;; If we have several article buffers, we kill them at exit.
5449       (unless gnus-single-article-buffer
5450         (gnus-kill-buffer gnus-article-buffer)
5451         (gnus-kill-buffer gnus-original-article-buffer)
5452         (setq gnus-article-current nil))
5453       (if (not gnus-kill-summary-on-exit)
5454           (gnus-deaden-summary)
5455         (gnus-close-group group)
5456         (gnus-summary-clear-local-variables)
5457         (set-buffer gnus-group-buffer)
5458         (gnus-summary-clear-local-variables)
5459         (when (get-buffer gnus-summary-buffer)
5460           (kill-buffer gnus-summary-buffer)))
5461       (unless gnus-single-article-buffer
5462         (setq gnus-article-current nil))
5463       (when gnus-use-trees
5464         (gnus-tree-close group))
5465       (gnus-async-prefetch-remove-group group)
5466       (when (get-buffer gnus-article-buffer)
5467         (bury-buffer gnus-article-buffer))
5468       ;; Return to the group buffer.
5469       (gnus-configure-windows 'group 'force)
5470       ;; Clear the current group name.
5471       (setq gnus-newsgroup-name nil)
5472       (when (equal (gnus-group-group-name) group)
5473         (gnus-group-next-unread-group 1))
5474       (when quit-config
5475         (gnus-handle-ephemeral-exit quit-config)))))
5476
5477 (defun gnus-handle-ephemeral-exit (quit-config)
5478   "Handle movement when leaving an ephemeral group.
5479 The state which existed when entering the ephemeral is reset."
5480   (if (not (buffer-name (car quit-config)))
5481       (gnus-configure-windows 'group 'force)
5482     (set-buffer (car quit-config))
5483     (cond ((eq major-mode 'gnus-summary-mode)
5484            (gnus-set-global-variables))
5485           ((eq major-mode 'gnus-article-mode)
5486            (save-excursion
5487              ;; The `gnus-summary-buffer' variable may point
5488              ;; to the old summary buffer when using a single
5489              ;; article buffer.
5490              (unless (gnus-buffer-live-p gnus-summary-buffer)
5491                (set-buffer gnus-group-buffer))
5492              (set-buffer gnus-summary-buffer)
5493              (gnus-set-global-variables))))
5494     (if (or (eq (cdr quit-config) 'article)
5495             (eq (cdr quit-config) 'pick))
5496         (progn
5497           ;; The current article may be from the ephemeral group
5498           ;; thus it is best that we reload this article
5499           (gnus-summary-show-article)
5500           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5501               (gnus-configure-windows 'pick 'force)
5502             (gnus-configure-windows (cdr quit-config) 'force)))
5503       (gnus-configure-windows (cdr quit-config) 'force))
5504     (when (eq major-mode 'gnus-summary-mode)
5505       (gnus-summary-next-subject 1 nil t)
5506       (gnus-summary-recenter)
5507       (gnus-summary-position-point))))
5508
5509 (defun gnus-summary-preview-mime-message ()
5510   "MIME decode and play this message."
5511   (interactive)
5512   (let ((gnus-break-pages nil)
5513         (gnus-show-mime t))
5514     (gnus-summary-select-article gnus-show-all-headers t))
5515   (select-window (get-buffer-window gnus-article-buffer)))
5516
5517 ;;; Dead summaries.
5518
5519 (defvar gnus-dead-summary-mode-map nil)
5520
5521 (unless gnus-dead-summary-mode-map
5522   (setq gnus-dead-summary-mode-map (make-keymap))
5523   (suppress-keymap gnus-dead-summary-mode-map)
5524   (substitute-key-definition
5525    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5526   (let ((keys '("\C-d" "\r" "\177" [delete])))
5527     (while keys
5528       (define-key gnus-dead-summary-mode-map
5529         (pop keys) 'gnus-summary-wake-up-the-dead))))
5530
5531 (defvar gnus-dead-summary-mode nil
5532   "Minor mode for Gnus summary buffers.")
5533
5534 (defun gnus-dead-summary-mode (&optional arg)
5535   "Minor mode for Gnus summary buffers."
5536   (interactive "P")
5537   (when (eq major-mode 'gnus-summary-mode)
5538     (make-local-variable 'gnus-dead-summary-mode)
5539     (setq gnus-dead-summary-mode
5540           (if (null arg) (not gnus-dead-summary-mode)
5541             (> (prefix-numeric-value arg) 0)))
5542     (when gnus-dead-summary-mode
5543       (gnus-add-minor-mode
5544        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5545
5546 (defun gnus-deaden-summary ()
5547   "Make the current summary buffer into a dead summary buffer."
5548   ;; Kill any previous dead summary buffer.
5549   (when (and gnus-dead-summary
5550              (buffer-name gnus-dead-summary))
5551     (save-excursion
5552       (set-buffer gnus-dead-summary)
5553       (when gnus-dead-summary-mode
5554         (kill-buffer (current-buffer)))))
5555   ;; Make this the current dead summary.
5556   (setq gnus-dead-summary (current-buffer))
5557   (gnus-dead-summary-mode 1)
5558   (let ((name (buffer-name)))
5559     (when (string-match "Summary" name)
5560       (rename-buffer
5561        (concat (substring name 0 (match-beginning 0)) "Dead "
5562                (substring name (match-beginning 0)))
5563        t))))
5564
5565 (defun gnus-kill-or-deaden-summary (buffer)
5566   "Kill or deaden the summary BUFFER."
5567   (save-excursion
5568     (when (and (buffer-name buffer)
5569                (not gnus-single-article-buffer))
5570       (save-excursion
5571         (set-buffer buffer)
5572         (gnus-kill-buffer gnus-article-buffer)
5573         (gnus-kill-buffer gnus-original-article-buffer)))
5574     (cond (gnus-kill-summary-on-exit
5575            (when (and gnus-use-trees
5576                       (gnus-buffer-exists-p buffer))
5577              (save-excursion
5578                (set-buffer buffer)
5579                (gnus-tree-close gnus-newsgroup-name)))
5580            (gnus-kill-buffer buffer))
5581           ((gnus-buffer-exists-p buffer)
5582            (save-excursion
5583              (set-buffer buffer)
5584              (gnus-deaden-summary))))))
5585
5586 (defun gnus-summary-wake-up-the-dead (&rest args)
5587   "Wake up the dead summary buffer."
5588   (interactive)
5589   (gnus-dead-summary-mode -1)
5590   (let ((name (buffer-name)))
5591     (when (string-match "Dead " name)
5592       (rename-buffer
5593        (concat (substring name 0 (match-beginning 0))
5594                (substring name (match-end 0)))
5595        t)))
5596   (gnus-message 3 "This dead summary is now alive again"))
5597
5598 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5599 (defun gnus-summary-fetch-faq (&optional faq-dir)
5600   "Fetch the FAQ for the current group.
5601 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5602 in."
5603   (interactive
5604    (list
5605     (when current-prefix-arg
5606       (completing-read
5607        "Faq dir: " (and (listp gnus-group-faq-directory)
5608                         (mapcar (lambda (file) (list file))
5609                                 gnus-group-faq-directory))))))
5610   (let (gnus-faq-buffer)
5611     (when (setq gnus-faq-buffer
5612                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5613       (gnus-configure-windows 'summary-faq))))
5614
5615 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5616 (defun gnus-summary-describe-group (&optional force)
5617   "Describe the current newsgroup."
5618   (interactive "P")
5619   (gnus-group-describe-group force gnus-newsgroup-name))
5620
5621 (defun gnus-summary-describe-briefly ()
5622   "Describe summary mode commands briefly."
5623   (interactive)
5624   (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")))
5625
5626 ;; Walking around group mode buffer from summary mode.
5627
5628 (defun gnus-summary-next-group (&optional no-article target-group backward)
5629   "Exit current newsgroup and then select next unread newsgroup.
5630 If prefix argument NO-ARTICLE is non-nil, no article is selected
5631 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5632 previous group instead."
5633   (interactive "P")
5634   ;; Stop pre-fetching.
5635   (gnus-async-halt-prefetch)
5636   (let ((current-group gnus-newsgroup-name)
5637         (current-buffer (current-buffer))
5638         entered)
5639     ;; First we semi-exit this group to update Xrefs and all variables.
5640     ;; We can't do a real exit, because the window conf must remain
5641     ;; the same in case the user is prompted for info, and we don't
5642     ;; want the window conf to change before that...
5643     (gnus-summary-exit t)
5644     (while (not entered)
5645       ;; Then we find what group we are supposed to enter.
5646       (set-buffer gnus-group-buffer)
5647       (gnus-group-jump-to-group current-group)
5648       (setq target-group
5649             (or target-group
5650                 (if (eq gnus-keep-same-level 'best)
5651                     (gnus-summary-best-group gnus-newsgroup-name)
5652                   (gnus-summary-search-group backward gnus-keep-same-level))))
5653       (if (not target-group)
5654           ;; There are no further groups, so we return to the group
5655           ;; buffer.
5656           (progn
5657             (gnus-message 5 "Returning to the group buffer")
5658             (setq entered t)
5659             (when (gnus-buffer-live-p current-buffer)
5660               (set-buffer current-buffer)
5661               (gnus-summary-exit))
5662             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5663         ;; We try to enter the target group.
5664         (gnus-group-jump-to-group target-group)
5665         (let ((unreads (gnus-group-group-unread)))
5666           (if (and (or (eq t unreads)
5667                        (and unreads (not (zerop unreads))))
5668                    (gnus-summary-read-group
5669                     target-group nil no-article
5670                     (and (buffer-name current-buffer) current-buffer)
5671                     nil backward))
5672               (setq entered t)
5673             (setq current-group target-group
5674                   target-group nil)))))))
5675
5676 (defun gnus-summary-prev-group (&optional no-article)
5677   "Exit current newsgroup and then select previous unread newsgroup.
5678 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5679   (interactive "P")
5680   (gnus-summary-next-group no-article nil t))
5681
5682 ;; Walking around summary lines.
5683
5684 (defun gnus-summary-first-subject (&optional unread undownloaded)
5685   "Go to the first unread subject.
5686 If UNREAD is non-nil, go to the first unread article.
5687 Returns the article selected or nil if there are no unread articles."
5688   (interactive "P")
5689   (prog1
5690       (cond
5691        ;; Empty summary.
5692        ((null gnus-newsgroup-data)
5693         (gnus-message 3 "No articles in the group")
5694         nil)
5695        ;; Pick the first article.
5696        ((not unread)
5697         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5698         (gnus-data-number (car gnus-newsgroup-data)))
5699        ;; No unread articles.
5700        ((null gnus-newsgroup-unreads)
5701         (gnus-message 3 "No more unread articles")
5702         nil)
5703        ;; Find the first unread article.
5704        (t
5705         (let ((data gnus-newsgroup-data))
5706           (while (and data
5707                       (and (not (and undownloaded
5708                                      (eq gnus-undownloaded-mark
5709                                          (gnus-data-mark (car data)))))
5710                            (not (gnus-data-unread-p (car data)))))
5711             (setq data (cdr data)))
5712           (when data
5713             (goto-char (gnus-data-pos (car data)))
5714             (gnus-data-number (car data))))))
5715     (gnus-summary-position-point)))
5716
5717 (defun gnus-summary-next-subject (n &optional unread dont-display)
5718   "Go to next N'th summary line.
5719 If N is negative, go to the previous N'th subject line.
5720 If UNREAD is non-nil, only unread articles are selected.
5721 The difference between N and the actual number of steps taken is
5722 returned."
5723   (interactive "p")
5724   (let ((backward (< n 0))
5725         (n (abs n)))
5726     (while (and (> n 0)
5727                 (if backward
5728                     (gnus-summary-find-prev unread)
5729                   (gnus-summary-find-next unread)))
5730       (gnus-summary-show-thread)
5731       (setq n (1- n)))
5732     (when (/= 0 n)
5733       (gnus-message 7 "No more%s articles"
5734                     (if unread " unread" "")))
5735     (unless dont-display
5736       (gnus-summary-recenter)
5737       (gnus-summary-position-point))
5738     n))
5739
5740 (defun gnus-summary-next-unread-subject (n)
5741   "Go to next N'th unread summary line."
5742   (interactive "p")
5743   (gnus-summary-next-subject n t))
5744
5745 (defun gnus-summary-prev-subject (n &optional unread)
5746   "Go to previous N'th summary line.
5747 If optional argument UNREAD is non-nil, only unread article is selected."
5748   (interactive "p")
5749   (gnus-summary-next-subject (- n) unread))
5750
5751 (defun gnus-summary-prev-unread-subject (n)
5752   "Go to previous N'th unread summary line."
5753   (interactive "p")
5754   (gnus-summary-next-subject (- n) t))
5755
5756 (defun gnus-summary-goto-subject (article &optional force silent)
5757   "Go the subject line of ARTICLE.
5758 If FORCE, also allow jumping to articles not currently shown."
5759   (interactive "nArticle number: ")
5760   (let ((b (point))
5761         (data (gnus-data-find article)))
5762     ;; We read in the article if we have to.
5763     (and (not data)
5764          force
5765          (gnus-summary-insert-subject
5766           article
5767           (if (or (numberp force) (vectorp force)) force)
5768           t)
5769          (setq data (gnus-data-find article)))
5770     (goto-char b)
5771     (if (not data)
5772         (progn
5773           (unless silent
5774             (gnus-message 3 "Can't find article %d" article))
5775           nil)
5776       (goto-char (gnus-data-pos data))
5777       (gnus-summary-position-point)
5778       article)))
5779
5780 ;; Walking around summary lines with displaying articles.
5781
5782 (defun gnus-summary-expand-window (&optional arg)
5783   "Make the summary buffer take up the entire Emacs frame.
5784 Given a prefix, will force an `article' buffer configuration."
5785   (interactive "P")
5786   (if arg
5787       (gnus-configure-windows 'article 'force)
5788     (gnus-configure-windows 'summary 'force)))
5789
5790 (defun gnus-summary-display-article (article &optional all-header)
5791   "Display ARTICLE in article buffer."
5792   (gnus-set-global-variables)
5793   (if (null article)
5794       nil
5795     (prog1
5796         (if gnus-summary-display-article-function
5797             (funcall gnus-summary-display-article-function article all-header)
5798           (gnus-article-prepare article all-header))
5799       (with-current-buffer gnus-article-buffer
5800         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5801              nil))
5802       (gnus-run-hooks 'gnus-select-article-hook)
5803       (when (and gnus-current-article
5804                  (not (zerop gnus-current-article)))
5805         (gnus-summary-goto-subject gnus-current-article))
5806       (gnus-summary-recenter)
5807       (when (and gnus-use-trees gnus-show-threads)
5808         (gnus-possibly-generate-tree article)
5809         (gnus-highlight-selected-tree article))
5810       ;; Successfully display article.
5811       (gnus-article-set-window-start
5812        (cdr (assq article gnus-newsgroup-bookmarks))))))
5813
5814 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5815   "Select the current article.
5816 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5817 non-nil, the article will be re-fetched even if it already present in
5818 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5819 be displayed."
5820   ;; Make sure we are in the summary buffer to work around bbdb bug.
5821   (unless (eq major-mode 'gnus-summary-mode)
5822     (set-buffer gnus-summary-buffer))
5823   (let ((article (or article (gnus-summary-article-number)))
5824         (all-headers (not (not all-headers))) ;Must be T or NIL.
5825         gnus-summary-display-article-function
5826         did)
5827     (and (not pseudo)
5828          (gnus-summary-article-pseudo-p article)
5829          (error "This is a pseudo-article"))
5830     (prog1
5831         (save-excursion
5832           (set-buffer gnus-summary-buffer)
5833           (if (or (and gnus-single-article-buffer
5834                        (or (null gnus-current-article)
5835                            (null gnus-article-current)
5836                            (null (get-buffer gnus-article-buffer))
5837                            (not (eq article (cdr gnus-article-current)))
5838                            (not (equal (car gnus-article-current)
5839                                        gnus-newsgroup-name))))
5840                   (and (not gnus-single-article-buffer)
5841                        (or (null gnus-current-article)
5842                            (not (eq gnus-current-article article))))
5843                   force)
5844               ;; The requested article is different from the current article.
5845               (prog1
5846                   (gnus-summary-display-article article all-headers)
5847                 (setq did article)
5848                 (when (or all-headers gnus-show-all-headers)
5849                   (if (eq 'gnus-summary-toggle-mime this-command)
5850                       (gnus-article-show-all)
5851                     (gnus-article-show-all-headers))))
5852             (when (or all-headers gnus-show-all-headers)
5853               (gnus-article-show-all-headers))
5854             'old))
5855       (when did
5856         (gnus-article-set-window-start
5857          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5858
5859 (defun gnus-summary-set-current-mark (&optional current-mark)
5860   "Obsolete function."
5861   nil)
5862
5863 (defun gnus-summary-next-article (&optional unread subject backward push)
5864   "Select the next article.
5865 If UNREAD, only unread articles are selected.
5866 If SUBJECT, only articles with SUBJECT are selected.
5867 If BACKWARD, the previous article is selected instead of the next."
5868   (interactive "P")
5869   (cond
5870    ;; Is there such an article?
5871    ((and (gnus-summary-search-forward unread subject backward)
5872          (or (gnus-summary-display-article (gnus-summary-article-number))
5873              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5874     (gnus-summary-position-point))
5875    ;; If not, we try the first unread, if that is wanted.
5876    ((and subject
5877          gnus-auto-select-same
5878          (gnus-summary-first-unread-article))
5879     (gnus-summary-position-point)
5880     (gnus-message 6 "Wrapped"))
5881    ;; Try to get next/previous article not displayed in this group.
5882    ((and gnus-auto-extend-newsgroup
5883          (not unread) (not subject))
5884     (gnus-summary-goto-article
5885      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5886      nil (count-lines (point-min) (point))))
5887    ;; Go to next/previous group.
5888    (t
5889     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5890       (gnus-summary-jump-to-group gnus-newsgroup-name))
5891     (let ((cmd last-command-char)
5892           (point
5893            (save-excursion
5894              (set-buffer gnus-group-buffer)
5895              (point)))
5896           (group
5897            (if (eq gnus-keep-same-level 'best)
5898                (gnus-summary-best-group gnus-newsgroup-name)
5899              (gnus-summary-search-group backward gnus-keep-same-level))))
5900       ;; For some reason, the group window gets selected.  We change
5901       ;; it back.
5902       (select-window (get-buffer-window (current-buffer)))
5903       ;; Select next unread newsgroup automagically.
5904       (cond
5905        ((or (not gnus-auto-select-next)
5906             (not cmd))
5907         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5908        ((or (eq gnus-auto-select-next 'quietly)
5909             (and (eq gnus-auto-select-next 'slightly-quietly)
5910                  push)
5911             (and (eq gnus-auto-select-next 'almost-quietly)
5912                  (gnus-summary-last-article-p)))
5913         ;; Select quietly.
5914         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5915             (gnus-summary-exit)
5916           (gnus-message 7 "No more%s articles (%s)..."
5917                         (if unread " unread" "")
5918                         (if group (concat "selecting " group)
5919                           "exiting"))
5920           (gnus-summary-next-group nil group backward)))
5921        (t
5922         (when (gnus-key-press-event-p last-input-event)
5923           (gnus-summary-walk-group-buffer
5924            gnus-newsgroup-name cmd unread backward point))))))))
5925
5926 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5927   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5928                       (?\C-p (gnus-group-prev-unread-group 1))))
5929         (cursor-in-echo-area t)
5930         keve key group ended)
5931     (save-excursion
5932       (set-buffer gnus-group-buffer)
5933       (goto-char start)
5934       (setq group
5935             (if (eq gnus-keep-same-level 'best)
5936                 (gnus-summary-best-group gnus-newsgroup-name)
5937               (gnus-summary-search-group backward gnus-keep-same-level))))
5938     (while (not ended)
5939       (gnus-message
5940        5 "No more%s articles%s" (if unread " unread" "")
5941        (if (and group
5942                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5943            (format " (Type %s for %s [%s])"
5944                    (single-key-description cmd) group
5945                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5946          (format " (Type %s to exit %s)"
5947                  (single-key-description cmd)
5948                  gnus-newsgroup-name)))
5949       ;; Confirm auto selection.
5950       (setq key (car (setq keve (gnus-read-event-char))))
5951       (setq ended t)
5952       (cond
5953        ((assq key keystrokes)
5954         (let ((obuf (current-buffer)))
5955           (switch-to-buffer gnus-group-buffer)
5956           (when group
5957             (gnus-group-jump-to-group group))
5958           (eval (cadr (assq key keystrokes)))
5959           (setq group (gnus-group-group-name))
5960           (switch-to-buffer obuf))
5961         (setq ended nil))
5962        ((equal key cmd)
5963         (if (or (not group)
5964                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5965             (gnus-summary-exit)
5966           (gnus-summary-next-group nil group backward)))
5967        (t
5968         (push (cdr keve) unread-command-events))))))
5969
5970 (defun gnus-summary-next-unread-article ()
5971   "Select unread article after current one."
5972   (interactive)
5973   (gnus-summary-next-article
5974    (or (not (eq gnus-summary-goto-unread 'never))
5975        (gnus-summary-last-article-p (gnus-summary-article-number)))
5976    (and gnus-auto-select-same
5977         (gnus-summary-article-subject))))
5978
5979 (defun gnus-summary-prev-article (&optional unread subject)
5980   "Select the article after the current one.
5981 If UNREAD is non-nil, only unread articles are selected."
5982   (interactive "P")
5983   (gnus-summary-next-article unread subject t))
5984
5985 (defun gnus-summary-prev-unread-article ()
5986   "Select unread article before current one."
5987   (interactive)
5988   (gnus-summary-prev-article
5989    (or (not (eq gnus-summary-goto-unread 'never))
5990        (gnus-summary-first-article-p (gnus-summary-article-number)))
5991    (and gnus-auto-select-same
5992         (gnus-summary-article-subject))))
5993
5994 (defun gnus-summary-next-page (&optional lines circular)
5995   "Show next page of the selected article.
5996 If at the end of the current article, select the next article.
5997 LINES says how many lines should be scrolled up.
5998
5999 If CIRCULAR is non-nil, go to the start of the article instead of
6000 selecting the next article when reaching the end of the current
6001 article."
6002   (interactive "P")
6003   (setq gnus-summary-buffer (current-buffer))
6004   (gnus-set-global-variables)
6005   (let ((article (gnus-summary-article-number))
6006         (article-window (get-buffer-window gnus-article-buffer t))
6007         endp)
6008     ;; If the buffer is empty, we have no article.
6009     (unless article
6010       (error "No article to select"))
6011     (gnus-configure-windows 'article)
6012     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6013         (if (and (eq gnus-summary-goto-unread 'never)
6014                  (not (gnus-summary-last-article-p article)))
6015             (gnus-summary-next-article)
6016           (gnus-summary-next-unread-article))
6017       (if (or (null gnus-current-article)
6018               (null gnus-article-current)
6019               (/= article (cdr gnus-article-current))
6020               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6021           ;; Selected subject is different from current article's.
6022           (gnus-summary-display-article article)
6023         (when article-window
6024           (gnus-eval-in-buffer-window gnus-article-buffer
6025             (setq endp (gnus-article-next-page lines)))
6026           (when endp
6027             (cond (circular
6028                    (gnus-summary-beginning-of-article))
6029                   (lines
6030                    (gnus-message 3 "End of message"))
6031                   ((null lines)
6032                    (if (and (eq gnus-summary-goto-unread 'never)
6033                             (not (gnus-summary-last-article-p article)))
6034                        (gnus-summary-next-article)
6035                      (gnus-summary-next-unread-article))))))))
6036     (gnus-summary-recenter)
6037     (gnus-summary-position-point)))
6038
6039 (defun gnus-summary-prev-page (&optional lines move)
6040   "Show previous page of selected article.
6041 Argument LINES specifies lines to be scrolled down.
6042 If MOVE, move to the previous unread article if point is at
6043 the beginning of the buffer."
6044   (interactive "P")
6045   (let ((article (gnus-summary-article-number))
6046         (article-window (get-buffer-window gnus-article-buffer t))
6047         endp)
6048     (gnus-configure-windows 'article)
6049     (if (or (null gnus-current-article)
6050             (null gnus-article-current)
6051             (/= article (cdr gnus-article-current))
6052             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6053         ;; Selected subject is different from current article's.
6054         (gnus-summary-display-article article)
6055       (gnus-summary-recenter)
6056       (when article-window
6057         (gnus-eval-in-buffer-window gnus-article-buffer
6058           (setq endp (gnus-article-prev-page lines)))
6059         (when (and move endp)
6060           (cond (lines
6061                  (gnus-message 3 "Beginning of message"))
6062                 ((null lines)
6063                  (if (and (eq gnus-summary-goto-unread 'never)
6064                           (not (gnus-summary-first-article-p article)))
6065                      (gnus-summary-prev-article)
6066                    (gnus-summary-prev-unread-article))))))))
6067   (gnus-summary-position-point))
6068
6069 (defun gnus-summary-prev-page-or-article (&optional lines)
6070   "Show previous page of selected article.
6071 Argument LINES specifies lines to be scrolled down.
6072 If at the beginning of the article, go to the next article."
6073   (interactive "P")
6074   (gnus-summary-prev-page lines t))
6075
6076 (defun gnus-summary-scroll-up (lines)
6077   "Scroll up (or down) one line current article.
6078 Argument LINES specifies lines to be scrolled up (or down if negative)."
6079   (interactive "p")
6080   (gnus-configure-windows 'article)
6081   (gnus-summary-show-thread)
6082   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6083     (gnus-eval-in-buffer-window gnus-article-buffer
6084       (cond ((> lines 0)
6085              (when (gnus-article-next-page lines)
6086                (gnus-message 3 "End of message")))
6087             ((< lines 0)
6088              (gnus-article-prev-page (- lines))))))
6089   (gnus-summary-recenter)
6090   (gnus-summary-position-point))
6091
6092 (defun gnus-summary-scroll-down (lines)
6093   "Scroll down (or up) one line current article.
6094 Argument LINES specifies lines to be scrolled down (or up if negative)."
6095   (interactive "p")
6096   (gnus-summary-scroll-up (- lines)))
6097
6098 (defun gnus-summary-next-same-subject ()
6099   "Select next article which has the same subject as current one."
6100   (interactive)
6101   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6102
6103 (defun gnus-summary-prev-same-subject ()
6104   "Select previous article which has the same subject as current one."
6105   (interactive)
6106   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6107
6108 (defun gnus-summary-next-unread-same-subject ()
6109   "Select next unread article which has the same subject as current one."
6110   (interactive)
6111   (gnus-summary-next-article t (gnus-summary-article-subject)))
6112
6113 (defun gnus-summary-prev-unread-same-subject ()
6114   "Select previous unread article which has the same subject as current one."
6115   (interactive)
6116   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6117
6118 (defun gnus-summary-first-unread-article ()
6119   "Select the first unread article.
6120 Return nil if there are no unread articles."
6121   (interactive)
6122   (prog1
6123       (when (gnus-summary-first-subject t)
6124         (gnus-summary-show-thread)
6125         (gnus-summary-first-subject t)
6126         (gnus-summary-display-article (gnus-summary-article-number)))
6127     (gnus-summary-position-point)))
6128
6129 (defun gnus-summary-first-unread-subject ()
6130   "Place the point on the subject line of the first unread article.
6131 Return nil if there are no unread articles."
6132   (interactive)
6133   (prog1
6134       (when (gnus-summary-first-subject t)
6135         (gnus-summary-show-thread)
6136         (gnus-summary-first-subject t))
6137     (gnus-summary-position-point)))
6138
6139 (defun gnus-summary-first-article ()
6140   "Select the first article.
6141 Return nil if there are no articles."
6142   (interactive)
6143   (prog1
6144       (when (gnus-summary-first-subject)
6145         (gnus-summary-show-thread)
6146         (gnus-summary-first-subject)
6147         (gnus-summary-display-article (gnus-summary-article-number)))
6148     (gnus-summary-position-point)))
6149
6150 (defun gnus-summary-best-unread-article ()
6151   "Select the unread article with the highest score."
6152   (interactive)
6153   (let ((best -1000000)
6154         (data gnus-newsgroup-data)
6155         article score)
6156     (while data
6157       (and (gnus-data-unread-p (car data))
6158            (> (setq score
6159                     (gnus-summary-article-score (gnus-data-number (car data))))
6160               best)
6161            (setq best score
6162                  article (gnus-data-number (car data))))
6163       (setq data (cdr data)))
6164     (prog1
6165         (if article
6166             (gnus-summary-goto-article article)
6167           (error "No unread articles"))
6168       (gnus-summary-position-point))))
6169
6170 (defun gnus-summary-last-subject ()
6171   "Go to the last displayed subject line in the group."
6172   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6173     (when article
6174       (gnus-summary-goto-subject article))))
6175
6176 (defun gnus-summary-goto-article (article &optional all-headers force)
6177   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6178 If ALL-HEADERS is non-nil, no header lines are hidden.
6179 If FORCE, go to the article even if it isn't displayed.  If FORCE
6180 is a number, it is the line the article is to be displayed on."
6181   (interactive
6182    (list
6183     (completing-read
6184      "Article number or Message-ID: "
6185      (mapcar (lambda (number) (list (int-to-string number)))
6186              gnus-newsgroup-limit))
6187     current-prefix-arg
6188     t))
6189   (prog1
6190       (if (and (stringp article)
6191                (string-match "@" article))
6192           (gnus-summary-refer-article article)
6193         (when (stringp article)
6194           (setq article (string-to-number article)))
6195         (if (gnus-summary-goto-subject article force)
6196             (gnus-summary-display-article article all-headers)
6197           (gnus-message 4 "Couldn't go to article %s" article) nil))
6198     (gnus-summary-position-point)))
6199
6200 (defun gnus-summary-goto-last-article ()
6201   "Go to the previously read article."
6202   (interactive)
6203   (prog1
6204       (when gnus-last-article
6205         (gnus-summary-goto-article gnus-last-article nil t))
6206     (gnus-summary-position-point)))
6207
6208 (defun gnus-summary-pop-article (number)
6209   "Pop one article off the history and go to the previous.
6210 NUMBER articles will be popped off."
6211   (interactive "p")
6212   (let (to)
6213     (setq gnus-newsgroup-history
6214           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6215     (if to
6216         (gnus-summary-goto-article (car to) nil t)
6217       (error "Article history empty")))
6218   (gnus-summary-position-point))
6219
6220 ;; Summary commands and functions for limiting the summary buffer.
6221
6222 (defun gnus-summary-limit-to-articles (n)
6223   "Limit the summary buffer to the next N articles.
6224 If not given a prefix, use the process marked articles instead."
6225   (interactive "P")
6226   (prog1
6227       (let ((articles (gnus-summary-work-articles n)))
6228         (setq gnus-newsgroup-processable nil)
6229         (gnus-summary-limit articles))
6230     (gnus-summary-position-point)))
6231
6232 (defun gnus-summary-pop-limit (&optional total)
6233   "Restore the previous limit.
6234 If given a prefix, remove all limits."
6235   (interactive "P")
6236   (when total
6237     (setq gnus-newsgroup-limits
6238           (list (mapcar (lambda (h) (mail-header-number h))
6239                         gnus-newsgroup-headers))))
6240   (unless gnus-newsgroup-limits
6241     (error "No limit to pop"))
6242   (prog1
6243       (gnus-summary-limit nil 'pop)
6244     (gnus-summary-position-point)))
6245
6246 (defun gnus-summary-limit-to-subject (subject &optional header)
6247   "Limit the summary buffer to articles that have subjects that match a regexp."
6248   (interactive "sLimit to subject (regexp): ")
6249   (unless header
6250     (setq header "subject"))
6251   (when (not (equal "" subject))
6252     (prog1
6253         (let ((articles (gnus-summary-find-matching
6254                          (or header "subject") subject 'all)))
6255           (unless articles
6256             (error "Found no matches for \"%s\"" subject))
6257           (gnus-summary-limit articles))
6258       (gnus-summary-position-point))))
6259
6260 (defun gnus-summary-limit-to-author (from)
6261   "Limit the summary buffer to articles that have authors that match a regexp."
6262   (interactive "sLimit to author (regexp): ")
6263   (gnus-summary-limit-to-subject from "from"))
6264
6265 (defun gnus-summary-limit-to-age (age &optional younger-p)
6266   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6267 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6268 articles that are younger than AGE days."
6269   (interactive
6270    (let ((younger current-prefix-arg)
6271          (days-got nil)
6272          days)
6273      (while (not days-got)
6274        (setq days (if younger
6275                       (read-string "Limit to articles within (in days): ")
6276                     (read-string "Limit to articles old than (in days): ")))
6277        (when (> (length days) 0)
6278          (setq days (read days)))
6279        (if (numberp days)
6280            (setq days-got t)
6281          (message "Please enter a number.")
6282          (sleep-for 1)))
6283      (list days younger)))
6284   (prog1
6285       (let ((data gnus-newsgroup-data)
6286             (cutoff (days-to-time age))
6287             articles d date is-younger)
6288         (while (setq d (pop data))
6289           (when (and (vectorp (gnus-data-header d))
6290                      (setq date (mail-header-date (gnus-data-header d))))
6291             (setq is-younger (time-less-p
6292                               (time-since (condition-case ()
6293                                               (date-to-time date)
6294                                             (error '(0 0))))
6295                               cutoff))
6296             (when (if younger-p
6297                       is-younger
6298                     (not is-younger))
6299               (push (gnus-data-number d) articles))))
6300         (gnus-summary-limit (nreverse articles)))
6301     (gnus-summary-position-point)))
6302
6303 (defun gnus-summary-limit-to-extra (header regexp)
6304   "Limit the summary buffer to articles that match an 'extra' header."
6305   (interactive
6306    (let ((header
6307           (intern
6308            (gnus-completing-read
6309             (symbol-name (car gnus-extra-headers))      
6310             "Limit extra header:"       
6311             (mapcar (lambda (x) 
6312                       (cons (symbol-name x) x))
6313                     gnus-extra-headers)
6314             nil                 
6315             t))))
6316      (list header
6317            (read-string (format "Limit to header %s (regexp): " header)))))
6318   (when (not (equal "" regexp))
6319     (prog1
6320         (let ((articles (gnus-summary-find-matching
6321                          (cons 'extra header) regexp 'all)))
6322           (unless articles
6323             (error "Found no matches for \"%s\"" regexp))
6324           (gnus-summary-limit articles))
6325       (gnus-summary-position-point))))
6326
6327 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6328 (make-obsolete
6329  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6330
6331 (defun gnus-summary-limit-to-unread (&optional all)
6332   "Limit the summary buffer to articles that are not marked as read.
6333 If ALL is non-nil, limit strictly to unread articles."
6334   (interactive "P")
6335   (if all
6336       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6337     (gnus-summary-limit-to-marks
6338      ;; Concat all the marks that say that an article is read and have
6339      ;; those removed.
6340      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6341            gnus-killed-mark gnus-kill-file-mark
6342            gnus-low-score-mark gnus-expirable-mark
6343            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6344            gnus-duplicate-mark gnus-souped-mark)
6345      'reverse)))
6346
6347 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6348 (make-obsolete 'gnus-summary-delete-marked-with
6349                'gnus-summary-limit-exlude-marks)
6350
6351 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6352   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6353 If REVERSE, limit the summary buffer to articles that are marked
6354 with MARKS.  MARKS can either be a string of marks or a list of marks.
6355 Returns how many articles were removed."
6356   (interactive "sMarks: ")
6357   (gnus-summary-limit-to-marks marks t))
6358
6359 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6360   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6361 If REVERSE (the prefix), limit the summary buffer to articles that are
6362 not marked with MARKS.  MARKS can either be a string of marks or a
6363 list of marks.
6364 Returns how many articles were removed."
6365   (interactive "sMarks: \nP")
6366   (prog1
6367       (let ((data gnus-newsgroup-data)
6368             (marks (if (listp marks) marks
6369                      (append marks nil))) ; Transform to list.
6370             articles)
6371         (while data
6372           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6373                   (memq (gnus-data-mark (car data)) marks))
6374             (push (gnus-data-number (car data)) articles))
6375           (setq data (cdr data)))
6376         (gnus-summary-limit articles))
6377     (gnus-summary-position-point)))
6378
6379 (defun gnus-summary-limit-to-score (&optional score)
6380   "Limit to articles with score at or above SCORE."
6381   (interactive "P")
6382   (setq score (if score
6383                   (prefix-numeric-value score)
6384                 (or gnus-summary-default-score 0)))
6385   (let ((data gnus-newsgroup-data)
6386         articles)
6387     (while data
6388       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6389                 score)
6390         (push (gnus-data-number (car data)) articles))
6391       (setq data (cdr data)))
6392     (prog1
6393         (gnus-summary-limit articles)
6394       (gnus-summary-position-point))))
6395
6396 (defun gnus-summary-limit-include-thread (id)
6397   "Display all the hidden articles that in the current thread."
6398   (interactive (list (mail-header-id (gnus-summary-article-header))))
6399   (let ((articles (gnus-articles-in-thread
6400                    (gnus-id-to-thread (gnus-root-id id)))))
6401     (prog1
6402         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6403       (gnus-summary-position-point))))
6404
6405 (defun gnus-summary-limit-include-dormant ()
6406   "Display all the hidden articles that are marked as dormant.
6407 Note that this command only works on a subset of the articles currently
6408 fetched for this group."
6409   (interactive)
6410   (unless gnus-newsgroup-dormant
6411     (error "There are no dormant articles in this group"))
6412   (prog1
6413       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6414     (gnus-summary-position-point)))
6415
6416 (defun gnus-summary-limit-exclude-dormant ()
6417   "Hide all dormant articles."
6418   (interactive)
6419   (prog1
6420       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6421     (gnus-summary-position-point)))
6422
6423 (defun gnus-summary-limit-exclude-childless-dormant ()
6424   "Hide all dormant articles that have no children."
6425   (interactive)
6426   (let ((data (gnus-data-list t))
6427         articles d children)
6428     ;; Find all articles that are either not dormant or have
6429     ;; children.
6430     (while (setq d (pop data))
6431       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6432                 (and (setq children
6433                            (gnus-article-children (gnus-data-number d)))
6434                      (let (found)
6435                        (while children
6436                          (when (memq (car children) articles)
6437                            (setq children nil
6438                                  found t))
6439                          (pop children))
6440                        found)))
6441         (push (gnus-data-number d) articles)))
6442     ;; Do the limiting.
6443     (prog1
6444         (gnus-summary-limit articles)
6445       (gnus-summary-position-point))))
6446
6447 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6448   "Mark all unread excluded articles as read.
6449 If ALL, mark even excluded ticked and dormants as read."
6450   (interactive "P")
6451   (let ((articles (gnus-sorted-complement
6452                    (sort
6453                     (mapcar (lambda (h) (mail-header-number h))
6454                             gnus-newsgroup-headers)
6455                     '<)
6456                    (sort gnus-newsgroup-limit '<)))
6457         article)
6458     (setq gnus-newsgroup-unreads
6459           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6460     (if all
6461         (setq gnus-newsgroup-dormant nil
6462               gnus-newsgroup-marked nil
6463               gnus-newsgroup-reads
6464               (nconc
6465                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6466                gnus-newsgroup-reads))
6467       (while (setq article (pop articles))
6468         (unless (or (memq article gnus-newsgroup-dormant)
6469                     (memq article gnus-newsgroup-marked))
6470           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6471
6472 (defun gnus-summary-limit (articles &optional pop)
6473   (if pop
6474       ;; We pop the previous limit off the stack and use that.
6475       (setq articles (car gnus-newsgroup-limits)
6476             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6477     ;; We use the new limit, so we push the old limit on the stack.
6478     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6479   ;; Set the limit.
6480   (setq gnus-newsgroup-limit articles)
6481   (let ((total (length gnus-newsgroup-data))
6482         (data (gnus-data-find-list (gnus-summary-article-number)))
6483         (gnus-summary-mark-below nil)   ; Inhibit this.
6484         found)
6485     ;; This will do all the work of generating the new summary buffer
6486     ;; according to the new limit.
6487     (gnus-summary-prepare)
6488     ;; Hide any threads, possibly.
6489     (and gnus-show-threads
6490          gnus-thread-hide-subtree
6491          (gnus-summary-hide-all-threads))
6492     ;; Try to return to the article you were at, or one in the
6493     ;; neighborhood.
6494     (when data
6495       ;; We try to find some article after the current one.
6496       (while data
6497         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6498           (setq data nil
6499                 found t))
6500         (setq data (cdr data))))
6501     (unless found
6502       ;; If there is no data, that means that we were after the last
6503       ;; article.  The same goes when we can't find any articles
6504       ;; after the current one.
6505       (goto-char (point-max))
6506       (gnus-summary-find-prev))
6507     (gnus-set-mode-line 'summary)
6508     ;; We return how many articles were removed from the summary
6509     ;; buffer as a result of the new limit.
6510     (- total (length gnus-newsgroup-data))))
6511
6512 (defsubst gnus-invisible-cut-children (threads)
6513   (let ((num 0))
6514     (while threads
6515       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6516         (incf num))
6517       (pop threads))
6518     (< num 2)))
6519
6520 (defsubst gnus-cut-thread (thread)
6521   "Go forwards in the thread until we find an article that we want to display."
6522   (when (or (eq gnus-fetch-old-headers 'some)
6523             (eq gnus-fetch-old-headers 'invisible)
6524             (numberp gnus-fetch-old-headers)
6525             (eq gnus-build-sparse-threads 'some)
6526             (eq gnus-build-sparse-threads 'more))
6527     ;; Deal with old-fetched headers and sparse threads.
6528     (while (and
6529             thread
6530             (or
6531              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6532              (gnus-summary-article-ancient-p
6533               (mail-header-number (car thread))))
6534             (if (or (<= (length (cdr thread)) 1)
6535                     (eq gnus-fetch-old-headers 'invisible))
6536                 (setq gnus-newsgroup-limit
6537                       (delq (mail-header-number (car thread))
6538                             gnus-newsgroup-limit)
6539                       thread (cadr thread))
6540               (when (gnus-invisible-cut-children (cdr thread))
6541                 (let ((th (cdr thread)))
6542                   (while th
6543                     (if (memq (mail-header-number (caar th))
6544                               gnus-newsgroup-limit)
6545                         (setq thread (car th)
6546                               th nil)
6547                       (setq th (cdr th))))))))))
6548   thread)
6549
6550 (defun gnus-cut-threads (threads)
6551   "Cut off all uninteresting articles from the beginning of threads."
6552   (when (or (eq gnus-fetch-old-headers 'some)
6553             (eq gnus-fetch-old-headers 'invisible)
6554             (numberp gnus-fetch-old-headers)
6555             (eq gnus-build-sparse-threads 'some)
6556             (eq gnus-build-sparse-threads 'more))
6557     (let ((th threads))
6558       (while th
6559         (setcar th (gnus-cut-thread (car th)))
6560         (setq th (cdr th)))))
6561   ;; Remove nixed out threads.
6562   (delq nil threads))
6563
6564 (defun gnus-summary-initial-limit (&optional show-if-empty)
6565   "Figure out what the initial limit is supposed to be on group entry.
6566 This entails weeding out unwanted dormants, low-scored articles,
6567 fetch-old-headers verbiage, and so on."
6568   ;; Most groups have nothing to remove.
6569   (if (or gnus-inhibit-limiting
6570           (and (null gnus-newsgroup-dormant)
6571                (not (eq gnus-fetch-old-headers 'some))
6572                (not (numberp gnus-fetch-old-headers))
6573                (not (eq gnus-fetch-old-headers 'invisible))
6574                (null gnus-summary-expunge-below)
6575                (not (eq gnus-build-sparse-threads 'some))
6576                (not (eq gnus-build-sparse-threads 'more))
6577                (null gnus-thread-expunge-below)
6578                (not gnus-use-nocem)))
6579       ()                                ; Do nothing.
6580     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6581     (setq gnus-newsgroup-limit nil)
6582     (mapatoms
6583      (lambda (node)
6584        (unless (car (symbol-value node))
6585          ;; These threads have no parents -- they are roots.
6586          (let ((nodes (cdr (symbol-value node)))
6587                thread)
6588            (while nodes
6589              (if (and gnus-thread-expunge-below
6590                       (< (gnus-thread-total-score (car nodes))
6591                          gnus-thread-expunge-below))
6592                  (gnus-expunge-thread (pop nodes))
6593                (setq thread (pop nodes))
6594                (gnus-summary-limit-children thread))))))
6595      gnus-newsgroup-dependencies)
6596     ;; If this limitation resulted in an empty group, we might
6597     ;; pop the previous limit and use it instead.
6598     (when (and (not gnus-newsgroup-limit)
6599                show-if-empty)
6600       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6601     gnus-newsgroup-limit))
6602
6603 (defun gnus-summary-limit-children (thread)
6604   "Return 1 if this subthread is visible and 0 if it is not."
6605   ;; First we get the number of visible children to this thread.  This
6606   ;; is done by recursing down the thread using this function, so this
6607   ;; will really go down to a leaf article first, before slowly
6608   ;; working its way up towards the root.
6609   (when thread
6610     (let ((children
6611            (if (cdr thread)
6612                (apply '+ (mapcar 'gnus-summary-limit-children
6613                                  (cdr thread)))
6614              0))
6615           (number (mail-header-number (car thread)))
6616           score)
6617       (if (and
6618            (not (memq number gnus-newsgroup-marked))
6619            (or
6620             ;; If this article is dormant and has absolutely no visible
6621             ;; children, then this article isn't visible.
6622             (and (memq number gnus-newsgroup-dormant)
6623                  (zerop children))
6624             ;; If this is "fetch-old-headered" and there is no
6625             ;; visible children, then we don't want this article.
6626             (and (or (eq gnus-fetch-old-headers 'some)
6627                      (numberp gnus-fetch-old-headers))
6628                  (gnus-summary-article-ancient-p number)
6629                  (zerop children))
6630             ;; If this is "fetch-old-headered" and `invisible', then
6631             ;; we don't want this article.
6632             (and (eq gnus-fetch-old-headers 'invisible)
6633                  (gnus-summary-article-ancient-p number))
6634             ;; If this is a sparsely inserted article with no children,
6635             ;; we don't want it.
6636             (and (eq gnus-build-sparse-threads 'some)
6637                  (gnus-summary-article-sparse-p number)
6638                  (zerop children))
6639             ;; If we use expunging, and this article is really
6640             ;; low-scored, then we don't want this article.
6641             (when (and gnus-summary-expunge-below
6642                        (< (setq score
6643                                 (or (cdr (assq number gnus-newsgroup-scored))
6644                                     gnus-summary-default-score))
6645                           gnus-summary-expunge-below))
6646               ;; We increase the expunge-tally here, but that has
6647               ;; nothing to do with the limits, really.
6648               (incf gnus-newsgroup-expunged-tally)
6649               ;; We also mark as read here, if that's wanted.
6650               (when (and gnus-summary-mark-below
6651                          (< score gnus-summary-mark-below))
6652                 (setq gnus-newsgroup-unreads
6653                       (delq number gnus-newsgroup-unreads))
6654                 (if gnus-newsgroup-auto-expire
6655                     (push number gnus-newsgroup-expirable)
6656                   (push (cons number gnus-low-score-mark)
6657                         gnus-newsgroup-reads)))
6658               t)
6659             ;; Check NoCeM things.
6660             (if (and gnus-use-nocem
6661                      (gnus-nocem-unwanted-article-p
6662                       (mail-header-id (car thread))))
6663                 (progn
6664                   (setq gnus-newsgroup-unreads
6665                         (delq number gnus-newsgroup-unreads))
6666                   t))))
6667           ;; Nope, invisible article.
6668           0
6669         ;; Ok, this article is to be visible, so we add it to the limit
6670         ;; and return 1.
6671         (push number gnus-newsgroup-limit)
6672         1))))
6673
6674 (defun gnus-expunge-thread (thread)
6675   "Mark all articles in THREAD as read."
6676   (let* ((number (mail-header-number (car thread))))
6677     (incf gnus-newsgroup-expunged-tally)
6678     ;; We also mark as read here, if that's wanted.
6679     (setq gnus-newsgroup-unreads
6680           (delq number gnus-newsgroup-unreads))
6681     (if gnus-newsgroup-auto-expire
6682         (push number gnus-newsgroup-expirable)
6683       (push (cons number gnus-low-score-mark)
6684             gnus-newsgroup-reads)))
6685   ;; Go recursively through all subthreads.
6686   (mapcar 'gnus-expunge-thread (cdr thread)))
6687
6688 ;; Summary article oriented commands
6689
6690 (defun gnus-summary-refer-parent-article (n)
6691   "Refer parent article N times.
6692 If N is negative, go to ancestor -N instead.
6693 The difference between N and the number of articles fetched is returned."
6694   (interactive "p")
6695   (let ((skip 1)
6696         error header ref)
6697     (when (not (natnump n))
6698       (setq skip (abs n)
6699             n 1))
6700     (while (and (> n 0)
6701                 (not error))
6702       (setq header (gnus-summary-article-header))
6703       (if (and (eq (mail-header-number header)
6704                    (cdr gnus-article-current))
6705                (equal gnus-newsgroup-name
6706                       (car gnus-article-current)))
6707           ;; If we try to find the parent of the currently
6708           ;; displayed article, then we take a look at the actual
6709           ;; References header, since this is slightly more
6710           ;; reliable than the References field we got from the
6711           ;; server.
6712           (save-excursion
6713             (set-buffer gnus-original-article-buffer)
6714             (nnheader-narrow-to-headers)
6715             (unless (setq ref (message-fetch-field "references"))
6716               (setq ref (message-fetch-field "in-reply-to")))
6717             (widen))
6718         (setq ref
6719               ;; It's not the current article, so we take a bet on
6720               ;; the value we got from the server.
6721               (mail-header-references header)))
6722       (if (and ref
6723                (not (equal ref "")))
6724           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6725             (gnus-message 1 "Couldn't find parent"))
6726         (gnus-message 1 "No references in article %d"
6727                       (gnus-summary-article-number))
6728         (setq error t))
6729       (decf n))
6730     (gnus-summary-position-point)
6731     n))
6732
6733 (defun gnus-summary-refer-references ()
6734   "Fetch all articles mentioned in the References header.
6735 Return the number of articles fetched."
6736   (interactive)
6737   (let ((ref (mail-header-references (gnus-summary-article-header)))
6738         (current (gnus-summary-article-number))
6739         (n 0))
6740     (if (or (not ref)
6741             (equal ref ""))
6742         (error "No References in the current article")
6743       ;; For each Message-ID in the References header...
6744       (while (string-match "<[^>]*>" ref)
6745         (incf n)
6746         ;; ... fetch that article.
6747         (gnus-summary-refer-article
6748          (prog1 (match-string 0 ref)
6749            (setq ref (substring ref (match-end 0))))))
6750       (gnus-summary-goto-subject current)
6751       (gnus-summary-position-point)
6752       n)))
6753
6754 (defun gnus-summary-refer-thread (&optional limit)
6755   "Fetch all articles in the current thread.
6756 If LIMIT (the numerical prefix), fetch that many old headers instead
6757 of what's specified by the `gnus-refer-thread-limit' variable."
6758   (interactive "P")
6759   (let ((id (mail-header-id (gnus-summary-article-header)))
6760         (limit (if limit (prefix-numeric-value limit)
6761                  gnus-refer-thread-limit)))
6762     ;; We want to fetch LIMIT *old* headers, but we also have to
6763     ;; re-fetch all the headers in the current buffer, because many of
6764     ;; them may be undisplayed.  So we adjust LIMIT.
6765     (when (numberp limit)
6766       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6767     (unless (eq gnus-fetch-old-headers 'invisible)
6768       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6769       ;; Retrieve the headers and read them in.
6770       (if (eq (gnus-retrieve-headers
6771                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6772               'nov)
6773           (gnus-build-all-threads)
6774         (error "Can't fetch thread from backends that don't support NOV"))
6775       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6776     (gnus-summary-limit-include-thread id)))
6777
6778 (defun gnus-summary-refer-article (message-id)
6779   "Fetch an article specified by MESSAGE-ID."
6780   (interactive "sMessage-ID: ")
6781   (when (and (stringp message-id)
6782              (not (zerop (length message-id))))
6783     ;; Construct the correct Message-ID if necessary.
6784     ;; Suggested by tale@pawl.rpi.edu.
6785     (unless (string-match "^<" message-id)
6786       (setq message-id (concat "<" message-id)))
6787     (unless (string-match ">$" message-id)
6788       (setq message-id (concat message-id ">")))
6789     (let* ((header (gnus-id-to-header message-id))
6790            (sparse (and header
6791                         (gnus-summary-article-sparse-p
6792                          (mail-header-number header))
6793                         (memq (mail-header-number header)
6794                               gnus-newsgroup-limit)))
6795            number)
6796       (cond
6797        ;; If the article is present in the buffer we just go to it.
6798        ((and header
6799              (or (not (gnus-summary-article-sparse-p
6800                        (mail-header-number header)))
6801                  sparse))
6802         (prog1
6803             (gnus-summary-goto-article
6804              (mail-header-number header) nil t)
6805           (when sparse
6806             (gnus-summary-update-article (mail-header-number header)))))
6807        (t
6808         ;; We fetch the article.
6809         (catch 'found
6810           (dolist (gnus-override-method
6811                    (cond ((null gnus-refer-article-method)
6812                           (list 'current gnus-select-method))
6813                          ((consp (car gnus-refer-article-method))
6814                           gnus-refer-article-method)
6815                          (t
6816                           (list gnus-refer-article-method))))
6817             (when (eq 'current gnus-override-method)
6818               (setq gnus-override-method gnus-current-select-method))
6819             (gnus-check-server gnus-override-method)
6820             ;; Fetch the header, and display the article.
6821             (when (setq number (gnus-summary-insert-subject message-id))
6822               (gnus-summary-select-article nil nil nil number)
6823               (throw 'found t)))
6824           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6825
6826 (defun gnus-summary-edit-parameters ()
6827   "Edit the group parameters of the current group."
6828   (interactive)
6829   (gnus-group-edit-group gnus-newsgroup-name 'params))
6830
6831 (defun gnus-summary-customize-parameters ()
6832   "Customize the group parameters of the current group."
6833   (interactive)
6834   (gnus-group-customize gnus-newsgroup-name))
6835
6836 (defun gnus-summary-enter-digest-group (&optional force)
6837   "Enter an nndoc group based on the current article.
6838 If FORCE, force a digest interpretation.  If not, try
6839 to guess what the document format is."
6840   (interactive "P")
6841   (let ((conf gnus-current-window-configuration))
6842     (save-excursion
6843       (gnus-summary-select-article))
6844     (setq gnus-current-window-configuration conf)
6845     (let* ((name (format "%s-%d"
6846                          (gnus-group-prefixed-name
6847                           gnus-newsgroup-name (list 'nndoc ""))
6848                          (save-excursion
6849                            (set-buffer gnus-summary-buffer)
6850                            gnus-current-article)))
6851            (ogroup gnus-newsgroup-name)
6852            (params (append (gnus-info-params (gnus-get-info ogroup))
6853                            (list (cons 'to-group ogroup))
6854                            (list (cons 'save-article-group ogroup))))
6855            (case-fold-search t)
6856            (buf (current-buffer))
6857            dig to-address)
6858       (save-excursion
6859         (set-buffer gnus-original-article-buffer)
6860         ;; Have the digest group inherit the main mail address of
6861         ;; the parent article.
6862         (when (setq to-address (or (message-fetch-field "reply-to")
6863                                    (message-fetch-field "from")))
6864           (setq params (append (list (cons 'to-address to-address)))))
6865         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6866         (insert-buffer-substring gnus-original-article-buffer)
6867         ;; Remove lines that may lead nndoc to misinterpret the
6868         ;; document type.
6869         (narrow-to-region
6870          (goto-char (point-min))
6871          (or (search-forward "\n\n" nil t) (point)))
6872         (goto-char (point-min))
6873         (delete-matching-lines "^Path:\\|^From ")
6874         (widen))
6875       (unwind-protect
6876           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6877                     (gnus-newsgroup-ephemeral-ignored-charsets
6878                      gnus-newsgroup-ignored-charsets))
6879                 (gnus-group-read-ephemeral-group
6880                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6881                               (nndoc-article-type
6882                                ,(if force 'mbox 'guess))) t))
6883               ;; Make all postings to this group go to the parent group.
6884               (nconc (gnus-info-params (gnus-get-info name))
6885                      params)
6886             ;; Couldn't select this doc group.
6887             (switch-to-buffer buf)
6888             (gnus-set-global-variables)
6889             (gnus-configure-windows 'summary)
6890             (gnus-message 3 "Article couldn't be entered?"))
6891         (kill-buffer dig)))))
6892
6893 (defun gnus-summary-read-document (n)
6894   "Open a new group based on the current article(s).
6895 This will allow you to read digests and other similar
6896 documents as newsgroups.
6897 Obeys the standard process/prefix convention."
6898   (interactive "P")
6899   (let* ((articles (gnus-summary-work-articles n))
6900          (ogroup gnus-newsgroup-name)
6901          (params (append (gnus-info-params (gnus-get-info ogroup))
6902                          (list (cons 'to-group ogroup))))
6903          article group egroup groups vgroup)
6904     (while (setq article (pop articles))
6905       (setq group (format "%s-%d" gnus-newsgroup-name article))
6906       (gnus-summary-remove-process-mark article)
6907       (when (gnus-summary-display-article article)
6908         (save-excursion
6909           (with-temp-buffer
6910             (insert-buffer-substring gnus-original-article-buffer)
6911             ;; Remove some headers that may lead nndoc to make
6912             ;; the wrong guess.
6913             (message-narrow-to-head)
6914             (goto-char (point-min))
6915             (delete-matching-lines "^\\(Path\\):\\|^From ")
6916             (widen)
6917             (if (setq egroup
6918                       (gnus-group-read-ephemeral-group
6919                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6920                                      (nndoc-article-type guess))
6921                        t nil t))
6922                 (progn
6923                   ;; Make all postings to this group go to the parent group.
6924                   (nconc (gnus-info-params (gnus-get-info egroup))
6925                          params)
6926                   (push egroup groups))
6927               ;; Couldn't select this doc group.
6928               (gnus-error 3 "Article couldn't be entered"))))))
6929     ;; Now we have selected all the documents.
6930     (cond
6931      ((not groups)
6932       (error "None of the articles could be interpreted as documents"))
6933      ((gnus-group-read-ephemeral-group
6934        (setq vgroup (format
6935                      "nnvirtual:%s-%s" gnus-newsgroup-name
6936                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6937        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6938        t
6939        (cons (current-buffer) 'summary)))
6940      (t
6941       (error "Couldn't select virtual nndoc group")))))
6942
6943 (defun gnus-summary-isearch-article (&optional regexp-p)
6944   "Do incremental search forward on the current article.
6945 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6946   (interactive "P")
6947   (let* ((gnus-inhibit-treatment t)
6948          (old (gnus-summary-select-article)))
6949     (gnus-configure-windows 'article)
6950     (gnus-eval-in-buffer-window gnus-article-buffer
6951       (save-restriction
6952         (widen)
6953         (when (eq 'old old)
6954           (gnus-article-show-all-headers))
6955         (goto-char (point-min))
6956         (isearch-forward regexp-p)))))
6957
6958 (defun gnus-summary-search-article-forward (regexp &optional backward)
6959   "Search for an article containing REGEXP forward.
6960 If BACKWARD, search backward instead."
6961   (interactive
6962    (list (read-string
6963           (format "Search article %s (regexp%s): "
6964                   (if current-prefix-arg "backward" "forward")
6965                   (if gnus-last-search-regexp
6966                       (concat ", default " gnus-last-search-regexp)
6967                     "")))
6968          current-prefix-arg))
6969   (if (string-equal regexp "")
6970       (setq regexp (or gnus-last-search-regexp ""))
6971     (setq gnus-last-search-regexp regexp))
6972   (if (gnus-summary-search-article regexp backward)
6973       (gnus-summary-show-thread)
6974     (error "Search failed: \"%s\"" regexp)))
6975
6976 (defun gnus-summary-search-article-backward (regexp)
6977   "Search for an article containing REGEXP backward."
6978   (interactive
6979    (list (read-string
6980           (format "Search article backward (regexp%s): "
6981                   (if gnus-last-search-regexp
6982                       (concat ", default " gnus-last-search-regexp)
6983                     "")))))
6984   (gnus-summary-search-article-forward regexp 'backward))
6985
6986 (eval-when-compile
6987   (defmacro gnus-summary-search-article-position-point (regexp backward)
6988     "Dehighlight the last matched text and goto the beginning position."
6989     (` (if (and gnus-summary-search-article-matched-data
6990                 (let ((text (caddr gnus-summary-search-article-matched-data))
6991                       (inhibit-read-only t)
6992                       buffer-read-only)
6993                   (delete-region
6994                    (goto-char (car gnus-summary-search-article-matched-data))
6995                    (cadr gnus-summary-search-article-matched-data))
6996                   (insert text)
6997                   (string-match (, regexp) text)))
6998            (if (, backward) (beginning-of-line) (end-of-line))
6999          (goto-char (if (, backward) (point-max) (point-min))))))
7000
7001   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7002     "Place point where X-Face image is displayed."
7003     (if (featurep 'xemacs)
7004         (` (let ((end (if (search-forward "\n\n" nil t)
7005                           (goto-char (1- (point)))
7006                         (point-min)))
7007                  extent)
7008              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7009              (unless (and (re-search-forward "^From:" end t)
7010                           (setq extent (extent-at (point)))
7011                           (extent-begin-glyph extent))
7012                (goto-char (, opoint)))))
7013       (` (let ((end (if (search-forward "\n\n" nil t)
7014                         (goto-char (1- (point)))
7015                       (point-min))))
7016            (goto-char
7017             (or (text-property-any (or (search-backward "\n\n" nil t)
7018                                        (point-min))
7019                                    end 'x-face-mule-bitmap-image t)
7020                 (, opoint)))))))
7021
7022   (defmacro gnus-summary-search-article-highlight-matched-text
7023     (backward treated x-face)
7024     "Highlight matched text in the function `gnus-summary-search-article'."
7025     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7026              (end (set-marker (make-marker) (match-end 0)))
7027              (inhibit-read-only t)
7028              buffer-read-only)
7029          (unless treated
7030            (let ((,@
7031                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7032                     (mapcar
7033                      (lambda (item) (setq items (delq item items)))
7034                      '(gnus-treat-buttonize
7035                        gnus-treat-fill-article
7036                        gnus-treat-fill-long-lines
7037                        gnus-treat-emphasize
7038                        gnus-treat-highlight-headers
7039                        gnus-treat-highlight-citation
7040                        gnus-treat-highlight-signature
7041                        gnus-treat-overstrike
7042                        gnus-treat-display-xface
7043                        gnus-treat-buttonize-head
7044                        gnus-treat-decode-article-as-default-mime-charset))
7045                     (static-if (featurep 'xemacs)
7046                         items
7047                       (cons '(x-face-mule-delete-x-face-field
7048                               (quote never))
7049                             items))))
7050                  (gnus-treat-display-xface
7051                   (when (, x-face) gnus-treat-display-xface)))
7052              (gnus-article-prepare-mime-display)))
7053          (goto-char (if (, backward) start end))
7054          (when (, x-face)
7055            (gnus-summary-search-article-highlight-goto-x-face (point)))
7056          (setq gnus-summary-search-article-matched-data
7057                (list start end (buffer-substring start end)))
7058          (unless (eq start end);; matched text has been deleted. :-<
7059            (put-text-property start end 'face
7060                               (or (find-face 'isearch)
7061                                   'secondary-selection))))))
7062   )
7063
7064 (defun gnus-summary-search-article (regexp &optional backward)
7065   "Search for an article containing REGEXP.
7066 Optional argument BACKWARD means do search for backward.
7067 `gnus-select-article-hook' is not called during the search."
7068   ;; We have to require this here to make sure that the following
7069   ;; dynamic binding isn't shadowed by autoloading.
7070   (require 'gnus-async)
7071   (require 'gnus-art)
7072   (let ((gnus-select-article-hook nil)  ;Disable hook.
7073         (gnus-article-display-hook nil)
7074         (gnus-article-prepare-hook nil)
7075         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7076         (gnus-use-article-prefetch nil)
7077         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7078         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7079         (sum (current-buffer))
7080         (found nil)
7081         point treated)
7082     (gnus-save-hidden-threads
7083       (static-if (featurep 'xemacs)
7084           (let ((gnus-inhibit-treatment t))
7085             (setq treated (eq 'old (gnus-summary-select-article)))
7086             (when (and treated
7087                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7088                                  (window-live-p (get-buffer-window
7089                                                  gnus-article-buffer t)))))
7090               (gnus-summary-select-article nil t)
7091               (setq treated nil)))
7092         (let ((gnus-inhibit-treatment t)
7093               (x-face-mule-delete-x-face-field 'never))
7094           (setq treated (eq 'old (gnus-summary-select-article)))
7095           (when (and treated
7096                      (not
7097                       (and (gnus-buffer-live-p gnus-article-buffer)
7098                            (window-live-p (get-buffer-window
7099                                            gnus-article-buffer t))
7100                            (or (not (string-match "^\\^X-Face:" regexp))
7101                                (with-current-buffer gnus-article-buffer
7102                                  gnus-summary-search-article-matched-data)))))
7103             (gnus-summary-select-article nil t)
7104             (setq treated nil))))
7105       (set-buffer gnus-article-buffer)
7106       (widen)
7107       (if treated
7108           (progn
7109             (gnus-article-show-all-headers)
7110             (gnus-summary-search-article-position-point regexp backward))
7111         (goto-char (if backward (point-max) (point-min))))
7112       (while (not found)
7113         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7114         (if (if backward
7115                 (re-search-backward regexp nil t)
7116               (re-search-forward regexp nil t))
7117             ;; We found the regexp.
7118             (progn
7119               (gnus-summary-search-article-highlight-matched-text
7120                backward treated (string-match "^\\^X-Face:" regexp))
7121               (setq found 'found)
7122               (forward-line
7123                (/ (- 2 (window-height
7124                         (get-buffer-window gnus-article-buffer t)))
7125                   2))
7126               (set-window-start
7127                (get-buffer-window (current-buffer))
7128                (point))
7129               (set-buffer sum)
7130               (setq point (point)))
7131           ;; We didn't find it, so we go to the next article.
7132           (set-buffer sum)
7133           (setq found 'not)
7134           (while (eq found 'not)
7135             (if (not (if backward (gnus-summary-find-prev)
7136                        (gnus-summary-find-next)))
7137                 ;; No more articles.
7138                 (setq found t)
7139               ;; Select the next article and adjust point.
7140               (unless (gnus-summary-article-sparse-p
7141                        (gnus-summary-article-number))
7142                 (setq found nil)
7143                 (let ((gnus-inhibit-treatment t))
7144                   (gnus-summary-select-article))
7145                 (setq treated nil)
7146                 (set-buffer gnus-article-buffer)
7147                 (widen)
7148                 (goto-char (if backward (point-max) (point-min))))))))
7149       (gnus-message 7 ""))
7150     ;; Return whether we found the regexp.
7151     (when (eq found 'found)
7152       (goto-char point)
7153       (gnus-summary-show-thread)
7154       (gnus-summary-goto-subject gnus-current-article)
7155       (gnus-summary-position-point)
7156       t)))
7157
7158 (defun gnus-summary-find-matching (header regexp &optional backward unread
7159                                           not-case-fold)
7160   "Return a list of all articles that match REGEXP on HEADER.
7161 The search stars on the current article and goes forwards unless
7162 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7163 If UNREAD is non-nil, only unread articles will
7164 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7165 in the comparisons."
7166   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7167                 (gnus-data-find-list
7168                  (gnus-summary-article-number) (gnus-data-list backward))))
7169         (case-fold-search (not not-case-fold))
7170         articles d func)
7171     (if (consp header)
7172         (if (eq (car header) 'extra)
7173             (setq func
7174                   `(lambda (h)
7175                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7176                          "")))
7177           (error "%s is an invalid header" header))
7178       (unless (fboundp (intern (concat "mail-header-" header)))
7179         (error "%s is not a valid header" header))
7180       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7181     (while data
7182       (setq d (car data))
7183       (and (or (not unread)             ; We want all articles...
7184                (gnus-data-unread-p d))  ; Or just unreads.
7185            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7186            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7187            (push (gnus-data-number d) articles)) ; Success!
7188       (setq data (cdr data)))
7189     (nreverse articles)))
7190
7191 (defun gnus-summary-execute-command (header regexp command &optional backward)
7192   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7193 If HEADER is an empty string (or nil), the match is done on the entire
7194 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7195   (interactive
7196    (list (let ((completion-ignore-case t))
7197            (completing-read
7198             "Header name: "
7199             (mapcar (lambda (string) (list string))
7200                     '("Number" "Subject" "From" "Lines" "Date"
7201                       "Message-ID" "Xref" "References" "Body"))
7202             nil 'require-match))
7203          (read-string "Regexp: ")
7204          (read-key-sequence "Command: ")
7205          current-prefix-arg))
7206   (when (equal header "Body")
7207     (setq header ""))
7208   ;; Hidden thread subtrees must be searched as well.
7209   (gnus-summary-show-all-threads)
7210   ;; We don't want to change current point nor window configuration.
7211   (save-excursion
7212     (save-window-excursion
7213       (gnus-message 6 "Executing %s..." (key-description command))
7214       ;; We'd like to execute COMMAND interactively so as to give arguments.
7215       (gnus-execute header regexp
7216                     `(call-interactively ',(key-binding command))
7217                     backward)
7218       (gnus-message 6 "Executing %s...done" (key-description command)))))
7219
7220 (defun gnus-summary-beginning-of-article ()
7221   "Scroll the article back to the beginning."
7222   (interactive)
7223   (gnus-summary-select-article)
7224   (gnus-configure-windows 'article)
7225   (gnus-eval-in-buffer-window gnus-article-buffer
7226     (widen)
7227     (goto-char (point-min))
7228     (when gnus-page-broken
7229       (gnus-narrow-to-page))))
7230
7231 (defun gnus-summary-end-of-article ()
7232   "Scroll to the end of the article."
7233   (interactive)
7234   (gnus-summary-select-article)
7235   (gnus-configure-windows 'article)
7236   (gnus-eval-in-buffer-window gnus-article-buffer
7237     (widen)
7238     (goto-char (point-max))
7239     (recenter -3)
7240     (when gnus-page-broken
7241       (gnus-narrow-to-page))))
7242
7243 (defun gnus-summary-print-article (&optional filename n)
7244   "Generate and print a PostScript image of the N next (mail) articles.
7245
7246 If N is negative, print the N previous articles.  If N is nil and articles
7247 have been marked with the process mark, print these instead.
7248
7249 If the optional first argument FILENAME is nil, send the image to the
7250 printer.  If FILENAME is a string, save the PostScript image in a file with
7251 that name.  If FILENAME is a number, prompt the user for the name of the file
7252 to save in."
7253   (interactive (list (ps-print-preprint current-prefix-arg)
7254                      current-prefix-arg))
7255   (dolist (article (gnus-summary-work-articles n))
7256     (gnus-summary-select-article nil nil 'pseudo article)
7257     (gnus-eval-in-buffer-window gnus-article-buffer
7258       (let ((buffer (generate-new-buffer " *print*")))
7259         (unwind-protect
7260             (progn
7261               (copy-to-buffer buffer (point-min) (point-max))
7262               (set-buffer buffer)
7263               (gnus-article-delete-invisible-text)
7264               (let ((ps-left-header
7265                      (list
7266                       (concat "("
7267                               (mail-header-subject gnus-current-headers) ")")
7268                       (concat "("
7269                               (mail-header-from gnus-current-headers) ")")))
7270                     (ps-right-header
7271                      (list
7272                       "/pagenumberstring load"
7273                       (concat "("
7274                               (mail-header-date gnus-current-headers) ")"))))
7275                 (gnus-run-hooks 'gnus-ps-print-hook)
7276                 (save-excursion
7277                   (ps-print-buffer-with-faces filename))))
7278           (kill-buffer buffer))))))
7279
7280 (defun gnus-summary-show-article (&optional arg)
7281   "Force re-fetching of the current article.
7282 If ARG (the prefix) is a number, show the article with the charset 
7283 defined in `gnus-summary-show-article-charset-alist', or the charset
7284 inputed.
7285 If ARG (the prefix) is non-nil and not a number, show the raw article 
7286 without any article massaging functions being run."
7287   (interactive "P")
7288   (cond 
7289    ((numberp arg)
7290     (let ((gnus-newsgroup-charset 
7291            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7292                (read-coding-system "Charset: ")))
7293           (gnus-newsgroup-ignored-charsets 'gnus-all))
7294       (gnus-summary-select-article nil 'force)))
7295    ((not arg)
7296     ;; Select the article the normal way.
7297     (gnus-summary-select-article nil 'force))
7298    (t
7299     ;; We have to require this here to make sure that the following
7300     ;; dynamic binding isn't shadowed by autoloading.
7301     (require 'gnus-async)
7302     (require 'gnus-art)
7303     ;; Bind the article treatment functions to nil.
7304     (let ((gnus-have-all-headers t)
7305           gnus-article-display-hook
7306           gnus-article-prepare-hook
7307           gnus-article-decode-hook
7308           gnus-break-pages
7309           gnus-show-mime)
7310       (gnus-summary-select-article nil 'force))))
7311   (gnus-summary-goto-subject gnus-current-article)
7312   (gnus-summary-position-point))
7313
7314 (defun gnus-summary-verbose-headers (&optional arg)
7315   "Toggle permanent full header display.
7316 If ARG is a positive number, turn header display on.
7317 If ARG is a negative number, turn header display off."
7318   (interactive "P")
7319   (setq gnus-show-all-headers
7320         (cond ((or (not (numberp arg))
7321                    (zerop arg))
7322                (not gnus-show-all-headers))
7323               ((natnump arg)
7324                t)))
7325   (gnus-summary-show-article))
7326
7327 (defun gnus-summary-toggle-header (&optional arg)
7328   "Show the headers if they are hidden, or hide them if they are shown.
7329 If ARG is a positive number, show the entire header.
7330 If ARG is a negative number, hide the unwanted header lines."
7331   (interactive "P")
7332   (save-excursion
7333     (set-buffer gnus-article-buffer)
7334     (save-restriction
7335       (let* ((buffer-read-only nil)
7336              (inhibit-point-motion-hooks t)
7337              hidden e)
7338         (setq hidden
7339               (if (numberp arg)
7340                   (>= arg 0)
7341                 (save-restriction 
7342                   (article-narrow-to-head)
7343                   (gnus-article-hidden-text-p 'headers))))
7344         (goto-char (point-min))
7345         (when (search-forward "\n\n" nil t)
7346           (delete-region (point-min) (1- (point))))
7347         (goto-char (point-min))
7348         (save-excursion
7349           (set-buffer gnus-original-article-buffer)
7350           (goto-char (point-min))
7351           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7352         (insert-buffer-substring gnus-original-article-buffer 1 e)
7353         (save-restriction
7354           (narrow-to-region (point-min) (point))
7355           (article-decode-encoded-words)
7356           (if  hidden
7357               (let ((gnus-treat-hide-headers nil)
7358                     (gnus-treat-hide-boring-headers nil))
7359                 (gnus-treat-article 'head))
7360             (gnus-treat-article 'head)))))))
7361
7362 (defun gnus-summary-show-all-headers ()
7363   "Make all header lines visible."
7364   (interactive)
7365   (gnus-article-show-all-headers))
7366
7367 (defun gnus-summary-toggle-mime (&optional arg)
7368   "Toggle MIME processing.
7369 If ARG is a positive number, turn MIME processing on."
7370   (interactive "P")
7371   (setq gnus-show-mime
7372         (if (null arg)
7373             (not gnus-show-mime)
7374           (> (prefix-numeric-value arg) 0)))
7375   (gnus-summary-select-article t 'force))
7376
7377 (defun gnus-summary-caesar-message (&optional arg)
7378   "Caesar rotate the current article by 13.
7379 The numerical prefix specifies how many places to rotate each letter
7380 forward."
7381   (interactive "P")
7382   (gnus-summary-select-article)
7383   (let ((mail-header-separator ""))
7384     (gnus-eval-in-buffer-window gnus-article-buffer
7385       (save-restriction
7386         (widen)
7387         (let ((start (window-start))
7388               buffer-read-only)
7389           (message-caesar-buffer-body arg)
7390           (set-window-start (get-buffer-window (current-buffer)) start))))))
7391
7392 (defun gnus-summary-stop-page-breaking ()
7393   "Stop page breaking in the current article."
7394   (interactive)
7395   (gnus-summary-select-article)
7396   (gnus-eval-in-buffer-window gnus-article-buffer
7397     (widen)
7398     (when (gnus-visual-p 'page-marker)
7399       (let ((buffer-read-only nil))
7400         (gnus-remove-text-with-property 'gnus-prev)
7401         (gnus-remove-text-with-property 'gnus-next))
7402       (setq gnus-page-broken nil))))
7403
7404 (defun gnus-summary-move-article (&optional n to-newsgroup
7405                                             select-method action)
7406   "Move the current article to a different newsgroup.
7407 If N is a positive number, move the N next articles.
7408 If N is a negative number, move the N previous articles.
7409 If N is nil and any articles have been marked with the process mark,
7410 move those articles instead.
7411 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7412 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7413 re-spool using this method.
7414
7415 For this function to work, both the current newsgroup and the
7416 newsgroup that you want to move to have to support the `request-move'
7417 and `request-accept' functions."
7418   (interactive "P")
7419   (unless action
7420     (setq action 'move))
7421   ;; Disable marking as read.
7422   (let (gnus-mark-article-hook)
7423     (save-window-excursion
7424       (gnus-summary-select-article)))
7425   ;; Check whether the source group supports the required functions.
7426   (cond ((and (eq action 'move)
7427               (not (gnus-check-backend-function
7428                     'request-move-article gnus-newsgroup-name)))
7429          (error "The current group does not support article moving"))
7430         ((and (eq action 'crosspost)
7431               (not (gnus-check-backend-function
7432                     'request-replace-article gnus-newsgroup-name)))
7433          (error "The current group does not support article editing")))
7434   (let ((articles (gnus-summary-work-articles n))
7435         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7436         (names '((move "Move" "Moving")
7437                  (copy "Copy" "Copying")
7438                  (crosspost "Crosspost" "Crossposting")))
7439         (copy-buf (save-excursion
7440                     (nnheader-set-temp-buffer " *copy article*")))
7441         (default-marks gnus-article-mark-lists)
7442         (no-expire-marks (delete '(expirable . expire)
7443                                  (copy-sequence gnus-article-mark-lists)))
7444         art-group to-method new-xref article to-groups)
7445     (unless (assq action names)
7446       (error "Unknown action %s" action))
7447     ;; Read the newsgroup name.
7448     (when (and (not to-newsgroup)
7449                (not select-method))
7450       (setq to-newsgroup
7451             (gnus-read-move-group-name
7452              (cadr (assq action names))
7453              (symbol-value (intern (format "gnus-current-%s-group" action)))
7454              articles prefix))
7455       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7456     (setq to-method (or select-method
7457                         (gnus-group-name-to-method to-newsgroup)))
7458     ;; Check the method we are to move this article to...
7459     (unless (gnus-check-backend-function
7460              'request-accept-article (car to-method))
7461       (error "%s does not support article copying" (car to-method)))
7462     (unless (gnus-check-server to-method)
7463       (error "Can't open server %s" (car to-method)))
7464     (gnus-message 6 "%s to %s: %s..."
7465                   (caddr (assq action names))
7466                   (or (car select-method) to-newsgroup) articles)
7467     (while articles
7468       (setq article (pop articles))
7469       (setq
7470        art-group
7471        (cond
7472         ;; Move the article.
7473         ((eq action 'move)
7474          ;; Remove this article from future suppression.
7475          (gnus-dup-unsuppress-article article)
7476          (gnus-request-move-article
7477           article                       ; Article to move
7478           gnus-newsgroup-name           ; From newsgroup
7479           (nth 1 (gnus-find-method-for-group
7480                   gnus-newsgroup-name)) ; Server
7481           (list 'gnus-request-accept-article
7482                 to-newsgroup (list 'quote select-method)
7483                 (not articles) t)               ; Accept form
7484           (not articles)))              ; Only save nov last time
7485         ;; Copy the article.
7486         ((eq action 'copy)
7487          (save-excursion
7488            (set-buffer copy-buf)
7489            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7490              (gnus-request-accept-article
7491               to-newsgroup select-method (not articles) t))))
7492         ;; Crosspost the article.
7493         ((eq action 'crosspost)
7494          (let ((xref (message-tokenize-header
7495                       (mail-header-xref (gnus-summary-article-header article))
7496                       " ")))
7497            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7498                                   ":" article))
7499            (unless xref
7500              (setq xref (list (system-name))))
7501            (setq new-xref
7502                  (concat
7503                   (mapconcat 'identity
7504                              (delete "Xref:" (delete new-xref xref))
7505                              " ")
7506                   " " new-xref))
7507            (save-excursion
7508              (set-buffer copy-buf)
7509              ;; First put the article in the destination group.
7510              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7511              (when (consp (setq art-group
7512                                 (gnus-request-accept-article
7513                                  to-newsgroup select-method (not articles))))
7514                (setq new-xref (concat new-xref " " (car art-group)
7515                                       ":" (cdr art-group)))
7516                ;; Now we have the new Xrefs header, so we insert
7517                ;; it and replace the new article.
7518                (nnheader-replace-header "Xref" new-xref)
7519                (gnus-request-replace-article
7520                 (cdr art-group) to-newsgroup (current-buffer))
7521                art-group))))))
7522       (cond
7523        ((not art-group)
7524        (gnus-message 1 "Couldn't %s article %s: %s"
7525                      (cadr (assq action names)) article
7526                      (nnheader-get-report (car to-method))))
7527        ((and (eq art-group 'junk)
7528              (eq action 'move))
7529         (gnus-summary-mark-article article gnus-canceled-mark)
7530         (gnus-message 4 "Deleted article %s" article))
7531        (t
7532         (let* ((pto-group (gnus-group-prefixed-name
7533                            (car art-group) to-method))
7534                (entry
7535                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7536                (info (nth 2 entry))
7537                (to-group (gnus-info-group info))
7538                to-marks)
7539           ;; Update the group that has been moved to.
7540           (when (and info
7541                      (memq action '(move copy)))
7542             (unless (member to-group to-groups)
7543               (push to-group to-groups))
7544
7545             (unless (memq article gnus-newsgroup-unreads)
7546               (push 'read to-marks)
7547               (gnus-info-set-read
7548                info (gnus-add-to-range (gnus-info-read info)
7549                                        (list (cdr art-group)))))
7550
7551             ;; Copy any marks over to the new group.
7552             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7553                              default-marks
7554                            no-expire-marks))
7555                   (to-article (cdr art-group)))
7556
7557               ;; See whether the article is to be put in the cache.
7558               (when gnus-use-cache
7559                 (gnus-cache-possibly-enter-article
7560                  to-group to-article
7561                  (let ((header (copy-sequence
7562                                 (gnus-summary-article-header article))))
7563                    (mail-header-set-number header to-article)
7564                    header)
7565                  (memq article gnus-newsgroup-marked)
7566                  (memq article gnus-newsgroup-dormant)
7567                  (memq article gnus-newsgroup-unreads)))
7568
7569               (when (and (equal to-group gnus-newsgroup-name)
7570                          (not (memq article gnus-newsgroup-unreads)))
7571                 ;; Mark this article as read in this group.
7572                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7573                 (setcdr (gnus-active to-group) to-article)
7574                 (setcdr gnus-newsgroup-active to-article))
7575
7576               (while marks
7577                 (when (memq article (symbol-value
7578                                      (intern (format "gnus-newsgroup-%s"
7579                                                      (caar marks)))))
7580                   (push (cdar marks) to-marks)
7581                   ;; If the other group is the same as this group,
7582                   ;; then we have to add the mark to the list.
7583                   (when (equal to-group gnus-newsgroup-name)
7584                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7585                          (cons to-article
7586                                (symbol-value
7587                                 (intern (format "gnus-newsgroup-%s"
7588                                                 (caar marks)))))))
7589                   ;; Copy the marks to other group.
7590                   (gnus-add-marked-articles
7591                    to-group (cdar marks) (list to-article) info))
7592                 (setq marks (cdr marks)))
7593
7594               (gnus-request-set-mark to-group (list (list (list to-article)
7595                                                           'set
7596                                                           to-marks)))
7597
7598               (gnus-dribble-enter
7599                (concat "(gnus-group-set-info '"
7600                        (gnus-prin1-to-string (gnus-get-info to-group))
7601                        ")"))))
7602
7603           ;; Update the Xref header in this article to point to
7604           ;; the new crossposted article we have just created.
7605           (when (eq action 'crosspost)
7606             (save-excursion
7607               (set-buffer copy-buf)
7608               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7609               (nnheader-replace-header "Xref" new-xref)
7610               (gnus-request-replace-article
7611                article gnus-newsgroup-name (current-buffer)))))
7612
7613         ;;;!!!Why is this necessary?
7614         (set-buffer gnus-summary-buffer)
7615
7616         (gnus-summary-goto-subject article)
7617         (when (eq action 'move)
7618           (gnus-summary-mark-article article gnus-canceled-mark))))
7619       (gnus-summary-remove-process-mark article))
7620     ;; Re-activate all groups that have been moved to.
7621     (while to-groups
7622       (save-excursion
7623         (set-buffer gnus-group-buffer)
7624         (when (gnus-group-goto-group (car to-groups) t)
7625           (gnus-group-get-new-news-this-group 1 t))
7626         (pop to-groups)))
7627
7628     (gnus-kill-buffer copy-buf)
7629     (gnus-summary-position-point)
7630     (gnus-set-mode-line 'summary)))
7631
7632 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7633   "Move the current article to a different newsgroup.
7634 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7635 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7636 re-spool using this method."
7637   (interactive "P")
7638   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7639
7640 (defun gnus-summary-crosspost-article (&optional n)
7641   "Crosspost the current article to some other group."
7642   (interactive "P")
7643   (gnus-summary-move-article n nil nil 'crosspost))
7644
7645 (defcustom gnus-summary-respool-default-method nil
7646   "Default method for respooling an article.
7647 If nil, use to the current newsgroup method."
7648   :type '(choice (gnus-select-method :value (nnml ""))
7649                  (const nil))
7650   :group 'gnus-summary-mail)
7651
7652 (defun gnus-summary-respool-article (&optional n method)
7653   "Respool the current article.
7654 The article will be squeezed through the mail spooling process again,
7655 which means that it will be put in some mail newsgroup or other
7656 depending on `nnmail-split-methods'.
7657 If N is a positive number, respool the N next articles.
7658 If N is a negative number, respool the N previous articles.
7659 If N is nil and any articles have been marked with the process mark,
7660 respool those articles instead.
7661
7662 Respooling can be done both from mail groups and \"real\" newsgroups.
7663 In the former case, the articles in question will be moved from the
7664 current group into whatever groups they are destined to.  In the
7665 latter case, they will be copied into the relevant groups."
7666   (interactive
7667    (list current-prefix-arg
7668          (let* ((methods (gnus-methods-using 'respool))
7669                 (methname
7670                  (symbol-name (or gnus-summary-respool-default-method
7671                                   (car (gnus-find-method-for-group
7672                                         gnus-newsgroup-name)))))
7673                 (method
7674                  (gnus-completing-read
7675                   methname "What backend do you want to use when respooling?"
7676                   methods nil t nil 'gnus-mail-method-history))
7677                 ms)
7678            (cond
7679             ((zerop (length (setq ms (gnus-servers-using-backend
7680                                       (intern method)))))
7681              (list (intern method) ""))
7682             ((= 1 (length ms))
7683              (car ms))
7684             (t
7685              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7686                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7687                            ms-alist))))))))
7688   (unless method
7689     (error "No method given for respooling"))
7690   (if (assoc (symbol-name
7691               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7692              (gnus-methods-using 'respool))
7693       (gnus-summary-move-article n nil method)
7694     (gnus-summary-copy-article n nil method)))
7695
7696 (defun gnus-summary-import-article (file)
7697   "Import an arbitrary file into a mail newsgroup."
7698   (interactive "fImport file: ")
7699   (let ((group gnus-newsgroup-name)
7700         (now (current-time))
7701         atts lines)
7702     (unless (gnus-check-backend-function 'request-accept-article group)
7703       (error "%s does not support article importing" group))
7704     (or (file-readable-p file)
7705         (not (file-regular-p file))
7706         (error "Can't read %s" file))
7707     (save-excursion
7708       (set-buffer (gnus-get-buffer-create " *import file*"))
7709       (erase-buffer)
7710       (nnheader-insert-file-contents file)
7711       (goto-char (point-min))
7712       (unless (nnheader-article-p)
7713         ;; This doesn't look like an article, so we fudge some headers.
7714         (setq atts (file-attributes file)
7715               lines (count-lines (point-min) (point-max)))
7716         (insert "From: " (read-string "From: ") "\n"
7717                 "Subject: " (read-string "Subject: ") "\n"
7718                 "Date: " (message-make-date (nth 5 atts))
7719                 "\n"
7720                 "Message-ID: " (message-make-message-id) "\n"
7721                 "Lines: " (int-to-string lines) "\n"
7722                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7723       (gnus-request-accept-article group nil t)
7724       (kill-buffer (current-buffer)))))
7725
7726 (defun gnus-summary-article-posted-p ()
7727   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7728 This will be the case if the article has both been mailed and posted."
7729   (interactive)
7730   (let ((id (mail-header-references (gnus-summary-article-header)))
7731         (gnus-override-method
7732          (or gnus-refer-article-method gnus-select-method)))
7733     (if (gnus-request-head id "")
7734         (gnus-message 2 "The current message was found on %s"
7735                       gnus-override-method)
7736       (gnus-message 2 "The current message couldn't be found on %s"
7737                     gnus-override-method)
7738       nil)))
7739
7740 (defun gnus-summary-expire-articles (&optional now)
7741   "Expire all articles that are marked as expirable in the current group."
7742   (interactive)
7743   (when (gnus-check-backend-function
7744          'request-expire-articles gnus-newsgroup-name)
7745     ;; This backend supports expiry.
7746     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7747            (expirable (if total
7748                           (progn
7749                             ;; We need to update the info for
7750                             ;; this group for `gnus-list-of-read-articles'
7751                             ;; to give us the right answer.
7752                             (gnus-run-hooks 'gnus-exit-group-hook)
7753                             (gnus-summary-update-info)
7754                             (gnus-list-of-read-articles gnus-newsgroup-name))
7755                         (setq gnus-newsgroup-expirable
7756                               (sort gnus-newsgroup-expirable '<))))
7757            (expiry-wait (if now 'immediate
7758                           (gnus-group-find-parameter
7759                            gnus-newsgroup-name 'expiry-wait)))
7760            es)
7761       (when expirable
7762         ;; There are expirable articles in this group, so we run them
7763         ;; through the expiry process.
7764         (gnus-message 6 "Expiring articles...")
7765         (unless (gnus-check-group gnus-newsgroup-name)
7766           (error "Can't open server for %s" gnus-newsgroup-name))
7767         ;; The list of articles that weren't expired is returned.
7768         (save-excursion
7769           (if expiry-wait
7770               (let ((nnmail-expiry-wait-function nil)
7771                     (nnmail-expiry-wait expiry-wait))
7772                 (setq es (gnus-request-expire-articles
7773                           expirable gnus-newsgroup-name)))
7774             (setq es (gnus-request-expire-articles
7775                       expirable gnus-newsgroup-name))))
7776         (unless total
7777           (setq gnus-newsgroup-expirable es))
7778         ;; We go through the old list of expirable, and mark all
7779         ;; really expired articles as nonexistent.
7780         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7781           (let ((gnus-use-cache nil))
7782             (while expirable
7783               (unless (memq (car expirable) es)
7784                 (when (gnus-data-find (car expirable))
7785                   (gnus-summary-mark-article
7786                    (car expirable) gnus-canceled-mark)))
7787               (setq expirable (cdr expirable)))))
7788         (gnus-message 6 "Expiring articles...done")))))
7789
7790 (defun gnus-summary-expire-articles-now ()
7791   "Expunge all expirable articles in the current group.
7792 This means that *all* articles that are marked as expirable will be
7793 deleted forever, right now."
7794   (interactive)
7795   (or gnus-expert-user
7796       (gnus-yes-or-no-p
7797        "Are you really, really, really sure you want to delete all these messages? ")
7798       (error "Phew!"))
7799   (gnus-summary-expire-articles t))
7800
7801 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7802 (defun gnus-summary-delete-article (&optional n)
7803   "Delete the N next (mail) articles.
7804 This command actually deletes articles.  This is not a marking
7805 command.  The article will disappear forever from your life, never to
7806 return.
7807 If N is negative, delete backwards.
7808 If N is nil and articles have been marked with the process mark,
7809 delete these instead."
7810   (interactive "P")
7811   (unless (gnus-check-backend-function 'request-expire-articles
7812                                        gnus-newsgroup-name)
7813     (error "The current newsgroup does not support article deletion"))
7814   ;; Compute the list of articles to delete.
7815   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7816         not-deleted)
7817     (if (and gnus-novice-user
7818              (not (gnus-yes-or-no-p
7819                    (format "Do you really want to delete %s forever? "
7820                            (if (> (length articles) 1)
7821                                (format "these %s articles" (length articles))
7822                              "this article")))))
7823         ()
7824       ;; Delete the articles.
7825       (setq not-deleted (gnus-request-expire-articles
7826                          articles gnus-newsgroup-name 'force))
7827       (while articles
7828         (gnus-summary-remove-process-mark (car articles))
7829         ;; The backend might not have been able to delete the article
7830         ;; after all.
7831         (unless (memq (car articles) not-deleted)
7832           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7833         (setq articles (cdr articles)))
7834       (when not-deleted
7835         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7836     (gnus-summary-position-point)
7837     (gnus-set-mode-line 'summary)
7838     not-deleted))
7839
7840 (defun gnus-summary-edit-article (&optional force)
7841   "Edit the current article.
7842 This will have permanent effect only in mail groups.
7843 If FORCE is non-nil, allow editing of articles even in read-only
7844 groups."
7845   (interactive "P")
7846   (save-excursion
7847     (set-buffer gnus-summary-buffer)
7848     (let ((mail-parse-charset gnus-newsgroup-charset)
7849           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7850       (gnus-set-global-variables)
7851       (when (and (not force)
7852                  (gnus-group-read-only-p))
7853         (error "The current newsgroup does not support article editing"))
7854       (gnus-summary-show-article t)
7855       (gnus-article-edit-article
7856        'ignore
7857        `(lambda (no-highlight)
7858           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7859                 (mail-parse-ignored-charsets
7860                  ',gnus-newsgroup-ignored-charsets))
7861             (gnus-summary-edit-article-done
7862              ,(or (mail-header-references gnus-current-headers) "")
7863              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7864
7865 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7866
7867 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7868                                                  no-highlight)
7869   "Make edits to the current article permanent."
7870   (interactive)
7871   ;; Replace the article.
7872   (let ((buf (current-buffer)))
7873     (with-temp-buffer
7874       (insert-buffer-substring buf)
7875       (if (and (not read-only)
7876                (not (gnus-request-replace-article
7877                      (cdr gnus-article-current) (car gnus-article-current)
7878                      (current-buffer) t)))
7879           (error "Couldn't replace article")
7880         ;; Update the summary buffer.
7881         (if (and references
7882                  (equal (message-tokenize-header references " ")
7883                         (message-tokenize-header
7884                          (or (message-fetch-field "references") "") " ")))
7885             ;; We only have to update this line.
7886             (save-excursion
7887               (save-restriction
7888                 (message-narrow-to-head)
7889                 (let ((head (buffer-string))
7890                       header)
7891                   (with-temp-buffer
7892                     (insert (format "211 %d Article retrieved.\n"
7893                                     (cdr gnus-article-current)))
7894                     (insert head)
7895                     (insert ".\n")
7896                     (let ((nntp-server-buffer (current-buffer)))
7897                       (setq header (car (gnus-get-newsgroup-headers
7898                                          (save-excursion
7899                                            (set-buffer gnus-summary-buffer)
7900                                            gnus-newsgroup-dependencies)
7901                                          t))))
7902                     (save-excursion
7903                       (set-buffer gnus-summary-buffer)
7904                       (gnus-data-set-header
7905                        (gnus-data-find (cdr gnus-article-current))
7906                        header)
7907                       (gnus-summary-update-article-line
7908                        (cdr gnus-article-current) header))))))
7909           ;; Update threads.
7910           (set-buffer (or buffer gnus-summary-buffer))
7911           (gnus-summary-update-article (cdr gnus-article-current)))
7912         ;; Prettify the article buffer again.
7913         (unless no-highlight
7914           (save-excursion
7915             (set-buffer gnus-article-buffer)
7916             ;;;!!! Fix this -- article should be rehighlighted.
7917             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7918             (set-buffer gnus-original-article-buffer)
7919             (gnus-request-article
7920              (cdr gnus-article-current)
7921              (car gnus-article-current) (current-buffer))))
7922         ;; Prettify the summary buffer line.
7923         (when (gnus-visual-p 'summary-highlight 'highlight)
7924           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7925
7926 (defun gnus-summary-edit-wash (key)
7927   "Perform editing command KEY in the article buffer."
7928   (interactive
7929    (list
7930     (progn
7931       (message "%s" (concat (this-command-keys) "- "))
7932       (read-char))))
7933   (message "")
7934   (gnus-summary-edit-article)
7935   (execute-kbd-macro (concat (this-command-keys) key))
7936   (gnus-article-edit-done))
7937
7938 ;;; Respooling
7939
7940 (defun gnus-summary-respool-query (&optional silent trace)
7941   "Query where the respool algorithm would put this article."
7942   (interactive)
7943   (let (gnus-mark-article-hook)
7944     (gnus-summary-select-article)
7945     (save-excursion
7946       (set-buffer gnus-original-article-buffer)
7947       (save-restriction
7948         (message-narrow-to-head)
7949         (let ((groups (nnmail-article-group 'identity trace)))
7950           (unless silent
7951             (if groups
7952                 (message "This message would go to %s"
7953                          (mapconcat 'car groups ", "))
7954               (message "This message would go to no groups"))
7955             groups))))))
7956
7957 (defun gnus-summary-respool-trace ()
7958   "Trace where the respool algorithm would put this article.
7959 Display a buffer showing all fancy splitting patterns which matched."
7960   (interactive)
7961   (gnus-summary-respool-query nil t))
7962
7963 ;; Summary marking commands.
7964
7965 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7966   "Mark articles which has the same subject as read, and then select the next.
7967 If UNMARK is positive, remove any kind of mark.
7968 If UNMARK is negative, tick articles."
7969   (interactive "P")
7970   (when unmark
7971     (setq unmark (prefix-numeric-value unmark)))
7972   (let ((count
7973          (gnus-summary-mark-same-subject
7974           (gnus-summary-article-subject) unmark)))
7975     ;; Select next unread article.  If auto-select-same mode, should
7976     ;; select the first unread article.
7977     (gnus-summary-next-article t (and gnus-auto-select-same
7978                                       (gnus-summary-article-subject)))
7979     (gnus-message 7 "%d article%s marked as %s"
7980                   count (if (= count 1) " is" "s are")
7981                   (if unmark "unread" "read"))))
7982
7983 (defun gnus-summary-kill-same-subject (&optional unmark)
7984   "Mark articles which has the same subject as read.
7985 If UNMARK is positive, remove any kind of mark.
7986 If UNMARK is negative, tick articles."
7987   (interactive "P")
7988   (when unmark
7989     (setq unmark (prefix-numeric-value unmark)))
7990   (let ((count
7991          (gnus-summary-mark-same-subject
7992           (gnus-summary-article-subject) unmark)))
7993     ;; If marked as read, go to next unread subject.
7994     (when (null unmark)
7995       ;; Go to next unread subject.
7996       (gnus-summary-next-subject 1 t))
7997     (gnus-message 7 "%d articles are marked as %s"
7998                   count (if unmark "unread" "read"))))
7999
8000 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8001   "Mark articles with same SUBJECT as read, and return marked number.
8002 If optional argument UNMARK is positive, remove any kinds of marks.
8003 If optional argument UNMARK is negative, mark articles as unread instead."
8004   (let ((count 1))
8005     (save-excursion
8006       (cond
8007        ((null unmark)                   ; Mark as read.
8008         (while (and
8009                 (progn
8010                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8011                   (gnus-summary-show-thread) t)
8012                 (gnus-summary-find-subject subject))
8013           (setq count (1+ count))))
8014        ((> unmark 0)                    ; Tick.
8015         (while (and
8016                 (progn
8017                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8018                   (gnus-summary-show-thread) t)
8019                 (gnus-summary-find-subject subject))
8020           (setq count (1+ count))))
8021        (t                               ; Mark as unread.
8022         (while (and
8023                 (progn
8024                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8025                   (gnus-summary-show-thread) t)
8026                 (gnus-summary-find-subject subject))
8027           (setq count (1+ count)))))
8028       (gnus-set-mode-line 'summary)
8029       ;; Return the number of marked articles.
8030       count)))
8031
8032 (defun gnus-summary-mark-as-processable (n &optional unmark)
8033   "Set the process mark on the next N articles.
8034 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8035 the process mark instead.  The difference between N and the actual
8036 number of articles marked is returned."
8037   (interactive "p")
8038   (let ((backward (< n 0))
8039         (n (abs n)))
8040     (while (and
8041             (> n 0)
8042             (if unmark
8043                 (gnus-summary-remove-process-mark
8044                  (gnus-summary-article-number))
8045               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8046             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8047       (setq n (1- n)))
8048     (when (/= 0 n)
8049       (gnus-message 7 "No more articles"))
8050     (gnus-summary-recenter)
8051     (gnus-summary-position-point)
8052     n))
8053
8054 (defun gnus-summary-unmark-as-processable (n)
8055   "Remove the process mark from the next N articles.
8056 If N is negative, unmark backward instead.  The difference between N and
8057 the actual number of articles unmarked is returned."
8058   (interactive "p")
8059   (gnus-summary-mark-as-processable n t))
8060
8061 (defun gnus-summary-unmark-all-processable ()
8062   "Remove the process mark from all articles."
8063   (interactive)
8064   (save-excursion
8065     (while gnus-newsgroup-processable
8066       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8067   (gnus-summary-position-point))
8068
8069 (defun gnus-summary-mark-as-expirable (n)
8070   "Mark N articles forward as expirable.
8071 If N is negative, mark backward instead.  The difference between N and
8072 the actual number of articles marked is returned."
8073   (interactive "p")
8074   (gnus-summary-mark-forward n gnus-expirable-mark))
8075
8076 (defun gnus-summary-mark-article-as-replied (article)
8077   "Mark ARTICLE replied and update the summary line."
8078   (push article gnus-newsgroup-replied)
8079   (let ((buffer-read-only nil))
8080     (when (gnus-summary-goto-subject article nil t)
8081       (gnus-summary-update-secondary-mark article))))
8082
8083 (defun gnus-summary-set-bookmark (article)
8084   "Set a bookmark in current article."
8085   (interactive (list (gnus-summary-article-number)))
8086   (when (or (not (get-buffer gnus-article-buffer))
8087             (not gnus-current-article)
8088             (not gnus-article-current)
8089             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8090     (error "No current article selected"))
8091   ;; Remove old bookmark, if one exists.
8092   (let ((old (assq article gnus-newsgroup-bookmarks)))
8093     (when old
8094       (setq gnus-newsgroup-bookmarks
8095             (delq old gnus-newsgroup-bookmarks))))
8096   ;; Set the new bookmark, which is on the form
8097   ;; (article-number . line-number-in-body).
8098   (push
8099    (cons article
8100          (save-excursion
8101            (set-buffer gnus-article-buffer)
8102            (count-lines
8103             (min (point)
8104                  (save-excursion
8105                    (goto-char (point-min))
8106                    (search-forward "\n\n" nil t)
8107                    (point)))
8108             (point))))
8109    gnus-newsgroup-bookmarks)
8110   (gnus-message 6 "A bookmark has been added to the current article."))
8111
8112 (defun gnus-summary-remove-bookmark (article)
8113   "Remove the bookmark from the current article."
8114   (interactive (list (gnus-summary-article-number)))
8115   ;; Remove old bookmark, if one exists.
8116   (let ((old (assq article gnus-newsgroup-bookmarks)))
8117     (if old
8118         (progn
8119           (setq gnus-newsgroup-bookmarks
8120                 (delq old gnus-newsgroup-bookmarks))
8121           (gnus-message 6 "Removed bookmark."))
8122       (gnus-message 6 "No bookmark in current article."))))
8123
8124 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8125 (defun gnus-summary-mark-as-dormant (n)
8126   "Mark N articles forward as dormant.
8127 If N is negative, mark backward instead.  The difference between N and
8128 the actual number of articles marked is returned."
8129   (interactive "p")
8130   (gnus-summary-mark-forward n gnus-dormant-mark))
8131
8132 (defun gnus-summary-set-process-mark (article)
8133   "Set the process mark on ARTICLE and update the summary line."
8134   (setq gnus-newsgroup-processable
8135         (cons article
8136               (delq article gnus-newsgroup-processable)))
8137   (when (gnus-summary-goto-subject article)
8138     (gnus-summary-show-thread)
8139     (gnus-summary-goto-subject article)
8140     (gnus-summary-update-secondary-mark article)))
8141
8142 (defun gnus-summary-remove-process-mark (article)
8143   "Remove the process mark from ARTICLE and update the summary line."
8144   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8145   (when (gnus-summary-goto-subject article)
8146     (gnus-summary-show-thread)
8147     (gnus-summary-goto-subject article)
8148     (gnus-summary-update-secondary-mark article)))
8149
8150 (defun gnus-summary-set-saved-mark (article)
8151   "Set the process mark on ARTICLE and update the summary line."
8152   (push article gnus-newsgroup-saved)
8153   (when (gnus-summary-goto-subject article)
8154     (gnus-summary-update-secondary-mark article)))
8155
8156 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8157   "Mark N articles as read forwards.
8158 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8159 The difference between N and the actual number of articles marked is
8160 returned."
8161   (interactive "p")
8162   (gnus-summary-show-thread)
8163   (let ((backward (< n 0))
8164         (gnus-summary-goto-unread
8165          (and gnus-summary-goto-unread
8166               (not (eq gnus-summary-goto-unread 'never))
8167               (not (memq mark (list gnus-unread-mark
8168                                     gnus-ticked-mark gnus-dormant-mark)))))
8169         (n (abs n))
8170         (mark (or mark gnus-del-mark)))
8171     (while (and (> n 0)
8172                 (gnus-summary-mark-article nil mark no-expire)
8173                 (zerop (gnus-summary-next-subject
8174                         (if backward -1 1)
8175                         (and gnus-summary-goto-unread
8176                              (not (eq gnus-summary-goto-unread 'never)))
8177                         t)))
8178       (setq n (1- n)))
8179     (when (/= 0 n)
8180       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8181     (gnus-summary-recenter)
8182     (gnus-summary-position-point)
8183     (gnus-set-mode-line 'summary)
8184     n))
8185
8186 (defun gnus-summary-mark-article-as-read (mark)
8187   "Mark the current article quickly as read with MARK."
8188   (let ((article (gnus-summary-article-number)))
8189     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8190     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8191     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8192     (push (cons article mark) gnus-newsgroup-reads)
8193     ;; Possibly remove from cache, if that is used.
8194     (when gnus-use-cache
8195       (gnus-cache-enter-remove-article article))
8196     ;; Allow the backend to change the mark.
8197     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8198     ;; Check for auto-expiry.
8199     (when (and gnus-newsgroup-auto-expire
8200                (memq mark gnus-auto-expirable-marks))
8201       (setq mark gnus-expirable-mark)
8202       ;; Let the backend know about the mark change.
8203       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8204       (push article gnus-newsgroup-expirable))
8205     ;; Set the mark in the buffer.
8206     (gnus-summary-update-mark mark 'unread)
8207     t))
8208
8209 (defun gnus-summary-mark-article-as-unread (mark)
8210   "Mark the current article quickly as unread with MARK."
8211   (let* ((article (gnus-summary-article-number))
8212          (old-mark (gnus-summary-article-mark article)))
8213     ;; Allow the backend to change the mark.
8214     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8215     (if (eq mark old-mark)
8216         t
8217       (if (<= article 0)
8218           (progn
8219             (gnus-error 1 "Can't mark negative article numbers")
8220             nil)
8221         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8222         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8223         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8224         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8225         (cond ((= mark gnus-ticked-mark)
8226                (push article gnus-newsgroup-marked))
8227               ((= mark gnus-dormant-mark)
8228                (push article gnus-newsgroup-dormant))
8229               (t
8230                (push article gnus-newsgroup-unreads)))
8231         (gnus-pull article gnus-newsgroup-reads)
8232
8233         ;; See whether the article is to be put in the cache.
8234         (and gnus-use-cache
8235              (vectorp (gnus-summary-article-header article))
8236              (save-excursion
8237                (gnus-cache-possibly-enter-article
8238                 gnus-newsgroup-name article
8239                 (gnus-summary-article-header article)
8240                 (= mark gnus-ticked-mark)
8241                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8242
8243         ;; Fix the mark.
8244         (gnus-summary-update-mark mark 'unread)
8245         t))))
8246
8247 (defun gnus-summary-mark-article (&optional article mark no-expire)
8248   "Mark ARTICLE with MARK.  MARK can be any character.
8249 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8250 `??' (dormant) and `?E' (expirable).
8251 If MARK is nil, then the default character `?r' is used.
8252 If ARTICLE is nil, then the article on the current line will be
8253 marked."
8254   ;; The mark might be a string.
8255   (when (stringp mark)
8256     (setq mark (aref mark 0)))
8257   ;; If no mark is given, then we check auto-expiring.
8258   (when (null mark)
8259     (setq mark gnus-del-mark))
8260   (when (and (not no-expire)
8261              gnus-newsgroup-auto-expire
8262              (memq mark gnus-auto-expirable-marks))
8263     (setq mark gnus-expirable-mark))
8264   (let ((article (or article (gnus-summary-article-number)))
8265         (old-mark (gnus-summary-article-mark article)))
8266     ;; Allow the backend to change the mark.
8267     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8268     (if (eq mark old-mark)
8269         t
8270       (unless article
8271         (error "No article on current line"))
8272       (if (not (if (or (= mark gnus-unread-mark)
8273                        (= mark gnus-ticked-mark)
8274                        (= mark gnus-dormant-mark))
8275                    (gnus-mark-article-as-unread article mark)
8276                  (gnus-mark-article-as-read article mark)))
8277           t
8278         ;; See whether the article is to be put in the cache.
8279         (and gnus-use-cache
8280              (not (= mark gnus-canceled-mark))
8281              (vectorp (gnus-summary-article-header article))
8282              (save-excursion
8283                (gnus-cache-possibly-enter-article
8284                 gnus-newsgroup-name article
8285                 (gnus-summary-article-header article)
8286                 (= mark gnus-ticked-mark)
8287                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8288
8289         (when (gnus-summary-goto-subject article nil t)
8290           (let ((buffer-read-only nil))
8291             (gnus-summary-show-thread)
8292             ;; Fix the mark.
8293             (gnus-summary-update-mark mark 'unread)
8294             t))))))
8295
8296 (defun gnus-summary-update-secondary-mark (article)
8297   "Update the secondary (read, process, cache) mark."
8298   (gnus-summary-update-mark
8299    (cond ((memq article gnus-newsgroup-processable)
8300           gnus-process-mark)
8301          ((memq article gnus-newsgroup-cached)
8302           gnus-cached-mark)
8303          ((memq article gnus-newsgroup-replied)
8304           gnus-replied-mark)
8305          ((memq article gnus-newsgroup-saved)
8306           gnus-saved-mark)
8307          (t gnus-unread-mark))
8308    'replied)
8309   (when (gnus-visual-p 'summary-highlight 'highlight)
8310     (gnus-run-hooks 'gnus-summary-update-hook))
8311   t)
8312
8313 (defun gnus-summary-update-mark (mark type)
8314   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8315         (buffer-read-only nil))
8316     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8317     (when forward
8318       (when (looking-at "\r")
8319         (incf forward))
8320       (when (<= (+ forward (point)) (point-max))
8321         ;; Go to the right position on the line.
8322         (goto-char (+ forward (point)))
8323         ;; Replace the old mark with the new mark.
8324         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8325         ;; Optionally update the marks by some user rule.
8326         (when (eq type 'unread)
8327           (gnus-data-set-mark
8328            (gnus-data-find (gnus-summary-article-number)) mark)
8329           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8330
8331 (defun gnus-mark-article-as-read (article &optional mark)
8332   "Enter ARTICLE in the pertinent lists and remove it from others."
8333   ;; Make the article expirable.
8334   (let ((mark (or mark gnus-del-mark)))
8335     (if (= mark gnus-expirable-mark)
8336         (push article gnus-newsgroup-expirable)
8337       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8338     ;; Remove from unread and marked lists.
8339     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8340     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8341     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8342     (push (cons article mark) gnus-newsgroup-reads)
8343     ;; Possibly remove from cache, if that is used.
8344     (when gnus-use-cache
8345       (gnus-cache-enter-remove-article article))
8346     t))
8347
8348 (defun gnus-mark-article-as-unread (article &optional mark)
8349   "Enter ARTICLE in the pertinent lists and remove it from others."
8350   (let ((mark (or mark gnus-ticked-mark)))
8351     (if (<= article 0)
8352         (progn
8353           (gnus-error 1 "Can't mark negative article numbers")
8354           nil)
8355       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8356             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8357             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8358             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8359
8360       ;; Unsuppress duplicates?
8361       (when gnus-suppress-duplicates
8362         (gnus-dup-unsuppress-article article))
8363
8364       (cond ((= mark gnus-ticked-mark)
8365              (push article gnus-newsgroup-marked))
8366             ((= mark gnus-dormant-mark)
8367              (push article gnus-newsgroup-dormant))
8368             (t
8369              (push article gnus-newsgroup-unreads)))
8370       (gnus-pull article gnus-newsgroup-reads)
8371       t)))
8372
8373 (defalias 'gnus-summary-mark-as-unread-forward
8374   'gnus-summary-tick-article-forward)
8375 (make-obsolete 'gnus-summary-mark-as-unread-forward
8376                'gnus-summary-tick-article-forward)
8377 (defun gnus-summary-tick-article-forward (n)
8378   "Tick N articles forwards.
8379 If N is negative, tick backwards instead.
8380 The difference between N and the number of articles ticked is returned."
8381   (interactive "p")
8382   (gnus-summary-mark-forward n gnus-ticked-mark))
8383
8384 (defalias 'gnus-summary-mark-as-unread-backward
8385   'gnus-summary-tick-article-backward)
8386 (make-obsolete 'gnus-summary-mark-as-unread-backward
8387                'gnus-summary-tick-article-backward)
8388 (defun gnus-summary-tick-article-backward (n)
8389   "Tick N articles backwards.
8390 The difference between N and the number of articles ticked is returned."
8391   (interactive "p")
8392   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8393
8394 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8395 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8396 (defun gnus-summary-tick-article (&optional article clear-mark)
8397   "Mark current article as unread.
8398 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8399 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8400   (interactive)
8401   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8402                                        gnus-ticked-mark)))
8403
8404 (defun gnus-summary-mark-as-read-forward (n)
8405   "Mark N articles as read forwards.
8406 If N is negative, mark backwards instead.
8407 The difference between N and the actual number of articles marked is
8408 returned."
8409   (interactive "p")
8410   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8411
8412 (defun gnus-summary-mark-as-read-backward (n)
8413   "Mark the N articles as read backwards.
8414 The difference between N and the actual number of articles marked is
8415 returned."
8416   (interactive "p")
8417   (gnus-summary-mark-forward
8418    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8419
8420 (defun gnus-summary-mark-as-read (&optional article mark)
8421   "Mark current article as read.
8422 ARTICLE specifies the article to be marked as read.
8423 MARK specifies a string to be inserted at the beginning of the line."
8424   (gnus-summary-mark-article article mark))
8425
8426 (defun gnus-summary-clear-mark-forward (n)
8427   "Clear marks from N articles forward.
8428 If N is negative, clear backward instead.
8429 The difference between N and the number of marks cleared is returned."
8430   (interactive "p")
8431   (gnus-summary-mark-forward n gnus-unread-mark))
8432
8433 (defun gnus-summary-clear-mark-backward (n)
8434   "Clear marks from N articles backward.
8435 The difference between N and the number of marks cleared is returned."
8436   (interactive "p")
8437   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8438
8439 (defun gnus-summary-mark-unread-as-read ()
8440   "Intended to be used by `gnus-summary-mark-article-hook'."
8441   (when (memq gnus-current-article gnus-newsgroup-unreads)
8442     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8443
8444 (defun gnus-summary-mark-read-and-unread-as-read ()
8445   "Intended to be used by `gnus-summary-mark-article-hook'."
8446   (let ((mark (gnus-summary-article-mark)))
8447     (when (or (gnus-unread-mark-p mark)
8448               (gnus-read-mark-p mark))
8449       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8450
8451 (defun gnus-summary-mark-region-as-read (point mark all)
8452   "Mark all unread articles between point and mark as read.
8453 If given a prefix, mark all articles between point and mark as read,
8454 even ticked and dormant ones."
8455   (interactive "r\nP")
8456   (save-excursion
8457     (let (article)
8458       (goto-char point)
8459       (beginning-of-line)
8460       (while (and
8461               (< (point) mark)
8462               (progn
8463                 (when (or all
8464                           (memq (setq article (gnus-summary-article-number))
8465                                 gnus-newsgroup-unreads))
8466                   (gnus-summary-mark-article article gnus-del-mark))
8467                 t)
8468               (gnus-summary-find-next))))))
8469
8470 (defun gnus-summary-mark-below (score mark)
8471   "Mark articles with score less than SCORE with MARK."
8472   (interactive "P\ncMark: ")
8473   (setq score (if score
8474                   (prefix-numeric-value score)
8475                 (or gnus-summary-default-score 0)))
8476   (save-excursion
8477     (set-buffer gnus-summary-buffer)
8478     (goto-char (point-min))
8479     (while
8480         (progn
8481           (and (< (gnus-summary-article-score) score)
8482                (gnus-summary-mark-article nil mark))
8483           (gnus-summary-find-next)))))
8484
8485 (defun gnus-summary-kill-below (&optional score)
8486   "Mark articles with score below SCORE as read."
8487   (interactive "P")
8488   (gnus-summary-mark-below score gnus-killed-mark))
8489
8490 (defun gnus-summary-clear-above (&optional score)
8491   "Clear all marks from articles with score above SCORE."
8492   (interactive "P")
8493   (gnus-summary-mark-above score gnus-unread-mark))
8494
8495 (defun gnus-summary-tick-above (&optional score)
8496   "Tick all articles with score above SCORE."
8497   (interactive "P")
8498   (gnus-summary-mark-above score gnus-ticked-mark))
8499
8500 (defun gnus-summary-mark-above (score mark)
8501   "Mark articles with score over SCORE with MARK."
8502   (interactive "P\ncMark: ")
8503   (setq score (if score
8504                   (prefix-numeric-value score)
8505                 (or gnus-summary-default-score 0)))
8506   (save-excursion
8507     (set-buffer gnus-summary-buffer)
8508     (goto-char (point-min))
8509     (while (and (progn
8510                   (when (> (gnus-summary-article-score) score)
8511                     (gnus-summary-mark-article nil mark))
8512                   t)
8513                 (gnus-summary-find-next)))))
8514
8515 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8516 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8517 (defun gnus-summary-limit-include-expunged (&optional no-error)
8518   "Display all the hidden articles that were expunged for low scores."
8519   (interactive)
8520   (let ((buffer-read-only nil))
8521     (let ((scored gnus-newsgroup-scored)
8522           headers h)
8523       (while scored
8524         (unless (gnus-summary-goto-subject (caar scored))
8525           (and (setq h (gnus-summary-article-header (caar scored)))
8526                (< (cdar scored) gnus-summary-expunge-below)
8527                (push h headers)))
8528         (setq scored (cdr scored)))
8529       (if (not headers)
8530           (when (not no-error)
8531             (error "No expunged articles hidden"))
8532         (goto-char (point-min))
8533         (gnus-summary-prepare-unthreaded (nreverse headers))
8534         (goto-char (point-min))
8535         (gnus-summary-position-point)
8536         t))))
8537
8538 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8539   "Mark all unread articles in this newsgroup as read.
8540 If prefix argument ALL is non-nil, ticked and dormant articles will
8541 also be marked as read.
8542 If QUIETLY is non-nil, no questions will be asked.
8543 If TO-HERE is non-nil, it should be a point in the buffer.  All
8544 articles before this point will be marked as read.
8545 Note that this function will only catch up the unread article
8546 in the current summary buffer limitation.
8547 The number of articles marked as read is returned."
8548   (interactive "P")
8549   (prog1
8550       (save-excursion
8551         (when (or quietly
8552                   (not gnus-interactive-catchup) ;Without confirmation?
8553                   gnus-expert-user
8554                   (gnus-y-or-n-p
8555                    (if all
8556                        "Mark absolutely all articles as read? "
8557                      "Mark all unread articles as read? ")))
8558           (if (and not-mark
8559                    (not gnus-newsgroup-adaptive)
8560                    (not gnus-newsgroup-auto-expire)
8561                    (not gnus-suppress-duplicates)
8562                    (or (not gnus-use-cache)
8563                        (eq gnus-use-cache 'passive)))
8564               (progn
8565                 (when all
8566                   (setq gnus-newsgroup-marked nil
8567                         gnus-newsgroup-dormant nil))
8568                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8569             ;; We actually mark all articles as canceled, which we
8570             ;; have to do when using auto-expiry or adaptive scoring.
8571             (gnus-summary-show-all-threads)
8572             (when (gnus-summary-first-subject (not all) t)
8573               (while (and
8574                       (if to-here (< (point) to-here) t)
8575                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8576                       (gnus-summary-find-next (not all) nil nil t))))
8577             (gnus-set-mode-line 'summary))
8578           t))
8579     (gnus-summary-position-point)))
8580
8581 (defun gnus-summary-catchup-to-here (&optional all)
8582   "Mark all unticked articles before the current one as read.
8583 If ALL is non-nil, also mark ticked and dormant articles as read."
8584   (interactive "P")
8585   (save-excursion
8586     (gnus-save-hidden-threads
8587       (let ((beg (point)))
8588         ;; We check that there are unread articles.
8589         (when (or all (gnus-summary-find-prev))
8590           (gnus-summary-catchup all t beg)))))
8591   (gnus-summary-position-point))
8592
8593 (defun gnus-summary-catchup-all (&optional quietly)
8594   "Mark all articles in this newsgroup as read."
8595   (interactive "P")
8596   (gnus-summary-catchup t quietly))
8597
8598 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8599   "Mark all unread articles in this group as read, then exit.
8600 If prefix argument ALL is non-nil, all articles are marked as read."
8601   (interactive "P")
8602   (when (gnus-summary-catchup all quietly nil 'fast)
8603     ;; Select next newsgroup or exit.
8604     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8605              (eq gnus-auto-select-next 'quietly))
8606         (gnus-summary-next-group nil)
8607       (gnus-summary-exit))))
8608
8609 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8610   "Mark all articles in this newsgroup as read, and then exit."
8611   (interactive "P")
8612   (gnus-summary-catchup-and-exit t quietly))
8613
8614 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8615   "Mark all articles in this group as read and select the next group.
8616 If given a prefix, mark all articles, unread as well as ticked, as
8617 read."
8618   (interactive "P")
8619   (save-excursion
8620     (gnus-summary-catchup all))
8621   (gnus-summary-next-group))
8622
8623 ;; Thread-based commands.
8624
8625 (defun gnus-summary-articles-in-thread (&optional article)
8626   "Return a list of all articles in the current thread.
8627 If ARTICLE is non-nil, return all articles in the thread that starts
8628 with that article."
8629   (let* ((article (or article (gnus-summary-article-number)))
8630          (data (gnus-data-find-list article))
8631          (top-level (gnus-data-level (car data)))
8632          (top-subject
8633           (cond ((null gnus-thread-operation-ignore-subject)
8634                  (gnus-simplify-subject-re
8635                   (mail-header-subject (gnus-data-header (car data)))))
8636                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8637                  (gnus-simplify-subject-fuzzy
8638                   (mail-header-subject (gnus-data-header (car data)))))
8639                 (t nil)))
8640          (end-point (save-excursion
8641                       (if (gnus-summary-go-to-next-thread)
8642                           (point) (point-max))))
8643          articles)
8644     (while (and data
8645                 (< (gnus-data-pos (car data)) end-point))
8646       (when (or (not top-subject)
8647                 (string= top-subject
8648                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8649                              (gnus-simplify-subject-fuzzy
8650                               (mail-header-subject
8651                                (gnus-data-header (car data))))
8652                            (gnus-simplify-subject-re
8653                             (mail-header-subject
8654                              (gnus-data-header (car data)))))))
8655         (push (gnus-data-number (car data)) articles))
8656       (unless (and (setq data (cdr data))
8657                    (> (gnus-data-level (car data)) top-level))
8658         (setq data nil)))
8659     ;; Return the list of articles.
8660     (nreverse articles)))
8661
8662 (defun gnus-summary-rethread-current ()
8663   "Rethread the thread the current article is part of."
8664   (interactive)
8665   (let* ((gnus-show-threads t)
8666          (article (gnus-summary-article-number))
8667          (id (mail-header-id (gnus-summary-article-header)))
8668          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8669     (unless id
8670       (error "No article on the current line"))
8671     (gnus-rebuild-thread id)
8672     (gnus-summary-goto-subject article)))
8673
8674 (defun gnus-summary-reparent-thread ()
8675   "Make the current article child of the marked (or previous) article.
8676
8677 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8678 is non-nil or the Subject: of both articles are the same."
8679   (interactive)
8680   (unless (not (gnus-group-read-only-p))
8681     (error "The current newsgroup does not support article editing"))
8682   (unless (<= (length gnus-newsgroup-processable) 1)
8683     (error "No more than one article may be marked"))
8684   (save-window-excursion
8685     (let ((gnus-article-buffer " *reparent*")
8686           (current-article (gnus-summary-article-number))
8687           ;; First grab the marked article, otherwise one line up.
8688           (parent-article (if (not (null gnus-newsgroup-processable))
8689                               (car gnus-newsgroup-processable)
8690                             (save-excursion
8691                               (if (eq (forward-line -1) 0)
8692                                   (gnus-summary-article-number)
8693                                 (error "Beginning of summary buffer"))))))
8694       (unless (not (eq current-article parent-article))
8695         (error "An article may not be self-referential"))
8696       (let ((message-id (mail-header-id
8697                          (gnus-summary-article-header parent-article))))
8698         (unless (and message-id (not (equal message-id "")))
8699           (error "No message-id in desired parent"))
8700         (gnus-with-article current-article
8701           (goto-char (point-min))
8702           (if (re-search-forward "^References: " nil t)
8703               (progn
8704                 (re-search-forward "^[^ \t]" nil t)
8705                 (forward-line -1)
8706                 (end-of-line)
8707                 (insert " " message-id))
8708             (insert "References: " message-id "\n")))
8709         (set-buffer gnus-summary-buffer)
8710         (gnus-summary-unmark-all-processable)
8711         (gnus-summary-update-article current-article)
8712         (gnus-summary-rethread-current)
8713         (gnus-message 3 "Article %d is now the child of article %d"
8714                       current-article parent-article)))))
8715
8716 (defun gnus-summary-toggle-threads (&optional arg)
8717   "Toggle showing conversation threads.
8718 If ARG is positive number, turn showing conversation threads on."
8719   (interactive "P")
8720   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8721     (setq gnus-show-threads
8722           (if (null arg) (not gnus-show-threads)
8723             (> (prefix-numeric-value arg) 0)))
8724     (gnus-summary-prepare)
8725     (gnus-summary-goto-subject current)
8726     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8727     (gnus-summary-position-point)))
8728
8729 (defun gnus-summary-show-all-threads ()
8730   "Show all threads."
8731   (interactive)
8732   (save-excursion
8733     (let ((buffer-read-only nil))
8734       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8735   (gnus-summary-position-point))
8736
8737 (defun gnus-summary-show-thread ()
8738   "Show thread subtrees.
8739 Returns nil if no thread was there to be shown."
8740   (interactive)
8741   (let ((buffer-read-only nil)
8742         (orig (point))
8743         ;; first goto end then to beg, to have point at beg after let
8744         (end (progn (end-of-line) (point)))
8745         (beg (progn (beginning-of-line) (point))))
8746     (prog1
8747         ;; Any hidden lines here?
8748         (search-forward "\r" end t)
8749       (subst-char-in-region beg end ?\^M ?\n t)
8750       (goto-char orig)
8751       (gnus-summary-position-point))))
8752
8753 (defun gnus-summary-hide-all-threads ()
8754   "Hide all thread subtrees."
8755   (interactive)
8756   (save-excursion
8757     (goto-char (point-min))
8758     (gnus-summary-hide-thread)
8759     (while (zerop (gnus-summary-next-thread 1 t))
8760       (gnus-summary-hide-thread)))
8761   (gnus-summary-position-point))
8762
8763 (defun gnus-summary-hide-thread ()
8764   "Hide thread subtrees.
8765 Returns nil if no threads were there to be hidden."
8766   (interactive)
8767   (let ((buffer-read-only nil)
8768         (start (point))
8769         (article (gnus-summary-article-number)))
8770     (goto-char start)
8771     ;; Go forward until either the buffer ends or the subthread
8772     ;; ends.
8773     (when (and (not (eobp))
8774                (or (zerop (gnus-summary-next-thread 1 t))
8775                    (goto-char (point-max))))
8776       (prog1
8777           (if (and (> (point) start)
8778                    (search-backward "\n" start t))
8779               (progn
8780                 (subst-char-in-region start (point) ?\n ?\^M)
8781                 (gnus-summary-goto-subject article))
8782             (goto-char start)
8783             nil)
8784         ;;(gnus-summary-position-point)
8785         ))))
8786
8787 (defun gnus-summary-go-to-next-thread (&optional previous)
8788   "Go to the same level (or less) next thread.
8789 If PREVIOUS is non-nil, go to previous thread instead.
8790 Return the article number moved to, or nil if moving was impossible."
8791   (let ((level (gnus-summary-thread-level))
8792         (way (if previous -1 1))
8793         (beg (point)))
8794     (forward-line way)
8795     (while (and (not (eobp))
8796                 (< level (gnus-summary-thread-level)))
8797       (forward-line way))
8798     (if (eobp)
8799         (progn
8800           (goto-char beg)
8801           nil)
8802       (setq beg (point))
8803       (prog1
8804           (gnus-summary-article-number)
8805         (goto-char beg)))))
8806
8807 (defun gnus-summary-next-thread (n &optional silent)
8808   "Go to the same level next N'th thread.
8809 If N is negative, search backward instead.
8810 Returns the difference between N and the number of skips actually
8811 done.
8812
8813 If SILENT, don't output messages."
8814   (interactive "p")
8815   (let ((backward (< n 0))
8816         (n (abs n)))
8817     (while (and (> n 0)
8818                 (gnus-summary-go-to-next-thread backward))
8819       (decf n))
8820     (unless silent
8821       (gnus-summary-position-point))
8822     (when (and (not silent) (/= 0 n))
8823       (gnus-message 7 "No more threads"))
8824     n))
8825
8826 (defun gnus-summary-prev-thread (n)
8827   "Go to the same level previous N'th thread.
8828 Returns the difference between N and the number of skips actually
8829 done."
8830   (interactive "p")
8831   (gnus-summary-next-thread (- n)))
8832
8833 (defun gnus-summary-go-down-thread ()
8834   "Go down one level in the current thread."
8835   (let ((children (gnus-summary-article-children)))
8836     (when children
8837       (gnus-summary-goto-subject (car children)))))
8838
8839 (defun gnus-summary-go-up-thread ()
8840   "Go up one level in the current thread."
8841   (let ((parent (gnus-summary-article-parent)))
8842     (when parent
8843       (gnus-summary-goto-subject parent))))
8844
8845 (defun gnus-summary-down-thread (n)
8846   "Go down thread N steps.
8847 If N is negative, go up instead.
8848 Returns the difference between N and how many steps down that were
8849 taken."
8850   (interactive "p")
8851   (let ((up (< n 0))
8852         (n (abs n)))
8853     (while (and (> n 0)
8854                 (if up (gnus-summary-go-up-thread)
8855                   (gnus-summary-go-down-thread)))
8856       (setq n (1- n)))
8857     (gnus-summary-position-point)
8858     (when (/= 0 n)
8859       (gnus-message 7 "Can't go further"))
8860     n))
8861
8862 (defun gnus-summary-up-thread (n)
8863   "Go up thread N steps.
8864 If N is negative, go up instead.
8865 Returns the difference between N and how many steps down that were
8866 taken."
8867   (interactive "p")
8868   (gnus-summary-down-thread (- n)))
8869
8870 (defun gnus-summary-top-thread ()
8871   "Go to the top of the thread."
8872   (interactive)
8873   (while (gnus-summary-go-up-thread))
8874   (gnus-summary-article-number))
8875
8876 (defun gnus-summary-kill-thread (&optional unmark)
8877   "Mark articles under current thread as read.
8878 If the prefix argument is positive, remove any kinds of marks.
8879 If the prefix argument is negative, tick articles instead."
8880   (interactive "P")
8881   (when unmark
8882     (setq unmark (prefix-numeric-value unmark)))
8883   (let ((articles (gnus-summary-articles-in-thread)))
8884     (save-excursion
8885       ;; Expand the thread.
8886       (gnus-summary-show-thread)
8887       ;; Mark all the articles.
8888       (while articles
8889         (gnus-summary-goto-subject (car articles))
8890         (cond ((null unmark)
8891                (gnus-summary-mark-article-as-read gnus-killed-mark))
8892               ((> unmark 0)
8893                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8894               (t
8895                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8896         (setq articles (cdr articles))))
8897     ;; Hide killed subtrees.
8898     (and (null unmark)
8899          gnus-thread-hide-killed
8900          (gnus-summary-hide-thread))
8901     ;; If marked as read, go to next unread subject.
8902     (when (null unmark)
8903       ;; Go to next unread subject.
8904       (gnus-summary-next-subject 1 t)))
8905   (gnus-set-mode-line 'summary))
8906
8907 ;; Summary sorting commands
8908
8909 (defun gnus-summary-sort-by-number (&optional reverse)
8910   "Sort the summary buffer by article number.
8911 Argument REVERSE means reverse order."
8912   (interactive "P")
8913   (gnus-summary-sort 'number reverse))
8914
8915 (defun gnus-summary-sort-by-author (&optional reverse)
8916   "Sort the summary buffer by author name alphabetically.
8917 If case-fold-search is non-nil, case of letters is ignored.
8918 Argument REVERSE means reverse order."
8919   (interactive "P")
8920   (gnus-summary-sort 'author reverse))
8921
8922 (defun gnus-summary-sort-by-subject (&optional reverse)
8923   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8924 If case-fold-search is non-nil, case of letters is ignored.
8925 Argument REVERSE means reverse order."
8926   (interactive "P")
8927   (gnus-summary-sort 'subject reverse))
8928
8929 (defun gnus-summary-sort-by-date (&optional reverse)
8930   "Sort the summary buffer by date.
8931 Argument REVERSE means reverse order."
8932   (interactive "P")
8933   (gnus-summary-sort 'date reverse))
8934
8935 (defun gnus-summary-sort-by-score (&optional reverse)
8936   "Sort the summary buffer by score.
8937 Argument REVERSE means reverse order."
8938   (interactive "P")
8939   (gnus-summary-sort 'score reverse))
8940
8941 (defun gnus-summary-sort-by-lines (&optional reverse)
8942   "Sort the summary buffer by the number of lines.
8943 Argument REVERSE means reverse order."
8944   (interactive "P")
8945   (gnus-summary-sort 'lines reverse))
8946
8947 (defun gnus-summary-sort-by-chars (&optional reverse)
8948   "Sort the summary buffer by article length.
8949 Argument REVERSE means reverse order."
8950   (interactive "P")
8951   (gnus-summary-sort 'chars reverse))   
8952
8953 (defun gnus-summary-sort (predicate reverse)
8954   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8955   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8956          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8957          (gnus-thread-sort-functions
8958           (if (not reverse)
8959               thread
8960             `(lambda (t1 t2)
8961                (,thread t2 t1))))
8962          (gnus-article-sort-functions
8963           (if (not reverse)
8964               article
8965             `(lambda (t1 t2)
8966                (,article t2 t1))))
8967          (buffer-read-only)
8968          (gnus-summary-prepare-hook nil))
8969     ;; We do the sorting by regenerating the threads.
8970     (gnus-summary-prepare)
8971     ;; Hide subthreads if needed.
8972     (when (and gnus-show-threads gnus-thread-hide-subtree)
8973       (gnus-summary-hide-all-threads))))
8974
8975 ;; Summary saving commands.
8976
8977 (defun gnus-summary-save-article (&optional n not-saved)
8978   "Save the current article using the default saver function.
8979 If N is a positive number, save the N next articles.
8980 If N is a negative number, save the N previous articles.
8981 If N is nil and any articles have been marked with the process mark,
8982 save those articles instead.
8983 The variable `gnus-default-article-saver' specifies the saver function."
8984   (interactive "P")
8985   (let* ((articles (gnus-summary-work-articles n))
8986          (save-buffer (save-excursion
8987                         (nnheader-set-temp-buffer " *Gnus Save*")))
8988          (num (length articles))
8989          header file)
8990     (dolist (article articles)
8991       (setq header (gnus-summary-article-header article))
8992       (if (not (vectorp header))
8993           ;; This is a pseudo-article.
8994           (if (assq 'name header)
8995               (gnus-copy-file (cdr (assq 'name header)))
8996             (gnus-message 1 "Article %d is unsaveable" article))
8997         ;; This is a real article.
8998         (save-window-excursion
8999           (gnus-summary-select-article t nil nil article))
9000         (save-excursion
9001           (set-buffer save-buffer)
9002           (erase-buffer)
9003           (insert-buffer-substring gnus-original-article-buffer))
9004         (setq file (gnus-article-save save-buffer file num))
9005         (gnus-summary-remove-process-mark article)
9006         (unless not-saved
9007           (gnus-summary-set-saved-mark article))))
9008     (gnus-kill-buffer save-buffer)
9009     (gnus-summary-position-point)
9010     (gnus-set-mode-line 'summary)
9011     n))
9012
9013 (defun gnus-summary-pipe-output (&optional arg)
9014   "Pipe the current article to a subprocess.
9015 If N is a positive number, pipe the N next articles.
9016 If N is a negative number, pipe the N previous articles.
9017 If N is nil and any articles have been marked with the process mark,
9018 pipe those articles instead."
9019   (interactive "P")
9020   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9021     (gnus-summary-save-article arg t))
9022   (gnus-configure-windows 'pipe))
9023
9024 (defun gnus-summary-save-article-mail (&optional arg)
9025   "Append the current article to an mail file.
9026 If N is a positive number, save the N next articles.
9027 If N is a negative number, save the N previous articles.
9028 If N is nil and any articles have been marked with the process mark,
9029 save those articles instead."
9030   (interactive "P")
9031   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9032     (gnus-summary-save-article arg)))
9033
9034 (defun gnus-summary-save-article-rmail (&optional arg)
9035   "Append the current article to an rmail file.
9036 If N is a positive number, save the N next articles.
9037 If N is a negative number, save the N previous articles.
9038 If N is nil and any articles have been marked with the process mark,
9039 save those articles instead."
9040   (interactive "P")
9041   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9042     (gnus-summary-save-article arg)))
9043
9044 (defun gnus-summary-save-article-file (&optional arg)
9045   "Append the current article to a file.
9046 If N is a positive number, save the N next articles.
9047 If N is a negative number, save the N previous articles.
9048 If N is nil and any articles have been marked with the process mark,
9049 save those articles instead."
9050   (interactive "P")
9051   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9052     (gnus-summary-save-article arg)))
9053
9054 (defun gnus-summary-write-article-file (&optional arg)
9055   "Write the current article to a file, deleting the previous file.
9056 If N is a positive number, save the N next articles.
9057 If N is a negative number, save the N previous articles.
9058 If N is nil and any articles have been marked with the process mark,
9059 save those articles instead."
9060   (interactive "P")
9061   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9062     (gnus-summary-save-article arg)))
9063
9064 (defun gnus-summary-save-article-body-file (&optional arg)
9065   "Append the current article body to a file.
9066 If N is a positive number, save the N next articles.
9067 If N is a negative number, save the N previous articles.
9068 If N is nil and any articles have been marked with the process mark,
9069 save those articles instead."
9070   (interactive "P")
9071   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9072     (gnus-summary-save-article arg)))
9073
9074 (defun gnus-summary-pipe-message (program)
9075   "Pipe the current article through PROGRAM."
9076   (interactive "sProgram: ")
9077   (gnus-summary-select-article)
9078   (let ((mail-header-separator ""))
9079     (gnus-eval-in-buffer-window gnus-article-buffer
9080       (save-restriction
9081         (widen)
9082         (let ((start (window-start))
9083               buffer-read-only)
9084           (message-pipe-buffer-body program)
9085           (set-window-start (get-buffer-window (current-buffer)) start))))))
9086
9087 (defun gnus-get-split-value (methods)
9088   "Return a value based on the split METHODS."
9089   (let (split-name method result match)
9090     (when methods
9091       (save-excursion
9092         (set-buffer gnus-original-article-buffer)
9093         (save-restriction
9094           (nnheader-narrow-to-headers)
9095           (while methods
9096             (goto-char (point-min))
9097             (setq method (pop methods))
9098             (setq match (car method))
9099             (when (cond
9100                    ((stringp match)
9101                     ;; Regular expression.
9102                     (ignore-errors
9103                       (re-search-forward match nil t)))
9104                    ((gnus-functionp match)
9105                     ;; Function.
9106                     (save-restriction
9107                       (widen)
9108                       (setq result (funcall match gnus-newsgroup-name))))
9109                    ((consp match)
9110                     ;; Form.
9111                     (save-restriction
9112                       (widen)
9113                       (setq result (eval match)))))
9114               (setq split-name (append (cdr method) split-name))
9115               (cond ((stringp result)
9116                      (push (expand-file-name
9117                             result gnus-article-save-directory)
9118                            split-name))
9119                     ((consp result)
9120                      (setq split-name (append result split-name)))))))))
9121     (nreverse split-name)))
9122
9123 (defun gnus-valid-move-group-p (group)
9124   (and (boundp group)
9125        (symbol-name group)
9126        (symbol-value group)
9127        (gnus-get-function (gnus-find-method-for-group
9128                            (symbol-name group)) 'request-accept-article t)))
9129
9130 (defun gnus-read-move-group-name (prompt default articles prefix)
9131   "Read a group name."
9132   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9133          (minibuffer-confirm-incomplete nil) ; XEmacs
9134          (prom
9135           (format "%s %s to:"
9136                   prompt
9137                   (if (> (length articles) 1)
9138                       (format "these %d articles" (length articles))
9139                     "this article")))
9140          (to-newsgroup
9141           (cond
9142            ((null split-name)
9143             (gnus-completing-read default prom
9144                                   gnus-active-hashtb
9145                                   'gnus-valid-move-group-p
9146                                   nil prefix
9147                                   'gnus-group-history))
9148            ((= 1 (length split-name))
9149             (gnus-completing-read (car split-name) prom
9150                                   gnus-active-hashtb
9151                                   'gnus-valid-move-group-p
9152                                   nil nil
9153                                   'gnus-group-history))
9154            (t
9155             (gnus-completing-read nil prom
9156                                   (mapcar (lambda (el) (list el))
9157                                           (nreverse split-name))
9158                                   nil nil nil
9159                                   'gnus-group-history)))))
9160     (when to-newsgroup
9161       (if (or (string= to-newsgroup "")
9162               (string= to-newsgroup prefix))
9163           (setq to-newsgroup default))
9164       (unless to-newsgroup
9165         (error "No group name entered"))
9166       (or (gnus-active to-newsgroup)
9167           (gnus-activate-group to-newsgroup)
9168           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9169                                      to-newsgroup))
9170               (or (and (gnus-request-create-group
9171                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
9172                        (gnus-activate-group
9173                         to-newsgroup nil nil
9174                         (gnus-group-name-to-method to-newsgroup))
9175                        (gnus-subscribe-group to-newsgroup))
9176                   (error "Couldn't create group %s" to-newsgroup)))
9177           (error "No such group: %s" to-newsgroup)))
9178     to-newsgroup))
9179
9180 (defun gnus-summary-save-parts (type dir n reverse)
9181   "Save parts matching TYPE to DIR.
9182 If REVERSE, save parts that do not match TYPE."
9183   (interactive
9184    (list (read-string "Save parts of type: " "image/.*")
9185          (read-file-name "Save to directory: " t nil t)
9186          current-prefix-arg))
9187   (gnus-summary-iterate n
9188     (let ((gnus-display-mime-function nil)
9189           (gnus-inhibit-treatment t))
9190       (gnus-summary-select-article))
9191     (save-excursion
9192       (set-buffer gnus-article-buffer)
9193       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9194         (when handles
9195           (gnus-summary-save-parts-1 type dir handles reverse)
9196           (mm-destroy-parts handles))))))
9197
9198 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9199   (if (stringp (car handle))
9200       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9201               (cdr handle))
9202     (when (if reverse
9203               (not (string-match type (mm-handle-media-type handle)))
9204             (string-match type (mm-handle-media-type handle)))
9205       (let ((file (expand-file-name
9206                    (file-name-nondirectory
9207                     (or
9208                      (mail-content-type-get
9209                       (mm-handle-disposition handle) 'filename)
9210                      (concat gnus-newsgroup-name "." gnus-current-article)))
9211                    dir)))
9212         (unless (file-exists-p file)
9213           (mm-save-part-to-file handle file))))))
9214
9215 ;; Summary extract commands
9216
9217 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9218   (let ((buffer-read-only nil)
9219         (article (gnus-summary-article-number))
9220         after-article b e)
9221     (unless (gnus-summary-goto-subject article)
9222       (error "No such article: %d" article))
9223     (gnus-summary-position-point)
9224     ;; If all commands are to be bunched up on one line, we collect
9225     ;; them here.
9226     (unless gnus-view-pseudos-separately
9227       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9228             files action)
9229         (while ps
9230           (setq action (cdr (assq 'action (car ps))))
9231           (setq files (list (cdr (assq 'name (car ps)))))
9232           (while (and ps (cdr ps)
9233                       (string= (or action "1")
9234                                (or (cdr (assq 'action (cadr ps))) "2")))
9235             (push (cdr (assq 'name (cadr ps))) files)
9236             (setcdr ps (cddr ps)))
9237           (when files
9238             (when (not (string-match "%s" action))
9239               (push " " files))
9240             (push " " files)
9241             (when (assq 'execute (car ps))
9242               (setcdr (assq 'execute (car ps))
9243                       (funcall (if (string-match "%s" action)
9244                                    'format 'concat)
9245                                action
9246                                (mapconcat
9247                                 (lambda (f)
9248                                   (if (equal f " ")
9249                                       f
9250                                     (gnus-quote-arg-for-sh-or-csh f)))
9251                                 files " ")))))
9252           (setq ps (cdr ps)))))
9253     (if (and gnus-view-pseudos (not not-view))
9254         (while pslist
9255           (when (assq 'execute (car pslist))
9256             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9257                                   (eq gnus-view-pseudos 'not-confirm)))
9258           (setq pslist (cdr pslist)))
9259       (save-excursion
9260         (while pslist
9261           (setq after-article (or (cdr (assq 'article (car pslist)))
9262                                   (gnus-summary-article-number)))
9263           (gnus-summary-goto-subject after-article)
9264           (forward-line 1)
9265           (setq b (point))
9266           (insert "    " (file-name-nondirectory
9267                           (cdr (assq 'name (car pslist))))
9268                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9269           (setq e (point))
9270           (forward-line -1)             ; back to `b'
9271           (gnus-add-text-properties
9272            b (1- e) (list 'gnus-number gnus-reffed-article-number
9273                           gnus-mouse-face-prop gnus-mouse-face))
9274           (gnus-data-enter
9275            after-article gnus-reffed-article-number
9276            gnus-unread-mark b (car pslist) 0 (- e b))
9277           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9278           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9279           (setq pslist (cdr pslist)))))))
9280
9281 (defun gnus-pseudos< (p1 p2)
9282   (let ((c1 (cdr (assq 'action p1)))
9283         (c2 (cdr (assq 'action p2))))
9284     (and c1 c2 (string< c1 c2))))
9285
9286 (defun gnus-request-pseudo-article (props)
9287   (cond ((assq 'execute props)
9288          (gnus-execute-command (cdr (assq 'execute props)))))
9289   (let ((gnus-current-article (gnus-summary-article-number)))
9290     (gnus-run-hooks 'gnus-mark-article-hook)))
9291
9292 (defun gnus-execute-command (command &optional automatic)
9293   (save-excursion
9294     (gnus-article-setup-buffer)
9295     (set-buffer gnus-article-buffer)
9296     (setq buffer-read-only nil)
9297     (let ((command (if automatic command
9298                      (read-string "Command: " (cons command 0)))))
9299       (erase-buffer)
9300       (insert "$ " command "\n\n")
9301       (if gnus-view-pseudo-asynchronously
9302           (start-process "gnus-execute" (current-buffer) shell-file-name
9303                          shell-command-switch command)
9304         (call-process shell-file-name nil t nil
9305                       shell-command-switch command)))))
9306
9307 ;; Summary kill commands.
9308
9309 (defun gnus-summary-edit-global-kill (article)
9310   "Edit the \"global\" kill file."
9311   (interactive (list (gnus-summary-article-number)))
9312   (gnus-group-edit-global-kill article))
9313
9314 (defun gnus-summary-edit-local-kill ()
9315   "Edit a local kill file applied to the current newsgroup."
9316   (interactive)
9317   (setq gnus-current-headers (gnus-summary-article-header))
9318   (gnus-group-edit-local-kill
9319    (gnus-summary-article-number) gnus-newsgroup-name))
9320
9321 ;;; Header reading.
9322
9323 (defun gnus-read-header (id &optional header)
9324   "Read the headers of article ID and enter them into the Gnus system."
9325   (let ((group gnus-newsgroup-name)
9326         (gnus-override-method
9327          (and (gnus-news-group-p gnus-newsgroup-name)
9328               gnus-refer-article-method))
9329         where)
9330     ;; First we check to see whether the header in question is already
9331     ;; fetched.
9332     (if (stringp id)
9333         ;; This is a Message-ID.
9334         (setq header (or header (gnus-id-to-header id)))
9335       ;; This is an article number.
9336       (setq header (or header (gnus-summary-article-header id))))
9337     (if (and header
9338              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9339         ;; We have found the header.
9340         header
9341       ;; If this is a sparse article, we have to nix out its
9342       ;; previous entry in the thread hashtb.
9343       (when (and header
9344                  (gnus-summary-article-sparse-p (mail-header-number header)))
9345         (let* ((parent (gnus-parent-id (mail-header-references header)))
9346                (thread (and parent (gnus-id-to-thread parent))))
9347           (when thread
9348             (delq (assq header thread) thread))))
9349       ;; We have to really fetch the header to this article.
9350       (save-excursion
9351         (set-buffer nntp-server-buffer)
9352         (when (setq where (gnus-request-head id group))
9353           (nnheader-fold-continuation-lines)
9354           (goto-char (point-max))
9355           (insert ".\n")
9356           (goto-char (point-min))
9357           (insert "211 ")
9358           (princ (cond
9359                   ((numberp id) id)
9360                   ((cdr where) (cdr where))
9361                   (header (mail-header-number header))
9362                   (t gnus-reffed-article-number))
9363                  (current-buffer))
9364           (insert " Article retrieved.\n"))
9365         (if (or (not where)
9366                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9367             ()                          ; Malformed head.
9368           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9369             (when (and (stringp id)
9370                        (not (string= (gnus-group-real-name group)
9371                                      (car where))))
9372               ;; If we fetched by Message-ID and the article came
9373               ;; from a different group, we fudge some bogus article
9374               ;; numbers for this article.
9375               (mail-header-set-number header gnus-reffed-article-number))
9376             (save-excursion
9377               (set-buffer gnus-summary-buffer)
9378               (decf gnus-reffed-article-number)
9379               (gnus-remove-header (mail-header-number header))
9380               (push header gnus-newsgroup-headers)
9381               (setq gnus-current-headers header)
9382               (push (mail-header-number header) gnus-newsgroup-limit)))
9383           header)))))
9384
9385 (defun gnus-remove-header (number)
9386   "Remove header NUMBER from `gnus-newsgroup-headers'."
9387   (if (and gnus-newsgroup-headers
9388            (= number (mail-header-number (car gnus-newsgroup-headers))))
9389       (pop gnus-newsgroup-headers)
9390     (let ((headers gnus-newsgroup-headers))
9391       (while (and (cdr headers)
9392                   (not (= number (mail-header-number (cadr headers)))))
9393         (pop headers))
9394       (when (cdr headers)
9395         (setcdr headers (cddr headers))))))
9396
9397 ;;;
9398 ;;; summary highlights
9399 ;;;
9400
9401 (defun gnus-highlight-selected-summary ()
9402   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9403   ;; Highlight selected article in summary buffer
9404   (when gnus-summary-selected-face
9405     (save-excursion
9406       (let* ((beg (progn (beginning-of-line) (point)))
9407              (end (progn (end-of-line) (point)))
9408              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9409              (from (if (get-text-property beg gnus-mouse-face-prop)
9410                        beg
9411                      (or (next-single-property-change
9412                           beg gnus-mouse-face-prop nil end)
9413                          beg)))
9414              (to
9415               (if (= from end)
9416                   (- from 2)
9417                 (or (next-single-property-change
9418                      from gnus-mouse-face-prop nil end)
9419                     end))))
9420         ;; If no mouse-face prop on line we will have to = from = end,
9421         ;; so we highlight the entire line instead.
9422         (when (= (+ to 2) from)
9423           (setq from beg)
9424           (setq to end))
9425         (if gnus-newsgroup-selected-overlay
9426             ;; Move old overlay.
9427             (gnus-move-overlay
9428              gnus-newsgroup-selected-overlay from to (current-buffer))
9429           ;; Create new overlay.
9430           (gnus-overlay-put
9431            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9432            'face gnus-summary-selected-face))))))
9433
9434 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9435 (defun gnus-summary-highlight-line ()
9436   "Highlight current line according to `gnus-summary-highlight'."
9437   (let* ((list gnus-summary-highlight)
9438          (p (point))
9439          (end (progn (end-of-line) (point)))
9440          ;; now find out where the line starts and leave point there.
9441          (beg (progn (beginning-of-line) (point)))
9442          (article (gnus-summary-article-number))
9443          (score (or (cdr (assq (or article gnus-current-article)
9444                                gnus-newsgroup-scored))
9445                     gnus-summary-default-score 0))
9446          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9447          (inhibit-read-only t))
9448     ;; Eval the cars of the lists until we find a match.
9449     (let ((default gnus-summary-default-score))
9450       (while (and list
9451                   (not (eval (caar list))))
9452         (setq list (cdr list))))
9453     (let ((face (cdar list)))
9454       (unless (eq face (get-text-property beg 'face))
9455         (gnus-put-text-property-excluding-characters-with-faces
9456          beg end 'face
9457          (setq face (if (boundp face) (symbol-value face) face)))
9458         (when gnus-summary-highlight-line-function
9459           (funcall gnus-summary-highlight-line-function article face))))
9460     (goto-char p)))
9461
9462 (defun gnus-update-read-articles (group unread &optional compute)
9463   "Update the list of read articles in GROUP."
9464   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9465          (entry (gnus-gethash group gnus-newsrc-hashtb))
9466          (info (nth 2 entry))
9467          (prev 1)
9468          (unread (sort (copy-sequence unread) '<))
9469          read)
9470     (if (or (not info) (not active))
9471         ;; There is no info on this group if it was, in fact,
9472         ;; killed.  Gnus stores no information on killed groups, so
9473         ;; there's nothing to be done.
9474         ;; One could store the information somewhere temporarily,
9475         ;; perhaps...  Hmmm...
9476         ()
9477       ;; Remove any negative articles numbers.
9478       (while (and unread (< (car unread) 0))
9479         (setq unread (cdr unread)))
9480       ;; Remove any expired article numbers
9481       (while (and unread (< (car unread) (car active)))
9482         (setq unread (cdr unread)))
9483       ;; Compute the ranges of read articles by looking at the list of
9484       ;; unread articles.
9485       (while unread
9486         (when (/= (car unread) prev)
9487           (push (if (= prev (1- (car unread))) prev
9488                   (cons prev (1- (car unread))))
9489                 read))
9490         (setq prev (1+ (car unread)))
9491         (setq unread (cdr unread)))
9492       (when (<= prev (cdr active))
9493         (push (cons prev (cdr active)) read))
9494       (setq read (if (> (length read) 1) (nreverse read) read))
9495       (if compute
9496           read
9497         (save-excursion
9498           (set-buffer gnus-group-buffer)
9499           (gnus-undo-register
9500             `(progn
9501                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9502                (gnus-info-set-read ',info ',(gnus-info-read info))
9503                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9504                (gnus-group-update-group ,group t))))
9505        ;; Propagate the read marks to the backend.
9506        (if (gnus-check-backend-function 'request-set-mark group)
9507            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9508                  (add (gnus-remove-from-range read (gnus-info-read info))))
9509              (when (or add del)
9510                (unless (gnus-check-group group)
9511                  (error "Can't open server for %s" group))
9512                (gnus-request-set-mark
9513                 group (delq nil (list (if add (list add 'add '(read)))
9514                                       (if del (list del 'del '(read)))))))))
9515         ;; Enter this list into the group info.
9516         (gnus-info-set-read info read)
9517         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9518         (gnus-get-unread-articles-in-group info (gnus-active group))
9519         t))))
9520
9521 (defun gnus-offer-save-summaries ()
9522   "Offer to save all active summary buffers."
9523   (save-excursion
9524     (let ((buflist (buffer-list))
9525           buffers bufname)
9526       ;; Go through all buffers and find all summaries.
9527       (while buflist
9528         (and (setq bufname (buffer-name (car buflist)))
9529              (string-match "Summary" bufname)
9530              (save-excursion
9531                (set-buffer bufname)
9532                ;; We check that this is, indeed, a summary buffer.
9533                (and (eq major-mode 'gnus-summary-mode)
9534                     ;; Also make sure this isn't bogus.
9535                     gnus-newsgroup-prepared
9536                     ;; Also make sure that this isn't a dead summary buffer.
9537                     (not gnus-dead-summary-mode)))
9538              (push bufname buffers))
9539         (setq buflist (cdr buflist)))
9540       ;; Go through all these summary buffers and offer to save them.
9541       (when buffers
9542         (map-y-or-n-p
9543          "Update summary buffer %s? "
9544          (lambda (buf)
9545            (switch-to-buffer buf)
9546            (gnus-summary-exit))
9547          buffers)))))
9548
9549
9550 ;;; @ for mime-partial
9551 ;;;
9552
9553 (defun gnus-request-partial-message ()
9554   (save-excursion
9555     (let ((number (gnus-summary-article-number))
9556           (group gnus-newsgroup-name)
9557           (mother gnus-article-buffer))
9558       (set-buffer (get-buffer-create " *Partial Article*"))
9559       (erase-buffer)
9560       (setq mime-preview-buffer mother)
9561       (gnus-request-article-this-buffer number group)
9562       (mime-parse-buffer)
9563       )))
9564
9565 (autoload 'mime-combine-message/partial-pieces-automatically
9566   "mime-partial"
9567   "Internal method to combine message/partial messages automatically.")
9568
9569 (mime-add-condition
9570  'action '((type . message)(subtype . partial)
9571            (major-mode . gnus-original-article-mode)
9572            (method . mime-combine-message/partial-pieces-automatically)
9573            (summary-buffer-exp . gnus-summary-buffer)
9574            (request-partial-message-method . gnus-request-partial-message)
9575            ))
9576
9577
9578 ;;; @ for message/rfc822
9579 ;;;
9580
9581 (defun gnus-mime-extract-message/rfc822 (entity situation)
9582   (let (group article num cwin swin cur)
9583     (with-current-buffer (mime-entity-buffer entity)
9584       (save-restriction
9585         (narrow-to-region (mime-entity-body-start entity)
9586                           (mime-entity-body-end entity))
9587         (setq group (or (cdr (assq 'group situation))
9588                         (completing-read "Group: "
9589                                          gnus-active-hashtb
9590                                          nil
9591                                          (gnus-read-active-file-p)
9592                                          gnus-newsgroup-name))
9593               article (gnus-request-accept-article group)
9594               )
9595         ))
9596     (when (and (consp article)
9597                (numberp (setq article (cdr article))))
9598       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9599             cwin (get-buffer-window (current-buffer) t)
9600             )
9601       (save-window-excursion
9602         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9603             (select-window swin)
9604           (set-buffer gnus-summary-buffer)
9605           )
9606         (setq cur gnus-current-article)
9607         (forward-line num)
9608         (let (gnus-show-threads)
9609           (gnus-summary-goto-subject article t)
9610           )
9611         (gnus-summary-clear-mark-forward 1)
9612         (gnus-summary-goto-subject cur)
9613         )
9614       (when (and cwin (window-frame cwin))
9615         (select-frame (window-frame cwin))
9616         )
9617       (when (boundp 'mime-acting-situation-to-override)
9618         (set-alist 'mime-acting-situation-to-override
9619                    'group
9620                    group)
9621         (set-alist 'mime-acting-situation-to-override
9622                    'after-method
9623                    `(progn
9624                       (save-current-buffer
9625                         (set-buffer gnus-group-buffer)
9626                         (gnus-activate-group ,group)
9627                         )
9628                       (gnus-summary-goto-article ,cur
9629                                                  gnus-show-all-headers)
9630                       ))
9631         (set-alist 'mime-acting-situation-to-override
9632                    'number num)
9633         )
9634       )))
9635
9636 (mime-add-condition
9637  'action '((type . message)(subtype . rfc822)
9638            (major-mode . gnus-original-article-mode)
9639            (method . gnus-mime-extract-message/rfc822)
9640            (mode . "extract")
9641            ))
9642
9643 (mime-add-condition
9644  'action '((type . message)(subtype . news)
9645            (major-mode . gnus-original-article-mode)
9646            (method . gnus-mime-extract-message/rfc822)
9647            (mode . "extract")
9648            ))
9649
9650 (defun gnus-mime-extract-multipart (entity situation)
9651   (let ((children (mime-entity-children entity))
9652         mime-acting-situation-to-override
9653         f)
9654     (while children
9655       (mime-play-entity (car children)
9656                         (cons (assq 'mode situation)
9657                               mime-acting-situation-to-override))
9658       (setq children (cdr children)))
9659     (if (setq f (cdr (assq 'after-method
9660                            mime-acting-situation-to-override)))
9661         (eval f)
9662       )))  
9663
9664 (mime-add-condition
9665  'action '((type . multipart)
9666            (method . gnus-mime-extract-multipart)
9667            (mode . "extract")
9668            )
9669  'with-default)
9670
9671
9672 ;;; @ end
9673 ;;;
9674
9675 (defun gnus-summary-setup-default-charset ()
9676   "Setup newsgroup default charset."
9677   (if (equal gnus-newsgroup-name "nndraft:drafts")
9678       (setq gnus-newsgroup-charset nil)
9679   (let* ((name (and gnus-newsgroup-name
9680                    (gnus-group-real-name gnus-newsgroup-name)))
9681          (ignored-charsets 
9682           (or gnus-newsgroup-ephemeral-ignored-charsets
9683               (append
9684                (and gnus-newsgroup-name
9685                     (or (gnus-group-find-parameter gnus-newsgroup-name
9686                                                    'ignored-charsets t)
9687                         (let ((alist gnus-group-ignored-charsets-alist)
9688                            elem (charsets nil))
9689                           (while (setq elem (pop alist))
9690                             (when (and name
9691                                        (string-match (car elem) name))
9692                               (setq alist nil
9693                                     charsets (cdr elem))))
9694                           charsets))))
9695               gnus-newsgroup-ignored-charsets)))
9696     (setq gnus-newsgroup-charset
9697           (or gnus-newsgroup-ephemeral-charset
9698               (and gnus-newsgroup-name
9699                    (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9700                        (let ((alist gnus-group-charset-alist)
9701                              elem charset)
9702                          (while (setq elem (pop alist))
9703                            (when (and name
9704                                       (string-match (car elem) name))
9705                              (setq alist nil
9706                                    charset (cadr elem))))
9707                          charset)))
9708               gnus-default-charset))
9709     (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9710          ignored-charsets))))
9711
9712 ;;;
9713 ;;; Mime Commands
9714 ;;;
9715
9716 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9717   "Display the current article buffer fully MIME-buttonized.
9718 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9719 treated as multipart/mixed."
9720   (interactive "P")
9721   (require 'gnus-art)
9722   (let ((gnus-unbuttonized-mime-types nil)
9723         (gnus-mime-display-multipart-as-mixed show-all-parts))
9724     (gnus-summary-show-article)))
9725
9726 (defun gnus-summary-repair-multipart (article)
9727   "Add a Content-Type header to a multipart article without one."
9728   (interactive (list (gnus-summary-article-number)))
9729   (gnus-with-article article
9730     (message-narrow-to-head)
9731     (goto-char (point-max))
9732     (widen)
9733     (when (search-forward "\n--" nil t)
9734       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9735         (message-narrow-to-head)
9736         (message-remove-header "Mime-Version")
9737         (message-remove-header "Content-Type")
9738         (goto-char (point-max))
9739         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9740                         separator))
9741         (insert "Mime-Version: 1.0\n")
9742         (widen))))
9743   (let (gnus-mark-article-hook)
9744     (gnus-summary-select-article t t nil article)))
9745
9746 (defun gnus-summary-toggle-display-buttonized ()
9747   "Toggle the buttonizing of the article buffer."
9748   (interactive)
9749   (require 'gnus-art)
9750   (if (setq gnus-inhibit-mime-unbuttonizing
9751             (not gnus-inhibit-mime-unbuttonizing))
9752       (let ((gnus-unbuttonized-mime-types nil))
9753         (gnus-summary-show-article))
9754     (gnus-summary-show-article)))
9755
9756 ;;;
9757 ;;; Intelli-mouse commmands
9758 ;;;
9759
9760 (defun gnus-wheel-summary-scroll (event)
9761   (interactive "e")
9762   (let ((amount (if (memq 'shift (event-modifiers event))
9763                     (car gnus-wheel-scroll-amount)
9764                   (cdr gnus-wheel-scroll-amount)))
9765         (direction (- (* (static-if (featurep 'xemacs)
9766                              (event-button event)
9767                            (cond ((eq 'mouse-4 (event-basic-type event))
9768                                   4)
9769                                  ((eq 'mouse-5 (event-basic-type event))
9770                                   5)))
9771                          2) 9))
9772         edge)
9773     (gnus-summary-scroll-up (* amount direction))
9774     (when (gnus-eval-in-buffer-window gnus-article-buffer
9775             (save-restriction
9776               (widen)
9777               (and (if (< 0 direction)
9778                        (gnus-article-next-page 0)
9779                      (gnus-article-prev-page 0)
9780                      (bobp))
9781                    (if (setq edge (get-text-property
9782                                    (point-min) 'gnus-wheel-edge))
9783                        (setq edge (* edge direction))
9784                      (setq edge -1))
9785                    (or (plusp edge)
9786                        (let ((buffer-read-only nil)
9787                              (inhibit-read-only t))
9788                          (put-text-property (point-min) (point-max)
9789                                             'gnus-wheel-edge direction)
9790                          nil))
9791                    (or (> edge gnus-wheel-edge-resistance)
9792                        (let ((buffer-read-only nil)
9793                              (inhibit-read-only t))
9794                          (put-text-property (point-min) (point-max)
9795                                             'gnus-wheel-edge
9796                                             (* (1+ edge) direction))
9797                          nil))
9798                    (eq last-command 'gnus-wheel-summary-scroll))
9799               ))
9800       (gnus-summary-next-article nil nil (minusp direction)))
9801     ))
9802
9803 (defun gnus-wheel-install ()
9804   "Enable mouse wheel support on summary window."
9805   (when gnus-use-wheel
9806     (let ((keys 
9807            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9808       (dolist (key keys)
9809         (define-key gnus-summary-mode-map key
9810           'gnus-wheel-summary-scroll)))))
9811
9812 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9813
9814 ;;;
9815 ;;; with article
9816 ;;;
9817
9818 (defmacro gnus-with-article (article &rest forms)
9819   "Select ARTICLE and perform FORMS in the original article buffer.
9820 Then replace the article with the result."
9821   `(progn
9822      ;; We don't want the article to be marked as read.
9823      (let (gnus-mark-article-hook)
9824        (gnus-summary-select-article t t nil ,article))
9825      (set-buffer gnus-original-article-buffer)
9826      ,@forms
9827      (if (not (gnus-check-backend-function
9828                'request-replace-article (car gnus-article-current)))
9829          (gnus-message 5 "Read-only group; not replacing")
9830        (unless (gnus-request-replace-article
9831                 ,article (car gnus-article-current)
9832                 (current-buffer) t)
9833          (error "Couldn't replace article")))
9834      ;; The cache and backlog have to be flushed somewhat.
9835      (when gnus-keep-backlog
9836        (gnus-backlog-remove-article
9837         (car gnus-article-current) (cdr gnus-article-current)))
9838      (when gnus-use-cache
9839        (gnus-cache-update-article
9840         (car gnus-article-current) (cdr gnus-article-current)))))
9841
9842 (put 'gnus-with-article 'lisp-indent-function 1)
9843 (put 'gnus-with-article 'edebug-form-spec '(form body))
9844
9845 ;;;
9846 ;;; Generic summary marking commands
9847 ;;;
9848
9849 (defvar gnus-summary-marking-alist
9850   '((read gnus-del-mark "d")
9851     (unread gnus-unread-mark "u")
9852     (ticked gnus-ticked-mark "!")
9853     (dormant gnus-dormant-mark "?")
9854     (expirable gnus-expirable-mark "e"))
9855   "An alist of names/marks/keystrokes.")
9856
9857 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9858 (defvar gnus-summary-mark-map)
9859
9860 (defun gnus-summary-make-all-marking-commands ()
9861   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9862   (dolist (elem gnus-summary-marking-alist)
9863     (apply 'gnus-summary-make-marking-command elem)))
9864
9865 (defun gnus-summary-make-marking-command (name mark keystroke)
9866   (let ((map (make-sparse-keymap)))
9867     (define-key gnus-summary-generic-mark-map keystroke map)
9868     (dolist (lway `((next "next" next nil "n")
9869                     (next-unread "next unread" next t "N")
9870                     (prev "previous" prev nil "p")
9871                     (prev-unread "previous unread" prev t "P")
9872                     (nomove "" nil nil ,keystroke)))
9873       (let ((func (gnus-summary-make-marking-command-1
9874                    mark (car lway) lway name)))
9875         (setq func (eval func))
9876         (define-key map (nth 4 lway) func)))))
9877       
9878 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9879   `(defun ,(intern
9880             (format "gnus-summary-put-mark-as-%s%s"
9881                     name (if (eq way 'nomove)
9882                              ""
9883                            (concat "-" (symbol-name way)))))
9884      (n)
9885      ,(format
9886        "Mark the current article as %s%s.
9887 If N, the prefix, then repeat N times.
9888 If N is negative, move in reverse order.
9889 The difference between N and the actual number of articles marked is
9890 returned."
9891        name (car (cdr lway)))
9892      (interactive "p")
9893      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9894     
9895 (defun gnus-summary-generic-mark (n mark move unread)
9896   "Mark N articles with MARK."
9897   (unless (eq major-mode 'gnus-summary-mode)
9898     (error "This command can only be used in the summary buffer"))
9899   (gnus-summary-show-thread)
9900   (let ((nummove
9901          (cond
9902           ((eq move 'next) 1)
9903           ((eq move 'prev) -1)
9904           (t 0))))
9905     (if (zerop nummove)
9906         (setq n 1)
9907       (when (< n 0)
9908         (setq n (abs n)
9909               nummove (* -1 nummove))))
9910     (while (and (> n 0)
9911                 (gnus-summary-mark-article nil mark)
9912                 (zerop (gnus-summary-next-subject nummove unread t)))
9913       (setq n (1- n)))
9914     (when (/= 0 n)
9915       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9916     (gnus-summary-recenter)
9917     (gnus-summary-position-point)
9918     (gnus-set-mode-line 'summary)
9919     n))
9920
9921 (gnus-summary-make-all-marking-commands)
9922
9923 (gnus-ems-redefine)
9924
9925 (provide 'gnus-sum)
9926
9927 (run-hooks 'gnus-sum-load-hook)
9928
9929 ;;; gnus-sum.el ends here