Sync with `t-gnus-6_14'
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mime-view)
41
42 (eval-when-compile
43   (require 'mime-play)
44   (require 'static))
45
46 (eval-and-compile
47   (autoload 'gnus-cache-articles-in-group "gnus-cache")
48   (autoload 'pgg-decrypt-region "pgg" nil t)
49   (autoload 'pgg-verify-region "pgg" nil t))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, many commands will go to the next unread article.
186 This applies to marking commands as well as other commands that
187 \"naturally\" select the next article, like, for instance, `SPC' at
188 the end of an article.
189
190 If nil, the marking commands do NOT go to the next unread article
191 (they go to the next article instead).  If `never', commands that
192 usually go to the next unread article, will go to the next article,
193 whether it is read or not."
194   :group 'gnus-summary-marks
195   :link '(custom-manual "(gnus)Setting Marks")
196   :type '(choice (const :tag "off" nil)
197                  (const never)
198                  (sexp :menu-tag "on" t)))
199
200 (defcustom gnus-summary-default-score 0
201   "*Default article score level.
202 All scores generated by the score files will be added to this score.
203 If this variable is nil, scoring will be disabled."
204   :group 'gnus-score-default
205   :type '(choice (const :tag "disable")
206                  integer))
207
208 (defcustom gnus-summary-zcore-fuzz 0
209   "*Fuzziness factor for the zcore in the summary buffer.
210 Articles with scores closer than this to `gnus-summary-default-score'
211 will not be marked."
212   :group 'gnus-summary-format
213   :type 'integer)
214
215 (defcustom gnus-simplify-subject-fuzzy-regexp nil
216   "*Strings to be removed when doing fuzzy matches.
217 This can either be a regular expression or list of regular expressions
218 that will be removed from subject strings if fuzzy subject
219 simplification is selected."
220   :group 'gnus-thread
221   :type '(repeat regexp))
222
223 (defcustom gnus-show-threads t
224   "*If non-nil, display threads in summary mode."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-subtree nil
229   "*If non-nil, hide all threads initially.
230 If threads are hidden, you have to run the command
231 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
232 to expose hidden threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-killed t
237   "*If non-nil, hide killed threads automatically."
238   :group 'gnus-thread
239   :type 'boolean)
240
241 (defcustom gnus-thread-ignore-subject t
242   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
243 If nil, articles that have different subjects from their parents will
244 start separate threads."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-operation-ignore-subject t
249   "*If non-nil, subjects will be ignored when doing thread commands.
250 This affects commands like `gnus-summary-kill-thread' and
251 `gnus-summary-lower-thread'.
252
253 If this variable is nil, articles in the same thread with different
254 subjects will not be included in the operation in question.  If this
255 variable is `fuzzy', only articles that have subjects that are fuzzily
256 equal will be included."
257   :group 'gnus-thread
258   :type '(choice (const :tag "off" nil)
259                  (const fuzzy)
260                  (sexp :tag "on" t)))
261
262 (defcustom gnus-thread-indent-level 4
263   "*Number that says how much each sub-thread should be indented."
264   :group 'gnus-thread
265   :type 'integer)
266
267 (defcustom gnus-auto-extend-newsgroup t
268   "*If non-nil, extend newsgroup forward and backward when requested."
269   :group 'gnus-summary-choose
270   :type 'boolean)
271
272 (defcustom gnus-auto-select-first t
273   "*If nil, don't select the first unread article when entering a group.
274 If this variable is `best', select the highest-scored unread article
275 in the group.  If t, select the first unread article.
276
277 This variable can also be a function to place point on a likely
278 subject line.  Useful values include `gnus-summary-first-unread-subject',
279 `gnus-summary-first-unread-article' and
280 `gnus-summary-best-unread-article'.
281
282 If you want to prevent automatic selection of the first unread article
283 in some newsgroups, set the variable to nil in
284 `gnus-select-group-hook'."
285   :group 'gnus-group-select
286   :type '(choice (const :tag "none" nil)
287                  (const best)
288                  (sexp :menu-tag "first" t)
289                  (function-item gnus-summary-first-unread-subject)
290                  (function-item gnus-summary-first-unread-article)
291                  (function-item gnus-summary-best-unread-article)))
292
293 (defcustom gnus-dont-select-after-jump-to-other-group nil
294   "If non-nil, don't select the first unread article after entering the
295 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
296 it is depend on the value of `gnus-auto-select-first' whether to select
297 or not."
298   :group 'gnus-group-select
299   :type 'boolean)
300
301 (defcustom gnus-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-show-mime t
368   "*If non-nil, do mime processing of articles.
369 The articles will simply be fed to the function given by
370 `gnus-article-display-method-for-mime'."
371   :group 'gnus-article-mime
372   :type 'boolean)
373
374 (defcustom gnus-move-split-methods nil
375   "*Variable used to suggest where articles are to be moved to.
376 It uses the same syntax as the `gnus-split-methods' variable."
377   :group 'gnus-summary-mail
378   :type '(repeat (choice (list :value (fun) function)
379                          (cons :value ("" "") regexp (repeat string))
380                          (sexp :value nil))))
381
382 (defcustom gnus-unread-mark ?  ;Whitespace
383   "*Mark used for unread articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-ticked-mark ?!
388   "*Mark used for ticked articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-dormant-mark ??
393   "*Mark used for dormant articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-del-mark ?r
398   "*Mark used for del'd articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-read-mark ?R
403   "*Mark used for read articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-expirable-mark ?E
408   "*Mark used for expirable articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-killed-mark ?K
413   "*Mark used for killed articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-souped-mark ?F
418   "*Mark used for killed articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-kill-file-mark ?X
423   "*Mark used for articles killed by kill files."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-low-score-mark ?Y
428   "*Mark used for articles with a low score."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-catchup-mark ?C
433   "*Mark used for articles that are caught up."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-replied-mark ?A
438   "*Mark used for articles that have been replied to."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-cached-mark ?*
443   "*Mark used for articles that are in the cache."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-saved-mark ?S
448   "*Mark used for articles that have been saved to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ancient-mark ?O
453   "*Mark used for ancient articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-sparse-mark ?Q
458   "*Mark used for sparsely reffed articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-canceled-mark ?G
463   "*Mark used for canceled articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-duplicate-mark ?M
468   "*Mark used for duplicate articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-undownloaded-mark ?@
473   "*Mark used for articles that weren't downloaded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-downloadable-mark ?%
478   "*Mark used for articles that are to be downloaded."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unsendable-mark ?=
483   "*Mark used for articles that won't be sent."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-score-over-mark ?+
488   "*Score mark used for articles with high scores."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-score-below-mark ?-
493   "*Score mark used for articles with low scores."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-empty-thread-mark ?  ;Whitespace
498   "*There is no thread under the article."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-not-empty-thread-mark ?=
503   "*There is a thread under the article."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-view-pseudo-asynchronously nil
508   "*If non-nil, Gnus will view pseudo-articles asynchronously."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-auto-expirable-marks
513   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
514         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
515         gnus-souped-mark gnus-duplicate-mark)
516   "*The list of marks converted into expiration if a group is auto-expirable."
517   :group 'gnus-summary
518   :type '(repeat character))
519
520 (defcustom gnus-inhibit-user-auto-expire t
521   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
522   :group 'gnus-summary
523   :type 'boolean)
524
525 (defcustom gnus-view-pseudos nil
526   "*If `automatic', pseudo-articles will be viewed automatically.
527 If `not-confirm', pseudos will be viewed automatically, and the user
528 will not be asked to confirm the command."
529   :group 'gnus-extract-view
530   :type '(choice (const :tag "off" nil)
531                  (const automatic)
532                  (const not-confirm)))
533
534 (defcustom gnus-view-pseudos-separately t
535   "*If non-nil, one pseudo-article will be created for each file to be viewed.
536 If nil, all files that use the same viewing command will be given as a
537 list of parameters to that command."
538   :group 'gnus-extract-view
539   :type 'boolean)
540
541 (defcustom gnus-insert-pseudo-articles t
542   "*If non-nil, insert pseudo-articles when decoding articles."
543   :group 'gnus-extract-view
544   :type 'boolean)
545
546 (defcustom gnus-summary-dummy-line-format
547   "  %(:                          :%) %S\n"
548   "*The format specification for the dummy roots in the summary buffer.
549 It works along the same lines as a normal formatting string,
550 with some simple extensions.
551
552 %S  The subject"
553   :group 'gnus-threading
554   :type 'string)
555
556 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
557   "*The format specification for the summary mode line.
558 It works along the same lines as a normal formatting string,
559 with some simple extensions:
560
561 %G  Group name
562 %p  Unprefixed group name
563 %A  Current article number
564 %z  Current article score
565 %V  Gnus version
566 %U  Number of unread articles in the group
567 %e  Number of unselected articles in the group
568 %Z  A string with unread/unselected article counts
569 %g  Shortish group name
570 %S  Subject of the current article
571 %u  User-defined spec
572 %s  Current score file name
573 %d  Number of dormant articles
574 %r  Number of articles that have been marked as read in this session
575 %E  Number of articles expunged by the score files"
576   :group 'gnus-summary-format
577   :type 'string)
578
579 (defcustom gnus-list-identifiers nil
580   "Regexp that matches list identifiers to be removed from subject.
581 This can also be a list of regexps."
582   :group 'gnus-summary-format
583   :group 'gnus-article-hiding
584   :type '(choice (const :tag "none" nil)
585                  (regexp :value ".*")
586                  (repeat :value (".*") regexp)))
587
588 (defcustom gnus-summary-mark-below 0
589   "*Mark all articles with a score below this variable as read.
590 This variable is local to each summary buffer and usually set by the
591 score file."
592   :group 'gnus-score-default
593   :type 'integer)
594
595 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
596   "*List of functions used for sorting articles in the summary buffer.
597 This variable is only used when not using a threaded display."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-article-sort-by-number)
600                          (function-item gnus-article-sort-by-author)
601                          (function-item gnus-article-sort-by-subject)
602                          (function-item gnus-article-sort-by-date)
603                          (function-item gnus-article-sort-by-score)
604                          (function :tag "other"))))
605
606 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
607   "*List of functions used for sorting threads in the summary buffer.
608 By default, threads are sorted by article number.
609
610 Each function takes two threads and return non-nil if the first thread
611 should be sorted before the other.  If you use more than one function,
612 the primary sort function should be the last.  You should probably
613 always include `gnus-thread-sort-by-number' in the list of sorting
614 functions -- preferably first.
615
616 Ready-made functions include `gnus-thread-sort-by-number',
617 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
618 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
619 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
620   :group 'gnus-summary-sort
621   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
622                          (function-item gnus-thread-sort-by-author)
623                          (function-item gnus-thread-sort-by-subject)
624                          (function-item gnus-thread-sort-by-date)
625                          (function-item gnus-thread-sort-by-score)
626                          (function-item gnus-thread-sort-by-total-score)
627                          (function :tag "other"))))
628
629 (defcustom gnus-thread-score-function '+
630   "*Function used for calculating the total score of a thread.
631
632 The function is called with the scores of the article and each
633 subthread and should then return the score of the thread.
634
635 Some functions you can use are `+', `max', or `min'."
636   :group 'gnus-summary-sort
637   :type 'function)
638
639 (defcustom gnus-summary-expunge-below nil
640   "All articles that have a score less than this variable will be expunged.
641 This variable is local to the summary buffers."
642   :group 'gnus-score-default
643   :type '(choice (const :tag "off" nil)
644                  integer))
645
646 (defcustom gnus-thread-expunge-below nil
647   "All threads that have a total score less than this variable will be expunged.
648 See `gnus-thread-score-function' for en explanation of what a
649 \"thread score\" is.
650
651 This variable is local to the summary buffers."
652   :group 'gnus-threading
653   :group 'gnus-score-default
654   :type '(choice (const :tag "off" nil)
655                  integer))
656
657 (defcustom gnus-summary-mode-hook nil
658   "*A hook for Gnus summary mode.
659 This hook is run before any variables are set in the summary buffer."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-menu-hook nil
664   "*Hook run after the creation of the summary mode menu."
665   :group 'gnus-summary-visual
666   :type 'hook)
667
668 (defcustom gnus-summary-exit-hook nil
669   "*A hook called on exit from the summary buffer.
670 It will be called with point in the group buffer."
671   :group 'gnus-summary-exit
672   :type 'hook)
673
674 (defcustom gnus-summary-prepare-hook nil
675   "*A hook called after the summary buffer has been generated.
676 If you want to modify the summary buffer, you can use this hook."
677   :group 'gnus-summary-various
678   :type 'hook)
679
680 (defcustom gnus-summary-prepared-hook nil
681   "*A hook called as the last thing after the summary buffer has been generated."
682   :group 'gnus-summary-various
683   :type 'hook)
684
685 (defcustom gnus-summary-generate-hook nil
686   "*A hook run just before generating the summary buffer.
687 This hook is commonly used to customize threading variables and the
688 like."
689   :group 'gnus-summary-various
690   :type 'hook)
691
692 (defcustom gnus-select-group-hook nil
693   "*A hook called when a newsgroup is selected.
694
695 If you'd like to simplify subjects like the
696 `gnus-summary-next-same-subject' command does, you can use the
697 following hook:
698
699  (setq gnus-select-group-hook
700       (list
701         (lambda ()
702           (mapcar (lambda (header)
703                      (mail-header-set-subject
704                       header
705                       (gnus-simplify-subject
706                        (mail-header-subject header) 're-only)))
707                   gnus-newsgroup-headers))))"
708   :group 'gnus-group-select
709   :type 'hook)
710
711 (defcustom gnus-select-article-hook nil
712   "*A hook called when an article is selected."
713   :group 'gnus-summary-choose
714   :type 'hook)
715
716 (defcustom gnus-visual-mark-article-hook
717   (list 'gnus-highlight-selected-summary)
718   "*Hook run after selecting an article in the summary buffer.
719 It is meant to be used for highlighting the article in some way.  It
720 is not run if `gnus-visual' is nil."
721   :group 'gnus-summary-visual
722   :type 'hook)
723
724 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
725   "*A hook called before parsing the headers."
726   :group 'gnus-various
727   :type 'hook)
728
729 (defcustom gnus-exit-group-hook nil
730   "*A hook called when exiting summary mode.
731 This hook is not called from the non-updating exit commands like `Q'."
732   :group 'gnus-various
733   :type 'hook)
734
735 (defcustom gnus-summary-update-hook
736   (list 'gnus-summary-highlight-line)
737   "*A hook called when a summary line is changed.
738 The hook will not be called if `gnus-visual' is nil.
739
740 The default function `gnus-summary-highlight-line' will
741 highlight the line according to the `gnus-summary-highlight'
742 variable."
743   :group 'gnus-summary-visual
744   :type 'hook)
745
746 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
747   "*A hook called when an article is selected for the first time.
748 The hook is intended to mark an article as read (or unread)
749 automatically when it is selected."
750   :group 'gnus-summary-choose
751   :type 'hook)
752
753 (defcustom gnus-group-no-more-groups-hook nil
754   "*A hook run when returning to group mode having no more (unread) groups."
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-ps-print-hook nil
759   "*A hook run before ps-printing something from Gnus."
760   :group 'gnus-summary
761   :type 'hook)
762
763 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
764   "Face used for highlighting the current article in the summary buffer."
765   :group 'gnus-summary-visual
766   :type 'face)
767
768 (defcustom gnus-summary-highlight
769   '(((= mark gnus-canceled-mark)
770      . gnus-summary-cancelled-face)
771     ((and (> score default)
772           (or (= mark gnus-dormant-mark)
773               (= mark gnus-ticked-mark)))
774      . gnus-summary-high-ticked-face)
775     ((and (< score default)
776           (or (= mark gnus-dormant-mark)
777               (= mark gnus-ticked-mark)))
778      . gnus-summary-low-ticked-face)
779     ((or (= mark gnus-dormant-mark)
780          (= mark gnus-ticked-mark))
781      . gnus-summary-normal-ticked-face)
782     ((and (> score default) (= mark gnus-ancient-mark))
783      . gnus-summary-high-ancient-face)
784     ((and (< score default) (= mark gnus-ancient-mark))
785      . gnus-summary-low-ancient-face)
786     ((= mark gnus-ancient-mark)
787      . gnus-summary-normal-ancient-face)
788     ((and (> score default) (= mark gnus-unread-mark))
789      . gnus-summary-high-unread-face)
790     ((and (< score default) (= mark gnus-unread-mark))
791      . gnus-summary-low-unread-face)
792     ((and (memq article gnus-newsgroup-incorporated) 
793           (= mark gnus-unread-mark))
794      . gnus-summary-incorporated-face)
795     ((= mark gnus-unread-mark)
796      . gnus-summary-normal-unread-face)
797     ((and (> score default) (memq mark (list gnus-downloadable-mark
798                                              gnus-undownloaded-mark)))
799      . gnus-summary-high-unread-face)
800     ((and (< score default) (memq mark (list gnus-downloadable-mark
801                                              gnus-undownloaded-mark)))
802      . gnus-summary-low-unread-face)
803     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
804      . gnus-summary-normal-unread-face)
805     ((> score default)
806      . gnus-summary-high-read-face)
807     ((< score default)
808      . gnus-summary-low-read-face)
809     (t
810      . gnus-summary-normal-read-face))
811   "*Controls the highlighting of summary buffer lines.
812
813 A list of (FORM . FACE) pairs.  When deciding how a a particular
814 summary line should be displayed, each form is evaluated.  The content
815 of the face field after the first true form is used.  You can change
816 how those summary lines are displayed, by editing the face field.
817
818 You can use the following variables in the FORM field.
819
820 score:   The articles score
821 default: The default article score.
822 below:   The score below which articles are automatically marked as read.
823 mark:    The articles mark."
824   :group 'gnus-summary-visual
825   :type '(repeat (cons (sexp :tag "Form" nil)
826                        face)))
827
828 (defcustom gnus-alter-header-function nil
829   "Function called to allow alteration of article header structures.
830 The function is called with one parameter, the article header vector,
831 which it may alter in any way.")
832
833 (defvar gnus-decode-encoded-word-function
834   (mime-find-field-decoder 'From 'nov)
835   "Variable that says which function should be used to decode a string with encoded words.")
836
837 (defcustom gnus-extra-headers nil
838   "*Extra headers to parse."
839   :group 'gnus-summary
840   :type '(repeat symbol))
841
842 (defcustom gnus-ignored-from-addresses
843   (and user-mail-address (regexp-quote user-mail-address))
844   "*Regexp of From headers that may be suppressed in favor of To headers."
845   :group 'gnus-summary
846   :type 'regexp)
847
848 (defcustom gnus-group-charset-alist
849   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
850     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
851     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
852     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
853     ("^relcom\\>" koi8-r)
854     ("^fido7\\>" koi8-r)
855     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
856     ("^israel\\>" iso-8859-1)
857     ("^han\\>" euc-kr)
858     ("^alt.chinese.text.big5\\>" chinese-big5)
859     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
860     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
861     (".*" iso-8859-1))
862   "Alist of regexps (to match group names) and default charsets to be used when reading."
863   :type '(repeat (list (regexp :tag "Group")
864                        (symbol :tag "Charset")))
865   :group 'gnus-charset)
866
867 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
868   "List of charsets that should be ignored.
869 When these charsets are used in the \"charset\" parameter, the
870 default charset will be used instead."
871   :type '(repeat symbol)
872   :group 'gnus-charset)
873
874 (defcustom gnus-group-ignored-charsets-alist 
875   '(("alt\\.chinese\\.text" iso-8859-1))
876   "Alist of regexps (to match group names) and charsets that should be ignored.
877 When these charsets are used in the \"charset\" parameter, the
878 default charset will be used instead."
879   :type '(repeat (cons (regexp :tag "Group")
880                        (repeat symbol)))
881   :group 'gnus-charset)
882
883 (defcustom gnus-group-highlight-words-alist nil
884   "Alist of group regexps and highlight regexps.
885 This variable uses the same syntax as `gnus-emphasis-alist'."
886   :type '(repeat (cons (regexp :tag "Group")
887                        (repeat (list (regexp :tag "Highlight regexp")
888                                      (number :tag "Group for entire word" 0)
889                                      (number :tag "Group for displayed part" 0)
890                                      (symbol :tag "Face" 
891                                              gnus-emphasis-highlight-words)))))
892   :group 'gnus-summary-visual)
893
894 (defcustom gnus-use-wheel nil
895   "Use Intelli-mouse on summary movement"
896   :type 'boolean
897   :group 'gnus-summary-maneuvering)
898
899 (defcustom gnus-wheel-scroll-amount '(5 . 1)
900   "Amount to scroll messages by spinning the mouse wheel.
901 This is actually a cons cell, where the first item is the amount to scroll
902 on a normal wheel event, and the second is the amount to scroll when the
903 wheel is moved with the shift key depressed."
904   :type '(cons (integer :tag "Shift") integer)
905   :group 'gnus-summary-maneuvering)
906
907 (defcustom gnus-wheel-edge-resistance 2
908   "How hard it should be to change the current article
909 by moving the mouse over the edge of the article window."
910   :type 'integer
911   :group 'gnus-summary-maneuvering)
912
913 (defcustom gnus-summary-show-article-charset-alist
914   nil
915   "Alist of number and charset.
916 The article will be shown with the charset corresponding to the
917 numbered argument.
918 For example: ((1 . cn-gb-2312) (2 . big5))."
919   :type '(repeat (cons (number :tag "Argument" 1)
920                        (symbol :tag "Charset")))
921   :group 'gnus-charset)
922
923 (defcustom gnus-preserve-marks t
924   "Whether marks are preserved when moving, copying and respooling messages."
925   :type 'boolean
926   :group 'gnus-summary-marks)
927
928 (defcustom gnus-alter-articles-to-read-function nil
929   "Function to be called to alter the list of articles to be selected."
930   :type 'function
931   :group 'gnus-summary)
932
933 ;;; Internal variables
934
935 (defvar gnus-article-mime-handles nil)
936 (defvar gnus-article-decoded-p nil)
937 (defvar gnus-scores-exclude-files nil)
938 (defvar gnus-page-broken nil)
939 (defvar gnus-inhibit-mime-unbuttonizing nil)
940
941 (defvar gnus-original-article nil)
942 (defvar gnus-article-internal-prepare-hook nil)
943 (defvar gnus-newsgroup-process-stack nil)
944
945 (defvar gnus-thread-indent-array nil)
946 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
947 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
948   "Function called to sort the articles within a thread after it has been gathered together.")
949
950 ;; Avoid highlighting in kill files.
951 (defvar gnus-summary-inhibit-highlight nil)
952 (defvar gnus-newsgroup-selected-overlay nil)
953 (defvar gnus-inhibit-limiting nil)
954 (defvar gnus-newsgroup-adaptive-score-file nil)
955 (defvar gnus-current-score-file nil)
956 (defvar gnus-current-move-group nil)
957 (defvar gnus-current-copy-group nil)
958 (defvar gnus-current-crosspost-group nil)
959
960 (defvar gnus-newsgroup-dependencies nil)
961 (defvar gnus-newsgroup-adaptive nil)
962 (defvar gnus-summary-display-article-function nil)
963 (defvar gnus-summary-highlight-line-function nil
964   "Function called after highlighting a summary line.")
965
966 (defvar gnus-summary-line-format-alist
967   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
968     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
969     (?s gnus-tmp-subject-or-nil ?s)
970     (?n gnus-tmp-name ?s)
971     (?A (std11-address-string
972          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
973     (?a (or (std11-full-name-string
974              (car (mime-entity-read-field gnus-tmp-header 'From)))
975             gnus-tmp-from) ?s)
976     (?F gnus-tmp-from ?s)
977     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
978     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
979     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
980     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
981     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
982     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
983     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
984     (?L gnus-tmp-lines ?d)
985     (?I gnus-tmp-indentation ?s)
986     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
987     (?R gnus-tmp-replied ?c)
988     (?\[ gnus-tmp-opening-bracket ?c)
989     (?\] gnus-tmp-closing-bracket ?c)
990     (?\> (make-string gnus-tmp-level ? ) ?s)
991     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
992     (?i gnus-tmp-score ?d)
993     (?z gnus-tmp-score-char ?c)
994     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
995     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
996     (?U gnus-tmp-unread ?c)
997     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
998     (?t (gnus-summary-number-of-articles-in-thread
999          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1000         ?d)
1001     (?e (gnus-summary-number-of-articles-in-thread
1002          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1003         ?c)
1004     (?u gnus-tmp-user-defined ?s)
1005     (?P (gnus-pick-line-number) ?d))
1006   "An alist of format specifications that can appear in summary lines,
1007 and what variables they correspond with, along with the type of the
1008 variable (string, integer, character, etc).")
1009
1010 (defvar gnus-summary-dummy-line-format-alist
1011   `((?S gnus-tmp-subject ?s)
1012     (?N gnus-tmp-number ?d)
1013     (?u gnus-tmp-user-defined ?s)))
1014
1015 (defvar gnus-summary-mode-line-format-alist
1016   `((?G gnus-tmp-group-name ?s)
1017     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1018     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1019     (?A gnus-tmp-article-number ?d)
1020     (?Z gnus-tmp-unread-and-unselected ?s)
1021     (?V gnus-version ?s)
1022     (?U gnus-tmp-unread-and-unticked ?d)
1023     (?S gnus-tmp-subject ?s)
1024     (?e gnus-tmp-unselected ?d)
1025     (?u gnus-tmp-user-defined ?s)
1026     (?d (length gnus-newsgroup-dormant) ?d)
1027     (?t (length gnus-newsgroup-marked) ?d)
1028     (?r (length gnus-newsgroup-reads) ?d)
1029     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1030     (?E gnus-newsgroup-expunged-tally ?d)
1031     (?s (gnus-current-score-file-nondirectory) ?s)))
1032
1033 (defvar gnus-last-search-regexp nil
1034   "Default regexp for article search command.")
1035
1036 (defvar gnus-summary-search-article-matched-data nil
1037   "Last matched data of article search command.  It is the local variable
1038 in `gnus-article-buffer' which consists of the list of start position,
1039 end position and text.")
1040
1041 (defvar gnus-last-shell-command nil
1042   "Default shell command on article.")
1043
1044 (defvar gnus-newsgroup-begin nil)
1045 (defvar gnus-newsgroup-end nil)
1046 (defvar gnus-newsgroup-last-rmail nil)
1047 (defvar gnus-newsgroup-last-mail nil)
1048 (defvar gnus-newsgroup-last-folder nil)
1049 (defvar gnus-newsgroup-last-file nil)
1050 (defvar gnus-newsgroup-auto-expire nil)
1051 (defvar gnus-newsgroup-active nil)
1052
1053 (defvar gnus-newsgroup-data nil)
1054 (defvar gnus-newsgroup-data-reverse nil)
1055 (defvar gnus-newsgroup-limit nil)
1056 (defvar gnus-newsgroup-limits nil)
1057
1058 (defvar gnus-newsgroup-unreads nil
1059   "List of unread articles in the current newsgroup.")
1060
1061 (defvar gnus-newsgroup-unselected nil
1062   "List of unselected unread articles in the current newsgroup.")
1063
1064 (defvar gnus-newsgroup-reads nil
1065   "Alist of read articles and article marks in the current newsgroup.")
1066
1067 (defvar gnus-newsgroup-expunged-tally nil)
1068
1069 (defvar gnus-newsgroup-marked nil
1070   "List of ticked articles in the current newsgroup (a subset of unread art).")
1071
1072 (defvar gnus-newsgroup-killed nil
1073   "List of ranges of articles that have been through the scoring process.")
1074
1075 (defvar gnus-newsgroup-cached nil
1076   "List of articles that come from the article cache.")
1077
1078 (defvar gnus-newsgroup-saved nil
1079   "List of articles that have been saved.")
1080
1081 (defvar gnus-newsgroup-kill-headers nil)
1082
1083 (defvar gnus-newsgroup-replied nil
1084   "List of articles that have been replied to in the current newsgroup.")
1085
1086 (defvar gnus-newsgroup-expirable nil
1087   "List of articles in the current newsgroup that can be expired.")
1088
1089 (defvar gnus-newsgroup-processable nil
1090   "List of articles in the current newsgroup that can be processed.")
1091
1092 (defvar gnus-newsgroup-downloadable nil
1093   "List of articles in the current newsgroup that can be processed.")
1094
1095 (defvar gnus-newsgroup-undownloaded nil
1096   "List of articles in the current newsgroup that haven't been downloaded..")
1097
1098 (defvar gnus-newsgroup-unsendable nil
1099   "List of articles in the current newsgroup that won't be sent.")
1100
1101 (defvar gnus-newsgroup-bookmarks nil
1102   "List of articles in the current newsgroup that have bookmarks.")
1103
1104 (defvar gnus-newsgroup-dormant nil
1105   "List of dormant articles in the current newsgroup.")
1106
1107 (defvar gnus-newsgroup-scored nil
1108   "List of scored articles in the current newsgroup.")
1109
1110 (defvar gnus-newsgroup-incorporated nil
1111   "List of incorporated articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-headers nil
1114   "List of article headers in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-threads nil)
1117
1118 (defvar gnus-newsgroup-prepared nil
1119   "Whether the current group has been prepared properly.")
1120
1121 (defvar gnus-newsgroup-ancient nil
1122   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1123
1124 (defvar gnus-newsgroup-sparse nil)
1125
1126 (defvar gnus-current-article nil)
1127 (defvar gnus-article-current nil)
1128 (defvar gnus-current-headers nil)
1129 (defvar gnus-have-all-headers nil)
1130 (defvar gnus-last-article nil)
1131 (defvar gnus-newsgroup-history nil)
1132 (defvar gnus-newsgroup-charset nil)
1133 (defvar gnus-newsgroup-ephemeral-charset nil)
1134 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1135
1136 (defconst gnus-summary-local-variables
1137   '(gnus-newsgroup-name
1138     gnus-newsgroup-begin gnus-newsgroup-end
1139     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1140     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1141     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1142     gnus-newsgroup-unselected gnus-newsgroup-marked
1143     gnus-newsgroup-reads gnus-newsgroup-saved
1144     gnus-newsgroup-replied gnus-newsgroup-expirable
1145     gnus-newsgroup-processable gnus-newsgroup-killed
1146     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1147     gnus-newsgroup-unsendable
1148     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1149     gnus-newsgroup-headers gnus-newsgroup-threads
1150     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1151     gnus-current-article gnus-current-headers gnus-have-all-headers
1152     gnus-last-article gnus-article-internal-prepare-hook
1153     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1154     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1155     gnus-thread-expunge-below
1156     gnus-score-alist gnus-current-score-file
1157     (gnus-summary-expunge-below . global)
1158     (gnus-summary-mark-below . global)
1159     gnus-newsgroup-active gnus-scores-exclude-files
1160     gnus-newsgroup-history gnus-newsgroup-ancient
1161     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1162     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1163     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1164     (gnus-newsgroup-expunged-tally . 0)
1165     gnus-cache-removable-articles gnus-newsgroup-cached
1166     gnus-newsgroup-data gnus-newsgroup-data-reverse
1167     gnus-newsgroup-limit gnus-newsgroup-limits
1168     gnus-newsgroup-charset
1169     gnus-newsgroup-incorporated)
1170   "Variables that are buffer-local to the summary buffers.")
1171
1172 ;; Byte-compiler warning.
1173 (defvar gnus-article-mode-map)
1174
1175 ;; Subject simplification.
1176
1177 (defun gnus-simplify-whitespace (str)
1178   "Remove excessive whitespace."
1179   (let ((mystr str))
1180     ;; Multiple spaces.
1181     (while (string-match "[ \t][ \t]+" mystr)
1182       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1183                           " "
1184                           (substring mystr (match-end 0)))))
1185     ;; Leading spaces.
1186     (when (string-match "^[ \t]+" mystr)
1187       (setq mystr (substring mystr (match-end 0))))
1188     ;; Trailing spaces.
1189     (when (string-match "[ \t]+$" mystr)
1190       (setq mystr (substring mystr 0 (match-beginning 0))))
1191     mystr))
1192
1193 (defsubst gnus-simplify-subject-re (subject)
1194   "Remove \"Re:\" from subject lines."
1195   (if (string-match "^[Rr][Ee]: *" subject)
1196       (substring subject (match-end 0))
1197     subject))
1198
1199 (defun gnus-simplify-subject (subject &optional re-only)
1200   "Remove `Re:' and words in parentheses.
1201 If RE-ONLY is non-nil, strip leading `Re:'s only."
1202   (let ((case-fold-search t))           ;Ignore case.
1203     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1204     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1205       (setq subject (substring subject (match-end 0))))
1206     ;; Remove uninteresting prefixes.
1207     (when (and (not re-only)
1208                gnus-simplify-ignored-prefixes
1209                (string-match gnus-simplify-ignored-prefixes subject))
1210       (setq subject (substring subject (match-end 0))))
1211     ;; Remove words in parentheses from end.
1212     (unless re-only
1213       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1214         (setq subject (substring subject 0 (match-beginning 0)))))
1215     ;; Return subject string.
1216     subject))
1217
1218 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1219 ;; all whitespace.
1220 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1221   (goto-char (point-min))
1222   (while (re-search-forward regexp nil t)
1223     (replace-match (or newtext ""))))
1224
1225 (defun gnus-simplify-buffer-fuzzy ()
1226   "Simplify string in the buffer fuzzily.
1227 The string in the accessible portion of the current buffer is simplified.
1228 It is assumed to be a single-line subject.
1229 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1230 matter is removed.  Additional things can be deleted by setting
1231 gnus-simplify-subject-fuzzy-regexp."
1232   (let ((case-fold-search t)
1233         (modified-tick))
1234     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1235
1236     (while (not (eq modified-tick (buffer-modified-tick)))
1237       (setq modified-tick (buffer-modified-tick))
1238       (cond
1239        ((listp gnus-simplify-subject-fuzzy-regexp)
1240         (mapcar 'gnus-simplify-buffer-fuzzy-step
1241                 gnus-simplify-subject-fuzzy-regexp))
1242        (gnus-simplify-subject-fuzzy-regexp
1243         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1244       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1245       (gnus-simplify-buffer-fuzzy-step
1246        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1247       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1248
1249     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1250     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1251     (gnus-simplify-buffer-fuzzy-step " $")
1252     (gnus-simplify-buffer-fuzzy-step "^ +")))
1253
1254 (defun gnus-simplify-subject-fuzzy (subject)
1255   "Simplify a subject string fuzzily.
1256 See `gnus-simplify-buffer-fuzzy' for details."
1257   (save-excursion
1258     (gnus-set-work-buffer)
1259     (let ((case-fold-search t))
1260       ;; Remove uninteresting prefixes.
1261       (when (and gnus-simplify-ignored-prefixes
1262                  (string-match gnus-simplify-ignored-prefixes subject))
1263         (setq subject (substring subject (match-end 0))))
1264       (insert subject)
1265       (inline (gnus-simplify-buffer-fuzzy))
1266       (buffer-string))))
1267
1268 (defsubst gnus-simplify-subject-fully (subject)
1269   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1270   (cond
1271    (gnus-simplify-subject-functions
1272     (gnus-map-function gnus-simplify-subject-functions subject))
1273    ((null gnus-summary-gather-subject-limit)
1274     (gnus-simplify-subject-re subject))
1275    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1276     (gnus-simplify-subject-fuzzy subject))
1277    ((numberp gnus-summary-gather-subject-limit)
1278     (gnus-limit-string (gnus-simplify-subject-re subject)
1279                        gnus-summary-gather-subject-limit))
1280    (t
1281     subject)))
1282
1283 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1284   "Check whether two subjects are equal.
1285 If optional argument simple-first is t, first argument is already
1286 simplified."
1287   (cond
1288    ((null simple-first)
1289     (equal (gnus-simplify-subject-fully s1)
1290            (gnus-simplify-subject-fully s2)))
1291    (t
1292     (equal s1
1293            (gnus-simplify-subject-fully s2)))))
1294
1295 (defun gnus-summary-bubble-group ()
1296   "Increase the score of the current group.
1297 This is a handy function to add to `gnus-summary-exit-hook' to
1298 increase the score of each group you read."
1299   (gnus-group-add-score gnus-newsgroup-name))
1300
1301 \f
1302 ;;;
1303 ;;; Gnus summary mode
1304 ;;;
1305
1306 (put 'gnus-summary-mode 'mode-class 'special)
1307
1308 (when t
1309   ;; Non-orthogonal keys
1310
1311   (gnus-define-keys gnus-summary-mode-map
1312     " " gnus-summary-next-page
1313     "\177" gnus-summary-prev-page
1314     [delete] gnus-summary-prev-page
1315     [backspace] gnus-summary-prev-page
1316     "\r" gnus-summary-scroll-up
1317     "\M-\r" gnus-summary-scroll-down
1318     "n" gnus-summary-next-unread-article
1319     "p" gnus-summary-prev-unread-article
1320     "N" gnus-summary-next-article
1321     "P" gnus-summary-prev-article
1322     "\M-\C-n" gnus-summary-next-same-subject
1323     "\M-\C-p" gnus-summary-prev-same-subject
1324     "\M-n" gnus-summary-next-unread-subject
1325     "\M-p" gnus-summary-prev-unread-subject
1326     "." gnus-summary-first-unread-article
1327     "," gnus-summary-best-unread-article
1328     "\M-s" gnus-summary-search-article-forward
1329     "\M-r" gnus-summary-search-article-backward
1330     "<" gnus-summary-beginning-of-article
1331     ">" gnus-summary-end-of-article
1332     "j" gnus-summary-goto-article
1333     "^" gnus-summary-refer-parent-article
1334     "\M-^" gnus-summary-refer-article
1335     "u" gnus-summary-tick-article-forward
1336     "!" gnus-summary-tick-article-forward
1337     "U" gnus-summary-tick-article-backward
1338     "d" gnus-summary-mark-as-read-forward
1339     "D" gnus-summary-mark-as-read-backward
1340     "E" gnus-summary-mark-as-expirable
1341     "\M-u" gnus-summary-clear-mark-forward
1342     "\M-U" gnus-summary-clear-mark-backward
1343     "k" gnus-summary-kill-same-subject-and-select
1344     "\C-k" gnus-summary-kill-same-subject
1345     "\M-\C-k" gnus-summary-kill-thread
1346     "\M-\C-l" gnus-summary-lower-thread
1347     "e" gnus-summary-edit-article
1348     "#" gnus-summary-mark-as-processable
1349     "\M-#" gnus-summary-unmark-as-processable
1350     "\M-\C-t" gnus-summary-toggle-threads
1351     "\M-\C-s" gnus-summary-show-thread
1352     "\M-\C-h" gnus-summary-hide-thread
1353     "\M-\C-f" gnus-summary-next-thread
1354     "\M-\C-b" gnus-summary-prev-thread
1355     [(meta down)] gnus-summary-next-thread
1356     [(meta up)] gnus-summary-prev-thread
1357     "\M-\C-u" gnus-summary-up-thread
1358     "\M-\C-d" gnus-summary-down-thread
1359     "&" gnus-summary-execute-command
1360     "c" gnus-summary-catchup-and-exit
1361     "\C-w" gnus-summary-mark-region-as-read
1362     "\C-t" gnus-summary-toggle-truncation
1363     "?" gnus-summary-mark-as-dormant
1364     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1365     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1366     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1367     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1368     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1369     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1370     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1371     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1372     "=" gnus-summary-expand-window
1373     "\C-x\C-s" gnus-summary-reselect-current-group
1374     "\M-g" gnus-summary-rescan-group
1375     "w" gnus-summary-stop-page-breaking
1376     "\C-c\C-r" gnus-summary-caesar-message
1377     "\M-t" gnus-summary-toggle-mime
1378     "f" gnus-summary-followup
1379     "F" gnus-summary-followup-with-original
1380     "C" gnus-summary-cancel-article
1381     "r" gnus-summary-reply
1382     "R" gnus-summary-reply-with-original
1383     "\C-c\C-f" gnus-summary-mail-forward
1384     "o" gnus-summary-save-article
1385     "\C-o" gnus-summary-save-article-mail
1386     "|" gnus-summary-pipe-output
1387     "\M-k" gnus-summary-edit-local-kill
1388     "\M-K" gnus-summary-edit-global-kill
1389     ;; "V" gnus-version
1390     "\C-c\C-d" gnus-summary-describe-group
1391     "q" gnus-summary-exit
1392     "Q" gnus-summary-exit-no-update
1393     "\C-c\C-i" gnus-info-find-node
1394     gnus-mouse-2 gnus-mouse-pick-article
1395     "m" gnus-summary-mail-other-window
1396     "a" gnus-summary-post-news
1397     "x" gnus-summary-limit-to-unread
1398     "s" gnus-summary-isearch-article
1399     "t" gnus-article-toggle-headers
1400     "g" gnus-summary-show-article
1401     "l" gnus-summary-goto-last-article
1402     "v" gnus-summary-preview-mime-message
1403     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1404     "\C-d" gnus-summary-enter-digest-group
1405     "\M-\C-d" gnus-summary-read-document
1406     "\M-\C-e" gnus-summary-edit-parameters
1407     "\M-\C-a" gnus-summary-customize-parameters
1408     "\C-c\C-b" gnus-bug
1409     "*" gnus-cache-enter-article
1410     "\M-*" gnus-cache-remove-article
1411     "\M-&" gnus-summary-universal-argument
1412     "\C-l" gnus-recenter
1413     "I" gnus-summary-increase-score
1414     "L" gnus-summary-lower-score
1415     "\M-i" gnus-symbolic-argument
1416     "h" gnus-summary-select-article-buffer
1417
1418     "V" gnus-summary-score-map
1419     "X" gnus-uu-extract-map
1420     "S" gnus-summary-send-map)
1421
1422   ;; Sort of orthogonal keymap
1423   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1424     "t" gnus-summary-tick-article-forward
1425     "!" gnus-summary-tick-article-forward
1426     "d" gnus-summary-mark-as-read-forward
1427     "r" gnus-summary-mark-as-read-forward
1428     "c" gnus-summary-clear-mark-forward
1429     " " gnus-summary-clear-mark-forward
1430     "e" gnus-summary-mark-as-expirable
1431     "x" gnus-summary-mark-as-expirable
1432     "?" gnus-summary-mark-as-dormant
1433     "b" gnus-summary-set-bookmark
1434     "B" gnus-summary-remove-bookmark
1435     "#" gnus-summary-mark-as-processable
1436     "\M-#" gnus-summary-unmark-as-processable
1437     "S" gnus-summary-limit-include-expunged
1438     "C" gnus-summary-catchup
1439     "H" gnus-summary-catchup-to-here
1440     "\C-c" gnus-summary-catchup-all
1441     "k" gnus-summary-kill-same-subject-and-select
1442     "K" gnus-summary-kill-same-subject
1443     "P" gnus-uu-mark-map)
1444
1445   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1446     "c" gnus-summary-clear-above
1447     "u" gnus-summary-tick-above
1448     "m" gnus-summary-mark-above
1449     "k" gnus-summary-kill-below)
1450
1451   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1452     "/" gnus-summary-limit-to-subject
1453     "n" gnus-summary-limit-to-articles
1454     "w" gnus-summary-pop-limit
1455     "s" gnus-summary-limit-to-subject
1456     "a" gnus-summary-limit-to-author
1457     "u" gnus-summary-limit-to-unread
1458     "m" gnus-summary-limit-to-marks
1459     "M" gnus-summary-limit-exclude-marks
1460     "v" gnus-summary-limit-to-score
1461     "*" gnus-summary-limit-include-cached
1462     "D" gnus-summary-limit-include-dormant
1463     "T" gnus-summary-limit-include-thread
1464     "d" gnus-summary-limit-exclude-dormant
1465     "t" gnus-summary-limit-to-age
1466     "x" gnus-summary-limit-to-extra 
1467     "E" gnus-summary-limit-include-expunged
1468     "c" gnus-summary-limit-exclude-childless-dormant
1469     "C" gnus-summary-limit-mark-excluded-as-read)
1470
1471   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1472     "n" gnus-summary-next-unread-article
1473     "p" gnus-summary-prev-unread-article
1474     "N" gnus-summary-next-article
1475     "P" gnus-summary-prev-article
1476     "\C-n" gnus-summary-next-same-subject
1477     "\C-p" gnus-summary-prev-same-subject
1478     "\M-n" gnus-summary-next-unread-subject
1479     "\M-p" gnus-summary-prev-unread-subject
1480     "f" gnus-summary-first-unread-article
1481     "b" gnus-summary-best-unread-article
1482     "j" gnus-summary-goto-article
1483     "g" gnus-summary-goto-subject
1484     "l" gnus-summary-goto-last-article
1485     "o" gnus-summary-pop-article)
1486
1487   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1488     "k" gnus-summary-kill-thread
1489     "l" gnus-summary-lower-thread
1490     "i" gnus-summary-raise-thread
1491     "T" gnus-summary-toggle-threads
1492     "t" gnus-summary-rethread-current
1493     "^" gnus-summary-reparent-thread
1494     "s" gnus-summary-show-thread
1495     "S" gnus-summary-show-all-threads
1496     "h" gnus-summary-hide-thread
1497     "H" gnus-summary-hide-all-threads
1498     "n" gnus-summary-next-thread
1499     "p" gnus-summary-prev-thread
1500     "u" gnus-summary-up-thread
1501     "o" gnus-summary-top-thread
1502     "d" gnus-summary-down-thread
1503     "#" gnus-uu-mark-thread
1504     "\M-#" gnus-uu-unmark-thread)
1505
1506   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1507     "g" gnus-summary-prepare
1508     "c" gnus-summary-insert-cached-articles)
1509
1510   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1511     "c" gnus-summary-catchup-and-exit
1512     "C" gnus-summary-catchup-all-and-exit
1513     "E" gnus-summary-exit-no-update
1514     "J" gnus-summary-jump-to-other-group
1515     "Q" gnus-summary-exit
1516     "Z" gnus-summary-exit
1517     "n" gnus-summary-catchup-and-goto-next-group
1518     "R" gnus-summary-reselect-current-group
1519     "G" gnus-summary-rescan-group
1520     "N" gnus-summary-next-group
1521     "s" gnus-summary-save-newsrc
1522     "P" gnus-summary-prev-group)
1523
1524   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1525     " " gnus-summary-next-page
1526     "n" gnus-summary-next-page
1527     "\177" gnus-summary-prev-page
1528     [delete] gnus-summary-prev-page
1529     "p" gnus-summary-prev-page
1530     "\r" gnus-summary-scroll-up
1531     "\M-\r" gnus-summary-scroll-down
1532     "<" gnus-summary-beginning-of-article
1533     ">" gnus-summary-end-of-article
1534     "b" gnus-summary-beginning-of-article
1535     "e" gnus-summary-end-of-article
1536     "^" gnus-summary-refer-parent-article
1537     "r" gnus-summary-refer-parent-article
1538     "D" gnus-summary-enter-digest-group
1539     "R" gnus-summary-refer-references
1540     "T" gnus-summary-refer-thread
1541     "g" gnus-summary-show-article
1542     "s" gnus-summary-isearch-article
1543     "P" gnus-summary-print-article
1544     "t" gnus-article-babel
1545     "d" gnus-summary-decrypt-article
1546     "v" gnus-summary-verify-article)
1547
1548   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1549     "b" gnus-article-add-buttons
1550     "B" gnus-article-add-buttons-to-head
1551     "o" gnus-article-treat-overstrike
1552     "e" gnus-article-emphasize
1553     "w" gnus-article-fill-cited-article
1554     "Q" gnus-article-fill-long-lines
1555     "C" gnus-article-capitalize-sentences
1556     "c" gnus-article-remove-cr
1557     "Z" gnus-article-decode-HZ
1558     "f" gnus-article-display-x-face
1559     "l" gnus-summary-stop-page-breaking
1560     "r" gnus-summary-caesar-message
1561     "t" gnus-article-toggle-headers
1562     "v" gnus-summary-verbose-headers
1563     "m" gnus-summary-toggle-mime
1564     "H" gnus-article-strip-headers-in-body
1565     "d" gnus-article-treat-dumbquotes
1566     "s" gnus-smiley-display)
1567
1568   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1569     "a" gnus-article-hide
1570     "h" gnus-article-toggle-headers
1571     "b" gnus-article-hide-boring-headers
1572     "s" gnus-article-hide-signature
1573     "c" gnus-article-hide-citation
1574     "C" gnus-article-hide-citation-in-followups
1575     "l" gnus-article-hide-list-identifiers
1576     "p" gnus-article-hide-pgp
1577     "B" gnus-article-strip-banner
1578     "P" gnus-article-hide-pem
1579     "\C-c" gnus-article-hide-citation-maybe)
1580
1581   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1582     "a" gnus-article-highlight
1583     "h" gnus-article-highlight-headers
1584     "c" gnus-article-highlight-citation
1585     "s" gnus-article-highlight-signature)
1586
1587   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1588     "z" gnus-article-date-ut
1589     "u" gnus-article-date-ut
1590     "l" gnus-article-date-local
1591     "e" gnus-article-date-lapsed
1592     "o" gnus-article-date-original
1593     "i" gnus-article-date-iso8601
1594     "s" gnus-article-date-user)
1595
1596   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1597     "t" gnus-article-remove-trailing-blank-lines
1598     "l" gnus-article-strip-leading-blank-lines
1599     "m" gnus-article-strip-multiple-blank-lines
1600     "a" gnus-article-strip-blank-lines
1601     "A" gnus-article-strip-all-blank-lines
1602     "s" gnus-article-strip-leading-space
1603     "e" gnus-article-strip-trailing-space)
1604
1605   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1606     "v" gnus-version
1607     "f" gnus-summary-fetch-faq
1608     "d" gnus-summary-describe-group
1609     "h" gnus-summary-describe-briefly
1610     "i" gnus-info-find-node)
1611
1612   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1613     "e" gnus-summary-expire-articles
1614     "\M-\C-e" gnus-summary-expire-articles-now
1615     "\177" gnus-summary-delete-article
1616     [delete] gnus-summary-delete-article
1617     [backspace] gnus-summary-delete-article
1618     "m" gnus-summary-move-article
1619     "r" gnus-summary-respool-article
1620     "w" gnus-summary-edit-article
1621     "c" gnus-summary-copy-article
1622     "B" gnus-summary-crosspost-article
1623     "q" gnus-summary-respool-query
1624     "t" gnus-summary-respool-trace
1625     "i" gnus-summary-import-article
1626     "p" gnus-summary-article-posted-p)
1627
1628   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1629     "o" gnus-summary-save-article
1630     "m" gnus-summary-save-article-mail
1631     "F" gnus-summary-write-article-file
1632     "r" gnus-summary-save-article-rmail
1633     "f" gnus-summary-save-article-file
1634     "b" gnus-summary-save-article-body-file
1635     "h" gnus-summary-save-article-folder
1636     "v" gnus-summary-save-article-vm
1637     "p" gnus-summary-pipe-output
1638     "s" gnus-soup-add-article)
1639
1640   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1641     "b" gnus-summary-display-buttonized
1642     "m" gnus-summary-repair-multipart
1643     "v" gnus-article-view-part
1644     "o" gnus-article-save-part
1645     "c" gnus-article-copy-part
1646     "e" gnus-article-externalize-part
1647     "i" gnus-article-inline-part
1648     "|" gnus-article-pipe-part))
1649
1650 (defun gnus-summary-make-menu-bar ()
1651   (gnus-turn-off-edit-menu 'summary)
1652
1653   (unless (boundp 'gnus-summary-misc-menu)
1654
1655     (easy-menu-define
1656      gnus-summary-kill-menu gnus-summary-mode-map ""
1657      (cons
1658       "Score"
1659       (nconc
1660        (list
1661         ["Enter score..." gnus-summary-score-entry t]
1662         ["Customize" gnus-score-customize t])
1663        (gnus-make-score-map 'increase)
1664        (gnus-make-score-map 'lower)
1665        '(("Mark"
1666           ["Kill below" gnus-summary-kill-below t]
1667           ["Mark above" gnus-summary-mark-above t]
1668           ["Tick above" gnus-summary-tick-above t]
1669           ["Clear above" gnus-summary-clear-above t])
1670          ["Current score" gnus-summary-current-score t]
1671          ["Set score" gnus-summary-set-score t]
1672          ["Switch current score file..." gnus-score-change-score-file t]
1673          ["Set mark below..." gnus-score-set-mark-below t]
1674          ["Set expunge below..." gnus-score-set-expunge-below t]
1675          ["Edit current score file" gnus-score-edit-current-scores t]
1676          ["Edit score file" gnus-score-edit-file t]
1677          ["Trace score" gnus-score-find-trace t]
1678          ["Find words" gnus-score-find-favourite-words t]
1679          ["Rescore buffer" gnus-summary-rescore t]
1680          ["Increase score..." gnus-summary-increase-score t]
1681          ["Lower score..." gnus-summary-lower-score t]))))
1682
1683     ;; Define both the Article menu in the summary buffer and the equivalent
1684     ;; Commands menu in the article buffer here for consistency.
1685     (let ((innards
1686            '(("Hide"
1687               ["All" gnus-article-hide t]
1688               ["Headers" gnus-article-toggle-headers t]
1689               ["Signature" gnus-article-hide-signature t]
1690               ["Citation" gnus-article-hide-citation t]
1691               ["List identifiers" gnus-article-hide-list-identifiers t]
1692               ["PGP" gnus-article-hide-pgp t]
1693               ["Banner" gnus-article-strip-banner t]
1694               ["Boring headers" gnus-article-hide-boring-headers t])
1695              ("Highlight"
1696               ["All" gnus-article-highlight t]
1697               ["Headers" gnus-article-highlight-headers t]
1698               ["Signature" gnus-article-highlight-signature t]
1699               ["Citation" gnus-article-highlight-citation t])
1700              ("Date"
1701               ["Local" gnus-article-date-local t]
1702               ["ISO8601" gnus-article-date-iso8601 t]
1703               ["UT" gnus-article-date-ut t]
1704               ["Original" gnus-article-date-original t]
1705               ["Lapsed" gnus-article-date-lapsed t]
1706               ["User-defined" gnus-article-date-user t])
1707              ("Washing"
1708               ("Remove Blanks"
1709                ["Leading" gnus-article-strip-leading-blank-lines t]
1710                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1711                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1712                ["All of the above" gnus-article-strip-blank-lines t]
1713                ["All" gnus-article-strip-all-blank-lines t]
1714                ["Leading space" gnus-article-strip-leading-space t]
1715                ["Trailing space" gnus-article-strip-trailing-space t])
1716               ["Overstrike" gnus-article-treat-overstrike t]
1717               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1718               ["Emphasis" gnus-article-emphasize t]
1719               ["Word wrap" gnus-article-fill-cited-article t]
1720               ["Fill long lines" gnus-article-fill-long-lines t]
1721               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1722               ["CR" gnus-article-remove-cr t]
1723               ["Show X-Face" gnus-article-display-x-face t]
1724               ["Rot 13" gnus-summary-caesar-message t]
1725               ["Unix pipe" gnus-summary-pipe-message t]
1726               ["Add buttons" gnus-article-add-buttons t]
1727               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1728               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1729               ["Toggle MIME" gnus-summary-toggle-mime t]
1730               ["Verbose header" gnus-summary-verbose-headers t]
1731               ["Toggle header" gnus-summary-toggle-header t]
1732               ["Toggle smileys" gnus-smiley-display t]
1733               ["HZ" gnus-article-decode-HZ t])
1734              ("Output"
1735               ["Save in default format" gnus-summary-save-article t]
1736               ["Save in file" gnus-summary-save-article-file t]
1737               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1738               ["Save in MH folder" gnus-summary-save-article-folder t]
1739               ["Save in VM folder" gnus-summary-save-article-vm t]
1740               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1741               ["Save body in file" gnus-summary-save-article-body-file t]
1742               ["Pipe through a filter" gnus-summary-pipe-output t]
1743               ["Add to SOUP packet" gnus-soup-add-article t]
1744               ["Print" gnus-summary-print-article t])
1745              ("Backend"
1746               ["Respool article..." gnus-summary-respool-article t]
1747               ["Move article..." gnus-summary-move-article
1748                (gnus-check-backend-function
1749                 'request-move-article gnus-newsgroup-name)]
1750               ["Copy article..." gnus-summary-copy-article t]
1751               ["Crosspost article..." gnus-summary-crosspost-article
1752                (gnus-check-backend-function
1753                 'request-replace-article gnus-newsgroup-name)]
1754               ["Import file..." gnus-summary-import-article t]
1755               ["Check if posted" gnus-summary-article-posted-p t]
1756               ["Edit article" gnus-summary-edit-article
1757                (not (gnus-group-read-only-p))]
1758               ["Delete article" gnus-summary-delete-article
1759                (gnus-check-backend-function
1760                 'request-expire-articles gnus-newsgroup-name)]
1761               ["Query respool" gnus-summary-respool-query t]
1762               ["Trace respool" gnus-summary-respool-trace t]
1763               ["Delete expirable articles" gnus-summary-expire-articles-now
1764                (gnus-check-backend-function
1765                 'request-expire-articles gnus-newsgroup-name)])
1766              ("Extract"
1767               ["Uudecode" gnus-uu-decode-uu t]
1768               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1769               ["Unshar" gnus-uu-decode-unshar t]
1770               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1771               ["Save" gnus-uu-decode-save t]
1772               ["Binhex" gnus-uu-decode-binhex t]
1773               ["Postscript" gnus-uu-decode-postscript t])
1774              ("Cache"
1775               ["Enter article" gnus-cache-enter-article t]
1776               ["Remove article" gnus-cache-remove-article t])
1777              ["Translate" gnus-article-babel t]
1778              ["Select article buffer" gnus-summary-select-article-buffer t]
1779              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1780              ["Isearch article..." gnus-summary-isearch-article t]
1781              ["Beginning of the article" gnus-summary-beginning-of-article t]
1782              ["End of the article" gnus-summary-end-of-article t]
1783              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1784              ["Fetch referenced articles" gnus-summary-refer-references t]
1785              ["Fetch current thread" gnus-summary-refer-thread t]
1786              ["Fetch article with id..." gnus-summary-refer-article t]
1787              ["Redisplay" gnus-summary-show-article t])))
1788       (easy-menu-define
1789        gnus-summary-article-menu gnus-summary-mode-map ""
1790        (cons "Article" innards))
1791
1792       (easy-menu-define
1793        gnus-article-commands-menu gnus-article-mode-map ""
1794        (cons "Commands" innards)))
1795
1796     (easy-menu-define
1797      gnus-summary-thread-menu gnus-summary-mode-map ""
1798      '("Threads"
1799        ["Toggle threading" gnus-summary-toggle-threads t]
1800        ["Hide threads" gnus-summary-hide-all-threads t]
1801        ["Show threads" gnus-summary-show-all-threads t]
1802        ["Hide thread" gnus-summary-hide-thread t]
1803        ["Show thread" gnus-summary-show-thread t]
1804        ["Go to next thread" gnus-summary-next-thread t]
1805        ["Go to previous thread" gnus-summary-prev-thread t]
1806        ["Go down thread" gnus-summary-down-thread t]
1807        ["Go up thread" gnus-summary-up-thread t]
1808        ["Top of thread" gnus-summary-top-thread t]
1809        ["Mark thread as read" gnus-summary-kill-thread t]
1810        ["Lower thread score" gnus-summary-lower-thread t]
1811        ["Raise thread score" gnus-summary-raise-thread t]
1812        ["Rethread current" gnus-summary-rethread-current t]))
1813
1814     (easy-menu-define
1815      gnus-summary-post-menu gnus-summary-mode-map ""
1816      '("Post"
1817        ["Post an article" gnus-summary-post-news t]
1818        ["Followup" gnus-summary-followup t]
1819        ["Followup and yank" gnus-summary-followup-with-original t]
1820        ["Supersede article" gnus-summary-supersede-article t]
1821        ["Cancel article" gnus-summary-cancel-article t]
1822        ["Reply" gnus-summary-reply t]
1823        ["Reply and yank" gnus-summary-reply-with-original t]
1824        ["Wide reply" gnus-summary-wide-reply t]
1825        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1826        ["Mail forward" gnus-summary-mail-forward t]
1827        ["Post forward" gnus-summary-post-forward t]
1828        ["Digest and mail" gnus-uu-digest-mail-forward t]
1829        ["Digest and post" gnus-uu-digest-post-forward t]
1830        ["Resend message" gnus-summary-resend-message t]
1831        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1832        ["Send a mail" gnus-summary-mail-other-window t]
1833        ["Uuencode and post" gnus-uu-post-news t]
1834        ["Followup via news" gnus-summary-followup-to-mail t]
1835        ["Followup via news and yank"
1836         gnus-summary-followup-to-mail-with-original t]
1837        ;;("Draft"
1838        ;;["Send" gnus-summary-send-draft t]
1839        ;;["Send bounced" gnus-resend-bounced-mail t])
1840        ))
1841
1842     (easy-menu-define
1843      gnus-summary-misc-menu gnus-summary-mode-map ""
1844      '("Misc"
1845        ("Mark Read"
1846         ["Mark as read" gnus-summary-mark-as-read-forward t]
1847         ["Mark same subject and select"
1848          gnus-summary-kill-same-subject-and-select t]
1849         ["Mark same subject" gnus-summary-kill-same-subject t]
1850         ["Catchup" gnus-summary-catchup t]
1851         ["Catchup all" gnus-summary-catchup-all t]
1852         ["Catchup to here" gnus-summary-catchup-to-here t]
1853         ["Catchup region" gnus-summary-mark-region-as-read t]
1854         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1855        ("Mark Various"
1856         ["Tick" gnus-summary-tick-article-forward t]
1857         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1858         ["Remove marks" gnus-summary-clear-mark-forward t]
1859         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1860         ["Set bookmark" gnus-summary-set-bookmark t]
1861         ["Remove bookmark" gnus-summary-remove-bookmark t])
1862        ("Mark Limit"
1863         ["Marks..." gnus-summary-limit-to-marks t]
1864         ["Subject..." gnus-summary-limit-to-subject t]
1865         ["Author..." gnus-summary-limit-to-author t]
1866         ["Age..." gnus-summary-limit-to-age t]
1867         ["Extra..." gnus-summary-limit-to-extra t]
1868         ["Score" gnus-summary-limit-to-score t]
1869         ["Unread" gnus-summary-limit-to-unread t]
1870         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1871         ["Articles" gnus-summary-limit-to-articles t]
1872         ["Pop limit" gnus-summary-pop-limit t]
1873         ["Show dormant" gnus-summary-limit-include-dormant t]
1874         ["Hide childless dormant"
1875          gnus-summary-limit-exclude-childless-dormant t]
1876         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1877         ["Hide marked" gnus-summary-limit-exclude-marks t]
1878         ["Show expunged" gnus-summary-show-all-expunged t])
1879        ("Process Mark"
1880         ["Set mark" gnus-summary-mark-as-processable t]
1881         ["Remove mark" gnus-summary-unmark-as-processable t]
1882         ["Remove all marks" gnus-summary-unmark-all-processable t]
1883         ["Mark above" gnus-uu-mark-over t]
1884         ["Mark series" gnus-uu-mark-series t]
1885         ["Mark region" gnus-uu-mark-region t]
1886         ["Unmark region" gnus-uu-unmark-region t]
1887         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1888         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1889         ["Mark all" gnus-uu-mark-all t]
1890         ["Mark buffer" gnus-uu-mark-buffer t]
1891         ["Mark sparse" gnus-uu-mark-sparse t]
1892         ["Mark thread" gnus-uu-mark-thread t]
1893         ["Unmark thread" gnus-uu-unmark-thread t]
1894         ("Process Mark Sets"
1895          ["Kill" gnus-summary-kill-process-mark t]
1896          ["Yank" gnus-summary-yank-process-mark
1897           gnus-newsgroup-process-stack]
1898          ["Save" gnus-summary-save-process-mark t]))
1899        ("Scroll article"
1900         ["Page forward" gnus-summary-next-page t]
1901         ["Page backward" gnus-summary-prev-page t]
1902         ["Line forward" gnus-summary-scroll-up t])
1903        ("Move"
1904         ["Next unread article" gnus-summary-next-unread-article t]
1905         ["Previous unread article" gnus-summary-prev-unread-article t]
1906         ["Next article" gnus-summary-next-article t]
1907         ["Previous article" gnus-summary-prev-article t]
1908         ["Next unread subject" gnus-summary-next-unread-subject t]
1909         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1910         ["Next article same subject" gnus-summary-next-same-subject t]
1911         ["Previous article same subject" gnus-summary-prev-same-subject t]
1912         ["First unread article" gnus-summary-first-unread-article t]
1913         ["Best unread article" gnus-summary-best-unread-article t]
1914         ["Go to subject number..." gnus-summary-goto-subject t]
1915         ["Go to article number..." gnus-summary-goto-article t]
1916         ["Go to the last article" gnus-summary-goto-last-article t]
1917         ["Pop article off history" gnus-summary-pop-article t])
1918        ("Sort"
1919         ["Sort by number" gnus-summary-sort-by-number t]
1920         ["Sort by author" gnus-summary-sort-by-author t]
1921         ["Sort by subject" gnus-summary-sort-by-subject t]
1922         ["Sort by date" gnus-summary-sort-by-date t]
1923         ["Sort by score" gnus-summary-sort-by-score t]
1924         ["Sort by lines" gnus-summary-sort-by-lines t]
1925         ["Sort by characters" gnus-summary-sort-by-chars t])
1926        ("Help"
1927         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1928         ["Describe group" gnus-summary-describe-group t]
1929         ["Read manual" gnus-info-find-node t])
1930        ("Modes"
1931         ["Pick and read" gnus-pick-mode t]
1932         ["Binary" gnus-binary-mode t])
1933        ("Regeneration"
1934         ["Regenerate" gnus-summary-prepare t]
1935         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1936         ["Toggle threading" gnus-summary-toggle-threads t])
1937        ["Filter articles..." gnus-summary-execute-command t]
1938        ["Run command on subjects..." gnus-summary-universal-argument t]
1939        ["Search articles forward..." gnus-summary-search-article-forward t]
1940        ["Search articles backward..." gnus-summary-search-article-backward t]
1941        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1942        ["Expand window" gnus-summary-expand-window t]
1943        ["Expire expirable articles" gnus-summary-expire-articles
1944         (gnus-check-backend-function
1945          'request-expire-articles gnus-newsgroup-name)]
1946        ["Edit local kill file" gnus-summary-edit-local-kill t]
1947        ["Edit main kill file" gnus-summary-edit-global-kill t]
1948        ["Edit group parameters" gnus-summary-edit-parameters t]
1949        ["Customize group parameters" gnus-summary-customize-parameters t]
1950        ["Send a bug report" gnus-bug t]
1951        ("Exit"
1952         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1953         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1954         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1955         ["Exit group" gnus-summary-exit t]
1956         ["Exit group without updating" gnus-summary-exit-no-update t]
1957         ["Exit and goto next group" gnus-summary-next-group t]
1958         ["Exit and goto prev group" gnus-summary-prev-group t]
1959         ["Reselect group" gnus-summary-reselect-current-group t]
1960         ["Rescan group" gnus-summary-rescan-group t]
1961         ["Update dribble" gnus-summary-save-newsrc t])))
1962
1963     (gnus-run-hooks 'gnus-summary-menu-hook)))
1964
1965 (defun gnus-score-set-default (var value)
1966   "A version of set that updates the GNU Emacs menu-bar."
1967   (set var value)
1968   ;; It is the message that forces the active status to be updated.
1969   (message ""))
1970
1971 (defun gnus-make-score-map (type)
1972   "Make a summary score map of type TYPE."
1973   (if t
1974       nil
1975     (let ((headers '(("author" "from" string)
1976                      ("subject" "subject" string)
1977                      ("article body" "body" string)
1978                      ("article head" "head" string)
1979                      ("xref" "xref" string)
1980                      ("extra header" "extra" string)
1981                      ("lines" "lines" number)
1982                      ("followups to author" "followup" string)))
1983           (types '((number ("less than" <)
1984                            ("greater than" >)
1985                            ("equal" =))
1986                    (string ("substring" s)
1987                            ("exact string" e)
1988                            ("fuzzy string" f)
1989                            ("regexp" r))))
1990           (perms '(("temporary" (current-time-string))
1991                    ("permanent" nil)
1992                    ("immediate" now)))
1993           header)
1994       (list
1995        (apply
1996         'nconc
1997         (list
1998          (if (eq type 'lower)
1999              "Lower score"
2000            "Increase score"))
2001         (let (outh)
2002           (while headers
2003             (setq header (car headers))
2004             (setq outh
2005                   (cons
2006                    (apply
2007                     'nconc
2008                     (list (car header))
2009                     (let ((ts (cdr (assoc (nth 2 header) types)))
2010                           outt)
2011                       (while ts
2012                         (setq outt
2013                               (cons
2014                                (apply
2015                                 'nconc
2016                                 (list (caar ts))
2017                                 (let ((ps perms)
2018                                       outp)
2019                                   (while ps
2020                                     (setq outp
2021                                           (cons
2022                                            (vector
2023                                             (caar ps)
2024                                             (list
2025                                              'gnus-summary-score-entry
2026                                              (nth 1 header)
2027                                              (if (or (string= (nth 1 header)
2028                                                               "head")
2029                                                      (string= (nth 1 header)
2030                                                               "body"))
2031                                                  ""
2032                                                (list 'gnus-summary-header
2033                                                      (nth 1 header)))
2034                                              (list 'quote (nth 1 (car ts)))
2035                                              (list 'gnus-score-delta-default
2036                                                    nil)
2037                                              (nth 1 (car ps))
2038                                              t)
2039                                             t)
2040                                            outp))
2041                                     (setq ps (cdr ps)))
2042                                   (list (nreverse outp))))
2043                                outt))
2044                         (setq ts (cdr ts)))
2045                       (list (nreverse outt))))
2046                    outh))
2047             (setq headers (cdr headers)))
2048           (list (nreverse outh))))))))
2049
2050 \f
2051
2052 (defun gnus-summary-mode (&optional group)
2053   "Major mode for reading articles.
2054
2055 All normal editing commands are switched off.
2056 \\<gnus-summary-mode-map>
2057 Each line in this buffer represents one article.  To read an
2058 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2059 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2060 respectively.
2061
2062 You can also post articles and send mail from this buffer.  To
2063 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2064 of an article, type `\\[gnus-summary-reply]'.
2065
2066 There are approx. one gazillion commands you can execute in this
2067 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2068
2069 The following commands are available:
2070
2071 \\{gnus-summary-mode-map}"
2072   (interactive)
2073   (when (gnus-visual-p 'summary-menu 'menu)
2074     (gnus-summary-make-menu-bar))
2075   (kill-all-local-variables)
2076   (gnus-summary-make-local-variables)
2077   (gnus-make-thread-indent-array)
2078   (gnus-simplify-mode-line)
2079   (setq major-mode 'gnus-summary-mode)
2080   (setq mode-name "Summary")
2081   (make-local-variable 'minor-mode-alist)
2082   (use-local-map gnus-summary-mode-map)
2083   (buffer-disable-undo)
2084   (setq buffer-read-only t)             ;Disable modification
2085   (setq truncate-lines t)
2086   (setq selective-display t)
2087   (setq selective-display-ellipses t)   ;Display `...'
2088   (gnus-summary-set-display-table)
2089   (gnus-set-default-directory)
2090   (setq gnus-newsgroup-name group)
2091   (unless (gnus-news-group-p group)
2092     (setq gnus-newsgroup-incorporated
2093           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2094   (make-local-variable 'gnus-summary-line-format)
2095   (make-local-variable 'gnus-summary-line-format-spec)
2096   (make-local-variable 'gnus-summary-dummy-line-format)
2097   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2098   (make-local-variable 'gnus-summary-mark-positions)
2099   (make-local-hook 'pre-command-hook)
2100   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2101   (gnus-run-hooks 'gnus-summary-mode-hook)
2102   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2103   (gnus-update-summary-mark-positions))
2104
2105 (defun gnus-summary-make-local-variables ()
2106   "Make all the local summary buffer variables."
2107   (let (global)
2108     (dolist (local gnus-summary-local-variables)
2109       (if (consp local)
2110           (progn
2111             (if (eq (cdr local) 'global)
2112                 ;; Copy the global value of the variable.
2113                 (setq global (symbol-value (car local)))
2114               ;; Use the value from the list.
2115               (setq global (eval (cdr local))))
2116             (set (make-local-variable (car local)) global))
2117         ;; Simple nil-valued local variable.
2118         (set (make-local-variable local) nil)))))
2119
2120 (defun gnus-summary-clear-local-variables ()
2121   (let ((locals gnus-summary-local-variables))
2122     (while locals
2123       (if (consp (car locals))
2124           (and (vectorp (caar locals))
2125                (set (caar locals) nil))
2126         (and (vectorp (car locals))
2127              (set (car locals) nil)))
2128       (setq locals (cdr locals)))))
2129
2130 ;; Summary data functions.
2131
2132 (defmacro gnus-data-number (data)
2133   `(car ,data))
2134
2135 (defmacro gnus-data-set-number (data number)
2136   `(setcar ,data ,number))
2137
2138 (defmacro gnus-data-mark (data)
2139   `(nth 1 ,data))
2140
2141 (defmacro gnus-data-set-mark (data mark)
2142   `(setcar (nthcdr 1 ,data) ,mark))
2143
2144 (defmacro gnus-data-pos (data)
2145   `(nth 2 ,data))
2146
2147 (defmacro gnus-data-set-pos (data pos)
2148   `(setcar (nthcdr 2 ,data) ,pos))
2149
2150 (defmacro gnus-data-header (data)
2151   `(nth 3 ,data))
2152
2153 (defmacro gnus-data-set-header (data header)
2154   `(setcar (nthcdr 3 ,data) ,header))
2155
2156 (defmacro gnus-data-level (data)
2157   `(nth 4 ,data))
2158
2159 (defmacro gnus-data-unread-p (data)
2160   `(= (nth 1 ,data) gnus-unread-mark))
2161
2162 (defmacro gnus-data-read-p (data)
2163   `(/= (nth 1 ,data) gnus-unread-mark))
2164
2165 (defmacro gnus-data-pseudo-p (data)
2166   `(consp (nth 3 ,data)))
2167
2168 (defmacro gnus-data-find (number)
2169   `(assq ,number gnus-newsgroup-data))
2170
2171 (defmacro gnus-data-find-list (number &optional data)
2172   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2173      (memq (assq ,number bdata)
2174            bdata)))
2175
2176 (defmacro gnus-data-make (number mark pos header level)
2177   `(list ,number ,mark ,pos ,header ,level))
2178
2179 (defun gnus-data-enter (after-article number mark pos header level offset)
2180   (let ((data (gnus-data-find-list after-article)))
2181     (unless data
2182       (error "No such article: %d" after-article))
2183     (setcdr data (cons (gnus-data-make number mark pos header level)
2184                        (cdr data)))
2185     (setq gnus-newsgroup-data-reverse nil)
2186     (gnus-data-update-list (cddr data) offset)))
2187
2188 (defun gnus-data-enter-list (after-article list &optional offset)
2189   (when list
2190     (let ((data (and after-article (gnus-data-find-list after-article)))
2191           (ilist list))
2192       (if (not (or data
2193                    after-article))
2194           (let ((odata gnus-newsgroup-data))
2195             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2196             (when offset
2197               (gnus-data-update-list odata offset)))
2198         ;; Find the last element in the list to be spliced into the main
2199         ;; list.
2200         (while (cdr list)
2201           (setq list (cdr list)))
2202         (if (not data)
2203             (progn
2204               (setcdr list gnus-newsgroup-data)
2205               (setq gnus-newsgroup-data ilist)
2206               (when offset
2207                 (gnus-data-update-list (cdr list) offset)))
2208           (setcdr list (cdr data))
2209           (setcdr data ilist)
2210           (when offset
2211             (gnus-data-update-list (cdr list) offset))))
2212       (setq gnus-newsgroup-data-reverse nil))))
2213
2214 (defun gnus-data-remove (article &optional offset)
2215   (let ((data gnus-newsgroup-data))
2216     (if (= (gnus-data-number (car data)) article)
2217         (progn
2218           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2219                 gnus-newsgroup-data-reverse nil)
2220           (when offset
2221             (gnus-data-update-list gnus-newsgroup-data offset)))
2222       (while (cdr data)
2223         (when (= (gnus-data-number (cadr data)) article)
2224           (setcdr data (cddr data))
2225           (when offset
2226             (gnus-data-update-list (cdr data) offset))
2227           (setq data nil
2228                 gnus-newsgroup-data-reverse nil))
2229         (setq data (cdr data))))))
2230
2231 (defmacro gnus-data-list (backward)
2232   `(if ,backward
2233        (or gnus-newsgroup-data-reverse
2234            (setq gnus-newsgroup-data-reverse
2235                  (reverse gnus-newsgroup-data)))
2236      gnus-newsgroup-data))
2237
2238 (defun gnus-data-update-list (data offset)
2239   "Add OFFSET to the POS of all data entries in DATA."
2240   (setq gnus-newsgroup-data-reverse nil)
2241   (while data
2242     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2243     (setq data (cdr data))))
2244
2245 (defun gnus-summary-article-pseudo-p (article)
2246   "Say whether this article is a pseudo article or not."
2247   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2248
2249 (defmacro gnus-summary-article-sparse-p (article)
2250   "Say whether this article is a sparse article or not."
2251   `(memq ,article gnus-newsgroup-sparse))
2252
2253 (defmacro gnus-summary-article-ancient-p (article)
2254   "Say whether this article is a sparse article or not."
2255   `(memq ,article gnus-newsgroup-ancient))
2256
2257 (defun gnus-article-parent-p (number)
2258   "Say whether this article is a parent or not."
2259   (let ((data (gnus-data-find-list number)))
2260     (and (cdr data)                     ; There has to be an article after...
2261          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2262             (gnus-data-level (nth 1 data))))))
2263
2264 (defun gnus-article-children (number)
2265   "Return a list of all children to NUMBER."
2266   (let* ((data (gnus-data-find-list number))
2267          (level (gnus-data-level (car data)))
2268          children)
2269     (setq data (cdr data))
2270     (while (and data
2271                 (= (gnus-data-level (car data)) (1+ level)))
2272       (push (gnus-data-number (car data)) children)
2273       (setq data (cdr data)))
2274     children))
2275
2276 (defmacro gnus-summary-skip-intangible ()
2277   "If the current article is intangible, then jump to a different article."
2278   '(let ((to (get-text-property (point) 'gnus-intangible)))
2279      (and to (gnus-summary-goto-subject to))))
2280
2281 (defmacro gnus-summary-article-intangible-p ()
2282   "Say whether this article is intangible or not."
2283   '(get-text-property (point) 'gnus-intangible))
2284
2285 (defun gnus-article-read-p (article)
2286   "Say whether ARTICLE is read or not."
2287   (not (or (memq article gnus-newsgroup-marked)
2288            (memq article gnus-newsgroup-unreads)
2289            (memq article gnus-newsgroup-unselected)
2290            (memq article gnus-newsgroup-dormant))))
2291
2292 ;; Some summary mode macros.
2293
2294 (defmacro gnus-summary-article-number ()
2295   "The article number of the article on the current line.
2296 If there isn's an article number here, then we return the current
2297 article number."
2298   '(progn
2299      (gnus-summary-skip-intangible)
2300      (or (get-text-property (point) 'gnus-number)
2301          (gnus-summary-last-subject))))
2302
2303 (defmacro gnus-summary-article-header (&optional number)
2304   "Return the header of article NUMBER."
2305   `(gnus-data-header (gnus-data-find
2306                       ,(or number '(gnus-summary-article-number)))))
2307
2308 (defmacro gnus-summary-thread-level (&optional number)
2309   "Return the level of thread that starts with article NUMBER."
2310   `(if (and (eq gnus-summary-make-false-root 'dummy)
2311             (get-text-property (point) 'gnus-intangible))
2312        0
2313      (gnus-data-level (gnus-data-find
2314                        ,(or number '(gnus-summary-article-number))))))
2315
2316 (defmacro gnus-summary-article-mark (&optional number)
2317   "Return the mark of article NUMBER."
2318   `(gnus-data-mark (gnus-data-find
2319                     ,(or number '(gnus-summary-article-number)))))
2320
2321 (defmacro gnus-summary-article-pos (&optional number)
2322   "Return the position of the line of article NUMBER."
2323   `(gnus-data-pos (gnus-data-find
2324                    ,(or number '(gnus-summary-article-number)))))
2325
2326 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2327 (defmacro gnus-summary-article-subject (&optional number)
2328   "Return current subject string or nil if nothing."
2329   `(let ((headers
2330           ,(if number
2331                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2332              '(gnus-data-header (assq (gnus-summary-article-number)
2333                                       gnus-newsgroup-data)))))
2334      (and headers
2335           (vectorp headers)
2336           (mail-header-subject headers))))
2337
2338 (defmacro gnus-summary-article-score (&optional number)
2339   "Return current article score."
2340   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2341                   gnus-newsgroup-scored))
2342        gnus-summary-default-score 0))
2343
2344 (defun gnus-summary-article-children (&optional number)
2345   "Return a list of article numbers that are children of article NUMBER."
2346   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2347          (level (gnus-data-level (car data)))
2348          l children)
2349     (while (and (setq data (cdr data))
2350                 (> (setq l (gnus-data-level (car data))) level))
2351       (and (= (1+ level) l)
2352            (push (gnus-data-number (car data))
2353                  children)))
2354     (nreverse children)))
2355
2356 (defun gnus-summary-article-parent (&optional number)
2357   "Return the article number of the parent of article NUMBER."
2358   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2359                                     (gnus-data-list t)))
2360          (level (gnus-data-level (car data))))
2361     (if (zerop level)
2362         ()                              ; This is a root.
2363       ;; We search until we find an article with a level less than
2364       ;; this one.  That function has to be the parent.
2365       (while (and (setq data (cdr data))
2366                   (not (< (gnus-data-level (car data)) level))))
2367       (and data (gnus-data-number (car data))))))
2368
2369 (defun gnus-unread-mark-p (mark)
2370   "Say whether MARK is the unread mark."
2371   (= mark gnus-unread-mark))
2372
2373 (defun gnus-read-mark-p (mark)
2374   "Say whether MARK is one of the marks that mark as read.
2375 This is all marks except unread, ticked, dormant, and expirable."
2376   (not (or (= mark gnus-unread-mark)
2377            (= mark gnus-ticked-mark)
2378            (= mark gnus-dormant-mark)
2379            (= mark gnus-expirable-mark))))
2380
2381 (defmacro gnus-article-mark (number)
2382   "Return the MARK of article NUMBER.
2383 This macro should only be used when computing the mark the \"first\"
2384 time; i.e., when generating the summary lines.  After that,
2385 `gnus-summary-article-mark' should be used to examine the
2386 marks of articles."
2387   `(cond
2388     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2389     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2390     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2391     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2392     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2393     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2394     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2395     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2396            gnus-ancient-mark))))
2397
2398 ;; Saving hidden threads.
2399
2400 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2401 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2402
2403 (defmacro gnus-save-hidden-threads (&rest forms)
2404   "Save hidden threads, eval FORMS, and restore the hidden threads."
2405   (let ((config (make-symbol "config")))
2406     `(let ((,config (gnus-hidden-threads-configuration)))
2407        (unwind-protect
2408            (save-excursion
2409              ,@forms)
2410          (gnus-restore-hidden-threads-configuration ,config)))))
2411
2412 (defun gnus-data-compute-positions ()
2413   "Compute the positions of all articles."
2414   (setq gnus-newsgroup-data-reverse nil)
2415   (let ((data gnus-newsgroup-data))
2416     (save-excursion
2417       (gnus-save-hidden-threads
2418         (gnus-summary-show-all-threads)
2419         (goto-char (point-min))
2420         (while data
2421           (while (get-text-property (point) 'gnus-intangible)
2422             (forward-line 1))
2423           (gnus-data-set-pos (car data) (+ (point) 3))
2424           (setq data (cdr data))
2425           (forward-line 1))))))
2426
2427 (defun gnus-hidden-threads-configuration ()
2428   "Return the current hidden threads configuration."
2429   (save-excursion
2430     (let (config)
2431       (goto-char (point-min))
2432       (while (search-forward "\r" nil t)
2433         (push (1- (point)) config))
2434       config)))
2435
2436 (defun gnus-restore-hidden-threads-configuration (config)
2437   "Restore hidden threads configuration from CONFIG."
2438   (let (point buffer-read-only)
2439     (while (setq point (pop config))
2440       (when (and (< point (point-max))
2441                  (goto-char point)
2442                  (eq (char-after) ?\n))
2443         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2444
2445 ;; Various summary mode internalish functions.
2446
2447 (defun gnus-mouse-pick-article (e)
2448   (interactive "e")
2449   (mouse-set-point e)
2450   (gnus-summary-next-page nil t))
2451
2452 (defun gnus-summary-set-display-table ()
2453   ;; Change the display table.  Odd characters have a tendency to mess
2454   ;; up nicely formatted displays - we make all possible glyphs
2455   ;; display only a single character.
2456
2457   ;; We start from the standard display table, if any.
2458   (let ((table (or (copy-sequence standard-display-table)
2459                    (make-display-table)))
2460         (i 32))
2461     ;; Nix out all the control chars...
2462     (while (>= (setq i (1- i)) 0)
2463       (aset table i [??]))
2464     ;; ... but not newline and cr, of course.  (cr is necessary for the
2465     ;; selective display).
2466     (aset table ?\n nil)
2467     (aset table ?\r nil)
2468     ;; We keep TAB as well.
2469     (aset table ?\t nil)
2470     ;; We nix out any glyphs over 126 that are not set already.
2471     (let ((i 256))
2472       (while (>= (setq i (1- i)) 127)
2473         ;; Only modify if the entry is nil.
2474         (unless (aref table i)
2475           (aset table i [??]))))
2476     (setq buffer-display-table table)))
2477
2478 (defun gnus-summary-setup-buffer (group)
2479   "Initialize summary buffer."
2480   (let ((buffer (concat "*Summary " group "*")))
2481     (if (get-buffer buffer)
2482         (progn
2483           (set-buffer buffer)
2484           (setq gnus-summary-buffer (current-buffer))
2485           (not gnus-newsgroup-prepared))
2486       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2487       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2488       (gnus-summary-mode group)
2489       (when gnus-carpal
2490         (gnus-carpal-setup-buffer 'summary))
2491       (unless gnus-single-article-buffer
2492         (make-local-variable 'gnus-article-buffer)
2493         (make-local-variable 'gnus-article-current)
2494         (make-local-variable 'gnus-original-article-buffer))
2495       (setq gnus-newsgroup-name group)
2496       t)))
2497
2498 (defun gnus-set-global-variables ()
2499   ;; Set the global equivalents of the summary buffer-local variables
2500   ;; to the latest values they had.  These reflect the summary buffer
2501   ;; that was in action when the last article was fetched.
2502   (when (eq major-mode 'gnus-summary-mode)
2503     (setq gnus-summary-buffer (current-buffer))
2504     (let ((name gnus-newsgroup-name)
2505           (marked gnus-newsgroup-marked)
2506           (unread gnus-newsgroup-unreads)
2507           (headers gnus-current-headers)
2508           (data gnus-newsgroup-data)
2509           (summary gnus-summary-buffer)
2510           (article-buffer gnus-article-buffer)
2511           (original gnus-original-article-buffer)
2512           (gac gnus-article-current)
2513           (reffed gnus-reffed-article-number)
2514           (score-file gnus-current-score-file)
2515           (default-charset gnus-newsgroup-charset))
2516       (save-excursion
2517         (set-buffer gnus-group-buffer)
2518         (setq gnus-newsgroup-name name
2519               gnus-newsgroup-marked marked
2520               gnus-newsgroup-unreads unread
2521               gnus-current-headers headers
2522               gnus-newsgroup-data data
2523               gnus-article-current gac
2524               gnus-summary-buffer summary
2525               gnus-article-buffer article-buffer
2526               gnus-original-article-buffer original
2527               gnus-reffed-article-number reffed
2528               gnus-current-score-file score-file
2529               gnus-newsgroup-charset default-charset)
2530         ;; The article buffer also has local variables.
2531         (when (gnus-buffer-live-p gnus-article-buffer)
2532           (set-buffer gnus-article-buffer)
2533           (setq gnus-summary-buffer summary))))))
2534
2535 (defun gnus-summary-article-unread-p (article)
2536   "Say whether ARTICLE is unread or not."
2537   (memq article gnus-newsgroup-unreads))
2538
2539 (defun gnus-summary-first-article-p (&optional article)
2540   "Return whether ARTICLE is the first article in the buffer."
2541   (if (not (setq article (or article (gnus-summary-article-number))))
2542       nil
2543     (eq article (caar gnus-newsgroup-data))))
2544
2545 (defun gnus-summary-last-article-p (&optional article)
2546   "Return whether ARTICLE is the last article in the buffer."
2547   (if (not (setq article (or article (gnus-summary-article-number))))
2548       ;; All non-existent numbers are the last article.  :-)
2549       t
2550     (not (cdr (gnus-data-find-list article)))))
2551
2552 (defun gnus-make-thread-indent-array ()
2553   (let ((n 200))
2554     (unless (and gnus-thread-indent-array
2555                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2556       (setq gnus-thread-indent-array (make-vector 201 "")
2557             gnus-thread-indent-array-level gnus-thread-indent-level)
2558       (while (>= n 0)
2559         (aset gnus-thread-indent-array n
2560               (make-string (* n gnus-thread-indent-level) ? ))
2561         (setq n (1- n))))))
2562
2563 (defun gnus-update-summary-mark-positions ()
2564   "Compute where the summary marks are to go."
2565   (save-excursion
2566     (when (gnus-buffer-exists-p gnus-summary-buffer)
2567       (set-buffer gnus-summary-buffer))
2568     (let ((gnus-replied-mark 129)
2569           (gnus-score-below-mark 130)
2570           (gnus-score-over-mark 130)
2571           (gnus-download-mark 131)
2572           (spec gnus-summary-line-format-spec)
2573           gnus-visual pos)
2574       (save-excursion
2575         (gnus-set-work-buffer)
2576         (let ((gnus-summary-line-format-spec spec)
2577               (gnus-newsgroup-downloadable '((0 . t))))
2578           (gnus-summary-insert-line
2579            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2580            0 nil 128 t nil "" nil 1)
2581           (goto-char (point-min))
2582           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2583                                              (- (point) 2)))))
2584           (goto-char (point-min))
2585           (push (cons 'replied (and (search-forward "\201" nil t)
2586                                     (- (point) 2)))
2587                 pos)
2588           (goto-char (point-min))
2589           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2590                 pos)
2591           (goto-char (point-min))
2592           (push (cons 'download
2593                       (and (search-forward "\203" nil t) (- (point) 2)))
2594                 pos)))
2595       (setq gnus-summary-mark-positions pos))))
2596
2597 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2598   "Insert a dummy root in the summary buffer."
2599   (beginning-of-line)
2600   (gnus-add-text-properties
2601    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2602    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2603
2604 (defun gnus-summary-from-or-to-or-newsgroups (header)
2605   (let ((to (cdr (assq 'To (mail-header-extra header))))
2606         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2607         (default-mime-charset (with-current-buffer gnus-summary-buffer
2608                                 default-mime-charset)))
2609     (cond
2610      ((and to
2611            gnus-ignored-from-addresses
2612            (string-match gnus-ignored-from-addresses
2613                          (mail-header-from header)))
2614       (concat "-> "
2615               (or (car (funcall gnus-extract-address-components
2616                                 (funcall
2617                                  gnus-decode-encoded-word-function to)))
2618                   (funcall gnus-decode-encoded-word-function to))))
2619      ((and newsgroups
2620            gnus-ignored-from-addresses
2621            (string-match gnus-ignored-from-addresses
2622                          (mail-header-from header)))
2623       (concat "=> " newsgroups))
2624      (t
2625       (or (car (funcall gnus-extract-address-components
2626                         (mail-header-from header)))
2627           (mail-header-from header))))))
2628
2629 (defun gnus-summary-insert-line (gnus-tmp-header
2630                                  gnus-tmp-level gnus-tmp-current
2631                                  gnus-tmp-unread gnus-tmp-replied
2632                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2633                                  &optional gnus-tmp-dummy gnus-tmp-score
2634                                  gnus-tmp-process)
2635   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2636          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2637          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2638          (gnus-tmp-score-char
2639           (if (or (null gnus-summary-default-score)
2640                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2641                       gnus-summary-zcore-fuzz))
2642               ?  ;Whitespace
2643             (if (< gnus-tmp-score gnus-summary-default-score)
2644                 gnus-score-below-mark gnus-score-over-mark)))
2645          (gnus-tmp-replied
2646           (cond (gnus-tmp-process gnus-process-mark)
2647                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2648                  gnus-cached-mark)
2649                 (gnus-tmp-replied gnus-replied-mark)
2650                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2651                  gnus-saved-mark)
2652                 (t gnus-unread-mark)))
2653          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2654          (gnus-tmp-name
2655           (cond
2656            ((string-match "<[^>]+> *$" gnus-tmp-from)
2657             (let ((beg (match-beginning 0)))
2658               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2659                        (substring gnus-tmp-from (1+ (match-beginning 0))
2660                                   (1- (match-end 0))))
2661                   (substring gnus-tmp-from 0 beg))))
2662            ((string-match "(.+)" gnus-tmp-from)
2663             (substring gnus-tmp-from
2664                        (1+ (match-beginning 0)) (1- (match-end 0))))
2665            (t gnus-tmp-from)))
2666          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2667          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2668          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2669          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2670          (buffer-read-only nil))
2671     (when (string= gnus-tmp-name "")
2672       (setq gnus-tmp-name gnus-tmp-from))
2673     (unless (numberp gnus-tmp-lines)
2674       (setq gnus-tmp-lines 0))
2675     (gnus-put-text-property-excluding-characters-with-faces
2676      (point)
2677      (progn (eval gnus-summary-line-format-spec) (point))
2678      'gnus-number gnus-tmp-number)
2679     (when (gnus-visual-p 'summary-highlight 'highlight)
2680       (forward-line -1)
2681       (gnus-run-hooks 'gnus-summary-update-hook)
2682       (forward-line 1))))
2683
2684 (defun gnus-summary-update-line (&optional dont-update)
2685   ;; Update summary line after change.
2686   (when (and gnus-summary-default-score
2687              (not gnus-summary-inhibit-highlight))
2688     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2689            (article (gnus-summary-article-number))
2690            (score (gnus-summary-article-score article)))
2691       (unless dont-update
2692         (if (and gnus-summary-mark-below
2693                  (< (gnus-summary-article-score)
2694                     gnus-summary-mark-below))
2695             ;; This article has a low score, so we mark it as read.
2696             (when (memq article gnus-newsgroup-unreads)
2697               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2698           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2699             ;; This article was previously marked as read on account
2700             ;; of a low score, but now it has risen, so we mark it as
2701             ;; unread.
2702             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2703         (gnus-summary-update-mark
2704          (if (or (null gnus-summary-default-score)
2705                  (<= (abs (- score gnus-summary-default-score))
2706                      gnus-summary-zcore-fuzz))
2707              ?  ;Whitespace
2708            (if (< score gnus-summary-default-score)
2709                gnus-score-below-mark gnus-score-over-mark))
2710          'score))
2711       ;; Do visual highlighting.
2712       (when (gnus-visual-p 'summary-highlight 'highlight)
2713         (gnus-run-hooks 'gnus-summary-update-hook)))))
2714
2715 (defvar gnus-tmp-new-adopts nil)
2716
2717 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2718   "Return the number of articles in THREAD.
2719 This may be 0 in some cases -- if none of the articles in
2720 the thread are to be displayed."
2721   (let* ((number
2722           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2723           (cond
2724            ((not (listp thread))
2725             1)
2726            ((and (consp thread) (cdr thread))
2727             (apply
2728              '+ 1 (mapcar
2729                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2730            ((null thread)
2731             1)
2732            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2733             1)
2734            (t 0))))
2735     (when (and level (zerop level) gnus-tmp-new-adopts)
2736       (incf number
2737             (apply '+ (mapcar
2738                        'gnus-summary-number-of-articles-in-thread
2739                        gnus-tmp-new-adopts))))
2740     (if char
2741         (if (> number 1) gnus-not-empty-thread-mark
2742           gnus-empty-thread-mark)
2743       number)))
2744
2745 (defun gnus-summary-set-local-parameters (group)
2746   "Go through the local params of GROUP and set all variable specs in that list."
2747   (let ((params (gnus-group-find-parameter group))
2748         elem)
2749     (while params
2750       (setq elem (car params)
2751             params (cdr params))
2752       (and (consp elem)                 ; Has to be a cons.
2753            (consp (cdr elem))           ; The cdr has to be a list.
2754            (symbolp (car elem))         ; Has to be a symbol in there.
2755            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2756            (ignore-errors               ; So we set it.
2757              (make-local-variable (car elem))
2758              (set (car elem) (eval (nth 1 elem))))))))
2759
2760 (defun gnus-summary-read-group (group &optional show-all no-article
2761                                       kill-buffer no-display backward
2762                                       select-articles)
2763   "Start reading news in newsgroup GROUP.
2764 If SHOW-ALL is non-nil, already read articles are also listed.
2765 If NO-ARTICLE is non-nil, no article is selected initially.
2766 If NO-DISPLAY, don't generate a summary buffer."
2767   (let (result)
2768     (while (and group
2769                 (null (setq result
2770                             (let ((gnus-auto-select-next nil))
2771                               (or (gnus-summary-read-group-1
2772                                    group show-all no-article
2773                                    kill-buffer no-display
2774                                    select-articles)
2775                                   (setq show-all nil
2776                                         select-articles nil)))))
2777                 (eq gnus-auto-select-next 'quietly))
2778       (set-buffer gnus-group-buffer)
2779       ;; The entry function called above goes to the next
2780       ;; group automatically, so we go two groups back
2781       ;; if we are searching for the previous group.
2782       (when backward
2783         (gnus-group-prev-unread-group 2))
2784       (if (not (equal group (gnus-group-group-name)))
2785           (setq group (gnus-group-group-name))
2786         (setq group nil)))
2787     result))
2788
2789 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2790   "Directly jump to the other GROUP from summary buffer.
2791 If SHOW-ALL is non-nil, already read articles are also listed."
2792   (interactive
2793    (if (eq gnus-summary-buffer (current-buffer))
2794        (list (completing-read
2795               "Group: " gnus-active-hashtb nil t
2796               (when (and gnus-newsgroup-name
2797                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2798                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2799               'gnus-group-history)
2800              current-prefix-arg)
2801      (error "%s must be invoked from a gnus summary buffer." this-command)))
2802   (unless (or (zerop (length group))
2803               (and gnus-newsgroup-name
2804                    (string-equal gnus-newsgroup-name group)))
2805     (gnus-summary-exit)
2806     (gnus-summary-read-group group show-all
2807                              gnus-dont-select-after-jump-to-other-group)))
2808
2809 (defun gnus-summary-read-group-1 (group show-all no-article
2810                                         kill-buffer no-display
2811                                         &optional select-articles)
2812   ;; Killed foreign groups can't be entered.
2813   (when (and (not (gnus-group-native-p group))
2814              (not (gnus-gethash group gnus-newsrc-hashtb)))
2815     (error "Dead non-native groups can't be entered"))
2816   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2817   (let* ((new-group (gnus-summary-setup-buffer group))
2818          (quit-config (gnus-group-quit-config group))
2819          (did-select (and new-group (gnus-select-newsgroup
2820                                      group show-all select-articles))))
2821     (cond
2822      ;; This summary buffer exists already, so we just select it.
2823      ((not new-group)
2824       (gnus-set-global-variables)
2825       (when kill-buffer
2826         (gnus-kill-or-deaden-summary kill-buffer))
2827       (gnus-configure-windows 'summary 'force)
2828       (gnus-set-mode-line 'summary)
2829       (gnus-summary-position-point)
2830       (message "")
2831       t)
2832      ;; We couldn't select this group.
2833      ((null did-select)
2834       (when (and (eq major-mode 'gnus-summary-mode)
2835                  (not (equal (current-buffer) kill-buffer)))
2836         (kill-buffer (current-buffer))
2837         (if (not quit-config)
2838             (progn
2839               ;; Update the info -- marks might need to be removed,
2840               ;; for instance.
2841               (gnus-summary-update-info)
2842               (set-buffer gnus-group-buffer)
2843               (gnus-group-jump-to-group group)
2844               (gnus-group-next-unread-group 1))
2845           (gnus-handle-ephemeral-exit quit-config)))
2846       (gnus-message 3 "Can't select group")
2847       nil)
2848      ;; The user did a `C-g' while prompting for number of articles,
2849      ;; so we exit this group.
2850      ((eq did-select 'quit)
2851       (and (eq major-mode 'gnus-summary-mode)
2852            (not (equal (current-buffer) kill-buffer))
2853            (kill-buffer (current-buffer)))
2854       (when kill-buffer
2855         (gnus-kill-or-deaden-summary kill-buffer))
2856       (if (not quit-config)
2857           (progn
2858             (set-buffer gnus-group-buffer)
2859             (gnus-group-jump-to-group group)
2860             (gnus-group-next-unread-group 1)
2861             (gnus-configure-windows 'group 'force))
2862         (gnus-handle-ephemeral-exit quit-config))
2863       ;; Finally signal the quit.
2864       (signal 'quit nil))
2865      ;; The group was successfully selected.
2866      (t
2867       (gnus-set-global-variables)
2868       ;; Save the active value in effect when the group was entered.
2869       (setq gnus-newsgroup-active
2870             (gnus-copy-sequence
2871              (gnus-active gnus-newsgroup-name)))
2872       ;; You can change the summary buffer in some way with this hook.
2873       (gnus-run-hooks 'gnus-select-group-hook)
2874       ;; Set any local variables in the group parameters.
2875       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2876       (gnus-update-format-specifications
2877        nil 'summary 'summary-mode 'summary-dummy)
2878       (gnus-update-summary-mark-positions)
2879       ;; Do score processing.
2880       (when gnus-use-scoring
2881         (gnus-possibly-score-headers))
2882       ;; Check whether to fill in the gaps in the threads.
2883       (when gnus-build-sparse-threads
2884         (gnus-build-sparse-threads))
2885       ;; Find the initial limit.
2886       (if gnus-show-threads
2887           (if show-all
2888               (let ((gnus-newsgroup-dormant nil))
2889                 (gnus-summary-initial-limit show-all))
2890             (gnus-summary-initial-limit show-all))
2891         ;; When untreaded, all articles are always shown.
2892         (setq gnus-newsgroup-limit
2893               (mapcar
2894                (lambda (header) (mail-header-number header))
2895                gnus-newsgroup-headers)))
2896       ;; Generate the summary buffer.
2897       (unless no-display
2898         (gnus-summary-prepare))
2899       (when gnus-use-trees
2900         (gnus-tree-open group)
2901         (setq gnus-summary-highlight-line-function
2902               'gnus-tree-highlight-article))
2903       ;; If the summary buffer is empty, but there are some low-scored
2904       ;; articles or some excluded dormants, we include these in the
2905       ;; buffer.
2906       (when (and (zerop (buffer-size))
2907                  (not no-display))
2908         (cond (gnus-newsgroup-dormant
2909                (gnus-summary-limit-include-dormant))
2910               ((and gnus-newsgroup-scored show-all)
2911                (gnus-summary-limit-include-expunged t))))
2912       ;; Function `gnus-apply-kill-file' must be called in this hook.
2913       (gnus-run-hooks 'gnus-apply-kill-hook)
2914       (if (and (zerop (buffer-size))
2915                (not no-display))
2916           (progn
2917             ;; This newsgroup is empty.
2918             (gnus-summary-catchup-and-exit nil t)
2919             (gnus-message 6 "No unread news")
2920             (when kill-buffer
2921               (gnus-kill-or-deaden-summary kill-buffer))
2922             ;; Return nil from this function.
2923             nil)
2924         ;; Hide conversation thread subtrees.  We cannot do this in
2925         ;; gnus-summary-prepare-hook since kill processing may not
2926         ;; work with hidden articles.
2927         (and gnus-show-threads
2928              gnus-thread-hide-subtree
2929              (gnus-summary-hide-all-threads))
2930         (when kill-buffer
2931           (gnus-kill-or-deaden-summary kill-buffer))
2932         ;; Show first unread article if requested.
2933         (if (and (not no-article)
2934                  (not no-display)
2935                  gnus-newsgroup-unreads
2936                  gnus-auto-select-first)
2937             (progn
2938               (gnus-configure-windows 'summary)
2939               (cond
2940                ((eq gnus-auto-select-first 'best)
2941                 (gnus-summary-best-unread-article))
2942                ((eq gnus-auto-select-first t)
2943                 (gnus-summary-first-unread-article))
2944                ((gnus-functionp gnus-auto-select-first)
2945                 (funcall gnus-auto-select-first))))
2946           ;; Don't select any articles, just move point to the first
2947           ;; article in the group.
2948           (goto-char (point-min))
2949           (gnus-summary-position-point)
2950           (gnus-configure-windows 'summary 'force)
2951           (gnus-set-mode-line 'summary))
2952         (when (get-buffer-window gnus-group-buffer t)
2953           ;; Gotta use windows, because recenter does weird stuff if
2954           ;; the current buffer ain't the displayed window.
2955           (let ((owin (selected-window)))
2956             (select-window (get-buffer-window gnus-group-buffer t))
2957             (when (gnus-group-goto-group group)
2958               (recenter))
2959             (select-window owin)))
2960         ;; Mark this buffer as "prepared".
2961         (setq gnus-newsgroup-prepared t)
2962         (gnus-run-hooks 'gnus-summary-prepared-hook)
2963         t)))))
2964
2965 (defun gnus-summary-prepare ()
2966   "Generate the summary buffer."
2967   (interactive)
2968   (let ((buffer-read-only nil))
2969     (erase-buffer)
2970     (setq gnus-newsgroup-data nil
2971           gnus-newsgroup-data-reverse nil)
2972     (gnus-run-hooks 'gnus-summary-generate-hook)
2973     ;; Generate the buffer, either with threads or without.
2974     (when gnus-newsgroup-headers
2975       (gnus-summary-prepare-threads
2976        (if gnus-show-threads
2977            (gnus-sort-gathered-threads
2978             (funcall gnus-summary-thread-gathering-function
2979                      (gnus-sort-threads
2980                       (gnus-cut-threads (gnus-make-threads)))))
2981          ;; Unthreaded display.
2982          (gnus-sort-articles gnus-newsgroup-headers))))
2983     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2984     ;; Call hooks for modifying summary buffer.
2985     (goto-char (point-min))
2986     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2987
2988 (defsubst gnus-general-simplify-subject (subject)
2989   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2990   (setq subject
2991         (cond
2992          ;; Truncate the subject.
2993          (gnus-simplify-subject-functions
2994           (gnus-map-function gnus-simplify-subject-functions subject))
2995          ((numberp gnus-summary-gather-subject-limit)
2996           (setq subject (gnus-simplify-subject-re subject))
2997           (if (> (length subject) gnus-summary-gather-subject-limit)
2998               (substring subject 0 gnus-summary-gather-subject-limit)
2999             subject))
3000          ;; Fuzzily simplify it.
3001          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3002           (gnus-simplify-subject-fuzzy subject))
3003          ;; Just remove the leading "Re:".
3004          (t
3005           (gnus-simplify-subject-re subject))))
3006
3007   (if (and gnus-summary-gather-exclude-subject
3008            (string-match gnus-summary-gather-exclude-subject subject))
3009       nil                               ; This article shouldn't be gathered
3010     subject))
3011
3012 (defun gnus-summary-simplify-subject-query ()
3013   "Query where the respool algorithm would put this article."
3014   (interactive)
3015   (gnus-summary-select-article)
3016   (message "%s"
3017            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3018
3019 (defun gnus-gather-threads-by-subject (threads)
3020   "Gather threads by looking at Subject headers."
3021   (if (not gnus-summary-make-false-root)
3022       threads
3023     (let ((hashtb (gnus-make-hashtable 1024))
3024           (prev threads)
3025           (result threads)
3026           subject hthread whole-subject)
3027       (while threads
3028         (setq subject (gnus-general-simplify-subject
3029                        (setq whole-subject (mail-header-subject
3030                                             (caar threads)))))
3031         (when subject
3032           (if (setq hthread (gnus-gethash subject hashtb))
3033               (progn
3034                 ;; We enter a dummy root into the thread, if we
3035                 ;; haven't done that already.
3036                 (unless (stringp (caar hthread))
3037                   (setcar hthread (list whole-subject (car hthread))))
3038                 ;; We add this new gathered thread to this gathered
3039                 ;; thread.
3040                 (setcdr (car hthread)
3041                         (nconc (cdar hthread) (list (car threads))))
3042                 ;; Remove it from the list of threads.
3043                 (setcdr prev (cdr threads))
3044                 (setq threads prev))
3045             ;; Enter this thread into the hash table.
3046             (gnus-sethash subject threads hashtb)))
3047         (setq prev threads)
3048         (setq threads (cdr threads)))
3049       result)))
3050
3051 (defun gnus-gather-threads-by-references (threads)
3052   "Gather threads by looking at References headers."
3053   (let ((idhashtb (gnus-make-hashtable 1024))
3054         (thhashtb (gnus-make-hashtable 1024))
3055         (prev threads)
3056         (result threads)
3057         ids references id gthread gid entered ref)
3058     (while threads
3059       (when (setq references (mail-header-references (caar threads)))
3060         (setq id (mail-header-id (caar threads))
3061               ids (gnus-split-references references)
3062               entered nil)
3063         (while (setq ref (pop ids))
3064           (setq ids (delete ref ids))
3065           (if (not (setq gid (gnus-gethash ref idhashtb)))
3066               (progn
3067                 (gnus-sethash ref id idhashtb)
3068                 (gnus-sethash id threads thhashtb))
3069             (setq gthread (gnus-gethash gid thhashtb))
3070             (unless entered
3071               ;; We enter a dummy root into the thread, if we
3072               ;; haven't done that already.
3073               (unless (stringp (caar gthread))
3074                 (setcar gthread (list (mail-header-subject (caar gthread))
3075                                       (car gthread))))
3076               ;; We add this new gathered thread to this gathered
3077               ;; thread.
3078               (setcdr (car gthread)
3079                       (nconc (cdar gthread) (list (car threads)))))
3080             ;; Add it into the thread hash table.
3081             (gnus-sethash id gthread thhashtb)
3082             (setq entered t)
3083             ;; Remove it from the list of threads.
3084             (setcdr prev (cdr threads))
3085             (setq threads prev))))
3086       (setq prev threads)
3087       (setq threads (cdr threads)))
3088     result))
3089
3090 (defun gnus-sort-gathered-threads (threads)
3091   "Sort subtreads inside each gathered thread by article number."
3092   (let ((result threads))
3093     (while threads
3094       (when (stringp (caar threads))
3095         (setcdr (car threads)
3096                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3097       (setq threads (cdr threads)))
3098     result))
3099
3100 (defun gnus-thread-loop-p (root thread)
3101   "Say whether ROOT is in THREAD."
3102   (let ((stack (list thread))
3103         (infloop 0)
3104         th)
3105     (while (setq thread (pop stack))
3106       (setq th (cdr thread))
3107       (while (and th
3108                   (not (eq (caar th) root)))
3109         (pop th))
3110       (if th
3111           ;; We have found a loop.
3112           (let (ref-dep)
3113             (setcdr thread (delq (car th) (cdr thread)))
3114             (if (boundp (setq ref-dep (intern "none"
3115                                               gnus-newsgroup-dependencies)))
3116                 (setcdr (symbol-value ref-dep)
3117                         (nconc (cdr (symbol-value ref-dep))
3118                                (list (car th))))
3119               (set ref-dep (list nil (car th))))
3120             (setq infloop 1
3121                   stack nil))
3122         ;; Push all the subthreads onto the stack.
3123         (push (cdr thread) stack)))
3124     infloop))
3125
3126 (defun gnus-make-threads ()
3127   "Go through the dependency hashtb and find the roots.  Return all threads."
3128   (let (threads)
3129     (while (catch 'infloop
3130              (mapatoms
3131               (lambda (refs)
3132                 ;; Deal with self-referencing References loops.
3133                 (when (and (car (symbol-value refs))
3134                            (not (zerop
3135                                  (apply
3136                                   '+
3137                                   (mapcar
3138                                    (lambda (thread)
3139                                      (gnus-thread-loop-p
3140                                       (car (symbol-value refs)) thread))
3141                                    (cdr (symbol-value refs)))))))
3142                   (setq threads nil)
3143                   (throw 'infloop t))
3144                 (unless (car (symbol-value refs))
3145                   ;; These threads do not refer back to any other articles,
3146                   ;; so they're roots.
3147                   (setq threads (append (cdr (symbol-value refs)) threads))))
3148               gnus-newsgroup-dependencies)))
3149     threads))
3150
3151 ;; Build the thread tree.
3152 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3153   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3154
3155 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3156 if it was already present.
3157
3158 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3159 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3160 Message-IDs will be renamed be renamed to a unique Message-ID before
3161 being entered.
3162
3163 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3164   (let* ((id (mail-header-id header))
3165          (id-dep (and id (intern id dependencies)))
3166          ref ref-dep ref-header)
3167     ;; Enter this `header' in the `dependencies' table.
3168     (cond
3169      ((not id-dep)
3170       (setq header nil))
3171      ;; The first two cases do the normal part: enter a new `header'
3172      ;; in the `dependencies' table.
3173      ((not (boundp id-dep))
3174       (set id-dep (list header)))
3175      ((null (car (symbol-value id-dep)))
3176       (setcar (symbol-value id-dep) header))
3177
3178      ;; From here the `header' was already present in the
3179      ;; `dependencies' table.
3180      (force-new
3181       ;; Overrides an existing entry;
3182       ;; just set the header part of the entry.
3183       (setcar (symbol-value id-dep) header))
3184
3185      ;; Renames the existing `header' to a unique Message-ID.
3186      ((not gnus-summary-ignore-duplicates)
3187       ;; An article with this Message-ID has already been seen.
3188       ;; We rename the Message-ID.
3189       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3190            (list header))
3191       (mail-header-set-id header id))
3192
3193      ;; The last case ignores an existing entry, except it adds any
3194      ;; additional Xrefs (in case the two articles came from different
3195      ;; servers.
3196      ;; Also sets `header' to `nil' meaning that the `dependencies'
3197      ;; table was *not* modified.
3198      (t
3199       (mail-header-set-xref
3200        (car (symbol-value id-dep))
3201        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3202                    "")
3203                (or (mail-header-xref header) "")))
3204       (setq header nil)))
3205
3206     (when header
3207       ;; First check if that we are not creating a References loop.
3208       (setq ref (gnus-parent-id (mail-header-references header)))
3209       (while (and ref
3210                   (setq ref-dep (intern-soft ref dependencies))
3211                   (boundp ref-dep)
3212                   (setq ref-header (car (symbol-value ref-dep))))
3213         (if (string= id ref)
3214             ;; Yuk!  This is a reference loop.  Make the article be a
3215             ;; root article.
3216             (progn
3217               (mail-header-set-references (car (symbol-value id-dep)) "none")
3218               (setq ref nil))
3219           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3220       (setq ref (gnus-parent-id (mail-header-references header)))
3221       (setq ref-dep (intern (or ref "none") dependencies))
3222       (if (boundp ref-dep)
3223           (setcdr (symbol-value ref-dep)
3224                   (nconc (cdr (symbol-value ref-dep))
3225                          (list (symbol-value id-dep))))
3226         (set ref-dep (list nil (symbol-value id-dep)))))
3227     header))
3228
3229 (defun gnus-build-sparse-threads ()
3230   (let ((headers gnus-newsgroup-headers)
3231         (mail-parse-charset gnus-newsgroup-charset)
3232         (gnus-summary-ignore-duplicates t)
3233         header references generation relations
3234         subject child end new-child date)
3235     ;; First we create an alist of generations/relations, where
3236     ;; generations is how much we trust the relation, and the relation
3237     ;; is parent/child.
3238     (gnus-message 7 "Making sparse threads...")
3239     (save-excursion
3240       (nnheader-set-temp-buffer " *gnus sparse threads*")
3241       (while (setq header (pop headers))
3242         (when (and (setq references (mail-header-references header))
3243                    (not (string= references "")))
3244           (insert references)
3245           (setq child (mail-header-id header)
3246                 subject (mail-header-subject header)
3247                 date (mail-header-date header)
3248                 generation 0)
3249           (while (search-backward ">" nil t)
3250             (setq end (1+ (point)))
3251             (when (search-backward "<" nil t)
3252               (setq new-child (buffer-substring (point) end))
3253               (push (list (incf generation)
3254                           child (setq child new-child)
3255                           subject date)
3256                     relations)))
3257           (when child
3258             (push (list (1+ generation) child nil subject) relations))
3259           (erase-buffer)))
3260       (kill-buffer (current-buffer)))
3261     ;; Sort over trustworthiness.
3262     (mapcar
3263      (lambda (relation)
3264        (when (gnus-dependencies-add-header
3265               (make-full-mail-header
3266                gnus-reffed-article-number
3267                (nth 3 relation) "" (or (nth 4 relation) "")
3268                (nth 1 relation)
3269                (or (nth 2 relation) "") 0 0 "")
3270               gnus-newsgroup-dependencies nil)
3271          (push gnus-reffed-article-number gnus-newsgroup-limit)
3272          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3273          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3274                gnus-newsgroup-reads)
3275          (decf gnus-reffed-article-number)))
3276      (sort relations 'car-less-than-car))
3277     (gnus-message 7 "Making sparse threads...done")))
3278
3279 (defun gnus-build-old-threads ()
3280   ;; Look at all the articles that refer back to old articles, and
3281   ;; fetch the headers for the articles that aren't there.  This will
3282   ;; build complete threads - if the roots haven't been expired by the
3283   ;; server, that is.
3284   (let ((mail-parse-charset gnus-newsgroup-charset)
3285         id heads)
3286     (mapatoms
3287      (lambda (refs)
3288        (when (not (car (symbol-value refs)))
3289          (setq heads (cdr (symbol-value refs)))
3290          (while heads
3291            (if (memq (mail-header-number (caar heads))
3292                      gnus-newsgroup-dormant)
3293                (setq heads (cdr heads))
3294              (setq id (symbol-name refs))
3295              (while (and (setq id (gnus-build-get-header id))
3296                          (not (car (gnus-id-to-thread id)))))
3297              (setq heads nil)))))
3298      gnus-newsgroup-dependencies)))
3299
3300 ;; This function has to be called with point after the article number
3301 ;; on the beginning of the line.
3302 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3303   (let ((eol (gnus-point-at-eol))
3304         (buffer (current-buffer))
3305         header)
3306
3307     ;; overview: [num subject from date id refs chars lines misc]
3308     (unless (eobp)
3309       (forward-char))
3310
3311     (setq header
3312           (make-full-mail-header
3313            number                               ; number
3314            (nnheader-nov-field)                 ; subject
3315            (nnheader-nov-field)                 ; from
3316            (nnheader-nov-field)                 ; date
3317            (nnheader-nov-read-message-id)       ; id
3318            (nnheader-nov-field)                 ; refs
3319            (nnheader-nov-read-integer)          ; chars
3320            (nnheader-nov-read-integer)          ; lines
3321            (unless (eobp)
3322              (nnheader-nov-field))              ; misc
3323            (nnheader-nov-parse-extra)))         ; extra
3324
3325     (when gnus-alter-header-function
3326       (funcall gnus-alter-header-function header))
3327     (gnus-dependencies-add-header header dependencies force-new)))
3328
3329 (defun gnus-build-get-header (id)
3330   ;; Look through the buffer of NOV lines and find the header to
3331   ;; ID.  Enter this line into the dependencies hash table, and return
3332   ;; the id of the parent article (if any).
3333   (let ((deps gnus-newsgroup-dependencies)
3334         found header)
3335     (prog1
3336         (save-excursion
3337           (set-buffer nntp-server-buffer)
3338           (let ((case-fold-search nil))
3339             (goto-char (point-min))
3340             (while (and (not found)
3341                         (search-forward id nil t))
3342               (beginning-of-line)
3343               (setq found (looking-at
3344                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3345                                    (regexp-quote id))))
3346               (or found (beginning-of-line 2)))
3347             (when found
3348               (beginning-of-line)
3349               (and
3350                (setq header (gnus-nov-parse-line
3351                              (read (current-buffer)) deps))
3352                (gnus-parent-id (mail-header-references header))))))
3353       (when header
3354         (let ((number (mail-header-number header)))
3355           (push number gnus-newsgroup-limit)
3356           (push header gnus-newsgroup-headers)
3357           (if (memq number gnus-newsgroup-unselected)
3358               (progn
3359                 (push number gnus-newsgroup-unreads)
3360                 (setq gnus-newsgroup-unselected
3361                       (delq number gnus-newsgroup-unselected)))
3362             (push number gnus-newsgroup-ancient)))))))
3363
3364 (defun gnus-build-all-threads ()
3365   "Read all the headers."
3366   (let ((gnus-summary-ignore-duplicates t)
3367         (mail-parse-charset gnus-newsgroup-charset)
3368         (dependencies gnus-newsgroup-dependencies)
3369         header article)
3370     (save-excursion
3371       (set-buffer nntp-server-buffer)
3372       (let ((case-fold-search nil))
3373         (goto-char (point-min))
3374         (while (not (eobp))
3375           (ignore-errors
3376             (setq article (read (current-buffer))
3377                   header (gnus-nov-parse-line article dependencies)))
3378           (when header
3379             (save-excursion
3380               (set-buffer gnus-summary-buffer)
3381               (push header gnus-newsgroup-headers)
3382               (if (memq (setq article (mail-header-number header))
3383                         gnus-newsgroup-unselected)
3384                   (progn
3385                     (push article gnus-newsgroup-unreads)
3386                     (setq gnus-newsgroup-unselected
3387                           (delq article gnus-newsgroup-unselected)))
3388                 (push article gnus-newsgroup-ancient)))
3389             (forward-line 1)))))))
3390
3391 (defun gnus-summary-update-article-line (article header)
3392   "Update the line for ARTICLE using HEADERS."
3393   (let* ((id (mail-header-id header))
3394          (thread (gnus-id-to-thread id)))
3395     (unless thread
3396       (error "Article in no thread"))
3397     ;; Update the thread.
3398     (setcar thread header)
3399     (gnus-summary-goto-subject article)
3400     (let* ((datal (gnus-data-find-list article))
3401            (data (car datal))
3402            (length (when (cdr datal)
3403                      (- (gnus-data-pos data)
3404                         (gnus-data-pos (cadr datal)))))
3405            (buffer-read-only nil)
3406            (level (gnus-summary-thread-level)))
3407       (gnus-delete-line)
3408       (gnus-summary-insert-line
3409        header level nil (gnus-article-mark article)
3410        (memq article gnus-newsgroup-replied)
3411        (memq article gnus-newsgroup-expirable)
3412        ;; Only insert the Subject string when it's different
3413        ;; from the previous Subject string.
3414        (if (and
3415             gnus-show-threads
3416             (gnus-subject-equal
3417              (condition-case ()
3418                  (mail-header-subject
3419                   (gnus-data-header
3420                    (cadr
3421                     (gnus-data-find-list
3422                      article
3423                      (gnus-data-list t)))))
3424                ;; Error on the side of excessive subjects.
3425                (error ""))
3426              (mail-header-subject header)))
3427            ""
3428          (mail-header-subject header))
3429        nil (cdr (assq article gnus-newsgroup-scored))
3430        (memq article gnus-newsgroup-processable))
3431       (when length
3432         (gnus-data-update-list
3433          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3434
3435 (defun gnus-summary-update-article (article &optional iheader)
3436   "Update ARTICLE in the summary buffer."
3437   (set-buffer gnus-summary-buffer)
3438   (let* ((header (gnus-summary-article-header article))
3439          (id (mail-header-id header))
3440          (data (gnus-data-find article))
3441          (thread (gnus-id-to-thread id))
3442          (references (mail-header-references header))
3443          (parent
3444           (gnus-id-to-thread
3445            (or (gnus-parent-id
3446                 (when (and references
3447                            (not (equal "" references)))
3448                   references))
3449                "none")))
3450          (buffer-read-only nil)
3451          (old (car thread)))
3452     (when thread
3453       (unless iheader
3454         (setcar thread nil)
3455         (when parent
3456           (delq thread parent)))
3457       (if (gnus-summary-insert-subject id header)
3458           ;; Set the (possibly) new article number in the data structure.
3459           (gnus-data-set-number data (gnus-id-to-article id))
3460         (setcar thread old)
3461         nil))))
3462
3463 (defun gnus-rebuild-thread (id &optional line)
3464   "Rebuild the thread containing ID.
3465 If LINE, insert the rebuilt thread starting on line LINE."
3466   (let ((buffer-read-only nil)
3467         old-pos current thread data)
3468     (if (not gnus-show-threads)
3469         (setq thread (list (car (gnus-id-to-thread id))))
3470       ;; Get the thread this article is part of.
3471       (setq thread (gnus-remove-thread id)))
3472     (setq old-pos (gnus-point-at-bol))
3473     (setq current (save-excursion
3474                     (and (zerop (forward-line -1))
3475                          (gnus-summary-article-number))))
3476     ;; If this is a gathered thread, we have to go some re-gathering.
3477     (when (stringp (car thread))
3478       (let ((subject (car thread))
3479             roots thr)
3480         (setq thread (cdr thread))
3481         (while thread
3482           (unless (memq (setq thr (gnus-id-to-thread
3483                                    (gnus-root-id
3484                                     (mail-header-id (caar thread)))))
3485                         roots)
3486             (push thr roots))
3487           (setq thread (cdr thread)))
3488         ;; We now have all (unique) roots.
3489         (if (= (length roots) 1)
3490             ;; All the loose roots are now one solid root.
3491             (setq thread (car roots))
3492           (setq thread (cons subject (gnus-sort-threads roots))))))
3493     (let (threads)
3494       ;; We then insert this thread into the summary buffer.
3495       (when line
3496         (goto-char (point-min))
3497         (forward-line (1- line)))
3498       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3499         (if gnus-show-threads
3500             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3501           (gnus-summary-prepare-unthreaded thread))
3502         (setq data (nreverse gnus-newsgroup-data))
3503         (setq threads gnus-newsgroup-threads))
3504       ;; We splice the new data into the data structure.
3505       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3506       ;;!!! then we want to insert at the beginning of the buffer.
3507       ;;!!! That happens to be true with Gnus now, but that may
3508       ;;!!! change in the future.  Perhaps.
3509       (gnus-data-enter-list
3510        (if line nil current) data (- (point) old-pos))
3511       (setq gnus-newsgroup-threads
3512             (nconc threads gnus-newsgroup-threads))
3513       (gnus-data-compute-positions))))
3514
3515 (defun gnus-number-to-header (number)
3516   "Return the header for article NUMBER."
3517   (let ((headers gnus-newsgroup-headers))
3518     (while (and headers
3519                 (not (= number (mail-header-number (car headers)))))
3520       (pop headers))
3521     (when headers
3522       (car headers))))
3523
3524 (defun gnus-parent-headers (in-headers &optional generation)
3525   "Return the headers of the GENERATIONeth parent of HEADERS."
3526   (unless generation
3527     (setq generation 1))
3528   (let ((parent t)
3529         (headers in-headers)
3530         references)
3531     (while (and parent
3532                 (not (zerop generation))
3533                 (setq references (mail-header-references headers)))
3534       (setq headers (if (and references
3535                              (setq parent (gnus-parent-id references)))
3536                         (car (gnus-id-to-thread parent))
3537                       nil))
3538       (decf generation))
3539     (and (not (eq headers in-headers))
3540          headers)))
3541
3542 (defun gnus-id-to-thread (id)
3543   "Return the (sub-)thread where ID appears."
3544   (gnus-gethash id gnus-newsgroup-dependencies))
3545
3546 (defun gnus-id-to-article (id)
3547   "Return the article number of ID."
3548   (let ((thread (gnus-id-to-thread id)))
3549     (when (and thread
3550                (car thread))
3551       (mail-header-number (car thread)))))
3552
3553 (defun gnus-id-to-header (id)
3554   "Return the article headers of ID."
3555   (car (gnus-id-to-thread id)))
3556
3557 (defun gnus-article-displayed-root-p (article)
3558   "Say whether ARTICLE is a root(ish) article."
3559   (let ((level (gnus-summary-thread-level article))
3560         (refs (mail-header-references  (gnus-summary-article-header article)))
3561         particle)
3562     (cond
3563      ((null level) nil)
3564      ((zerop level) t)
3565      ((null refs) t)
3566      ((null (gnus-parent-id refs)) t)
3567      ((and (= 1 level)
3568            (null (setq particle (gnus-id-to-article
3569                                  (gnus-parent-id refs))))
3570            (null (gnus-summary-thread-level particle)))))))
3571
3572 (defun gnus-root-id (id)
3573   "Return the id of the root of the thread where ID appears."
3574   (let (last-id prev)
3575     (while (and id (setq prev (car (gnus-id-to-thread id))))
3576       (setq last-id id
3577             id (gnus-parent-id (mail-header-references prev))))
3578     last-id))
3579
3580 (defun gnus-articles-in-thread (thread)
3581   "Return the list of articles in THREAD."
3582   (cons (mail-header-number (car thread))
3583         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3584
3585 (defun gnus-remove-thread (id &optional dont-remove)
3586   "Remove the thread that has ID in it."
3587   (let (headers thread last-id)
3588     ;; First go up in this thread until we find the root.
3589     (setq last-id (gnus-root-id id)
3590           headers (message-flatten-list (gnus-id-to-thread last-id)))
3591     ;; We have now found the real root of this thread.  It might have
3592     ;; been gathered into some loose thread, so we have to search
3593     ;; through the threads to find the thread we wanted.
3594     (let ((threads gnus-newsgroup-threads)
3595           sub)
3596       (while threads
3597         (setq sub (car threads))
3598         (if (stringp (car sub))
3599             ;; This is a gathered thread, so we look at the roots
3600             ;; below it to find whether this article is in this
3601             ;; gathered root.
3602             (progn
3603               (setq sub (cdr sub))
3604               (while sub
3605                 (when (member (caar sub) headers)
3606                   (setq thread (car threads)
3607                         threads nil
3608                         sub nil))
3609                 (setq sub (cdr sub))))
3610           ;; It's an ordinary thread, so we check it.
3611           (when (eq (car sub) (car headers))
3612             (setq thread sub
3613                   threads nil)))
3614         (setq threads (cdr threads)))
3615       ;; If this article is in no thread, then it's a root.
3616       (if thread
3617           (unless dont-remove
3618             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3619         (setq thread (gnus-id-to-thread last-id)))
3620       (when thread
3621         (prog1
3622             thread                      ; We return this thread.
3623           (unless dont-remove
3624             (if (stringp (car thread))
3625                 (progn
3626                   ;; If we use dummy roots, then we have to remove the
3627                   ;; dummy root as well.
3628                   (when (eq gnus-summary-make-false-root 'dummy)
3629                     ;; We go to the dummy root by going to
3630                     ;; the first sub-"thread", and then one line up.
3631                     (gnus-summary-goto-article
3632                      (mail-header-number (caadr thread)))
3633                     (forward-line -1)
3634                     (gnus-delete-line)
3635                     (gnus-data-compute-positions))
3636                   (setq thread (cdr thread))
3637                   (while thread
3638                     (gnus-remove-thread-1 (car thread))
3639                     (setq thread (cdr thread))))
3640               (gnus-remove-thread-1 thread))))))))
3641
3642 (defun gnus-remove-thread-1 (thread)
3643   "Remove the thread THREAD recursively."
3644   (let ((number (mail-header-number (pop thread)))
3645         d)
3646     (setq thread (reverse thread))
3647     (while thread
3648       (gnus-remove-thread-1 (pop thread)))
3649     (when (setq d (gnus-data-find number))
3650       (goto-char (gnus-data-pos d))
3651       (gnus-summary-show-thread)
3652       (gnus-data-remove
3653        number
3654        (- (gnus-point-at-bol)
3655           (prog1
3656               (1+ (gnus-point-at-eol))
3657             (gnus-delete-line)))))))
3658
3659 (defun gnus-sort-threads (threads)
3660   "Sort THREADS."
3661   (if (not gnus-thread-sort-functions)
3662       threads
3663     (gnus-message 8 "Sorting threads...")
3664     (prog1
3665         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3666       (gnus-message 8 "Sorting threads...done"))))
3667
3668 (defun gnus-sort-articles (articles)
3669   "Sort ARTICLES."
3670   (when gnus-article-sort-functions
3671     (gnus-message 7 "Sorting articles...")
3672     (prog1
3673         (setq gnus-newsgroup-headers
3674               (sort articles (gnus-make-sort-function
3675                               gnus-article-sort-functions)))
3676       (gnus-message 7 "Sorting articles...done"))))
3677
3678 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3679 (defmacro gnus-thread-header (thread)
3680   ;; Return header of first article in THREAD.
3681   ;; Note that THREAD must never, ever be anything else than a variable -
3682   ;; using some other form will lead to serious barfage.
3683   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3684   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3685   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" 
3686         (vector thread) 2))
3687
3688 (defsubst gnus-article-sort-by-number (h1 h2)
3689   "Sort articles by article number."
3690   (< (mail-header-number h1)
3691      (mail-header-number h2)))
3692
3693 (defun gnus-thread-sort-by-number (h1 h2)
3694   "Sort threads by root article number."
3695   (gnus-article-sort-by-number
3696    (gnus-thread-header h1) (gnus-thread-header h2)))
3697
3698 (defsubst gnus-article-sort-by-lines (h1 h2)
3699   "Sort articles by article Lines header."
3700   (< (mail-header-lines h1)
3701      (mail-header-lines h2)))
3702
3703 (defun gnus-thread-sort-by-lines (h1 h2)
3704   "Sort threads by root article Lines header."
3705   (gnus-article-sort-by-lines
3706    (gnus-thread-header h1) (gnus-thread-header h2)))
3707
3708 (defsubst gnus-article-sort-by-chars (h1 h2)
3709   "Sort articles by octet length."
3710   (< (mail-header-chars h1)
3711      (mail-header-chars h2)))
3712
3713 (defun gnus-thread-sort-by-chars (h1 h2)
3714   "Sort threads by root article octet length."
3715   (gnus-article-sort-by-chars
3716    (gnus-thread-header h1) (gnus-thread-header h2)))
3717
3718 (defsubst gnus-article-sort-by-author (h1 h2)
3719   "Sort articles by root author."
3720   (string-lessp
3721    (let ((addr (car (mime-entity-read-field h1 'From))))
3722      (or (std11-full-name-string addr)
3723          (std11-address-string addr)
3724          ""))
3725    (let ((addr (car (mime-entity-read-field h2 'From))))
3726      (or (std11-full-name-string addr)
3727          (std11-address-string addr)
3728          ""))
3729    ))
3730
3731 (defun gnus-thread-sort-by-author (h1 h2)
3732   "Sort threads by root author."
3733   (gnus-article-sort-by-author
3734    (gnus-thread-header h1)  (gnus-thread-header h2)))
3735
3736 (defsubst gnus-article-sort-by-subject (h1 h2)
3737   "Sort articles by root subject."
3738   (string-lessp
3739    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3740    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3741
3742 (defun gnus-thread-sort-by-subject (h1 h2)
3743   "Sort threads by root subject."
3744   (gnus-article-sort-by-subject
3745    (gnus-thread-header h1) (gnus-thread-header h2)))
3746
3747 (defsubst gnus-article-sort-by-date (h1 h2)
3748   "Sort articles by root article date."
3749   (time-less-p
3750    (gnus-date-get-time (mail-header-date h1))
3751    (gnus-date-get-time (mail-header-date h2))))
3752
3753 (defun gnus-thread-sort-by-date (h1 h2)
3754   "Sort threads by root article date."
3755   (gnus-article-sort-by-date
3756    (gnus-thread-header h1) (gnus-thread-header h2)))
3757
3758 (defsubst gnus-article-sort-by-score (h1 h2)
3759   "Sort articles by root article score.
3760 Unscored articles will be counted as having a score of zero."
3761   (> (or (cdr (assq (mail-header-number h1)
3762                     gnus-newsgroup-scored))
3763          gnus-summary-default-score 0)
3764      (or (cdr (assq (mail-header-number h2)
3765                     gnus-newsgroup-scored))
3766          gnus-summary-default-score 0)))
3767
3768 (defun gnus-thread-sort-by-score (h1 h2)
3769   "Sort threads by root article score."
3770   (gnus-article-sort-by-score
3771    (gnus-thread-header h1) (gnus-thread-header h2)))
3772
3773 (defun gnus-thread-sort-by-total-score (h1 h2)
3774   "Sort threads by the sum of all scores in the thread.
3775 Unscored articles will be counted as having a score of zero."
3776   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3777
3778 (defun gnus-thread-total-score (thread)
3779   ;; This function find the total score of THREAD.
3780   (cond ((null thread)
3781          0)
3782         ((consp thread)
3783          (if (stringp (car thread))
3784              (apply gnus-thread-score-function 0
3785                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3786            (gnus-thread-total-score-1 thread)))
3787         (t
3788          (gnus-thread-total-score-1 (list thread)))))
3789
3790 (defun gnus-thread-total-score-1 (root)
3791   ;; This function find the total score of the thread below ROOT.
3792   (setq root (car root))
3793   (apply gnus-thread-score-function
3794          (or (append
3795               (mapcar 'gnus-thread-total-score
3796                       (cdr (gnus-id-to-thread (mail-header-id root))))
3797               (when (> (mail-header-number root) 0)
3798                 (list (or (cdr (assq (mail-header-number root)
3799                                      gnus-newsgroup-scored))
3800                           gnus-summary-default-score 0))))
3801              (list gnus-summary-default-score)
3802              '(0))))
3803
3804 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3805 (defvar gnus-tmp-prev-subject nil)
3806 (defvar gnus-tmp-false-parent nil)
3807 (defvar gnus-tmp-root-expunged nil)
3808 (defvar gnus-tmp-dummy-line nil)
3809
3810 (defvar gnus-tmp-header)
3811 (defun gnus-extra-header (type &optional header)
3812   "Return the extra header of TYPE."
3813   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3814       ""))
3815
3816 (defun gnus-summary-prepare-threads (threads)
3817   "Prepare summary buffer from THREADS and indentation LEVEL.
3818 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3819 or a straight list of headers."
3820   (gnus-message 7 "Generating summary...")
3821
3822   (setq gnus-newsgroup-threads threads)
3823   (beginning-of-line)
3824
3825   (let ((gnus-tmp-level 0)
3826         (default-score (or gnus-summary-default-score 0))
3827         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3828         thread number subject stack state gnus-tmp-gathered beg-match
3829         new-roots gnus-tmp-new-adopts thread-end
3830         gnus-tmp-header gnus-tmp-unread
3831         gnus-tmp-replied gnus-tmp-subject-or-nil
3832         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3833         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3834         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3835
3836     (setq gnus-tmp-prev-subject nil)
3837
3838     (if (vectorp (car threads))
3839         ;; If this is a straight (sic) list of headers, then a
3840         ;; threaded summary display isn't required, so we just create
3841         ;; an unthreaded one.
3842         (gnus-summary-prepare-unthreaded threads)
3843
3844       ;; Do the threaded display.
3845
3846       (while (or threads stack gnus-tmp-new-adopts new-roots)
3847
3848         (if (and (= gnus-tmp-level 0)
3849                  (or (not stack)
3850                      (= (caar stack) 0))
3851                  (not gnus-tmp-false-parent)
3852                  (or gnus-tmp-new-adopts new-roots))
3853             (if gnus-tmp-new-adopts
3854                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3855                       thread (list (car gnus-tmp-new-adopts))
3856                       gnus-tmp-header (caar thread)
3857                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3858               (when new-roots
3859                 (setq thread (list (car new-roots))
3860                       gnus-tmp-header (caar thread)
3861                       new-roots (cdr new-roots))))
3862
3863           (if threads
3864               ;; If there are some threads, we do them before the
3865               ;; threads on the stack.
3866               (setq thread threads
3867                     gnus-tmp-header (caar thread))
3868             ;; There were no current threads, so we pop something off
3869             ;; the stack.
3870             (setq state (car stack)
3871                   gnus-tmp-level (car state)
3872                   thread (cdr state)
3873                   stack (cdr stack)
3874                   gnus-tmp-header (caar thread))))
3875
3876         (setq gnus-tmp-false-parent nil)
3877         (setq gnus-tmp-root-expunged nil)
3878         (setq thread-end nil)
3879
3880         (if (stringp gnus-tmp-header)
3881             ;; The header is a dummy root.
3882             (cond
3883              ((eq gnus-summary-make-false-root 'adopt)
3884               ;; We let the first article adopt the rest.
3885               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3886                                                (cddar thread)))
3887               (setq gnus-tmp-gathered
3888                     (nconc (mapcar
3889                             (lambda (h) (mail-header-number (car h)))
3890                             (cddar thread))
3891                            gnus-tmp-gathered))
3892               (setq thread (cons (list (caar thread)
3893                                        (cadar thread))
3894                                  (cdr thread)))
3895               (setq gnus-tmp-level -1
3896                     gnus-tmp-false-parent t))
3897              ((eq gnus-summary-make-false-root 'empty)
3898               ;; We print adopted articles with empty subject fields.
3899               (setq gnus-tmp-gathered
3900                     (nconc (mapcar
3901                             (lambda (h) (mail-header-number (car h)))
3902                             (cddar thread))
3903                            gnus-tmp-gathered))
3904               (setq gnus-tmp-level -1))
3905              ((eq gnus-summary-make-false-root 'dummy)
3906               ;; We remember that we probably want to output a dummy
3907               ;; root.
3908               (setq gnus-tmp-dummy-line gnus-tmp-header)
3909               (setq gnus-tmp-prev-subject gnus-tmp-header))
3910              (t
3911               ;; We do not make a root for the gathered
3912               ;; sub-threads at all.
3913               (setq gnus-tmp-level -1)))
3914
3915           (setq number (mail-header-number gnus-tmp-header)
3916                 subject (mail-header-subject gnus-tmp-header))
3917
3918           (cond
3919            ;; If the thread has changed subject, we might want to make
3920            ;; this subthread into a root.
3921            ((and (null gnus-thread-ignore-subject)
3922                  (not (zerop gnus-tmp-level))
3923                  gnus-tmp-prev-subject
3924                  (not (inline
3925                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3926             (setq new-roots (nconc new-roots (list (car thread)))
3927                   thread-end t
3928                   gnus-tmp-header nil))
3929            ;; If the article lies outside the current limit,
3930            ;; then we do not display it.
3931            ((not (memq number gnus-newsgroup-limit))
3932             (setq gnus-tmp-gathered
3933                   (nconc (mapcar
3934                           (lambda (h) (mail-header-number (car h)))
3935                           (cdar thread))
3936                          gnus-tmp-gathered))
3937             (setq gnus-tmp-new-adopts (if (cdar thread)
3938                                           (append gnus-tmp-new-adopts
3939                                                   (cdar thread))
3940                                         gnus-tmp-new-adopts)
3941                   thread-end t
3942                   gnus-tmp-header nil)
3943             (when (zerop gnus-tmp-level)
3944               (setq gnus-tmp-root-expunged t)))
3945            ;; Perhaps this article is to be marked as read?
3946            ((and gnus-summary-mark-below
3947                  (< (or (cdr (assq number gnus-newsgroup-scored))
3948                         default-score)
3949                     gnus-summary-mark-below)
3950                  ;; Don't touch sparse articles.
3951                  (not (gnus-summary-article-sparse-p number))
3952                  (not (gnus-summary-article-ancient-p number)))
3953             (setq gnus-newsgroup-unreads
3954                   (delq number gnus-newsgroup-unreads))
3955             (if gnus-newsgroup-auto-expire
3956                 (push number gnus-newsgroup-expirable)
3957               (push (cons number gnus-low-score-mark)
3958                     gnus-newsgroup-reads))))
3959
3960           (when gnus-tmp-header
3961             ;; We may have an old dummy line to output before this
3962             ;; article.
3963             (when (and gnus-tmp-dummy-line
3964                        (gnus-subject-equal
3965                         gnus-tmp-dummy-line
3966                         (mail-header-subject gnus-tmp-header)))
3967               (gnus-summary-insert-dummy-line
3968                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3969               (setq gnus-tmp-dummy-line nil))
3970
3971             ;; Compute the mark.
3972             (setq gnus-tmp-unread (gnus-article-mark number))
3973
3974             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3975                                   gnus-tmp-header gnus-tmp-level)
3976                   gnus-newsgroup-data)
3977
3978             ;; Actually insert the line.
3979             (setq
3980              gnus-tmp-subject-or-nil
3981              (cond
3982               ((and gnus-thread-ignore-subject
3983                     gnus-tmp-prev-subject
3984                     (not (inline (gnus-subject-equal
3985                                   gnus-tmp-prev-subject subject))))
3986                subject)
3987               ((zerop gnus-tmp-level)
3988                (if (and (eq gnus-summary-make-false-root 'empty)
3989                         (memq number gnus-tmp-gathered)
3990                         gnus-tmp-prev-subject
3991                         (inline (gnus-subject-equal
3992                                  gnus-tmp-prev-subject subject)))
3993                    gnus-summary-same-subject
3994                  subject))
3995               (t gnus-summary-same-subject)))
3996             (if (and (eq gnus-summary-make-false-root 'adopt)
3997                      (= gnus-tmp-level 1)
3998                      (memq number gnus-tmp-gathered))
3999                 (setq gnus-tmp-opening-bracket ?\<
4000                       gnus-tmp-closing-bracket ?\>)
4001               (setq gnus-tmp-opening-bracket ?\[
4002                     gnus-tmp-closing-bracket ?\]))
4003             (setq
4004              gnus-tmp-indentation
4005              (aref gnus-thread-indent-array gnus-tmp-level)
4006              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4007              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4008                                 gnus-summary-default-score 0)
4009              gnus-tmp-score-char
4010              (if (or (null gnus-summary-default-score)
4011                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4012                          gnus-summary-zcore-fuzz))
4013                  ?  ;Whitespace
4014                (if (< gnus-tmp-score gnus-summary-default-score)
4015                    gnus-score-below-mark gnus-score-over-mark))
4016              gnus-tmp-replied
4017              (cond ((memq number gnus-newsgroup-processable)
4018                     gnus-process-mark)
4019                    ((memq number gnus-newsgroup-cached)
4020                     gnus-cached-mark)
4021                    ((memq number gnus-newsgroup-replied)
4022                     gnus-replied-mark)
4023                    ((memq number gnus-newsgroup-saved)
4024                     gnus-saved-mark)
4025                    (t gnus-unread-mark))
4026              gnus-tmp-from (mail-header-from gnus-tmp-header)
4027              gnus-tmp-name
4028              (cond
4029               ((string-match "<[^>]+> *$" gnus-tmp-from)
4030                (setq beg-match (match-beginning 0))
4031                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4032                         (substring gnus-tmp-from (1+ (match-beginning 0))
4033                                    (1- (match-end 0))))
4034                    (substring gnus-tmp-from 0 beg-match)))
4035               ((string-match "(.+)" gnus-tmp-from)
4036                (substring gnus-tmp-from
4037                           (1+ (match-beginning 0)) (1- (match-end 0))))
4038               (t gnus-tmp-from)))
4039             (when (string= gnus-tmp-name "")
4040               (setq gnus-tmp-name gnus-tmp-from))
4041             (unless (numberp gnus-tmp-lines)
4042               (setq gnus-tmp-lines 0))
4043             (gnus-put-text-property
4044              (point)
4045              (progn (eval gnus-summary-line-format-spec) (point))
4046              'gnus-number number)
4047             (when gnus-visual-p
4048               (forward-line -1)
4049               (gnus-run-hooks 'gnus-summary-update-hook)
4050               (forward-line 1))
4051
4052             (setq gnus-tmp-prev-subject subject)))
4053
4054         (when (nth 1 thread)
4055           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4056         (incf gnus-tmp-level)
4057         (setq threads (if thread-end nil (cdar thread)))
4058         (unless threads
4059           (setq gnus-tmp-level 0)))))
4060   (gnus-message 7 "Generating summary...done"))
4061
4062 (defun gnus-summary-prepare-unthreaded (headers)
4063   "Generate an unthreaded summary buffer based on HEADERS."
4064   (let (header number mark)
4065
4066     (beginning-of-line)
4067
4068     (while headers
4069       ;; We may have to root out some bad articles...
4070       (when (memq (setq number (mail-header-number
4071                                 (setq header (pop headers))))
4072                   gnus-newsgroup-limit)
4073         ;; Mark article as read when it has a low score.
4074         (when (and gnus-summary-mark-below
4075                    (< (or (cdr (assq number gnus-newsgroup-scored))
4076                           gnus-summary-default-score 0)
4077                       gnus-summary-mark-below)
4078                    (not (gnus-summary-article-ancient-p number)))
4079           (setq gnus-newsgroup-unreads
4080                 (delq number gnus-newsgroup-unreads))
4081           (if gnus-newsgroup-auto-expire
4082               (push number gnus-newsgroup-expirable)
4083             (push (cons number gnus-low-score-mark)
4084                   gnus-newsgroup-reads)))
4085
4086         (setq mark (gnus-article-mark number))
4087         (push (gnus-data-make number mark (1+ (point)) header 0)
4088               gnus-newsgroup-data)
4089         (gnus-summary-insert-line
4090          header 0 number
4091          mark (memq number gnus-newsgroup-replied)
4092          (memq number gnus-newsgroup-expirable)
4093          (mail-header-subject header) nil
4094          (cdr (assq number gnus-newsgroup-scored))
4095          (memq number gnus-newsgroup-processable))))))
4096
4097 (defun gnus-summary-remove-list-identifiers ()
4098   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4099   (let ((regexp (if (stringp gnus-list-identifiers)
4100                     gnus-list-identifiers
4101                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4102     (dolist (header gnus-newsgroup-headers)
4103       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4104                                   " *\\)\\)+\\(Re: +\\)?\\)")
4105                           (mail-header-subject header))
4106         (mail-header-set-subject
4107          header (concat (substring (mail-header-subject header)
4108                                    0 (match-beginning 1))
4109                         (or
4110                          (match-string 3 (mail-header-subject header))
4111                          (match-string 5 (mail-header-subject header)))
4112                         (substring (mail-header-subject header)
4113                                    (match-end 1))))))))
4114
4115 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4116   "Select newsgroup GROUP.
4117 If READ-ALL is non-nil, all articles in the group are selected.
4118 If SELECT-ARTICLES, only select those articles from GROUP."
4119   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4120          ;;!!! Dirty hack; should be removed.
4121          (gnus-summary-ignore-duplicates
4122           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4123               t
4124             gnus-summary-ignore-duplicates))
4125          (info (nth 2 entry))
4126          articles fetched-articles cached)
4127
4128     (unless (gnus-check-server
4129              (setq gnus-current-select-method
4130                    (gnus-find-method-for-group group)))
4131       (error "Couldn't open server"))
4132
4133     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4134         (gnus-activate-group group)     ; Or we can activate it...
4135         (progn                          ; Or we bug out.
4136           (when (equal major-mode 'gnus-summary-mode)
4137             (kill-buffer (current-buffer)))
4138           (error "Couldn't request group %s: %s"
4139                  group (gnus-status-message group))))
4140
4141     (unless (gnus-request-group group t)
4142       (when (equal major-mode 'gnus-summary-mode)
4143         (kill-buffer (current-buffer)))
4144       (error "Couldn't request group %s: %s"
4145              group (gnus-status-message group)))
4146
4147     (setq gnus-newsgroup-name group)
4148     (setq gnus-newsgroup-unselected nil)
4149     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4150     (gnus-summary-setup-default-charset)
4151
4152     ;; Adjust and set lists of article marks.
4153     (when info
4154       (gnus-adjust-marked-articles info))
4155
4156     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4157     (setq cached
4158           (if (gnus-virtual-group-p group)
4159               gnus-newsgroup-cached
4160             (gnus-cache-articles-in-group group)))
4161
4162     (setq gnus-newsgroup-unreads
4163           (gnus-set-difference
4164            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4165            gnus-newsgroup-dormant))
4166
4167     (setq gnus-newsgroup-processable nil)
4168
4169     (gnus-update-read-articles group gnus-newsgroup-unreads)
4170
4171     (if (setq articles select-articles)
4172         (setq gnus-newsgroup-unselected
4173               (gnus-sorted-intersection
4174                gnus-newsgroup-unreads
4175                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4176       (setq articles (gnus-articles-to-read group read-all)))
4177
4178     (cond
4179      ((null articles)
4180       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4181       'quit)
4182      ((eq articles 0) nil)
4183      (t
4184       ;; Init the dependencies hash table.
4185       (setq gnus-newsgroup-dependencies
4186             (gnus-make-hashtable (length articles)))
4187       (gnus-set-global-variables)
4188       ;; Retrieve the headers and read them in.
4189       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4190       (setq gnus-newsgroup-headers
4191             (gnus-retrieve-parsed-headers
4192              articles gnus-newsgroup-name
4193              ;; We might want to fetch old headers, but
4194              ;; not if there is only 1 article.
4195              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4196                            (not (numberp gnus-fetch-old-headers)))
4197                       (> (length articles) 1))
4198                   gnus-fetch-old-headers)))
4199       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4200
4201       ;; Suppress duplicates?
4202       (when gnus-suppress-duplicates
4203         (gnus-dup-suppress-articles))
4204
4205       ;; Set the initial limit.
4206       (setq gnus-newsgroup-limit (copy-sequence articles))
4207       ;; Remove canceled articles from the list of unread articles.
4208       (setq gnus-newsgroup-unreads
4209             (gnus-set-sorted-intersection
4210              gnus-newsgroup-unreads
4211              (setq fetched-articles
4212                    (mapcar (lambda (headers) (mail-header-number headers))
4213                            gnus-newsgroup-headers))))
4214       ;; Removed marked articles that do not exist.
4215       (gnus-update-missing-marks
4216        (gnus-sorted-complement fetched-articles articles))
4217
4218       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4219       (when cached
4220         (setq gnus-newsgroup-cached cached))
4221
4222       ;; We might want to build some more threads first.
4223       (when (and gnus-fetch-old-headers
4224                  (eq gnus-headers-retrieved-by 'nov))
4225         (if (eq gnus-fetch-old-headers 'invisible)
4226             (gnus-build-all-threads)
4227           (gnus-build-old-threads)))
4228       ;; Let the Gnus agent mark articles as read.
4229       (when gnus-agent
4230         (gnus-agent-get-undownloaded-list))
4231       ;; Remove list identifiers from subject
4232       (when gnus-list-identifiers
4233         (gnus-summary-remove-list-identifiers))
4234       ;; Check whether auto-expire is to be done in this group.
4235       (setq gnus-newsgroup-auto-expire
4236             (gnus-group-auto-expirable-p group))
4237       ;; Set up the article buffer now, if necessary.
4238       (unless gnus-single-article-buffer
4239         (gnus-article-setup-buffer))
4240       ;; First and last article in this newsgroup.
4241       (when gnus-newsgroup-headers
4242         (setq gnus-newsgroup-begin
4243               (mail-header-number (car gnus-newsgroup-headers))
4244               gnus-newsgroup-end
4245               (mail-header-number
4246                (gnus-last-element gnus-newsgroup-headers))))
4247       ;; GROUP is successfully selected.
4248       (or gnus-newsgroup-headers t)))))
4249
4250 (defun gnus-articles-to-read (group &optional read-all)
4251   ;; Find out what articles the user wants to read.
4252   (let* ((articles
4253           ;; Select all articles if `read-all' is non-nil, or if there
4254           ;; are no unread articles.
4255           (if (or read-all
4256                   (and (zerop (length gnus-newsgroup-marked))
4257                        (zerop (length gnus-newsgroup-unreads)))
4258                   (eq (gnus-group-find-parameter group 'display)
4259                       'all))
4260               (or
4261                (gnus-uncompress-range (gnus-active group))
4262                (gnus-cache-articles-in-group group))
4263             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4264                           (copy-sequence gnus-newsgroup-unreads))
4265                   '<)))
4266          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4267          (scored (length scored-list))
4268          (number (length articles))
4269          (marked (+ (length gnus-newsgroup-marked)
4270                     (length gnus-newsgroup-dormant)))
4271          (select
4272           (cond
4273            ((numberp read-all)
4274             read-all)
4275            (t
4276             (condition-case ()
4277                 (cond
4278                  ((and (or (<= scored marked) (= scored number))
4279                        (natnump gnus-large-newsgroup)
4280                        (> number gnus-large-newsgroup))
4281                   (let* ((cursor-in-echo-area nil)
4282                          (input (read-from-minibuffer
4283                                  (format
4284                                   "How many articles from %s (max %d): "
4285                                   (gnus-limit-string gnus-newsgroup-name 35)
4286                                   number)
4287                                  (cons (number-to-string gnus-large-newsgroup)
4288                                        0))))
4289                     (if (string-match "^[ \t]*$" input)
4290                         number
4291                       input)))
4292                  ((and (> scored marked) (< scored number)
4293                        (> (- scored number) 20))
4294                   (let ((input
4295                          (read-string
4296                           (format "%s %s (%d scored, %d total): "
4297                                   "How many articles from"
4298                                   group scored number))))
4299                     (if (string-match "^[ \t]*$" input)
4300                         number input)))
4301                  (t number))
4302               (quit nil))))))
4303     (setq select (if (stringp select) (string-to-number select) select))
4304     (if (or (null select) (zerop select))
4305         select
4306       (if (and (not (zerop scored)) (<= (abs select) scored))
4307           (progn
4308             (setq articles (sort scored-list '<))
4309             (setq number (length articles)))
4310         (setq articles (copy-sequence articles)))
4311
4312       (when (< (abs select) number)
4313         (if (< select 0)
4314             ;; Select the N oldest articles.
4315             (setcdr (nthcdr (1- (abs select)) articles) nil)
4316           ;; Select the N most recent articles.
4317           (setq articles (nthcdr (- number select) articles))))
4318       (setq gnus-newsgroup-unselected
4319             (gnus-sorted-intersection
4320              gnus-newsgroup-unreads
4321              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4322       (when gnus-alter-articles-to-read-function
4323         (setq gnus-newsgroup-unreads
4324               (sort 
4325                (funcall gnus-alter-articles-to-read-function
4326                         gnus-newsgroup-name gnus-newsgroup-unreads)
4327                '<)))
4328       articles)))
4329
4330 (defun gnus-killed-articles (killed articles)
4331   (let (out)
4332     (while articles
4333       (when (inline (gnus-member-of-range (car articles) killed))
4334         (push (car articles) out))
4335       (setq articles (cdr articles)))
4336     out))
4337
4338 (defun gnus-uncompress-marks (marks)
4339   "Uncompress the mark ranges in MARKS."
4340   (let ((uncompressed '(score bookmark))
4341         out)
4342     (while marks
4343       (if (memq (caar marks) uncompressed)
4344           (push (car marks) out)
4345         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4346       (setq marks (cdr marks)))
4347     out))
4348
4349 (defun gnus-adjust-marked-articles (info)
4350   "Set all article lists and remove all marks that are no longer valid."
4351   (let* ((marked-lists (gnus-info-marks info))
4352          (active (gnus-active (gnus-info-group info)))
4353          (min (car active))
4354          (max (cdr active))
4355          (types gnus-article-mark-lists)
4356          (uncompressed '(score bookmark killed))
4357          marks var articles article mark)
4358
4359     (while marked-lists
4360       (setq marks (pop marked-lists))
4361       (set (setq var (intern (format "gnus-newsgroup-%s"
4362                                      (car (rassq (setq mark (car marks))
4363                                                  types)))))
4364            (if (memq (car marks) uncompressed) (cdr marks)
4365              (gnus-uncompress-range (cdr marks))))
4366
4367       (setq articles (symbol-value var))
4368
4369       ;; All articles have to be subsets of the active articles.
4370       (cond
4371        ;; Adjust "simple" lists.
4372        ((memq mark '(tick dormant expire reply save))
4373         (while articles
4374           (when (or (< (setq article (pop articles)) min) (> article max))
4375             (set var (delq article (symbol-value var))))))
4376        ;; Adjust assocs.
4377        ((memq mark uncompressed)
4378         (when (not (listp (cdr (symbol-value var))))
4379           (set var (list (symbol-value var))))
4380         (when (not (listp (cdr articles)))
4381           (setq articles (list articles)))
4382         (while articles
4383           (when (or (not (consp (setq article (pop articles))))
4384                     (< (car article) min)
4385                     (> (car article) max))
4386             (set var (delq article (symbol-value var))))))))))
4387
4388 (defun gnus-update-missing-marks (missing)
4389   "Go through the list of MISSING articles and remove them from the mark lists."
4390   (when missing
4391     (let ((types gnus-article-mark-lists)
4392           var m)
4393       ;; Go through all types.
4394       (while types
4395         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4396         (when (symbol-value var)
4397           ;; This list has articles.  So we delete all missing articles
4398           ;; from it.
4399           (setq m missing)
4400           (while m
4401             (set var (delq (pop m) (symbol-value var)))))))))
4402
4403 (defun gnus-update-marks ()
4404   "Enter the various lists of marked articles into the newsgroup info list."
4405   (let ((types gnus-article-mark-lists)
4406         (info (gnus-get-info gnus-newsgroup-name))
4407         (uncompressed '(score bookmark killed))
4408         type list newmarked symbol delta-marks)
4409     (when info
4410       ;; Add all marks lists to the list of marks lists.
4411       (while (setq type (pop types))
4412         (setq list (symbol-value
4413                     (setq symbol
4414                           (intern (format "gnus-newsgroup-%s"
4415                                           (car type))))))
4416
4417         (when list
4418           ;; Get rid of the entries of the articles that have the
4419           ;; default score.
4420           (when (and (eq (cdr type) 'score)
4421                      gnus-save-score
4422                      list)
4423             (let* ((arts list)
4424                    (prev (cons nil list))
4425                    (all prev))
4426               (while arts
4427                 (if (or (not (consp (car arts)))
4428                         (= (cdar arts) gnus-summary-default-score))
4429                     (setcdr prev (cdr arts))
4430                   (setq prev arts))
4431                 (setq arts (cdr arts)))
4432               (setq list (cdr all)))))
4433
4434         (unless (memq (cdr type) uncompressed)
4435           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4436        
4437         (when (gnus-check-backend-function
4438                'request-set-mark gnus-newsgroup-name)
4439           ;; uncompressed:s are not proper flags (they are cons cells)
4440           ;; cache is a internal gnus flag
4441           (unless (memq (cdr type) (cons 'cache uncompressed))
4442             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4443                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4444                    (add (gnus-remove-from-range
4445                          (gnus-copy-sequence list) old)))
4446               (when add
4447                 (push (list add 'add (list (cdr type))) delta-marks))
4448               (when del
4449                 (push (list del 'del (list (cdr type))) delta-marks)))))
4450           
4451         (when list
4452           (push (cons (cdr type) list) newmarked)))
4453
4454       (when delta-marks
4455         (unless (gnus-check-group gnus-newsgroup-name)
4456           (error "Can't open server for %s" gnus-newsgroup-name))
4457         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4458           
4459       ;; Enter these new marks into the info of the group.
4460       (if (nthcdr 3 info)
4461           (setcar (nthcdr 3 info) newmarked)
4462         ;; Add the marks lists to the end of the info.
4463         (when newmarked
4464           (setcdr (nthcdr 2 info) (list newmarked))))
4465
4466       ;; Cut off the end of the info if there's nothing else there.
4467       (let ((i 5))
4468         (while (and (> i 2)
4469                     (not (nth i info)))
4470           (when (nthcdr (decf i) info)
4471             (setcdr (nthcdr i info) nil)))))))
4472
4473 (defun gnus-set-mode-line (where)
4474   "This function sets the mode line of the article or summary buffers.
4475 If WHERE is `summary', the summary mode line format will be used."
4476   ;; Is this mode line one we keep updated?
4477   (when (and (memq where gnus-updated-mode-lines)
4478              (symbol-value
4479               (intern (format "gnus-%s-mode-line-format-spec" where))))
4480     (let (mode-string)
4481       (save-excursion
4482         ;; We evaluate this in the summary buffer since these
4483         ;; variables are buffer-local to that buffer.
4484         (set-buffer gnus-summary-buffer)
4485         ;; We bind all these variables that are used in the `eval' form
4486         ;; below.
4487         (let* ((mformat (symbol-value
4488                          (intern
4489                           (format "gnus-%s-mode-line-format-spec" where))))
4490                (gnus-tmp-group-name gnus-newsgroup-name)
4491                (gnus-tmp-article-number (or gnus-current-article 0))
4492                (gnus-tmp-unread gnus-newsgroup-unreads)
4493                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4494                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4495                (gnus-tmp-unread-and-unselected
4496                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4497                             (zerop gnus-tmp-unselected))
4498                        "")
4499                       ((zerop gnus-tmp-unselected)
4500                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4501                       (t (format "{%d(+%d) more}"
4502                                  gnus-tmp-unread-and-unticked
4503                                  gnus-tmp-unselected))))
4504                (gnus-tmp-subject
4505                 (if (and gnus-current-headers
4506                          (vectorp gnus-current-headers))
4507                     (gnus-mode-string-quote
4508                      (mail-header-subject gnus-current-headers))
4509                   ""))
4510                bufname-length max-len
4511                gnus-tmp-header);; passed as argument to any user-format-funcs
4512           (setq mode-string (eval mformat))
4513           (setq bufname-length (if (string-match "%b" mode-string)
4514                                    (- (length
4515                                        (buffer-name
4516                                         (if (eq where 'summary)
4517                                             nil
4518                                           (get-buffer gnus-article-buffer))))
4519                                       2)
4520                                  0))
4521           (setq max-len (max 4 (if gnus-mode-non-string-length
4522                                    (- (window-width)
4523                                       gnus-mode-non-string-length
4524                                       bufname-length)
4525                                  (length mode-string))))
4526           ;; We might have to chop a bit of the string off...
4527           (when (> (length mode-string) max-len)
4528             (setq mode-string
4529                   (concat (gnus-truncate-string mode-string (- max-len 3))
4530                           "...")))
4531           ;; Pad the mode string a bit.
4532           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4533       ;; Update the mode line.
4534       (setq mode-line-buffer-identification
4535             (gnus-mode-line-buffer-identification (list mode-string)))
4536       (set-buffer-modified-p t))))
4537
4538 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4539   "Go through the HEADERS list and add all Xrefs to a hash table.
4540 The resulting hash table is returned, or nil if no Xrefs were found."
4541   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4542          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4543          (xref-hashtb (gnus-make-hashtable))
4544          start group entry number xrefs header)
4545     (while headers
4546       (setq header (pop headers))
4547       (when (and (setq xrefs (mail-header-xref header))
4548                  (not (memq (setq number (mail-header-number header))
4549                             unreads)))
4550         (setq start 0)
4551         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4552           (setq start (match-end 0))
4553           (setq group (if prefix
4554                           (concat prefix (substring xrefs (match-beginning 1)
4555                                                     (match-end 1)))
4556                         (substring xrefs (match-beginning 1) (match-end 1))))
4557           (setq number
4558                 (string-to-int (substring xrefs (match-beginning 2)
4559                                           (match-end 2))))
4560           (if (setq entry (gnus-gethash group xref-hashtb))
4561               (setcdr entry (cons number (cdr entry)))
4562             (gnus-sethash group (cons number nil) xref-hashtb)))))
4563     (and start xref-hashtb)))
4564
4565 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4566   "Look through all the headers and mark the Xrefs as read."
4567   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4568         name entry info xref-hashtb idlist method nth4)
4569     (save-excursion
4570       (set-buffer gnus-group-buffer)
4571       (when (setq xref-hashtb
4572                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4573         (mapatoms
4574          (lambda (group)
4575            (unless (string= from-newsgroup (setq name (symbol-name group)))
4576              (setq idlist (symbol-value group))
4577              ;; Dead groups are not updated.
4578              (and (prog1
4579                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4580                             info (nth 2 entry))
4581                     (when (stringp (setq nth4 (gnus-info-method info)))
4582                       (setq nth4 (gnus-server-to-method nth4))))
4583                   ;; Only do the xrefs if the group has the same
4584                   ;; select method as the group we have just read.
4585                   (or (gnus-methods-equal-p
4586                        nth4 (gnus-find-method-for-group from-newsgroup))
4587                       virtual
4588                       (equal nth4 (setq method (gnus-find-method-for-group
4589                                                 from-newsgroup)))
4590                       (and (equal (car nth4) (car method))
4591                            (equal (nth 1 nth4) (nth 1 method))))
4592                   gnus-use-cross-reference
4593                   (or (not (eq gnus-use-cross-reference t))
4594                       virtual
4595                       ;; Only do cross-references on subscribed
4596                       ;; groups, if that is what is wanted.
4597                       (<= (gnus-info-level info) gnus-level-subscribed))
4598                   (gnus-group-make-articles-read name idlist))))
4599          xref-hashtb)))))
4600
4601 (defun gnus-compute-read-articles (group articles)
4602   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4603          (info (nth 2 entry))
4604          (active (gnus-active group))
4605          ninfo)
4606     (when entry
4607       ;; First peel off all invalid article numbers.
4608       (when active
4609         (let ((ids articles)
4610               id first)
4611           (while (setq id (pop ids))
4612             (when (and first (> id (cdr active)))
4613               ;; We'll end up in this situation in one particular
4614               ;; obscure situation.  If you re-scan a group and get
4615               ;; a new article that is cross-posted to a different
4616               ;; group that has not been re-scanned, you might get
4617               ;; crossposted article that has a higher number than
4618               ;; Gnus believes possible.  So we re-activate this
4619               ;; group as well.  This might mean doing the
4620               ;; crossposting thingy will *increase* the number
4621               ;; of articles in some groups.  Tsk, tsk.
4622               (setq active (or (gnus-activate-group group) active)))
4623             (when (or (> id (cdr active))
4624                       (< id (car active)))
4625               (setq articles (delq id articles))))))
4626       ;; If the read list is nil, we init it.
4627       (if (and active
4628                (null (gnus-info-read info))
4629                (> (car active) 1))
4630           (setq ninfo (cons 1 (1- (car active))))
4631         (setq ninfo (gnus-info-read info)))
4632       ;; Then we add the read articles to the range.
4633       (gnus-add-to-range
4634        ninfo (setq articles (sort articles '<))))))
4635
4636 (defun gnus-group-make-articles-read (group articles)
4637   "Update the info of GROUP to say that ARTICLES are read."
4638   (let* ((num 0)
4639          (entry (gnus-gethash group gnus-newsrc-hashtb))
4640          (info (nth 2 entry))
4641          (active (gnus-active group))
4642          range)
4643     (when entry
4644       (setq range (gnus-compute-read-articles group articles))
4645       (save-excursion
4646         (set-buffer gnus-group-buffer)
4647         (gnus-undo-register
4648           `(progn
4649              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4650              (gnus-info-set-read ',info ',(gnus-info-read info))
4651              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4652              (gnus-group-update-group ,group t))))
4653       ;; Add the read articles to the range.
4654       (gnus-info-set-read info range)
4655       ;; Then we have to re-compute how many unread
4656       ;; articles there are in this group.
4657       (when active
4658         (cond
4659          ((not range)
4660           (setq num (- (1+ (cdr active)) (car active))))
4661          ((not (listp (cdr range)))
4662           (setq num (- (cdr active) (- (1+ (cdr range))
4663                                        (car range)))))
4664          (t
4665           (while range
4666             (if (numberp (car range))
4667                 (setq num (1+ num))
4668               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4669             (setq range (cdr range)))
4670           (setq num (- (cdr active) num))))
4671         ;; Update the number of unread articles.
4672         (setcar entry num)
4673         ;; Update the group buffer.
4674         (gnus-group-update-group group t)))))
4675
4676 (defvar gnus-newsgroup-none-id 0)
4677
4678 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4679   (let ((cur nntp-server-buffer)
4680         (dependencies
4681          (or dependencies
4682              (save-excursion (set-buffer gnus-summary-buffer)
4683                              gnus-newsgroup-dependencies)))
4684         headers id end ref
4685         (mail-parse-charset gnus-newsgroup-charset)
4686         (mail-parse-ignored-charsets
4687          (save-excursion (condition-case nil
4688                              (set-buffer gnus-summary-buffer)
4689                            (error))
4690                          gnus-newsgroup-ignored-charsets)))
4691     (save-excursion
4692       (set-buffer nntp-server-buffer)
4693       ;; Translate all TAB characters into SPACE characters.
4694       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4695       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4696       (gnus-run-hooks 'gnus-parse-headers-hook)
4697       (let ((case-fold-search t)
4698             in-reply-to header p lines chars ctype)
4699         (goto-char (point-min))
4700         ;; Search to the beginning of the next header.  Error messages
4701         ;; do not begin with 2 or 3.
4702         (while (re-search-forward "^[23][0-9]+ " nil t)
4703           (setq id nil
4704                 ref nil)
4705           ;; This implementation of this function, with nine
4706           ;; search-forwards instead of the one re-search-forward and
4707           ;; a case (which basically was the old function) is actually
4708           ;; about twice as fast, even though it looks messier.  You
4709           ;; can't have everything, I guess.  Speed and elegance
4710           ;; doesn't always go hand in hand.
4711           (setq
4712            header
4713            (make-full-mail-header
4714             ;; Number.
4715             (prog1
4716                 (read cur)
4717               (end-of-line)
4718               (setq p (point))
4719               (narrow-to-region (point)
4720                                 (or (and (search-forward "\n.\n" nil t)
4721                                          (- (point) 2))
4722                                     (point))))
4723             ;; Subject.
4724             (progn
4725               (goto-char p)
4726               (if (search-forward "\nsubject: " nil t)
4727                   (buffer-substring (match-end 0) (std11-field-end))
4728                 "(none)"))
4729             ;; From.
4730             (progn
4731               (goto-char p)
4732               (if (search-forward "\nfrom: " nil t)
4733                   (buffer-substring (match-end 0) (std11-field-end))
4734                 "(nobody)"))
4735             ;; Date.
4736             (progn
4737               (goto-char p)
4738               (if (search-forward "\ndate: " nil t)
4739                   (buffer-substring (match-end 0) (std11-field-end))
4740                 ""))
4741             ;; Message-ID.
4742             (progn
4743               (goto-char p)
4744               (setq id (if (re-search-forward
4745                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4746                            ;; We do it this way to make sure the Message-ID
4747                            ;; is (somewhat) syntactically valid.
4748                            (buffer-substring (match-beginning 1)
4749                                              (match-end 1))
4750                          ;; If there was no message-id, we just fake one
4751                          ;; to make subsequent routines simpler.
4752                          (nnheader-generate-fake-message-id))))
4753             ;; References.
4754             (progn
4755               (goto-char p)
4756               (if (search-forward "\nreferences: " nil t)
4757                   (progn
4758                     (setq end (point))
4759                     (prog1
4760                         (buffer-substring (match-end 0) (std11-field-end))
4761                       (setq ref
4762                             (buffer-substring
4763                              (progn
4764                                ;; (end-of-line)
4765                                (search-backward ">" end t)
4766                                (1+ (point)))
4767                              (progn
4768                                (search-backward "<" end t)
4769                                (point))))))
4770                 ;; Get the references from the in-reply-to header if there
4771                 ;; were no references and the in-reply-to header looks
4772                 ;; promising.
4773                 (if (and (search-forward "\nin-reply-to: " nil t)
4774                          (setq in-reply-to
4775                                (buffer-substring (match-end 0)
4776                                                  (std11-field-end)))
4777                          (string-match "<[^>]+>" in-reply-to))
4778                     (let (ref2)
4779                       (setq ref (substring in-reply-to (match-beginning 0)
4780                                            (match-end 0)))
4781                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4782                         (setq ref2 (substring in-reply-to (match-beginning 0)
4783                                               (match-end 0)))
4784                         (when (> (length ref2) (length ref))
4785                           (setq ref ref2)))
4786                       ref)
4787                   (setq ref nil))))
4788             ;; Chars.
4789             (progn
4790               (goto-char p)
4791               (if (search-forward "\nchars: " nil t)
4792                   (if (numberp (setq chars (ignore-errors (read cur))))
4793                       chars 0)
4794                 0))
4795             ;; Lines.
4796             (progn
4797               (goto-char p)
4798               (if (search-forward "\nlines: " nil t)
4799                   (if (numberp (setq lines (ignore-errors (read cur))))
4800                       lines 0)
4801                 0))
4802             ;; Xref.
4803             (progn
4804               (goto-char p)
4805               (and (search-forward "\nxref: " nil t)
4806                    (buffer-substring (match-end 0) (std11-field-end))))
4807             ;; Extra.
4808             (when gnus-extra-headers
4809               (let ((extra gnus-extra-headers)
4810                     out)
4811                 (while extra
4812                   (goto-char p)
4813                   (when (search-forward
4814                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4815                     (push (cons (car extra)
4816                                 (buffer-substring (match-end 0)
4817                                                   (std11-field-end)))
4818                           out))
4819                   (pop extra))
4820                 out))))
4821           (goto-char p)
4822           (if (and (search-forward "\ncontent-type: " nil t)
4823                    (setq ctype
4824                          (buffer-substring (match-end 0) (std11-field-end))))
4825               (mime-entity-set-content-type-internal
4826                header (mime-parse-Content-Type ctype)))
4827           (when (equal id ref)
4828             (setq ref nil))
4829
4830           (when gnus-alter-header-function
4831             (funcall gnus-alter-header-function header)
4832             (setq id (mail-header-id header)
4833                   ref (gnus-parent-id (mail-header-references header))))
4834
4835           (when (setq header
4836                       (gnus-dependencies-add-header
4837                        header dependencies force-new))
4838             (push header headers))
4839           (goto-char (point-max))
4840           (widen))
4841         (nreverse headers)))))
4842
4843 ;; Goes through the xover lines and returns a list of vectors
4844 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4845                                                   force-new dependencies
4846                                                   group also-fetch-heads)
4847   "Parse the news overview data in the server buffer, and return a
4848 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4849   ;; Get the Xref when the users reads the articles since most/some
4850   ;; NNTP servers do not include Xrefs when using XOVER.
4851   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4852   (let ((mail-parse-charset gnus-newsgroup-charset)
4853         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4854         (cur nntp-server-buffer)
4855         (dependencies (or dependencies gnus-newsgroup-dependencies))
4856         number headers header)
4857     (save-excursion
4858       (set-buffer nntp-server-buffer)
4859       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4860       ;; Allow the user to mangle the headers before parsing them.
4861       (gnus-run-hooks 'gnus-parse-headers-hook)
4862       (goto-char (point-min))
4863       (while (not (eobp))
4864         (condition-case ()
4865             (while (and sequence (not (eobp)))
4866               (setq number (read cur))
4867               (while (and sequence
4868                           (< (car sequence) number))
4869                 (setq sequence (cdr sequence)))
4870               (and sequence
4871                    (eq number (car sequence))
4872                    (progn
4873                      (setq sequence (cdr sequence))
4874                      (setq header (inline
4875                                     (gnus-nov-parse-line
4876                                      number dependencies force-new))))
4877                    (push header headers))
4878               (forward-line 1))
4879           (error
4880            (gnus-error 4 "Strange nov line (%d)"
4881                        (count-lines (point-min) (point)))))
4882         (forward-line 1))
4883       ;; A common bug in inn is that if you have posted an article and
4884       ;; then retrieves the active file, it will answer correctly --
4885       ;; the new article is included.  However, a NOV entry for the
4886       ;; article may not have been generated yet, so this may fail.
4887       ;; We work around this problem by retrieving the last few
4888       ;; headers using HEAD.
4889       (if (or (not also-fetch-heads)
4890               (not sequence))
4891           ;; We (probably) got all the headers.
4892           (nreverse headers)
4893         (let ((gnus-nov-is-evil t))
4894           (nconc
4895            (nreverse headers)
4896            (gnus-retrieve-parsed-headers sequence group)
4897            ))))))
4898
4899 (defun gnus-article-get-xrefs ()
4900   "Fill in the Xref value in `gnus-current-headers', if necessary.
4901 This is meant to be called in `gnus-article-internal-prepare-hook'."
4902   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4903                                  gnus-current-headers)))
4904     (or (not gnus-use-cross-reference)
4905         (not headers)
4906         (and (mail-header-xref headers)
4907              (not (string= (mail-header-xref headers) "")))
4908         (let ((case-fold-search t)
4909               xref)
4910           (save-restriction
4911             (nnheader-narrow-to-headers)
4912             (goto-char (point-min))
4913             (when (or (and (not (eobp))
4914                            (eq (downcase (char-after)) ?x)
4915                            (looking-at "Xref:"))
4916                       (search-forward "\nXref:" nil t))
4917               (goto-char (1+ (match-end 0)))
4918               (setq xref (buffer-substring (point)
4919                                            (progn (end-of-line) (point))))
4920               (mail-header-set-xref headers xref)))))))
4921
4922 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4923   "Find article ID and insert the summary line for that article.
4924 OLD-HEADER can either be a header or a line number to insert
4925 the subject line on."
4926   (let* ((line (and (numberp old-header) old-header))
4927          (old-header (and (vectorp old-header) old-header))
4928          (header (cond ((and old-header use-old-header)
4929                         old-header)
4930                        ((and (numberp id)
4931                              (gnus-number-to-header id))
4932                         (gnus-number-to-header id))
4933                        (t
4934                         (gnus-read-header id))))
4935          (number (and (numberp id) id))
4936          d)
4937     (when header
4938       ;; Rebuild the thread that this article is part of and go to the
4939       ;; article we have fetched.
4940       (when (and (not gnus-show-threads)
4941                  old-header)
4942         (when (and number
4943                    (setq d (gnus-data-find (mail-header-number old-header))))
4944           (goto-char (gnus-data-pos d))
4945           (gnus-data-remove
4946            number
4947            (- (gnus-point-at-bol)
4948               (prog1
4949                   (1+ (gnus-point-at-eol))
4950                 (gnus-delete-line))))))
4951       (when old-header
4952         (mail-header-set-number header (mail-header-number old-header)))
4953       (setq gnus-newsgroup-sparse
4954             (delq (setq number (mail-header-number header))
4955                   gnus-newsgroup-sparse))
4956       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4957       (push number gnus-newsgroup-limit)
4958       (gnus-rebuild-thread (mail-header-id header) line)
4959       (gnus-summary-goto-subject number nil t))
4960     (when (and (numberp number)
4961                (> number 0))
4962       ;; We have to update the boundaries even if we can't fetch the
4963       ;; article if ID is a number -- so that the next `P' or `N'
4964       ;; command will fetch the previous (or next) article even
4965       ;; if the one we tried to fetch this time has been canceled.
4966       (when (> number gnus-newsgroup-end)
4967         (setq gnus-newsgroup-end number))
4968       (when (< number gnus-newsgroup-begin)
4969         (setq gnus-newsgroup-begin number))
4970       (setq gnus-newsgroup-unselected
4971             (delq number gnus-newsgroup-unselected)))
4972     ;; Report back a success?
4973     (and header (mail-header-number header))))
4974
4975 ;;; Process/prefix in the summary buffer
4976
4977 (defun gnus-summary-work-articles (n)
4978   "Return a list of articles to be worked upon.
4979 The prefix argument, the list of process marked articles, and the
4980 current article will be taken into consideration."
4981   (save-excursion
4982     (set-buffer gnus-summary-buffer)
4983     (cond
4984      (n
4985       ;; A numerical prefix has been given.
4986       (setq n (prefix-numeric-value n))
4987       (let ((backward (< n 0))
4988             (n (abs (prefix-numeric-value n)))
4989             articles article)
4990         (save-excursion
4991           (while
4992               (and (> n 0)
4993                    (push (setq article (gnus-summary-article-number))
4994                          articles)
4995                    (if backward
4996                        (gnus-summary-find-prev nil article)
4997                      (gnus-summary-find-next nil article)))
4998             (decf n)))
4999         (nreverse articles)))
5000      ((and (gnus-region-active-p) (mark))
5001       (message "region active")
5002       ;; Work on the region between point and mark.
5003       (let ((max (max (point) (mark)))
5004             articles article)
5005         (save-excursion
5006           (goto-char (min (point) (mark)))
5007           (while
5008               (and
5009                (push (setq article (gnus-summary-article-number)) articles)
5010                (gnus-summary-find-next nil article)
5011                (< (point) max)))
5012           (nreverse articles))))
5013      (gnus-newsgroup-processable
5014       ;; There are process-marked articles present.
5015       ;; Save current state.
5016       (gnus-summary-save-process-mark)
5017       ;; Return the list.
5018       (reverse gnus-newsgroup-processable))
5019      (t
5020       ;; Just return the current article.
5021       (list (gnus-summary-article-number))))))
5022
5023 (defmacro gnus-summary-iterate (arg &rest forms)
5024   "Iterate over the process/prefixed articles and do FORMS.
5025 ARG is the interactive prefix given to the command.  FORMS will be
5026 executed with point over the summary line of the articles."
5027   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5028     `(let ((,articles (gnus-summary-work-articles ,arg)))
5029        (while ,articles
5030          (gnus-summary-goto-subject (car ,articles))
5031          ,@forms
5032          (pop ,articles)))))
5033
5034 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5035 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5036
5037 (defun gnus-summary-save-process-mark ()
5038   "Push the current set of process marked articles on the stack."
5039   (interactive)
5040   (push (copy-sequence gnus-newsgroup-processable)
5041         gnus-newsgroup-process-stack))
5042
5043 (defun gnus-summary-kill-process-mark ()
5044   "Push the current set of process marked articles on the stack and unmark."
5045   (interactive)
5046   (gnus-summary-save-process-mark)
5047   (gnus-summary-unmark-all-processable))
5048
5049 (defun gnus-summary-yank-process-mark ()
5050   "Pop the last process mark state off the stack and restore it."
5051   (interactive)
5052   (unless gnus-newsgroup-process-stack
5053     (error "Empty mark stack"))
5054   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5055
5056 (defun gnus-summary-process-mark-set (set)
5057   "Make SET into the current process marked articles."
5058   (gnus-summary-unmark-all-processable)
5059   (while set
5060     (gnus-summary-set-process-mark (pop set))))
5061
5062 ;;; Searching and stuff
5063
5064 (defun gnus-summary-search-group (&optional backward use-level)
5065   "Search for next unread newsgroup.
5066 If optional argument BACKWARD is non-nil, search backward instead."
5067   (save-excursion
5068     (set-buffer gnus-group-buffer)
5069     (when (gnus-group-search-forward
5070            backward nil (if use-level (gnus-group-group-level) nil))
5071       (gnus-group-group-name))))
5072
5073 (defun gnus-summary-best-group (&optional exclude-group)
5074   "Find the name of the best unread group.
5075 If EXCLUDE-GROUP, do not go to this group."
5076   (save-excursion
5077     (set-buffer gnus-group-buffer)
5078     (save-excursion
5079       (gnus-group-best-unread-group exclude-group))))
5080
5081 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5082   (if backward (gnus-summary-find-prev)
5083     (let* ((dummy (gnus-summary-article-intangible-p))
5084            (article (or article (gnus-summary-article-number)))
5085            (arts (gnus-data-find-list article))
5086            result)
5087       (when (and (not dummy)
5088                  (or (not gnus-summary-check-current)
5089                      (not unread)
5090                      (not (gnus-data-unread-p (car arts)))))
5091         (setq arts (cdr arts)))
5092       (when (setq result
5093                   (if unread
5094                       (progn
5095                         (while arts
5096                           (when (or (and undownloaded
5097                                          (eq gnus-undownloaded-mark
5098                                              (gnus-data-mark (car arts))))
5099                                     (gnus-data-unread-p (car arts)))
5100                             (setq result (car arts)
5101                                   arts nil))
5102                           (setq arts (cdr arts)))
5103                         result)
5104                     (car arts)))
5105         (goto-char (gnus-data-pos result))
5106         (gnus-data-number result)))))
5107
5108 (defun gnus-summary-find-prev (&optional unread article)
5109   (let* ((eobp (eobp))
5110          (article (or article (gnus-summary-article-number)))
5111          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5112          result)
5113     (when (and (not eobp)
5114                (or (not gnus-summary-check-current)
5115                    (not unread)
5116                    (not (gnus-data-unread-p (car arts)))))
5117       (setq arts (cdr arts)))
5118     (when (setq result
5119                 (if unread
5120                     (progn
5121                       (while arts
5122                         (when (gnus-data-unread-p (car arts))
5123                           (setq result (car arts)
5124                                 arts nil))
5125                         (setq arts (cdr arts)))
5126                       result)
5127                   (car arts)))
5128       (goto-char (gnus-data-pos result))
5129       (gnus-data-number result))))
5130
5131 (defun gnus-summary-find-subject (subject &optional unread backward article)
5132   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5133          (article (or article (gnus-summary-article-number)))
5134          (articles (gnus-data-list backward))
5135          (arts (gnus-data-find-list article articles))
5136          result)
5137     (when (or (not gnus-summary-check-current)
5138               (not unread)
5139               (not (gnus-data-unread-p (car arts))))
5140       (setq arts (cdr arts)))
5141     (while arts
5142       (and (or (not unread)
5143                (gnus-data-unread-p (car arts)))
5144            (vectorp (gnus-data-header (car arts)))
5145            (gnus-subject-equal
5146             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5147            (setq result (car arts)
5148                  arts nil))
5149       (setq arts (cdr arts)))
5150     (and result
5151          (goto-char (gnus-data-pos result))
5152          (gnus-data-number result))))
5153
5154 (defun gnus-summary-search-forward (&optional unread subject backward)
5155   "Search forward for an article.
5156 If UNREAD, look for unread articles.  If SUBJECT, look for
5157 articles with that subject.  If BACKWARD, search backward instead."
5158   (cond (subject (gnus-summary-find-subject subject unread backward))
5159         (backward (gnus-summary-find-prev unread))
5160         (t (gnus-summary-find-next unread))))
5161
5162 (defun gnus-recenter (&optional n)
5163   "Center point in window and redisplay frame.
5164 Also do horizontal recentering."
5165   (interactive "P")
5166   (when (and nil
5167              gnus-auto-center-summary
5168              (not (eq gnus-auto-center-summary 'vertical)))
5169     (gnus-horizontal-recenter))
5170   (recenter n))
5171
5172 (defun gnus-summary-recenter ()
5173   "Center point in the summary window.
5174 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5175 displayed, no centering will be performed."
5176   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5177   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5178   (interactive)
5179   (let* ((top (cond ((< (window-height) 4) 0)
5180                     ((< (window-height) 7) 1)
5181                     (t (if (numberp gnus-auto-center-summary)
5182                            gnus-auto-center-summary
5183                          2))))
5184          (height (1- (window-height)))
5185          (bottom (save-excursion (goto-char (point-max))
5186                                  (forward-line (- height))
5187                                  (point)))
5188          (window (get-buffer-window (current-buffer))))
5189     ;; The user has to want it.
5190     (when gnus-auto-center-summary
5191       (when (get-buffer-window gnus-article-buffer)
5192         ;; Only do recentering when the article buffer is displayed,
5193         ;; Set the window start to either `bottom', which is the biggest
5194         ;; possible valid number, or the second line from the top,
5195         ;; whichever is the least.
5196         (set-window-start
5197          window (min bottom (save-excursion
5198                               (forward-line (- top)) (point)))
5199          t))
5200       ;; Do horizontal recentering while we're at it.
5201       (when (and (get-buffer-window (current-buffer) t)
5202                  (not (eq gnus-auto-center-summary 'vertical)))
5203         (let ((selected (selected-window)))
5204           (select-window (get-buffer-window (current-buffer) t))
5205           (gnus-summary-position-point)
5206           (gnus-horizontal-recenter)
5207           (select-window selected))))))
5208
5209 (defun gnus-summary-jump-to-group (newsgroup)
5210   "Move point to NEWSGROUP in group mode buffer."
5211   ;; Keep update point of group mode buffer if visible.
5212   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5213       (save-window-excursion
5214         ;; Take care of tree window mode.
5215         (when (get-buffer-window gnus-group-buffer)
5216           (pop-to-buffer gnus-group-buffer))
5217         (gnus-group-jump-to-group newsgroup))
5218     (save-excursion
5219       ;; Take care of tree window mode.
5220       (if (get-buffer-window gnus-group-buffer)
5221           (pop-to-buffer gnus-group-buffer)
5222         (set-buffer gnus-group-buffer))
5223       (gnus-group-jump-to-group newsgroup))))
5224
5225 ;; This function returns a list of article numbers based on the
5226 ;; difference between the ranges of read articles in this group and
5227 ;; the range of active articles.
5228 (defun gnus-list-of-unread-articles (group)
5229   (let* ((read (gnus-info-read (gnus-get-info group)))
5230          (active (or (gnus-active group) (gnus-activate-group group)))
5231          (last (cdr active))
5232          first nlast unread)
5233     ;; If none are read, then all are unread.
5234     (if (not read)
5235         (setq first (car active))
5236       ;; If the range of read articles is a single range, then the
5237       ;; first unread article is the article after the last read
5238       ;; article.  Sounds logical, doesn't it?
5239       (if (and (not (listp (cdr read)))
5240                (or (< (car read) (car active))
5241                    (progn (setq read (list read))
5242                           nil)))
5243           (setq first (max (car active) (1+ (cdr read))))
5244         ;; `read' is a list of ranges.
5245         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5246                                   (caar read)))
5247                   1)
5248           (setq first (car active)))
5249         (while read
5250           (when first
5251             (while (< first nlast)
5252               (push first unread)
5253               (setq first (1+ first))))
5254           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5255           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5256           (setq read (cdr read)))))
5257     ;; And add the last unread articles.
5258     (while (<= first last)
5259       (push first unread)
5260       (setq first (1+ first)))
5261     ;; Return the list of unread articles.
5262     (delq 0 (nreverse unread))))
5263
5264 (defun gnus-list-of-read-articles (group)
5265   "Return a list of unread, unticked and non-dormant articles."
5266   (let* ((info (gnus-get-info group))
5267          (marked (gnus-info-marks info))
5268          (active (gnus-active group)))
5269     (and info active
5270          (gnus-set-difference
5271           (gnus-sorted-complement
5272            (gnus-uncompress-range active)
5273            (gnus-list-of-unread-articles group))
5274           (append
5275            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5276            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5277
5278 ;; Various summary commands
5279
5280 (defun gnus-summary-select-article-buffer ()
5281   "Reconfigure windows to show article buffer."
5282   (interactive)
5283   (if (not (gnus-buffer-live-p gnus-article-buffer))
5284       (error "There is no article buffer for this summary buffer")
5285     (gnus-configure-windows 'article)
5286     (select-window (get-buffer-window gnus-article-buffer))))
5287
5288 (defun gnus-summary-universal-argument (arg)
5289   "Perform any operation on all articles that are process/prefixed."
5290   (interactive "P")
5291   (let ((articles (gnus-summary-work-articles arg))
5292         func article)
5293     (if (eq
5294          (setq
5295           func
5296           (key-binding
5297            (read-key-sequence
5298             (substitute-command-keys
5299              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5300          'undefined)
5301         (gnus-error 1 "Undefined key")
5302       (save-excursion
5303         (while articles
5304           (gnus-summary-goto-subject (setq article (pop articles)))
5305           (let (gnus-newsgroup-processable)
5306             (command-execute func))
5307           (gnus-summary-remove-process-mark article)))))
5308   (gnus-summary-position-point))
5309
5310 (defun gnus-summary-toggle-truncation (&optional arg)
5311   "Toggle truncation of summary lines.
5312 With arg, turn line truncation on iff arg is positive."
5313   (interactive "P")
5314   (setq truncate-lines
5315         (if (null arg) (not truncate-lines)
5316           (> (prefix-numeric-value arg) 0)))
5317   (redraw-display))
5318
5319 (defun gnus-summary-reselect-current-group (&optional all rescan)
5320   "Exit and then reselect the current newsgroup.
5321 The prefix argument ALL means to select all articles."
5322   (interactive "P")
5323   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5324     (error "Ephemeral groups can't be reselected"))
5325   (let ((current-subject (gnus-summary-article-number))
5326         (group gnus-newsgroup-name))
5327     (setq gnus-newsgroup-begin nil)
5328     (gnus-summary-exit)
5329     ;; We have to adjust the point of group mode buffer because
5330     ;; point was moved to the next unread newsgroup by exiting.
5331     (gnus-summary-jump-to-group group)
5332     (when rescan
5333       (save-excursion
5334         (save-window-excursion
5335           ;; Don't show group contents.
5336           (set-window-start (selected-window) (point-max))
5337           (gnus-group-get-new-news-this-group 1))))
5338     (gnus-group-read-group all t)
5339     (gnus-summary-goto-subject current-subject nil t)))
5340
5341 (defun gnus-summary-rescan-group (&optional all)
5342   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5343   (interactive "P")
5344   (gnus-summary-reselect-current-group all t))
5345
5346 (defun gnus-summary-update-info (&optional non-destructive)
5347   (save-excursion
5348     (let ((group gnus-newsgroup-name))
5349       (when group
5350         (when gnus-newsgroup-kill-headers
5351           (setq gnus-newsgroup-killed
5352                 (gnus-compress-sequence
5353                  (nconc
5354                   (gnus-set-sorted-intersection
5355                    (gnus-uncompress-range gnus-newsgroup-killed)
5356                    (setq gnus-newsgroup-unselected
5357                          (sort gnus-newsgroup-unselected '<)))
5358                   (setq gnus-newsgroup-unreads
5359                         (sort gnus-newsgroup-unreads '<)))
5360                  t)))
5361         (unless (listp (cdr gnus-newsgroup-killed))
5362           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5363         (let ((headers gnus-newsgroup-headers))
5364           ;; Set the new ranges of read articles.
5365           (save-excursion
5366             (set-buffer gnus-group-buffer)
5367             (gnus-undo-force-boundary))
5368           (gnus-update-read-articles
5369            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5370           ;; Set the current article marks.
5371           (let ((gnus-newsgroup-scored
5372                  (if (and (not gnus-save-score)
5373                           (not non-destructive))
5374                      nil
5375                    gnus-newsgroup-scored)))
5376             (save-excursion
5377               (gnus-update-marks)))
5378           ;; Do the cross-ref thing.
5379           (when gnus-use-cross-reference
5380             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5381           ;; Do not switch windows but change the buffer to work.
5382           (set-buffer gnus-group-buffer)
5383           (unless (gnus-ephemeral-group-p group)
5384             (gnus-group-update-group group)))))))
5385
5386 (defun gnus-summary-save-newsrc (&optional force)
5387   "Save the current number of read/marked articles in the dribble buffer.
5388 The dribble buffer will then be saved.
5389 If FORCE (the prefix), also save the .newsrc file(s)."
5390   (interactive "P")
5391   (gnus-summary-update-info t)
5392   (if force
5393       (gnus-save-newsrc-file)
5394     (gnus-dribble-save)))
5395
5396 (defun gnus-summary-exit (&optional temporary)
5397   "Exit reading current newsgroup, and then return to group selection mode.
5398 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5399   (interactive)
5400   (gnus-set-global-variables)
5401   (gnus-kill-save-kill-buffer)
5402   (gnus-async-halt-prefetch)
5403   (let* ((group gnus-newsgroup-name)
5404          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5405          (mode major-mode)
5406          (group-point nil)
5407          (buf (current-buffer)))
5408     (unless quit-config
5409       ;; Do adaptive scoring, and possibly save score files.
5410       (when gnus-newsgroup-adaptive
5411         (gnus-score-adaptive))
5412       (when gnus-use-scoring
5413         (gnus-score-save)))
5414     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5415     ;; If we have several article buffers, we kill them at exit.
5416     (unless gnus-single-article-buffer
5417       (gnus-kill-buffer gnus-original-article-buffer)
5418       (setq gnus-article-current nil))
5419     (when gnus-use-cache
5420       (gnus-cache-possibly-remove-articles)
5421       (gnus-cache-save-buffers))
5422     (gnus-async-prefetch-remove-group group)
5423     (when gnus-suppress-duplicates
5424       (gnus-dup-enter-articles))
5425     (when gnus-use-trees
5426       (gnus-tree-close group))
5427     (when gnus-use-cache
5428       (gnus-cache-write-active))
5429     ;; Remove entries for this group.
5430     (nnmail-purge-split-history (gnus-group-real-name group))
5431     ;; Make all changes in this group permanent.
5432     (unless quit-config
5433       (gnus-run-hooks 'gnus-exit-group-hook)
5434       (gnus-summary-update-info))
5435     (gnus-close-group group)
5436     ;; Make sure where we were, and go to next newsgroup.
5437     (set-buffer gnus-group-buffer)
5438     (unless quit-config
5439       (gnus-group-jump-to-group group))
5440     (gnus-run-hooks 'gnus-summary-exit-hook)
5441     (unless (or quit-config
5442                 ;; If this group has disappeared from the summary
5443                 ;; buffer, don't skip forwards.
5444                 (not (string= group (gnus-group-group-name))))
5445       (gnus-group-next-unread-group 1))
5446     (setq group-point (point))
5447     (if temporary
5448         nil                             ;Nothing to do.
5449       ;; If we have several article buffers, we kill them at exit.
5450       (unless gnus-single-article-buffer
5451         (gnus-kill-buffer gnus-article-buffer)
5452         (gnus-kill-buffer gnus-original-article-buffer)
5453         (setq gnus-article-current nil))
5454       (set-buffer buf)
5455       (if (not gnus-kill-summary-on-exit)
5456           (gnus-deaden-summary)
5457         ;; We set all buffer-local variables to nil.  It is unclear why
5458         ;; this is needed, but if we don't, buffer-local variables are
5459         ;; not garbage-collected, it seems.  This would the lead to en
5460         ;; ever-growing Emacs.
5461         (gnus-summary-clear-local-variables)
5462         (when (get-buffer gnus-article-buffer)
5463           (bury-buffer gnus-article-buffer))
5464         ;; We clear the global counterparts of the buffer-local
5465         ;; variables as well, just to be on the safe side.
5466         (set-buffer gnus-group-buffer)
5467         (gnus-summary-clear-local-variables)
5468         ;; Return to group mode buffer.
5469         (when (eq mode 'gnus-summary-mode)
5470           (gnus-kill-buffer buf)))
5471       (setq gnus-current-select-method gnus-select-method)
5472       (pop-to-buffer gnus-group-buffer)
5473       (if (not quit-config)
5474           (progn
5475             (goto-char group-point)
5476             (gnus-configure-windows 'group 'force)
5477             (unless (pos-visible-in-window-p)
5478               (forward-line (/ (static-if (featurep 'xemacs)
5479                                    (window-displayed-height)
5480                                  (1- (window-height)))
5481                                -2))
5482               (set-window-start (selected-window) (point))
5483               (goto-char group-point)))
5484         (gnus-handle-ephemeral-exit quit-config))
5485       ;; Clear the current group name.
5486       (unless quit-config
5487         (setq gnus-newsgroup-name nil)))))
5488
5489 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5490 (defun gnus-summary-exit-no-update (&optional no-questions)
5491   "Quit reading current newsgroup without updating read article info."
5492   (interactive)
5493   (let* ((group gnus-newsgroup-name)
5494          (quit-config (gnus-group-quit-config group)))
5495     (when (or no-questions
5496               gnus-expert-user
5497               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5498       (gnus-async-halt-prefetch)
5499       (mapcar 'funcall
5500               (delq 'gnus-summary-expire-articles
5501                     (copy-sequence gnus-summary-prepare-exit-hook)))
5502       ;; If we have several article buffers, we kill them at exit.
5503       (unless gnus-single-article-buffer
5504         (gnus-kill-buffer gnus-article-buffer)
5505         (gnus-kill-buffer gnus-original-article-buffer)
5506         (setq gnus-article-current nil))
5507       (if (not gnus-kill-summary-on-exit)
5508           (gnus-deaden-summary)
5509         (gnus-close-group group)
5510         (gnus-summary-clear-local-variables)
5511         (set-buffer gnus-group-buffer)
5512         (gnus-summary-clear-local-variables)
5513         (when (get-buffer gnus-summary-buffer)
5514           (kill-buffer gnus-summary-buffer)))
5515       (unless gnus-single-article-buffer
5516         (setq gnus-article-current nil))
5517       (when gnus-use-trees
5518         (gnus-tree-close group))
5519       (gnus-async-prefetch-remove-group group)
5520       (when (get-buffer gnus-article-buffer)
5521         (bury-buffer gnus-article-buffer))
5522       ;; Return to the group buffer.
5523       (gnus-configure-windows 'group 'force)
5524       ;; Clear the current group name.
5525       (setq gnus-newsgroup-name nil)
5526       (when (equal (gnus-group-group-name) group)
5527         (gnus-group-next-unread-group 1))
5528       (when quit-config
5529         (gnus-handle-ephemeral-exit quit-config)))))
5530
5531 (defun gnus-handle-ephemeral-exit (quit-config)
5532   "Handle movement when leaving an ephemeral group.
5533 The state which existed when entering the ephemeral is reset."
5534   (if (not (buffer-name (car quit-config)))
5535       (gnus-configure-windows 'group 'force)
5536     (set-buffer (car quit-config))
5537     (cond ((eq major-mode 'gnus-summary-mode)
5538            (gnus-set-global-variables))
5539           ((eq major-mode 'gnus-article-mode)
5540            (save-excursion
5541              ;; The `gnus-summary-buffer' variable may point
5542              ;; to the old summary buffer when using a single
5543              ;; article buffer.
5544              (unless (gnus-buffer-live-p gnus-summary-buffer)
5545                (set-buffer gnus-group-buffer))
5546              (set-buffer gnus-summary-buffer)
5547              (gnus-set-global-variables))))
5548     (if (or (eq (cdr quit-config) 'article)
5549             (eq (cdr quit-config) 'pick))
5550         (progn
5551           ;; The current article may be from the ephemeral group
5552           ;; thus it is best that we reload this article
5553           (gnus-summary-show-article)
5554           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5555               (gnus-configure-windows 'pick 'force)
5556             (gnus-configure-windows (cdr quit-config) 'force)))
5557       (gnus-configure-windows (cdr quit-config) 'force))
5558     (when (eq major-mode 'gnus-summary-mode)
5559       (gnus-summary-next-subject 1 nil t)
5560       (gnus-summary-recenter)
5561       (gnus-summary-position-point))))
5562
5563 (defun gnus-summary-preview-mime-message ()
5564   "MIME decode and play this message."
5565   (interactive)
5566   (let ((gnus-break-pages nil)
5567         (gnus-show-mime t))
5568     (gnus-summary-select-article gnus-show-all-headers t))
5569   (select-window (get-buffer-window gnus-article-buffer)))
5570
5571 ;;; Dead summaries.
5572
5573 (defvar gnus-dead-summary-mode-map nil)
5574
5575 (unless gnus-dead-summary-mode-map
5576   (setq gnus-dead-summary-mode-map (make-keymap))
5577   (suppress-keymap gnus-dead-summary-mode-map)
5578   (substitute-key-definition
5579    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5580   (let ((keys '("\C-d" "\r" "\177" [delete])))
5581     (while keys
5582       (define-key gnus-dead-summary-mode-map
5583         (pop keys) 'gnus-summary-wake-up-the-dead))))
5584
5585 (defvar gnus-dead-summary-mode nil
5586   "Minor mode for Gnus summary buffers.")
5587
5588 (defun gnus-dead-summary-mode (&optional arg)
5589   "Minor mode for Gnus summary buffers."
5590   (interactive "P")
5591   (when (eq major-mode 'gnus-summary-mode)
5592     (make-local-variable 'gnus-dead-summary-mode)
5593     (setq gnus-dead-summary-mode
5594           (if (null arg) (not gnus-dead-summary-mode)
5595             (> (prefix-numeric-value arg) 0)))
5596     (when gnus-dead-summary-mode
5597       (gnus-add-minor-mode
5598        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5599
5600 (defun gnus-deaden-summary ()
5601   "Make the current summary buffer into a dead summary buffer."
5602   ;; Kill any previous dead summary buffer.
5603   (when (and gnus-dead-summary
5604              (buffer-name gnus-dead-summary))
5605     (save-excursion
5606       (set-buffer gnus-dead-summary)
5607       (when gnus-dead-summary-mode
5608         (kill-buffer (current-buffer)))))
5609   ;; Make this the current dead summary.
5610   (setq gnus-dead-summary (current-buffer))
5611   (gnus-dead-summary-mode 1)
5612   (let ((name (buffer-name)))
5613     (when (string-match "Summary" name)
5614       (rename-buffer
5615        (concat (substring name 0 (match-beginning 0)) "Dead "
5616                (substring name (match-beginning 0)))
5617        t)
5618       (bury-buffer))))
5619
5620 (defun gnus-kill-or-deaden-summary (buffer)
5621   "Kill or deaden the summary BUFFER."
5622   (save-excursion
5623     (when (and (buffer-name buffer)
5624                (not gnus-single-article-buffer))
5625       (save-excursion
5626         (set-buffer buffer)
5627         (gnus-kill-buffer gnus-article-buffer)
5628         (gnus-kill-buffer gnus-original-article-buffer)))
5629     (cond (gnus-kill-summary-on-exit
5630            (when (and gnus-use-trees
5631                       (gnus-buffer-exists-p buffer))
5632              (save-excursion
5633                (set-buffer buffer)
5634                (gnus-tree-close gnus-newsgroup-name)))
5635            (gnus-kill-buffer buffer))
5636           ((gnus-buffer-exists-p buffer)
5637            (save-excursion
5638              (set-buffer buffer)
5639              (gnus-deaden-summary))))))
5640
5641 (defun gnus-summary-wake-up-the-dead (&rest args)
5642   "Wake up the dead summary buffer."
5643   (interactive)
5644   (gnus-dead-summary-mode -1)
5645   (let ((name (buffer-name)))
5646     (when (string-match "Dead " name)
5647       (rename-buffer
5648        (concat (substring name 0 (match-beginning 0))
5649                (substring name (match-end 0)))
5650        t)))
5651   (gnus-message 3 "This dead summary is now alive again"))
5652
5653 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5654 (defun gnus-summary-fetch-faq (&optional faq-dir)
5655   "Fetch the FAQ for the current group.
5656 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5657 in."
5658   (interactive
5659    (list
5660     (when current-prefix-arg
5661       (completing-read
5662        "Faq dir: " (and (listp gnus-group-faq-directory)
5663                         (mapcar (lambda (file) (list file))
5664                                 gnus-group-faq-directory))))))
5665   (let (gnus-faq-buffer)
5666     (when (setq gnus-faq-buffer
5667                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5668       (gnus-configure-windows 'summary-faq))))
5669
5670 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5671 (defun gnus-summary-describe-group (&optional force)
5672   "Describe the current newsgroup."
5673   (interactive "P")
5674   (gnus-group-describe-group force gnus-newsgroup-name))
5675
5676 (defun gnus-summary-describe-briefly ()
5677   "Describe summary mode commands briefly."
5678   (interactive)
5679   (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")))
5680
5681 ;; Walking around group mode buffer from summary mode.
5682
5683 (defun gnus-summary-next-group (&optional no-article target-group backward)
5684   "Exit current newsgroup and then select next unread newsgroup.
5685 If prefix argument NO-ARTICLE is non-nil, no article is selected
5686 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5687 previous group instead."
5688   (interactive "P")
5689   ;; Stop pre-fetching.
5690   (gnus-async-halt-prefetch)
5691   (let ((current-group gnus-newsgroup-name)
5692         (current-buffer (current-buffer))
5693         entered)
5694     ;; First we semi-exit this group to update Xrefs and all variables.
5695     ;; We can't do a real exit, because the window conf must remain
5696     ;; the same in case the user is prompted for info, and we don't
5697     ;; want the window conf to change before that...
5698     (gnus-summary-exit t)
5699     (while (not entered)
5700       ;; Then we find what group we are supposed to enter.
5701       (set-buffer gnus-group-buffer)
5702       (gnus-group-jump-to-group current-group)
5703       (setq target-group
5704             (or target-group
5705                 (if (eq gnus-keep-same-level 'best)
5706                     (gnus-summary-best-group gnus-newsgroup-name)
5707                   (gnus-summary-search-group backward gnus-keep-same-level))))
5708       (if (not target-group)
5709           ;; There are no further groups, so we return to the group
5710           ;; buffer.
5711           (progn
5712             (gnus-message 5 "Returning to the group buffer")
5713             (setq entered t)
5714             (when (gnus-buffer-live-p current-buffer)
5715               (set-buffer current-buffer)
5716               (gnus-summary-exit))
5717             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5718         ;; We try to enter the target group.
5719         (gnus-group-jump-to-group target-group)
5720         (let ((unreads (gnus-group-group-unread)))
5721           (if (and (or (eq t unreads)
5722                        (and unreads (not (zerop unreads))))
5723                    (gnus-summary-read-group
5724                     target-group nil no-article
5725                     (and (buffer-name current-buffer) current-buffer)
5726                     nil backward))
5727               (setq entered t)
5728             (setq current-group target-group
5729                   target-group nil)))))))
5730
5731 (defun gnus-summary-prev-group (&optional no-article)
5732   "Exit current newsgroup and then select previous unread newsgroup.
5733 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5734   (interactive "P")
5735   (gnus-summary-next-group no-article nil t))
5736
5737 ;; Walking around summary lines.
5738
5739 (defun gnus-summary-first-subject (&optional unread undownloaded)
5740   "Go to the first unread subject.
5741 If UNREAD is non-nil, go to the first unread article.
5742 Returns the article selected or nil if there are no unread articles."
5743   (interactive "P")
5744   (prog1
5745       (cond
5746        ;; Empty summary.
5747        ((null gnus-newsgroup-data)
5748         (gnus-message 3 "No articles in the group")
5749         nil)
5750        ;; Pick the first article.
5751        ((not unread)
5752         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5753         (gnus-data-number (car gnus-newsgroup-data)))
5754        ;; No unread articles.
5755        ((null gnus-newsgroup-unreads)
5756         (gnus-message 3 "No more unread articles")
5757         nil)
5758        ;; Find the first unread article.
5759        (t
5760         (let ((data gnus-newsgroup-data))
5761           (while (and data
5762                       (and (not (and undownloaded
5763                                      (eq gnus-undownloaded-mark
5764                                          (gnus-data-mark (car data)))))
5765                            (not (gnus-data-unread-p (car data)))))
5766             (setq data (cdr data)))
5767           (when data
5768             (goto-char (gnus-data-pos (car data)))
5769             (gnus-data-number (car data))))))
5770     (gnus-summary-position-point)))
5771
5772 (defun gnus-summary-next-subject (n &optional unread dont-display)
5773   "Go to next N'th summary line.
5774 If N is negative, go to the previous N'th subject line.
5775 If UNREAD is non-nil, only unread articles are selected.
5776 The difference between N and the actual number of steps taken is
5777 returned."
5778   (interactive "p")
5779   (let ((backward (< n 0))
5780         (n (abs n)))
5781     (while (and (> n 0)
5782                 (if backward
5783                     (gnus-summary-find-prev unread)
5784                   (gnus-summary-find-next unread)))
5785       (unless (zerop (setq n (1- n)))
5786         (gnus-summary-show-thread)))
5787     (when (/= 0 n)
5788       (gnus-message 7 "No more%s articles"
5789                     (if unread " unread" "")))
5790     (unless dont-display
5791       (gnus-summary-recenter)
5792       (gnus-summary-position-point))
5793     n))
5794
5795 (defun gnus-summary-next-unread-subject (n)
5796   "Go to next N'th unread summary line."
5797   (interactive "p")
5798   (gnus-summary-next-subject n t))
5799
5800 (defun gnus-summary-prev-subject (n &optional unread)
5801   "Go to previous N'th summary line.
5802 If optional argument UNREAD is non-nil, only unread article is selected."
5803   (interactive "p")
5804   (gnus-summary-next-subject (- n) unread))
5805
5806 (defun gnus-summary-prev-unread-subject (n)
5807   "Go to previous N'th unread summary line."
5808   (interactive "p")
5809   (gnus-summary-next-subject (- n) t))
5810
5811 (defun gnus-summary-goto-subject (article &optional force silent)
5812   "Go the subject line of ARTICLE.
5813 If FORCE, also allow jumping to articles not currently shown."
5814   (interactive "nArticle number: ")
5815   (let ((b (point))
5816         (data (gnus-data-find article)))
5817     ;; We read in the article if we have to.
5818     (and (not data)
5819          force
5820          (gnus-summary-insert-subject
5821           article
5822           (if (or (numberp force) (vectorp force)) force)
5823           t)
5824          (setq data (gnus-data-find article)))
5825     (goto-char b)
5826     (if (not data)
5827         (progn
5828           (unless silent
5829             (gnus-message 3 "Can't find article %d" article))
5830           nil)
5831       (goto-char (gnus-data-pos data))
5832       (gnus-summary-position-point)
5833       article)))
5834
5835 ;; Walking around summary lines with displaying articles.
5836
5837 (defun gnus-summary-expand-window (&optional arg)
5838   "Make the summary buffer take up the entire Emacs frame.
5839 Given a prefix, will force an `article' buffer configuration."
5840   (interactive "P")
5841   (if arg
5842       (gnus-configure-windows 'article 'force)
5843     (gnus-configure-windows 'summary 'force)))
5844
5845 (defun gnus-summary-display-article (article &optional all-header)
5846   "Display ARTICLE in article buffer."
5847   (gnus-set-global-variables)
5848   (if (null article)
5849       nil
5850     (prog1
5851         (if gnus-summary-display-article-function
5852             (funcall gnus-summary-display-article-function article all-header)
5853           (gnus-article-prepare article all-header))
5854       (with-current-buffer gnus-article-buffer
5855         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5856              nil))
5857       (gnus-run-hooks 'gnus-select-article-hook)
5858       (when (and gnus-current-article
5859                  (not (zerop gnus-current-article)))
5860         (gnus-summary-goto-subject gnus-current-article))
5861       (gnus-summary-recenter)
5862       (when (and gnus-use-trees gnus-show-threads)
5863         (gnus-possibly-generate-tree article)
5864         (gnus-highlight-selected-tree article))
5865       ;; Successfully display article.
5866       (gnus-article-set-window-start
5867        (cdr (assq article gnus-newsgroup-bookmarks))))))
5868
5869 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5870   "Select the current article.
5871 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5872 non-nil, the article will be re-fetched even if it already present in
5873 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5874 be displayed."
5875   ;; Make sure we are in the summary buffer to work around bbdb bug.
5876   (unless (eq major-mode 'gnus-summary-mode)
5877     (set-buffer gnus-summary-buffer))
5878   (let ((article (or article (gnus-summary-article-number)))
5879         (all-headers (not (not all-headers))) ;Must be T or NIL.
5880         gnus-summary-display-article-function)
5881     (and (not pseudo)
5882          (gnus-summary-article-pseudo-p article)
5883          (error "This is a pseudo-article"))
5884     (save-excursion
5885       (set-buffer gnus-summary-buffer)
5886       (if (or (and gnus-single-article-buffer
5887                    (or (null gnus-current-article)
5888                        (null gnus-article-current)
5889                        (null (get-buffer gnus-article-buffer))
5890                        (not (eq article (cdr gnus-article-current)))
5891                        (not (equal (car gnus-article-current)
5892                                    gnus-newsgroup-name))))
5893               (and (not gnus-single-article-buffer)
5894                    (or (null gnus-current-article)
5895                        (not (eq gnus-current-article article))))
5896               force)
5897           ;; The requested article is different from the current article.
5898           (progn
5899             (gnus-summary-display-article article all-headers)
5900             (when (or all-headers gnus-show-all-headers)
5901               (gnus-article-show-all-headers))
5902             (gnus-article-set-window-start
5903              (cdr (assq article gnus-newsgroup-bookmarks)))
5904             article)
5905         (when (or all-headers gnus-show-all-headers)
5906           (gnus-article-show-all-headers))
5907         'old))))
5908
5909 (defun gnus-summary-set-current-mark (&optional current-mark)
5910   "Obsolete function."
5911   nil)
5912
5913 (defun gnus-summary-next-article (&optional unread subject backward push)
5914   "Select the next article.
5915 If UNREAD, only unread articles are selected.
5916 If SUBJECT, only articles with SUBJECT are selected.
5917 If BACKWARD, the previous article is selected instead of the next."
5918   (interactive "P")
5919   (cond
5920    ;; Is there such an article?
5921    ((and (gnus-summary-search-forward unread subject backward)
5922          (or (gnus-summary-display-article (gnus-summary-article-number))
5923              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5924     (gnus-summary-position-point))
5925    ;; If not, we try the first unread, if that is wanted.
5926    ((and subject
5927          gnus-auto-select-same
5928          (gnus-summary-first-unread-article))
5929     (gnus-summary-position-point)
5930     (gnus-message 6 "Wrapped"))
5931    ;; Try to get next/previous article not displayed in this group.
5932    ((and gnus-auto-extend-newsgroup
5933          (not unread) (not subject))
5934     (gnus-summary-goto-article
5935      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5936      nil (count-lines (point-min) (point))))
5937    ;; Go to next/previous group.
5938    (t
5939     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5940       (gnus-summary-jump-to-group gnus-newsgroup-name))
5941     (let ((cmd last-command-char)
5942           (point
5943            (save-excursion
5944              (set-buffer gnus-group-buffer)
5945              (point)))
5946           (group
5947            (if (eq gnus-keep-same-level 'best)
5948                (gnus-summary-best-group gnus-newsgroup-name)
5949              (gnus-summary-search-group backward gnus-keep-same-level))))
5950       ;; For some reason, the group window gets selected.  We change
5951       ;; it back.
5952       (select-window (get-buffer-window (current-buffer)))
5953       ;; Select next unread newsgroup automagically.
5954       (cond
5955        ((or (not gnus-auto-select-next)
5956             (not cmd))
5957         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5958        ((or (eq gnus-auto-select-next 'quietly)
5959             (and (eq gnus-auto-select-next 'slightly-quietly)
5960                  push)
5961             (and (eq gnus-auto-select-next 'almost-quietly)
5962                  (gnus-summary-last-article-p)))
5963         ;; Select quietly.
5964         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5965             (gnus-summary-exit)
5966           (gnus-message 7 "No more%s articles (%s)..."
5967                         (if unread " unread" "")
5968                         (if group (concat "selecting " group)
5969                           "exiting"))
5970           (gnus-summary-next-group nil group backward)))
5971        (t
5972         (when (gnus-key-press-event-p last-input-event)
5973           (gnus-summary-walk-group-buffer
5974            gnus-newsgroup-name cmd unread backward point))))))))
5975
5976 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5977   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5978                       (?\C-p (gnus-group-prev-unread-group 1))))
5979         (cursor-in-echo-area t)
5980         keve key group ended)
5981     (save-excursion
5982       (set-buffer gnus-group-buffer)
5983       (goto-char start)
5984       (setq group
5985             (if (eq gnus-keep-same-level 'best)
5986                 (gnus-summary-best-group gnus-newsgroup-name)
5987               (gnus-summary-search-group backward gnus-keep-same-level))))
5988     (while (not ended)
5989       (gnus-message
5990        5 "No more%s articles%s" (if unread " unread" "")
5991        (if (and group
5992                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5993            (format " (Type %s for %s [%s])"
5994                    (single-key-description cmd) group
5995                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5996          (format " (Type %s to exit %s)"
5997                  (single-key-description cmd)
5998                  gnus-newsgroup-name)))
5999       ;; Confirm auto selection.
6000       (setq key (car (setq keve (gnus-read-event-char))))
6001       (setq ended t)
6002       (cond
6003        ((assq key keystrokes)
6004         (let ((obuf (current-buffer)))
6005           (switch-to-buffer gnus-group-buffer)
6006           (when group
6007             (gnus-group-jump-to-group group))
6008           (eval (cadr (assq key keystrokes)))
6009           (setq group (gnus-group-group-name))
6010           (switch-to-buffer obuf))
6011         (setq ended nil))
6012        ((equal key cmd)
6013         (if (or (not group)
6014                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6015             (gnus-summary-exit)
6016           (gnus-summary-next-group nil group backward)))
6017        (t
6018         (push (cdr keve) unread-command-events))))))
6019
6020 (defun gnus-summary-next-unread-article ()
6021   "Select unread article after current one."
6022   (interactive)
6023   (gnus-summary-next-article
6024    (or (not (eq gnus-summary-goto-unread 'never))
6025        (gnus-summary-last-article-p (gnus-summary-article-number)))
6026    (and gnus-auto-select-same
6027         (gnus-summary-article-subject))))
6028
6029 (defun gnus-summary-prev-article (&optional unread subject)
6030   "Select the article after the current one.
6031 If UNREAD is non-nil, only unread articles are selected."
6032   (interactive "P")
6033   (gnus-summary-next-article unread subject t))
6034
6035 (defun gnus-summary-prev-unread-article ()
6036   "Select unread article before current one."
6037   (interactive)
6038   (gnus-summary-prev-article
6039    (or (not (eq gnus-summary-goto-unread 'never))
6040        (gnus-summary-first-article-p (gnus-summary-article-number)))
6041    (and gnus-auto-select-same
6042         (gnus-summary-article-subject))))
6043
6044 (defun gnus-summary-next-page (&optional lines circular)
6045   "Show next page of the selected article.
6046 If at the end of the current article, select the next article.
6047 LINES says how many lines should be scrolled up.
6048
6049 If CIRCULAR is non-nil, go to the start of the article instead of
6050 selecting the next article when reaching the end of the current
6051 article."
6052   (interactive "P")
6053   (setq gnus-summary-buffer (current-buffer))
6054   (gnus-set-global-variables)
6055   (let ((article (gnus-summary-article-number))
6056         (article-window (get-buffer-window gnus-article-buffer t))
6057         endp)
6058     ;; If the buffer is empty, we have no article.
6059     (unless article
6060       (error "No article to select"))
6061     (gnus-configure-windows 'article)
6062     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6063         (if (and (eq gnus-summary-goto-unread 'never)
6064                  (not (gnus-summary-last-article-p article)))
6065             (gnus-summary-next-article)
6066           (gnus-summary-next-unread-article))
6067       (if (or (null gnus-current-article)
6068               (null gnus-article-current)
6069               (/= article (cdr gnus-article-current))
6070               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6071           ;; Selected subject is different from current article's.
6072           (gnus-summary-display-article article)
6073         (when article-window
6074           (gnus-eval-in-buffer-window gnus-article-buffer
6075             (setq endp (gnus-article-next-page lines)))
6076           (when endp
6077             (cond (circular
6078                    (gnus-summary-beginning-of-article))
6079                   (lines
6080                    (gnus-message 3 "End of message"))
6081                   ((null lines)
6082                    (if (and (eq gnus-summary-goto-unread 'never)
6083                             (not (gnus-summary-last-article-p article)))
6084                        (gnus-summary-next-article)
6085                      (gnus-summary-next-unread-article))))))))
6086     (gnus-summary-recenter)
6087     (gnus-summary-position-point)))
6088
6089 (defun gnus-summary-prev-page (&optional lines move)
6090   "Show previous page of selected article.
6091 Argument LINES specifies lines to be scrolled down.
6092 If MOVE, move to the previous unread article if point is at
6093 the beginning of the buffer."
6094   (interactive "P")
6095   (let ((article (gnus-summary-article-number))
6096         (article-window (get-buffer-window gnus-article-buffer t))
6097         endp)
6098     (gnus-configure-windows 'article)
6099     (if (or (null gnus-current-article)
6100             (null gnus-article-current)
6101             (/= article (cdr gnus-article-current))
6102             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6103         ;; Selected subject is different from current article's.
6104         (gnus-summary-display-article article)
6105       (gnus-summary-recenter)
6106       (when article-window
6107         (gnus-eval-in-buffer-window gnus-article-buffer
6108           (setq endp (gnus-article-prev-page lines)))
6109         (when (and move endp)
6110           (cond (lines
6111                  (gnus-message 3 "Beginning of message"))
6112                 ((null lines)
6113                  (if (and (eq gnus-summary-goto-unread 'never)
6114                           (not (gnus-summary-first-article-p article)))
6115                      (gnus-summary-prev-article)
6116                    (gnus-summary-prev-unread-article))))))))
6117   (gnus-summary-position-point))
6118
6119 (defun gnus-summary-prev-page-or-article (&optional lines)
6120   "Show previous page of selected article.
6121 Argument LINES specifies lines to be scrolled down.
6122 If at the beginning of the article, go to the next article."
6123   (interactive "P")
6124   (gnus-summary-prev-page lines t))
6125
6126 (defun gnus-summary-scroll-up (lines)
6127   "Scroll up (or down) one line current article.
6128 Argument LINES specifies lines to be scrolled up (or down if negative)."
6129   (interactive "p")
6130   (gnus-configure-windows 'article)
6131   (gnus-summary-show-thread)
6132   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6133     (gnus-eval-in-buffer-window gnus-article-buffer
6134       (cond ((> lines 0)
6135              (when (gnus-article-next-page lines)
6136                (gnus-message 3 "End of message")))
6137             ((< lines 0)
6138              (gnus-article-prev-page (- lines))))))
6139   (gnus-summary-recenter)
6140   (gnus-summary-position-point))
6141
6142 (defun gnus-summary-scroll-down (lines)
6143   "Scroll down (or up) one line current article.
6144 Argument LINES specifies lines to be scrolled down (or up if negative)."
6145   (interactive "p")
6146   (gnus-summary-scroll-up (- lines)))
6147
6148 (defun gnus-summary-next-same-subject ()
6149   "Select next article which has the same subject as current one."
6150   (interactive)
6151   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6152
6153 (defun gnus-summary-prev-same-subject ()
6154   "Select previous article which has the same subject as current one."
6155   (interactive)
6156   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6157
6158 (defun gnus-summary-next-unread-same-subject ()
6159   "Select next unread article which has the same subject as current one."
6160   (interactive)
6161   (gnus-summary-next-article t (gnus-summary-article-subject)))
6162
6163 (defun gnus-summary-prev-unread-same-subject ()
6164   "Select previous unread article which has the same subject as current one."
6165   (interactive)
6166   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6167
6168 (defun gnus-summary-first-unread-article ()
6169   "Select the first unread article.
6170 Return nil if there are no unread articles."
6171   (interactive)
6172   (prog1
6173       (when (gnus-summary-first-subject t)
6174         (gnus-summary-show-thread)
6175         (gnus-summary-first-subject t)
6176         (gnus-summary-display-article (gnus-summary-article-number)))
6177     (gnus-summary-position-point)))
6178
6179 (defun gnus-summary-first-unread-subject ()
6180   "Place the point on the subject line of the first unread article.
6181 Return nil if there are no unread articles."
6182   (interactive)
6183   (prog1
6184       (when (gnus-summary-first-subject t)
6185         (gnus-summary-show-thread)
6186         (gnus-summary-first-subject t))
6187     (gnus-summary-position-point)))
6188
6189 (defun gnus-summary-first-article ()
6190   "Select the first article.
6191 Return nil if there are no articles."
6192   (interactive)
6193   (prog1
6194       (when (gnus-summary-first-subject)
6195         (gnus-summary-show-thread)
6196         (gnus-summary-first-subject)
6197         (gnus-summary-display-article (gnus-summary-article-number)))
6198     (gnus-summary-position-point)))
6199
6200 (defun gnus-summary-best-unread-article ()
6201   "Select the unread article with the highest score."
6202   (interactive)
6203   (let ((best -1000000)
6204         (data gnus-newsgroup-data)
6205         article score)
6206     (while data
6207       (and (gnus-data-unread-p (car data))
6208            (> (setq score
6209                     (gnus-summary-article-score (gnus-data-number (car data))))
6210               best)
6211            (setq best score
6212                  article (gnus-data-number (car data))))
6213       (setq data (cdr data)))
6214     (prog1
6215         (if article
6216             (gnus-summary-goto-article article)
6217           (error "No unread articles"))
6218       (gnus-summary-position-point))))
6219
6220 (defun gnus-summary-last-subject ()
6221   "Go to the last displayed subject line in the group."
6222   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6223     (when article
6224       (gnus-summary-goto-subject article))))
6225
6226 (defun gnus-summary-goto-article (article &optional all-headers force)
6227   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6228 If ALL-HEADERS is non-nil, no header lines are hidden.
6229 If FORCE, go to the article even if it isn't displayed.  If FORCE
6230 is a number, it is the line the article is to be displayed on."
6231   (interactive
6232    (list
6233     (completing-read
6234      "Article number or Message-ID: "
6235      (mapcar (lambda (number) (list (int-to-string number)))
6236              gnus-newsgroup-limit))
6237     current-prefix-arg
6238     t))
6239   (prog1
6240       (if (and (stringp article)
6241                (string-match "@" article))
6242           (gnus-summary-refer-article article)
6243         (when (stringp article)
6244           (setq article (string-to-number article)))
6245         (if (gnus-summary-goto-subject article force)
6246             (gnus-summary-display-article article all-headers)
6247           (gnus-message 4 "Couldn't go to article %s" article) nil))
6248     (gnus-summary-position-point)))
6249
6250 (defun gnus-summary-goto-last-article ()
6251   "Go to the previously read article."
6252   (interactive)
6253   (prog1
6254       (when gnus-last-article
6255         (gnus-summary-goto-article gnus-last-article nil t))
6256     (gnus-summary-position-point)))
6257
6258 (defun gnus-summary-pop-article (number)
6259   "Pop one article off the history and go to the previous.
6260 NUMBER articles will be popped off."
6261   (interactive "p")
6262   (let (to)
6263     (setq gnus-newsgroup-history
6264           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6265     (if to
6266         (gnus-summary-goto-article (car to) nil t)
6267       (error "Article history empty")))
6268   (gnus-summary-position-point))
6269
6270 ;; Summary commands and functions for limiting the summary buffer.
6271
6272 (defun gnus-summary-limit-to-articles (n)
6273   "Limit the summary buffer to the next N articles.
6274 If not given a prefix, use the process marked articles instead."
6275   (interactive "P")
6276   (prog1
6277       (let ((articles (gnus-summary-work-articles n)))
6278         (setq gnus-newsgroup-processable nil)
6279         (gnus-summary-limit articles))
6280     (gnus-summary-position-point)))
6281
6282 (defun gnus-summary-pop-limit (&optional total)
6283   "Restore the previous limit.
6284 If given a prefix, remove all limits."
6285   (interactive "P")
6286   (when total
6287     (setq gnus-newsgroup-limits
6288           (list (mapcar (lambda (h) (mail-header-number h))
6289                         gnus-newsgroup-headers))))
6290   (unless gnus-newsgroup-limits
6291     (error "No limit to pop"))
6292   (prog1
6293       (gnus-summary-limit nil 'pop)
6294     (gnus-summary-position-point)))
6295
6296 (defun gnus-summary-limit-to-subject (subject &optional header)
6297   "Limit the summary buffer to articles that have subjects that match a regexp."
6298   (interactive "sLimit to subject (regexp): ")
6299   (unless header
6300     (setq header "subject"))
6301   (when (not (equal "" subject))
6302     (prog1
6303         (let ((articles (gnus-summary-find-matching
6304                          (or header "subject") subject 'all)))
6305           (unless articles
6306             (error "Found no matches for \"%s\"" subject))
6307           (gnus-summary-limit articles))
6308       (gnus-summary-position-point))))
6309
6310 (defun gnus-summary-limit-to-author (from)
6311   "Limit the summary buffer to articles that have authors that match a regexp."
6312   (interactive "sLimit to author (regexp): ")
6313   (gnus-summary-limit-to-subject from "from"))
6314
6315 (defun gnus-summary-limit-to-age (age &optional younger-p)
6316   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6317 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6318 articles that are younger than AGE days."
6319   (interactive
6320    (let ((younger current-prefix-arg)
6321          (days-got nil)
6322          days)
6323      (while (not days-got)
6324        (setq days (if younger
6325                       (read-string "Limit to articles within (in days): ")
6326                     (read-string "Limit to articles old than (in days): ")))
6327        (when (> (length days) 0)
6328          (setq days (read days)))
6329        (if (numberp days)
6330            (setq days-got t)
6331          (message "Please enter a number.")
6332          (sleep-for 1)))
6333      (list days younger)))
6334   (prog1
6335       (let ((data gnus-newsgroup-data)
6336             (cutoff (days-to-time age))
6337             articles d date is-younger)
6338         (while (setq d (pop data))
6339           (when (and (vectorp (gnus-data-header d))
6340                      (setq date (mail-header-date (gnus-data-header d))))
6341             (setq is-younger (time-less-p
6342                               (time-since (condition-case ()
6343                                               (date-to-time date)
6344                                             (error '(0 0))))
6345                               cutoff))
6346             (when (if younger-p
6347                       is-younger
6348                     (not is-younger))
6349               (push (gnus-data-number d) articles))))
6350         (gnus-summary-limit (nreverse articles)))
6351     (gnus-summary-position-point)))
6352
6353 (defun gnus-summary-limit-to-extra (header regexp)
6354   "Limit the summary buffer to articles that match an 'extra' header."
6355   (interactive
6356    (let ((header
6357           (intern
6358            (gnus-completing-read
6359             (symbol-name (car gnus-extra-headers))      
6360             "Limit extra header:"       
6361             (mapcar (lambda (x) 
6362                       (cons (symbol-name x) x))
6363                     gnus-extra-headers)
6364             nil                 
6365             t))))
6366      (list header
6367            (read-string (format "Limit to header %s (regexp): " header)))))
6368   (when (not (equal "" regexp))
6369     (prog1
6370         (let ((articles (gnus-summary-find-matching
6371                          (cons 'extra header) regexp 'all)))
6372           (unless articles
6373             (error "Found no matches for \"%s\"" regexp))
6374           (gnus-summary-limit articles))
6375       (gnus-summary-position-point))))
6376
6377 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6378 (make-obsolete
6379  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6380
6381 (defun gnus-summary-limit-to-unread (&optional all)
6382   "Limit the summary buffer to articles that are not marked as read.
6383 If ALL is non-nil, limit strictly to unread articles."
6384   (interactive "P")
6385   (if all
6386       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6387     (gnus-summary-limit-to-marks
6388      ;; Concat all the marks that say that an article is read and have
6389      ;; those removed.
6390      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6391            gnus-killed-mark gnus-kill-file-mark
6392            gnus-low-score-mark gnus-expirable-mark
6393            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6394            gnus-duplicate-mark gnus-souped-mark)
6395      'reverse)))
6396
6397 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6398 (make-obsolete 'gnus-summary-delete-marked-with
6399                'gnus-summary-limit-exlude-marks)
6400
6401 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6402   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6403 If REVERSE, limit the summary buffer to articles that are marked
6404 with MARKS.  MARKS can either be a string of marks or a list of marks.
6405 Returns how many articles were removed."
6406   (interactive "sMarks: ")
6407   (gnus-summary-limit-to-marks marks t))
6408
6409 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6410   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6411 If REVERSE (the prefix), limit the summary buffer to articles that are
6412 not marked with MARKS.  MARKS can either be a string of marks or a
6413 list of marks.
6414 Returns how many articles were removed."
6415   (interactive "sMarks: \nP")
6416   (prog1
6417       (let ((data gnus-newsgroup-data)
6418             (marks (if (listp marks) marks
6419                      (append marks nil))) ; Transform to list.
6420             articles)
6421         (while data
6422           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6423                   (memq (gnus-data-mark (car data)) marks))
6424             (push (gnus-data-number (car data)) articles))
6425           (setq data (cdr data)))
6426         (gnus-summary-limit articles))
6427     (gnus-summary-position-point)))
6428
6429 (defun gnus-summary-limit-to-score (&optional score)
6430   "Limit to articles with score at or above SCORE."
6431   (interactive "P")
6432   (setq score (if score
6433                   (prefix-numeric-value score)
6434                 (or gnus-summary-default-score 0)))
6435   (let ((data gnus-newsgroup-data)
6436         articles)
6437     (while data
6438       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6439                 score)
6440         (push (gnus-data-number (car data)) articles))
6441       (setq data (cdr data)))
6442     (prog1
6443         (gnus-summary-limit articles)
6444       (gnus-summary-position-point))))
6445
6446 (defun gnus-summary-limit-include-thread (id)
6447   "Display all the hidden articles that in the current thread."
6448   (interactive (list (mail-header-id (gnus-summary-article-header))))
6449   (let ((articles (gnus-articles-in-thread
6450                    (gnus-id-to-thread (gnus-root-id id)))))
6451     (prog1
6452         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6453       (gnus-summary-position-point))))
6454
6455 (defun gnus-summary-limit-include-dormant ()
6456   "Display all the hidden articles that are marked as dormant.
6457 Note that this command only works on a subset of the articles currently
6458 fetched for this group."
6459   (interactive)
6460   (unless gnus-newsgroup-dormant
6461     (error "There are no dormant articles in this group"))
6462   (prog1
6463       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6464     (gnus-summary-position-point)))
6465
6466 (defun gnus-summary-limit-exclude-dormant ()
6467   "Hide all dormant articles."
6468   (interactive)
6469   (prog1
6470       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6471     (gnus-summary-position-point)))
6472
6473 (defun gnus-summary-limit-exclude-childless-dormant ()
6474   "Hide all dormant articles that have no children."
6475   (interactive)
6476   (let ((data (gnus-data-list t))
6477         articles d children)
6478     ;; Find all articles that are either not dormant or have
6479     ;; children.
6480     (while (setq d (pop data))
6481       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6482                 (and (setq children
6483                            (gnus-article-children (gnus-data-number d)))
6484                      (let (found)
6485                        (while children
6486                          (when (memq (car children) articles)
6487                            (setq children nil
6488                                  found t))
6489                          (pop children))
6490                        found)))
6491         (push (gnus-data-number d) articles)))
6492     ;; Do the limiting.
6493     (prog1
6494         (gnus-summary-limit articles)
6495       (gnus-summary-position-point))))
6496
6497 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6498   "Mark all unread excluded articles as read.
6499 If ALL, mark even excluded ticked and dormants as read."
6500   (interactive "P")
6501   (let ((articles (gnus-sorted-complement
6502                    (sort
6503                     (mapcar (lambda (h) (mail-header-number h))
6504                             gnus-newsgroup-headers)
6505                     '<)
6506                    (sort gnus-newsgroup-limit '<)))
6507         article)
6508     (setq gnus-newsgroup-unreads
6509           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6510     (if all
6511         (setq gnus-newsgroup-dormant nil
6512               gnus-newsgroup-marked nil
6513               gnus-newsgroup-reads
6514               (nconc
6515                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6516                gnus-newsgroup-reads))
6517       (while (setq article (pop articles))
6518         (unless (or (memq article gnus-newsgroup-dormant)
6519                     (memq article gnus-newsgroup-marked))
6520           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6521
6522 (defun gnus-summary-limit (articles &optional pop)
6523   (if pop
6524       ;; We pop the previous limit off the stack and use that.
6525       (setq articles (car gnus-newsgroup-limits)
6526             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6527     ;; We use the new limit, so we push the old limit on the stack.
6528     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6529   ;; Set the limit.
6530   (setq gnus-newsgroup-limit articles)
6531   (let ((total (length gnus-newsgroup-data))
6532         (data (gnus-data-find-list (gnus-summary-article-number)))
6533         (gnus-summary-mark-below nil)   ; Inhibit this.
6534         found)
6535     ;; This will do all the work of generating the new summary buffer
6536     ;; according to the new limit.
6537     (gnus-summary-prepare)
6538     ;; Hide any threads, possibly.
6539     (and gnus-show-threads
6540          gnus-thread-hide-subtree
6541          (gnus-summary-hide-all-threads))
6542     ;; Try to return to the article you were at, or one in the
6543     ;; neighborhood.
6544     (when data
6545       ;; We try to find some article after the current one.
6546       (while data
6547         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6548           (setq data nil
6549                 found t))
6550         (setq data (cdr data))))
6551     (unless found
6552       ;; If there is no data, that means that we were after the last
6553       ;; article.  The same goes when we can't find any articles
6554       ;; after the current one.
6555       (goto-char (point-max))
6556       (gnus-summary-find-prev))
6557     (gnus-set-mode-line 'summary)
6558     ;; We return how many articles were removed from the summary
6559     ;; buffer as a result of the new limit.
6560     (- total (length gnus-newsgroup-data))))
6561
6562 (defsubst gnus-invisible-cut-children (threads)
6563   (let ((num 0))
6564     (while threads
6565       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6566         (incf num))
6567       (pop threads))
6568     (< num 2)))
6569
6570 (defsubst gnus-cut-thread (thread)
6571   "Go forwards in the thread until we find an article that we want to display."
6572   (when (or (eq gnus-fetch-old-headers 'some)
6573             (eq gnus-fetch-old-headers 'invisible)
6574             (numberp gnus-fetch-old-headers)
6575             (eq gnus-build-sparse-threads 'some)
6576             (eq gnus-build-sparse-threads 'more))
6577     ;; Deal with old-fetched headers and sparse threads.
6578     (while (and
6579             thread
6580             (or
6581              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6582              (gnus-summary-article-ancient-p
6583               (mail-header-number (car thread))))
6584             (if (or (<= (length (cdr thread)) 1)
6585                     (eq gnus-fetch-old-headers 'invisible))
6586                 (setq gnus-newsgroup-limit
6587                       (delq (mail-header-number (car thread))
6588                             gnus-newsgroup-limit)
6589                       thread (cadr thread))
6590               (when (gnus-invisible-cut-children (cdr thread))
6591                 (let ((th (cdr thread)))
6592                   (while th
6593                     (if (memq (mail-header-number (caar th))
6594                               gnus-newsgroup-limit)
6595                         (setq thread (car th)
6596                               th nil)
6597                       (setq th (cdr th))))))))))
6598   thread)
6599
6600 (defun gnus-cut-threads (threads)
6601   "Cut off all uninteresting articles from the beginning of threads."
6602   (when (or (eq gnus-fetch-old-headers 'some)
6603             (eq gnus-fetch-old-headers 'invisible)
6604             (numberp gnus-fetch-old-headers)
6605             (eq gnus-build-sparse-threads 'some)
6606             (eq gnus-build-sparse-threads 'more))
6607     (let ((th threads))
6608       (while th
6609         (setcar th (gnus-cut-thread (car th)))
6610         (setq th (cdr th)))))
6611   ;; Remove nixed out threads.
6612   (delq nil threads))
6613
6614 (defun gnus-summary-initial-limit (&optional show-if-empty)
6615   "Figure out what the initial limit is supposed to be on group entry.
6616 This entails weeding out unwanted dormants, low-scored articles,
6617 fetch-old-headers verbiage, and so on."
6618   ;; Most groups have nothing to remove.
6619   (if (or gnus-inhibit-limiting
6620           (and (null gnus-newsgroup-dormant)
6621                (not (eq gnus-fetch-old-headers 'some))
6622                (not (numberp gnus-fetch-old-headers))
6623                (not (eq gnus-fetch-old-headers 'invisible))
6624                (null gnus-summary-expunge-below)
6625                (not (eq gnus-build-sparse-threads 'some))
6626                (not (eq gnus-build-sparse-threads 'more))
6627                (null gnus-thread-expunge-below)
6628                (not gnus-use-nocem)))
6629       ()                                ; Do nothing.
6630     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6631     (setq gnus-newsgroup-limit nil)
6632     (mapatoms
6633      (lambda (node)
6634        (unless (car (symbol-value node))
6635          ;; These threads have no parents -- they are roots.
6636          (let ((nodes (cdr (symbol-value node)))
6637                thread)
6638            (while nodes
6639              (if (and gnus-thread-expunge-below
6640                       (< (gnus-thread-total-score (car nodes))
6641                          gnus-thread-expunge-below))
6642                  (gnus-expunge-thread (pop nodes))
6643                (setq thread (pop nodes))
6644                (gnus-summary-limit-children thread))))))
6645      gnus-newsgroup-dependencies)
6646     ;; If this limitation resulted in an empty group, we might
6647     ;; pop the previous limit and use it instead.
6648     (when (and (not gnus-newsgroup-limit)
6649                show-if-empty)
6650       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6651     gnus-newsgroup-limit))
6652
6653 (defun gnus-summary-limit-children (thread)
6654   "Return 1 if this subthread is visible and 0 if it is not."
6655   ;; First we get the number of visible children to this thread.  This
6656   ;; is done by recursing down the thread using this function, so this
6657   ;; will really go down to a leaf article first, before slowly
6658   ;; working its way up towards the root.
6659   (when thread
6660     (let ((children
6661            (if (cdr thread)
6662                (apply '+ (mapcar 'gnus-summary-limit-children
6663                                  (cdr thread)))
6664              0))
6665           (number (mail-header-number (car thread)))
6666           score)
6667       (if (and
6668            (not (memq number gnus-newsgroup-marked))
6669            (or
6670             ;; If this article is dormant and has absolutely no visible
6671             ;; children, then this article isn't visible.
6672             (and (memq number gnus-newsgroup-dormant)
6673                  (zerop children))
6674             ;; If this is "fetch-old-headered" and there is no
6675             ;; visible children, then we don't want this article.
6676             (and (or (eq gnus-fetch-old-headers 'some)
6677                      (numberp gnus-fetch-old-headers))
6678                  (gnus-summary-article-ancient-p number)
6679                  (zerop children))
6680             ;; If this is "fetch-old-headered" and `invisible', then
6681             ;; we don't want this article.
6682             (and (eq gnus-fetch-old-headers 'invisible)
6683                  (gnus-summary-article-ancient-p number))
6684             ;; If this is a sparsely inserted article with no children,
6685             ;; we don't want it.
6686             (and (eq gnus-build-sparse-threads 'some)
6687                  (gnus-summary-article-sparse-p number)
6688                  (zerop children))
6689             ;; If we use expunging, and this article is really
6690             ;; low-scored, then we don't want this article.
6691             (when (and gnus-summary-expunge-below
6692                        (< (setq score
6693                                 (or (cdr (assq number gnus-newsgroup-scored))
6694                                     gnus-summary-default-score))
6695                           gnus-summary-expunge-below))
6696               ;; We increase the expunge-tally here, but that has
6697               ;; nothing to do with the limits, really.
6698               (incf gnus-newsgroup-expunged-tally)
6699               ;; We also mark as read here, if that's wanted.
6700               (when (and gnus-summary-mark-below
6701                          (< score gnus-summary-mark-below))
6702                 (setq gnus-newsgroup-unreads
6703                       (delq number gnus-newsgroup-unreads))
6704                 (if gnus-newsgroup-auto-expire
6705                     (push number gnus-newsgroup-expirable)
6706                   (push (cons number gnus-low-score-mark)
6707                         gnus-newsgroup-reads)))
6708               t)
6709             ;; Check NoCeM things.
6710             (if (and gnus-use-nocem
6711                      (gnus-nocem-unwanted-article-p
6712                       (mail-header-id (car thread))))
6713                 (progn
6714                   (setq gnus-newsgroup-unreads
6715                         (delq number gnus-newsgroup-unreads))
6716                   t))))
6717           ;; Nope, invisible article.
6718           0
6719         ;; Ok, this article is to be visible, so we add it to the limit
6720         ;; and return 1.
6721         (push number gnus-newsgroup-limit)
6722         1))))
6723
6724 (defun gnus-expunge-thread (thread)
6725   "Mark all articles in THREAD as read."
6726   (let* ((number (mail-header-number (car thread))))
6727     (incf gnus-newsgroup-expunged-tally)
6728     ;; We also mark as read here, if that's wanted.
6729     (setq gnus-newsgroup-unreads
6730           (delq number gnus-newsgroup-unreads))
6731     (if gnus-newsgroup-auto-expire
6732         (push number gnus-newsgroup-expirable)
6733       (push (cons number gnus-low-score-mark)
6734             gnus-newsgroup-reads)))
6735   ;; Go recursively through all subthreads.
6736   (mapcar 'gnus-expunge-thread (cdr thread)))
6737
6738 ;; Summary article oriented commands
6739
6740 (defun gnus-summary-refer-parent-article (n)
6741   "Refer parent article N times.
6742 If N is negative, go to ancestor -N instead.
6743 The difference between N and the number of articles fetched is returned."
6744   (interactive "p")
6745   (let ((skip 1)
6746         error header ref)
6747     (when (not (natnump n))
6748       (setq skip (abs n)
6749             n 1))
6750     (while (and (> n 0)
6751                 (not error))
6752       (setq header (gnus-summary-article-header))
6753       (if (and (eq (mail-header-number header)
6754                    (cdr gnus-article-current))
6755                (equal gnus-newsgroup-name
6756                       (car gnus-article-current)))
6757           ;; If we try to find the parent of the currently
6758           ;; displayed article, then we take a look at the actual
6759           ;; References header, since this is slightly more
6760           ;; reliable than the References field we got from the
6761           ;; server.
6762           (save-excursion
6763             (set-buffer gnus-original-article-buffer)
6764             (nnheader-narrow-to-headers)
6765             (unless (setq ref (message-fetch-field "references"))
6766               (setq ref (message-fetch-field "in-reply-to")))
6767             (widen))
6768         (setq ref
6769               ;; It's not the current article, so we take a bet on
6770               ;; the value we got from the server.
6771               (mail-header-references header)))
6772       (if (and ref
6773                (not (equal ref "")))
6774           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6775             (gnus-message 1 "Couldn't find parent"))
6776         (gnus-message 1 "No references in article %d"
6777                       (gnus-summary-article-number))
6778         (setq error t))
6779       (decf n))
6780     (gnus-summary-position-point)
6781     n))
6782
6783 (defun gnus-summary-refer-references ()
6784   "Fetch all articles mentioned in the References header.
6785 Return the number of articles fetched."
6786   (interactive)
6787   (let ((ref (mail-header-references (gnus-summary-article-header)))
6788         (current (gnus-summary-article-number))
6789         (n 0))
6790     (if (or (not ref)
6791             (equal ref ""))
6792         (error "No References in the current article")
6793       ;; For each Message-ID in the References header...
6794       (while (string-match "<[^>]*>" ref)
6795         (incf n)
6796         ;; ... fetch that article.
6797         (gnus-summary-refer-article
6798          (prog1 (match-string 0 ref)
6799            (setq ref (substring ref (match-end 0))))))
6800       (gnus-summary-goto-subject current)
6801       (gnus-summary-position-point)
6802       n)))
6803
6804 (defun gnus-summary-refer-thread (&optional limit)
6805   "Fetch all articles in the current thread.
6806 If LIMIT (the numerical prefix), fetch that many old headers instead
6807 of what's specified by the `gnus-refer-thread-limit' variable."
6808   (interactive "P")
6809   (let ((id (mail-header-id (gnus-summary-article-header)))
6810         (limit (if limit (prefix-numeric-value limit)
6811                  gnus-refer-thread-limit)))
6812     ;; We want to fetch LIMIT *old* headers, but we also have to
6813     ;; re-fetch all the headers in the current buffer, because many of
6814     ;; them may be undisplayed.  So we adjust LIMIT.
6815     (when (numberp limit)
6816       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6817     (unless (eq gnus-fetch-old-headers 'invisible)
6818       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6819       ;; Retrieve the headers and read them in.
6820       (if (eq (gnus-retrieve-headers
6821                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6822               'nov)
6823           (gnus-build-all-threads)
6824         (error "Can't fetch thread from backends that don't support NOV"))
6825       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6826     (gnus-summary-limit-include-thread id)))
6827
6828 (defun gnus-summary-refer-article (message-id)
6829   "Fetch an article specified by MESSAGE-ID."
6830   (interactive "sMessage-ID: ")
6831   (when (and (stringp message-id)
6832              (not (zerop (length message-id))))
6833     ;; Construct the correct Message-ID if necessary.
6834     ;; Suggested by tale@pawl.rpi.edu.
6835     (unless (string-match "^<" message-id)
6836       (setq message-id (concat "<" message-id)))
6837     (unless (string-match ">$" message-id)
6838       (setq message-id (concat message-id ">")))
6839     (let* ((header (gnus-id-to-header message-id))
6840            (sparse (and header
6841                         (gnus-summary-article-sparse-p
6842                          (mail-header-number header))
6843                         (memq (mail-header-number header)
6844                               gnus-newsgroup-limit)))
6845            number)
6846       (cond
6847        ;; If the article is present in the buffer we just go to it.
6848        ((and header
6849              (or (not (gnus-summary-article-sparse-p
6850                        (mail-header-number header)))
6851                  sparse))
6852         (prog1
6853             (gnus-summary-goto-article
6854              (mail-header-number header) nil t)
6855           (when sparse
6856             (gnus-summary-update-article (mail-header-number header)))))
6857        (t
6858         ;; We fetch the article.
6859         (catch 'found
6860           (dolist (gnus-override-method (gnus-refer-article-methods))
6861             (gnus-check-server gnus-override-method)
6862             ;; Fetch the header, and display the article.
6863             (when (setq number (gnus-summary-insert-subject message-id))
6864               (gnus-summary-select-article nil nil nil number)
6865               (throw 'found t)))
6866           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6867
6868 (defun gnus-refer-article-methods ()
6869   "Return a list of referrable methods."
6870   (cond
6871    ;; No method, so we default to current and native.
6872    ((null gnus-refer-article-method)
6873     (list gnus-current-select-method gnus-select-method))
6874    ;; Current.
6875    ((eq 'current gnus-refer-article-method)
6876     (list gnus-current-select-method))
6877    ;; List of select methods.
6878    ((not (stringp (cadr gnus-refer-article-method)))
6879     (let (out)
6880       (dolist (method gnus-refer-article-method)
6881         (push (if (eq 'current method)
6882                   gnus-current-select-method
6883                 method)
6884               out))
6885       (nreverse out)))
6886    ;; One single select method.
6887    (t
6888     (list gnus-refer-article-method))))
6889
6890 (defun gnus-summary-edit-parameters ()
6891   "Edit the group parameters of the current group."
6892   (interactive)
6893   (gnus-group-edit-group gnus-newsgroup-name 'params))
6894
6895 (defun gnus-summary-customize-parameters ()
6896   "Customize the group parameters of the current group."
6897   (interactive)
6898   (gnus-group-customize gnus-newsgroup-name))
6899
6900 (defun gnus-summary-enter-digest-group (&optional force)
6901   "Enter an nndoc group based on the current article.
6902 If FORCE, force a digest interpretation.  If not, try
6903 to guess what the document format is."
6904   (interactive "P")
6905   (let ((conf gnus-current-window-configuration))
6906     (save-excursion
6907       (gnus-summary-select-article))
6908     (setq gnus-current-window-configuration conf)
6909     (let* ((name (format "%s-%d"
6910                          (gnus-group-prefixed-name
6911                           gnus-newsgroup-name (list 'nndoc ""))
6912                          (save-excursion
6913                            (set-buffer gnus-summary-buffer)
6914                            gnus-current-article)))
6915            (ogroup gnus-newsgroup-name)
6916            (params (append (gnus-info-params (gnus-get-info ogroup))
6917                            (list (cons 'to-group ogroup))
6918                            (list (cons 'save-article-group ogroup))))
6919            (case-fold-search t)
6920            (buf (current-buffer))
6921            dig to-address)
6922       (save-excursion
6923         (set-buffer gnus-original-article-buffer)
6924         ;; Have the digest group inherit the main mail address of
6925         ;; the parent article.
6926         (when (setq to-address (or (message-fetch-field "reply-to")
6927                                    (message-fetch-field "from")))
6928           (setq params (append (list (cons 'to-address to-address)))))
6929         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6930         (insert-buffer-substring gnus-original-article-buffer)
6931         ;; Remove lines that may lead nndoc to misinterpret the
6932         ;; document type.
6933         (narrow-to-region
6934          (goto-char (point-min))
6935          (or (search-forward "\n\n" nil t) (point)))
6936         (goto-char (point-min))
6937         (delete-matching-lines "^Path:\\|^From ")
6938         (widen))
6939       (unwind-protect
6940           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6941                     (gnus-newsgroup-ephemeral-ignored-charsets
6942                      gnus-newsgroup-ignored-charsets))
6943                 (gnus-group-read-ephemeral-group
6944                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6945                               (nndoc-article-type
6946                                ,(if force 'mbox 'guess))) t))
6947               ;; Make all postings to this group go to the parent group.
6948               (nconc (gnus-info-params (gnus-get-info name))
6949                      params)
6950             ;; Couldn't select this doc group.
6951             (switch-to-buffer buf)
6952             (gnus-set-global-variables)
6953             (gnus-configure-windows 'summary)
6954             (gnus-message 3 "Article couldn't be entered?"))
6955         (kill-buffer dig)))))
6956
6957 (defun gnus-summary-read-document (n)
6958   "Open a new group based on the current article(s).
6959 This will allow you to read digests and other similar
6960 documents as newsgroups.
6961 Obeys the standard process/prefix convention."
6962   (interactive "P")
6963   (let* ((articles (gnus-summary-work-articles n))
6964          (ogroup gnus-newsgroup-name)
6965          (params (append (gnus-info-params (gnus-get-info ogroup))
6966                          (list (cons 'to-group ogroup))))
6967          article group egroup groups vgroup)
6968     (while (setq article (pop articles))
6969       (setq group (format "%s-%d" gnus-newsgroup-name article))
6970       (gnus-summary-remove-process-mark article)
6971       (when (gnus-summary-display-article article)
6972         (save-excursion
6973           (with-temp-buffer
6974             (insert-buffer-substring gnus-original-article-buffer)
6975             ;; Remove some headers that may lead nndoc to make
6976             ;; the wrong guess.
6977             (message-narrow-to-head)
6978             (goto-char (point-min))
6979             (delete-matching-lines "^\\(Path\\):\\|^From ")
6980             (widen)
6981             (if (setq egroup
6982                       (gnus-group-read-ephemeral-group
6983                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6984                                      (nndoc-article-type guess))
6985                        t nil t))
6986                 (progn
6987                   ;; Make all postings to this group go to the parent group.
6988                   (nconc (gnus-info-params (gnus-get-info egroup))
6989                          params)
6990                   (push egroup groups))
6991               ;; Couldn't select this doc group.
6992               (gnus-error 3 "Article couldn't be entered"))))))
6993     ;; Now we have selected all the documents.
6994     (cond
6995      ((not groups)
6996       (error "None of the articles could be interpreted as documents"))
6997      ((gnus-group-read-ephemeral-group
6998        (setq vgroup (format
6999                      "nnvirtual:%s-%s" gnus-newsgroup-name
7000                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7001        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7002        t
7003        (cons (current-buffer) 'summary)))
7004      (t
7005       (error "Couldn't select virtual nndoc group")))))
7006
7007 (defun gnus-summary-isearch-article (&optional regexp-p)
7008   "Do incremental search forward on the current article.
7009 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7010   (interactive "P")
7011   (let* ((gnus-inhibit-treatment t)
7012          (old (gnus-summary-select-article)))
7013     (gnus-configure-windows 'article)
7014     (gnus-eval-in-buffer-window gnus-article-buffer
7015       (save-restriction
7016         (widen)
7017         (when (eq 'old old)
7018           (gnus-article-show-all-headers))
7019         (goto-char (point-min))
7020         (isearch-forward regexp-p)))))
7021
7022 (defun gnus-summary-search-article-forward (regexp &optional backward)
7023   "Search for an article containing REGEXP forward.
7024 If BACKWARD, search backward instead."
7025   (interactive
7026    (list (read-string
7027           (format "Search article %s (regexp%s): "
7028                   (if current-prefix-arg "backward" "forward")
7029                   (if gnus-last-search-regexp
7030                       (concat ", default " gnus-last-search-regexp)
7031                     "")))
7032          current-prefix-arg))
7033   (if (string-equal regexp "")
7034       (setq regexp (or gnus-last-search-regexp ""))
7035     (setq gnus-last-search-regexp regexp))
7036   (if (gnus-summary-search-article regexp backward)
7037       (gnus-summary-show-thread)
7038     (error "Search failed: \"%s\"" regexp)))
7039
7040 (defun gnus-summary-search-article-backward (regexp)
7041   "Search for an article containing REGEXP backward."
7042   (interactive
7043    (list (read-string
7044           (format "Search article backward (regexp%s): "
7045                   (if gnus-last-search-regexp
7046                       (concat ", default " gnus-last-search-regexp)
7047                     "")))))
7048   (gnus-summary-search-article-forward regexp 'backward))
7049
7050 (eval-when-compile
7051   (defmacro gnus-summary-search-article-position-point (regexp backward)
7052     "Dehighlight the last matched text and goto the beginning position."
7053     (` (if (and gnus-summary-search-article-matched-data
7054                 (let ((text (caddr gnus-summary-search-article-matched-data))
7055                       (inhibit-read-only t)
7056                       buffer-read-only)
7057                   (delete-region
7058                    (goto-char (car gnus-summary-search-article-matched-data))
7059                    (cadr gnus-summary-search-article-matched-data))
7060                   (insert text)
7061                   (string-match (, regexp) text)))
7062            (if (, backward) (beginning-of-line) (end-of-line))
7063          (goto-char (if (, backward) (point-max) (point-min))))))
7064
7065   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7066     "Place point where X-Face image is displayed."
7067     (if (featurep 'xemacs)
7068         (` (let ((end (if (search-forward "\n\n" nil t)
7069                           (goto-char (1- (point)))
7070                         (point-min)))
7071                  extent)
7072              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7073              (unless (and (re-search-forward "^From:" end t)
7074                           (setq extent (extent-at (point)))
7075                           (extent-begin-glyph extent))
7076                (goto-char (, opoint)))))
7077       (` (let ((end (if (search-forward "\n\n" nil t)
7078                         (goto-char (1- (point)))
7079                       (point-min))))
7080            (goto-char
7081             (or (text-property-any (or (search-backward "\n\n" nil t)
7082                                        (point-min))
7083                                    end 'x-face-mule-bitmap-image t)
7084                 (, opoint)))))))
7085
7086   (defmacro gnus-summary-search-article-highlight-matched-text
7087     (backward treated x-face)
7088     "Highlight matched text in the function `gnus-summary-search-article'."
7089     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7090              (end (set-marker (make-marker) (match-end 0)))
7091              (inhibit-read-only t)
7092              buffer-read-only)
7093          (unless treated
7094            (let ((,@
7095                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7096                     (mapcar
7097                      (lambda (item) (setq items (delq item items)))
7098                      '(gnus-treat-buttonize
7099                        gnus-treat-fill-article
7100                        gnus-treat-fill-long-lines
7101                        gnus-treat-emphasize
7102                        gnus-treat-highlight-headers
7103                        gnus-treat-highlight-citation
7104                        gnus-treat-highlight-signature
7105                        gnus-treat-overstrike
7106                        gnus-treat-display-xface
7107                        gnus-treat-buttonize-head
7108                        gnus-treat-decode-article-as-default-mime-charset))
7109                     (static-if (featurep 'xemacs)
7110                         items
7111                       (cons '(x-face-mule-delete-x-face-field
7112                               (quote never))
7113                             items))))
7114                  (gnus-treat-display-xface
7115                   (when (, x-face) gnus-treat-display-xface)))
7116              (gnus-article-prepare-mime-display)))
7117          (goto-char (if (, backward) start end))
7118          (when (, x-face)
7119            (gnus-summary-search-article-highlight-goto-x-face (point)))
7120          (setq gnus-summary-search-article-matched-data
7121                (list start end (buffer-substring start end)))
7122          (unless (eq start end);; matched text has been deleted. :-<
7123            (put-text-property start end 'face
7124                               (or (find-face 'isearch)
7125                                   'secondary-selection))))))
7126   )
7127
7128 (defun gnus-summary-search-article (regexp &optional backward)
7129   "Search for an article containing REGEXP.
7130 Optional argument BACKWARD means do search for backward.
7131 `gnus-select-article-hook' is not called during the search."
7132   ;; We have to require this here to make sure that the following
7133   ;; dynamic binding isn't shadowed by autoloading.
7134   (require 'gnus-async)
7135   (require 'gnus-art)
7136   (let ((gnus-select-article-hook nil)  ;Disable hook.
7137         (gnus-article-display-hook nil)
7138         (gnus-article-prepare-hook nil)
7139         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7140         (gnus-use-article-prefetch nil)
7141         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7142         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7143         (sum (current-buffer))
7144         (found nil)
7145         point treated)
7146     (gnus-save-hidden-threads
7147       (static-if (featurep 'xemacs)
7148           (let ((gnus-inhibit-treatment t))
7149             (setq treated (eq 'old (gnus-summary-select-article)))
7150             (when (and treated
7151                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7152                                  (window-live-p (get-buffer-window
7153                                                  gnus-article-buffer t)))))
7154               (gnus-summary-select-article nil t)
7155               (setq treated nil)))
7156         (let ((gnus-inhibit-treatment t)
7157               (x-face-mule-delete-x-face-field 'never))
7158           (setq treated (eq 'old (gnus-summary-select-article)))
7159           (when (and treated
7160                      (not
7161                       (and (gnus-buffer-live-p gnus-article-buffer)
7162                            (window-live-p (get-buffer-window
7163                                            gnus-article-buffer t))
7164                            (or (not (string-match "^\\^X-Face:" regexp))
7165                                (with-current-buffer gnus-article-buffer
7166                                  gnus-summary-search-article-matched-data)))))
7167             (gnus-summary-select-article nil t)
7168             (setq treated nil))))
7169       (set-buffer gnus-article-buffer)
7170       (widen)
7171       (if treated
7172           (progn
7173             (gnus-article-show-all-headers)
7174             (gnus-summary-search-article-position-point regexp backward))
7175         (goto-char (if backward (point-max) (point-min))))
7176       (while (not found)
7177         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7178         (if (if backward
7179                 (re-search-backward regexp nil t)
7180               (re-search-forward regexp nil t))
7181             ;; We found the regexp.
7182             (progn
7183               (gnus-summary-search-article-highlight-matched-text
7184                backward treated (string-match "^\\^X-Face:" regexp))
7185               (setq found 'found)
7186               (forward-line
7187                (/ (- 2 (window-height
7188                         (get-buffer-window gnus-article-buffer t)))
7189                   2))
7190               (set-window-start
7191                (get-buffer-window (current-buffer))
7192                (point))
7193               (set-buffer sum)
7194               (setq point (point)))
7195           ;; We didn't find it, so we go to the next article.
7196           (set-buffer sum)
7197           (setq found 'not)
7198           (while (eq found 'not)
7199             (if (not (if backward (gnus-summary-find-prev)
7200                        (gnus-summary-find-next)))
7201                 ;; No more articles.
7202                 (setq found t)
7203               ;; Select the next article and adjust point.
7204               (unless (gnus-summary-article-sparse-p
7205                        (gnus-summary-article-number))
7206                 (setq found nil)
7207                 (let ((gnus-inhibit-treatment t))
7208                   (gnus-summary-select-article))
7209                 (setq treated nil)
7210                 (set-buffer gnus-article-buffer)
7211                 (widen)
7212                 (goto-char (if backward (point-max) (point-min))))))))
7213       (gnus-message 7 ""))
7214     ;; Return whether we found the regexp.
7215     (when (eq found 'found)
7216       (goto-char point)
7217       (gnus-summary-show-thread)
7218       (gnus-summary-goto-subject gnus-current-article)
7219       (gnus-summary-position-point)
7220       t)))
7221
7222 (defun gnus-summary-find-matching (header regexp &optional backward unread
7223                                           not-case-fold)
7224   "Return a list of all articles that match REGEXP on HEADER.
7225 The search stars on the current article and goes forwards unless
7226 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7227 If UNREAD is non-nil, only unread articles will
7228 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7229 in the comparisons."
7230   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7231                 (gnus-data-find-list
7232                  (gnus-summary-article-number) (gnus-data-list backward))))
7233         (case-fold-search (not not-case-fold))
7234         articles d func)
7235     (if (consp header)
7236         (if (eq (car header) 'extra)
7237             (setq func
7238                   `(lambda (h)
7239                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7240                          "")))
7241           (error "%s is an invalid header" header))
7242       (unless (fboundp (intern (concat "mail-header-" header)))
7243         (error "%s is not a valid header" header))
7244       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7245     (while data
7246       (setq d (car data))
7247       (and (or (not unread)             ; We want all articles...
7248                (gnus-data-unread-p d))  ; Or just unreads.
7249            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7250            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7251            (push (gnus-data-number d) articles)) ; Success!
7252       (setq data (cdr data)))
7253     (nreverse articles)))
7254
7255 (defun gnus-summary-execute-command (header regexp command &optional backward)
7256   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7257 If HEADER is an empty string (or nil), the match is done on the entire
7258 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7259   (interactive
7260    (list (let ((completion-ignore-case t))
7261            (completing-read
7262             "Header name: "
7263             (mapcar (lambda (string) (list string))
7264                     '("Number" "Subject" "From" "Lines" "Date"
7265                       "Message-ID" "Xref" "References" "Body"))
7266             nil 'require-match))
7267          (read-string "Regexp: ")
7268          (read-key-sequence "Command: ")
7269          current-prefix-arg))
7270   (when (equal header "Body")
7271     (setq header ""))
7272   ;; Hidden thread subtrees must be searched as well.
7273   (gnus-summary-show-all-threads)
7274   ;; We don't want to change current point nor window configuration.
7275   (save-excursion
7276     (save-window-excursion
7277       (gnus-message 6 "Executing %s..." (key-description command))
7278       ;; We'd like to execute COMMAND interactively so as to give arguments.
7279       (gnus-execute header regexp
7280                     `(call-interactively ',(key-binding command))
7281                     backward)
7282       (gnus-message 6 "Executing %s...done" (key-description command)))))
7283
7284 (defun gnus-summary-beginning-of-article ()
7285   "Scroll the article back to the beginning."
7286   (interactive)
7287   (gnus-summary-select-article)
7288   (gnus-configure-windows 'article)
7289   (gnus-eval-in-buffer-window gnus-article-buffer
7290     (widen)
7291     (goto-char (point-min))
7292     (when gnus-page-broken
7293       (gnus-narrow-to-page))))
7294
7295 (defun gnus-summary-end-of-article ()
7296   "Scroll to the end of the article."
7297   (interactive)
7298   (gnus-summary-select-article)
7299   (gnus-configure-windows 'article)
7300   (gnus-eval-in-buffer-window gnus-article-buffer
7301     (widen)
7302     (goto-char (point-max))
7303     (recenter -3)
7304     (when gnus-page-broken
7305       (gnus-narrow-to-page))))
7306
7307 (defun gnus-summary-print-article (&optional filename n)
7308   "Generate and print a PostScript image of the N next (mail) articles.
7309
7310 If N is negative, print the N previous articles.  If N is nil and articles
7311 have been marked with the process mark, print these instead.
7312
7313 If the optional first argument FILENAME is nil, send the image to the
7314 printer.  If FILENAME is a string, save the PostScript image in a file with
7315 that name.  If FILENAME is a number, prompt the user for the name of the file
7316 to save in."
7317   (interactive (list (ps-print-preprint current-prefix-arg)
7318                      current-prefix-arg))
7319   (dolist (article (gnus-summary-work-articles n))
7320     (gnus-summary-select-article nil nil 'pseudo article)
7321     (gnus-eval-in-buffer-window gnus-article-buffer
7322       (let ((buffer (generate-new-buffer " *print*")))
7323         (unwind-protect
7324             (progn
7325               (copy-to-buffer buffer (point-min) (point-max))
7326               (set-buffer buffer)
7327               (gnus-article-delete-invisible-text)
7328               (let ((ps-left-header
7329                      (list
7330                       (concat "("
7331                               (mail-header-subject gnus-current-headers) ")")
7332                       (concat "("
7333                               (mail-header-from gnus-current-headers) ")")))
7334                     (ps-right-header
7335                      (list
7336                       "/pagenumberstring load"
7337                       (concat "("
7338                               (mail-header-date gnus-current-headers) ")"))))
7339                 (gnus-run-hooks 'gnus-ps-print-hook)
7340                 (save-excursion
7341                   (ps-print-buffer-with-faces filename))))
7342           (kill-buffer buffer))))))
7343
7344 (defun gnus-summary-show-article (&optional arg)
7345   "Force re-fetching of the current article.
7346 If ARG (the prefix) is a number, show the article with the charset 
7347 defined in `gnus-summary-show-article-charset-alist', or the charset
7348 inputed.
7349 If ARG (the prefix) is non-nil and not a number, show the raw article 
7350 without any article massaging functions being run."
7351   (interactive "P")
7352   (cond 
7353    ((numberp arg)
7354     (let ((gnus-newsgroup-charset 
7355            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7356                (read-coding-system "Charset: ")))
7357           (gnus-newsgroup-ignored-charsets 'gnus-all))
7358       (gnus-summary-select-article nil 'force)))
7359    ((not arg)
7360     ;; Select the article the normal way.
7361     (gnus-summary-select-article nil 'force))
7362    (t
7363     ;; We have to require this here to make sure that the following
7364     ;; dynamic binding isn't shadowed by autoloading.
7365     (require 'gnus-async)
7366     (require 'gnus-art)
7367     ;; Bind the article treatment functions to nil.
7368     (let ((gnus-have-all-headers t)
7369           gnus-article-display-hook
7370           gnus-article-prepare-hook
7371           gnus-article-decode-hook
7372           gnus-break-pages
7373           gnus-show-mime)
7374       (gnus-summary-select-article nil 'force))))
7375   (gnus-summary-goto-subject gnus-current-article)
7376   (gnus-summary-position-point))
7377
7378 (defun gnus-summary-verbose-headers (&optional arg)
7379   "Toggle permanent full header display.
7380 If ARG is a positive number, turn header display on.
7381 If ARG is a negative number, turn header display off."
7382   (interactive "P")
7383   (setq gnus-show-all-headers
7384         (cond ((or (not (numberp arg))
7385                    (zerop arg))
7386                (not gnus-show-all-headers))
7387               ((natnump arg)
7388                t)))
7389   (gnus-summary-show-article))
7390
7391 (defun gnus-summary-toggle-header (&optional arg)
7392   "Show the headers if they are hidden, or hide them if they are shown.
7393 If ARG is a positive number, show the entire header.
7394 If ARG is a negative number, hide the unwanted header lines."
7395   (interactive "P")
7396   (save-excursion
7397     (set-buffer gnus-article-buffer)
7398     (save-restriction
7399       (let* ((buffer-read-only nil)
7400              (inhibit-point-motion-hooks t)
7401              hidden e)
7402         (setq hidden
7403               (if (numberp arg)
7404                   (>= arg 0)
7405                 (save-restriction 
7406                   (article-narrow-to-head)
7407                   (gnus-article-hidden-text-p 'headers))))
7408         (goto-char (point-min))
7409         (when (search-forward "\n\n" nil t)
7410           (delete-region (point-min) (1- (point))))
7411         (goto-char (point-min))
7412         (save-excursion
7413           (set-buffer gnus-original-article-buffer)
7414           (goto-char (point-min))
7415           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7416         (insert-buffer-substring gnus-original-article-buffer 1 e)
7417         (save-restriction
7418           (narrow-to-region (point-min) (point))
7419           (article-decode-encoded-words)
7420           (if  hidden
7421               (let ((gnus-treat-hide-headers nil)
7422                     (gnus-treat-hide-boring-headers nil))
7423                 (setq gnus-article-wash-types
7424                       (delq 'headers gnus-article-wash-types))
7425                 (gnus-treat-article 'head))
7426             (gnus-treat-article 'head)))
7427         (gnus-set-mode-line 'article)))))
7428
7429 (defun gnus-summary-show-all-headers ()
7430   "Make all header lines visible."
7431   (interactive)
7432   (gnus-article-show-all-headers))
7433
7434 (defun gnus-summary-toggle-mime (&optional arg)
7435   "Toggle MIME processing.
7436 If ARG is a positive number, turn MIME processing on."
7437   (interactive "P")
7438   (setq gnus-show-mime
7439         (if (null arg)
7440             (not gnus-show-mime)
7441           (> (prefix-numeric-value arg) 0)))
7442   (gnus-summary-select-article t 'force))
7443
7444 (defun gnus-summary-caesar-message (&optional arg)
7445   "Caesar rotate the current article by 13.
7446 The numerical prefix specifies how many places to rotate each letter
7447 forward."
7448   (interactive "P")
7449   (gnus-summary-select-article)
7450   (let ((mail-header-separator ""))
7451     (gnus-eval-in-buffer-window gnus-article-buffer
7452       (save-restriction
7453         (widen)
7454         (let ((start (window-start))
7455               buffer-read-only)
7456           (message-caesar-buffer-body arg)
7457           (set-window-start (get-buffer-window (current-buffer)) start))))))
7458
7459 (defun gnus-summary-stop-page-breaking ()
7460   "Stop page breaking in the current article."
7461   (interactive)
7462   (gnus-summary-select-article)
7463   (gnus-eval-in-buffer-window gnus-article-buffer
7464     (widen)
7465     (when (gnus-visual-p 'page-marker)
7466       (let ((buffer-read-only nil))
7467         (gnus-remove-text-with-property 'gnus-prev)
7468         (gnus-remove-text-with-property 'gnus-next))
7469       (setq gnus-page-broken nil))))
7470
7471 (defun gnus-summary-move-article (&optional n to-newsgroup
7472                                             select-method action)
7473   "Move the current article to a different newsgroup.
7474 If N is a positive number, move the N next articles.
7475 If N is a negative number, move the N previous articles.
7476 If N is nil and any articles have been marked with the process mark,
7477 move those articles instead.
7478 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7479 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7480 re-spool using this method.
7481
7482 For this function to work, both the current newsgroup and the
7483 newsgroup that you want to move to have to support the `request-move'
7484 and `request-accept' functions.
7485
7486 ACTION can be either `move' (the default), `crosspost' or `copy'."
7487   (interactive "P")
7488   (unless action
7489     (setq action 'move))
7490   ;; Disable marking as read.
7491   (let (gnus-mark-article-hook)
7492     (save-window-excursion
7493       (gnus-summary-select-article)))
7494   ;; Check whether the source group supports the required functions.
7495   (cond ((and (eq action 'move)
7496               (not (gnus-check-backend-function
7497                     'request-move-article gnus-newsgroup-name)))
7498          (error "The current group does not support article moving"))
7499         ((and (eq action 'crosspost)
7500               (not (gnus-check-backend-function
7501                     'request-replace-article gnus-newsgroup-name)))
7502          (error "The current group does not support article editing")))
7503   (let ((articles (gnus-summary-work-articles n))
7504         (prefix (if (gnus-check-backend-function
7505                     'request-move-article gnus-newsgroup-name)
7506                     (gnus-group-real-prefix gnus-newsgroup-name)
7507                   ""))
7508         (names '((move "Move" "Moving")
7509                  (copy "Copy" "Copying")
7510                  (crosspost "Crosspost" "Crossposting")))
7511         (copy-buf (save-excursion
7512                     (nnheader-set-temp-buffer " *copy article*")))
7513         (default-marks gnus-article-mark-lists)
7514         (no-expire-marks (delete '(expirable . expire)
7515                                  (copy-sequence gnus-article-mark-lists)))
7516         art-group to-method new-xref article to-groups)
7517     (unless (assq action names)
7518       (error "Unknown action %s" action))
7519     ;; Read the newsgroup name.
7520     (when (and (not to-newsgroup)
7521                (not select-method))
7522       (setq to-newsgroup
7523             (gnus-read-move-group-name
7524              (cadr (assq action names))
7525              (symbol-value (intern (format "gnus-current-%s-group" action)))
7526              articles prefix))
7527       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7528     (setq to-method (or select-method
7529                         (gnus-server-to-method
7530                          (gnus-group-method to-newsgroup))))
7531     ;; Check the method we are to move this article to...
7532     (unless (gnus-check-backend-function
7533              'request-accept-article (car to-method))
7534       (error "%s does not support article copying" (car to-method)))
7535     (unless (gnus-check-server to-method)
7536       (error "Can't open server %s" (car to-method)))
7537     (gnus-message 6 "%s to %s: %s..."
7538                   (caddr (assq action names))
7539                   (or (car select-method) to-newsgroup) articles)
7540     (while articles
7541       (setq article (pop articles))
7542       (setq
7543        art-group
7544        (cond
7545         ;; Move the article.
7546         ((eq action 'move)
7547          ;; Remove this article from future suppression.
7548          (gnus-dup-unsuppress-article article)
7549          (gnus-request-move-article
7550           article                       ; Article to move
7551           gnus-newsgroup-name           ; From newsgroup
7552           (nth 1 (gnus-find-method-for-group
7553                   gnus-newsgroup-name)) ; Server
7554           (list 'gnus-request-accept-article
7555                 to-newsgroup (list 'quote select-method)
7556                 (not articles) t)       ; Accept form
7557           (not articles)))              ; Only save nov last time
7558         ;; Copy the article.
7559         ((eq action 'copy)
7560          (save-excursion
7561            (set-buffer copy-buf)
7562            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7563              (gnus-request-accept-article
7564               to-newsgroup select-method (not articles) t))))
7565         ;; Crosspost the article.
7566         ((eq action 'crosspost)
7567          (let ((xref (message-tokenize-header
7568                       (mail-header-xref (gnus-summary-article-header article))
7569                       " ")))
7570            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7571                                   ":" article))
7572            (unless xref
7573              (setq xref (list (system-name))))
7574            (setq new-xref
7575                  (concat
7576                   (mapconcat 'identity
7577                              (delete "Xref:" (delete new-xref xref))
7578                              " ")
7579                   " " new-xref))
7580            (save-excursion
7581              (set-buffer copy-buf)
7582              ;; First put the article in the destination group.
7583              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7584              (when (consp (setq art-group
7585                                 (gnus-request-accept-article
7586                                  to-newsgroup select-method (not articles))))
7587                (setq new-xref (concat new-xref " " (car art-group)
7588                                       ":" (cdr art-group)))
7589                ;; Now we have the new Xrefs header, so we insert
7590                ;; it and replace the new article.
7591                (nnheader-replace-header "Xref" new-xref)
7592                (gnus-request-replace-article
7593                 (cdr art-group) to-newsgroup (current-buffer))
7594                art-group))))))
7595       (cond
7596        ((not art-group)
7597         (gnus-message 1 "Couldn't %s article %s: %s"
7598                       (cadr (assq action names)) article
7599                       (nnheader-get-report (car to-method))))
7600        ((eq art-group 'junk)
7601         (when (eq action 'move)
7602           (gnus-summary-mark-article article gnus-canceled-mark)
7603           (gnus-message 4 "Deleted article %s" article)))
7604        (t
7605         (let* ((pto-group (gnus-group-prefixed-name
7606                            (car art-group) to-method))
7607                (entry
7608                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7609                (info (nth 2 entry))
7610                (to-group (gnus-info-group info))
7611                to-marks)
7612           ;; Update the group that has been moved to.
7613           (when (and info
7614                      (memq action '(move copy)))
7615             (unless (member to-group to-groups)
7616               (push to-group to-groups))
7617
7618             (unless (memq article gnus-newsgroup-unreads)
7619               (push 'read to-marks)
7620               (gnus-info-set-read
7621                info (gnus-add-to-range (gnus-info-read info)
7622                                        (list (cdr art-group)))))
7623
7624             ;; See whether the article is to be put in the cache.
7625             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7626                              default-marks
7627                            no-expire-marks))
7628                   (to-article (cdr art-group)))
7629
7630               ;; Enter the article into the cache in the new group,
7631               ;; if that is required.
7632               (when gnus-use-cache
7633                 (gnus-cache-possibly-enter-article
7634                  to-group to-article
7635                  (let ((header (copy-sequence
7636                                 (gnus-summary-article-header article))))
7637                    (mail-header-set-number header to-article)
7638                    header)
7639                  (memq article gnus-newsgroup-marked)
7640                  (memq article gnus-newsgroup-dormant)
7641                  (memq article gnus-newsgroup-unreads)))
7642
7643               (when gnus-preserve-marks 
7644                 ;; Copy any marks over to the new group.
7645                 (when (and (equal to-group gnus-newsgroup-name)
7646                            (not (memq article gnus-newsgroup-unreads)))
7647                   ;; Mark this article as read in this group.
7648                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7649                   (setcdr (gnus-active to-group) to-article)
7650                   (setcdr gnus-newsgroup-active to-article))
7651
7652                 (while marks
7653                   (when (memq article (symbol-value
7654                                        (intern (format "gnus-newsgroup-%s"
7655                                                        (caar marks)))))
7656                     (push (cdar marks) to-marks)
7657                     ;; If the other group is the same as this group,
7658                     ;; then we have to add the mark to the list.
7659                     (when (equal to-group gnus-newsgroup-name)
7660                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7661                            (cons to-article
7662                                  (symbol-value
7663                                   (intern (format "gnus-newsgroup-%s"
7664                                                   (caar marks)))))))
7665                     ;; Copy the marks to other group.
7666                     (gnus-add-marked-articles
7667                      to-group (cdar marks) (list to-article) info))
7668                   (setq marks (cdr marks)))
7669
7670                 (gnus-request-set-mark to-group (list (list (list to-article)
7671                                                             'set
7672                                                             to-marks))))
7673
7674               (gnus-dribble-enter
7675                (concat "(gnus-group-set-info '"
7676                        (gnus-prin1-to-string (gnus-get-info to-group))
7677                        ")"))))
7678
7679           ;; Update the Xref header in this article to point to
7680           ;; the new crossposted article we have just created.
7681           (when (eq action 'crosspost)
7682             (save-excursion
7683               (set-buffer copy-buf)
7684               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7685               (nnheader-replace-header "Xref" new-xref)
7686               (gnus-request-replace-article
7687                article gnus-newsgroup-name (current-buffer)))))
7688
7689         ;;;!!!Why is this necessary?
7690         (set-buffer gnus-summary-buffer)
7691
7692         (gnus-summary-goto-subject article)
7693         (when (eq action 'move)
7694           (gnus-summary-mark-article article gnus-canceled-mark))))
7695       (gnus-summary-remove-process-mark article))
7696     ;; Re-activate all groups that have been moved to.
7697     (while to-groups
7698       (save-excursion
7699         (set-buffer gnus-group-buffer)
7700         (when (gnus-group-goto-group (car to-groups) t)
7701           (gnus-group-get-new-news-this-group 1 t))
7702         (pop to-groups)))
7703
7704     (gnus-kill-buffer copy-buf)
7705     (gnus-summary-position-point)
7706     (gnus-set-mode-line 'summary)))
7707
7708 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7709   "Move the current article to a different newsgroup.
7710 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7711 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7712 re-spool using this method."
7713   (interactive "P")
7714   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7715
7716 (defun gnus-summary-crosspost-article (&optional n)
7717   "Crosspost the current article to some other group."
7718   (interactive "P")
7719   (gnus-summary-move-article n nil nil 'crosspost))
7720
7721 (defcustom gnus-summary-respool-default-method nil
7722   "Default method for respooling an article.
7723 If nil, use to the current newsgroup method."
7724   :type '(choice (gnus-select-method :value (nnml ""))
7725                  (const nil))
7726   :group 'gnus-summary-mail)
7727
7728 (defun gnus-summary-respool-article (&optional n method)
7729   "Respool the current article.
7730 The article will be squeezed through the mail spooling process again,
7731 which means that it will be put in some mail newsgroup or other
7732 depending on `nnmail-split-methods'.
7733 If N is a positive number, respool the N next articles.
7734 If N is a negative number, respool the N previous articles.
7735 If N is nil and any articles have been marked with the process mark,
7736 respool those articles instead.
7737
7738 Respooling can be done both from mail groups and \"real\" newsgroups.
7739 In the former case, the articles in question will be moved from the
7740 current group into whatever groups they are destined to.  In the
7741 latter case, they will be copied into the relevant groups."
7742   (interactive
7743    (list current-prefix-arg
7744          (let* ((methods (gnus-methods-using 'respool))
7745                 (methname
7746                  (symbol-name (or gnus-summary-respool-default-method
7747                                   (car (gnus-find-method-for-group
7748                                         gnus-newsgroup-name)))))
7749                 (method
7750                  (gnus-completing-read
7751                   methname "What backend do you want to use when respooling?"
7752                   methods nil t nil 'gnus-mail-method-history))
7753                 ms)
7754            (cond
7755             ((zerop (length (setq ms (gnus-servers-using-backend
7756                                       (intern method)))))
7757              (list (intern method) ""))
7758             ((= 1 (length ms))
7759              (car ms))
7760             (t
7761              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7762                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7763                            ms-alist))))))))
7764   (unless method
7765     (error "No method given for respooling"))
7766   (if (assoc (symbol-name
7767               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7768              (gnus-methods-using 'respool))
7769       (gnus-summary-move-article n nil method)
7770     (gnus-summary-copy-article n nil method)))
7771
7772 (defun gnus-summary-import-article (file)
7773   "Import an arbitrary file into a mail newsgroup."
7774   (interactive "fImport file: ")
7775   (let ((group gnus-newsgroup-name)
7776         (now (current-time))
7777         atts lines)
7778     (unless (gnus-check-backend-function 'request-accept-article group)
7779       (error "%s does not support article importing" group))
7780     (or (file-readable-p file)
7781         (not (file-regular-p file))
7782         (error "Can't read %s" file))
7783     (save-excursion
7784       (set-buffer (gnus-get-buffer-create " *import file*"))
7785       (erase-buffer)
7786       (nnheader-insert-file-contents file)
7787       (goto-char (point-min))
7788       (unless (nnheader-article-p)
7789         ;; This doesn't look like an article, so we fudge some headers.
7790         (setq atts (file-attributes file)
7791               lines (count-lines (point-min) (point-max)))
7792         (insert "From: " (read-string "From: ") "\n"
7793                 "Subject: " (read-string "Subject: ") "\n"
7794                 "Date: " (message-make-date (nth 5 atts))
7795                 "\n"
7796                 "Message-ID: " (message-make-message-id) "\n"
7797                 "Lines: " (int-to-string lines) "\n"
7798                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7799       (gnus-request-accept-article group nil t)
7800       (kill-buffer (current-buffer)))))
7801
7802 (defun gnus-summary-article-posted-p ()
7803   "Say whether the current (mail) article is available from news as well.
7804 This will be the case if the article has both been mailed and posted."
7805   (interactive)
7806   (let ((id (mail-header-references (gnus-summary-article-header)))
7807         (gnus-override-method (car (gnus-refer-article-methods))))
7808     (if (gnus-request-head id "")
7809         (gnus-message 2 "The current message was found on %s"
7810                       gnus-override-method)
7811       (gnus-message 2 "The current message couldn't be found on %s"
7812                     gnus-override-method)
7813       nil)))
7814
7815 (defun gnus-summary-expire-articles (&optional now)
7816   "Expire all articles that are marked as expirable in the current group."
7817   (interactive)
7818   (when (gnus-check-backend-function
7819          'request-expire-articles gnus-newsgroup-name)
7820     ;; This backend supports expiry.
7821     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7822            (expirable (if total
7823                           (progn
7824                             ;; We need to update the info for
7825                             ;; this group for `gnus-list-of-read-articles'
7826                             ;; to give us the right answer.
7827                             (gnus-run-hooks 'gnus-exit-group-hook)
7828                             (gnus-summary-update-info)
7829                             (gnus-list-of-read-articles gnus-newsgroup-name))
7830                         (setq gnus-newsgroup-expirable
7831                               (sort gnus-newsgroup-expirable '<))))
7832            (expiry-wait (if now 'immediate
7833                           (gnus-group-find-parameter
7834                            gnus-newsgroup-name 'expiry-wait)))
7835            es)
7836       (when expirable
7837         ;; There are expirable articles in this group, so we run them
7838         ;; through the expiry process.
7839         (gnus-message 6 "Expiring articles...")
7840         (unless (gnus-check-group gnus-newsgroup-name)
7841           (error "Can't open server for %s" gnus-newsgroup-name))
7842         ;; The list of articles that weren't expired is returned.
7843         (save-excursion
7844           (if expiry-wait
7845               (let ((nnmail-expiry-wait-function nil)
7846                     (nnmail-expiry-wait expiry-wait))
7847                 (setq es (gnus-request-expire-articles
7848                           expirable gnus-newsgroup-name)))
7849             (setq es (gnus-request-expire-articles
7850                       expirable gnus-newsgroup-name))))
7851         (unless total
7852           (setq gnus-newsgroup-expirable es))
7853         ;; We go through the old list of expirable, and mark all
7854         ;; really expired articles as nonexistent.
7855         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7856           (let ((gnus-use-cache nil))
7857             (while expirable
7858               (unless (memq (car expirable) es)
7859                 (when (gnus-data-find (car expirable))
7860                   (gnus-summary-mark-article
7861                    (car expirable) gnus-canceled-mark)))
7862               (setq expirable (cdr expirable)))))
7863         (gnus-message 6 "Expiring articles...done")))))
7864
7865 (defun gnus-summary-expire-articles-now ()
7866   "Expunge all expirable articles in the current group.
7867 This means that *all* articles that are marked as expirable will be
7868 deleted forever, right now."
7869   (interactive)
7870   (or gnus-expert-user
7871       (gnus-yes-or-no-p
7872        "Are you really, really, really sure you want to delete all these messages? ")
7873       (error "Phew!"))
7874   (gnus-summary-expire-articles t))
7875
7876 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7877 (defun gnus-summary-delete-article (&optional n)
7878   "Delete the N next (mail) articles.
7879 This command actually deletes articles.  This is not a marking
7880 command.  The article will disappear forever from your life, never to
7881 return.
7882 If N is negative, delete backwards.
7883 If N is nil and articles have been marked with the process mark,
7884 delete these instead."
7885   (interactive "P")
7886   (unless (gnus-check-backend-function 'request-expire-articles
7887                                        gnus-newsgroup-name)
7888     (error "The current newsgroup does not support article deletion"))
7889   ;; Compute the list of articles to delete.
7890   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7891         not-deleted)
7892     (if (and gnus-novice-user
7893              (not (gnus-yes-or-no-p
7894                    (format "Do you really want to delete %s forever? "
7895                            (if (> (length articles) 1)
7896                                (format "these %s articles" (length articles))
7897                              "this article")))))
7898         ()
7899       ;; Delete the articles.
7900       (setq not-deleted (gnus-request-expire-articles
7901                          articles gnus-newsgroup-name 'force))
7902       (while articles
7903         (gnus-summary-remove-process-mark (car articles))
7904         ;; The backend might not have been able to delete the article
7905         ;; after all.
7906         (unless (memq (car articles) not-deleted)
7907           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7908         (setq articles (cdr articles)))
7909       (when not-deleted
7910         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7911     (gnus-summary-position-point)
7912     (gnus-set-mode-line 'summary)
7913     not-deleted))
7914
7915 (defun gnus-summary-edit-article (&optional force)
7916   "Edit the current article.
7917 This will have permanent effect only in mail groups.
7918 If FORCE is non-nil, allow editing of articles even in read-only
7919 groups."
7920   (interactive "P")
7921   (save-excursion
7922     (set-buffer gnus-summary-buffer)
7923     (let ((mail-parse-charset gnus-newsgroup-charset)
7924           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7925       (gnus-set-global-variables)
7926       (when (and (not force)
7927                  (gnus-group-read-only-p))
7928         (error "The current newsgroup does not support article editing"))
7929       (gnus-summary-show-article t)
7930       (gnus-article-edit-article
7931        'ignore
7932        `(lambda (no-highlight)
7933           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7934                 (mail-parse-ignored-charsets
7935                  ',gnus-newsgroup-ignored-charsets))
7936             (gnus-summary-edit-article-done
7937              ,(or (mail-header-references gnus-current-headers) "")
7938              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7939
7940 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7941
7942 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7943                                                  no-highlight)
7944   "Make edits to the current article permanent."
7945   (interactive)
7946   ;; Replace the article.
7947   (let ((buf (current-buffer)))
7948     (with-temp-buffer
7949       (insert-buffer-substring buf)
7950       (if (and (not read-only)
7951                (not (gnus-request-replace-article
7952                      (cdr gnus-article-current) (car gnus-article-current)
7953                      (current-buffer) t)))
7954           (error "Couldn't replace article")
7955         ;; Update the summary buffer.
7956         (if (and references
7957                  (equal (message-tokenize-header references " ")
7958                         (message-tokenize-header
7959                          (or (message-fetch-field "references") "") " ")))
7960             ;; We only have to update this line.
7961             (save-excursion
7962               (save-restriction
7963                 (message-narrow-to-head)
7964                 (let ((head (buffer-string))
7965                       header)
7966                   (with-temp-buffer
7967                     (insert (format "211 %d Article retrieved.\n"
7968                                     (cdr gnus-article-current)))
7969                     (insert head)
7970                     (insert ".\n")
7971                     (let ((nntp-server-buffer (current-buffer)))
7972                       (setq header (car (gnus-get-newsgroup-headers
7973                                          (save-excursion
7974                                            (set-buffer gnus-summary-buffer)
7975                                            gnus-newsgroup-dependencies)
7976                                          t))))
7977                     (save-excursion
7978                       (set-buffer gnus-summary-buffer)
7979                       (gnus-data-set-header
7980                        (gnus-data-find (cdr gnus-article-current))
7981                        header)
7982                       (gnus-summary-update-article-line
7983                        (cdr gnus-article-current) header))))))
7984           ;; Update threads.
7985           (set-buffer (or buffer gnus-summary-buffer))
7986           (gnus-summary-update-article (cdr gnus-article-current)))
7987         ;; Prettify the article buffer again.
7988         (unless no-highlight
7989           (save-excursion
7990             (set-buffer gnus-article-buffer)
7991             ;;;!!! Fix this -- article should be rehighlighted.
7992             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7993             (set-buffer gnus-original-article-buffer)
7994             (gnus-request-article
7995              (cdr gnus-article-current)
7996              (car gnus-article-current) (current-buffer))))
7997         ;; Prettify the summary buffer line.
7998         (when (gnus-visual-p 'summary-highlight 'highlight)
7999           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8000
8001 (defun gnus-summary-edit-wash (key)
8002   "Perform editing command KEY in the article buffer."
8003   (interactive
8004    (list
8005     (progn
8006       (message "%s" (concat (this-command-keys) "- "))
8007       (read-char))))
8008   (message "")
8009   (gnus-summary-edit-article)
8010   (execute-kbd-macro (concat (this-command-keys) key))
8011   (gnus-article-edit-done))
8012
8013 ;;; Respooling
8014
8015 (defun gnus-summary-respool-query (&optional silent trace)
8016   "Query where the respool algorithm would put this article."
8017   (interactive)
8018   (let (gnus-mark-article-hook)
8019     (gnus-summary-select-article)
8020     (save-excursion
8021       (set-buffer gnus-original-article-buffer)
8022       (save-restriction
8023         (message-narrow-to-head)
8024         (let ((groups (nnmail-article-group 'identity trace)))
8025           (unless silent
8026             (if groups
8027                 (message "This message would go to %s"
8028                          (mapconcat 'car groups ", "))
8029               (message "This message would go to no groups"))
8030             groups))))))
8031
8032 (defun gnus-summary-respool-trace ()
8033   "Trace where the respool algorithm would put this article.
8034 Display a buffer showing all fancy splitting patterns which matched."
8035   (interactive)
8036   (gnus-summary-respool-query nil t))
8037
8038 ;; Summary marking commands.
8039
8040 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8041   "Mark articles which has the same subject as read, and then select the next.
8042 If UNMARK is positive, remove any kind of mark.
8043 If UNMARK is negative, tick articles."
8044   (interactive "P")
8045   (when unmark
8046     (setq unmark (prefix-numeric-value unmark)))
8047   (let ((count
8048          (gnus-summary-mark-same-subject
8049           (gnus-summary-article-subject) unmark)))
8050     ;; Select next unread article.  If auto-select-same mode, should
8051     ;; select the first unread article.
8052     (gnus-summary-next-article t (and gnus-auto-select-same
8053                                       (gnus-summary-article-subject)))
8054     (gnus-message 7 "%d article%s marked as %s"
8055                   count (if (= count 1) " is" "s are")
8056                   (if unmark "unread" "read"))))
8057
8058 (defun gnus-summary-kill-same-subject (&optional unmark)
8059   "Mark articles which has the same subject as read.
8060 If UNMARK is positive, remove any kind of mark.
8061 If UNMARK is negative, tick articles."
8062   (interactive "P")
8063   (when unmark
8064     (setq unmark (prefix-numeric-value unmark)))
8065   (let ((count
8066          (gnus-summary-mark-same-subject
8067           (gnus-summary-article-subject) unmark)))
8068     ;; If marked as read, go to next unread subject.
8069     (when (null unmark)
8070       ;; Go to next unread subject.
8071       (gnus-summary-next-subject 1 t))
8072     (gnus-message 7 "%d articles are marked as %s"
8073                   count (if unmark "unread" "read"))))
8074
8075 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8076   "Mark articles with same SUBJECT as read, and return marked number.
8077 If optional argument UNMARK is positive, remove any kinds of marks.
8078 If optional argument UNMARK is negative, mark articles as unread instead."
8079   (let ((count 1))
8080     (save-excursion
8081       (cond
8082        ((null unmark)                   ; Mark as read.
8083         (while (and
8084                 (progn
8085                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8086                   (gnus-summary-show-thread) t)
8087                 (gnus-summary-find-subject subject))
8088           (setq count (1+ count))))
8089        ((> unmark 0)                    ; Tick.
8090         (while (and
8091                 (progn
8092                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8093                   (gnus-summary-show-thread) t)
8094                 (gnus-summary-find-subject subject))
8095           (setq count (1+ count))))
8096        (t                               ; Mark as unread.
8097         (while (and
8098                 (progn
8099                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8100                   (gnus-summary-show-thread) t)
8101                 (gnus-summary-find-subject subject))
8102           (setq count (1+ count)))))
8103       (gnus-set-mode-line 'summary)
8104       ;; Return the number of marked articles.
8105       count)))
8106
8107 (defun gnus-summary-mark-as-processable (n &optional unmark)
8108   "Set the process mark on the next N articles.
8109 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8110 the process mark instead.  The difference between N and the actual
8111 number of articles marked is returned."
8112   (interactive "p")
8113   (let ((backward (< n 0))
8114         (n (abs n)))
8115     (while (and
8116             (> n 0)
8117             (if unmark
8118                 (gnus-summary-remove-process-mark
8119                  (gnus-summary-article-number))
8120               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8121             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8122       (setq n (1- n)))
8123     (when (/= 0 n)
8124       (gnus-message 7 "No more articles"))
8125     (gnus-summary-recenter)
8126     (gnus-summary-position-point)
8127     n))
8128
8129 (defun gnus-summary-unmark-as-processable (n)
8130   "Remove the process mark from the next N articles.
8131 If N is negative, unmark backward instead.  The difference between N and
8132 the actual number of articles unmarked is returned."
8133   (interactive "p")
8134   (gnus-summary-mark-as-processable n t))
8135
8136 (defun gnus-summary-unmark-all-processable ()
8137   "Remove the process mark from all articles."
8138   (interactive)
8139   (save-excursion
8140     (while gnus-newsgroup-processable
8141       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8142   (gnus-summary-position-point))
8143
8144 (defun gnus-summary-mark-as-expirable (n)
8145   "Mark N articles forward as expirable.
8146 If N is negative, mark backward instead.  The difference between N and
8147 the actual number of articles marked is returned."
8148   (interactive "p")
8149   (gnus-summary-mark-forward n gnus-expirable-mark))
8150
8151 (defun gnus-summary-mark-article-as-replied (article)
8152   "Mark ARTICLE replied and update the summary line."
8153   (push article gnus-newsgroup-replied)
8154   (let ((buffer-read-only nil))
8155     (when (gnus-summary-goto-subject article nil t)
8156       (gnus-summary-update-secondary-mark article))))
8157
8158 (defun gnus-summary-set-bookmark (article)
8159   "Set a bookmark in current article."
8160   (interactive (list (gnus-summary-article-number)))
8161   (when (or (not (get-buffer gnus-article-buffer))
8162             (not gnus-current-article)
8163             (not gnus-article-current)
8164             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8165     (error "No current article selected"))
8166   ;; Remove old bookmark, if one exists.
8167   (let ((old (assq article gnus-newsgroup-bookmarks)))
8168     (when old
8169       (setq gnus-newsgroup-bookmarks
8170             (delq old gnus-newsgroup-bookmarks))))
8171   ;; Set the new bookmark, which is on the form
8172   ;; (article-number . line-number-in-body).
8173   (push
8174    (cons article
8175          (save-excursion
8176            (set-buffer gnus-article-buffer)
8177            (count-lines
8178             (min (point)
8179                  (save-excursion
8180                    (goto-char (point-min))
8181                    (search-forward "\n\n" nil t)
8182                    (point)))
8183             (point))))
8184    gnus-newsgroup-bookmarks)
8185   (gnus-message 6 "A bookmark has been added to the current article."))
8186
8187 (defun gnus-summary-remove-bookmark (article)
8188   "Remove the bookmark from the current article."
8189   (interactive (list (gnus-summary-article-number)))
8190   ;; Remove old bookmark, if one exists.
8191   (let ((old (assq article gnus-newsgroup-bookmarks)))
8192     (if old
8193         (progn
8194           (setq gnus-newsgroup-bookmarks
8195                 (delq old gnus-newsgroup-bookmarks))
8196           (gnus-message 6 "Removed bookmark."))
8197       (gnus-message 6 "No bookmark in current article."))))
8198
8199 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8200 (defun gnus-summary-mark-as-dormant (n)
8201   "Mark N articles forward as dormant.
8202 If N is negative, mark backward instead.  The difference between N and
8203 the actual number of articles marked is returned."
8204   (interactive "p")
8205   (gnus-summary-mark-forward n gnus-dormant-mark))
8206
8207 (defun gnus-summary-set-process-mark (article)
8208   "Set the process mark on ARTICLE and update the summary line."
8209   (setq gnus-newsgroup-processable
8210         (cons article
8211               (delq article gnus-newsgroup-processable)))
8212   (when (gnus-summary-goto-subject article)
8213     (gnus-summary-show-thread)
8214     (gnus-summary-goto-subject article)
8215     (gnus-summary-update-secondary-mark article)))
8216
8217 (defun gnus-summary-remove-process-mark (article)
8218   "Remove the process mark from ARTICLE and update the summary line."
8219   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8220   (when (gnus-summary-goto-subject article)
8221     (gnus-summary-show-thread)
8222     (gnus-summary-goto-subject article)
8223     (gnus-summary-update-secondary-mark article)))
8224
8225 (defun gnus-summary-set-saved-mark (article)
8226   "Set the process mark on ARTICLE and update the summary line."
8227   (push article gnus-newsgroup-saved)
8228   (when (gnus-summary-goto-subject article)
8229     (gnus-summary-update-secondary-mark article)))
8230
8231 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8232   "Mark N articles as read forwards.
8233 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8234 The difference between N and the actual number of articles marked is
8235 returned.
8236 Iff NO-EXPIRE, auto-expiry will be inhibited."
8237   (interactive "p")
8238   (gnus-summary-show-thread)
8239   (let ((backward (< n 0))
8240         (gnus-summary-goto-unread
8241          (and gnus-summary-goto-unread
8242               (not (eq gnus-summary-goto-unread 'never))
8243               (not (memq mark (list gnus-unread-mark
8244                                     gnus-ticked-mark gnus-dormant-mark)))))
8245         (n (abs n))
8246         (mark (or mark gnus-del-mark)))
8247     (while (and (> n 0)
8248                 (gnus-summary-mark-article nil mark no-expire)
8249                 (zerop (gnus-summary-next-subject
8250                         (if backward -1 1)
8251                         (and gnus-summary-goto-unread
8252                              (not (eq gnus-summary-goto-unread 'never)))
8253                         t)))
8254       (setq n (1- n)))
8255     (when (/= 0 n)
8256       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8257     (gnus-summary-recenter)
8258     (gnus-summary-position-point)
8259     (gnus-set-mode-line 'summary)
8260     n))
8261
8262 (defun gnus-summary-mark-article-as-read (mark)
8263   "Mark the current article quickly as read with MARK."
8264   (let ((article (gnus-summary-article-number)))
8265     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8266     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8267     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8268     (push (cons article mark) gnus-newsgroup-reads)
8269     ;; Possibly remove from cache, if that is used.
8270     (when gnus-use-cache
8271       (gnus-cache-enter-remove-article article))
8272     ;; Allow the backend to change the mark.
8273     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8274     ;; Check for auto-expiry.
8275     (when (and gnus-newsgroup-auto-expire
8276                (memq mark gnus-auto-expirable-marks))
8277       (setq mark gnus-expirable-mark)
8278       ;; Let the backend know about the mark change.
8279       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8280       (push article gnus-newsgroup-expirable))
8281     ;; Set the mark in the buffer.
8282     (gnus-summary-update-mark mark 'unread)
8283     t))
8284
8285 (defun gnus-summary-mark-article-as-unread (mark)
8286   "Mark the current article quickly as unread with MARK."
8287   (let* ((article (gnus-summary-article-number))
8288          (old-mark (gnus-summary-article-mark article)))
8289     ;; Allow the backend to change the mark.
8290     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8291     (if (eq mark old-mark)
8292         t
8293       (if (<= article 0)
8294           (progn
8295             (gnus-error 1 "Can't mark negative article numbers")
8296             nil)
8297         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8298         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8299         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8300         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8301         (cond ((= mark gnus-ticked-mark)
8302                (push article gnus-newsgroup-marked))
8303               ((= mark gnus-dormant-mark)
8304                (push article gnus-newsgroup-dormant))
8305               (t
8306                (push article gnus-newsgroup-unreads)))
8307         (gnus-pull article gnus-newsgroup-reads)
8308
8309         ;; See whether the article is to be put in the cache.
8310         (and gnus-use-cache
8311              (vectorp (gnus-summary-article-header article))
8312              (save-excursion
8313                (gnus-cache-possibly-enter-article
8314                 gnus-newsgroup-name article
8315                 (gnus-summary-article-header article)
8316                 (= mark gnus-ticked-mark)
8317                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8318
8319         ;; Fix the mark.
8320         (gnus-summary-update-mark mark 'unread)
8321         t))))
8322
8323 (defun gnus-summary-mark-article (&optional article mark no-expire)
8324   "Mark ARTICLE with MARK.  MARK can be any character.
8325 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8326 `??' (dormant) and `?E' (expirable).
8327 If MARK is nil, then the default character `?r' is used.
8328 If ARTICLE is nil, then the article on the current line will be
8329 marked.
8330 Iff NO-EXPIRE, auto-expiry will be inhibited."
8331   ;; The mark might be a string.
8332   (when (stringp mark)
8333     (setq mark (aref mark 0)))
8334   ;; If no mark is given, then we check auto-expiring.
8335   (when (null mark)
8336     (setq mark gnus-del-mark))
8337   (when (and (not no-expire)
8338              gnus-newsgroup-auto-expire
8339              (memq mark gnus-auto-expirable-marks))
8340     (setq mark gnus-expirable-mark))
8341   (let ((article (or article (gnus-summary-article-number)))
8342         (old-mark (gnus-summary-article-mark article)))
8343     ;; Allow the backend to change the mark.
8344     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8345     (if (eq mark old-mark)
8346         t
8347       (unless article
8348         (error "No article on current line"))
8349       (if (not (if (or (= mark gnus-unread-mark)
8350                        (= mark gnus-ticked-mark)
8351                        (= mark gnus-dormant-mark))
8352                    (gnus-mark-article-as-unread article mark)
8353                  (gnus-mark-article-as-read article mark)))
8354           t
8355         ;; See whether the article is to be put in the cache.
8356         (and gnus-use-cache
8357              (not (= mark gnus-canceled-mark))
8358              (vectorp (gnus-summary-article-header article))
8359              (save-excursion
8360                (gnus-cache-possibly-enter-article
8361                 gnus-newsgroup-name article
8362                 (gnus-summary-article-header article)
8363                 (= mark gnus-ticked-mark)
8364                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8365
8366         (when (gnus-summary-goto-subject article nil t)
8367           (let ((buffer-read-only nil))
8368             (gnus-summary-show-thread)
8369             ;; Fix the mark.
8370             (gnus-summary-update-mark mark 'unread)
8371             t))))))
8372
8373 (defun gnus-summary-update-secondary-mark (article)
8374   "Update the secondary (read, process, cache) mark."
8375   (gnus-summary-update-mark
8376    (cond ((memq article gnus-newsgroup-processable)
8377           gnus-process-mark)
8378          ((memq article gnus-newsgroup-cached)
8379           gnus-cached-mark)
8380          ((memq article gnus-newsgroup-replied)
8381           gnus-replied-mark)
8382          ((memq article gnus-newsgroup-saved)
8383           gnus-saved-mark)
8384          (t gnus-unread-mark))
8385    'replied)
8386   (when (gnus-visual-p 'summary-highlight 'highlight)
8387     (gnus-run-hooks 'gnus-summary-update-hook))
8388   t)
8389
8390 (defun gnus-summary-update-mark (mark type)
8391   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8392         (buffer-read-only nil))
8393     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8394     (when forward
8395       (when (looking-at "\r")
8396         (incf forward))
8397       (when (<= (+ forward (point)) (point-max))
8398         ;; Go to the right position on the line.
8399         (goto-char (+ forward (point)))
8400         ;; Replace the old mark with the new mark.
8401         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8402         ;; Optionally update the marks by some user rule.
8403         (when (eq type 'unread)
8404           (gnus-data-set-mark
8405            (gnus-data-find (gnus-summary-article-number)) mark)
8406           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8407
8408 (defun gnus-mark-article-as-read (article &optional mark)
8409   "Enter ARTICLE in the pertinent lists and remove it from others."
8410   ;; Make the article expirable.
8411   (let ((mark (or mark gnus-del-mark)))
8412     (if (= mark gnus-expirable-mark)
8413         (push article gnus-newsgroup-expirable)
8414       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8415     ;; Remove from unread and marked lists.
8416     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8417     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8418     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8419     (push (cons article mark) gnus-newsgroup-reads)
8420     ;; Possibly remove from cache, if that is used.
8421     (when gnus-use-cache
8422       (gnus-cache-enter-remove-article article))
8423     t))
8424
8425 (defun gnus-mark-article-as-unread (article &optional mark)
8426   "Enter ARTICLE in the pertinent lists and remove it from others."
8427   (let ((mark (or mark gnus-ticked-mark)))
8428     (if (<= article 0)
8429         (progn
8430           (gnus-error 1 "Can't mark negative article numbers")
8431           nil)
8432       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8433             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8434             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8435             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8436
8437       ;; Unsuppress duplicates?
8438       (when gnus-suppress-duplicates
8439         (gnus-dup-unsuppress-article article))
8440
8441       (cond ((= mark gnus-ticked-mark)
8442              (push article gnus-newsgroup-marked))
8443             ((= mark gnus-dormant-mark)
8444              (push article gnus-newsgroup-dormant))
8445             (t
8446              (push article gnus-newsgroup-unreads)))
8447       (gnus-pull article gnus-newsgroup-reads)
8448       t)))
8449
8450 (defalias 'gnus-summary-mark-as-unread-forward
8451   'gnus-summary-tick-article-forward)
8452 (make-obsolete 'gnus-summary-mark-as-unread-forward
8453                'gnus-summary-tick-article-forward)
8454 (defun gnus-summary-tick-article-forward (n)
8455   "Tick N articles forwards.
8456 If N is negative, tick backwards instead.
8457 The difference between N and the number of articles ticked is returned."
8458   (interactive "p")
8459   (gnus-summary-mark-forward n gnus-ticked-mark))
8460
8461 (defalias 'gnus-summary-mark-as-unread-backward
8462   'gnus-summary-tick-article-backward)
8463 (make-obsolete 'gnus-summary-mark-as-unread-backward
8464                'gnus-summary-tick-article-backward)
8465 (defun gnus-summary-tick-article-backward (n)
8466   "Tick N articles backwards.
8467 The difference between N and the number of articles ticked is returned."
8468   (interactive "p")
8469   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8470
8471 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8472 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8473 (defun gnus-summary-tick-article (&optional article clear-mark)
8474   "Mark current article as unread.
8475 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8476 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8477   (interactive)
8478   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8479                                        gnus-ticked-mark)))
8480
8481 (defun gnus-summary-mark-as-read-forward (n)
8482   "Mark N articles as read forwards.
8483 If N is negative, mark backwards instead.
8484 The difference between N and the actual number of articles marked is
8485 returned."
8486   (interactive "p")
8487   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8488
8489 (defun gnus-summary-mark-as-read-backward (n)
8490   "Mark the N articles as read backwards.
8491 The difference between N and the actual number of articles marked is
8492 returned."
8493   (interactive "p")
8494   (gnus-summary-mark-forward
8495    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8496
8497 (defun gnus-summary-mark-as-read (&optional article mark)
8498   "Mark current article as read.
8499 ARTICLE specifies the article to be marked as read.
8500 MARK specifies a string to be inserted at the beginning of the line."
8501   (gnus-summary-mark-article article mark))
8502
8503 (defun gnus-summary-clear-mark-forward (n)
8504   "Clear marks from N articles forward.
8505 If N is negative, clear backward instead.
8506 The difference between N and the number of marks cleared is returned."
8507   (interactive "p")
8508   (gnus-summary-mark-forward n gnus-unread-mark))
8509
8510 (defun gnus-summary-clear-mark-backward (n)
8511   "Clear marks from N articles backward.
8512 The difference between N and the number of marks cleared is returned."
8513   (interactive "p")
8514   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8515
8516 (defun gnus-summary-mark-unread-as-read ()
8517   "Intended to be used by `gnus-summary-mark-article-hook'."
8518   (when (memq gnus-current-article gnus-newsgroup-unreads)
8519     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8520
8521 (defun gnus-summary-mark-read-and-unread-as-read ()
8522   "Intended to be used by `gnus-summary-mark-article-hook'."
8523   (let ((mark (gnus-summary-article-mark)))
8524     (when (or (gnus-unread-mark-p mark)
8525               (gnus-read-mark-p mark))
8526       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8527
8528 (defun gnus-summary-mark-region-as-read (point mark all)
8529   "Mark all unread articles between point and mark as read.
8530 If given a prefix, mark all articles between point and mark as read,
8531 even ticked and dormant ones."
8532   (interactive "r\nP")
8533   (save-excursion
8534     (let (article)
8535       (goto-char point)
8536       (beginning-of-line)
8537       (while (and
8538               (< (point) mark)
8539               (progn
8540                 (when (or all
8541                           (memq (setq article (gnus-summary-article-number))
8542                                 gnus-newsgroup-unreads))
8543                   (gnus-summary-mark-article article gnus-del-mark))
8544                 t)
8545               (gnus-summary-find-next))))))
8546
8547 (defun gnus-summary-mark-below (score mark)
8548   "Mark articles with score less than SCORE with MARK."
8549   (interactive "P\ncMark: ")
8550   (setq score (if score
8551                   (prefix-numeric-value score)
8552                 (or gnus-summary-default-score 0)))
8553   (save-excursion
8554     (set-buffer gnus-summary-buffer)
8555     (goto-char (point-min))
8556     (while
8557         (progn
8558           (and (< (gnus-summary-article-score) score)
8559                (gnus-summary-mark-article nil mark))
8560           (gnus-summary-find-next)))))
8561
8562 (defun gnus-summary-kill-below (&optional score)
8563   "Mark articles with score below SCORE as read."
8564   (interactive "P")
8565   (gnus-summary-mark-below score gnus-killed-mark))
8566
8567 (defun gnus-summary-clear-above (&optional score)
8568   "Clear all marks from articles with score above SCORE."
8569   (interactive "P")
8570   (gnus-summary-mark-above score gnus-unread-mark))
8571
8572 (defun gnus-summary-tick-above (&optional score)
8573   "Tick all articles with score above SCORE."
8574   (interactive "P")
8575   (gnus-summary-mark-above score gnus-ticked-mark))
8576
8577 (defun gnus-summary-mark-above (score mark)
8578   "Mark articles with score over SCORE with MARK."
8579   (interactive "P\ncMark: ")
8580   (setq score (if score
8581                   (prefix-numeric-value score)
8582                 (or gnus-summary-default-score 0)))
8583   (save-excursion
8584     (set-buffer gnus-summary-buffer)
8585     (goto-char (point-min))
8586     (while (and (progn
8587                   (when (> (gnus-summary-article-score) score)
8588                     (gnus-summary-mark-article nil mark))
8589                   t)
8590                 (gnus-summary-find-next)))))
8591
8592 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8593 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8594 (defun gnus-summary-limit-include-expunged (&optional no-error)
8595   "Display all the hidden articles that were expunged for low scores."
8596   (interactive)
8597   (let ((buffer-read-only nil))
8598     (let ((scored gnus-newsgroup-scored)
8599           headers h)
8600       (while scored
8601         (unless (gnus-summary-goto-subject (caar scored))
8602           (and (setq h (gnus-summary-article-header (caar scored)))
8603                (< (cdar scored) gnus-summary-expunge-below)
8604                (push h headers)))
8605         (setq scored (cdr scored)))
8606       (if (not headers)
8607           (when (not no-error)
8608             (error "No expunged articles hidden"))
8609         (goto-char (point-min))
8610         (gnus-summary-prepare-unthreaded (nreverse headers))
8611         (goto-char (point-min))
8612         (gnus-summary-position-point)
8613         t))))
8614
8615 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8616   "Mark all unread articles in this newsgroup as read.
8617 If prefix argument ALL is non-nil, ticked and dormant articles will
8618 also be marked as read.
8619 If QUIETLY is non-nil, no questions will be asked.
8620 If TO-HERE is non-nil, it should be a point in the buffer.  All
8621 articles before this point will be marked as read.
8622 Note that this function will only catch up the unread article
8623 in the current summary buffer limitation.
8624 The number of articles marked as read is returned."
8625   (interactive "P")
8626   (prog1
8627       (save-excursion
8628         (when (or quietly
8629                   (not gnus-interactive-catchup) ;Without confirmation?
8630                   gnus-expert-user
8631                   (gnus-y-or-n-p
8632                    (if all
8633                        "Mark absolutely all articles as read? "
8634                      "Mark all unread articles as read? ")))
8635           (if (and not-mark
8636                    (not gnus-newsgroup-adaptive)
8637                    (not gnus-newsgroup-auto-expire)
8638                    (not gnus-suppress-duplicates)
8639                    (or (not gnus-use-cache)
8640                        (eq gnus-use-cache 'passive)))
8641               (progn
8642                 (when all
8643                   (setq gnus-newsgroup-marked nil
8644                         gnus-newsgroup-dormant nil))
8645                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8646             ;; We actually mark all articles as canceled, which we
8647             ;; have to do when using auto-expiry or adaptive scoring.
8648             (gnus-summary-show-all-threads)
8649             (when (gnus-summary-first-subject (not all) t)
8650               (while (and
8651                       (if to-here (< (point) to-here) t)
8652                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8653                       (gnus-summary-find-next (not all) nil nil t))))
8654             (gnus-set-mode-line 'summary))
8655           t))
8656     (gnus-summary-position-point)))
8657
8658 (defun gnus-summary-catchup-to-here (&optional all)
8659   "Mark all unticked articles before the current one as read.
8660 If ALL is non-nil, also mark ticked and dormant articles as read."
8661   (interactive "P")
8662   (save-excursion
8663     (gnus-save-hidden-threads
8664       (let ((beg (point)))
8665         ;; We check that there are unread articles.
8666         (when (or all (gnus-summary-find-prev))
8667           (gnus-summary-catchup all t beg)))))
8668   (gnus-summary-position-point))
8669
8670 (defun gnus-summary-catchup-all (&optional quietly)
8671   "Mark all articles in this newsgroup as read."
8672   (interactive "P")
8673   (gnus-summary-catchup t quietly))
8674
8675 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8676   "Mark all unread articles in this group as read, then exit.
8677 If prefix argument ALL is non-nil, all articles are marked as read."
8678   (interactive "P")
8679   (when (gnus-summary-catchup all quietly nil 'fast)
8680     ;; Select next newsgroup or exit.
8681     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8682              (eq gnus-auto-select-next 'quietly))
8683         (gnus-summary-next-group nil)
8684       (gnus-summary-exit))))
8685
8686 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8687   "Mark all articles in this newsgroup as read, and then exit."
8688   (interactive "P")
8689   (gnus-summary-catchup-and-exit t quietly))
8690
8691 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8692   "Mark all articles in this group as read and select the next group.
8693 If given a prefix, mark all articles, unread as well as ticked, as
8694 read."
8695   (interactive "P")
8696   (save-excursion
8697     (gnus-summary-catchup all))
8698   (gnus-summary-next-group))
8699
8700 ;;;
8701 ;;; with article
8702 ;;;
8703
8704 (defmacro gnus-with-article (article &rest forms)
8705   "Select ARTICLE and perform FORMS in the original article buffer.
8706 Then replace the article with the result."
8707   `(progn
8708      ;; We don't want the article to be marked as read.
8709      (let (gnus-mark-article-hook)
8710        (gnus-summary-select-article t t nil ,article))
8711      (set-buffer gnus-original-article-buffer)
8712      ,@forms
8713      (if (not (gnus-check-backend-function
8714                'request-replace-article (car gnus-article-current)))
8715          (gnus-message 5 "Read-only group; not replacing")
8716        (unless (gnus-request-replace-article
8717                 ,article (car gnus-article-current)
8718                 (current-buffer) t)
8719          (error "Couldn't replace article")))
8720      ;; The cache and backlog have to be flushed somewhat.
8721      (when gnus-keep-backlog
8722        (gnus-backlog-remove-article
8723         (car gnus-article-current) (cdr gnus-article-current)))
8724      (when gnus-use-cache
8725        (gnus-cache-update-article
8726         (car gnus-article-current) (cdr gnus-article-current)))))
8727
8728 (put 'gnus-with-article 'lisp-indent-function 1)
8729 (put 'gnus-with-article 'edebug-form-spec '(form body))
8730
8731 ;; Thread-based commands.
8732
8733 (defun gnus-summary-articles-in-thread (&optional article)
8734   "Return a list of all articles in the current thread.
8735 If ARTICLE is non-nil, return all articles in the thread that starts
8736 with that article."
8737   (let* ((article (or article (gnus-summary-article-number)))
8738          (data (gnus-data-find-list article))
8739          (top-level (gnus-data-level (car data)))
8740          (top-subject
8741           (cond ((null gnus-thread-operation-ignore-subject)
8742                  (gnus-simplify-subject-re
8743                   (mail-header-subject (gnus-data-header (car data)))))
8744                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8745                  (gnus-simplify-subject-fuzzy
8746                   (mail-header-subject (gnus-data-header (car data)))))
8747                 (t nil)))
8748          (end-point (save-excursion
8749                       (if (gnus-summary-go-to-next-thread)
8750                           (point) (point-max))))
8751          articles)
8752     (while (and data
8753                 (< (gnus-data-pos (car data)) end-point))
8754       (when (or (not top-subject)
8755                 (string= top-subject
8756                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8757                              (gnus-simplify-subject-fuzzy
8758                               (mail-header-subject
8759                                (gnus-data-header (car data))))
8760                            (gnus-simplify-subject-re
8761                             (mail-header-subject
8762                              (gnus-data-header (car data)))))))
8763         (push (gnus-data-number (car data)) articles))
8764       (unless (and (setq data (cdr data))
8765                    (> (gnus-data-level (car data)) top-level))
8766         (setq data nil)))
8767     ;; Return the list of articles.
8768     (nreverse articles)))
8769
8770 (defun gnus-summary-rethread-current ()
8771   "Rethread the thread the current article is part of."
8772   (interactive)
8773   (let* ((gnus-show-threads t)
8774          (article (gnus-summary-article-number))
8775          (id (mail-header-id (gnus-summary-article-header)))
8776          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8777     (unless id
8778       (error "No article on the current line"))
8779     (gnus-rebuild-thread id)
8780     (gnus-summary-goto-subject article)))
8781
8782 (defun gnus-summary-reparent-thread ()
8783   "Make the current article child of the marked (or previous) article.
8784
8785 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8786 is non-nil or the Subject: of both articles are the same."
8787   (interactive)
8788   (unless (not (gnus-group-read-only-p))
8789     (error "The current newsgroup does not support article editing"))
8790   (unless (<= (length gnus-newsgroup-processable) 1)
8791     (error "No more than one article may be marked"))
8792   (save-window-excursion
8793     (let ((gnus-article-buffer " *reparent*")
8794           (current-article (gnus-summary-article-number))
8795           ;; First grab the marked article, otherwise one line up.
8796           (parent-article (if (not (null gnus-newsgroup-processable))
8797                               (car gnus-newsgroup-processable)
8798                             (save-excursion
8799                               (if (eq (forward-line -1) 0)
8800                                   (gnus-summary-article-number)
8801                                 (error "Beginning of summary buffer"))))))
8802       (unless (not (eq current-article parent-article))
8803         (error "An article may not be self-referential"))
8804       (let ((message-id (mail-header-id
8805                          (gnus-summary-article-header parent-article))))
8806         (unless (and message-id (not (equal message-id "")))
8807           (error "No message-id in desired parent"))
8808         (gnus-with-article current-article
8809           (goto-char (point-min))
8810           (if (re-search-forward "^References: " nil t)
8811               (progn
8812                 (re-search-forward "^[^ \t]" nil t)
8813                 (forward-line -1)
8814                 (end-of-line)
8815                 (insert " " message-id))
8816             (insert "References: " message-id "\n")))
8817         (set-buffer gnus-summary-buffer)
8818         (gnus-summary-unmark-all-processable)
8819         (gnus-summary-update-article current-article)
8820         (gnus-summary-rethread-current)
8821         (gnus-message 3 "Article %d is now the child of article %d"
8822                       current-article parent-article)))))
8823
8824 (defun gnus-summary-toggle-threads (&optional arg)
8825   "Toggle showing conversation threads.
8826 If ARG is positive number, turn showing conversation threads on."
8827   (interactive "P")
8828   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8829     (setq gnus-show-threads
8830           (if (null arg) (not gnus-show-threads)
8831             (> (prefix-numeric-value arg) 0)))
8832     (gnus-summary-prepare)
8833     (gnus-summary-goto-subject current)
8834     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8835     (gnus-summary-position-point)))
8836
8837 (defun gnus-summary-show-all-threads ()
8838   "Show all threads."
8839   (interactive)
8840   (save-excursion
8841     (let ((buffer-read-only nil))
8842       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8843   (gnus-summary-position-point))
8844
8845 (defun gnus-summary-show-thread ()
8846   "Show thread subtrees.
8847 Returns nil if no thread was there to be shown."
8848   (interactive)
8849   (let ((buffer-read-only nil)
8850         (orig (point))
8851         ;; first goto end then to beg, to have point at beg after let
8852         (end (progn (end-of-line) (point)))
8853         (beg (progn (beginning-of-line) (point))))
8854     (prog1
8855         ;; Any hidden lines here?
8856         (search-forward "\r" end t)
8857       (subst-char-in-region beg end ?\^M ?\n t)
8858       (goto-char orig)
8859       (gnus-summary-position-point))))
8860
8861 (defun gnus-summary-hide-all-threads ()
8862   "Hide all thread subtrees."
8863   (interactive)
8864   (save-excursion
8865     (goto-char (point-min))
8866     (gnus-summary-hide-thread)
8867     (while (zerop (gnus-summary-next-thread 1 t))
8868       (gnus-summary-hide-thread)))
8869   (gnus-summary-position-point))
8870
8871 (defun gnus-summary-hide-thread ()
8872   "Hide thread subtrees.
8873 Returns nil if no threads were there to be hidden."
8874   (interactive)
8875   (let ((buffer-read-only nil)
8876         (start (point))
8877         (article (gnus-summary-article-number)))
8878     (goto-char start)
8879     ;; Go forward until either the buffer ends or the subthread
8880     ;; ends.
8881     (when (and (not (eobp))
8882                (or (zerop (gnus-summary-next-thread 1 t))
8883                    (goto-char (point-max))))
8884       (prog1
8885           (if (and (> (point) start)
8886                    (search-backward "\n" start t))
8887               (progn
8888                 (subst-char-in-region start (point) ?\n ?\^M)
8889                 (gnus-summary-goto-subject article))
8890             (goto-char start)
8891             nil)))))
8892
8893 (defun gnus-summary-go-to-next-thread (&optional previous)
8894   "Go to the same level (or less) next thread.
8895 If PREVIOUS is non-nil, go to previous thread instead.
8896 Return the article number moved to, or nil if moving was impossible."
8897   (let ((level (gnus-summary-thread-level))
8898         (way (if previous -1 1))
8899         (beg (point)))
8900     (forward-line way)
8901     (while (and (not (eobp))
8902                 (< level (gnus-summary-thread-level)))
8903       (forward-line way))
8904     (if (eobp)
8905         (progn
8906           (goto-char beg)
8907           nil)
8908       (setq beg (point))
8909       (prog1
8910           (gnus-summary-article-number)
8911         (goto-char beg)))))
8912
8913 (defun gnus-summary-next-thread (n &optional silent)
8914   "Go to the same level next N'th thread.
8915 If N is negative, search backward instead.
8916 Returns the difference between N and the number of skips actually
8917 done.
8918
8919 If SILENT, don't output messages."
8920   (interactive "p")
8921   (let ((backward (< n 0))
8922         (n (abs n)))
8923     (while (and (> n 0)
8924                 (gnus-summary-go-to-next-thread backward))
8925       (decf n))
8926     (unless silent
8927       (gnus-summary-position-point))
8928     (when (and (not silent) (/= 0 n))
8929       (gnus-message 7 "No more threads"))
8930     n))
8931
8932 (defun gnus-summary-prev-thread (n)
8933   "Go to the same level previous N'th thread.
8934 Returns the difference between N and the number of skips actually
8935 done."
8936   (interactive "p")
8937   (gnus-summary-next-thread (- n)))
8938
8939 (defun gnus-summary-go-down-thread ()
8940   "Go down one level in the current thread."
8941   (let ((children (gnus-summary-article-children)))
8942     (when children
8943       (gnus-summary-goto-subject (car children)))))
8944
8945 (defun gnus-summary-go-up-thread ()
8946   "Go up one level in the current thread."
8947   (let ((parent (gnus-summary-article-parent)))
8948     (when parent
8949       (gnus-summary-goto-subject parent))))
8950
8951 (defun gnus-summary-down-thread (n)
8952   "Go down thread N steps.
8953 If N is negative, go up instead.
8954 Returns the difference between N and how many steps down that were
8955 taken."
8956   (interactive "p")
8957   (let ((up (< n 0))
8958         (n (abs n)))
8959     (while (and (> n 0)
8960                 (if up (gnus-summary-go-up-thread)
8961                   (gnus-summary-go-down-thread)))
8962       (setq n (1- n)))
8963     (gnus-summary-position-point)
8964     (when (/= 0 n)
8965       (gnus-message 7 "Can't go further"))
8966     n))
8967
8968 (defun gnus-summary-up-thread (n)
8969   "Go up thread N steps.
8970 If N is negative, go up instead.
8971 Returns the difference between N and how many steps down that were
8972 taken."
8973   (interactive "p")
8974   (gnus-summary-down-thread (- n)))
8975
8976 (defun gnus-summary-top-thread ()
8977   "Go to the top of the thread."
8978   (interactive)
8979   (while (gnus-summary-go-up-thread))
8980   (gnus-summary-article-number))
8981
8982 (defun gnus-summary-kill-thread (&optional unmark)
8983   "Mark articles under current thread as read.
8984 If the prefix argument is positive, remove any kinds of marks.
8985 If the prefix argument is negative, tick articles instead."
8986   (interactive "P")
8987   (when unmark
8988     (setq unmark (prefix-numeric-value unmark)))
8989   (let ((articles (gnus-summary-articles-in-thread)))
8990     (save-excursion
8991       ;; Expand the thread.
8992       (gnus-summary-show-thread)
8993       ;; Mark all the articles.
8994       (while articles
8995         (gnus-summary-goto-subject (car articles))
8996         (cond ((null unmark)
8997                (gnus-summary-mark-article-as-read gnus-killed-mark))
8998               ((> unmark 0)
8999                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9000               (t
9001                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9002         (setq articles (cdr articles))))
9003     ;; Hide killed subtrees.
9004     (and (null unmark)
9005          gnus-thread-hide-killed
9006          (gnus-summary-hide-thread))
9007     ;; If marked as read, go to next unread subject.
9008     (when (null unmark)
9009       ;; Go to next unread subject.
9010       (gnus-summary-next-subject 1 t)))
9011   (gnus-set-mode-line 'summary))
9012
9013 ;; Summary sorting commands
9014
9015 (defun gnus-summary-sort-by-number (&optional reverse)
9016   "Sort the summary buffer by article number.
9017 Argument REVERSE means reverse order."
9018   (interactive "P")
9019   (gnus-summary-sort 'number reverse))
9020
9021 (defun gnus-summary-sort-by-author (&optional reverse)
9022   "Sort the summary buffer by author name alphabetically.
9023 If case-fold-search is non-nil, case of letters is ignored.
9024 Argument REVERSE means reverse order."
9025   (interactive "P")
9026   (gnus-summary-sort 'author reverse))
9027
9028 (defun gnus-summary-sort-by-subject (&optional reverse)
9029   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9030 If case-fold-search is non-nil, case of letters is ignored.
9031 Argument REVERSE means reverse order."
9032   (interactive "P")
9033   (gnus-summary-sort 'subject reverse))
9034
9035 (defun gnus-summary-sort-by-date (&optional reverse)
9036   "Sort the summary buffer by date.
9037 Argument REVERSE means reverse order."
9038   (interactive "P")
9039   (gnus-summary-sort 'date reverse))
9040
9041 (defun gnus-summary-sort-by-score (&optional reverse)
9042   "Sort the summary buffer by score.
9043 Argument REVERSE means reverse order."
9044   (interactive "P")
9045   (gnus-summary-sort 'score reverse))
9046
9047 (defun gnus-summary-sort-by-lines (&optional reverse)
9048   "Sort the summary buffer by the number of lines.
9049 Argument REVERSE means reverse order."
9050   (interactive "P")
9051   (gnus-summary-sort 'lines reverse))
9052
9053 (defun gnus-summary-sort-by-chars (&optional reverse)
9054   "Sort the summary buffer by article length.
9055 Argument REVERSE means reverse order."
9056   (interactive "P")
9057   (gnus-summary-sort 'chars reverse))   
9058
9059 (defun gnus-summary-sort (predicate reverse)
9060   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9061   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9062          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9063          (gnus-thread-sort-functions
9064           (if (not reverse)
9065               thread
9066             `(lambda (t1 t2)
9067                (,thread t2 t1))))
9068          (gnus-article-sort-functions
9069           (if (not reverse)
9070               article
9071             `(lambda (t1 t2)
9072                (,article t2 t1))))
9073          (buffer-read-only)
9074          (gnus-summary-prepare-hook nil))
9075     ;; We do the sorting by regenerating the threads.
9076     (gnus-summary-prepare)
9077     ;; Hide subthreads if needed.
9078     (when (and gnus-show-threads gnus-thread-hide-subtree)
9079       (gnus-summary-hide-all-threads))))
9080
9081 ;; Summary saving commands.
9082
9083 (defun gnus-summary-save-article (&optional n not-saved)
9084   "Save the current article using the default saver function.
9085 If N is a positive number, save the N next articles.
9086 If N is a negative number, save the N previous articles.
9087 If N is nil and any articles have been marked with the process mark,
9088 save those articles instead.
9089 The variable `gnus-default-article-saver' specifies the saver function."
9090   (interactive "P")
9091   (let* ((articles (gnus-summary-work-articles n))
9092          (save-buffer (save-excursion
9093                         (nnheader-set-temp-buffer " *Gnus Save*")))
9094          (num (length articles))
9095          header file)
9096     (dolist (article articles)
9097       (setq header (gnus-summary-article-header article))
9098       (if (not (vectorp header))
9099           ;; This is a pseudo-article.
9100           (if (assq 'name header)
9101               (gnus-copy-file (cdr (assq 'name header)))
9102             (gnus-message 1 "Article %d is unsaveable" article))
9103         ;; This is a real article.
9104         (save-window-excursion
9105           (gnus-summary-select-article t nil nil article))
9106         (save-excursion
9107           (set-buffer save-buffer)
9108           (erase-buffer)
9109           (insert-buffer-substring gnus-original-article-buffer))
9110         (setq file (gnus-article-save save-buffer file num))
9111         (gnus-summary-remove-process-mark article)
9112         (unless not-saved
9113           (gnus-summary-set-saved-mark article))))
9114     (gnus-kill-buffer save-buffer)
9115     (gnus-summary-position-point)
9116     (gnus-set-mode-line 'summary)
9117     n))
9118
9119 (defun gnus-summary-pipe-output (&optional arg)
9120   "Pipe the current article to a subprocess.
9121 If N is a positive number, pipe the N next articles.
9122 If N is a negative number, pipe the N previous articles.
9123 If N is nil and any articles have been marked with the process mark,
9124 pipe those articles instead."
9125   (interactive "P")
9126   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9127     (gnus-summary-save-article arg t))
9128   (gnus-configure-windows 'pipe))
9129
9130 (defun gnus-summary-save-article-mail (&optional arg)
9131   "Append the current article to an mail file.
9132 If N is a positive number, save the N next articles.
9133 If N is a negative number, save the N previous articles.
9134 If N is nil and any articles have been marked with the process mark,
9135 save those articles instead."
9136   (interactive "P")
9137   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9138     (gnus-summary-save-article arg)))
9139
9140 (defun gnus-summary-save-article-rmail (&optional arg)
9141   "Append the current article to an rmail file.
9142 If N is a positive number, save the N next articles.
9143 If N is a negative number, save the N previous articles.
9144 If N is nil and any articles have been marked with the process mark,
9145 save those articles instead."
9146   (interactive "P")
9147   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9148     (gnus-summary-save-article arg)))
9149
9150 (defun gnus-summary-save-article-file (&optional arg)
9151   "Append the current article to a file.
9152 If N is a positive number, save the N next articles.
9153 If N is a negative number, save the N previous articles.
9154 If N is nil and any articles have been marked with the process mark,
9155 save those articles instead."
9156   (interactive "P")
9157   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9158     (gnus-summary-save-article arg)))
9159
9160 (defun gnus-summary-write-article-file (&optional arg)
9161   "Write the current article to a file, deleting the previous file.
9162 If N is a positive number, save the N next articles.
9163 If N is a negative number, save the N previous articles.
9164 If N is nil and any articles have been marked with the process mark,
9165 save those articles instead."
9166   (interactive "P")
9167   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9168     (gnus-summary-save-article arg)))
9169
9170 (defun gnus-summary-save-article-body-file (&optional arg)
9171   "Append the current article body to a file.
9172 If N is a positive number, save the N next articles.
9173 If N is a negative number, save the N previous articles.
9174 If N is nil and any articles have been marked with the process mark,
9175 save those articles instead."
9176   (interactive "P")
9177   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9178     (gnus-summary-save-article arg)))
9179
9180 (defun gnus-summary-pipe-message (program)
9181   "Pipe the current article through PROGRAM."
9182   (interactive "sProgram: ")
9183   (gnus-summary-select-article)
9184   (let ((mail-header-separator ""))
9185     (gnus-eval-in-buffer-window gnus-article-buffer
9186       (save-restriction
9187         (widen)
9188         (let ((start (window-start))
9189               buffer-read-only)
9190           (message-pipe-buffer-body program)
9191           (set-window-start (get-buffer-window (current-buffer)) start))))))
9192
9193 (defun gnus-get-split-value (methods)
9194   "Return a value based on the split METHODS."
9195   (let (split-name method result match)
9196     (when methods
9197       (save-excursion
9198         (set-buffer gnus-original-article-buffer)
9199         (save-restriction
9200           (nnheader-narrow-to-headers)
9201           (while methods
9202             (goto-char (point-min))
9203             (setq method (pop methods))
9204             (setq match (car method))
9205             (when (cond
9206                    ((stringp match)
9207                     ;; Regular expression.
9208                     (ignore-errors
9209                       (re-search-forward match nil t)))
9210                    ((gnus-functionp match)
9211                     ;; Function.
9212                     (save-restriction
9213                       (widen)
9214                       (setq result (funcall match gnus-newsgroup-name))))
9215                    ((consp match)
9216                     ;; Form.
9217                     (save-restriction
9218                       (widen)
9219                       (setq result (eval match)))))
9220               (setq split-name (append (cdr method) split-name))
9221               (cond ((stringp result)
9222                      (push (expand-file-name
9223                             result gnus-article-save-directory)
9224                            split-name))
9225                     ((consp result)
9226                      (setq split-name (append result split-name)))))))))
9227     (nreverse split-name)))
9228
9229 (defun gnus-valid-move-group-p (group)
9230   (and (boundp group)
9231        (symbol-name group)
9232        (symbol-value group)
9233        (gnus-get-function (gnus-find-method-for-group
9234                            (symbol-name group)) 'request-accept-article t)))
9235
9236 (defun gnus-read-move-group-name (prompt default articles prefix)
9237   "Read a group name."
9238   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9239          (minibuffer-confirm-incomplete nil) ; XEmacs
9240          (prom
9241           (format "%s %s to:"
9242                   prompt
9243                   (if (> (length articles) 1)
9244                       (format "these %d articles" (length articles))
9245                     "this article")))
9246          (to-newsgroup
9247           (cond
9248            ((null split-name)
9249             (gnus-completing-read default prom
9250                                   gnus-active-hashtb
9251                                   'gnus-valid-move-group-p
9252                                   nil prefix
9253                                   'gnus-group-history))
9254            ((= 1 (length split-name))
9255             (gnus-completing-read (car split-name) prom
9256                                   gnus-active-hashtb
9257                                   'gnus-valid-move-group-p
9258                                   nil nil
9259                                   'gnus-group-history))
9260            (t
9261             (gnus-completing-read nil prom
9262                                   (mapcar (lambda (el) (list el))
9263                                           (nreverse split-name))
9264                                   nil nil nil
9265                                   'gnus-group-history))))
9266          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9267     (when to-newsgroup
9268       (if (or (string= to-newsgroup "")
9269               (string= to-newsgroup prefix))
9270           (setq to-newsgroup default))
9271       (unless to-newsgroup
9272         (error "No group name entered"))
9273       (or (gnus-active to-newsgroup)
9274           (gnus-activate-group to-newsgroup nil nil to-method)
9275           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9276                                      to-newsgroup))
9277               (or (and (gnus-request-create-group to-newsgroup to-method)
9278                        (gnus-activate-group
9279                         to-newsgroup nil nil to-method)
9280                        (gnus-subscribe-group to-newsgroup))
9281                   (error "Couldn't create group %s" to-newsgroup)))
9282           (error "No such group: %s" to-newsgroup)))
9283     to-newsgroup))
9284
9285 (defun gnus-summary-save-parts (type dir n &optional reverse)
9286   "Save parts matching TYPE to DIR.
9287 If REVERSE, save parts that do not match TYPE."
9288   (interactive
9289    (list (read-string "Save parts of type: " "image/.*")
9290          (read-file-name "Save to directory: " nil nil t)
9291          current-prefix-arg))
9292   (gnus-summary-iterate n
9293     (let ((gnus-display-mime-function nil)
9294           (gnus-inhibit-treatment t))
9295       (gnus-summary-select-article))
9296     (save-excursion
9297       (set-buffer gnus-article-buffer)
9298       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
9299         (when handles
9300           (gnus-summary-save-parts-1 type dir handles reverse)
9301           (mm-destroy-parts handles))))))
9302
9303 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9304   (if (stringp (car handle))
9305       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9306               (cdr handle))
9307     (when (if reverse
9308               (not (string-match type (mm-handle-media-type handle)))
9309             (string-match type (mm-handle-media-type handle)))
9310       (let ((file (expand-file-name
9311                    (file-name-nondirectory
9312                     (or
9313                      (mail-content-type-get
9314                       (mm-handle-disposition handle) 'filename)
9315                      (concat gnus-newsgroup-name "." gnus-current-article)))
9316                    dir)))
9317         (unless (file-exists-p file)
9318           (mm-save-part-to-file handle file))))))
9319
9320 ;; Summary extract commands
9321
9322 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9323   (let ((buffer-read-only nil)
9324         (article (gnus-summary-article-number))
9325         after-article b e)
9326     (unless (gnus-summary-goto-subject article)
9327       (error "No such article: %d" article))
9328     (gnus-summary-position-point)
9329     ;; If all commands are to be bunched up on one line, we collect
9330     ;; them here.
9331     (unless gnus-view-pseudos-separately
9332       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9333             files action)
9334         (while ps
9335           (setq action (cdr (assq 'action (car ps))))
9336           (setq files (list (cdr (assq 'name (car ps)))))
9337           (while (and ps (cdr ps)
9338                       (string= (or action "1")
9339                                (or (cdr (assq 'action (cadr ps))) "2")))
9340             (push (cdr (assq 'name (cadr ps))) files)
9341             (setcdr ps (cddr ps)))
9342           (when files
9343             (when (not (string-match "%s" action))
9344               (push " " files))
9345             (push " " files)
9346             (when (assq 'execute (car ps))
9347               (setcdr (assq 'execute (car ps))
9348                       (funcall (if (string-match "%s" action)
9349                                    'format 'concat)
9350                                action
9351                                (mapconcat
9352                                 (lambda (f)
9353                                   (if (equal f " ")
9354                                       f
9355                                     (gnus-quote-arg-for-sh-or-csh f)))
9356                                 files " ")))))
9357           (setq ps (cdr ps)))))
9358     (if (and gnus-view-pseudos (not not-view))
9359         (while pslist
9360           (when (assq 'execute (car pslist))
9361             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9362                                   (eq gnus-view-pseudos 'not-confirm)))
9363           (setq pslist (cdr pslist)))
9364       (save-excursion
9365         (while pslist
9366           (setq after-article (or (cdr (assq 'article (car pslist)))
9367                                   (gnus-summary-article-number)))
9368           (gnus-summary-goto-subject after-article)
9369           (forward-line 1)
9370           (setq b (point))
9371           (insert "    " (file-name-nondirectory
9372                           (cdr (assq 'name (car pslist))))
9373                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9374           (setq e (point))
9375           (forward-line -1)             ; back to `b'
9376           (gnus-add-text-properties
9377            b (1- e) (list 'gnus-number gnus-reffed-article-number
9378                           gnus-mouse-face-prop gnus-mouse-face))
9379           (gnus-data-enter
9380            after-article gnus-reffed-article-number
9381            gnus-unread-mark b (car pslist) 0 (- e b))
9382           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9383           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9384           (setq pslist (cdr pslist)))))))
9385
9386 (defun gnus-pseudos< (p1 p2)
9387   (let ((c1 (cdr (assq 'action p1)))
9388         (c2 (cdr (assq 'action p2))))
9389     (and c1 c2 (string< c1 c2))))
9390
9391 (defun gnus-request-pseudo-article (props)
9392   (cond ((assq 'execute props)
9393          (gnus-execute-command (cdr (assq 'execute props)))))
9394   (let ((gnus-current-article (gnus-summary-article-number)))
9395     (gnus-run-hooks 'gnus-mark-article-hook)))
9396
9397 (defun gnus-execute-command (command &optional automatic)
9398   (save-excursion
9399     (gnus-article-setup-buffer)
9400     (set-buffer gnus-article-buffer)
9401     (setq buffer-read-only nil)
9402     (let ((command (if automatic command
9403                      (read-string "Command: " (cons command 0)))))
9404       (erase-buffer)
9405       (insert "$ " command "\n\n")
9406       (if gnus-view-pseudo-asynchronously
9407           (start-process "gnus-execute" (current-buffer) shell-file-name
9408                          shell-command-switch command)
9409         (call-process shell-file-name nil t nil
9410                       shell-command-switch command)))))
9411
9412 ;; Summary kill commands.
9413
9414 (defun gnus-summary-edit-global-kill (article)
9415   "Edit the \"global\" kill file."
9416   (interactive (list (gnus-summary-article-number)))
9417   (gnus-group-edit-global-kill article))
9418
9419 (defun gnus-summary-edit-local-kill ()
9420   "Edit a local kill file applied to the current newsgroup."
9421   (interactive)
9422   (setq gnus-current-headers (gnus-summary-article-header))
9423   (gnus-group-edit-local-kill
9424    (gnus-summary-article-number) gnus-newsgroup-name))
9425
9426 ;;; Header reading.
9427
9428 (defun gnus-read-header (id &optional header)
9429   "Read the headers of article ID and enter them into the Gnus system."
9430   (let ((group gnus-newsgroup-name)
9431         (gnus-override-method
9432          (or
9433           gnus-override-method
9434           (and (gnus-news-group-p gnus-newsgroup-name)
9435                (car (gnus-refer-article-methods)))))
9436         where)
9437     ;; First we check to see whether the header in question is already
9438     ;; fetched.
9439     (if (stringp id)
9440         ;; This is a Message-ID.
9441         (setq header (or header (gnus-id-to-header id)))
9442       ;; This is an article number.
9443       (setq header (or header (gnus-summary-article-header id))))
9444     (if (and header
9445              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9446         ;; We have found the header.
9447         header
9448       ;; If this is a sparse article, we have to nix out its
9449       ;; previous entry in the thread hashtb.
9450       (when (and header
9451                  (gnus-summary-article-sparse-p (mail-header-number header)))
9452         (let* ((parent (gnus-parent-id (mail-header-references header)))
9453                (thread (and parent (gnus-id-to-thread parent))))
9454           (when thread
9455             (delq (assq header thread) thread))))
9456       ;; We have to really fetch the header to this article.
9457       (save-excursion
9458         (set-buffer nntp-server-buffer)
9459         (when (setq where (gnus-request-head id group))
9460           (nnheader-fold-continuation-lines)
9461           (goto-char (point-max))
9462           (insert ".\n")
9463           (goto-char (point-min))
9464           (insert "211 ")
9465           (princ (cond
9466                   ((numberp id) id)
9467                   ((cdr where) (cdr where))
9468                   (header (mail-header-number header))
9469                   (t gnus-reffed-article-number))
9470                  (current-buffer))
9471           (insert " Article retrieved.\n"))
9472         (if (or (not where)
9473                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9474             ()                          ; Malformed head.
9475           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9476             (when (and (stringp id)
9477                        (not (string= (gnus-group-real-name group)
9478                                      (car where))))
9479               ;; If we fetched by Message-ID and the article came
9480               ;; from a different group, we fudge some bogus article
9481               ;; numbers for this article.
9482               (mail-header-set-number header gnus-reffed-article-number))
9483             (save-excursion
9484               (set-buffer gnus-summary-buffer)
9485               (decf gnus-reffed-article-number)
9486               (gnus-remove-header (mail-header-number header))
9487               (push header gnus-newsgroup-headers)
9488               (setq gnus-current-headers header)
9489               (push (mail-header-number header) gnus-newsgroup-limit)))
9490           header)))))
9491
9492 (defun gnus-remove-header (number)
9493   "Remove header NUMBER from `gnus-newsgroup-headers'."
9494   (if (and gnus-newsgroup-headers
9495            (= number (mail-header-number (car gnus-newsgroup-headers))))
9496       (pop gnus-newsgroup-headers)
9497     (let ((headers gnus-newsgroup-headers))
9498       (while (and (cdr headers)
9499                   (not (= number (mail-header-number (cadr headers)))))
9500         (pop headers))
9501       (when (cdr headers)
9502         (setcdr headers (cddr headers))))))
9503
9504 ;;;
9505 ;;; summary highlights
9506 ;;;
9507
9508 (defun gnus-highlight-selected-summary ()
9509   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9510   ;; Highlight selected article in summary buffer
9511   (when gnus-summary-selected-face
9512     (save-excursion
9513       (let* ((beg (progn (beginning-of-line) (point)))
9514              (end (progn (end-of-line) (point)))
9515              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9516              (from (if (get-text-property beg gnus-mouse-face-prop)
9517                        beg
9518                      (or (next-single-property-change
9519                           beg gnus-mouse-face-prop nil end)
9520                          beg)))
9521              (to
9522               (if (= from end)
9523                   (- from 2)
9524                 (or (next-single-property-change
9525                      from gnus-mouse-face-prop nil end)
9526                     end))))
9527         ;; If no mouse-face prop on line we will have to = from = end,
9528         ;; so we highlight the entire line instead.
9529         (when (= (+ to 2) from)
9530           (setq from beg)
9531           (setq to end))
9532         (if gnus-newsgroup-selected-overlay
9533             ;; Move old overlay.
9534             (gnus-move-overlay
9535              gnus-newsgroup-selected-overlay from to (current-buffer))
9536           ;; Create new overlay.
9537           (gnus-overlay-put
9538            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9539            'face gnus-summary-selected-face))))))
9540
9541 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9542 (defun gnus-summary-highlight-line ()
9543   "Highlight current line according to `gnus-summary-highlight'."
9544   (let* ((list gnus-summary-highlight)
9545          (p (point))
9546          (end (progn (end-of-line) (point)))
9547          ;; now find out where the line starts and leave point there.
9548          (beg (progn (beginning-of-line) (point)))
9549          (article (gnus-summary-article-number))
9550          (score (or (cdr (assq (or article gnus-current-article)
9551                                gnus-newsgroup-scored))
9552                     gnus-summary-default-score 0))
9553          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9554          (inhibit-read-only t))
9555     ;; Eval the cars of the lists until we find a match.
9556     (let ((default gnus-summary-default-score))
9557       (while (and list
9558                   (not (eval (caar list))))
9559         (setq list (cdr list))))
9560     (let ((face (cdar list)))
9561       (unless (eq face (get-text-property beg 'face))
9562         (gnus-put-text-property-excluding-characters-with-faces
9563          beg end 'face
9564          (setq face (if (boundp face) (symbol-value face) face)))
9565         (when gnus-summary-highlight-line-function
9566           (funcall gnus-summary-highlight-line-function article face))))
9567     (goto-char p)))
9568
9569 (defun gnus-update-read-articles (group unread &optional compute)
9570   "Update the list of read articles in GROUP."
9571   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9572          (entry (gnus-gethash group gnus-newsrc-hashtb))
9573          (info (nth 2 entry))
9574          (prev 1)
9575          (unread (sort (copy-sequence unread) '<))
9576          read)
9577     (if (or (not info) (not active))
9578         ;; There is no info on this group if it was, in fact,
9579         ;; killed.  Gnus stores no information on killed groups, so
9580         ;; there's nothing to be done.
9581         ;; One could store the information somewhere temporarily,
9582         ;; perhaps...  Hmmm...
9583         ()
9584       ;; Remove any negative articles numbers.
9585       (while (and unread (< (car unread) 0))
9586         (setq unread (cdr unread)))
9587       ;; Remove any expired article numbers
9588       (while (and unread (< (car unread) (car active)))
9589         (setq unread (cdr unread)))
9590       ;; Compute the ranges of read articles by looking at the list of
9591       ;; unread articles.
9592       (while unread
9593         (when (/= (car unread) prev)
9594           (push (if (= prev (1- (car unread))) prev
9595                   (cons prev (1- (car unread))))
9596                 read))
9597         (setq prev (1+ (car unread)))
9598         (setq unread (cdr unread)))
9599       (when (<= prev (cdr active))
9600         (push (cons prev (cdr active)) read))
9601       (setq read (if (> (length read) 1) (nreverse read) read))
9602       (if compute
9603           read
9604         (save-excursion
9605           (set-buffer gnus-group-buffer)
9606           (gnus-undo-register
9607             `(progn
9608                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9609                (gnus-info-set-read ',info ',(gnus-info-read info))
9610                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9611                (gnus-group-update-group ,group t))))
9612         ;; Propagate the read marks to the backend.
9613         (if (gnus-check-backend-function 'request-set-mark group)
9614             (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9615                   (add (gnus-remove-from-range read (gnus-info-read info))))
9616               (when (or add del)
9617                 (unless (gnus-check-group group)
9618                   (error "Can't open server for %s" group))
9619                 (gnus-request-set-mark
9620                  group (delq nil (list (if add (list add 'add '(read)))
9621                                        (if del (list del 'del '(read)))))))))
9622         ;; Enter this list into the group info.
9623         (gnus-info-set-read info read)
9624         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9625         (gnus-get-unread-articles-in-group info (gnus-active group))
9626         t))))
9627
9628 (defun gnus-offer-save-summaries ()
9629   "Offer to save all active summary buffers."
9630   (save-excursion
9631     (let ((buflist (buffer-list))
9632           buffers bufname)
9633       ;; Go through all buffers and find all summaries.
9634       (while buflist
9635         (and (setq bufname (buffer-name (car buflist)))
9636              (string-match "Summary" bufname)
9637              (save-excursion
9638                (set-buffer bufname)
9639                ;; We check that this is, indeed, a summary buffer.
9640                (and (eq major-mode 'gnus-summary-mode)
9641                     ;; Also make sure this isn't bogus.
9642                     gnus-newsgroup-prepared
9643                     ;; Also make sure that this isn't a dead summary buffer.
9644                     (not gnus-dead-summary-mode)))
9645              (push bufname buffers))
9646         (setq buflist (cdr buflist)))
9647       ;; Go through all these summary buffers and offer to save them.
9648       (when buffers
9649         (map-y-or-n-p
9650          "Update summary buffer %s? "
9651          (lambda (buf)
9652            (switch-to-buffer buf)
9653            (gnus-summary-exit))
9654          buffers)))))
9655
9656
9657 ;;; @ for mime-partial
9658 ;;;
9659
9660 (defun gnus-request-partial-message ()
9661   (save-excursion
9662     (let ((number (gnus-summary-article-number))
9663           (group gnus-newsgroup-name)
9664           (mother gnus-article-buffer))
9665       (set-buffer (get-buffer-create " *Partial Article*"))
9666       (erase-buffer)
9667       (setq mime-preview-buffer mother)
9668       (gnus-request-article-this-buffer number group)
9669       (mime-parse-buffer)
9670       )))
9671
9672 (autoload 'mime-combine-message/partial-pieces-automatically
9673   "mime-partial"
9674   "Internal method to combine message/partial messages automatically.")
9675
9676 (mime-add-condition
9677  'action '((type . message)(subtype . partial)
9678            (major-mode . gnus-original-article-mode)
9679            (method . mime-combine-message/partial-pieces-automatically)
9680            (summary-buffer-exp . gnus-summary-buffer)
9681            (request-partial-message-method . gnus-request-partial-message)
9682            ))
9683
9684
9685 ;;; @ for message/rfc822
9686 ;;;
9687
9688 (defun gnus-mime-extract-message/rfc822 (entity situation)
9689   (let (group article num cwin swin cur)
9690     (with-current-buffer (mime-entity-buffer entity)
9691       (save-restriction
9692         (narrow-to-region (mime-entity-body-start entity)
9693                           (mime-entity-body-end entity))
9694         (setq group (or (cdr (assq 'group situation))
9695                         (completing-read "Group: "
9696                                          gnus-active-hashtb
9697                                          nil
9698                                          (gnus-read-active-file-p)
9699                                          gnus-newsgroup-name))
9700               article (gnus-request-accept-article group)
9701               )
9702         ))
9703     (when (and (consp article)
9704                (numberp (setq article (cdr article))))
9705       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9706             cwin (get-buffer-window (current-buffer) t)
9707             )
9708       (save-window-excursion
9709         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9710             (select-window swin)
9711           (set-buffer gnus-summary-buffer)
9712           )
9713         (setq cur gnus-current-article)
9714         (forward-line num)
9715         (let (gnus-show-threads)
9716           (gnus-summary-goto-subject article t)
9717           )
9718         (gnus-summary-clear-mark-forward 1)
9719         (gnus-summary-goto-subject cur)
9720         )
9721       (when (and cwin (window-frame cwin))
9722         (select-frame (window-frame cwin))
9723         )
9724       (when (boundp 'mime-acting-situation-to-override)
9725         (set-alist 'mime-acting-situation-to-override
9726                    'group
9727                    group)
9728         (set-alist 'mime-acting-situation-to-override
9729                    'after-method
9730                    `(progn
9731                       (save-current-buffer
9732                         (set-buffer gnus-group-buffer)
9733                         (gnus-activate-group ,group)
9734                         )
9735                       (gnus-summary-goto-article ,cur
9736                                                  gnus-show-all-headers)
9737                       ))
9738         (set-alist 'mime-acting-situation-to-override
9739                    'number num)
9740         )
9741       )))
9742
9743 (mime-add-condition
9744  'action '((type . message)(subtype . rfc822)
9745            (major-mode . gnus-original-article-mode)
9746            (method . gnus-mime-extract-message/rfc822)
9747            (mode . "extract")
9748            ))
9749
9750 (mime-add-condition
9751  'action '((type . message)(subtype . news)
9752            (major-mode . gnus-original-article-mode)
9753            (method . gnus-mime-extract-message/rfc822)
9754            (mode . "extract")
9755            ))
9756
9757 (defun gnus-mime-extract-multipart (entity situation)
9758   (let ((children (mime-entity-children entity))
9759         mime-acting-situation-to-override
9760         f)
9761     (while children
9762       (mime-play-entity (car children)
9763                         (cons (assq 'mode situation)
9764                               mime-acting-situation-to-override))
9765       (setq children (cdr children)))
9766     (if (setq f (cdr (assq 'after-method
9767                            mime-acting-situation-to-override)))
9768         (eval f)
9769       )))  
9770
9771 (mime-add-condition
9772  'action '((type . multipart)
9773            (method . gnus-mime-extract-multipart)
9774            (mode . "extract")
9775            )
9776  'with-default)
9777
9778
9779 ;;; @ end
9780 ;;;
9781
9782 (defun gnus-summary-setup-default-charset ()
9783   "Setup newsgroup default charset."
9784   (if (equal gnus-newsgroup-name "nndraft:drafts")
9785       (setq gnus-newsgroup-charset nil)
9786     (let* ((name (and gnus-newsgroup-name
9787                       (gnus-group-real-name gnus-newsgroup-name)))
9788            (ignored-charsets 
9789             (or gnus-newsgroup-ephemeral-ignored-charsets
9790                 (append
9791                  (and gnus-newsgroup-name
9792                       (or (gnus-group-find-parameter gnus-newsgroup-name
9793                                                      'ignored-charsets t)
9794                           (let ((alist gnus-group-ignored-charsets-alist)
9795                                 elem (charsets nil))
9796                             (while (setq elem (pop alist))
9797                               (when (and name
9798                                          (string-match (car elem) name))
9799                                 (setq alist nil
9800                                       charsets (cdr elem))))
9801                             charsets)))
9802                  gnus-newsgroup-ignored-charsets))))
9803       (setq gnus-newsgroup-charset
9804             (or gnus-newsgroup-ephemeral-charset
9805                 (and gnus-newsgroup-name
9806                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9807                          (let ((alist gnus-group-charset-alist)
9808                                elem charset)
9809                            (while (setq elem (pop alist))
9810                              (when (and name
9811                                         (string-match (car elem) name))
9812                                (setq alist nil
9813                                      charset (cadr elem))))
9814                            charset)))
9815                 gnus-default-charset))
9816       (set (make-local-variable 'gnus-newsgroup-ignored-charsets) 
9817            ignored-charsets))))
9818
9819 ;;;
9820 ;;; Mime Commands
9821 ;;;
9822
9823 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9824   "Display the current article buffer fully MIME-buttonized.
9825 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9826 treated as multipart/mixed."
9827   (interactive "P")
9828   (require 'gnus-art)
9829   (let ((gnus-unbuttonized-mime-types nil)
9830         (gnus-mime-display-multipart-as-mixed show-all-parts))
9831     (gnus-summary-show-article)))
9832
9833 (defun gnus-summary-repair-multipart (article)
9834   "Add a Content-Type header to a multipart article without one."
9835   (interactive (list (gnus-summary-article-number)))
9836   (gnus-with-article article
9837     (message-narrow-to-head)
9838     (goto-char (point-max))
9839     (widen)
9840     (when (search-forward "\n--" nil t)
9841       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9842         (message-narrow-to-head)
9843         (message-remove-header "Mime-Version")
9844         (message-remove-header "Content-Type")
9845         (goto-char (point-max))
9846         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9847                         separator))
9848         (insert "Mime-Version: 1.0\n")
9849         (widen))))
9850   (let (gnus-mark-article-hook)
9851     (gnus-summary-select-article t t nil article)))
9852
9853 (defun gnus-summary-toggle-display-buttonized ()
9854   "Toggle the buttonizing of the article buffer."
9855   (interactive)
9856   (require 'gnus-art)
9857   (if (setq gnus-inhibit-mime-unbuttonizing
9858             (not gnus-inhibit-mime-unbuttonizing))
9859       (let ((gnus-unbuttonized-mime-types nil))
9860         (gnus-summary-show-article))
9861     (gnus-summary-show-article)))
9862
9863 ;;;
9864 ;;; Intelli-mouse commmands
9865 ;;;
9866
9867 (defun gnus-wheel-summary-scroll (event)
9868   (interactive "e")
9869   (let ((amount (if (memq 'shift (event-modifiers event))
9870                     (car gnus-wheel-scroll-amount)
9871                   (cdr gnus-wheel-scroll-amount)))
9872         (direction (- (* (static-if (featurep 'xemacs)
9873                              (event-button event)
9874                            (cond ((eq 'mouse-4 (event-basic-type event))
9875                                   4)
9876                                  ((eq 'mouse-5 (event-basic-type event))
9877                                   5)))
9878                          2) 9))
9879         edge)
9880     (gnus-summary-scroll-up (* amount direction))
9881     (when (gnus-eval-in-buffer-window gnus-article-buffer
9882             (save-restriction
9883               (widen)
9884               (and (if (< 0 direction)
9885                        (gnus-article-next-page 0)
9886                      (gnus-article-prev-page 0)
9887                      (bobp))
9888                    (if (setq edge (get-text-property
9889                                    (point-min) 'gnus-wheel-edge))
9890                        (setq edge (* edge direction))
9891                      (setq edge -1))
9892                    (or (plusp edge)
9893                        (let ((buffer-read-only nil)
9894                              (inhibit-read-only t))
9895                          (put-text-property (point-min) (point-max)
9896                                             'gnus-wheel-edge direction)
9897                          nil))
9898                    (or (> edge gnus-wheel-edge-resistance)
9899                        (let ((buffer-read-only nil)
9900                              (inhibit-read-only t))
9901                          (put-text-property (point-min) (point-max)
9902                                             'gnus-wheel-edge
9903                                             (* (1+ edge) direction))
9904                          nil))
9905                    (eq last-command 'gnus-wheel-summary-scroll))))
9906       (gnus-summary-next-article nil nil (minusp direction)))))
9907
9908 (defun gnus-wheel-install ()
9909   "Enable mouse wheel support on summary window."
9910   (when gnus-use-wheel
9911     (let ((keys 
9912            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9913       (dolist (key keys)
9914         (define-key gnus-summary-mode-map key
9915           'gnus-wheel-summary-scroll)))))
9916
9917 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9918
9919 ;;;
9920 ;;; Traditional PGP commmands
9921 ;;;
9922
9923 (defun gnus-summary-decrypt-article (&optional force)
9924   "Decrypt the current article in traditional PGP way.
9925 This will have permanent effect only in mail groups.
9926 If FORCE is non-nil, allow editing of articles even in read-only
9927 groups."
9928   (interactive "P")
9929   (gnus-summary-select-article t)
9930   (gnus-eval-in-buffer-window gnus-article-buffer
9931     (save-excursion
9932       (save-restriction
9933         (widen)
9934         (goto-char (point-min))
9935         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9936           (error "Not a traditional PGP message!"))
9937         (let ((armor-start (match-beginning 0)))
9938           (if (and (pgg-decrypt-region armor-start (point-max))
9939                    (or force (not (gnus-group-read-only-p))))
9940               (let ((inhibit-read-only t) 
9941                     buffer-read-only)
9942                 (delete-region armor-start
9943                                (progn 
9944                                  (re-search-forward "^-+END PGP" nil t)
9945                                  (beginning-of-line 2)
9946                                  (point)))
9947                 (insert-buffer-substring pgg-output-buffer))))))))
9948
9949 (defun gnus-summary-verify-article ()
9950   "Verify the current article in traditional PGP way."
9951   (interactive)
9952   (save-excursion
9953     (set-buffer gnus-original-article-buffer)
9954     (goto-char (point-min))
9955     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
9956       (error "Not a traditional PGP message!"))
9957     (re-search-forward "^-+END PGP" nil t)
9958     (beginning-of-line 2)
9959     (call-interactively (function pgg-verify-region))))
9960
9961 ;;;
9962 ;;; Generic summary marking commands
9963 ;;;
9964
9965 (defvar gnus-summary-marking-alist
9966   '((read gnus-del-mark "d")
9967     (unread gnus-unread-mark "u")
9968     (ticked gnus-ticked-mark "!")
9969     (dormant gnus-dormant-mark "?")
9970     (expirable gnus-expirable-mark "e"))
9971   "An alist of names/marks/keystrokes.")
9972
9973 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9974 (defvar gnus-summary-mark-map)
9975
9976 (defun gnus-summary-make-all-marking-commands ()
9977   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9978   (dolist (elem gnus-summary-marking-alist)
9979     (apply 'gnus-summary-make-marking-command elem)))
9980
9981 (defun gnus-summary-make-marking-command (name mark keystroke)
9982   (let ((map (make-sparse-keymap)))
9983     (define-key gnus-summary-generic-mark-map keystroke map)
9984     (dolist (lway `((next "next" next nil "n")
9985                     (next-unread "next unread" next t "N")
9986                     (prev "previous" prev nil "p")
9987                     (prev-unread "previous unread" prev t "P")
9988                     (nomove "" nil nil ,keystroke)))
9989       (let ((func (gnus-summary-make-marking-command-1
9990                    mark (car lway) lway name)))
9991         (setq func (eval func))
9992         (define-key map (nth 4 lway) func)))))
9993       
9994 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9995   `(defun ,(intern
9996             (format "gnus-summary-put-mark-as-%s%s"
9997                     name (if (eq way 'nomove)
9998                              ""
9999                            (concat "-" (symbol-name way)))))
10000      (n)
10001      ,(format
10002        "Mark the current article as %s%s.
10003 If N, the prefix, then repeat N times.
10004 If N is negative, move in reverse order.
10005 The difference between N and the actual number of articles marked is
10006 returned."
10007        name (car (cdr lway)))
10008      (interactive "p")
10009      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10010     
10011 (defun gnus-summary-generic-mark (n mark move unread)
10012   "Mark N articles with MARK."
10013   (unless (eq major-mode 'gnus-summary-mode)
10014     (error "This command can only be used in the summary buffer"))
10015   (gnus-summary-show-thread)
10016   (let ((nummove
10017          (cond
10018           ((eq move 'next) 1)
10019           ((eq move 'prev) -1)
10020           (t 0))))
10021     (if (zerop nummove)
10022         (setq n 1)
10023       (when (< n 0)
10024         (setq n (abs n)
10025               nummove (* -1 nummove))))
10026     (while (and (> n 0)
10027                 (gnus-summary-mark-article nil mark)
10028                 (zerop (gnus-summary-next-subject nummove unread t)))
10029       (setq n (1- n)))
10030     (when (/= 0 n)
10031       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10032     (gnus-summary-recenter)
10033     (gnus-summary-position-point)
10034     (gnus-set-mode-line 'summary)
10035     n))
10036
10037 (gnus-summary-make-all-marking-commands)
10038
10039 (gnus-ems-redefine)
10040
10041 (provide 'gnus-sum)
10042
10043 (run-hooks 'gnus-sum-load-hook)
10044
10045 ;;; gnus-sum.el ends here