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