Synch 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 (defcustom gnus-orphan-score nil
934   "*All orphans get this score added.  Set in the score file."
935   :group 'gnus-score-default
936   :type '(choice (const nil)
937                  integer))
938
939 (defcustom gnus-summary-save-parts-default-mime "image/.*"
940   "*A regexp to match MIME parts when saving multiple parts of a message
941 with gnus-summary-save-parts (X m). This regexp will be used by default
942 when prompting the user for which type of files to save."
943   :group 'gnus-summary
944   :type 'regexp)
945
946
947 ;;; Internal variables
948
949 (defvar gnus-article-mime-handles nil)
950 (defvar gnus-article-decoded-p nil)
951 (defvar gnus-scores-exclude-files nil)
952 (defvar gnus-page-broken nil)
953 (defvar gnus-inhibit-mime-unbuttonizing nil)
954
955 (defvar gnus-original-article nil)
956 (defvar gnus-article-internal-prepare-hook nil)
957 (defvar gnus-newsgroup-process-stack nil)
958
959 (defvar gnus-thread-indent-array nil)
960 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
961 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
962   "Function called to sort the articles within a thread after it has been gathered together.")
963
964 (defvar gnus-summary-save-parts-type-history nil)
965 (defvar gnus-summary-save-parts-last-directory nil)
966
967 ;; Avoid highlighting in kill files.
968 (defvar gnus-summary-inhibit-highlight nil)
969 (defvar gnus-newsgroup-selected-overlay nil)
970 (defvar gnus-inhibit-limiting nil)
971 (defvar gnus-newsgroup-adaptive-score-file nil)
972 (defvar gnus-current-score-file nil)
973 (defvar gnus-current-move-group nil)
974 (defvar gnus-current-copy-group nil)
975 (defvar gnus-current-crosspost-group nil)
976
977 (defvar gnus-newsgroup-dependencies nil)
978 (defvar gnus-newsgroup-adaptive nil)
979 (defvar gnus-summary-display-article-function nil)
980 (defvar gnus-summary-highlight-line-function nil
981   "Function called after highlighting a summary line.")
982
983 (defvar gnus-summary-line-format-alist
984   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
985     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
986     (?s gnus-tmp-subject-or-nil ?s)
987     (?n gnus-tmp-name ?s)
988     (?A (std11-address-string
989          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
990     (?a (or (std11-full-name-string
991              (car (mime-entity-read-field gnus-tmp-header 'From)))
992             gnus-tmp-from) ?s)
993     (?F gnus-tmp-from ?s)
994     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
995     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
996     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
997     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
998     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
999     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1000     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1001     (?L gnus-tmp-lines ?d)
1002     (?I gnus-tmp-indentation ?s)
1003     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1004     (?R gnus-tmp-replied ?c)
1005     (?\[ gnus-tmp-opening-bracket ?c)
1006     (?\] gnus-tmp-closing-bracket ?c)
1007     (?\> (make-string gnus-tmp-level ? ) ?s)
1008     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1009     (?i gnus-tmp-score ?d)
1010     (?z gnus-tmp-score-char ?c)
1011     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1012     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1013     (?U gnus-tmp-unread ?c)
1014     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1015     (?t (gnus-summary-number-of-articles-in-thread
1016          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1017         ?d)
1018     (?e (gnus-summary-number-of-articles-in-thread
1019          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1020         ?c)
1021     (?u gnus-tmp-user-defined ?s)
1022     (?P (gnus-pick-line-number) ?d))
1023   "An alist of format specifications that can appear in summary lines.
1024 These are paired with what variables they correspond with, along with
1025 the type of the variable (string, integer, character, etc).")
1026
1027 (defvar gnus-summary-dummy-line-format-alist
1028   `((?S gnus-tmp-subject ?s)
1029     (?N gnus-tmp-number ?d)
1030     (?u gnus-tmp-user-defined ?s)))
1031
1032 (defvar gnus-summary-mode-line-format-alist
1033   `((?G gnus-tmp-group-name ?s)
1034     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1035     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1036     (?A gnus-tmp-article-number ?d)
1037     (?Z gnus-tmp-unread-and-unselected ?s)
1038     (?V gnus-version ?s)
1039     (?U gnus-tmp-unread-and-unticked ?d)
1040     (?S gnus-tmp-subject ?s)
1041     (?e gnus-tmp-unselected ?d)
1042     (?u gnus-tmp-user-defined ?s)
1043     (?d (length gnus-newsgroup-dormant) ?d)
1044     (?t (length gnus-newsgroup-marked) ?d)
1045     (?r (length gnus-newsgroup-reads) ?d)
1046     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1047     (?E gnus-newsgroup-expunged-tally ?d)
1048     (?s (gnus-current-score-file-nondirectory) ?s)))
1049
1050 (defvar gnus-last-search-regexp nil
1051   "Default regexp for article search command.")
1052
1053 (defvar gnus-summary-search-article-matched-data nil
1054   "Last matched data of article search command.  It is the local variable
1055 in `gnus-article-buffer' which consists of the list of start position,
1056 end position and text.")
1057
1058 (defvar gnus-last-shell-command nil
1059   "Default shell command on article.")
1060
1061 (defvar gnus-newsgroup-begin nil)
1062 (defvar gnus-newsgroup-end nil)
1063 (defvar gnus-newsgroup-last-rmail nil)
1064 (defvar gnus-newsgroup-last-mail nil)
1065 (defvar gnus-newsgroup-last-folder nil)
1066 (defvar gnus-newsgroup-last-file nil)
1067 (defvar gnus-newsgroup-auto-expire nil)
1068 (defvar gnus-newsgroup-active nil)
1069
1070 (defvar gnus-newsgroup-data nil)
1071 (defvar gnus-newsgroup-data-reverse nil)
1072 (defvar gnus-newsgroup-limit nil)
1073 (defvar gnus-newsgroup-limits nil)
1074
1075 (defvar gnus-newsgroup-unreads nil
1076   "List of unread articles in the current newsgroup.")
1077
1078 (defvar gnus-newsgroup-unselected nil
1079   "List of unselected unread articles in the current newsgroup.")
1080
1081 (defvar gnus-newsgroup-reads nil
1082   "Alist of read articles and article marks in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-expunged-tally nil)
1085
1086 (defvar gnus-newsgroup-marked nil
1087   "List of ticked articles in the current newsgroup (a subset of unread art).")
1088
1089 (defvar gnus-newsgroup-killed nil
1090   "List of ranges of articles that have been through the scoring process.")
1091
1092 (defvar gnus-newsgroup-cached nil
1093   "List of articles that come from the article cache.")
1094
1095 (defvar gnus-newsgroup-saved nil
1096   "List of articles that have been saved.")
1097
1098 (defvar gnus-newsgroup-kill-headers nil)
1099
1100 (defvar gnus-newsgroup-replied nil
1101   "List of articles that have been replied to in the current newsgroup.")
1102
1103 (defvar gnus-newsgroup-expirable nil
1104   "List of articles in the current newsgroup that can be expired.")
1105
1106 (defvar gnus-newsgroup-processable nil
1107   "List of articles in the current newsgroup that can be processed.")
1108
1109 (defvar gnus-newsgroup-downloadable nil
1110   "List of articles in the current newsgroup that can be processed.")
1111
1112 (defvar gnus-newsgroup-undownloaded nil
1113   "List of articles in the current newsgroup that haven't been downloaded..")
1114
1115 (defvar gnus-newsgroup-unsendable nil
1116   "List of articles in the current newsgroup that won't be sent.")
1117
1118 (defvar gnus-newsgroup-bookmarks nil
1119   "List of articles in the current newsgroup that have bookmarks.")
1120
1121 (defvar gnus-newsgroup-dormant nil
1122   "List of dormant articles in the current newsgroup.")
1123
1124 (defvar gnus-newsgroup-scored nil
1125   "List of scored articles in the current newsgroup.")
1126
1127 (defvar gnus-newsgroup-incorporated nil
1128   "List of incorporated articles in the current newsgroup.")
1129
1130 (defvar gnus-newsgroup-headers nil
1131   "List of article headers in the current newsgroup.")
1132
1133 (defvar gnus-newsgroup-threads nil)
1134
1135 (defvar gnus-newsgroup-prepared nil
1136   "Whether the current group has been prepared properly.")
1137
1138 (defvar gnus-newsgroup-ancient nil
1139   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1140
1141 (defvar gnus-newsgroup-sparse nil)
1142
1143 (defvar gnus-current-article nil)
1144 (defvar gnus-article-current nil)
1145 (defvar gnus-current-headers nil)
1146 (defvar gnus-have-all-headers nil)
1147 (defvar gnus-last-article nil)
1148 (defvar gnus-newsgroup-history nil)
1149 (defvar gnus-newsgroup-charset nil)
1150 (defvar gnus-newsgroup-ephemeral-charset nil)
1151 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1152
1153 (defconst gnus-summary-local-variables
1154   '(gnus-newsgroup-name
1155     gnus-newsgroup-begin gnus-newsgroup-end
1156     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1157     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1158     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1159     gnus-newsgroup-unselected gnus-newsgroup-marked
1160     gnus-newsgroup-reads gnus-newsgroup-saved
1161     gnus-newsgroup-replied gnus-newsgroup-expirable
1162     gnus-newsgroup-processable gnus-newsgroup-killed
1163     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1164     gnus-newsgroup-unsendable
1165     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1166     gnus-newsgroup-headers gnus-newsgroup-threads
1167     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1168     gnus-current-article gnus-current-headers gnus-have-all-headers
1169     gnus-last-article gnus-article-internal-prepare-hook
1170     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1171     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1172     gnus-thread-expunge-below
1173     gnus-score-alist gnus-current-score-file
1174     (gnus-summary-expunge-below . global)
1175     (gnus-summary-mark-below . global)
1176     (gnus-orphan-score . global)
1177     gnus-newsgroup-active gnus-scores-exclude-files
1178     gnus-newsgroup-history gnus-newsgroup-ancient
1179     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1180     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1181     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1182     (gnus-newsgroup-expunged-tally . 0)
1183     gnus-cache-removable-articles gnus-newsgroup-cached
1184     gnus-newsgroup-data gnus-newsgroup-data-reverse
1185     gnus-newsgroup-limit gnus-newsgroup-limits
1186     gnus-newsgroup-charset
1187     gnus-newsgroup-incorporated)
1188   "Variables that are buffer-local to the summary buffers.")
1189
1190 ;; Byte-compiler warning.
1191 (defvar gnus-article-mode-map)
1192
1193 ;; Subject simplification.
1194
1195 (defun gnus-simplify-whitespace (str)
1196   "Remove excessive whitespace from STR."
1197   (let ((mystr str))
1198     ;; Multiple spaces.
1199     (while (string-match "[ \t][ \t]+" mystr)
1200       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1201                           " "
1202                           (substring mystr (match-end 0)))))
1203     ;; Leading spaces.
1204     (when (string-match "^[ \t]+" mystr)
1205       (setq mystr (substring mystr (match-end 0))))
1206     ;; Trailing spaces.
1207     (when (string-match "[ \t]+$" mystr)
1208       (setq mystr (substring mystr 0 (match-beginning 0))))
1209     mystr))
1210
1211 (defsubst gnus-simplify-subject-re (subject)
1212   "Remove \"Re:\" from subject lines."
1213   (if (string-match "^[Rr][Ee]: *" subject)
1214       (substring subject (match-end 0))
1215     subject))
1216
1217 (defun gnus-simplify-subject (subject &optional re-only)
1218   "Remove `Re:' and words in parentheses.
1219 If RE-ONLY is non-nil, strip leading `Re:'s only."
1220   (let ((case-fold-search t))           ;Ignore case.
1221     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1222     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1223       (setq subject (substring subject (match-end 0))))
1224     ;; Remove uninteresting prefixes.
1225     (when (and (not re-only)
1226                gnus-simplify-ignored-prefixes
1227                (string-match gnus-simplify-ignored-prefixes subject))
1228       (setq subject (substring subject (match-end 0))))
1229     ;; Remove words in parentheses from end.
1230     (unless re-only
1231       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1232         (setq subject (substring subject 0 (match-beginning 0)))))
1233     ;; Return subject string.
1234     subject))
1235
1236 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1237 ;; all whitespace.
1238 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1239   (goto-char (point-min))
1240   (while (re-search-forward regexp nil t)
1241     (replace-match (or newtext ""))))
1242
1243 (defun gnus-simplify-buffer-fuzzy ()
1244   "Simplify string in the buffer fuzzily.
1245 The string in the accessible portion of the current buffer is simplified.
1246 It is assumed to be a single-line subject.
1247 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1248 matter is removed.  Additional things can be deleted by setting
1249 `gnus-simplify-subject-fuzzy-regexp'."
1250   (let ((case-fold-search t)
1251         (modified-tick))
1252     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1253
1254     (while (not (eq modified-tick (buffer-modified-tick)))
1255       (setq modified-tick (buffer-modified-tick))
1256       (cond
1257        ((listp gnus-simplify-subject-fuzzy-regexp)
1258         (mapcar 'gnus-simplify-buffer-fuzzy-step
1259                 gnus-simplify-subject-fuzzy-regexp))
1260        (gnus-simplify-subject-fuzzy-regexp
1261         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1262       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1263       (gnus-simplify-buffer-fuzzy-step
1264        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1265       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1266
1267     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1268     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1269     (gnus-simplify-buffer-fuzzy-step " $")
1270     (gnus-simplify-buffer-fuzzy-step "^ +")))
1271
1272 (defun gnus-simplify-subject-fuzzy (subject)
1273   "Simplify a subject string fuzzily.
1274 See `gnus-simplify-buffer-fuzzy' for details."
1275   (save-excursion
1276     (gnus-set-work-buffer)
1277     (let ((case-fold-search t))
1278       ;; Remove uninteresting prefixes.
1279       (when (and gnus-simplify-ignored-prefixes
1280                  (string-match gnus-simplify-ignored-prefixes subject))
1281         (setq subject (substring subject (match-end 0))))
1282       (insert subject)
1283       (inline (gnus-simplify-buffer-fuzzy))
1284       (buffer-string))))
1285
1286 (defsubst gnus-simplify-subject-fully (subject)
1287   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1288   (cond
1289    (gnus-simplify-subject-functions
1290     (gnus-map-function gnus-simplify-subject-functions subject))
1291    ((null gnus-summary-gather-subject-limit)
1292     (gnus-simplify-subject-re subject))
1293    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1294     (gnus-simplify-subject-fuzzy subject))
1295    ((numberp gnus-summary-gather-subject-limit)
1296     (gnus-limit-string (gnus-simplify-subject-re subject)
1297                        gnus-summary-gather-subject-limit))
1298    (t
1299     subject)))
1300
1301 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1302   "Check whether two subjects are equal.
1303 If optional argument simple-first is t, first argument is already
1304 simplified."
1305   (cond
1306    ((null simple-first)
1307     (equal (gnus-simplify-subject-fully s1)
1308            (gnus-simplify-subject-fully s2)))
1309    (t
1310     (equal s1
1311            (gnus-simplify-subject-fully s2)))))
1312
1313 (defun gnus-summary-bubble-group ()
1314   "Increase the score of the current group.
1315 This is a handy function to add to `gnus-summary-exit-hook' to
1316 increase the score of each group you read."
1317   (gnus-group-add-score gnus-newsgroup-name))
1318
1319 \f
1320 ;;;
1321 ;;; Gnus summary mode
1322 ;;;
1323
1324 (put 'gnus-summary-mode 'mode-class 'special)
1325
1326 (when t
1327   ;; Non-orthogonal keys
1328
1329   (gnus-define-keys gnus-summary-mode-map
1330     " " gnus-summary-next-page
1331     "\177" gnus-summary-prev-page
1332     [delete] gnus-summary-prev-page
1333     [backspace] gnus-summary-prev-page
1334     "\r" gnus-summary-scroll-up
1335     "\M-\r" gnus-summary-scroll-down
1336     "n" gnus-summary-next-unread-article
1337     "p" gnus-summary-prev-unread-article
1338     "N" gnus-summary-next-article
1339     "P" gnus-summary-prev-article
1340     "\M-\C-n" gnus-summary-next-same-subject
1341     "\M-\C-p" gnus-summary-prev-same-subject
1342     "\M-n" gnus-summary-next-unread-subject
1343     "\M-p" gnus-summary-prev-unread-subject
1344     "." gnus-summary-first-unread-article
1345     "," gnus-summary-best-unread-article
1346     "\M-s" gnus-summary-search-article-forward
1347     "\M-r" gnus-summary-search-article-backward
1348     "<" gnus-summary-beginning-of-article
1349     ">" gnus-summary-end-of-article
1350     "j" gnus-summary-goto-article
1351     "^" gnus-summary-refer-parent-article
1352     "\M-^" gnus-summary-refer-article
1353     "u" gnus-summary-tick-article-forward
1354     "!" gnus-summary-tick-article-forward
1355     "U" gnus-summary-tick-article-backward
1356     "d" gnus-summary-mark-as-read-forward
1357     "D" gnus-summary-mark-as-read-backward
1358     "E" gnus-summary-mark-as-expirable
1359     "\M-u" gnus-summary-clear-mark-forward
1360     "\M-U" gnus-summary-clear-mark-backward
1361     "k" gnus-summary-kill-same-subject-and-select
1362     "\C-k" gnus-summary-kill-same-subject
1363     "\M-\C-k" gnus-summary-kill-thread
1364     "\M-\C-l" gnus-summary-lower-thread
1365     "e" gnus-summary-edit-article
1366     "#" gnus-summary-mark-as-processable
1367     "\M-#" gnus-summary-unmark-as-processable
1368     "\M-\C-t" gnus-summary-toggle-threads
1369     "\M-\C-s" gnus-summary-show-thread
1370     "\M-\C-h" gnus-summary-hide-thread
1371     "\M-\C-f" gnus-summary-next-thread
1372     "\M-\C-b" gnus-summary-prev-thread
1373     [(meta down)] gnus-summary-next-thread
1374     [(meta up)] gnus-summary-prev-thread
1375     "\M-\C-u" gnus-summary-up-thread
1376     "\M-\C-d" gnus-summary-down-thread
1377     "&" gnus-summary-execute-command
1378     "c" gnus-summary-catchup-and-exit
1379     "\C-w" gnus-summary-mark-region-as-read
1380     "\C-t" gnus-summary-toggle-truncation
1381     "?" gnus-summary-mark-as-dormant
1382     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1383     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1384     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1385     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1386     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1387     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1388     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1389     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1390     "=" gnus-summary-expand-window
1391     "\C-x\C-s" gnus-summary-reselect-current-group
1392     "\M-g" gnus-summary-rescan-group
1393     "w" gnus-summary-stop-page-breaking
1394     "\C-c\C-r" gnus-summary-caesar-message
1395     "\M-t" gnus-summary-toggle-mime
1396     "f" gnus-summary-followup
1397     "F" gnus-summary-followup-with-original
1398     "C" gnus-summary-cancel-article
1399     "r" gnus-summary-reply
1400     "R" gnus-summary-reply-with-original
1401     "\C-c\C-f" gnus-summary-mail-forward
1402     "o" gnus-summary-save-article
1403     "\C-o" gnus-summary-save-article-mail
1404     "|" gnus-summary-pipe-output
1405     "\M-k" gnus-summary-edit-local-kill
1406     "\M-K" gnus-summary-edit-global-kill
1407     ;; "V" gnus-version
1408     "\C-c\C-d" gnus-summary-describe-group
1409     "q" gnus-summary-exit
1410     "Q" gnus-summary-exit-no-update
1411     "\C-c\C-i" gnus-info-find-node
1412     gnus-mouse-2 gnus-mouse-pick-article
1413     "m" gnus-summary-mail-other-window
1414     "a" gnus-summary-post-news
1415     "x" gnus-summary-limit-to-unread
1416     "s" gnus-summary-isearch-article
1417     "t" gnus-article-toggle-headers
1418     "g" gnus-summary-show-article
1419     "l" gnus-summary-goto-last-article
1420     "v" gnus-summary-preview-mime-message
1421     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1422     "\C-d" gnus-summary-enter-digest-group
1423     "\M-\C-d" gnus-summary-read-document
1424     "\M-\C-e" gnus-summary-edit-parameters
1425     "\M-\C-a" gnus-summary-customize-parameters
1426     "\C-c\C-b" gnus-bug
1427     "*" gnus-cache-enter-article
1428     "\M-*" gnus-cache-remove-article
1429     "\M-&" gnus-summary-universal-argument
1430     "\C-l" gnus-recenter
1431     "I" gnus-summary-increase-score
1432     "L" gnus-summary-lower-score
1433     "\M-i" gnus-symbolic-argument
1434     "h" gnus-summary-select-article-buffer
1435
1436     "V" gnus-summary-score-map
1437     "X" gnus-uu-extract-map
1438     "S" gnus-summary-send-map)
1439
1440   ;; Sort of orthogonal keymap
1441   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1442     "t" gnus-summary-tick-article-forward
1443     "!" gnus-summary-tick-article-forward
1444     "d" gnus-summary-mark-as-read-forward
1445     "r" gnus-summary-mark-as-read-forward
1446     "c" gnus-summary-clear-mark-forward
1447     " " gnus-summary-clear-mark-forward
1448     "e" gnus-summary-mark-as-expirable
1449     "x" gnus-summary-mark-as-expirable
1450     "?" gnus-summary-mark-as-dormant
1451     "b" gnus-summary-set-bookmark
1452     "B" gnus-summary-remove-bookmark
1453     "#" gnus-summary-mark-as-processable
1454     "\M-#" gnus-summary-unmark-as-processable
1455     "S" gnus-summary-limit-include-expunged
1456     "C" gnus-summary-catchup
1457     "H" gnus-summary-catchup-to-here
1458     "\C-c" gnus-summary-catchup-all
1459     "k" gnus-summary-kill-same-subject-and-select
1460     "K" gnus-summary-kill-same-subject
1461     "P" gnus-uu-mark-map)
1462
1463   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1464     "c" gnus-summary-clear-above
1465     "u" gnus-summary-tick-above
1466     "m" gnus-summary-mark-above
1467     "k" gnus-summary-kill-below)
1468
1469   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1470     "/" gnus-summary-limit-to-subject
1471     "n" gnus-summary-limit-to-articles
1472     "w" gnus-summary-pop-limit
1473     "s" gnus-summary-limit-to-subject
1474     "a" gnus-summary-limit-to-author
1475     "u" gnus-summary-limit-to-unread
1476     "m" gnus-summary-limit-to-marks
1477     "M" gnus-summary-limit-exclude-marks
1478     "v" gnus-summary-limit-to-score
1479     "*" gnus-summary-limit-include-cached
1480     "D" gnus-summary-limit-include-dormant
1481     "T" gnus-summary-limit-include-thread
1482     "d" gnus-summary-limit-exclude-dormant
1483     "t" gnus-summary-limit-to-age
1484     "x" gnus-summary-limit-to-extra
1485     "E" gnus-summary-limit-include-expunged
1486     "c" gnus-summary-limit-exclude-childless-dormant
1487     "C" gnus-summary-limit-mark-excluded-as-read)
1488
1489   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1490     "n" gnus-summary-next-unread-article
1491     "p" gnus-summary-prev-unread-article
1492     "N" gnus-summary-next-article
1493     "P" gnus-summary-prev-article
1494     "\C-n" gnus-summary-next-same-subject
1495     "\C-p" gnus-summary-prev-same-subject
1496     "\M-n" gnus-summary-next-unread-subject
1497     "\M-p" gnus-summary-prev-unread-subject
1498     "f" gnus-summary-first-unread-article
1499     "b" gnus-summary-best-unread-article
1500     "j" gnus-summary-goto-article
1501     "g" gnus-summary-goto-subject
1502     "l" gnus-summary-goto-last-article
1503     "o" gnus-summary-pop-article)
1504
1505   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1506     "k" gnus-summary-kill-thread
1507     "l" gnus-summary-lower-thread
1508     "i" gnus-summary-raise-thread
1509     "T" gnus-summary-toggle-threads
1510     "t" gnus-summary-rethread-current
1511     "^" gnus-summary-reparent-thread
1512     "s" gnus-summary-show-thread
1513     "S" gnus-summary-show-all-threads
1514     "h" gnus-summary-hide-thread
1515     "H" gnus-summary-hide-all-threads
1516     "n" gnus-summary-next-thread
1517     "p" gnus-summary-prev-thread
1518     "u" gnus-summary-up-thread
1519     "o" gnus-summary-top-thread
1520     "d" gnus-summary-down-thread
1521     "#" gnus-uu-mark-thread
1522     "\M-#" gnus-uu-unmark-thread)
1523
1524   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1525     "g" gnus-summary-prepare
1526     "c" gnus-summary-insert-cached-articles)
1527
1528   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1529     "c" gnus-summary-catchup-and-exit
1530     "C" gnus-summary-catchup-all-and-exit
1531     "E" gnus-summary-exit-no-update
1532     "J" gnus-summary-jump-to-other-group
1533     "Q" gnus-summary-exit
1534     "Z" gnus-summary-exit
1535     "n" gnus-summary-catchup-and-goto-next-group
1536     "R" gnus-summary-reselect-current-group
1537     "G" gnus-summary-rescan-group
1538     "N" gnus-summary-next-group
1539     "s" gnus-summary-save-newsrc
1540     "P" gnus-summary-prev-group)
1541
1542   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1543     " " gnus-summary-next-page
1544     "n" gnus-summary-next-page
1545     "\177" gnus-summary-prev-page
1546     [delete] gnus-summary-prev-page
1547     "p" gnus-summary-prev-page
1548     "\r" gnus-summary-scroll-up
1549     "\M-\r" gnus-summary-scroll-down
1550     "<" gnus-summary-beginning-of-article
1551     ">" gnus-summary-end-of-article
1552     "b" gnus-summary-beginning-of-article
1553     "e" gnus-summary-end-of-article
1554     "^" gnus-summary-refer-parent-article
1555     "r" gnus-summary-refer-parent-article
1556     "D" gnus-summary-enter-digest-group
1557     "R" gnus-summary-refer-references
1558     "T" gnus-summary-refer-thread
1559     "g" gnus-summary-show-article
1560     "s" gnus-summary-isearch-article
1561     "P" gnus-summary-print-article
1562     "t" gnus-article-babel
1563     "d" gnus-summary-decrypt-article
1564     "v" gnus-summary-verify-article)
1565
1566   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1567     "b" gnus-article-add-buttons
1568     "B" gnus-article-add-buttons-to-head
1569     "o" gnus-article-treat-overstrike
1570     "e" gnus-article-emphasize
1571     "w" gnus-article-fill-cited-article
1572     "Q" gnus-article-fill-long-lines
1573     "C" gnus-article-capitalize-sentences
1574     "c" gnus-article-remove-cr
1575     "Z" gnus-article-decode-HZ
1576     "f" gnus-article-display-x-face
1577     "l" gnus-summary-stop-page-breaking
1578     "r" gnus-summary-caesar-message
1579     "t" gnus-article-toggle-headers
1580     "v" gnus-summary-verbose-headers
1581     "m" gnus-summary-toggle-mime
1582     "H" gnus-article-strip-headers-in-body
1583     "d" gnus-article-treat-dumbquotes
1584     "s" gnus-smiley-display)
1585
1586   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1587     "a" gnus-article-hide
1588     "h" gnus-article-toggle-headers
1589     "b" gnus-article-hide-boring-headers
1590     "s" gnus-article-hide-signature
1591     "c" gnus-article-hide-citation
1592     "C" gnus-article-hide-citation-in-followups
1593     "l" gnus-article-hide-list-identifiers
1594     "p" gnus-article-hide-pgp
1595     "B" gnus-article-strip-banner
1596     "P" gnus-article-hide-pem
1597     "\C-c" gnus-article-hide-citation-maybe)
1598
1599   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1600     "a" gnus-article-highlight
1601     "h" gnus-article-highlight-headers
1602     "c" gnus-article-highlight-citation
1603     "s" gnus-article-highlight-signature)
1604
1605   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1606     "z" gnus-article-date-ut
1607     "u" gnus-article-date-ut
1608     "l" gnus-article-date-local
1609     "e" gnus-article-date-lapsed
1610     "o" gnus-article-date-original
1611     "i" gnus-article-date-iso8601
1612     "s" gnus-article-date-user)
1613
1614   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1615     "t" gnus-article-remove-trailing-blank-lines
1616     "l" gnus-article-strip-leading-blank-lines
1617     "m" gnus-article-strip-multiple-blank-lines
1618     "a" gnus-article-strip-blank-lines
1619     "A" gnus-article-strip-all-blank-lines
1620     "s" gnus-article-strip-leading-space
1621     "e" gnus-article-strip-trailing-space)
1622
1623   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1624     "v" gnus-version
1625     "f" gnus-summary-fetch-faq
1626     "d" gnus-summary-describe-group
1627     "h" gnus-summary-describe-briefly
1628     "i" gnus-info-find-node)
1629
1630   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1631     "e" gnus-summary-expire-articles
1632     "\M-\C-e" gnus-summary-expire-articles-now
1633     "\177" gnus-summary-delete-article
1634     [delete] gnus-summary-delete-article
1635     [backspace] gnus-summary-delete-article
1636     "m" gnus-summary-move-article
1637     "r" gnus-summary-respool-article
1638     "w" gnus-summary-edit-article
1639     "c" gnus-summary-copy-article
1640     "B" gnus-summary-crosspost-article
1641     "q" gnus-summary-respool-query
1642     "t" gnus-summary-respool-trace
1643     "i" gnus-summary-import-article
1644     "p" gnus-summary-article-posted-p)
1645
1646   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1647     "o" gnus-summary-save-article
1648     "m" gnus-summary-save-article-mail
1649     "F" gnus-summary-write-article-file
1650     "r" gnus-summary-save-article-rmail
1651     "f" gnus-summary-save-article-file
1652     "b" gnus-summary-save-article-body-file
1653     "h" gnus-summary-save-article-folder
1654     "v" gnus-summary-save-article-vm
1655     "p" gnus-summary-pipe-output
1656     "s" gnus-soup-add-article)
1657
1658   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1659     "b" gnus-summary-display-buttonized
1660     "m" gnus-summary-repair-multipart
1661     "v" gnus-article-view-part
1662     "o" gnus-article-save-part
1663     "c" gnus-article-copy-part
1664     "e" gnus-article-externalize-part
1665     "i" gnus-article-inline-part
1666     "|" gnus-article-pipe-part))
1667
1668 (defun gnus-summary-make-menu-bar ()
1669   (gnus-turn-off-edit-menu 'summary)
1670
1671   (unless (boundp 'gnus-summary-misc-menu)
1672
1673     (easy-menu-define
1674      gnus-summary-kill-menu gnus-summary-mode-map ""
1675      (cons
1676       "Score"
1677       (nconc
1678        (list
1679         ["Enter score..." gnus-summary-score-entry t]
1680         ["Customize" gnus-score-customize t])
1681        (gnus-make-score-map 'increase)
1682        (gnus-make-score-map 'lower)
1683        '(("Mark"
1684           ["Kill below" gnus-summary-kill-below t]
1685           ["Mark above" gnus-summary-mark-above t]
1686           ["Tick above" gnus-summary-tick-above t]
1687           ["Clear above" gnus-summary-clear-above t])
1688          ["Current score" gnus-summary-current-score t]
1689          ["Set score" gnus-summary-set-score t]
1690          ["Switch current score file..." gnus-score-change-score-file t]
1691          ["Set mark below..." gnus-score-set-mark-below t]
1692          ["Set expunge below..." gnus-score-set-expunge-below t]
1693          ["Edit current score file" gnus-score-edit-current-scores t]
1694          ["Edit score file" gnus-score-edit-file t]
1695          ["Trace score" gnus-score-find-trace t]
1696          ["Find words" gnus-score-find-favourite-words t]
1697          ["Rescore buffer" gnus-summary-rescore t]
1698          ["Increase score..." gnus-summary-increase-score t]
1699          ["Lower score..." gnus-summary-lower-score t]))))
1700
1701     ;; Define both the Article menu in the summary buffer and the equivalent
1702     ;; Commands menu in the article buffer here for consistency.
1703     (let ((innards
1704            '(("Hide"
1705               ["All" gnus-article-hide t]
1706               ["Headers" gnus-article-toggle-headers t]
1707               ["Signature" gnus-article-hide-signature t]
1708               ["Citation" gnus-article-hide-citation t]
1709               ["List identifiers" gnus-article-hide-list-identifiers t]
1710               ["PGP" gnus-article-hide-pgp t]
1711               ["Banner" gnus-article-strip-banner t]
1712               ["Boring headers" gnus-article-hide-boring-headers t])
1713              ("Highlight"
1714               ["All" gnus-article-highlight t]
1715               ["Headers" gnus-article-highlight-headers t]
1716               ["Signature" gnus-article-highlight-signature t]
1717               ["Citation" gnus-article-highlight-citation t])
1718              ("Date"
1719               ["Local" gnus-article-date-local t]
1720               ["ISO8601" gnus-article-date-iso8601 t]
1721               ["UT" gnus-article-date-ut t]
1722               ["Original" gnus-article-date-original t]
1723               ["Lapsed" gnus-article-date-lapsed t]
1724               ["User-defined" gnus-article-date-user t])
1725              ("Washing"
1726               ("Remove Blanks"
1727                ["Leading" gnus-article-strip-leading-blank-lines t]
1728                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1729                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1730                ["All of the above" gnus-article-strip-blank-lines t]
1731                ["All" gnus-article-strip-all-blank-lines t]
1732                ["Leading space" gnus-article-strip-leading-space t]
1733                ["Trailing space" gnus-article-strip-trailing-space t])
1734               ["Overstrike" gnus-article-treat-overstrike t]
1735               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1736               ["Emphasis" gnus-article-emphasize t]
1737               ["Word wrap" gnus-article-fill-cited-article t]
1738               ["Fill long lines" gnus-article-fill-long-lines t]
1739               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1740               ["CR" gnus-article-remove-cr t]
1741               ["Show X-Face" gnus-article-display-x-face t]
1742               ["Rot 13" gnus-summary-caesar-message t]
1743               ["Unix pipe" gnus-summary-pipe-message t]
1744               ["Add buttons" gnus-article-add-buttons t]
1745               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1746               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1747               ["Toggle MIME" gnus-summary-toggle-mime t]
1748               ["Verbose header" gnus-summary-verbose-headers t]
1749               ["Toggle header" gnus-summary-toggle-header t]
1750               ["Toggle smileys" gnus-smiley-display t]
1751               ["HZ" gnus-article-decode-HZ t])
1752              ("Output"
1753               ["Save in default format" gnus-summary-save-article t]
1754               ["Save in file" gnus-summary-save-article-file t]
1755               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1756               ["Save in MH folder" gnus-summary-save-article-folder t]
1757               ["Save in VM folder" gnus-summary-save-article-vm t]
1758               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1759               ["Save body in file" gnus-summary-save-article-body-file t]
1760               ["Pipe through a filter" gnus-summary-pipe-output t]
1761               ["Add to SOUP packet" gnus-soup-add-article t]
1762               ["Print" gnus-summary-print-article t])
1763              ("Backend"
1764               ["Respool article..." gnus-summary-respool-article t]
1765               ["Move article..." gnus-summary-move-article
1766                (gnus-check-backend-function
1767                 'request-move-article gnus-newsgroup-name)]
1768               ["Copy article..." gnus-summary-copy-article t]
1769               ["Crosspost article..." gnus-summary-crosspost-article
1770                (gnus-check-backend-function
1771                 'request-replace-article gnus-newsgroup-name)]
1772               ["Import file..." gnus-summary-import-article t]
1773               ["Check if posted" gnus-summary-article-posted-p t]
1774               ["Edit article" gnus-summary-edit-article
1775                (not (gnus-group-read-only-p))]
1776               ["Delete article" gnus-summary-delete-article
1777                (gnus-check-backend-function
1778                 'request-expire-articles gnus-newsgroup-name)]
1779               ["Query respool" gnus-summary-respool-query t]
1780               ["Trace respool" gnus-summary-respool-trace t]
1781               ["Delete expirable articles" gnus-summary-expire-articles-now
1782                (gnus-check-backend-function
1783                 'request-expire-articles gnus-newsgroup-name)])
1784              ("Extract"
1785               ["Uudecode" gnus-uu-decode-uu t]
1786               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1787               ["Unshar" gnus-uu-decode-unshar t]
1788               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1789               ["Save" gnus-uu-decode-save t]
1790               ["Binhex" gnus-uu-decode-binhex t]
1791               ["Postscript" gnus-uu-decode-postscript t])
1792              ("Cache"
1793               ["Enter article" gnus-cache-enter-article t]
1794               ["Remove article" gnus-cache-remove-article t])
1795              ["Translate" gnus-article-babel t]
1796              ["Select article buffer" gnus-summary-select-article-buffer t]
1797              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1798              ["Isearch article..." gnus-summary-isearch-article t]
1799              ["Beginning of the article" gnus-summary-beginning-of-article t]
1800              ["End of the article" gnus-summary-end-of-article t]
1801              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1802              ["Fetch referenced articles" gnus-summary-refer-references t]
1803              ["Fetch current thread" gnus-summary-refer-thread t]
1804              ["Fetch article with id..." gnus-summary-refer-article t]
1805              ["Redisplay" gnus-summary-show-article t])))
1806       (easy-menu-define
1807        gnus-summary-article-menu gnus-summary-mode-map ""
1808        (cons "Article" innards))
1809
1810       (easy-menu-define
1811        gnus-article-commands-menu gnus-article-mode-map ""
1812        (cons "Commands" innards)))
1813
1814     (easy-menu-define
1815      gnus-summary-thread-menu gnus-summary-mode-map ""
1816      '("Threads"
1817        ["Toggle threading" gnus-summary-toggle-threads t]
1818        ["Hide threads" gnus-summary-hide-all-threads t]
1819        ["Show threads" gnus-summary-show-all-threads t]
1820        ["Hide thread" gnus-summary-hide-thread t]
1821        ["Show thread" gnus-summary-show-thread t]
1822        ["Go to next thread" gnus-summary-next-thread t]
1823        ["Go to previous thread" gnus-summary-prev-thread t]
1824        ["Go down thread" gnus-summary-down-thread t]
1825        ["Go up thread" gnus-summary-up-thread t]
1826        ["Top of thread" gnus-summary-top-thread t]
1827        ["Mark thread as read" gnus-summary-kill-thread t]
1828        ["Lower thread score" gnus-summary-lower-thread t]
1829        ["Raise thread score" gnus-summary-raise-thread t]
1830        ["Rethread current" gnus-summary-rethread-current t]))
1831
1832     (easy-menu-define
1833      gnus-summary-post-menu gnus-summary-mode-map ""
1834      '("Post"
1835        ["Post an article" gnus-summary-post-news t]
1836        ["Followup" gnus-summary-followup t]
1837        ["Followup and yank" gnus-summary-followup-with-original t]
1838        ["Supersede article" gnus-summary-supersede-article t]
1839        ["Cancel article" gnus-summary-cancel-article t]
1840        ["Reply" gnus-summary-reply t]
1841        ["Reply and yank" gnus-summary-reply-with-original t]
1842        ["Wide reply" gnus-summary-wide-reply t]
1843        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1844        ["Mail forward" gnus-summary-mail-forward t]
1845        ["Post forward" gnus-summary-post-forward t]
1846        ["Digest and mail" gnus-uu-digest-mail-forward t]
1847        ["Digest and post" gnus-uu-digest-post-forward t]
1848        ["Resend message" gnus-summary-resend-message t]
1849        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1850        ["Send a mail" gnus-summary-mail-other-window t]
1851        ["Uuencode and post" gnus-uu-post-news t]
1852        ["Followup via news" gnus-summary-followup-to-mail t]
1853        ["Followup via news and yank"
1854         gnus-summary-followup-to-mail-with-original t]
1855        ;;("Draft"
1856        ;;["Send" gnus-summary-send-draft t]
1857        ;;["Send bounced" gnus-resend-bounced-mail t])
1858        ))
1859
1860     (easy-menu-define
1861      gnus-summary-misc-menu gnus-summary-mode-map ""
1862      '("Misc"
1863        ("Mark Read"
1864         ["Mark as read" gnus-summary-mark-as-read-forward t]
1865         ["Mark same subject and select"
1866          gnus-summary-kill-same-subject-and-select t]
1867         ["Mark same subject" gnus-summary-kill-same-subject t]
1868         ["Catchup" gnus-summary-catchup t]
1869         ["Catchup all" gnus-summary-catchup-all t]
1870         ["Catchup to here" gnus-summary-catchup-to-here t]
1871         ["Catchup region" gnus-summary-mark-region-as-read t]
1872         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1873        ("Mark Various"
1874         ["Tick" gnus-summary-tick-article-forward t]
1875         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1876         ["Remove marks" gnus-summary-clear-mark-forward t]
1877         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1878         ["Set bookmark" gnus-summary-set-bookmark t]
1879         ["Remove bookmark" gnus-summary-remove-bookmark t])
1880        ("Mark Limit"
1881         ["Marks..." gnus-summary-limit-to-marks t]
1882         ["Subject..." gnus-summary-limit-to-subject t]
1883         ["Author..." gnus-summary-limit-to-author t]
1884         ["Age..." gnus-summary-limit-to-age t]
1885         ["Extra..." gnus-summary-limit-to-extra t]
1886         ["Score" gnus-summary-limit-to-score t]
1887         ["Unread" gnus-summary-limit-to-unread t]
1888         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1889         ["Articles" gnus-summary-limit-to-articles t]
1890         ["Pop limit" gnus-summary-pop-limit t]
1891         ["Show dormant" gnus-summary-limit-include-dormant t]
1892         ["Hide childless dormant"
1893          gnus-summary-limit-exclude-childless-dormant t]
1894         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1895         ["Hide marked" gnus-summary-limit-exclude-marks t]
1896         ["Show expunged" gnus-summary-show-all-expunged t])
1897        ("Process Mark"
1898         ["Set mark" gnus-summary-mark-as-processable t]
1899         ["Remove mark" gnus-summary-unmark-as-processable t]
1900         ["Remove all marks" gnus-summary-unmark-all-processable t]
1901         ["Mark above" gnus-uu-mark-over t]
1902         ["Mark series" gnus-uu-mark-series t]
1903         ["Mark region" gnus-uu-mark-region t]
1904         ["Unmark region" gnus-uu-unmark-region t]
1905         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1906         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1907         ["Mark all" gnus-uu-mark-all t]
1908         ["Mark buffer" gnus-uu-mark-buffer t]
1909         ["Mark sparse" gnus-uu-mark-sparse t]
1910         ["Mark thread" gnus-uu-mark-thread t]
1911         ["Unmark thread" gnus-uu-unmark-thread t]
1912         ("Process Mark Sets"
1913          ["Kill" gnus-summary-kill-process-mark t]
1914          ["Yank" gnus-summary-yank-process-mark
1915           gnus-newsgroup-process-stack]
1916          ["Save" gnus-summary-save-process-mark t]))
1917        ("Scroll article"
1918         ["Page forward" gnus-summary-next-page t]
1919         ["Page backward" gnus-summary-prev-page t]
1920         ["Line forward" gnus-summary-scroll-up t])
1921        ("Move"
1922         ["Next unread article" gnus-summary-next-unread-article t]
1923         ["Previous unread article" gnus-summary-prev-unread-article t]
1924         ["Next article" gnus-summary-next-article t]
1925         ["Previous article" gnus-summary-prev-article t]
1926         ["Next unread subject" gnus-summary-next-unread-subject t]
1927         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1928         ["Next article same subject" gnus-summary-next-same-subject t]
1929         ["Previous article same subject" gnus-summary-prev-same-subject t]
1930         ["First unread article" gnus-summary-first-unread-article t]
1931         ["Best unread article" gnus-summary-best-unread-article t]
1932         ["Go to subject number..." gnus-summary-goto-subject t]
1933         ["Go to article number..." gnus-summary-goto-article t]
1934         ["Go to the last article" gnus-summary-goto-last-article t]
1935         ["Pop article off history" gnus-summary-pop-article t])
1936        ("Sort"
1937         ["Sort by number" gnus-summary-sort-by-number t]
1938         ["Sort by author" gnus-summary-sort-by-author t]
1939         ["Sort by subject" gnus-summary-sort-by-subject t]
1940         ["Sort by date" gnus-summary-sort-by-date t]
1941         ["Sort by score" gnus-summary-sort-by-score t]
1942         ["Sort by lines" gnus-summary-sort-by-lines t]
1943         ["Sort by characters" gnus-summary-sort-by-chars t])
1944        ("Help"
1945         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1946         ["Describe group" gnus-summary-describe-group t]
1947         ["Read manual" gnus-info-find-node t])
1948        ("Modes"
1949         ["Pick and read" gnus-pick-mode t]
1950         ["Binary" gnus-binary-mode t])
1951        ("Regeneration"
1952         ["Regenerate" gnus-summary-prepare t]
1953         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1954         ["Toggle threading" gnus-summary-toggle-threads t])
1955        ["Filter articles..." gnus-summary-execute-command t]
1956        ["Run command on subjects..." gnus-summary-universal-argument t]
1957        ["Search articles forward..." gnus-summary-search-article-forward t]
1958        ["Search articles backward..." gnus-summary-search-article-backward t]
1959        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1960        ["Expand window" gnus-summary-expand-window t]
1961        ["Expire expirable articles" gnus-summary-expire-articles
1962         (gnus-check-backend-function
1963          'request-expire-articles gnus-newsgroup-name)]
1964        ["Edit local kill file" gnus-summary-edit-local-kill t]
1965        ["Edit main kill file" gnus-summary-edit-global-kill t]
1966        ["Edit group parameters" gnus-summary-edit-parameters t]
1967        ["Customize group parameters" gnus-summary-customize-parameters t]
1968        ["Send a bug report" gnus-bug t]
1969        ("Exit"
1970         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1971         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1972         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1973         ["Exit group" gnus-summary-exit t]
1974         ["Exit group without updating" gnus-summary-exit-no-update t]
1975         ["Exit and goto next group" gnus-summary-next-group t]
1976         ["Exit and goto prev group" gnus-summary-prev-group t]
1977         ["Reselect group" gnus-summary-reselect-current-group t]
1978         ["Rescan group" gnus-summary-rescan-group t]
1979         ["Update dribble" gnus-summary-save-newsrc t])))
1980
1981     (gnus-run-hooks 'gnus-summary-menu-hook)))
1982
1983 (defun gnus-score-set-default (var value)
1984   "A version of set that updates the GNU Emacs menu-bar."
1985   (set var value)
1986   ;; It is the message that forces the active status to be updated.
1987   (message ""))
1988
1989 (defun gnus-make-score-map (type)
1990   "Make a summary score map of type TYPE."
1991   (if t
1992       nil
1993     (let ((headers '(("author" "from" string)
1994                      ("subject" "subject" string)
1995                      ("article body" "body" string)
1996                      ("article head" "head" string)
1997                      ("xref" "xref" string)
1998                      ("extra header" "extra" string)
1999                      ("lines" "lines" number)
2000                      ("followups to author" "followup" string)))
2001           (types '((number ("less than" <)
2002                            ("greater than" >)
2003                            ("equal" =))
2004                    (string ("substring" s)
2005                            ("exact string" e)
2006                            ("fuzzy string" f)
2007                            ("regexp" r))))
2008           (perms '(("temporary" (current-time-string))
2009                    ("permanent" nil)
2010                    ("immediate" now)))
2011           header)
2012       (list
2013        (apply
2014         'nconc
2015         (list
2016          (if (eq type 'lower)
2017              "Lower score"
2018            "Increase score"))
2019         (let (outh)
2020           (while headers
2021             (setq header (car headers))
2022             (setq outh
2023                   (cons
2024                    (apply
2025                     'nconc
2026                     (list (car header))
2027                     (let ((ts (cdr (assoc (nth 2 header) types)))
2028                           outt)
2029                       (while ts
2030                         (setq outt
2031                               (cons
2032                                (apply
2033                                 'nconc
2034                                 (list (caar ts))
2035                                 (let ((ps perms)
2036                                       outp)
2037                                   (while ps
2038                                     (setq outp
2039                                           (cons
2040                                            (vector
2041                                             (caar ps)
2042                                             (list
2043                                              'gnus-summary-score-entry
2044                                              (nth 1 header)
2045                                              (if (or (string= (nth 1 header)
2046                                                               "head")
2047                                                      (string= (nth 1 header)
2048                                                               "body"))
2049                                                  ""
2050                                                (list 'gnus-summary-header
2051                                                      (nth 1 header)))
2052                                              (list 'quote (nth 1 (car ts)))
2053                                              (list 'gnus-score-delta-default
2054                                                    nil)
2055                                              (nth 1 (car ps))
2056                                              t)
2057                                             t)
2058                                            outp))
2059                                     (setq ps (cdr ps)))
2060                                   (list (nreverse outp))))
2061                                outt))
2062                         (setq ts (cdr ts)))
2063                       (list (nreverse outt))))
2064                    outh))
2065             (setq headers (cdr headers)))
2066           (list (nreverse outh))))))))
2067
2068 \f
2069
2070 (defun gnus-summary-mode (&optional group)
2071   "Major mode for reading articles.
2072
2073 All normal editing commands are switched off.
2074 \\<gnus-summary-mode-map>
2075 Each line in this buffer represents one article.  To read an
2076 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2077 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2078 respectively.
2079
2080 You can also post articles and send mail from this buffer.  To
2081 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2082 of an article, type `\\[gnus-summary-reply]'.
2083
2084 There are approx. one gazillion commands you can execute in this
2085 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2086
2087 The following commands are available:
2088
2089 \\{gnus-summary-mode-map}"
2090   (interactive)
2091   (when (gnus-visual-p 'summary-menu 'menu)
2092     (gnus-summary-make-menu-bar))
2093   (kill-all-local-variables)
2094   (gnus-summary-make-local-variables)
2095   (gnus-make-thread-indent-array)
2096   (gnus-simplify-mode-line)
2097   (setq major-mode 'gnus-summary-mode)
2098   (setq mode-name "Summary")
2099   (make-local-variable 'minor-mode-alist)
2100   (use-local-map gnus-summary-mode-map)
2101   (buffer-disable-undo)
2102   (setq buffer-read-only t)             ;Disable modification
2103   (setq truncate-lines t)
2104   (setq selective-display t)
2105   (setq selective-display-ellipses t)   ;Display `...'
2106   (gnus-summary-set-display-table)
2107   (gnus-set-default-directory)
2108   (setq gnus-newsgroup-name group)
2109   (unless (gnus-news-group-p group)
2110     (setq gnus-newsgroup-incorporated
2111           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2112   (make-local-variable 'gnus-summary-line-format)
2113   (make-local-variable 'gnus-summary-line-format-spec)
2114   (make-local-variable 'gnus-summary-dummy-line-format)
2115   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2116   (make-local-variable 'gnus-summary-mark-positions)
2117   (make-local-hook 'pre-command-hook)
2118   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2119   (gnus-run-hooks 'gnus-summary-mode-hook)
2120   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2121   (gnus-update-summary-mark-positions))
2122
2123 (defun gnus-summary-make-local-variables ()
2124   "Make all the local summary buffer variables."
2125   (let (global)
2126     (dolist (local gnus-summary-local-variables)
2127       (if (consp local)
2128           (progn
2129             (if (eq (cdr local) 'global)
2130                 ;; Copy the global value of the variable.
2131                 (setq global (symbol-value (car local)))
2132               ;; Use the value from the list.
2133               (setq global (eval (cdr local))))
2134             (set (make-local-variable (car local)) global))
2135         ;; Simple nil-valued local variable.
2136         (set (make-local-variable local) nil)))))
2137
2138 (defun gnus-summary-clear-local-variables ()
2139   (let ((locals gnus-summary-local-variables))
2140     (while locals
2141       (if (consp (car locals))
2142           (and (vectorp (caar locals))
2143                (set (caar locals) nil))
2144         (and (vectorp (car locals))
2145              (set (car locals) nil)))
2146       (setq locals (cdr locals)))))
2147
2148 ;; Summary data functions.
2149
2150 (defmacro gnus-data-number (data)
2151   `(car ,data))
2152
2153 (defmacro gnus-data-set-number (data number)
2154   `(setcar ,data ,number))
2155
2156 (defmacro gnus-data-mark (data)
2157   `(nth 1 ,data))
2158
2159 (defmacro gnus-data-set-mark (data mark)
2160   `(setcar (nthcdr 1 ,data) ,mark))
2161
2162 (defmacro gnus-data-pos (data)
2163   `(nth 2 ,data))
2164
2165 (defmacro gnus-data-set-pos (data pos)
2166   `(setcar (nthcdr 2 ,data) ,pos))
2167
2168 (defmacro gnus-data-header (data)
2169   `(nth 3 ,data))
2170
2171 (defmacro gnus-data-set-header (data header)
2172   `(setcar (nthcdr 3 ,data) ,header))
2173
2174 (defmacro gnus-data-level (data)
2175   `(nth 4 ,data))
2176
2177 (defmacro gnus-data-unread-p (data)
2178   `(= (nth 1 ,data) gnus-unread-mark))
2179
2180 (defmacro gnus-data-read-p (data)
2181   `(/= (nth 1 ,data) gnus-unread-mark))
2182
2183 (defmacro gnus-data-pseudo-p (data)
2184   `(consp (nth 3 ,data)))
2185
2186 (defmacro gnus-data-find (number)
2187   `(assq ,number gnus-newsgroup-data))
2188
2189 (defmacro gnus-data-find-list (number &optional data)
2190   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2191      (memq (assq ,number bdata)
2192            bdata)))
2193
2194 (defmacro gnus-data-make (number mark pos header level)
2195   `(list ,number ,mark ,pos ,header ,level))
2196
2197 (defun gnus-data-enter (after-article number mark pos header level offset)
2198   (let ((data (gnus-data-find-list after-article)))
2199     (unless data
2200       (error "No such article: %d" after-article))
2201     (setcdr data (cons (gnus-data-make number mark pos header level)
2202                        (cdr data)))
2203     (setq gnus-newsgroup-data-reverse nil)
2204     (gnus-data-update-list (cddr data) offset)))
2205
2206 (defun gnus-data-enter-list (after-article list &optional offset)
2207   (when list
2208     (let ((data (and after-article (gnus-data-find-list after-article)))
2209           (ilist list))
2210       (if (not (or data
2211                    after-article))
2212           (let ((odata gnus-newsgroup-data))
2213             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2214             (when offset
2215               (gnus-data-update-list odata offset)))
2216         ;; Find the last element in the list to be spliced into the main
2217         ;; list.
2218         (while (cdr list)
2219           (setq list (cdr list)))
2220         (if (not data)
2221             (progn
2222               (setcdr list gnus-newsgroup-data)
2223               (setq gnus-newsgroup-data ilist)
2224               (when offset
2225                 (gnus-data-update-list (cdr list) offset)))
2226           (setcdr list (cdr data))
2227           (setcdr data ilist)
2228           (when offset
2229             (gnus-data-update-list (cdr list) offset))))
2230       (setq gnus-newsgroup-data-reverse nil))))
2231
2232 (defun gnus-data-remove (article &optional offset)
2233   (let ((data gnus-newsgroup-data))
2234     (if (= (gnus-data-number (car data)) article)
2235         (progn
2236           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2237                 gnus-newsgroup-data-reverse nil)
2238           (when offset
2239             (gnus-data-update-list gnus-newsgroup-data offset)))
2240       (while (cdr data)
2241         (when (= (gnus-data-number (cadr data)) article)
2242           (setcdr data (cddr data))
2243           (when offset
2244             (gnus-data-update-list (cdr data) offset))
2245           (setq data nil
2246                 gnus-newsgroup-data-reverse nil))
2247         (setq data (cdr data))))))
2248
2249 (defmacro gnus-data-list (backward)
2250   `(if ,backward
2251        (or gnus-newsgroup-data-reverse
2252            (setq gnus-newsgroup-data-reverse
2253                  (reverse gnus-newsgroup-data)))
2254      gnus-newsgroup-data))
2255
2256 (defun gnus-data-update-list (data offset)
2257   "Add OFFSET to the POS of all data entries in DATA."
2258   (setq gnus-newsgroup-data-reverse nil)
2259   (while data
2260     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2261     (setq data (cdr data))))
2262
2263 (defun gnus-summary-article-pseudo-p (article)
2264   "Say whether this article is a pseudo article or not."
2265   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2266
2267 (defmacro gnus-summary-article-sparse-p (article)
2268   "Say whether this article is a sparse article or not."
2269   `(memq ,article gnus-newsgroup-sparse))
2270
2271 (defmacro gnus-summary-article-ancient-p (article)
2272   "Say whether this article is a sparse article or not."
2273   `(memq ,article gnus-newsgroup-ancient))
2274
2275 (defun gnus-article-parent-p (number)
2276   "Say whether this article is a parent or not."
2277   (let ((data (gnus-data-find-list number)))
2278     (and (cdr data)                     ; There has to be an article after...
2279          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2280             (gnus-data-level (nth 1 data))))))
2281
2282 (defun gnus-article-children (number)
2283   "Return a list of all children to NUMBER."
2284   (let* ((data (gnus-data-find-list number))
2285          (level (gnus-data-level (car data)))
2286          children)
2287     (setq data (cdr data))
2288     (while (and data
2289                 (= (gnus-data-level (car data)) (1+ level)))
2290       (push (gnus-data-number (car data)) children)
2291       (setq data (cdr data)))
2292     children))
2293
2294 (defmacro gnus-summary-skip-intangible ()
2295   "If the current article is intangible, then jump to a different article."
2296   '(let ((to (get-text-property (point) 'gnus-intangible)))
2297      (and to (gnus-summary-goto-subject to))))
2298
2299 (defmacro gnus-summary-article-intangible-p ()
2300   "Say whether this article is intangible or not."
2301   '(get-text-property (point) 'gnus-intangible))
2302
2303 (defun gnus-article-read-p (article)
2304   "Say whether ARTICLE is read or not."
2305   (not (or (memq article gnus-newsgroup-marked)
2306            (memq article gnus-newsgroup-unreads)
2307            (memq article gnus-newsgroup-unselected)
2308            (memq article gnus-newsgroup-dormant))))
2309
2310 ;; Some summary mode macros.
2311
2312 (defmacro gnus-summary-article-number ()
2313   "The article number of the article on the current line.
2314 If there isn's an article number here, then we return the current
2315 article number."
2316   '(progn
2317      (gnus-summary-skip-intangible)
2318      (or (get-text-property (point) 'gnus-number)
2319          (gnus-summary-last-subject))))
2320
2321 (defmacro gnus-summary-article-header (&optional number)
2322   "Return the header of article NUMBER."
2323   `(gnus-data-header (gnus-data-find
2324                       ,(or number '(gnus-summary-article-number)))))
2325
2326 (defmacro gnus-summary-thread-level (&optional number)
2327   "Return the level of thread that starts with article NUMBER."
2328   `(if (and (eq gnus-summary-make-false-root 'dummy)
2329             (get-text-property (point) 'gnus-intangible))
2330        0
2331      (gnus-data-level (gnus-data-find
2332                        ,(or number '(gnus-summary-article-number))))))
2333
2334 (defmacro gnus-summary-article-mark (&optional number)
2335   "Return the mark of article NUMBER."
2336   `(gnus-data-mark (gnus-data-find
2337                     ,(or number '(gnus-summary-article-number)))))
2338
2339 (defmacro gnus-summary-article-pos (&optional number)
2340   "Return the position of the line of article NUMBER."
2341   `(gnus-data-pos (gnus-data-find
2342                    ,(or number '(gnus-summary-article-number)))))
2343
2344 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2345 (defmacro gnus-summary-article-subject (&optional number)
2346   "Return current subject string or nil if nothing."
2347   `(let ((headers
2348           ,(if number
2349                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2350              '(gnus-data-header (assq (gnus-summary-article-number)
2351                                       gnus-newsgroup-data)))))
2352      (and headers
2353           (vectorp headers)
2354           (mail-header-subject headers))))
2355
2356 (defmacro gnus-summary-article-score (&optional number)
2357   "Return current article score."
2358   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2359                   gnus-newsgroup-scored))
2360        gnus-summary-default-score 0))
2361
2362 (defun gnus-summary-article-children (&optional number)
2363   "Return a list of article numbers that are children of article NUMBER."
2364   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2365          (level (gnus-data-level (car data)))
2366          l children)
2367     (while (and (setq data (cdr data))
2368                 (> (setq l (gnus-data-level (car data))) level))
2369       (and (= (1+ level) l)
2370            (push (gnus-data-number (car data))
2371                  children)))
2372     (nreverse children)))
2373
2374 (defun gnus-summary-article-parent (&optional number)
2375   "Return the article number of the parent of article NUMBER."
2376   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2377                                     (gnus-data-list t)))
2378          (level (gnus-data-level (car data))))
2379     (if (zerop level)
2380         ()                              ; This is a root.
2381       ;; We search until we find an article with a level less than
2382       ;; this one.  That function has to be the parent.
2383       (while (and (setq data (cdr data))
2384                   (not (< (gnus-data-level (car data)) level))))
2385       (and data (gnus-data-number (car data))))))
2386
2387 (defun gnus-unread-mark-p (mark)
2388   "Say whether MARK is the unread mark."
2389   (= mark gnus-unread-mark))
2390
2391 (defun gnus-read-mark-p (mark)
2392   "Say whether MARK is one of the marks that mark as read.
2393 This is all marks except unread, ticked, dormant, and expirable."
2394   (not (or (= mark gnus-unread-mark)
2395            (= mark gnus-ticked-mark)
2396            (= mark gnus-dormant-mark)
2397            (= mark gnus-expirable-mark))))
2398
2399 (defmacro gnus-article-mark (number)
2400   "Return the MARK of article NUMBER.
2401 This macro should only be used when computing the mark the \"first\"
2402 time; i.e., when generating the summary lines.  After that,
2403 `gnus-summary-article-mark' should be used to examine the
2404 marks of articles."
2405   `(cond
2406     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2407     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2408     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2409     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2410     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2411     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2412     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2413     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2414            gnus-ancient-mark))))
2415
2416 ;; Saving hidden threads.
2417
2418 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2419 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2420
2421 (defmacro gnus-save-hidden-threads (&rest forms)
2422   "Save hidden threads, eval FORMS, and restore the hidden threads."
2423   (let ((config (make-symbol "config")))
2424     `(let ((,config (gnus-hidden-threads-configuration)))
2425        (unwind-protect
2426            (save-excursion
2427              ,@forms)
2428          (gnus-restore-hidden-threads-configuration ,config)))))
2429
2430 (defun gnus-data-compute-positions ()
2431   "Compute the positions of all articles."
2432   (setq gnus-newsgroup-data-reverse nil)
2433   (let ((data gnus-newsgroup-data))
2434     (save-excursion
2435       (gnus-save-hidden-threads
2436         (gnus-summary-show-all-threads)
2437         (goto-char (point-min))
2438         (while data
2439           (while (get-text-property (point) 'gnus-intangible)
2440             (forward-line 1))
2441           (gnus-data-set-pos (car data) (+ (point) 3))
2442           (setq data (cdr data))
2443           (forward-line 1))))))
2444
2445 (defun gnus-hidden-threads-configuration ()
2446   "Return the current hidden threads configuration."
2447   (save-excursion
2448     (let (config)
2449       (goto-char (point-min))
2450       (while (search-forward "\r" nil t)
2451         (push (1- (point)) config))
2452       config)))
2453
2454 (defun gnus-restore-hidden-threads-configuration (config)
2455   "Restore hidden threads configuration from CONFIG."
2456   (save-excursion
2457     (let (point buffer-read-only)
2458       (while (setq point (pop config))
2459         (when (and (< point (point-max))
2460                    (goto-char point)
2461                    (eq (char-after) ?\n))
2462           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2463
2464 ;; Various summary mode internalish functions.
2465
2466 (defun gnus-mouse-pick-article (e)
2467   (interactive "e")
2468   (mouse-set-point e)
2469   (gnus-summary-next-page nil t))
2470
2471 (defun gnus-summary-set-display-table ()
2472   "Change the display table.
2473 Odd characters have a tendency to mess
2474 up nicely formatted displays - we make all possible glyphs
2475 display only a single character."
2476
2477   ;; We start from the standard display table, if any.
2478   (let ((table (or (copy-sequence standard-display-table)
2479                    (make-display-table)))
2480         (i 32))
2481     ;; Nix out all the control chars...
2482     (while (>= (setq i (1- i)) 0)
2483       (aset table i [??]))
2484     ;; ... but not newline and cr, of course.  (cr is necessary for the
2485     ;; selective display).
2486     (aset table ?\n nil)
2487     (aset table ?\r nil)
2488     ;; We keep TAB as well.
2489     (aset table ?\t nil)
2490     ;; We nix out any glyphs over 126 that are not set already.
2491     (let ((i 256))
2492       (while (>= (setq i (1- i)) 127)
2493         ;; Only modify if the entry is nil.
2494         (unless (aref table i)
2495           (aset table i [??]))))
2496     (setq buffer-display-table table)))
2497
2498 (defun gnus-summary-setup-buffer (group)
2499   "Initialize summary buffer."
2500   (let ((buffer (concat "*Summary " group "*")))
2501     (if (get-buffer buffer)
2502         (progn
2503           (set-buffer buffer)
2504           (setq gnus-summary-buffer (current-buffer))
2505           (not gnus-newsgroup-prepared))
2506       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2507       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2508       (gnus-summary-mode group)
2509       (when gnus-carpal
2510         (gnus-carpal-setup-buffer 'summary))
2511       (unless gnus-single-article-buffer
2512         (make-local-variable 'gnus-article-buffer)
2513         (make-local-variable 'gnus-article-current)
2514         (make-local-variable 'gnus-original-article-buffer))
2515       (setq gnus-newsgroup-name group)
2516       t)))
2517
2518 (defun gnus-set-global-variables ()
2519   "Set the global equivalents of the buffer-local variables.
2520 They are set to the latest values they had.  These reflect the summary
2521 buffer that was in action when the last article was fetched."
2522   (when (eq major-mode 'gnus-summary-mode)
2523     (setq gnus-summary-buffer (current-buffer))
2524     (let ((name gnus-newsgroup-name)
2525           (marked gnus-newsgroup-marked)
2526           (unread gnus-newsgroup-unreads)
2527           (headers gnus-current-headers)
2528           (data gnus-newsgroup-data)
2529           (summary gnus-summary-buffer)
2530           (article-buffer gnus-article-buffer)
2531           (original gnus-original-article-buffer)
2532           (gac gnus-article-current)
2533           (reffed gnus-reffed-article-number)
2534           (score-file gnus-current-score-file)
2535           (default-charset gnus-newsgroup-charset))
2536       (save-excursion
2537         (set-buffer gnus-group-buffer)
2538         (setq gnus-newsgroup-name name
2539               gnus-newsgroup-marked marked
2540               gnus-newsgroup-unreads unread
2541               gnus-current-headers headers
2542               gnus-newsgroup-data data
2543               gnus-article-current gac
2544               gnus-summary-buffer summary
2545               gnus-article-buffer article-buffer
2546               gnus-original-article-buffer original
2547               gnus-reffed-article-number reffed
2548               gnus-current-score-file score-file
2549               gnus-newsgroup-charset default-charset)
2550         ;; The article buffer also has local variables.
2551         (when (gnus-buffer-live-p gnus-article-buffer)
2552           (set-buffer gnus-article-buffer)
2553           (setq gnus-summary-buffer summary))))))
2554
2555 (defun gnus-summary-article-unread-p (article)
2556   "Say whether ARTICLE is unread or not."
2557   (memq article gnus-newsgroup-unreads))
2558
2559 (defun gnus-summary-first-article-p (&optional article)
2560   "Return whether ARTICLE is the first article in the buffer."
2561   (if (not (setq article (or article (gnus-summary-article-number))))
2562       nil
2563     (eq article (caar gnus-newsgroup-data))))
2564
2565 (defun gnus-summary-last-article-p (&optional article)
2566   "Return whether ARTICLE is the last article in the buffer."
2567   (if (not (setq article (or article (gnus-summary-article-number))))
2568       ;; All non-existent numbers are the last article.  :-)
2569       t
2570     (not (cdr (gnus-data-find-list article)))))
2571
2572 (defun gnus-make-thread-indent-array ()
2573   (let ((n 200))
2574     (unless (and gnus-thread-indent-array
2575                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2576       (setq gnus-thread-indent-array (make-vector 201 "")
2577             gnus-thread-indent-array-level gnus-thread-indent-level)
2578       (while (>= n 0)
2579         (aset gnus-thread-indent-array n
2580               (make-string (* n gnus-thread-indent-level) ? ))
2581         (setq n (1- n))))))
2582
2583 (defun gnus-update-summary-mark-positions ()
2584   "Compute where the summary marks are to go."
2585   (save-excursion
2586     (when (gnus-buffer-exists-p gnus-summary-buffer)
2587       (set-buffer gnus-summary-buffer))
2588     (let ((gnus-replied-mark 129)
2589           (gnus-score-below-mark 130)
2590           (gnus-score-over-mark 130)
2591           (gnus-download-mark 131)
2592           (spec gnus-summary-line-format-spec)
2593           gnus-visual pos)
2594       (save-excursion
2595         (gnus-set-work-buffer)
2596         (let ((gnus-summary-line-format-spec spec)
2597               (gnus-newsgroup-downloadable '((0 . t))))
2598           (gnus-summary-insert-line
2599            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2600            0 nil 128 t nil "" nil 1)
2601           (goto-char (point-min))
2602           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2603                                              (- (point) 2)))))
2604           (goto-char (point-min))
2605           (push (cons 'replied (and (search-forward "\201" nil t)
2606                                     (- (point) 2)))
2607                 pos)
2608           (goto-char (point-min))
2609           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2610                 pos)
2611           (goto-char (point-min))
2612           (push (cons 'download
2613                       (and (search-forward "\203" nil t) (- (point) 2)))
2614                 pos)))
2615       (setq gnus-summary-mark-positions pos))))
2616
2617 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2618   "Insert a dummy root in the summary buffer."
2619   (beginning-of-line)
2620   (gnus-add-text-properties
2621    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2622    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2623
2624 (defun gnus-summary-from-or-to-or-newsgroups (header)
2625   (let ((to (cdr (assq 'To (mail-header-extra header))))
2626         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2627         (default-mime-charset (with-current-buffer gnus-summary-buffer
2628                                 default-mime-charset)))
2629     (cond
2630      ((and to
2631            gnus-ignored-from-addresses
2632            (string-match gnus-ignored-from-addresses
2633                          (mail-header-from header)))
2634       (concat "-> "
2635               (or (car (funcall gnus-extract-address-components
2636                                 (funcall
2637                                  gnus-decode-encoded-word-function to)))
2638                   (funcall gnus-decode-encoded-word-function to))))
2639      ((and newsgroups
2640            gnus-ignored-from-addresses
2641            (string-match gnus-ignored-from-addresses
2642                          (mail-header-from header)))
2643       (concat "=> " newsgroups))
2644      (t
2645       (or (car (funcall gnus-extract-address-components
2646                         (mail-header-from header)))
2647           (mail-header-from header))))))
2648
2649 (defun gnus-summary-insert-line (gnus-tmp-header
2650                                  gnus-tmp-level gnus-tmp-current
2651                                  gnus-tmp-unread gnus-tmp-replied
2652                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2653                                  &optional gnus-tmp-dummy gnus-tmp-score
2654                                  gnus-tmp-process)
2655   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2656          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2657          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2658          (gnus-tmp-score-char
2659           (if (or (null gnus-summary-default-score)
2660                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2661                       gnus-summary-zcore-fuzz))
2662               ?  ;Whitespace
2663             (if (< gnus-tmp-score gnus-summary-default-score)
2664                 gnus-score-below-mark gnus-score-over-mark)))
2665          (gnus-tmp-replied
2666           (cond (gnus-tmp-process gnus-process-mark)
2667                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2668                  gnus-cached-mark)
2669                 (gnus-tmp-replied gnus-replied-mark)
2670                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2671                  gnus-saved-mark)
2672                 (t gnus-unread-mark)))
2673          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2674          (gnus-tmp-name
2675           (cond
2676            ((string-match "<[^>]+> *$" gnus-tmp-from)
2677             (let ((beg (match-beginning 0)))
2678               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2679                        (substring gnus-tmp-from (1+ (match-beginning 0))
2680                                   (1- (match-end 0))))
2681                   (substring gnus-tmp-from 0 beg))))
2682            ((string-match "(.+)" gnus-tmp-from)
2683             (substring gnus-tmp-from
2684                        (1+ (match-beginning 0)) (1- (match-end 0))))
2685            (t gnus-tmp-from)))
2686          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2687          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2688          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2689          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2690          (buffer-read-only nil))
2691     (when (string= gnus-tmp-name "")
2692       (setq gnus-tmp-name gnus-tmp-from))
2693     (unless (numberp gnus-tmp-lines)
2694       (setq gnus-tmp-lines 0))
2695     (gnus-put-text-property-excluding-characters-with-faces
2696      (point)
2697      (progn (eval gnus-summary-line-format-spec) (point))
2698      'gnus-number gnus-tmp-number)
2699     (when (gnus-visual-p 'summary-highlight 'highlight)
2700       (forward-line -1)
2701       (gnus-run-hooks 'gnus-summary-update-hook)
2702       (forward-line 1))))
2703
2704 (defun gnus-summary-update-line (&optional dont-update)
2705   "Update summary line after change."
2706   (when (and gnus-summary-default-score
2707              (not gnus-summary-inhibit-highlight))
2708     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2709            (article (gnus-summary-article-number))
2710            (score (gnus-summary-article-score article)))
2711       (unless dont-update
2712         (if (and gnus-summary-mark-below
2713                  (< (gnus-summary-article-score)
2714                     gnus-summary-mark-below))
2715             ;; This article has a low score, so we mark it as read.
2716             (when (memq article gnus-newsgroup-unreads)
2717               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2718           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2719             ;; This article was previously marked as read on account
2720             ;; of a low score, but now it has risen, so we mark it as
2721             ;; unread.
2722             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2723         (gnus-summary-update-mark
2724          (if (or (null gnus-summary-default-score)
2725                  (<= (abs (- score gnus-summary-default-score))
2726                      gnus-summary-zcore-fuzz))
2727              ?  ;Whitespace
2728            (if (< score gnus-summary-default-score)
2729                gnus-score-below-mark gnus-score-over-mark))
2730          'score))
2731       ;; Do visual highlighting.
2732       (when (gnus-visual-p 'summary-highlight 'highlight)
2733         (gnus-run-hooks 'gnus-summary-update-hook)))))
2734
2735 (defvar gnus-tmp-new-adopts nil)
2736
2737 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2738   "Return the number of articles in THREAD.
2739 This may be 0 in some cases -- if none of the articles in
2740 the thread are to be displayed."
2741   (let* ((number
2742           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2743           (cond
2744            ((not (listp thread))
2745             1)
2746            ((and (consp thread) (cdr thread))
2747             (apply
2748              '+ 1 (mapcar
2749                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2750            ((null thread)
2751             1)
2752            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2753             1)
2754            (t 0))))
2755     (when (and level (zerop level) gnus-tmp-new-adopts)
2756       (incf number
2757             (apply '+ (mapcar
2758                        'gnus-summary-number-of-articles-in-thread
2759                        gnus-tmp-new-adopts))))
2760     (if char
2761         (if (> number 1) gnus-not-empty-thread-mark
2762           gnus-empty-thread-mark)
2763       number)))
2764
2765 (defun gnus-summary-set-local-parameters (group)
2766   "Go through the local params of GROUP and set all variable specs in that list."
2767   (let ((params (gnus-group-find-parameter group))
2768         elem)
2769     (while params
2770       (setq elem (car params)
2771             params (cdr params))
2772       (and (consp elem)                 ; Has to be a cons.
2773            (consp (cdr elem))           ; The cdr has to be a list.
2774            (symbolp (car elem))         ; Has to be a symbol in there.
2775            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2776            (ignore-errors               ; So we set it.
2777              (make-local-variable (car elem))
2778              (set (car elem) (eval (nth 1 elem))))))))
2779
2780 (defun gnus-summary-read-group (group &optional show-all no-article
2781                                       kill-buffer no-display backward
2782                                       select-articles)
2783   "Start reading news in newsgroup GROUP.
2784 If SHOW-ALL is non-nil, already read articles are also listed.
2785 If NO-ARTICLE is non-nil, no article is selected initially.
2786 If NO-DISPLAY, don't generate a summary buffer."
2787   (let (result)
2788     (while (and group
2789                 (null (setq result
2790                             (let ((gnus-auto-select-next nil))
2791                               (or (gnus-summary-read-group-1
2792                                    group show-all no-article
2793                                    kill-buffer no-display
2794                                    select-articles)
2795                                   (setq show-all nil
2796                                         select-articles nil)))))
2797                 (eq gnus-auto-select-next 'quietly))
2798       (set-buffer gnus-group-buffer)
2799       ;; The entry function called above goes to the next
2800       ;; group automatically, so we go two groups back
2801       ;; if we are searching for the previous group.
2802       (when backward
2803         (gnus-group-prev-unread-group 2))
2804       (if (not (equal group (gnus-group-group-name)))
2805           (setq group (gnus-group-group-name))
2806         (setq group nil)))
2807     result))
2808
2809 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2810   "Directly jump to the other GROUP from summary buffer.
2811 If SHOW-ALL is non-nil, already read articles are also listed."
2812   (interactive
2813    (if (eq gnus-summary-buffer (current-buffer))
2814        (list (completing-read
2815               "Group: " gnus-active-hashtb nil t
2816               (when (and gnus-newsgroup-name
2817                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2818                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2819               'gnus-group-history)
2820              current-prefix-arg)
2821      (error "%s must be invoked from a gnus summary buffer." this-command)))
2822   (unless (or (zerop (length group))
2823               (and gnus-newsgroup-name
2824                    (string-equal gnus-newsgroup-name group)))
2825     (gnus-summary-exit)
2826     (gnus-summary-read-group group show-all
2827                              gnus-dont-select-after-jump-to-other-group)))
2828
2829 (defun gnus-summary-read-group-1 (group show-all no-article
2830                                         kill-buffer no-display
2831                                         &optional select-articles)
2832   ;; Killed foreign groups can't be entered.
2833   (when (and (not (gnus-group-native-p group))
2834              (not (gnus-gethash group gnus-newsrc-hashtb)))
2835     (error "Dead non-native groups can't be entered"))
2836   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2837   (let* ((new-group (gnus-summary-setup-buffer group))
2838          (quit-config (gnus-group-quit-config group))
2839          (did-select (and new-group (gnus-select-newsgroup
2840                                      group show-all select-articles))))
2841     (cond
2842      ;; This summary buffer exists already, so we just select it.
2843      ((not new-group)
2844       (gnus-set-global-variables)
2845       (when kill-buffer
2846         (gnus-kill-or-deaden-summary kill-buffer))
2847       (gnus-configure-windows 'summary 'force)
2848       (gnus-set-mode-line 'summary)
2849       (gnus-summary-position-point)
2850       (message "")
2851       t)
2852      ;; We couldn't select this group.
2853      ((null did-select)
2854       (when (and (eq major-mode 'gnus-summary-mode)
2855                  (not (equal (current-buffer) kill-buffer)))
2856         (kill-buffer (current-buffer))
2857         (if (not quit-config)
2858             (progn
2859               ;; Update the info -- marks might need to be removed,
2860               ;; for instance.
2861               (gnus-summary-update-info)
2862               (set-buffer gnus-group-buffer)
2863               (gnus-group-jump-to-group group)
2864               (gnus-group-next-unread-group 1))
2865           (gnus-handle-ephemeral-exit quit-config)))
2866       (gnus-message 3 "Can't select group")
2867       nil)
2868      ;; The user did a `C-g' while prompting for number of articles,
2869      ;; so we exit this group.
2870      ((eq did-select 'quit)
2871       (and (eq major-mode 'gnus-summary-mode)
2872            (not (equal (current-buffer) kill-buffer))
2873            (kill-buffer (current-buffer)))
2874       (when kill-buffer
2875         (gnus-kill-or-deaden-summary kill-buffer))
2876       (if (not quit-config)
2877           (progn
2878             (set-buffer gnus-group-buffer)
2879             (gnus-group-jump-to-group group)
2880             (gnus-group-next-unread-group 1)
2881             (gnus-configure-windows 'group 'force))
2882         (gnus-handle-ephemeral-exit quit-config))
2883       ;; Finally signal the quit.
2884       (signal 'quit nil))
2885      ;; The group was successfully selected.
2886      (t
2887       (gnus-set-global-variables)
2888       ;; Save the active value in effect when the group was entered.
2889       (setq gnus-newsgroup-active
2890             (gnus-copy-sequence
2891              (gnus-active gnus-newsgroup-name)))
2892       ;; You can change the summary buffer in some way with this hook.
2893       (gnus-run-hooks 'gnus-select-group-hook)
2894       ;; Set any local variables in the group parameters.
2895       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2896       (gnus-update-format-specifications
2897        nil 'summary 'summary-mode 'summary-dummy)
2898       (gnus-update-summary-mark-positions)
2899       ;; Do score processing.
2900       (when gnus-use-scoring
2901         (gnus-possibly-score-headers))
2902       ;; Check whether to fill in the gaps in the threads.
2903       (when gnus-build-sparse-threads
2904         (gnus-build-sparse-threads))
2905       ;; Find the initial limit.
2906       (if gnus-show-threads
2907           (if show-all
2908               (let ((gnus-newsgroup-dormant nil))
2909                 (gnus-summary-initial-limit show-all))
2910             (gnus-summary-initial-limit show-all))
2911         ;; When untreaded, all articles are always shown.
2912         (setq gnus-newsgroup-limit
2913               (mapcar
2914                (lambda (header) (mail-header-number header))
2915                gnus-newsgroup-headers)))
2916       ;; Generate the summary buffer.
2917       (unless no-display
2918         (gnus-summary-prepare))
2919       (when gnus-use-trees
2920         (gnus-tree-open group)
2921         (setq gnus-summary-highlight-line-function
2922               'gnus-tree-highlight-article))
2923       ;; If the summary buffer is empty, but there are some low-scored
2924       ;; articles or some excluded dormants, we include these in the
2925       ;; buffer.
2926       (when (and (zerop (buffer-size))
2927                  (not no-display))
2928         (cond (gnus-newsgroup-dormant
2929                (gnus-summary-limit-include-dormant))
2930               ((and gnus-newsgroup-scored show-all)
2931                (gnus-summary-limit-include-expunged t))))
2932       ;; Function `gnus-apply-kill-file' must be called in this hook.
2933       (gnus-run-hooks 'gnus-apply-kill-hook)
2934       (if (and (zerop (buffer-size))
2935                (not no-display))
2936           (progn
2937             ;; This newsgroup is empty.
2938             (gnus-summary-catchup-and-exit nil t)
2939             (gnus-message 6 "No unread news")
2940             (when kill-buffer
2941               (gnus-kill-or-deaden-summary kill-buffer))
2942             ;; Return nil from this function.
2943             nil)
2944         ;; Hide conversation thread subtrees.  We cannot do this in
2945         ;; gnus-summary-prepare-hook since kill processing may not
2946         ;; work with hidden articles.
2947         (and gnus-show-threads
2948              gnus-thread-hide-subtree
2949              (gnus-summary-hide-all-threads))
2950         (when kill-buffer
2951           (gnus-kill-or-deaden-summary kill-buffer))
2952         ;; Show first unread article if requested.
2953         (if (and (not no-article)
2954                  (not no-display)
2955                  gnus-newsgroup-unreads
2956                  gnus-auto-select-first)
2957             (progn
2958               (gnus-configure-windows 'summary)
2959               (cond
2960                ((eq gnus-auto-select-first 'best)
2961                 (gnus-summary-best-unread-article))
2962                ((eq gnus-auto-select-first t)
2963                 (gnus-summary-first-unread-article))
2964                ((gnus-functionp gnus-auto-select-first)
2965                 (funcall gnus-auto-select-first))))
2966           ;; Don't select any articles, just move point to the first
2967           ;; article in the group.
2968           (goto-char (point-min))
2969           (gnus-summary-position-point)
2970           (gnus-configure-windows 'summary 'force)
2971           (gnus-set-mode-line 'summary))
2972         (when (get-buffer-window gnus-group-buffer t)
2973           ;; Gotta use windows, because recenter does weird stuff if
2974           ;; the current buffer ain't the displayed window.
2975           (let ((owin (selected-window)))
2976             (select-window (get-buffer-window gnus-group-buffer t))
2977             (when (gnus-group-goto-group group)
2978               (recenter))
2979             (select-window owin)))
2980         ;; Mark this buffer as "prepared".
2981         (setq gnus-newsgroup-prepared t)
2982         (gnus-run-hooks 'gnus-summary-prepared-hook)
2983         t)))))
2984
2985 (defun gnus-summary-prepare ()
2986   "Generate the summary buffer."
2987   (interactive)
2988   (let ((buffer-read-only nil))
2989     (erase-buffer)
2990     (setq gnus-newsgroup-data nil
2991           gnus-newsgroup-data-reverse nil)
2992     (gnus-run-hooks 'gnus-summary-generate-hook)
2993     ;; Generate the buffer, either with threads or without.
2994     (when gnus-newsgroup-headers
2995       (gnus-summary-prepare-threads
2996        (if gnus-show-threads
2997            (gnus-sort-gathered-threads
2998             (funcall gnus-summary-thread-gathering-function
2999                      (gnus-sort-threads
3000                       (gnus-cut-threads (gnus-make-threads)))))
3001          ;; Unthreaded display.
3002          (gnus-sort-articles gnus-newsgroup-headers))))
3003     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3004     ;; Call hooks for modifying summary buffer.
3005     (goto-char (point-min))
3006     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3007
3008 (defsubst gnus-general-simplify-subject (subject)
3009   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3010   (setq subject
3011         (cond
3012          ;; Truncate the subject.
3013          (gnus-simplify-subject-functions
3014           (gnus-map-function gnus-simplify-subject-functions subject))
3015          ((numberp gnus-summary-gather-subject-limit)
3016           (setq subject (gnus-simplify-subject-re subject))
3017           (if (> (length subject) gnus-summary-gather-subject-limit)
3018               (substring subject 0 gnus-summary-gather-subject-limit)
3019             subject))
3020          ;; Fuzzily simplify it.
3021          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3022           (gnus-simplify-subject-fuzzy subject))
3023          ;; Just remove the leading "Re:".
3024          (t
3025           (gnus-simplify-subject-re subject))))
3026
3027   (if (and gnus-summary-gather-exclude-subject
3028            (string-match gnus-summary-gather-exclude-subject subject))
3029       nil                               ; This article shouldn't be gathered
3030     subject))
3031
3032 (defun gnus-summary-simplify-subject-query ()
3033   "Query where the respool algorithm would put this article."
3034   (interactive)
3035   (gnus-summary-select-article)
3036   (message "%s"
3037            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3038
3039 (defun gnus-gather-threads-by-subject (threads)
3040   "Gather threads by looking at Subject headers."
3041   (if (not gnus-summary-make-false-root)
3042       threads
3043     (let ((hashtb (gnus-make-hashtable 1024))
3044           (prev threads)
3045           (result threads)
3046           subject hthread whole-subject)
3047       (while threads
3048         (setq subject (gnus-general-simplify-subject
3049                        (setq whole-subject (mail-header-subject
3050                                             (caar threads)))))
3051         (when subject
3052           (if (setq hthread (gnus-gethash subject hashtb))
3053               (progn
3054                 ;; We enter a dummy root into the thread, if we
3055                 ;; haven't done that already.
3056                 (unless (stringp (caar hthread))
3057                   (setcar hthread (list whole-subject (car hthread))))
3058                 ;; We add this new gathered thread to this gathered
3059                 ;; thread.
3060                 (setcdr (car hthread)
3061                         (nconc (cdar hthread) (list (car threads))))
3062                 ;; Remove it from the list of threads.
3063                 (setcdr prev (cdr threads))
3064                 (setq threads prev))
3065             ;; Enter this thread into the hash table.
3066             (gnus-sethash subject threads hashtb)))
3067         (setq prev threads)
3068         (setq threads (cdr threads)))
3069       result)))
3070
3071 (defun gnus-gather-threads-by-references (threads)
3072   "Gather threads by looking at References headers."
3073   (let ((idhashtb (gnus-make-hashtable 1024))
3074         (thhashtb (gnus-make-hashtable 1024))
3075         (prev threads)
3076         (result threads)
3077         ids references id gthread gid entered ref)
3078     (while threads
3079       (when (setq references (mail-header-references (caar threads)))
3080         (setq id (mail-header-id (caar threads))
3081               ids (gnus-split-references references)
3082               entered nil)
3083         (while (setq ref (pop ids))
3084           (setq ids (delete ref ids))
3085           (if (not (setq gid (gnus-gethash ref idhashtb)))
3086               (progn
3087                 (gnus-sethash ref id idhashtb)
3088                 (gnus-sethash id threads thhashtb))
3089             (setq gthread (gnus-gethash gid thhashtb))
3090             (unless entered
3091               ;; We enter a dummy root into the thread, if we
3092               ;; haven't done that already.
3093               (unless (stringp (caar gthread))
3094                 (setcar gthread (list (mail-header-subject (caar gthread))
3095                                       (car gthread))))
3096               ;; We add this new gathered thread to this gathered
3097               ;; thread.
3098               (setcdr (car gthread)
3099                       (nconc (cdar gthread) (list (car threads)))))
3100             ;; Add it into the thread hash table.
3101             (gnus-sethash id gthread thhashtb)
3102             (setq entered t)
3103             ;; Remove it from the list of threads.
3104             (setcdr prev (cdr threads))
3105             (setq threads prev))))
3106       (setq prev threads)
3107       (setq threads (cdr threads)))
3108     result))
3109
3110 (defun gnus-sort-gathered-threads (threads)
3111   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3112   (let ((result threads))
3113     (while threads
3114       (when (stringp (caar threads))
3115         (setcdr (car threads)
3116                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3117       (setq threads (cdr threads)))
3118     result))
3119
3120 (defun gnus-thread-loop-p (root thread)
3121   "Say whether ROOT is in THREAD."
3122   (let ((stack (list thread))
3123         (infloop 0)
3124         th)
3125     (while (setq thread (pop stack))
3126       (setq th (cdr thread))
3127       (while (and th
3128                   (not (eq (caar th) root)))
3129         (pop th))
3130       (if th
3131           ;; We have found a loop.
3132           (let (ref-dep)
3133             (setcdr thread (delq (car th) (cdr thread)))
3134             (if (boundp (setq ref-dep (intern "none"
3135                                               gnus-newsgroup-dependencies)))
3136                 (setcdr (symbol-value ref-dep)
3137                         (nconc (cdr (symbol-value ref-dep))
3138                                (list (car th))))
3139               (set ref-dep (list nil (car th))))
3140             (setq infloop 1
3141                   stack nil))
3142         ;; Push all the subthreads onto the stack.
3143         (push (cdr thread) stack)))
3144     infloop))
3145
3146 (defun gnus-make-threads ()
3147   "Go through the dependency hashtb and find the roots.  Return all threads."
3148   (let (threads)
3149     (while (catch 'infloop
3150              (mapatoms
3151               (lambda (refs)
3152                 ;; Deal with self-referencing References loops.
3153                 (when (and (car (symbol-value refs))
3154                            (not (zerop
3155                                  (apply
3156                                   '+
3157                                   (mapcar
3158                                    (lambda (thread)
3159                                      (gnus-thread-loop-p
3160                                       (car (symbol-value refs)) thread))
3161                                    (cdr (symbol-value refs)))))))
3162                   (setq threads nil)
3163                   (throw 'infloop t))
3164                 (unless (car (symbol-value refs))
3165                   ;; These threads do not refer back to any other articles,
3166                   ;; so they're roots.
3167                   (setq threads (append (cdr (symbol-value refs)) threads))))
3168               gnus-newsgroup-dependencies)))
3169     threads))
3170
3171 ;; Build the thread tree.
3172 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3173   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3174
3175 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3176 if it was already present.
3177
3178 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3179 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3180 Message-IDs will be renamed be renamed to a unique Message-ID before
3181 being entered.
3182
3183 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3184   (let* ((id (mail-header-id header))
3185          (id-dep (and id (intern id dependencies)))
3186          ref ref-dep ref-header)
3187     ;; Enter this `header' in the `dependencies' table.
3188     (cond
3189      ((not id-dep)
3190       (setq header nil))
3191      ;; The first two cases do the normal part: enter a new `header'
3192      ;; in the `dependencies' table.
3193      ((not (boundp id-dep))
3194       (set id-dep (list header)))
3195      ((null (car (symbol-value id-dep)))
3196       (setcar (symbol-value id-dep) header))
3197
3198      ;; From here the `header' was already present in the
3199      ;; `dependencies' table.
3200      (force-new
3201       ;; Overrides an existing entry;
3202       ;; just set the header part of the entry.
3203       (setcar (symbol-value id-dep) header))
3204
3205      ;; Renames the existing `header' to a unique Message-ID.
3206      ((not gnus-summary-ignore-duplicates)
3207       ;; An article with this Message-ID has already been seen.
3208       ;; We rename the Message-ID.
3209       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3210            (list header))
3211       (mail-header-set-id header id))
3212
3213      ;; The last case ignores an existing entry, except it adds any
3214      ;; additional Xrefs (in case the two articles came from different
3215      ;; servers.
3216      ;; Also sets `header' to `nil' meaning that the `dependencies'
3217      ;; table was *not* modified.
3218      (t
3219       (mail-header-set-xref
3220        (car (symbol-value id-dep))
3221        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3222                    "")
3223                (or (mail-header-xref header) "")))
3224       (setq header nil)))
3225
3226     (when header
3227       ;; First check if that we are not creating a References loop.
3228       (setq ref (gnus-parent-id (mail-header-references header)))
3229       (while (and ref
3230                   (setq ref-dep (intern-soft ref dependencies))
3231                   (boundp ref-dep)
3232                   (setq ref-header (car (symbol-value ref-dep))))
3233         (if (string= id ref)
3234             ;; Yuk!  This is a reference loop.  Make the article be a
3235             ;; root article.
3236             (progn
3237               (mail-header-set-references (car (symbol-value id-dep)) "none")
3238               (setq ref nil))
3239           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3240       (setq ref (gnus-parent-id (mail-header-references header)))
3241       (setq ref-dep (intern (or ref "none") dependencies))
3242       (if (boundp ref-dep)
3243           (setcdr (symbol-value ref-dep)
3244                   (nconc (cdr (symbol-value ref-dep))
3245                          (list (symbol-value id-dep))))
3246         (set ref-dep (list nil (symbol-value id-dep)))))
3247     header))
3248
3249 (defun gnus-build-sparse-threads ()
3250   (let ((headers gnus-newsgroup-headers)
3251         (mail-parse-charset gnus-newsgroup-charset)
3252         (gnus-summary-ignore-duplicates t)
3253         header references generation relations
3254         subject child end new-child date)
3255     ;; First we create an alist of generations/relations, where
3256     ;; generations is how much we trust the relation, and the relation
3257     ;; is parent/child.
3258     (gnus-message 7 "Making sparse threads...")
3259     (save-excursion
3260       (nnheader-set-temp-buffer " *gnus sparse threads*")
3261       (while (setq header (pop headers))
3262         (when (and (setq references (mail-header-references header))
3263                    (not (string= references "")))
3264           (insert references)
3265           (setq child (mail-header-id header)
3266                 subject (mail-header-subject header)
3267                 date (mail-header-date header)
3268                 generation 0)
3269           (while (search-backward ">" nil t)
3270             (setq end (1+ (point)))
3271             (when (search-backward "<" nil t)
3272               (setq new-child (buffer-substring (point) end))
3273               (push (list (incf generation)
3274                           child (setq child new-child)
3275                           subject date)
3276                     relations)))
3277           (when child
3278             (push (list (1+ generation) child nil subject) relations))
3279           (erase-buffer)))
3280       (kill-buffer (current-buffer)))
3281     ;; Sort over trustworthiness.
3282     (mapcar
3283      (lambda (relation)
3284        (when (gnus-dependencies-add-header
3285               (make-full-mail-header-from-decoded-header
3286                gnus-reffed-article-number
3287                (nth 3 relation) "" (or (nth 4 relation) "")
3288                (nth 1 relation)
3289                (or (nth 2 relation) "") 0 0 "")
3290               gnus-newsgroup-dependencies nil)
3291          (push gnus-reffed-article-number gnus-newsgroup-limit)
3292          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3293          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3294                gnus-newsgroup-reads)
3295          (decf gnus-reffed-article-number)))
3296      (sort relations 'car-less-than-car))
3297     (gnus-message 7 "Making sparse threads...done")))
3298
3299 (defun gnus-build-old-threads ()
3300   ;; Look at all the articles that refer back to old articles, and
3301   ;; fetch the headers for the articles that aren't there.  This will
3302   ;; build complete threads - if the roots haven't been expired by the
3303   ;; server, that is.
3304   (let ((mail-parse-charset gnus-newsgroup-charset)
3305         id heads)
3306     (mapatoms
3307      (lambda (refs)
3308        (when (not (car (symbol-value refs)))
3309          (setq heads (cdr (symbol-value refs)))
3310          (while heads
3311            (if (memq (mail-header-number (caar heads))
3312                      gnus-newsgroup-dormant)
3313                (setq heads (cdr heads))
3314              (setq id (symbol-name refs))
3315              (while (and (setq id (gnus-build-get-header id))
3316                          (not (car (gnus-id-to-thread id)))))
3317              (setq heads nil)))))
3318      gnus-newsgroup-dependencies)))
3319
3320 ;; This function has to be called with point after the article number
3321 ;; on the beginning of the line.
3322 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3323   (let ((eol (gnus-point-at-eol))
3324         (buffer (current-buffer))
3325         header)
3326
3327     ;; overview: [num subject from date id refs chars lines misc]
3328     (unwind-protect
3329         (progn
3330           (narrow-to-region (point) eol)
3331           (unless (eobp)
3332             (forward-char))
3333
3334           (setq header
3335                 (make-full-mail-header
3336                  number                         ; number
3337                  (nnheader-nov-field)           ; subject
3338                  (nnheader-nov-field)           ; from
3339                  (nnheader-nov-field)           ; date
3340                  (nnheader-nov-read-message-id) ; id
3341                  (nnheader-nov-field)           ; refs
3342                  (nnheader-nov-read-integer)    ; chars
3343                  (nnheader-nov-read-integer)    ; lines
3344                  (unless (eobp)
3345                    (if (looking-at "Xref: ")
3346                        (goto-char (match-end 0)))
3347                    (nnheader-nov-field))        ; Xref
3348                  (nnheader-nov-parse-extra))))  ; extra
3349
3350       (widen))
3351
3352     (when gnus-alter-header-function
3353       (funcall gnus-alter-header-function header))
3354     (gnus-dependencies-add-header header dependencies force-new)))
3355
3356 (defun gnus-build-get-header (id)
3357   "Look through the buffer of NOV lines and find the header to ID.
3358 Enter this line into the dependencies hash table, and return
3359 the id of the parent article (if any)."
3360   (let ((deps gnus-newsgroup-dependencies)
3361         found header)
3362     (prog1
3363         (save-excursion
3364           (set-buffer nntp-server-buffer)
3365           (let ((case-fold-search nil))
3366             (goto-char (point-min))
3367             (while (and (not found)
3368                         (search-forward id nil t))
3369               (beginning-of-line)
3370               (setq found (looking-at
3371                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3372                                    (regexp-quote id))))
3373               (or found (beginning-of-line 2)))
3374             (when found
3375               (beginning-of-line)
3376               (and
3377                (setq header (gnus-nov-parse-line
3378                              (read (current-buffer)) deps))
3379                (gnus-parent-id (mail-header-references header))))))
3380       (when header
3381         (let ((number (mail-header-number header)))
3382           (push number gnus-newsgroup-limit)
3383           (push header gnus-newsgroup-headers)
3384           (if (memq number gnus-newsgroup-unselected)
3385               (progn
3386                 (push number gnus-newsgroup-unreads)
3387                 (setq gnus-newsgroup-unselected
3388                       (delq number gnus-newsgroup-unselected)))
3389             (push number gnus-newsgroup-ancient)))))))
3390
3391 (defun gnus-build-all-threads ()
3392   "Read all the headers."
3393   (let ((gnus-summary-ignore-duplicates t)
3394         (mail-parse-charset gnus-newsgroup-charset)
3395         (dependencies gnus-newsgroup-dependencies)
3396         header article)
3397     (save-excursion
3398       (set-buffer nntp-server-buffer)
3399       (let ((case-fold-search nil))
3400         (goto-char (point-min))
3401         (while (not (eobp))
3402           (ignore-errors
3403             (setq article (read (current-buffer))
3404                   header (gnus-nov-parse-line article dependencies)))
3405           (when header
3406             (save-excursion
3407               (set-buffer gnus-summary-buffer)
3408               (push header gnus-newsgroup-headers)
3409               (if (memq (setq article (mail-header-number header))
3410                         gnus-newsgroup-unselected)
3411                   (progn
3412                     (push article gnus-newsgroup-unreads)
3413                     (setq gnus-newsgroup-unselected
3414                           (delq article gnus-newsgroup-unselected)))
3415                 (push article gnus-newsgroup-ancient)))
3416             (forward-line 1)))))))
3417
3418 (defun gnus-summary-update-article-line (article header)
3419   "Update the line for ARTICLE using HEADERS."
3420   (let* ((id (mail-header-id header))
3421          (thread (gnus-id-to-thread id)))
3422     (unless thread
3423       (error "Article in no thread"))
3424     ;; Update the thread.
3425     (setcar thread header)
3426     (gnus-summary-goto-subject article)
3427     (let* ((datal (gnus-data-find-list article))
3428            (data (car datal))
3429            (length (when (cdr datal)
3430                      (- (gnus-data-pos data)
3431                         (gnus-data-pos (cadr datal)))))
3432            (buffer-read-only nil)
3433            (level (gnus-summary-thread-level)))
3434       (gnus-delete-line)
3435       (gnus-summary-insert-line
3436        header level nil (gnus-article-mark article)
3437        (memq article gnus-newsgroup-replied)
3438        (memq article gnus-newsgroup-expirable)
3439        ;; Only insert the Subject string when it's different
3440        ;; from the previous Subject string.
3441        (if (and
3442             gnus-show-threads
3443             (gnus-subject-equal
3444              (condition-case ()
3445                  (mail-header-subject
3446                   (gnus-data-header
3447                    (cadr
3448                     (gnus-data-find-list
3449                      article
3450                      (gnus-data-list t)))))
3451                ;; Error on the side of excessive subjects.
3452                (error ""))
3453              (mail-header-subject header)))
3454            ""
3455          (mail-header-subject header))
3456        nil (cdr (assq article gnus-newsgroup-scored))
3457        (memq article gnus-newsgroup-processable))
3458       (when length
3459         (gnus-data-update-list
3460          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3461
3462 (defun gnus-summary-update-article (article &optional iheader)
3463   "Update ARTICLE in the summary buffer."
3464   (set-buffer gnus-summary-buffer)
3465   (let* ((header (gnus-summary-article-header article))
3466          (id (mail-header-id header))
3467          (data (gnus-data-find article))
3468          (thread (gnus-id-to-thread id))
3469          (references (mail-header-references header))
3470          (parent
3471           (gnus-id-to-thread
3472            (or (gnus-parent-id
3473                 (when (and references
3474                            (not (equal "" references)))
3475                   references))
3476                "none")))
3477          (buffer-read-only nil)
3478          (old (car thread)))
3479     (when thread
3480       (unless iheader
3481         (setcar thread nil)
3482         (when parent
3483           (delq thread parent)))
3484       (if (gnus-summary-insert-subject id header)
3485           ;; Set the (possibly) new article number in the data structure.
3486           (gnus-data-set-number data (gnus-id-to-article id))
3487         (setcar thread old)
3488         nil))))
3489
3490 (defun gnus-rebuild-thread (id &optional line)
3491   "Rebuild the thread containing ID.
3492 If LINE, insert the rebuilt thread starting on line LINE."
3493   (let ((buffer-read-only nil)
3494         old-pos current thread data)
3495     (if (not gnus-show-threads)
3496         (setq thread (list (car (gnus-id-to-thread id))))
3497       ;; Get the thread this article is part of.
3498       (setq thread (gnus-remove-thread id)))
3499     (setq old-pos (gnus-point-at-bol))
3500     (setq current (save-excursion
3501                     (and (zerop (forward-line -1))
3502                          (gnus-summary-article-number))))
3503     ;; If this is a gathered thread, we have to go some re-gathering.
3504     (when (stringp (car thread))
3505       (let ((subject (car thread))
3506             roots thr)
3507         (setq thread (cdr thread))
3508         (while thread
3509           (unless (memq (setq thr (gnus-id-to-thread
3510                                    (gnus-root-id
3511                                     (mail-header-id (caar thread)))))
3512                         roots)
3513             (push thr roots))
3514           (setq thread (cdr thread)))
3515         ;; We now have all (unique) roots.
3516         (if (= (length roots) 1)
3517             ;; All the loose roots are now one solid root.
3518             (setq thread (car roots))
3519           (setq thread (cons subject (gnus-sort-threads roots))))))
3520     (let (threads)
3521       ;; We then insert this thread into the summary buffer.
3522       (when line
3523         (goto-char (point-min))
3524         (forward-line (1- line)))
3525       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3526         (if gnus-show-threads
3527             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3528           (gnus-summary-prepare-unthreaded thread))
3529         (setq data (nreverse gnus-newsgroup-data))
3530         (setq threads gnus-newsgroup-threads))
3531       ;; We splice the new data into the data structure.
3532       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3533       ;;!!! then we want to insert at the beginning of the buffer.
3534       ;;!!! That happens to be true with Gnus now, but that may
3535       ;;!!! change in the future.  Perhaps.
3536       (gnus-data-enter-list
3537        (if line nil current) data (- (point) old-pos))
3538       (setq gnus-newsgroup-threads
3539             (nconc threads gnus-newsgroup-threads))
3540       (gnus-data-compute-positions))))
3541
3542 (defun gnus-number-to-header (number)
3543   "Return the header for article NUMBER."
3544   (let ((headers gnus-newsgroup-headers))
3545     (while (and headers
3546                 (not (= number (mail-header-number (car headers)))))
3547       (pop headers))
3548     (when headers
3549       (car headers))))
3550
3551 (defun gnus-parent-headers (in-headers &optional generation)
3552   "Return the headers of the GENERATIONeth parent of HEADERS."
3553   (unless generation
3554     (setq generation 1))
3555   (let ((parent t)
3556         (headers in-headers)
3557         references)
3558     (while (and parent
3559                 (not (zerop generation))
3560                 (setq references (mail-header-references headers)))
3561       (setq headers (if (and references
3562                              (setq parent (gnus-parent-id references)))
3563                         (car (gnus-id-to-thread parent))
3564                       nil))
3565       (decf generation))
3566     (and (not (eq headers in-headers))
3567          headers)))
3568
3569 (defun gnus-id-to-thread (id)
3570   "Return the (sub-)thread where ID appears."
3571   (gnus-gethash id gnus-newsgroup-dependencies))
3572
3573 (defun gnus-id-to-article (id)
3574   "Return the article number of ID."
3575   (let ((thread (gnus-id-to-thread id)))
3576     (when (and thread
3577                (car thread))
3578       (mail-header-number (car thread)))))
3579
3580 (defun gnus-id-to-header (id)
3581   "Return the article headers of ID."
3582   (car (gnus-id-to-thread id)))
3583
3584 (defun gnus-article-displayed-root-p (article)
3585   "Say whether ARTICLE is a root(ish) article."
3586   (let ((level (gnus-summary-thread-level article))
3587         (refs (mail-header-references  (gnus-summary-article-header article)))
3588         particle)
3589     (cond
3590      ((null level) nil)
3591      ((zerop level) t)
3592      ((null refs) t)
3593      ((null (gnus-parent-id refs)) t)
3594      ((and (= 1 level)
3595            (null (setq particle (gnus-id-to-article
3596                                  (gnus-parent-id refs))))
3597            (null (gnus-summary-thread-level particle)))))))
3598
3599 (defun gnus-root-id (id)
3600   "Return the id of the root of the thread where ID appears."
3601   (let (last-id prev)
3602     (while (and id (setq prev (car (gnus-id-to-thread id))))
3603       (setq last-id id
3604             id (gnus-parent-id (mail-header-references prev))))
3605     last-id))
3606
3607 (defun gnus-articles-in-thread (thread)
3608   "Return the list of articles in THREAD."
3609   (cons (mail-header-number (car thread))
3610         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3611
3612 (defun gnus-remove-thread (id &optional dont-remove)
3613   "Remove the thread that has ID in it."
3614   (let (headers thread last-id)
3615     ;; First go up in this thread until we find the root.
3616     (setq last-id (gnus-root-id id)
3617           headers (message-flatten-list (gnus-id-to-thread last-id)))
3618     ;; We have now found the real root of this thread.  It might have
3619     ;; been gathered into some loose thread, so we have to search
3620     ;; through the threads to find the thread we wanted.
3621     (let ((threads gnus-newsgroup-threads)
3622           sub)
3623       (while threads
3624         (setq sub (car threads))
3625         (if (stringp (car sub))
3626             ;; This is a gathered thread, so we look at the roots
3627             ;; below it to find whether this article is in this
3628             ;; gathered root.
3629             (progn
3630               (setq sub (cdr sub))
3631               (while sub
3632                 (when (member (caar sub) headers)
3633                   (setq thread (car threads)
3634                         threads nil
3635                         sub nil))
3636                 (setq sub (cdr sub))))
3637           ;; It's an ordinary thread, so we check it.
3638           (when (eq (car sub) (car headers))
3639             (setq thread sub
3640                   threads nil)))
3641         (setq threads (cdr threads)))
3642       ;; If this article is in no thread, then it's a root.
3643       (if thread
3644           (unless dont-remove
3645             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3646         (setq thread (gnus-id-to-thread last-id)))
3647       (when thread
3648         (prog1
3649             thread                      ; We return this thread.
3650           (unless dont-remove
3651             (if (stringp (car thread))
3652                 (progn
3653                   ;; If we use dummy roots, then we have to remove the
3654                   ;; dummy root as well.
3655                   (when (eq gnus-summary-make-false-root 'dummy)
3656                     ;; We go to the dummy root by going to
3657                     ;; the first sub-"thread", and then one line up.
3658                     (gnus-summary-goto-article
3659                      (mail-header-number (caadr thread)))
3660                     (forward-line -1)
3661                     (gnus-delete-line)
3662                     (gnus-data-compute-positions))
3663                   (setq thread (cdr thread))
3664                   (while thread
3665                     (gnus-remove-thread-1 (car thread))
3666                     (setq thread (cdr thread))))
3667               (gnus-remove-thread-1 thread))))))))
3668
3669 (defun gnus-remove-thread-1 (thread)
3670   "Remove the thread THREAD recursively."
3671   (let ((number (mail-header-number (pop thread)))
3672         d)
3673     (setq thread (reverse thread))
3674     (while thread
3675       (gnus-remove-thread-1 (pop thread)))
3676     (when (setq d (gnus-data-find number))
3677       (goto-char (gnus-data-pos d))
3678       (gnus-summary-show-thread)
3679       (gnus-data-remove
3680        number
3681        (- (gnus-point-at-bol)
3682           (prog1
3683               (1+ (gnus-point-at-eol))
3684             (gnus-delete-line)))))))
3685
3686 (defun gnus-sort-threads-1 (threads func)
3687   (sort (mapcar (lambda (thread)
3688                   (cons (car thread)
3689                         (and (cdr thread)
3690                              (gnus-sort-threads-1 (cdr thread) func))))
3691                 threads) func))
3692
3693 (defun gnus-sort-threads (threads)
3694   "Sort THREADS."
3695   (if (not gnus-thread-sort-functions)
3696       threads
3697     (gnus-message 8 "Sorting threads...")
3698     (prog1
3699         (gnus-sort-threads-1 
3700          threads 
3701          (gnus-make-sort-function gnus-thread-sort-functions))
3702       (gnus-message 8 "Sorting threads...done"))))
3703
3704 (defun gnus-sort-articles (articles)
3705   "Sort ARTICLES."
3706   (when gnus-article-sort-functions
3707     (gnus-message 7 "Sorting articles...")
3708     (prog1
3709         (setq gnus-newsgroup-headers
3710               (sort articles (gnus-make-sort-function
3711                               gnus-article-sort-functions)))
3712       (gnus-message 7 "Sorting articles...done"))))
3713
3714 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3715 (defmacro gnus-thread-header (thread)
3716   "Return header of first article in THREAD.
3717 Note that THREAD must never, ever be anything else than a variable -
3718 using some other form will lead to serious barfage."
3719   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3720   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3721   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3722         (vector thread) 2))
3723
3724 (defsubst gnus-article-sort-by-number (h1 h2)
3725   "Sort articles by article number."
3726   (< (mail-header-number h1)
3727      (mail-header-number h2)))
3728
3729 (defun gnus-thread-sort-by-number (h1 h2)
3730   "Sort threads by root article number."
3731   (gnus-article-sort-by-number
3732    (gnus-thread-header h1) (gnus-thread-header h2)))
3733
3734 (defsubst gnus-article-sort-by-lines (h1 h2)
3735   "Sort articles by article Lines header."
3736   (< (mail-header-lines h1)
3737      (mail-header-lines h2)))
3738
3739 (defun gnus-thread-sort-by-lines (h1 h2)
3740   "Sort threads by root article Lines header."
3741   (gnus-article-sort-by-lines
3742    (gnus-thread-header h1) (gnus-thread-header h2)))
3743
3744 (defsubst gnus-article-sort-by-chars (h1 h2)
3745   "Sort articles by octet length."
3746   (< (mail-header-chars h1)
3747      (mail-header-chars h2)))
3748
3749 (defun gnus-thread-sort-by-chars (h1 h2)
3750   "Sort threads by root article octet length."
3751   (gnus-article-sort-by-chars
3752    (gnus-thread-header h1) (gnus-thread-header h2)))
3753
3754 (defsubst gnus-article-sort-by-author (h1 h2)
3755   "Sort articles by root author."
3756   (string-lessp
3757    (let ((addr (car (mime-entity-read-field h1 'From))))
3758      (or (std11-full-name-string addr)
3759          (std11-address-string addr)
3760          ""))
3761    (let ((addr (car (mime-entity-read-field h2 'From))))
3762      (or (std11-full-name-string addr)
3763          (std11-address-string addr)
3764          ""))
3765    ))
3766
3767 (defun gnus-thread-sort-by-author (h1 h2)
3768   "Sort threads by root author."
3769   (gnus-article-sort-by-author
3770    (gnus-thread-header h1)  (gnus-thread-header h2)))
3771
3772 (defsubst gnus-article-sort-by-subject (h1 h2)
3773   "Sort articles by root subject."
3774   (string-lessp
3775    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3776    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3777
3778 (defun gnus-thread-sort-by-subject (h1 h2)
3779   "Sort threads by root subject."
3780   (gnus-article-sort-by-subject
3781    (gnus-thread-header h1) (gnus-thread-header h2)))
3782
3783 (defsubst gnus-article-sort-by-date (h1 h2)
3784   "Sort articles by root article date."
3785   (time-less-p
3786    (gnus-date-get-time (mail-header-date h1))
3787    (gnus-date-get-time (mail-header-date h2))))
3788
3789 (defun gnus-thread-sort-by-date (h1 h2)
3790   "Sort threads by root article date."
3791   (gnus-article-sort-by-date
3792    (gnus-thread-header h1) (gnus-thread-header h2)))
3793
3794 (defsubst gnus-article-sort-by-score (h1 h2)
3795   "Sort articles by root article score.
3796 Unscored articles will be counted as having a score of zero."
3797   (> (or (cdr (assq (mail-header-number h1)
3798                     gnus-newsgroup-scored))
3799          gnus-summary-default-score 0)
3800      (or (cdr (assq (mail-header-number h2)
3801                     gnus-newsgroup-scored))
3802          gnus-summary-default-score 0)))
3803
3804 (defun gnus-thread-sort-by-score (h1 h2)
3805   "Sort threads by root article score."
3806   (gnus-article-sort-by-score
3807    (gnus-thread-header h1) (gnus-thread-header h2)))
3808
3809 (defun gnus-thread-sort-by-total-score (h1 h2)
3810   "Sort threads by the sum of all scores in the thread.
3811 Unscored articles will be counted as having a score of zero."
3812   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3813
3814 (defun gnus-thread-total-score (thread)
3815   ;; This function find the total score of THREAD.
3816   (cond ((null thread)
3817          0)
3818         ((consp thread)
3819          (if (stringp (car thread))
3820              (apply gnus-thread-score-function 0
3821                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3822            (gnus-thread-total-score-1 thread)))
3823         (t
3824          (gnus-thread-total-score-1 (list thread)))))
3825
3826 (defun gnus-thread-total-score-1 (root)
3827   ;; This function find the total score of the thread below ROOT.
3828   (setq root (car root))
3829   (apply gnus-thread-score-function
3830          (or (append
3831               (mapcar 'gnus-thread-total-score
3832                       (cdr (gnus-id-to-thread (mail-header-id root))))
3833               (when (> (mail-header-number root) 0)
3834                 (list (or (cdr (assq (mail-header-number root)
3835                                      gnus-newsgroup-scored))
3836                           gnus-summary-default-score 0))))
3837              (list gnus-summary-default-score)
3838              '(0))))
3839
3840 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3841 (defvar gnus-tmp-prev-subject nil)
3842 (defvar gnus-tmp-false-parent nil)
3843 (defvar gnus-tmp-root-expunged nil)
3844 (defvar gnus-tmp-dummy-line nil)
3845
3846 (defvar gnus-tmp-header)
3847 (defun gnus-extra-header (type &optional header)
3848   "Return the extra header of TYPE."
3849   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3850       ""))
3851
3852 (defun gnus-summary-prepare-threads (threads)
3853   "Prepare summary buffer from THREADS and indentation LEVEL.
3854 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3855 or a straight list of headers."
3856   (gnus-message 7 "Generating summary...")
3857
3858   (setq gnus-newsgroup-threads threads)
3859   (beginning-of-line)
3860
3861   (let ((gnus-tmp-level 0)
3862         (default-score (or gnus-summary-default-score 0))
3863         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3864         thread number subject stack state gnus-tmp-gathered beg-match
3865         new-roots gnus-tmp-new-adopts thread-end
3866         gnus-tmp-header gnus-tmp-unread
3867         gnus-tmp-replied gnus-tmp-subject-or-nil
3868         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3869         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3870         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3871
3872     (setq gnus-tmp-prev-subject nil)
3873
3874     (if (vectorp (car threads))
3875         ;; If this is a straight (sic) list of headers, then a
3876         ;; threaded summary display isn't required, so we just create
3877         ;; an unthreaded one.
3878         (gnus-summary-prepare-unthreaded threads)
3879
3880       ;; Do the threaded display.
3881
3882       (while (or threads stack gnus-tmp-new-adopts new-roots)
3883
3884         (if (and (= gnus-tmp-level 0)
3885                  (or (not stack)
3886                      (= (caar stack) 0))
3887                  (not gnus-tmp-false-parent)
3888                  (or gnus-tmp-new-adopts new-roots))
3889             (if gnus-tmp-new-adopts
3890                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3891                       thread (list (car gnus-tmp-new-adopts))
3892                       gnus-tmp-header (caar thread)
3893                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3894               (when new-roots
3895                 (setq thread (list (car new-roots))
3896                       gnus-tmp-header (caar thread)
3897                       new-roots (cdr new-roots))))
3898
3899           (if threads
3900               ;; If there are some threads, we do them before the
3901               ;; threads on the stack.
3902               (setq thread threads
3903                     gnus-tmp-header (caar thread))
3904             ;; There were no current threads, so we pop something off
3905             ;; the stack.
3906             (setq state (car stack)
3907                   gnus-tmp-level (car state)
3908                   thread (cdr state)
3909                   stack (cdr stack)
3910                   gnus-tmp-header (caar thread))))
3911
3912         (setq gnus-tmp-false-parent nil)
3913         (setq gnus-tmp-root-expunged nil)
3914         (setq thread-end nil)
3915
3916         (if (stringp gnus-tmp-header)
3917             ;; The header is a dummy root.
3918             (cond
3919              ((eq gnus-summary-make-false-root 'adopt)
3920               ;; We let the first article adopt the rest.
3921               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3922                                                (cddar thread)))
3923               (setq gnus-tmp-gathered
3924                     (nconc (mapcar
3925                             (lambda (h) (mail-header-number (car h)))
3926                             (cddar thread))
3927                            gnus-tmp-gathered))
3928               (setq thread (cons (list (caar thread)
3929                                        (cadar thread))
3930                                  (cdr thread)))
3931               (setq gnus-tmp-level -1
3932                     gnus-tmp-false-parent t))
3933              ((eq gnus-summary-make-false-root 'empty)
3934               ;; We print adopted articles with empty subject fields.
3935               (setq gnus-tmp-gathered
3936                     (nconc (mapcar
3937                             (lambda (h) (mail-header-number (car h)))
3938                             (cddar thread))
3939                            gnus-tmp-gathered))
3940               (setq gnus-tmp-level -1))
3941              ((eq gnus-summary-make-false-root 'dummy)
3942               ;; We remember that we probably want to output a dummy
3943               ;; root.
3944               (setq gnus-tmp-dummy-line gnus-tmp-header)
3945               (setq gnus-tmp-prev-subject gnus-tmp-header))
3946              (t
3947               ;; We do not make a root for the gathered
3948               ;; sub-threads at all.
3949               (setq gnus-tmp-level -1)))
3950
3951           (setq number (mail-header-number gnus-tmp-header)
3952                 subject (mail-header-subject gnus-tmp-header))
3953
3954           (cond
3955            ;; If the thread has changed subject, we might want to make
3956            ;; this subthread into a root.
3957            ((and (null gnus-thread-ignore-subject)
3958                  (not (zerop gnus-tmp-level))
3959                  gnus-tmp-prev-subject
3960                  (not (inline
3961                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3962             (setq new-roots (nconc new-roots (list (car thread)))
3963                   thread-end t
3964                   gnus-tmp-header nil))
3965            ;; If the article lies outside the current limit,
3966            ;; then we do not display it.
3967            ((not (memq number gnus-newsgroup-limit))
3968             (setq gnus-tmp-gathered
3969                   (nconc (mapcar
3970                           (lambda (h) (mail-header-number (car h)))
3971                           (cdar thread))
3972                          gnus-tmp-gathered))
3973             (setq gnus-tmp-new-adopts (if (cdar thread)
3974                                           (append gnus-tmp-new-adopts
3975                                                   (cdar thread))
3976                                         gnus-tmp-new-adopts)
3977                   thread-end t
3978                   gnus-tmp-header nil)
3979             (when (zerop gnus-tmp-level)
3980               (setq gnus-tmp-root-expunged t)))
3981            ;; Perhaps this article is to be marked as read?
3982            ((and gnus-summary-mark-below
3983                  (< (or (cdr (assq number gnus-newsgroup-scored))
3984                         default-score)
3985                     gnus-summary-mark-below)
3986                  ;; Don't touch sparse articles.
3987                  (not (gnus-summary-article-sparse-p number))
3988                  (not (gnus-summary-article-ancient-p number)))
3989             (setq gnus-newsgroup-unreads
3990                   (delq number gnus-newsgroup-unreads))
3991             (if gnus-newsgroup-auto-expire
3992                 (push number gnus-newsgroup-expirable)
3993               (push (cons number gnus-low-score-mark)
3994                     gnus-newsgroup-reads))))
3995
3996           (when gnus-tmp-header
3997             ;; We may have an old dummy line to output before this
3998             ;; article.
3999             (when (and gnus-tmp-dummy-line
4000                        (gnus-subject-equal
4001                         gnus-tmp-dummy-line
4002                         (mail-header-subject gnus-tmp-header)))
4003               (gnus-summary-insert-dummy-line
4004                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4005               (setq gnus-tmp-dummy-line nil))
4006
4007             ;; Compute the mark.
4008             (setq gnus-tmp-unread (gnus-article-mark number))
4009
4010             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4011                                   gnus-tmp-header gnus-tmp-level)
4012                   gnus-newsgroup-data)
4013
4014             ;; Actually insert the line.
4015             (setq
4016              gnus-tmp-subject-or-nil
4017              (cond
4018               ((and gnus-thread-ignore-subject
4019                     gnus-tmp-prev-subject
4020                     (not (inline (gnus-subject-equal
4021                                   gnus-tmp-prev-subject subject))))
4022                subject)
4023               ((zerop gnus-tmp-level)
4024                (if (and (eq gnus-summary-make-false-root 'empty)
4025                         (memq number gnus-tmp-gathered)
4026                         gnus-tmp-prev-subject
4027                         (inline (gnus-subject-equal
4028                                  gnus-tmp-prev-subject subject)))
4029                    gnus-summary-same-subject
4030                  subject))
4031               (t gnus-summary-same-subject)))
4032             (if (and (eq gnus-summary-make-false-root 'adopt)
4033                      (= gnus-tmp-level 1)
4034                      (memq number gnus-tmp-gathered))
4035                 (setq gnus-tmp-opening-bracket ?\<
4036                       gnus-tmp-closing-bracket ?\>)
4037               (setq gnus-tmp-opening-bracket ?\[
4038                     gnus-tmp-closing-bracket ?\]))
4039             (setq
4040              gnus-tmp-indentation
4041              (aref gnus-thread-indent-array gnus-tmp-level)
4042              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4043              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4044                                 gnus-summary-default-score 0)
4045              gnus-tmp-score-char
4046              (if (or (null gnus-summary-default-score)
4047                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4048                          gnus-summary-zcore-fuzz))
4049                  ?  ;Whitespace
4050                (if (< gnus-tmp-score gnus-summary-default-score)
4051                    gnus-score-below-mark gnus-score-over-mark))
4052              gnus-tmp-replied
4053              (cond ((memq number gnus-newsgroup-processable)
4054                     gnus-process-mark)
4055                    ((memq number gnus-newsgroup-cached)
4056                     gnus-cached-mark)
4057                    ((memq number gnus-newsgroup-replied)
4058                     gnus-replied-mark)
4059                    ((memq number gnus-newsgroup-saved)
4060                     gnus-saved-mark)
4061                    (t gnus-unread-mark))
4062              gnus-tmp-from (mail-header-from gnus-tmp-header)
4063              gnus-tmp-name
4064              (cond
4065               ((string-match "<[^>]+> *$" gnus-tmp-from)
4066                (setq beg-match (match-beginning 0))
4067                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4068                         (substring gnus-tmp-from (1+ (match-beginning 0))
4069                                    (1- (match-end 0))))
4070                    (substring gnus-tmp-from 0 beg-match)))
4071               ((string-match "(.+)" gnus-tmp-from)
4072                (substring gnus-tmp-from
4073                           (1+ (match-beginning 0)) (1- (match-end 0))))
4074               (t gnus-tmp-from)))
4075             (when (string= gnus-tmp-name "")
4076               (setq gnus-tmp-name gnus-tmp-from))
4077             (unless (numberp gnus-tmp-lines)
4078               (setq gnus-tmp-lines 0))
4079             (gnus-put-text-property
4080              (point)
4081              (progn (eval gnus-summary-line-format-spec) (point))
4082              'gnus-number number)
4083             (when gnus-visual-p
4084               (forward-line -1)
4085               (gnus-run-hooks 'gnus-summary-update-hook)
4086               (forward-line 1))
4087
4088             (setq gnus-tmp-prev-subject subject)))
4089
4090         (when (nth 1 thread)
4091           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4092         (incf gnus-tmp-level)
4093         (setq threads (if thread-end nil (cdar thread)))
4094         (unless threads
4095           (setq gnus-tmp-level 0)))))
4096   (gnus-message 7 "Generating summary...done"))
4097
4098 (defun gnus-summary-prepare-unthreaded (headers)
4099   "Generate an unthreaded summary buffer based on HEADERS."
4100   (let (header number mark)
4101
4102     (beginning-of-line)
4103
4104     (while headers
4105       ;; We may have to root out some bad articles...
4106       (when (memq (setq number (mail-header-number
4107                                 (setq header (pop headers))))
4108                   gnus-newsgroup-limit)
4109         ;; Mark article as read when it has a low score.
4110         (when (and gnus-summary-mark-below
4111                    (< (or (cdr (assq number gnus-newsgroup-scored))
4112                           gnus-summary-default-score 0)
4113                       gnus-summary-mark-below)
4114                    (not (gnus-summary-article-ancient-p number)))
4115           (setq gnus-newsgroup-unreads
4116                 (delq number gnus-newsgroup-unreads))
4117           (if gnus-newsgroup-auto-expire
4118               (push number gnus-newsgroup-expirable)
4119             (push (cons number gnus-low-score-mark)
4120                   gnus-newsgroup-reads)))
4121
4122         (setq mark (gnus-article-mark number))
4123         (push (gnus-data-make number mark (1+ (point)) header 0)
4124               gnus-newsgroup-data)
4125         (gnus-summary-insert-line
4126          header 0 number
4127          mark (memq number gnus-newsgroup-replied)
4128          (memq number gnus-newsgroup-expirable)
4129          (mail-header-subject header) nil
4130          (cdr (assq number gnus-newsgroup-scored))
4131          (memq number gnus-newsgroup-processable))))))
4132
4133 (defun gnus-summary-remove-list-identifiers ()
4134   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4135   (let ((regexp (if (stringp gnus-list-identifiers)
4136                     gnus-list-identifiers
4137                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4138     (dolist (header gnus-newsgroup-headers)
4139       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4140                                   " *\\)\\)+\\(Re: +\\)?\\)")
4141                           (mail-header-subject header))
4142         (mail-header-set-subject
4143          header (concat (substring (mail-header-subject header)
4144                                    0 (match-beginning 1))
4145                         (or
4146                          (match-string 3 (mail-header-subject header))
4147                          (match-string 5 (mail-header-subject header)))
4148                         (substring (mail-header-subject header)
4149                                    (match-end 1))))))))
4150
4151 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4152   "Select newsgroup GROUP.
4153 If READ-ALL is non-nil, all articles in the group are selected.
4154 If SELECT-ARTICLES, only select those articles from GROUP."
4155   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4156          ;;!!! Dirty hack; should be removed.
4157          (gnus-summary-ignore-duplicates
4158           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4159               t
4160             gnus-summary-ignore-duplicates))
4161          (info (nth 2 entry))
4162          articles fetched-articles cached)
4163
4164     (unless (gnus-check-server
4165              (setq gnus-current-select-method
4166                    (gnus-find-method-for-group group)))
4167       (error "Couldn't open server"))
4168
4169     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4170         (gnus-activate-group group)     ; Or we can activate it...
4171         (progn                          ; Or we bug out.
4172           (when (equal major-mode 'gnus-summary-mode)
4173             (kill-buffer (current-buffer)))
4174           (error "Couldn't request group %s: %s"
4175                  group (gnus-status-message group))))
4176
4177     (unless (gnus-request-group group t)
4178       (when (equal major-mode 'gnus-summary-mode)
4179         (kill-buffer (current-buffer)))
4180       (error "Couldn't request group %s: %s"
4181              group (gnus-status-message group)))
4182
4183     (setq gnus-newsgroup-name group)
4184     (setq gnus-newsgroup-unselected nil)
4185     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4186     (gnus-summary-setup-default-charset)
4187
4188     ;; Adjust and set lists of article marks.
4189     (when info
4190       (gnus-adjust-marked-articles info))
4191
4192     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4193     (setq cached
4194           (if (gnus-virtual-group-p group)
4195               gnus-newsgroup-cached
4196             (gnus-cache-articles-in-group group)))
4197
4198     (setq gnus-newsgroup-unreads
4199           (gnus-set-difference
4200            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4201            gnus-newsgroup-dormant))
4202
4203     (setq gnus-newsgroup-processable nil)
4204
4205     (gnus-update-read-articles group gnus-newsgroup-unreads)
4206
4207     (if (setq articles select-articles)
4208         (setq gnus-newsgroup-unselected
4209               (gnus-sorted-intersection
4210                gnus-newsgroup-unreads
4211                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4212       (setq articles (gnus-articles-to-read group read-all)))
4213
4214     (cond
4215      ((null articles)
4216       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4217       'quit)
4218      ((eq articles 0) nil)
4219      (t
4220       ;; Init the dependencies hash table.
4221       (setq gnus-newsgroup-dependencies
4222             (gnus-make-hashtable (length articles)))
4223       (gnus-set-global-variables)
4224       ;; Retrieve the headers and read them in.
4225       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4226       (setq gnus-newsgroup-headers
4227             (gnus-retrieve-parsed-headers
4228              articles gnus-newsgroup-name
4229              ;; We might want to fetch old headers, but
4230              ;; not if there is only 1 article.
4231              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4232                            (not (numberp gnus-fetch-old-headers)))
4233                       (> (length articles) 1))
4234                   gnus-fetch-old-headers)))
4235       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4236
4237       ;; Suppress duplicates?
4238       (when gnus-suppress-duplicates
4239         (gnus-dup-suppress-articles))
4240
4241       ;; Set the initial limit.
4242       (setq gnus-newsgroup-limit (copy-sequence articles))
4243       ;; Remove canceled articles from the list of unread articles.
4244       (setq gnus-newsgroup-unreads
4245             (gnus-set-sorted-intersection
4246              gnus-newsgroup-unreads
4247              (setq fetched-articles
4248                    (mapcar (lambda (headers) (mail-header-number headers))
4249                            gnus-newsgroup-headers))))
4250       ;; Removed marked articles that do not exist.
4251       (gnus-update-missing-marks
4252        (gnus-sorted-complement fetched-articles articles))
4253
4254       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4255       (when cached
4256         (setq gnus-newsgroup-cached cached))
4257
4258       ;; We might want to build some more threads first.
4259       (when (and gnus-fetch-old-headers
4260                  (eq gnus-headers-retrieved-by 'nov))
4261         (if (eq gnus-fetch-old-headers 'invisible)
4262             (gnus-build-all-threads)
4263           (gnus-build-old-threads)))
4264       ;; Let the Gnus agent mark articles as read.
4265       (when gnus-agent
4266         (gnus-agent-get-undownloaded-list))
4267       ;; Remove list identifiers from subject
4268       (when gnus-list-identifiers
4269         (gnus-summary-remove-list-identifiers))
4270       ;; Check whether auto-expire is to be done in this group.
4271       (setq gnus-newsgroup-auto-expire
4272             (gnus-group-auto-expirable-p group))
4273       ;; Set up the article buffer now, if necessary.
4274       (unless gnus-single-article-buffer
4275         (gnus-article-setup-buffer))
4276       ;; First and last article in this newsgroup.
4277       (when gnus-newsgroup-headers
4278         (setq gnus-newsgroup-begin
4279               (mail-header-number (car gnus-newsgroup-headers))
4280               gnus-newsgroup-end
4281               (mail-header-number
4282                (gnus-last-element gnus-newsgroup-headers))))
4283       ;; GROUP is successfully selected.
4284       (or gnus-newsgroup-headers t)))))
4285
4286 (defun gnus-articles-to-read (group &optional read-all)
4287   "Find out what articles the user wants to read."
4288   (let* ((articles
4289           ;; Select all articles if `read-all' is non-nil, or if there
4290           ;; are no unread articles.
4291           (if (or read-all
4292                   (and (zerop (length gnus-newsgroup-marked))
4293                        (zerop (length gnus-newsgroup-unreads)))
4294                   (eq (gnus-group-find-parameter group 'display)
4295                       'all))
4296               (or
4297                (gnus-uncompress-range (gnus-active group))
4298                (gnus-cache-articles-in-group group))
4299             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4300                           (copy-sequence gnus-newsgroup-unreads))
4301                   '<)))
4302          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4303          (scored (length scored-list))
4304          (number (length articles))
4305          (marked (+ (length gnus-newsgroup-marked)
4306                     (length gnus-newsgroup-dormant)))
4307          (select
4308           (cond
4309            ((numberp read-all)
4310             read-all)
4311            (t
4312             (condition-case ()
4313                 (cond
4314                  ((and (or (<= scored marked) (= scored number))
4315                        (natnump gnus-large-newsgroup)
4316                        (> number gnus-large-newsgroup))
4317                   (let* ((cursor-in-echo-area nil)
4318                          (input (read-from-minibuffer
4319                                  (format
4320                                   "How many articles from %s (max %d): "
4321                                   (gnus-limit-string gnus-newsgroup-name 35)
4322                                   number)
4323                                  (cons (number-to-string gnus-large-newsgroup)
4324                                        0))))
4325                     (if (string-match "^[ \t]*$" input)
4326                         number
4327                       input)))
4328                  ((and (> scored marked) (< scored number)
4329                        (> (- scored number) 20))
4330                   (let ((input
4331                          (read-string
4332                           (format "%s %s (%d scored, %d total): "
4333                                   "How many articles from"
4334                                   group scored number))))
4335                     (if (string-match "^[ \t]*$" input)
4336                         number input)))
4337                  (t number))
4338               (quit
4339                (message "Quit getting the articles to read")
4340                nil))))))
4341     (setq select (if (stringp select) (string-to-number select) select))
4342     (if (or (null select) (zerop select))
4343         select
4344       (if (and (not (zerop scored)) (<= (abs select) scored))
4345           (progn
4346             (setq articles (sort scored-list '<))
4347             (setq number (length articles)))
4348         (setq articles (copy-sequence articles)))
4349
4350       (when (< (abs select) number)
4351         (if (< select 0)
4352             ;; Select the N oldest articles.
4353             (setcdr (nthcdr (1- (abs select)) articles) nil)
4354           ;; Select the N most recent articles.
4355           (setq articles (nthcdr (- number select) articles))))
4356       (setq gnus-newsgroup-unselected
4357             (gnus-sorted-intersection
4358              gnus-newsgroup-unreads
4359              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4360       (when gnus-alter-articles-to-read-function
4361         (setq gnus-newsgroup-unreads
4362               (sort
4363                (funcall gnus-alter-articles-to-read-function
4364                         gnus-newsgroup-name gnus-newsgroup-unreads)
4365                '<)))
4366       articles)))
4367
4368 (defun gnus-killed-articles (killed articles)
4369   (let (out)
4370     (while articles
4371       (when (inline (gnus-member-of-range (car articles) killed))
4372         (push (car articles) out))
4373       (setq articles (cdr articles)))
4374     out))
4375
4376 (defun gnus-uncompress-marks (marks)
4377   "Uncompress the mark ranges in MARKS."
4378   (let ((uncompressed '(score bookmark))
4379         out)
4380     (while marks
4381       (if (memq (caar marks) uncompressed)
4382           (push (car marks) out)
4383         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4384       (setq marks (cdr marks)))
4385     out))
4386
4387 (defun gnus-adjust-marked-articles (info)
4388   "Set all article lists and remove all marks that are no longer valid."
4389   (let* ((marked-lists (gnus-info-marks info))
4390          (active (gnus-active (gnus-info-group info)))
4391          (min (car active))
4392          (max (cdr active))
4393          (types gnus-article-mark-lists)
4394          (uncompressed '(score bookmark killed))
4395          marks var articles article mark)
4396
4397     (while marked-lists
4398       (setq marks (pop marked-lists))
4399       (set (setq var (intern (format "gnus-newsgroup-%s"
4400                                      (car (rassq (setq mark (car marks))
4401                                                  types)))))
4402            (if (memq (car marks) uncompressed) (cdr marks)
4403              (gnus-uncompress-range (cdr marks))))
4404
4405       (setq articles (symbol-value var))
4406
4407       ;; All articles have to be subsets of the active articles.
4408       (cond
4409        ;; Adjust "simple" lists.
4410        ((memq mark '(tick dormant expire reply save))
4411         (while articles
4412           (when (or (< (setq article (pop articles)) min) (> article max))
4413             (set var (delq article (symbol-value var))))))
4414        ;; Adjust assocs.
4415        ((memq mark uncompressed)
4416         (when (not (listp (cdr (symbol-value var))))
4417           (set var (list (symbol-value var))))
4418         (when (not (listp (cdr articles)))
4419           (setq articles (list articles)))
4420         (while articles
4421           (when (or (not (consp (setq article (pop articles))))
4422                     (< (car article) min)
4423                     (> (car article) max))
4424             (set var (delq article (symbol-value var))))))))))
4425
4426 (defun gnus-update-missing-marks (missing)
4427   "Go through the list of MISSING articles and remove them from the mark lists."
4428   (when missing
4429     (let ((types gnus-article-mark-lists)
4430           var m)
4431       ;; Go through all types.
4432       (while types
4433         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4434         (when (symbol-value var)
4435           ;; This list has articles.  So we delete all missing articles
4436           ;; from it.
4437           (setq m missing)
4438           (while m
4439             (set var (delq (pop m) (symbol-value var)))))))))
4440
4441 (defun gnus-update-marks ()
4442   "Enter the various lists of marked articles into the newsgroup info list."
4443   (let ((types gnus-article-mark-lists)
4444         (info (gnus-get-info gnus-newsgroup-name))
4445         (uncompressed '(score bookmark killed))
4446         type list newmarked symbol delta-marks)
4447     (when info
4448       ;; Add all marks lists to the list of marks lists.
4449       (while (setq type (pop types))
4450         (setq list (symbol-value
4451                     (setq symbol
4452                           (intern (format "gnus-newsgroup-%s"
4453                                           (car type))))))
4454
4455         (when list
4456           ;; Get rid of the entries of the articles that have the
4457           ;; default score.
4458           (when (and (eq (cdr type) 'score)
4459                      gnus-save-score
4460                      list)
4461             (let* ((arts list)
4462                    (prev (cons nil list))
4463                    (all prev))
4464               (while arts
4465                 (if (or (not (consp (car arts)))
4466                         (= (cdar arts) gnus-summary-default-score))
4467                     (setcdr prev (cdr arts))
4468                   (setq prev arts))
4469                 (setq arts (cdr arts)))
4470               (setq list (cdr all)))))
4471
4472         (unless (memq (cdr type) uncompressed)
4473           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4474
4475         (when (gnus-check-backend-function
4476                'request-set-mark gnus-newsgroup-name)
4477           ;; propagate flags to server, with the following exceptions:
4478           ;; uncompressed:s are not proper flags (they are cons cells)
4479           ;; cache is a internal gnus flag
4480           ;; download are local to one gnus installation (well)
4481           ;; unsend are for nndraft groups only
4482           ;; xxx: generality of this?  this suits nnimap anyway
4483           (unless (memq (cdr type) (append '(cache download unsend)
4484                                            uncompressed))
4485             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4486                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4487                    (add (gnus-remove-from-range
4488                          (gnus-copy-sequence list) old)))
4489               (when add
4490                 (push (list add 'add (list (cdr type))) delta-marks))
4491               (when del
4492                 (push (list del 'del (list (cdr type))) delta-marks)))))
4493
4494         (when list
4495           (push (cons (cdr type) list) newmarked)))
4496
4497       (when delta-marks
4498         (unless (gnus-check-group gnus-newsgroup-name)
4499           (error "Can't open server for %s" gnus-newsgroup-name))
4500         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4501
4502       ;; Enter these new marks into the info of the group.
4503       (if (nthcdr 3 info)
4504           (setcar (nthcdr 3 info) newmarked)
4505         ;; Add the marks lists to the end of the info.
4506         (when newmarked
4507           (setcdr (nthcdr 2 info) (list newmarked))))
4508
4509       ;; Cut off the end of the info if there's nothing else there.
4510       (let ((i 5))
4511         (while (and (> i 2)
4512                     (not (nth i info)))
4513           (when (nthcdr (decf i) info)
4514             (setcdr (nthcdr i info) nil)))))))
4515
4516 (defun gnus-set-mode-line (where)
4517   "Set the mode line of the article or summary buffers.
4518 If WHERE is `summary', the summary mode line format will be used."
4519   ;; Is this mode line one we keep updated?
4520   (when (and (memq where gnus-updated-mode-lines)
4521              (symbol-value
4522               (intern (format "gnus-%s-mode-line-format-spec" where))))
4523     (let (mode-string)
4524       (save-excursion
4525         ;; We evaluate this in the summary buffer since these
4526         ;; variables are buffer-local to that buffer.
4527         (set-buffer gnus-summary-buffer)
4528         ;; We bind all these variables that are used in the `eval' form
4529         ;; below.
4530         (let* ((mformat (symbol-value
4531                          (intern
4532                           (format "gnus-%s-mode-line-format-spec" where))))
4533                (gnus-tmp-group-name (gnus-group-name-decode
4534                                      gnus-newsgroup-name
4535                                      (gnus-group-name-charset
4536                                       nil
4537                                       gnus-newsgroup-name)))
4538                (gnus-tmp-article-number (or gnus-current-article 0))
4539                (gnus-tmp-unread gnus-newsgroup-unreads)
4540                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4541                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4542                (gnus-tmp-unread-and-unselected
4543                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4544                             (zerop gnus-tmp-unselected))
4545                        "")
4546                       ((zerop gnus-tmp-unselected)
4547                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4548                       (t (format "{%d(+%d) more}"
4549                                  gnus-tmp-unread-and-unticked
4550                                  gnus-tmp-unselected))))
4551                (gnus-tmp-subject
4552                 (if (and gnus-current-headers
4553                          (vectorp gnus-current-headers))
4554                     (gnus-mode-string-quote
4555                      (mail-header-subject gnus-current-headers))
4556                   ""))
4557                bufname-length max-len
4558                gnus-tmp-header);; passed as argument to any user-format-funcs
4559           (setq mode-string (eval mformat))
4560           (setq bufname-length (if (string-match "%b" mode-string)
4561                                    (- (length
4562                                        (buffer-name
4563                                         (if (eq where 'summary)
4564                                             nil
4565                                           (get-buffer gnus-article-buffer))))
4566                                       2)
4567                                  0))
4568           (setq max-len (max 4 (if gnus-mode-non-string-length
4569                                    (- (window-width)
4570                                       gnus-mode-non-string-length
4571                                       bufname-length)
4572                                  (length mode-string))))
4573           ;; We might have to chop a bit of the string off...
4574           (when (> (length mode-string) max-len)
4575             (setq mode-string
4576                   (concat (gnus-truncate-string mode-string (- max-len 3))
4577                           "...")))
4578           ;; Pad the mode string a bit.
4579           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4580       ;; Update the mode line.
4581       (setq mode-line-buffer-identification
4582             (gnus-mode-line-buffer-identification (list mode-string)))
4583       (set-buffer-modified-p t))))
4584
4585 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4586   "Go through the HEADERS list and add all Xrefs to a hash table.
4587 The resulting hash table is returned, or nil if no Xrefs were found."
4588   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4589          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4590          (xref-hashtb (gnus-make-hashtable))
4591          start group entry number xrefs header)
4592     (while headers
4593       (setq header (pop headers))
4594       (when (and (setq xrefs (mail-header-xref header))
4595                  (not (memq (setq number (mail-header-number header))
4596                             unreads)))
4597         (setq start 0)
4598         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4599           (setq start (match-end 0))
4600           (setq group (if prefix
4601                           (concat prefix (substring xrefs (match-beginning 1)
4602                                                     (match-end 1)))
4603                         (substring xrefs (match-beginning 1) (match-end 1))))
4604           (setq number
4605                 (string-to-int (substring xrefs (match-beginning 2)
4606                                           (match-end 2))))
4607           (if (setq entry (gnus-gethash group xref-hashtb))
4608               (setcdr entry (cons number (cdr entry)))
4609             (gnus-sethash group (cons number nil) xref-hashtb)))))
4610     (and start xref-hashtb)))
4611
4612 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4613   "Look through all the headers and mark the Xrefs as read."
4614   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4615         name entry info xref-hashtb idlist method nth4)
4616     (save-excursion
4617       (set-buffer gnus-group-buffer)
4618       (when (setq xref-hashtb
4619                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4620         (mapatoms
4621          (lambda (group)
4622            (unless (string= from-newsgroup (setq name (symbol-name group)))
4623              (setq idlist (symbol-value group))
4624              ;; Dead groups are not updated.
4625              (and (prog1
4626                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4627                             info (nth 2 entry))
4628                     (when (stringp (setq nth4 (gnus-info-method info)))
4629                       (setq nth4 (gnus-server-to-method nth4))))
4630                   ;; Only do the xrefs if the group has the same
4631                   ;; select method as the group we have just read.
4632                   (or (gnus-methods-equal-p
4633                        nth4 (gnus-find-method-for-group from-newsgroup))
4634                       virtual
4635                       (equal nth4 (setq method (gnus-find-method-for-group
4636                                                 from-newsgroup)))
4637                       (and (equal (car nth4) (car method))
4638                            (equal (nth 1 nth4) (nth 1 method))))
4639                   gnus-use-cross-reference
4640                   (or (not (eq gnus-use-cross-reference t))
4641                       virtual
4642                       ;; Only do cross-references on subscribed
4643                       ;; groups, if that is what is wanted.
4644                       (<= (gnus-info-level info) gnus-level-subscribed))
4645                   (gnus-group-make-articles-read name idlist))))
4646          xref-hashtb)))))
4647
4648 (defun gnus-compute-read-articles (group articles)
4649   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4650          (info (nth 2 entry))
4651          (active (gnus-active group))
4652          ninfo)
4653     (when entry
4654       ;; First peel off all invalid article numbers.
4655       (when active
4656         (let ((ids articles)
4657               id first)
4658           (while (setq id (pop ids))
4659             (when (and first (> id (cdr active)))
4660               ;; We'll end up in this situation in one particular
4661               ;; obscure situation.  If you re-scan a group and get
4662               ;; a new article that is cross-posted to a different
4663               ;; group that has not been re-scanned, you might get
4664               ;; crossposted article that has a higher number than
4665               ;; Gnus believes possible.  So we re-activate this
4666               ;; group as well.  This might mean doing the
4667               ;; crossposting thingy will *increase* the number
4668               ;; of articles in some groups.  Tsk, tsk.
4669               (setq active (or (gnus-activate-group group) active)))
4670             (when (or (> id (cdr active))
4671                       (< id (car active)))
4672               (setq articles (delq id articles))))))
4673       ;; If the read list is nil, we init it.
4674       (if (and active
4675                (null (gnus-info-read info))
4676                (> (car active) 1))
4677           (setq ninfo (cons 1 (1- (car active))))
4678         (setq ninfo (gnus-info-read info)))
4679       ;; Then we add the read articles to the range.
4680       (gnus-add-to-range
4681        ninfo (setq articles (sort articles '<))))))
4682
4683 (defun gnus-group-make-articles-read (group articles)
4684   "Update the info of GROUP to say that ARTICLES are read."
4685   (let* ((num 0)
4686          (entry (gnus-gethash group gnus-newsrc-hashtb))
4687          (info (nth 2 entry))
4688          (active (gnus-active group))
4689          range)
4690     (when entry
4691       (setq range (gnus-compute-read-articles group articles))
4692       (save-excursion
4693         (set-buffer gnus-group-buffer)
4694         (gnus-undo-register
4695           `(progn
4696              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4697              (gnus-info-set-read ',info ',(gnus-info-read info))
4698              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4699              (gnus-group-update-group ,group t))))
4700       ;; Add the read articles to the range.
4701       (gnus-info-set-read info range)
4702       ;; Then we have to re-compute how many unread
4703       ;; articles there are in this group.
4704       (when active
4705         (cond
4706          ((not range)
4707           (setq num (- (1+ (cdr active)) (car active))))
4708          ((not (listp (cdr range)))
4709           (setq num (- (cdr active) (- (1+ (cdr range))
4710                                        (car range)))))
4711          (t
4712           (while range
4713             (if (numberp (car range))
4714                 (setq num (1+ num))
4715               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4716             (setq range (cdr range)))
4717           (setq num (- (cdr active) num))))
4718         ;; Update the number of unread articles.
4719         (setcar entry num)
4720         ;; Update the group buffer.
4721         (gnus-group-update-group group t)))))
4722
4723 (defvar gnus-newsgroup-none-id 0)
4724
4725 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4726   (let ((cur nntp-server-buffer)
4727         (dependencies
4728          (or dependencies
4729              (save-excursion (set-buffer gnus-summary-buffer)
4730                              gnus-newsgroup-dependencies)))
4731         headers id end ref
4732         (mail-parse-charset gnus-newsgroup-charset)
4733         (mail-parse-ignored-charsets
4734          (save-excursion (condition-case nil
4735                              (set-buffer gnus-summary-buffer)
4736                            (error))
4737                          gnus-newsgroup-ignored-charsets)))
4738     (save-excursion
4739       (set-buffer nntp-server-buffer)
4740       ;; Translate all TAB characters into SPACE characters.
4741       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4742       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4743       (gnus-run-hooks 'gnus-parse-headers-hook)
4744       (let ((case-fold-search t)
4745             in-reply-to header p lines chars ctype)
4746         (goto-char (point-min))
4747         ;; Search to the beginning of the next header.  Error messages
4748         ;; do not begin with 2 or 3.
4749         (while (re-search-forward "^[23][0-9]+ " nil t)
4750           (setq id nil
4751                 ref nil)
4752           ;; This implementation of this function, with nine
4753           ;; search-forwards instead of the one re-search-forward and
4754           ;; a case (which basically was the old function) is actually
4755           ;; about twice as fast, even though it looks messier.  You
4756           ;; can't have everything, I guess.  Speed and elegance
4757           ;; doesn't always go hand in hand.
4758           (setq
4759            header
4760            (make-full-mail-header
4761             ;; Number.
4762             (prog1
4763                 (read cur)
4764               (end-of-line)
4765               (setq p (point))
4766               (narrow-to-region (point)
4767                                 (or (and (search-forward "\n.\n" nil t)
4768                                          (- (point) 2))
4769                                     (point))))
4770             ;; Subject.
4771             (progn
4772               (goto-char p)
4773               (if (search-forward "\nsubject: " nil t)
4774                   (buffer-substring (match-end 0) (std11-field-end))
4775                 "(none)"))
4776             ;; From.
4777             (progn
4778               (goto-char p)
4779               (if (search-forward "\nfrom: " nil t)
4780                   (buffer-substring (match-end 0) (std11-field-end))
4781                 "(nobody)"))
4782             ;; Date.
4783             (progn
4784               (goto-char p)
4785               (if (search-forward "\ndate: " nil t)
4786                   (buffer-substring (match-end 0) (std11-field-end))
4787                 ""))
4788             ;; Message-ID.
4789             (progn
4790               (goto-char p)
4791               (setq id (if (re-search-forward
4792                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4793                            ;; We do it this way to make sure the Message-ID
4794                            ;; is (somewhat) syntactically valid.
4795                            (buffer-substring (match-beginning 1)
4796                                              (match-end 1))
4797                          ;; If there was no message-id, we just fake one
4798                          ;; to make subsequent routines simpler.
4799                          (nnheader-generate-fake-message-id))))
4800             ;; References.
4801             (progn
4802               (goto-char p)
4803               (if (search-forward "\nreferences: " nil t)
4804                   (progn
4805                     (setq end (point))
4806                     (prog1
4807                         (buffer-substring (match-end 0) (std11-field-end))
4808                       (setq ref
4809                             (buffer-substring
4810                              (progn
4811                                ;; (end-of-line)
4812                                (search-backward ">" end t)
4813                                (1+ (point)))
4814                              (progn
4815                                (search-backward "<" end t)
4816                                (point))))))
4817                 ;; Get the references from the in-reply-to header if there
4818                 ;; were no references and the in-reply-to header looks
4819                 ;; promising.
4820                 (if (and (search-forward "\nin-reply-to: " nil t)
4821                          (setq in-reply-to
4822                                (buffer-substring (match-end 0)
4823                                                  (std11-field-end)))
4824                          (string-match "<[^>]+>" in-reply-to))
4825                     (let (ref2)
4826                       (setq ref (substring in-reply-to (match-beginning 0)
4827                                            (match-end 0)))
4828                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4829                         (setq ref2 (substring in-reply-to (match-beginning 0)
4830                                               (match-end 0)))
4831                         (when (> (length ref2) (length ref))
4832                           (setq ref ref2)))
4833                       ref)
4834                   (setq ref nil))))
4835             ;; Chars.
4836             (progn
4837               (goto-char p)
4838               (if (search-forward "\nchars: " nil t)
4839                   (if (numberp (setq chars (ignore-errors (read cur))))
4840                       chars 0)
4841                 0))
4842             ;; Lines.
4843             (progn
4844               (goto-char p)
4845               (if (search-forward "\nlines: " nil t)
4846                   (if (numberp (setq lines (ignore-errors (read cur))))
4847                       lines 0)
4848                 0))
4849             ;; Xref.
4850             (progn
4851               (goto-char p)
4852               (and (search-forward "\nxref: " nil t)
4853                    (buffer-substring (match-end 0) (std11-field-end))))
4854             ;; Extra.
4855             (when gnus-extra-headers
4856               (let ((extra gnus-extra-headers)
4857                     out)
4858                 (while extra
4859                   (goto-char p)
4860                   (when (search-forward
4861                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4862                     (push (cons (car extra)
4863                                 (buffer-substring (match-end 0)
4864                                                   (std11-field-end)))
4865                           out))
4866                   (pop extra))
4867                 out))))
4868           (goto-char p)
4869           (if (and (search-forward "\ncontent-type: " nil t)
4870                    (setq ctype
4871                          (buffer-substring (match-end 0) (std11-field-end))))
4872               (mime-entity-set-content-type-internal
4873                header (mime-parse-Content-Type ctype)))
4874           (when (equal id ref)
4875             (setq ref nil))
4876
4877           (when gnus-alter-header-function
4878             (funcall gnus-alter-header-function header)
4879             (setq id (mail-header-id header)
4880                   ref (gnus-parent-id (mail-header-references header))))
4881
4882           (when (setq header
4883                       (gnus-dependencies-add-header
4884                        header dependencies force-new))
4885             (push header headers))
4886           (goto-char (point-max))
4887           (widen))
4888         (nreverse headers)))))
4889
4890 ;; Goes through the xover lines and returns a list of vectors
4891 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4892                                                   force-new dependencies
4893                                                   group also-fetch-heads)
4894   "Parse the news overview data in the server buffer.
4895 Return a list of headers that match SEQUENCE (see
4896 `nntp-retrieve-headers')."
4897   ;; Get the Xref when the users reads the articles since most/some
4898   ;; NNTP servers do not include Xrefs when using XOVER.
4899   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4900   (let ((mail-parse-charset gnus-newsgroup-charset)
4901         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4902         (cur nntp-server-buffer)
4903         (dependencies (or dependencies gnus-newsgroup-dependencies))
4904         number headers header)
4905     (save-excursion
4906       (set-buffer nntp-server-buffer)
4907       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4908       ;; Allow the user to mangle the headers before parsing them.
4909       (gnus-run-hooks 'gnus-parse-headers-hook)
4910       (goto-char (point-min))
4911       (while (not (eobp))
4912         (condition-case ()
4913             (while (and sequence (not (eobp)))
4914               (setq number (read cur))
4915               (while (and sequence
4916                           (< (car sequence) number))
4917                 (setq sequence (cdr sequence)))
4918               (and sequence
4919                    (eq number (car sequence))
4920                    (progn
4921                      (setq sequence (cdr sequence))
4922                      (setq header (inline
4923                                     (gnus-nov-parse-line
4924                                      number dependencies force-new))))
4925                    (push header headers))
4926               (forward-line 1))
4927           (error
4928            (gnus-error 4 "Strange nov line (%d)"
4929                        (count-lines (point-min) (point)))))
4930         (forward-line 1))
4931       ;; A common bug in inn is that if you have posted an article and
4932       ;; then retrieves the active file, it will answer correctly --
4933       ;; the new article is included.  However, a NOV entry for the
4934       ;; article may not have been generated yet, so this may fail.
4935       ;; We work around this problem by retrieving the last few
4936       ;; headers using HEAD.
4937       (if (or (not also-fetch-heads)
4938               (not sequence))
4939           ;; We (probably) got all the headers.
4940           (nreverse headers)
4941         (let ((gnus-nov-is-evil t))
4942           (nconc
4943            (nreverse headers)
4944            (gnus-retrieve-parsed-headers sequence group)
4945            ))))))
4946
4947 (defun gnus-article-get-xrefs ()
4948   "Fill in the Xref value in `gnus-current-headers', if necessary.
4949 This is meant to be called in `gnus-article-internal-prepare-hook'."
4950   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4951                                  gnus-current-headers)))
4952     (or (not gnus-use-cross-reference)
4953         (not headers)
4954         (and (mail-header-xref headers)
4955              (not (string= (mail-header-xref headers) "")))
4956         (let ((case-fold-search t)
4957               xref)
4958           (save-restriction
4959             (nnheader-narrow-to-headers)
4960             (goto-char (point-min))
4961             (when (or (and (not (eobp))
4962                            (eq (downcase (char-after)) ?x)
4963                            (looking-at "Xref:"))
4964                       (search-forward "\nXref:" nil t))
4965               (goto-char (1+ (match-end 0)))
4966               (setq xref (buffer-substring (point)
4967                                            (progn (end-of-line) (point))))
4968               (mail-header-set-xref headers xref)))))))
4969
4970 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4971   "Find article ID and insert the summary line for that article.
4972 OLD-HEADER can either be a header or a line number to insert
4973 the subject line on."
4974   (let* ((line (and (numberp old-header) old-header))
4975          (old-header (and (vectorp old-header) old-header))
4976          (header (cond ((and old-header use-old-header)
4977                         old-header)
4978                        ((and (numberp id)
4979                              (gnus-number-to-header id))
4980                         (gnus-number-to-header id))
4981                        (t
4982                         (gnus-read-header id))))
4983          (number (and (numberp id) id))
4984          d)
4985     (when header
4986       ;; Rebuild the thread that this article is part of and go to the
4987       ;; article we have fetched.
4988       (when (and (not gnus-show-threads)
4989                  old-header)
4990         (when (and number
4991                    (setq d (gnus-data-find (mail-header-number old-header))))
4992           (goto-char (gnus-data-pos d))
4993           (gnus-data-remove
4994            number
4995            (- (gnus-point-at-bol)
4996               (prog1
4997                   (1+ (gnus-point-at-eol))
4998                 (gnus-delete-line))))))
4999       (when old-header
5000         (mail-header-set-number header (mail-header-number old-header)))
5001       (setq gnus-newsgroup-sparse
5002             (delq (setq number (mail-header-number header))
5003                   gnus-newsgroup-sparse))
5004       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5005       (push number gnus-newsgroup-limit)
5006       (gnus-rebuild-thread (mail-header-id header) line)
5007       (gnus-summary-goto-subject number nil t))
5008     (when (and (numberp number)
5009                (> number 0))
5010       ;; We have to update the boundaries even if we can't fetch the
5011       ;; article if ID is a number -- so that the next `P' or `N'
5012       ;; command will fetch the previous (or next) article even
5013       ;; if the one we tried to fetch this time has been canceled.
5014       (when (> number gnus-newsgroup-end)
5015         (setq gnus-newsgroup-end number))
5016       (when (< number gnus-newsgroup-begin)
5017         (setq gnus-newsgroup-begin number))
5018       (setq gnus-newsgroup-unselected
5019             (delq number gnus-newsgroup-unselected)))
5020     ;; Report back a success?
5021     (and header (mail-header-number header))))
5022
5023 ;;; Process/prefix in the summary buffer
5024
5025 (defun gnus-summary-work-articles (n)
5026   "Return a list of articles to be worked upon.
5027 The prefix argument, the list of process marked articles, and the
5028 current article will be taken into consideration."
5029   (save-excursion
5030     (set-buffer gnus-summary-buffer)
5031     (cond
5032      (n
5033       ;; A numerical prefix has been given.
5034       (setq n (prefix-numeric-value n))
5035       (let ((backward (< n 0))
5036             (n (abs (prefix-numeric-value n)))
5037             articles article)
5038         (save-excursion
5039           (while
5040               (and (> n 0)
5041                    (push (setq article (gnus-summary-article-number))
5042                          articles)
5043                    (if backward
5044                        (gnus-summary-find-prev nil article)
5045                      (gnus-summary-find-next nil article)))
5046             (decf n)))
5047         (nreverse articles)))
5048      ((and (gnus-region-active-p) (mark))
5049       (message "region active")
5050       ;; Work on the region between point and mark.
5051       (let ((max (max (point) (mark)))
5052             articles article)
5053         (save-excursion
5054           (goto-char (min (point) (mark)))
5055           (while
5056               (and
5057                (push (setq article (gnus-summary-article-number)) articles)
5058                (gnus-summary-find-next nil article)
5059                (< (point) max)))
5060           (nreverse articles))))
5061      (gnus-newsgroup-processable
5062       ;; There are process-marked articles present.
5063       ;; Save current state.
5064       (gnus-summary-save-process-mark)
5065       ;; Return the list.
5066       (reverse gnus-newsgroup-processable))
5067      (t
5068       ;; Just return the current article.
5069       (list (gnus-summary-article-number))))))
5070
5071 (defmacro gnus-summary-iterate (arg &rest forms)
5072   "Iterate over the process/prefixed articles and do FORMS.
5073 ARG is the interactive prefix given to the command.  FORMS will be
5074 executed with point over the summary line of the articles."
5075   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5076     `(let ((,articles (gnus-summary-work-articles ,arg)))
5077        (while ,articles
5078          (gnus-summary-goto-subject (car ,articles))
5079          ,@forms
5080          (pop ,articles)))))
5081
5082 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5083 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5084
5085 (defun gnus-summary-save-process-mark ()
5086   "Push the current set of process marked articles on the stack."
5087   (interactive)
5088   (push (copy-sequence gnus-newsgroup-processable)
5089         gnus-newsgroup-process-stack))
5090
5091 (defun gnus-summary-kill-process-mark ()
5092   "Push the current set of process marked articles on the stack and unmark."
5093   (interactive)
5094   (gnus-summary-save-process-mark)
5095   (gnus-summary-unmark-all-processable))
5096
5097 (defun gnus-summary-yank-process-mark ()
5098   "Pop the last process mark state off the stack and restore it."
5099   (interactive)
5100   (unless gnus-newsgroup-process-stack
5101     (error "Empty mark stack"))
5102   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5103
5104 (defun gnus-summary-process-mark-set (set)
5105   "Make SET into the current process marked articles."
5106   (gnus-summary-unmark-all-processable)
5107   (while set
5108     (gnus-summary-set-process-mark (pop set))))
5109
5110 ;;; Searching and stuff
5111
5112 (defun gnus-summary-search-group (&optional backward use-level)
5113   "Search for next unread newsgroup.
5114 If optional argument BACKWARD is non-nil, search backward instead."
5115   (save-excursion
5116     (set-buffer gnus-group-buffer)
5117     (when (gnus-group-search-forward
5118            backward nil (if use-level (gnus-group-group-level) nil))
5119       (gnus-group-group-name))))
5120
5121 (defun gnus-summary-best-group (&optional exclude-group)
5122   "Find the name of the best unread group.
5123 If EXCLUDE-GROUP, do not go to this group."
5124   (save-excursion
5125     (set-buffer gnus-group-buffer)
5126     (save-excursion
5127       (gnus-group-best-unread-group exclude-group))))
5128
5129 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5130   (if backward (gnus-summary-find-prev)
5131     (let* ((dummy (gnus-summary-article-intangible-p))
5132            (article (or article (gnus-summary-article-number)))
5133            (arts (gnus-data-find-list article))
5134            result)
5135       (when (and (not dummy)
5136                  (or (not gnus-summary-check-current)
5137                      (not unread)
5138                      (not (gnus-data-unread-p (car arts)))))
5139         (setq arts (cdr arts)))
5140       (when (setq result
5141                   (if unread
5142                       (progn
5143                         (while arts
5144                           (when (or (and undownloaded
5145                                          (eq gnus-undownloaded-mark
5146                                              (gnus-data-mark (car arts))))
5147                                     (gnus-data-unread-p (car arts)))
5148                             (setq result (car arts)
5149                                   arts nil))
5150                           (setq arts (cdr arts)))
5151                         result)
5152                     (car arts)))
5153         (goto-char (gnus-data-pos result))
5154         (gnus-data-number result)))))
5155
5156 (defun gnus-summary-find-prev (&optional unread article)
5157   (let* ((eobp (eobp))
5158          (article (or article (gnus-summary-article-number)))
5159          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5160          result)
5161     (when (and (not eobp)
5162                (or (not gnus-summary-check-current)
5163                    (not unread)
5164                    (not (gnus-data-unread-p (car arts)))))
5165       (setq arts (cdr arts)))
5166     (when (setq result
5167                 (if unread
5168                     (progn
5169                       (while arts
5170                         (when (gnus-data-unread-p (car arts))
5171                           (setq result (car arts)
5172                                 arts nil))
5173                         (setq arts (cdr arts)))
5174                       result)
5175                   (car arts)))
5176       (goto-char (gnus-data-pos result))
5177       (gnus-data-number result))))
5178
5179 (defun gnus-summary-find-subject (subject &optional unread backward article)
5180   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5181          (article (or article (gnus-summary-article-number)))
5182          (articles (gnus-data-list backward))
5183          (arts (gnus-data-find-list article articles))
5184          result)
5185     (when (or (not gnus-summary-check-current)
5186               (not unread)
5187               (not (gnus-data-unread-p (car arts))))
5188       (setq arts (cdr arts)))
5189     (while arts
5190       (and (or (not unread)
5191                (gnus-data-unread-p (car arts)))
5192            (vectorp (gnus-data-header (car arts)))
5193            (gnus-subject-equal
5194             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5195            (setq result (car arts)
5196                  arts nil))
5197       (setq arts (cdr arts)))
5198     (and result
5199          (goto-char (gnus-data-pos result))
5200          (gnus-data-number result))))
5201
5202 (defun gnus-summary-search-forward (&optional unread subject backward)
5203   "Search forward for an article.
5204 If UNREAD, look for unread articles.  If SUBJECT, look for
5205 articles with that subject.  If BACKWARD, search backward instead."
5206   (cond (subject (gnus-summary-find-subject subject unread backward))
5207         (backward (gnus-summary-find-prev unread))
5208         (t (gnus-summary-find-next unread))))
5209
5210 (defun gnus-recenter (&optional n)
5211   "Center point in window and redisplay frame.
5212 Also do horizontal recentering."
5213   (interactive "P")
5214   (when (and gnus-auto-center-summary
5215              (not (eq gnus-auto-center-summary 'vertical)))
5216     (gnus-horizontal-recenter))
5217   (recenter n))
5218
5219 (defun gnus-summary-recenter ()
5220   "Center point in the summary window.
5221 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5222 displayed, no centering will be performed."
5223   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5224   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5225   (interactive)
5226   (let* ((top (cond ((< (window-height) 4) 0)
5227                     ((< (window-height) 7) 1)
5228                     (t (if (numberp gnus-auto-center-summary)
5229                            gnus-auto-center-summary
5230                          2))))
5231          (height (1- (window-height)))
5232          (bottom (save-excursion (goto-char (point-max))
5233                                  (forward-line (- height))
5234                                  (point)))
5235          (window (get-buffer-window (current-buffer))))
5236     ;; The user has to want it.
5237     (when gnus-auto-center-summary
5238       (when (get-buffer-window gnus-article-buffer)
5239         ;; Only do recentering when the article buffer is displayed,
5240         ;; Set the window start to either `bottom', which is the biggest
5241         ;; possible valid number, or the second line from the top,
5242         ;; whichever is the least.
5243         (set-window-start
5244          window (min bottom (save-excursion
5245                               (forward-line (- top)) (point)))
5246          t))
5247       ;; Do horizontal recentering while we're at it.
5248       (when (and (get-buffer-window (current-buffer) t)
5249                  (not (eq gnus-auto-center-summary 'vertical)))
5250         (let ((selected (selected-window)))
5251           (select-window (get-buffer-window (current-buffer) t))
5252           (gnus-summary-position-point)
5253           (gnus-horizontal-recenter)
5254           (select-window selected))))))
5255
5256 (defun gnus-summary-jump-to-group (newsgroup)
5257   "Move point to NEWSGROUP in group mode buffer."
5258   ;; Keep update point of group mode buffer if visible.
5259   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5260       (save-window-excursion
5261         ;; Take care of tree window mode.
5262         (when (get-buffer-window gnus-group-buffer)
5263           (pop-to-buffer gnus-group-buffer))
5264         (gnus-group-jump-to-group newsgroup))
5265     (save-excursion
5266       ;; Take care of tree window mode.
5267       (if (get-buffer-window gnus-group-buffer)
5268           (pop-to-buffer gnus-group-buffer)
5269         (set-buffer gnus-group-buffer))
5270       (gnus-group-jump-to-group newsgroup))))
5271
5272 ;; This function returns a list of article numbers based on the
5273 ;; difference between the ranges of read articles in this group and
5274 ;; the range of active articles.
5275 (defun gnus-list-of-unread-articles (group)
5276   (let* ((read (gnus-info-read (gnus-get-info group)))
5277          (active (or (gnus-active group) (gnus-activate-group group)))
5278          (last (cdr active))
5279          first nlast unread)
5280     ;; If none are read, then all are unread.
5281     (if (not read)
5282         (setq first (car active))
5283       ;; If the range of read articles is a single range, then the
5284       ;; first unread article is the article after the last read
5285       ;; article.  Sounds logical, doesn't it?
5286       (if (and (not (listp (cdr read)))
5287                (or (< (car read) (car active))
5288                    (progn (setq read (list read))
5289                           nil)))
5290           (setq first (max (car active) (1+ (cdr read))))
5291         ;; `read' is a list of ranges.
5292         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5293                                   (caar read)))
5294                   1)
5295           (setq first (car active)))
5296         (while read
5297           (when first
5298             (while (< first nlast)
5299               (push first unread)
5300               (setq first (1+ first))))
5301           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5302           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5303           (setq read (cdr read)))))
5304     ;; And add the last unread articles.
5305     (while (<= first last)
5306       (push first unread)
5307       (setq first (1+ first)))
5308     ;; Return the list of unread articles.
5309     (delq 0 (nreverse unread))))
5310
5311 (defun gnus-list-of-read-articles (group)
5312   "Return a list of unread, unticked and non-dormant articles."
5313   (let* ((info (gnus-get-info group))
5314          (marked (gnus-info-marks info))
5315          (active (gnus-active group)))
5316     (and info active
5317          (gnus-set-difference
5318           (gnus-sorted-complement
5319            (gnus-uncompress-range active)
5320            (gnus-list-of-unread-articles group))
5321           (append
5322            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5323            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5324
5325 ;; Various summary commands
5326
5327 (defun gnus-summary-select-article-buffer ()
5328   "Reconfigure windows to show article buffer."
5329   (interactive)
5330   (if (not (gnus-buffer-live-p gnus-article-buffer))
5331       (error "There is no article buffer for this summary buffer")
5332     (gnus-configure-windows 'article)
5333     (select-window (get-buffer-window gnus-article-buffer))))
5334
5335 (defun gnus-summary-universal-argument (arg)
5336   "Perform any operation on all articles that are process/prefixed."
5337   (interactive "P")
5338   (let ((articles (gnus-summary-work-articles arg))
5339         func article)
5340     (if (eq
5341          (setq
5342           func
5343           (key-binding
5344            (read-key-sequence
5345             (substitute-command-keys
5346              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5347          'undefined)
5348         (gnus-error 1 "Undefined key")
5349       (save-excursion
5350         (while articles
5351           (gnus-summary-goto-subject (setq article (pop articles)))
5352           (let (gnus-newsgroup-processable)
5353             (command-execute func))
5354           (gnus-summary-remove-process-mark article)))))
5355   (gnus-summary-position-point))
5356
5357 (defun gnus-summary-toggle-truncation (&optional arg)
5358   "Toggle truncation of summary lines.
5359 With arg, turn line truncation on iff arg is positive."
5360   (interactive "P")
5361   (setq truncate-lines
5362         (if (null arg) (not truncate-lines)
5363           (> (prefix-numeric-value arg) 0)))
5364   (redraw-display))
5365
5366 (defun gnus-summary-reselect-current-group (&optional all rescan)
5367   "Exit and then reselect the current newsgroup.
5368 The prefix argument ALL means to select all articles."
5369   (interactive "P")
5370   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5371     (error "Ephemeral groups can't be reselected"))
5372   (let ((current-subject (gnus-summary-article-number))
5373         (group gnus-newsgroup-name))
5374     (setq gnus-newsgroup-begin nil)
5375     (gnus-summary-exit)
5376     ;; We have to adjust the point of group mode buffer because
5377     ;; point was moved to the next unread newsgroup by exiting.
5378     (gnus-summary-jump-to-group group)
5379     (when rescan
5380       (save-excursion
5381         (save-window-excursion
5382           ;; Don't show group contents.
5383           (set-window-start (selected-window) (point-max))
5384           (gnus-group-get-new-news-this-group 1))))
5385     (gnus-group-read-group all t)
5386     (gnus-summary-goto-subject current-subject nil t)))
5387
5388 (defun gnus-summary-rescan-group (&optional all)
5389   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5390   (interactive "P")
5391   (gnus-summary-reselect-current-group all t))
5392
5393 (defun gnus-summary-update-info (&optional non-destructive)
5394   (save-excursion
5395     (let ((group gnus-newsgroup-name))
5396       (when group
5397         (when gnus-newsgroup-kill-headers
5398           (setq gnus-newsgroup-killed
5399                 (gnus-compress-sequence
5400                  (nconc
5401                   (gnus-set-sorted-intersection
5402                    (gnus-uncompress-range gnus-newsgroup-killed)
5403                    (setq gnus-newsgroup-unselected
5404                          (sort gnus-newsgroup-unselected '<)))
5405                   (setq gnus-newsgroup-unreads
5406                         (sort gnus-newsgroup-unreads '<)))
5407                  t)))
5408         (unless (listp (cdr gnus-newsgroup-killed))
5409           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5410         (let ((headers gnus-newsgroup-headers))
5411           ;; Set the new ranges of read articles.
5412           (save-excursion
5413             (set-buffer gnus-group-buffer)
5414             (gnus-undo-force-boundary))
5415           (gnus-update-read-articles
5416            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5417           ;; Set the current article marks.
5418           (let ((gnus-newsgroup-scored
5419                  (if (and (not gnus-save-score)
5420                           (not non-destructive))
5421                      nil
5422                    gnus-newsgroup-scored)))
5423             (save-excursion
5424               (gnus-update-marks)))
5425           ;; Do the cross-ref thing.
5426           (when gnus-use-cross-reference
5427             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5428           ;; Do not switch windows but change the buffer to work.
5429           (set-buffer gnus-group-buffer)
5430           (unless (gnus-ephemeral-group-p group)
5431             (gnus-group-update-group group)))))))
5432
5433 (defun gnus-summary-save-newsrc (&optional force)
5434   "Save the current number of read/marked articles in the dribble buffer.
5435 The dribble buffer will then be saved.
5436 If FORCE (the prefix), also save the .newsrc file(s)."
5437   (interactive "P")
5438   (gnus-summary-update-info t)
5439   (if force
5440       (gnus-save-newsrc-file)
5441     (gnus-dribble-save)))
5442
5443 (defun gnus-summary-exit (&optional temporary)
5444   "Exit reading current newsgroup, and then return to group selection mode.
5445 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5446   (interactive)
5447   (gnus-set-global-variables)
5448   (gnus-kill-save-kill-buffer)
5449   (gnus-async-halt-prefetch)
5450   (let* ((group gnus-newsgroup-name)
5451          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5452          (mode major-mode)
5453          (group-point nil)
5454          (buf (current-buffer)))
5455     (unless quit-config
5456       ;; Do adaptive scoring, and possibly save score files.
5457       (when gnus-newsgroup-adaptive
5458         (gnus-score-adaptive))
5459       (when gnus-use-scoring
5460         (gnus-score-save)))
5461     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5462     ;; If we have several article buffers, we kill them at exit.
5463     (unless gnus-single-article-buffer
5464       (gnus-kill-buffer gnus-original-article-buffer)
5465       (setq gnus-article-current nil))
5466     (when gnus-use-cache
5467       (gnus-cache-possibly-remove-articles)
5468       (gnus-cache-save-buffers))
5469     (gnus-async-prefetch-remove-group group)
5470     (when gnus-suppress-duplicates
5471       (gnus-dup-enter-articles))
5472     (when gnus-use-trees
5473       (gnus-tree-close group))
5474     (when gnus-use-cache
5475       (gnus-cache-write-active))
5476     ;; Remove entries for this group.
5477     (nnmail-purge-split-history (gnus-group-real-name group))
5478     ;; Make all changes in this group permanent.
5479     (unless quit-config
5480       (gnus-run-hooks 'gnus-exit-group-hook)
5481       (gnus-summary-update-info))
5482     (gnus-close-group group)
5483     ;; Make sure where we were, and go to next newsgroup.
5484     (set-buffer gnus-group-buffer)
5485     (unless quit-config
5486       (gnus-group-jump-to-group group))
5487     (gnus-run-hooks 'gnus-summary-exit-hook)
5488     (unless (or quit-config
5489                 ;; If this group has disappeared from the summary
5490                 ;; buffer, don't skip forwards.
5491                 (not (string= group (gnus-group-group-name))))
5492       (gnus-group-next-unread-group 1))
5493     (setq group-point (point))
5494     (if temporary
5495         nil                             ;Nothing to do.
5496       ;; If we have several article buffers, we kill them at exit.
5497       (unless gnus-single-article-buffer
5498         (gnus-kill-buffer gnus-article-buffer)
5499         (gnus-kill-buffer gnus-original-article-buffer)
5500         (setq gnus-article-current nil))
5501       (set-buffer buf)
5502       (if (not gnus-kill-summary-on-exit)
5503           (gnus-deaden-summary)
5504         ;; We set all buffer-local variables to nil.  It is unclear why
5505         ;; this is needed, but if we don't, buffer-local variables are
5506         ;; not garbage-collected, it seems.  This would the lead to en
5507         ;; ever-growing Emacs.
5508         (gnus-summary-clear-local-variables)
5509         (when (get-buffer gnus-article-buffer)
5510           (bury-buffer gnus-article-buffer))
5511         ;; We clear the global counterparts of the buffer-local
5512         ;; variables as well, just to be on the safe side.
5513         (set-buffer gnus-group-buffer)
5514         (gnus-summary-clear-local-variables)
5515         ;; Return to group mode buffer.
5516         (when (eq mode 'gnus-summary-mode)
5517           (gnus-kill-buffer buf)))
5518       (setq gnus-current-select-method gnus-select-method)
5519       (pop-to-buffer gnus-group-buffer)
5520       (if (not quit-config)
5521           (progn
5522             (goto-char group-point)
5523             (gnus-configure-windows 'group 'force)
5524             (unless (pos-visible-in-window-p)
5525               (forward-line (/ (static-if (featurep 'xemacs)
5526                                    (window-displayed-height)
5527                                  (1- (window-height)))
5528                                -2))
5529               (set-window-start (selected-window) (point))
5530               (goto-char group-point)))
5531         (gnus-handle-ephemeral-exit quit-config))
5532       ;; Clear the current group name.
5533       (unless quit-config
5534         (setq gnus-newsgroup-name nil)))))
5535
5536 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5537 (defun gnus-summary-exit-no-update (&optional no-questions)
5538   "Quit reading current newsgroup without updating read article info."
5539   (interactive)
5540   (let* ((group gnus-newsgroup-name)
5541          (quit-config (gnus-group-quit-config group)))
5542     (when (or no-questions
5543               gnus-expert-user
5544               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5545       (gnus-async-halt-prefetch)
5546       (mapcar 'funcall
5547               (delq 'gnus-summary-expire-articles
5548                     (copy-sequence gnus-summary-prepare-exit-hook)))
5549       ;; If we have several article buffers, we kill them at exit.
5550       (unless gnus-single-article-buffer
5551         (gnus-kill-buffer gnus-article-buffer)
5552         (gnus-kill-buffer gnus-original-article-buffer)
5553         (setq gnus-article-current nil))
5554       (if (not gnus-kill-summary-on-exit)
5555           (gnus-deaden-summary)
5556         (gnus-close-group group)
5557         (gnus-summary-clear-local-variables)
5558         (set-buffer gnus-group-buffer)
5559         (gnus-summary-clear-local-variables)
5560         (when (get-buffer gnus-summary-buffer)
5561           (kill-buffer gnus-summary-buffer)))
5562       (unless gnus-single-article-buffer
5563         (setq gnus-article-current nil))
5564       (when gnus-use-trees
5565         (gnus-tree-close group))
5566       (gnus-async-prefetch-remove-group group)
5567       (when (get-buffer gnus-article-buffer)
5568         (bury-buffer gnus-article-buffer))
5569       ;; Return to the group buffer.
5570       (gnus-configure-windows 'group 'force)
5571       ;; Clear the current group name.
5572       (setq gnus-newsgroup-name nil)
5573       (when (equal (gnus-group-group-name) group)
5574         (gnus-group-next-unread-group 1))
5575       (when quit-config
5576         (gnus-handle-ephemeral-exit quit-config)))))
5577
5578 (defun gnus-handle-ephemeral-exit (quit-config)
5579   "Handle movement when leaving an ephemeral group.
5580 The state which existed when entering the ephemeral is reset."
5581   (if (not (buffer-name (car quit-config)))
5582       (gnus-configure-windows 'group 'force)
5583     (set-buffer (car quit-config))
5584     (cond ((eq major-mode 'gnus-summary-mode)
5585            (gnus-set-global-variables))
5586           ((eq major-mode 'gnus-article-mode)
5587            (save-excursion
5588              ;; The `gnus-summary-buffer' variable may point
5589              ;; to the old summary buffer when using a single
5590              ;; article buffer.
5591              (unless (gnus-buffer-live-p gnus-summary-buffer)
5592                (set-buffer gnus-group-buffer))
5593              (set-buffer gnus-summary-buffer)
5594              (gnus-set-global-variables))))
5595     (if (or (eq (cdr quit-config) 'article)
5596             (eq (cdr quit-config) 'pick))
5597         (progn
5598           ;; The current article may be from the ephemeral group
5599           ;; thus it is best that we reload this article
5600           (gnus-summary-show-article)
5601           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5602               (gnus-configure-windows 'pick 'force)
5603             (gnus-configure-windows (cdr quit-config) 'force)))
5604       (gnus-configure-windows (cdr quit-config) 'force))
5605     (when (eq major-mode 'gnus-summary-mode)
5606       (gnus-summary-next-subject 1 nil t)
5607       (gnus-summary-recenter)
5608       (gnus-summary-position-point))))
5609
5610 (defun gnus-summary-preview-mime-message ()
5611   "MIME decode and play this message."
5612   (interactive)
5613   (let ((gnus-break-pages nil)
5614         (gnus-show-mime t))
5615     (gnus-summary-select-article gnus-show-all-headers t))
5616   (select-window (get-buffer-window gnus-article-buffer)))
5617
5618 ;;; Dead summaries.
5619
5620 (defvar gnus-dead-summary-mode-map nil)
5621
5622 (unless gnus-dead-summary-mode-map
5623   (setq gnus-dead-summary-mode-map (make-keymap))
5624   (suppress-keymap gnus-dead-summary-mode-map)
5625   (substitute-key-definition
5626    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5627   (let ((keys '("\C-d" "\r" "\177" [delete])))
5628     (while keys
5629       (define-key gnus-dead-summary-mode-map
5630         (pop keys) 'gnus-summary-wake-up-the-dead))))
5631
5632 (defvar gnus-dead-summary-mode nil
5633   "Minor mode for Gnus summary buffers.")
5634
5635 (defun gnus-dead-summary-mode (&optional arg)
5636   "Minor mode for Gnus summary buffers."
5637   (interactive "P")
5638   (when (eq major-mode 'gnus-summary-mode)
5639     (make-local-variable 'gnus-dead-summary-mode)
5640     (setq gnus-dead-summary-mode
5641           (if (null arg) (not gnus-dead-summary-mode)
5642             (> (prefix-numeric-value arg) 0)))
5643     (when gnus-dead-summary-mode
5644       (gnus-add-minor-mode
5645        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5646
5647 (defun gnus-deaden-summary ()
5648   "Make the current summary buffer into a dead summary buffer."
5649   ;; Kill any previous dead summary buffer.
5650   (when (and gnus-dead-summary
5651              (buffer-name gnus-dead-summary))
5652     (save-excursion
5653       (set-buffer gnus-dead-summary)
5654       (when gnus-dead-summary-mode
5655         (kill-buffer (current-buffer)))))
5656   ;; Make this the current dead summary.
5657   (setq gnus-dead-summary (current-buffer))
5658   (gnus-dead-summary-mode 1)
5659   (let ((name (buffer-name)))
5660     (when (string-match "Summary" name)
5661       (rename-buffer
5662        (concat (substring name 0 (match-beginning 0)) "Dead "
5663                (substring name (match-beginning 0)))
5664        t)
5665       (bury-buffer))))
5666
5667 (defun gnus-kill-or-deaden-summary (buffer)
5668   "Kill or deaden the summary BUFFER."
5669   (save-excursion
5670     (when (and (buffer-name buffer)
5671                (not gnus-single-article-buffer))
5672       (save-excursion
5673         (set-buffer buffer)
5674         (gnus-kill-buffer gnus-article-buffer)
5675         (gnus-kill-buffer gnus-original-article-buffer)))
5676     (cond (gnus-kill-summary-on-exit
5677            (when (and gnus-use-trees
5678                       (gnus-buffer-exists-p buffer))
5679              (save-excursion
5680                (set-buffer buffer)
5681                (gnus-tree-close gnus-newsgroup-name)))
5682            (gnus-kill-buffer buffer))
5683           ((gnus-buffer-exists-p buffer)
5684            (save-excursion
5685              (set-buffer buffer)
5686              (gnus-deaden-summary))))))
5687
5688 (defun gnus-summary-wake-up-the-dead (&rest args)
5689   "Wake up the dead summary buffer."
5690   (interactive)
5691   (gnus-dead-summary-mode -1)
5692   (let ((name (buffer-name)))
5693     (when (string-match "Dead " name)
5694       (rename-buffer
5695        (concat (substring name 0 (match-beginning 0))
5696                (substring name (match-end 0)))
5697        t)))
5698   (gnus-message 3 "This dead summary is now alive again"))
5699
5700 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5701 (defun gnus-summary-fetch-faq (&optional faq-dir)
5702   "Fetch the FAQ for the current group.
5703 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5704 in."
5705   (interactive
5706    (list
5707     (when current-prefix-arg
5708       (completing-read
5709        "Faq dir: " (and (listp gnus-group-faq-directory)
5710                         (mapcar (lambda (file) (list file))
5711                                 gnus-group-faq-directory))))))
5712   (let (gnus-faq-buffer)
5713     (when (setq gnus-faq-buffer
5714                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5715       (gnus-configure-windows 'summary-faq))))
5716
5717 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5718 (defun gnus-summary-describe-group (&optional force)
5719   "Describe the current newsgroup."
5720   (interactive "P")
5721   (gnus-group-describe-group force gnus-newsgroup-name))
5722
5723 (defun gnus-summary-describe-briefly ()
5724   "Describe summary mode commands briefly."
5725   (interactive)
5726   (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")))
5727
5728 ;; Walking around group mode buffer from summary mode.
5729
5730 (defun gnus-summary-next-group (&optional no-article target-group backward)
5731   "Exit current newsgroup and then select next unread newsgroup.
5732 If prefix argument NO-ARTICLE is non-nil, no article is selected
5733 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5734 previous group instead."
5735   (interactive "P")
5736   ;; Stop pre-fetching.
5737   (gnus-async-halt-prefetch)
5738   (let ((current-group gnus-newsgroup-name)
5739         (current-buffer (current-buffer))
5740         entered)
5741     ;; First we semi-exit this group to update Xrefs and all variables.
5742     ;; We can't do a real exit, because the window conf must remain
5743     ;; the same in case the user is prompted for info, and we don't
5744     ;; want the window conf to change before that...
5745     (gnus-summary-exit t)
5746     (while (not entered)
5747       ;; Then we find what group we are supposed to enter.
5748       (set-buffer gnus-group-buffer)
5749       (gnus-group-jump-to-group current-group)
5750       (setq target-group
5751             (or target-group
5752                 (if (eq gnus-keep-same-level 'best)
5753                     (gnus-summary-best-group gnus-newsgroup-name)
5754                   (gnus-summary-search-group backward gnus-keep-same-level))))
5755       (if (not target-group)
5756           ;; There are no further groups, so we return to the group
5757           ;; buffer.
5758           (progn
5759             (gnus-message 5 "Returning to the group buffer")
5760             (setq entered t)
5761             (when (gnus-buffer-live-p current-buffer)
5762               (set-buffer current-buffer)
5763               (gnus-summary-exit))
5764             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5765         ;; We try to enter the target group.
5766         (gnus-group-jump-to-group target-group)
5767         (let ((unreads (gnus-group-group-unread)))
5768           (if (and (or (eq t unreads)
5769                        (and unreads (not (zerop unreads))))
5770                    (gnus-summary-read-group
5771                     target-group nil no-article
5772                     (and (buffer-name current-buffer) current-buffer)
5773                     nil backward))
5774               (setq entered t)
5775             (setq current-group target-group
5776                   target-group nil)))))))
5777
5778 (defun gnus-summary-prev-group (&optional no-article)
5779   "Exit current newsgroup and then select previous unread newsgroup.
5780 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5781   (interactive "P")
5782   (gnus-summary-next-group no-article nil t))
5783
5784 ;; Walking around summary lines.
5785
5786 (defun gnus-summary-first-subject (&optional unread undownloaded)
5787   "Go to the first unread subject.
5788 If UNREAD is non-nil, go to the first unread article.
5789 Returns the article selected or nil if there are no unread articles."
5790   (interactive "P")
5791   (prog1
5792       (cond
5793        ;; Empty summary.
5794        ((null gnus-newsgroup-data)
5795         (gnus-message 3 "No articles in the group")
5796         nil)
5797        ;; Pick the first article.
5798        ((not unread)
5799         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5800         (gnus-data-number (car gnus-newsgroup-data)))
5801        ;; No unread articles.
5802        ((null gnus-newsgroup-unreads)
5803         (gnus-message 3 "No more unread articles")
5804         nil)
5805        ;; Find the first unread article.
5806        (t
5807         (let ((data gnus-newsgroup-data))
5808           (while (and data
5809                       (and (not (and undownloaded
5810                                      (eq gnus-undownloaded-mark
5811                                          (gnus-data-mark (car data)))))
5812                            (not (gnus-data-unread-p (car data)))))
5813             (setq data (cdr data)))
5814           (when data
5815             (goto-char (gnus-data-pos (car data)))
5816             (gnus-data-number (car data))))))
5817     (gnus-summary-position-point)))
5818
5819 (defun gnus-summary-next-subject (n &optional unread dont-display)
5820   "Go to next N'th summary line.
5821 If N is negative, go to the previous N'th subject line.
5822 If UNREAD is non-nil, only unread articles are selected.
5823 The difference between N and the actual number of steps taken is
5824 returned."
5825   (interactive "p")
5826   (let ((backward (< n 0))
5827         (n (abs n)))
5828     (while (and (> n 0)
5829                 (if backward
5830                     (gnus-summary-find-prev unread)
5831                   (gnus-summary-find-next unread)))
5832       (unless (zerop (setq n (1- n)))
5833         (gnus-summary-show-thread)))
5834     (when (/= 0 n)
5835       (gnus-message 7 "No more%s articles"
5836                     (if unread " unread" "")))
5837     (unless dont-display
5838       (gnus-summary-recenter)
5839       (gnus-summary-position-point))
5840     n))
5841
5842 (defun gnus-summary-next-unread-subject (n)
5843   "Go to next N'th unread summary line."
5844   (interactive "p")
5845   (gnus-summary-next-subject n t))
5846
5847 (defun gnus-summary-prev-subject (n &optional unread)
5848   "Go to previous N'th summary line.
5849 If optional argument UNREAD is non-nil, only unread article is selected."
5850   (interactive "p")
5851   (gnus-summary-next-subject (- n) unread))
5852
5853 (defun gnus-summary-prev-unread-subject (n)
5854   "Go to previous N'th unread summary line."
5855   (interactive "p")
5856   (gnus-summary-next-subject (- n) t))
5857
5858 (defun gnus-summary-goto-subject (article &optional force silent)
5859   "Go the subject line of ARTICLE.
5860 If FORCE, also allow jumping to articles not currently shown."
5861   (interactive "nArticle number: ")
5862   (let ((b (point))
5863         (data (gnus-data-find article)))
5864     ;; We read in the article if we have to.
5865     (and (not data)
5866          force
5867          (gnus-summary-insert-subject
5868           article
5869           (if (or (numberp force) (vectorp force)) force)
5870           t)
5871          (setq data (gnus-data-find article)))
5872     (goto-char b)
5873     (if (not data)
5874         (progn
5875           (unless silent
5876             (gnus-message 3 "Can't find article %d" article))
5877           nil)
5878       (goto-char (gnus-data-pos data))
5879       (gnus-summary-position-point)
5880       article)))
5881
5882 ;; Walking around summary lines with displaying articles.
5883
5884 (defun gnus-summary-expand-window (&optional arg)
5885   "Make the summary buffer take up the entire Emacs frame.
5886 Given a prefix, will force an `article' buffer configuration."
5887   (interactive "P")
5888   (if arg
5889       (gnus-configure-windows 'article 'force)
5890     (gnus-configure-windows 'summary 'force)))
5891
5892 (defun gnus-summary-display-article (article &optional all-header)
5893   "Display ARTICLE in article buffer."
5894   (gnus-set-global-variables)
5895   (if (null article)
5896       nil
5897     (prog1
5898         (if gnus-summary-display-article-function
5899             (funcall gnus-summary-display-article-function article all-header)
5900           (gnus-article-prepare article all-header))
5901       (with-current-buffer gnus-article-buffer
5902         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5903              nil))
5904       (gnus-run-hooks 'gnus-select-article-hook)
5905       (when (and gnus-current-article
5906                  (not (zerop gnus-current-article)))
5907         (gnus-summary-goto-subject gnus-current-article))
5908       (gnus-summary-recenter)
5909       (when (and gnus-use-trees gnus-show-threads)
5910         (gnus-possibly-generate-tree article)
5911         (gnus-highlight-selected-tree article))
5912       ;; Successfully display article.
5913       (gnus-article-set-window-start
5914        (cdr (assq article gnus-newsgroup-bookmarks))))))
5915
5916 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5917   "Select the current article.
5918 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5919 non-nil, the article will be re-fetched even if it already present in
5920 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5921 be displayed."
5922   ;; Make sure we are in the summary buffer to work around bbdb bug.
5923   (unless (eq major-mode 'gnus-summary-mode)
5924     (set-buffer gnus-summary-buffer))
5925   (let ((article (or article (gnus-summary-article-number)))
5926         (all-headers (not (not all-headers))) ;Must be T or NIL.
5927         gnus-summary-display-article-function)
5928     (and (not pseudo)
5929          (gnus-summary-article-pseudo-p article)
5930          (error "This is a pseudo-article"))
5931     (save-excursion
5932       (set-buffer gnus-summary-buffer)
5933       (if (or (and gnus-single-article-buffer
5934                    (or (null gnus-current-article)
5935                        (null gnus-article-current)
5936                        (null (get-buffer gnus-article-buffer))
5937                        (not (eq article (cdr gnus-article-current)))
5938                        (not (equal (car gnus-article-current)
5939                                    gnus-newsgroup-name))))
5940               (and (not gnus-single-article-buffer)
5941                    (or (null gnus-current-article)
5942                        (not (eq gnus-current-article article))))
5943               force)
5944           ;; The requested article is different from the current article.
5945           (progn
5946             (gnus-summary-display-article article all-headers)
5947             (when (or all-headers gnus-show-all-headers)
5948               (gnus-article-show-all-headers))
5949             (gnus-article-set-window-start
5950              (cdr (assq article gnus-newsgroup-bookmarks)))
5951             article)
5952         (when (or all-headers gnus-show-all-headers)
5953           (gnus-article-show-all-headers))
5954         'old))))
5955
5956 (defun gnus-summary-set-current-mark (&optional current-mark)
5957   "Obsolete function."
5958   nil)
5959
5960 (defun gnus-summary-next-article (&optional unread subject backward push)
5961   "Select the next article.
5962 If UNREAD, only unread articles are selected.
5963 If SUBJECT, only articles with SUBJECT are selected.
5964 If BACKWARD, the previous article is selected instead of the next."
5965   (interactive "P")
5966   (cond
5967    ;; Is there such an article?
5968    ((and (gnus-summary-search-forward unread subject backward)
5969          (or (gnus-summary-display-article (gnus-summary-article-number))
5970              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5971     (gnus-summary-position-point))
5972    ;; If not, we try the first unread, if that is wanted.
5973    ((and subject
5974          gnus-auto-select-same
5975          (gnus-summary-first-unread-article))
5976     (gnus-summary-position-point)
5977     (gnus-message 6 "Wrapped"))
5978    ;; Try to get next/previous article not displayed in this group.
5979    ((and gnus-auto-extend-newsgroup
5980          (not unread) (not subject))
5981     (gnus-summary-goto-article
5982      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5983      nil (count-lines (point-min) (point))))
5984    ;; Go to next/previous group.
5985    (t
5986     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5987       (gnus-summary-jump-to-group gnus-newsgroup-name))
5988     (let ((cmd last-command-char)
5989           (point
5990            (save-excursion
5991              (set-buffer gnus-group-buffer)
5992              (point)))
5993           (group
5994            (if (eq gnus-keep-same-level 'best)
5995                (gnus-summary-best-group gnus-newsgroup-name)
5996              (gnus-summary-search-group backward gnus-keep-same-level))))
5997       ;; For some reason, the group window gets selected.  We change
5998       ;; it back.
5999       (select-window (get-buffer-window (current-buffer)))
6000       ;; Select next unread newsgroup automagically.
6001       (cond
6002        ((or (not gnus-auto-select-next)
6003             (not cmd))
6004         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6005        ((or (eq gnus-auto-select-next 'quietly)
6006             (and (eq gnus-auto-select-next 'slightly-quietly)
6007                  push)
6008             (and (eq gnus-auto-select-next 'almost-quietly)
6009                  (gnus-summary-last-article-p)))
6010         ;; Select quietly.
6011         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6012             (gnus-summary-exit)
6013           (gnus-message 7 "No more%s articles (%s)..."
6014                         (if unread " unread" "")
6015                         (if group (concat "selecting " group)
6016                           "exiting"))
6017           (gnus-summary-next-group nil group backward)))
6018        (t
6019         (when (gnus-key-press-event-p last-input-event)
6020           (gnus-summary-walk-group-buffer
6021            gnus-newsgroup-name cmd unread backward point))))))))
6022
6023 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6024   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6025                       (?\C-p (gnus-group-prev-unread-group 1))))
6026         (cursor-in-echo-area t)
6027         keve key group ended)
6028     (save-excursion
6029       (set-buffer gnus-group-buffer)
6030       (goto-char start)
6031       (setq group
6032             (if (eq gnus-keep-same-level 'best)
6033                 (gnus-summary-best-group gnus-newsgroup-name)
6034               (gnus-summary-search-group backward gnus-keep-same-level))))
6035     (while (not ended)
6036       (gnus-message
6037        5 "No more%s articles%s" (if unread " unread" "")
6038        (if (and group
6039                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6040            (format " (Type %s for %s [%s])"
6041                    (single-key-description cmd) group
6042                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6043          (format " (Type %s to exit %s)"
6044                  (single-key-description cmd)
6045                  gnus-newsgroup-name)))
6046       ;; Confirm auto selection.
6047       (setq key (car (setq keve (gnus-read-event-char))))
6048       (setq ended t)
6049       (cond
6050        ((assq key keystrokes)
6051         (let ((obuf (current-buffer)))
6052           (switch-to-buffer gnus-group-buffer)
6053           (when group
6054             (gnus-group-jump-to-group group))
6055           (eval (cadr (assq key keystrokes)))
6056           (setq group (gnus-group-group-name))
6057           (switch-to-buffer obuf))
6058         (setq ended nil))
6059        ((equal key cmd)
6060         (if (or (not group)
6061                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6062             (gnus-summary-exit)
6063           (gnus-summary-next-group nil group backward)))
6064        (t
6065         (push (cdr keve) unread-command-events))))))
6066
6067 (defun gnus-summary-next-unread-article ()
6068   "Select unread article after current one."
6069   (interactive)
6070   (gnus-summary-next-article
6071    (or (not (eq gnus-summary-goto-unread 'never))
6072        (gnus-summary-last-article-p (gnus-summary-article-number)))
6073    (and gnus-auto-select-same
6074         (gnus-summary-article-subject))))
6075
6076 (defun gnus-summary-prev-article (&optional unread subject)
6077   "Select the article after the current one.
6078 If UNREAD is non-nil, only unread articles are selected."
6079   (interactive "P")
6080   (gnus-summary-next-article unread subject t))
6081
6082 (defun gnus-summary-prev-unread-article ()
6083   "Select unread article before current one."
6084   (interactive)
6085   (gnus-summary-prev-article
6086    (or (not (eq gnus-summary-goto-unread 'never))
6087        (gnus-summary-first-article-p (gnus-summary-article-number)))
6088    (and gnus-auto-select-same
6089         (gnus-summary-article-subject))))
6090
6091 (defun gnus-summary-next-page (&optional lines circular)
6092   "Show next page of the selected article.
6093 If at the end of the current article, select the next article.
6094 LINES says how many lines should be scrolled up.
6095
6096 If CIRCULAR is non-nil, go to the start of the article instead of
6097 selecting the next article when reaching the end of the current
6098 article."
6099   (interactive "P")
6100   (setq gnus-summary-buffer (current-buffer))
6101   (gnus-set-global-variables)
6102   (let ((article (gnus-summary-article-number))
6103         (article-window (get-buffer-window gnus-article-buffer t))
6104         endp)
6105     ;; If the buffer is empty, we have no article.
6106     (unless article
6107       (error "No article to select"))
6108     (gnus-configure-windows 'article)
6109     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6110         (if (and (eq gnus-summary-goto-unread 'never)
6111                  (not (gnus-summary-last-article-p article)))
6112             (gnus-summary-next-article)
6113           (gnus-summary-next-unread-article))
6114       (if (or (null gnus-current-article)
6115               (null gnus-article-current)
6116               (/= article (cdr gnus-article-current))
6117               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6118           ;; Selected subject is different from current article's.
6119           (gnus-summary-display-article article)
6120         (when article-window
6121           (gnus-eval-in-buffer-window gnus-article-buffer
6122             (setq endp (gnus-article-next-page lines)))
6123           (when endp
6124             (cond (circular
6125                    (gnus-summary-beginning-of-article))
6126                   (lines
6127                    (gnus-message 3 "End of message"))
6128                   ((null lines)
6129                    (if (and (eq gnus-summary-goto-unread 'never)
6130                             (not (gnus-summary-last-article-p article)))
6131                        (gnus-summary-next-article)
6132                      (gnus-summary-next-unread-article))))))))
6133     (gnus-summary-recenter)
6134     (gnus-summary-position-point)))
6135
6136 (defun gnus-summary-prev-page (&optional lines move)
6137   "Show previous page of selected article.
6138 Argument LINES specifies lines to be scrolled down.
6139 If MOVE, move to the previous unread article if point is at
6140 the beginning of the buffer."
6141   (interactive "P")
6142   (let ((article (gnus-summary-article-number))
6143         (article-window (get-buffer-window gnus-article-buffer t))
6144         endp)
6145     (gnus-configure-windows 'article)
6146     (if (or (null gnus-current-article)
6147             (null gnus-article-current)
6148             (/= article (cdr gnus-article-current))
6149             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6150         ;; Selected subject is different from current article's.
6151         (gnus-summary-display-article article)
6152       (gnus-summary-recenter)
6153       (when article-window
6154         (gnus-eval-in-buffer-window gnus-article-buffer
6155           (setq endp (gnus-article-prev-page lines)))
6156         (when (and move endp)
6157           (cond (lines
6158                  (gnus-message 3 "Beginning of message"))
6159                 ((null lines)
6160                  (if (and (eq gnus-summary-goto-unread 'never)
6161                           (not (gnus-summary-first-article-p article)))
6162                      (gnus-summary-prev-article)
6163                    (gnus-summary-prev-unread-article))))))))
6164   (gnus-summary-position-point))
6165
6166 (defun gnus-summary-prev-page-or-article (&optional lines)
6167   "Show previous page of selected article.
6168 Argument LINES specifies lines to be scrolled down.
6169 If at the beginning of the article, go to the next article."
6170   (interactive "P")
6171   (gnus-summary-prev-page lines t))
6172
6173 (defun gnus-summary-scroll-up (lines)
6174   "Scroll up (or down) one line current article.
6175 Argument LINES specifies lines to be scrolled up (or down if negative)."
6176   (interactive "p")
6177   (gnus-configure-windows 'article)
6178   (gnus-summary-show-thread)
6179   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6180     (gnus-eval-in-buffer-window gnus-article-buffer
6181       (cond ((> lines 0)
6182              (when (gnus-article-next-page lines)
6183                (gnus-message 3 "End of message")))
6184             ((< lines 0)
6185              (gnus-article-prev-page (- lines))))))
6186   (gnus-summary-recenter)
6187   (gnus-summary-position-point))
6188
6189 (defun gnus-summary-scroll-down (lines)
6190   "Scroll down (or up) one line current article.
6191 Argument LINES specifies lines to be scrolled down (or up if negative)."
6192   (interactive "p")
6193   (gnus-summary-scroll-up (- lines)))
6194
6195 (defun gnus-summary-next-same-subject ()
6196   "Select next article which has the same subject as current one."
6197   (interactive)
6198   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6199
6200 (defun gnus-summary-prev-same-subject ()
6201   "Select previous article which has the same subject as current one."
6202   (interactive)
6203   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6204
6205 (defun gnus-summary-next-unread-same-subject ()
6206   "Select next unread article which has the same subject as current one."
6207   (interactive)
6208   (gnus-summary-next-article t (gnus-summary-article-subject)))
6209
6210 (defun gnus-summary-prev-unread-same-subject ()
6211   "Select previous unread article which has the same subject as current one."
6212   (interactive)
6213   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6214
6215 (defun gnus-summary-first-unread-article ()
6216   "Select the first unread article.
6217 Return nil if there are no unread articles."
6218   (interactive)
6219   (prog1
6220       (when (gnus-summary-first-subject t)
6221         (gnus-summary-show-thread)
6222         (gnus-summary-first-subject t)
6223         (gnus-summary-display-article (gnus-summary-article-number)))
6224     (gnus-summary-position-point)))
6225
6226 (defun gnus-summary-first-unread-subject ()
6227   "Place the point on the subject line of the first unread article.
6228 Return nil if there are no unread articles."
6229   (interactive)
6230   (prog1
6231       (when (gnus-summary-first-subject t)
6232         (gnus-summary-show-thread)
6233         (gnus-summary-first-subject t))
6234     (gnus-summary-position-point)))
6235
6236 (defun gnus-summary-first-article ()
6237   "Select the first article.
6238 Return nil if there are no articles."
6239   (interactive)
6240   (prog1
6241       (when (gnus-summary-first-subject)
6242         (gnus-summary-show-thread)
6243         (gnus-summary-first-subject)
6244         (gnus-summary-display-article (gnus-summary-article-number)))
6245     (gnus-summary-position-point)))
6246
6247 (defun gnus-summary-best-unread-article ()
6248   "Select the unread article with the highest score."
6249   (interactive)
6250   (let ((best -1000000)
6251         (data gnus-newsgroup-data)
6252         article score)
6253     (while data
6254       (and (gnus-data-unread-p (car data))
6255            (> (setq score
6256                     (gnus-summary-article-score (gnus-data-number (car data))))
6257               best)
6258            (setq best score
6259                  article (gnus-data-number (car data))))
6260       (setq data (cdr data)))
6261     (prog1
6262         (if article
6263             (gnus-summary-goto-article article)
6264           (error "No unread articles"))
6265       (gnus-summary-position-point))))
6266
6267 (defun gnus-summary-last-subject ()
6268   "Go to the last displayed subject line in the group."
6269   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6270     (when article
6271       (gnus-summary-goto-subject article))))
6272
6273 (defun gnus-summary-goto-article (article &optional all-headers force)
6274   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6275 If ALL-HEADERS is non-nil, no header lines are hidden.
6276 If FORCE, go to the article even if it isn't displayed.  If FORCE
6277 is a number, it is the line the article is to be displayed on."
6278   (interactive
6279    (list
6280     (completing-read
6281      "Article number or Message-ID: "
6282      (mapcar (lambda (number) (list (int-to-string number)))
6283              gnus-newsgroup-limit))
6284     current-prefix-arg
6285     t))
6286   (prog1
6287       (if (and (stringp article)
6288                (string-match "@" article))
6289           (gnus-summary-refer-article article)
6290         (when (stringp article)
6291           (setq article (string-to-number article)))
6292         (if (gnus-summary-goto-subject article force)
6293             (gnus-summary-display-article article all-headers)
6294           (gnus-message 4 "Couldn't go to article %s" article) nil))
6295     (gnus-summary-position-point)))
6296
6297 (defun gnus-summary-goto-last-article ()
6298   "Go to the previously read article."
6299   (interactive)
6300   (prog1
6301       (when gnus-last-article
6302         (gnus-summary-goto-article gnus-last-article nil t))
6303     (gnus-summary-position-point)))
6304
6305 (defun gnus-summary-pop-article (number)
6306   "Pop one article off the history and go to the previous.
6307 NUMBER articles will be popped off."
6308   (interactive "p")
6309   (let (to)
6310     (setq gnus-newsgroup-history
6311           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6312     (if to
6313         (gnus-summary-goto-article (car to) nil t)
6314       (error "Article history empty")))
6315   (gnus-summary-position-point))
6316
6317 ;; Summary commands and functions for limiting the summary buffer.
6318
6319 (defun gnus-summary-limit-to-articles (n)
6320   "Limit the summary buffer to the next N articles.
6321 If not given a prefix, use the process marked articles instead."
6322   (interactive "P")
6323   (prog1
6324       (let ((articles (gnus-summary-work-articles n)))
6325         (setq gnus-newsgroup-processable nil)
6326         (gnus-summary-limit articles))
6327     (gnus-summary-position-point)))
6328
6329 (defun gnus-summary-pop-limit (&optional total)
6330   "Restore the previous limit.
6331 If given a prefix, remove all limits."
6332   (interactive "P")
6333   (when total
6334     (setq gnus-newsgroup-limits
6335           (list (mapcar (lambda (h) (mail-header-number h))
6336                         gnus-newsgroup-headers))))
6337   (unless gnus-newsgroup-limits
6338     (error "No limit to pop"))
6339   (prog1
6340       (gnus-summary-limit nil 'pop)
6341     (gnus-summary-position-point)))
6342
6343 (defun gnus-summary-limit-to-subject (subject &optional header)
6344   "Limit the summary buffer to articles that have subjects that match a regexp."
6345   (interactive "sLimit to subject (regexp): ")
6346   (unless header
6347     (setq header "subject"))
6348   (when (not (equal "" subject))
6349     (prog1
6350         (let ((articles (gnus-summary-find-matching
6351                          (or header "subject") subject 'all)))
6352           (unless articles
6353             (error "Found no matches for \"%s\"" subject))
6354           (gnus-summary-limit articles))
6355       (gnus-summary-position-point))))
6356
6357 (defun gnus-summary-limit-to-author (from)
6358   "Limit the summary buffer to articles that have authors that match a regexp."
6359   (interactive "sLimit to author (regexp): ")
6360   (gnus-summary-limit-to-subject from "from"))
6361
6362 (defun gnus-summary-limit-to-age (age &optional younger-p)
6363   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6364 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6365 articles that are younger than AGE days."
6366   (interactive
6367    (let ((younger current-prefix-arg)
6368          (days-got nil)
6369          days)
6370      (while (not days-got)
6371        (setq days (if younger
6372                       (read-string "Limit to articles within (in days): ")
6373                     (read-string "Limit to articles old than (in days): ")))
6374        (when (> (length days) 0)
6375          (setq days (read days)))
6376        (if (numberp days)
6377            (setq days-got t)
6378          (message "Please enter a number.")
6379          (sleep-for 1)))
6380      (list days younger)))
6381   (prog1
6382       (let ((data gnus-newsgroup-data)
6383             (cutoff (days-to-time age))
6384             articles d date is-younger)
6385         (while (setq d (pop data))
6386           (when (and (vectorp (gnus-data-header d))
6387                      (setq date (mail-header-date (gnus-data-header d))))
6388             (setq is-younger (time-less-p
6389                               (time-since (condition-case ()
6390                                               (date-to-time date)
6391                                             (error '(0 0))))
6392                               cutoff))
6393             (when (if younger-p
6394                       is-younger
6395                     (not is-younger))
6396               (push (gnus-data-number d) articles))))
6397         (gnus-summary-limit (nreverse articles)))
6398     (gnus-summary-position-point)))
6399
6400 (defun gnus-summary-limit-to-extra (header regexp)
6401   "Limit the summary buffer to articles that match an 'extra' header."
6402   (interactive
6403    (let ((header
6404           (intern
6405            (gnus-completing-read
6406             (symbol-name (car gnus-extra-headers))
6407             "Limit extra header:"
6408             (mapcar (lambda (x)
6409                       (cons (symbol-name x) x))
6410                     gnus-extra-headers)
6411             nil
6412             t))))
6413      (list header
6414            (read-string (format "Limit to header %s (regexp): " header)))))
6415   (when (not (equal "" regexp))
6416     (prog1
6417         (let ((articles (gnus-summary-find-matching
6418                          (cons 'extra header) regexp 'all)))
6419           (unless articles
6420             (error "Found no matches for \"%s\"" regexp))
6421           (gnus-summary-limit articles))
6422       (gnus-summary-position-point))))
6423
6424 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6425 (make-obsolete
6426  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6427
6428 (defun gnus-summary-limit-to-unread (&optional all)
6429   "Limit the summary buffer to articles that are not marked as read.
6430 If ALL is non-nil, limit strictly to unread articles."
6431   (interactive "P")
6432   (if all
6433       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6434     (gnus-summary-limit-to-marks
6435      ;; Concat all the marks that say that an article is read and have
6436      ;; those removed.
6437      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6438            gnus-killed-mark gnus-kill-file-mark
6439            gnus-low-score-mark gnus-expirable-mark
6440            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6441            gnus-duplicate-mark gnus-souped-mark)
6442      'reverse)))
6443
6444 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6445 (make-obsolete 'gnus-summary-delete-marked-with
6446                'gnus-summary-limit-exlude-marks)
6447
6448 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6449   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6450 If REVERSE, limit the summary buffer to articles that are marked
6451 with MARKS.  MARKS can either be a string of marks or a list of marks.
6452 Returns how many articles were removed."
6453   (interactive "sMarks: ")
6454   (gnus-summary-limit-to-marks marks t))
6455
6456 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6457   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6458 If REVERSE (the prefix), limit the summary buffer to articles that are
6459 not marked with MARKS.  MARKS can either be a string of marks or a
6460 list of marks.
6461 Returns how many articles were removed."
6462   (interactive "sMarks: \nP")
6463   (prog1
6464       (let ((data gnus-newsgroup-data)
6465             (marks (if (listp marks) marks
6466                      (append marks nil))) ; Transform to list.
6467             articles)
6468         (while data
6469           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6470                   (memq (gnus-data-mark (car data)) marks))
6471             (push (gnus-data-number (car data)) articles))
6472           (setq data (cdr data)))
6473         (gnus-summary-limit articles))
6474     (gnus-summary-position-point)))
6475
6476 (defun gnus-summary-limit-to-score (&optional score)
6477   "Limit to articles with score at or above SCORE."
6478   (interactive "P")
6479   (setq score (if score
6480                   (prefix-numeric-value score)
6481                 (or gnus-summary-default-score 0)))
6482   (let ((data gnus-newsgroup-data)
6483         articles)
6484     (while data
6485       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6486                 score)
6487         (push (gnus-data-number (car data)) articles))
6488       (setq data (cdr data)))
6489     (prog1
6490         (gnus-summary-limit articles)
6491       (gnus-summary-position-point))))
6492
6493 (defun gnus-summary-limit-include-thread (id)
6494   "Display all the hidden articles that in the current thread."
6495   (interactive (list (mail-header-id (gnus-summary-article-header))))
6496   (let ((articles (gnus-articles-in-thread
6497                    (gnus-id-to-thread (gnus-root-id id)))))
6498     (prog1
6499         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6500       (gnus-summary-position-point))))
6501
6502 (defun gnus-summary-limit-include-dormant ()
6503   "Display all the hidden articles that are marked as dormant.
6504 Note that this command only works on a subset of the articles currently
6505 fetched for this group."
6506   (interactive)
6507   (unless gnus-newsgroup-dormant
6508     (error "There are no dormant articles in this group"))
6509   (prog1
6510       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6511     (gnus-summary-position-point)))
6512
6513 (defun gnus-summary-limit-exclude-dormant ()
6514   "Hide all dormant articles."
6515   (interactive)
6516   (prog1
6517       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6518     (gnus-summary-position-point)))
6519
6520 (defun gnus-summary-limit-exclude-childless-dormant ()
6521   "Hide all dormant articles that have no children."
6522   (interactive)
6523   (let ((data (gnus-data-list t))
6524         articles d children)
6525     ;; Find all articles that are either not dormant or have
6526     ;; children.
6527     (while (setq d (pop data))
6528       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6529                 (and (setq children
6530                            (gnus-article-children (gnus-data-number d)))
6531                      (let (found)
6532                        (while children
6533                          (when (memq (car children) articles)
6534                            (setq children nil
6535                                  found t))
6536                          (pop children))
6537                        found)))
6538         (push (gnus-data-number d) articles)))
6539     ;; Do the limiting.
6540     (prog1
6541         (gnus-summary-limit articles)
6542       (gnus-summary-position-point))))
6543
6544 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6545   "Mark all unread excluded articles as read.
6546 If ALL, mark even excluded ticked and dormants as read."
6547   (interactive "P")
6548   (let ((articles (gnus-sorted-complement
6549                    (sort
6550                     (mapcar (lambda (h) (mail-header-number h))
6551                             gnus-newsgroup-headers)
6552                     '<)
6553                    (sort gnus-newsgroup-limit '<)))
6554         article)
6555     (setq gnus-newsgroup-unreads
6556           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6557     (if all
6558         (setq gnus-newsgroup-dormant nil
6559               gnus-newsgroup-marked nil
6560               gnus-newsgroup-reads
6561               (nconc
6562                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6563                gnus-newsgroup-reads))
6564       (while (setq article (pop articles))
6565         (unless (or (memq article gnus-newsgroup-dormant)
6566                     (memq article gnus-newsgroup-marked))
6567           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6568
6569 (defun gnus-summary-limit (articles &optional pop)
6570   (if pop
6571       ;; We pop the previous limit off the stack and use that.
6572       (setq articles (car gnus-newsgroup-limits)
6573             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6574     ;; We use the new limit, so we push the old limit on the stack.
6575     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6576   ;; Set the limit.
6577   (setq gnus-newsgroup-limit articles)
6578   (let ((total (length gnus-newsgroup-data))
6579         (data (gnus-data-find-list (gnus-summary-article-number)))
6580         (gnus-summary-mark-below nil)   ; Inhibit this.
6581         found)
6582     ;; This will do all the work of generating the new summary buffer
6583     ;; according to the new limit.
6584     (gnus-summary-prepare)
6585     ;; Hide any threads, possibly.
6586     (and gnus-show-threads
6587          gnus-thread-hide-subtree
6588          (gnus-summary-hide-all-threads))
6589     ;; Try to return to the article you were at, or one in the
6590     ;; neighborhood.
6591     (when data
6592       ;; We try to find some article after the current one.
6593       (while data
6594         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6595           (setq data nil
6596                 found t))
6597         (setq data (cdr data))))
6598     (unless found
6599       ;; If there is no data, that means that we were after the last
6600       ;; article.  The same goes when we can't find any articles
6601       ;; after the current one.
6602       (goto-char (point-max))
6603       (gnus-summary-find-prev))
6604     (gnus-set-mode-line 'summary)
6605     ;; We return how many articles were removed from the summary
6606     ;; buffer as a result of the new limit.
6607     (- total (length gnus-newsgroup-data))))
6608
6609 (defsubst gnus-invisible-cut-children (threads)
6610   (let ((num 0))
6611     (while threads
6612       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6613         (incf num))
6614       (pop threads))
6615     (< num 2)))
6616
6617 (defsubst gnus-cut-thread (thread)
6618   "Go forwards in the thread until we find an article that we want to display."
6619   (when (or (eq gnus-fetch-old-headers 'some)
6620             (eq gnus-fetch-old-headers 'invisible)
6621             (numberp gnus-fetch-old-headers)
6622             (eq gnus-build-sparse-threads 'some)
6623             (eq gnus-build-sparse-threads 'more))
6624     ;; Deal with old-fetched headers and sparse threads.
6625     (while (and
6626             thread
6627             (or
6628              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6629              (gnus-summary-article-ancient-p
6630               (mail-header-number (car thread))))
6631             (if (or (<= (length (cdr thread)) 1)
6632                     (eq gnus-fetch-old-headers 'invisible))
6633                 (setq gnus-newsgroup-limit
6634                       (delq (mail-header-number (car thread))
6635                             gnus-newsgroup-limit)
6636                       thread (cadr thread))
6637               (when (gnus-invisible-cut-children (cdr thread))
6638                 (let ((th (cdr thread)))
6639                   (while th
6640                     (if (memq (mail-header-number (caar th))
6641                               gnus-newsgroup-limit)
6642                         (setq thread (car th)
6643                               th nil)
6644                       (setq th (cdr th))))))))))
6645   thread)
6646
6647 (defun gnus-cut-threads (threads)
6648   "Cut off all uninteresting articles from the beginning of threads."
6649   (when (or (eq gnus-fetch-old-headers 'some)
6650             (eq gnus-fetch-old-headers 'invisible)
6651             (numberp gnus-fetch-old-headers)
6652             (eq gnus-build-sparse-threads 'some)
6653             (eq gnus-build-sparse-threads 'more))
6654     (let ((th threads))
6655       (while th
6656         (setcar th (gnus-cut-thread (car th)))
6657         (setq th (cdr th)))))
6658   ;; Remove nixed out threads.
6659   (delq nil threads))
6660
6661 (defun gnus-summary-initial-limit (&optional show-if-empty)
6662   "Figure out what the initial limit is supposed to be on group entry.
6663 This entails weeding out unwanted dormants, low-scored articles,
6664 fetch-old-headers verbiage, and so on."
6665   ;; Most groups have nothing to remove.
6666   (if (or gnus-inhibit-limiting
6667           (and (null gnus-newsgroup-dormant)
6668                (not (eq gnus-fetch-old-headers 'some))
6669                (not (numberp gnus-fetch-old-headers))
6670                (not (eq gnus-fetch-old-headers 'invisible))
6671                (null gnus-summary-expunge-below)
6672                (not (eq gnus-build-sparse-threads 'some))
6673                (not (eq gnus-build-sparse-threads 'more))
6674                (null gnus-thread-expunge-below)
6675                (not gnus-use-nocem)))
6676       ()                                ; Do nothing.
6677     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6678     (setq gnus-newsgroup-limit nil)
6679     (mapatoms
6680      (lambda (node)
6681        (unless (car (symbol-value node))
6682          ;; These threads have no parents -- they are roots.
6683          (let ((nodes (cdr (symbol-value node)))
6684                thread)
6685            (while nodes
6686              (if (and gnus-thread-expunge-below
6687                       (< (gnus-thread-total-score (car nodes))
6688                          gnus-thread-expunge-below))
6689                  (gnus-expunge-thread (pop nodes))
6690                (setq thread (pop nodes))
6691                (gnus-summary-limit-children thread))))))
6692      gnus-newsgroup-dependencies)
6693     ;; If this limitation resulted in an empty group, we might
6694     ;; pop the previous limit and use it instead.
6695     (when (and (not gnus-newsgroup-limit)
6696                show-if-empty)
6697       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6698     gnus-newsgroup-limit))
6699
6700 (defun gnus-summary-limit-children (thread)
6701   "Return 1 if this subthread is visible and 0 if it is not."
6702   ;; First we get the number of visible children to this thread.  This
6703   ;; is done by recursing down the thread using this function, so this
6704   ;; will really go down to a leaf article first, before slowly
6705   ;; working its way up towards the root.
6706   (when thread
6707     (let ((children
6708            (if (cdr thread)
6709                (apply '+ (mapcar 'gnus-summary-limit-children
6710                                  (cdr thread)))
6711              0))
6712           (number (mail-header-number (car thread)))
6713           score)
6714       (if (and
6715            (not (memq number gnus-newsgroup-marked))
6716            (or
6717             ;; If this article is dormant and has absolutely no visible
6718             ;; children, then this article isn't visible.
6719             (and (memq number gnus-newsgroup-dormant)
6720                  (zerop children))
6721             ;; If this is "fetch-old-headered" and there is no
6722             ;; visible children, then we don't want this article.
6723             (and (or (eq gnus-fetch-old-headers 'some)
6724                      (numberp gnus-fetch-old-headers))
6725                  (gnus-summary-article-ancient-p number)
6726                  (zerop children))
6727             ;; If this is "fetch-old-headered" and `invisible', then
6728             ;; we don't want this article.
6729             (and (eq gnus-fetch-old-headers 'invisible)
6730                  (gnus-summary-article-ancient-p number))
6731             ;; If this is a sparsely inserted article with no children,
6732             ;; we don't want it.
6733             (and (eq gnus-build-sparse-threads 'some)
6734                  (gnus-summary-article-sparse-p number)
6735                  (zerop children))
6736             ;; If we use expunging, and this article is really
6737             ;; low-scored, then we don't want this article.
6738             (when (and gnus-summary-expunge-below
6739                        (< (setq score
6740                                 (or (cdr (assq number gnus-newsgroup-scored))
6741                                     gnus-summary-default-score))
6742                           gnus-summary-expunge-below))
6743               ;; We increase the expunge-tally here, but that has
6744               ;; nothing to do with the limits, really.
6745               (incf gnus-newsgroup-expunged-tally)
6746               ;; We also mark as read here, if that's wanted.
6747               (when (and gnus-summary-mark-below
6748                          (< score gnus-summary-mark-below))
6749                 (setq gnus-newsgroup-unreads
6750                       (delq number gnus-newsgroup-unreads))
6751                 (if gnus-newsgroup-auto-expire
6752                     (push number gnus-newsgroup-expirable)
6753                   (push (cons number gnus-low-score-mark)
6754                         gnus-newsgroup-reads)))
6755               t)
6756             ;; Check NoCeM things.
6757             (if (and gnus-use-nocem
6758                      (gnus-nocem-unwanted-article-p
6759                       (mail-header-id (car thread))))
6760                 (progn
6761                   (setq gnus-newsgroup-unreads
6762                         (delq number gnus-newsgroup-unreads))
6763                   t))))
6764           ;; Nope, invisible article.
6765           0
6766         ;; Ok, this article is to be visible, so we add it to the limit
6767         ;; and return 1.
6768         (push number gnus-newsgroup-limit)
6769         1))))
6770
6771 (defun gnus-expunge-thread (thread)
6772   "Mark all articles in THREAD as read."
6773   (let* ((number (mail-header-number (car thread))))
6774     (incf gnus-newsgroup-expunged-tally)
6775     ;; We also mark as read here, if that's wanted.
6776     (setq gnus-newsgroup-unreads
6777           (delq number gnus-newsgroup-unreads))
6778     (if gnus-newsgroup-auto-expire
6779         (push number gnus-newsgroup-expirable)
6780       (push (cons number gnus-low-score-mark)
6781             gnus-newsgroup-reads)))
6782   ;; Go recursively through all subthreads.
6783   (mapcar 'gnus-expunge-thread (cdr thread)))
6784
6785 ;; Summary article oriented commands
6786
6787 (defun gnus-summary-refer-parent-article (n)
6788   "Refer parent article N times.
6789 If N is negative, go to ancestor -N instead.
6790 The difference between N and the number of articles fetched is returned."
6791   (interactive "p")
6792   (let ((skip 1)
6793         error header ref)
6794     (when (not (natnump n))
6795       (setq skip (abs n)
6796             n 1))
6797     (while (and (> n 0)
6798                 (not error))
6799       (setq header (gnus-summary-article-header))
6800       (if (and (eq (mail-header-number header)
6801                    (cdr gnus-article-current))
6802                (equal gnus-newsgroup-name
6803                       (car gnus-article-current)))
6804           ;; If we try to find the parent of the currently
6805           ;; displayed article, then we take a look at the actual
6806           ;; References header, since this is slightly more
6807           ;; reliable than the References field we got from the
6808           ;; server.
6809           (save-excursion
6810             (set-buffer gnus-original-article-buffer)
6811             (nnheader-narrow-to-headers)
6812             (unless (setq ref (message-fetch-field "references"))
6813               (setq ref (message-fetch-field "in-reply-to")))
6814             (widen))
6815         (setq ref
6816               ;; It's not the current article, so we take a bet on
6817               ;; the value we got from the server.
6818               (mail-header-references header)))
6819       (if (and ref
6820                (not (equal ref "")))
6821           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6822             (gnus-message 1 "Couldn't find parent"))
6823         (gnus-message 1 "No references in article %d"
6824                       (gnus-summary-article-number))
6825         (setq error t))
6826       (decf n))
6827     (gnus-summary-position-point)
6828     n))
6829
6830 (defun gnus-summary-refer-references ()
6831   "Fetch all articles mentioned in the References header.
6832 Return the number of articles fetched."
6833   (interactive)
6834   (let ((ref (mail-header-references (gnus-summary-article-header)))
6835         (current (gnus-summary-article-number))
6836         (n 0))
6837     (if (or (not ref)
6838             (equal ref ""))
6839         (error "No References in the current article")
6840       ;; For each Message-ID in the References header...
6841       (while (string-match "<[^>]*>" ref)
6842         (incf n)
6843         ;; ... fetch that article.
6844         (gnus-summary-refer-article
6845          (prog1 (match-string 0 ref)
6846            (setq ref (substring ref (match-end 0))))))
6847       (gnus-summary-goto-subject current)
6848       (gnus-summary-position-point)
6849       n)))
6850
6851 (defun gnus-summary-refer-thread (&optional limit)
6852   "Fetch all articles in the current thread.
6853 If LIMIT (the numerical prefix), fetch that many old headers instead
6854 of what's specified by the `gnus-refer-thread-limit' variable."
6855   (interactive "P")
6856   (let ((id (mail-header-id (gnus-summary-article-header)))
6857         (limit (if limit (prefix-numeric-value limit)
6858                  gnus-refer-thread-limit)))
6859     ;; We want to fetch LIMIT *old* headers, but we also have to
6860     ;; re-fetch all the headers in the current buffer, because many of
6861     ;; them may be undisplayed.  So we adjust LIMIT.
6862     (when (numberp limit)
6863       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6864     (unless (eq gnus-fetch-old-headers 'invisible)
6865       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6866       ;; Retrieve the headers and read them in.
6867       (if (eq (gnus-retrieve-headers
6868                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6869               'nov)
6870           (gnus-build-all-threads)
6871         (error "Can't fetch thread from backends that don't support NOV"))
6872       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6873     (gnus-summary-limit-include-thread id)))
6874
6875 (defun gnus-summary-refer-article (message-id)
6876   "Fetch an article specified by MESSAGE-ID."
6877   (interactive "sMessage-ID: ")
6878   (when (and (stringp message-id)
6879              (not (zerop (length message-id))))
6880     ;; Construct the correct Message-ID if necessary.
6881     ;; Suggested by tale@pawl.rpi.edu.
6882     (unless (string-match "^<" message-id)
6883       (setq message-id (concat "<" message-id)))
6884     (unless (string-match ">$" message-id)
6885       (setq message-id (concat message-id ">")))
6886     (let* ((header (gnus-id-to-header message-id))
6887            (sparse (and header
6888                         (gnus-summary-article-sparse-p
6889                          (mail-header-number header))
6890                         (memq (mail-header-number header)
6891                               gnus-newsgroup-limit)))
6892            number)
6893       (cond
6894        ;; If the article is present in the buffer we just go to it.
6895        ((and header
6896              (or (not (gnus-summary-article-sparse-p
6897                        (mail-header-number header)))
6898                  sparse))
6899         (prog1
6900             (gnus-summary-goto-article
6901              (mail-header-number header) nil t)
6902           (when sparse
6903             (gnus-summary-update-article (mail-header-number header)))))
6904        (t
6905         ;; We fetch the article.
6906         (catch 'found
6907           (dolist (gnus-override-method (gnus-refer-article-methods))
6908             (gnus-check-server gnus-override-method)
6909             ;; Fetch the header, and display the article.
6910             (when (setq number (gnus-summary-insert-subject message-id))
6911               (gnus-summary-select-article nil nil nil number)
6912               (throw 'found t)))
6913           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6914
6915 (defun gnus-refer-article-methods ()
6916   "Return a list of referrable methods."
6917   (cond
6918    ;; No method, so we default to current and native.
6919    ((null gnus-refer-article-method)
6920     (list gnus-current-select-method gnus-select-method))
6921    ;; Current.
6922    ((eq 'current gnus-refer-article-method)
6923     (list gnus-current-select-method))
6924    ;; List of select methods.
6925    ((not (stringp (cadr gnus-refer-article-method)))
6926     (let (out)
6927       (dolist (method gnus-refer-article-method)
6928         (push (if (eq 'current method)
6929                   gnus-current-select-method
6930                 method)
6931               out))
6932       (nreverse out)))
6933    ;; One single select method.
6934    (t
6935     (list gnus-refer-article-method))))
6936
6937 (defun gnus-summary-edit-parameters ()
6938   "Edit the group parameters of the current group."
6939   (interactive)
6940   (gnus-group-edit-group gnus-newsgroup-name 'params))
6941
6942 (defun gnus-summary-customize-parameters ()
6943   "Customize the group parameters of the current group."
6944   (interactive)
6945   (gnus-group-customize gnus-newsgroup-name))
6946
6947 (defun gnus-summary-enter-digest-group (&optional force)
6948   "Enter an nndoc group based on the current article.
6949 If FORCE, force a digest interpretation.  If not, try
6950 to guess what the document format is."
6951   (interactive "P")
6952   (let ((conf gnus-current-window-configuration))
6953     (save-excursion
6954       (gnus-summary-select-article))
6955     (setq gnus-current-window-configuration conf)
6956     (let* ((name (format "%s-%d"
6957                          (gnus-group-prefixed-name
6958                           gnus-newsgroup-name (list 'nndoc ""))
6959                          (save-excursion
6960                            (set-buffer gnus-summary-buffer)
6961                            gnus-current-article)))
6962            (ogroup gnus-newsgroup-name)
6963            (params (append (gnus-info-params (gnus-get-info ogroup))
6964                            (list (cons 'to-group ogroup))
6965                            (list (cons 'save-article-group ogroup))))
6966            (case-fold-search t)
6967            (buf (current-buffer))
6968            dig to-address)
6969       (save-excursion
6970         (set-buffer gnus-original-article-buffer)
6971         ;; Have the digest group inherit the main mail address of
6972         ;; the parent article.
6973         (when (setq to-address (or (message-fetch-field "reply-to")
6974                                    (message-fetch-field "from")))
6975           (setq params (append 
6976                         (list (cons 'to-address 
6977                                     (funcall gnus-decode-encoded-word-function
6978                                              to-address))))))
6979         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6980         (insert-buffer-substring gnus-original-article-buffer)
6981         ;; Remove lines that may lead nndoc to misinterpret the
6982         ;; document type.
6983         (narrow-to-region
6984          (goto-char (point-min))
6985          (or (search-forward "\n\n" nil t) (point)))
6986         (goto-char (point-min))
6987         (delete-matching-lines "^Path:\\|^From ")
6988         (widen))
6989       (unwind-protect
6990           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6991                     (gnus-newsgroup-ephemeral-ignored-charsets
6992                      gnus-newsgroup-ignored-charsets))
6993                 (gnus-group-read-ephemeral-group
6994                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6995                               (nndoc-article-type
6996                                ,(if force 'mbox 'guess))) t))
6997               ;; Make all postings to this group go to the parent group.
6998               (nconc (gnus-info-params (gnus-get-info name))
6999                      params)
7000             ;; Couldn't select this doc group.
7001             (switch-to-buffer buf)
7002             (gnus-set-global-variables)
7003             (gnus-configure-windows 'summary)
7004             (gnus-message 3 "Article couldn't be entered?"))
7005         (kill-buffer dig)))))
7006
7007 (defun gnus-summary-read-document (n)
7008   "Open a new group based on the current article(s).
7009 This will allow you to read digests and other similar
7010 documents as newsgroups.
7011 Obeys the standard process/prefix convention."
7012   (interactive "P")
7013   (let* ((articles (gnus-summary-work-articles n))
7014          (ogroup gnus-newsgroup-name)
7015          (params (append (gnus-info-params (gnus-get-info ogroup))
7016                          (list (cons 'to-group ogroup))))
7017          article group egroup groups vgroup)
7018     (while (setq article (pop articles))
7019       (setq group (format "%s-%d" gnus-newsgroup-name article))
7020       (gnus-summary-remove-process-mark article)
7021       (when (gnus-summary-display-article article)
7022         (save-excursion
7023           (with-temp-buffer
7024             (insert-buffer-substring gnus-original-article-buffer)
7025             ;; Remove some headers that may lead nndoc to make
7026             ;; the wrong guess.
7027             (message-narrow-to-head)
7028             (goto-char (point-min))
7029             (delete-matching-lines "^\\(Path\\):\\|^From ")
7030             (widen)
7031             (if (setq egroup
7032                       (gnus-group-read-ephemeral-group
7033                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7034                                      (nndoc-article-type guess))
7035                        t nil t))
7036                 (progn
7037                   ;; Make all postings to this group go to the parent group.
7038                   (nconc (gnus-info-params (gnus-get-info egroup))
7039                          params)
7040                   (push egroup groups))
7041               ;; Couldn't select this doc group.
7042               (gnus-error 3 "Article couldn't be entered"))))))
7043     ;; Now we have selected all the documents.
7044     (cond
7045      ((not groups)
7046       (error "None of the articles could be interpreted as documents"))
7047      ((gnus-group-read-ephemeral-group
7048        (setq vgroup (format
7049                      "nnvirtual:%s-%s" gnus-newsgroup-name
7050                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7051        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7052        t
7053        (cons (current-buffer) 'summary)))
7054      (t
7055       (error "Couldn't select virtual nndoc group")))))
7056
7057 (defun gnus-summary-isearch-article (&optional regexp-p)
7058   "Do incremental search forward on the current article.
7059 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7060   (interactive "P")
7061   (let* ((gnus-inhibit-treatment t)
7062          (old (gnus-summary-select-article)))
7063     (gnus-configure-windows 'article)
7064     (gnus-eval-in-buffer-window gnus-article-buffer
7065       (save-restriction
7066         (widen)
7067         (when (eq 'old old)
7068           (gnus-article-show-all-headers))
7069         (goto-char (point-min))
7070         (isearch-forward regexp-p)))))
7071
7072 (defun gnus-summary-search-article-forward (regexp &optional backward)
7073   "Search for an article containing REGEXP forward.
7074 If BACKWARD, search backward instead."
7075   (interactive
7076    (list (read-string
7077           (format "Search article %s (regexp%s): "
7078                   (if current-prefix-arg "backward" "forward")
7079                   (if gnus-last-search-regexp
7080                       (concat ", default " gnus-last-search-regexp)
7081                     "")))
7082          current-prefix-arg))
7083   (if (string-equal regexp "")
7084       (setq regexp (or gnus-last-search-regexp ""))
7085     (setq gnus-last-search-regexp regexp))
7086   (if (gnus-summary-search-article regexp backward)
7087       (gnus-summary-show-thread)
7088     (error "Search failed: \"%s\"" regexp)))
7089
7090 (defun gnus-summary-search-article-backward (regexp)
7091   "Search for an article containing REGEXP backward."
7092   (interactive
7093    (list (read-string
7094           (format "Search article backward (regexp%s): "
7095                   (if gnus-last-search-regexp
7096                       (concat ", default " gnus-last-search-regexp)
7097                     "")))))
7098   (gnus-summary-search-article-forward regexp 'backward))
7099
7100 (eval-when-compile
7101   (defmacro gnus-summary-search-article-position-point (regexp backward)
7102     "Dehighlight the last matched text and goto the beginning position."
7103     (` (if (and gnus-summary-search-article-matched-data
7104                 (let ((text (caddr gnus-summary-search-article-matched-data))
7105                       (inhibit-read-only t)
7106                       buffer-read-only)
7107                   (delete-region
7108                    (goto-char (car gnus-summary-search-article-matched-data))
7109                    (cadr gnus-summary-search-article-matched-data))
7110                   (insert text)
7111                   (string-match (, regexp) text)))
7112            (if (, backward) (beginning-of-line) (end-of-line))
7113          (goto-char (if (, backward) (point-max) (point-min))))))
7114
7115   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7116     "Place point where X-Face image is displayed."
7117     (if (featurep 'xemacs)
7118         (` (let ((end (if (search-forward "\n\n" nil t)
7119                           (goto-char (1- (point)))
7120                         (point-min)))
7121                  extent)
7122              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7123              (unless (and (re-search-forward "^From:" end t)
7124                           (setq extent (extent-at (point)))
7125                           (extent-begin-glyph extent))
7126                (goto-char (, opoint)))))
7127       (` (let ((end (if (search-forward "\n\n" nil t)
7128                         (goto-char (1- (point)))
7129                       (point-min))))
7130            (goto-char
7131             (or (text-property-any (or (search-backward "\n\n" nil t)
7132                                        (point-min))
7133                                    end 'x-face-mule-bitmap-image t)
7134                 (, opoint)))))))
7135
7136   (defmacro gnus-summary-search-article-highlight-matched-text
7137     (backward treated x-face)
7138     "Highlight matched text in the function `gnus-summary-search-article'."
7139     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7140              (end (set-marker (make-marker) (match-end 0)))
7141              (inhibit-read-only t)
7142              buffer-read-only)
7143          (unless treated
7144            (let ((,@
7145                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7146                     (mapcar
7147                      (lambda (item) (setq items (delq item items)))
7148                      '(gnus-treat-buttonize
7149                        gnus-treat-fill-article
7150                        gnus-treat-fill-long-lines
7151                        gnus-treat-emphasize
7152                        gnus-treat-highlight-headers
7153                        gnus-treat-highlight-citation
7154                        gnus-treat-highlight-signature
7155                        gnus-treat-overstrike
7156                        gnus-treat-display-xface
7157                        gnus-treat-buttonize-head
7158                        gnus-treat-decode-article-as-default-mime-charset))
7159                     (static-if (featurep 'xemacs)
7160                         items
7161                       (cons '(x-face-mule-delete-x-face-field
7162                               (quote never))
7163                             items))))
7164                  (gnus-treat-display-xface
7165                   (when (, x-face) gnus-treat-display-xface)))
7166              (gnus-article-prepare-mime-display)))
7167          (goto-char (if (, backward) start end))
7168          (when (, x-face)
7169            (gnus-summary-search-article-highlight-goto-x-face (point)))
7170          (setq gnus-summary-search-article-matched-data
7171                (list start end (buffer-substring start end)))
7172          (unless (eq start end);; matched text has been deleted. :-<
7173            (put-text-property start end 'face
7174                               (or (find-face 'isearch)
7175                                   'secondary-selection))))))
7176   )
7177
7178 (defun gnus-summary-search-article (regexp &optional backward)
7179   "Search for an article containing REGEXP.
7180 Optional argument BACKWARD means do search for backward.
7181 `gnus-select-article-hook' is not called during the search."
7182   ;; We have to require this here to make sure that the following
7183   ;; dynamic binding isn't shadowed by autoloading.
7184   (require 'gnus-async)
7185   (require 'gnus-art)
7186   (let ((gnus-select-article-hook nil)  ;Disable hook.
7187         (gnus-article-display-hook nil)
7188         (gnus-article-prepare-hook nil)
7189         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7190         (gnus-use-article-prefetch nil)
7191         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7192         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7193         (sum (current-buffer))
7194         (found nil)
7195         point treated)
7196     (gnus-save-hidden-threads
7197       (static-if (featurep 'xemacs)
7198           (let ((gnus-inhibit-treatment t))
7199             (setq treated (eq 'old (gnus-summary-select-article)))
7200             (when (and treated
7201                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7202                                  (window-live-p (get-buffer-window
7203                                                  gnus-article-buffer t)))))
7204               (gnus-summary-select-article nil t)
7205               (setq treated nil)))
7206         (let ((gnus-inhibit-treatment t)
7207               (x-face-mule-delete-x-face-field 'never))
7208           (setq treated (eq 'old (gnus-summary-select-article)))
7209           (when (and treated
7210                      (not
7211                       (and (gnus-buffer-live-p gnus-article-buffer)
7212                            (window-live-p (get-buffer-window
7213                                            gnus-article-buffer t))
7214                            (or (not (string-match "^\\^X-Face:" regexp))
7215                                (with-current-buffer gnus-article-buffer
7216                                  gnus-summary-search-article-matched-data)))))
7217             (gnus-summary-select-article nil t)
7218             (setq treated nil))))
7219       (set-buffer gnus-article-buffer)
7220       (widen)
7221       (if treated
7222           (progn
7223             (gnus-article-show-all-headers)
7224             (gnus-summary-search-article-position-point regexp backward))
7225         (goto-char (if backward (point-max) (point-min))))
7226       (while (not found)
7227         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7228         (if (if backward
7229                 (re-search-backward regexp nil t)
7230               (re-search-forward regexp nil t))
7231             ;; We found the regexp.
7232             (progn
7233               (gnus-summary-search-article-highlight-matched-text
7234                backward treated (string-match "^\\^X-Face:" regexp))
7235               (setq found 'found)
7236               (forward-line
7237                (/ (- 2 (window-height
7238                         (get-buffer-window gnus-article-buffer t)))
7239                   2))
7240               (set-window-start
7241                (get-buffer-window (current-buffer))
7242                (point))
7243               (set-buffer sum)
7244               (setq point (point)))
7245           ;; We didn't find it, so we go to the next article.
7246           (set-buffer sum)
7247           (setq found 'not)
7248           (while (eq found 'not)
7249             (if (not (if backward (gnus-summary-find-prev)
7250                        (gnus-summary-find-next)))
7251                 ;; No more articles.
7252                 (setq found t)
7253               ;; Select the next article and adjust point.
7254               (unless (gnus-summary-article-sparse-p
7255                        (gnus-summary-article-number))
7256                 (setq found nil)
7257                 (let ((gnus-inhibit-treatment t))
7258                   (gnus-summary-select-article))
7259                 (setq treated nil)
7260                 (set-buffer gnus-article-buffer)
7261                 (widen)
7262                 (goto-char (if backward (point-max) (point-min))))))))
7263       (gnus-message 7 ""))
7264     ;; Return whether we found the regexp.
7265     (when (eq found 'found)
7266       (goto-char point)
7267       (gnus-summary-show-thread)
7268       (gnus-summary-goto-subject gnus-current-article)
7269       (gnus-summary-position-point)
7270       t)))
7271
7272 (defun gnus-summary-find-matching (header regexp &optional backward unread
7273                                           not-case-fold)
7274   "Return a list of all articles that match REGEXP on HEADER.
7275 The search stars on the current article and goes forwards unless
7276 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7277 If UNREAD is non-nil, only unread articles will
7278 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7279 in the comparisons."
7280   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7281                 (gnus-data-find-list
7282                  (gnus-summary-article-number) (gnus-data-list backward))))
7283         (case-fold-search (not not-case-fold))
7284         articles d func)
7285     (if (consp header)
7286         (if (eq (car header) 'extra)
7287             (setq func
7288                   `(lambda (h)
7289                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7290                          "")))
7291           (error "%s is an invalid header" header))
7292       (unless (fboundp (intern (concat "mail-header-" header)))
7293         (error "%s is not a valid header" header))
7294       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7295     (while data
7296       (setq d (car data))
7297       (and (or (not unread)             ; We want all articles...
7298                (gnus-data-unread-p d))  ; Or just unreads.
7299            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7300            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7301            (push (gnus-data-number d) articles)) ; Success!
7302       (setq data (cdr data)))
7303     (nreverse articles)))
7304
7305 (defun gnus-summary-execute-command (header regexp command &optional backward)
7306   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7307 If HEADER is an empty string (or nil), the match is done on the entire
7308 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7309   (interactive
7310    (list (let ((completion-ignore-case t))
7311            (completing-read
7312             "Header name: "
7313             (mapcar (lambda (string) (list string))
7314                     '("Number" "Subject" "From" "Lines" "Date"
7315                       "Message-ID" "Xref" "References" "Body"))
7316             nil 'require-match))
7317          (read-string "Regexp: ")
7318          (read-key-sequence "Command: ")
7319          current-prefix-arg))
7320   (when (equal header "Body")
7321     (setq header ""))
7322   ;; Hidden thread subtrees must be searched as well.
7323   (gnus-summary-show-all-threads)
7324   ;; We don't want to change current point nor window configuration.
7325   (save-excursion
7326     (save-window-excursion
7327       (gnus-message 6 "Executing %s..." (key-description command))
7328       ;; We'd like to execute COMMAND interactively so as to give arguments.
7329       (gnus-execute header regexp
7330                     `(call-interactively ',(key-binding command))
7331                     backward)
7332       (gnus-message 6 "Executing %s...done" (key-description command)))))
7333
7334 (defun gnus-summary-beginning-of-article ()
7335   "Scroll the article back to the beginning."
7336   (interactive)
7337   (gnus-summary-select-article)
7338   (gnus-configure-windows 'article)
7339   (gnus-eval-in-buffer-window gnus-article-buffer
7340     (widen)
7341     (goto-char (point-min))
7342     (when gnus-page-broken
7343       (gnus-narrow-to-page))))
7344
7345 (defun gnus-summary-end-of-article ()
7346   "Scroll to the end of the article."
7347   (interactive)
7348   (gnus-summary-select-article)
7349   (gnus-configure-windows 'article)
7350   (gnus-eval-in-buffer-window gnus-article-buffer
7351     (widen)
7352     (goto-char (point-max))
7353     (recenter -3)
7354     (when gnus-page-broken
7355       (gnus-narrow-to-page))))
7356
7357 (defun gnus-summary-print-article (&optional filename n)
7358   "Generate and print a PostScript image of the N next (mail) articles.
7359
7360 If N is negative, print the N previous articles.  If N is nil and articles
7361 have been marked with the process mark, print these instead.
7362
7363 If the optional first argument FILENAME is nil, send the image to the
7364 printer.  If FILENAME is a string, save the PostScript image in a file with
7365 that name.  If FILENAME is a number, prompt the user for the name of the file
7366 to save in."
7367   (interactive (list (ps-print-preprint current-prefix-arg)
7368                      current-prefix-arg))
7369   (dolist (article (gnus-summary-work-articles n))
7370     (gnus-summary-select-article nil nil 'pseudo article)
7371     (gnus-eval-in-buffer-window gnus-article-buffer
7372       (let ((buffer (generate-new-buffer " *print*")))
7373         (unwind-protect
7374             (progn
7375               (copy-to-buffer buffer (point-min) (point-max))
7376               (set-buffer buffer)
7377               (gnus-article-delete-invisible-text)
7378               (let ((ps-left-header
7379                      (list
7380                       (concat "("
7381                               (mail-header-subject gnus-current-headers) ")")
7382                       (concat "("
7383                               (mail-header-from gnus-current-headers) ")")))
7384                     (ps-right-header
7385                      (list
7386                       "/pagenumberstring load"
7387                       (concat "("
7388                               (mail-header-date gnus-current-headers) ")"))))
7389                 (gnus-run-hooks 'gnus-ps-print-hook)
7390                 (save-excursion
7391                   (ps-print-buffer-with-faces filename))))
7392           (kill-buffer buffer))))))
7393
7394 (defun gnus-summary-show-article (&optional arg)
7395   "Force re-fetching of the current article.
7396 If ARG (the prefix) is a number, show the article with the charset
7397 defined in `gnus-summary-show-article-charset-alist', or the charset
7398 inputed.
7399 If ARG (the prefix) is non-nil and not a number, show the raw article
7400 without any article massaging functions being run."
7401   (interactive "P")
7402   (cond
7403    ((numberp arg)
7404     (let ((gnus-newsgroup-charset
7405            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7406                (read-coding-system "Charset: ")))
7407           (gnus-newsgroup-ignored-charsets 'gnus-all))
7408       (gnus-summary-select-article nil 'force)))
7409    ((not arg)
7410     ;; Select the article the normal way.
7411     (gnus-summary-select-article nil 'force))
7412    (t
7413     ;; We have to require this here to make sure that the following
7414     ;; dynamic binding isn't shadowed by autoloading.
7415     (require 'gnus-async)
7416     (require 'gnus-art)
7417     ;; Bind the article treatment functions to nil.
7418     (let ((gnus-have-all-headers t)
7419           gnus-article-display-hook
7420           gnus-article-prepare-hook
7421           gnus-article-decode-hook
7422           gnus-break-pages
7423           gnus-show-mime)
7424       (gnus-summary-select-article nil 'force))))
7425   (gnus-summary-goto-subject gnus-current-article)
7426   (gnus-summary-position-point))
7427
7428 (defun gnus-summary-verbose-headers (&optional arg)
7429   "Toggle permanent full header display.
7430 If ARG is a positive number, turn header display on.
7431 If ARG is a negative number, turn header display off."
7432   (interactive "P")
7433   (setq gnus-show-all-headers
7434         (cond ((or (not (numberp arg))
7435                    (zerop arg))
7436                (not gnus-show-all-headers))
7437               ((natnump arg)
7438                t)))
7439   (gnus-summary-show-article))
7440
7441 (defun gnus-summary-toggle-header (&optional arg)
7442   "Show the headers if they are hidden, or hide them if they are shown.
7443 If ARG is a positive number, show the entire header.
7444 If ARG is a negative number, hide the unwanted header lines."
7445   (interactive "P")
7446   (save-excursion
7447     (set-buffer gnus-article-buffer)
7448     (save-restriction
7449       (let* ((buffer-read-only nil)
7450              (inhibit-point-motion-hooks t)
7451              hidden e)
7452         (setq hidden
7453               (if (numberp arg)
7454                   (>= arg 0)
7455                 (save-restriction
7456                   (article-narrow-to-head)
7457                   (gnus-article-hidden-text-p 'headers))))
7458         (goto-char (point-min))
7459         (when (search-forward "\n\n" nil t)
7460           (delete-region (point-min) (1- (point))))
7461         (goto-char (point-min))
7462         (save-excursion
7463           (set-buffer gnus-original-article-buffer)
7464           (goto-char (point-min))
7465           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7466         (insert-buffer-substring gnus-original-article-buffer 1 e)
7467         (save-restriction
7468           (narrow-to-region (point-min) (point))
7469           (article-decode-encoded-words)
7470           (if  hidden
7471               (let ((gnus-treat-hide-headers nil)
7472                     (gnus-treat-hide-boring-headers nil))
7473                 (setq gnus-article-wash-types
7474                       (delq 'headers gnus-article-wash-types))
7475                 (gnus-treat-article 'head))
7476             (gnus-treat-article 'head)))
7477         (gnus-set-mode-line 'article)))))
7478
7479 (defun gnus-summary-show-all-headers ()
7480   "Make all header lines visible."
7481   (interactive)
7482   (gnus-article-show-all-headers))
7483
7484 (defun gnus-summary-toggle-mime (&optional arg)
7485   "Toggle MIME processing.
7486 If ARG is a positive number, turn MIME processing on."
7487   (interactive "P")
7488   (setq gnus-show-mime
7489         (if (null arg)
7490             (not gnus-show-mime)
7491           (> (prefix-numeric-value arg) 0)))
7492   (gnus-summary-select-article t 'force))
7493
7494 (defun gnus-summary-caesar-message (&optional arg)
7495   "Caesar rotate the current article by 13.
7496 The numerical prefix specifies how many places to rotate each letter
7497 forward."
7498   (interactive "P")
7499   (gnus-summary-select-article)
7500   (let ((mail-header-separator ""))
7501     (gnus-eval-in-buffer-window gnus-article-buffer
7502       (save-restriction
7503         (widen)
7504         (let ((start (window-start))
7505               buffer-read-only)
7506           (message-caesar-buffer-body arg)
7507           (set-window-start (get-buffer-window (current-buffer)) start))))))
7508
7509 (defun gnus-summary-stop-page-breaking ()
7510   "Stop page breaking in the current article."
7511   (interactive)
7512   (gnus-summary-select-article)
7513   (gnus-eval-in-buffer-window gnus-article-buffer
7514     (widen)
7515     (when (gnus-visual-p 'page-marker)
7516       (let ((buffer-read-only nil))
7517         (gnus-remove-text-with-property 'gnus-prev)
7518         (gnus-remove-text-with-property 'gnus-next))
7519       (setq gnus-page-broken nil))))
7520
7521 (defun gnus-summary-move-article (&optional n to-newsgroup
7522                                             select-method action)
7523   "Move the current article to a different newsgroup.
7524 If N is a positive number, move the N next articles.
7525 If N is a negative number, move the N previous articles.
7526 If N is nil and any articles have been marked with the process mark,
7527 move those articles instead.
7528 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7529 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7530 re-spool using this method.
7531
7532 For this function to work, both the current newsgroup and the
7533 newsgroup that you want to move to have to support the `request-move'
7534 and `request-accept' functions.
7535
7536 ACTION can be either `move' (the default), `crosspost' or `copy'."
7537   (interactive "P")
7538   (unless action
7539     (setq action 'move))
7540   ;; Disable marking as read.
7541   (let (gnus-mark-article-hook)
7542     (save-window-excursion
7543       (gnus-summary-select-article)))
7544   ;; Check whether the source group supports the required functions.
7545   (cond ((and (eq action 'move)
7546               (not (gnus-check-backend-function
7547                     'request-move-article gnus-newsgroup-name)))
7548          (error "The current group does not support article moving"))
7549         ((and (eq action 'crosspost)
7550               (not (gnus-check-backend-function
7551                     'request-replace-article gnus-newsgroup-name)))
7552          (error "The current group does not support article editing")))
7553   (let ((articles (gnus-summary-work-articles n))
7554         (prefix (if (gnus-check-backend-function
7555                     'request-move-article gnus-newsgroup-name)
7556                     (gnus-group-real-prefix gnus-newsgroup-name)
7557                   ""))
7558         (names '((move "Move" "Moving")
7559                  (copy "Copy" "Copying")
7560                  (crosspost "Crosspost" "Crossposting")))
7561         (copy-buf (save-excursion
7562                     (nnheader-set-temp-buffer " *copy article*")))
7563         (default-marks gnus-article-mark-lists)
7564         (no-expire-marks (delete '(expirable . expire)
7565                                  (copy-sequence gnus-article-mark-lists)))
7566         art-group to-method new-xref article to-groups)
7567     (unless (assq action names)
7568       (error "Unknown action %s" action))
7569     ;; Read the newsgroup name.
7570     (when (and (not to-newsgroup)
7571                (not select-method))
7572       (setq to-newsgroup
7573             (gnus-read-move-group-name
7574              (cadr (assq action names))
7575              (symbol-value (intern (format "gnus-current-%s-group" action)))
7576              articles prefix))
7577       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7578     (setq to-method (or select-method
7579                         (gnus-server-to-method
7580                          (gnus-group-method to-newsgroup))))
7581     ;; Check the method we are to move this article to...
7582     (unless (gnus-check-backend-function
7583              'request-accept-article (car to-method))
7584       (error "%s does not support article copying" (car to-method)))
7585     (unless (gnus-check-server to-method)
7586       (error "Can't open server %s" (car to-method)))
7587     (gnus-message 6 "%s to %s: %s..."
7588                   (caddr (assq action names))
7589                   (or (car select-method) to-newsgroup) articles)
7590     (while articles
7591       (setq article (pop articles))
7592       (setq
7593        art-group
7594        (cond
7595         ;; Move the article.
7596         ((eq action 'move)
7597          ;; Remove this article from future suppression.
7598          (gnus-dup-unsuppress-article article)
7599          (gnus-request-move-article
7600           article                       ; Article to move
7601           gnus-newsgroup-name           ; From newsgroup
7602           (nth 1 (gnus-find-method-for-group
7603                   gnus-newsgroup-name)) ; Server
7604           (list 'gnus-request-accept-article
7605                 to-newsgroup (list 'quote select-method)
7606                 (not articles) t)       ; Accept form
7607           (not articles)))              ; Only save nov last time
7608         ;; Copy the article.
7609         ((eq action 'copy)
7610          (save-excursion
7611            (set-buffer copy-buf)
7612            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7613              (gnus-request-accept-article
7614               to-newsgroup select-method (not articles) t))))
7615         ;; Crosspost the article.
7616         ((eq action 'crosspost)
7617          (let ((xref (message-tokenize-header
7618                       (mail-header-xref (gnus-summary-article-header article))
7619                       " ")))
7620            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7621                                   ":" article))
7622            (unless xref
7623              (setq xref (list (system-name))))
7624            (setq new-xref
7625                  (concat
7626                   (mapconcat 'identity
7627                              (delete "Xref:" (delete new-xref xref))
7628                              " ")
7629                   " " new-xref))
7630            (save-excursion
7631              (set-buffer copy-buf)
7632              ;; First put the article in the destination group.
7633              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7634              (when (consp (setq art-group
7635                                 (gnus-request-accept-article
7636                                  to-newsgroup select-method (not articles))))
7637                (setq new-xref (concat new-xref " " (car art-group)
7638                                       ":" (cdr art-group)))
7639                ;; Now we have the new Xrefs header, so we insert
7640                ;; it and replace the new article.
7641                (nnheader-replace-header "Xref" new-xref)
7642                (gnus-request-replace-article
7643                 (cdr art-group) to-newsgroup (current-buffer))
7644                art-group))))))
7645       (cond
7646        ((not art-group)
7647         (gnus-message 1 "Couldn't %s article %s: %s"
7648                       (cadr (assq action names)) article
7649                       (nnheader-get-report (car to-method))))
7650        ((eq art-group 'junk)
7651         (when (eq action 'move)
7652           (gnus-summary-mark-article article gnus-canceled-mark)
7653           (gnus-message 4 "Deleted article %s" article)))
7654        (t
7655         (let* ((pto-group (gnus-group-prefixed-name
7656                            (car art-group) to-method))
7657                (entry
7658                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7659                (info (nth 2 entry))
7660                (to-group (gnus-info-group info))
7661                to-marks)
7662           ;; Update the group that has been moved to.
7663           (when (and info
7664                      (memq action '(move copy)))
7665             (unless (member to-group to-groups)
7666               (push to-group to-groups))
7667
7668             (unless (memq article gnus-newsgroup-unreads)
7669               (push 'read to-marks)
7670               (gnus-info-set-read
7671                info (gnus-add-to-range (gnus-info-read info)
7672                                        (list (cdr art-group)))))
7673
7674             ;; See whether the article is to be put in the cache.
7675             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7676                              default-marks
7677                            no-expire-marks))
7678                   (to-article (cdr art-group)))
7679
7680               ;; Enter the article into the cache in the new group,
7681               ;; if that is required.
7682               (when gnus-use-cache
7683                 (gnus-cache-possibly-enter-article
7684                  to-group to-article
7685                  (let ((header (copy-sequence
7686                                 (gnus-summary-article-header article))))
7687                    (mail-header-set-number header to-article)
7688                    header)
7689                  (memq article gnus-newsgroup-marked)
7690                  (memq article gnus-newsgroup-dormant)
7691                  (memq article gnus-newsgroup-unreads)))
7692
7693               (when gnus-preserve-marks
7694                 ;; Copy any marks over to the new group.
7695                 (when (and (equal to-group gnus-newsgroup-name)
7696                            (not (memq article gnus-newsgroup-unreads)))
7697                   ;; Mark this article as read in this group.
7698                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7699                   (setcdr (gnus-active to-group) to-article)
7700                   (setcdr gnus-newsgroup-active to-article))
7701
7702                 (while marks
7703                   (when (memq article (symbol-value
7704                                        (intern (format "gnus-newsgroup-%s"
7705                                                        (caar marks)))))
7706                     (push (cdar marks) to-marks)
7707                     ;; If the other group is the same as this group,
7708                     ;; then we have to add the mark to the list.
7709                     (when (equal to-group gnus-newsgroup-name)
7710                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7711                            (cons to-article
7712                                  (symbol-value
7713                                   (intern (format "gnus-newsgroup-%s"
7714                                                   (caar marks)))))))
7715                     ;; Copy the marks to other group.
7716                     (gnus-add-marked-articles
7717                      to-group (cdar marks) (list to-article) info))
7718                   (setq marks (cdr marks)))
7719
7720                 (gnus-request-set-mark to-group (list (list (list to-article)
7721                                                             'set
7722                                                             to-marks))))
7723
7724               (gnus-dribble-enter
7725                (concat "(gnus-group-set-info '"
7726                        (gnus-prin1-to-string (gnus-get-info to-group))
7727                        ")"))))
7728
7729           ;; Update the Xref header in this article to point to
7730           ;; the new crossposted article we have just created.
7731           (when (eq action 'crosspost)
7732             (save-excursion
7733               (set-buffer copy-buf)
7734               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7735               (nnheader-replace-header "Xref" new-xref)
7736               (gnus-request-replace-article
7737                article gnus-newsgroup-name (current-buffer)))))
7738
7739         ;;;!!!Why is this necessary?
7740         (set-buffer gnus-summary-buffer)
7741
7742         (gnus-summary-goto-subject article)
7743         (when (eq action 'move)
7744           (gnus-summary-mark-article article gnus-canceled-mark))))
7745       (gnus-summary-remove-process-mark article))
7746     ;; Re-activate all groups that have been moved to.
7747     (while to-groups
7748       (save-excursion
7749         (set-buffer gnus-group-buffer)
7750         (when (gnus-group-goto-group (car to-groups) t)
7751           (gnus-group-get-new-news-this-group 1 t))
7752         (pop to-groups)))
7753
7754     (gnus-kill-buffer copy-buf)
7755     (gnus-summary-position-point)
7756     (gnus-set-mode-line 'summary)))
7757
7758 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7759   "Move the current article to a different newsgroup.
7760 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7761 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7762 re-spool using this method."
7763   (interactive "P")
7764   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7765
7766 (defun gnus-summary-crosspost-article (&optional n)
7767   "Crosspost the current article to some other group."
7768   (interactive "P")
7769   (gnus-summary-move-article n nil nil 'crosspost))
7770
7771 (defcustom gnus-summary-respool-default-method nil
7772   "Default method for respooling an article.
7773 If nil, use to the current newsgroup method."
7774   :type '(choice (gnus-select-method :value (nnml ""))
7775                  (const nil))
7776   :group 'gnus-summary-mail)
7777
7778 (defun gnus-summary-respool-article (&optional n method)
7779   "Respool the current article.
7780 The article will be squeezed through the mail spooling process again,
7781 which means that it will be put in some mail newsgroup or other
7782 depending on `nnmail-split-methods'.
7783 If N is a positive number, respool the N next articles.
7784 If N is a negative number, respool the N previous articles.
7785 If N is nil and any articles have been marked with the process mark,
7786 respool those articles instead.
7787
7788 Respooling can be done both from mail groups and \"real\" newsgroups.
7789 In the former case, the articles in question will be moved from the
7790 current group into whatever groups they are destined to.  In the
7791 latter case, they will be copied into the relevant groups."
7792   (interactive
7793    (list current-prefix-arg
7794          (let* ((methods (gnus-methods-using 'respool))
7795                 (methname
7796                  (symbol-name (or gnus-summary-respool-default-method
7797                                   (car (gnus-find-method-for-group
7798                                         gnus-newsgroup-name)))))
7799                 (method
7800                  (gnus-completing-read
7801                   methname "What backend do you want to use when respooling?"
7802                   methods nil t nil 'gnus-mail-method-history))
7803                 ms)
7804            (cond
7805             ((zerop (length (setq ms (gnus-servers-using-backend
7806                                       (intern method)))))
7807              (list (intern method) ""))
7808             ((= 1 (length ms))
7809              (car ms))
7810             (t
7811              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7812                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7813                            ms-alist))))))))
7814   (unless method
7815     (error "No method given for respooling"))
7816   (if (assoc (symbol-name
7817               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7818              (gnus-methods-using 'respool))
7819       (gnus-summary-move-article n nil method)
7820     (gnus-summary-copy-article n nil method)))
7821
7822 (defun gnus-summary-import-article (file)
7823   "Import an arbitrary file into a mail newsgroup."
7824   (interactive "fImport file: ")
7825   (let ((group gnus-newsgroup-name)
7826         (now (current-time))
7827         atts lines)
7828     (unless (gnus-check-backend-function 'request-accept-article group)
7829       (error "%s does not support article importing" group))
7830     (or (file-readable-p file)
7831         (not (file-regular-p file))
7832         (error "Can't read %s" file))
7833     (save-excursion
7834       (set-buffer (gnus-get-buffer-create " *import file*"))
7835       (erase-buffer)
7836       (nnheader-insert-file-contents file)
7837       (goto-char (point-min))
7838       (unless (nnheader-article-p)
7839         ;; This doesn't look like an article, so we fudge some headers.
7840         (setq atts (file-attributes file)
7841               lines (count-lines (point-min) (point-max)))
7842         (insert "From: " (read-string "From: ") "\n"
7843                 "Subject: " (read-string "Subject: ") "\n"
7844                 "Date: " (message-make-date (nth 5 atts))
7845                 "\n"
7846                 "Message-ID: " (message-make-message-id) "\n"
7847                 "Lines: " (int-to-string lines) "\n"
7848                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7849       (gnus-request-accept-article group nil t)
7850       (kill-buffer (current-buffer)))))
7851
7852 (defun gnus-summary-article-posted-p ()
7853   "Say whether the current (mail) article is available from news as well.
7854 This will be the case if the article has both been mailed and posted."
7855   (interactive)
7856   (let ((id (mail-header-references (gnus-summary-article-header)))
7857         (gnus-override-method (car (gnus-refer-article-methods))))
7858     (if (gnus-request-head id "")
7859         (gnus-message 2 "The current message was found on %s"
7860                       gnus-override-method)
7861       (gnus-message 2 "The current message couldn't be found on %s"
7862                     gnus-override-method)
7863       nil)))
7864
7865 (defun gnus-summary-expire-articles (&optional now)
7866   "Expire all articles that are marked as expirable in the current group."
7867   (interactive)
7868   (when (gnus-check-backend-function
7869          'request-expire-articles gnus-newsgroup-name)
7870     ;; This backend supports expiry.
7871     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7872            (expirable (if total
7873                           (progn
7874                             ;; We need to update the info for
7875                             ;; this group for `gnus-list-of-read-articles'
7876                             ;; to give us the right answer.
7877                             (gnus-run-hooks 'gnus-exit-group-hook)
7878                             (gnus-summary-update-info)
7879                             (gnus-list-of-read-articles gnus-newsgroup-name))
7880                         (setq gnus-newsgroup-expirable
7881                               (sort gnus-newsgroup-expirable '<))))
7882            (expiry-wait (if now 'immediate
7883                           (gnus-group-find-parameter
7884                            gnus-newsgroup-name 'expiry-wait)))
7885            (nnmail-expiry-target
7886             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7887                 nnmail-expiry-target))
7888            es)
7889       (when expirable
7890         ;; There are expirable articles in this group, so we run them
7891         ;; through the expiry process.
7892         (gnus-message 6 "Expiring articles...")
7893         (unless (gnus-check-group gnus-newsgroup-name)
7894           (error "Can't open server for %s" gnus-newsgroup-name))
7895         ;; The list of articles that weren't expired is returned.
7896         (save-excursion
7897           (if expiry-wait
7898               (let ((nnmail-expiry-wait-function nil)
7899                     (nnmail-expiry-wait expiry-wait))
7900                 (setq es (gnus-request-expire-articles
7901                           expirable gnus-newsgroup-name)))
7902             (setq es (gnus-request-expire-articles
7903                       expirable gnus-newsgroup-name))))
7904         (unless total
7905           (setq gnus-newsgroup-expirable es))
7906         ;; We go through the old list of expirable, and mark all
7907         ;; really expired articles as nonexistent.
7908         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7909           (let ((gnus-use-cache nil))
7910             (while expirable
7911               (unless (memq (car expirable) es)
7912                 (when (gnus-data-find (car expirable))
7913                   (gnus-summary-mark-article
7914                    (car expirable) gnus-canceled-mark)))
7915               (setq expirable (cdr expirable)))))
7916         (gnus-message 6 "Expiring articles...done")))))
7917
7918 (defun gnus-summary-expire-articles-now ()
7919   "Expunge all expirable articles in the current group.
7920 This means that *all* articles that are marked as expirable will be
7921 deleted forever, right now."
7922   (interactive)
7923   (or gnus-expert-user
7924       (gnus-yes-or-no-p
7925        "Are you really, really, really sure you want to delete all these messages? ")
7926       (error "Phew!"))
7927   (gnus-summary-expire-articles t))
7928
7929 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7930 (defun gnus-summary-delete-article (&optional n)
7931   "Delete the N next (mail) articles.
7932 This command actually deletes articles.  This is not a marking
7933 command.  The article will disappear forever from your life, never to
7934 return.
7935 If N is negative, delete backwards.
7936 If N is nil and articles have been marked with the process mark,
7937 delete these instead."
7938   (interactive "P")
7939   (unless (gnus-check-backend-function 'request-expire-articles
7940                                        gnus-newsgroup-name)
7941     (error "The current newsgroup does not support article deletion"))
7942   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7943     (error "Couldn't open server"))
7944   ;; Compute the list of articles to delete.
7945   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7946         not-deleted)
7947     (if (and gnus-novice-user
7948              (not (gnus-yes-or-no-p
7949                    (format "Do you really want to delete %s forever? "
7950                            (if (> (length articles) 1)
7951                                (format "these %s articles" (length articles))
7952                              "this article")))))
7953         ()
7954       ;; Delete the articles.
7955       (setq not-deleted (gnus-request-expire-articles
7956                          articles gnus-newsgroup-name 'force))
7957       (while articles
7958         (gnus-summary-remove-process-mark (car articles))
7959         ;; The backend might not have been able to delete the article
7960         ;; after all.
7961         (unless (memq (car articles) not-deleted)
7962           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7963         (setq articles (cdr articles)))
7964       (when not-deleted
7965         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7966     (gnus-summary-position-point)
7967     (gnus-set-mode-line 'summary)
7968     not-deleted))
7969
7970 (defun gnus-summary-edit-article (&optional force)
7971   "Edit the current article.
7972 This will have permanent effect only in mail groups.
7973 If FORCE is non-nil, allow editing of articles even in read-only
7974 groups."
7975   (interactive "P")
7976   (save-excursion
7977     (set-buffer gnus-summary-buffer)
7978     (let ((mail-parse-charset gnus-newsgroup-charset)
7979           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7980       (gnus-set-global-variables)
7981       (when (and (not force)
7982                  (gnus-group-read-only-p))
7983         (error "The current newsgroup does not support article editing"))
7984       (gnus-summary-show-article t)
7985       (gnus-article-edit-article
7986        'ignore
7987        `(lambda (no-highlight)
7988           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7989                 (mail-parse-ignored-charsets
7990                  ',gnus-newsgroup-ignored-charsets))
7991             (gnus-summary-edit-article-done
7992              ,(or (mail-header-references gnus-current-headers) "")
7993              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7994
7995 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7996
7997 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7998                                                  no-highlight)
7999   "Make edits to the current article permanent."
8000   (interactive)
8001   ;; Replace the article.
8002   (let ((buf (current-buffer)))
8003     (with-temp-buffer
8004       (insert-buffer-substring buf)
8005       (if (and (not read-only)
8006                (not (gnus-request-replace-article
8007                      (cdr gnus-article-current) (car gnus-article-current)
8008                      (current-buffer) t)))
8009           (error "Couldn't replace article")
8010         ;; Update the summary buffer.
8011         (if (and references
8012                  (equal (message-tokenize-header references " ")
8013                         (message-tokenize-header
8014                          (or (message-fetch-field "references") "") " ")))
8015             ;; We only have to update this line.
8016             (save-excursion
8017               (save-restriction
8018                 (message-narrow-to-head)
8019                 (let ((head (buffer-string))
8020                       header)
8021                   (with-temp-buffer
8022                     (insert (format "211 %d Article retrieved.\n"
8023                                     (cdr gnus-article-current)))
8024                     (insert head)
8025                     (insert ".\n")
8026                     (let ((nntp-server-buffer (current-buffer)))
8027                       (setq header (car (gnus-get-newsgroup-headers
8028                                          (save-excursion
8029                                            (set-buffer gnus-summary-buffer)
8030                                            gnus-newsgroup-dependencies)
8031                                          t))))
8032                     (save-excursion
8033                       (set-buffer gnus-summary-buffer)
8034                       (gnus-data-set-header
8035                        (gnus-data-find (cdr gnus-article-current))
8036                        header)
8037                       (gnus-summary-update-article-line
8038                        (cdr gnus-article-current) header))))))
8039           ;; Update threads.
8040           (set-buffer (or buffer gnus-summary-buffer))
8041           (gnus-summary-update-article (cdr gnus-article-current)))
8042         ;; Prettify the article buffer again.
8043         (unless no-highlight
8044           (save-excursion
8045             (set-buffer gnus-article-buffer)
8046             ;;;!!! Fix this -- article should be rehighlighted.
8047             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8048             (set-buffer gnus-original-article-buffer)
8049             (gnus-request-article
8050              (cdr gnus-article-current)
8051              (car gnus-article-current) (current-buffer))))
8052         ;; Prettify the summary buffer line.
8053         (when (gnus-visual-p 'summary-highlight 'highlight)
8054           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8055
8056 (defun gnus-summary-edit-wash (key)
8057   "Perform editing command KEY in the article buffer."
8058   (interactive
8059    (list
8060     (progn
8061       (message "%s" (concat (this-command-keys) "- "))
8062       (read-char))))
8063   (message "")
8064   (gnus-summary-edit-article)
8065   (execute-kbd-macro (concat (this-command-keys) key))
8066   (gnus-article-edit-done))
8067
8068 ;;; Respooling
8069
8070 (defun gnus-summary-respool-query (&optional silent trace)
8071   "Query where the respool algorithm would put this article."
8072   (interactive)
8073   (let (gnus-mark-article-hook)
8074     (gnus-summary-select-article)
8075     (save-excursion
8076       (set-buffer gnus-original-article-buffer)
8077       (save-restriction
8078         (message-narrow-to-head)
8079         (let ((groups (nnmail-article-group 'identity trace)))
8080           (unless silent
8081             (if groups
8082                 (message "This message would go to %s"
8083                          (mapconcat 'car groups ", "))
8084               (message "This message would go to no groups"))
8085             groups))))))
8086
8087 (defun gnus-summary-respool-trace ()
8088   "Trace where the respool algorithm would put this article.
8089 Display a buffer showing all fancy splitting patterns which matched."
8090   (interactive)
8091   (gnus-summary-respool-query nil t))
8092
8093 ;; Summary marking commands.
8094
8095 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8096   "Mark articles which has the same subject as read, and then select the next.
8097 If UNMARK is positive, remove any kind of mark.
8098 If UNMARK is negative, tick articles."
8099   (interactive "P")
8100   (when unmark
8101     (setq unmark (prefix-numeric-value unmark)))
8102   (let ((count
8103          (gnus-summary-mark-same-subject
8104           (gnus-summary-article-subject) unmark)))
8105     ;; Select next unread article.  If auto-select-same mode, should
8106     ;; select the first unread article.
8107     (gnus-summary-next-article t (and gnus-auto-select-same
8108                                       (gnus-summary-article-subject)))
8109     (gnus-message 7 "%d article%s marked as %s"
8110                   count (if (= count 1) " is" "s are")
8111                   (if unmark "unread" "read"))))
8112
8113 (defun gnus-summary-kill-same-subject (&optional unmark)
8114   "Mark articles which has the same subject as read.
8115 If UNMARK is positive, remove any kind of mark.
8116 If UNMARK is negative, tick articles."
8117   (interactive "P")
8118   (when unmark
8119     (setq unmark (prefix-numeric-value unmark)))
8120   (let ((count
8121          (gnus-summary-mark-same-subject
8122           (gnus-summary-article-subject) unmark)))
8123     ;; If marked as read, go to next unread subject.
8124     (when (null unmark)
8125       ;; Go to next unread subject.
8126       (gnus-summary-next-subject 1 t))
8127     (gnus-message 7 "%d articles are marked as %s"
8128                   count (if unmark "unread" "read"))))
8129
8130 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8131   "Mark articles with same SUBJECT as read, and return marked number.
8132 If optional argument UNMARK is positive, remove any kinds of marks.
8133 If optional argument UNMARK is negative, mark articles as unread instead."
8134   (let ((count 1))
8135     (save-excursion
8136       (cond
8137        ((null unmark)                   ; Mark as read.
8138         (while (and
8139                 (progn
8140                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8141                   (gnus-summary-show-thread) t)
8142                 (gnus-summary-find-subject subject))
8143           (setq count (1+ count))))
8144        ((> unmark 0)                    ; Tick.
8145         (while (and
8146                 (progn
8147                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8148                   (gnus-summary-show-thread) t)
8149                 (gnus-summary-find-subject subject))
8150           (setq count (1+ count))))
8151        (t                               ; Mark as unread.
8152         (while (and
8153                 (progn
8154                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8155                   (gnus-summary-show-thread) t)
8156                 (gnus-summary-find-subject subject))
8157           (setq count (1+ count)))))
8158       (gnus-set-mode-line 'summary)
8159       ;; Return the number of marked articles.
8160       count)))
8161
8162 (defun gnus-summary-mark-as-processable (n &optional unmark)
8163   "Set the process mark on the next N articles.
8164 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8165 the process mark instead.  The difference between N and the actual
8166 number of articles marked is returned."
8167   (interactive "p")
8168   (let ((backward (< n 0))
8169         (n (abs n)))
8170     (while (and
8171             (> n 0)
8172             (if unmark
8173                 (gnus-summary-remove-process-mark
8174                  (gnus-summary-article-number))
8175               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8176             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8177       (setq n (1- n)))
8178     (when (/= 0 n)
8179       (gnus-message 7 "No more articles"))
8180     (gnus-summary-recenter)
8181     (gnus-summary-position-point)
8182     n))
8183
8184 (defun gnus-summary-unmark-as-processable (n)
8185   "Remove the process mark from the next N articles.
8186 If N is negative, unmark backward instead.  The difference between N and
8187 the actual number of articles unmarked is returned."
8188   (interactive "p")
8189   (gnus-summary-mark-as-processable n t))
8190
8191 (defun gnus-summary-unmark-all-processable ()
8192   "Remove the process mark from all articles."
8193   (interactive)
8194   (save-excursion
8195     (while gnus-newsgroup-processable
8196       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8197   (gnus-summary-position-point))
8198
8199 (defun gnus-summary-mark-as-expirable (n)
8200   "Mark N articles forward as expirable.
8201 If N is negative, mark backward instead.  The difference between N and
8202 the actual number of articles marked is returned."
8203   (interactive "p")
8204   (gnus-summary-mark-forward n gnus-expirable-mark))
8205
8206 (defun gnus-summary-mark-article-as-replied (article)
8207   "Mark ARTICLE replied and update the summary line."
8208   (push article gnus-newsgroup-replied)
8209   (let ((buffer-read-only nil))
8210     (when (gnus-summary-goto-subject article nil t)
8211       (gnus-summary-update-secondary-mark article))))
8212
8213 (defun gnus-summary-set-bookmark (article)
8214   "Set a bookmark in current article."
8215   (interactive (list (gnus-summary-article-number)))
8216   (when (or (not (get-buffer gnus-article-buffer))
8217             (not gnus-current-article)
8218             (not gnus-article-current)
8219             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8220     (error "No current article selected"))
8221   ;; Remove old bookmark, if one exists.
8222   (let ((old (assq article gnus-newsgroup-bookmarks)))
8223     (when old
8224       (setq gnus-newsgroup-bookmarks
8225             (delq old gnus-newsgroup-bookmarks))))
8226   ;; Set the new bookmark, which is on the form
8227   ;; (article-number . line-number-in-body).
8228   (push
8229    (cons article
8230          (save-excursion
8231            (set-buffer gnus-article-buffer)
8232            (count-lines
8233             (min (point)
8234                  (save-excursion
8235                    (goto-char (point-min))
8236                    (search-forward "\n\n" nil t)
8237                    (point)))
8238             (point))))
8239    gnus-newsgroup-bookmarks)
8240   (gnus-message 6 "A bookmark has been added to the current article."))
8241
8242 (defun gnus-summary-remove-bookmark (article)
8243   "Remove the bookmark from the current article."
8244   (interactive (list (gnus-summary-article-number)))
8245   ;; Remove old bookmark, if one exists.
8246   (let ((old (assq article gnus-newsgroup-bookmarks)))
8247     (if old
8248         (progn
8249           (setq gnus-newsgroup-bookmarks
8250                 (delq old gnus-newsgroup-bookmarks))
8251           (gnus-message 6 "Removed bookmark."))
8252       (gnus-message 6 "No bookmark in current article."))))
8253
8254 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8255 (defun gnus-summary-mark-as-dormant (n)
8256   "Mark N articles forward as dormant.
8257 If N is negative, mark backward instead.  The difference between N and
8258 the actual number of articles marked is returned."
8259   (interactive "p")
8260   (gnus-summary-mark-forward n gnus-dormant-mark))
8261
8262 (defun gnus-summary-set-process-mark (article)
8263   "Set the process mark on ARTICLE and update the summary line."
8264   (setq gnus-newsgroup-processable
8265         (cons article
8266               (delq article gnus-newsgroup-processable)))
8267   (when (gnus-summary-goto-subject article)
8268     (gnus-summary-show-thread)
8269     (gnus-summary-goto-subject article)
8270     (gnus-summary-update-secondary-mark article)))
8271
8272 (defun gnus-summary-remove-process-mark (article)
8273   "Remove the process mark from ARTICLE and update the summary line."
8274   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8275   (when (gnus-summary-goto-subject article)
8276     (gnus-summary-show-thread)
8277     (gnus-summary-goto-subject article)
8278     (gnus-summary-update-secondary-mark article)))
8279
8280 (defun gnus-summary-set-saved-mark (article)
8281   "Set the process mark on ARTICLE and update the summary line."
8282   (push article gnus-newsgroup-saved)
8283   (when (gnus-summary-goto-subject article)
8284     (gnus-summary-update-secondary-mark article)))
8285
8286 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8287   "Mark N articles as read forwards.
8288 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8289 The difference between N and the actual number of articles marked is
8290 returned.
8291 Iff NO-EXPIRE, auto-expiry will be inhibited."
8292   (interactive "p")
8293   (gnus-summary-show-thread)
8294   (let ((backward (< n 0))
8295         (gnus-summary-goto-unread
8296          (and gnus-summary-goto-unread
8297               (not (eq gnus-summary-goto-unread 'never))
8298               (not (memq mark (list gnus-unread-mark
8299                                     gnus-ticked-mark gnus-dormant-mark)))))
8300         (n (abs n))
8301         (mark (or mark gnus-del-mark)))
8302     (while (and (> n 0)
8303                 (gnus-summary-mark-article nil mark no-expire)
8304                 (zerop (gnus-summary-next-subject
8305                         (if backward -1 1)
8306                         (and gnus-summary-goto-unread
8307                              (not (eq gnus-summary-goto-unread 'never)))
8308                         t)))
8309       (setq n (1- n)))
8310     (when (/= 0 n)
8311       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8312     (gnus-summary-recenter)
8313     (gnus-summary-position-point)
8314     (gnus-set-mode-line 'summary)
8315     n))
8316
8317 (defun gnus-summary-mark-article-as-read (mark)
8318   "Mark the current article quickly as read with MARK."
8319   (let ((article (gnus-summary-article-number)))
8320     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8321     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8322     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8323     (push (cons article mark) gnus-newsgroup-reads)
8324     ;; Possibly remove from cache, if that is used.
8325     (when gnus-use-cache
8326       (gnus-cache-enter-remove-article article))
8327     ;; Allow the backend to change the mark.
8328     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8329     ;; Check for auto-expiry.
8330     (when (and gnus-newsgroup-auto-expire
8331                (memq mark gnus-auto-expirable-marks))
8332       (setq mark gnus-expirable-mark)
8333       ;; Let the backend know about the mark change.
8334       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8335       (push article gnus-newsgroup-expirable))
8336     ;; Set the mark in the buffer.
8337     (gnus-summary-update-mark mark 'unread)
8338     t))
8339
8340 (defun gnus-summary-mark-article-as-unread (mark)
8341   "Mark the current article quickly as unread with MARK."
8342   (let* ((article (gnus-summary-article-number))
8343          (old-mark (gnus-summary-article-mark article)))
8344     ;; Allow the backend to change the mark.
8345     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8346     (if (eq mark old-mark)
8347         t
8348       (if (<= article 0)
8349           (progn
8350             (gnus-error 1 "Can't mark negative article numbers")
8351             nil)
8352         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8353         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8354         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8355         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8356         (cond ((= mark gnus-ticked-mark)
8357                (push article gnus-newsgroup-marked))
8358               ((= mark gnus-dormant-mark)
8359                (push article gnus-newsgroup-dormant))
8360               (t
8361                (push article gnus-newsgroup-unreads)))
8362         (gnus-pull article gnus-newsgroup-reads)
8363
8364         ;; See whether the article is to be put in the cache.
8365         (and gnus-use-cache
8366              (vectorp (gnus-summary-article-header article))
8367              (save-excursion
8368                (gnus-cache-possibly-enter-article
8369                 gnus-newsgroup-name article
8370                 (gnus-summary-article-header article)
8371                 (= mark gnus-ticked-mark)
8372                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8373
8374         ;; Fix the mark.
8375         (gnus-summary-update-mark mark 'unread)
8376         t))))
8377
8378 (defun gnus-summary-mark-article (&optional article mark no-expire)
8379   "Mark ARTICLE with MARK.  MARK can be any character.
8380 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8381 `??' (dormant) and `?E' (expirable).
8382 If MARK is nil, then the default character `?r' is used.
8383 If ARTICLE is nil, then the article on the current line will be
8384 marked.
8385 Iff NO-EXPIRE, auto-expiry will be inhibited."
8386   ;; The mark might be a string.
8387   (when (stringp mark)
8388     (setq mark (aref mark 0)))
8389   ;; If no mark is given, then we check auto-expiring.
8390   (when (null mark)
8391     (setq mark gnus-del-mark))
8392   (when (and (not no-expire)
8393              gnus-newsgroup-auto-expire
8394              (memq mark gnus-auto-expirable-marks))
8395     (setq mark gnus-expirable-mark))
8396   (let ((article (or article (gnus-summary-article-number)))
8397         (old-mark (gnus-summary-article-mark article)))
8398     ;; Allow the backend to change the mark.
8399     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8400     (if (eq mark old-mark)
8401         t
8402       (unless article
8403         (error "No article on current line"))
8404       (if (not (if (or (= mark gnus-unread-mark)
8405                        (= mark gnus-ticked-mark)
8406                        (= mark gnus-dormant-mark))
8407                    (gnus-mark-article-as-unread article mark)
8408                  (gnus-mark-article-as-read article mark)))
8409           t
8410         ;; See whether the article is to be put in the cache.
8411         (and gnus-use-cache
8412              (not (= mark gnus-canceled-mark))
8413              (vectorp (gnus-summary-article-header article))
8414              (save-excursion
8415                (gnus-cache-possibly-enter-article
8416                 gnus-newsgroup-name article
8417                 (gnus-summary-article-header article)
8418                 (= mark gnus-ticked-mark)
8419                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8420
8421         (when (gnus-summary-goto-subject article nil t)
8422           (let ((buffer-read-only nil))
8423             (gnus-summary-show-thread)
8424             ;; Fix the mark.
8425             (gnus-summary-update-mark mark 'unread)
8426             t))))))
8427
8428 (defun gnus-summary-update-secondary-mark (article)
8429   "Update the secondary (read, process, cache) mark."
8430   (gnus-summary-update-mark
8431    (cond ((memq article gnus-newsgroup-processable)
8432           gnus-process-mark)
8433          ((memq article gnus-newsgroup-cached)
8434           gnus-cached-mark)
8435          ((memq article gnus-newsgroup-replied)
8436           gnus-replied-mark)
8437          ((memq article gnus-newsgroup-saved)
8438           gnus-saved-mark)
8439          (t gnus-unread-mark))
8440    'replied)
8441   (when (gnus-visual-p 'summary-highlight 'highlight)
8442     (gnus-run-hooks 'gnus-summary-update-hook))
8443   t)
8444
8445 (defun gnus-summary-update-mark (mark type)
8446   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8447         (buffer-read-only nil))
8448     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8449     (when forward
8450       (when (looking-at "\r")
8451         (incf forward))
8452       (when (<= (+ forward (point)) (point-max))
8453         ;; Go to the right position on the line.
8454         (goto-char (+ forward (point)))
8455         ;; Replace the old mark with the new mark.
8456         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8457         ;; Optionally update the marks by some user rule.
8458         (when (eq type 'unread)
8459           (gnus-data-set-mark
8460            (gnus-data-find (gnus-summary-article-number)) mark)
8461           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8462
8463 (defun gnus-mark-article-as-read (article &optional mark)
8464   "Enter ARTICLE in the pertinent lists and remove it from others."
8465   ;; Make the article expirable.
8466   (let ((mark (or mark gnus-del-mark)))
8467     (if (= mark gnus-expirable-mark)
8468         (push article gnus-newsgroup-expirable)
8469       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8470     ;; Remove from unread and marked lists.
8471     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8472     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8473     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8474     (push (cons article mark) gnus-newsgroup-reads)
8475     ;; Possibly remove from cache, if that is used.
8476     (when gnus-use-cache
8477       (gnus-cache-enter-remove-article article))
8478     t))
8479
8480 (defun gnus-mark-article-as-unread (article &optional mark)
8481   "Enter ARTICLE in the pertinent lists and remove it from others."
8482   (let ((mark (or mark gnus-ticked-mark)))
8483     (if (<= article 0)
8484         (progn
8485           (gnus-error 1 "Can't mark negative article numbers")
8486           nil)
8487       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8488             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8489             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8490             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8491
8492       ;; Unsuppress duplicates?
8493       (when gnus-suppress-duplicates
8494         (gnus-dup-unsuppress-article article))
8495
8496       (cond ((= mark gnus-ticked-mark)
8497              (push article gnus-newsgroup-marked))
8498             ((= mark gnus-dormant-mark)
8499              (push article gnus-newsgroup-dormant))
8500             (t
8501              (push article gnus-newsgroup-unreads)))
8502       (gnus-pull article gnus-newsgroup-reads)
8503       t)))
8504
8505 (defalias 'gnus-summary-mark-as-unread-forward
8506   'gnus-summary-tick-article-forward)
8507 (make-obsolete 'gnus-summary-mark-as-unread-forward
8508                'gnus-summary-tick-article-forward)
8509 (defun gnus-summary-tick-article-forward (n)
8510   "Tick N articles forwards.
8511 If N is negative, tick backwards instead.
8512 The difference between N and the number of articles ticked is returned."
8513   (interactive "p")
8514   (gnus-summary-mark-forward n gnus-ticked-mark))
8515
8516 (defalias 'gnus-summary-mark-as-unread-backward
8517   'gnus-summary-tick-article-backward)
8518 (make-obsolete 'gnus-summary-mark-as-unread-backward
8519                'gnus-summary-tick-article-backward)
8520 (defun gnus-summary-tick-article-backward (n)
8521   "Tick N articles backwards.
8522 The difference between N and the number of articles ticked is returned."
8523   (interactive "p")
8524   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8525
8526 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8527 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8528 (defun gnus-summary-tick-article (&optional article clear-mark)
8529   "Mark current article as unread.
8530 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8531 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8532   (interactive)
8533   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8534                                        gnus-ticked-mark)))
8535
8536 (defun gnus-summary-mark-as-read-forward (n)
8537   "Mark N articles as read forwards.
8538 If N is negative, mark backwards instead.
8539 The difference between N and the actual number of articles marked is
8540 returned."
8541   (interactive "p")
8542   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8543
8544 (defun gnus-summary-mark-as-read-backward (n)
8545   "Mark the N articles as read backwards.
8546 The difference between N and the actual number of articles marked is
8547 returned."
8548   (interactive "p")
8549   (gnus-summary-mark-forward
8550    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8551
8552 (defun gnus-summary-mark-as-read (&optional article mark)
8553   "Mark current article as read.
8554 ARTICLE specifies the article to be marked as read.
8555 MARK specifies a string to be inserted at the beginning of the line."
8556   (gnus-summary-mark-article article mark))
8557
8558 (defun gnus-summary-clear-mark-forward (n)
8559   "Clear marks from N articles forward.
8560 If N is negative, clear backward instead.
8561 The difference between N and the number of marks cleared is returned."
8562   (interactive "p")
8563   (gnus-summary-mark-forward n gnus-unread-mark))
8564
8565 (defun gnus-summary-clear-mark-backward (n)
8566   "Clear marks from N articles backward.
8567 The difference between N and the number of marks cleared is returned."
8568   (interactive "p")
8569   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8570
8571 (defun gnus-summary-mark-unread-as-read ()
8572   "Intended to be used by `gnus-summary-mark-article-hook'."
8573   (when (memq gnus-current-article gnus-newsgroup-unreads)
8574     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8575
8576 (defun gnus-summary-mark-read-and-unread-as-read ()
8577   "Intended to be used by `gnus-summary-mark-article-hook'."
8578   (let ((mark (gnus-summary-article-mark)))
8579     (when (or (gnus-unread-mark-p mark)
8580               (gnus-read-mark-p mark))
8581       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8582
8583 (defun gnus-summary-mark-region-as-read (point mark all)
8584   "Mark all unread articles between point and mark as read.
8585 If given a prefix, mark all articles between point and mark as read,
8586 even ticked and dormant ones."
8587   (interactive "r\nP")
8588   (save-excursion
8589     (let (article)
8590       (goto-char point)
8591       (beginning-of-line)
8592       (while (and
8593               (< (point) mark)
8594               (progn
8595                 (when (or all
8596                           (memq (setq article (gnus-summary-article-number))
8597                                 gnus-newsgroup-unreads))
8598                   (gnus-summary-mark-article article gnus-del-mark))
8599                 t)
8600               (gnus-summary-find-next))))))
8601
8602 (defun gnus-summary-mark-below (score mark)
8603   "Mark articles with score less than SCORE with MARK."
8604   (interactive "P\ncMark: ")
8605   (setq score (if score
8606                   (prefix-numeric-value score)
8607                 (or gnus-summary-default-score 0)))
8608   (save-excursion
8609     (set-buffer gnus-summary-buffer)
8610     (goto-char (point-min))
8611     (while
8612         (progn
8613           (and (< (gnus-summary-article-score) score)
8614                (gnus-summary-mark-article nil mark))
8615           (gnus-summary-find-next)))))
8616
8617 (defun gnus-summary-kill-below (&optional score)
8618   "Mark articles with score below SCORE as read."
8619   (interactive "P")
8620   (gnus-summary-mark-below score gnus-killed-mark))
8621
8622 (defun gnus-summary-clear-above (&optional score)
8623   "Clear all marks from articles with score above SCORE."
8624   (interactive "P")
8625   (gnus-summary-mark-above score gnus-unread-mark))
8626
8627 (defun gnus-summary-tick-above (&optional score)
8628   "Tick all articles with score above SCORE."
8629   (interactive "P")
8630   (gnus-summary-mark-above score gnus-ticked-mark))
8631
8632 (defun gnus-summary-mark-above (score mark)
8633   "Mark articles with score over SCORE with MARK."
8634   (interactive "P\ncMark: ")
8635   (setq score (if score
8636                   (prefix-numeric-value score)
8637                 (or gnus-summary-default-score 0)))
8638   (save-excursion
8639     (set-buffer gnus-summary-buffer)
8640     (goto-char (point-min))
8641     (while (and (progn
8642                   (when (> (gnus-summary-article-score) score)
8643                     (gnus-summary-mark-article nil mark))
8644                   t)
8645                 (gnus-summary-find-next)))))
8646
8647 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8648 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8649 (defun gnus-summary-limit-include-expunged (&optional no-error)
8650   "Display all the hidden articles that were expunged for low scores."
8651   (interactive)
8652   (let ((buffer-read-only nil))
8653     (let ((scored gnus-newsgroup-scored)
8654           headers h)
8655       (while scored
8656         (unless (gnus-summary-goto-subject (caar scored))
8657           (and (setq h (gnus-summary-article-header (caar scored)))
8658                (< (cdar scored) gnus-summary-expunge-below)
8659                (push h headers)))
8660         (setq scored (cdr scored)))
8661       (if (not headers)
8662           (when (not no-error)
8663             (error "No expunged articles hidden"))
8664         (goto-char (point-min))
8665         (gnus-summary-prepare-unthreaded (nreverse headers))
8666         (goto-char (point-min))
8667         (gnus-summary-position-point)
8668         t))))
8669
8670 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8671   "Mark all unread articles in this newsgroup as read.
8672 If prefix argument ALL is non-nil, ticked and dormant articles will
8673 also be marked as read.
8674 If QUIETLY is non-nil, no questions will be asked.
8675 If TO-HERE is non-nil, it should be a point in the buffer.  All
8676 articles before this point will be marked as read.
8677 Note that this function will only catch up the unread article
8678 in the current summary buffer limitation.
8679 The number of articles marked as read is returned."
8680   (interactive "P")
8681   (prog1
8682       (save-excursion
8683         (when (or quietly
8684                   (not gnus-interactive-catchup) ;Without confirmation?
8685                   gnus-expert-user
8686                   (gnus-y-or-n-p
8687                    (if all
8688                        "Mark absolutely all articles as read? "
8689                      "Mark all unread articles as read? ")))
8690           (if (and not-mark
8691                    (not gnus-newsgroup-adaptive)
8692                    (not gnus-newsgroup-auto-expire)
8693                    (not gnus-suppress-duplicates)
8694                    (or (not gnus-use-cache)
8695                        (eq gnus-use-cache 'passive)))
8696               (progn
8697                 (when all
8698                   (setq gnus-newsgroup-marked nil
8699                         gnus-newsgroup-dormant nil))
8700                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8701             ;; We actually mark all articles as canceled, which we
8702             ;; have to do when using auto-expiry or adaptive scoring.
8703             (gnus-summary-show-all-threads)
8704             (when (gnus-summary-first-subject (not all) t)
8705               (while (and
8706                       (if to-here (< (point) to-here) t)
8707                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8708                       (gnus-summary-find-next (not all) nil nil t))))
8709             (gnus-set-mode-line 'summary))
8710           t))
8711     (gnus-summary-position-point)))
8712
8713 (defun gnus-summary-catchup-to-here (&optional all)
8714   "Mark all unticked articles before the current one as read.
8715 If ALL is non-nil, also mark ticked and dormant articles as read."
8716   (interactive "P")
8717   (save-excursion
8718     (gnus-save-hidden-threads
8719       (let ((beg (point)))
8720         ;; We check that there are unread articles.
8721         (when (or all (gnus-summary-find-prev))
8722           (gnus-summary-catchup all t beg)))))
8723   (gnus-summary-position-point))
8724
8725 (defun gnus-summary-catchup-all (&optional quietly)
8726   "Mark all articles in this newsgroup as read."
8727   (interactive "P")
8728   (gnus-summary-catchup t quietly))
8729
8730 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8731   "Mark all unread articles in this group as read, then exit.
8732 If prefix argument ALL is non-nil, all articles are marked as read."
8733   (interactive "P")
8734   (when (gnus-summary-catchup all quietly nil 'fast)
8735     ;; Select next newsgroup or exit.
8736     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8737              (eq gnus-auto-select-next 'quietly))
8738         (gnus-summary-next-group nil)
8739       (gnus-summary-exit))))
8740
8741 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8742   "Mark all articles in this newsgroup as read, and then exit."
8743   (interactive "P")
8744   (gnus-summary-catchup-and-exit t quietly))
8745
8746 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8747   "Mark all articles in this group as read and select the next group.
8748 If given a prefix, mark all articles, unread as well as ticked, as
8749 read."
8750   (interactive "P")
8751   (save-excursion
8752     (gnus-summary-catchup all))
8753   (gnus-summary-next-group))
8754
8755 ;;;
8756 ;;; with article
8757 ;;;
8758
8759 (defmacro gnus-with-article (article &rest forms)
8760   "Select ARTICLE and perform FORMS in the original article buffer.
8761 Then replace the article with the result."
8762   `(progn
8763      ;; We don't want the article to be marked as read.
8764      (let (gnus-mark-article-hook)
8765        (gnus-summary-select-article t t nil ,article))
8766      (set-buffer gnus-original-article-buffer)
8767      ,@forms
8768      (if (not (gnus-check-backend-function
8769                'request-replace-article (car gnus-article-current)))
8770          (gnus-message 5 "Read-only group; not replacing")
8771        (unless (gnus-request-replace-article
8772                 ,article (car gnus-article-current)
8773                 (current-buffer) t)
8774          (error "Couldn't replace article")))
8775      ;; The cache and backlog have to be flushed somewhat.
8776      (when gnus-keep-backlog
8777        (gnus-backlog-remove-article
8778         (car gnus-article-current) (cdr gnus-article-current)))
8779      (when gnus-use-cache
8780        (gnus-cache-update-article
8781         (car gnus-article-current) (cdr gnus-article-current)))))
8782
8783 (put 'gnus-with-article 'lisp-indent-function 1)
8784 (put 'gnus-with-article 'edebug-form-spec '(form body))
8785
8786 ;; Thread-based commands.
8787
8788 (defun gnus-summary-articles-in-thread (&optional article)
8789   "Return a list of all articles in the current thread.
8790 If ARTICLE is non-nil, return all articles in the thread that starts
8791 with that article."
8792   (let* ((article (or article (gnus-summary-article-number)))
8793          (data (gnus-data-find-list article))
8794          (top-level (gnus-data-level (car data)))
8795          (top-subject
8796           (cond ((null gnus-thread-operation-ignore-subject)
8797                  (gnus-simplify-subject-re
8798                   (mail-header-subject (gnus-data-header (car data)))))
8799                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8800                  (gnus-simplify-subject-fuzzy
8801                   (mail-header-subject (gnus-data-header (car data)))))
8802                 (t nil)))
8803          (end-point (save-excursion
8804                       (if (gnus-summary-go-to-next-thread)
8805                           (point) (point-max))))
8806          articles)
8807     (while (and data
8808                 (< (gnus-data-pos (car data)) end-point))
8809       (when (or (not top-subject)
8810                 (string= top-subject
8811                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8812                              (gnus-simplify-subject-fuzzy
8813                               (mail-header-subject
8814                                (gnus-data-header (car data))))
8815                            (gnus-simplify-subject-re
8816                             (mail-header-subject
8817                              (gnus-data-header (car data)))))))
8818         (push (gnus-data-number (car data)) articles))
8819       (unless (and (setq data (cdr data))
8820                    (> (gnus-data-level (car data)) top-level))
8821         (setq data nil)))
8822     ;; Return the list of articles.
8823     (nreverse articles)))
8824
8825 (defun gnus-summary-rethread-current ()
8826   "Rethread the thread the current article is part of."
8827   (interactive)
8828   (let* ((gnus-show-threads t)
8829          (article (gnus-summary-article-number))
8830          (id (mail-header-id (gnus-summary-article-header)))
8831          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8832     (unless id
8833       (error "No article on the current line"))
8834     (gnus-rebuild-thread id)
8835     (gnus-summary-goto-subject article)))
8836
8837 (defun gnus-summary-reparent-thread ()
8838   "Make the current article child of the marked (or previous) article.
8839
8840 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8841 is non-nil or the Subject: of both articles are the same."
8842   (interactive)
8843   (unless (not (gnus-group-read-only-p))
8844     (error "The current newsgroup does not support article editing"))
8845   (unless (<= (length gnus-newsgroup-processable) 1)
8846     (error "No more than one article may be marked"))
8847   (save-window-excursion
8848     (let ((gnus-article-buffer " *reparent*")
8849           (current-article (gnus-summary-article-number))
8850           ;; First grab the marked article, otherwise one line up.
8851           (parent-article (if (not (null gnus-newsgroup-processable))
8852                               (car gnus-newsgroup-processable)
8853                             (save-excursion
8854                               (if (eq (forward-line -1) 0)
8855                                   (gnus-summary-article-number)
8856                                 (error "Beginning of summary buffer"))))))
8857       (unless (not (eq current-article parent-article))
8858         (error "An article may not be self-referential"))
8859       (let ((message-id (mail-header-id
8860                          (gnus-summary-article-header parent-article))))
8861         (unless (and message-id (not (equal message-id "")))
8862           (error "No message-id in desired parent"))
8863         (gnus-with-article current-article
8864           (save-restriction
8865             (goto-char (point-min))
8866             (message-narrow-to-head)
8867             (if (re-search-forward "^References: " nil t)
8868                 (progn
8869                   (re-search-forward "^[^ \t]" nil t)
8870                   (forward-line -1)
8871                   (end-of-line)
8872                   (insert " " message-id))
8873               (insert "References: " message-id "\n"))))
8874         (set-buffer gnus-summary-buffer)
8875         (gnus-summary-unmark-all-processable)
8876         (gnus-summary-update-article current-article)
8877         (gnus-summary-rethread-current)
8878         (gnus-message 3 "Article %d is now the child of article %d"
8879                       current-article parent-article)))))
8880
8881 (defun gnus-summary-toggle-threads (&optional arg)
8882   "Toggle showing conversation threads.
8883 If ARG is positive number, turn showing conversation threads on."
8884   (interactive "P")
8885   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8886     (setq gnus-show-threads
8887           (if (null arg) (not gnus-show-threads)
8888             (> (prefix-numeric-value arg) 0)))
8889     (gnus-summary-prepare)
8890     (gnus-summary-goto-subject current)
8891     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8892     (gnus-summary-position-point)))
8893
8894 (defun gnus-summary-show-all-threads ()
8895   "Show all threads."
8896   (interactive)
8897   (save-excursion
8898     (let ((buffer-read-only nil))
8899       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8900   (gnus-summary-position-point))
8901
8902 (defun gnus-summary-show-thread ()
8903   "Show thread subtrees.
8904 Returns nil if no thread was there to be shown."
8905   (interactive)
8906   (let ((buffer-read-only nil)
8907         (orig (point))
8908         ;; first goto end then to beg, to have point at beg after let
8909         (end (progn (end-of-line) (point)))
8910         (beg (progn (beginning-of-line) (point))))
8911     (prog1
8912         ;; Any hidden lines here?
8913         (search-forward "\r" end t)
8914       (subst-char-in-region beg end ?\^M ?\n t)
8915       (goto-char orig)
8916       (gnus-summary-position-point))))
8917
8918 (defun gnus-summary-hide-all-threads ()
8919   "Hide all thread subtrees."
8920   (interactive)
8921   (save-excursion
8922     (goto-char (point-min))
8923     (gnus-summary-hide-thread)
8924     (while (zerop (gnus-summary-next-thread 1 t))
8925       (gnus-summary-hide-thread)))
8926   (gnus-summary-position-point))
8927
8928 (defun gnus-summary-hide-thread ()
8929   "Hide thread subtrees.
8930 Returns nil if no threads were there to be hidden."
8931   (interactive)
8932   (let ((buffer-read-only nil)
8933         (start (point))
8934         (article (gnus-summary-article-number)))
8935     (goto-char start)
8936     ;; Go forward until either the buffer ends or the subthread
8937     ;; ends.
8938     (when (and (not (eobp))
8939                (or (zerop (gnus-summary-next-thread 1 t))
8940                    (goto-char (point-max))))
8941       (prog1
8942           (if (and (> (point) start)
8943                    (search-backward "\n" start t))
8944               (progn
8945                 (subst-char-in-region start (point) ?\n ?\^M)
8946                 (gnus-summary-goto-subject article))
8947             (goto-char start)
8948             nil)))))
8949
8950 (defun gnus-summary-go-to-next-thread (&optional previous)
8951   "Go to the same level (or less) next thread.
8952 If PREVIOUS is non-nil, go to previous thread instead.
8953 Return the article number moved to, or nil if moving was impossible."
8954   (let ((level (gnus-summary-thread-level))
8955         (way (if previous -1 1))
8956         (beg (point)))
8957     (forward-line way)
8958     (while (and (not (eobp))
8959                 (< level (gnus-summary-thread-level)))
8960       (forward-line way))
8961     (if (eobp)
8962         (progn
8963           (goto-char beg)
8964           nil)
8965       (setq beg (point))
8966       (prog1
8967           (gnus-summary-article-number)
8968         (goto-char beg)))))
8969
8970 (defun gnus-summary-next-thread (n &optional silent)
8971   "Go to the same level next N'th thread.
8972 If N is negative, search backward instead.
8973 Returns the difference between N and the number of skips actually
8974 done.
8975
8976 If SILENT, don't output messages."
8977   (interactive "p")
8978   (let ((backward (< n 0))
8979         (n (abs n)))
8980     (while (and (> n 0)
8981                 (gnus-summary-go-to-next-thread backward))
8982       (decf n))
8983     (unless silent
8984       (gnus-summary-position-point))
8985     (when (and (not silent) (/= 0 n))
8986       (gnus-message 7 "No more threads"))
8987     n))
8988
8989 (defun gnus-summary-prev-thread (n)
8990   "Go to the same level previous N'th thread.
8991 Returns the difference between N and the number of skips actually
8992 done."
8993   (interactive "p")
8994   (gnus-summary-next-thread (- n)))
8995
8996 (defun gnus-summary-go-down-thread ()
8997   "Go down one level in the current thread."
8998   (let ((children (gnus-summary-article-children)))
8999     (when children
9000       (gnus-summary-goto-subject (car children)))))
9001
9002 (defun gnus-summary-go-up-thread ()
9003   "Go up one level in the current thread."
9004   (let ((parent (gnus-summary-article-parent)))
9005     (when parent
9006       (gnus-summary-goto-subject parent))))
9007
9008 (defun gnus-summary-down-thread (n)
9009   "Go down thread N steps.
9010 If N is negative, go up instead.
9011 Returns the difference between N and how many steps down that were
9012 taken."
9013   (interactive "p")
9014   (let ((up (< n 0))
9015         (n (abs n)))
9016     (while (and (> n 0)
9017                 (if up (gnus-summary-go-up-thread)
9018                   (gnus-summary-go-down-thread)))
9019       (setq n (1- n)))
9020     (gnus-summary-position-point)
9021     (when (/= 0 n)
9022       (gnus-message 7 "Can't go further"))
9023     n))
9024
9025 (defun gnus-summary-up-thread (n)
9026   "Go up thread N steps.
9027 If N is negative, go up instead.
9028 Returns the difference between N and how many steps down that were
9029 taken."
9030   (interactive "p")
9031   (gnus-summary-down-thread (- n)))
9032
9033 (defun gnus-summary-top-thread ()
9034   "Go to the top of the thread."
9035   (interactive)
9036   (while (gnus-summary-go-up-thread))
9037   (gnus-summary-article-number))
9038
9039 (defun gnus-summary-kill-thread (&optional unmark)
9040   "Mark articles under current thread as read.
9041 If the prefix argument is positive, remove any kinds of marks.
9042 If the prefix argument is negative, tick articles instead."
9043   (interactive "P")
9044   (when unmark
9045     (setq unmark (prefix-numeric-value unmark)))
9046   (let ((articles (gnus-summary-articles-in-thread)))
9047     (save-excursion
9048       ;; Expand the thread.
9049       (gnus-summary-show-thread)
9050       ;; Mark all the articles.
9051       (while articles
9052         (gnus-summary-goto-subject (car articles))
9053         (cond ((null unmark)
9054                (gnus-summary-mark-article-as-read gnus-killed-mark))
9055               ((> unmark 0)
9056                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9057               (t
9058                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9059         (setq articles (cdr articles))))
9060     ;; Hide killed subtrees.
9061     (and (null unmark)
9062          gnus-thread-hide-killed
9063          (gnus-summary-hide-thread))
9064     ;; If marked as read, go to next unread subject.
9065     (when (null unmark)
9066       ;; Go to next unread subject.
9067       (gnus-summary-next-subject 1 t)))
9068   (gnus-set-mode-line 'summary))
9069
9070 ;; Summary sorting commands
9071
9072 (defun gnus-summary-sort-by-number (&optional reverse)
9073   "Sort the summary buffer by article number.
9074 Argument REVERSE means reverse order."
9075   (interactive "P")
9076   (gnus-summary-sort 'number reverse))
9077
9078 (defun gnus-summary-sort-by-author (&optional reverse)
9079   "Sort the summary buffer by author name alphabetically.
9080 If `case-fold-search' is non-nil, case of letters is ignored.
9081 Argument REVERSE means reverse order."
9082   (interactive "P")
9083   (gnus-summary-sort 'author reverse))
9084
9085 (defun gnus-summary-sort-by-subject (&optional reverse)
9086   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9087 If `case-fold-search' is non-nil, case of letters is ignored.
9088 Argument REVERSE means reverse order."
9089   (interactive "P")
9090   (gnus-summary-sort 'subject reverse))
9091
9092 (defun gnus-summary-sort-by-date (&optional reverse)
9093   "Sort the summary buffer by date.
9094 Argument REVERSE means reverse order."
9095   (interactive "P")
9096   (gnus-summary-sort 'date reverse))
9097
9098 (defun gnus-summary-sort-by-score (&optional reverse)
9099   "Sort the summary buffer by score.
9100 Argument REVERSE means reverse order."
9101   (interactive "P")
9102   (gnus-summary-sort 'score reverse))
9103
9104 (defun gnus-summary-sort-by-lines (&optional reverse)
9105   "Sort the summary buffer by the number of lines.
9106 Argument REVERSE means reverse order."
9107   (interactive "P")
9108   (gnus-summary-sort 'lines reverse))
9109
9110 (defun gnus-summary-sort-by-chars (&optional reverse)
9111   "Sort the summary buffer by article length.
9112 Argument REVERSE means reverse order."
9113   (interactive "P")
9114   (gnus-summary-sort 'chars reverse))
9115
9116 (defun gnus-summary-sort (predicate reverse)
9117   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9118   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9119          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9120          (gnus-thread-sort-functions
9121           (if (not reverse)
9122               thread
9123             `(lambda (t1 t2)
9124                (,thread t2 t1))))
9125          (gnus-sort-gathered-threads-function
9126           gnus-thread-sort-functions)
9127          (gnus-article-sort-functions
9128           (if (not reverse)
9129               article
9130             `(lambda (t1 t2)
9131                (,article t2 t1))))
9132          (buffer-read-only)
9133          (gnus-summary-prepare-hook nil))
9134     ;; We do the sorting by regenerating the threads.
9135     (gnus-summary-prepare)
9136     ;; Hide subthreads if needed.
9137     (when (and gnus-show-threads gnus-thread-hide-subtree)
9138       (gnus-summary-hide-all-threads))))
9139
9140 ;; Summary saving commands.
9141
9142 (defun gnus-summary-save-article (&optional n not-saved)
9143   "Save the current article using the default saver function.
9144 If N is a positive number, save the N next articles.
9145 If N is a negative number, save the N previous articles.
9146 If N is nil and any articles have been marked with the process mark,
9147 save those articles instead.
9148 The variable `gnus-default-article-saver' specifies the saver function."
9149   (interactive "P")
9150   (let* ((articles (gnus-summary-work-articles n))
9151          (save-buffer (save-excursion
9152                         (nnheader-set-temp-buffer " *Gnus Save*")))
9153          (num (length articles))
9154          header file)
9155     (dolist (article articles)
9156       (setq header (gnus-summary-article-header article))
9157       (if (not (vectorp header))
9158           ;; This is a pseudo-article.
9159           (if (assq 'name header)
9160               (gnus-copy-file (cdr (assq 'name header)))
9161             (gnus-message 1 "Article %d is unsaveable" article))
9162         ;; This is a real article.
9163         (save-window-excursion
9164           (gnus-summary-select-article t nil nil article))
9165         (save-excursion
9166           (set-buffer save-buffer)
9167           (erase-buffer)
9168           (insert-buffer-substring gnus-original-article-buffer))
9169         (setq file (gnus-article-save save-buffer file num))
9170         (gnus-summary-remove-process-mark article)
9171         (unless not-saved
9172           (gnus-summary-set-saved-mark article))))
9173     (gnus-kill-buffer save-buffer)
9174     (gnus-summary-position-point)
9175     (gnus-set-mode-line 'summary)
9176     n))
9177
9178 (defun gnus-summary-pipe-output (&optional arg)
9179   "Pipe the current article to a subprocess.
9180 If N is a positive number, pipe the N next articles.
9181 If N is a negative number, pipe the N previous articles.
9182 If N is nil and any articles have been marked with the process mark,
9183 pipe those articles instead."
9184   (interactive "P")
9185   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9186     (gnus-summary-save-article arg t))
9187   (gnus-configure-windows 'pipe))
9188
9189 (defun gnus-summary-save-article-mail (&optional arg)
9190   "Append the current article to an mail file.
9191 If N is a positive number, save the N next articles.
9192 If N is a negative number, save the N previous articles.
9193 If N is nil and any articles have been marked with the process mark,
9194 save those articles instead."
9195   (interactive "P")
9196   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9197     (gnus-summary-save-article arg)))
9198
9199 (defun gnus-summary-save-article-rmail (&optional arg)
9200   "Append the current article to an rmail file.
9201 If N is a positive number, save the N next articles.
9202 If N is a negative number, save the N previous articles.
9203 If N is nil and any articles have been marked with the process mark,
9204 save those articles instead."
9205   (interactive "P")
9206   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9207     (gnus-summary-save-article arg)))
9208
9209 (defun gnus-summary-save-article-file (&optional arg)
9210   "Append the current article to a file.
9211 If N is a positive number, save the N next articles.
9212 If N is a negative number, save the N previous articles.
9213 If N is nil and any articles have been marked with the process mark,
9214 save those articles instead."
9215   (interactive "P")
9216   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9217     (gnus-summary-save-article arg)))
9218
9219 (defun gnus-summary-write-article-file (&optional arg)
9220   "Write the current article to a file, deleting the previous file.
9221 If N is a positive number, save the N next articles.
9222 If N is a negative number, save the N previous articles.
9223 If N is nil and any articles have been marked with the process mark,
9224 save those articles instead."
9225   (interactive "P")
9226   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9227     (gnus-summary-save-article arg)))
9228
9229 (defun gnus-summary-save-article-body-file (&optional arg)
9230   "Append the current article body to a file.
9231 If N is a positive number, save the N next articles.
9232 If N is a negative number, save the N previous articles.
9233 If N is nil and any articles have been marked with the process mark,
9234 save those articles instead."
9235   (interactive "P")
9236   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9237     (gnus-summary-save-article arg)))
9238
9239 (defun gnus-summary-pipe-message (program)
9240   "Pipe the current article through PROGRAM."
9241   (interactive "sProgram: ")
9242   (gnus-summary-select-article)
9243   (let ((mail-header-separator ""))
9244     (gnus-eval-in-buffer-window gnus-article-buffer
9245       (save-restriction
9246         (widen)
9247         (let ((start (window-start))
9248               buffer-read-only)
9249           (message-pipe-buffer-body program)
9250           (set-window-start (get-buffer-window (current-buffer)) start))))))
9251
9252 (defun gnus-get-split-value (methods)
9253   "Return a value based on the split METHODS."
9254   (let (split-name method result match)
9255     (when methods
9256       (save-excursion
9257         (set-buffer gnus-original-article-buffer)
9258         (save-restriction
9259           (nnheader-narrow-to-headers)
9260           (while methods
9261             (goto-char (point-min))
9262             (setq method (pop methods))
9263             (setq match (car method))
9264             (when (cond
9265                    ((stringp match)
9266                     ;; Regular expression.
9267                     (ignore-errors
9268                       (re-search-forward match nil t)))
9269                    ((gnus-functionp match)
9270                     ;; Function.
9271                     (save-restriction
9272                       (widen)
9273                       (setq result (funcall match gnus-newsgroup-name))))
9274                    ((consp match)
9275                     ;; Form.
9276                     (save-restriction
9277                       (widen)
9278                       (setq result (eval match)))))
9279               (setq split-name (append (cdr method) split-name))
9280               (cond ((stringp result)
9281                      (push (expand-file-name
9282                             result gnus-article-save-directory)
9283                            split-name))
9284                     ((consp result)
9285                      (setq split-name (append result split-name)))))))))
9286     (nreverse split-name)))
9287
9288 (defun gnus-valid-move-group-p (group)
9289   (and (boundp group)
9290        (symbol-name group)
9291        (symbol-value group)
9292        (gnus-get-function (gnus-find-method-for-group
9293                            (symbol-name group)) 'request-accept-article t)))
9294
9295 (defun gnus-read-move-group-name (prompt default articles prefix)
9296   "Read a group name."
9297   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9298          (minibuffer-confirm-incomplete nil) ; XEmacs
9299          (prom
9300           (format "%s %s to:"
9301                   prompt
9302                   (if (> (length articles) 1)
9303                       (format "these %d articles" (length articles))
9304                     "this article")))
9305          (to-newsgroup
9306           (cond
9307            ((null split-name)
9308             (gnus-completing-read default prom
9309                                   gnus-active-hashtb
9310                                   'gnus-valid-move-group-p
9311                                   nil prefix
9312                                   'gnus-group-history))
9313            ((= 1 (length split-name))
9314             (gnus-completing-read (car split-name) prom
9315                                   gnus-active-hashtb
9316                                   'gnus-valid-move-group-p
9317                                   nil nil
9318                                   'gnus-group-history))
9319            (t
9320             (gnus-completing-read nil prom
9321                                   (mapcar (lambda (el) (list el))
9322                                           (nreverse split-name))
9323                                   nil nil nil
9324                                   'gnus-group-history))))
9325          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9326     (when to-newsgroup
9327       (if (or (string= to-newsgroup "")
9328               (string= to-newsgroup prefix))
9329           (setq to-newsgroup default))
9330       (unless to-newsgroup
9331         (error "No group name entered"))
9332       (or (gnus-active to-newsgroup)
9333           (gnus-activate-group to-newsgroup nil nil to-method)
9334           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9335                                      to-newsgroup))
9336               (or (and (gnus-request-create-group to-newsgroup to-method)
9337                        (gnus-activate-group
9338                         to-newsgroup nil nil to-method)
9339                        (gnus-subscribe-group to-newsgroup))
9340                   (error "Couldn't create group %s" to-newsgroup)))
9341           (error "No such group: %s" to-newsgroup)))
9342     to-newsgroup))
9343
9344 (defun gnus-summary-save-parts (type dir n &optional reverse)
9345   "Save parts matching TYPE to DIR.
9346 If REVERSE, save parts that do not match TYPE."
9347   (interactive
9348    (list (read-string "Save parts of type: " 
9349                       (or (car gnus-summary-save-parts-type-history)
9350                           gnus-summary-save-parts-default-mime)
9351                       'gnus-summary-save-parts-type-history)
9352          (setq gnus-summary-save-parts-last-directory
9353                (read-file-name "Save to directory: " 
9354                                gnus-summary-save-parts-last-directory
9355                                nil t))
9356          current-prefix-arg))
9357   (gnus-summary-iterate n
9358     (let ((gnus-display-mime-function nil)
9359           (gnus-inhibit-treatment t))
9360       (gnus-summary-select-article))
9361     (save-excursion
9362       (set-buffer gnus-article-buffer)
9363       (let ((handles (or gnus-article-mime-handles
9364                          (mm-dissect-buffer) (mm-uu-dissect))))
9365         (when handles
9366           (gnus-summary-save-parts-1 type dir handles reverse)
9367           (unless gnus-article-mime-handles ;; Don't destroy this case.
9368             (mm-destroy-parts handles)))))))
9369
9370 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9371   (if (stringp (car handle))
9372       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9373               (cdr handle))
9374     (when (if reverse
9375               (not (string-match type (mm-handle-media-type handle)))
9376             (string-match type (mm-handle-media-type handle)))
9377       (let ((file (expand-file-name
9378                    (file-name-nondirectory
9379                     (or
9380                      (mail-content-type-get
9381                       (mm-handle-disposition handle) 'filename)
9382                      (concat gnus-newsgroup-name
9383                              "." (number-to-string
9384                                   (cdr gnus-article-current)))))
9385                    dir)))
9386         (unless (file-exists-p file)
9387           (mm-save-part-to-file handle file))))))
9388
9389 ;; Summary extract commands
9390
9391 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9392   (let ((buffer-read-only nil)
9393         (article (gnus-summary-article-number))
9394         after-article b e)
9395     (unless (gnus-summary-goto-subject article)
9396       (error "No such article: %d" article))
9397     (gnus-summary-position-point)
9398     ;; If all commands are to be bunched up on one line, we collect
9399     ;; them here.
9400     (unless gnus-view-pseudos-separately
9401       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9402             files action)
9403         (while ps
9404           (setq action (cdr (assq 'action (car ps))))
9405           (setq files (list (cdr (assq 'name (car ps)))))
9406           (while (and ps (cdr ps)
9407                       (string= (or action "1")
9408                                (or (cdr (assq 'action (cadr ps))) "2")))
9409             (push (cdr (assq 'name (cadr ps))) files)
9410             (setcdr ps (cddr ps)))
9411           (when files
9412             (when (not (string-match "%s" action))
9413               (push " " files))
9414             (push " " files)
9415             (when (assq 'execute (car ps))
9416               (setcdr (assq 'execute (car ps))
9417                       (funcall (if (string-match "%s" action)
9418                                    'format 'concat)
9419                                action
9420                                (mapconcat
9421                                 (lambda (f)
9422                                   (if (equal f " ")
9423                                       f
9424                                     (gnus-quote-arg-for-sh-or-csh f)))
9425                                 files " ")))))
9426           (setq ps (cdr ps)))))
9427     (if (and gnus-view-pseudos (not not-view))
9428         (while pslist
9429           (when (assq 'execute (car pslist))
9430             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9431                                   (eq gnus-view-pseudos 'not-confirm)))
9432           (setq pslist (cdr pslist)))
9433       (save-excursion
9434         (while pslist
9435           (setq after-article (or (cdr (assq 'article (car pslist)))
9436                                   (gnus-summary-article-number)))
9437           (gnus-summary-goto-subject after-article)
9438           (forward-line 1)
9439           (setq b (point))
9440           (insert "    " (file-name-nondirectory
9441                           (cdr (assq 'name (car pslist))))
9442                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9443           (setq e (point))
9444           (forward-line -1)             ; back to `b'
9445           (gnus-add-text-properties
9446            b (1- e) (list 'gnus-number gnus-reffed-article-number
9447                           gnus-mouse-face-prop gnus-mouse-face))
9448           (gnus-data-enter
9449            after-article gnus-reffed-article-number
9450            gnus-unread-mark b (car pslist) 0 (- e b))
9451           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9452           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9453           (setq pslist (cdr pslist)))))))
9454
9455 (defun gnus-pseudos< (p1 p2)
9456   (let ((c1 (cdr (assq 'action p1)))
9457         (c2 (cdr (assq 'action p2))))
9458     (and c1 c2 (string< c1 c2))))
9459
9460 (defun gnus-request-pseudo-article (props)
9461   (cond ((assq 'execute props)
9462          (gnus-execute-command (cdr (assq 'execute props)))))
9463   (let ((gnus-current-article (gnus-summary-article-number)))
9464     (gnus-run-hooks 'gnus-mark-article-hook)))
9465
9466 (defun gnus-execute-command (command &optional automatic)
9467   (save-excursion
9468     (gnus-article-setup-buffer)
9469     (set-buffer gnus-article-buffer)
9470     (setq buffer-read-only nil)
9471     (let ((command (if automatic command
9472                      (read-string "Command: " (cons command 0)))))
9473       (erase-buffer)
9474       (insert "$ " command "\n\n")
9475       (if gnus-view-pseudo-asynchronously
9476           (start-process "gnus-execute" (current-buffer) shell-file-name
9477                          shell-command-switch command)
9478         (call-process shell-file-name nil t nil
9479                       shell-command-switch command)))))
9480
9481 ;; Summary kill commands.
9482
9483 (defun gnus-summary-edit-global-kill (article)
9484   "Edit the \"global\" kill file."
9485   (interactive (list (gnus-summary-article-number)))
9486   (gnus-group-edit-global-kill article))
9487
9488 (defun gnus-summary-edit-local-kill ()
9489   "Edit a local kill file applied to the current newsgroup."
9490   (interactive)
9491   (setq gnus-current-headers (gnus-summary-article-header))
9492   (gnus-group-edit-local-kill
9493    (gnus-summary-article-number) gnus-newsgroup-name))
9494
9495 ;;; Header reading.
9496
9497 (defun gnus-read-header (id &optional header)
9498   "Read the headers of article ID and enter them into the Gnus system."
9499   (let ((group gnus-newsgroup-name)
9500         (gnus-override-method
9501          (or
9502           gnus-override-method
9503           (and (gnus-news-group-p gnus-newsgroup-name)
9504                (car (gnus-refer-article-methods)))))
9505         where)
9506     ;; First we check to see whether the header in question is already
9507     ;; fetched.
9508     (if (stringp id)
9509         ;; This is a Message-ID.
9510         (setq header (or header (gnus-id-to-header id)))
9511       ;; This is an article number.
9512       (setq header (or header (gnus-summary-article-header id))))
9513     (if (and header
9514              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9515         ;; We have found the header.
9516         header
9517       ;; If this is a sparse article, we have to nix out its
9518       ;; previous entry in the thread hashtb.
9519       (when (and header
9520                  (gnus-summary-article-sparse-p (mail-header-number header)))
9521         (let* ((parent (gnus-parent-id (mail-header-references header)))
9522                (thread (and parent (gnus-id-to-thread parent))))
9523           (when thread
9524             (delq (assq header thread) thread))))
9525       ;; We have to really fetch the header to this article.
9526       (save-excursion
9527         (set-buffer nntp-server-buffer)
9528         (when (setq where (gnus-request-head id group))
9529           (nnheader-fold-continuation-lines)
9530           (goto-char (point-max))
9531           (insert ".\n")
9532           (goto-char (point-min))
9533           (insert "211 ")
9534           (princ (cond
9535                   ((numberp id) id)
9536                   ((cdr where) (cdr where))
9537                   (header (mail-header-number header))
9538                   (t gnus-reffed-article-number))
9539                  (current-buffer))
9540           (insert " Article retrieved.\n"))
9541         (if (or (not where)
9542                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9543             ()                          ; Malformed head.
9544           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9545             (when (and (stringp id)
9546                        (not (string= (gnus-group-real-name group)
9547                                      (car where))))
9548               ;; If we fetched by Message-ID and the article came
9549               ;; from a different group, we fudge some bogus article
9550               ;; numbers for this article.
9551               (mail-header-set-number header gnus-reffed-article-number))
9552             (save-excursion
9553               (set-buffer gnus-summary-buffer)
9554               (decf gnus-reffed-article-number)
9555               (gnus-remove-header (mail-header-number header))
9556               (push header gnus-newsgroup-headers)
9557               (setq gnus-current-headers header)
9558               (push (mail-header-number header) gnus-newsgroup-limit)))
9559           header)))))
9560
9561 (defun gnus-remove-header (number)
9562   "Remove header NUMBER from `gnus-newsgroup-headers'."
9563   (if (and gnus-newsgroup-headers
9564            (= number (mail-header-number (car gnus-newsgroup-headers))))
9565       (pop gnus-newsgroup-headers)
9566     (let ((headers gnus-newsgroup-headers))
9567       (while (and (cdr headers)
9568                   (not (= number (mail-header-number (cadr headers)))))
9569         (pop headers))
9570       (when (cdr headers)
9571         (setcdr headers (cddr headers))))))
9572
9573 ;;;
9574 ;;; summary highlights
9575 ;;;
9576
9577 (defun gnus-highlight-selected-summary ()
9578   "Highlight selected article in summary buffer."
9579   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9580   (when gnus-summary-selected-face
9581     (save-excursion
9582       (let* ((beg (progn (beginning-of-line) (point)))
9583              (end (progn (end-of-line) (point)))
9584              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9585              (from (if (get-text-property beg gnus-mouse-face-prop)
9586                        beg
9587                      (or (next-single-property-change
9588                           beg gnus-mouse-face-prop nil end)
9589                          beg)))
9590              (to
9591               (if (= from end)
9592                   (- from 2)
9593                 (or (next-single-property-change
9594                      from gnus-mouse-face-prop nil end)
9595                     end))))
9596         ;; If no mouse-face prop on line we will have to = from = end,
9597         ;; so we highlight the entire line instead.
9598         (when (= (+ to 2) from)
9599           (setq from beg)
9600           (setq to end))
9601         (if gnus-newsgroup-selected-overlay
9602             ;; Move old overlay.
9603             (gnus-move-overlay
9604              gnus-newsgroup-selected-overlay from to (current-buffer))
9605           ;; Create new overlay.
9606           (gnus-overlay-put
9607            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9608            'face gnus-summary-selected-face))))))
9609
9610 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9611 (defun gnus-summary-highlight-line ()
9612   "Highlight current line according to `gnus-summary-highlight'."
9613   (let* ((list gnus-summary-highlight)
9614          (p (point))
9615          (end (progn (end-of-line) (point)))
9616          ;; now find out where the line starts and leave point there.
9617          (beg (progn (beginning-of-line) (point)))
9618          (article (gnus-summary-article-number))
9619          (score (or (cdr (assq (or article gnus-current-article)
9620                                gnus-newsgroup-scored))
9621                     gnus-summary-default-score 0))
9622          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9623          (inhibit-read-only t))
9624     ;; Eval the cars of the lists until we find a match.
9625     (let ((default gnus-summary-default-score))
9626       (while (and list
9627                   (not (eval (caar list))))
9628         (setq list (cdr list))))
9629     (let ((face (cdar list)))
9630       (unless (eq face (get-text-property beg 'face))
9631         (gnus-put-text-property-excluding-characters-with-faces
9632          beg end 'face
9633          (setq face (if (boundp face) (symbol-value face) face)))
9634         (when gnus-summary-highlight-line-function
9635           (funcall gnus-summary-highlight-line-function article face))))
9636     (goto-char p)))
9637
9638 (defun gnus-update-read-articles (group unread &optional compute)
9639   "Update the list of read articles in GROUP."
9640   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9641          (entry (gnus-gethash group gnus-newsrc-hashtb))
9642          (info (nth 2 entry))
9643          (prev 1)
9644          (unread (sort (copy-sequence unread) '<))
9645          read)
9646     (if (or (not info) (not active))
9647         ;; There is no info on this group if it was, in fact,
9648         ;; killed.  Gnus stores no information on killed groups, so
9649         ;; there's nothing to be done.
9650         ;; One could store the information somewhere temporarily,
9651         ;; perhaps...  Hmmm...
9652         ()
9653       ;; Remove any negative articles numbers.
9654       (while (and unread (< (car unread) 0))
9655         (setq unread (cdr unread)))
9656       ;; Remove any expired article numbers
9657       (while (and unread (< (car unread) (car active)))
9658         (setq unread (cdr unread)))
9659       ;; Compute the ranges of read articles by looking at the list of
9660       ;; unread articles.
9661       (while unread
9662         (when (/= (car unread) prev)
9663           (push (if (= prev (1- (car unread))) prev
9664                   (cons prev (1- (car unread))))
9665                 read))
9666         (setq prev (1+ (car unread)))
9667         (setq unread (cdr unread)))
9668       (when (<= prev (cdr active))
9669         (push (cons prev (cdr active)) read))
9670       (setq read (if (> (length read) 1) (nreverse read) read))
9671       (if compute
9672           read
9673         (save-excursion
9674           (let (setmarkundo)
9675             ;; Propagate the read marks to the backend.
9676             (when (gnus-check-backend-function 'request-set-mark group)
9677               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9678                     (add (gnus-remove-from-range read (gnus-info-read info))))
9679                 (when (or add del)
9680                   (unless (gnus-check-group group)
9681                     (error "Can't open server for %s" group))
9682                   (gnus-request-set-mark
9683                    group (delq nil (list (if add (list add 'add '(read)))
9684                                          (if del (list del 'del '(read))))))
9685                   (setq setmarkundo
9686                         `(gnus-request-set-mark
9687                           ,group
9688                           ',(delq nil (list
9689                                        (if del (list del 'add '(read)))
9690                                        (if add (list add 'del '(read))))))))))
9691             (set-buffer gnus-group-buffer)
9692             (gnus-undo-register
9693               `(progn
9694                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9695                  (gnus-info-set-read ',info ',(gnus-info-read info))
9696                  (gnus-get-unread-articles-in-group ',info 
9697                                                     (gnus-active ,group))
9698                  (gnus-group-update-group ,group t)
9699                  ,setmarkundo))))
9700         ;; Enter this list into the group info.
9701         (gnus-info-set-read info read)
9702         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9703         (gnus-get-unread-articles-in-group info (gnus-active group))
9704         t))))
9705
9706 (defun gnus-offer-save-summaries ()
9707   "Offer to save all active summary buffers."
9708   (save-excursion
9709     (let ((buflist (buffer-list))
9710           buffers bufname)
9711       ;; Go through all buffers and find all summaries.
9712       (while buflist
9713         (and (setq bufname (buffer-name (car buflist)))
9714              (string-match "Summary" bufname)
9715              (save-excursion
9716                (set-buffer bufname)
9717                ;; We check that this is, indeed, a summary buffer.
9718                (and (eq major-mode 'gnus-summary-mode)
9719                     ;; Also make sure this isn't bogus.
9720                     gnus-newsgroup-prepared
9721                     ;; Also make sure that this isn't a dead summary buffer.
9722                     (not gnus-dead-summary-mode)))
9723              (push bufname buffers))
9724         (setq buflist (cdr buflist)))
9725       ;; Go through all these summary buffers and offer to save them.
9726       (when buffers
9727         (map-y-or-n-p
9728          "Update summary buffer %s? "
9729          (lambda (buf)
9730            (switch-to-buffer buf)
9731            (gnus-summary-exit))
9732          buffers)))))
9733
9734
9735 ;;; @ for mime-partial
9736 ;;;
9737
9738 (defun gnus-request-partial-message ()
9739   (save-excursion
9740     (let ((number (gnus-summary-article-number))
9741           (group gnus-newsgroup-name)
9742           (mother gnus-article-buffer))
9743       (set-buffer (get-buffer-create " *Partial Article*"))
9744       (erase-buffer)
9745       (setq mime-preview-buffer mother)
9746       (gnus-request-article-this-buffer number group)
9747       (mime-parse-buffer)
9748       )))
9749
9750 (autoload 'mime-combine-message/partial-pieces-automatically
9751   "mime-partial"
9752   "Internal method to combine message/partial messages automatically.")
9753
9754 (mime-add-condition
9755  'action '((type . message)(subtype . partial)
9756            (major-mode . gnus-original-article-mode)
9757            (method . mime-combine-message/partial-pieces-automatically)
9758            (summary-buffer-exp . gnus-summary-buffer)
9759            (request-partial-message-method . gnus-request-partial-message)
9760            ))
9761
9762
9763 ;;; @ for message/rfc822
9764 ;;;
9765
9766 (defun gnus-mime-extract-message/rfc822 (entity situation)
9767   (let (group article num cwin swin cur)
9768     (with-temp-buffer
9769       (mime-insert-entity-content entity)
9770       (setq group (or (cdr (assq 'group situation))
9771                       (completing-read "Group: "
9772                                        gnus-active-hashtb
9773                                        nil
9774                                        (gnus-read-active-file-p)
9775                                        gnus-newsgroup-name))
9776             article (gnus-request-accept-article group)))
9777     (when (and (consp article)
9778                (numberp (setq article (cdr article))))
9779       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9780             cwin (get-buffer-window (current-buffer) t))
9781       (save-window-excursion
9782         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9783             (select-window swin)
9784           (set-buffer gnus-summary-buffer))
9785         (setq cur gnus-current-article)
9786         (forward-line num)
9787         (let (gnus-show-threads)
9788           (gnus-summary-goto-subject article t))
9789         (gnus-summary-clear-mark-forward 1)
9790         (gnus-summary-goto-subject cur))
9791       (when (and cwin (window-frame cwin))
9792         (select-frame (window-frame cwin)))
9793       (when (boundp 'mime-acting-situation-to-override)
9794         (set-alist 'mime-acting-situation-to-override
9795                    'group
9796                    group)
9797         (set-alist 'mime-acting-situation-to-override
9798                    'after-method
9799                    `(progn
9800                       (save-current-buffer
9801                         (set-buffer gnus-group-buffer)
9802                         (gnus-activate-group ,group))
9803                       (gnus-summary-goto-article ,cur
9804                                                  gnus-show-all-headers)))
9805         (set-alist 'mime-acting-situation-to-override
9806                    'number num)))))
9807
9808 (mime-add-condition
9809  'action '((type . message)(subtype . rfc822)
9810            (major-mode . gnus-original-article-mode)
9811            (method . gnus-mime-extract-message/rfc822)
9812            (mode . "extract")
9813            ))
9814
9815 (mime-add-condition
9816  'action '((type . message)(subtype . news)
9817            (major-mode . gnus-original-article-mode)
9818            (method . gnus-mime-extract-message/rfc822)
9819            (mode . "extract")
9820            ))
9821
9822 (defun gnus-mime-extract-multipart (entity situation)
9823   (let ((children (mime-entity-children entity))
9824         mime-acting-situation-to-override
9825         f)
9826     (while children
9827       (mime-play-entity (car children)
9828                         (cons (assq 'mode situation)
9829                               mime-acting-situation-to-override))
9830       (setq children (cdr children)))
9831     (if (setq f (cdr (assq 'after-method
9832                            mime-acting-situation-to-override)))
9833         (eval f)
9834       )))
9835
9836 (mime-add-condition
9837  'action '((type . multipart)
9838            (method . gnus-mime-extract-multipart)
9839            (mode . "extract")
9840            )
9841  'with-default)
9842
9843
9844 ;;; @ end
9845 ;;;
9846
9847 (defun gnus-summary-setup-default-charset ()
9848   "Setup newsgroup default charset."
9849   (if (equal gnus-newsgroup-name "nndraft:drafts")
9850       (setq gnus-newsgroup-charset nil)
9851     (let* ((name (and gnus-newsgroup-name
9852                       (gnus-group-real-name gnus-newsgroup-name)))
9853            (ignored-charsets
9854             (or gnus-newsgroup-ephemeral-ignored-charsets
9855                 (append
9856                  (and gnus-newsgroup-name
9857                       (or (gnus-group-find-parameter gnus-newsgroup-name
9858                                                      'ignored-charsets t)
9859                           (let ((alist gnus-group-ignored-charsets-alist)
9860                                 elem (charsets nil))
9861                             (while (setq elem (pop alist))
9862                               (when (and name
9863                                          (string-match (car elem) name))
9864                                 (setq alist nil
9865                                       charsets (cdr elem))))
9866                             charsets)))
9867                  gnus-newsgroup-ignored-charsets))))
9868       (setq gnus-newsgroup-charset
9869             (or gnus-newsgroup-ephemeral-charset
9870                 (and gnus-newsgroup-name
9871                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9872                          (let ((alist gnus-group-charset-alist)
9873                                elem charset)
9874                            (while (setq elem (pop alist))
9875                              (when (and name
9876                                         (string-match (car elem) name))
9877                                (setq alist nil
9878                                      charset (cadr elem))))
9879                            charset)))
9880                 gnus-default-charset))
9881       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9882            ignored-charsets))))
9883
9884 ;;;
9885 ;;; Mime Commands
9886 ;;;
9887
9888 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9889   "Display the current article buffer fully MIME-buttonized.
9890 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9891 treated as multipart/mixed."
9892   (interactive "P")
9893   (require 'gnus-art)
9894   (let ((gnus-unbuttonized-mime-types nil)
9895         (gnus-mime-display-multipart-as-mixed show-all-parts))
9896     (gnus-summary-show-article)))
9897
9898 (defun gnus-summary-repair-multipart (article)
9899   "Add a Content-Type header to a multipart article without one."
9900   (interactive (list (gnus-summary-article-number)))
9901   (gnus-with-article article
9902     (message-narrow-to-head)
9903     (goto-char (point-max))
9904     (widen)
9905     (when (search-forward "\n--" nil t)
9906       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9907         (message-narrow-to-head)
9908         (message-remove-header "Mime-Version")
9909         (message-remove-header "Content-Type")
9910         (goto-char (point-max))
9911         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9912                         separator))
9913         (insert "Mime-Version: 1.0\n")
9914         (widen))))
9915   (let (gnus-mark-article-hook)
9916     (gnus-summary-select-article t t nil article)))
9917
9918 (defun gnus-summary-toggle-display-buttonized ()
9919   "Toggle the buttonizing of the article buffer."
9920   (interactive)
9921   (require 'gnus-art)
9922   (if (setq gnus-inhibit-mime-unbuttonizing
9923             (not gnus-inhibit-mime-unbuttonizing))
9924       (let ((gnus-unbuttonized-mime-types nil))
9925         (gnus-summary-show-article))
9926     (gnus-summary-show-article)))
9927
9928 ;;;
9929 ;;; Intelli-mouse commmands
9930 ;;;
9931
9932 (defun gnus-wheel-summary-scroll (event)
9933   (interactive "e")
9934   (let ((amount (if (memq 'shift (event-modifiers event))
9935                     (car gnus-wheel-scroll-amount)
9936                   (cdr gnus-wheel-scroll-amount)))
9937         (direction (- (* (static-if (featurep 'xemacs)
9938                              (event-button event)
9939                            (cond ((eq 'mouse-4 (event-basic-type event))
9940                                   4)
9941                                  ((eq 'mouse-5 (event-basic-type event))
9942                                   5)))
9943                          2) 9))
9944         edge)
9945     (gnus-summary-scroll-up (* amount direction))
9946     (when (gnus-eval-in-buffer-window gnus-article-buffer
9947             (save-restriction
9948               (widen)
9949               (and (if (< 0 direction)
9950                        (gnus-article-next-page 0)
9951                      (gnus-article-prev-page 0)
9952                      (bobp))
9953                    (if (setq edge (get-text-property
9954                                    (point-min) 'gnus-wheel-edge))
9955                        (setq edge (* edge direction))
9956                      (setq edge -1))
9957                    (or (plusp edge)
9958                        (let ((buffer-read-only nil)
9959                              (inhibit-read-only t))
9960                          (put-text-property (point-min) (point-max)
9961                                             'gnus-wheel-edge direction)
9962                          nil))
9963                    (or (> edge gnus-wheel-edge-resistance)
9964                        (let ((buffer-read-only nil)
9965                              (inhibit-read-only t))
9966                          (put-text-property (point-min) (point-max)
9967                                             'gnus-wheel-edge
9968                                             (* (1+ edge) direction))
9969                          nil))
9970                    (eq last-command 'gnus-wheel-summary-scroll))))
9971       (gnus-summary-next-article nil nil (minusp direction)))))
9972
9973 (defun gnus-wheel-install ()
9974   "Enable mouse wheel support on summary window."
9975   (when gnus-use-wheel
9976     (let ((keys
9977            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9978       (dolist (key keys)
9979         (define-key gnus-summary-mode-map key
9980           'gnus-wheel-summary-scroll)))))
9981
9982 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9983
9984 ;;;
9985 ;;; Traditional PGP commmands
9986 ;;;
9987
9988 (defun gnus-summary-decrypt-article (&optional force)
9989   "Decrypt the current article in traditional PGP way.
9990 This will have permanent effect only in mail groups.
9991 If FORCE is non-nil, allow editing of articles even in read-only
9992 groups."
9993   (interactive "P")
9994   (gnus-summary-select-article t)
9995   (gnus-eval-in-buffer-window gnus-article-buffer
9996     (save-excursion
9997       (save-restriction
9998         (widen)
9999         (goto-char (point-min))
10000         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
10001           (error "Not a traditional PGP message!"))
10002         (let ((armor-start (match-beginning 0)))
10003           (if (and (pgg-decrypt-region armor-start (point-max))
10004                    (or force (not (gnus-group-read-only-p))))
10005               (let ((inhibit-read-only t)
10006                     buffer-read-only)
10007                 (delete-region armor-start
10008                                (progn
10009                                  (re-search-forward "^-+END PGP" nil t)
10010                                  (beginning-of-line 2)
10011                                  (point)))
10012                 (insert-buffer-substring pgg-output-buffer))))))))
10013
10014 (defun gnus-summary-verify-article ()
10015   "Verify the current article in traditional PGP way."
10016   (interactive)
10017   (save-excursion
10018     (set-buffer gnus-original-article-buffer)
10019     (goto-char (point-min))
10020     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
10021       (error "Not a traditional PGP message!"))
10022     (re-search-forward "^-+END PGP" nil t)
10023     (beginning-of-line 2)
10024     (call-interactively (function pgg-verify-region))))
10025
10026 ;;;
10027 ;;; Generic summary marking commands
10028 ;;;
10029
10030 (defvar gnus-summary-marking-alist
10031   '((read gnus-del-mark "d")
10032     (unread gnus-unread-mark "u")
10033     (ticked gnus-ticked-mark "!")
10034     (dormant gnus-dormant-mark "?")
10035     (expirable gnus-expirable-mark "e"))
10036   "An alist of names/marks/keystrokes.")
10037
10038 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10039 (defvar gnus-summary-mark-map)
10040
10041 (defun gnus-summary-make-all-marking-commands ()
10042   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10043   (dolist (elem gnus-summary-marking-alist)
10044     (apply 'gnus-summary-make-marking-command elem)))
10045
10046 (defun gnus-summary-make-marking-command (name mark keystroke)
10047   (let ((map (make-sparse-keymap)))
10048     (define-key gnus-summary-generic-mark-map keystroke map)
10049     (dolist (lway `((next "next" next nil "n")
10050                     (next-unread "next unread" next t "N")
10051                     (prev "previous" prev nil "p")
10052                     (prev-unread "previous unread" prev t "P")
10053                     (nomove "" nil nil ,keystroke)))
10054       (let ((func (gnus-summary-make-marking-command-1
10055                    mark (car lway) lway name)))
10056         (setq func (eval func))
10057         (define-key map (nth 4 lway) func)))))
10058
10059 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10060   `(defun ,(intern
10061             (format "gnus-summary-put-mark-as-%s%s"
10062                     name (if (eq way 'nomove)
10063                              ""
10064                            (concat "-" (symbol-name way)))))
10065      (n)
10066      ,(format
10067        "Mark the current article as %s%s.
10068 If N, the prefix, then repeat N times.
10069 If N is negative, move in reverse order.
10070 The difference between N and the actual number of articles marked is
10071 returned."
10072        name (car (cdr lway)))
10073      (interactive "p")
10074      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10075
10076 (defun gnus-summary-generic-mark (n mark move unread)
10077   "Mark N articles with MARK."
10078   (unless (eq major-mode 'gnus-summary-mode)
10079     (error "This command can only be used in the summary buffer"))
10080   (gnus-summary-show-thread)
10081   (let ((nummove
10082          (cond
10083           ((eq move 'next) 1)
10084           ((eq move 'prev) -1)
10085           (t 0))))
10086     (if (zerop nummove)
10087         (setq n 1)
10088       (when (< n 0)
10089         (setq n (abs n)
10090               nummove (* -1 nummove))))
10091     (while (and (> n 0)
10092                 (gnus-summary-mark-article nil mark)
10093                 (zerop (gnus-summary-next-subject nummove unread t)))
10094       (setq n (1- n)))
10095     (when (/= 0 n)
10096       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10097     (gnus-summary-recenter)
10098     (gnus-summary-position-point)
10099     (gnus-set-mode-line 'summary)
10100     n))
10101
10102 (gnus-summary-make-all-marking-commands)
10103
10104 (gnus-ems-redefine)
10105
10106 (provide 'gnus-sum)
10107
10108 (run-hooks 'gnus-sum-load-hook)
10109
10110 ;;; gnus-sum.el ends here