2001-07-15 Pavel Jan\e,Bm\e(Bk <Pavel@Janik.cz>
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 ;; Recursive :-(.
40 ;; (require 'gnus-art)
41 (require 'nnoo)
42 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
43 (autoload 'gnus-cache-write-active "gnus-cache")
44 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
45 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
46 (autoload 'mm-uu-dissect "mm-uu")
47
48 (defcustom gnus-kill-summary-on-exit t
49   "*If non-nil, kill the summary buffer when you exit from it.
50 If nil, the summary will become a \"*Dead Summary*\" buffer, and
51 it will be killed sometime later."
52   :group 'gnus-summary-exit
53   :type 'boolean)
54
55 (defcustom gnus-fetch-old-headers nil
56   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
57 If an unread article in the group refers to an older, already read (or
58 just marked as read) article, the old article will not normally be
59 displayed in the Summary buffer.  If this variable is non-nil, Gnus
60 will attempt to grab the headers to the old articles, and thereby
61 build complete threads.  If it has the value `some', only enough
62 headers to connect otherwise loose threads will be displayed.  This
63 variable can also be a number.  In that case, no more than that number
64 of old headers will be fetched.  If it has the value `invisible', all
65 old headers will be fetched, but none will be displayed.
66
67 The server has to support NOV for any of this to work."
68   :group 'gnus-thread
69   :type '(choice (const :tag "off" nil)
70                  (const some)
71                  number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-refer-thread-limit 200
75   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
76 If t, fetch all the available old headers."
77   :group 'gnus-thread
78   :type '(choice number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-summary-make-false-root 'adopt
82   "*nil means that Gnus won't gather loose threads.
83 If the root of a thread has expired or been read in a previous
84 session, the information necessary to build a complete thread has been
85 lost.  Instead of having many small sub-threads from this original thread
86 scattered all over the summary buffer, Gnus can gather them.
87
88 If non-nil, Gnus will try to gather all loose sub-threads from an
89 original thread into one large thread.
90
91 If this variable is non-nil, it should be one of `none', `adopt',
92 `dummy' or `empty'.
93
94 If this variable is `none', Gnus will not make a false root, but just
95 present the sub-threads after another.
96 If this variable is `dummy', Gnus will create a dummy root that will
97 have all the sub-threads as children.
98 If this variable is `adopt', Gnus will make one of the \"children\"
99 the parent and mark all the step-children as such.
100 If this variable is `empty', the \"children\" are printed with empty
101 subject fields.  (Or rather, they will be printed with a string
102 given by the `gnus-summary-same-subject' variable.)"
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const none)
106                  (const dummy)
107                  (const adopt)
108                  (const empty)))
109
110 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
111   "*A regexp to match subjects to be excluded from loose thread gathering.
112 As loose thread gathering is done on subjects only, that means that
113 there can be many false gatherings performed.  By rooting out certain
114 common subjects, gathering might become saner."
115   :group 'gnus-thread
116   :type 'regexp)
117
118 (defcustom gnus-summary-gather-subject-limit nil
119   "*Maximum length of subject comparisons when gathering loose threads.
120 Use nil to compare full subjects.  Setting this variable to a low
121 number will help gather threads that have been corrupted by
122 newsreaders chopping off subject lines, but it might also mean that
123 unrelated articles that have subject that happen to begin with the
124 same few characters will be incorrectly gathered.
125
126 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
127 comparing subjects."
128   :group 'gnus-thread
129   :type '(choice (const :tag "off" nil)
130                  (const fuzzy)
131                  (sexp :menu-tag "on" t)))
132
133 (defcustom gnus-simplify-subject-functions nil
134   "List of functions taking a string argument that simplify subjects.
135 The functions are applied recursively.
136
137 Useful functions to put in this list include: `gnus-simplify-subject-re',
138 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
139   :group 'gnus-thread
140   :type '(repeat function))
141
142 (defcustom gnus-simplify-ignored-prefixes nil
143   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  regexp))
147
148 (defcustom gnus-build-sparse-threads nil
149   "*If non-nil, fill in the gaps in threads.
150 If `some', only fill in the gaps that are needed to tie loose threads
151 together.  If `more', fill in all leaf nodes that Gnus can find.  If
152 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const some)
156                  (const more)
157                  (sexp :menu-tag "all" t)))
158
159 (defcustom gnus-summary-thread-gathering-function
160   'gnus-gather-threads-by-subject
161   "*Function used for gathering loose threads.
162 There are two pre-defined functions: `gnus-gather-threads-by-subject',
163 which only takes Subjects into consideration; and
164 `gnus-gather-threads-by-references', which compared the References
165 headers of the articles to find matches."
166   :group 'gnus-thread
167   :type '(radio (function-item gnus-gather-threads-by-subject)
168                 (function-item gnus-gather-threads-by-references)
169                 (function :tag "other")))
170
171 (defcustom gnus-summary-same-subject ""
172   "*String indicating that the current article has the same subject as the previous.
173 This variable will only be used if the value of
174 `gnus-summary-make-false-root' is `empty'."
175   :group 'gnus-summary-format
176   :type 'string)
177
178 (defcustom gnus-summary-goto-unread t
179   "*If t, many commands will go to the next unread article.
180 This applies to marking commands as well as other commands that
181 \"naturally\" select the next article, like, for instance, `SPC' at
182 the end of an article.
183
184 If nil, the marking commands do NOT go to the next unread article
185 (they go to the next article instead).  If `never', commands that
186 usually go to the next unread article, will go to the next article,
187 whether it is read or not."
188   :group 'gnus-summary-marks
189   :link '(custom-manual "(gnus)Setting Marks")
190   :type '(choice (const :tag "off" nil)
191                  (const never)
192                  (sexp :menu-tag "on" t)))
193
194 (defcustom gnus-summary-default-score 0
195   "*Default article score level.
196 All scores generated by the score files will be added to this score.
197 If this variable is nil, scoring will be disabled."
198   :group 'gnus-score-default
199   :type '(choice (const :tag "disable")
200                  integer))
201
202 (defcustom gnus-summary-default-high-score 0
203   "*Default threshold for a high scored article.
204 An article will be highlighted as high scored if its score is greater
205 than this score."
206   :group 'gnus-score-default
207   :type 'integer)
208
209 (defcustom gnus-summary-default-low-score 0
210   "*Default threshold for a low scored article.
211 An article will be highlighted as low scored if its score is smaller
212 than this score."
213   :group 'gnus-score-default
214   :type 'integer)
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
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-move-split-methods nil
368   "*Variable used to suggest where articles are to be moved to.
369 It uses the same syntax as the `gnus-split-methods' variable.
370 However, whereas `gnus-split-methods' specifies file names as targets,
371 this variable specifies group names."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ?  ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for souped articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-forwarded-mark ?O
438   "*Mark used for articles that have been forwarded."
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-no-mark ?  ;Whitespace
453   "*Mark used for articles that have no other secondary mark."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-ancient-mark ?O
458   "*Mark used for ancient articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-sparse-mark ?Q
463   "*Mark used for sparsely reffed articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-canceled-mark ?G
468   "*Mark used for canceled articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-duplicate-mark ?M
473   "*Mark used for duplicate articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-undownloaded-mark ?@
478   "*Mark used for articles that weren't downloaded."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-downloadable-mark ?%
483   "*Mark used for articles that are to be downloaded."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-unsendable-mark ?=
488   "*Mark used for articles that won't be sent."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-score-over-mark ?+
493   "*Score mark used for articles with high scores."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-score-below-mark ?-
498   "*Score mark used for articles with low scores."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-empty-thread-mark ?  ;Whitespace
503   "*There is no thread under the article."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-not-empty-thread-mark ?=
508   "*There is a thread under the article."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-view-pseudo-asynchronously nil
513   "*If non-nil, Gnus will view pseudo-articles asynchronously."
514   :group 'gnus-extract-view
515   :type 'boolean)
516
517 (defcustom gnus-auto-expirable-marks
518   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
519         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
520         gnus-souped-mark gnus-duplicate-mark)
521   "*The list of marks converted into expiration if a group is auto-expirable."
522   :version "21.1"
523   :group 'gnus-summary
524   :type '(repeat character))
525
526 (defcustom gnus-inhibit-user-auto-expire t
527   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
528   :version "21.1"
529   :group 'gnus-summary
530   :type 'boolean)
531
532 (defcustom gnus-view-pseudos nil
533   "*If `automatic', pseudo-articles will be viewed automatically.
534 If `not-confirm', pseudos will be viewed automatically, and the user
535 will not be asked to confirm the command."
536   :group 'gnus-extract-view
537   :type '(choice (const :tag "off" nil)
538                  (const automatic)
539                  (const not-confirm)))
540
541 (defcustom gnus-view-pseudos-separately t
542   "*If non-nil, one pseudo-article will be created for each file to be viewed.
543 If nil, all files that use the same viewing command will be given as a
544 list of parameters to that command."
545   :group 'gnus-extract-view
546   :type 'boolean)
547
548 (defcustom gnus-insert-pseudo-articles t
549   "*If non-nil, insert pseudo-articles when decoding articles."
550   :group 'gnus-extract-view
551   :type 'boolean)
552
553 (defcustom gnus-summary-dummy-line-format
554   "  %(:                          :%) %S\n"
555   "*The format specification for the dummy roots in the summary buffer.
556 It works along the same lines as a normal formatting string,
557 with some simple extensions.
558
559 %S  The subject"
560   :group 'gnus-threading
561   :type 'string)
562
563 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
564   "*The format specification for the summary mode line.
565 It works along the same lines as a normal formatting string,
566 with some simple extensions:
567
568 %G  Group name
569 %p  Unprefixed group name
570 %A  Current article number
571 %z  Current article score
572 %V  Gnus version
573 %U  Number of unread articles in the group
574 %e  Number of unselected articles in the group
575 %Z  A string with unread/unselected article counts
576 %g  Shortish group name
577 %S  Subject of the current article
578 %u  User-defined spec
579 %s  Current score file name
580 %d  Number of dormant articles
581 %r  Number of articles that have been marked as read in this session
582 %E  Number of articles expunged by the score files"
583   :group 'gnus-summary-format
584   :type 'string)
585
586 (defcustom gnus-list-identifiers nil
587   "Regexp that matches list identifiers to be removed from subject.
588 This can also be a list of regexps."
589   :version "21.1"
590   :group 'gnus-summary-format
591   :group 'gnus-article-hiding
592   :type '(choice (const :tag "none" nil)
593                  (regexp :value ".*")
594                  (repeat :value (".*") regexp)))
595
596 (defcustom gnus-summary-mark-below 0
597   "*Mark all articles with a score below this variable as read.
598 This variable is local to each summary buffer and usually set by the
599 score file."
600   :group 'gnus-score-default
601   :type 'integer)
602
603 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
604   "*List of functions used for sorting articles in the summary buffer.
605
606 Each function takes two articles and returns non-nil if the first
607 article should be sorted before the other.  If you use more than one
608 function, the primary sort function should be the last.  You should
609 probably always include `gnus-article-sort-by-number' in the list of
610 sorting functions -- preferably first.  Also note that sorting by date
611 is often much slower than sorting by number, and the sorting order is
612 very similar.  (Sorting by date means sorting by the time the message
613 was sent, sorting by number means sorting by arrival time.)
614
615 Ready-made functions include `gnus-article-sort-by-number',
616 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
617 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
618
619 When threading is turned on, the variable `gnus-thread-sort-functions'
620 controls how articles are sorted."
621   :group 'gnus-summary-sort
622   :type '(repeat (choice (function-item gnus-article-sort-by-number)
623                          (function-item gnus-article-sort-by-author)
624                          (function-item gnus-article-sort-by-subject)
625                          (function-item gnus-article-sort-by-date)
626                          (function-item gnus-article-sort-by-score)
627                          (function :tag "other"))))
628
629 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
630   "*List of functions used for sorting threads in the summary buffer.
631 By default, threads are sorted by article number.
632
633 Each function takes two threads and returns non-nil if the first
634 thread should be sorted before the other.  If you use more than one
635 function, the primary sort function should be the last.  You should
636 probably always include `gnus-thread-sort-by-number' in the list of
637 sorting functions -- preferably first.  Also note that sorting by date
638 is often much slower than sorting by number, and the sorting order is
639 very similar.  (Sorting by date means sorting by the time the message
640 was sent, sorting by number means sorting by arrival time.)
641
642 Ready-made functions include `gnus-thread-sort-by-number',
643 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
644 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
645 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
646
647 When threading is turned off, the variable
648 `gnus-article-sort-functions' controls how articles are sorted."
649   :group 'gnus-summary-sort
650   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
651                          (function-item gnus-thread-sort-by-author)
652                          (function-item gnus-thread-sort-by-subject)
653                          (function-item gnus-thread-sort-by-date)
654                          (function-item gnus-thread-sort-by-score)
655                          (function-item gnus-thread-sort-by-total-score)
656                          (function :tag "other"))))
657
658 (defcustom gnus-thread-score-function '+
659   "*Function used for calculating the total score of a thread.
660
661 The function is called with the scores of the article and each
662 subthread and should then return the score of the thread.
663
664 Some functions you can use are `+', `max', or `min'."
665   :group 'gnus-summary-sort
666   :type 'function)
667
668 (defcustom gnus-summary-expunge-below nil
669   "All articles that have a score less than this variable will be expunged.
670 This variable is local to the summary buffers."
671   :group 'gnus-score-default
672   :type '(choice (const :tag "off" nil)
673                  integer))
674
675 (defcustom gnus-thread-expunge-below nil
676   "All threads that have a total score less than this variable will be expunged.
677 See `gnus-thread-score-function' for en explanation of what a
678 \"thread score\" is.
679
680 This variable is local to the summary buffers."
681   :group 'gnus-threading
682   :group 'gnus-score-default
683   :type '(choice (const :tag "off" nil)
684                  integer))
685
686 (defcustom gnus-summary-mode-hook nil
687   "*A hook for Gnus summary mode.
688 This hook is run before any variables are set in the summary buffer."
689   :options '(turn-on-gnus-mailing-list-mode)
690   :group 'gnus-summary-various
691   :type 'hook)
692
693 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
694 (when (featurep 'xemacs)
695   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
696   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
697   (add-hook 'gnus-summary-mode-hook
698             'gnus-xmas-switch-horizontal-scrollbar-off))
699
700 (defcustom gnus-summary-menu-hook nil
701   "*Hook run after the creation of the summary mode menu."
702   :group 'gnus-summary-visual
703   :type 'hook)
704
705 (defcustom gnus-summary-exit-hook nil
706   "*A hook called on exit from the summary buffer.
707 It will be called with point in the group buffer."
708   :group 'gnus-summary-exit
709   :type 'hook)
710
711 (defcustom gnus-summary-prepare-hook nil
712   "*A hook called after the summary buffer has been generated.
713 If you want to modify the summary buffer, you can use this hook."
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 (defcustom gnus-summary-prepared-hook nil
718   "*A hook called as the last thing after the summary buffer has been generated."
719   :group 'gnus-summary-various
720   :type 'hook)
721
722 (defcustom gnus-summary-generate-hook nil
723   "*A hook run just before generating the summary buffer.
724 This hook is commonly used to customize threading variables and the
725 like."
726   :group 'gnus-summary-various
727   :type 'hook)
728
729 (defcustom gnus-select-group-hook nil
730   "*A hook called when a newsgroup is selected.
731
732 If you'd like to simplify subjects like the
733 `gnus-summary-next-same-subject' command does, you can use the
734 following hook:
735
736  (setq gnus-select-group-hook
737       (list
738         (lambda ()
739           (mapcar (lambda (header)
740                      (mail-header-set-subject
741                       header
742                       (gnus-simplify-subject
743                        (mail-header-subject header) 're-only)))
744                   gnus-newsgroup-headers))))"
745   :group 'gnus-group-select
746   :type 'hook)
747
748 (defcustom gnus-select-article-hook nil
749   "*A hook called when an article is selected."
750   :group 'gnus-summary-choose
751   :type 'hook)
752
753 (defcustom gnus-visual-mark-article-hook
754   (list 'gnus-highlight-selected-summary)
755   "*Hook run after selecting an article in the summary buffer.
756 It is meant to be used for highlighting the article in some way.  It
757 is not run if `gnus-visual' is nil."
758   :group 'gnus-summary-visual
759   :type 'hook)
760
761 (defcustom gnus-parse-headers-hook nil
762   "*A hook called before parsing the headers."
763   :group 'gnus-various
764   :type 'hook)
765
766 (defcustom gnus-exit-group-hook nil
767   "*A hook called when exiting summary mode.
768 This hook is not called from the non-updating exit commands like `Q'."
769   :group 'gnus-various
770   :type 'hook)
771
772 (defcustom gnus-summary-update-hook
773   (list 'gnus-summary-highlight-line)
774   "*A hook called when a summary line is changed.
775 The hook will not be called if `gnus-visual' is nil.
776
777 The default function `gnus-summary-highlight-line' will
778 highlight the line according to the `gnus-summary-highlight'
779 variable."
780   :group 'gnus-summary-visual
781   :type 'hook)
782
783 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
784   "*A hook called when an article is selected for the first time.
785 The hook is intended to mark an article as read (or unread)
786 automatically when it is selected."
787   :group 'gnus-summary-choose
788   :type 'hook)
789
790 (defcustom gnus-group-no-more-groups-hook nil
791   "*A hook run when returning to group mode having no more (unread) groups."
792   :group 'gnus-group-select
793   :type 'hook)
794
795 (defcustom gnus-ps-print-hook nil
796   "*A hook run before ps-printing something from Gnus."
797   :group 'gnus-summary
798   :type 'hook)
799
800 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
801   "Face used for highlighting the current article in the summary buffer."
802   :group 'gnus-summary-visual
803   :type 'face)
804
805 (defcustom gnus-summary-highlight
806   '(((= mark gnus-canceled-mark)
807      . gnus-summary-cancelled-face)
808     ((and (> score default-high)
809           (or (= mark gnus-dormant-mark)
810               (= mark gnus-ticked-mark)))
811      . gnus-summary-high-ticked-face)
812     ((and (< score default-low)
813           (or (= mark gnus-dormant-mark)
814               (= mark gnus-ticked-mark)))
815      . gnus-summary-low-ticked-face)
816     ((or (= mark gnus-dormant-mark)
817          (= mark gnus-ticked-mark))
818      . gnus-summary-normal-ticked-face)
819     ((and (> score default-high) (= mark gnus-ancient-mark))
820      . gnus-summary-high-ancient-face)
821     ((and (< score default-low) (= mark gnus-ancient-mark))
822      . gnus-summary-low-ancient-face)
823     ((= mark gnus-ancient-mark)
824      . gnus-summary-normal-ancient-face)
825     ((and (> score default-high) (= mark gnus-unread-mark))
826      . gnus-summary-high-unread-face)
827     ((and (< score default-low) (= mark gnus-unread-mark))
828      . gnus-summary-low-unread-face)
829     ((= mark gnus-unread-mark)
830      . gnus-summary-normal-unread-face)
831     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
832                                              gnus-undownloaded-mark)))
833      . gnus-summary-high-unread-face)
834     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
835                                              gnus-undownloaded-mark)))
836      . gnus-summary-low-unread-face)
837     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
838           (memq article gnus-newsgroup-unreads))
839      . gnus-summary-normal-unread-face)
840     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
841      . gnus-summary-normal-read-face)
842     ((> score default-high)
843      . gnus-summary-high-read-face)
844     ((< score default-low)
845      . gnus-summary-low-read-face)
846     (t
847      . gnus-summary-normal-read-face))
848   "*Controls the highlighting of summary buffer lines.
849
850 A list of (FORM . FACE) pairs.  When deciding how a a particular
851 summary line should be displayed, each form is evaluated.  The content
852 of the face field after the first true form is used.  You can change
853 how those summary lines are displayed, by editing the face field.
854
855 You can use the following variables in the FORM field.
856
857 score:        The article's score
858 default:      The default article score.
859 default-high: The default score for high scored articles.
860 default-low:  The default score for low scored articles.
861 below:        The score below which articles are automatically marked as read.
862 mark:         The articles mark."
863   :group 'gnus-summary-visual
864   :type '(repeat (cons (sexp :tag "Form" nil)
865                        face)))
866
867 (defcustom gnus-alter-header-function nil
868   "Function called to allow alteration of article header structures.
869 The function is called with one parameter, the article header vector,
870 which it may alter in any way.")
871
872 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
873   "Variable that says which function should be used to decode a string with encoded words.")
874
875 (defcustom gnus-extra-headers nil
876   "*Extra headers to parse."
877   :version "21.1"
878   :group 'gnus-summary
879   :type '(repeat symbol))
880
881 (defcustom gnus-ignored-from-addresses
882   (and user-mail-address (regexp-quote user-mail-address))
883   "*Regexp of From headers that may be suppressed in favor of To headers."
884   :version "21.1"
885   :group 'gnus-summary
886   :type 'regexp)
887
888 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
889   "List of charsets that should be ignored.
890 When these charsets are used in the \"charset\" parameter, the
891 default charset will be used instead."
892   :version "21.1"
893   :type '(repeat symbol)
894   :group 'gnus-charset)
895
896 (gnus-define-group-parameter
897  ignored-charsets
898  :type list
899  :function-document
900  "Return the ignored charsets of GROUP."
901  :variable gnus-group-ignored-charsets-alist
902  :variable-default 
903  '(("alt\\.chinese\\.text" iso-8859-1))
904  :variable-document
905  "Alist of regexps (to match group names) and charsets that should be ignored.
906 When these charsets are used in the \"charset\" parameter, the
907 default charset will be used instead."
908  :variable-group gnus-charset
909  :variable-type '(repeat (cons (regexp :tag "Group")
910                                (repeat symbol)))
911  :parameter-type '(choice :tag "Ignored charsets" 
912                           :value nil
913                           (repeat (symbol)))
914  :parameter-document       "\
915 List of charsets that should be ignored.
916
917 When these charsets are used in the \"charset\" parameter, the
918 default charset will be used instead.")
919
920 (defcustom gnus-group-highlight-words-alist nil
921   "Alist of group regexps and highlight regexps.
922 This variable uses the same syntax as `gnus-emphasis-alist'."
923   :version "21.1"
924   :type '(repeat (cons (regexp :tag "Group")
925                        (repeat (list (regexp :tag "Highlight regexp")
926                                      (number :tag "Group for entire word" 0)
927                                      (number :tag "Group for displayed part" 0)
928                                      (symbol :tag "Face"
929                                              gnus-emphasis-highlight-words)))))
930   :group 'gnus-summary-visual)
931
932 (defcustom gnus-summary-show-article-charset-alist
933   nil
934   "Alist of number and charset.
935 The article will be shown with the charset corresponding to the
936 numbered argument.
937 For example: ((1 . cn-gb-2312) (2 . big5))."
938   :version "21.1"
939   :type '(repeat (cons (number :tag "Argument" 1)
940                        (symbol :tag "Charset")))
941   :group 'gnus-charset)
942
943 (defcustom gnus-preserve-marks t
944   "Whether marks are preserved when moving, copying and respooling messages."
945   :version "21.1"
946   :type 'boolean
947   :group 'gnus-summary-marks)
948
949 (defcustom gnus-alter-articles-to-read-function nil
950   "Function to be called to alter the list of articles to be selected."
951   :type '(choice (const nil) function)
952   :group 'gnus-summary)
953
954 (defcustom gnus-orphan-score nil
955   "*All orphans get this score added.  Set in the score file."
956   :group 'gnus-score-default
957   :type '(choice (const nil)
958                  integer))
959
960 (defcustom gnus-summary-save-parts-default-mime "image/.*"
961   "*A regexp to match MIME parts when saving multiple parts of a message
962 with gnus-summary-save-parts (X m). This regexp will be used by default
963 when prompting the user for which type of files to save."
964   :group 'gnus-summary
965   :type 'regexp)
966
967
968 (defcustom gnus-summary-save-parts-default-mime "image/.*"
969   "*A regexp to match MIME parts when saving multiple parts of a message
970 with gnus-summary-save-parts (X m). This regexp will be used by default
971 when prompting the user for which type of files to save."
972   :group 'gnus-summary
973   :type 'regexp)
974
975 (defcustom gnus-read-all-available-headers nil
976   "Whether Gnus should parse all headers made available to it.
977 This is mostly relevant for slow backends where the user may
978 wish to widen the summary buffer to include all headers
979 that were fetched.  Say, for nnultimate groups."
980   :group 'gnus-summary
981   :type '(choice boolean regexp))
982
983 ;;; Internal variables
984
985 (defvar gnus-article-mime-handles nil)
986 (defvar gnus-article-decoded-p nil)
987 (defvar gnus-article-charset nil)
988 (defvar gnus-article-ignored-charsets nil)
989 (defvar gnus-scores-exclude-files nil)
990 (defvar gnus-page-broken nil)
991 (defvar gnus-inhibit-mime-unbuttonizing nil)
992
993 (defvar gnus-original-article nil)
994 (defvar gnus-article-internal-prepare-hook nil)
995 (defvar gnus-newsgroup-process-stack nil)
996
997 (defvar gnus-thread-indent-array nil)
998 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
999 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1000   "Function called to sort the articles within a thread after it has been gathered together.")
1001
1002 (defvar gnus-summary-save-parts-type-history nil)
1003 (defvar gnus-summary-save-parts-last-directory nil)
1004
1005 (defvar gnus-summary-save-parts-type-history nil)
1006 (defvar gnus-summary-save-parts-last-directory nil)
1007
1008 ;; Avoid highlighting in kill files.
1009 (defvar gnus-summary-inhibit-highlight nil)
1010 (defvar gnus-newsgroup-selected-overlay nil)
1011 (defvar gnus-inhibit-limiting nil)
1012 (defvar gnus-newsgroup-adaptive-score-file nil)
1013 (defvar gnus-current-score-file nil)
1014 (defvar gnus-current-move-group nil)
1015 (defvar gnus-current-copy-group nil)
1016 (defvar gnus-current-crosspost-group nil)
1017
1018 (defvar gnus-newsgroup-dependencies nil)
1019 (defvar gnus-newsgroup-adaptive nil)
1020 (defvar gnus-summary-display-article-function nil)
1021 (defvar gnus-summary-highlight-line-function nil
1022   "Function called after highlighting a summary line.")
1023
1024 (defvar gnus-summary-line-format-alist
1025   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1026     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1027     (?s gnus-tmp-subject-or-nil ?s)
1028     (?n gnus-tmp-name ?s)
1029     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1030         ?s)
1031     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1032             gnus-tmp-from) ?s)
1033     (?F gnus-tmp-from ?s)
1034     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1035     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1036     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1037     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1038     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1039     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1040     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1041     (?L gnus-tmp-lines ?s)
1042     (?I gnus-tmp-indentation ?s)
1043     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1044     (?R gnus-tmp-replied ?c)
1045     (?\[ gnus-tmp-opening-bracket ?c)
1046     (?\] gnus-tmp-closing-bracket ?c)
1047     (?\> (make-string gnus-tmp-level ? ) ?s)
1048     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1049     (?i gnus-tmp-score ?d)
1050     (?z gnus-tmp-score-char ?c)
1051     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1052     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1053     (?U gnus-tmp-unread ?c)
1054     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1055     (?t (gnus-summary-number-of-articles-in-thread
1056          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1057         ?d)
1058     (?e (gnus-summary-number-of-articles-in-thread
1059          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1060         ?c)
1061     (?u gnus-tmp-user-defined ?s)
1062     (?P (gnus-pick-line-number) ?d))
1063   "An alist of format specifications that can appear in summary lines.
1064 These are paired with what variables they correspond with, along with
1065 the type of the variable (string, integer, character, etc).")
1066
1067 (defvar gnus-summary-dummy-line-format-alist
1068   `((?S gnus-tmp-subject ?s)
1069     (?N gnus-tmp-number ?d)
1070     (?u gnus-tmp-user-defined ?s)))
1071
1072 (defvar gnus-summary-mode-line-format-alist
1073   `((?G gnus-tmp-group-name ?s)
1074     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1075     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1076     (?A gnus-tmp-article-number ?d)
1077     (?Z gnus-tmp-unread-and-unselected ?s)
1078     (?V gnus-version ?s)
1079     (?U gnus-tmp-unread-and-unticked ?d)
1080     (?S gnus-tmp-subject ?s)
1081     (?e gnus-tmp-unselected ?d)
1082     (?u gnus-tmp-user-defined ?s)
1083     (?d (length gnus-newsgroup-dormant) ?d)
1084     (?t (length gnus-newsgroup-marked) ?d)
1085     (?r (length gnus-newsgroup-reads) ?d)
1086     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1087     (?E gnus-newsgroup-expunged-tally ?d)
1088     (?s (gnus-current-score-file-nondirectory) ?s)))
1089
1090 (defvar gnus-last-search-regexp nil
1091   "Default regexp for article search command.")
1092
1093 (defvar gnus-last-shell-command nil
1094   "Default shell command on article.")
1095
1096 (defvar gnus-newsgroup-begin nil)
1097 (defvar gnus-newsgroup-end nil)
1098 (defvar gnus-newsgroup-last-rmail nil)
1099 (defvar gnus-newsgroup-last-mail nil)
1100 (defvar gnus-newsgroup-last-folder nil)
1101 (defvar gnus-newsgroup-last-file nil)
1102 (defvar gnus-newsgroup-auto-expire nil)
1103 (defvar gnus-newsgroup-active nil)
1104
1105 (defvar gnus-newsgroup-data nil)
1106 (defvar gnus-newsgroup-data-reverse nil)
1107 (defvar gnus-newsgroup-limit nil)
1108 (defvar gnus-newsgroup-limits nil)
1109
1110 (defvar gnus-newsgroup-unreads nil
1111   "List of unread articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-unselected nil
1114   "List of unselected unread articles in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-reads nil
1117   "Alist of read articles and article marks in the current newsgroup.")
1118
1119 (defvar gnus-newsgroup-expunged-tally nil)
1120
1121 (defvar gnus-newsgroup-marked nil
1122   "List of ticked articles in the current newsgroup (a subset of unread art).")
1123
1124 (defvar gnus-newsgroup-killed nil
1125   "List of ranges of articles that have been through the scoring process.")
1126
1127 (defvar gnus-newsgroup-cached nil
1128   "List of articles that come from the article cache.")
1129
1130 (defvar gnus-newsgroup-saved nil
1131   "List of articles that have been saved.")
1132
1133 (defvar gnus-newsgroup-kill-headers nil)
1134
1135 (defvar gnus-newsgroup-replied nil
1136   "List of articles that have been replied to in the current newsgroup.")
1137
1138 (defvar gnus-newsgroup-forwarded nil
1139   "List of articles that have been forwarded in the current newsgroup.")
1140
1141 (defvar gnus-newsgroup-expirable nil
1142   "List of articles in the current newsgroup that can be expired.")
1143
1144 (defvar gnus-newsgroup-processable nil
1145   "List of articles in the current newsgroup that can be processed.")
1146
1147 (defvar gnus-newsgroup-downloadable nil
1148   "List of articles in the current newsgroup that can be processed.")
1149
1150 (defvar gnus-newsgroup-undownloaded nil
1151   "List of articles in the current newsgroup that haven't been downloaded..")
1152
1153 (defvar gnus-newsgroup-unsendable nil
1154   "List of articles in the current newsgroup that won't be sent.")
1155
1156 (defvar gnus-newsgroup-bookmarks nil
1157   "List of articles in the current newsgroup that have bookmarks.")
1158
1159 (defvar gnus-newsgroup-dormant nil
1160   "List of dormant articles in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-scored nil
1163   "List of scored articles in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-headers nil
1166   "List of article headers in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-threads nil)
1169
1170 (defvar gnus-newsgroup-prepared nil
1171   "Whether the current group has been prepared properly.")
1172
1173 (defvar gnus-newsgroup-ancient nil
1174   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1175
1176 (defvar gnus-newsgroup-sparse nil)
1177
1178 (defvar gnus-current-article nil)
1179 (defvar gnus-article-current nil)
1180 (defvar gnus-current-headers nil)
1181 (defvar gnus-have-all-headers nil)
1182 (defvar gnus-last-article nil)
1183 (defvar gnus-newsgroup-history nil)
1184 (defvar gnus-newsgroup-charset nil)
1185 (defvar gnus-newsgroup-ephemeral-charset nil)
1186 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1187
1188 (defvar gnus-article-before-search nil)
1189
1190 (defconst gnus-summary-local-variables
1191   '(gnus-newsgroup-name
1192     gnus-newsgroup-begin gnus-newsgroup-end
1193     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1194     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1195     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1196     gnus-newsgroup-unselected gnus-newsgroup-marked
1197     gnus-newsgroup-reads gnus-newsgroup-saved
1198     gnus-newsgroup-replied gnus-newsgroup-forwarded
1199     gnus-newsgroup-expirable
1200     gnus-newsgroup-processable gnus-newsgroup-killed
1201     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1202     gnus-newsgroup-unsendable
1203     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1204     gnus-newsgroup-headers gnus-newsgroup-threads
1205     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1206     gnus-current-article gnus-current-headers gnus-have-all-headers
1207     gnus-last-article gnus-article-internal-prepare-hook
1208     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1209     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1210     gnus-thread-expunge-below
1211     gnus-score-alist gnus-current-score-file
1212     (gnus-summary-expunge-below . global)
1213     (gnus-summary-mark-below . global)
1214     (gnus-orphan-score . global)
1215     gnus-newsgroup-active gnus-scores-exclude-files
1216     gnus-newsgroup-history gnus-newsgroup-ancient
1217     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1218     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1219     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1220     (gnus-newsgroup-expunged-tally . 0)
1221     gnus-cache-removable-articles gnus-newsgroup-cached
1222     gnus-newsgroup-data gnus-newsgroup-data-reverse
1223     gnus-newsgroup-limit gnus-newsgroup-limits
1224     gnus-newsgroup-charset)
1225   "Variables that are buffer-local to the summary buffers.")
1226
1227 (defvar gnus-newsgroup-variables nil
1228   "Variables that have separate values in the newsgroups.")
1229
1230 ;; Byte-compiler warning.
1231 (eval-when-compile (defvar gnus-article-mode-map))
1232
1233 ;; MIME stuff.
1234
1235 (defvar gnus-decode-encoded-word-methods
1236   '(mail-decode-encoded-word-string)
1237   "List of methods used to decode encoded words.
1238
1239 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1240 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1241 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1242 whose names match REGEXP.
1243
1244 For example:
1245 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1246  mail-decode-encoded-word-string
1247  (\"chinese\" . rfc1843-decode-string))")
1248
1249 (defvar gnus-decode-encoded-word-methods-cache nil)
1250
1251 (defun gnus-multi-decode-encoded-word-string (string)
1252   "Apply the functions from `gnus-encoded-word-methods' that match."
1253   (unless (and gnus-decode-encoded-word-methods-cache
1254                (eq gnus-newsgroup-name
1255                    (car gnus-decode-encoded-word-methods-cache)))
1256     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1257     (mapcar (lambda (x)
1258               (if (symbolp x)
1259                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1260                 (if (and gnus-newsgroup-name
1261                          (string-match (car x) gnus-newsgroup-name))
1262                     (nconc gnus-decode-encoded-word-methods-cache
1263                            (list (cdr x))))))
1264           gnus-decode-encoded-word-methods))
1265   (let ((xlist gnus-decode-encoded-word-methods-cache))
1266     (pop xlist)
1267     (while xlist
1268       (setq string (funcall (pop xlist) string))))
1269   string)
1270
1271 ;; Subject simplification.
1272
1273 (defun gnus-simplify-whitespace (str)
1274   "Remove excessive whitespace from STR."
1275   (let ((mystr str))
1276     ;; Multiple spaces.
1277     (while (string-match "[ \t][ \t]+" mystr)
1278       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1279                           " "
1280                           (substring mystr (match-end 0)))))
1281     ;; Leading spaces.
1282     (when (string-match "^[ \t]+" mystr)
1283       (setq mystr (substring mystr (match-end 0))))
1284     ;; Trailing spaces.
1285     (when (string-match "[ \t]+$" mystr)
1286       (setq mystr (substring mystr 0 (match-beginning 0))))
1287     mystr))
1288
1289 (defsubst gnus-simplify-subject-re (subject)
1290   "Remove \"Re:\" from subject lines."
1291   (if (string-match message-subject-re-regexp subject)
1292       (substring subject (match-end 0))
1293     subject))
1294
1295 (defun gnus-simplify-subject (subject &optional re-only)
1296   "Remove `Re:' and words in parentheses.
1297 If RE-ONLY is non-nil, strip leading `Re:'s only."
1298   (let ((case-fold-search t))           ;Ignore case.
1299     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1300     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1301       (setq subject (substring subject (match-end 0))))
1302     ;; Remove uninteresting prefixes.
1303     (when (and (not re-only)
1304                gnus-simplify-ignored-prefixes
1305                (string-match gnus-simplify-ignored-prefixes subject))
1306       (setq subject (substring subject (match-end 0))))
1307     ;; Remove words in parentheses from end.
1308     (unless re-only
1309       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1310         (setq subject (substring subject 0 (match-beginning 0)))))
1311     ;; Return subject string.
1312     subject))
1313
1314 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1315 ;; all whitespace.
1316 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1317   (goto-char (point-min))
1318   (while (re-search-forward regexp nil t)
1319     (replace-match (or newtext ""))))
1320
1321 (defun gnus-simplify-buffer-fuzzy ()
1322   "Simplify string in the buffer fuzzily.
1323 The string in the accessible portion of the current buffer is simplified.
1324 It is assumed to be a single-line subject.
1325 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1326 matter is removed.  Additional things can be deleted by setting
1327 `gnus-simplify-subject-fuzzy-regexp'."
1328   (let ((case-fold-search t)
1329         (modified-tick))
1330     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1331
1332     (while (not (eq modified-tick (buffer-modified-tick)))
1333       (setq modified-tick (buffer-modified-tick))
1334       (cond
1335        ((listp gnus-simplify-subject-fuzzy-regexp)
1336         (mapcar 'gnus-simplify-buffer-fuzzy-step
1337                 gnus-simplify-subject-fuzzy-regexp))
1338        (gnus-simplify-subject-fuzzy-regexp
1339         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1340       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1341       (gnus-simplify-buffer-fuzzy-step
1342        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1343       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1344
1345     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1346     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1347     (gnus-simplify-buffer-fuzzy-step " $")
1348     (gnus-simplify-buffer-fuzzy-step "^ +")))
1349
1350 (defun gnus-simplify-subject-fuzzy (subject)
1351   "Simplify a subject string fuzzily.
1352 See `gnus-simplify-buffer-fuzzy' for details."
1353   (save-excursion
1354     (gnus-set-work-buffer)
1355     (let ((case-fold-search t))
1356       ;; Remove uninteresting prefixes.
1357       (when (and gnus-simplify-ignored-prefixes
1358                  (string-match gnus-simplify-ignored-prefixes subject))
1359         (setq subject (substring subject (match-end 0))))
1360       (insert subject)
1361       (inline (gnus-simplify-buffer-fuzzy))
1362       (buffer-string))))
1363
1364 (defsubst gnus-simplify-subject-fully (subject)
1365   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1366   (cond
1367    (gnus-simplify-subject-functions
1368     (gnus-map-function gnus-simplify-subject-functions subject))
1369    ((null gnus-summary-gather-subject-limit)
1370     (gnus-simplify-subject-re subject))
1371    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1372     (gnus-simplify-subject-fuzzy subject))
1373    ((numberp gnus-summary-gather-subject-limit)
1374     (gnus-limit-string (gnus-simplify-subject-re subject)
1375                        gnus-summary-gather-subject-limit))
1376    (t
1377     subject)))
1378
1379 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1380   "Check whether two subjects are equal.
1381 If optional argument simple-first is t, first argument is already
1382 simplified."
1383   (cond
1384    ((null simple-first)
1385     (equal (gnus-simplify-subject-fully s1)
1386            (gnus-simplify-subject-fully s2)))
1387    (t
1388     (equal s1
1389            (gnus-simplify-subject-fully s2)))))
1390
1391 (defun gnus-summary-bubble-group ()
1392   "Increase the score of the current group.
1393 This is a handy function to add to `gnus-summary-exit-hook' to
1394 increase the score of each group you read."
1395   (gnus-group-add-score gnus-newsgroup-name))
1396
1397 \f
1398 ;;;
1399 ;;; Gnus summary mode
1400 ;;;
1401
1402 (put 'gnus-summary-mode 'mode-class 'special)
1403
1404 (defvar gnus-article-commands-menu)
1405
1406 (when t
1407   ;; Non-orthogonal keys
1408
1409   (gnus-define-keys gnus-summary-mode-map
1410     " " gnus-summary-next-page
1411     "\177" gnus-summary-prev-page
1412     [delete] gnus-summary-prev-page
1413     [backspace] gnus-summary-prev-page
1414     "\r" gnus-summary-scroll-up
1415     "\M-\r" gnus-summary-scroll-down
1416     "n" gnus-summary-next-unread-article
1417     "p" gnus-summary-prev-unread-article
1418     "N" gnus-summary-next-article
1419     "P" gnus-summary-prev-article
1420     "\M-\C-n" gnus-summary-next-same-subject
1421     "\M-\C-p" gnus-summary-prev-same-subject
1422     "\M-n" gnus-summary-next-unread-subject
1423     "\M-p" gnus-summary-prev-unread-subject
1424     "." gnus-summary-first-unread-article
1425     "," gnus-summary-best-unread-article
1426     "\M-s" gnus-summary-search-article-forward
1427     "\M-r" gnus-summary-search-article-backward
1428     "<" gnus-summary-beginning-of-article
1429     ">" gnus-summary-end-of-article
1430     "j" gnus-summary-goto-article
1431     "^" gnus-summary-refer-parent-article
1432     "\M-^" gnus-summary-refer-article
1433     "u" gnus-summary-tick-article-forward
1434     "!" gnus-summary-tick-article-forward
1435     "U" gnus-summary-tick-article-backward
1436     "d" gnus-summary-mark-as-read-forward
1437     "D" gnus-summary-mark-as-read-backward
1438     "E" gnus-summary-mark-as-expirable
1439     "\M-u" gnus-summary-clear-mark-forward
1440     "\M-U" gnus-summary-clear-mark-backward
1441     "k" gnus-summary-kill-same-subject-and-select
1442     "\C-k" gnus-summary-kill-same-subject
1443     "\M-\C-k" gnus-summary-kill-thread
1444     "\M-\C-l" gnus-summary-lower-thread
1445     "e" gnus-summary-edit-article
1446     "#" gnus-summary-mark-as-processable
1447     "\M-#" gnus-summary-unmark-as-processable
1448     "\M-\C-t" gnus-summary-toggle-threads
1449     "\M-\C-s" gnus-summary-show-thread
1450     "\M-\C-h" gnus-summary-hide-thread
1451     "\M-\C-f" gnus-summary-next-thread
1452     "\M-\C-b" gnus-summary-prev-thread
1453     [(meta down)] gnus-summary-next-thread
1454     [(meta up)] gnus-summary-prev-thread
1455     "\M-\C-u" gnus-summary-up-thread
1456     "\M-\C-d" gnus-summary-down-thread
1457     "&" gnus-summary-execute-command
1458     "c" gnus-summary-catchup-and-exit
1459     "\C-w" gnus-summary-mark-region-as-read
1460     "\C-t" gnus-summary-toggle-truncation
1461     "?" gnus-summary-mark-as-dormant
1462     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1463     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1464     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1465     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1466     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1467     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1468     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1469     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1470     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1471     "=" gnus-summary-expand-window
1472     "\C-x\C-s" gnus-summary-reselect-current-group
1473     "\M-g" gnus-summary-rescan-group
1474     "w" gnus-summary-stop-page-breaking
1475     "\C-c\C-r" gnus-summary-caesar-message
1476     "f" gnus-summary-followup
1477     "F" gnus-summary-followup-with-original
1478     "C" gnus-summary-cancel-article
1479     "r" gnus-summary-reply
1480     "R" gnus-summary-reply-with-original
1481     "\C-c\C-f" gnus-summary-mail-forward
1482     "o" gnus-summary-save-article
1483     "\C-o" gnus-summary-save-article-mail
1484     "|" gnus-summary-pipe-output
1485     "\M-k" gnus-summary-edit-local-kill
1486     "\M-K" gnus-summary-edit-global-kill
1487     ;; "V" gnus-version
1488     "\C-c\C-d" gnus-summary-describe-group
1489     "q" gnus-summary-exit
1490     "Q" gnus-summary-exit-no-update
1491     "\C-c\C-i" gnus-info-find-node
1492     gnus-mouse-2 gnus-mouse-pick-article
1493     "m" gnus-summary-mail-other-window
1494     "a" gnus-summary-post-news
1495     "x" gnus-summary-limit-to-unread
1496     "s" gnus-summary-isearch-article
1497     "t" gnus-summary-toggle-header
1498     "g" gnus-summary-show-article
1499     "l" gnus-summary-goto-last-article
1500     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1501     "\C-d" gnus-summary-enter-digest-group
1502     "\M-\C-d" gnus-summary-read-document
1503     "\M-\C-e" gnus-summary-edit-parameters
1504     "\M-\C-a" gnus-summary-customize-parameters
1505     "\C-c\C-b" gnus-bug
1506     "*" gnus-cache-enter-article
1507     "\M-*" gnus-cache-remove-article
1508     "\M-&" gnus-summary-universal-argument
1509     "\C-l" gnus-recenter
1510     "I" gnus-summary-increase-score
1511     "L" gnus-summary-lower-score
1512     "\M-i" gnus-symbolic-argument
1513     "h" gnus-summary-select-article-buffer
1514
1515     "b" gnus-article-view-part
1516     "\M-t" gnus-summary-toggle-display-buttonized
1517
1518     "V" gnus-summary-score-map
1519     "X" gnus-uu-extract-map
1520     "S" gnus-summary-send-map)
1521
1522   ;; Sort of orthogonal keymap
1523   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1524     "t" gnus-summary-tick-article-forward
1525     "!" gnus-summary-tick-article-forward
1526     "d" gnus-summary-mark-as-read-forward
1527     "r" gnus-summary-mark-as-read-forward
1528     "c" gnus-summary-clear-mark-forward
1529     " " gnus-summary-clear-mark-forward
1530     "e" gnus-summary-mark-as-expirable
1531     "x" gnus-summary-mark-as-expirable
1532     "?" gnus-summary-mark-as-dormant
1533     "b" gnus-summary-set-bookmark
1534     "B" gnus-summary-remove-bookmark
1535     "#" gnus-summary-mark-as-processable
1536     "\M-#" gnus-summary-unmark-as-processable
1537     "S" gnus-summary-limit-include-expunged
1538     "C" gnus-summary-catchup
1539     "H" gnus-summary-catchup-to-here
1540     "h" gnus-summary-catchup-from-here
1541     "\C-c" gnus-summary-catchup-all
1542     "k" gnus-summary-kill-same-subject-and-select
1543     "K" gnus-summary-kill-same-subject
1544     "P" gnus-uu-mark-map)
1545
1546   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1547     "c" gnus-summary-clear-above
1548     "u" gnus-summary-tick-above
1549     "m" gnus-summary-mark-above
1550     "k" gnus-summary-kill-below)
1551
1552   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1553     "/" gnus-summary-limit-to-subject
1554     "n" gnus-summary-limit-to-articles
1555     "w" gnus-summary-pop-limit
1556     "s" gnus-summary-limit-to-subject
1557     "a" gnus-summary-limit-to-author
1558     "u" gnus-summary-limit-to-unread
1559     "m" gnus-summary-limit-to-marks
1560     "M" gnus-summary-limit-exclude-marks
1561     "v" gnus-summary-limit-to-score
1562     "*" gnus-summary-limit-include-cached
1563     "D" gnus-summary-limit-include-dormant
1564     "T" gnus-summary-limit-include-thread
1565     "d" gnus-summary-limit-exclude-dormant
1566     "t" gnus-summary-limit-to-age
1567     "x" gnus-summary-limit-to-extra
1568     "E" gnus-summary-limit-include-expunged
1569     "c" gnus-summary-limit-exclude-childless-dormant
1570     "C" gnus-summary-limit-mark-excluded-as-read
1571     "o" gnus-summary-insert-old-articles
1572     "N" gnus-summary-insert-new-articles)
1573
1574   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1575     "n" gnus-summary-next-unread-article
1576     "p" gnus-summary-prev-unread-article
1577     "N" gnus-summary-next-article
1578     "P" gnus-summary-prev-article
1579     "\C-n" gnus-summary-next-same-subject
1580     "\C-p" gnus-summary-prev-same-subject
1581     "\M-n" gnus-summary-next-unread-subject
1582     "\M-p" gnus-summary-prev-unread-subject
1583     "f" gnus-summary-first-unread-article
1584     "b" gnus-summary-best-unread-article
1585     "j" gnus-summary-goto-article
1586     "g" gnus-summary-goto-subject
1587     "l" gnus-summary-goto-last-article
1588     "o" gnus-summary-pop-article)
1589
1590   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1591     "k" gnus-summary-kill-thread
1592     "l" gnus-summary-lower-thread
1593     "i" gnus-summary-raise-thread
1594     "T" gnus-summary-toggle-threads
1595     "t" gnus-summary-rethread-current
1596     "^" gnus-summary-reparent-thread
1597     "s" gnus-summary-show-thread
1598     "S" gnus-summary-show-all-threads
1599     "h" gnus-summary-hide-thread
1600     "H" gnus-summary-hide-all-threads
1601     "n" gnus-summary-next-thread
1602     "p" gnus-summary-prev-thread
1603     "u" gnus-summary-up-thread
1604     "o" gnus-summary-top-thread
1605     "d" gnus-summary-down-thread
1606     "#" gnus-uu-mark-thread
1607     "\M-#" gnus-uu-unmark-thread)
1608
1609   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1610     "g" gnus-summary-prepare
1611     "c" gnus-summary-insert-cached-articles)
1612
1613   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1614     "c" gnus-summary-catchup-and-exit
1615     "C" gnus-summary-catchup-all-and-exit
1616     "E" gnus-summary-exit-no-update
1617     "Q" gnus-summary-exit
1618     "Z" gnus-summary-exit
1619     "n" gnus-summary-catchup-and-goto-next-group
1620     "R" gnus-summary-reselect-current-group
1621     "G" gnus-summary-rescan-group
1622     "N" gnus-summary-next-group
1623     "s" gnus-summary-save-newsrc
1624     "P" gnus-summary-prev-group)
1625
1626   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1627     " " gnus-summary-next-page
1628     "n" gnus-summary-next-page
1629     "\177" gnus-summary-prev-page
1630     [delete] gnus-summary-prev-page
1631     "p" gnus-summary-prev-page
1632     "\r" gnus-summary-scroll-up
1633     "\M-\r" gnus-summary-scroll-down
1634     "<" gnus-summary-beginning-of-article
1635     ">" gnus-summary-end-of-article
1636     "b" gnus-summary-beginning-of-article
1637     "e" gnus-summary-end-of-article
1638     "^" gnus-summary-refer-parent-article
1639     "r" gnus-summary-refer-parent-article
1640     "D" gnus-summary-enter-digest-group
1641     "R" gnus-summary-refer-references
1642     "T" gnus-summary-refer-thread
1643     "g" gnus-summary-show-article
1644     "s" gnus-summary-isearch-article
1645     "P" gnus-summary-print-article
1646     "M" gnus-mailing-list-insinuate
1647     "t" gnus-article-babel)
1648
1649   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1650     "b" gnus-article-add-buttons
1651     "B" gnus-article-add-buttons-to-head
1652     "o" gnus-article-treat-overstrike
1653     "e" gnus-article-emphasize
1654     "w" gnus-article-fill-cited-article
1655     "Q" gnus-article-fill-long-lines
1656     "C" gnus-article-capitalize-sentences
1657     "c" gnus-article-remove-cr
1658     "q" gnus-article-de-quoted-unreadable
1659     "6" gnus-article-de-base64-unreadable
1660     "Z" gnus-article-decode-HZ
1661     "h" gnus-article-wash-html
1662     "s" gnus-summary-force-verify-and-decrypt
1663     "f" gnus-article-display-x-face
1664     "l" gnus-summary-stop-page-breaking
1665     "r" gnus-summary-caesar-message
1666     "t" gnus-summary-toggle-header
1667     "v" gnus-summary-verbose-headers
1668     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1669     "p" gnus-article-verify-x-pgp-sig
1670     "d" gnus-article-treat-dumbquotes)
1671
1672   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1673     "a" gnus-article-hide
1674     "h" gnus-article-hide-headers
1675     "b" gnus-article-hide-boring-headers
1676     "s" gnus-article-hide-signature
1677     "c" gnus-article-hide-citation
1678     "C" gnus-article-hide-citation-in-followups
1679     "l" gnus-article-hide-list-identifiers
1680     "p" gnus-article-hide-pgp
1681     "B" gnus-article-strip-banner
1682     "P" gnus-article-hide-pem
1683     "\C-c" gnus-article-hide-citation-maybe)
1684
1685   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1686     "a" gnus-article-highlight
1687     "h" gnus-article-highlight-headers
1688     "c" gnus-article-highlight-citation
1689     "s" gnus-article-highlight-signature)
1690
1691   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1692     "w" gnus-article-decode-mime-words
1693     "c" gnus-article-decode-charset
1694     "v" gnus-mime-view-all-parts
1695     "b" gnus-article-view-part)
1696
1697   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1698     "z" gnus-article-date-ut
1699     "u" gnus-article-date-ut
1700     "l" gnus-article-date-local
1701     "p" gnus-article-date-english
1702     "e" gnus-article-date-lapsed
1703     "o" gnus-article-date-original
1704     "i" gnus-article-date-iso8601
1705     "s" gnus-article-date-user)
1706
1707   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1708     "t" gnus-article-remove-trailing-blank-lines
1709     "l" gnus-article-strip-leading-blank-lines
1710     "m" gnus-article-strip-multiple-blank-lines
1711     "a" gnus-article-strip-blank-lines
1712     "A" gnus-article-strip-all-blank-lines
1713     "s" gnus-article-strip-leading-space
1714     "e" gnus-article-strip-trailing-space
1715     "w" gnus-article-remove-leading-whitespace)
1716
1717   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1718     "v" gnus-version
1719     "f" gnus-summary-fetch-faq
1720     "d" gnus-summary-describe-group
1721     "h" gnus-summary-describe-briefly
1722     "i" gnus-info-find-node)
1723
1724   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1725     "e" gnus-summary-expire-articles
1726     "\M-\C-e" gnus-summary-expire-articles-now
1727     "\177" gnus-summary-delete-article
1728     [delete] gnus-summary-delete-article
1729     [backspace] gnus-summary-delete-article
1730     "m" gnus-summary-move-article
1731     "r" gnus-summary-respool-article
1732     "w" gnus-summary-edit-article
1733     "c" gnus-summary-copy-article
1734     "B" gnus-summary-crosspost-article
1735     "q" gnus-summary-respool-query
1736     "t" gnus-summary-respool-trace
1737     "i" gnus-summary-import-article
1738     "I" gnus-summary-create-article
1739     "p" gnus-summary-article-posted-p)
1740
1741   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1742     "o" gnus-summary-save-article
1743     "m" gnus-summary-save-article-mail
1744     "F" gnus-summary-write-article-file
1745     "r" gnus-summary-save-article-rmail
1746     "f" gnus-summary-save-article-file
1747     "b" gnus-summary-save-article-body-file
1748     "h" gnus-summary-save-article-folder
1749     "v" gnus-summary-save-article-vm
1750     "p" gnus-summary-pipe-output
1751     "s" gnus-soup-add-article)
1752
1753   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1754     "b" gnus-summary-display-buttonized
1755     "m" gnus-summary-repair-multipart
1756     "v" gnus-article-view-part
1757     "o" gnus-article-save-part
1758     "c" gnus-article-copy-part
1759     "C" gnus-article-view-part-as-charset
1760     "e" gnus-article-externalize-part
1761     "E" gnus-article-encrypt-body
1762     "i" gnus-article-inline-part
1763     "|" gnus-article-pipe-part))
1764
1765 (defvar gnus-article-post-menu nil)
1766
1767 (defun gnus-summary-make-menu-bar ()
1768   (gnus-turn-off-edit-menu 'summary)
1769
1770   (unless (boundp 'gnus-summary-misc-menu)
1771
1772     (easy-menu-define
1773      gnus-summary-kill-menu gnus-summary-mode-map ""
1774      (cons
1775       "Score"
1776       (nconc
1777        (list
1778         ["Customize" gnus-score-customize t])
1779        (gnus-make-score-map 'increase)
1780        (gnus-make-score-map 'lower)
1781        '(("Mark"
1782           ["Kill below" gnus-summary-kill-below t]
1783           ["Mark above" gnus-summary-mark-above t]
1784           ["Tick above" gnus-summary-tick-above t]
1785           ["Clear above" gnus-summary-clear-above t])
1786          ["Current score" gnus-summary-current-score t]
1787          ["Set score" gnus-summary-set-score t]
1788          ["Switch current score file..." gnus-score-change-score-file t]
1789          ["Set mark below..." gnus-score-set-mark-below t]
1790          ["Set expunge below..." gnus-score-set-expunge-below t]
1791          ["Edit current score file" gnus-score-edit-current-scores t]
1792          ["Edit score file" gnus-score-edit-file t]
1793          ["Trace score" gnus-score-find-trace t]
1794          ["Find words" gnus-score-find-favourite-words t]
1795          ["Rescore buffer" gnus-summary-rescore t]
1796          ["Increase score..." gnus-summary-increase-score t]
1797          ["Lower score..." gnus-summary-lower-score t]))))
1798
1799     ;; Define both the Article menu in the summary buffer and the equivalent
1800     ;; Commands menu in the article buffer here for consistency.
1801     (let ((innards
1802            `(("Hide"
1803               ["All" gnus-article-hide t]
1804               ["Headers" gnus-article-hide-headers t]
1805               ["Signature" gnus-article-hide-signature t]
1806               ["Citation" gnus-article-hide-citation t]
1807               ["List identifiers" gnus-article-hide-list-identifiers t]
1808               ["PGP" gnus-article-hide-pgp t]
1809               ["Banner" gnus-article-strip-banner t]
1810               ["Boring headers" gnus-article-hide-boring-headers t])
1811              ("Highlight"
1812               ["All" gnus-article-highlight t]
1813               ["Headers" gnus-article-highlight-headers t]
1814               ["Signature" gnus-article-highlight-signature t]
1815               ["Citation" gnus-article-highlight-citation t])
1816              ("MIME"
1817               ["Words" gnus-article-decode-mime-words t]
1818               ["Charset" gnus-article-decode-charset t]
1819               ["QP" gnus-article-de-quoted-unreadable t]
1820               ["Base64" gnus-article-de-base64-unreadable t]
1821               ["View all" gnus-mime-view-all-parts t]
1822               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1823               ["Encrypt body" gnus-article-encrypt-body t])
1824              ("Date"
1825               ["Local" gnus-article-date-local t]
1826               ["ISO8601" gnus-article-date-iso8601 t]
1827               ["UT" gnus-article-date-ut t]
1828               ["Original" gnus-article-date-original t]
1829               ["Lapsed" gnus-article-date-lapsed t]
1830               ["User-defined" gnus-article-date-user t])
1831              ("Washing"
1832               ("Remove Blanks"
1833                ["Leading" gnus-article-strip-leading-blank-lines t]
1834                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1835                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1836                ["All of the above" gnus-article-strip-blank-lines t]
1837                ["All" gnus-article-strip-all-blank-lines t]
1838                ["Leading space" gnus-article-strip-leading-space t]
1839                ["Trailing space" gnus-article-strip-trailing-space t]
1840                ["Leading space in headers" 
1841                 gnus-article-remove-leading-whitespace t])
1842               ["Overstrike" gnus-article-treat-overstrike t]
1843               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1844               ["Emphasis" gnus-article-emphasize t]
1845               ["Word wrap" gnus-article-fill-cited-article t]
1846               ["Fill long lines" gnus-article-fill-long-lines t]
1847               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1848               ["CR" gnus-article-remove-cr t]
1849               ["Show X-Face" gnus-article-display-x-face t]
1850               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1851               ["Base64" gnus-article-de-base64-unreadable t]
1852               ["Rot 13" gnus-summary-caesar-message
1853                ,@(if (featurep 'xemacs) '(t)
1854                    '(:help "\"Caesar rotate\" article by 13"))]
1855               ["Unix pipe" gnus-summary-pipe-message t]
1856               ["Add buttons" gnus-article-add-buttons t]
1857               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1858               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1859               ["Verbose header" gnus-summary-verbose-headers t]
1860               ["Toggle header" gnus-summary-toggle-header t]
1861               ["Html" gnus-article-wash-html t]
1862               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1863               ["HZ" gnus-article-decode-HZ t])
1864              ("Output"
1865               ["Save in default format" gnus-summary-save-article
1866                ,@(if (featurep 'xemacs) '(t)
1867                    '(:help "Save article using default method"))]
1868               ["Save in file" gnus-summary-save-article-file
1869                ,@(if (featurep 'xemacs) '(t)
1870                    '(:help "Save article in file"))]
1871               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1872               ["Save in MH folder" gnus-summary-save-article-folder t]
1873               ["Save in VM folder" gnus-summary-save-article-vm t]
1874               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1875               ["Save body in file" gnus-summary-save-article-body-file t]
1876               ["Pipe through a filter" gnus-summary-pipe-output t]
1877               ["Add to SOUP packet" gnus-soup-add-article t]
1878               ["Print" gnus-summary-print-article t])
1879              ("Backend"
1880               ["Respool article..." gnus-summary-respool-article t]
1881               ["Move article..." gnus-summary-move-article
1882                (gnus-check-backend-function
1883                 'request-move-article gnus-newsgroup-name)]
1884               ["Copy article..." gnus-summary-copy-article t]
1885               ["Crosspost article..." gnus-summary-crosspost-article
1886                (gnus-check-backend-function
1887                 'request-replace-article gnus-newsgroup-name)]
1888               ["Import file..." gnus-summary-import-article t]
1889               ["Create article..." gnus-summary-create-article t]
1890               ["Check if posted" gnus-summary-article-posted-p t]
1891               ["Edit article" gnus-summary-edit-article
1892                (not (gnus-group-read-only-p))]
1893               ["Delete article" gnus-summary-delete-article
1894                (gnus-check-backend-function
1895                 'request-expire-articles gnus-newsgroup-name)]
1896               ["Query respool" gnus-summary-respool-query t]
1897               ["Trace respool" gnus-summary-respool-trace t]
1898               ["Delete expirable articles" gnus-summary-expire-articles-now
1899                (gnus-check-backend-function
1900                 'request-expire-articles gnus-newsgroup-name)])
1901              ("Extract"
1902               ["Uudecode" gnus-uu-decode-uu
1903                ,@(if (featurep 'xemacs) '(t)
1904                    '(:help "Decode uuencoded article(s)"))]
1905               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1906               ["Unshar" gnus-uu-decode-unshar t]
1907               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1908               ["Save" gnus-uu-decode-save t]
1909               ["Binhex" gnus-uu-decode-binhex t]
1910               ["Postscript" gnus-uu-decode-postscript t])
1911              ("Cache"
1912               ["Enter article" gnus-cache-enter-article t]
1913               ["Remove article" gnus-cache-remove-article t])
1914              ["Translate" gnus-article-babel t]
1915              ["Select article buffer" gnus-summary-select-article-buffer t]
1916              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1917              ["Isearch article..." gnus-summary-isearch-article t]
1918              ["Beginning of the article" gnus-summary-beginning-of-article t]
1919              ["End of the article" gnus-summary-end-of-article t]
1920              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1921              ["Fetch referenced articles" gnus-summary-refer-references t]
1922              ["Fetch current thread" gnus-summary-refer-thread t]
1923              ["Fetch article with id..." gnus-summary-refer-article t]
1924              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1925              ["Redisplay" gnus-summary-show-article t]
1926              ["Raw article" gnus-summary-show-raw-article t])))
1927       (easy-menu-define
1928        gnus-summary-article-menu gnus-summary-mode-map ""
1929        (cons "Article" innards))
1930
1931       (if (not (keymapp gnus-summary-article-menu))
1932           (easy-menu-define
1933             gnus-article-commands-menu gnus-article-mode-map ""
1934             (cons "Commands" innards))
1935         ;; in Emacs, don't share menu.
1936         (setq gnus-article-commands-menu 
1937               (copy-keymap gnus-summary-article-menu))
1938         (define-key gnus-article-mode-map [menu-bar commands]
1939           (cons "Commands" gnus-article-commands-menu))))
1940
1941     (easy-menu-define
1942      gnus-summary-thread-menu gnus-summary-mode-map ""
1943      '("Threads"
1944        ["Toggle threading" gnus-summary-toggle-threads t]
1945        ["Hide threads" gnus-summary-hide-all-threads t]
1946        ["Show threads" gnus-summary-show-all-threads t]
1947        ["Hide thread" gnus-summary-hide-thread t]
1948        ["Show thread" gnus-summary-show-thread t]
1949        ["Go to next thread" gnus-summary-next-thread t]
1950        ["Go to previous thread" gnus-summary-prev-thread t]
1951        ["Go down thread" gnus-summary-down-thread t]
1952        ["Go up thread" gnus-summary-up-thread t]
1953        ["Top of thread" gnus-summary-top-thread t]
1954        ["Mark thread as read" gnus-summary-kill-thread t]
1955        ["Lower thread score" gnus-summary-lower-thread t]
1956        ["Raise thread score" gnus-summary-raise-thread t]
1957        ["Rethread current" gnus-summary-rethread-current t]))
1958
1959     (easy-menu-define
1960      gnus-summary-post-menu gnus-summary-mode-map ""
1961      `("Post"
1962        ["Post an article" gnus-summary-post-news
1963         ,@(if (featurep 'xemacs) '(t)
1964             '(:help "Post an article"))]
1965        ["Followup" gnus-summary-followup
1966         ,@(if (featurep 'xemacs) '(t)
1967             '(:help "Post followup to this article"))]
1968        ["Followup and yank" gnus-summary-followup-with-original
1969         ,@(if (featurep 'xemacs) '(t)
1970             '(:help "Post followup to this article, quoting its contents"))]
1971        ["Supersede article" gnus-summary-supersede-article t]
1972        ["Cancel article" gnus-summary-cancel-article
1973         ,@(if (featurep 'xemacs) '(t)
1974             '(:help "Cancel an article you posted"))]
1975        ["Reply" gnus-summary-reply t]
1976        ["Reply and yank" gnus-summary-reply-with-original t]
1977        ["Wide reply" gnus-summary-wide-reply t]
1978        ["Wide reply and yank" gnus-summary-wide-reply-with-original
1979         ,@(if (featurep 'xemacs) '(t)
1980             '(:help "Mail a reply, quoting this article"))]
1981        ["Mail forward" gnus-summary-mail-forward t]
1982        ["Post forward" gnus-summary-post-forward t]
1983        ["Digest and mail" gnus-uu-digest-mail-forward t]
1984        ["Digest and post" gnus-uu-digest-post-forward t]
1985        ["Resend message" gnus-summary-resend-message t]
1986        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1987        ["Send a mail" gnus-summary-mail-other-window t]
1988        ["Uuencode and post" gnus-uu-post-news
1989         ,@(if (featurep 'xemacs) '(t)
1990             '(:help "Post a uuencoded article"))]
1991        ["Followup via news" gnus-summary-followup-to-mail t]
1992        ["Followup via news and yank"
1993         gnus-summary-followup-to-mail-with-original t]
1994        ;;("Draft"
1995        ;;["Send" gnus-summary-send-draft t]
1996        ;;["Send bounced" gnus-resend-bounced-mail t])
1997        ))
1998
1999     (cond 
2000      ((not (keymapp gnus-summary-post-menu))
2001       (setq gnus-article-post-menu gnus-summary-post-menu))
2002      ((not gnus-article-post-menu)
2003       ;; Don't share post menu.
2004       (setq gnus-article-post-menu
2005             (copy-keymap gnus-summary-post-menu))))
2006     (define-key gnus-article-mode-map [menu-bar post]
2007       (cons "Post" gnus-article-post-menu))
2008
2009     (easy-menu-define
2010      gnus-summary-misc-menu gnus-summary-mode-map ""
2011      `("Misc"
2012        ("Mark Read"
2013         ["Mark as read" gnus-summary-mark-as-read-forward t]
2014         ["Mark same subject and select"
2015          gnus-summary-kill-same-subject-and-select t]
2016         ["Mark same subject" gnus-summary-kill-same-subject t]
2017         ["Catchup" gnus-summary-catchup
2018          ,@(if (featurep 'xemacs) '(t)
2019              '(:help "Mark unread articles in this group as read"))]
2020         ["Catchup all" gnus-summary-catchup-all t]
2021         ["Catchup to here" gnus-summary-catchup-to-here t]
2022         ["Catchup from here" gnus-summary-catchup-from-here t]
2023         ["Catchup region" gnus-summary-mark-region-as-read t]
2024         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2025        ("Mark Various"
2026         ["Tick" gnus-summary-tick-article-forward t]
2027         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2028         ["Remove marks" gnus-summary-clear-mark-forward t]
2029         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2030         ["Set bookmark" gnus-summary-set-bookmark t]
2031         ["Remove bookmark" gnus-summary-remove-bookmark t])
2032        ("Mark Limit"
2033         ["Marks..." gnus-summary-limit-to-marks t]
2034         ["Subject..." gnus-summary-limit-to-subject t]
2035         ["Author..." gnus-summary-limit-to-author t]
2036         ["Age..." gnus-summary-limit-to-age t]
2037         ["Extra..." gnus-summary-limit-to-extra t]
2038         ["Score" gnus-summary-limit-to-score t]
2039         ["Unread" gnus-summary-limit-to-unread t]
2040         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2041         ["Articles" gnus-summary-limit-to-articles t]
2042         ["Pop limit" gnus-summary-pop-limit t]
2043         ["Show dormant" gnus-summary-limit-include-dormant t]
2044         ["Hide childless dormant"
2045          gnus-summary-limit-exclude-childless-dormant t]
2046         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2047         ["Hide marked" gnus-summary-limit-exclude-marks t]
2048         ["Show expunged" gnus-summary-limit-include-expunged t])
2049        ("Process Mark"
2050         ["Set mark" gnus-summary-mark-as-processable t]
2051         ["Remove mark" gnus-summary-unmark-as-processable t]
2052         ["Remove all marks" gnus-summary-unmark-all-processable t]
2053         ["Mark above" gnus-uu-mark-over t]
2054         ["Mark series" gnus-uu-mark-series t]
2055         ["Mark region" gnus-uu-mark-region t]
2056         ["Unmark region" gnus-uu-unmark-region t]
2057         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2058         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2059         ["Mark all" gnus-uu-mark-all t]
2060         ["Mark buffer" gnus-uu-mark-buffer t]
2061         ["Mark sparse" gnus-uu-mark-sparse t]
2062         ["Mark thread" gnus-uu-mark-thread t]
2063         ["Unmark thread" gnus-uu-unmark-thread t]
2064         ("Process Mark Sets"
2065          ["Kill" gnus-summary-kill-process-mark t]
2066          ["Yank" gnus-summary-yank-process-mark
2067           gnus-newsgroup-process-stack]
2068          ["Save" gnus-summary-save-process-mark t]))
2069        ("Scroll article"
2070         ["Page forward" gnus-summary-next-page
2071          ,@(if (featurep 'xemacs) '(t)
2072              '(:help "Show next page of article"))]
2073         ["Page backward" gnus-summary-prev-page
2074          ,@(if (featurep 'xemacs) '(t)
2075              '(:help "Show previous page of article"))]
2076         ["Line forward" gnus-summary-scroll-up t])
2077        ("Move"
2078         ["Next unread article" gnus-summary-next-unread-article t]
2079         ["Previous unread article" gnus-summary-prev-unread-article t]
2080         ["Next article" gnus-summary-next-article t]
2081         ["Previous article" gnus-summary-prev-article t]
2082         ["Next unread subject" gnus-summary-next-unread-subject t]
2083         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2084         ["Next article same subject" gnus-summary-next-same-subject t]
2085         ["Previous article same subject" gnus-summary-prev-same-subject t]
2086         ["First unread article" gnus-summary-first-unread-article t]
2087         ["Best unread article" gnus-summary-best-unread-article t]
2088         ["Go to subject number..." gnus-summary-goto-subject t]
2089         ["Go to article number..." gnus-summary-goto-article t]
2090         ["Go to the last article" gnus-summary-goto-last-article t]
2091         ["Pop article off history" gnus-summary-pop-article t])
2092        ("Sort"
2093         ["Sort by number" gnus-summary-sort-by-number t]
2094         ["Sort by author" gnus-summary-sort-by-author t]
2095         ["Sort by subject" gnus-summary-sort-by-subject t]
2096         ["Sort by date" gnus-summary-sort-by-date t]
2097         ["Sort by score" gnus-summary-sort-by-score t]
2098         ["Sort by lines" gnus-summary-sort-by-lines t]
2099         ["Sort by characters" gnus-summary-sort-by-chars t]
2100         ["Original sort" gnus-summary-sort-by-original t])
2101        ("Help"
2102         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2103         ["Describe group" gnus-summary-describe-group t]
2104         ["Read manual" gnus-info-find-node t])
2105        ("Modes"
2106         ["Pick and read" gnus-pick-mode t]
2107         ["Binary" gnus-binary-mode t])
2108        ("Regeneration"
2109         ["Regenerate" gnus-summary-prepare t]
2110         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2111         ["Toggle threading" gnus-summary-toggle-threads t])
2112        ["See old articles" gnus-summary-insert-old-articles t]
2113        ["See new articles" gnus-summary-insert-new-articles t]
2114        ["Filter articles..." gnus-summary-execute-command t]
2115        ["Run command on subjects..." gnus-summary-universal-argument t]
2116        ["Search articles forward..." gnus-summary-search-article-forward t]
2117        ["Search articles backward..." gnus-summary-search-article-backward t]
2118        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2119        ["Expand window" gnus-summary-expand-window t]
2120        ["Expire expirable articles" gnus-summary-expire-articles
2121         (gnus-check-backend-function
2122          'request-expire-articles gnus-newsgroup-name)]
2123        ["Edit local kill file" gnus-summary-edit-local-kill t]
2124        ["Edit main kill file" gnus-summary-edit-global-kill t]
2125        ["Edit group parameters" gnus-summary-edit-parameters t]
2126        ["Customize group parameters" gnus-summary-customize-parameters t]
2127        ["Send a bug report" gnus-bug t]
2128        ("Exit"
2129         ["Catchup and exit" gnus-summary-catchup-and-exit
2130          ,@(if (featurep 'xemacs) '(t)
2131              '(:help "Mark unread articles in this group as read, then exit"))]
2132         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2133         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2134         ["Exit group" gnus-summary-exit
2135          ,@(if (featurep 'xemacs) '(t)
2136              '(:help "Exit current group, return to group selection mode"))]
2137         ["Exit group without updating" gnus-summary-exit-no-update t]
2138         ["Exit and goto next group" gnus-summary-next-group t]
2139         ["Exit and goto prev group" gnus-summary-prev-group t]
2140         ["Reselect group" gnus-summary-reselect-current-group t]
2141         ["Rescan group" gnus-summary-rescan-group t]
2142         ["Update dribble" gnus-summary-save-newsrc t])))
2143
2144     (gnus-run-hooks 'gnus-summary-menu-hook)))
2145
2146 (defvar gnus-summary-tool-bar-map nil)
2147
2148 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2149 (defun gnus-summary-make-tool-bar ()
2150   (if (and (fboundp 'tool-bar-add-item-from-menu)
2151            (default-value 'tool-bar-mode)
2152            (not gnus-summary-tool-bar-map))
2153       (setq gnus-summary-tool-bar-map
2154             (let ((tool-bar-map (make-sparse-keymap))
2155                   (load-path (mm-image-load-path)))
2156               (tool-bar-add-item-from-menu
2157                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2158               (tool-bar-add-item-from-menu
2159                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2160               (tool-bar-add-item-from-menu
2161                'gnus-summary-post-news "post" gnus-summary-mode-map)
2162               (tool-bar-add-item-from-menu
2163                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2164               (tool-bar-add-item-from-menu
2165                'gnus-summary-followup "followup" gnus-summary-mode-map)
2166               (tool-bar-add-item-from-menu
2167                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2168               (tool-bar-add-item-from-menu
2169                'gnus-summary-reply "reply" gnus-summary-mode-map)
2170               (tool-bar-add-item-from-menu
2171                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2172               (tool-bar-add-item-from-menu
2173                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2174               (tool-bar-add-item-from-menu
2175                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2176               (tool-bar-add-item-from-menu
2177                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2178               (tool-bar-add-item-from-menu
2179                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2180               (tool-bar-add-item-from-menu
2181                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2182               (tool-bar-add-item-from-menu
2183                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2184               (tool-bar-add-item-from-menu
2185                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2186               tool-bar-map)))
2187   (if gnus-summary-tool-bar-map
2188       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2189
2190 (defun gnus-score-set-default (var value)
2191   "A version of set that updates the GNU Emacs menu-bar."
2192   (set var value)
2193   ;; It is the message that forces the active status to be updated.
2194   (message ""))
2195
2196 (defun gnus-make-score-map (type)
2197   "Make a summary score map of type TYPE."
2198   (if t
2199       nil
2200     (let ((headers '(("author" "from" string)
2201                      ("subject" "subject" string)
2202                      ("article body" "body" string)
2203                      ("article head" "head" string)
2204                      ("xref" "xref" string)
2205                      ("extra header" "extra" string)
2206                      ("lines" "lines" number)
2207                      ("followups to author" "followup" string)))
2208           (types '((number ("less than" <)
2209                            ("greater than" >)
2210                            ("equal" =))
2211                    (string ("substring" s)
2212                            ("exact string" e)
2213                            ("fuzzy string" f)
2214                            ("regexp" r))))
2215           (perms '(("temporary" (current-time-string))
2216                    ("permanent" nil)
2217                    ("immediate" now)))
2218           header)
2219       (list
2220        (apply
2221         'nconc
2222         (list
2223          (if (eq type 'lower)
2224              "Lower score"
2225            "Increase score"))
2226         (let (outh)
2227           (while headers
2228             (setq header (car headers))
2229             (setq outh
2230                   (cons
2231                    (apply
2232                     'nconc
2233                     (list (car header))
2234                     (let ((ts (cdr (assoc (nth 2 header) types)))
2235                           outt)
2236                       (while ts
2237                         (setq outt
2238                               (cons
2239                                (apply
2240                                 'nconc
2241                                 (list (caar ts))
2242                                 (let ((ps perms)
2243                                       outp)
2244                                   (while ps
2245                                     (setq outp
2246                                           (cons
2247                                            (vector
2248                                             (caar ps)
2249                                             (list
2250                                              'gnus-summary-score-entry
2251                                              (nth 1 header)
2252                                              (if (or (string= (nth 1 header)
2253                                                               "head")
2254                                                      (string= (nth 1 header)
2255                                                               "body"))
2256                                                  ""
2257                                                (list 'gnus-summary-header
2258                                                      (nth 1 header)))
2259                                              (list 'quote (nth 1 (car ts)))
2260                                              (list 'gnus-score-delta-default
2261                                                    nil)
2262                                              (nth 1 (car ps))
2263                                              t)
2264                                             t)
2265                                            outp))
2266                                     (setq ps (cdr ps)))
2267                                   (list (nreverse outp))))
2268                                outt))
2269                         (setq ts (cdr ts)))
2270                       (list (nreverse outt))))
2271                    outh))
2272             (setq headers (cdr headers)))
2273           (list (nreverse outh))))))))
2274
2275 \f
2276
2277 (defun gnus-summary-mode (&optional group)
2278   "Major mode for reading articles.
2279
2280 All normal editing commands are switched off.
2281 \\<gnus-summary-mode-map>
2282 Each line in this buffer represents one article.  To read an
2283 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2284 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2285 respectively.
2286
2287 You can also post articles and send mail from this buffer.  To
2288 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2289 of an article, type `\\[gnus-summary-reply]'.
2290
2291 There are approx. one gazillion commands you can execute in this
2292 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2293
2294 The following commands are available:
2295
2296 \\{gnus-summary-mode-map}"
2297   (interactive)
2298   (kill-all-local-variables)
2299   (when (gnus-visual-p 'summary-menu 'menu)
2300     (gnus-summary-make-menu-bar)
2301     (gnus-summary-make-tool-bar))
2302   (gnus-summary-make-local-variables)
2303   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2304     (gnus-summary-make-local-variables))
2305   (gnus-make-thread-indent-array)
2306   (gnus-simplify-mode-line)
2307   (setq major-mode 'gnus-summary-mode)
2308   (setq mode-name "Summary")
2309   (make-local-variable 'minor-mode-alist)
2310   (use-local-map gnus-summary-mode-map)
2311   (buffer-disable-undo)
2312   (setq buffer-read-only t)             ;Disable modification
2313   (setq truncate-lines t)
2314   (setq selective-display t)
2315   (setq selective-display-ellipses t)   ;Display `...'
2316   (gnus-summary-set-display-table)
2317   (gnus-set-default-directory)
2318   (setq gnus-newsgroup-name group)
2319   (make-local-variable 'gnus-summary-line-format)
2320   (make-local-variable 'gnus-summary-line-format-spec)
2321   (make-local-variable 'gnus-summary-dummy-line-format)
2322   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2323   (make-local-variable 'gnus-summary-mark-positions)
2324   (make-local-hook 'pre-command-hook)
2325   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2326   (gnus-run-hooks 'gnus-summary-mode-hook)
2327   (turn-on-gnus-mailing-list-mode)
2328   (mm-enable-multibyte-mule4)
2329   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2330   (gnus-update-summary-mark-positions))
2331
2332 (defun gnus-summary-make-local-variables ()
2333   "Make all the local summary buffer variables."
2334   (let (global)
2335     (dolist (local gnus-summary-local-variables)
2336       (if (consp local)
2337           (progn
2338             (if (eq (cdr local) 'global)
2339                 ;; Copy the global value of the variable.
2340                 (setq global (symbol-value (car local)))
2341               ;; Use the value from the list.
2342               (setq global (eval (cdr local))))
2343             (set (make-local-variable (car local)) global))
2344         ;; Simple nil-valued local variable.
2345         (set (make-local-variable local) nil)))))
2346
2347 (defun gnus-summary-clear-local-variables ()
2348   (let ((locals gnus-summary-local-variables))
2349     (while locals
2350       (if (consp (car locals))
2351           (and (vectorp (caar locals))
2352                (set (caar locals) nil))
2353         (and (vectorp (car locals))
2354              (set (car locals) nil)))
2355       (setq locals (cdr locals)))))
2356
2357 ;; Summary data functions.
2358
2359 (defmacro gnus-data-number (data)
2360   `(car ,data))
2361
2362 (defmacro gnus-data-set-number (data number)
2363   `(setcar ,data ,number))
2364
2365 (defmacro gnus-data-mark (data)
2366   `(nth 1 ,data))
2367
2368 (defmacro gnus-data-set-mark (data mark)
2369   `(setcar (nthcdr 1 ,data) ,mark))
2370
2371 (defmacro gnus-data-pos (data)
2372   `(nth 2 ,data))
2373
2374 (defmacro gnus-data-set-pos (data pos)
2375   `(setcar (nthcdr 2 ,data) ,pos))
2376
2377 (defmacro gnus-data-header (data)
2378   `(nth 3 ,data))
2379
2380 (defmacro gnus-data-set-header (data header)
2381   `(setf (nth 3 ,data) ,header))
2382
2383 (defmacro gnus-data-level (data)
2384   `(nth 4 ,data))
2385
2386 (defmacro gnus-data-unread-p (data)
2387   `(= (nth 1 ,data) gnus-unread-mark))
2388
2389 (defmacro gnus-data-read-p (data)
2390   `(/= (nth 1 ,data) gnus-unread-mark))
2391
2392 (defmacro gnus-data-pseudo-p (data)
2393   `(consp (nth 3 ,data)))
2394
2395 (defmacro gnus-data-find (number)
2396   `(assq ,number gnus-newsgroup-data))
2397
2398 (defmacro gnus-data-find-list (number &optional data)
2399   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2400      (memq (assq ,number bdata)
2401            bdata)))
2402
2403 (defmacro gnus-data-make (number mark pos header level)
2404   `(list ,number ,mark ,pos ,header ,level))
2405
2406 (defun gnus-data-enter (after-article number mark pos header level offset)
2407   (let ((data (gnus-data-find-list after-article)))
2408     (unless data
2409       (error "No such article: %d" after-article))
2410     (setcdr data (cons (gnus-data-make number mark pos header level)
2411                        (cdr data)))
2412     (setq gnus-newsgroup-data-reverse nil)
2413     (gnus-data-update-list (cddr data) offset)))
2414
2415 (defun gnus-data-enter-list (after-article list &optional offset)
2416   (when list
2417     (let ((data (and after-article (gnus-data-find-list after-article)))
2418           (ilist list))
2419       (if (not (or data
2420                    after-article))
2421           (let ((odata gnus-newsgroup-data))
2422             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2423             (when offset
2424               (gnus-data-update-list odata offset)))
2425         ;; Find the last element in the list to be spliced into the main
2426         ;; list.
2427         (while (cdr list)
2428           (setq list (cdr list)))
2429         (if (not data)
2430             (progn
2431               (setcdr list gnus-newsgroup-data)
2432               (setq gnus-newsgroup-data ilist)
2433               (when offset
2434                 (gnus-data-update-list (cdr list) offset)))
2435           (setcdr list (cdr data))
2436           (setcdr data ilist)
2437           (when offset
2438             (gnus-data-update-list (cdr list) offset))))
2439       (setq gnus-newsgroup-data-reverse nil))))
2440
2441 (defun gnus-data-remove (article &optional offset)
2442   (let ((data gnus-newsgroup-data))
2443     (if (= (gnus-data-number (car data)) article)
2444         (progn
2445           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2446                 gnus-newsgroup-data-reverse nil)
2447           (when offset
2448             (gnus-data-update-list gnus-newsgroup-data offset)))
2449       (while (cdr data)
2450         (when (= (gnus-data-number (cadr data)) article)
2451           (setcdr data (cddr data))
2452           (when offset
2453             (gnus-data-update-list (cdr data) offset))
2454           (setq data nil
2455                 gnus-newsgroup-data-reverse nil))
2456         (setq data (cdr data))))))
2457
2458 (defmacro gnus-data-list (backward)
2459   `(if ,backward
2460        (or gnus-newsgroup-data-reverse
2461            (setq gnus-newsgroup-data-reverse
2462                  (reverse gnus-newsgroup-data)))
2463      gnus-newsgroup-data))
2464
2465 (defun gnus-data-update-list (data offset)
2466   "Add OFFSET to the POS of all data entries in DATA."
2467   (setq gnus-newsgroup-data-reverse nil)
2468   (while data
2469     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2470     (setq data (cdr data))))
2471
2472 (defun gnus-summary-article-pseudo-p (article)
2473   "Say whether this article is a pseudo article or not."
2474   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2475
2476 (defmacro gnus-summary-article-sparse-p (article)
2477   "Say whether this article is a sparse article or not."
2478   `(memq ,article gnus-newsgroup-sparse))
2479
2480 (defmacro gnus-summary-article-ancient-p (article)
2481   "Say whether this article is a sparse article or not."
2482   `(memq ,article gnus-newsgroup-ancient))
2483
2484 (defun gnus-article-parent-p (number)
2485   "Say whether this article is a parent or not."
2486   (let ((data (gnus-data-find-list number)))
2487     (and (cdr data)                     ; There has to be an article after...
2488          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2489             (gnus-data-level (nth 1 data))))))
2490
2491 (defun gnus-article-children (number)
2492   "Return a list of all children to NUMBER."
2493   (let* ((data (gnus-data-find-list number))
2494          (level (gnus-data-level (car data)))
2495          children)
2496     (setq data (cdr data))
2497     (while (and data
2498                 (= (gnus-data-level (car data)) (1+ level)))
2499       (push (gnus-data-number (car data)) children)
2500       (setq data (cdr data)))
2501     children))
2502
2503 (defmacro gnus-summary-skip-intangible ()
2504   "If the current article is intangible, then jump to a different article."
2505   '(let ((to (get-text-property (point) 'gnus-intangible)))
2506      (and to (gnus-summary-goto-subject to))))
2507
2508 (defmacro gnus-summary-article-intangible-p ()
2509   "Say whether this article is intangible or not."
2510   '(get-text-property (point) 'gnus-intangible))
2511
2512 (defun gnus-article-read-p (article)
2513   "Say whether ARTICLE is read or not."
2514   (not (or (memq article gnus-newsgroup-marked)
2515            (memq article gnus-newsgroup-unreads)
2516            (memq article gnus-newsgroup-unselected)
2517            (memq article gnus-newsgroup-dormant))))
2518
2519 ;; Some summary mode macros.
2520
2521 (defmacro gnus-summary-article-number ()
2522   "The article number of the article on the current line.
2523 If there isn's an article number here, then we return the current
2524 article number."
2525   '(progn
2526      (gnus-summary-skip-intangible)
2527      (or (get-text-property (point) 'gnus-number)
2528          (gnus-summary-last-subject))))
2529
2530 (defmacro gnus-summary-article-header (&optional number)
2531   "Return the header of article NUMBER."
2532   `(gnus-data-header (gnus-data-find
2533                       ,(or number '(gnus-summary-article-number)))))
2534
2535 (defmacro gnus-summary-thread-level (&optional number)
2536   "Return the level of thread that starts with article NUMBER."
2537   `(if (and (eq gnus-summary-make-false-root 'dummy)
2538             (get-text-property (point) 'gnus-intangible))
2539        0
2540      (gnus-data-level (gnus-data-find
2541                        ,(or number '(gnus-summary-article-number))))))
2542
2543 (defmacro gnus-summary-article-mark (&optional number)
2544   "Return the mark of article NUMBER."
2545   `(gnus-data-mark (gnus-data-find
2546                     ,(or number '(gnus-summary-article-number)))))
2547
2548 (defmacro gnus-summary-article-pos (&optional number)
2549   "Return the position of the line of article NUMBER."
2550   `(gnus-data-pos (gnus-data-find
2551                    ,(or number '(gnus-summary-article-number)))))
2552
2553 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2554 (defmacro gnus-summary-article-subject (&optional number)
2555   "Return current subject string or nil if nothing."
2556   `(let ((headers
2557           ,(if number
2558                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2559              '(gnus-data-header (assq (gnus-summary-article-number)
2560                                       gnus-newsgroup-data)))))
2561      (and headers
2562           (vectorp headers)
2563           (mail-header-subject headers))))
2564
2565 (defmacro gnus-summary-article-score (&optional number)
2566   "Return current article score."
2567   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2568                   gnus-newsgroup-scored))
2569        gnus-summary-default-score 0))
2570
2571 (defun gnus-summary-article-children (&optional number)
2572   "Return a list of article numbers that are children of article NUMBER."
2573   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2574          (level (gnus-data-level (car data)))
2575          l children)
2576     (while (and (setq data (cdr data))
2577                 (> (setq l (gnus-data-level (car data))) level))
2578       (and (= (1+ level) l)
2579            (push (gnus-data-number (car data))
2580                  children)))
2581     (nreverse children)))
2582
2583 (defun gnus-summary-article-parent (&optional number)
2584   "Return the article number of the parent of article NUMBER."
2585   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2586                                     (gnus-data-list t)))
2587          (level (gnus-data-level (car data))))
2588     (if (zerop level)
2589         ()                              ; This is a root.
2590       ;; We search until we find an article with a level less than
2591       ;; this one.  That function has to be the parent.
2592       (while (and (setq data (cdr data))
2593                   (not (< (gnus-data-level (car data)) level))))
2594       (and data (gnus-data-number (car data))))))
2595
2596 (defun gnus-unread-mark-p (mark)
2597   "Say whether MARK is the unread mark."
2598   (= mark gnus-unread-mark))
2599
2600 (defun gnus-read-mark-p (mark)
2601   "Say whether MARK is one of the marks that mark as read.
2602 This is all marks except unread, ticked, dormant, and expirable."
2603   (not (or (= mark gnus-unread-mark)
2604            (= mark gnus-ticked-mark)
2605            (= mark gnus-dormant-mark)
2606            (= mark gnus-expirable-mark))))
2607
2608 (defmacro gnus-article-mark (number)
2609   "Return the MARK of article NUMBER.
2610 This macro should only be used when computing the mark the \"first\"
2611 time; i.e., when generating the summary lines.  After that,
2612 `gnus-summary-article-mark' should be used to examine the
2613 marks of articles."
2614   `(cond
2615     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2616     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2617     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2618     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2619     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2620     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2621     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2622     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2623            gnus-ancient-mark))))
2624
2625 ;; Saving hidden threads.
2626
2627 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2628 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2629
2630 (defmacro gnus-save-hidden-threads (&rest forms)
2631   "Save hidden threads, eval FORMS, and restore the hidden threads."
2632   (let ((config (make-symbol "config")))
2633     `(let ((,config (gnus-hidden-threads-configuration)))
2634        (unwind-protect
2635            (save-excursion
2636              ,@forms)
2637          (gnus-restore-hidden-threads-configuration ,config)))))
2638
2639 (defun gnus-data-compute-positions ()
2640   "Compute the positions of all articles."
2641   (setq gnus-newsgroup-data-reverse nil)
2642   (let ((data gnus-newsgroup-data))
2643     (save-excursion
2644       (gnus-save-hidden-threads
2645         (gnus-summary-show-all-threads)
2646         (goto-char (point-min))
2647         (while data
2648           (while (get-text-property (point) 'gnus-intangible)
2649             (forward-line 1))
2650           (gnus-data-set-pos (car data) (+ (point) 3))
2651           (setq data (cdr data))
2652           (forward-line 1))))))
2653
2654 (defun gnus-hidden-threads-configuration ()
2655   "Return the current hidden threads configuration."
2656   (save-excursion
2657     (let (config)
2658       (goto-char (point-min))
2659       (while (search-forward "\r" nil t)
2660         (push (1- (point)) config))
2661       config)))
2662
2663 (defun gnus-restore-hidden-threads-configuration (config)
2664   "Restore hidden threads configuration from CONFIG."
2665   (save-excursion
2666     (let (point buffer-read-only)
2667       (while (setq point (pop config))
2668         (when (and (< point (point-max))
2669                    (goto-char point)
2670                    (eq (char-after) ?\n))
2671           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2672
2673 ;; Various summary mode internalish functions.
2674
2675 (defun gnus-mouse-pick-article (e)
2676   (interactive "e")
2677   (mouse-set-point e)
2678   (gnus-summary-next-page nil t))
2679
2680 (defun gnus-summary-set-display-table ()
2681   "Change the display table.
2682 Odd characters have a tendency to mess
2683 up nicely formatted displays - we make all possible glyphs
2684 display only a single character."
2685
2686   ;; We start from the standard display table, if any.
2687   (let ((table (or (copy-sequence standard-display-table)
2688                    (make-display-table)))
2689         (i 32))
2690     ;; Nix out all the control chars...
2691     (while (>= (setq i (1- i)) 0)
2692       (aset table i [??]))
2693     ;; ... but not newline and cr, of course.  (cr is necessary for the
2694     ;; selective display).
2695     (aset table ?\n nil)
2696     (aset table ?\r nil)
2697     ;; We keep TAB as well.
2698     (aset table ?\t nil)
2699     ;; We nix out any glyphs over 126 that are not set already.
2700     (let ((i 256))
2701       (while (>= (setq i (1- i)) 127)
2702         ;; Only modify if the entry is nil.
2703         (unless (aref table i)
2704           (aset table i [??]))))
2705     (setq buffer-display-table table)))
2706
2707 (defun gnus-summary-buffer-name (group)
2708   "Return the summary buffer name of GROUP."
2709   (concat "*Summary " group "*"))
2710
2711 (defun gnus-summary-setup-buffer (group)
2712   "Initialize summary buffer."
2713   (let ((buffer (gnus-summary-buffer-name group)))
2714     (if (get-buffer buffer)
2715         (progn
2716           (set-buffer buffer)
2717           (setq gnus-summary-buffer (current-buffer))
2718           (not gnus-newsgroup-prepared))
2719       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2720       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2721       (gnus-summary-mode group)
2722       (when gnus-carpal
2723         (gnus-carpal-setup-buffer 'summary))
2724       (unless gnus-single-article-buffer
2725         (make-local-variable 'gnus-article-buffer)
2726         (make-local-variable 'gnus-article-current)
2727         (make-local-variable 'gnus-original-article-buffer))
2728       (setq gnus-newsgroup-name group)
2729       ;; Set any local variables in the group parameters.
2730       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2731       t)))
2732
2733 (defun gnus-set-global-variables ()
2734   "Set the global equivalents of the buffer-local variables.
2735 They are set to the latest values they had.  These reflect the summary
2736 buffer that was in action when the last article was fetched."
2737   (when (eq major-mode 'gnus-summary-mode)
2738     (setq gnus-summary-buffer (current-buffer))
2739     (let ((name gnus-newsgroup-name)
2740           (marked gnus-newsgroup-marked)
2741           (unread gnus-newsgroup-unreads)
2742           (headers gnus-current-headers)
2743           (data gnus-newsgroup-data)
2744           (summary gnus-summary-buffer)
2745           (article-buffer gnus-article-buffer)
2746           (original gnus-original-article-buffer)
2747           (gac gnus-article-current)
2748           (reffed gnus-reffed-article-number)
2749           (score-file gnus-current-score-file)
2750           (default-charset gnus-newsgroup-charset)
2751           vlist)
2752       (let ((locals gnus-newsgroup-variables))
2753         (while locals
2754           (if (consp (car locals))
2755               (push (eval (caar locals)) vlist)
2756             (push (eval (car locals)) vlist))
2757           (setq locals (cdr locals)))
2758         (setq vlist (nreverse vlist)))
2759       (save-excursion
2760         (set-buffer gnus-group-buffer)
2761         (setq gnus-newsgroup-name name
2762               gnus-newsgroup-marked marked
2763               gnus-newsgroup-unreads unread
2764               gnus-current-headers headers
2765               gnus-newsgroup-data data
2766               gnus-article-current gac
2767               gnus-summary-buffer summary
2768               gnus-article-buffer article-buffer
2769               gnus-original-article-buffer original
2770               gnus-reffed-article-number reffed
2771               gnus-current-score-file score-file
2772               gnus-newsgroup-charset default-charset)
2773         (let ((locals gnus-newsgroup-variables))
2774           (while locals
2775             (if (consp (car locals))
2776                 (set (caar locals) (pop vlist))
2777               (set (car locals) (pop vlist)))
2778             (setq locals (cdr locals))))
2779         ;; The article buffer also has local variables.
2780         (when (gnus-buffer-live-p gnus-article-buffer)
2781           (set-buffer gnus-article-buffer)
2782           (setq gnus-summary-buffer summary))))))
2783
2784 (defun gnus-summary-article-unread-p (article)
2785   "Say whether ARTICLE is unread or not."
2786   (memq article gnus-newsgroup-unreads))
2787
2788 (defun gnus-summary-first-article-p (&optional article)
2789   "Return whether ARTICLE is the first article in the buffer."
2790   (if (not (setq article (or article (gnus-summary-article-number))))
2791       nil
2792     (eq article (caar gnus-newsgroup-data))))
2793
2794 (defun gnus-summary-last-article-p (&optional article)
2795   "Return whether ARTICLE is the last article in the buffer."
2796   (if (not (setq article (or article (gnus-summary-article-number))))
2797       ;; All non-existent numbers are the last article.  :-)
2798       t
2799     (not (cdr (gnus-data-find-list article)))))
2800
2801 (defun gnus-make-thread-indent-array ()
2802   (let ((n 200))
2803     (unless (and gnus-thread-indent-array
2804                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2805       (setq gnus-thread-indent-array (make-vector 201 "")
2806             gnus-thread-indent-array-level gnus-thread-indent-level)
2807       (while (>= n 0)
2808         (aset gnus-thread-indent-array n
2809               (make-string (* n gnus-thread-indent-level) ? ))
2810         (setq n (1- n))))))
2811
2812 (defun gnus-update-summary-mark-positions ()
2813   "Compute where the summary marks are to go."
2814   (save-excursion
2815     (when (gnus-buffer-exists-p gnus-summary-buffer)
2816       (set-buffer gnus-summary-buffer))
2817     (let ((gnus-replied-mark 129)
2818           (gnus-score-below-mark 130)
2819           (gnus-score-over-mark 130)
2820           (gnus-download-mark 131)
2821           (spec gnus-summary-line-format-spec)
2822           gnus-visual pos)
2823       (save-excursion
2824         (gnus-set-work-buffer)
2825         (let ((gnus-summary-line-format-spec spec)
2826               (gnus-newsgroup-downloadable '((0 . t))))
2827           (gnus-summary-insert-line
2828            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2829            0 nil 128 t nil "" nil 1)
2830           (goto-char (point-min))
2831           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2832                                              (- (point) 2)))))
2833           (goto-char (point-min))
2834           (push (cons 'replied (and (search-forward "\201" nil t)
2835                                     (- (point) 2)))
2836                 pos)
2837           (goto-char (point-min))
2838           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2839                 pos)
2840           (goto-char (point-min))
2841           (push (cons 'download
2842                       (and (search-forward "\203" nil t) (- (point) 2)))
2843                 pos)))
2844       (setq gnus-summary-mark-positions pos))))
2845
2846 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2847   "Insert a dummy root in the summary buffer."
2848   (beginning-of-line)
2849   (gnus-add-text-properties
2850    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2851    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2852
2853 (defun gnus-summary-from-or-to-or-newsgroups (header)
2854   (let ((to (cdr (assq 'To (mail-header-extra header))))
2855         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2856         (mail-parse-charset gnus-newsgroup-charset)
2857         (mail-parse-ignored-charsets
2858          (save-excursion (set-buffer gnus-summary-buffer)
2859                          gnus-newsgroup-ignored-charsets)))
2860     (cond
2861      ((and to
2862            gnus-ignored-from-addresses
2863            (string-match gnus-ignored-from-addresses
2864                          (mail-header-from header)))
2865       (concat "-> "
2866               (or (car (funcall gnus-extract-address-components
2867                                 (funcall
2868                                  gnus-decode-encoded-word-function to)))
2869                   (funcall gnus-decode-encoded-word-function to))))
2870      ((and newsgroups
2871            gnus-ignored-from-addresses
2872            (string-match gnus-ignored-from-addresses
2873                          (mail-header-from header)))
2874       (concat "=> " newsgroups))
2875      (t
2876       (or (car (funcall gnus-extract-address-components
2877                         (mail-header-from header)))
2878           (mail-header-from header))))))
2879
2880 (defun gnus-summary-insert-line (gnus-tmp-header
2881                                  gnus-tmp-level gnus-tmp-current
2882                                  gnus-tmp-unread gnus-tmp-replied
2883                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2884                                  &optional gnus-tmp-dummy gnus-tmp-score
2885                                  gnus-tmp-process)
2886   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2887          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2888          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2889          (gnus-tmp-score-char
2890           (if (or (null gnus-summary-default-score)
2891                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2892                       gnus-summary-zcore-fuzz))
2893               ?  ;Whitespace
2894             (if (< gnus-tmp-score gnus-summary-default-score)
2895                 gnus-score-below-mark gnus-score-over-mark)))
2896          (gnus-tmp-replied
2897           (cond (gnus-tmp-process gnus-process-mark)
2898                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2899                  gnus-cached-mark)
2900                 (gnus-tmp-replied gnus-replied-mark)
2901                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2902                  gnus-saved-mark)
2903                 (t gnus-no-mark)))
2904          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2905          (gnus-tmp-name
2906           (cond
2907            ((string-match "<[^>]+> *$" gnus-tmp-from)
2908             (let ((beg (match-beginning 0)))
2909               (or (and (string-match "^\".+\"" gnus-tmp-from)
2910                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2911                   (substring gnus-tmp-from 0 beg))))
2912            ((string-match "(.+)" gnus-tmp-from)
2913             (substring gnus-tmp-from
2914                        (1+ (match-beginning 0)) (1- (match-end 0))))
2915            (t gnus-tmp-from)))
2916          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2917          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2918          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2919          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2920          (buffer-read-only nil))
2921     (when (string= gnus-tmp-name "")
2922       (setq gnus-tmp-name gnus-tmp-from))
2923     (unless (numberp gnus-tmp-lines)
2924       (setq gnus-tmp-lines -1))
2925     (when (= gnus-tmp-lines -1)
2926       (setq gnus-tmp-lines "?"))
2927     (gnus-put-text-property
2928      (point)
2929      (progn (eval gnus-summary-line-format-spec) (point))
2930      'gnus-number gnus-tmp-number)
2931     (when (gnus-visual-p 'summary-highlight 'highlight)
2932       (forward-line -1)
2933       (gnus-run-hooks 'gnus-summary-update-hook)
2934       (forward-line 1))))
2935
2936 (defun gnus-summary-update-line (&optional dont-update)
2937   "Update summary line after change."
2938   (when (and gnus-summary-default-score
2939              (not gnus-summary-inhibit-highlight))
2940     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2941            (article (gnus-summary-article-number))
2942            (score (gnus-summary-article-score article)))
2943       (unless dont-update
2944         (if (and gnus-summary-mark-below
2945                  (< (gnus-summary-article-score)
2946                     gnus-summary-mark-below))
2947             ;; This article has a low score, so we mark it as read.
2948             (when (memq article gnus-newsgroup-unreads)
2949               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2950           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2951             ;; This article was previously marked as read on account
2952             ;; of a low score, but now it has risen, so we mark it as
2953             ;; unread.
2954             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2955         (gnus-summary-update-mark
2956          (if (or (null gnus-summary-default-score)
2957                  (<= (abs (- score gnus-summary-default-score))
2958                      gnus-summary-zcore-fuzz))
2959              ?  ;Whitespace
2960            (if (< score gnus-summary-default-score)
2961                gnus-score-below-mark gnus-score-over-mark))
2962          'score))
2963       ;; Do visual highlighting.
2964       (when (gnus-visual-p 'summary-highlight 'highlight)
2965         (gnus-run-hooks 'gnus-summary-update-hook)))))
2966
2967 (defvar gnus-tmp-new-adopts nil)
2968
2969 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2970   "Return the number of articles in THREAD.
2971 This may be 0 in some cases -- if none of the articles in
2972 the thread are to be displayed."
2973   (let* ((number
2974           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2975           (cond
2976            ((not (listp thread))
2977             1)
2978            ((and (consp thread) (cdr thread))
2979             (apply
2980              '+ 1 (mapcar
2981                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2982            ((null thread)
2983             1)
2984            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2985             1)
2986            (t 0))))
2987     (when (and level (zerop level) gnus-tmp-new-adopts)
2988       (incf number
2989             (apply '+ (mapcar
2990                        'gnus-summary-number-of-articles-in-thread
2991                        gnus-tmp-new-adopts))))
2992     (if char
2993         (if (> number 1) gnus-not-empty-thread-mark
2994           gnus-empty-thread-mark)
2995       number)))
2996
2997 (defun gnus-summary-set-local-parameters (group)
2998   "Go through the local params of GROUP and set all variable specs in that list."
2999   (let ((params (gnus-group-find-parameter group))
3000         (vars '(quit-config)) ; Ignore quit-config.
3001         elem)
3002     (while params
3003       (setq elem (car params)
3004             params (cdr params))
3005       (and (consp elem)                 ; Has to be a cons.
3006            (consp (cdr elem))           ; The cdr has to be a list.
3007            (symbolp (car elem))         ; Has to be a symbol in there.
3008            (not (memq (car elem) vars))
3009            (ignore-errors               ; So we set it.
3010              (push (car elem) vars)
3011              (make-local-variable (car elem))
3012              (set (car elem) (eval (nth 1 elem))))))))
3013
3014 (defun gnus-summary-read-group (group &optional show-all no-article
3015                                       kill-buffer no-display backward
3016                                       select-articles)
3017   "Start reading news in newsgroup GROUP.
3018 If SHOW-ALL is non-nil, already read articles are also listed.
3019 If NO-ARTICLE is non-nil, no article is selected initially.
3020 If NO-DISPLAY, don't generate a summary buffer."
3021   (let (result)
3022     (while (and group
3023                 (null (setq result
3024                             (let ((gnus-auto-select-next nil))
3025                               (or (gnus-summary-read-group-1
3026                                    group show-all no-article
3027                                    kill-buffer no-display
3028                                    select-articles)
3029                                   (setq show-all nil
3030                                         select-articles nil)))))
3031                 (eq gnus-auto-select-next 'quietly))
3032       (set-buffer gnus-group-buffer)
3033       ;; The entry function called above goes to the next
3034       ;; group automatically, so we go two groups back
3035       ;; if we are searching for the previous group.
3036       (when backward
3037         (gnus-group-prev-unread-group 2))
3038       (if (not (equal group (gnus-group-group-name)))
3039           (setq group (gnus-group-group-name))
3040         (setq group nil)))
3041     result))
3042
3043 (defun gnus-summary-read-group-1 (group show-all no-article
3044                                         kill-buffer no-display
3045                                         &optional select-articles)
3046   ;; Killed foreign groups can't be entered.
3047   ;;  (when (and (not (gnus-group-native-p group))
3048   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3049   ;;    (error "Dead non-native groups can't be entered"))
3050   (gnus-message 5 "Retrieving newsgroup: %s..." 
3051                 (gnus-group-decoded-name group))
3052   (let* ((new-group (gnus-summary-setup-buffer group))
3053          (quit-config (gnus-group-quit-config group))
3054          (did-select (and new-group (gnus-select-newsgroup
3055                                      group show-all select-articles))))
3056     (cond
3057      ;; This summary buffer exists already, so we just select it.
3058      ((not new-group)
3059       (gnus-set-global-variables)
3060       (when kill-buffer
3061         (gnus-kill-or-deaden-summary kill-buffer))
3062       (gnus-configure-windows 'summary 'force)
3063       (gnus-set-mode-line 'summary)
3064       (gnus-summary-position-point)
3065       (message "")
3066       t)
3067      ;; We couldn't select this group.
3068      ((null did-select)
3069       (when (and (eq major-mode 'gnus-summary-mode)
3070                  (not (equal (current-buffer) kill-buffer)))
3071         (kill-buffer (current-buffer))
3072         (if (not quit-config)
3073             (progn
3074               ;; Update the info -- marks might need to be removed,
3075               ;; for instance.
3076               (gnus-summary-update-info)
3077               (set-buffer gnus-group-buffer)
3078               (gnus-group-jump-to-group group)
3079               (gnus-group-next-unread-group 1))
3080           (gnus-handle-ephemeral-exit quit-config)))
3081       (let ((grpinfo (gnus-get-info group)))
3082         (if (null (gnus-info-read grpinfo))
3083             (gnus-message 3 "Group %s contains no messages" 
3084                           (gnus-group-decoded-name group))
3085           (gnus-message 3 "Can't select group")))
3086       nil)
3087      ;; The user did a `C-g' while prompting for number of articles,
3088      ;; so we exit this group.
3089      ((eq did-select 'quit)
3090       (and (eq major-mode 'gnus-summary-mode)
3091            (not (equal (current-buffer) kill-buffer))
3092            (kill-buffer (current-buffer)))
3093       (when kill-buffer
3094         (gnus-kill-or-deaden-summary kill-buffer))
3095       (if (not quit-config)
3096           (progn
3097             (set-buffer gnus-group-buffer)
3098             (gnus-group-jump-to-group group)
3099             (gnus-group-next-unread-group 1)
3100             (gnus-configure-windows 'group 'force))
3101         (gnus-handle-ephemeral-exit quit-config))
3102       ;; Finally signal the quit.
3103       (signal 'quit nil))
3104      ;; The group was successfully selected.
3105      (t
3106       (gnus-set-global-variables)
3107       ;; Save the active value in effect when the group was entered.
3108       (setq gnus-newsgroup-active
3109             (gnus-copy-sequence
3110              (gnus-active gnus-newsgroup-name)))
3111       ;; You can change the summary buffer in some way with this hook.
3112       (gnus-run-hooks 'gnus-select-group-hook)
3113       (gnus-update-format-specifications
3114        nil 'summary 'summary-mode 'summary-dummy)
3115       (gnus-update-summary-mark-positions)
3116       ;; Do score processing.
3117       (when gnus-use-scoring
3118         (gnus-possibly-score-headers))
3119       ;; Check whether to fill in the gaps in the threads.
3120       (when gnus-build-sparse-threads
3121         (gnus-build-sparse-threads))
3122       ;; Find the initial limit.
3123       (if gnus-show-threads
3124           (if show-all
3125               (let ((gnus-newsgroup-dormant nil))
3126                 (gnus-summary-initial-limit show-all))
3127             (gnus-summary-initial-limit show-all))
3128         ;; When untreaded, all articles are always shown.
3129         (setq gnus-newsgroup-limit
3130               (mapcar
3131                (lambda (header) (mail-header-number header))
3132                gnus-newsgroup-headers)))
3133       ;; Generate the summary buffer.
3134       (unless no-display
3135         (gnus-summary-prepare))
3136       (when gnus-use-trees
3137         (gnus-tree-open group)
3138         (setq gnus-summary-highlight-line-function
3139               'gnus-tree-highlight-article))
3140       ;; If the summary buffer is empty, but there are some low-scored
3141       ;; articles or some excluded dormants, we include these in the
3142       ;; buffer.
3143       (when (and (zerop (buffer-size))
3144                  (not no-display))
3145         (cond (gnus-newsgroup-dormant
3146                (gnus-summary-limit-include-dormant))
3147               ((and gnus-newsgroup-scored show-all)
3148                (gnus-summary-limit-include-expunged t))))
3149       ;; Function `gnus-apply-kill-file' must be called in this hook.
3150       (gnus-run-hooks 'gnus-apply-kill-hook)
3151       (if (and (zerop (buffer-size))
3152                (not no-display))
3153           (progn
3154             ;; This newsgroup is empty.
3155             (gnus-summary-catchup-and-exit nil t)
3156             (gnus-message 6 "No unread news")
3157             (when kill-buffer
3158               (gnus-kill-or-deaden-summary kill-buffer))
3159             ;; Return nil from this function.
3160             nil)
3161         ;; Hide conversation thread subtrees.  We cannot do this in
3162         ;; gnus-summary-prepare-hook since kill processing may not
3163         ;; work with hidden articles.
3164         (and gnus-show-threads
3165              gnus-thread-hide-subtree
3166              (gnus-summary-hide-all-threads))
3167         (when kill-buffer
3168           (gnus-kill-or-deaden-summary kill-buffer))
3169         ;; Show first unread article if requested.
3170         (if (and (not no-article)
3171                  (not no-display)
3172                  gnus-newsgroup-unreads
3173                  gnus-auto-select-first)
3174             (progn
3175               (gnus-configure-windows 'summary)
3176               (cond
3177                ((eq gnus-auto-select-first 'best)
3178                 (gnus-summary-best-unread-article))
3179                ((eq gnus-auto-select-first t)
3180                 (gnus-summary-first-unread-article))
3181                ((gnus-functionp gnus-auto-select-first)
3182                 (funcall gnus-auto-select-first))))
3183           ;; Don't select any articles, just move point to the first
3184           ;; article in the group.
3185           (goto-char (point-min))
3186           (gnus-summary-position-point)
3187           (gnus-configure-windows 'summary 'force)
3188           (gnus-set-mode-line 'summary))
3189         (when (get-buffer-window gnus-group-buffer t)
3190           ;; Gotta use windows, because recenter does weird stuff if
3191           ;; the current buffer ain't the displayed window.
3192           (let ((owin (selected-window)))
3193             (select-window (get-buffer-window gnus-group-buffer t))
3194             (when (gnus-group-goto-group group)
3195               (recenter))
3196             (select-window owin)))
3197         ;; Mark this buffer as "prepared".
3198         (setq gnus-newsgroup-prepared t)
3199         (gnus-run-hooks 'gnus-summary-prepared-hook)
3200         t)))))
3201
3202 (defun gnus-summary-prepare ()
3203   "Generate the summary buffer."
3204   (interactive)
3205   (let ((buffer-read-only nil))
3206     (erase-buffer)
3207     (setq gnus-newsgroup-data nil
3208           gnus-newsgroup-data-reverse nil)
3209     (gnus-run-hooks 'gnus-summary-generate-hook)
3210     ;; Generate the buffer, either with threads or without.
3211     (when gnus-newsgroup-headers
3212       (gnus-summary-prepare-threads
3213        (if gnus-show-threads
3214            (gnus-sort-gathered-threads
3215             (funcall gnus-summary-thread-gathering-function
3216                      (gnus-sort-threads
3217                       (gnus-cut-threads (gnus-make-threads)))))
3218          ;; Unthreaded display.
3219          (gnus-sort-articles gnus-newsgroup-headers))))
3220     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3221     ;; Call hooks for modifying summary buffer.
3222     (goto-char (point-min))
3223     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3224
3225 (defsubst gnus-general-simplify-subject (subject)
3226   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3227   (setq subject
3228         (cond
3229          ;; Truncate the subject.
3230          (gnus-simplify-subject-functions
3231           (gnus-map-function gnus-simplify-subject-functions subject))
3232          ((numberp gnus-summary-gather-subject-limit)
3233           (setq subject (gnus-simplify-subject-re subject))
3234           (if (> (length subject) gnus-summary-gather-subject-limit)
3235               (substring subject 0 gnus-summary-gather-subject-limit)
3236             subject))
3237          ;; Fuzzily simplify it.
3238          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3239           (gnus-simplify-subject-fuzzy subject))
3240          ;; Just remove the leading "Re:".
3241          (t
3242           (gnus-simplify-subject-re subject))))
3243
3244   (if (and gnus-summary-gather-exclude-subject
3245            (string-match gnus-summary-gather-exclude-subject subject))
3246       nil                               ; This article shouldn't be gathered
3247     subject))
3248
3249 (defun gnus-summary-simplify-subject-query ()
3250   "Query where the respool algorithm would put this article."
3251   (interactive)
3252   (gnus-summary-select-article)
3253   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3254
3255 (defun gnus-gather-threads-by-subject (threads)
3256   "Gather threads by looking at Subject headers."
3257   (if (not gnus-summary-make-false-root)
3258       threads
3259     (let ((hashtb (gnus-make-hashtable 1024))
3260           (prev threads)
3261           (result threads)
3262           subject hthread whole-subject)
3263       (while threads
3264         (setq subject (gnus-general-simplify-subject
3265                        (setq whole-subject (mail-header-subject
3266                                             (caar threads)))))
3267         (when subject
3268           (if (setq hthread (gnus-gethash subject hashtb))
3269               (progn
3270                 ;; We enter a dummy root into the thread, if we
3271                 ;; haven't done that already.
3272                 (unless (stringp (caar hthread))
3273                   (setcar hthread (list whole-subject (car hthread))))
3274                 ;; We add this new gathered thread to this gathered
3275                 ;; thread.
3276                 (setcdr (car hthread)
3277                         (nconc (cdar hthread) (list (car threads))))
3278                 ;; Remove it from the list of threads.
3279                 (setcdr prev (cdr threads))
3280                 (setq threads prev))
3281             ;; Enter this thread into the hash table.
3282             (gnus-sethash subject threads hashtb)))
3283         (setq prev threads)
3284         (setq threads (cdr threads)))
3285       result)))
3286
3287 (defun gnus-gather-threads-by-references (threads)
3288   "Gather threads by looking at References headers."
3289   (let ((idhashtb (gnus-make-hashtable 1024))
3290         (thhashtb (gnus-make-hashtable 1024))
3291         (prev threads)
3292         (result threads)
3293         ids references id gthread gid entered ref)
3294     (while threads
3295       (when (setq references (mail-header-references (caar threads)))
3296         (setq id (mail-header-id (caar threads))
3297               ids (gnus-split-references references)
3298               entered nil)
3299         (while (setq ref (pop ids))
3300           (setq ids (delete ref ids))
3301           (if (not (setq gid (gnus-gethash ref idhashtb)))
3302               (progn
3303                 (gnus-sethash ref id idhashtb)
3304                 (gnus-sethash id threads thhashtb))
3305             (setq gthread (gnus-gethash gid thhashtb))
3306             (unless entered
3307               ;; We enter a dummy root into the thread, if we
3308               ;; haven't done that already.
3309               (unless (stringp (caar gthread))
3310                 (setcar gthread (list (mail-header-subject (caar gthread))
3311                                       (car gthread))))
3312               ;; We add this new gathered thread to this gathered
3313               ;; thread.
3314               (setcdr (car gthread)
3315                       (nconc (cdar gthread) (list (car threads)))))
3316             ;; Add it into the thread hash table.
3317             (gnus-sethash id gthread thhashtb)
3318             (setq entered t)
3319             ;; Remove it from the list of threads.
3320             (setcdr prev (cdr threads))
3321             (setq threads prev))))
3322       (setq prev threads)
3323       (setq threads (cdr threads)))
3324     result))
3325
3326 (defun gnus-sort-gathered-threads (threads)
3327   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3328   (let ((result threads))
3329     (while threads
3330       (when (stringp (caar threads))
3331         (setcdr (car threads)
3332                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3333       (setq threads (cdr threads)))
3334     result))
3335
3336 (defun gnus-thread-loop-p (root thread)
3337   "Say whether ROOT is in THREAD."
3338   (let ((stack (list thread))
3339         (infloop 0)
3340         th)
3341     (while (setq thread (pop stack))
3342       (setq th (cdr thread))
3343       (while (and th
3344                   (not (eq (caar th) root)))
3345         (pop th))
3346       (if th
3347           ;; We have found a loop.
3348           (let (ref-dep)
3349             (setcdr thread (delq (car th) (cdr thread)))
3350             (if (boundp (setq ref-dep (intern "none"
3351                                               gnus-newsgroup-dependencies)))
3352                 (setcdr (symbol-value ref-dep)
3353                         (nconc (cdr (symbol-value ref-dep))
3354                                (list (car th))))
3355               (set ref-dep (list nil (car th))))
3356             (setq infloop 1
3357                   stack nil))
3358         ;; Push all the subthreads onto the stack.
3359         (push (cdr thread) stack)))
3360     infloop))
3361
3362 (defun gnus-make-threads ()
3363   "Go through the dependency hashtb and find the roots.  Return all threads."
3364   (let (threads)
3365     (while (catch 'infloop
3366              (mapatoms
3367               (lambda (refs)
3368                 ;; Deal with self-referencing References loops.
3369                 (when (and (car (symbol-value refs))
3370                            (not (zerop
3371                                  (apply
3372                                   '+
3373                                   (mapcar
3374                                    (lambda (thread)
3375                                      (gnus-thread-loop-p
3376                                       (car (symbol-value refs)) thread))
3377                                    (cdr (symbol-value refs)))))))
3378                   (setq threads nil)
3379                   (throw 'infloop t))
3380                 (unless (car (symbol-value refs))
3381                   ;; These threads do not refer back to any other articles,
3382                   ;; so they're roots.
3383                   (setq threads (append (cdr (symbol-value refs)) threads))))
3384               gnus-newsgroup-dependencies)))
3385     threads))
3386
3387 ;; Build the thread tree.
3388 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3389   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3390
3391 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3392 if it was already present.
3393
3394 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3395 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3396 Message-IDs will be renamed be renamed to a unique Message-ID before
3397 being entered.
3398
3399 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3400   (let* ((id (mail-header-id header))
3401          (id-dep (and id (intern id dependencies)))
3402          ref ref-dep ref-header)
3403     ;; Enter this `header' in the `dependencies' table.
3404     (cond
3405      ((not id-dep)
3406       (setq header nil))
3407      ;; The first two cases do the normal part: enter a new `header'
3408      ;; in the `dependencies' table.
3409      ((not (boundp id-dep))
3410       (set id-dep (list header)))
3411      ((null (car (symbol-value id-dep)))
3412       (setcar (symbol-value id-dep) header))
3413
3414      ;; From here the `header' was already present in the
3415      ;; `dependencies' table.
3416      (force-new
3417       ;; Overrides an existing entry;
3418       ;; just set the header part of the entry.
3419       (setcar (symbol-value id-dep) header))
3420
3421      ;; Renames the existing `header' to a unique Message-ID.
3422      ((not gnus-summary-ignore-duplicates)
3423       ;; An article with this Message-ID has already been seen.
3424       ;; We rename the Message-ID.
3425       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3426            (list header))
3427       (mail-header-set-id header id))
3428
3429      ;; The last case ignores an existing entry, except it adds any
3430      ;; additional Xrefs (in case the two articles came from different
3431      ;; servers.
3432      ;; Also sets `header' to `nil' meaning that the `dependencies'
3433      ;; table was *not* modified.
3434      (t
3435       (mail-header-set-xref
3436        (car (symbol-value id-dep))
3437        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3438                    "")
3439                (or (mail-header-xref header) "")))
3440       (setq header nil)))
3441
3442     (when header
3443       ;; First check if that we are not creating a References loop.
3444       (setq ref (gnus-parent-id (mail-header-references header)))
3445       (while (and ref
3446                   (setq ref-dep (intern-soft ref dependencies))
3447                   (boundp ref-dep)
3448                   (setq ref-header (car (symbol-value ref-dep))))
3449         (if (string= id ref)
3450             ;; Yuk!  This is a reference loop.  Make the article be a
3451             ;; root article.
3452             (progn
3453               (mail-header-set-references (car (symbol-value id-dep)) "none")
3454               (setq ref nil))
3455           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3456       (setq ref (gnus-parent-id (mail-header-references header)))
3457       (setq ref-dep (intern (or ref "none") dependencies))
3458       (if (boundp ref-dep)
3459           (setcdr (symbol-value ref-dep)
3460                   (nconc (cdr (symbol-value ref-dep))
3461                          (list (symbol-value id-dep))))
3462         (set ref-dep (list nil (symbol-value id-dep)))))
3463     header))
3464
3465 (defun gnus-build-sparse-threads ()
3466   (let ((headers gnus-newsgroup-headers)
3467         (mail-parse-charset gnus-newsgroup-charset)
3468         (gnus-summary-ignore-duplicates t)
3469         header references generation relations
3470         subject child end new-child date)
3471     ;; First we create an alist of generations/relations, where
3472     ;; generations is how much we trust the relation, and the relation
3473     ;; is parent/child.
3474     (gnus-message 7 "Making sparse threads...")
3475     (save-excursion
3476       (nnheader-set-temp-buffer " *gnus sparse threads*")
3477       (while (setq header (pop headers))
3478         (when (and (setq references (mail-header-references header))
3479                    (not (string= references "")))
3480           (insert references)
3481           (setq child (mail-header-id header)
3482                 subject (mail-header-subject header)
3483                 date (mail-header-date header)
3484                 generation 0)
3485           (while (search-backward ">" nil t)
3486             (setq end (1+ (point)))
3487             (when (search-backward "<" nil t)
3488               (setq new-child (buffer-substring (point) end))
3489               (push (list (incf generation)
3490                           child (setq child new-child)
3491                           subject date)
3492                     relations)))
3493           (when child
3494             (push (list (1+ generation) child nil subject) relations))
3495           (erase-buffer)))
3496       (kill-buffer (current-buffer)))
3497     ;; Sort over trustworthiness.
3498     (mapcar
3499      (lambda (relation)
3500        (when (gnus-dependencies-add-header
3501               (make-full-mail-header
3502                gnus-reffed-article-number
3503                (nth 3 relation) "" (or (nth 4 relation) "")
3504                (nth 1 relation)
3505                (or (nth 2 relation) "") 0 0 "")
3506               gnus-newsgroup-dependencies nil)
3507          (push gnus-reffed-article-number gnus-newsgroup-limit)
3508          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3509          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3510                gnus-newsgroup-reads)
3511          (decf gnus-reffed-article-number)))
3512      (sort relations 'car-less-than-car))
3513     (gnus-message 7 "Making sparse threads...done")))
3514
3515 (defun gnus-build-old-threads ()
3516   ;; Look at all the articles that refer back to old articles, and
3517   ;; fetch the headers for the articles that aren't there.  This will
3518   ;; build complete threads - if the roots haven't been expired by the
3519   ;; server, that is.
3520   (let ((mail-parse-charset gnus-newsgroup-charset)
3521         id heads)
3522     (mapatoms
3523      (lambda (refs)
3524        (when (not (car (symbol-value refs)))
3525          (setq heads (cdr (symbol-value refs)))
3526          (while heads
3527            (if (memq (mail-header-number (caar heads))
3528                      gnus-newsgroup-dormant)
3529                (setq heads (cdr heads))
3530              (setq id (symbol-name refs))
3531              (while (and (setq id (gnus-build-get-header id))
3532                          (not (car (gnus-id-to-thread id)))))
3533              (setq heads nil)))))
3534      gnus-newsgroup-dependencies)))
3535
3536 ;; This function has to be called with point after the article number
3537 ;; on the beginning of the line.
3538 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3539   (let ((eol (gnus-point-at-eol))
3540         (buffer (current-buffer))
3541         header)
3542
3543     ;; overview: [num subject from date id refs chars lines misc]
3544     (unwind-protect
3545         (progn
3546           (narrow-to-region (point) eol)
3547           (unless (eobp)
3548             (forward-char))
3549
3550           (setq header
3551                 (make-full-mail-header
3552                  number                 ; number
3553                  (funcall gnus-decode-encoded-word-function
3554                           (nnheader-nov-field)) ; subject
3555                  (funcall gnus-decode-encoded-word-function
3556                           (nnheader-nov-field)) ; from
3557                  (nnheader-nov-field)   ; date
3558                  (nnheader-nov-read-message-id) ; id
3559                  (nnheader-nov-field)   ; refs
3560                  (nnheader-nov-read-integer) ; chars
3561                  (nnheader-nov-read-integer) ; lines
3562                  (unless (eobp)
3563                    (if (looking-at "Xref: ")
3564                        (goto-char (match-end 0)))
3565                    (nnheader-nov-field)) ; Xref
3566                  (nnheader-nov-parse-extra)))) ; extra
3567
3568       (widen))
3569
3570     (when gnus-alter-header-function
3571       (funcall gnus-alter-header-function header))
3572     (gnus-dependencies-add-header header dependencies force-new)))
3573
3574 (defun gnus-build-get-header (id)
3575   "Look through the buffer of NOV lines and find the header to ID.
3576 Enter this line into the dependencies hash table, and return
3577 the id of the parent article (if any)."
3578   (let ((deps gnus-newsgroup-dependencies)
3579         found header)
3580     (prog1
3581         (save-excursion
3582           (set-buffer nntp-server-buffer)
3583           (let ((case-fold-search nil))
3584             (goto-char (point-min))
3585             (while (and (not found)
3586                         (search-forward id nil t))
3587               (beginning-of-line)
3588               (setq found (looking-at
3589                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3590                                    (regexp-quote id))))
3591               (or found (beginning-of-line 2)))
3592             (when found
3593               (beginning-of-line)
3594               (and
3595                (setq header (gnus-nov-parse-line
3596                              (read (current-buffer)) deps))
3597                (gnus-parent-id (mail-header-references header))))))
3598       (when header
3599         (let ((number (mail-header-number header)))
3600           (push number gnus-newsgroup-limit)
3601           (push header gnus-newsgroup-headers)
3602           (if (memq number gnus-newsgroup-unselected)
3603               (progn
3604                 (push number gnus-newsgroup-unreads)
3605                 (setq gnus-newsgroup-unselected
3606                       (delq number gnus-newsgroup-unselected)))
3607             (push number gnus-newsgroup-ancient)))))))
3608
3609 (defun gnus-build-all-threads ()
3610   "Read all the headers."
3611   (let ((gnus-summary-ignore-duplicates t)
3612         (mail-parse-charset gnus-newsgroup-charset)
3613         (dependencies gnus-newsgroup-dependencies)
3614         header article)
3615     (save-excursion
3616       (set-buffer nntp-server-buffer)
3617       (let ((case-fold-search nil))
3618         (goto-char (point-min))
3619         (while (not (eobp))
3620           (ignore-errors
3621             (setq article (read (current-buffer))
3622                   header (gnus-nov-parse-line article dependencies)))
3623           (when header
3624             (save-excursion
3625               (set-buffer gnus-summary-buffer)
3626               (push header gnus-newsgroup-headers)
3627               (if (memq (setq article (mail-header-number header))
3628                         gnus-newsgroup-unselected)
3629                   (progn
3630                     (push article gnus-newsgroup-unreads)
3631                     (setq gnus-newsgroup-unselected
3632                           (delq article gnus-newsgroup-unselected)))
3633                 (push article gnus-newsgroup-ancient)))
3634             (forward-line 1)))))))
3635
3636 (defun gnus-summary-update-article-line (article header)
3637   "Update the line for ARTICLE using HEADERS."
3638   (let* ((id (mail-header-id header))
3639          (thread (gnus-id-to-thread id)))
3640     (unless thread
3641       (error "Article in no thread"))
3642     ;; Update the thread.
3643     (setcar thread header)
3644     (gnus-summary-goto-subject article)
3645     (let* ((datal (gnus-data-find-list article))
3646            (data (car datal))
3647            (length (when (cdr datal)
3648                      (- (gnus-data-pos data)
3649                         (gnus-data-pos (cadr datal)))))
3650            (buffer-read-only nil)
3651            (level (gnus-summary-thread-level)))
3652       (gnus-delete-line)
3653       (gnus-summary-insert-line
3654        header level nil (gnus-article-mark article)
3655        (memq article gnus-newsgroup-replied)
3656        (memq article gnus-newsgroup-expirable)
3657        ;; Only insert the Subject string when it's different
3658        ;; from the previous Subject string.
3659        (if (and
3660             gnus-show-threads
3661             (gnus-subject-equal
3662              (condition-case ()
3663                  (mail-header-subject
3664                   (gnus-data-header
3665                    (cadr
3666                     (gnus-data-find-list
3667                      article
3668                      (gnus-data-list t)))))
3669                ;; Error on the side of excessive subjects.
3670                (error ""))
3671              (mail-header-subject header)))
3672            ""
3673          (mail-header-subject header))
3674        nil (cdr (assq article gnus-newsgroup-scored))
3675        (memq article gnus-newsgroup-processable))
3676       (when length
3677         (gnus-data-update-list
3678          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3679
3680 (defun gnus-summary-update-article (article &optional iheader)
3681   "Update ARTICLE in the summary buffer."
3682   (set-buffer gnus-summary-buffer)
3683   (let* ((header (gnus-summary-article-header article))
3684          (id (mail-header-id header))
3685          (data (gnus-data-find article))
3686          (thread (gnus-id-to-thread id))
3687          (references (mail-header-references header))
3688          (parent
3689           (gnus-id-to-thread
3690            (or (gnus-parent-id
3691                 (when (and references
3692                            (not (equal "" references)))
3693                   references))
3694                "none")))
3695          (buffer-read-only nil)
3696          (old (car thread)))
3697     (when thread
3698       (unless iheader
3699         (setcar thread nil)
3700         (when parent
3701           (delq thread parent)))
3702       (if (gnus-summary-insert-subject id header)
3703           ;; Set the (possibly) new article number in the data structure.
3704           (gnus-data-set-number data (gnus-id-to-article id))
3705         (setcar thread old)
3706         nil))))
3707
3708 (defun gnus-rebuild-thread (id &optional line)
3709   "Rebuild the thread containing ID.
3710 If LINE, insert the rebuilt thread starting on line LINE."
3711   (let ((buffer-read-only nil)
3712         old-pos current thread data)
3713     (if (not gnus-show-threads)
3714         (setq thread (list (car (gnus-id-to-thread id))))
3715       ;; Get the thread this article is part of.
3716       (setq thread (gnus-remove-thread id)))
3717     (setq old-pos (gnus-point-at-bol))
3718     (setq current (save-excursion
3719                     (and (re-search-backward "[\r\n]" nil t)
3720                          (gnus-summary-article-number))))
3721     ;; If this is a gathered thread, we have to go some re-gathering.
3722     (when (stringp (car thread))
3723       (let ((subject (car thread))
3724             roots thr)
3725         (setq thread (cdr thread))
3726         (while thread
3727           (unless (memq (setq thr (gnus-id-to-thread
3728                                    (gnus-root-id
3729                                     (mail-header-id (caar thread)))))
3730                         roots)
3731             (push thr roots))
3732           (setq thread (cdr thread)))
3733         ;; We now have all (unique) roots.
3734         (if (= (length roots) 1)
3735             ;; All the loose roots are now one solid root.
3736             (setq thread (car roots))
3737           (setq thread (cons subject (gnus-sort-threads roots))))))
3738     (let (threads)
3739       ;; We then insert this thread into the summary buffer.
3740       (when line
3741         (goto-char (point-min))
3742         (forward-line (1- line)))
3743       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3744         (if gnus-show-threads
3745             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3746           (gnus-summary-prepare-unthreaded thread))
3747         (setq data (nreverse gnus-newsgroup-data))
3748         (setq threads gnus-newsgroup-threads))
3749       ;; We splice the new data into the data structure.
3750       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3751       ;;!!! then we want to insert at the beginning of the buffer.
3752       ;;!!! That happens to be true with Gnus now, but that may
3753       ;;!!! change in the future.  Perhaps.
3754       (gnus-data-enter-list
3755        (if line nil current) data (- (point) old-pos))
3756       (setq gnus-newsgroup-threads
3757             (nconc threads gnus-newsgroup-threads))
3758       (gnus-data-compute-positions))))
3759
3760 (defun gnus-number-to-header (number)
3761   "Return the header for article NUMBER."
3762   (let ((headers gnus-newsgroup-headers))
3763     (while (and headers
3764                 (not (= number (mail-header-number (car headers)))))
3765       (pop headers))
3766     (when headers
3767       (car headers))))
3768
3769 (defun gnus-parent-headers (in-headers &optional generation)
3770   "Return the headers of the GENERATIONeth parent of HEADERS."
3771   (unless generation
3772     (setq generation 1))
3773   (let ((parent t)
3774         (headers in-headers)
3775         references)
3776     (while (and parent
3777                 (not (zerop generation))
3778                 (setq references (mail-header-references headers)))
3779       (setq headers (if (and references
3780                              (setq parent (gnus-parent-id references)))
3781                         (car (gnus-id-to-thread parent))
3782                       nil))
3783       (decf generation))
3784     (and (not (eq headers in-headers))
3785          headers)))
3786
3787 (defun gnus-id-to-thread (id)
3788   "Return the (sub-)thread where ID appears."
3789   (gnus-gethash id gnus-newsgroup-dependencies))
3790
3791 (defun gnus-id-to-article (id)
3792   "Return the article number of ID."
3793   (let ((thread (gnus-id-to-thread id)))
3794     (when (and thread
3795                (car thread))
3796       (mail-header-number (car thread)))))
3797
3798 (defun gnus-id-to-header (id)
3799   "Return the article headers of ID."
3800   (car (gnus-id-to-thread id)))
3801
3802 (defun gnus-article-displayed-root-p (article)
3803   "Say whether ARTICLE is a root(ish) article."
3804   (let ((level (gnus-summary-thread-level article))
3805         (refs (mail-header-references  (gnus-summary-article-header article)))
3806         particle)
3807     (cond
3808      ((null level) nil)
3809      ((zerop level) t)
3810      ((null refs) t)
3811      ((null (gnus-parent-id refs)) t)
3812      ((and (= 1 level)
3813            (null (setq particle (gnus-id-to-article
3814                                  (gnus-parent-id refs))))
3815            (null (gnus-summary-thread-level particle)))))))
3816
3817 (defun gnus-root-id (id)
3818   "Return the id of the root of the thread where ID appears."
3819   (let (last-id prev)
3820     (while (and id (setq prev (car (gnus-id-to-thread id))))
3821       (setq last-id id
3822             id (gnus-parent-id (mail-header-references prev))))
3823     last-id))
3824
3825 (defun gnus-articles-in-thread (thread)
3826   "Return the list of articles in THREAD."
3827   (cons (mail-header-number (car thread))
3828         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3829
3830 (defun gnus-remove-thread (id &optional dont-remove)
3831   "Remove the thread that has ID in it."
3832   (let (headers thread last-id)
3833     ;; First go up in this thread until we find the root.
3834     (setq last-id (gnus-root-id id)
3835           headers (message-flatten-list (gnus-id-to-thread last-id)))
3836     ;; We have now found the real root of this thread.  It might have
3837     ;; been gathered into some loose thread, so we have to search
3838     ;; through the threads to find the thread we wanted.
3839     (let ((threads gnus-newsgroup-threads)
3840           sub)
3841       (while threads
3842         (setq sub (car threads))
3843         (if (stringp (car sub))
3844             ;; This is a gathered thread, so we look at the roots
3845             ;; below it to find whether this article is in this
3846             ;; gathered root.
3847             (progn
3848               (setq sub (cdr sub))
3849               (while sub
3850                 (when (member (caar sub) headers)
3851                   (setq thread (car threads)
3852                         threads nil
3853                         sub nil))
3854                 (setq sub (cdr sub))))
3855           ;; It's an ordinary thread, so we check it.
3856           (when (eq (car sub) (car headers))
3857             (setq thread sub
3858                   threads nil)))
3859         (setq threads (cdr threads)))
3860       ;; If this article is in no thread, then it's a root.
3861       (if thread
3862           (unless dont-remove
3863             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3864         (setq thread (gnus-id-to-thread last-id)))
3865       (when thread
3866         (prog1
3867             thread                      ; We return this thread.
3868           (unless dont-remove
3869             (if (stringp (car thread))
3870                 (progn
3871                   ;; If we use dummy roots, then we have to remove the
3872                   ;; dummy root as well.
3873                   (when (eq gnus-summary-make-false-root 'dummy)
3874                     ;; We go to the dummy root by going to
3875                     ;; the first sub-"thread", and then one line up.
3876                     (gnus-summary-goto-article
3877                      (mail-header-number (caadr thread)))
3878                     (forward-line -1)
3879                     (gnus-delete-line)
3880                     (gnus-data-compute-positions))
3881                   (setq thread (cdr thread))
3882                   (while thread
3883                     (gnus-remove-thread-1 (car thread))
3884                     (setq thread (cdr thread))))
3885               (gnus-remove-thread-1 thread))))))))
3886
3887 (defun gnus-remove-thread-1 (thread)
3888   "Remove the thread THREAD recursively."
3889   (let ((number (mail-header-number (pop thread)))
3890         d)
3891     (setq thread (reverse thread))
3892     (while thread
3893       (gnus-remove-thread-1 (pop thread)))
3894     (when (setq d (gnus-data-find number))
3895       (goto-char (gnus-data-pos d))
3896       (gnus-summary-show-thread)
3897       (gnus-data-remove
3898        number
3899        (- (gnus-point-at-bol)
3900           (prog1
3901               (1+ (gnus-point-at-eol))
3902             (gnus-delete-line)))))))
3903
3904 (defun gnus-sort-threads-1 (threads func)
3905   (sort (mapcar (lambda (thread)
3906                   (cons (car thread)
3907                         (and (cdr thread)
3908                              (gnus-sort-threads-1 (cdr thread) func))))
3909                 threads) func))
3910
3911 (defun gnus-sort-threads (threads)
3912   "Sort THREADS."
3913   (if (not gnus-thread-sort-functions)
3914       threads
3915     (gnus-message 8 "Sorting threads...")
3916     (prog1
3917         (gnus-sort-threads-1
3918          threads
3919          (gnus-make-sort-function gnus-thread-sort-functions))
3920       (gnus-message 8 "Sorting threads...done"))))
3921
3922 (defun gnus-sort-articles (articles)
3923   "Sort ARTICLES."
3924   (when gnus-article-sort-functions
3925     (gnus-message 7 "Sorting articles...")
3926     (prog1
3927         (setq gnus-newsgroup-headers
3928               (sort articles (gnus-make-sort-function
3929                               gnus-article-sort-functions)))
3930       (gnus-message 7 "Sorting articles...done"))))
3931
3932 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3933 (defmacro gnus-thread-header (thread)
3934   "Return header of first article in THREAD.
3935 Note that THREAD must never, ever be anything else than a variable -
3936 using some other form will lead to serious barfage."
3937   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3938   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3939   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3940         (vector thread) 2))
3941
3942 (defsubst gnus-article-sort-by-number (h1 h2)
3943   "Sort articles by article number."
3944   (< (mail-header-number h1)
3945      (mail-header-number h2)))
3946
3947 (defun gnus-thread-sort-by-number (h1 h2)
3948   "Sort threads by root article number."
3949   (gnus-article-sort-by-number
3950    (gnus-thread-header h1) (gnus-thread-header h2)))
3951
3952 (defsubst gnus-article-sort-by-lines (h1 h2)
3953   "Sort articles by article Lines header."
3954   (< (mail-header-lines h1)
3955      (mail-header-lines h2)))
3956
3957 (defun gnus-thread-sort-by-lines (h1 h2)
3958   "Sort threads by root article Lines header."
3959   (gnus-article-sort-by-lines
3960    (gnus-thread-header h1) (gnus-thread-header h2)))
3961
3962 (defsubst gnus-article-sort-by-chars (h1 h2)
3963   "Sort articles by octet length."
3964   (< (mail-header-chars h1)
3965      (mail-header-chars h2)))
3966
3967 (defun gnus-thread-sort-by-chars (h1 h2)
3968   "Sort threads by root article octet length."
3969   (gnus-article-sort-by-chars
3970    (gnus-thread-header h1) (gnus-thread-header h2)))
3971
3972 (defsubst gnus-article-sort-by-author (h1 h2)
3973   "Sort articles by root author."
3974   (string-lessp
3975    (let ((extract (funcall
3976                    gnus-extract-address-components
3977                    (mail-header-from h1))))
3978      (or (car extract) (cadr extract) ""))
3979    (let ((extract (funcall
3980                    gnus-extract-address-components
3981                    (mail-header-from h2))))
3982      (or (car extract) (cadr extract) ""))))
3983
3984 (defun gnus-thread-sort-by-author (h1 h2)
3985   "Sort threads by root author."
3986   (gnus-article-sort-by-author
3987    (gnus-thread-header h1)  (gnus-thread-header h2)))
3988
3989 (defsubst gnus-article-sort-by-subject (h1 h2)
3990   "Sort articles by root subject."
3991   (string-lessp
3992    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3993    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3994
3995 (defun gnus-thread-sort-by-subject (h1 h2)
3996   "Sort threads by root subject."
3997   (gnus-article-sort-by-subject
3998    (gnus-thread-header h1) (gnus-thread-header h2)))
3999
4000 (defsubst gnus-article-sort-by-date (h1 h2)
4001   "Sort articles by root article date."
4002   (time-less-p
4003    (gnus-date-get-time (mail-header-date h1))
4004    (gnus-date-get-time (mail-header-date h2))))
4005
4006 (defun gnus-thread-sort-by-date (h1 h2)
4007   "Sort threads by root article date."
4008   (gnus-article-sort-by-date
4009    (gnus-thread-header h1) (gnus-thread-header h2)))
4010
4011 (defsubst gnus-article-sort-by-score (h1 h2)
4012   "Sort articles by root article score.
4013 Unscored articles will be counted as having a score of zero."
4014   (> (or (cdr (assq (mail-header-number h1)
4015                     gnus-newsgroup-scored))
4016          gnus-summary-default-score 0)
4017      (or (cdr (assq (mail-header-number h2)
4018                     gnus-newsgroup-scored))
4019          gnus-summary-default-score 0)))
4020
4021 (defun gnus-thread-sort-by-score (h1 h2)
4022   "Sort threads by root article score."
4023   (gnus-article-sort-by-score
4024    (gnus-thread-header h1) (gnus-thread-header h2)))
4025
4026 (defun gnus-thread-sort-by-total-score (h1 h2)
4027   "Sort threads by the sum of all scores in the thread.
4028 Unscored articles will be counted as having a score of zero."
4029   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4030
4031 (defun gnus-thread-total-score (thread)
4032   ;; This function find the total score of THREAD.
4033   (cond ((null thread)
4034          0)
4035         ((consp thread)
4036          (if (stringp (car thread))
4037              (apply gnus-thread-score-function 0
4038                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4039            (gnus-thread-total-score-1 thread)))
4040         (t
4041          (gnus-thread-total-score-1 (list thread)))))
4042
4043 (defun gnus-thread-total-score-1 (root)
4044   ;; This function find the total score of the thread below ROOT.
4045   (setq root (car root))
4046   (apply gnus-thread-score-function
4047          (or (append
4048               (mapcar 'gnus-thread-total-score
4049                       (cdr (gnus-id-to-thread (mail-header-id root))))
4050               (when (> (mail-header-number root) 0)
4051                 (list (or (cdr (assq (mail-header-number root)
4052                                      gnus-newsgroup-scored))
4053                           gnus-summary-default-score 0))))
4054              (list gnus-summary-default-score)
4055              '(0))))
4056
4057 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4058 (defvar gnus-tmp-prev-subject nil)
4059 (defvar gnus-tmp-false-parent nil)
4060 (defvar gnus-tmp-root-expunged nil)
4061 (defvar gnus-tmp-dummy-line nil)
4062
4063 (eval-when-compile (defvar gnus-tmp-header))
4064 (defun gnus-extra-header (type &optional header)
4065   "Return the extra header of TYPE."
4066   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4067       ""))
4068
4069 (defun gnus-summary-prepare-threads (threads)
4070   "Prepare summary buffer from THREADS and indentation LEVEL.
4071 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4072 or a straight list of headers."
4073   (gnus-message 7 "Generating summary...")
4074
4075   (setq gnus-newsgroup-threads threads)
4076   (beginning-of-line)
4077
4078   (let ((gnus-tmp-level 0)
4079         (default-score (or gnus-summary-default-score 0))
4080         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4081         thread number subject stack state gnus-tmp-gathered beg-match
4082         new-roots gnus-tmp-new-adopts thread-end
4083         gnus-tmp-header gnus-tmp-unread
4084         gnus-tmp-replied gnus-tmp-subject-or-nil
4085         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4086         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4087         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
4088
4089     (setq gnus-tmp-prev-subject nil)
4090
4091     (if (vectorp (car threads))
4092         ;; If this is a straight (sic) list of headers, then a
4093         ;; threaded summary display isn't required, so we just create
4094         ;; an unthreaded one.
4095         (gnus-summary-prepare-unthreaded threads)
4096
4097       ;; Do the threaded display.
4098
4099       (while (or threads stack gnus-tmp-new-adopts new-roots)
4100
4101         (if (and (= gnus-tmp-level 0)
4102                  (or (not stack)
4103                      (= (caar stack) 0))
4104                  (not gnus-tmp-false-parent)
4105                  (or gnus-tmp-new-adopts new-roots))
4106             (if gnus-tmp-new-adopts
4107                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4108                       thread (list (car gnus-tmp-new-adopts))
4109                       gnus-tmp-header (caar thread)
4110                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4111               (when new-roots
4112                 (setq thread (list (car new-roots))
4113                       gnus-tmp-header (caar thread)
4114                       new-roots (cdr new-roots))))
4115
4116           (if threads
4117               ;; If there are some threads, we do them before the
4118               ;; threads on the stack.
4119               (setq thread threads
4120                     gnus-tmp-header (caar thread))
4121             ;; There were no current threads, so we pop something off
4122             ;; the stack.
4123             (setq state (car stack)
4124                   gnus-tmp-level (car state)
4125                   thread (cdr state)
4126                   stack (cdr stack)
4127                   gnus-tmp-header (caar thread))))
4128
4129         (setq gnus-tmp-false-parent nil)
4130         (setq gnus-tmp-root-expunged nil)
4131         (setq thread-end nil)
4132
4133         (if (stringp gnus-tmp-header)
4134             ;; The header is a dummy root.
4135             (cond
4136              ((eq gnus-summary-make-false-root 'adopt)
4137               ;; We let the first article adopt the rest.
4138               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4139                                                (cddar thread)))
4140               (setq gnus-tmp-gathered
4141                     (nconc (mapcar
4142                             (lambda (h) (mail-header-number (car h)))
4143                             (cddar thread))
4144                            gnus-tmp-gathered))
4145               (setq thread (cons (list (caar thread)
4146                                        (cadar thread))
4147                                  (cdr thread)))
4148               (setq gnus-tmp-level -1
4149                     gnus-tmp-false-parent t))
4150              ((eq gnus-summary-make-false-root 'empty)
4151               ;; We print adopted articles with empty subject fields.
4152               (setq gnus-tmp-gathered
4153                     (nconc (mapcar
4154                             (lambda (h) (mail-header-number (car h)))
4155                             (cddar thread))
4156                            gnus-tmp-gathered))
4157               (setq gnus-tmp-level -1))
4158              ((eq gnus-summary-make-false-root 'dummy)
4159               ;; We remember that we probably want to output a dummy
4160               ;; root.
4161               (setq gnus-tmp-dummy-line gnus-tmp-header)
4162               (setq gnus-tmp-prev-subject gnus-tmp-header))
4163              (t
4164               ;; We do not make a root for the gathered
4165               ;; sub-threads at all.
4166               (setq gnus-tmp-level -1)))
4167
4168           (setq number (mail-header-number gnus-tmp-header)
4169                 subject (mail-header-subject gnus-tmp-header))
4170
4171           (cond
4172            ;; If the thread has changed subject, we might want to make
4173            ;; this subthread into a root.
4174            ((and (null gnus-thread-ignore-subject)
4175                  (not (zerop gnus-tmp-level))
4176                  gnus-tmp-prev-subject
4177                  (not (inline
4178                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4179             (setq new-roots (nconc new-roots (list (car thread)))
4180                   thread-end t
4181                   gnus-tmp-header nil))
4182            ;; If the article lies outside the current limit,
4183            ;; then we do not display it.
4184            ((not (memq number gnus-newsgroup-limit))
4185             (setq gnus-tmp-gathered
4186                   (nconc (mapcar
4187                           (lambda (h) (mail-header-number (car h)))
4188                           (cdar thread))
4189                          gnus-tmp-gathered))
4190             (setq gnus-tmp-new-adopts (if (cdar thread)
4191                                           (append gnus-tmp-new-adopts
4192                                                   (cdar thread))
4193                                         gnus-tmp-new-adopts)
4194                   thread-end t
4195                   gnus-tmp-header nil)
4196             (when (zerop gnus-tmp-level)
4197               (setq gnus-tmp-root-expunged t)))
4198            ;; Perhaps this article is to be marked as read?
4199            ((and gnus-summary-mark-below
4200                  (< (or (cdr (assq number gnus-newsgroup-scored))
4201                         default-score)
4202                     gnus-summary-mark-below)
4203                  ;; Don't touch sparse articles.
4204                  (not (gnus-summary-article-sparse-p number))
4205                  (not (gnus-summary-article-ancient-p number)))
4206             (setq gnus-newsgroup-unreads
4207                   (delq number gnus-newsgroup-unreads))
4208             (if gnus-newsgroup-auto-expire
4209                 (push number gnus-newsgroup-expirable)
4210               (push (cons number gnus-low-score-mark)
4211                     gnus-newsgroup-reads))))
4212
4213           (when gnus-tmp-header
4214             ;; We may have an old dummy line to output before this
4215             ;; article.
4216             (when (and gnus-tmp-dummy-line
4217                        (gnus-subject-equal
4218                         gnus-tmp-dummy-line
4219                         (mail-header-subject gnus-tmp-header)))
4220               (gnus-summary-insert-dummy-line
4221                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4222               (setq gnus-tmp-dummy-line nil))
4223
4224             ;; Compute the mark.
4225             (setq gnus-tmp-unread (gnus-article-mark number))
4226
4227             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4228                                   gnus-tmp-header gnus-tmp-level)
4229                   gnus-newsgroup-data)
4230
4231             ;; Actually insert the line.
4232             (setq
4233              gnus-tmp-subject-or-nil
4234              (cond
4235               ((and gnus-thread-ignore-subject
4236                     gnus-tmp-prev-subject
4237                     (not (inline (gnus-subject-equal
4238                                   gnus-tmp-prev-subject subject))))
4239                subject)
4240               ((zerop gnus-tmp-level)
4241                (if (and (eq gnus-summary-make-false-root 'empty)
4242                         (memq number gnus-tmp-gathered)
4243                         gnus-tmp-prev-subject
4244                         (inline (gnus-subject-equal
4245                                  gnus-tmp-prev-subject subject)))
4246                    gnus-summary-same-subject
4247                  subject))
4248               (t gnus-summary-same-subject)))
4249             (if (and (eq gnus-summary-make-false-root 'adopt)
4250                      (= gnus-tmp-level 1)
4251                      (memq number gnus-tmp-gathered))
4252                 (setq gnus-tmp-opening-bracket ?\<
4253                       gnus-tmp-closing-bracket ?\>)
4254               (setq gnus-tmp-opening-bracket ?\[
4255                     gnus-tmp-closing-bracket ?\]))
4256             (setq
4257              gnus-tmp-indentation
4258              (aref gnus-thread-indent-array gnus-tmp-level)
4259              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4260              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4261                                 gnus-summary-default-score 0)
4262              gnus-tmp-score-char
4263              (if (or (null gnus-summary-default-score)
4264                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4265                          gnus-summary-zcore-fuzz))
4266                  ?  ;Whitespace
4267                (if (< gnus-tmp-score gnus-summary-default-score)
4268                    gnus-score-below-mark gnus-score-over-mark))
4269              gnus-tmp-replied
4270              (cond ((memq number gnus-newsgroup-processable)
4271                     gnus-process-mark)
4272                    ((memq number gnus-newsgroup-cached)
4273                     gnus-cached-mark)
4274                    ((memq number gnus-newsgroup-replied)
4275                     gnus-replied-mark)
4276                    ((memq number gnus-newsgroup-forwarded)
4277                     gnus-forwarded-mark)
4278                    ((memq number gnus-newsgroup-saved)
4279                     gnus-saved-mark)
4280                    (t gnus-no-mark))
4281              gnus-tmp-from (mail-header-from gnus-tmp-header)
4282              gnus-tmp-name
4283              (cond
4284               ((string-match "<[^>]+> *$" gnus-tmp-from)
4285                (setq beg-match (match-beginning 0))
4286                (or (and (string-match "^\".+\"" gnus-tmp-from)
4287                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4288                    (substring gnus-tmp-from 0 beg-match)))
4289               ((string-match "(.+)" gnus-tmp-from)
4290                (substring gnus-tmp-from
4291                           (1+ (match-beginning 0)) (1- (match-end 0))))
4292               (t gnus-tmp-from)))
4293             (when (string= gnus-tmp-name "")
4294               (setq gnus-tmp-name gnus-tmp-from))
4295             (unless (numberp gnus-tmp-lines)
4296               (setq gnus-tmp-lines -1))
4297             (when (= gnus-tmp-lines -1)
4298               (setq gnus-tmp-lines "?"))
4299             (gnus-put-text-property
4300              (point)
4301              (progn (eval gnus-summary-line-format-spec) (point))
4302              'gnus-number number)
4303             (when gnus-visual-p
4304               (forward-line -1)
4305               (gnus-run-hooks 'gnus-summary-update-hook)
4306               (forward-line 1))
4307
4308             (setq gnus-tmp-prev-subject subject)))
4309
4310         (when (nth 1 thread)
4311           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4312         (incf gnus-tmp-level)
4313         (setq threads (if thread-end nil (cdar thread)))
4314         (unless threads
4315           (setq gnus-tmp-level 0)))))
4316   (gnus-message 7 "Generating summary...done"))
4317
4318 (defun gnus-summary-prepare-unthreaded (headers)
4319   "Generate an unthreaded summary buffer based on HEADERS."
4320   (let (header number mark)
4321
4322     (beginning-of-line)
4323
4324     (while headers
4325       ;; We may have to root out some bad articles...
4326       (when (memq (setq number (mail-header-number
4327                                 (setq header (pop headers))))
4328                   gnus-newsgroup-limit)
4329         ;; Mark article as read when it has a low score.
4330         (when (and gnus-summary-mark-below
4331                    (< (or (cdr (assq number gnus-newsgroup-scored))
4332                           gnus-summary-default-score 0)
4333                       gnus-summary-mark-below)
4334                    (not (gnus-summary-article-ancient-p number)))
4335           (setq gnus-newsgroup-unreads
4336                 (delq number gnus-newsgroup-unreads))
4337           (if gnus-newsgroup-auto-expire
4338               (push number gnus-newsgroup-expirable)
4339             (push (cons number gnus-low-score-mark)
4340                   gnus-newsgroup-reads)))
4341
4342         (setq mark (gnus-article-mark number))
4343         (push (gnus-data-make number mark (1+ (point)) header 0)
4344               gnus-newsgroup-data)
4345         (gnus-summary-insert-line
4346          header 0 number
4347          mark (memq number gnus-newsgroup-replied)
4348          (memq number gnus-newsgroup-expirable)
4349          (mail-header-subject header) nil
4350          (cdr (assq number gnus-newsgroup-scored))
4351          (memq number gnus-newsgroup-processable))))))
4352
4353 (defun gnus-summary-remove-list-identifiers ()
4354   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4355   (let ((regexp (if (consp gnus-list-identifiers)
4356                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4357                   gnus-list-identifiers))
4358         changed subject)
4359     (when regexp
4360       (dolist (header gnus-newsgroup-headers)
4361         (setq subject (mail-header-subject header)
4362               changed nil)
4363         (while (string-match
4364                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4365                 subject)
4366           (setq subject
4367                 (concat (substring subject 0 (match-beginning 2))
4368                         (substring subject (match-end 0)))
4369                 changed t))
4370         (when (and changed
4371                    (string-match
4372                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4373           (setq subject
4374                 (concat (substring subject 0 (match-beginning 1))
4375                         (substring subject (match-end 1)))))
4376         (when changed
4377           (mail-header-set-subject header subject))))))
4378
4379 (defun gnus-fetch-headers (articles)
4380   "Fetch headers of ARTICLES."
4381   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4382     (gnus-message 5 "Fetching headers for %s..." name)
4383     (prog1
4384         (if (eq 'nov
4385                 (setq gnus-headers-retrieved-by
4386                       (gnus-retrieve-headers
4387                        articles gnus-newsgroup-name
4388                        ;; We might want to fetch old headers, but
4389                        ;; not if there is only 1 article.
4390                        (and (or (and
4391                                  (not (eq gnus-fetch-old-headers 'some))
4392                                  (not (numberp gnus-fetch-old-headers)))
4393                                 (> (length articles) 1))
4394                             gnus-fetch-old-headers))))
4395             (gnus-get-newsgroup-headers-xover
4396              articles nil nil gnus-newsgroup-name t)
4397           (gnus-get-newsgroup-headers))
4398       (gnus-message 5 "Fetching headers for %s...done" name))))
4399
4400 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4401   "Select newsgroup GROUP.
4402 If READ-ALL is non-nil, all articles in the group are selected.
4403 If SELECT-ARTICLES, only select those articles from GROUP."
4404   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4405          ;;!!! Dirty hack; should be removed.
4406          (gnus-summary-ignore-duplicates
4407           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4408               t
4409             gnus-summary-ignore-duplicates))
4410          (info (nth 2 entry))
4411          articles fetched-articles cached)
4412
4413     (unless (gnus-check-server
4414              (setq gnus-current-select-method
4415                    (gnus-find-method-for-group group)))
4416       (error "Couldn't open server"))
4417
4418     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4419         (gnus-activate-group group)     ; Or we can activate it...
4420         (progn                          ; Or we bug out.
4421           (when (equal major-mode 'gnus-summary-mode)
4422             (kill-buffer (current-buffer)))
4423           (error "Couldn't activate group %s: %s"
4424                  group (gnus-status-message group))))
4425
4426     (unless (gnus-request-group group t)
4427       (when (equal major-mode 'gnus-summary-mode)
4428         (kill-buffer (current-buffer)))
4429       (error "Couldn't request group %s: %s"
4430              group (gnus-status-message group)))
4431
4432     (setq gnus-newsgroup-name group)
4433     (setq gnus-newsgroup-unselected nil)
4434     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4435     (gnus-summary-setup-default-charset)
4436
4437     ;; Adjust and set lists of article marks.
4438     (when info
4439       (gnus-adjust-marked-articles info))
4440
4441     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4442     (when (gnus-virtual-group-p group)
4443       (setq cached gnus-newsgroup-cached))
4444
4445     (setq gnus-newsgroup-unreads
4446           (gnus-set-difference
4447            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4448            gnus-newsgroup-dormant))
4449
4450     (setq gnus-newsgroup-processable nil)
4451
4452     (gnus-update-read-articles group gnus-newsgroup-unreads)
4453
4454     (if (setq articles select-articles)
4455         (setq gnus-newsgroup-unselected
4456               (gnus-sorted-intersection
4457                gnus-newsgroup-unreads
4458                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4459       (setq articles (gnus-articles-to-read group read-all)))
4460
4461     (cond
4462      ((null articles)
4463       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4464       'quit)
4465      ((eq articles 0) nil)
4466      (t
4467       ;; Init the dependencies hash table.
4468       (setq gnus-newsgroup-dependencies
4469             (gnus-make-hashtable (length articles)))
4470       (gnus-set-global-variables)
4471       ;; Retrieve the headers and read them in.
4472       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4473
4474       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4475       (when cached
4476         (setq gnus-newsgroup-cached cached))
4477
4478       ;; Suppress duplicates?
4479       (when gnus-suppress-duplicates
4480         (gnus-dup-suppress-articles))
4481
4482       ;; Set the initial limit.
4483       (setq gnus-newsgroup-limit (copy-sequence articles))
4484       ;; Remove canceled articles from the list of unread articles.
4485       (setq gnus-newsgroup-unreads
4486             (gnus-set-sorted-intersection
4487              gnus-newsgroup-unreads
4488              (setq fetched-articles
4489                    (mapcar (lambda (headers) (mail-header-number headers))
4490                            gnus-newsgroup-headers))))
4491       ;; Removed marked articles that do not exist.
4492       (gnus-update-missing-marks
4493        (gnus-sorted-complement fetched-articles articles))
4494       ;; We might want to build some more threads first.
4495       (when (and gnus-fetch-old-headers
4496                  (eq gnus-headers-retrieved-by 'nov))
4497         (if (eq gnus-fetch-old-headers 'invisible)
4498             (gnus-build-all-threads)
4499           (gnus-build-old-threads)))
4500       ;; Let the Gnus agent mark articles as read.
4501       (when gnus-agent
4502         (gnus-agent-get-undownloaded-list))
4503       ;; Remove list identifiers from subject
4504       (when gnus-list-identifiers
4505         (gnus-summary-remove-list-identifiers))
4506       ;; Check whether auto-expire is to be done in this group.
4507       (setq gnus-newsgroup-auto-expire
4508             (gnus-group-auto-expirable-p group))
4509       ;; Set up the article buffer now, if necessary.
4510       (unless gnus-single-article-buffer
4511         (gnus-article-setup-buffer))
4512       ;; First and last article in this newsgroup.
4513       (when gnus-newsgroup-headers
4514         (setq gnus-newsgroup-begin
4515               (mail-header-number (car gnus-newsgroup-headers))
4516               gnus-newsgroup-end
4517               (mail-header-number
4518                (gnus-last-element gnus-newsgroup-headers))))
4519       ;; GROUP is successfully selected.
4520       (or gnus-newsgroup-headers t)))))
4521
4522 (defun gnus-articles-to-read (group &optional read-all)
4523   "Find out what articles the user wants to read."
4524   (let* ((articles
4525           ;; Select all articles if `read-all' is non-nil, or if there
4526           ;; are no unread articles.
4527           (if (or read-all
4528                   (and (zerop (length gnus-newsgroup-marked))
4529                        (zerop (length gnus-newsgroup-unreads)))
4530                   (eq (gnus-group-find-parameter group 'display)
4531                       'all))
4532               (or
4533                (gnus-uncompress-range (gnus-active group))
4534                (gnus-cache-articles-in-group group))
4535             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4536                           (copy-sequence gnus-newsgroup-unreads))
4537                   '<)))
4538          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4539          (scored (length scored-list))
4540          (number (length articles))
4541          (marked (+ (length gnus-newsgroup-marked)
4542                     (length gnus-newsgroup-dormant)))
4543          (select
4544           (cond
4545            ((numberp read-all)
4546             read-all)
4547            (t
4548             (condition-case ()
4549                 (cond
4550                  ((and (or (<= scored marked) (= scored number))
4551                        (numberp gnus-large-newsgroup)
4552                        (> number gnus-large-newsgroup))
4553                   (let ((input
4554                          (read-string
4555                           (format
4556                            "How many articles from %s (default %d): "
4557                            (gnus-limit-string gnus-newsgroup-name 35)
4558                            number))))
4559                     (if (string-match "^[ \t]*$" input) number input)))
4560                  ((and (> scored marked) (< scored number)
4561                        (> (- scored number) 20))
4562                   (let ((input
4563                          (read-string
4564                           (format "%s %s (%d scored, %d total): "
4565                                   "How many articles from"
4566                                   group scored number))))
4567                     (if (string-match "^[ \t]*$" input)
4568                         number input)))
4569                  (t number))
4570               (quit
4571                (message "Quit getting the articles to read")
4572                nil))))))
4573     (setq select (if (stringp select) (string-to-number select) select))
4574     (if (or (null select) (zerop select))
4575         select
4576       (if (and (not (zerop scored)) (<= (abs select) scored))
4577           (progn
4578             (setq articles (sort scored-list '<))
4579             (setq number (length articles)))
4580         (setq articles (copy-sequence articles)))
4581
4582       (when (< (abs select) number)
4583         (if (< select 0)
4584             ;; Select the N oldest articles.
4585             (setcdr (nthcdr (1- (abs select)) articles) nil)
4586           ;; Select the N most recent articles.
4587           (setq articles (nthcdr (- number select) articles))))
4588       (setq gnus-newsgroup-unselected
4589             (gnus-sorted-intersection
4590              gnus-newsgroup-unreads
4591              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4592       (when gnus-alter-articles-to-read-function
4593         (setq gnus-newsgroup-unreads
4594               (sort
4595                (funcall gnus-alter-articles-to-read-function
4596                         gnus-newsgroup-name gnus-newsgroup-unreads)
4597                '<)))
4598       articles)))
4599
4600 (defun gnus-killed-articles (killed articles)
4601   (let (out)
4602     (while articles
4603       (when (inline (gnus-member-of-range (car articles) killed))
4604         (push (car articles) out))
4605       (setq articles (cdr articles)))
4606     out))
4607
4608 (defun gnus-uncompress-marks (marks)
4609   "Uncompress the mark ranges in MARKS."
4610   (let ((uncompressed '(score bookmark))
4611         out)
4612     (while marks
4613       (if (memq (caar marks) uncompressed)
4614           (push (car marks) out)
4615         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4616       (setq marks (cdr marks)))
4617     out))
4618
4619 (defun gnus-adjust-marked-articles (info)
4620   "Set all article lists and remove all marks that are no longer valid."
4621   (let* ((marked-lists (gnus-info-marks info))
4622          (active (gnus-active (gnus-info-group info)))
4623          (min (car active))
4624          (max (cdr active))
4625          (types gnus-article-mark-lists)
4626          (uncompressed '(score bookmark killed))
4627          marks var articles article mark)
4628
4629     (while marked-lists
4630       (setq marks (pop marked-lists))
4631       (set (setq var (intern (format "gnus-newsgroup-%s"
4632                                      (car (rassq (setq mark (car marks))
4633                                                  types)))))
4634            (if (memq (car marks) uncompressed) (cdr marks)
4635              (gnus-uncompress-range (cdr marks))))
4636
4637       (setq articles (symbol-value var))
4638
4639       ;; All articles have to be subsets of the active articles.
4640       (cond
4641        ;; Adjust "simple" lists.
4642        ((memq mark '(tick dormant expire reply save))
4643         (while articles
4644           (when (or (< (setq article (pop articles)) min) (> article max))
4645             (set var (delq article (symbol-value var))))))
4646        ;; Adjust assocs.
4647        ((memq mark uncompressed)
4648         (when (not (listp (cdr (symbol-value var))))
4649           (set var (list (symbol-value var))))
4650         (when (not (listp (cdr articles)))
4651           (setq articles (list articles)))
4652         (while articles
4653           (when (or (not (consp (setq article (pop articles))))
4654                     (< (car article) min)
4655                     (> (car article) max))
4656             (set var (delq article (symbol-value var))))))))))
4657
4658 (defun gnus-update-missing-marks (missing)
4659   "Go through the list of MISSING articles and remove them from the mark lists."
4660   (when missing
4661     (let ((types gnus-article-mark-lists)
4662           var m)
4663       ;; Go through all types.
4664       (while types
4665         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4666         (when (symbol-value var)
4667           ;; This list has articles.  So we delete all missing articles
4668           ;; from it.
4669           (setq m missing)
4670           (while m
4671             (set var (delq (pop m) (symbol-value var)))))))))
4672
4673 (defun gnus-update-marks ()
4674   "Enter the various lists of marked articles into the newsgroup info list."
4675   (let ((types gnus-article-mark-lists)
4676         (info (gnus-get-info gnus-newsgroup-name))
4677         (uncompressed '(score bookmark killed))
4678         type list newmarked symbol delta-marks)
4679     (when info
4680       ;; Add all marks lists to the list of marks lists.
4681       (while (setq type (pop types))
4682         (setq list (symbol-value
4683                     (setq symbol
4684                           (intern (format "gnus-newsgroup-%s"
4685                                           (car type))))))
4686
4687         (when list
4688           ;; Get rid of the entries of the articles that have the
4689           ;; default score.
4690           (when (and (eq (cdr type) 'score)
4691                      gnus-save-score
4692                      list)
4693             (let* ((arts list)
4694                    (prev (cons nil list))
4695                    (all prev))
4696               (while arts
4697                 (if (or (not (consp (car arts)))
4698                         (= (cdar arts) gnus-summary-default-score))
4699                     (setcdr prev (cdr arts))
4700                   (setq prev arts))
4701                 (setq arts (cdr arts)))
4702               (setq list (cdr all)))))
4703
4704         (unless (memq (cdr type) uncompressed)
4705           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4706
4707         (when (gnus-check-backend-function
4708                'request-set-mark gnus-newsgroup-name)
4709           ;; propagate flags to server, with the following exceptions:
4710           ;; uncompressed:s are not proper flags (they are cons cells)
4711           ;; cache is a internal gnus flag
4712           ;; download are local to one gnus installation (well)
4713           ;; unsend are for nndraft groups only
4714           ;; xxx: generality of this?  this suits nnimap anyway
4715           (unless (memq (cdr type) (append '(cache download unsend)
4716                                            uncompressed))
4717             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4718                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4719                    (add (gnus-remove-from-range
4720                          (gnus-copy-sequence list) old)))
4721               (when add
4722                 (push (list add 'add (list (cdr type))) delta-marks))
4723               (when del
4724                 (push (list del 'del (list (cdr type))) delta-marks)))))
4725
4726         (when list
4727           (push (cons (cdr type) list) newmarked)))
4728
4729       (when delta-marks
4730         (unless (gnus-check-group gnus-newsgroup-name)
4731           (error "Can't open server for %s" gnus-newsgroup-name))
4732         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4733
4734       ;; Enter these new marks into the info of the group.
4735       (if (nthcdr 3 info)
4736           (setcar (nthcdr 3 info) newmarked)
4737         ;; Add the marks lists to the end of the info.
4738         (when newmarked
4739           (setcdr (nthcdr 2 info) (list newmarked))))
4740
4741       ;; Cut off the end of the info if there's nothing else there.
4742       (let ((i 5))
4743         (while (and (> i 2)
4744                     (not (nth i info)))
4745           (when (nthcdr (decf i) info)
4746             (setcdr (nthcdr i info) nil)))))))
4747
4748 (defun gnus-set-mode-line (where)
4749   "Set the mode line of the article or summary buffers.
4750 If WHERE is `summary', the summary mode line format will be used."
4751   ;; Is this mode line one we keep updated?
4752   (when (and (memq where gnus-updated-mode-lines)
4753              (symbol-value
4754               (intern (format "gnus-%s-mode-line-format-spec" where))))
4755     (let (mode-string)
4756       (save-excursion
4757         ;; We evaluate this in the summary buffer since these
4758         ;; variables are buffer-local to that buffer.
4759         (set-buffer gnus-summary-buffer)
4760         ;; We bind all these variables that are used in the `eval' form
4761         ;; below.
4762         (let* ((mformat (symbol-value
4763                          (intern
4764                           (format "gnus-%s-mode-line-format-spec" where))))
4765                (gnus-tmp-group-name (gnus-group-decoded-name 
4766                                      gnus-newsgroup-name))
4767                (gnus-tmp-article-number (or gnus-current-article 0))
4768                (gnus-tmp-unread gnus-newsgroup-unreads)
4769                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4770                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4771                (gnus-tmp-unread-and-unselected
4772                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4773                             (zerop gnus-tmp-unselected))
4774                        "")
4775                       ((zerop gnus-tmp-unselected)
4776                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4777                       (t (format "{%d(+%d) more}"
4778                                  gnus-tmp-unread-and-unticked
4779                                  gnus-tmp-unselected))))
4780                (gnus-tmp-subject
4781                 (if (and gnus-current-headers
4782                          (vectorp gnus-current-headers))
4783                     (gnus-mode-string-quote
4784                      (mail-header-subject gnus-current-headers))
4785                   ""))
4786                bufname-length max-len
4787                gnus-tmp-header);; passed as argument to any user-format-funcs
4788           (setq mode-string (eval mformat))
4789           (setq bufname-length (if (string-match "%b" mode-string)
4790                                    (- (length
4791                                        (buffer-name
4792                                         (if (eq where 'summary)
4793                                             nil
4794                                           (get-buffer gnus-article-buffer))))
4795                                       2)
4796                                  0))
4797           (setq max-len (max 4 (if gnus-mode-non-string-length
4798                                    (- (window-width)
4799                                       gnus-mode-non-string-length
4800                                       bufname-length)
4801                                  (length mode-string))))
4802           ;; We might have to chop a bit of the string off...
4803           (when (> (length mode-string) max-len)
4804             (setq mode-string
4805                   (concat (truncate-string-to-width mode-string (- max-len 3))
4806                           "...")))
4807           ;; Pad the mode string a bit.
4808           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4809       ;; Update the mode line.
4810       (setq mode-line-buffer-identification
4811             (gnus-mode-line-buffer-identification (list mode-string)))
4812       (set-buffer-modified-p t))))
4813
4814 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4815   "Go through the HEADERS list and add all Xrefs to a hash table.
4816 The resulting hash table is returned, or nil if no Xrefs were found."
4817   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4818          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4819          (xref-hashtb (gnus-make-hashtable))
4820          start group entry number xrefs header)
4821     (while headers
4822       (setq header (pop headers))
4823       (when (and (setq xrefs (mail-header-xref header))
4824                  (not (memq (setq number (mail-header-number header))
4825                             unreads)))
4826         (setq start 0)
4827         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4828           (setq start (match-end 0))
4829           (setq group (if prefix
4830                           (concat prefix (substring xrefs (match-beginning 1)
4831                                                     (match-end 1)))
4832                         (substring xrefs (match-beginning 1) (match-end 1))))
4833           (setq number
4834                 (string-to-int (substring xrefs (match-beginning 2)
4835                                           (match-end 2))))
4836           (if (setq entry (gnus-gethash group xref-hashtb))
4837               (setcdr entry (cons number (cdr entry)))
4838             (gnus-sethash group (cons number nil) xref-hashtb)))))
4839     (and start xref-hashtb)))
4840
4841 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4842   "Look through all the headers and mark the Xrefs as read."
4843   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4844         name entry info xref-hashtb idlist method nth4)
4845     (save-excursion
4846       (set-buffer gnus-group-buffer)
4847       (when (setq xref-hashtb
4848                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4849         (mapatoms
4850          (lambda (group)
4851            (unless (string= from-newsgroup (setq name (symbol-name group)))
4852              (setq idlist (symbol-value group))
4853              ;; Dead groups are not updated.
4854              (and (prog1
4855                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4856                             info (nth 2 entry))
4857                     (when (stringp (setq nth4 (gnus-info-method info)))
4858                       (setq nth4 (gnus-server-to-method nth4))))
4859                   ;; Only do the xrefs if the group has the same
4860                   ;; select method as the group we have just read.
4861                   (or (gnus-methods-equal-p
4862                        nth4 (gnus-find-method-for-group from-newsgroup))
4863                       virtual
4864                       (equal nth4 (setq method (gnus-find-method-for-group
4865                                                 from-newsgroup)))
4866                       (and (equal (car nth4) (car method))
4867                            (equal (nth 1 nth4) (nth 1 method))))
4868                   gnus-use-cross-reference
4869                   (or (not (eq gnus-use-cross-reference t))
4870                       virtual
4871                       ;; Only do cross-references on subscribed
4872                       ;; groups, if that is what is wanted.
4873                       (<= (gnus-info-level info) gnus-level-subscribed))
4874                   (gnus-group-make-articles-read name idlist))))
4875          xref-hashtb)))))
4876
4877 (defun gnus-compute-read-articles (group articles)
4878   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4879          (info (nth 2 entry))
4880          (active (gnus-active group))
4881          ninfo)
4882     (when entry
4883       ;; First peel off all invalid article numbers.
4884       (when active
4885         (let ((ids articles)
4886               id first)
4887           (while (setq id (pop ids))
4888             (when (and first (> id (cdr active)))
4889               ;; We'll end up in this situation in one particular
4890               ;; obscure situation.  If you re-scan a group and get
4891               ;; a new article that is cross-posted to a different
4892               ;; group that has not been re-scanned, you might get
4893               ;; crossposted article that has a higher number than
4894               ;; Gnus believes possible.  So we re-activate this
4895               ;; group as well.  This might mean doing the
4896               ;; crossposting thingy will *increase* the number
4897               ;; of articles in some groups.  Tsk, tsk.
4898               (setq active (or (gnus-activate-group group) active)))
4899             (when (or (> id (cdr active))
4900                       (< id (car active)))
4901               (setq articles (delq id articles))))))
4902       ;; If the read list is nil, we init it.
4903       (if (and active
4904                (null (gnus-info-read info))
4905                (> (car active) 1))
4906           (setq ninfo (cons 1 (1- (car active))))
4907         (setq ninfo (gnus-info-read info)))
4908       ;; Then we add the read articles to the range.
4909       (gnus-add-to-range
4910        ninfo (setq articles (sort articles '<))))))
4911
4912 (defun gnus-group-make-articles-read (group articles)
4913   "Update the info of GROUP to say that ARTICLES are read."
4914   (let* ((num 0)
4915          (entry (gnus-gethash group gnus-newsrc-hashtb))
4916          (info (nth 2 entry))
4917          (active (gnus-active group))
4918          range)
4919     (when entry
4920       (setq range (gnus-compute-read-articles group articles))
4921       (save-excursion
4922         (set-buffer gnus-group-buffer)
4923         (gnus-undo-register
4924           `(progn
4925              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4926              (gnus-info-set-read ',info ',(gnus-info-read info))
4927              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4928              (gnus-group-update-group ,group t))))
4929       ;; Add the read articles to the range.
4930       (gnus-info-set-read info range)
4931       ;; Then we have to re-compute how many unread
4932       ;; articles there are in this group.
4933       (when active
4934         (cond
4935          ((not range)
4936           (setq num (- (1+ (cdr active)) (car active))))
4937          ((not (listp (cdr range)))
4938           (setq num (- (cdr active) (- (1+ (cdr range))
4939                                        (car range)))))
4940          (t
4941           (while range
4942             (if (numberp (car range))
4943                 (setq num (1+ num))
4944               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4945             (setq range (cdr range)))
4946           (setq num (- (cdr active) num))))
4947         ;; Update the number of unread articles.
4948         (setcar entry num)
4949         ;; Update the group buffer.
4950         (gnus-group-update-group group t)))))
4951
4952 (defvar gnus-newsgroup-none-id 0)
4953
4954 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4955   (let ((cur nntp-server-buffer)
4956         (dependencies
4957          (or dependencies
4958              (save-excursion (set-buffer gnus-summary-buffer)
4959                              gnus-newsgroup-dependencies)))
4960         headers id end ref
4961         (mail-parse-charset gnus-newsgroup-charset)
4962         (mail-parse-ignored-charsets
4963          (save-excursion (condition-case nil
4964                              (set-buffer gnus-summary-buffer)
4965                            (error))
4966                          gnus-newsgroup-ignored-charsets)))
4967     (save-excursion
4968       (set-buffer nntp-server-buffer)
4969       ;; Translate all TAB characters into SPACE characters.
4970       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4971       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4972       (gnus-run-hooks 'gnus-parse-headers-hook)
4973       (let ((case-fold-search t)
4974             in-reply-to header p lines chars)
4975         (goto-char (point-min))
4976         ;; Search to the beginning of the next header.  Error messages
4977         ;; do not begin with 2 or 3.
4978         (while (re-search-forward "^[23][0-9]+ " nil t)
4979           (setq id nil
4980                 ref nil)
4981           ;; This implementation of this function, with nine
4982           ;; search-forwards instead of the one re-search-forward and
4983           ;; a case (which basically was the old function) is actually
4984           ;; about twice as fast, even though it looks messier.  You
4985           ;; can't have everything, I guess.  Speed and elegance
4986           ;; doesn't always go hand in hand.
4987           (setq
4988            header
4989            (vector
4990             ;; Number.
4991             (prog1
4992                 (read cur)
4993               (end-of-line)
4994               (setq p (point))
4995               (narrow-to-region (point)
4996                                 (or (and (search-forward "\n.\n" nil t)
4997                                          (- (point) 2))
4998                                     (point))))
4999             ;; Subject.
5000             (progn
5001               (goto-char p)
5002               (if (search-forward "\nsubject:" nil t)
5003                   (funcall gnus-decode-encoded-word-function
5004                            (nnheader-header-value))
5005                 "(none)"))
5006             ;; From.
5007             (progn
5008               (goto-char p)
5009               (if (search-forward "\nfrom:" nil t)
5010                   (funcall gnus-decode-encoded-word-function
5011                            (nnheader-header-value))
5012                 "(nobody)"))
5013             ;; Date.
5014             (progn
5015               (goto-char p)
5016               (if (search-forward "\ndate:" nil t)
5017                   (nnheader-header-value) ""))
5018             ;; Message-ID.
5019             (progn
5020               (goto-char p)
5021               (setq id (if (re-search-forward
5022                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5023                            ;; We do it this way to make sure the Message-ID
5024                            ;; is (somewhat) syntactically valid.
5025                            (buffer-substring (match-beginning 1)
5026                                              (match-end 1))
5027                          ;; If there was no message-id, we just fake one
5028                          ;; to make subsequent routines simpler.
5029                          (nnheader-generate-fake-message-id))))
5030             ;; References.
5031             (progn
5032               (goto-char p)
5033               (if (search-forward "\nreferences:" nil t)
5034                   (progn
5035                     (setq end (point))
5036                     (prog1
5037                         (nnheader-header-value)
5038                       (setq ref
5039                             (buffer-substring
5040                              (progn
5041                                (end-of-line)
5042                                (search-backward ">" end t)
5043                                (1+ (point)))
5044                              (progn
5045                                (search-backward "<" end t)
5046                                (point))))))
5047                 ;; Get the references from the in-reply-to header if there
5048                 ;; were no references and the in-reply-to header looks
5049                 ;; promising.
5050                 (if (and (search-forward "\nin-reply-to:" nil t)
5051                          (setq in-reply-to (nnheader-header-value))
5052                          (string-match "<[^>]+>" in-reply-to))
5053                     (let (ref2)
5054                       (setq ref (substring in-reply-to (match-beginning 0)
5055                                            (match-end 0)))
5056                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5057                         (setq ref2 (substring in-reply-to (match-beginning 0)
5058                                               (match-end 0)))
5059                         (when (> (length ref2) (length ref))
5060                           (setq ref ref2)))
5061                       ref)
5062                   (setq ref nil))))
5063             ;; Chars.
5064             (progn
5065               (goto-char p)
5066               (if (search-forward "\nchars: " nil t)
5067                   (if (numberp (setq chars (ignore-errors (read cur))))
5068                       chars -1)
5069                 -1))
5070             ;; Lines.
5071             (progn
5072               (goto-char p)
5073               (if (search-forward "\nlines: " nil t)
5074                   (if (numberp (setq lines (ignore-errors (read cur))))
5075                       lines -1)
5076                 -1))
5077             ;; Xref.
5078             (progn
5079               (goto-char p)
5080               (and (search-forward "\nxref:" nil t)
5081                    (nnheader-header-value)))
5082             ;; Extra.
5083             (when gnus-extra-headers
5084               (let ((extra gnus-extra-headers)
5085                     out)
5086                 (while extra
5087                   (goto-char p)
5088                   (when (search-forward
5089                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5090                     (push (cons (car extra) (nnheader-header-value))
5091                           out))
5092                   (pop extra))
5093                 out))))
5094           (when (equal id ref)
5095             (setq ref nil))
5096
5097           (when gnus-alter-header-function
5098             (funcall gnus-alter-header-function header)
5099             (setq id (mail-header-id header)
5100                   ref (gnus-parent-id (mail-header-references header))))
5101
5102           (when (setq header
5103                       (gnus-dependencies-add-header
5104                        header dependencies force-new))
5105             (push header headers))
5106           (goto-char (point-max))
5107           (widen))
5108         (nreverse headers)))))
5109
5110 ;; Goes through the xover lines and returns a list of vectors
5111 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5112                                                   force-new dependencies
5113                                                   group also-fetch-heads)
5114   "Parse the news overview data in the server buffer.
5115 Return a list of headers that match SEQUENCE (see
5116 `nntp-retrieve-headers')."
5117   ;; Get the Xref when the users reads the articles since most/some
5118   ;; NNTP servers do not include Xrefs when using XOVER.
5119   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5120   (let ((mail-parse-charset gnus-newsgroup-charset)
5121         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5122         (cur nntp-server-buffer)
5123         (dependencies (or dependencies gnus-newsgroup-dependencies))
5124         (allp (cond
5125                ((eq gnus-read-all-available-headers t)
5126                 t)
5127                ((stringp gnus-read-all-available-headers)
5128                 (string-match gnus-read-all-available-headers group))
5129                (t
5130                 nil)))
5131         number headers header)
5132     (save-excursion
5133       (set-buffer nntp-server-buffer)
5134       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5135       ;; Allow the user to mangle the headers before parsing them.
5136       (gnus-run-hooks 'gnus-parse-headers-hook)
5137       (goto-char (point-min))
5138       (while (not (eobp))
5139         (condition-case ()
5140             (while (and (or sequence allp)
5141                         (not (eobp)))
5142               (setq number (read cur))
5143               (when (not allp)
5144                 (while (and sequence
5145                             (< (car sequence) number))
5146                   (setq sequence (cdr sequence))))
5147               (when (and (or allp
5148                              (and sequence
5149                                   (eq number (car sequence))))
5150                          (progn
5151                            (setq sequence (cdr sequence))
5152                            (setq header (inline
5153                                           (gnus-nov-parse-line
5154                                            number dependencies force-new)))))
5155                 (push header headers))
5156               (forward-line 1))
5157           (error
5158            (gnus-error 4 "Strange nov line (%d)"
5159                        (count-lines (point-min) (point)))))
5160         (forward-line 1))
5161       ;; A common bug in inn is that if you have posted an article and
5162       ;; then retrieves the active file, it will answer correctly --
5163       ;; the new article is included.  However, a NOV entry for the
5164       ;; article may not have been generated yet, so this may fail.
5165       ;; We work around this problem by retrieving the last few
5166       ;; headers using HEAD.
5167       (if (or (not also-fetch-heads)
5168               (not sequence))
5169           ;; We (probably) got all the headers.
5170           (nreverse headers)
5171         (let ((gnus-nov-is-evil t))
5172           (nconc
5173            (nreverse headers)
5174            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5175              (gnus-get-newsgroup-headers))))))))
5176
5177 (defun gnus-article-get-xrefs ()
5178   "Fill in the Xref value in `gnus-current-headers', if necessary.
5179 This is meant to be called in `gnus-article-internal-prepare-hook'."
5180   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5181                                  gnus-current-headers)))
5182     (or (not gnus-use-cross-reference)
5183         (not headers)
5184         (and (mail-header-xref headers)
5185              (not (string= (mail-header-xref headers) "")))
5186         (let ((case-fold-search t)
5187               xref)
5188           (save-restriction
5189             (nnheader-narrow-to-headers)
5190             (goto-char (point-min))
5191             (when (or (and (not (eobp))
5192                            (eq (downcase (char-after)) ?x)
5193                            (looking-at "Xref:"))
5194                       (search-forward "\nXref:" nil t))
5195               (goto-char (1+ (match-end 0)))
5196               (setq xref (buffer-substring (point)
5197                                            (progn (end-of-line) (point))))
5198               (mail-header-set-xref headers xref)))))))
5199
5200 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5201   "Find article ID and insert the summary line for that article.
5202 OLD-HEADER can either be a header or a line number to insert
5203 the subject line on."
5204   (let* ((line (and (numberp old-header) old-header))
5205          (old-header (and (vectorp old-header) old-header))
5206          (header (cond ((and old-header use-old-header)
5207                         old-header)
5208                        ((and (numberp id)
5209                              (gnus-number-to-header id))
5210                         (gnus-number-to-header id))
5211                        (t
5212                         (gnus-read-header id))))
5213          (number (and (numberp id) id))
5214          d)
5215     (when header
5216       ;; Rebuild the thread that this article is part of and go to the
5217       ;; article we have fetched.
5218       (when (and (not gnus-show-threads)
5219                  old-header)
5220         (when (and number
5221                    (setq d (gnus-data-find (mail-header-number old-header))))
5222           (goto-char (gnus-data-pos d))
5223           (gnus-data-remove
5224            number
5225            (- (gnus-point-at-bol)
5226               (prog1
5227                   (1+ (gnus-point-at-eol))
5228                 (gnus-delete-line))))))
5229       (when old-header
5230         (mail-header-set-number header (mail-header-number old-header)))
5231       (setq gnus-newsgroup-sparse
5232             (delq (setq number (mail-header-number header))
5233                   gnus-newsgroup-sparse))
5234       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5235       (push number gnus-newsgroup-limit)
5236       (gnus-rebuild-thread (mail-header-id header) line)
5237       (gnus-summary-goto-subject number nil t))
5238     (when (and (numberp number)
5239                (> number 0))
5240       ;; We have to update the boundaries even if we can't fetch the
5241       ;; article if ID is a number -- so that the next `P' or `N'
5242       ;; command will fetch the previous (or next) article even
5243       ;; if the one we tried to fetch this time has been canceled.
5244       (when (> number gnus-newsgroup-end)
5245         (setq gnus-newsgroup-end number))
5246       (when (< number gnus-newsgroup-begin)
5247         (setq gnus-newsgroup-begin number))
5248       (setq gnus-newsgroup-unselected
5249             (delq number gnus-newsgroup-unselected)))
5250     ;; Report back a success?
5251     (and header (mail-header-number header))))
5252
5253 ;;; Process/prefix in the summary buffer
5254
5255 (defun gnus-summary-work-articles (n)
5256   "Return a list of articles to be worked upon.
5257 The prefix argument, the list of process marked articles, and the
5258 current article will be taken into consideration."
5259   (save-excursion
5260     (set-buffer gnus-summary-buffer)
5261     (cond
5262      (n
5263       ;; A numerical prefix has been given.
5264       (setq n (prefix-numeric-value n))
5265       (let ((backward (< n 0))
5266             (n (abs (prefix-numeric-value n)))
5267             articles article)
5268         (save-excursion
5269           (while
5270               (and (> n 0)
5271                    (push (setq article (gnus-summary-article-number))
5272                          articles)
5273                    (if backward
5274                        (gnus-summary-find-prev nil article)
5275                      (gnus-summary-find-next nil article)))
5276             (decf n)))
5277         (nreverse articles)))
5278      ((and (gnus-region-active-p) (mark))
5279       (message "region active")
5280       ;; Work on the region between point and mark.
5281       (let ((max (max (point) (mark)))
5282             articles article)
5283         (save-excursion
5284           (goto-char (min (min (point) (mark))))
5285           (while
5286               (and
5287                (push (setq article (gnus-summary-article-number)) articles)
5288                (gnus-summary-find-next nil article)
5289                (< (point) max)))
5290           (nreverse articles))))
5291      (gnus-newsgroup-processable
5292       ;; There are process-marked articles present.
5293       ;; Save current state.
5294       (gnus-summary-save-process-mark)
5295       ;; Return the list.
5296       (reverse gnus-newsgroup-processable))
5297      (t
5298       ;; Just return the current article.
5299       (list (gnus-summary-article-number))))))
5300
5301 (defmacro gnus-summary-iterate (arg &rest forms)
5302   "Iterate over the process/prefixed articles and do FORMS.
5303 ARG is the interactive prefix given to the command.  FORMS will be
5304 executed with point over the summary line of the articles."
5305   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5306     `(let ((,articles (gnus-summary-work-articles ,arg)))
5307        (while ,articles
5308          (gnus-summary-goto-subject (car ,articles))
5309          ,@forms
5310          (pop ,articles)))))
5311
5312 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5313 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5314
5315 (defun gnus-summary-save-process-mark ()
5316   "Push the current set of process marked articles on the stack."
5317   (interactive)
5318   (push (copy-sequence gnus-newsgroup-processable)
5319         gnus-newsgroup-process-stack))
5320
5321 (defun gnus-summary-kill-process-mark ()
5322   "Push the current set of process marked articles on the stack and unmark."
5323   (interactive)
5324   (gnus-summary-save-process-mark)
5325   (gnus-summary-unmark-all-processable))
5326
5327 (defun gnus-summary-yank-process-mark ()
5328   "Pop the last process mark state off the stack and restore it."
5329   (interactive)
5330   (unless gnus-newsgroup-process-stack
5331     (error "Empty mark stack"))
5332   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5333
5334 (defun gnus-summary-process-mark-set (set)
5335   "Make SET into the current process marked articles."
5336   (gnus-summary-unmark-all-processable)
5337   (while set
5338     (gnus-summary-set-process-mark (pop set))))
5339
5340 ;;; Searching and stuff
5341
5342 (defun gnus-summary-search-group (&optional backward use-level)
5343   "Search for next unread newsgroup.
5344 If optional argument BACKWARD is non-nil, search backward instead."
5345   (save-excursion
5346     (set-buffer gnus-group-buffer)
5347     (when (gnus-group-search-forward
5348            backward nil (if use-level (gnus-group-group-level) nil))
5349       (gnus-group-group-name))))
5350
5351 (defun gnus-summary-best-group (&optional exclude-group)
5352   "Find the name of the best unread group.
5353 If EXCLUDE-GROUP, do not go to this group."
5354   (save-excursion
5355     (set-buffer gnus-group-buffer)
5356     (save-excursion
5357       (gnus-group-best-unread-group exclude-group))))
5358
5359 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5360   (if backward (gnus-summary-find-prev)
5361     (let* ((dummy (gnus-summary-article-intangible-p))
5362            (article (or article (gnus-summary-article-number)))
5363            (arts (gnus-data-find-list article))
5364            result)
5365       (when (and (not dummy)
5366                  (or (not gnus-summary-check-current)
5367                      (not unread)
5368                      (not (gnus-data-unread-p (car arts)))))
5369         (setq arts (cdr arts)))
5370       (when (setq result
5371                   (if unread
5372                       (progn
5373                         (while arts
5374                           (when (or (and undownloaded
5375                                          (eq gnus-undownloaded-mark
5376                                              (gnus-data-mark (car arts))))
5377                                     (gnus-data-unread-p (car arts)))
5378                             (setq result (car arts)
5379                                   arts nil))
5380                           (setq arts (cdr arts)))
5381                         result)
5382                     (car arts)))
5383         (goto-char (gnus-data-pos result))
5384         (gnus-data-number result)))))
5385
5386 (defun gnus-summary-find-prev (&optional unread article)
5387   (let* ((eobp (eobp))
5388          (article (or article (gnus-summary-article-number)))
5389          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5390          result)
5391     (when (and (not eobp)
5392                (or (not gnus-summary-check-current)
5393                    (not unread)
5394                    (not (gnus-data-unread-p (car arts)))))
5395       (setq arts (cdr arts)))
5396     (when (setq result
5397                 (if unread
5398                     (progn
5399                       (while arts
5400                         (when (gnus-data-unread-p (car arts))
5401                           (setq result (car arts)
5402                                 arts nil))
5403                         (setq arts (cdr arts)))
5404                       result)
5405                   (car arts)))
5406       (goto-char (gnus-data-pos result))
5407       (gnus-data-number result))))
5408
5409 (defun gnus-summary-find-subject (subject &optional unread backward article)
5410   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5411          (article (or article (gnus-summary-article-number)))
5412          (articles (gnus-data-list backward))
5413          (arts (gnus-data-find-list article articles))
5414          result)
5415     (when (or (not gnus-summary-check-current)
5416               (not unread)
5417               (not (gnus-data-unread-p (car arts))))
5418       (setq arts (cdr arts)))
5419     (while arts
5420       (and (or (not unread)
5421                (gnus-data-unread-p (car arts)))
5422            (vectorp (gnus-data-header (car arts)))
5423            (gnus-subject-equal
5424             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5425            (setq result (car arts)
5426                  arts nil))
5427       (setq arts (cdr arts)))
5428     (and result
5429          (goto-char (gnus-data-pos result))
5430          (gnus-data-number result))))
5431
5432 (defun gnus-summary-search-forward (&optional unread subject backward)
5433   "Search forward for an article.
5434 If UNREAD, look for unread articles.  If SUBJECT, look for
5435 articles with that subject.  If BACKWARD, search backward instead."
5436   (cond (subject (gnus-summary-find-subject subject unread backward))
5437         (backward (gnus-summary-find-prev unread))
5438         (t (gnus-summary-find-next unread))))
5439
5440 (defun gnus-recenter (&optional n)
5441   "Center point in window and redisplay frame.
5442 Also do horizontal recentering."
5443   (interactive "P")
5444   (when (and gnus-auto-center-summary
5445              (not (eq gnus-auto-center-summary 'vertical)))
5446     (gnus-horizontal-recenter))
5447   (recenter n))
5448
5449 (defun gnus-summary-recenter ()
5450   "Center point in the summary window.
5451 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5452 displayed, no centering will be performed."
5453   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5454   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5455   (interactive)
5456   (let* ((top (cond ((< (window-height) 4) 0)
5457                     ((< (window-height) 7) 1)
5458                     (t (if (numberp gnus-auto-center-summary)
5459                            gnus-auto-center-summary
5460                          2))))
5461          (height (1- (window-height)))
5462          (bottom (save-excursion (goto-char (point-max))
5463                                  (forward-line (- height))
5464                                  (point)))
5465          (window (get-buffer-window (current-buffer))))
5466     ;; The user has to want it.
5467     (when gnus-auto-center-summary
5468       (when (get-buffer-window gnus-article-buffer)
5469         ;; Only do recentering when the article buffer is displayed,
5470         ;; Set the window start to either `bottom', which is the biggest
5471         ;; possible valid number, or the second line from the top,
5472         ;; whichever is the least.
5473         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5474           (if (> bottom top-pos)
5475               ;; Keep the second line from the top visible
5476               (set-window-start window top-pos t)
5477             ;; Try to keep the bottom line visible; if it's partially
5478             ;; obscured, either scroll one more line to make it fully
5479             ;; visible, or revert to using TOP-POS.
5480             (save-excursion
5481               (goto-char (point-max))
5482               (forward-line -1)
5483               (let ((last-line-start (point)))
5484                 (goto-char bottom)
5485                 (set-window-start window (point) t)
5486                 (when (not (pos-visible-in-window-p last-line-start window))
5487                   (forward-line 1)
5488                   (set-window-start window (min (point) top-pos) t)))))))
5489       ;; Do horizontal recentering while we're at it.
5490       (when (and (get-buffer-window (current-buffer) t)
5491                  (not (eq gnus-auto-center-summary 'vertical)))
5492         (let ((selected (selected-window)))
5493           (select-window (get-buffer-window (current-buffer) t))
5494           (gnus-summary-position-point)
5495           (gnus-horizontal-recenter)
5496           (select-window selected))))))
5497
5498 (defun gnus-summary-jump-to-group (newsgroup)
5499   "Move point to NEWSGROUP in group mode buffer."
5500   ;; Keep update point of group mode buffer if visible.
5501   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5502       (save-window-excursion
5503         ;; Take care of tree window mode.
5504         (when (get-buffer-window gnus-group-buffer)
5505           (pop-to-buffer gnus-group-buffer))
5506         (gnus-group-jump-to-group newsgroup))
5507     (save-excursion
5508       ;; Take care of tree window mode.
5509       (if (get-buffer-window gnus-group-buffer)
5510           (pop-to-buffer gnus-group-buffer)
5511         (set-buffer gnus-group-buffer))
5512       (gnus-group-jump-to-group newsgroup))))
5513
5514 ;; This function returns a list of article numbers based on the
5515 ;; difference between the ranges of read articles in this group and
5516 ;; the range of active articles.
5517 (defun gnus-list-of-unread-articles (group)
5518   (let* ((read (gnus-info-read (gnus-get-info group)))
5519          (active (or (gnus-active group) (gnus-activate-group group)))
5520          (last (cdr active))
5521          first nlast unread)
5522     ;; If none are read, then all are unread.
5523     (if (not read)
5524         (setq first (car active))
5525       ;; If the range of read articles is a single range, then the
5526       ;; first unread article is the article after the last read
5527       ;; article.  Sounds logical, doesn't it?
5528       (if (and (not (listp (cdr read)))
5529                (or (< (car read) (car active))
5530                    (progn (setq read (list read))
5531                           nil)))
5532           (setq first (max (car active) (1+ (cdr read))))
5533         ;; `read' is a list of ranges.
5534         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5535                                   (caar read)))
5536                   1)
5537           (setq first (car active)))
5538         (while read
5539           (when first
5540             (while (< first nlast)
5541               (push first unread)
5542               (setq first (1+ first))))
5543           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5544           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5545           (setq read (cdr read)))))
5546     ;; And add the last unread articles.
5547     (while (<= first last)
5548       (push first unread)
5549       (setq first (1+ first)))
5550     ;; Return the list of unread articles.
5551     (delq 0 (nreverse unread))))
5552
5553 (defun gnus-list-of-read-articles (group)
5554   "Return a list of unread, unticked and non-dormant articles."
5555   (let* ((info (gnus-get-info group))
5556          (marked (gnus-info-marks info))
5557          (active (gnus-active group)))
5558     (and info active
5559          (gnus-set-difference
5560           (gnus-sorted-complement
5561            (gnus-uncompress-range active)
5562            (gnus-list-of-unread-articles group))
5563           (append
5564            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5565            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5566
5567 ;; Various summary commands
5568
5569 (defun gnus-summary-select-article-buffer ()
5570   "Reconfigure windows to show article buffer."
5571   (interactive)
5572   (if (not (gnus-buffer-live-p gnus-article-buffer))
5573       (error "There is no article buffer for this summary buffer")
5574     (gnus-configure-windows 'article)
5575     (select-window (get-buffer-window gnus-article-buffer))))
5576
5577 (defun gnus-summary-universal-argument (arg)
5578   "Perform any operation on all articles that are process/prefixed."
5579   (interactive "P")
5580   (let ((articles (gnus-summary-work-articles arg))
5581         func article)
5582     (if (eq
5583          (setq
5584           func
5585           (key-binding
5586            (read-key-sequence
5587             (substitute-command-keys
5588              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5589          'undefined)
5590         (gnus-error 1 "Undefined key")
5591       (save-excursion
5592         (while articles
5593           (gnus-summary-goto-subject (setq article (pop articles)))
5594           (let (gnus-newsgroup-processable)
5595             (command-execute func))
5596           (gnus-summary-remove-process-mark article)))))
5597   (gnus-summary-position-point))
5598
5599 (defun gnus-summary-toggle-truncation (&optional arg)
5600   "Toggle truncation of summary lines.
5601 With arg, turn line truncation on iff arg is positive."
5602   (interactive "P")
5603   (setq truncate-lines
5604         (if (null arg) (not truncate-lines)
5605           (> (prefix-numeric-value arg) 0)))
5606   (redraw-display))
5607
5608 (defun gnus-summary-reselect-current-group (&optional all rescan)
5609   "Exit and then reselect the current newsgroup.
5610 The prefix argument ALL means to select all articles."
5611   (interactive "P")
5612   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5613     (error "Ephemeral groups can't be reselected"))
5614   (let ((current-subject (gnus-summary-article-number))
5615         (group gnus-newsgroup-name))
5616     (setq gnus-newsgroup-begin nil)
5617     (gnus-summary-exit)
5618     ;; We have to adjust the point of group mode buffer because
5619     ;; point was moved to the next unread newsgroup by exiting.
5620     (gnus-summary-jump-to-group group)
5621     (when rescan
5622       (save-excursion
5623         (gnus-group-get-new-news-this-group 1)))
5624     (gnus-group-read-group all t)
5625     (gnus-summary-goto-subject current-subject nil t)))
5626
5627 (defun gnus-summary-rescan-group (&optional all)
5628   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5629   (interactive "P")
5630   (gnus-summary-reselect-current-group all t))
5631
5632 (defun gnus-summary-update-info (&optional non-destructive)
5633   (save-excursion
5634     (let ((group gnus-newsgroup-name))
5635       (when group
5636         (when gnus-newsgroup-kill-headers
5637           (setq gnus-newsgroup-killed
5638                 (gnus-compress-sequence
5639                  (nconc
5640                   (gnus-set-sorted-intersection
5641                    (gnus-uncompress-range gnus-newsgroup-killed)
5642                    (setq gnus-newsgroup-unselected
5643                          (sort gnus-newsgroup-unselected '<)))
5644                   (setq gnus-newsgroup-unreads
5645                         (sort gnus-newsgroup-unreads '<)))
5646                  t)))
5647         (unless (listp (cdr gnus-newsgroup-killed))
5648           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5649         (let ((headers gnus-newsgroup-headers))
5650           ;; Set the new ranges of read articles.
5651           (save-excursion
5652             (set-buffer gnus-group-buffer)
5653             (gnus-undo-force-boundary))
5654           (gnus-update-read-articles
5655            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5656           ;; Set the current article marks.
5657           (let ((gnus-newsgroup-scored
5658                  (if (and (not gnus-save-score)
5659                           (not non-destructive))
5660                      nil
5661                    gnus-newsgroup-scored)))
5662             (save-excursion
5663               (gnus-update-marks)))
5664           ;; Do the cross-ref thing.
5665           (when gnus-use-cross-reference
5666             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5667           ;; Do not switch windows but change the buffer to work.
5668           (set-buffer gnus-group-buffer)
5669           (unless (gnus-ephemeral-group-p group)
5670             (gnus-group-update-group group)))))))
5671
5672 (defun gnus-summary-save-newsrc (&optional force)
5673   "Save the current number of read/marked articles in the dribble buffer.
5674 The dribble buffer will then be saved.
5675 If FORCE (the prefix), also save the .newsrc file(s)."
5676   (interactive "P")
5677   (gnus-summary-update-info t)
5678   (if force
5679       (gnus-save-newsrc-file)
5680     (gnus-dribble-save)))
5681
5682 (defun gnus-summary-exit (&optional temporary)
5683   "Exit reading current newsgroup, and then return to group selection mode.
5684 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5685   (interactive)
5686   (gnus-set-global-variables)
5687   (when (gnus-buffer-live-p gnus-article-buffer)
5688     (save-excursion
5689       (set-buffer gnus-article-buffer)
5690       (mm-destroy-parts gnus-article-mime-handles)
5691       ;; Set it to nil for safety reason.
5692       (setq gnus-article-mime-handle-alist nil)
5693       (setq gnus-article-mime-handles nil)))
5694   (gnus-kill-save-kill-buffer)
5695   (gnus-async-halt-prefetch)
5696   (let* ((group gnus-newsgroup-name)
5697          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5698          (mode major-mode)
5699          (group-point nil)
5700          (buf (current-buffer)))
5701     (unless quit-config
5702       ;; Do adaptive scoring, and possibly save score files.
5703       (when gnus-newsgroup-adaptive
5704         (gnus-score-adaptive))
5705       (when gnus-use-scoring
5706         (gnus-score-save)))
5707     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5708     ;; If we have several article buffers, we kill them at exit.
5709     (unless gnus-single-article-buffer
5710       (gnus-kill-buffer gnus-original-article-buffer)
5711       (setq gnus-article-current nil))
5712     (when gnus-use-cache
5713       (gnus-cache-possibly-remove-articles)
5714       (gnus-cache-save-buffers))
5715     (gnus-async-prefetch-remove-group group)
5716     (when gnus-suppress-duplicates
5717       (gnus-dup-enter-articles))
5718     (when gnus-use-trees
5719       (gnus-tree-close group))
5720     (when gnus-use-cache
5721       (gnus-cache-write-active))
5722     ;; Remove entries for this group.
5723     (nnmail-purge-split-history (gnus-group-real-name group))
5724     ;; Make all changes in this group permanent.
5725     (unless quit-config
5726       (gnus-run-hooks 'gnus-exit-group-hook)
5727       (gnus-summary-update-info))
5728     (gnus-close-group group)
5729     ;; Make sure where we were, and go to next newsgroup.
5730     (set-buffer gnus-group-buffer)
5731     (unless quit-config
5732       (gnus-group-jump-to-group group))
5733     (gnus-run-hooks 'gnus-summary-exit-hook)
5734     (unless (or quit-config
5735                 ;; If this group has disappeared from the summary
5736                 ;; buffer, don't skip forwards.
5737                 (not (string= group (gnus-group-group-name))))
5738       (gnus-group-next-unread-group 1))
5739     (setq group-point (point))
5740     (if temporary
5741         nil                             ;Nothing to do.
5742       ;; If we have several article buffers, we kill them at exit.
5743       (unless gnus-single-article-buffer
5744         (gnus-kill-buffer gnus-article-buffer)
5745         (gnus-kill-buffer gnus-original-article-buffer)
5746         (setq gnus-article-current nil))
5747       (set-buffer buf)
5748       (if (not gnus-kill-summary-on-exit)
5749           (progn
5750             (gnus-deaden-summary)
5751             (setq mode nil))
5752         ;; We set all buffer-local variables to nil.  It is unclear why
5753         ;; this is needed, but if we don't, buffer-local variables are
5754         ;; not garbage-collected, it seems.  This would the lead to en
5755         ;; ever-growing Emacs.
5756         (gnus-summary-clear-local-variables)
5757         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5758           (gnus-summary-clear-local-variables))
5759         (when (get-buffer gnus-article-buffer)
5760           (bury-buffer gnus-article-buffer))
5761         ;; We clear the global counterparts of the buffer-local
5762         ;; variables as well, just to be on the safe side.
5763         (set-buffer gnus-group-buffer)
5764         (gnus-summary-clear-local-variables)
5765         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5766           (gnus-summary-clear-local-variables)))
5767       (setq gnus-current-select-method gnus-select-method)
5768       (pop-to-buffer gnus-group-buffer)
5769       (if (not quit-config)
5770           (progn
5771             (goto-char group-point)
5772             (gnus-configure-windows 'group 'force))
5773         (gnus-handle-ephemeral-exit quit-config))
5774       ;; Return to group mode buffer.
5775       (when (eq mode 'gnus-summary-mode)
5776         (gnus-kill-buffer buf))
5777       ;; Clear the current group name.
5778       (unless quit-config
5779         (setq gnus-newsgroup-name nil)))))
5780
5781 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5782 (defun gnus-summary-exit-no-update (&optional no-questions)
5783   "Quit reading current newsgroup without updating read article info."
5784   (interactive)
5785   (let* ((group gnus-newsgroup-name)
5786          (quit-config (gnus-group-quit-config group)))
5787     (when (or no-questions
5788               gnus-expert-user
5789               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5790       (gnus-async-halt-prefetch)
5791       (mapcar 'funcall
5792               (delq 'gnus-summary-expire-articles
5793                     (copy-sequence gnus-summary-prepare-exit-hook)))
5794       (when (gnus-buffer-live-p gnus-article-buffer)
5795         (save-excursion
5796           (set-buffer gnus-article-buffer)
5797           (mm-destroy-parts gnus-article-mime-handles)
5798           ;; Set it to nil for safety reason.
5799           (setq gnus-article-mime-handle-alist nil)
5800           (setq gnus-article-mime-handles nil)))
5801       ;; If we have several article buffers, we kill them at exit.
5802       (unless gnus-single-article-buffer
5803         (gnus-kill-buffer gnus-article-buffer)
5804         (gnus-kill-buffer gnus-original-article-buffer)
5805         (setq gnus-article-current nil))
5806       (if (not gnus-kill-summary-on-exit)
5807           (gnus-deaden-summary)
5808         (gnus-close-group group)
5809         (gnus-summary-clear-local-variables)
5810         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5811           (gnus-summary-clear-local-variables))
5812         (set-buffer gnus-group-buffer)
5813         (gnus-summary-clear-local-variables)
5814         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5815           (gnus-summary-clear-local-variables))
5816         (when (get-buffer gnus-summary-buffer)
5817           (kill-buffer gnus-summary-buffer)))
5818       (unless gnus-single-article-buffer
5819         (setq gnus-article-current nil))
5820       (when gnus-use-trees
5821         (gnus-tree-close group))
5822       (gnus-async-prefetch-remove-group group)
5823       (when (get-buffer gnus-article-buffer)
5824         (bury-buffer gnus-article-buffer))
5825       ;; Return to the group buffer.
5826       (gnus-configure-windows 'group 'force)
5827       ;; Clear the current group name.
5828       (setq gnus-newsgroup-name nil)
5829       (when (equal (gnus-group-group-name) group)
5830         (gnus-group-next-unread-group 1))
5831       (when quit-config
5832         (gnus-handle-ephemeral-exit quit-config)))))
5833
5834 (defun gnus-handle-ephemeral-exit (quit-config)
5835   "Handle movement when leaving an ephemeral group.
5836 The state which existed when entering the ephemeral is reset."
5837   (if (not (buffer-name (car quit-config)))
5838       (gnus-configure-windows 'group 'force)
5839     (set-buffer (car quit-config))
5840     (cond ((eq major-mode 'gnus-summary-mode)
5841            (gnus-set-global-variables))
5842           ((eq major-mode 'gnus-article-mode)
5843            (save-excursion
5844              ;; The `gnus-summary-buffer' variable may point
5845              ;; to the old summary buffer when using a single
5846              ;; article buffer.
5847              (unless (gnus-buffer-live-p gnus-summary-buffer)
5848                (set-buffer gnus-group-buffer))
5849              (set-buffer gnus-summary-buffer)
5850              (gnus-set-global-variables))))
5851     (if (or (eq (cdr quit-config) 'article)
5852             (eq (cdr quit-config) 'pick))
5853         (progn
5854           ;; The current article may be from the ephemeral group
5855           ;; thus it is best that we reload this article
5856           (gnus-summary-show-article)
5857           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5858               (gnus-configure-windows 'pick 'force)
5859             (gnus-configure-windows (cdr quit-config) 'force)))
5860       (gnus-configure-windows (cdr quit-config) 'force))
5861     (when (eq major-mode 'gnus-summary-mode)
5862       (gnus-summary-next-subject 1 nil t)
5863       (gnus-summary-recenter)
5864       (gnus-summary-position-point))))
5865
5866 ;;; Dead summaries.
5867
5868 (defvar gnus-dead-summary-mode-map nil)
5869
5870 (unless gnus-dead-summary-mode-map
5871   (setq gnus-dead-summary-mode-map (make-keymap))
5872   (suppress-keymap gnus-dead-summary-mode-map)
5873   (substitute-key-definition
5874    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5875   (let ((keys '("\C-d" "\r" "\177" [delete])))
5876     (while keys
5877       (define-key gnus-dead-summary-mode-map
5878         (pop keys) 'gnus-summary-wake-up-the-dead))))
5879
5880 (defvar gnus-dead-summary-mode nil
5881   "Minor mode for Gnus summary buffers.")
5882
5883 (defun gnus-dead-summary-mode (&optional arg)
5884   "Minor mode for Gnus summary buffers."
5885   (interactive "P")
5886   (when (eq major-mode 'gnus-summary-mode)
5887     (make-local-variable 'gnus-dead-summary-mode)
5888     (setq gnus-dead-summary-mode
5889           (if (null arg) (not gnus-dead-summary-mode)
5890             (> (prefix-numeric-value arg) 0)))
5891     (when gnus-dead-summary-mode
5892       (gnus-add-minor-mode
5893        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5894
5895 (defun gnus-deaden-summary ()
5896   "Make the current summary buffer into a dead summary buffer."
5897   ;; Kill any previous dead summary buffer.
5898   (when (and gnus-dead-summary
5899              (buffer-name gnus-dead-summary))
5900     (save-excursion
5901       (set-buffer gnus-dead-summary)
5902       (when gnus-dead-summary-mode
5903         (kill-buffer (current-buffer)))))
5904   ;; Make this the current dead summary.
5905   (setq gnus-dead-summary (current-buffer))
5906   (gnus-dead-summary-mode 1)
5907   (let ((name (buffer-name)))
5908     (when (string-match "Summary" name)
5909       (rename-buffer
5910        (concat (substring name 0 (match-beginning 0)) "Dead "
5911                (substring name (match-beginning 0)))
5912        t)
5913       (bury-buffer))))
5914
5915 (defun gnus-kill-or-deaden-summary (buffer)
5916   "Kill or deaden the summary BUFFER."
5917   (save-excursion
5918     (when (and (buffer-name buffer)
5919                (not gnus-single-article-buffer))
5920       (save-excursion
5921         (set-buffer buffer)
5922         (gnus-kill-buffer gnus-article-buffer)
5923         (gnus-kill-buffer gnus-original-article-buffer)))
5924     (cond (gnus-kill-summary-on-exit
5925            (when (and gnus-use-trees
5926                       (gnus-buffer-exists-p buffer))
5927              (save-excursion
5928                (set-buffer buffer)
5929                (gnus-tree-close gnus-newsgroup-name)))
5930            (gnus-kill-buffer buffer))
5931           ((gnus-buffer-exists-p buffer)
5932            (save-excursion
5933              (set-buffer buffer)
5934              (gnus-deaden-summary))))))
5935
5936 (defun gnus-summary-wake-up-the-dead (&rest args)
5937   "Wake up the dead summary buffer."
5938   (interactive)
5939   (gnus-dead-summary-mode -1)
5940   (let ((name (buffer-name)))
5941     (when (string-match "Dead " name)
5942       (rename-buffer
5943        (concat (substring name 0 (match-beginning 0))
5944                (substring name (match-end 0)))
5945        t)))
5946   (gnus-message 3 "This dead summary is now alive again"))
5947
5948 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5949 (defun gnus-summary-fetch-faq (&optional faq-dir)
5950   "Fetch the FAQ for the current group.
5951 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5952 in."
5953   (interactive
5954    (list
5955     (when current-prefix-arg
5956       (completing-read
5957        "Faq dir: " (and (listp gnus-group-faq-directory)
5958                         (mapcar (lambda (file) (list file))
5959                                 gnus-group-faq-directory))))))
5960   (let (gnus-faq-buffer)
5961     (when (setq gnus-faq-buffer
5962                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5963       (gnus-configure-windows 'summary-faq))))
5964
5965 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5966 (defun gnus-summary-describe-group (&optional force)
5967   "Describe the current newsgroup."
5968   (interactive "P")
5969   (gnus-group-describe-group force gnus-newsgroup-name))
5970
5971 (defun gnus-summary-describe-briefly ()
5972   "Describe summary mode commands briefly."
5973   (interactive)
5974   (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")))
5975
5976 ;; Walking around group mode buffer from summary mode.
5977
5978 (defun gnus-summary-next-group (&optional no-article target-group backward)
5979   "Exit current newsgroup and then select next unread newsgroup.
5980 If prefix argument NO-ARTICLE is non-nil, no article is selected
5981 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5982 previous group instead."
5983   (interactive "P")
5984   ;; Stop pre-fetching.
5985   (gnus-async-halt-prefetch)
5986   (let ((current-group gnus-newsgroup-name)
5987         (current-buffer (current-buffer))
5988         entered)
5989     ;; First we semi-exit this group to update Xrefs and all variables.
5990     ;; We can't do a real exit, because the window conf must remain
5991     ;; the same in case the user is prompted for info, and we don't
5992     ;; want the window conf to change before that...
5993     (gnus-summary-exit t)
5994     (while (not entered)
5995       ;; Then we find what group we are supposed to enter.
5996       (set-buffer gnus-group-buffer)
5997       (gnus-group-jump-to-group current-group)
5998       (setq target-group
5999             (or target-group
6000                 (if (eq gnus-keep-same-level 'best)
6001                     (gnus-summary-best-group gnus-newsgroup-name)
6002                   (gnus-summary-search-group backward gnus-keep-same-level))))
6003       (if (not target-group)
6004           ;; There are no further groups, so we return to the group
6005           ;; buffer.
6006           (progn
6007             (gnus-message 5 "Returning to the group buffer")
6008             (setq entered t)
6009             (when (gnus-buffer-live-p current-buffer)
6010               (set-buffer current-buffer)
6011               (gnus-summary-exit))
6012             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6013         ;; We try to enter the target group.
6014         (gnus-group-jump-to-group target-group)
6015         (let ((unreads (gnus-group-group-unread)))
6016           (if (and (or (eq t unreads)
6017                        (and unreads (not (zerop unreads))))
6018                    (gnus-summary-read-group
6019                     target-group nil no-article
6020                     (and (buffer-name current-buffer) current-buffer)
6021                     nil backward))
6022               (setq entered t)
6023             (setq current-group target-group
6024                   target-group nil)))))))
6025
6026 (defun gnus-summary-prev-group (&optional no-article)
6027   "Exit current newsgroup and then select previous unread newsgroup.
6028 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6029   (interactive "P")
6030   (gnus-summary-next-group no-article nil t))
6031
6032 ;; Walking around summary lines.
6033
6034 (defun gnus-summary-first-subject (&optional unread undownloaded)
6035   "Go to the first unread subject.
6036 If UNREAD is non-nil, go to the first unread article.
6037 Returns the article selected or nil if there are no unread articles."
6038   (interactive "P")
6039   (prog1
6040       (cond
6041        ;; Empty summary.
6042        ((null gnus-newsgroup-data)
6043         (gnus-message 3 "No articles in the group")
6044         nil)
6045        ;; Pick the first article.
6046        ((not unread)
6047         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6048         (gnus-data-number (car gnus-newsgroup-data)))
6049        ;; No unread articles.
6050        ((null gnus-newsgroup-unreads)
6051         (gnus-message 3 "No more unread articles")
6052         nil)
6053        ;; Find the first unread article.
6054        (t
6055         (let ((data gnus-newsgroup-data))
6056           (while (and data
6057                       (and (not (and undownloaded
6058                                      (eq gnus-undownloaded-mark
6059                                          (gnus-data-mark (car data)))))
6060                            (not (gnus-data-unread-p (car data)))))
6061             (setq data (cdr data)))
6062           (when data
6063             (goto-char (gnus-data-pos (car data)))
6064             (gnus-data-number (car data))))))
6065     (gnus-summary-position-point)))
6066
6067 (defun gnus-summary-next-subject (n &optional unread dont-display)
6068   "Go to next N'th summary line.
6069 If N is negative, go to the previous N'th subject line.
6070 If UNREAD is non-nil, only unread articles are selected.
6071 The difference between N and the actual number of steps taken is
6072 returned."
6073   (interactive "p")
6074   (let ((backward (< n 0))
6075         (n (abs n)))
6076     (while (and (> n 0)
6077                 (if backward
6078                     (gnus-summary-find-prev unread)
6079                   (gnus-summary-find-next unread)))
6080       (unless (zerop (setq n (1- n)))
6081         (gnus-summary-show-thread)))
6082     (when (/= 0 n)
6083       (gnus-message 7 "No more%s articles"
6084                     (if unread " unread" "")))
6085     (unless dont-display
6086       (gnus-summary-recenter)
6087       (gnus-summary-position-point))
6088     n))
6089
6090 (defun gnus-summary-next-unread-subject (n)
6091   "Go to next N'th unread summary line."
6092   (interactive "p")
6093   (gnus-summary-next-subject n t))
6094
6095 (defun gnus-summary-prev-subject (n &optional unread)
6096   "Go to previous N'th summary line.
6097 If optional argument UNREAD is non-nil, only unread article is selected."
6098   (interactive "p")
6099   (gnus-summary-next-subject (- n) unread))
6100
6101 (defun gnus-summary-prev-unread-subject (n)
6102   "Go to previous N'th unread summary line."
6103   (interactive "p")
6104   (gnus-summary-next-subject (- n) t))
6105
6106 (defun gnus-summary-goto-subject (article &optional force silent)
6107   "Go the subject line of ARTICLE.
6108 If FORCE, also allow jumping to articles not currently shown."
6109   (interactive "nArticle number: ")
6110   (let ((b (point))
6111         (data (gnus-data-find article)))
6112     ;; We read in the article if we have to.
6113     (and (not data)
6114          force
6115          (gnus-summary-insert-subject
6116           article
6117           (if (or (numberp force) (vectorp force)) force)
6118           t)
6119          (setq data (gnus-data-find article)))
6120     (goto-char b)
6121     (if (not data)
6122         (progn
6123           (unless silent
6124             (gnus-message 3 "Can't find article %d" article))
6125           nil)
6126       (goto-char (gnus-data-pos data))
6127       (gnus-summary-position-point)
6128       article)))
6129
6130 ;; Walking around summary lines with displaying articles.
6131
6132 (defun gnus-summary-expand-window (&optional arg)
6133   "Make the summary buffer take up the entire Emacs frame.
6134 Given a prefix, will force an `article' buffer configuration."
6135   (interactive "P")
6136   (if arg
6137       (gnus-configure-windows 'article 'force)
6138     (gnus-configure-windows 'summary 'force)))
6139
6140 (defun gnus-summary-display-article (article &optional all-header)
6141   "Display ARTICLE in article buffer."
6142   (when (gnus-buffer-live-p gnus-article-buffer)
6143     (with-current-buffer gnus-article-buffer
6144       (mm-enable-multibyte-mule4)))
6145   (gnus-set-global-variables)
6146   (when (gnus-buffer-live-p gnus-article-buffer)
6147     (with-current-buffer gnus-article-buffer
6148       (setq gnus-article-charset gnus-newsgroup-charset)
6149       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6150       (mm-enable-multibyte-mule4)))
6151   (if (null article)
6152       nil
6153     (prog1
6154         (if gnus-summary-display-article-function
6155             (funcall gnus-summary-display-article-function article all-header)
6156           (gnus-article-prepare article all-header))
6157       (gnus-run-hooks 'gnus-select-article-hook)
6158       (when (and gnus-current-article
6159                  (not (zerop gnus-current-article)))
6160         (gnus-summary-goto-subject gnus-current-article))
6161       (gnus-summary-recenter)
6162       (when (and gnus-use-trees gnus-show-threads)
6163         (gnus-possibly-generate-tree article)
6164         (gnus-highlight-selected-tree article))
6165       ;; Successfully display article.
6166       (gnus-article-set-window-start
6167        (cdr (assq article gnus-newsgroup-bookmarks))))))
6168
6169 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6170   "Select the current article.
6171 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6172 non-nil, the article will be re-fetched even if it already present in
6173 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6174 be displayed."
6175   ;; Make sure we are in the summary buffer to work around bbdb bug.
6176   (unless (eq major-mode 'gnus-summary-mode)
6177     (set-buffer gnus-summary-buffer))
6178   (let ((article (or article (gnus-summary-article-number)))
6179         (all-headers (not (not all-headers))) ;Must be T or NIL.
6180         gnus-summary-display-article-function)
6181     (and (not pseudo)
6182          (gnus-summary-article-pseudo-p article)
6183          (error "This is a pseudo-article"))
6184     (save-excursion
6185       (set-buffer gnus-summary-buffer)
6186       (if (or (and gnus-single-article-buffer
6187                    (or (null gnus-current-article)
6188                        (null gnus-article-current)
6189                        (null (get-buffer gnus-article-buffer))
6190                        (not (eq article (cdr gnus-article-current)))
6191                        (not (equal (car gnus-article-current)
6192                                    gnus-newsgroup-name))))
6193               (and (not gnus-single-article-buffer)
6194                    (or (null gnus-current-article)
6195                        (not (eq gnus-current-article article))))
6196               force)
6197           ;; The requested article is different from the current article.
6198           (progn
6199             (gnus-summary-display-article article all-headers)
6200             (when (gnus-buffer-live-p gnus-article-buffer)
6201                (with-current-buffer gnus-article-buffer
6202                 (if (not gnus-article-decoded-p) ;; a local variable
6203                     (mm-disable-multibyte-mule4))))
6204             (when (or all-headers gnus-show-all-headers)
6205               (gnus-article-show-all-headers))
6206             (gnus-article-set-window-start
6207              (cdr (assq article gnus-newsgroup-bookmarks)))
6208             article)
6209         (when (or all-headers gnus-show-all-headers)
6210           (gnus-article-show-all-headers))
6211         'old))))
6212
6213 (defun gnus-summary-force-verify-and-decrypt ()
6214   (interactive)
6215   (let ((mm-verify-option 'known)
6216         (mm-decrypt-option 'known))
6217     (gnus-summary-select-article nil 'force)))
6218
6219 (defun gnus-summary-set-current-mark (&optional current-mark)
6220   "Obsolete function."
6221   nil)
6222
6223 (defun gnus-summary-next-article (&optional unread subject backward push)
6224   "Select the next article.
6225 If UNREAD, only unread articles are selected.
6226 If SUBJECT, only articles with SUBJECT are selected.
6227 If BACKWARD, the previous article is selected instead of the next."
6228   (interactive "P")
6229   (cond
6230    ;; Is there such an article?
6231    ((and (gnus-summary-search-forward unread subject backward)
6232          (or (gnus-summary-display-article (gnus-summary-article-number))
6233              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6234     (gnus-summary-position-point))
6235    ;; If not, we try the first unread, if that is wanted.
6236    ((and subject
6237          gnus-auto-select-same
6238          (gnus-summary-first-unread-article))
6239     (gnus-summary-position-point)
6240     (gnus-message 6 "Wrapped"))
6241    ;; Try to get next/previous article not displayed in this group.
6242    ((and gnus-auto-extend-newsgroup
6243          (not unread) (not subject))
6244     (gnus-summary-goto-article
6245      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6246      nil (count-lines (point-min) (point))))
6247    ;; Go to next/previous group.
6248    (t
6249     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6250       (gnus-summary-jump-to-group gnus-newsgroup-name))
6251     (let ((cmd last-command-char)
6252           (point
6253            (save-excursion
6254              (set-buffer gnus-group-buffer)
6255              (point)))
6256           (group
6257            (if (eq gnus-keep-same-level 'best)
6258                (gnus-summary-best-group gnus-newsgroup-name)
6259              (gnus-summary-search-group backward gnus-keep-same-level))))
6260       ;; For some reason, the group window gets selected.  We change
6261       ;; it back.
6262       (select-window (get-buffer-window (current-buffer)))
6263       ;; Select next unread newsgroup automagically.
6264       (cond
6265        ((or (not gnus-auto-select-next)
6266             (not cmd))
6267         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6268        ((or (eq gnus-auto-select-next 'quietly)
6269             (and (eq gnus-auto-select-next 'slightly-quietly)
6270                  push)
6271             (and (eq gnus-auto-select-next 'almost-quietly)
6272                  (gnus-summary-last-article-p)))
6273         ;; Select quietly.
6274         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6275             (gnus-summary-exit)
6276           (gnus-message 7 "No more%s articles (%s)..."
6277                         (if unread " unread" "")
6278                         (if group (concat "selecting " group)
6279                           "exiting"))
6280           (gnus-summary-next-group nil group backward)))
6281        (t
6282         (when (gnus-key-press-event-p last-input-event)
6283           (gnus-summary-walk-group-buffer
6284            gnus-newsgroup-name cmd unread backward point))))))))
6285
6286 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6287   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6288                       (?\C-p (gnus-group-prev-unread-group 1))))
6289         (cursor-in-echo-area t)
6290         keve key group ended)
6291     (save-excursion
6292       (set-buffer gnus-group-buffer)
6293       (goto-char start)
6294       (setq group
6295             (if (eq gnus-keep-same-level 'best)
6296                 (gnus-summary-best-group gnus-newsgroup-name)
6297               (gnus-summary-search-group backward gnus-keep-same-level))))
6298     (while (not ended)
6299       (gnus-message
6300        5 "No more%s articles%s" (if unread " unread" "")
6301        (if (and group
6302                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6303            (format " (Type %s for %s [%s])"
6304                    (single-key-description cmd) group
6305                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6306          (format " (Type %s to exit %s)"
6307                  (single-key-description cmd)
6308                  gnus-newsgroup-name)))
6309       ;; Confirm auto selection.
6310       (setq key (car (setq keve (gnus-read-event-char))))
6311       (setq ended t)
6312       (cond
6313        ((assq key keystrokes)
6314         (let ((obuf (current-buffer)))
6315           (switch-to-buffer gnus-group-buffer)
6316           (when group
6317             (gnus-group-jump-to-group group))
6318           (eval (cadr (assq key keystrokes)))
6319           (setq group (gnus-group-group-name))
6320           (switch-to-buffer obuf))
6321         (setq ended nil))
6322        ((equal key cmd)
6323         (if (or (not group)
6324                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6325             (gnus-summary-exit)
6326           (gnus-summary-next-group nil group backward)))
6327        (t
6328         (push (cdr keve) unread-command-events))))))
6329
6330 (defun gnus-summary-next-unread-article ()
6331   "Select unread article after current one."
6332   (interactive)
6333   (gnus-summary-next-article
6334    (or (not (eq gnus-summary-goto-unread 'never))
6335        (gnus-summary-last-article-p (gnus-summary-article-number)))
6336    (and gnus-auto-select-same
6337         (gnus-summary-article-subject))))
6338
6339 (defun gnus-summary-prev-article (&optional unread subject)
6340   "Select the article after the current one.
6341 If UNREAD is non-nil, only unread articles are selected."
6342   (interactive "P")
6343   (gnus-summary-next-article unread subject t))
6344
6345 (defun gnus-summary-prev-unread-article ()
6346   "Select unread article before current one."
6347   (interactive)
6348   (gnus-summary-prev-article
6349    (or (not (eq gnus-summary-goto-unread 'never))
6350        (gnus-summary-first-article-p (gnus-summary-article-number)))
6351    (and gnus-auto-select-same
6352         (gnus-summary-article-subject))))
6353
6354 (defun gnus-summary-next-page (&optional lines circular)
6355   "Show next page of the selected article.
6356 If at the end of the current article, select the next article.
6357 LINES says how many lines should be scrolled up.
6358
6359 If CIRCULAR is non-nil, go to the start of the article instead of
6360 selecting the next article when reaching the end of the current
6361 article."
6362   (interactive "P")
6363   (setq gnus-summary-buffer (current-buffer))
6364   (gnus-set-global-variables)
6365   (let ((article (gnus-summary-article-number))
6366         (article-window (get-buffer-window gnus-article-buffer t))
6367         endp)
6368     ;; If the buffer is empty, we have no article.
6369     (unless article
6370       (error "No article to select"))
6371     (gnus-configure-windows 'article)
6372     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6373         (if (and (eq gnus-summary-goto-unread 'never)
6374                  (not (gnus-summary-last-article-p article)))
6375             (gnus-summary-next-article)
6376           (gnus-summary-next-unread-article))
6377       (if (or (null gnus-current-article)
6378               (null gnus-article-current)
6379               (/= article (cdr gnus-article-current))
6380               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6381           ;; Selected subject is different from current article's.
6382           (gnus-summary-display-article article)
6383         (when article-window
6384           (gnus-eval-in-buffer-window gnus-article-buffer
6385             (setq endp (gnus-article-next-page lines)))
6386           (when endp
6387             (cond (circular
6388                    (gnus-summary-beginning-of-article))
6389                   (lines
6390                    (gnus-message 3 "End of message"))
6391                   ((null lines)
6392                    (if (and (eq gnus-summary-goto-unread 'never)
6393                             (not (gnus-summary-last-article-p article)))
6394                        (gnus-summary-next-article)
6395                      (gnus-summary-next-unread-article))))))))
6396     (gnus-summary-recenter)
6397     (gnus-summary-position-point)))
6398
6399 (defun gnus-summary-prev-page (&optional lines move)
6400   "Show previous page of selected article.
6401 Argument LINES specifies lines to be scrolled down.
6402 If MOVE, move to the previous unread article if point is at
6403 the beginning of the buffer."
6404   (interactive "P")
6405   (let ((article (gnus-summary-article-number))
6406         (article-window (get-buffer-window gnus-article-buffer t))
6407         endp)
6408     (gnus-configure-windows 'article)
6409     (if (or (null gnus-current-article)
6410             (null gnus-article-current)
6411             (/= article (cdr gnus-article-current))
6412             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6413         ;; Selected subject is different from current article's.
6414         (gnus-summary-display-article article)
6415       (gnus-summary-recenter)
6416       (when article-window
6417         (gnus-eval-in-buffer-window gnus-article-buffer
6418           (setq endp (gnus-article-prev-page lines)))
6419         (when (and move endp)
6420           (cond (lines
6421                  (gnus-message 3 "Beginning of message"))
6422                 ((null lines)
6423                  (if (and (eq gnus-summary-goto-unread 'never)
6424                           (not (gnus-summary-first-article-p article)))
6425                      (gnus-summary-prev-article)
6426                    (gnus-summary-prev-unread-article))))))))
6427   (gnus-summary-position-point))
6428
6429 (defun gnus-summary-prev-page-or-article (&optional lines)
6430   "Show previous page of selected article.
6431 Argument LINES specifies lines to be scrolled down.
6432 If at the beginning of the article, go to the next article."
6433   (interactive "P")
6434   (gnus-summary-prev-page lines t))
6435
6436 (defun gnus-summary-scroll-up (lines)
6437   "Scroll up (or down) one line current article.
6438 Argument LINES specifies lines to be scrolled up (or down if negative)."
6439   (interactive "p")
6440   (gnus-configure-windows 'article)
6441   (gnus-summary-show-thread)
6442   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6443     (gnus-eval-in-buffer-window gnus-article-buffer
6444       (cond ((> lines 0)
6445              (when (gnus-article-next-page lines)
6446                (gnus-message 3 "End of message")))
6447             ((< lines 0)
6448              (gnus-article-prev-page (- lines))))))
6449   (gnus-summary-recenter)
6450   (gnus-summary-position-point))
6451
6452 (defun gnus-summary-scroll-down (lines)
6453   "Scroll down (or up) one line current article.
6454 Argument LINES specifies lines to be scrolled down (or up if negative)."
6455   (interactive "p")
6456   (gnus-summary-scroll-up (- lines)))
6457
6458 (defun gnus-summary-next-same-subject ()
6459   "Select next article which has the same subject as current one."
6460   (interactive)
6461   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6462
6463 (defun gnus-summary-prev-same-subject ()
6464   "Select previous article which has the same subject as current one."
6465   (interactive)
6466   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6467
6468 (defun gnus-summary-next-unread-same-subject ()
6469   "Select next unread article which has the same subject as current one."
6470   (interactive)
6471   (gnus-summary-next-article t (gnus-summary-article-subject)))
6472
6473 (defun gnus-summary-prev-unread-same-subject ()
6474   "Select previous unread article which has the same subject as current one."
6475   (interactive)
6476   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6477
6478 (defun gnus-summary-first-unread-article ()
6479   "Select the first unread article.
6480 Return nil if there are no unread articles."
6481   (interactive)
6482   (prog1
6483       (when (gnus-summary-first-subject t)
6484         (gnus-summary-show-thread)
6485         (gnus-summary-first-subject t)
6486         (gnus-summary-display-article (gnus-summary-article-number)))
6487     (gnus-summary-position-point)))
6488
6489 (defun gnus-summary-first-unread-subject ()
6490   "Place the point on the subject line of the first unread article.
6491 Return nil if there are no unread articles."
6492   (interactive)
6493   (prog1
6494       (when (gnus-summary-first-subject t)
6495         (gnus-summary-show-thread)
6496         (gnus-summary-first-subject t))
6497     (gnus-summary-position-point)))
6498
6499 (defun gnus-summary-first-article ()
6500   "Select the first article.
6501 Return nil if there are no articles."
6502   (interactive)
6503   (prog1
6504       (when (gnus-summary-first-subject)
6505         (gnus-summary-show-thread)
6506         (gnus-summary-first-subject)
6507         (gnus-summary-display-article (gnus-summary-article-number)))
6508     (gnus-summary-position-point)))
6509
6510 (defun gnus-summary-best-unread-article ()
6511   "Select the unread article with the highest score."
6512   (interactive)
6513   (let ((best -1000000)
6514         (data gnus-newsgroup-data)
6515         article score)
6516     (while data
6517       (and (gnus-data-unread-p (car data))
6518            (> (setq score
6519                     (gnus-summary-article-score (gnus-data-number (car data))))
6520               best)
6521            (setq best score
6522                  article (gnus-data-number (car data))))
6523       (setq data (cdr data)))
6524     (prog1
6525         (if article
6526             (gnus-summary-goto-article article)
6527           (error "No unread articles"))
6528       (gnus-summary-position-point))))
6529
6530 (defun gnus-summary-last-subject ()
6531   "Go to the last displayed subject line in the group."
6532   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6533     (when article
6534       (gnus-summary-goto-subject article))))
6535
6536 (defun gnus-summary-goto-article (article &optional all-headers force)
6537   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6538 If ALL-HEADERS is non-nil, no header lines are hidden.
6539 If FORCE, go to the article even if it isn't displayed.  If FORCE
6540 is a number, it is the line the article is to be displayed on."
6541   (interactive
6542    (list
6543     (completing-read
6544      "Article number or Message-ID: "
6545      (mapcar (lambda (number) (list (int-to-string number)))
6546              gnus-newsgroup-limit))
6547     current-prefix-arg
6548     t))
6549   (prog1
6550       (if (and (stringp article)
6551                (string-match "@" article))
6552           (gnus-summary-refer-article article)
6553         (when (stringp article)
6554           (setq article (string-to-number article)))
6555         (if (gnus-summary-goto-subject article force)
6556             (gnus-summary-display-article article all-headers)
6557           (gnus-message 4 "Couldn't go to article %s" article) nil))
6558     (gnus-summary-position-point)))
6559
6560 (defun gnus-summary-goto-last-article ()
6561   "Go to the previously read article."
6562   (interactive)
6563   (prog1
6564       (when gnus-last-article
6565         (gnus-summary-goto-article gnus-last-article nil t))
6566     (gnus-summary-position-point)))
6567
6568 (defun gnus-summary-pop-article (number)
6569   "Pop one article off the history and go to the previous.
6570 NUMBER articles will be popped off."
6571   (interactive "p")
6572   (let (to)
6573     (setq gnus-newsgroup-history
6574           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6575     (if to
6576         (gnus-summary-goto-article (car to) nil t)
6577       (error "Article history empty")))
6578   (gnus-summary-position-point))
6579
6580 ;; Summary commands and functions for limiting the summary buffer.
6581
6582 (defun gnus-summary-limit-to-articles (n)
6583   "Limit the summary buffer to the next N articles.
6584 If not given a prefix, use the process marked articles instead."
6585   (interactive "P")
6586   (prog1
6587       (let ((articles (gnus-summary-work-articles n)))
6588         (setq gnus-newsgroup-processable nil)
6589         (gnus-summary-limit articles))
6590     (gnus-summary-position-point)))
6591
6592 (defun gnus-summary-pop-limit (&optional total)
6593   "Restore the previous limit.
6594 If given a prefix, remove all limits."
6595   (interactive "P")
6596   (when total
6597     (setq gnus-newsgroup-limits
6598           (list (mapcar (lambda (h) (mail-header-number h))
6599                         gnus-newsgroup-headers))))
6600   (unless gnus-newsgroup-limits
6601     (error "No limit to pop"))
6602   (prog1
6603       (gnus-summary-limit nil 'pop)
6604     (gnus-summary-position-point)))
6605
6606 (defun gnus-summary-limit-to-subject (subject &optional header)
6607   "Limit the summary buffer to articles that have subjects that match a regexp."
6608   (interactive "sLimit to subject (regexp): ")
6609   (unless header
6610     (setq header "subject"))
6611   (when (not (equal "" subject))
6612     (prog1
6613         (let ((articles (gnus-summary-find-matching
6614                          (or header "subject") subject 'all)))
6615           (unless articles
6616             (error "Found no matches for \"%s\"" subject))
6617           (gnus-summary-limit articles))
6618       (gnus-summary-position-point))))
6619
6620 (defun gnus-summary-limit-to-author (from)
6621   "Limit the summary buffer to articles that have authors that match a regexp."
6622   (interactive "sLimit to author (regexp): ")
6623   (gnus-summary-limit-to-subject from "from"))
6624
6625 (defun gnus-summary-limit-to-age (age &optional younger-p)
6626   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6627 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6628 articles that are younger than AGE days."
6629   (interactive
6630    (let ((younger current-prefix-arg)
6631          (days-got nil)
6632          days)
6633      (while (not days-got)
6634        (setq days (if younger
6635                       (read-string "Limit to articles within (in days): ")
6636                     (read-string "Limit to articles older than (in days): ")))
6637        (when (> (length days) 0)
6638          (setq days (read days)))
6639        (if (numberp days)
6640            (setq days-got t)
6641          (message "Please enter a number.")
6642          (sleep-for 1)))
6643      (list days younger)))
6644   (prog1
6645       (let ((data gnus-newsgroup-data)
6646             (cutoff (days-to-time age))
6647             articles d date is-younger)
6648         (while (setq d (pop data))
6649           (when (and (vectorp (gnus-data-header d))
6650                      (setq date (mail-header-date (gnus-data-header d))))
6651             (setq is-younger (time-less-p
6652                               (time-since (condition-case ()
6653                                               (date-to-time date)
6654                                             (error '(0 0))))
6655                               cutoff))
6656             (when (if younger-p
6657                       is-younger
6658                     (not is-younger))
6659               (push (gnus-data-number d) articles))))
6660         (gnus-summary-limit (nreverse articles)))
6661     (gnus-summary-position-point)))
6662
6663 (defun gnus-summary-limit-to-extra (header regexp)
6664   "Limit the summary buffer to articles that match an 'extra' header."
6665   (interactive
6666    (let ((header
6667           (intern
6668            (gnus-completing-read
6669             (symbol-name (car gnus-extra-headers))
6670             "Limit extra header:"
6671             (mapcar (lambda (x)
6672                       (cons (symbol-name x) x))
6673                     gnus-extra-headers)
6674             nil
6675             t))))
6676      (list header
6677            (read-string (format "Limit to header %s (regexp): " header)))))
6678   (when (not (equal "" regexp))
6679     (prog1
6680         (let ((articles (gnus-summary-find-matching
6681                          (cons 'extra header) regexp 'all)))
6682           (unless articles
6683             (error "Found no matches for \"%s\"" regexp))
6684           (gnus-summary-limit articles))
6685       (gnus-summary-position-point))))
6686
6687 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6688 (make-obsolete
6689  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6690
6691 (defun gnus-summary-limit-to-unread (&optional all)
6692   "Limit the summary buffer to articles that are not marked as read.
6693 If ALL is non-nil, limit strictly to unread articles."
6694   (interactive "P")
6695   (if all
6696       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6697     (gnus-summary-limit-to-marks
6698      ;; Concat all the marks that say that an article is read and have
6699      ;; those removed.
6700      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6701            gnus-killed-mark gnus-kill-file-mark
6702            gnus-low-score-mark gnus-expirable-mark
6703            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6704            gnus-duplicate-mark gnus-souped-mark)
6705      'reverse)))
6706
6707 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6708 (make-obsolete 'gnus-summary-delete-marked-with
6709                'gnus-summary-limit-exlude-marks)
6710
6711 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6712   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6713 If REVERSE, limit the summary buffer to articles that are marked
6714 with MARKS.  MARKS can either be a string of marks or a list of marks.
6715 Returns how many articles were removed."
6716   (interactive "sMarks: ")
6717   (gnus-summary-limit-to-marks marks t))
6718
6719 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6720   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6721 If REVERSE (the prefix), limit the summary buffer to articles that are
6722 not marked with MARKS.  MARKS can either be a string of marks or a
6723 list of marks.
6724 Returns how many articles were removed."
6725   (interactive "sMarks: \nP")
6726   (prog1
6727       (let ((data gnus-newsgroup-data)
6728             (marks (if (listp marks) marks
6729                      (append marks nil))) ; Transform to list.
6730             articles)
6731         (while data
6732           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6733                   (memq (gnus-data-mark (car data)) marks))
6734             (push (gnus-data-number (car data)) articles))
6735           (setq data (cdr data)))
6736         (gnus-summary-limit articles))
6737     (gnus-summary-position-point)))
6738
6739 (defun gnus-summary-limit-to-score (&optional score)
6740   "Limit to articles with score at or above SCORE."
6741   (interactive "P")
6742   (setq score (if score
6743                   (prefix-numeric-value score)
6744                 (or gnus-summary-default-score 0)))
6745   (let ((data gnus-newsgroup-data)
6746         articles)
6747     (while data
6748       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6749                 score)
6750         (push (gnus-data-number (car data)) articles))
6751       (setq data (cdr data)))
6752     (prog1
6753         (gnus-summary-limit articles)
6754       (gnus-summary-position-point))))
6755
6756 (defun gnus-summary-limit-include-thread (id)
6757   "Display all the hidden articles that is in the thread with ID in it.
6758 When called interactively, ID is the Message-ID of the current
6759 article."
6760   (interactive (list (mail-header-id (gnus-summary-article-header))))
6761   (let ((articles (gnus-articles-in-thread
6762                    (gnus-id-to-thread (gnus-root-id id)))))
6763     (prog1
6764         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6765         (gnus-summary-limit-include-matching-articles
6766          "subject"
6767          (regexp-quote (gnus-simplify-subject-re
6768                         (mail-header-subject (gnus-id-to-header id)))))
6769       (gnus-summary-position-point))))
6770
6771 (defun gnus-summary-limit-include-matching-articles (header regexp)
6772   "Display all the hidden articles that have HEADERs that match REGEXP."
6773   (interactive (list (read-string "Match on header: ")
6774                      (read-string "Regexp: ")))
6775   (let ((articles (gnus-find-matching-articles header regexp)))
6776     (prog1
6777         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6778       (gnus-summary-position-point))))
6779
6780 (defun gnus-summary-limit-include-dormant ()
6781   "Display all the hidden articles that are marked as dormant.
6782 Note that this command only works on a subset of the articles currently
6783 fetched for this group."
6784   (interactive)
6785   (unless gnus-newsgroup-dormant
6786     (error "There are no dormant articles in this group"))
6787   (prog1
6788       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6789     (gnus-summary-position-point)))
6790
6791 (defun gnus-summary-limit-exclude-dormant ()
6792   "Hide all dormant articles."
6793   (interactive)
6794   (prog1
6795       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6796     (gnus-summary-position-point)))
6797
6798 (defun gnus-summary-limit-exclude-childless-dormant ()
6799   "Hide all dormant articles that have no children."
6800   (interactive)
6801   (let ((data (gnus-data-list t))
6802         articles d children)
6803     ;; Find all articles that are either not dormant or have
6804     ;; children.
6805     (while (setq d (pop data))
6806       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6807                 (and (setq children
6808                            (gnus-article-children (gnus-data-number d)))
6809                      (let (found)
6810                        (while children
6811                          (when (memq (car children) articles)
6812                            (setq children nil
6813                                  found t))
6814                          (pop children))
6815                        found)))
6816         (push (gnus-data-number d) articles)))
6817     ;; Do the limiting.
6818     (prog1
6819         (gnus-summary-limit articles)
6820       (gnus-summary-position-point))))
6821
6822 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6823   "Mark all unread excluded articles as read.
6824 If ALL, mark even excluded ticked and dormants as read."
6825   (interactive "P")
6826   (let ((articles (gnus-sorted-complement
6827                    (sort
6828                     (mapcar (lambda (h) (mail-header-number h))
6829                             gnus-newsgroup-headers)
6830                     '<)
6831                    (sort gnus-newsgroup-limit '<)))
6832         article)
6833     (setq gnus-newsgroup-unreads
6834           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6835     (if all
6836         (setq gnus-newsgroup-dormant nil
6837               gnus-newsgroup-marked nil
6838               gnus-newsgroup-reads
6839               (nconc
6840                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6841                gnus-newsgroup-reads))
6842       (while (setq article (pop articles))
6843         (unless (or (memq article gnus-newsgroup-dormant)
6844                     (memq article gnus-newsgroup-marked))
6845           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6846
6847 (defun gnus-summary-limit (articles &optional pop)
6848   (if pop
6849       ;; We pop the previous limit off the stack and use that.
6850       (setq articles (car gnus-newsgroup-limits)
6851             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6852     ;; We use the new limit, so we push the old limit on the stack.
6853     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6854   ;; Set the limit.
6855   (setq gnus-newsgroup-limit articles)
6856   (let ((total (length gnus-newsgroup-data))
6857         (data (gnus-data-find-list (gnus-summary-article-number)))
6858         (gnus-summary-mark-below nil)   ; Inhibit this.
6859         found)
6860     ;; This will do all the work of generating the new summary buffer
6861     ;; according to the new limit.
6862     (gnus-summary-prepare)
6863     ;; Hide any threads, possibly.
6864     (and gnus-show-threads
6865          gnus-thread-hide-subtree
6866          (gnus-summary-hide-all-threads))
6867     ;; Try to return to the article you were at, or one in the
6868     ;; neighborhood.
6869     (when data
6870       ;; We try to find some article after the current one.
6871       (while data
6872         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6873           (setq data nil
6874                 found t))
6875         (setq data (cdr data))))
6876     (unless found
6877       ;; If there is no data, that means that we were after the last
6878       ;; article.  The same goes when we can't find any articles
6879       ;; after the current one.
6880       (goto-char (point-max))
6881       (gnus-summary-find-prev))
6882     (gnus-set-mode-line 'summary)
6883     ;; We return how many articles were removed from the summary
6884     ;; buffer as a result of the new limit.
6885     (- total (length gnus-newsgroup-data))))
6886
6887 (defsubst gnus-invisible-cut-children (threads)
6888   (let ((num 0))
6889     (while threads
6890       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6891         (incf num))
6892       (pop threads))
6893     (< num 2)))
6894
6895 (defsubst gnus-cut-thread (thread)
6896   "Go forwards in the thread until we find an article that we want to display."
6897   (when (or (eq gnus-fetch-old-headers 'some)
6898             (eq gnus-fetch-old-headers 'invisible)
6899             (numberp gnus-fetch-old-headers)
6900             (eq gnus-build-sparse-threads 'some)
6901             (eq gnus-build-sparse-threads 'more))
6902     ;; Deal with old-fetched headers and sparse threads.
6903     (while (and
6904             thread
6905             (or
6906              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6907              (gnus-summary-article-ancient-p
6908               (mail-header-number (car thread))))
6909             (if (or (<= (length (cdr thread)) 1)
6910                     (eq gnus-fetch-old-headers 'invisible))
6911                 (setq gnus-newsgroup-limit
6912                       (delq (mail-header-number (car thread))
6913                             gnus-newsgroup-limit)
6914                       thread (cadr thread))
6915               (when (gnus-invisible-cut-children (cdr thread))
6916                 (let ((th (cdr thread)))
6917                   (while th
6918                     (if (memq (mail-header-number (caar th))
6919                               gnus-newsgroup-limit)
6920                         (setq thread (car th)
6921                               th nil)
6922                       (setq th (cdr th))))))))))
6923   thread)
6924
6925 (defun gnus-cut-threads (threads)
6926   "Cut off all uninteresting articles from the beginning of threads."
6927   (when (or (eq gnus-fetch-old-headers 'some)
6928             (eq gnus-fetch-old-headers 'invisible)
6929             (numberp gnus-fetch-old-headers)
6930             (eq gnus-build-sparse-threads 'some)
6931             (eq gnus-build-sparse-threads 'more))
6932     (let ((th threads))
6933       (while th
6934         (setcar th (gnus-cut-thread (car th)))
6935         (setq th (cdr th)))))
6936   ;; Remove nixed out threads.
6937   (delq nil threads))
6938
6939 (defun gnus-summary-initial-limit (&optional show-if-empty)
6940   "Figure out what the initial limit is supposed to be on group entry.
6941 This entails weeding out unwanted dormants, low-scored articles,
6942 fetch-old-headers verbiage, and so on."
6943   ;; Most groups have nothing to remove.
6944   (if (or gnus-inhibit-limiting
6945           (and (null gnus-newsgroup-dormant)
6946                (not (eq gnus-fetch-old-headers 'some))
6947                (not (numberp gnus-fetch-old-headers))
6948                (not (eq gnus-fetch-old-headers 'invisible))
6949                (null gnus-summary-expunge-below)
6950                (not (eq gnus-build-sparse-threads 'some))
6951                (not (eq gnus-build-sparse-threads 'more))
6952                (null gnus-thread-expunge-below)
6953                (not gnus-use-nocem)))
6954       ()                                ; Do nothing.
6955     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6956     (setq gnus-newsgroup-limit nil)
6957     (mapatoms
6958      (lambda (node)
6959        (unless (car (symbol-value node))
6960          ;; These threads have no parents -- they are roots.
6961          (let ((nodes (cdr (symbol-value node)))
6962                thread)
6963            (while nodes
6964              (if (and gnus-thread-expunge-below
6965                       (< (gnus-thread-total-score (car nodes))
6966                          gnus-thread-expunge-below))
6967                  (gnus-expunge-thread (pop nodes))
6968                (setq thread (pop nodes))
6969                (gnus-summary-limit-children thread))))))
6970      gnus-newsgroup-dependencies)
6971     ;; If this limitation resulted in an empty group, we might
6972     ;; pop the previous limit and use it instead.
6973     (when (and (not gnus-newsgroup-limit)
6974                show-if-empty)
6975       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6976     gnus-newsgroup-limit))
6977
6978 (defun gnus-summary-limit-children (thread)
6979   "Return 1 if this subthread is visible and 0 if it is not."
6980   ;; First we get the number of visible children to this thread.  This
6981   ;; is done by recursing down the thread using this function, so this
6982   ;; will really go down to a leaf article first, before slowly
6983   ;; working its way up towards the root.
6984   (when thread
6985     (let ((children
6986            (if (cdr thread)
6987                (apply '+ (mapcar 'gnus-summary-limit-children
6988                                  (cdr thread)))
6989              0))
6990           (number (mail-header-number (car thread)))
6991           score)
6992       (if (and
6993            (not (memq number gnus-newsgroup-marked))
6994            (or
6995             ;; If this article is dormant and has absolutely no visible
6996             ;; children, then this article isn't visible.
6997             (and (memq number gnus-newsgroup-dormant)
6998                  (zerop children))
6999             ;; If this is "fetch-old-headered" and there is no
7000             ;; visible children, then we don't want this article.
7001             (and (or (eq gnus-fetch-old-headers 'some)
7002                      (numberp gnus-fetch-old-headers))
7003                  (gnus-summary-article-ancient-p number)
7004                  (zerop children))
7005             ;; If this is "fetch-old-headered" and `invisible', then
7006             ;; we don't want this article.
7007             (and (eq gnus-fetch-old-headers 'invisible)
7008                  (gnus-summary-article-ancient-p number))
7009             ;; If this is a sparsely inserted article with no children,
7010             ;; we don't want it.
7011             (and (eq gnus-build-sparse-threads 'some)
7012                  (gnus-summary-article-sparse-p number)
7013                  (zerop children))
7014             ;; If we use expunging, and this article is really
7015             ;; low-scored, then we don't want this article.
7016             (when (and gnus-summary-expunge-below
7017                        (< (setq score
7018                                 (or (cdr (assq number gnus-newsgroup-scored))
7019                                     gnus-summary-default-score))
7020                           gnus-summary-expunge-below))
7021               ;; We increase the expunge-tally here, but that has
7022               ;; nothing to do with the limits, really.
7023               (incf gnus-newsgroup-expunged-tally)
7024               ;; We also mark as read here, if that's wanted.
7025               (when (and gnus-summary-mark-below
7026                          (< score gnus-summary-mark-below))
7027                 (setq gnus-newsgroup-unreads
7028                       (delq number gnus-newsgroup-unreads))
7029                 (if gnus-newsgroup-auto-expire
7030                     (push number gnus-newsgroup-expirable)
7031                   (push (cons number gnus-low-score-mark)
7032                         gnus-newsgroup-reads)))
7033               t)
7034             ;; Check NoCeM things.
7035             (if (and gnus-use-nocem
7036                      (gnus-nocem-unwanted-article-p
7037                       (mail-header-id (car thread))))
7038                 (progn
7039                   (setq gnus-newsgroup-unreads
7040                         (delq number gnus-newsgroup-unreads))
7041                   t))))
7042           ;; Nope, invisible article.
7043           0
7044         ;; Ok, this article is to be visible, so we add it to the limit
7045         ;; and return 1.
7046         (push number gnus-newsgroup-limit)
7047         1))))
7048
7049 (defun gnus-expunge-thread (thread)
7050   "Mark all articles in THREAD as read."
7051   (let* ((number (mail-header-number (car thread))))
7052     (incf gnus-newsgroup-expunged-tally)
7053     ;; We also mark as read here, if that's wanted.
7054     (setq gnus-newsgroup-unreads
7055           (delq number gnus-newsgroup-unreads))
7056     (if gnus-newsgroup-auto-expire
7057         (push number gnus-newsgroup-expirable)
7058       (push (cons number gnus-low-score-mark)
7059             gnus-newsgroup-reads)))
7060   ;; Go recursively through all subthreads.
7061   (mapcar 'gnus-expunge-thread (cdr thread)))
7062
7063 ;; Summary article oriented commands
7064
7065 (defun gnus-summary-refer-parent-article (n)
7066   "Refer parent article N times.
7067 If N is negative, go to ancestor -N instead.
7068 The difference between N and the number of articles fetched is returned."
7069   (interactive "p")
7070   (let ((skip 1)
7071         error header ref)
7072     (when (not (natnump n))
7073       (setq skip (abs n)
7074             n 1))
7075     (while (and (> n 0)
7076                 (not error))
7077       (setq header (gnus-summary-article-header))
7078       (if (and (eq (mail-header-number header)
7079                    (cdr gnus-article-current))
7080                (equal gnus-newsgroup-name
7081                       (car gnus-article-current)))
7082           ;; If we try to find the parent of the currently
7083           ;; displayed article, then we take a look at the actual
7084           ;; References header, since this is slightly more
7085           ;; reliable than the References field we got from the
7086           ;; server.
7087           (save-excursion
7088             (set-buffer gnus-original-article-buffer)
7089             (nnheader-narrow-to-headers)
7090             (unless (setq ref (message-fetch-field "references"))
7091               (setq ref (message-fetch-field "in-reply-to")))
7092             (widen))
7093         (setq ref
7094               ;; It's not the current article, so we take a bet on
7095               ;; the value we got from the server.
7096               (mail-header-references header)))
7097       (if (and ref
7098                (not (equal ref "")))
7099           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7100             (gnus-message 1 "Couldn't find parent"))
7101         (gnus-message 1 "No references in article %d"
7102                       (gnus-summary-article-number))
7103         (setq error t))
7104       (decf n))
7105     (gnus-summary-position-point)
7106     n))
7107
7108 (defun gnus-summary-refer-references ()
7109   "Fetch all articles mentioned in the References header.
7110 Return the number of articles fetched."
7111   (interactive)
7112   (let ((ref (mail-header-references (gnus-summary-article-header)))
7113         (current (gnus-summary-article-number))
7114         (n 0))
7115     (if (or (not ref)
7116             (equal ref ""))
7117         (error "No References in the current article")
7118       ;; For each Message-ID in the References header...
7119       (while (string-match "<[^>]*>" ref)
7120         (incf n)
7121         ;; ... fetch that article.
7122         (gnus-summary-refer-article
7123          (prog1 (match-string 0 ref)
7124            (setq ref (substring ref (match-end 0))))))
7125       (gnus-summary-goto-subject current)
7126       (gnus-summary-position-point)
7127       n)))
7128
7129 (defun gnus-summary-refer-thread (&optional limit)
7130   "Fetch all articles in the current thread.
7131 If LIMIT (the numerical prefix), fetch that many old headers instead
7132 of what's specified by the `gnus-refer-thread-limit' variable."
7133   (interactive "P")
7134   (let ((id (mail-header-id (gnus-summary-article-header)))
7135         (limit (if limit (prefix-numeric-value limit)
7136                  gnus-refer-thread-limit)))
7137     ;; We want to fetch LIMIT *old* headers, but we also have to
7138     ;; re-fetch all the headers in the current buffer, because many of
7139     ;; them may be undisplayed.  So we adjust LIMIT.
7140     (when (numberp limit)
7141       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7142     (unless (eq gnus-fetch-old-headers 'invisible)
7143       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7144       ;; Retrieve the headers and read them in.
7145       (if (eq (gnus-retrieve-headers
7146                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7147               'nov)
7148           (gnus-build-all-threads)
7149         (error "Can't fetch thread from backends that don't support NOV"))
7150       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7151     (gnus-summary-limit-include-thread id)))
7152
7153 (defun gnus-summary-refer-article (message-id)
7154   "Fetch an article specified by MESSAGE-ID."
7155   (interactive "sMessage-ID: ")
7156   (when (and (stringp message-id)
7157              (not (zerop (length message-id))))
7158     ;; Construct the correct Message-ID if necessary.
7159     ;; Suggested by tale@pawl.rpi.edu.
7160     (unless (string-match "^<" message-id)
7161       (setq message-id (concat "<" message-id)))
7162     (unless (string-match ">$" message-id)
7163       (setq message-id (concat message-id ">")))
7164     (let* ((header (gnus-id-to-header message-id))
7165            (sparse (and header
7166                         (gnus-summary-article-sparse-p
7167                          (mail-header-number header))
7168                         (memq (mail-header-number header)
7169                               gnus-newsgroup-limit)))
7170            number)
7171       (cond
7172        ;; If the article is present in the buffer we just go to it.
7173        ((and header
7174              (or (not (gnus-summary-article-sparse-p
7175                        (mail-header-number header)))
7176                  sparse))
7177         (prog1
7178             (gnus-summary-goto-article
7179              (mail-header-number header) nil t)
7180           (when sparse
7181             (gnus-summary-update-article (mail-header-number header)))))
7182        (t
7183         ;; We fetch the article.
7184         (catch 'found
7185           (dolist (gnus-override-method (gnus-refer-article-methods))
7186             (gnus-check-server gnus-override-method)
7187             ;; Fetch the header, and display the article.
7188             (when (setq number (gnus-summary-insert-subject message-id))
7189               (gnus-summary-select-article nil nil nil number)
7190               (throw 'found t)))
7191           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7192
7193 (defun gnus-refer-article-methods ()
7194   "Return a list of referrable methods."
7195   (cond
7196    ;; No method, so we default to current and native.
7197    ((null gnus-refer-article-method)
7198     (list gnus-current-select-method gnus-select-method))
7199    ;; Current.
7200    ((eq 'current gnus-refer-article-method)
7201     (list gnus-current-select-method))
7202    ;; List of select methods.
7203    ((not (and (symbolp (car gnus-refer-article-method))
7204               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7205     (let (out)
7206       (dolist (method gnus-refer-article-method)
7207         (push (if (eq 'current method)
7208                   gnus-current-select-method
7209                 method)
7210               out))
7211       (nreverse out)))
7212    ;; One single select method.
7213    (t
7214     (list gnus-refer-article-method))))
7215
7216 (defun gnus-summary-edit-parameters ()
7217   "Edit the group parameters of the current group."
7218   (interactive)
7219   (gnus-group-edit-group gnus-newsgroup-name 'params))
7220
7221 (defun gnus-summary-customize-parameters ()
7222   "Customize the group parameters of the current group."
7223   (interactive)
7224   (gnus-group-customize gnus-newsgroup-name))
7225
7226 (defun gnus-summary-enter-digest-group (&optional force)
7227   "Enter an nndoc group based on the current article.
7228 If FORCE, force a digest interpretation.  If not, try
7229 to guess what the document format is."
7230   (interactive "P")
7231   (let ((conf gnus-current-window-configuration))
7232     (save-excursion
7233       (gnus-summary-select-article))
7234     (setq gnus-current-window-configuration conf)
7235     (let* ((name (format "%s-%d"
7236                          (gnus-group-prefixed-name
7237                           gnus-newsgroup-name (list 'nndoc ""))
7238                          (save-excursion
7239                            (set-buffer gnus-summary-buffer)
7240                            gnus-current-article)))
7241            (ogroup gnus-newsgroup-name)
7242            (params (append (gnus-info-params (gnus-get-info ogroup))
7243                            (list (cons 'to-group ogroup))
7244                            (list (cons 'save-article-group ogroup))))
7245            (case-fold-search t)
7246            (buf (current-buffer))
7247            dig to-address)
7248       (save-excursion
7249         (set-buffer gnus-original-article-buffer)
7250         ;; Have the digest group inherit the main mail address of
7251         ;; the parent article.
7252         (when (setq to-address (or (message-fetch-field "reply-to")
7253                                    (message-fetch-field "from")))
7254           (setq params (append
7255                         (list (cons 'to-address
7256                                     (funcall gnus-decode-encoded-word-function
7257                                              to-address))))))
7258         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7259         (insert-buffer-substring gnus-original-article-buffer)
7260         ;; Remove lines that may lead nndoc to misinterpret the
7261         ;; document type.
7262         (narrow-to-region
7263          (goto-char (point-min))
7264          (or (search-forward "\n\n" nil t) (point)))
7265         (goto-char (point-min))
7266         (delete-matching-lines "^Path:\\|^From ")
7267         (widen))
7268       (unwind-protect
7269           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7270                     (gnus-newsgroup-ephemeral-ignored-charsets
7271                      gnus-newsgroup-ignored-charsets))
7272                 (gnus-group-read-ephemeral-group
7273                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7274                               (nndoc-article-type
7275                                ,(if force 'mbox 'guess))) t))
7276               ;; Make all postings to this group go to the parent group.
7277               (nconc (gnus-info-params (gnus-get-info name))
7278                      params)
7279             ;; Couldn't select this doc group.
7280             (switch-to-buffer buf)
7281             (gnus-set-global-variables)
7282             (gnus-configure-windows 'summary)
7283             (gnus-message 3 "Article couldn't be entered?"))
7284         (kill-buffer dig)))))
7285
7286 (defun gnus-summary-read-document (n)
7287   "Open a new group based on the current article(s).
7288 This will allow you to read digests and other similar
7289 documents as newsgroups.
7290 Obeys the standard process/prefix convention."
7291   (interactive "P")
7292   (let* ((articles (gnus-summary-work-articles n))
7293          (ogroup gnus-newsgroup-name)
7294          (params (append (gnus-info-params (gnus-get-info ogroup))
7295                          (list (cons 'to-group ogroup))))
7296          article group egroup groups vgroup)
7297     (while (setq article (pop articles))
7298       (setq group (format "%s-%d" gnus-newsgroup-name article))
7299       (gnus-summary-remove-process-mark article)
7300       (when (gnus-summary-display-article article)
7301         (save-excursion
7302           (with-temp-buffer
7303             (insert-buffer-substring gnus-original-article-buffer)
7304             ;; Remove some headers that may lead nndoc to make
7305             ;; the wrong guess.
7306             (message-narrow-to-head)
7307             (goto-char (point-min))
7308             (delete-matching-lines "^\\(Path\\):\\|^From ")
7309             (widen)
7310             (if (setq egroup
7311                       (gnus-group-read-ephemeral-group
7312                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7313                                      (nndoc-article-type guess))
7314                        t nil t))
7315                 (progn
7316                   ;; Make all postings to this group go to the parent group.
7317                   (nconc (gnus-info-params (gnus-get-info egroup))
7318                          params)
7319                   (push egroup groups))
7320               ;; Couldn't select this doc group.
7321               (gnus-error 3 "Article couldn't be entered"))))))
7322     ;; Now we have selected all the documents.
7323     (cond
7324      ((not groups)
7325       (error "None of the articles could be interpreted as documents"))
7326      ((gnus-group-read-ephemeral-group
7327        (setq vgroup (format
7328                      "nnvirtual:%s-%s" gnus-newsgroup-name
7329                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7330        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7331        t
7332        (cons (current-buffer) 'summary)))
7333      (t
7334       (error "Couldn't select virtual nndoc group")))))
7335
7336 (defun gnus-summary-isearch-article (&optional regexp-p)
7337   "Do incremental search forward on the current article.
7338 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7339   (interactive "P")
7340   (gnus-summary-select-article)
7341   (gnus-configure-windows 'article)
7342   (gnus-eval-in-buffer-window gnus-article-buffer
7343     (save-restriction
7344       (widen)
7345       (isearch-forward regexp-p))))
7346
7347 (defun gnus-summary-search-article-forward (regexp &optional backward)
7348   "Search for an article containing REGEXP forward.
7349 If BACKWARD, search backward instead."
7350   (interactive
7351    (list (read-string
7352           (format "Search article %s (regexp%s): "
7353                   (if current-prefix-arg "backward" "forward")
7354                   (if gnus-last-search-regexp
7355                       (concat ", default " gnus-last-search-regexp)
7356                     "")))
7357          current-prefix-arg))
7358   (if (string-equal regexp "")
7359       (setq regexp (or gnus-last-search-regexp ""))
7360     (setq gnus-last-search-regexp regexp)
7361     (setq gnus-article-before-search gnus-current-article))
7362   ;; Intentionally set gnus-last-article.
7363   (setq gnus-last-article gnus-article-before-search)
7364   (let ((gnus-last-article gnus-last-article))
7365     (if (gnus-summary-search-article regexp backward)
7366         (gnus-summary-show-thread)
7367       (error "Search failed: \"%s\"" regexp))))
7368
7369 (defun gnus-summary-search-article-backward (regexp)
7370   "Search for an article containing REGEXP backward."
7371   (interactive
7372    (list (read-string
7373           (format "Search article backward (regexp%s): "
7374                   (if gnus-last-search-regexp
7375                       (concat ", default " gnus-last-search-regexp)
7376                     "")))))
7377   (gnus-summary-search-article-forward regexp 'backward))
7378
7379 (defun gnus-summary-search-article (regexp &optional backward)
7380   "Search for an article containing REGEXP.
7381 Optional argument BACKWARD means do search for backward.
7382 `gnus-select-article-hook' is not called during the search."
7383   ;; We have to require this here to make sure that the following
7384   ;; dynamic binding isn't shadowed by autoloading.
7385   (require 'gnus-async)
7386   (require 'gnus-art)
7387   (let ((gnus-select-article-hook nil)  ;Disable hook.
7388         (gnus-article-prepare-hook nil)
7389         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7390         (gnus-use-article-prefetch nil)
7391         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7392         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7393         (sum (current-buffer))
7394         (gnus-display-mime-function nil)
7395         (found nil)
7396         point)
7397     (gnus-save-hidden-threads
7398       (gnus-summary-select-article)
7399       (set-buffer gnus-article-buffer)
7400       (goto-char (window-point (get-buffer-window (current-buffer))))
7401       (when backward
7402         (forward-line -1))
7403       (while (not found)
7404         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7405         (if (if backward
7406                 (re-search-backward regexp nil t)
7407               (re-search-forward regexp nil t))
7408             ;; We found the regexp.
7409             (progn
7410               (setq found 'found)
7411               (beginning-of-line)
7412               (set-window-start
7413                (get-buffer-window (current-buffer))
7414                (point))
7415               (forward-line 1)
7416               (set-window-point
7417                (get-buffer-window (current-buffer))
7418                (point))
7419               (set-buffer sum)
7420               (setq point (point)))
7421           ;; We didn't find it, so we go to the next article.
7422           (set-buffer sum)
7423           (setq found 'not)
7424           (while (eq found 'not)
7425             (if (not (if backward (gnus-summary-find-prev)
7426                        (gnus-summary-find-next)))
7427                 ;; No more articles.
7428                 (setq found t)
7429               ;; Select the next article and adjust point.
7430               (unless (gnus-summary-article-sparse-p
7431                        (gnus-summary-article-number))
7432                 (setq found nil)
7433                 (gnus-summary-select-article)
7434                 (set-buffer gnus-article-buffer)
7435                 (widen)
7436                 (goto-char (if backward (point-max) (point-min))))))))
7437       (gnus-message 7 ""))
7438     ;; Return whether we found the regexp.
7439     (when (eq found 'found)
7440       (goto-char point)
7441       (gnus-summary-show-thread)
7442       (gnus-summary-goto-subject gnus-current-article)
7443       (gnus-summary-position-point)
7444       t)))
7445
7446 (defun gnus-find-matching-articles (header regexp)
7447   "Return a list of all articles that match REGEXP on HEADER.
7448 This search includes all articles in the current group that Gnus has
7449 fetched headers for, whether they are displayed or not."
7450   (let ((articles nil)
7451         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7452         (case-fold-search t))
7453     (dolist (header gnus-newsgroup-headers)
7454       (when (string-match regexp (funcall func header))
7455         (push (mail-header-number header) articles)))
7456     (nreverse articles)))
7457
7458 (defun gnus-summary-find-matching (header regexp &optional backward unread
7459                                           not-case-fold)
7460   "Return a list of all articles that match REGEXP on HEADER.
7461 The search stars on the current article and goes forwards unless
7462 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7463 If UNREAD is non-nil, only unread articles will
7464 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7465 in the comparisons."
7466   (let ((case-fold-search (not not-case-fold))
7467         articles d func)
7468     (if (consp header)
7469         (if (eq (car header) 'extra)
7470             (setq func
7471                   `(lambda (h)
7472                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7473                          "")))
7474           (error "%s is an invalid header" header))
7475       (unless (fboundp (intern (concat "mail-header-" header)))
7476         (error "%s is not a valid header" header))
7477       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7478     (dolist (d (if (eq backward 'all)
7479                    gnus-newsgroup-data
7480                  (gnus-data-find-list
7481                   (gnus-summary-article-number)
7482                   (gnus-data-list backward))))
7483       (when (and (or (not unread)       ; We want all articles...
7484                      (gnus-data-unread-p d)) ; Or just unreads.
7485                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7486                  (string-match regexp
7487                                (funcall func (gnus-data-header d)))) ; Match.
7488         (push (gnus-data-number d) articles))) ; Success!
7489     (nreverse articles)))
7490
7491 (defun gnus-summary-execute-command (header regexp command &optional backward)
7492   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7493 If HEADER is an empty string (or nil), the match is done on the entire
7494 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7495   (interactive
7496    (list (let ((completion-ignore-case t))
7497            (completing-read
7498             "Header name: "
7499             (mapcar (lambda (header) (list (format "%s" header)))
7500                     (append
7501                      '("Number" "Subject" "From" "Lines" "Date"
7502                        "Message-ID" "Xref" "References" "Body")
7503                      gnus-extra-headers))
7504             nil 'require-match))
7505          (read-string "Regexp: ")
7506          (read-key-sequence "Command: ")
7507          current-prefix-arg))
7508   (when (equal header "Body")
7509     (setq header ""))
7510   ;; Hidden thread subtrees must be searched as well.
7511   (gnus-summary-show-all-threads)
7512   ;; We don't want to change current point nor window configuration.
7513   (save-excursion
7514     (save-window-excursion
7515       (gnus-message 6 "Executing %s..." (key-description command))
7516       ;; We'd like to execute COMMAND interactively so as to give arguments.
7517       (gnus-execute header regexp
7518                     `(call-interactively ',(key-binding command))
7519                     backward)
7520       (gnus-message 6 "Executing %s...done" (key-description command)))))
7521
7522 (defun gnus-summary-beginning-of-article ()
7523   "Scroll the article back to the beginning."
7524   (interactive)
7525   (gnus-summary-select-article)
7526   (gnus-configure-windows 'article)
7527   (gnus-eval-in-buffer-window gnus-article-buffer
7528     (widen)
7529     (goto-char (point-min))
7530     (when gnus-page-broken
7531       (gnus-narrow-to-page))))
7532
7533 (defun gnus-summary-end-of-article ()
7534   "Scroll to the end of the article."
7535   (interactive)
7536   (gnus-summary-select-article)
7537   (gnus-configure-windows 'article)
7538   (gnus-eval-in-buffer-window gnus-article-buffer
7539     (widen)
7540     (goto-char (point-max))
7541     (recenter -3)
7542     (when gnus-page-broken
7543       (gnus-narrow-to-page))))
7544
7545 (defun gnus-summary-print-article (&optional filename n)
7546   "Generate and print a PostScript image of the N next (mail) articles.
7547
7548 If N is negative, print the N previous articles.  If N is nil and articles
7549 have been marked with the process mark, print these instead.
7550
7551 If the optional first argument FILENAME is nil, send the image to the
7552 printer.  If FILENAME is a string, save the PostScript image in a file with
7553 that name.  If FILENAME is a number, prompt the user for the name of the file
7554 to save in."
7555   (interactive (list (ps-print-preprint current-prefix-arg)))
7556   (dolist (article (gnus-summary-work-articles n))
7557     (gnus-summary-select-article nil nil 'pseudo article)
7558     (gnus-eval-in-buffer-window gnus-article-buffer
7559       (let ((buffer (generate-new-buffer " *print*")))
7560         (unwind-protect
7561             (progn
7562               (copy-to-buffer buffer (point-min) (point-max))
7563               (set-buffer buffer)
7564               (gnus-article-delete-invisible-text)
7565               (when (gnus-visual-p 'article-highlight 'highlight)
7566                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7567                 ;; highlight.
7568                 (let ((gnus-article-buffer buffer))
7569                   (gnus-article-highlight-citation t)
7570                   (gnus-article-highlight-signature)))
7571               (let ((ps-left-header
7572                      (list
7573                       (concat "("
7574                               (mail-header-subject gnus-current-headers) ")")
7575                       (concat "("
7576                               (mail-header-from gnus-current-headers) ")")))
7577                     (ps-right-header
7578                      (list
7579                       "/pagenumberstring load"
7580                       (concat "("
7581                               (mail-header-date gnus-current-headers) ")"))))
7582                 (gnus-run-hooks 'gnus-ps-print-hook)
7583                 (save-excursion
7584                   (if window-system
7585                       (ps-spool-buffer-with-faces)
7586                     (ps-spool-buffer)))))
7587           (kill-buffer buffer))))
7588     (gnus-summary-remove-process-mark article))
7589   (ps-despool filename))
7590
7591 (defun gnus-summary-show-article (&optional arg)
7592   "Force re-fetching of the current article.
7593 If ARG (the prefix) is a number, show the article with the charset
7594 defined in `gnus-summary-show-article-charset-alist', or the charset
7595 inputed.
7596 If ARG (the prefix) is non-nil and not a number, show the raw article
7597 without any article massaging functions being run."
7598   (interactive "P")
7599   (cond
7600    ((numberp arg)
7601     (let ((gnus-newsgroup-charset
7602            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7603                (read-coding-system "Charset: ")))
7604           (gnus-newsgroup-ignored-charsets 'gnus-all))
7605       (gnus-summary-select-article nil 'force)
7606       (let ((deps gnus-newsgroup-dependencies)
7607             head header)
7608         (save-excursion
7609           (set-buffer gnus-original-article-buffer)
7610           (save-restriction
7611             (message-narrow-to-head)
7612             (setq head (buffer-string)))
7613           (with-temp-buffer
7614             (insert (format "211 %d Article retrieved.\n"
7615                             (cdr gnus-article-current)))
7616             (insert head)
7617             (insert ".\n")
7618             (let ((nntp-server-buffer (current-buffer)))
7619               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7620         (gnus-data-set-header
7621          (gnus-data-find (cdr gnus-article-current))
7622          header)
7623         (gnus-summary-update-article-line
7624          (cdr gnus-article-current) header))))
7625    ((not arg)
7626     ;; Select the article the normal way.
7627     (gnus-summary-select-article nil 'force))
7628    (t
7629     ;; We have to require this here to make sure that the following
7630     ;; dynamic binding isn't shadowed by autoloading.
7631     (require 'gnus-async)
7632     (require 'gnus-art)
7633     ;; Bind the article treatment functions to nil.
7634     (let ((gnus-have-all-headers t)
7635           gnus-article-prepare-hook
7636           gnus-article-decode-hook
7637           gnus-display-mime-function
7638           gnus-break-pages)
7639       ;; Destroy any MIME parts.
7640       (when (gnus-buffer-live-p gnus-article-buffer)
7641         (save-excursion
7642           (set-buffer gnus-article-buffer)
7643           (mm-destroy-parts gnus-article-mime-handles)
7644           ;; Set it to nil for safety reason.
7645           (setq gnus-article-mime-handle-alist nil)
7646           (setq gnus-article-mime-handles nil)))
7647       (gnus-summary-select-article nil 'force))))
7648   (gnus-summary-goto-subject gnus-current-article)
7649   (gnus-summary-position-point))
7650
7651 (defun gnus-summary-show-raw-article ()
7652   "Show the raw article without any article massaging functions being run."
7653   (interactive)
7654   (gnus-summary-show-article t))
7655
7656 (defun gnus-summary-verbose-headers (&optional arg)
7657   "Toggle permanent full header display.
7658 If ARG is a positive number, turn header display on.
7659 If ARG is a negative number, turn header display off."
7660   (interactive "P")
7661   (setq gnus-show-all-headers
7662         (cond ((or (not (numberp arg))
7663                    (zerop arg))
7664                (not gnus-show-all-headers))
7665               ((natnump arg)
7666                t)))
7667   (gnus-summary-show-article))
7668
7669 (defun gnus-summary-toggle-header (&optional arg)
7670   "Show the headers if they are hidden, or hide them if they are shown.
7671 If ARG is a positive number, show the entire header.
7672 If ARG is a negative number, hide the unwanted header lines."
7673   (interactive "P")
7674   (save-excursion
7675     (set-buffer gnus-article-buffer)
7676     (save-restriction
7677       (let* ((buffer-read-only nil)
7678              (inhibit-point-motion-hooks t)
7679              hidden e)
7680         (setq hidden
7681               (if (numberp arg)
7682                   (>= arg 0)
7683                 (save-restriction
7684                   (article-narrow-to-head)
7685                   (gnus-article-hidden-text-p 'headers))))
7686         (goto-char (point-min))
7687         (when (search-forward "\n\n" nil t)
7688           (delete-region (point-min) (1- (point))))
7689         (goto-char (point-min))
7690         (save-excursion
7691           (set-buffer gnus-original-article-buffer)
7692           (goto-char (point-min))
7693           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7694         (insert-buffer-substring gnus-original-article-buffer 1 e)
7695         (save-restriction
7696           (narrow-to-region (point-min) (point))
7697           (article-decode-encoded-words)
7698           (if  hidden
7699               (let ((gnus-treat-hide-headers nil)
7700                     (gnus-treat-hide-boring-headers nil))
7701                 (setq gnus-article-wash-types
7702                       (delq 'headers gnus-article-wash-types))
7703                 (gnus-treat-article 'head))
7704             (gnus-treat-article 'head)))
7705         (gnus-set-mode-line 'article)))))
7706
7707 (defun gnus-summary-show-all-headers ()
7708   "Make all header lines visible."
7709   (interactive)
7710   (gnus-summary-toggle-header 1))
7711
7712 (defun gnus-summary-caesar-message (&optional arg)
7713   "Caesar rotate the current article by 13.
7714 The numerical prefix specifies how many places to rotate each letter
7715 forward."
7716   (interactive "P")
7717   (gnus-summary-select-article)
7718   (let ((mail-header-separator ""))
7719     (gnus-eval-in-buffer-window gnus-article-buffer
7720       (save-restriction
7721         (widen)
7722         (let ((start (window-start))
7723               buffer-read-only)
7724           (message-caesar-buffer-body arg)
7725           (set-window-start (get-buffer-window (current-buffer)) start))))))
7726
7727 (defun gnus-summary-stop-page-breaking ()
7728   "Stop page breaking in the current article."
7729   (interactive)
7730   (gnus-summary-select-article)
7731   (gnus-eval-in-buffer-window gnus-article-buffer
7732     (widen)
7733     (when (gnus-visual-p 'page-marker)
7734       (let ((buffer-read-only nil))
7735         (gnus-remove-text-with-property 'gnus-prev)
7736         (gnus-remove-text-with-property 'gnus-next))
7737       (setq gnus-page-broken nil))))
7738
7739 (defun gnus-summary-move-article (&optional n to-newsgroup
7740                                             select-method action)
7741   "Move the current article to a different newsgroup.
7742 If N is a positive number, move the N next articles.
7743 If N is a negative number, move the N previous articles.
7744 If N is nil and any articles have been marked with the process mark,
7745 move those articles instead.
7746 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7747 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7748 re-spool using this method.
7749
7750 For this function to work, both the current newsgroup and the
7751 newsgroup that you want to move to have to support the `request-move'
7752 and `request-accept' functions.
7753
7754 ACTION can be either `move' (the default), `crosspost' or `copy'."
7755   (interactive "P")
7756   (unless action
7757     (setq action 'move))
7758   ;; Disable marking as read.
7759   (let (gnus-mark-article-hook)
7760     (save-window-excursion
7761       (gnus-summary-select-article)))
7762   ;; Check whether the source group supports the required functions.
7763   (cond ((and (eq action 'move)
7764               (not (gnus-check-backend-function
7765                     'request-move-article gnus-newsgroup-name)))
7766          (error "The current group does not support article moving"))
7767         ((and (eq action 'crosspost)
7768               (not (gnus-check-backend-function
7769                     'request-replace-article gnus-newsgroup-name)))
7770          (error "The current group does not support article editing")))
7771   (let ((articles (gnus-summary-work-articles n))
7772         (prefix (if (gnus-check-backend-function
7773                     'request-move-article gnus-newsgroup-name)
7774                     (gnus-group-real-prefix gnus-newsgroup-name)
7775                   ""))
7776         (names '((move "Move" "Moving")
7777                  (copy "Copy" "Copying")
7778                  (crosspost "Crosspost" "Crossposting")))
7779         (copy-buf (save-excursion
7780                     (nnheader-set-temp-buffer " *copy article*")))
7781         art-group to-method new-xref article to-groups)
7782     (unless (assq action names)
7783       (error "Unknown action %s" action))
7784     ;; Read the newsgroup name.
7785     (when (and (not to-newsgroup)
7786                (not select-method))
7787       (setq to-newsgroup
7788             (gnus-read-move-group-name
7789              (cadr (assq action names))
7790              (symbol-value (intern (format "gnus-current-%s-group" action)))
7791              articles prefix))
7792       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7793     (setq to-method (or select-method
7794                         (gnus-server-to-method
7795                          (gnus-group-method to-newsgroup))))
7796     ;; Check the method we are to move this article to...
7797     (unless (gnus-check-backend-function
7798              'request-accept-article (car to-method))
7799       (error "%s does not support article copying" (car to-method)))
7800     (unless (gnus-check-server to-method)
7801       (error "Can't open server %s" (car to-method)))
7802     (gnus-message 6 "%s to %s: %s..."
7803                   (caddr (assq action names))
7804                   (or (car select-method) to-newsgroup) articles)
7805     (while articles
7806       (setq article (pop articles))
7807       (setq
7808        art-group
7809        (cond
7810         ;; Move the article.
7811         ((eq action 'move)
7812          ;; Remove this article from future suppression.
7813          (gnus-dup-unsuppress-article article)
7814          (gnus-request-move-article
7815           article                       ; Article to move
7816           gnus-newsgroup-name           ; From newsgroup
7817           (nth 1 (gnus-find-method-for-group
7818                   gnus-newsgroup-name)) ; Server
7819           (list 'gnus-request-accept-article
7820                 to-newsgroup (list 'quote select-method)
7821                 (not articles) t)       ; Accept form
7822           (not articles)))              ; Only save nov last time
7823         ;; Copy the article.
7824         ((eq action 'copy)
7825          (save-excursion
7826            (set-buffer copy-buf)
7827            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7828              (gnus-request-accept-article
7829               to-newsgroup select-method (not articles) t))))
7830         ;; Crosspost the article.
7831         ((eq action 'crosspost)
7832          (let ((xref (message-tokenize-header
7833                       (mail-header-xref (gnus-summary-article-header article))
7834                       " ")))
7835            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7836                                   ":" article))
7837            (unless xref
7838              (setq xref (list (system-name))))
7839            (setq new-xref
7840                  (concat
7841                   (mapconcat 'identity
7842                              (delete "Xref:" (delete new-xref xref))
7843                              " ")
7844                   " " new-xref))
7845            (save-excursion
7846              (set-buffer copy-buf)
7847              ;; First put the article in the destination group.
7848              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7849              (when (consp (setq art-group
7850                                 (gnus-request-accept-article
7851                                  to-newsgroup select-method (not articles))))
7852                (setq new-xref (concat new-xref " " (car art-group)
7853                                       ":" (cdr art-group)))
7854                ;; Now we have the new Xrefs header, so we insert
7855                ;; it and replace the new article.
7856                (nnheader-replace-header "Xref" new-xref)
7857                (gnus-request-replace-article
7858                 (cdr art-group) to-newsgroup (current-buffer))
7859                art-group))))))
7860       (cond
7861        ((not art-group)
7862         (gnus-message 1 "Couldn't %s article %s: %s"
7863                       (cadr (assq action names)) article
7864                       (nnheader-get-report (car to-method))))
7865        ((eq art-group 'junk)
7866         (when (eq action 'move)
7867           (gnus-summary-mark-article article gnus-canceled-mark)
7868           (gnus-message 4 "Deleted article %s" article)))
7869        (t
7870         (let* ((pto-group (gnus-group-prefixed-name
7871                            (car art-group) to-method))
7872                (entry
7873                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7874                (info (nth 2 entry))
7875                (to-group (gnus-info-group info))
7876                to-marks)
7877           ;; Update the group that has been moved to.
7878           (when (and info
7879                      (memq action '(move copy)))
7880             (unless (member to-group to-groups)
7881               (push to-group to-groups))
7882
7883             (unless (memq article gnus-newsgroup-unreads)
7884               (push 'read to-marks)
7885               (gnus-info-set-read
7886                info (gnus-add-to-range (gnus-info-read info)
7887                                        (list (cdr art-group)))))
7888
7889             ;; See whether the article is to be put in the cache.
7890             (let ((marks gnus-article-mark-lists)
7891                   (to-article (cdr art-group)))
7892
7893               ;; Enter the article into the cache in the new group,
7894               ;; if that is required.
7895               (when gnus-use-cache
7896                 (gnus-cache-possibly-enter-article
7897                  to-group to-article
7898                  (memq article gnus-newsgroup-marked)
7899                  (memq article gnus-newsgroup-dormant)
7900                  (memq article gnus-newsgroup-unreads)))
7901
7902               (when gnus-preserve-marks
7903                 ;; Copy any marks over to the new group.
7904                 (when (and (equal to-group gnus-newsgroup-name)
7905                            (not (memq article gnus-newsgroup-unreads)))
7906                   ;; Mark this article as read in this group.
7907                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7908                   (setcdr (gnus-active to-group) to-article)
7909                   (setcdr gnus-newsgroup-active to-article))
7910
7911                 (while marks
7912                   (when (memq article (symbol-value
7913                                        (intern (format "gnus-newsgroup-%s"
7914                                                        (caar marks)))))
7915                     (push (cdar marks) to-marks)
7916                     ;; If the other group is the same as this group,
7917                     ;; then we have to add the mark to the list.
7918                     (when (equal to-group gnus-newsgroup-name)
7919                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7920                            (cons to-article
7921                                  (symbol-value
7922                                   (intern (format "gnus-newsgroup-%s"
7923                                                   (caar marks)))))))
7924                     ;; Copy the marks to other group.
7925                     (gnus-add-marked-articles
7926                      to-group (cdar marks) (list to-article) info))
7927                   (setq marks (cdr marks)))
7928
7929                 (gnus-request-set-mark to-group (list (list (list to-article)
7930                                                             'set
7931                                                             to-marks))))
7932
7933               (gnus-dribble-enter
7934                (concat "(gnus-group-set-info '"
7935                        (gnus-prin1-to-string (gnus-get-info to-group))
7936                        ")"))))
7937
7938           ;; Update the Xref header in this article to point to
7939           ;; the new crossposted article we have just created.
7940           (when (eq action 'crosspost)
7941             (save-excursion
7942               (set-buffer copy-buf)
7943               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7944               (nnheader-replace-header "Xref" new-xref)
7945               (gnus-request-replace-article
7946                article gnus-newsgroup-name (current-buffer)))))
7947
7948         ;;;!!!Why is this necessary?
7949         (set-buffer gnus-summary-buffer)
7950
7951         (gnus-summary-goto-subject article)
7952         (when (eq action 'move)
7953           (gnus-summary-mark-article article gnus-canceled-mark))))
7954       (gnus-summary-remove-process-mark article))
7955     ;; Re-activate all groups that have been moved to.
7956     (while to-groups
7957       (save-excursion
7958         (set-buffer gnus-group-buffer)
7959         (when (gnus-group-goto-group (car to-groups) t)
7960           (gnus-group-get-new-news-this-group 1 t))
7961         (pop to-groups)))
7962
7963     (gnus-kill-buffer copy-buf)
7964     (gnus-summary-position-point)
7965     (gnus-set-mode-line 'summary)))
7966
7967 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7968   "Move the current article to a different newsgroup.
7969 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7970 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7971 re-spool using this method."
7972   (interactive "P")
7973   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7974
7975 (defun gnus-summary-crosspost-article (&optional n)
7976   "Crosspost the current article to some other group."
7977   (interactive "P")
7978   (gnus-summary-move-article n nil nil 'crosspost))
7979
7980 (defcustom gnus-summary-respool-default-method nil
7981   "Default method for respooling an article.
7982 If nil, use to the current newsgroup method."
7983   :type '(choice (gnus-select-method :value (nnml ""))
7984                  (const nil))
7985   :group 'gnus-summary-mail)
7986
7987 (defun gnus-summary-respool-article (&optional n method)
7988   "Respool the current article.
7989 The article will be squeezed through the mail spooling process again,
7990 which means that it will be put in some mail newsgroup or other
7991 depending on `nnmail-split-methods'.
7992 If N is a positive number, respool the N next articles.
7993 If N is a negative number, respool the N previous articles.
7994 If N is nil and any articles have been marked with the process mark,
7995 respool those articles instead.
7996
7997 Respooling can be done both from mail groups and \"real\" newsgroups.
7998 In the former case, the articles in question will be moved from the
7999 current group into whatever groups they are destined to.  In the
8000 latter case, they will be copied into the relevant groups."
8001   (interactive
8002    (list current-prefix-arg
8003          (let* ((methods (gnus-methods-using 'respool))
8004                 (methname
8005                  (symbol-name (or gnus-summary-respool-default-method
8006                                   (car (gnus-find-method-for-group
8007                                         gnus-newsgroup-name)))))
8008                 (method
8009                  (gnus-completing-read
8010                   methname "What backend do you want to use when respooling?"
8011                   methods nil t nil 'gnus-mail-method-history))
8012                 ms)
8013            (cond
8014             ((zerop (length (setq ms (gnus-servers-using-backend
8015                                       (intern method)))))
8016              (list (intern method) ""))
8017             ((= 1 (length ms))
8018              (car ms))
8019             (t
8020              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8021                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8022                            ms-alist))))))))
8023   (unless method
8024     (error "No method given for respooling"))
8025   (if (assoc (symbol-name
8026               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8027              (gnus-methods-using 'respool))
8028       (gnus-summary-move-article n nil method)
8029     (gnus-summary-copy-article n nil method)))
8030
8031 (defun gnus-summary-import-article (file &optional edit)
8032   "Import an arbitrary file into a mail newsgroup."
8033   (interactive "fImport file: \nP")
8034   (let ((group gnus-newsgroup-name)
8035         (now (current-time))
8036         atts lines group-art)
8037     (unless (gnus-check-backend-function 'request-accept-article group)
8038       (error "%s does not support article importing" group))
8039     (or (file-readable-p file)
8040         (not (file-regular-p file))
8041         (error "Can't read %s" file))
8042     (save-excursion
8043       (set-buffer (gnus-get-buffer-create " *import file*"))
8044       (erase-buffer)
8045       (nnheader-insert-file-contents file)
8046       (goto-char (point-min))
8047       (unless (nnheader-article-p)
8048         ;; This doesn't look like an article, so we fudge some headers.
8049         (setq atts (file-attributes file)
8050               lines (count-lines (point-min) (point-max)))
8051         (insert "From: " (read-string "From: ") "\n"
8052                 "Subject: " (read-string "Subject: ") "\n"
8053                 "Date: " (message-make-date (nth 5 atts)) "\n"
8054                 "Message-ID: " (message-make-message-id) "\n"
8055                 "Lines: " (int-to-string lines) "\n"
8056                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8057       (setq group-art (gnus-request-accept-article group nil t))
8058       (kill-buffer (current-buffer)))
8059     (setq gnus-newsgroup-active (gnus-activate-group group))
8060     (forward-line 1)
8061     (gnus-summary-goto-article (cdr group-art) nil t)
8062     (when edit
8063       (gnus-summary-edit-article))))
8064
8065 (defun gnus-summary-create-article ()
8066   "Create an article in a mail newsgroup."
8067   (interactive)
8068   (let ((group gnus-newsgroup-name)
8069         (now (current-time))
8070         group-art)
8071     (unless (gnus-check-backend-function 'request-accept-article group)
8072       (error "%s does not support article importing" group))
8073     (save-excursion
8074       (set-buffer (gnus-get-buffer-create " *import file*"))
8075       (erase-buffer)
8076       (goto-char (point-min))
8077       ;; This doesn't look like an article, so we fudge some headers.
8078       (insert "From: " (read-string "From: ") "\n"
8079               "Subject: " (read-string "Subject: ") "\n"
8080               "Date: " (message-make-date now) "\n"
8081               "Message-ID: " (message-make-message-id) "\n")
8082       (setq group-art (gnus-request-accept-article group nil t))
8083       (kill-buffer (current-buffer)))
8084     (setq gnus-newsgroup-active (gnus-activate-group group))
8085     (forward-line 1)
8086     (gnus-summary-goto-article (cdr group-art) nil t)
8087     (gnus-summary-edit-article)))
8088
8089 (defun gnus-summary-article-posted-p ()
8090   "Say whether the current (mail) article is available from news as well.
8091 This will be the case if the article has both been mailed and posted."
8092   (interactive)
8093   (let ((id (mail-header-references (gnus-summary-article-header)))
8094         (gnus-override-method (car (gnus-refer-article-methods))))
8095     (if (gnus-request-head id "")
8096         (gnus-message 2 "The current message was found on %s"
8097                       gnus-override-method)
8098       (gnus-message 2 "The current message couldn't be found on %s"
8099                     gnus-override-method)
8100       nil)))
8101
8102 (defun gnus-summary-expire-articles (&optional now)
8103   "Expire all articles that are marked as expirable in the current group."
8104   (interactive)
8105   (when (gnus-check-backend-function
8106          'request-expire-articles gnus-newsgroup-name)
8107     ;; This backend supports expiry.
8108     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8109            (expirable (if total
8110                           (progn
8111                             ;; We need to update the info for
8112                             ;; this group for `gnus-list-of-read-articles'
8113                             ;; to give us the right answer.
8114                             (gnus-run-hooks 'gnus-exit-group-hook)
8115                             (gnus-summary-update-info)
8116                             (gnus-list-of-read-articles gnus-newsgroup-name))
8117                         (setq gnus-newsgroup-expirable
8118                               (sort gnus-newsgroup-expirable '<))))
8119            (expiry-wait (if now 'immediate
8120                           (gnus-group-find-parameter
8121                            gnus-newsgroup-name 'expiry-wait)))
8122            (nnmail-expiry-target
8123             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8124                 nnmail-expiry-target))
8125            es)
8126       (when expirable
8127         ;; There are expirable articles in this group, so we run them
8128         ;; through the expiry process.
8129         (gnus-message 6 "Expiring articles...")
8130         (unless (gnus-check-group gnus-newsgroup-name)
8131           (error "Can't open server for %s" gnus-newsgroup-name))
8132         ;; The list of articles that weren't expired is returned.
8133         (save-excursion
8134           (if expiry-wait
8135               (let ((nnmail-expiry-wait-function nil)
8136                     (nnmail-expiry-wait expiry-wait))
8137                 (setq es (gnus-request-expire-articles
8138                           expirable gnus-newsgroup-name)))
8139             (setq es (gnus-request-expire-articles
8140                       expirable gnus-newsgroup-name)))
8141           (unless total
8142             (setq gnus-newsgroup-expirable es))
8143           ;; We go through the old list of expirable, and mark all
8144           ;; really expired articles as nonexistent.
8145           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
8146             (let ((gnus-use-cache nil))
8147               (while expirable
8148                 (unless (memq (car expirable) es)
8149                   (when (gnus-data-find (car expirable))
8150                     (gnus-summary-mark-article
8151                      (car expirable) gnus-canceled-mark)))
8152                 (setq expirable (cdr expirable))))))
8153         (gnus-message 6 "Expiring articles...done")))))
8154
8155 (defun gnus-summary-expire-articles-now ()
8156   "Expunge all expirable articles in the current group.
8157 This means that *all* articles that are marked as expirable will be
8158 deleted forever, right now."
8159   (interactive)
8160   (or gnus-expert-user
8161       (gnus-yes-or-no-p
8162        "Are you really, really, really sure you want to delete all these messages? ")
8163       (error "Phew!"))
8164   (gnus-summary-expire-articles t))
8165
8166 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8167 (defun gnus-summary-delete-article (&optional n)
8168   "Delete the N next (mail) articles.
8169 This command actually deletes articles.  This is not a marking
8170 command.  The article will disappear forever from your life, never to
8171 return.
8172 If N is negative, delete backwards.
8173 If N is nil and articles have been marked with the process mark,
8174 delete these instead."
8175   (interactive "P")
8176   (unless (gnus-check-backend-function 'request-expire-articles
8177                                        gnus-newsgroup-name)
8178     (error "The current newsgroup does not support article deletion"))
8179   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8180     (error "Couldn't open server"))
8181   ;; Compute the list of articles to delete.
8182   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8183         not-deleted)
8184     (if (and gnus-novice-user
8185              (not (gnus-yes-or-no-p
8186                    (format "Do you really want to delete %s forever? "
8187                            (if (> (length articles) 1)
8188                                (format "these %s articles" (length articles))
8189                              "this article")))))
8190         ()
8191       ;; Delete the articles.
8192       (setq not-deleted (gnus-request-expire-articles
8193                          articles gnus-newsgroup-name 'force))
8194       (while articles
8195         (gnus-summary-remove-process-mark (car articles))
8196         ;; The backend might not have been able to delete the article
8197         ;; after all.
8198         (unless (memq (car articles) not-deleted)
8199           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8200         (setq articles (cdr articles)))
8201       (when not-deleted
8202         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8203     (gnus-summary-position-point)
8204     (gnus-set-mode-line 'summary)
8205     not-deleted))
8206
8207 (defun gnus-summary-edit-article (&optional arg)
8208   "Edit the current article.
8209 This will have permanent effect only in mail groups.
8210 If ARG is nil, edit the decoded articles.
8211 If ARG is 1, edit the raw articles.
8212 If ARG is 2, edit the raw articles even in read-only groups.
8213 If ARG is 3, edit the articles with the current handles.
8214 Otherwise, allow editing of articles even in read-only
8215 groups."
8216   (interactive "P")
8217   (let (force raw current-handles)
8218     (cond
8219      ((null arg))
8220      ((eq arg 1) (setq raw t))
8221      ((eq arg 2) (setq raw t
8222                        force t))
8223      ((eq arg 3) (setq current-handles
8224                        (and (gnus-buffer-live-p gnus-article-buffer)
8225                             (with-current-buffer gnus-article-buffer
8226                               (prog1
8227                                   gnus-article-mime-handles
8228                                   (setq gnus-article-mime-handles nil))))))
8229      (t (setq force t)))
8230     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8231         (error "Can't edit the raw article in group nndraft:drafts"))
8232     (save-excursion
8233       (set-buffer gnus-summary-buffer)
8234       (let ((mail-parse-charset gnus-newsgroup-charset)
8235             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8236         (gnus-set-global-variables)
8237         (when (and (not force)
8238                    (gnus-group-read-only-p))
8239           (error "The current newsgroup does not support article editing"))
8240         (gnus-summary-show-article t)
8241         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8242           (with-current-buffer gnus-article-buffer
8243             (mm-enable-multibyte-mule4)))
8244         (if (equal gnus-newsgroup-name "nndraft:drafts")
8245             (setq raw t))
8246         (gnus-article-edit-article
8247          (if raw 'ignore
8248            `(lambda ()
8249               (let ((mbl mml-buffer-list))
8250                 (setq mml-buffer-list nil)
8251                 (mime-to-mml ,'current-handles)
8252                 (make-local-hook 'kill-buffer-hook)
8253                 (let ((mbl1 mml-buffer-list))
8254                   (setq mml-buffer-list mbl)
8255                   (set (make-local-variable 'mml-buffer-list) mbl1))
8256                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8257          `(lambda (no-highlight)
8258             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8259                   (message-options message-options)
8260                   (message-options-set-recipient)
8261                   (mail-parse-ignored-charsets
8262                    ',gnus-newsgroup-ignored-charsets))
8263               ,(if (not raw) '(progn
8264                                 (mml-to-mime)
8265                                 (mml-destroy-buffers)
8266                                 (remove-hook 'kill-buffer-hook
8267                                              'mml-destroy-buffers t)
8268                                 (kill-local-variable 'mml-buffer-list)))
8269               (gnus-summary-edit-article-done
8270                ,(or (mail-header-references gnus-current-headers) "")
8271                ,(gnus-group-read-only-p)
8272                ,gnus-summary-buffer no-highlight))))))))
8273
8274 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8275
8276 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8277                                                  no-highlight)
8278   "Make edits to the current article permanent."
8279   (interactive)
8280   (save-excursion
8281     ;; The buffer restriction contains the entire article if it exists.
8282     (when (article-goto-body)
8283       (let ((lines (count-lines (point) (point-max)))
8284             (length (- (point-max) (point)))
8285             (case-fold-search t)
8286             (body (copy-marker (point))))
8287         (goto-char (point-min))
8288         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8289           (delete-region (match-beginning 1) (match-end 1))
8290           (insert (number-to-string length)))
8291         (goto-char (point-min))
8292         (when (re-search-forward
8293                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8294           (delete-region (match-beginning 1) (match-end 1))
8295           (insert (number-to-string length)))
8296         (goto-char (point-min))
8297         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8298           (delete-region (match-beginning 1) (match-end 1))
8299           (insert (number-to-string lines))))))
8300   ;; Replace the article.
8301   (let ((buf (current-buffer)))
8302     (with-temp-buffer
8303       (insert-buffer-substring buf)
8304
8305       (if (and (not read-only)
8306                (not (gnus-request-replace-article
8307                      (cdr gnus-article-current) (car gnus-article-current)
8308                      (current-buffer) t)))
8309           (error "Couldn't replace article")
8310         ;; Update the summary buffer.
8311         (if (and references
8312                  (equal (message-tokenize-header references " ")
8313                         (message-tokenize-header
8314                          (or (message-fetch-field "references") "") " ")))
8315             ;; We only have to update this line.
8316             (save-excursion
8317               (save-restriction
8318                 (message-narrow-to-head)
8319                 (let ((head (buffer-string))
8320                       header)
8321                   (with-temp-buffer
8322                     (insert (format "211 %d Article retrieved.\n"
8323                                     (cdr gnus-article-current)))
8324                     (insert head)
8325                     (insert ".\n")
8326                     (let ((nntp-server-buffer (current-buffer)))
8327                       (setq header (car (gnus-get-newsgroup-headers
8328                                          (save-excursion
8329                                            (set-buffer gnus-summary-buffer)
8330                                            gnus-newsgroup-dependencies)
8331                                          t))))
8332                     (save-excursion
8333                       (set-buffer gnus-summary-buffer)
8334                       (gnus-data-set-header
8335                        (gnus-data-find (cdr gnus-article-current))
8336                        header)
8337                       (gnus-summary-update-article-line
8338                        (cdr gnus-article-current) header))))))
8339           ;; Update threads.
8340           (set-buffer (or buffer gnus-summary-buffer))
8341           (gnus-summary-update-article (cdr gnus-article-current)))
8342         ;; Prettify the article buffer again.
8343         (unless no-highlight
8344           (save-excursion
8345             (set-buffer gnus-article-buffer)
8346             ;;;!!! Fix this -- article should be rehighlighted.
8347             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8348             (set-buffer gnus-original-article-buffer)
8349             (gnus-request-article
8350              (cdr gnus-article-current)
8351              (car gnus-article-current) (current-buffer))))
8352         ;; Prettify the summary buffer line.
8353         (when (gnus-visual-p 'summary-highlight 'highlight)
8354           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8355
8356 (defun gnus-summary-edit-wash (key)
8357   "Perform editing command KEY in the article buffer."
8358   (interactive
8359    (list
8360     (progn
8361       (message "%s" (concat (this-command-keys) "- "))
8362       (read-char))))
8363   (message "")
8364   (gnus-summary-edit-article)
8365   (execute-kbd-macro (concat (this-command-keys) key))
8366   (gnus-article-edit-done))
8367
8368 ;;; Respooling
8369
8370 (defun gnus-summary-respool-query (&optional silent trace)
8371   "Query where the respool algorithm would put this article."
8372   (interactive)
8373   (let (gnus-mark-article-hook)
8374     (gnus-summary-select-article)
8375     (save-excursion
8376       (set-buffer gnus-original-article-buffer)
8377       (save-restriction
8378         (message-narrow-to-head)
8379         (let ((groups (nnmail-article-group 'identity trace)))
8380           (unless silent
8381             (if groups
8382                 (message "This message would go to %s"
8383                          (mapconcat 'car groups ", "))
8384               (message "This message would go to no groups"))
8385             groups))))))
8386
8387 (defun gnus-summary-respool-trace ()
8388   "Trace where the respool algorithm would put this article.
8389 Display a buffer showing all fancy splitting patterns which matched."
8390   (interactive)
8391   (gnus-summary-respool-query nil t))
8392
8393 ;; Summary marking commands.
8394
8395 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8396   "Mark articles which has the same subject as read, and then select the next.
8397 If UNMARK is positive, remove any kind of mark.
8398 If UNMARK is negative, tick articles."
8399   (interactive "P")
8400   (when unmark
8401     (setq unmark (prefix-numeric-value unmark)))
8402   (let ((count
8403          (gnus-summary-mark-same-subject
8404           (gnus-summary-article-subject) unmark)))
8405     ;; Select next unread article.  If auto-select-same mode, should
8406     ;; select the first unread article.
8407     (gnus-summary-next-article t (and gnus-auto-select-same
8408                                       (gnus-summary-article-subject)))
8409     (gnus-message 7 "%d article%s marked as %s"
8410                   count (if (= count 1) " is" "s are")
8411                   (if unmark "unread" "read"))))
8412
8413 (defun gnus-summary-kill-same-subject (&optional unmark)
8414   "Mark articles which has the same subject as read.
8415 If UNMARK is positive, remove any kind of mark.
8416 If UNMARK is negative, tick articles."
8417   (interactive "P")
8418   (when unmark
8419     (setq unmark (prefix-numeric-value unmark)))
8420   (let ((count
8421          (gnus-summary-mark-same-subject
8422           (gnus-summary-article-subject) unmark)))
8423     ;; If marked as read, go to next unread subject.
8424     (when (null unmark)
8425       ;; Go to next unread subject.
8426       (gnus-summary-next-subject 1 t))
8427     (gnus-message 7 "%d articles are marked as %s"
8428                   count (if unmark "unread" "read"))))
8429
8430 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8431   "Mark articles with same SUBJECT as read, and return marked number.
8432 If optional argument UNMARK is positive, remove any kinds of marks.
8433 If optional argument UNMARK is negative, mark articles as unread instead."
8434   (let ((count 1))
8435     (save-excursion
8436       (cond
8437        ((null unmark)                   ; Mark as read.
8438         (while (and
8439                 (progn
8440                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8441                   (gnus-summary-show-thread) t)
8442                 (gnus-summary-find-subject subject))
8443           (setq count (1+ count))))
8444        ((> unmark 0)                    ; Tick.
8445         (while (and
8446                 (progn
8447                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8448                   (gnus-summary-show-thread) t)
8449                 (gnus-summary-find-subject subject))
8450           (setq count (1+ count))))
8451        (t                               ; Mark as unread.
8452         (while (and
8453                 (progn
8454                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8455                   (gnus-summary-show-thread) t)
8456                 (gnus-summary-find-subject subject))
8457           (setq count (1+ count)))))
8458       (gnus-set-mode-line 'summary)
8459       ;; Return the number of marked articles.
8460       count)))
8461
8462 (defun gnus-summary-mark-as-processable (n &optional unmark)
8463   "Set the process mark on the next N articles.
8464 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8465 the process mark instead.  The difference between N and the actual
8466 number of articles marked is returned."
8467   (interactive "P")
8468   (if (and (null n) (gnus-region-active-p))
8469       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8470     (setq n (prefix-numeric-value n))
8471     (let ((backward (< n 0))
8472           (n (abs n)))
8473       (while (and
8474               (> n 0)
8475               (if unmark
8476                 (gnus-summary-remove-process-mark
8477                  (gnus-summary-article-number))
8478                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8479               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8480         (setq n (1- n)))
8481       (when (/= 0 n)
8482         (gnus-message 7 "No more articles"))
8483       (gnus-summary-recenter)
8484       (gnus-summary-position-point)
8485       n)))
8486
8487 (defun gnus-summary-unmark-as-processable (n)
8488   "Remove the process mark from the next N articles.
8489 If N is negative, unmark backward instead.  The difference between N and
8490 the actual number of articles unmarked is returned."
8491   (interactive "P")
8492   (gnus-summary-mark-as-processable n t))
8493
8494 (defun gnus-summary-unmark-all-processable ()
8495   "Remove the process mark from all articles."
8496   (interactive)
8497   (save-excursion
8498     (while gnus-newsgroup-processable
8499       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8500   (gnus-summary-position-point))
8501
8502 (defun gnus-summary-add-mark (article type)
8503   "Mark ARTICLE with a mark of TYPE."
8504   (let ((vtype (car (assq type gnus-article-mark-lists)))
8505         var)
8506     (if (not vtype)
8507         (error "No such mark type: %s" type)
8508       (setq var (intern (format "gnus-newsgroup-%s" type)))
8509       (set var (cons article (symbol-value var)))
8510       (if (memq type '(processable cached replied forwarded saved))
8511           (gnus-summary-update-secondary-mark article)
8512         ;;; !!! This is bobus.  We should find out what primary
8513         ;;; !!! mark we want to set.
8514         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8515
8516 (defun gnus-summary-mark-as-expirable (n)
8517   "Mark N articles forward as expirable.
8518 If N is negative, mark backward instead.  The difference between N and
8519 the actual number of articles marked is returned."
8520   (interactive "p")
8521   (gnus-summary-mark-forward n gnus-expirable-mark))
8522
8523 (defun gnus-summary-mark-article-as-replied (article)
8524   "Mark ARTICLE as replied to and update the summary line.
8525 ARTICLE can also be a list of articles."
8526   (interactive (list (gnus-summary-article-number)))
8527   (let ((articles (if (listp article) article (list article))))
8528     (dolist (article articles)
8529       (push article gnus-newsgroup-replied)
8530       (let ((buffer-read-only nil))
8531         (when (gnus-summary-goto-subject article nil t)
8532           (gnus-summary-update-secondary-mark article))))))
8533
8534 (defun gnus-summary-mark-article-as-forwarded (article)
8535   "Mark ARTICLE as forwarded and update the summary line.
8536 ARTICLE can also be a list of articles."
8537   (let ((articles (if (listp article) article (list article))))
8538     (dolist (article articles)
8539       (push article gnus-newsgroup-forwarded)
8540       (let ((buffer-read-only nil))
8541         (when (gnus-summary-goto-subject article nil t)
8542           (gnus-summary-update-secondary-mark article))))))
8543
8544 (defun gnus-summary-set-bookmark (article)
8545   "Set a bookmark in current article."
8546   (interactive (list (gnus-summary-article-number)))
8547   (when (or (not (get-buffer gnus-article-buffer))
8548             (not gnus-current-article)
8549             (not gnus-article-current)
8550             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8551     (error "No current article selected"))
8552   ;; Remove old bookmark, if one exists.
8553   (let ((old (assq article gnus-newsgroup-bookmarks)))
8554     (when old
8555       (setq gnus-newsgroup-bookmarks
8556             (delq old gnus-newsgroup-bookmarks))))
8557   ;; Set the new bookmark, which is on the form
8558   ;; (article-number . line-number-in-body).
8559   (push
8560    (cons article
8561          (save-excursion
8562            (set-buffer gnus-article-buffer)
8563            (count-lines
8564             (min (point)
8565                  (save-excursion
8566                    (goto-char (point-min))
8567                    (search-forward "\n\n" nil t)
8568                    (point)))
8569             (point))))
8570    gnus-newsgroup-bookmarks)
8571   (gnus-message 6 "A bookmark has been added to the current article."))
8572
8573 (defun gnus-summary-remove-bookmark (article)
8574   "Remove the bookmark from the current article."
8575   (interactive (list (gnus-summary-article-number)))
8576   ;; Remove old bookmark, if one exists.
8577   (let ((old (assq article gnus-newsgroup-bookmarks)))
8578     (if old
8579         (progn
8580           (setq gnus-newsgroup-bookmarks
8581                 (delq old gnus-newsgroup-bookmarks))
8582           (gnus-message 6 "Removed bookmark."))
8583       (gnus-message 6 "No bookmark in current article."))))
8584
8585 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8586 (defun gnus-summary-mark-as-dormant (n)
8587   "Mark N articles forward as dormant.
8588 If N is negative, mark backward instead.  The difference between N and
8589 the actual number of articles marked is returned."
8590   (interactive "p")
8591   (gnus-summary-mark-forward n gnus-dormant-mark))
8592
8593 (defun gnus-summary-set-process-mark (article)
8594   "Set the process mark on ARTICLE and update the summary line."
8595   (setq gnus-newsgroup-processable
8596         (cons article
8597               (delq article gnus-newsgroup-processable)))
8598   (when (gnus-summary-goto-subject article)
8599     (gnus-summary-show-thread)
8600     (gnus-summary-goto-subject article)
8601     (gnus-summary-update-secondary-mark article)))
8602
8603 (defun gnus-summary-remove-process-mark (article)
8604   "Remove the process mark from ARTICLE and update the summary line."
8605   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8606   (when (gnus-summary-goto-subject article)
8607     (gnus-summary-show-thread)
8608     (gnus-summary-goto-subject article)
8609     (gnus-summary-update-secondary-mark article)))
8610
8611 (defun gnus-summary-set-saved-mark (article)
8612   "Set the process mark on ARTICLE and update the summary line."
8613   (push article gnus-newsgroup-saved)
8614   (when (gnus-summary-goto-subject article)
8615     (gnus-summary-update-secondary-mark article)))
8616
8617 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8618   "Mark N articles as read forwards.
8619 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8620 The difference between N and the actual number of articles marked is
8621 returned.
8622 Iff NO-EXPIRE, auto-expiry will be inhibited."
8623   (interactive "p")
8624   (gnus-summary-show-thread)
8625   (let ((backward (< n 0))
8626         (gnus-summary-goto-unread
8627          (and gnus-summary-goto-unread
8628               (not (eq gnus-summary-goto-unread 'never))
8629               (not (memq mark (list gnus-unread-mark
8630                                     gnus-ticked-mark gnus-dormant-mark)))))
8631         (n (abs n))
8632         (mark (or mark gnus-del-mark)))
8633     (while (and (> n 0)
8634                 (gnus-summary-mark-article nil mark no-expire)
8635                 (zerop (gnus-summary-next-subject
8636                         (if backward -1 1)
8637                         (and gnus-summary-goto-unread
8638                              (not (eq gnus-summary-goto-unread 'never)))
8639                         t)))
8640       (setq n (1- n)))
8641     (when (/= 0 n)
8642       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8643     (gnus-summary-recenter)
8644     (gnus-summary-position-point)
8645     (gnus-set-mode-line 'summary)
8646     n))
8647
8648 (defun gnus-summary-mark-article-as-read (mark)
8649   "Mark the current article quickly as read with MARK."
8650   (let ((article (gnus-summary-article-number)))
8651     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8652     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8653     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8654     (push (cons article mark) gnus-newsgroup-reads)
8655     ;; Possibly remove from cache, if that is used.
8656     (when gnus-use-cache
8657       (gnus-cache-enter-remove-article article))
8658     ;; Allow the backend to change the mark.
8659     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8660     ;; Check for auto-expiry.
8661     (when (and gnus-newsgroup-auto-expire
8662                (memq mark gnus-auto-expirable-marks))
8663       (setq mark gnus-expirable-mark)
8664       ;; Let the backend know about the mark change.
8665       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8666       (push article gnus-newsgroup-expirable))
8667     ;; Set the mark in the buffer.
8668     (gnus-summary-update-mark mark 'unread)
8669     t))
8670
8671 (defun gnus-summary-mark-article-as-unread (mark)
8672   "Mark the current article quickly as unread with MARK."
8673   (let* ((article (gnus-summary-article-number))
8674          (old-mark (gnus-summary-article-mark article)))
8675     ;; Allow the backend to change the mark.
8676     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8677     (if (eq mark old-mark)
8678         t
8679       (if (<= article 0)
8680           (progn
8681             (gnus-error 1 "Can't mark negative article numbers")
8682             nil)
8683         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8684         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8685         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8686         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8687         (cond ((= mark gnus-ticked-mark)
8688                (push article gnus-newsgroup-marked))
8689               ((= mark gnus-dormant-mark)
8690                (push article gnus-newsgroup-dormant))
8691               (t
8692                (push article gnus-newsgroup-unreads)))
8693         (gnus-pull article gnus-newsgroup-reads)
8694
8695         ;; See whether the article is to be put in the cache.
8696         (and gnus-use-cache
8697              (vectorp (gnus-summary-article-header article))
8698              (save-excursion
8699                (gnus-cache-possibly-enter-article
8700                 gnus-newsgroup-name article
8701                 (= mark gnus-ticked-mark)
8702                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8703
8704         ;; Fix the mark.
8705         (gnus-summary-update-mark mark 'unread)
8706         t))))
8707
8708 (defun gnus-summary-mark-article (&optional article mark no-expire)
8709   "Mark ARTICLE with MARK.  MARK can be any character.
8710 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8711 `??' (dormant) and `?E' (expirable).
8712 If MARK is nil, then the default character `?r' is used.
8713 If ARTICLE is nil, then the article on the current line will be
8714 marked.
8715 Iff NO-EXPIRE, auto-expiry will be inhibited."
8716   ;; The mark might be a string.
8717   (when (stringp mark)
8718     (setq mark (aref mark 0)))
8719   ;; If no mark is given, then we check auto-expiring.
8720   (when (null mark)
8721     (setq mark gnus-del-mark))
8722   (when (and (not no-expire)
8723              gnus-newsgroup-auto-expire
8724              (memq mark gnus-auto-expirable-marks))
8725     (setq mark gnus-expirable-mark))
8726   (let ((article (or article (gnus-summary-article-number)))
8727         (old-mark (gnus-summary-article-mark article)))
8728     ;; Allow the backend to change the mark.
8729     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8730     (if (eq mark old-mark)
8731         t
8732       (unless article
8733         (error "No article on current line"))
8734       (if (not (if (or (= mark gnus-unread-mark)
8735                        (= mark gnus-ticked-mark)
8736                        (= mark gnus-dormant-mark))
8737                    (gnus-mark-article-as-unread article mark)
8738                  (gnus-mark-article-as-read article mark)))
8739           t
8740         ;; See whether the article is to be put in the cache.
8741         (and gnus-use-cache
8742              (not (= mark gnus-canceled-mark))
8743              (vectorp (gnus-summary-article-header article))
8744              (save-excursion
8745                (gnus-cache-possibly-enter-article
8746                 gnus-newsgroup-name article
8747                 (= mark gnus-ticked-mark)
8748                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8749
8750         (when (gnus-summary-goto-subject article nil t)
8751           (let ((buffer-read-only nil))
8752             (gnus-summary-show-thread)
8753             ;; Fix the mark.
8754             (gnus-summary-update-mark mark 'unread)
8755             t))))))
8756
8757 (defun gnus-summary-update-secondary-mark (article)
8758   "Update the secondary (read, process, cache) mark."
8759   (gnus-summary-update-mark
8760    (cond ((memq article gnus-newsgroup-processable)
8761           gnus-process-mark)
8762          ((memq article gnus-newsgroup-cached)
8763           gnus-cached-mark)
8764          ((memq article gnus-newsgroup-replied)
8765           gnus-replied-mark)
8766          ((memq article gnus-newsgroup-forwarded)
8767           gnus-forwarded-mark)
8768          ((memq article gnus-newsgroup-saved)
8769           gnus-saved-mark)
8770          (t gnus-no-mark))
8771    'replied)
8772   (when (gnus-visual-p 'summary-highlight 'highlight)
8773     (gnus-run-hooks 'gnus-summary-update-hook))
8774   t)
8775
8776 (defun gnus-summary-update-mark (mark type)
8777   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8778         (buffer-read-only nil))
8779     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8780     (when forward
8781       (when (looking-at "\r")
8782         (incf forward))
8783       (when (<= (+ forward (point)) (point-max))
8784         ;; Go to the right position on the line.
8785         (goto-char (+ forward (point)))
8786         ;; Replace the old mark with the new mark.
8787         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8788         ;; Optionally update the marks by some user rule.
8789         (when (eq type 'unread)
8790           (gnus-data-set-mark
8791            (gnus-data-find (gnus-summary-article-number)) mark)
8792           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8793
8794 (defun gnus-mark-article-as-read (article &optional mark)
8795   "Enter ARTICLE in the pertinent lists and remove it from others."
8796   ;; Make the article expirable.
8797   (let ((mark (or mark gnus-del-mark)))
8798     (if (= mark gnus-expirable-mark)
8799         (push article gnus-newsgroup-expirable)
8800       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8801     ;; Remove from unread and marked lists.
8802     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8803     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8804     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8805     (push (cons article mark) gnus-newsgroup-reads)
8806     ;; Possibly remove from cache, if that is used.
8807     (when gnus-use-cache
8808       (gnus-cache-enter-remove-article article))
8809     t))
8810
8811 (defun gnus-mark-article-as-unread (article &optional mark)
8812   "Enter ARTICLE in the pertinent lists and remove it from others."
8813   (let ((mark (or mark gnus-ticked-mark)))
8814     (if (<= article 0)
8815         (progn
8816           (gnus-error 1 "Can't mark negative article numbers")
8817           nil)
8818       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8819             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8820             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8821             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8822
8823       ;; Unsuppress duplicates?
8824       (when gnus-suppress-duplicates
8825         (gnus-dup-unsuppress-article article))
8826
8827       (cond ((= mark gnus-ticked-mark)
8828              (push article gnus-newsgroup-marked))
8829             ((= mark gnus-dormant-mark)
8830              (push article gnus-newsgroup-dormant))
8831             (t
8832              (push article gnus-newsgroup-unreads)))
8833       (gnus-pull article gnus-newsgroup-reads)
8834       t)))
8835
8836 (defalias 'gnus-summary-mark-as-unread-forward
8837   'gnus-summary-tick-article-forward)
8838 (make-obsolete 'gnus-summary-mark-as-unread-forward
8839                'gnus-summary-tick-article-forward)
8840 (defun gnus-summary-tick-article-forward (n)
8841   "Tick N articles forwards.
8842 If N is negative, tick backwards instead.
8843 The difference between N and the number of articles ticked is returned."
8844   (interactive "p")
8845   (gnus-summary-mark-forward n gnus-ticked-mark))
8846
8847 (defalias 'gnus-summary-mark-as-unread-backward
8848   'gnus-summary-tick-article-backward)
8849 (make-obsolete 'gnus-summary-mark-as-unread-backward
8850                'gnus-summary-tick-article-backward)
8851 (defun gnus-summary-tick-article-backward (n)
8852   "Tick N articles backwards.
8853 The difference between N and the number of articles ticked is returned."
8854   (interactive "p")
8855   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8856
8857 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8858 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8859 (defun gnus-summary-tick-article (&optional article clear-mark)
8860   "Mark current article as unread.
8861 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8862 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8863   (interactive)
8864   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8865                                        gnus-ticked-mark)))
8866
8867 (defun gnus-summary-mark-as-read-forward (n)
8868   "Mark N articles as read forwards.
8869 If N is negative, mark backwards instead.
8870 The difference between N and the actual number of articles marked is
8871 returned."
8872   (interactive "p")
8873   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8874
8875 (defun gnus-summary-mark-as-read-backward (n)
8876   "Mark the N articles as read backwards.
8877 The difference between N and the actual number of articles marked is
8878 returned."
8879   (interactive "p")
8880   (gnus-summary-mark-forward
8881    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8882
8883 (defun gnus-summary-mark-as-read (&optional article mark)
8884   "Mark current article as read.
8885 ARTICLE specifies the article to be marked as read.
8886 MARK specifies a string to be inserted at the beginning of the line."
8887   (gnus-summary-mark-article article mark))
8888
8889 (defun gnus-summary-clear-mark-forward (n)
8890   "Clear marks from N articles forward.
8891 If N is negative, clear backward instead.
8892 The difference between N and the number of marks cleared is returned."
8893   (interactive "p")
8894   (gnus-summary-mark-forward n gnus-unread-mark))
8895
8896 (defun gnus-summary-clear-mark-backward (n)
8897   "Clear marks from N articles backward.
8898 The difference between N and the number of marks cleared is returned."
8899   (interactive "p")
8900   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8901
8902 (defun gnus-summary-mark-unread-as-read ()
8903   "Intended to be used by `gnus-summary-mark-article-hook'."
8904   (when (memq gnus-current-article gnus-newsgroup-unreads)
8905     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8906
8907 (defun gnus-summary-mark-read-and-unread-as-read ()
8908   "Intended to be used by `gnus-summary-mark-article-hook'."
8909   (let ((mark (gnus-summary-article-mark)))
8910     (when (or (gnus-unread-mark-p mark)
8911               (gnus-read-mark-p mark))
8912       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8913
8914 (defun gnus-summary-mark-unread-as-ticked ()
8915    "Intended to be used by `gnus-summary-mark-article-hook'."
8916   (when (memq gnus-current-article gnus-newsgroup-unreads)
8917     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
8918
8919 (defun gnus-summary-mark-region-as-read (point mark all)
8920   "Mark all unread articles between point and mark as read.
8921 If given a prefix, mark all articles between point and mark as read,
8922 even ticked and dormant ones."
8923   (interactive "r\nP")
8924   (save-excursion
8925     (let (article)
8926       (goto-char point)
8927       (beginning-of-line)
8928       (while (and
8929               (< (point) mark)
8930               (progn
8931                 (when (or all
8932                           (memq (setq article (gnus-summary-article-number))
8933                                 gnus-newsgroup-unreads))
8934                   (gnus-summary-mark-article article gnus-del-mark))
8935                 t)
8936               (gnus-summary-find-next))))))
8937
8938 (defun gnus-summary-mark-below (score mark)
8939   "Mark articles with score less than SCORE with MARK."
8940   (interactive "P\ncMark: ")
8941   (setq score (if score
8942                   (prefix-numeric-value score)
8943                 (or gnus-summary-default-score 0)))
8944   (save-excursion
8945     (set-buffer gnus-summary-buffer)
8946     (goto-char (point-min))
8947     (while
8948         (progn
8949           (and (< (gnus-summary-article-score) score)
8950                (gnus-summary-mark-article nil mark))
8951           (gnus-summary-find-next)))))
8952
8953 (defun gnus-summary-kill-below (&optional score)
8954   "Mark articles with score below SCORE as read."
8955   (interactive "P")
8956   (gnus-summary-mark-below score gnus-killed-mark))
8957
8958 (defun gnus-summary-clear-above (&optional score)
8959   "Clear all marks from articles with score above SCORE."
8960   (interactive "P")
8961   (gnus-summary-mark-above score gnus-unread-mark))
8962
8963 (defun gnus-summary-tick-above (&optional score)
8964   "Tick all articles with score above SCORE."
8965   (interactive "P")
8966   (gnus-summary-mark-above score gnus-ticked-mark))
8967
8968 (defun gnus-summary-mark-above (score mark)
8969   "Mark articles with score over SCORE with MARK."
8970   (interactive "P\ncMark: ")
8971   (setq score (if score
8972                   (prefix-numeric-value score)
8973                 (or gnus-summary-default-score 0)))
8974   (save-excursion
8975     (set-buffer gnus-summary-buffer)
8976     (goto-char (point-min))
8977     (while (and (progn
8978                   (when (> (gnus-summary-article-score) score)
8979                     (gnus-summary-mark-article nil mark))
8980                   t)
8981                 (gnus-summary-find-next)))))
8982
8983 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8984 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8985 (defun gnus-summary-limit-include-expunged (&optional no-error)
8986   "Display all the hidden articles that were expunged for low scores."
8987   (interactive)
8988   (let ((buffer-read-only nil))
8989     (let ((scored gnus-newsgroup-scored)
8990           headers h)
8991       (while scored
8992         (unless (gnus-summary-article-header (caar scored))
8993           (and (setq h (gnus-number-to-header (caar scored)))
8994                (< (cdar scored) gnus-summary-expunge-below)
8995                (push h headers)))
8996         (setq scored (cdr scored)))
8997       (if (not headers)
8998           (when (not no-error)
8999             (error "No expunged articles hidden"))
9000         (goto-char (point-min))
9001         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9002         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9003         (mapcar (lambda (x) (push (mail-header-number x) 
9004                                   gnus-newsgroup-limit))
9005                 headers)
9006         (gnus-summary-prepare-unthreaded (nreverse headers))
9007         (goto-char (point-min))
9008         (gnus-summary-position-point)
9009         t))))
9010
9011 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9012   "Mark all unread articles in this newsgroup as read.
9013 If prefix argument ALL is non-nil, ticked and dormant articles will
9014 also be marked as read.
9015 If QUIETLY is non-nil, no questions will be asked.
9016 If TO-HERE is non-nil, it should be a point in the buffer.  All
9017 articles before (after, if REVERSE is set) this point will be marked as read.
9018 Note that this function will only catch up the unread article
9019 in the current summary buffer limitation.
9020 The number of articles marked as read is returned."
9021   (interactive "P")
9022   (prog1
9023       (save-excursion
9024         (when (or quietly
9025                   (not gnus-interactive-catchup) ;Without confirmation?
9026                   gnus-expert-user
9027                   (gnus-y-or-n-p
9028                    (if all
9029                        "Mark absolutely all articles as read? "
9030                      "Mark all unread articles as read? ")))
9031           (if (and not-mark
9032                    (not gnus-newsgroup-adaptive)
9033                    (not gnus-newsgroup-auto-expire)
9034                    (not gnus-suppress-duplicates)
9035                    (or (not gnus-use-cache)
9036                        (eq gnus-use-cache 'passive)))
9037               (progn
9038                 (when all
9039                   (setq gnus-newsgroup-marked nil
9040                         gnus-newsgroup-dormant nil))
9041                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9042             ;; We actually mark all articles as canceled, which we
9043             ;; have to do when using auto-expiry or adaptive scoring.
9044             (gnus-summary-show-all-threads)
9045             (if (and to-here reverse)
9046                 (progn
9047                   (goto-char to-here)
9048                   (while (and
9049                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9050                           (gnus-summary-find-next (not all) nil nil t))))
9051               (when (gnus-summary-first-subject (not all) t)
9052                 (while (and
9053                         (if to-here (< (point) to-here) t)
9054                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9055                         (gnus-summary-find-next (not all) nil nil t)))))
9056             (gnus-set-mode-line 'summary))
9057           t))
9058     (gnus-summary-position-point)))
9059
9060 (defun gnus-summary-catchup-to-here (&optional all)
9061   "Mark all unticked articles before the current one as read.
9062 If ALL is non-nil, also mark ticked and dormant articles as read."
9063   (interactive "P")
9064   (save-excursion
9065     (gnus-save-hidden-threads
9066       (let ((beg (point)))
9067         ;; We check that there are unread articles.
9068         (when (or all (gnus-summary-find-prev))
9069           (gnus-summary-catchup all t beg)))))
9070   (gnus-summary-position-point))
9071
9072 (defun gnus-summary-catchup-from-here (&optional all)
9073   "Mark all unticked articles after the current one as read.
9074 If ALL is non-nil, also mark ticked and dormant articles as read."
9075   (interactive "P")
9076   (save-excursion
9077     (gnus-save-hidden-threads
9078       (let ((beg (point)))
9079         ;; We check that there are unread articles.
9080         (when (or all (gnus-summary-find-next))
9081           (gnus-summary-catchup all t beg nil t)))))
9082
9083   (gnus-summary-position-point))
9084 (defun gnus-summary-catchup-all (&optional quietly)
9085   "Mark all articles in this newsgroup as read."
9086   (interactive "P")
9087   (gnus-summary-catchup t quietly))
9088
9089 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9090   "Mark all unread articles in this group as read, then exit.
9091 If prefix argument ALL is non-nil, all articles are marked as read.
9092 If QUIETLY is non-nil, no questions will be asked."
9093   (interactive "P")
9094   (when (gnus-summary-catchup all quietly nil 'fast)
9095     ;; Select next newsgroup or exit.
9096     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9097              (eq gnus-auto-select-next 'quietly))
9098         (gnus-summary-next-group nil)
9099       (gnus-summary-exit))))
9100
9101 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9102   "Mark all articles in this newsgroup as read, and then exit."
9103   (interactive "P")
9104   (gnus-summary-catchup-and-exit t quietly))
9105
9106 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9107   "Mark all articles in this group as read and select the next group.
9108 If given a prefix, mark all articles, unread as well as ticked, as
9109 read."
9110   (interactive "P")
9111   (save-excursion
9112     (gnus-summary-catchup all))
9113   (gnus-summary-next-group))
9114
9115 ;;;
9116 ;;; with article
9117 ;;;
9118
9119 (defmacro gnus-with-article (article &rest forms)
9120   "Select ARTICLE and perform FORMS in the original article buffer.
9121 Then replace the article with the result."
9122   `(progn
9123      ;; We don't want the article to be marked as read.
9124      (let (gnus-mark-article-hook)
9125        (gnus-summary-select-article t t nil ,article))
9126      (set-buffer gnus-original-article-buffer)
9127      ,@forms
9128      (if (not (gnus-check-backend-function
9129                'request-replace-article (car gnus-article-current)))
9130          (gnus-message 5 "Read-only group; not replacing")
9131        (unless (gnus-request-replace-article
9132                 ,article (car gnus-article-current)
9133                 (current-buffer) t)
9134          (error "Couldn't replace article")))
9135      ;; The cache and backlog have to be flushed somewhat.
9136      (when gnus-keep-backlog
9137        (gnus-backlog-remove-article
9138         (car gnus-article-current) (cdr gnus-article-current)))
9139      (when gnus-use-cache
9140        (gnus-cache-update-article
9141         (car gnus-article-current) (cdr gnus-article-current)))))
9142
9143 (put 'gnus-with-article 'lisp-indent-function 1)
9144 (put 'gnus-with-article 'edebug-form-spec '(form body))
9145
9146 ;; Thread-based commands.
9147
9148 (defun gnus-summary-articles-in-thread (&optional article)
9149   "Return a list of all articles in the current thread.
9150 If ARTICLE is non-nil, return all articles in the thread that starts
9151 with that article."
9152   (let* ((article (or article (gnus-summary-article-number)))
9153          (data (gnus-data-find-list article))
9154          (top-level (gnus-data-level (car data)))
9155          (top-subject
9156           (cond ((null gnus-thread-operation-ignore-subject)
9157                  (gnus-simplify-subject-re
9158                   (mail-header-subject (gnus-data-header (car data)))))
9159                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9160                  (gnus-simplify-subject-fuzzy
9161                   (mail-header-subject (gnus-data-header (car data)))))
9162                 (t nil)))
9163          (end-point (save-excursion
9164                       (if (gnus-summary-go-to-next-thread)
9165                           (point) (point-max))))
9166          articles)
9167     (while (and data
9168                 (< (gnus-data-pos (car data)) end-point))
9169       (when (or (not top-subject)
9170                 (string= top-subject
9171                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9172                              (gnus-simplify-subject-fuzzy
9173                               (mail-header-subject
9174                                (gnus-data-header (car data))))
9175                            (gnus-simplify-subject-re
9176                             (mail-header-subject
9177                              (gnus-data-header (car data)))))))
9178         (push (gnus-data-number (car data)) articles))
9179       (unless (and (setq data (cdr data))
9180                    (> (gnus-data-level (car data)) top-level))
9181         (setq data nil)))
9182     ;; Return the list of articles.
9183     (nreverse articles)))
9184
9185 (defun gnus-summary-rethread-current ()
9186   "Rethread the thread the current article is part of."
9187   (interactive)
9188   (let* ((gnus-show-threads t)
9189          (article (gnus-summary-article-number))
9190          (id (mail-header-id (gnus-summary-article-header)))
9191          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9192     (unless id
9193       (error "No article on the current line"))
9194     (gnus-rebuild-thread id)
9195     (gnus-summary-goto-subject article)))
9196
9197 (defun gnus-summary-reparent-thread ()
9198   "Make the current article child of the marked (or previous) article.
9199
9200 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9201 is non-nil or the Subject: of both articles are the same."
9202   (interactive)
9203   (unless (not (gnus-group-read-only-p))
9204     (error "The current newsgroup does not support article editing"))
9205   (unless (<= (length gnus-newsgroup-processable) 1)
9206     (error "No more than one article may be marked"))
9207   (save-window-excursion
9208     (let ((gnus-article-buffer " *reparent*")
9209           (current-article (gnus-summary-article-number))
9210           ;; First grab the marked article, otherwise one line up.
9211           (parent-article (if (not (null gnus-newsgroup-processable))
9212                               (car gnus-newsgroup-processable)
9213                             (save-excursion
9214                               (if (eq (forward-line -1) 0)
9215                                   (gnus-summary-article-number)
9216                                 (error "Beginning of summary buffer"))))))
9217       (unless (not (eq current-article parent-article))
9218         (error "An article may not be self-referential"))
9219       (let ((message-id (mail-header-id
9220                          (gnus-summary-article-header parent-article))))
9221         (unless (and message-id (not (equal message-id "")))
9222           (error "No message-id in desired parent"))
9223         (gnus-with-article current-article
9224           (save-restriction
9225             (goto-char (point-min))
9226             (message-narrow-to-head)
9227             (if (re-search-forward "^References: " nil t)
9228                 (progn
9229                   (re-search-forward "^[^ \t]" nil t)
9230                   (forward-line -1)
9231                   (end-of-line)
9232                   (insert " " message-id))
9233               (insert "References: " message-id "\n"))))
9234         (set-buffer gnus-summary-buffer)
9235         (gnus-summary-unmark-all-processable)
9236         (gnus-summary-update-article current-article)
9237         (gnus-summary-rethread-current)
9238         (gnus-message 3 "Article %d is now the child of article %d"
9239                       current-article parent-article)))))
9240
9241 (defun gnus-summary-toggle-threads (&optional arg)
9242   "Toggle showing conversation threads.
9243 If ARG is positive number, turn showing conversation threads on."
9244   (interactive "P")
9245   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9246     (setq gnus-show-threads
9247           (if (null arg) (not gnus-show-threads)
9248             (> (prefix-numeric-value arg) 0)))
9249     (gnus-summary-prepare)
9250     (gnus-summary-goto-subject current)
9251     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9252     (gnus-summary-position-point)))
9253
9254 (defun gnus-summary-show-all-threads ()
9255   "Show all threads."
9256   (interactive)
9257   (save-excursion
9258     (let ((buffer-read-only nil))
9259       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9260   (gnus-summary-position-point))
9261
9262 (defun gnus-summary-show-thread ()
9263   "Show thread subtrees.
9264 Returns nil if no thread was there to be shown."
9265   (interactive)
9266   (let ((buffer-read-only nil)
9267         (orig (point))
9268         ;; first goto end then to beg, to have point at beg after let
9269         (end (progn (end-of-line) (point)))
9270         (beg (progn (beginning-of-line) (point))))
9271     (prog1
9272         ;; Any hidden lines here?
9273         (search-forward "\r" end t)
9274       (subst-char-in-region beg end ?\^M ?\n t)
9275       (goto-char orig)
9276       (gnus-summary-position-point))))
9277
9278 (defun gnus-summary-hide-all-threads ()
9279   "Hide all thread subtrees."
9280   (interactive)
9281   (save-excursion
9282     (goto-char (point-min))
9283     (gnus-summary-hide-thread)
9284     (while (zerop (gnus-summary-next-thread 1 t))
9285       (gnus-summary-hide-thread)))
9286   (gnus-summary-position-point))
9287
9288 (defun gnus-summary-hide-thread ()
9289   "Hide thread subtrees.
9290 Returns nil if no threads were there to be hidden."
9291   (interactive)
9292   (let ((buffer-read-only nil)
9293         (start (point))
9294         (article (gnus-summary-article-number)))
9295     (goto-char start)
9296     ;; Go forward until either the buffer ends or the subthread
9297     ;; ends.
9298     (when (and (not (eobp))
9299                (or (zerop (gnus-summary-next-thread 1 t))
9300                    (goto-char (point-max))))
9301       (prog1
9302           (if (and (> (point) start)
9303                    (search-backward "\n" start t))
9304               (progn
9305                 (subst-char-in-region start (point) ?\n ?\^M)
9306                 (gnus-summary-goto-subject article))
9307             (goto-char start)
9308             nil)))))
9309
9310 (defun gnus-summary-go-to-next-thread (&optional previous)
9311   "Go to the same level (or less) next thread.
9312 If PREVIOUS is non-nil, go to previous thread instead.
9313 Return the article number moved to, or nil if moving was impossible."
9314   (let ((level (gnus-summary-thread-level))
9315         (way (if previous -1 1))
9316         (beg (point)))
9317     (forward-line way)
9318     (while (and (not (eobp))
9319                 (< level (gnus-summary-thread-level)))
9320       (forward-line way))
9321     (if (eobp)
9322         (progn
9323           (goto-char beg)
9324           nil)
9325       (setq beg (point))
9326       (prog1
9327           (gnus-summary-article-number)
9328         (goto-char beg)))))
9329
9330 (defun gnus-summary-next-thread (n &optional silent)
9331   "Go to the same level next N'th thread.
9332 If N is negative, search backward instead.
9333 Returns the difference between N and the number of skips actually
9334 done.
9335
9336 If SILENT, don't output messages."
9337   (interactive "p")
9338   (let ((backward (< n 0))
9339         (n (abs n)))
9340     (while (and (> n 0)
9341                 (gnus-summary-go-to-next-thread backward))
9342       (decf n))
9343     (unless silent
9344       (gnus-summary-position-point))
9345     (when (and (not silent) (/= 0 n))
9346       (gnus-message 7 "No more threads"))
9347     n))
9348
9349 (defun gnus-summary-prev-thread (n)
9350   "Go to the same level previous N'th thread.
9351 Returns the difference between N and the number of skips actually
9352 done."
9353   (interactive "p")
9354   (gnus-summary-next-thread (- n)))
9355
9356 (defun gnus-summary-go-down-thread ()
9357   "Go down one level in the current thread."
9358   (let ((children (gnus-summary-article-children)))
9359     (when children
9360       (gnus-summary-goto-subject (car children)))))
9361
9362 (defun gnus-summary-go-up-thread ()
9363   "Go up one level in the current thread."
9364   (let ((parent (gnus-summary-article-parent)))
9365     (when parent
9366       (gnus-summary-goto-subject parent))))
9367
9368 (defun gnus-summary-down-thread (n)
9369   "Go down thread N steps.
9370 If N is negative, go up instead.
9371 Returns the difference between N and how many steps down that were
9372 taken."
9373   (interactive "p")
9374   (let ((up (< n 0))
9375         (n (abs n)))
9376     (while (and (> n 0)
9377                 (if up (gnus-summary-go-up-thread)
9378                   (gnus-summary-go-down-thread)))
9379       (setq n (1- n)))
9380     (gnus-summary-position-point)
9381     (when (/= 0 n)
9382       (gnus-message 7 "Can't go further"))
9383     n))
9384
9385 (defun gnus-summary-up-thread (n)
9386   "Go up thread N steps.
9387 If N is negative, go down instead.
9388 Returns the difference between N and how many steps down that were
9389 taken."
9390   (interactive "p")
9391   (gnus-summary-down-thread (- n)))
9392
9393 (defun gnus-summary-top-thread ()
9394   "Go to the top of the thread."
9395   (interactive)
9396   (while (gnus-summary-go-up-thread))
9397   (gnus-summary-article-number))
9398
9399 (defun gnus-summary-kill-thread (&optional unmark)
9400   "Mark articles under current thread as read.
9401 If the prefix argument is positive, remove any kinds of marks.
9402 If the prefix argument is negative, tick articles instead."
9403   (interactive "P")
9404   (when unmark
9405     (setq unmark (prefix-numeric-value unmark)))
9406   (let ((articles (gnus-summary-articles-in-thread)))
9407     (save-excursion
9408       ;; Expand the thread.
9409       (gnus-summary-show-thread)
9410       ;; Mark all the articles.
9411       (while articles
9412         (gnus-summary-goto-subject (car articles))
9413         (cond ((null unmark)
9414                (gnus-summary-mark-article-as-read gnus-killed-mark))
9415               ((> unmark 0)
9416                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9417               (t
9418                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9419         (setq articles (cdr articles))))
9420     ;; Hide killed subtrees.
9421     (and (null unmark)
9422          gnus-thread-hide-killed
9423          (gnus-summary-hide-thread))
9424     ;; If marked as read, go to next unread subject.
9425     (when (null unmark)
9426       ;; Go to next unread subject.
9427       (gnus-summary-next-subject 1 t)))
9428   (gnus-set-mode-line 'summary))
9429
9430 ;; Summary sorting commands
9431
9432 (defun gnus-summary-sort-by-number (&optional reverse)
9433   "Sort the summary buffer by article number.
9434 Argument REVERSE means reverse order."
9435   (interactive "P")
9436   (gnus-summary-sort 'number reverse))
9437
9438 (defun gnus-summary-sort-by-author (&optional reverse)
9439   "Sort the summary buffer by author name alphabetically.
9440 If `case-fold-search' is non-nil, case of letters is ignored.
9441 Argument REVERSE means reverse order."
9442   (interactive "P")
9443   (gnus-summary-sort 'author reverse))
9444
9445 (defun gnus-summary-sort-by-subject (&optional reverse)
9446   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9447 If `case-fold-search' is non-nil, case of letters is ignored.
9448 Argument REVERSE means reverse order."
9449   (interactive "P")
9450   (gnus-summary-sort 'subject reverse))
9451
9452 (defun gnus-summary-sort-by-date (&optional reverse)
9453   "Sort the summary buffer by date.
9454 Argument REVERSE means reverse order."
9455   (interactive "P")
9456   (gnus-summary-sort 'date reverse))
9457
9458 (defun gnus-summary-sort-by-score (&optional reverse)
9459   "Sort the summary buffer by score.
9460 Argument REVERSE means reverse order."
9461   (interactive "P")
9462   (gnus-summary-sort 'score reverse))
9463
9464 (defun gnus-summary-sort-by-lines (&optional reverse)
9465   "Sort the summary buffer by the number of lines.
9466 Argument REVERSE means reverse order."
9467   (interactive "P")
9468   (gnus-summary-sort 'lines reverse))
9469
9470 (defun gnus-summary-sort-by-chars (&optional reverse)
9471   "Sort the summary buffer by article length.
9472 Argument REVERSE means reverse order."
9473   (interactive "P")
9474   (gnus-summary-sort 'chars reverse))
9475
9476 (defun gnus-summary-sort-by-original (&optional reverse)
9477   "Sort the summary buffer using the default sorting method.
9478 Argument REVERSE means reverse order."
9479   (interactive "P")
9480   (let* ((buffer-read-only)
9481          (gnus-summary-prepare-hook nil))
9482     ;; We do the sorting by regenerating the threads.
9483     (gnus-summary-prepare)
9484     ;; Hide subthreads if needed.
9485     (when (and gnus-show-threads gnus-thread-hide-subtree)
9486       (gnus-summary-hide-all-threads))))
9487
9488 (defun gnus-summary-sort (predicate reverse)
9489   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9490   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9491          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9492          (gnus-thread-sort-functions
9493           (if (not reverse)
9494               thread
9495             `(lambda (t1 t2)
9496                (,thread t2 t1))))
9497          (gnus-sort-gathered-threads-function
9498           gnus-thread-sort-functions)
9499          (gnus-article-sort-functions
9500           (if (not reverse)
9501               article
9502             `(lambda (t1 t2)
9503                (,article t2 t1))))
9504          (buffer-read-only)
9505          (gnus-summary-prepare-hook nil))
9506     ;; We do the sorting by regenerating the threads.
9507     (gnus-summary-prepare)
9508     ;; Hide subthreads if needed.
9509     (when (and gnus-show-threads gnus-thread-hide-subtree)
9510       (gnus-summary-hide-all-threads))))
9511
9512 ;; Summary saving commands.
9513
9514 (defun gnus-summary-save-article (&optional n not-saved)
9515   "Save the current article using the default saver function.
9516 If N is a positive number, save the N next articles.
9517 If N is a negative number, save the N previous articles.
9518 If N is nil and any articles have been marked with the process mark,
9519 save those articles instead.
9520 The variable `gnus-default-article-saver' specifies the saver function."
9521   (interactive "P")
9522   (let* ((articles (gnus-summary-work-articles n))
9523          (save-buffer (save-excursion
9524                         (nnheader-set-temp-buffer " *Gnus Save*")))
9525          (num (length articles))
9526          header file)
9527     (dolist (article articles)
9528       (setq header (gnus-summary-article-header article))
9529       (if (not (vectorp header))
9530           ;; This is a pseudo-article.
9531           (if (assq 'name header)
9532               (gnus-copy-file (cdr (assq 'name header)))
9533             (gnus-message 1 "Article %d is unsaveable" article))
9534         ;; This is a real article.
9535         (save-window-excursion
9536           (gnus-summary-select-article t nil nil article))
9537         (save-excursion
9538           (set-buffer save-buffer)
9539           (erase-buffer)
9540           (insert-buffer-substring gnus-original-article-buffer))
9541         (setq file (gnus-article-save save-buffer file num))
9542         (gnus-summary-remove-process-mark article)
9543         (unless not-saved
9544           (gnus-summary-set-saved-mark article))))
9545     (gnus-kill-buffer save-buffer)
9546     (gnus-summary-position-point)
9547     (gnus-set-mode-line 'summary)
9548     n))
9549
9550 (defun gnus-summary-pipe-output (&optional arg)
9551   "Pipe the current article to a subprocess.
9552 If N is a positive number, pipe the N next articles.
9553 If N is a negative number, pipe the N previous articles.
9554 If N is nil and any articles have been marked with the process mark,
9555 pipe those articles instead."
9556   (interactive "P")
9557   (require 'gnus-art)
9558   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9559     (gnus-summary-save-article arg t))
9560   (let ((buffer (get-buffer "*Shell Command Output*")))
9561     (if (and buffer 
9562              (with-current-buffer buffer (> (point-max) (point-min))))
9563         (gnus-configure-windows 'pipe))))
9564
9565 (defun gnus-summary-save-article-mail (&optional arg)
9566   "Append the current article to an mail file.
9567 If N is a positive number, save the N next articles.
9568 If N is a negative number, save the N previous articles.
9569 If N is nil and any articles have been marked with the process mark,
9570 save those articles instead."
9571   (interactive "P")
9572   (require 'gnus-art)
9573   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9574     (gnus-summary-save-article arg)))
9575
9576 (defun gnus-summary-save-article-rmail (&optional arg)
9577   "Append the current article to an rmail file.
9578 If N is a positive number, save the N next articles.
9579 If N is a negative number, save the N previous articles.
9580 If N is nil and any articles have been marked with the process mark,
9581 save those articles instead."
9582   (interactive "P")
9583   (require 'gnus-art)
9584   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9585     (gnus-summary-save-article arg)))
9586
9587 (defun gnus-summary-save-article-file (&optional arg)
9588   "Append the current article to a file.
9589 If N is a positive number, save the N next articles.
9590 If N is a negative number, save the N previous articles.
9591 If N is nil and any articles have been marked with the process mark,
9592 save those articles instead."
9593   (interactive "P")
9594   (require 'gnus-art)
9595   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9596     (gnus-summary-save-article arg)))
9597
9598 (defun gnus-summary-write-article-file (&optional arg)
9599   "Write the current article to a file, deleting the previous file.
9600 If N is a positive number, save the N next articles.
9601 If N is a negative number, save the N previous articles.
9602 If N is nil and any articles have been marked with the process mark,
9603 save those articles instead."
9604   (interactive "P")
9605   (require 'gnus-art)
9606   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9607     (gnus-summary-save-article arg)))
9608
9609 (defun gnus-summary-save-article-body-file (&optional arg)
9610   "Append the current article body to a file.
9611 If N is a positive number, save the N next articles.
9612 If N is a negative number, save the N previous articles.
9613 If N is nil and any articles have been marked with the process mark,
9614 save those articles instead."
9615   (interactive "P")
9616   (require 'gnus-art)
9617   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9618     (gnus-summary-save-article arg)))
9619
9620 (defun gnus-summary-pipe-message (program)
9621   "Pipe the current article through PROGRAM."
9622   (interactive "sProgram: ")
9623   (gnus-summary-select-article)
9624   (let ((mail-header-separator ""))
9625     (gnus-eval-in-buffer-window gnus-article-buffer
9626       (save-restriction
9627         (widen)
9628         (let ((start (window-start))
9629               buffer-read-only)
9630           (message-pipe-buffer-body program)
9631           (set-window-start (get-buffer-window (current-buffer)) start))))))
9632
9633 (defun gnus-get-split-value (methods)
9634   "Return a value based on the split METHODS."
9635   (let (split-name method result match)
9636     (when methods
9637       (save-excursion
9638         (set-buffer gnus-original-article-buffer)
9639         (save-restriction
9640           (nnheader-narrow-to-headers)
9641           (while (and methods (not split-name))
9642             (goto-char (point-min))
9643             (setq method (pop methods))
9644             (setq match (car method))
9645             (when (cond
9646                    ((stringp match)
9647                     ;; Regular expression.
9648                     (ignore-errors
9649                       (re-search-forward match nil t)))
9650                    ((gnus-functionp match)
9651                     ;; Function.
9652                     (save-restriction
9653                       (widen)
9654                       (setq result (funcall match gnus-newsgroup-name))))
9655                    ((consp match)
9656                     ;; Form.
9657                     (save-restriction
9658                       (widen)
9659                       (setq result (eval match)))))
9660               (setq split-name (cdr method))
9661               (cond ((stringp result)
9662                      (push (expand-file-name
9663                             result gnus-article-save-directory)
9664                            split-name))
9665                     ((consp result)
9666                      (setq split-name (append result split-name)))))))))
9667     (nreverse split-name)))
9668
9669 (defun gnus-valid-move-group-p (group)
9670   (and (boundp group)
9671        (symbol-name group)
9672        (symbol-value group)
9673        (gnus-get-function (gnus-find-method-for-group
9674                            (symbol-name group)) 'request-accept-article t)))
9675
9676 (defun gnus-read-move-group-name (prompt default articles prefix)
9677   "Read a group name."
9678   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9679          (minibuffer-confirm-incomplete nil) ; XEmacs
9680          (prom
9681           (format "%s %s to:"
9682                   prompt
9683                   (if (> (length articles) 1)
9684                       (format "these %d articles" (length articles))
9685                     "this article")))
9686          (to-newsgroup
9687           (cond
9688            ((null split-name)
9689             (gnus-completing-read default prom
9690                                   gnus-active-hashtb
9691                                   'gnus-valid-move-group-p
9692                                   nil prefix
9693                                   'gnus-group-history))
9694            ((= 1 (length split-name))
9695             (gnus-completing-read (car split-name) prom
9696                                   gnus-active-hashtb
9697                                   'gnus-valid-move-group-p
9698                                   nil nil
9699                                   'gnus-group-history))
9700            (t
9701             (gnus-completing-read nil prom
9702                                   (mapcar (lambda (el) (list el))
9703                                           (nreverse split-name))
9704                                   nil nil nil
9705                                   'gnus-group-history))))
9706          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9707     (when to-newsgroup
9708       (if (or (string= to-newsgroup "")
9709               (string= to-newsgroup prefix))
9710           (setq to-newsgroup default))
9711       (unless to-newsgroup
9712         (error "No group name entered"))
9713       (or (gnus-active to-newsgroup)
9714           (gnus-activate-group to-newsgroup nil nil to-method)
9715           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9716                                      to-newsgroup))
9717               (or (and (gnus-request-create-group to-newsgroup to-method)
9718                        (gnus-activate-group
9719                         to-newsgroup nil nil to-method)
9720                        (gnus-subscribe-group to-newsgroup))
9721                   (error "Couldn't create group %s" to-newsgroup)))
9722           (error "No such group: %s" to-newsgroup)))
9723     to-newsgroup))
9724
9725 (defun gnus-summary-save-parts (type dir n &optional reverse)
9726   "Save parts matching TYPE to DIR.
9727 If REVERSE, save parts that do not match TYPE."
9728   (interactive
9729    (list (read-string "Save parts of type: "
9730                       (or (car gnus-summary-save-parts-type-history)
9731                           gnus-summary-save-parts-default-mime)
9732                       'gnus-summary-save-parts-type-history)
9733          (setq gnus-summary-save-parts-last-directory
9734                (read-file-name "Save to directory: "
9735                                gnus-summary-save-parts-last-directory
9736                                nil t))
9737          current-prefix-arg))
9738   (gnus-summary-iterate n
9739     (let ((gnus-display-mime-function nil)
9740           (gnus-inhibit-treatment t))
9741       (gnus-summary-select-article))
9742     (save-excursion
9743       (set-buffer gnus-article-buffer)
9744       (let ((handles (or gnus-article-mime-handles
9745                          (mm-dissect-buffer) (mm-uu-dissect))))
9746         (when handles
9747           (gnus-summary-save-parts-1 type dir handles reverse)
9748           (unless gnus-article-mime-handles ;; Don't destroy this case.
9749             (mm-destroy-parts handles)))))))
9750
9751 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9752   (if (stringp (car handle))
9753       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9754               (cdr handle))
9755     (when (if reverse
9756               (not (string-match type (mm-handle-media-type handle)))
9757             (string-match type (mm-handle-media-type handle)))
9758       (let ((file (expand-file-name
9759                    (file-name-nondirectory
9760                     (or
9761                      (mail-content-type-get
9762                       (mm-handle-disposition handle) 'filename)
9763                      (concat gnus-newsgroup-name
9764                              "." (number-to-string
9765                                   (cdr gnus-article-current)))))
9766                    dir)))
9767         (unless (file-exists-p file)
9768           (mm-save-part-to-file handle file))))))
9769
9770 ;; Summary extract commands
9771
9772 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9773   (let ((buffer-read-only nil)
9774         (article (gnus-summary-article-number))
9775         after-article b e)
9776     (unless (gnus-summary-goto-subject article)
9777       (error "No such article: %d" article))
9778     (gnus-summary-position-point)
9779     ;; If all commands are to be bunched up on one line, we collect
9780     ;; them here.
9781     (unless gnus-view-pseudos-separately
9782       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9783             files action)
9784         (while ps
9785           (setq action (cdr (assq 'action (car ps))))
9786           (setq files (list (cdr (assq 'name (car ps)))))
9787           (while (and ps (cdr ps)
9788                       (string= (or action "1")
9789                                (or (cdr (assq 'action (cadr ps))) "2")))
9790             (push (cdr (assq 'name (cadr ps))) files)
9791             (setcdr ps (cddr ps)))
9792           (when files
9793             (when (not (string-match "%s" action))
9794               (push " " files))
9795             (push " " files)
9796             (when (assq 'execute (car ps))
9797               (setcdr (assq 'execute (car ps))
9798                       (funcall (if (string-match "%s" action)
9799                                    'format 'concat)
9800                                action
9801                                (mapconcat
9802                                 (lambda (f)
9803                                   (if (equal f " ")
9804                                       f
9805                                     (mm-quote-arg f)))
9806                                 files " ")))))
9807           (setq ps (cdr ps)))))
9808     (if (and gnus-view-pseudos (not not-view))
9809         (while pslist
9810           (when (assq 'execute (car pslist))
9811             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9812                                   (eq gnus-view-pseudos 'not-confirm)))
9813           (setq pslist (cdr pslist)))
9814       (save-excursion
9815         (while pslist
9816           (setq after-article (or (cdr (assq 'article (car pslist)))
9817                                   (gnus-summary-article-number)))
9818           (gnus-summary-goto-subject after-article)
9819           (forward-line 1)
9820           (setq b (point))
9821           (insert "    " (file-name-nondirectory
9822                           (cdr (assq 'name (car pslist))))
9823                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9824           (setq e (point))
9825           (forward-line -1)             ; back to `b'
9826           (gnus-add-text-properties
9827            b (1- e) (list 'gnus-number gnus-reffed-article-number
9828                           gnus-mouse-face-prop gnus-mouse-face))
9829           (gnus-data-enter
9830            after-article gnus-reffed-article-number
9831            gnus-unread-mark b (car pslist) 0 (- e b))
9832           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9833           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9834           (setq pslist (cdr pslist)))))))
9835
9836 (defun gnus-pseudos< (p1 p2)
9837   (let ((c1 (cdr (assq 'action p1)))
9838         (c2 (cdr (assq 'action p2))))
9839     (and c1 c2 (string< c1 c2))))
9840
9841 (defun gnus-request-pseudo-article (props)
9842   (cond ((assq 'execute props)
9843          (gnus-execute-command (cdr (assq 'execute props)))))
9844   (let ((gnus-current-article (gnus-summary-article-number)))
9845     (gnus-run-hooks 'gnus-mark-article-hook)))
9846
9847 (defun gnus-execute-command (command &optional automatic)
9848   (save-excursion
9849     (gnus-article-setup-buffer)
9850     (set-buffer gnus-article-buffer)
9851     (setq buffer-read-only nil)
9852     (let ((command (if automatic command
9853                      (read-string "Command: " (cons command 0)))))
9854       (erase-buffer)
9855       (insert "$ " command "\n\n")
9856       (if gnus-view-pseudo-asynchronously
9857           (start-process "gnus-execute" (current-buffer) shell-file-name
9858                          shell-command-switch command)
9859         (call-process shell-file-name nil t nil
9860                       shell-command-switch command)))))
9861
9862 ;; Summary kill commands.
9863
9864 (defun gnus-summary-edit-global-kill (article)
9865   "Edit the \"global\" kill file."
9866   (interactive (list (gnus-summary-article-number)))
9867   (gnus-group-edit-global-kill article))
9868
9869 (defun gnus-summary-edit-local-kill ()
9870   "Edit a local kill file applied to the current newsgroup."
9871   (interactive)
9872   (setq gnus-current-headers (gnus-summary-article-header))
9873   (gnus-group-edit-local-kill
9874    (gnus-summary-article-number) gnus-newsgroup-name))
9875
9876 ;;; Header reading.
9877
9878 (defun gnus-read-header (id &optional header)
9879   "Read the headers of article ID and enter them into the Gnus system."
9880   (let ((group gnus-newsgroup-name)
9881         (gnus-override-method
9882          (or
9883           gnus-override-method
9884           (and (gnus-news-group-p gnus-newsgroup-name)
9885                (car (gnus-refer-article-methods)))))
9886         where)
9887     ;; First we check to see whether the header in question is already
9888     ;; fetched.
9889     (if (stringp id)
9890         ;; This is a Message-ID.
9891         (setq header (or header (gnus-id-to-header id)))
9892       ;; This is an article number.
9893       (setq header (or header (gnus-summary-article-header id))))
9894     (if (and header
9895              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9896         ;; We have found the header.
9897         header
9898       ;; If this is a sparse article, we have to nix out its
9899       ;; previous entry in the thread hashtb.
9900       (when (and header
9901                  (gnus-summary-article-sparse-p (mail-header-number header)))
9902         (let* ((parent (gnus-parent-id (mail-header-references header)))
9903                (thread (and parent (gnus-id-to-thread parent))))
9904           (when thread
9905             (delq (assq header thread) thread))))
9906       ;; We have to really fetch the header to this article.
9907       (save-excursion
9908         (set-buffer nntp-server-buffer)
9909         (when (setq where (gnus-request-head id group))
9910           (nnheader-fold-continuation-lines)
9911           (goto-char (point-max))
9912           (insert ".\n")
9913           (goto-char (point-min))
9914           (insert "211 ")
9915           (princ (cond
9916                   ((numberp id) id)
9917                   ((cdr where) (cdr where))
9918                   (header (mail-header-number header))
9919                   (t gnus-reffed-article-number))
9920                  (current-buffer))
9921           (insert " Article retrieved.\n"))
9922         (if (or (not where)
9923                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9924             ()                          ; Malformed head.
9925           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9926             (when (and (stringp id)
9927                        (not (string= (gnus-group-real-name group)
9928                                      (car where))))
9929               ;; If we fetched by Message-ID and the article came
9930               ;; from a different group, we fudge some bogus article
9931               ;; numbers for this article.
9932               (mail-header-set-number header gnus-reffed-article-number))
9933             (save-excursion
9934               (set-buffer gnus-summary-buffer)
9935               (decf gnus-reffed-article-number)
9936               (gnus-remove-header (mail-header-number header))
9937               (push header gnus-newsgroup-headers)
9938               (setq gnus-current-headers header)
9939               (push (mail-header-number header) gnus-newsgroup-limit)))
9940           header)))))
9941
9942 (defun gnus-remove-header (number)
9943   "Remove header NUMBER from `gnus-newsgroup-headers'."
9944   (if (and gnus-newsgroup-headers
9945            (= number (mail-header-number (car gnus-newsgroup-headers))))
9946       (pop gnus-newsgroup-headers)
9947     (let ((headers gnus-newsgroup-headers))
9948       (while (and (cdr headers)
9949                   (not (= number (mail-header-number (cadr headers)))))
9950         (pop headers))
9951       (when (cdr headers)
9952         (setcdr headers (cddr headers))))))
9953
9954 ;;;
9955 ;;; summary highlights
9956 ;;;
9957
9958 (defun gnus-highlight-selected-summary ()
9959   "Highlight selected article in summary buffer."
9960   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9961   (when gnus-summary-selected-face
9962     (save-excursion
9963       (let* ((beg (progn (beginning-of-line) (point)))
9964              (end (progn (end-of-line) (point)))
9965              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9966              (from (if (get-text-property beg gnus-mouse-face-prop)
9967                        beg
9968                      (or (next-single-property-change
9969                           beg gnus-mouse-face-prop nil end)
9970                          beg)))
9971              (to
9972               (if (= from end)
9973                   (- from 2)
9974                 (or (next-single-property-change
9975                      from gnus-mouse-face-prop nil end)
9976                     end))))
9977         ;; If no mouse-face prop on line we will have to = from = end,
9978         ;; so we highlight the entire line instead.
9979         (when (= (+ to 2) from)
9980           (setq from beg)
9981           (setq to end))
9982         (if gnus-newsgroup-selected-overlay
9983             ;; Move old overlay.
9984             (gnus-move-overlay
9985              gnus-newsgroup-selected-overlay from to (current-buffer))
9986           ;; Create new overlay.
9987           (gnus-overlay-put
9988            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9989            'face gnus-summary-selected-face))))))
9990
9991 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9992 (defun gnus-summary-highlight-line ()
9993   "Highlight current line according to `gnus-summary-highlight'."
9994   (let* ((list gnus-summary-highlight)
9995          (p (point))
9996          (end (progn (end-of-line) (point)))
9997          ;; now find out where the line starts and leave point there.
9998          (beg (progn (beginning-of-line) (point)))
9999          (article (gnus-summary-article-number))
10000          (score (or (cdr (assq (or article gnus-current-article)
10001                                gnus-newsgroup-scored))
10002                     gnus-summary-default-score 0))
10003          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10004          (inhibit-read-only t))
10005     ;; Eval the cars of the lists until we find a match.
10006     (let ((default gnus-summary-default-score)
10007           (default-high gnus-summary-default-high-score)
10008           (default-low gnus-summary-default-low-score))
10009       (while (and list
10010                   (not (eval (caar list))))
10011         (setq list (cdr list))))
10012     (let ((face (cdar list)))
10013       (unless (eq face (get-text-property beg 'face))
10014         (gnus-put-text-property-excluding-characters-with-faces
10015          beg end 'face
10016          (setq face (if (boundp face) (symbol-value face) face)))
10017         (when gnus-summary-highlight-line-function
10018           (funcall gnus-summary-highlight-line-function article face))))
10019     (goto-char p)))
10020
10021 (defun gnus-update-read-articles (group unread &optional compute)
10022   "Update the list of read articles in GROUP."
10023   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10024          (entry (gnus-gethash group gnus-newsrc-hashtb))
10025          (info (nth 2 entry))
10026          (prev 1)
10027          (unread (sort (copy-sequence unread) '<))
10028          read)
10029     (if (or (not info) (not active))
10030         ;; There is no info on this group if it was, in fact,
10031         ;; killed.  Gnus stores no information on killed groups, so
10032         ;; there's nothing to be done.
10033         ;; One could store the information somewhere temporarily,
10034         ;; perhaps...  Hmmm...
10035         ()
10036       ;; Remove any negative articles numbers.
10037       (while (and unread (< (car unread) 0))
10038         (setq unread (cdr unread)))
10039       ;; Remove any expired article numbers
10040       (while (and unread (< (car unread) (car active)))
10041         (setq unread (cdr unread)))
10042       ;; Compute the ranges of read articles by looking at the list of
10043       ;; unread articles.
10044       (while unread
10045         (when (/= (car unread) prev)
10046           (push (if (= prev (1- (car unread))) prev
10047                   (cons prev (1- (car unread))))
10048                 read))
10049         (setq prev (1+ (car unread)))
10050         (setq unread (cdr unread)))
10051       (when (<= prev (cdr active))
10052         (push (cons prev (cdr active)) read))
10053       (setq read (if (> (length read) 1) (nreverse read) read))
10054       (if compute
10055           read
10056         (save-excursion
10057           (let (setmarkundo)
10058             ;; Propagate the read marks to the backend.
10059             (when (gnus-check-backend-function 'request-set-mark group)
10060               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10061                     (add (gnus-remove-from-range read (gnus-info-read info))))
10062                 (when (or add del)
10063                   (unless (gnus-check-group group)
10064                     (error "Can't open server for %s" group))
10065                   (gnus-request-set-mark
10066                    group (delq nil (list (if add (list add 'add '(read)))
10067                                          (if del (list del 'del '(read))))))
10068                   (setq setmarkundo
10069                         `(gnus-request-set-mark
10070                           ,group
10071                           ',(delq nil (list
10072                                        (if del (list del 'add '(read)))
10073                                        (if add (list add 'del '(read))))))))))
10074             (set-buffer gnus-group-buffer)
10075             (gnus-undo-register
10076               `(progn
10077                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10078                  (gnus-info-set-read ',info ',(gnus-info-read info))
10079                  (gnus-get-unread-articles-in-group ',info
10080                                                     (gnus-active ,group))
10081                  (gnus-group-update-group ,group t)
10082                  ,setmarkundo))))
10083         ;; Enter this list into the group info.
10084         (gnus-info-set-read info read)
10085         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10086         (gnus-get-unread-articles-in-group info (gnus-active group))
10087         t))))
10088
10089 (defun gnus-offer-save-summaries ()
10090   "Offer to save all active summary buffers."
10091   (let (buffers)
10092     ;; Go through all buffers and find all summaries.
10093     (dolist (buffer (buffer-list))
10094       (when (and (setq buffer (buffer-name buffer))
10095                  (string-match "Summary" buffer)
10096                  (save-excursion
10097                    (set-buffer buffer)
10098                    ;; We check that this is, indeed, a summary buffer.
10099                    (and (eq major-mode 'gnus-summary-mode)
10100                         ;; Also make sure this isn't bogus.
10101                         gnus-newsgroup-prepared
10102                         ;; Also make sure that this isn't a
10103                         ;; dead summary buffer.
10104                         (not gnus-dead-summary-mode))))
10105         (push buffer buffers)))
10106     ;; Go through all these summary buffers and offer to save them.
10107     (when buffers
10108       (save-excursion
10109         (map-y-or-n-p
10110          "Update summary buffer %s? "
10111          (lambda (buf)
10112            (switch-to-buffer buf)
10113            (gnus-summary-exit))
10114          buffers)))))
10115
10116 (defun gnus-summary-setup-default-charset ()
10117   "Setup newsgroup default charset."
10118   (if (equal gnus-newsgroup-name "nndraft:drafts")
10119       (setq gnus-newsgroup-charset nil)
10120     (let* ((ignored-charsets
10121             (or gnus-newsgroup-ephemeral-ignored-charsets
10122                 (append
10123                  (and gnus-newsgroup-name
10124                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10125                  gnus-newsgroup-ignored-charsets))))
10126       (setq gnus-newsgroup-charset
10127             (or gnus-newsgroup-ephemeral-charset
10128                 (and gnus-newsgroup-name
10129                      (gnus-parameter-charset gnus-newsgroup-name))
10130                 gnus-default-charset))
10131       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10132            ignored-charsets))))
10133
10134 ;;;
10135 ;;; Mime Commands
10136 ;;;
10137
10138 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10139   "Display the current article buffer fully MIME-buttonized.
10140 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10141 treated as multipart/mixed."
10142   (interactive "P")
10143   (require 'gnus-art)
10144   (let ((gnus-unbuttonized-mime-types nil)
10145         (gnus-mime-display-multipart-as-mixed show-all-parts))
10146     (gnus-summary-show-article)))
10147
10148 (defun gnus-summary-repair-multipart (article)
10149   "Add a Content-Type header to a multipart article without one."
10150   (interactive (list (gnus-summary-article-number)))
10151   (gnus-with-article article
10152     (message-narrow-to-head)
10153     (message-remove-header "Mime-Version")
10154     (goto-char (point-max))
10155     (insert "Mime-Version: 1.0\n")
10156     (widen)
10157     (when (search-forward "\n--" nil t)
10158       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10159         (message-narrow-to-head)
10160         (message-remove-header "Content-Type")
10161         (goto-char (point-max))
10162         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10163                         separator))
10164         (widen))))
10165   (let (gnus-mark-article-hook)
10166     (gnus-summary-select-article t t nil article)))
10167
10168 (defun gnus-summary-toggle-display-buttonized ()
10169   "Toggle the buttonizing of the article buffer."
10170   (interactive)
10171   (require 'gnus-art)
10172   (if (setq gnus-inhibit-mime-unbuttonizing
10173             (not gnus-inhibit-mime-unbuttonizing))
10174       (let ((gnus-unbuttonized-mime-types nil))
10175         (gnus-summary-show-article))
10176     (gnus-summary-show-article)))
10177
10178 ;;;
10179 ;;; Generic summary marking commands
10180 ;;;
10181
10182 (defvar gnus-summary-marking-alist
10183   '((read gnus-del-mark "d")
10184     (unread gnus-unread-mark "u")
10185     (ticked gnus-ticked-mark "!")
10186     (dormant gnus-dormant-mark "?")
10187     (expirable gnus-expirable-mark "e"))
10188   "An alist of names/marks/keystrokes.")
10189
10190 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10191 (defvar gnus-summary-mark-map)
10192
10193 (defun gnus-summary-make-all-marking-commands ()
10194   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10195   (dolist (elem gnus-summary-marking-alist)
10196     (apply 'gnus-summary-make-marking-command elem)))
10197
10198 (defun gnus-summary-make-marking-command (name mark keystroke)
10199   (let ((map (make-sparse-keymap)))
10200     (define-key gnus-summary-generic-mark-map keystroke map)
10201     (dolist (lway `((next "next" next nil "n")
10202                     (next-unread "next unread" next t "N")
10203                     (prev "previous" prev nil "p")
10204                     (prev-unread "previous unread" prev t "P")
10205                     (nomove "" nil nil ,keystroke)))
10206       (let ((func (gnus-summary-make-marking-command-1
10207                    mark (car lway) lway name)))
10208         (setq func (eval func))
10209         (define-key map (nth 4 lway) func)))))
10210
10211 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10212   `(defun ,(intern
10213             (format "gnus-summary-put-mark-as-%s%s"
10214                     name (if (eq way 'nomove)
10215                              ""
10216                            (concat "-" (symbol-name way)))))
10217      (n)
10218      ,(format
10219        "Mark the current article as %s%s.
10220 If N, the prefix, then repeat N times.
10221 If N is negative, move in reverse order.
10222 The difference between N and the actual number of articles marked is
10223 returned."
10224        name (cadr lway))
10225      (interactive "p")
10226      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10227
10228 (defun gnus-summary-generic-mark (n mark move unread)
10229   "Mark N articles with MARK."
10230   (unless (eq major-mode 'gnus-summary-mode)
10231     (error "This command can only be used in the summary buffer"))
10232   (gnus-summary-show-thread)
10233   (let ((nummove
10234          (cond
10235           ((eq move 'next) 1)
10236           ((eq move 'prev) -1)
10237           (t 0))))
10238     (if (zerop nummove)
10239         (setq n 1)
10240       (when (< n 0)
10241         (setq n (abs n)
10242               nummove (* -1 nummove))))
10243     (while (and (> n 0)
10244                 (gnus-summary-mark-article nil mark)
10245                 (zerop (gnus-summary-next-subject nummove unread t)))
10246       (setq n (1- n)))
10247     (when (/= 0 n)
10248       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10249     (gnus-summary-recenter)
10250     (gnus-summary-position-point)
10251     (gnus-set-mode-line 'summary)
10252     n))
10253
10254 (defun gnus-summary-insert-articles (articles)
10255   (when (setq articles
10256               (gnus-set-difference articles
10257                                    (mapcar (lambda (h) (mail-header-number h))
10258                                            gnus-newsgroup-headers)))
10259     (setq gnus-newsgroup-headers 
10260           (merge 'list
10261                  gnus-newsgroup-headers
10262                  (gnus-fetch-headers articles)
10263                  'gnus-article-sort-by-number))
10264     ;; Suppress duplicates?
10265     (when gnus-suppress-duplicates
10266       (gnus-dup-suppress-articles))
10267     
10268     ;; We might want to build some more threads first.
10269     (when (and gnus-fetch-old-headers
10270                (eq gnus-headers-retrieved-by 'nov))
10271       (if (eq gnus-fetch-old-headers 'invisible)
10272         (gnus-build-all-threads)
10273         (gnus-build-old-threads)))
10274     ;; Let the Gnus agent mark articles as read.
10275     (when gnus-agent
10276       (gnus-agent-get-undownloaded-list))
10277     ;; Remove list identifiers from subject
10278     (when gnus-list-identifiers
10279       (gnus-summary-remove-list-identifiers))
10280     ;; First and last article in this newsgroup.
10281     (when gnus-newsgroup-headers
10282       (setq gnus-newsgroup-begin
10283             (mail-header-number (car gnus-newsgroup-headers))
10284             gnus-newsgroup-end
10285             (mail-header-number
10286              (gnus-last-element gnus-newsgroup-headers))))
10287     (when gnus-use-scoring
10288       (gnus-possibly-score-headers))))
10289
10290 (defun gnus-summary-insert-old-articles (&optional all)
10291   "Insert all old articles in this group.
10292 If ALL is non-nil, already read articles become readable.
10293 If ALL is a number, fetch this number of articles."
10294   (interactive "P")
10295   (prog1
10296       (let ((old (mapcar 'car gnus-newsgroup-data))
10297             (i (car gnus-newsgroup-active))
10298             older len)
10299         (while (<= i (cdr gnus-newsgroup-active))
10300           (or (memq i old) (push i older))
10301           (incf i))
10302         (setq len (length older))
10303         (cond 
10304          ((null older) nil)
10305          ((numberp all) 
10306           (if (< all len)
10307               (setq older (subseq older 0 all))))
10308          (all nil)
10309          (t
10310           (if (and (numberp gnus-large-newsgroup)
10311                    (> len gnus-large-newsgroup))
10312               (let ((input
10313                      (read-string
10314                       (format
10315                        "How many articles from %s (default %d): "
10316                        (gnus-limit-string 
10317                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10318                        len))))
10319                 (unless (string-match "^[ \t]*$" input) 
10320                   (setq all (string-to-number input))
10321                   (if (< all len)
10322                       (setq older (subseq older 0 all))))))))
10323         (if (not older)
10324             (message "No old news.")
10325           (gnus-summary-insert-articles older)
10326           (gnus-summary-limit (gnus-union older old))))
10327     (gnus-summary-position-point)))
10328
10329 (defun gnus-summary-insert-new-articles ()
10330   "Insert all new articles in this group."
10331   (interactive)
10332   (prog1
10333       (let ((old (mapcar 'car gnus-newsgroup-data))
10334             (old-active gnus-newsgroup-active)
10335             (nnmail-fetched-sources (list t))
10336             i new)
10337         (setq gnus-newsgroup-active 
10338               (gnus-activate-group gnus-newsgroup-name 'scan))
10339         (setq i (1+ (cdr old-active)))
10340         (while (<= i (cdr gnus-newsgroup-active))
10341           (push i new)
10342           (incf i))
10343         (if (not new)
10344             (message "No gnus is bad news.")
10345           (setq new (nreverse new))
10346           (gnus-summary-insert-articles new)
10347           (setq gnus-newsgroup-unreads
10348                 (append gnus-newsgroup-unreads new))
10349           (gnus-summary-limit (gnus-union old new))))
10350     (gnus-summary-position-point)))
10351
10352 (gnus-summary-make-all-marking-commands)
10353
10354 (gnus-ems-redefine)
10355
10356 (provide 'gnus-sum)
10357
10358 (run-hooks 'gnus-sum-load-hook)
10359
10360 ;;; gnus-sum.el ends here