Importing Oort Gnus v0.05.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46
47 (defcustom gnus-kill-summary-on-exit t
48   "*If non-nil, kill the summary buffer when you exit from it.
49 If nil, the summary will become a \"*Dead Summary*\" buffer, and
50 it will be killed sometime later."
51   :group 'gnus-summary-exit
52   :type 'boolean)
53
54 (defcustom gnus-fetch-old-headers nil
55   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
56 If an unread article in the group refers to an older, already read (or
57 just marked as read) article, the old article will not normally be
58 displayed in the Summary buffer.  If this variable is non-nil, Gnus
59 will attempt to grab the headers to the old articles, and thereby
60 build complete threads.  If it has the value `some', only enough
61 headers to connect otherwise loose threads will be displayed.  This
62 variable can also be a number.  In that case, no more than that number
63 of old headers will be fetched.  If it has the value `invisible', all
64 old headers will be fetched, but none will be displayed.
65
66 The server has to support NOV for any of this to work."
67   :group 'gnus-thread
68   :type '(choice (const :tag "off" nil)
69                  (const some)
70                  number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-refer-thread-limit 200
74   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
75 If t, fetch all the available old headers."
76   :group 'gnus-thread
77   :type '(choice number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-summary-make-false-root 'adopt
81   "*nil means that Gnus won't gather loose threads.
82 If the root of a thread has expired or been read in a previous
83 session, the information necessary to build a complete thread has been
84 lost.  Instead of having many small sub-threads from this original thread
85 scattered all over the summary buffer, Gnus can gather them.
86
87 If non-nil, Gnus will try to gather all loose sub-threads from an
88 original thread into one large thread.
89
90 If this variable is non-nil, it should be one of `none', `adopt',
91 `dummy' or `empty'.
92
93 If this variable is `none', Gnus will not make a false root, but just
94 present the sub-threads after another.
95 If this variable is `dummy', Gnus will create a dummy root that will
96 have all the sub-threads as children.
97 If this variable is `adopt', Gnus will make one of the \"children\"
98 the parent and mark all the step-children as such.
99 If this variable is `empty', the \"children\" are printed with empty
100 subject fields.  (Or rather, they will be printed with a string
101 given by the `gnus-summary-same-subject' variable.)"
102   :group 'gnus-thread
103   :type '(choice (const :tag "off" nil)
104                  (const none)
105                  (const dummy)
106                  (const adopt)
107                  (const empty)))
108
109 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
110   "*A regexp to match subjects to be excluded from loose thread gathering.
111 As loose thread gathering is done on subjects only, that means that
112 there can be many false gatherings performed.  By rooting out certain
113 common subjects, gathering might become saner."
114   :group 'gnus-thread
115   :type 'regexp)
116
117 (defcustom gnus-summary-gather-subject-limit nil
118   "*Maximum length of subject comparisons when gathering loose threads.
119 Use nil to compare full subjects.  Setting this variable to a low
120 number will help gather threads that have been corrupted by
121 newsreaders chopping off subject lines, but it might also mean that
122 unrelated articles that have subject that happen to begin with the
123 same few characters will be incorrectly gathered.
124
125 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
126 comparing subjects."
127   :group 'gnus-thread
128   :type '(choice (const :tag "off" nil)
129                  (const fuzzy)
130                  (sexp :menu-tag "on" t)))
131
132 (defcustom gnus-simplify-subject-functions nil
133   "List of functions taking a string argument that simplify subjects.
134 The functions are applied recursively.
135
136 Useful functions to put in this list include: `gnus-simplify-subject-re',
137 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
138   :group 'gnus-thread
139   :type '(repeat function))
140
141 (defcustom gnus-simplify-ignored-prefixes nil
142   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  regexp))
146
147 (defcustom gnus-build-sparse-threads nil
148   "*If non-nil, fill in the gaps in threads.
149 If `some', only fill in the gaps that are needed to tie loose threads
150 together.  If `more', fill in all leaf nodes that Gnus can find.  If
151 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  (const some)
155                  (const more)
156                  (sexp :menu-tag "all" t)))
157
158 (defcustom gnus-summary-thread-gathering-function
159   'gnus-gather-threads-by-subject
160   "*Function used for gathering loose threads.
161 There are two pre-defined functions: `gnus-gather-threads-by-subject',
162 which only takes Subjects into consideration; and
163 `gnus-gather-threads-by-references', which compared the References
164 headers of the articles to find matches."
165   :group 'gnus-thread
166   :type '(radio (function-item gnus-gather-threads-by-subject)
167                 (function-item gnus-gather-threads-by-references)
168                 (function :tag "other")))
169
170 (defcustom gnus-summary-same-subject ""
171   "*String indicating that the current article has the same subject as the previous.
172 This variable will only be used if the value of
173 `gnus-summary-make-false-root' is `empty'."
174   :group 'gnus-summary-format
175   :type 'string)
176
177 (defcustom gnus-summary-goto-unread t
178   "*If t, many commands will go to the next unread article.
179 This applies to marking commands as well as other commands that
180 \"naturally\" select the next article, like, for instance, `SPC' at
181 the end of an article.
182
183 If nil, the marking commands do NOT go to the next unread article
184 (they go to the next article instead).  If `never', commands that
185 usually go to the next unread article, will go to the next article,
186 whether it is read or not."
187   :group 'gnus-summary-marks
188   :link '(custom-manual "(gnus)Setting Marks")
189   :type '(choice (const :tag "off" nil)
190                  (const never)
191                  (sexp :menu-tag "on" t)))
192
193 (defcustom gnus-summary-default-score 0
194   "*Default article score level.
195 All scores generated by the score files will be added to this score.
196 If this variable is nil, scoring will be disabled."
197   :group 'gnus-score-default
198   :type '(choice (const :tag "disable")
199                  integer))
200
201 (defcustom gnus-summary-default-high-score 0
202   "*Default threshold for a high scored article.
203 An article will be highlighted as high scored if its score is greater
204 than this score."
205   :group 'gnus-score-default
206   :type 'integer)
207
208 (defcustom gnus-summary-default-low-score 0
209   "*Default threshold for a low scored article.
210 An article will be highlighted as low scored if its score is smaller
211 than this score."
212   :group 'gnus-score-default
213   :type 'integer)
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 This can be a predicate specifier which says which threads to hide.
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 non-nil, select the article under point.
282 Which article this is is controlled by the `gnus-auto-select-subject'
283 variable.
284
285 If you want to prevent automatic selection of articles in some
286 newsgroups, set the variable to nil in `gnus-select-group-hook'."
287   :group 'gnus-group-select
288   :type '(choice (const :tag "none" nil)
289                  (sexp :menu-tag "first" t)))
290
291 (defcustom gnus-auto-select-subject 'unread
292   "*Says what subject to place under point when entering a group.
293
294 This variable can either be the symbols `first' (place point on the
295 first subject), `unread' (place point on the subject line of the first
296 unread article), `best' (place point on the subject line of the
297 higest-scored article), `unseen' (place point on the subject line of
298 the first unseen article), 'unseen-or-unread' (place point on the subject
299 line of the first unseen article or, if all article have been seen, on the
300 subject line of the first unread article), or a function to be called to
301 place point on some subject line.."
302   :group 'gnus-group-select
303   :type '(choice (const best)
304                  (const unread)
305                  (const first)
306                  (const unseen)
307                  (const unseen-or-unread)))
308
309 (defcustom gnus-auto-select-next t
310   "*If non-nil, offer to go to the next group from the end of the previous.
311 If the value is t and the next newsgroup is empty, Gnus will exit
312 summary mode and go back to group mode.  If the value is neither nil
313 nor t, Gnus will select the following unread newsgroup.  In
314 particular, if the value is the symbol `quietly', the next unread
315 newsgroup will be selected without any confirmation, and if it is
316 `almost-quietly', the next group will be selected without any
317 confirmation if you are located on the last article in the group.
318 Finally, if this variable is `slightly-quietly', the `Z n' command
319 will go to the next group without confirmation."
320   :group 'gnus-summary-maneuvering
321   :type '(choice (const :tag "off" nil)
322                  (const quietly)
323                  (const almost-quietly)
324                  (const slightly-quietly)
325                  (sexp :menu-tag "on" t)))
326
327 (defcustom gnus-auto-select-same nil
328   "*If non-nil, select the next article with the same subject.
329 If there are no more articles with the same subject, go to
330 the first unread article."
331   :group 'gnus-summary-maneuvering
332   :type 'boolean)
333
334 (defcustom gnus-summary-check-current nil
335   "*If non-nil, consider the current article when moving.
336 The \"unread\" movement commands will stay on the same line if the
337 current article is unread."
338   :group 'gnus-summary-maneuvering
339   :type 'boolean)
340
341 (defcustom gnus-auto-center-summary t
342   "*If non-nil, always center the current summary buffer.
343 In particular, if `vertical' do only vertical recentering.  If non-nil
344 and non-`vertical', do both horizontal and vertical recentering."
345   :group 'gnus-summary-maneuvering
346   :type '(choice (const :tag "none" nil)
347                  (const vertical)
348                  (integer :tag "height")
349                  (sexp :menu-tag "both" t)))
350
351 (defcustom gnus-show-all-headers nil
352   "*If non-nil, don't hide any headers."
353   :group 'gnus-article-hiding
354   :group 'gnus-article-headers
355   :type 'boolean)
356
357 (defcustom gnus-summary-ignore-duplicates nil
358   "*If non-nil, ignore articles with identical Message-ID headers."
359   :group 'gnus-summary
360   :type 'boolean)
361
362 (defcustom gnus-single-article-buffer t
363   "*If non-nil, display all articles in the same buffer.
364 If nil, each group will get its own article buffer."
365   :group 'gnus-article-various
366   :type 'boolean)
367
368 (defcustom gnus-break-pages t
369   "*If non-nil, do page breaking on articles.
370 The page delimiter is specified by the `gnus-page-delimiter'
371 variable."
372   :group 'gnus-article-various
373   :type 'boolean)
374
375 (defcustom gnus-move-split-methods nil
376   "*Variable used to suggest where articles are to be moved to.
377 It uses the same syntax as the `gnus-split-methods' variable.
378 However, whereas `gnus-split-methods' specifies file names as targets,
379 this variable specifies group names."
380   :group 'gnus-summary-mail
381   :type '(repeat (choice (list :value (fun) function)
382                          (cons :value ("" "") regexp (repeat string))
383                          (sexp :value nil))))
384
385 (defcustom gnus-unread-mark ?           ;Whitespace
386   "*Mark used for unread articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-ticked-mark ?!
391   "*Mark used for ticked articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-dormant-mark ??
396   "*Mark used for dormant articles."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-del-mark ?r
401   "*Mark used for del'd articles."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-read-mark ?R
406   "*Mark used for read articles."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-expirable-mark ?E
411   "*Mark used for expirable articles."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-killed-mark ?K
416   "*Mark used for killed articles."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-souped-mark ?F
421   "*Mark used for souped articles."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-kill-file-mark ?X
426   "*Mark used for articles killed by kill files."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-low-score-mark ?Y
431   "*Mark used for articles with a low score."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-catchup-mark ?C
436   "*Mark used for articles that are caught up."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-replied-mark ?A
441   "*Mark used for articles that have been replied to."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-forwarded-mark ?F
446   "*Mark used for articles that have been forwarded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-recent-mark ?N
451   "*Mark used for articles that are recent."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-cached-mark ?*
456   "*Mark used for articles that are in the cache."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-saved-mark ?S
461   "*Mark used for articles that have been saved."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-unseen-mark ?.
466   "*Mark used for articles that haven't been seen."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-no-mark ?               ;Whitespace
471   "*Mark used for articles that have no other secondary mark."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-ancient-mark ?O
476   "*Mark used for ancient articles."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-sparse-mark ?Q
481   "*Mark used for sparsely reffed articles."
482   :group 'gnus-summary-marks
483   :type 'character)
484
485 (defcustom gnus-canceled-mark ?G
486   "*Mark used for canceled articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-duplicate-mark ?M
491   "*Mark used for duplicate articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-undownloaded-mark ?@
496   "*Mark used for articles that weren't downloaded."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-downloadable-mark ?%
501   "*Mark used for articles that are to be downloaded."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-unsendable-mark ?=
506   "*Mark used for articles that won't be sent."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-score-over-mark ?+
511   "*Score mark used for articles with high scores."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-score-below-mark ?-
516   "*Score mark used for articles with low scores."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-empty-thread-mark ?     ;Whitespace
521   "*There is no thread under the article."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-not-empty-thread-mark ?=
526   "*There is a thread under the article."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-view-pseudo-asynchronously nil
531   "*If non-nil, Gnus will view pseudo-articles asynchronously."
532   :group 'gnus-extract-view
533   :type 'boolean)
534
535 (defcustom gnus-auto-expirable-marks
536   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
537         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
538         gnus-souped-mark gnus-duplicate-mark)
539   "*The list of marks converted into expiration if a group is auto-expirable."
540   :version "21.1"
541   :group 'gnus-summary
542   :type '(repeat character))
543
544 (defcustom gnus-inhibit-user-auto-expire t
545   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
546   :version "21.1"
547   :group 'gnus-summary
548   :type 'boolean)
549
550 (defcustom gnus-view-pseudos nil
551   "*If `automatic', pseudo-articles will be viewed automatically.
552 If `not-confirm', pseudos will be viewed automatically, and the user
553 will not be asked to confirm the command."
554   :group 'gnus-extract-view
555   :type '(choice (const :tag "off" nil)
556                  (const automatic)
557                  (const not-confirm)))
558
559 (defcustom gnus-view-pseudos-separately t
560   "*If non-nil, one pseudo-article will be created for each file to be viewed.
561 If nil, all files that use the same viewing command will be given as a
562 list of parameters to that command."
563   :group 'gnus-extract-view
564   :type 'boolean)
565
566 (defcustom gnus-insert-pseudo-articles t
567   "*If non-nil, insert pseudo-articles when decoding articles."
568   :group 'gnus-extract-view
569   :type 'boolean)
570
571 (defcustom gnus-summary-dummy-line-format
572   "  %(:                          :%) %S\n"
573   "*The format specification for the dummy roots in the summary buffer.
574 It works along the same lines as a normal formatting string,
575 with some simple extensions.
576
577 %S  The subject
578
579 General format specifiers can also be used.  
580 See (gnus)Formatting Variables."
581   :link '(custom-manual "(gnus)Formatting Variables")
582   :group 'gnus-threading
583   :type 'string)
584
585 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
586   "*The format specification for the summary mode line.
587 It works along the same lines as a normal formatting string,
588 with some simple extensions:
589
590 %G  Group name
591 %p  Unprefixed group name
592 %A  Current article number
593 %z  Current article score
594 %V  Gnus version
595 %U  Number of unread articles in the group
596 %e  Number of unselected articles in the group
597 %Z  A string with unread/unselected article counts
598 %g  Shortish group name
599 %S  Subject of the current article
600 %u  User-defined spec
601 %s  Current score file name
602 %d  Number of dormant articles
603 %r  Number of articles that have been marked as read in this session
604 %E  Number of articles expunged by the score files"
605   :group 'gnus-summary-format
606   :type 'string)
607
608 (defcustom gnus-list-identifiers nil
609   "Regexp that matches list identifiers to be removed from subject.
610 This can also be a list of regexps."
611   :version "21.1"
612   :group 'gnus-summary-format
613   :group 'gnus-article-hiding
614   :type '(choice (const :tag "none" nil)
615                  (regexp :value ".*")
616                  (repeat :value (".*") regexp)))
617
618 (defcustom gnus-summary-mark-below 0
619   "*Mark all articles with a score below this variable as read.
620 This variable is local to each summary buffer and usually set by the
621 score file."
622   :group 'gnus-score-default
623   :type 'integer)
624
625 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
626   "*List of functions used for sorting articles in the summary buffer.
627
628 Each function takes two articles and returns non-nil if the first
629 article should be sorted before the other.  If you use more than one
630 function, the primary sort function should be the last.  You should
631 probably always include `gnus-article-sort-by-number' in the list of
632 sorting functions -- preferably first.  Also note that sorting by date
633 is often much slower than sorting by number, and the sorting order is
634 very similar.  (Sorting by date means sorting by the time the message
635 was sent, sorting by number means sorting by arrival time.)
636
637 Ready-made functions include `gnus-article-sort-by-number',
638 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
639 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
640
641 When threading is turned on, the variable `gnus-thread-sort-functions'
642 controls how articles are sorted."
643   :group 'gnus-summary-sort
644   :type '(repeat (choice (function-item gnus-article-sort-by-number)
645                          (function-item gnus-article-sort-by-author)
646                          (function-item gnus-article-sort-by-subject)
647                          (function-item gnus-article-sort-by-date)
648                          (function-item gnus-article-sort-by-score)
649                          (function :tag "other"))))
650
651 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
652   "*List of functions used for sorting threads in the summary buffer.
653 By default, threads are sorted by article number.
654
655 Each function takes two threads and returns non-nil if the first
656 thread should be sorted before the other.  If you use more than one
657 function, the primary sort function should be the last.  You should
658 probably always include `gnus-thread-sort-by-number' in the list of
659 sorting functions -- preferably first.  Also note that sorting by date
660 is often much slower than sorting by number, and the sorting order is
661 very similar.  (Sorting by date means sorting by the time the message
662 was sent, sorting by number means sorting by arrival time.)
663
664 Ready-made functions include `gnus-thread-sort-by-number',
665 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
666 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
667 `gnus-thread-sort-by-most-recent-number',
668 `gnus-thread-sort-by-most-recent-date', and
669 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
670
671 When threading is turned off, the variable
672 `gnus-article-sort-functions' controls how articles are sorted."
673   :group 'gnus-summary-sort
674   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
675                          (function-item gnus-thread-sort-by-author)
676                          (function-item gnus-thread-sort-by-subject)
677                          (function-item gnus-thread-sort-by-date)
678                          (function-item gnus-thread-sort-by-score)
679                          (function-item gnus-thread-sort-by-total-score)
680                          (function :tag "other"))))
681
682 (defcustom gnus-thread-score-function '+
683   "*Function used for calculating the total score of a thread.
684
685 The function is called with the scores of the article and each
686 subthread and should then return the score of the thread.
687
688 Some functions you can use are `+', `max', or `min'."
689   :group 'gnus-summary-sort
690   :type 'function)
691
692 (defcustom gnus-summary-expunge-below nil
693   "All articles that have a score less than this variable will be expunged.
694 This variable is local to the summary buffers."
695   :group 'gnus-score-default
696   :type '(choice (const :tag "off" nil)
697                  integer))
698
699 (defcustom gnus-thread-expunge-below nil
700   "All threads that have a total score less than this variable will be expunged.
701 See `gnus-thread-score-function' for en explanation of what a
702 \"thread score\" is.
703
704 This variable is local to the summary buffers."
705   :group 'gnus-threading
706   :group 'gnus-score-default
707   :type '(choice (const :tag "off" nil)
708                  integer))
709
710 (defcustom gnus-summary-mode-hook nil
711   "*A hook for Gnus summary mode.
712 This hook is run before any variables are set in the summary buffer."
713   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
714   :group 'gnus-summary-various
715   :type 'hook)
716
717 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
718 (when (featurep 'xemacs)
719   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
720   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
721   (add-hook 'gnus-summary-mode-hook
722             'gnus-xmas-switch-horizontal-scrollbar-off))
723
724 (defcustom gnus-summary-menu-hook nil
725   "*Hook run after the creation of the summary mode menu."
726   :group 'gnus-summary-visual
727   :type 'hook)
728
729 (defcustom gnus-summary-exit-hook nil
730   "*A hook called on exit from the summary buffer.
731 It will be called with point in the group buffer."
732   :group 'gnus-summary-exit
733   :type 'hook)
734
735 (defcustom gnus-summary-prepare-hook nil
736   "*A hook called after the summary buffer has been generated.
737 If you want to modify the summary buffer, you can use this hook."
738   :group 'gnus-summary-various
739   :type 'hook)
740
741 (defcustom gnus-summary-prepared-hook nil
742   "*A hook called as the last thing after the summary buffer has been generated."
743   :group 'gnus-summary-various
744   :type 'hook)
745
746 (defcustom gnus-summary-generate-hook nil
747   "*A hook run just before generating the summary buffer.
748 This hook is commonly used to customize threading variables and the
749 like."
750   :group 'gnus-summary-various
751   :type 'hook)
752
753 (defcustom gnus-select-group-hook nil
754   "*A hook called when a newsgroup is selected.
755
756 If you'd like to simplify subjects like the
757 `gnus-summary-next-same-subject' command does, you can use the
758 following hook:
759
760  (add-hook gnus-select-group-hook
761            (lambda ()
762              (mapcar (lambda (header)
763                        (mail-header-set-subject
764                         header
765                         (gnus-simplify-subject
766                          (mail-header-subject header) 're-only)))
767                      gnus-newsgroup-headers)))"
768   :group 'gnus-group-select
769   :type 'hook)
770
771 (defcustom gnus-select-article-hook nil
772   "*A hook called when an article is selected."
773   :group 'gnus-summary-choose
774   :type 'hook)
775
776 (defcustom gnus-visual-mark-article-hook
777   (list 'gnus-highlight-selected-summary)
778   "*Hook run after selecting an article in the summary buffer.
779 It is meant to be used for highlighting the article in some way.  It
780 is not run if `gnus-visual' is nil."
781   :group 'gnus-summary-visual
782   :type 'hook)
783
784 (defcustom gnus-parse-headers-hook nil
785   "*A hook called before parsing the headers."
786   :group 'gnus-various
787   :type 'hook)
788
789 (defcustom gnus-exit-group-hook nil
790   "*A hook called when exiting summary mode.
791 This hook is not called from the non-updating exit commands like `Q'."
792   :group 'gnus-various
793   :type 'hook)
794
795 (defcustom gnus-summary-update-hook
796   (list 'gnus-summary-highlight-line)
797   "*A hook called when a summary line is changed.
798 The hook will not be called if `gnus-visual' is nil.
799
800 The default function `gnus-summary-highlight-line' will
801 highlight the line according to the `gnus-summary-highlight'
802 variable."
803   :group 'gnus-summary-visual
804   :type 'hook)
805
806 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
807   "*A hook called when an article is selected for the first time.
808 The hook is intended to mark an article as read (or unread)
809 automatically when it is selected."
810   :group 'gnus-summary-choose
811   :type 'hook)
812
813 (defcustom gnus-group-no-more-groups-hook nil
814   "*A hook run when returning to group mode having no more (unread) groups."
815   :group 'gnus-group-select
816   :type 'hook)
817
818 (defcustom gnus-ps-print-hook nil
819   "*A hook run before ps-printing something from Gnus."
820   :group 'gnus-summary
821   :type 'hook)
822
823 (defcustom gnus-summary-display-arrow
824   (and (fboundp 'display-graphic-p)
825        (display-graphic-p))
826   "*If non-nil, display an arrow highlighting the current article."
827   :version "21.1"
828   :group 'gnus-summary
829   :type 'boolean)
830
831 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
832   "Face used for highlighting the current article in the summary buffer."
833   :group 'gnus-summary-visual
834   :type 'face)
835
836 (defcustom gnus-summary-highlight
837   '(((= mark gnus-canceled-mark)
838      . gnus-summary-cancelled-face)
839     ((and (> score default-high)
840           (or (= mark gnus-dormant-mark)
841               (= mark gnus-ticked-mark)))
842      . gnus-summary-high-ticked-face)
843     ((and (< score default-low)
844           (or (= mark gnus-dormant-mark)
845               (= mark gnus-ticked-mark)))
846      . gnus-summary-low-ticked-face)
847     ((or (= mark gnus-dormant-mark)
848          (= mark gnus-ticked-mark))
849      . gnus-summary-normal-ticked-face)
850     ((and (> score default-high) (= mark gnus-ancient-mark))
851      . gnus-summary-high-ancient-face)
852     ((and (< score default-low) (= mark gnus-ancient-mark))
853      . gnus-summary-low-ancient-face)
854     ((= mark gnus-ancient-mark)
855      . gnus-summary-normal-ancient-face)
856     ((and (> score default-high) (= mark gnus-unread-mark))
857      . gnus-summary-high-unread-face)
858     ((and (< score default-low) (= mark gnus-unread-mark))
859      . gnus-summary-low-unread-face)
860     ((= mark gnus-unread-mark)
861      . gnus-summary-normal-unread-face)
862     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
863                                                   gnus-undownloaded-mark)))
864      . gnus-summary-high-unread-face)
865     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
866                                                  gnus-undownloaded-mark)))
867      . gnus-summary-low-unread-face)
868     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
869           (memq article gnus-newsgroup-unreads))
870      . gnus-summary-normal-unread-face)
871     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
872      . gnus-summary-normal-read-face)
873     ((> score default-high)
874      . gnus-summary-high-read-face)
875     ((< score default-low)
876      . gnus-summary-low-read-face)
877     (t
878      . gnus-summary-normal-read-face))
879   "*Controls the highlighting of summary buffer lines.
880
881 A list of (FORM . FACE) pairs.  When deciding how a a particular
882 summary line should be displayed, each form is evaluated.  The content
883 of the face field after the first true form is used.  You can change
884 how those summary lines are displayed, by editing the face field.
885
886 You can use the following variables in the FORM field.
887
888 score:        The article's score
889 default:      The default article score.
890 default-high: The default score for high scored articles.
891 default-low:  The default score for low scored articles.
892 below:        The score below which articles are automatically marked as read.
893 mark:         The articles mark."
894   :group 'gnus-summary-visual
895   :type '(repeat (cons (sexp :tag "Form" nil)
896                        face)))
897
898 (defcustom gnus-alter-header-function nil
899   "Function called to allow alteration of article header structures.
900 The function is called with one parameter, the article header vector,
901 which it may alter in any way.")
902
903 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
904   "Variable that says which function should be used to decode a string with encoded words.")
905
906 (defcustom gnus-extra-headers '(To Newsgroups)
907   "*Extra headers to parse."
908   :version "21.1"
909   :group 'gnus-summary
910   :type '(repeat symbol))
911
912 (defcustom gnus-ignored-from-addresses
913   (and user-mail-address (regexp-quote user-mail-address))
914   "*Regexp of From headers that may be suppressed in favor of To headers."
915   :version "21.1"
916   :group 'gnus-summary
917   :type 'regexp)
918
919 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
920   "List of charsets that should be ignored.
921 When these charsets are used in the \"charset\" parameter, the
922 default charset will be used instead."
923   :version "21.1"
924   :type '(repeat symbol)
925   :group 'gnus-charset)
926
927 (gnus-define-group-parameter
928  ignored-charsets
929  :type list
930  :function-document
931  "Return the ignored charsets of GROUP."
932  :variable gnus-group-ignored-charsets-alist
933  :variable-default
934  '(("alt\\.chinese\\.text" iso-8859-1))
935  :variable-document
936  "Alist of regexps (to match group names) and charsets that should be ignored.
937 When these charsets are used in the \"charset\" parameter, the
938 default charset will be used instead."
939  :variable-group gnus-charset
940  :variable-type '(repeat (cons (regexp :tag "Group")
941                                (repeat symbol)))
942  :parameter-type '(choice :tag "Ignored charsets"
943                           :value nil
944                           (repeat (symbol)))
945  :parameter-document       "\
946 List of charsets that should be ignored.
947
948 When these charsets are used in the \"charset\" parameter, the
949 default charset will be used instead.")
950
951 (defcustom gnus-group-highlight-words-alist nil
952   "Alist of group regexps and highlight regexps.
953 This variable uses the same syntax as `gnus-emphasis-alist'."
954   :version "21.1"
955   :type '(repeat (cons (regexp :tag "Group")
956                        (repeat (list (regexp :tag "Highlight regexp")
957                                      (number :tag "Group for entire word" 0)
958                                      (number :tag "Group for displayed part" 0)
959                                      (symbol :tag "Face"
960                                              gnus-emphasis-highlight-words)))))
961   :group 'gnus-summary-visual)
962
963 (defcustom gnus-summary-show-article-charset-alist
964   nil
965   "Alist of number and charset.
966 The article will be shown with the charset corresponding to the
967 numbered argument.
968 For example: ((1 . cn-gb-2312) (2 . big5))."
969   :version "21.1"
970   :type '(repeat (cons (number :tag "Argument" 1)
971                        (symbol :tag "Charset")))
972   :group 'gnus-charset)
973
974 (defcustom gnus-preserve-marks t
975   "Whether marks are preserved when moving, copying and respooling messages."
976   :version "21.1"
977   :type 'boolean
978   :group 'gnus-summary-marks)
979
980 (defcustom gnus-alter-articles-to-read-function nil
981   "Function to be called to alter the list of articles to be selected."
982   :type '(choice (const nil) function)
983   :group 'gnus-summary)
984
985 (defcustom gnus-orphan-score nil
986   "*All orphans get this score added.  Set in the score file."
987   :group 'gnus-score-default
988   :type '(choice (const nil)
989                  integer))
990
991 (defcustom gnus-summary-save-parts-default-mime "image/.*"
992   "*A regexp to match MIME parts when saving multiple parts of a message
993 with gnus-summary-save-parts (X m). This regexp will be used by default
994 when prompting the user for which type of files to save."
995   :group 'gnus-summary
996   :type 'regexp)
997
998
999 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1000   "*A regexp to match MIME parts when saving multiple parts of a message
1001 with gnus-summary-save-parts (X m). This regexp will be used by default
1002 when prompting the user for which type of files to save."
1003   :group 'gnus-summary
1004   :type 'regexp)
1005
1006 (defcustom gnus-read-all-available-headers nil
1007   "Whether Gnus should parse all headers made available to it.
1008 This is mostly relevant for slow backends where the user may
1009 wish to widen the summary buffer to include all headers
1010 that were fetched.  Say, for nnultimate groups."
1011   :group 'gnus-summary
1012   :type '(choice boolean regexp))
1013
1014 (defcustom gnus-summary-muttprint-program "muttprint"
1015   "Command (and optional arguments) used to run Muttprint."
1016   :group 'gnus-summary
1017   :type 'string)
1018
1019 ;;; Internal variables
1020
1021 (defvar gnus-summary-display-cache nil)
1022 (defvar gnus-article-mime-handles nil)
1023 (defvar gnus-article-decoded-p nil)
1024 (defvar gnus-article-charset nil)
1025 (defvar gnus-article-ignored-charsets nil)
1026 (defvar gnus-scores-exclude-files nil)
1027 (defvar gnus-page-broken nil)
1028 (defvar gnus-inhibit-mime-unbuttonizing nil)
1029
1030 (defvar gnus-original-article nil)
1031 (defvar gnus-article-internal-prepare-hook nil)
1032 (defvar gnus-newsgroup-process-stack nil)
1033
1034 (defvar gnus-thread-indent-array nil)
1035 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1036 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1037   "Function called to sort the articles within a thread after it has been gathered together.")
1038
1039 (defvar gnus-summary-save-parts-type-history nil)
1040 (defvar gnus-summary-save-parts-last-directory nil)
1041
1042 (defvar gnus-summary-save-parts-type-history nil)
1043 (defvar gnus-summary-save-parts-last-directory nil)
1044
1045 ;; Avoid highlighting in kill files.
1046 (defvar gnus-summary-inhibit-highlight nil)
1047 (defvar gnus-newsgroup-selected-overlay nil)
1048 (defvar gnus-inhibit-limiting nil)
1049 (defvar gnus-newsgroup-adaptive-score-file nil)
1050 (defvar gnus-current-score-file nil)
1051 (defvar gnus-current-move-group nil)
1052 (defvar gnus-current-copy-group nil)
1053 (defvar gnus-current-crosspost-group nil)
1054 (defvar gnus-newsgroup-display nil)
1055
1056 (defvar gnus-newsgroup-dependencies nil)
1057 (defvar gnus-newsgroup-adaptive nil)
1058 (defvar gnus-summary-display-article-function nil)
1059 (defvar gnus-summary-highlight-line-function nil
1060   "Function called after highlighting a summary line.")
1061
1062 (defvar gnus-summary-line-format-alist
1063   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1064     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1065     (?s gnus-tmp-subject-or-nil ?s)
1066     (?n gnus-tmp-name ?s)
1067     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1068         ?s)
1069     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1070             gnus-tmp-from) ?s)
1071     (?F gnus-tmp-from ?s)
1072     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1073     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1074     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1075     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1076     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1077     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1078     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1079     (?L gnus-tmp-lines ?s)
1080     (?I gnus-tmp-indentation ?s)
1081     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1082     (?R gnus-tmp-replied ?c)
1083     (?\[ gnus-tmp-opening-bracket ?c)
1084     (?\] gnus-tmp-closing-bracket ?c)
1085     (?\> (make-string gnus-tmp-level ? ) ?s)
1086     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1087     (?i gnus-tmp-score ?d)
1088     (?z gnus-tmp-score-char ?c)
1089     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1090     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1091     (?U gnus-tmp-unread ?c)
1092     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1093         ?s)
1094     (?t (gnus-summary-number-of-articles-in-thread
1095          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1096         ?d)
1097     (?e (gnus-summary-number-of-articles-in-thread
1098          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1099         ?c)
1100     (?u gnus-tmp-user-defined ?s)
1101     (?P (gnus-pick-line-number) ?d)
1102     (?B gnus-tmp-thread-tree-header-string ?s)
1103     (user-date (gnus-user-date
1104                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1105   "An alist of format specifications that can appear in summary lines.
1106 These are paired with what variables they correspond with, along with
1107 the type of the variable (string, integer, character, etc).")
1108
1109 (defvar gnus-summary-dummy-line-format-alist
1110   `((?S gnus-tmp-subject ?s)
1111     (?N gnus-tmp-number ?d)
1112     (?u gnus-tmp-user-defined ?s)))
1113
1114 (defvar gnus-summary-mode-line-format-alist
1115   `((?G gnus-tmp-group-name ?s)
1116     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1117     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1118     (?A gnus-tmp-article-number ?d)
1119     (?Z gnus-tmp-unread-and-unselected ?s)
1120     (?V gnus-version ?s)
1121     (?U gnus-tmp-unread-and-unticked ?d)
1122     (?S gnus-tmp-subject ?s)
1123     (?e gnus-tmp-unselected ?d)
1124     (?u gnus-tmp-user-defined ?s)
1125     (?d (length gnus-newsgroup-dormant) ?d)
1126     (?t (length gnus-newsgroup-marked) ?d)
1127     (?r (length gnus-newsgroup-reads) ?d)
1128     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1129     (?E gnus-newsgroup-expunged-tally ?d)
1130     (?s (gnus-current-score-file-nondirectory) ?s)))
1131
1132 (defvar gnus-last-search-regexp nil
1133   "Default regexp for article search command.")
1134
1135 (defvar gnus-last-shell-command nil
1136   "Default shell command on article.")
1137
1138 (defvar gnus-newsgroup-begin nil)
1139 (defvar gnus-newsgroup-end nil)
1140 (defvar gnus-newsgroup-last-rmail nil)
1141 (defvar gnus-newsgroup-last-mail nil)
1142 (defvar gnus-newsgroup-last-folder nil)
1143 (defvar gnus-newsgroup-last-file nil)
1144 (defvar gnus-newsgroup-auto-expire nil)
1145 (defvar gnus-newsgroup-active nil)
1146
1147 (defvar gnus-newsgroup-data nil)
1148 (defvar gnus-newsgroup-data-reverse nil)
1149 (defvar gnus-newsgroup-limit nil)
1150 (defvar gnus-newsgroup-limits nil)
1151
1152 (defvar gnus-newsgroup-unreads nil
1153   "List of unread articles in the current newsgroup.")
1154
1155 (defvar gnus-newsgroup-unselected nil
1156   "List of unselected unread articles in the current newsgroup.")
1157
1158 (defvar gnus-newsgroup-reads nil
1159   "Alist of read articles and article marks in the current newsgroup.")
1160
1161 (defvar gnus-newsgroup-expunged-tally nil)
1162
1163 (defvar gnus-newsgroup-marked nil
1164   "List of ticked articles in the current newsgroup (a subset of unread art).")
1165
1166 (defvar gnus-newsgroup-killed nil
1167   "List of ranges of articles that have been through the scoring process.")
1168
1169 (defvar gnus-newsgroup-cached nil
1170   "List of articles that come from the article cache.")
1171
1172 (defvar gnus-newsgroup-saved nil
1173   "List of articles that have been saved.")
1174
1175 (defvar gnus-newsgroup-kill-headers nil)
1176
1177 (defvar gnus-newsgroup-replied nil
1178   "List of articles that have been replied to in the current newsgroup.")
1179
1180 (defvar gnus-newsgroup-forwarded nil
1181   "List of articles that have been forwarded in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-recent nil
1184   "List of articles that have are recent in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-expirable nil
1187   "List of articles in the current newsgroup that can be expired.")
1188
1189 (defvar gnus-newsgroup-processable nil
1190   "List of articles in the current newsgroup that can be processed.")
1191
1192 (defvar gnus-newsgroup-downloadable nil
1193   "List of articles in the current newsgroup that can be processed.")
1194
1195 (defvar gnus-newsgroup-undownloaded nil
1196   "List of articles in the current newsgroup that haven't been downloaded..")
1197
1198 (defvar gnus-newsgroup-unsendable nil
1199   "List of articles in the current newsgroup that won't be sent.")
1200
1201 (defvar gnus-newsgroup-bookmarks nil
1202   "List of articles in the current newsgroup that have bookmarks.")
1203
1204 (defvar gnus-newsgroup-dormant nil
1205   "List of dormant articles in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-unseen nil
1208   "List of unseen articles in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-seen nil
1211   "Range of seen articles in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-articles nil
1214   "List of articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-scored nil
1217   "List of scored articles in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-headers nil
1220   "List of article headers in the current newsgroup.")
1221
1222 (defvar gnus-newsgroup-threads nil)
1223
1224 (defvar gnus-newsgroup-prepared nil
1225   "Whether the current group has been prepared properly.")
1226
1227 (defvar gnus-newsgroup-ancient nil
1228   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-sparse nil)
1231
1232 (defvar gnus-current-article nil)
1233 (defvar gnus-article-current nil)
1234 (defvar gnus-current-headers nil)
1235 (defvar gnus-have-all-headers nil)
1236 (defvar gnus-last-article nil)
1237 (defvar gnus-newsgroup-history nil)
1238 (defvar gnus-newsgroup-charset nil)
1239 (defvar gnus-newsgroup-ephemeral-charset nil)
1240 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1241
1242 (defvar gnus-article-before-search nil)
1243
1244 (defconst gnus-summary-local-variables
1245   '(gnus-newsgroup-name
1246     gnus-newsgroup-begin gnus-newsgroup-end
1247     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1248     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1249     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1250     gnus-newsgroup-unselected gnus-newsgroup-marked
1251     gnus-newsgroup-reads gnus-newsgroup-saved
1252     gnus-newsgroup-replied gnus-newsgroup-forwarded
1253     gnus-newsgroup-recent
1254     gnus-newsgroup-expirable
1255     gnus-newsgroup-processable gnus-newsgroup-killed
1256     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1257     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1258     gnus-newsgroup-seen gnus-newsgroup-articles
1259     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1260     gnus-newsgroup-headers gnus-newsgroup-threads
1261     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1262     gnus-current-article gnus-current-headers gnus-have-all-headers
1263     gnus-last-article gnus-article-internal-prepare-hook
1264     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1265     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1266     gnus-thread-expunge-below
1267     gnus-score-alist gnus-current-score-file
1268     (gnus-summary-expunge-below . global)
1269     (gnus-summary-mark-below . global)
1270     (gnus-orphan-score . global)
1271     gnus-newsgroup-active gnus-scores-exclude-files
1272     gnus-newsgroup-history gnus-newsgroup-ancient
1273     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1274     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1275     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1276     (gnus-newsgroup-expunged-tally . 0)
1277     gnus-cache-removable-articles gnus-newsgroup-cached
1278     gnus-newsgroup-data gnus-newsgroup-data-reverse
1279     gnus-newsgroup-limit gnus-newsgroup-limits
1280     gnus-newsgroup-charset gnus-newsgroup-display)
1281   "Variables that are buffer-local to the summary buffers.")
1282
1283 (defvar gnus-newsgroup-variables nil
1284   "A list of variables that have separate values in different newsgroups.
1285 A list of newsgroup (summary buffer) local variables, or cons of
1286 variables and their default values (when the default values are not
1287 nil), that should be made global while the summary buffer is active.
1288 These variables can be used to set variables in the group parameters
1289 while still allowing them to affect operations done in other
1290 buffers. For example:
1291
1292 \(setq gnus-newsgroup-variables
1293      '(message-use-followup-to
1294        (gnus-visible-headers .
1295          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1296 ")
1297
1298 ;; Byte-compiler warning.
1299 (eval-when-compile (defvar gnus-article-mode-map))
1300
1301 ;; MIME stuff.
1302
1303 (defvar gnus-decode-encoded-word-methods
1304   '(mail-decode-encoded-word-string)
1305   "List of methods used to decode encoded words.
1306
1307 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1308 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1309 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1310 whose names match REGEXP.
1311
1312 For example:
1313 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1314  mail-decode-encoded-word-string
1315  (\"chinese\" . rfc1843-decode-string))")
1316
1317 (defvar gnus-decode-encoded-word-methods-cache nil)
1318
1319 (defun gnus-multi-decode-encoded-word-string (string)
1320   "Apply the functions from `gnus-encoded-word-methods' that match."
1321   (unless (and gnus-decode-encoded-word-methods-cache
1322                (eq gnus-newsgroup-name
1323                    (car gnus-decode-encoded-word-methods-cache)))
1324     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1325     (mapcar (lambda (x)
1326               (if (symbolp x)
1327                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1328                 (if (and gnus-newsgroup-name
1329                          (string-match (car x) gnus-newsgroup-name))
1330                     (nconc gnus-decode-encoded-word-methods-cache
1331                            (list (cdr x))))))
1332             gnus-decode-encoded-word-methods))
1333   (let ((xlist gnus-decode-encoded-word-methods-cache))
1334     (pop xlist)
1335     (while xlist
1336       (setq string (funcall (pop xlist) string))))
1337   string)
1338
1339 ;; Subject simplification.
1340
1341 (defun gnus-simplify-whitespace (str)
1342   "Remove excessive whitespace from STR."
1343   (let ((mystr str))
1344     ;; Multiple spaces.
1345     (while (string-match "[ \t][ \t]+" mystr)
1346       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1347                           " "
1348                           (substring mystr (match-end 0)))))
1349     ;; Leading spaces.
1350     (when (string-match "^[ \t]+" mystr)
1351       (setq mystr (substring mystr (match-end 0))))
1352     ;; Trailing spaces.
1353     (when (string-match "[ \t]+$" mystr)
1354       (setq mystr (substring mystr 0 (match-beginning 0))))
1355     mystr))
1356
1357 (defsubst gnus-simplify-subject-re (subject)
1358   "Remove \"Re:\" from subject lines."
1359   (if (string-match message-subject-re-regexp subject)
1360       (substring subject (match-end 0))
1361     subject))
1362
1363 (defun gnus-simplify-subject (subject &optional re-only)
1364   "Remove `Re:' and words in parentheses.
1365 If RE-ONLY is non-nil, strip leading `Re:'s only."
1366   (let ((case-fold-search t))           ;Ignore case.
1367     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1368     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1369       (setq subject (substring subject (match-end 0))))
1370     ;; Remove uninteresting prefixes.
1371     (when (and (not re-only)
1372                gnus-simplify-ignored-prefixes
1373                (string-match gnus-simplify-ignored-prefixes subject))
1374       (setq subject (substring subject (match-end 0))))
1375     ;; Remove words in parentheses from end.
1376     (unless re-only
1377       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1378         (setq subject (substring subject 0 (match-beginning 0)))))
1379     ;; Return subject string.
1380     subject))
1381
1382 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1383 ;; all whitespace.
1384 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1385   (goto-char (point-min))
1386   (while (re-search-forward regexp nil t)
1387     (replace-match (or newtext ""))))
1388
1389 (defun gnus-simplify-buffer-fuzzy ()
1390   "Simplify string in the buffer fuzzily.
1391 The string in the accessible portion of the current buffer is simplified.
1392 It is assumed to be a single-line subject.
1393 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1394 matter is removed.  Additional things can be deleted by setting
1395 `gnus-simplify-subject-fuzzy-regexp'."
1396   (let ((case-fold-search t)
1397         (modified-tick))
1398     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1399
1400     (while (not (eq modified-tick (buffer-modified-tick)))
1401       (setq modified-tick (buffer-modified-tick))
1402       (cond
1403        ((listp gnus-simplify-subject-fuzzy-regexp)
1404         (mapcar 'gnus-simplify-buffer-fuzzy-step
1405                 gnus-simplify-subject-fuzzy-regexp))
1406        (gnus-simplify-subject-fuzzy-regexp
1407         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1408       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1409       (gnus-simplify-buffer-fuzzy-step
1410        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1411       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1412
1413     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1414     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1415     (gnus-simplify-buffer-fuzzy-step " $")
1416     (gnus-simplify-buffer-fuzzy-step "^ +")))
1417
1418 (defun gnus-simplify-subject-fuzzy (subject)
1419   "Simplify a subject string fuzzily.
1420 See `gnus-simplify-buffer-fuzzy' for details."
1421   (save-excursion
1422     (gnus-set-work-buffer)
1423     (let ((case-fold-search t))
1424       ;; Remove uninteresting prefixes.
1425       (when (and gnus-simplify-ignored-prefixes
1426                  (string-match gnus-simplify-ignored-prefixes subject))
1427         (setq subject (substring subject (match-end 0))))
1428       (insert subject)
1429       (inline (gnus-simplify-buffer-fuzzy))
1430       (buffer-string))))
1431
1432 (defsubst gnus-simplify-subject-fully (subject)
1433   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1434   (cond
1435    (gnus-simplify-subject-functions
1436     (gnus-map-function gnus-simplify-subject-functions subject))
1437    ((null gnus-summary-gather-subject-limit)
1438     (gnus-simplify-subject-re subject))
1439    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1440     (gnus-simplify-subject-fuzzy subject))
1441    ((numberp gnus-summary-gather-subject-limit)
1442     (gnus-limit-string (gnus-simplify-subject-re subject)
1443                        gnus-summary-gather-subject-limit))
1444    (t
1445     subject)))
1446
1447 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1448   "Check whether two subjects are equal.
1449 If optional argument simple-first is t, first argument is already
1450 simplified."
1451   (cond
1452    ((null simple-first)
1453     (equal (gnus-simplify-subject-fully s1)
1454            (gnus-simplify-subject-fully s2)))
1455    (t
1456     (equal s1
1457            (gnus-simplify-subject-fully s2)))))
1458
1459 (defun gnus-summary-bubble-group ()
1460   "Increase the score of the current group.
1461 This is a handy function to add to `gnus-summary-exit-hook' to
1462 increase the score of each group you read."
1463   (gnus-group-add-score gnus-newsgroup-name))
1464
1465 \f
1466 ;;;
1467 ;;; Gnus summary mode
1468 ;;;
1469
1470 (put 'gnus-summary-mode 'mode-class 'special)
1471
1472 (defvar gnus-article-commands-menu)
1473
1474 (when t
1475   ;; Non-orthogonal keys
1476
1477   (gnus-define-keys gnus-summary-mode-map
1478     " " gnus-summary-next-page
1479     "\177" gnus-summary-prev-page
1480     [delete] gnus-summary-prev-page
1481     [backspace] gnus-summary-prev-page
1482     "\r" gnus-summary-scroll-up
1483     "\M-\r" gnus-summary-scroll-down
1484     "n" gnus-summary-next-unread-article
1485     "p" gnus-summary-prev-unread-article
1486     "N" gnus-summary-next-article
1487     "P" gnus-summary-prev-article
1488     "\M-\C-n" gnus-summary-next-same-subject
1489     "\M-\C-p" gnus-summary-prev-same-subject
1490     "\M-n" gnus-summary-next-unread-subject
1491     "\M-p" gnus-summary-prev-unread-subject
1492     "." gnus-summary-first-unread-article
1493     "," gnus-summary-best-unread-article
1494     "\M-s" gnus-summary-search-article-forward
1495     "\M-r" gnus-summary-search-article-backward
1496     "<" gnus-summary-beginning-of-article
1497     ">" gnus-summary-end-of-article
1498     "j" gnus-summary-goto-article
1499     "^" gnus-summary-refer-parent-article
1500     "\M-^" gnus-summary-refer-article
1501     "u" gnus-summary-tick-article-forward
1502     "!" gnus-summary-tick-article-forward
1503     "U" gnus-summary-tick-article-backward
1504     "d" gnus-summary-mark-as-read-forward
1505     "D" gnus-summary-mark-as-read-backward
1506     "E" gnus-summary-mark-as-expirable
1507     "\M-u" gnus-summary-clear-mark-forward
1508     "\M-U" gnus-summary-clear-mark-backward
1509     "k" gnus-summary-kill-same-subject-and-select
1510     "\C-k" gnus-summary-kill-same-subject
1511     "\M-\C-k" gnus-summary-kill-thread
1512     "\M-\C-l" gnus-summary-lower-thread
1513     "e" gnus-summary-edit-article
1514     "#" gnus-summary-mark-as-processable
1515     "\M-#" gnus-summary-unmark-as-processable
1516     "\M-\C-t" gnus-summary-toggle-threads
1517     "\M-\C-s" gnus-summary-show-thread
1518     "\M-\C-h" gnus-summary-hide-thread
1519     "\M-\C-f" gnus-summary-next-thread
1520     "\M-\C-b" gnus-summary-prev-thread
1521     [(meta down)] gnus-summary-next-thread
1522     [(meta up)] gnus-summary-prev-thread
1523     "\M-\C-u" gnus-summary-up-thread
1524     "\M-\C-d" gnus-summary-down-thread
1525     "&" gnus-summary-execute-command
1526     "c" gnus-summary-catchup-and-exit
1527     "\C-w" gnus-summary-mark-region-as-read
1528     "\C-t" gnus-summary-toggle-truncation
1529     "?" gnus-summary-mark-as-dormant
1530     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1531     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1532     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1533     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1534     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1535     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1536     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1537     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1538     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1539     "=" gnus-summary-expand-window
1540     "\C-x\C-s" gnus-summary-reselect-current-group
1541     "\M-g" gnus-summary-rescan-group
1542     "w" gnus-summary-stop-page-breaking
1543     "\C-c\C-r" gnus-summary-caesar-message
1544     "f" gnus-summary-followup
1545     "F" gnus-summary-followup-with-original
1546     "C" gnus-summary-cancel-article
1547     "r" gnus-summary-reply
1548     "R" gnus-summary-reply-with-original
1549     "\C-c\C-f" gnus-summary-mail-forward
1550     "o" gnus-summary-save-article
1551     "\C-o" gnus-summary-save-article-mail
1552     "|" gnus-summary-pipe-output
1553     "\M-k" gnus-summary-edit-local-kill
1554     "\M-K" gnus-summary-edit-global-kill
1555     ;; "V" gnus-version
1556     "\C-c\C-d" gnus-summary-describe-group
1557     "q" gnus-summary-exit
1558     "Q" gnus-summary-exit-no-update
1559     "\C-c\C-i" gnus-info-find-node
1560     gnus-mouse-2 gnus-mouse-pick-article
1561     "m" gnus-summary-mail-other-window
1562     "a" gnus-summary-post-news
1563     "i" gnus-summary-news-other-window
1564     "x" gnus-summary-limit-to-unread
1565     "s" gnus-summary-isearch-article
1566     "t" gnus-summary-toggle-header
1567     "g" gnus-summary-show-article
1568     "l" gnus-summary-goto-last-article
1569     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1570     "\C-d" gnus-summary-enter-digest-group
1571     "\M-\C-d" gnus-summary-read-document
1572     "\M-\C-e" gnus-summary-edit-parameters
1573     "\M-\C-a" gnus-summary-customize-parameters
1574     "\C-c\C-b" gnus-bug
1575     "*" gnus-cache-enter-article
1576     "\M-*" gnus-cache-remove-article
1577     "\M-&" gnus-summary-universal-argument
1578     "\C-l" gnus-recenter
1579     "I" gnus-summary-increase-score
1580     "L" gnus-summary-lower-score
1581     "\M-i" gnus-symbolic-argument
1582     "h" gnus-summary-select-article-buffer
1583
1584     "b" gnus-article-view-part
1585     "\M-t" gnus-summary-toggle-display-buttonized
1586
1587     "V" gnus-summary-score-map
1588     "X" gnus-uu-extract-map
1589     "S" gnus-summary-send-map)
1590
1591   ;; Sort of orthogonal keymap
1592   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1593     "t" gnus-summary-tick-article-forward
1594     "!" gnus-summary-tick-article-forward
1595     "d" gnus-summary-mark-as-read-forward
1596     "r" gnus-summary-mark-as-read-forward
1597     "c" gnus-summary-clear-mark-forward
1598     " " gnus-summary-clear-mark-forward
1599     "e" gnus-summary-mark-as-expirable
1600     "x" gnus-summary-mark-as-expirable
1601     "?" gnus-summary-mark-as-dormant
1602     "b" gnus-summary-set-bookmark
1603     "B" gnus-summary-remove-bookmark
1604     "#" gnus-summary-mark-as-processable
1605     "\M-#" gnus-summary-unmark-as-processable
1606     "S" gnus-summary-limit-include-expunged
1607     "C" gnus-summary-catchup
1608     "H" gnus-summary-catchup-to-here
1609     "h" gnus-summary-catchup-from-here
1610     "\C-c" gnus-summary-catchup-all
1611     "k" gnus-summary-kill-same-subject-and-select
1612     "K" gnus-summary-kill-same-subject
1613     "P" gnus-uu-mark-map)
1614
1615   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1616     "c" gnus-summary-clear-above
1617     "u" gnus-summary-tick-above
1618     "m" gnus-summary-mark-above
1619     "k" gnus-summary-kill-below)
1620
1621   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1622     "/" gnus-summary-limit-to-subject
1623     "n" gnus-summary-limit-to-articles
1624     "w" gnus-summary-pop-limit
1625     "s" gnus-summary-limit-to-subject
1626     "a" gnus-summary-limit-to-author
1627     "u" gnus-summary-limit-to-unread
1628     "m" gnus-summary-limit-to-marks
1629     "M" gnus-summary-limit-exclude-marks
1630     "v" gnus-summary-limit-to-score
1631     "*" gnus-summary-limit-include-cached
1632     "D" gnus-summary-limit-include-dormant
1633     "T" gnus-summary-limit-include-thread
1634     "d" gnus-summary-limit-exclude-dormant
1635     "t" gnus-summary-limit-to-age
1636     "x" gnus-summary-limit-to-extra
1637     "p" gnus-summary-limit-to-display-predicate
1638     "E" gnus-summary-limit-include-expunged
1639     "c" gnus-summary-limit-exclude-childless-dormant
1640     "C" gnus-summary-limit-mark-excluded-as-read
1641     "o" gnus-summary-insert-old-articles
1642     "N" gnus-summary-insert-new-articles)
1643
1644   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1645     "n" gnus-summary-next-unread-article
1646     "p" gnus-summary-prev-unread-article
1647     "N" gnus-summary-next-article
1648     "P" gnus-summary-prev-article
1649     "\C-n" gnus-summary-next-same-subject
1650     "\C-p" gnus-summary-prev-same-subject
1651     "\M-n" gnus-summary-next-unread-subject
1652     "\M-p" gnus-summary-prev-unread-subject
1653     "f" gnus-summary-first-unread-article
1654     "b" gnus-summary-best-unread-article
1655     "j" gnus-summary-goto-article
1656     "g" gnus-summary-goto-subject
1657     "l" gnus-summary-goto-last-article
1658     "o" gnus-summary-pop-article)
1659
1660   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1661     "k" gnus-summary-kill-thread
1662     "l" gnus-summary-lower-thread
1663     "i" gnus-summary-raise-thread
1664     "T" gnus-summary-toggle-threads
1665     "t" gnus-summary-rethread-current
1666     "^" gnus-summary-reparent-thread
1667     "s" gnus-summary-show-thread
1668     "S" gnus-summary-show-all-threads
1669     "h" gnus-summary-hide-thread
1670     "H" gnus-summary-hide-all-threads
1671     "n" gnus-summary-next-thread
1672     "p" gnus-summary-prev-thread
1673     "u" gnus-summary-up-thread
1674     "o" gnus-summary-top-thread
1675     "d" gnus-summary-down-thread
1676     "#" gnus-uu-mark-thread
1677     "\M-#" gnus-uu-unmark-thread)
1678
1679   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1680     "g" gnus-summary-prepare
1681     "c" gnus-summary-insert-cached-articles)
1682
1683   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1684     "c" gnus-summary-catchup-and-exit
1685     "C" gnus-summary-catchup-all-and-exit
1686     "E" gnus-summary-exit-no-update
1687     "Q" gnus-summary-exit
1688     "Z" gnus-summary-exit
1689     "n" gnus-summary-catchup-and-goto-next-group
1690     "R" gnus-summary-reselect-current-group
1691     "G" gnus-summary-rescan-group
1692     "N" gnus-summary-next-group
1693     "s" gnus-summary-save-newsrc
1694     "P" gnus-summary-prev-group)
1695
1696   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1697     " " gnus-summary-next-page
1698     "n" gnus-summary-next-page
1699     "\177" gnus-summary-prev-page
1700     [delete] gnus-summary-prev-page
1701     "p" gnus-summary-prev-page
1702     "\r" gnus-summary-scroll-up
1703     "\M-\r" gnus-summary-scroll-down
1704     "<" gnus-summary-beginning-of-article
1705     ">" gnus-summary-end-of-article
1706     "b" gnus-summary-beginning-of-article
1707     "e" gnus-summary-end-of-article
1708     "^" gnus-summary-refer-parent-article
1709     "r" gnus-summary-refer-parent-article
1710     "D" gnus-summary-enter-digest-group
1711     "R" gnus-summary-refer-references
1712     "T" gnus-summary-refer-thread
1713     "g" gnus-summary-show-article
1714     "s" gnus-summary-isearch-article
1715     "P" gnus-summary-print-article
1716     "M" gnus-mailing-list-insinuate
1717     "t" gnus-article-babel)
1718
1719   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1720     "b" gnus-article-add-buttons
1721     "B" gnus-article-add-buttons-to-head
1722     "o" gnus-article-treat-overstrike
1723     "e" gnus-article-emphasize
1724     "w" gnus-article-fill-cited-article
1725     "Q" gnus-article-fill-long-lines
1726     "C" gnus-article-capitalize-sentences
1727     "c" gnus-article-remove-cr
1728     "q" gnus-article-de-quoted-unreadable
1729     "6" gnus-article-de-base64-unreadable
1730     "Z" gnus-article-decode-HZ
1731     "h" gnus-article-wash-html
1732     "s" gnus-summary-force-verify-and-decrypt
1733     "f" gnus-article-display-x-face
1734     "l" gnus-summary-stop-page-breaking
1735     "r" gnus-summary-caesar-message
1736     "t" gnus-summary-toggle-header
1737     "g" gnus-treat-smiley
1738     "v" gnus-summary-verbose-headers
1739     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1740     "p" gnus-article-verify-x-pgp-sig
1741     "d" gnus-article-treat-dumbquotes)
1742
1743   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1744     "a" gnus-article-hide
1745     "h" gnus-article-hide-headers
1746     "b" gnus-article-hide-boring-headers
1747     "s" gnus-article-hide-signature
1748     "c" gnus-article-hide-citation
1749     "C" gnus-article-hide-citation-in-followups
1750     "l" gnus-article-hide-list-identifiers
1751     "p" gnus-article-hide-pgp
1752     "B" gnus-article-strip-banner
1753     "P" gnus-article-hide-pem
1754     "\C-c" gnus-article-hide-citation-maybe)
1755
1756   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1757     "a" gnus-article-highlight
1758     "h" gnus-article-highlight-headers
1759     "c" gnus-article-highlight-citation
1760     "s" gnus-article-highlight-signature)
1761
1762   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1763     "f" gnus-article-treat-fold-headers
1764     "u" gnus-article-treat-unfold-headers
1765     "n" gnus-article-treat-fold-newsgroups)
1766
1767   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1768     "x" gnus-article-display-x-face
1769     "s" gnus-treat-smiley
1770     "D" gnus-article-remove-images
1771     "f" gnus-treat-from-picon
1772     "m" gnus-treat-mail-picon
1773     "n" gnus-treat-newsgroups-picon)
1774
1775   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1776     "w" gnus-article-decode-mime-words
1777     "c" gnus-article-decode-charset
1778     "v" gnus-mime-view-all-parts
1779     "b" gnus-article-view-part)
1780
1781   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1782     "z" gnus-article-date-ut
1783     "u" gnus-article-date-ut
1784     "l" gnus-article-date-local
1785     "p" gnus-article-date-english
1786     "e" gnus-article-date-lapsed
1787     "o" gnus-article-date-original
1788     "i" gnus-article-date-iso8601
1789     "s" gnus-article-date-user)
1790
1791   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1792     "t" gnus-article-remove-trailing-blank-lines
1793     "l" gnus-article-strip-leading-blank-lines
1794     "m" gnus-article-strip-multiple-blank-lines
1795     "a" gnus-article-strip-blank-lines
1796     "A" gnus-article-strip-all-blank-lines
1797     "s" gnus-article-strip-leading-space
1798     "e" gnus-article-strip-trailing-space
1799     "w" gnus-article-remove-leading-whitespace)
1800
1801   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1802     "v" gnus-version
1803     "f" gnus-summary-fetch-faq
1804     "d" gnus-summary-describe-group
1805     "h" gnus-summary-describe-briefly
1806     "i" gnus-info-find-node)
1807
1808   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1809     "e" gnus-summary-expire-articles
1810     "\M-\C-e" gnus-summary-expire-articles-now
1811     "\177" gnus-summary-delete-article
1812     [delete] gnus-summary-delete-article
1813     [backspace] gnus-summary-delete-article
1814     "m" gnus-summary-move-article
1815     "r" gnus-summary-respool-article
1816     "w" gnus-summary-edit-article
1817     "c" gnus-summary-copy-article
1818     "B" gnus-summary-crosspost-article
1819     "q" gnus-summary-respool-query
1820     "t" gnus-summary-respool-trace
1821     "i" gnus-summary-import-article
1822     "I" gnus-summary-create-article
1823     "p" gnus-summary-article-posted-p)
1824
1825   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1826     "o" gnus-summary-save-article
1827     "m" gnus-summary-save-article-mail
1828     "F" gnus-summary-write-article-file
1829     "r" gnus-summary-save-article-rmail
1830     "f" gnus-summary-save-article-file
1831     "b" gnus-summary-save-article-body-file
1832     "h" gnus-summary-save-article-folder
1833     "v" gnus-summary-save-article-vm
1834     "p" gnus-summary-pipe-output
1835     "P" gnus-summary-muttprint
1836     "s" gnus-soup-add-article)
1837
1838   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1839     "b" gnus-summary-display-buttonized
1840     "m" gnus-summary-repair-multipart
1841     "v" gnus-article-view-part
1842     "o" gnus-article-save-part
1843     "c" gnus-article-copy-part
1844     "C" gnus-article-view-part-as-charset
1845     "e" gnus-article-externalize-part
1846     "E" gnus-article-encrypt-body
1847     "i" gnus-article-inline-part
1848     "|" gnus-article-pipe-part))
1849
1850 (defvar gnus-article-post-menu nil)
1851
1852 (defun gnus-summary-make-menu-bar ()
1853   (gnus-turn-off-edit-menu 'summary)
1854
1855   (unless (boundp 'gnus-summary-misc-menu)
1856
1857     (easy-menu-define
1858       gnus-summary-kill-menu gnus-summary-mode-map ""
1859       (cons
1860        "Score"
1861        (nconc
1862         (list
1863          ["Customize" gnus-score-customize t])
1864         (gnus-make-score-map 'increase)
1865         (gnus-make-score-map 'lower)
1866         '(("Mark"
1867            ["Kill below" gnus-summary-kill-below t]
1868            ["Mark above" gnus-summary-mark-above t]
1869            ["Tick above" gnus-summary-tick-above t]
1870            ["Clear above" gnus-summary-clear-above t])
1871           ["Current score" gnus-summary-current-score t]
1872           ["Set score" gnus-summary-set-score t]
1873           ["Switch current score file..." gnus-score-change-score-file t]
1874           ["Set mark below..." gnus-score-set-mark-below t]
1875           ["Set expunge below..." gnus-score-set-expunge-below t]
1876           ["Edit current score file" gnus-score-edit-current-scores t]
1877           ["Edit score file" gnus-score-edit-file t]
1878           ["Trace score" gnus-score-find-trace t]
1879           ["Find words" gnus-score-find-favourite-words t]
1880           ["Rescore buffer" gnus-summary-rescore t]
1881           ["Increase score..." gnus-summary-increase-score t]
1882           ["Lower score..." gnus-summary-lower-score t]))))
1883
1884     ;; Define both the Article menu in the summary buffer and the
1885     ;; equivalent Commands menu in the article buffer here for
1886     ;; consistency.
1887     (let ((innards
1888            `(("Hide"
1889               ["All" gnus-article-hide t]
1890               ["Headers" gnus-article-hide-headers t]
1891               ["Signature" gnus-article-hide-signature t]
1892               ["Citation" gnus-article-hide-citation t]
1893               ["List identifiers" gnus-article-hide-list-identifiers t]
1894               ["PGP" gnus-article-hide-pgp t]
1895               ["Banner" gnus-article-strip-banner t]
1896               ["Boring headers" gnus-article-hide-boring-headers t])
1897              ("Highlight"
1898               ["All" gnus-article-highlight t]
1899               ["Headers" gnus-article-highlight-headers t]
1900               ["Signature" gnus-article-highlight-signature t]
1901               ["Citation" gnus-article-highlight-citation t])
1902              ("MIME"
1903               ["Words" gnus-article-decode-mime-words t]
1904               ["Charset" gnus-article-decode-charset t]
1905               ["QP" gnus-article-de-quoted-unreadable t]
1906               ["Base64" gnus-article-de-base64-unreadable t]
1907               ["View all" gnus-mime-view-all-parts t]
1908               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1909               ["Encrypt body" gnus-article-encrypt-body t])
1910              ("Date"
1911               ["Local" gnus-article-date-local t]
1912               ["ISO8601" gnus-article-date-iso8601 t]
1913               ["UT" gnus-article-date-ut t]
1914               ["Original" gnus-article-date-original t]
1915               ["Lapsed" gnus-article-date-lapsed t]
1916               ["User-defined" gnus-article-date-user t])
1917              ("Display"
1918               ["Remove images" gnus-article-remove-images t]
1919               ["Toggle smiley" gnus-treat-smiley t]
1920               ["Show X-Face" gnus-article-display-x-face t]
1921               ["Show picons in From" gnus-treat-from-picon t]
1922               ["Show picons in mail headers" gnus-treat-mail-picon t]
1923               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1924              ("Washing"
1925               ("Remove Blanks"
1926                ["Leading" gnus-article-strip-leading-blank-lines t]
1927                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1928                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1929                ["All of the above" gnus-article-strip-blank-lines t]
1930                ["All" gnus-article-strip-all-blank-lines t]
1931                ["Leading space" gnus-article-strip-leading-space t]
1932                ["Trailing space" gnus-article-strip-trailing-space t]
1933                ["Leading space in headers"
1934                 gnus-article-remove-leading-whitespace t])
1935               ["Overstrike" gnus-article-treat-overstrike t]
1936               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1937               ["Emphasis" gnus-article-emphasize t]
1938               ["Word wrap" gnus-article-fill-cited-article t]
1939               ["Fill long lines" gnus-article-fill-long-lines t]
1940               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1941               ["CR" gnus-article-remove-cr t]
1942               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1943               ["Base64" gnus-article-de-base64-unreadable t]
1944               ["Rot 13" gnus-summary-caesar-message
1945                ,@(if (featurep 'xemacs) '(t)
1946                    '(:help "\"Caesar rotate\" article by 13"))]
1947               ["Unix pipe" gnus-summary-pipe-message t]
1948               ["Add buttons" gnus-article-add-buttons t]
1949               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1950               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1951               ["Verbose header" gnus-summary-verbose-headers t]
1952               ["Toggle header" gnus-summary-toggle-header t]
1953               ["Unfold headers" gnus-article-treat-unfold-headers t]
1954               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1955               ["Html" gnus-article-wash-html t]
1956               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1957               ["HZ" gnus-article-decode-HZ t])
1958              ("Output"
1959               ["Save in default format" gnus-summary-save-article
1960                ,@(if (featurep 'xemacs) '(t)
1961                    '(:help "Save article using default method"))]
1962               ["Save in file" gnus-summary-save-article-file
1963                ,@(if (featurep 'xemacs) '(t)
1964                    '(:help "Save article in file"))]
1965               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1966               ["Save in MH folder" gnus-summary-save-article-folder t]
1967               ["Save in VM folder" gnus-summary-save-article-vm t]
1968               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1969               ["Save body in file" gnus-summary-save-article-body-file t]
1970               ["Pipe through a filter" gnus-summary-pipe-output t]
1971               ["Add to SOUP packet" gnus-soup-add-article t]
1972               ["Print with Muttprint" gnus-summary-muttprint t]
1973               ["Print" gnus-summary-print-article t])
1974              ("Backend"
1975               ["Respool article..." gnus-summary-respool-article t]
1976               ["Move article..." gnus-summary-move-article
1977                (gnus-check-backend-function
1978                 'request-move-article gnus-newsgroup-name)]
1979               ["Copy article..." gnus-summary-copy-article t]
1980               ["Crosspost article..." gnus-summary-crosspost-article
1981                (gnus-check-backend-function
1982                 'request-replace-article gnus-newsgroup-name)]
1983               ["Import file..." gnus-summary-import-article t]
1984               ["Create article..." gnus-summary-create-article t]
1985               ["Check if posted" gnus-summary-article-posted-p t]
1986               ["Edit article" gnus-summary-edit-article
1987                (not (gnus-group-read-only-p))]
1988               ["Delete article" gnus-summary-delete-article
1989                (gnus-check-backend-function
1990                 'request-expire-articles gnus-newsgroup-name)]
1991               ["Query respool" gnus-summary-respool-query t]
1992               ["Trace respool" gnus-summary-respool-trace t]
1993               ["Delete expirable articles" gnus-summary-expire-articles-now
1994                (gnus-check-backend-function
1995                 'request-expire-articles gnus-newsgroup-name)])
1996              ("Extract"
1997               ["Uudecode" gnus-uu-decode-uu
1998                ,@(if (featurep 'xemacs) '(t)
1999                    '(:help "Decode uuencoded article(s)"))]
2000               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2001               ["Unshar" gnus-uu-decode-unshar t]
2002               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2003               ["Save" gnus-uu-decode-save t]
2004               ["Binhex" gnus-uu-decode-binhex t]
2005               ["Postscript" gnus-uu-decode-postscript t])
2006              ("Cache"
2007               ["Enter article" gnus-cache-enter-article t]
2008               ["Remove article" gnus-cache-remove-article t])
2009              ["Translate" gnus-article-babel t]
2010              ["Select article buffer" gnus-summary-select-article-buffer t]
2011              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2012              ["Isearch article..." gnus-summary-isearch-article t]
2013              ["Beginning of the article" gnus-summary-beginning-of-article t]
2014              ["End of the article" gnus-summary-end-of-article t]
2015              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2016              ["Fetch referenced articles" gnus-summary-refer-references t]
2017              ["Fetch current thread" gnus-summary-refer-thread t]
2018              ["Fetch article with id..." gnus-summary-refer-article t]
2019              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2020              ["Redisplay" gnus-summary-show-article t]
2021              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2022       (easy-menu-define
2023         gnus-summary-article-menu gnus-summary-mode-map ""
2024         (cons "Article" innards))
2025
2026       (if (not (keymapp gnus-summary-article-menu))
2027           (easy-menu-define
2028             gnus-article-commands-menu gnus-article-mode-map ""
2029             (cons "Commands" innards))
2030         ;; in Emacs, don't share menu.
2031         (setq gnus-article-commands-menu
2032               (copy-keymap gnus-summary-article-menu))
2033         (define-key gnus-article-mode-map [menu-bar commands]
2034           (cons "Commands" gnus-article-commands-menu))))
2035
2036     (easy-menu-define
2037       gnus-summary-thread-menu gnus-summary-mode-map ""
2038       '("Threads"
2039         ["Toggle threading" gnus-summary-toggle-threads t]
2040         ["Hide threads" gnus-summary-hide-all-threads t]
2041         ["Show threads" gnus-summary-show-all-threads t]
2042         ["Hide thread" gnus-summary-hide-thread t]
2043         ["Show thread" gnus-summary-show-thread t]
2044         ["Go to next thread" gnus-summary-next-thread t]
2045         ["Go to previous thread" gnus-summary-prev-thread t]
2046         ["Go down thread" gnus-summary-down-thread t]
2047         ["Go up thread" gnus-summary-up-thread t]
2048         ["Top of thread" gnus-summary-top-thread t]
2049         ["Mark thread as read" gnus-summary-kill-thread t]
2050         ["Lower thread score" gnus-summary-lower-thread t]
2051         ["Raise thread score" gnus-summary-raise-thread t]
2052         ["Rethread current" gnus-summary-rethread-current t]))
2053
2054     (easy-menu-define
2055       gnus-summary-post-menu gnus-summary-mode-map ""
2056       `("Post"
2057         ["Send a message (mail or news)" gnus-summary-post-news
2058          ,@(if (featurep 'xemacs) '(t)
2059              '(:help "Post an article"))]
2060         ["Followup" gnus-summary-followup
2061          ,@(if (featurep 'xemacs) '(t)
2062              '(:help "Post followup to this article"))]
2063         ["Followup and yank" gnus-summary-followup-with-original
2064          ,@(if (featurep 'xemacs) '(t)
2065              '(:help "Post followup to this article, quoting its contents"))]
2066         ["Supersede article" gnus-summary-supersede-article t]
2067         ["Cancel article" gnus-summary-cancel-article
2068          ,@(if (featurep 'xemacs) '(t)
2069              '(:help "Cancel an article you posted"))]
2070         ["Reply" gnus-summary-reply t]
2071         ["Reply and yank" gnus-summary-reply-with-original t]
2072         ["Wide reply" gnus-summary-wide-reply t]
2073         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2074          ,@(if (featurep 'xemacs) '(t)
2075              '(:help "Mail a reply, quoting this article"))]
2076         ["Very wide reply" gnus-summary-very-wide-reply t]
2077         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2078         ,@(if (featurep 'xemacs) '(t)
2079             '(:help "Mail a very wide reply, quoting this article"))]
2080         ["Mail forward" gnus-summary-mail-forward t]
2081         ["Post forward" gnus-summary-post-forward t]
2082         ["Digest and mail" gnus-uu-digest-mail-forward t]
2083         ["Digest and post" gnus-uu-digest-post-forward t]
2084         ["Resend message" gnus-summary-resend-message t]
2085         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2086         ["Send a mail" gnus-summary-mail-other-window t]
2087         ["Create a local message" gnus-summary-news-other-window t]
2088         ["Uuencode and post" gnus-uu-post-news
2089          ,@(if (featurep 'xemacs) '(t)
2090              '(:help "Post a uuencoded article"))]
2091         ["Followup via news" gnus-summary-followup-to-mail t]
2092         ["Followup via news and yank"
2093          gnus-summary-followup-to-mail-with-original t]
2094         ;;("Draft"
2095         ;;["Send" gnus-summary-send-draft t]
2096         ;;["Send bounced" gnus-resend-bounced-mail t])
2097         ))
2098
2099     (cond
2100      ((not (keymapp gnus-summary-post-menu))
2101       (setq gnus-article-post-menu gnus-summary-post-menu))
2102      ((not gnus-article-post-menu)
2103       ;; Don't share post menu.
2104       (setq gnus-article-post-menu
2105             (copy-keymap gnus-summary-post-menu))))
2106     (define-key gnus-article-mode-map [menu-bar post]
2107       (cons "Post" gnus-article-post-menu))
2108
2109     (easy-menu-define
2110       gnus-summary-misc-menu gnus-summary-mode-map ""
2111       `("Gnus"
2112         ("Mark Read"
2113          ["Mark as read" gnus-summary-mark-as-read-forward t]
2114          ["Mark same subject and select"
2115           gnus-summary-kill-same-subject-and-select t]
2116          ["Mark same subject" gnus-summary-kill-same-subject t]
2117          ["Catchup" gnus-summary-catchup
2118           ,@(if (featurep 'xemacs) '(t)
2119               '(:help "Mark unread articles in this group as read"))]
2120          ["Catchup all" gnus-summary-catchup-all t]
2121          ["Catchup to here" gnus-summary-catchup-to-here t]
2122          ["Catchup from here" gnus-summary-catchup-from-here t]
2123          ["Catchup region" gnus-summary-mark-region-as-read t]
2124          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2125         ("Mark Various"
2126          ["Tick" gnus-summary-tick-article-forward t]
2127          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2128          ["Remove marks" gnus-summary-clear-mark-forward t]
2129          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2130          ["Set bookmark" gnus-summary-set-bookmark t]
2131          ["Remove bookmark" gnus-summary-remove-bookmark t])
2132         ("Limit to"
2133          ["Marks..." gnus-summary-limit-to-marks t]
2134          ["Subject..." gnus-summary-limit-to-subject t]
2135          ["Author..." gnus-summary-limit-to-author t]
2136          ["Age..." gnus-summary-limit-to-age t]
2137          ["Extra..." gnus-summary-limit-to-extra t]
2138          ["Score" gnus-summary-limit-to-score t]
2139          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2140          ["Unread" gnus-summary-limit-to-unread t]
2141          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2142          ["Articles" gnus-summary-limit-to-articles t]
2143          ["Pop limit" gnus-summary-pop-limit t]
2144          ["Show dormant" gnus-summary-limit-include-dormant t]
2145          ["Hide childless dormant"
2146           gnus-summary-limit-exclude-childless-dormant t]
2147          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2148          ["Hide marked" gnus-summary-limit-exclude-marks t]
2149          ["Show expunged" gnus-summary-limit-include-expunged t])
2150         ("Process Mark"
2151          ["Set mark" gnus-summary-mark-as-processable t]
2152          ["Remove mark" gnus-summary-unmark-as-processable t]
2153          ["Remove all marks" gnus-summary-unmark-all-processable t]
2154          ["Mark above" gnus-uu-mark-over t]
2155          ["Mark series" gnus-uu-mark-series t]
2156          ["Mark region" gnus-uu-mark-region t]
2157          ["Unmark region" gnus-uu-unmark-region t]
2158          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2159          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2160          ["Mark all" gnus-uu-mark-all t]
2161          ["Mark buffer" gnus-uu-mark-buffer t]
2162          ["Mark sparse" gnus-uu-mark-sparse t]
2163          ["Mark thread" gnus-uu-mark-thread t]
2164          ["Unmark thread" gnus-uu-unmark-thread t]
2165          ("Process Mark Sets"
2166           ["Kill" gnus-summary-kill-process-mark t]
2167           ["Yank" gnus-summary-yank-process-mark
2168            gnus-newsgroup-process-stack]
2169           ["Save" gnus-summary-save-process-mark t]))
2170         ("Scroll article"
2171          ["Page forward" gnus-summary-next-page
2172           ,@(if (featurep 'xemacs) '(t)
2173               '(:help "Show next page of article"))]
2174          ["Page backward" gnus-summary-prev-page
2175           ,@(if (featurep 'xemacs) '(t)
2176               '(:help "Show previous page of article"))]
2177          ["Line forward" gnus-summary-scroll-up t])
2178         ("Move"
2179          ["Next unread article" gnus-summary-next-unread-article t]
2180          ["Previous unread article" gnus-summary-prev-unread-article t]
2181          ["Next article" gnus-summary-next-article t]
2182          ["Previous article" gnus-summary-prev-article t]
2183          ["Next unread subject" gnus-summary-next-unread-subject t]
2184          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2185          ["Next article same subject" gnus-summary-next-same-subject t]
2186          ["Previous article same subject" gnus-summary-prev-same-subject t]
2187          ["First unread article" gnus-summary-first-unread-article t]
2188          ["Best unread article" gnus-summary-best-unread-article t]
2189          ["Go to subject number..." gnus-summary-goto-subject t]
2190          ["Go to article number..." gnus-summary-goto-article t]
2191          ["Go to the last article" gnus-summary-goto-last-article t]
2192          ["Pop article off history" gnus-summary-pop-article t])
2193         ("Sort"
2194          ["Sort by number" gnus-summary-sort-by-number t]
2195          ["Sort by author" gnus-summary-sort-by-author t]
2196          ["Sort by subject" gnus-summary-sort-by-subject t]
2197          ["Sort by date" gnus-summary-sort-by-date t]
2198          ["Sort by score" gnus-summary-sort-by-score t]
2199          ["Sort by lines" gnus-summary-sort-by-lines t]
2200          ["Sort by characters" gnus-summary-sort-by-chars t]
2201          ["Original sort" gnus-summary-sort-by-original t])
2202         ("Help"
2203          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2204          ["Describe group" gnus-summary-describe-group t]
2205          ["Read manual" gnus-info-find-node t])
2206         ("Modes"
2207          ["Pick and read" gnus-pick-mode t]
2208          ["Binary" gnus-binary-mode t])
2209         ("Regeneration"
2210          ["Regenerate" gnus-summary-prepare t]
2211          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2212          ["Toggle threading" gnus-summary-toggle-threads t])
2213         ["See old articles" gnus-summary-insert-old-articles t]
2214         ["See new articles" gnus-summary-insert-new-articles t]
2215         ["Filter articles..." gnus-summary-execute-command t]
2216         ["Run command on subjects..." gnus-summary-universal-argument t]
2217         ["Search articles forward..." gnus-summary-search-article-forward t]
2218         ["Search articles backward..." gnus-summary-search-article-backward t]
2219         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2220         ["Expand window" gnus-summary-expand-window t]
2221         ["Expire expirable articles" gnus-summary-expire-articles
2222          (gnus-check-backend-function
2223           'request-expire-articles gnus-newsgroup-name)]
2224         ["Edit local kill file" gnus-summary-edit-local-kill t]
2225         ["Edit main kill file" gnus-summary-edit-global-kill t]
2226         ["Edit group parameters" gnus-summary-edit-parameters t]
2227         ["Customize group parameters" gnus-summary-customize-parameters t]
2228         ["Send a bug report" gnus-bug t]
2229         ("Exit"
2230          ["Catchup and exit" gnus-summary-catchup-and-exit
2231           ,@(if (featurep 'xemacs) '(t)
2232               '(:help "Mark unread articles in this group as read, then exit"))]
2233          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2234          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2235          ["Exit group" gnus-summary-exit
2236           ,@(if (featurep 'xemacs) '(t)
2237               '(:help "Exit current group, return to group selection mode"))]
2238          ["Exit group without updating" gnus-summary-exit-no-update t]
2239          ["Exit and goto next group" gnus-summary-next-group t]
2240          ["Exit and goto prev group" gnus-summary-prev-group t]
2241          ["Reselect group" gnus-summary-reselect-current-group t]
2242          ["Rescan group" gnus-summary-rescan-group t]
2243          ["Update dribble" gnus-summary-save-newsrc t])))
2244
2245     (gnus-run-hooks 'gnus-summary-menu-hook)))
2246
2247 (defvar gnus-summary-tool-bar-map nil)
2248
2249 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2250 (defun gnus-summary-make-tool-bar ()
2251   (if (and (fboundp 'tool-bar-add-item-from-menu)
2252            (default-value 'tool-bar-mode)
2253            (not gnus-summary-tool-bar-map))
2254       (setq gnus-summary-tool-bar-map
2255             (let ((tool-bar-map (make-sparse-keymap))
2256                   (load-path (mm-image-load-path)))
2257               (tool-bar-add-item-from-menu
2258                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2259               (tool-bar-add-item-from-menu
2260                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2261               (tool-bar-add-item-from-menu
2262                'gnus-summary-post-news "post" gnus-summary-mode-map)
2263               (tool-bar-add-item-from-menu
2264                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2265               (tool-bar-add-item-from-menu
2266                'gnus-summary-followup "followup" gnus-summary-mode-map)
2267               (tool-bar-add-item-from-menu
2268                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2269               (tool-bar-add-item-from-menu
2270                'gnus-summary-reply "reply" gnus-summary-mode-map)
2271               (tool-bar-add-item-from-menu
2272                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2273               (tool-bar-add-item-from-menu
2274                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2275               (tool-bar-add-item-from-menu
2276                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2277               (tool-bar-add-item-from-menu
2278                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2279               (tool-bar-add-item-from-menu
2280                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2281               (tool-bar-add-item-from-menu
2282                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2283               (tool-bar-add-item-from-menu
2284                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2285               (tool-bar-add-item-from-menu
2286                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2287               tool-bar-map)))
2288   (if gnus-summary-tool-bar-map
2289       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2290
2291 (defun gnus-score-set-default (var value)
2292   "A version of set that updates the GNU Emacs menu-bar."
2293   (set var value)
2294   ;; It is the message that forces the active status to be updated.
2295   (message ""))
2296
2297 (defun gnus-make-score-map (type)
2298   "Make a summary score map of type TYPE."
2299   (if t
2300       nil
2301     (let ((headers '(("author" "from" string)
2302                      ("subject" "subject" string)
2303                      ("article body" "body" string)
2304                      ("article head" "head" string)
2305                      ("xref" "xref" string)
2306                      ("extra header" "extra" string)
2307                      ("lines" "lines" number)
2308                      ("followups to author" "followup" string)))
2309           (types '((number ("less than" <)
2310                            ("greater than" >)
2311                            ("equal" =))
2312                    (string ("substring" s)
2313                            ("exact string" e)
2314                            ("fuzzy string" f)
2315                            ("regexp" r))))
2316           (perms '(("temporary" (current-time-string))
2317                    ("permanent" nil)
2318                    ("immediate" now)))
2319           header)
2320       (list
2321        (apply
2322         'nconc
2323         (list
2324          (if (eq type 'lower)
2325              "Lower score"
2326            "Increase score"))
2327         (let (outh)
2328           (while headers
2329             (setq header (car headers))
2330             (setq outh
2331                   (cons
2332                    (apply
2333                     'nconc
2334                     (list (car header))
2335                     (let ((ts (cdr (assoc (nth 2 header) types)))
2336                           outt)
2337                       (while ts
2338                         (setq outt
2339                               (cons
2340                                (apply
2341                                 'nconc
2342                                 (list (caar ts))
2343                                 (let ((ps perms)
2344                                       outp)
2345                                   (while ps
2346                                     (setq outp
2347                                           (cons
2348                                            (vector
2349                                             (caar ps)
2350                                             (list
2351                                              'gnus-summary-score-entry
2352                                              (nth 1 header)
2353                                              (if (or (string= (nth 1 header)
2354                                                               "head")
2355                                                      (string= (nth 1 header)
2356                                                               "body"))
2357                                                  ""
2358                                                (list 'gnus-summary-header
2359                                                      (nth 1 header)))
2360                                              (list 'quote (nth 1 (car ts)))
2361                                              (list 'gnus-score-delta-default
2362                                                    nil)
2363                                              (nth 1 (car ps))
2364                                              t)
2365                                             t)
2366                                            outp))
2367                                     (setq ps (cdr ps)))
2368                                   (list (nreverse outp))))
2369                                outt))
2370                         (setq ts (cdr ts)))
2371                       (list (nreverse outt))))
2372                    outh))
2373             (setq headers (cdr headers)))
2374           (list (nreverse outh))))))))
2375
2376 \f
2377
2378 (defun gnus-summary-mode (&optional group)
2379   "Major mode for reading articles.
2380
2381 All normal editing commands are switched off.
2382 \\<gnus-summary-mode-map>
2383 Each line in this buffer represents one article.  To read an
2384 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2385 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2386 respectively.
2387
2388 You can also post articles and send mail from this buffer.  To
2389 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2390 of an article, type `\\[gnus-summary-reply]'.
2391
2392 There are approx. one gazillion commands you can execute in this
2393 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2394
2395 The following commands are available:
2396
2397 \\{gnus-summary-mode-map}"
2398   (interactive)
2399   (kill-all-local-variables)
2400   (when (gnus-visual-p 'summary-menu 'menu)
2401     (gnus-summary-make-menu-bar)
2402     (gnus-summary-make-tool-bar))
2403   (gnus-summary-make-local-variables)
2404   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2405     (gnus-summary-make-local-variables))
2406   (gnus-make-thread-indent-array)
2407   (gnus-simplify-mode-line)
2408   (setq major-mode 'gnus-summary-mode)
2409   (setq mode-name "Summary")
2410   (make-local-variable 'minor-mode-alist)
2411   (use-local-map gnus-summary-mode-map)
2412   (buffer-disable-undo)
2413   (setq buffer-read-only t)             ;Disable modification
2414   (setq truncate-lines t)
2415   (setq selective-display t)
2416   (setq selective-display-ellipses t)   ;Display `...'
2417   (gnus-summary-set-display-table)
2418   (gnus-set-default-directory)
2419   (setq gnus-newsgroup-name group)
2420   (make-local-variable 'gnus-summary-line-format)
2421   (make-local-variable 'gnus-summary-line-format-spec)
2422   (make-local-variable 'gnus-summary-dummy-line-format)
2423   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2424   (make-local-variable 'gnus-summary-mark-positions)
2425   (make-local-hook 'pre-command-hook)
2426   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2427   (gnus-run-hooks 'gnus-summary-mode-hook)
2428   (turn-on-gnus-mailing-list-mode)
2429   (mm-enable-multibyte)
2430   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2431   (gnus-update-summary-mark-positions))
2432
2433 (defun gnus-summary-make-local-variables ()
2434   "Make all the local summary buffer variables."
2435   (let (global)
2436     (dolist (local gnus-summary-local-variables)
2437       (if (consp local)
2438           (progn
2439             (if (eq (cdr local) 'global)
2440                 ;; Copy the global value of the variable.
2441                 (setq global (symbol-value (car local)))
2442               ;; Use the value from the list.
2443               (setq global (eval (cdr local))))
2444             (set (make-local-variable (car local)) global))
2445         ;; Simple nil-valued local variable.
2446         (set (make-local-variable local) nil)))))
2447
2448 (defun gnus-summary-clear-local-variables ()
2449   (let ((locals gnus-summary-local-variables))
2450     (while locals
2451       (if (consp (car locals))
2452           (and (vectorp (caar locals))
2453                (set (caar locals) nil))
2454         (and (vectorp (car locals))
2455              (set (car locals) nil)))
2456       (setq locals (cdr locals)))))
2457
2458 ;; Summary data functions.
2459
2460 (defmacro gnus-data-number (data)
2461   `(car ,data))
2462
2463 (defmacro gnus-data-set-number (data number)
2464   `(setcar ,data ,number))
2465
2466 (defmacro gnus-data-mark (data)
2467   `(nth 1 ,data))
2468
2469 (defmacro gnus-data-set-mark (data mark)
2470   `(setcar (nthcdr 1 ,data) ,mark))
2471
2472 (defmacro gnus-data-pos (data)
2473   `(nth 2 ,data))
2474
2475 (defmacro gnus-data-set-pos (data pos)
2476   `(setcar (nthcdr 2 ,data) ,pos))
2477
2478 (defmacro gnus-data-header (data)
2479   `(nth 3 ,data))
2480
2481 (defmacro gnus-data-set-header (data header)
2482   `(setf (nth 3 ,data) ,header))
2483
2484 (defmacro gnus-data-level (data)
2485   `(nth 4 ,data))
2486
2487 (defmacro gnus-data-unread-p (data)
2488   `(= (nth 1 ,data) gnus-unread-mark))
2489
2490 (defmacro gnus-data-read-p (data)
2491   `(/= (nth 1 ,data) gnus-unread-mark))
2492
2493 (defmacro gnus-data-pseudo-p (data)
2494   `(consp (nth 3 ,data)))
2495
2496 (defmacro gnus-data-find (number)
2497   `(assq ,number gnus-newsgroup-data))
2498
2499 (defmacro gnus-data-find-list (number &optional data)
2500   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2501      (memq (assq ,number bdata)
2502            bdata)))
2503
2504 (defmacro gnus-data-make (number mark pos header level)
2505   `(list ,number ,mark ,pos ,header ,level))
2506
2507 (defun gnus-data-enter (after-article number mark pos header level offset)
2508   (let ((data (gnus-data-find-list after-article)))
2509     (unless data
2510       (error "No such article: %d" after-article))
2511     (setcdr data (cons (gnus-data-make number mark pos header level)
2512                        (cdr data)))
2513     (setq gnus-newsgroup-data-reverse nil)
2514     (gnus-data-update-list (cddr data) offset)))
2515
2516 (defun gnus-data-enter-list (after-article list &optional offset)
2517   (when list
2518     (let ((data (and after-article (gnus-data-find-list after-article)))
2519           (ilist list))
2520       (if (not (or data
2521                    after-article))
2522           (let ((odata gnus-newsgroup-data))
2523             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2524             (when offset
2525               (gnus-data-update-list odata offset)))
2526       ;; Find the last element in the list to be spliced into the main
2527         ;; list.
2528         (while (cdr list)
2529           (setq list (cdr list)))
2530         (if (not data)
2531             (progn
2532               (setcdr list gnus-newsgroup-data)
2533               (setq gnus-newsgroup-data ilist)
2534               (when offset
2535                 (gnus-data-update-list (cdr list) offset)))
2536           (setcdr list (cdr data))
2537           (setcdr data ilist)
2538           (when offset
2539             (gnus-data-update-list (cdr list) offset))))
2540       (setq gnus-newsgroup-data-reverse nil))))
2541
2542 (defun gnus-data-remove (article &optional offset)
2543   (let ((data gnus-newsgroup-data))
2544     (if (= (gnus-data-number (car data)) article)
2545         (progn
2546           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2547                 gnus-newsgroup-data-reverse nil)
2548           (when offset
2549             (gnus-data-update-list gnus-newsgroup-data offset)))
2550       (while (cdr data)
2551         (when (= (gnus-data-number (cadr data)) article)
2552           (setcdr data (cddr data))
2553           (when offset
2554             (gnus-data-update-list (cdr data) offset))
2555           (setq data nil
2556                 gnus-newsgroup-data-reverse nil))
2557         (setq data (cdr data))))))
2558
2559 (defmacro gnus-data-list (backward)
2560   `(if ,backward
2561        (or gnus-newsgroup-data-reverse
2562            (setq gnus-newsgroup-data-reverse
2563                  (reverse gnus-newsgroup-data)))
2564      gnus-newsgroup-data))
2565
2566 (defun gnus-data-update-list (data offset)
2567   "Add OFFSET to the POS of all data entries in DATA."
2568   (setq gnus-newsgroup-data-reverse nil)
2569   (while data
2570     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2571     (setq data (cdr data))))
2572
2573 (defun gnus-summary-article-pseudo-p (article)
2574   "Say whether this article is a pseudo article or not."
2575   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2576
2577 (defmacro gnus-summary-article-sparse-p (article)
2578   "Say whether this article is a sparse article or not."
2579   `(memq ,article gnus-newsgroup-sparse))
2580
2581 (defmacro gnus-summary-article-ancient-p (article)
2582   "Say whether this article is a sparse article or not."
2583   `(memq ,article gnus-newsgroup-ancient))
2584
2585 (defun gnus-article-parent-p (number)
2586   "Say whether this article is a parent or not."
2587   (let ((data (gnus-data-find-list number)))
2588     (and (cdr data)              ; There has to be an article after...
2589          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2590             (gnus-data-level (nth 1 data))))))
2591
2592 (defun gnus-article-children (number)
2593   "Return a list of all children to NUMBER."
2594   (let* ((data (gnus-data-find-list number))
2595          (level (gnus-data-level (car data)))
2596          children)
2597     (setq data (cdr data))
2598     (while (and data
2599                 (= (gnus-data-level (car data)) (1+ level)))
2600       (push (gnus-data-number (car data)) children)
2601       (setq data (cdr data)))
2602     children))
2603
2604 (defmacro gnus-summary-skip-intangible ()
2605   "If the current article is intangible, then jump to a different article."
2606   '(let ((to (get-text-property (point) 'gnus-intangible)))
2607      (and to (gnus-summary-goto-subject to))))
2608
2609 (defmacro gnus-summary-article-intangible-p ()
2610   "Say whether this article is intangible or not."
2611   '(get-text-property (point) 'gnus-intangible))
2612
2613 (defun gnus-article-read-p (article)
2614   "Say whether ARTICLE is read or not."
2615   (not (or (memq article gnus-newsgroup-marked)
2616            (memq article gnus-newsgroup-unreads)
2617            (memq article gnus-newsgroup-unselected)
2618            (memq article gnus-newsgroup-dormant))))
2619
2620 ;; Some summary mode macros.
2621
2622 (defmacro gnus-summary-article-number ()
2623   "The article number of the article on the current line.
2624 If there isn's an article number here, then we return the current
2625 article number."
2626   '(progn
2627      (gnus-summary-skip-intangible)
2628      (or (get-text-property (point) 'gnus-number)
2629          (gnus-summary-last-subject))))
2630
2631 (defmacro gnus-summary-article-header (&optional number)
2632   "Return the header of article NUMBER."
2633   `(gnus-data-header (gnus-data-find
2634                       ,(or number '(gnus-summary-article-number)))))
2635
2636 (defmacro gnus-summary-thread-level (&optional number)
2637   "Return the level of thread that starts with article NUMBER."
2638   `(if (and (eq gnus-summary-make-false-root 'dummy)
2639             (get-text-property (point) 'gnus-intangible))
2640        0
2641      (gnus-data-level (gnus-data-find
2642                        ,(or number '(gnus-summary-article-number))))))
2643
2644 (defmacro gnus-summary-article-mark (&optional number)
2645   "Return the mark of article NUMBER."
2646   `(gnus-data-mark (gnus-data-find
2647                     ,(or number '(gnus-summary-article-number)))))
2648
2649 (defmacro gnus-summary-article-pos (&optional number)
2650   "Return the position of the line of article NUMBER."
2651   `(gnus-data-pos (gnus-data-find
2652                    ,(or number '(gnus-summary-article-number)))))
2653
2654 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2655 (defmacro gnus-summary-article-subject (&optional number)
2656   "Return current subject string or nil if nothing."
2657   `(let ((headers
2658           ,(if number
2659                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2660              '(gnus-data-header (assq (gnus-summary-article-number)
2661                                       gnus-newsgroup-data)))))
2662      (and headers
2663           (vectorp headers)
2664           (mail-header-subject headers))))
2665
2666 (defmacro gnus-summary-article-score (&optional number)
2667   "Return current article score."
2668   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2669                   gnus-newsgroup-scored))
2670        gnus-summary-default-score 0))
2671
2672 (defun gnus-summary-article-children (&optional number)
2673   "Return a list of article numbers that are children of article NUMBER."
2674   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2675          (level (gnus-data-level (car data)))
2676          l children)
2677     (while (and (setq data (cdr data))
2678                 (> (setq l (gnus-data-level (car data))) level))
2679       (and (= (1+ level) l)
2680            (push (gnus-data-number (car data))
2681                  children)))
2682     (nreverse children)))
2683
2684 (defun gnus-summary-article-parent (&optional number)
2685   "Return the article number of the parent of article NUMBER."
2686   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2687                                     (gnus-data-list t)))
2688          (level (gnus-data-level (car data))))
2689     (if (zerop level)
2690         ()                              ; This is a root.
2691       ;; We search until we find an article with a level less than
2692       ;; this one.  That function has to be the parent.
2693       (while (and (setq data (cdr data))
2694                   (not (< (gnus-data-level (car data)) level))))
2695       (and data (gnus-data-number (car data))))))
2696
2697 (defun gnus-unread-mark-p (mark)
2698   "Say whether MARK is the unread mark."
2699   (= mark gnus-unread-mark))
2700
2701 (defun gnus-read-mark-p (mark)
2702   "Say whether MARK is one of the marks that mark as read.
2703 This is all marks except unread, ticked, dormant, and expirable."
2704   (not (or (= mark gnus-unread-mark)
2705            (= mark gnus-ticked-mark)
2706            (= mark gnus-dormant-mark)
2707            (= mark gnus-expirable-mark))))
2708
2709 (defmacro gnus-article-mark (number)
2710   "Return the MARK of article NUMBER.
2711 This macro should only be used when computing the mark the \"first\"
2712 time; i.e., when generating the summary lines.  After that,
2713 `gnus-summary-article-mark' should be used to examine the
2714 marks of articles."
2715   `(cond
2716     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2717     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2718     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2719     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2720     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2721     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2722     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2723     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2724            gnus-ancient-mark))))
2725
2726 ;; Saving hidden threads.
2727
2728 (defmacro gnus-save-hidden-threads (&rest forms)
2729   "Save hidden threads, eval FORMS, and restore the hidden threads."
2730   (let ((config (make-symbol "config")))
2731     `(let ((,config (gnus-hidden-threads-configuration)))
2732        (unwind-protect
2733            (save-excursion
2734              ,@forms)
2735          (gnus-restore-hidden-threads-configuration ,config)))))
2736 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2737 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2738
2739 (defun gnus-data-compute-positions ()
2740   "Compute the positions of all articles."
2741   (setq gnus-newsgroup-data-reverse nil)
2742   (let ((data gnus-newsgroup-data))
2743     (save-excursion
2744       (gnus-save-hidden-threads
2745         (gnus-summary-show-all-threads)
2746         (goto-char (point-min))
2747         (while data
2748           (while (get-text-property (point) 'gnus-intangible)
2749             (forward-line 1))
2750           (gnus-data-set-pos (car data) (+ (point) 3))
2751           (setq data (cdr data))
2752           (forward-line 1))))))
2753
2754 (defun gnus-hidden-threads-configuration ()
2755   "Return the current hidden threads configuration."
2756   (save-excursion
2757     (let (config)
2758       (goto-char (point-min))
2759       (while (search-forward "\r" nil t)
2760         (push (1- (point)) config))
2761       config)))
2762
2763 (defun gnus-restore-hidden-threads-configuration (config)
2764   "Restore hidden threads configuration from CONFIG."
2765   (save-excursion
2766     (let (point buffer-read-only)
2767       (while (setq point (pop config))
2768         (when (and (< point (point-max))
2769                    (goto-char point)
2770                    (eq (char-after) ?\n))
2771           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2772
2773 ;; Various summary mode internalish functions.
2774
2775 (defun gnus-mouse-pick-article (e)
2776   (interactive "e")
2777   (mouse-set-point e)
2778   (gnus-summary-next-page nil t))
2779
2780 (defun gnus-summary-set-display-table ()
2781   "Change the display table.
2782 Odd characters have a tendency to mess
2783 up nicely formatted displays - we make all possible glyphs
2784 display only a single character."
2785
2786   ;; We start from the standard display table, if any.
2787   (let ((table (or (copy-sequence standard-display-table)
2788                    (make-display-table)))
2789         (i 32))
2790     ;; Nix out all the control chars...
2791     (while (>= (setq i (1- i)) 0)
2792       (aset table i [??]))
2793    ;; ... but not newline and cr, of course.  (cr is necessary for the
2794     ;; selective display).
2795     (aset table ?\n nil)
2796     (aset table ?\r nil)
2797     ;; We keep TAB as well.
2798     (aset table ?\t nil)
2799     ;; We nix out any glyphs over 126 that are not set already.
2800     (let ((i 256))
2801       (while (>= (setq i (1- i)) 127)
2802         ;; Only modify if the entry is nil.
2803         (unless (aref table i)
2804           (aset table i [??]))))
2805     (setq buffer-display-table table)))
2806
2807 (defun gnus-summary-set-article-display-arrow (pos)
2808   "Update the overlay arrow to point to line at position POS."
2809   (when (and gnus-summary-display-arrow
2810              (boundp 'overlay-arrow-position)
2811              (boundp 'overlay-arrow-string))
2812     (save-excursion
2813       (goto-char pos)
2814       (beginning-of-line)
2815       (unless overlay-arrow-position
2816         (setq overlay-arrow-position (make-marker)))
2817       (setq overlay-arrow-string "=>"
2818             overlay-arrow-position (set-marker overlay-arrow-position
2819                                                (point)
2820                                                (current-buffer))))))
2821
2822 (defun gnus-summary-buffer-name (group)
2823   "Return the summary buffer name of GROUP."
2824   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2825
2826 (defun gnus-summary-setup-buffer (group)
2827   "Initialize summary buffer."
2828   (let ((buffer (gnus-summary-buffer-name group))
2829         (dead-name (concat "*Dead Summary "
2830                            (gnus-group-decoded-name group) "*")))
2831     ;; If a dead summary buffer exists, we kill it.
2832     (when (gnus-buffer-live-p dead-name)
2833       (gnus-kill-buffer dead-name))
2834     (if (get-buffer buffer)
2835         (progn
2836           (set-buffer buffer)
2837           (setq gnus-summary-buffer (current-buffer))
2838           (not gnus-newsgroup-prepared))
2839       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2840       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2841       (gnus-summary-mode group)
2842       (when gnus-carpal
2843         (gnus-carpal-setup-buffer 'summary))
2844       (unless gnus-single-article-buffer
2845         (make-local-variable 'gnus-article-buffer)
2846         (make-local-variable 'gnus-article-current)
2847         (make-local-variable 'gnus-original-article-buffer))
2848       (setq gnus-newsgroup-name group)
2849       ;; Set any local variables in the group parameters.
2850       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2851       t)))
2852
2853 (defun gnus-set-global-variables ()
2854   "Set the global equivalents of the buffer-local variables.
2855 They are set to the latest values they had.  These reflect the summary
2856 buffer that was in action when the last article was fetched."
2857   (when (eq major-mode 'gnus-summary-mode)
2858     (setq gnus-summary-buffer (current-buffer))
2859     (let ((name gnus-newsgroup-name)
2860           (marked gnus-newsgroup-marked)
2861           (unread gnus-newsgroup-unreads)
2862           (headers gnus-current-headers)
2863           (data gnus-newsgroup-data)
2864           (summary gnus-summary-buffer)
2865           (article-buffer gnus-article-buffer)
2866           (original gnus-original-article-buffer)
2867           (gac gnus-article-current)
2868           (reffed gnus-reffed-article-number)
2869           (score-file gnus-current-score-file)
2870           (default-charset gnus-newsgroup-charset)
2871           vlist)
2872       (let ((locals gnus-newsgroup-variables))
2873         (while locals
2874           (if (consp (car locals))
2875               (push (eval (caar locals)) vlist)
2876             (push (eval (car locals)) vlist))
2877           (setq locals (cdr locals)))
2878         (setq vlist (nreverse vlist)))
2879       (save-excursion
2880         (set-buffer gnus-group-buffer)
2881         (setq gnus-newsgroup-name name
2882               gnus-newsgroup-marked marked
2883               gnus-newsgroup-unreads unread
2884               gnus-current-headers headers
2885               gnus-newsgroup-data data
2886               gnus-article-current gac
2887               gnus-summary-buffer summary
2888               gnus-article-buffer article-buffer
2889               gnus-original-article-buffer original
2890               gnus-reffed-article-number reffed
2891               gnus-current-score-file score-file
2892               gnus-newsgroup-charset default-charset)
2893         (let ((locals gnus-newsgroup-variables))
2894           (while locals
2895             (if (consp (car locals))
2896                 (set (caar locals) (pop vlist))
2897               (set (car locals) (pop vlist)))
2898             (setq locals (cdr locals))))
2899         ;; The article buffer also has local variables.
2900         (when (gnus-buffer-live-p gnus-article-buffer)
2901           (set-buffer gnus-article-buffer)
2902           (setq gnus-summary-buffer summary))))))
2903
2904 (defun gnus-summary-article-unread-p (article)
2905   "Say whether ARTICLE is unread or not."
2906   (memq article gnus-newsgroup-unreads))
2907
2908 (defun gnus-summary-first-article-p (&optional article)
2909   "Return whether ARTICLE is the first article in the buffer."
2910   (if (not (setq article (or article (gnus-summary-article-number))))
2911       nil
2912     (eq article (caar gnus-newsgroup-data))))
2913
2914 (defun gnus-summary-last-article-p (&optional article)
2915   "Return whether ARTICLE is the last article in the buffer."
2916   (if (not (setq article (or article (gnus-summary-article-number))))
2917       ;; All non-existent numbers are the last article.  :-)
2918       t
2919     (not (cdr (gnus-data-find-list article)))))
2920
2921 (defun gnus-make-thread-indent-array ()
2922   (let ((n 200))
2923     (unless (and gnus-thread-indent-array
2924                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2925       (setq gnus-thread-indent-array (make-vector 201 "")
2926             gnus-thread-indent-array-level gnus-thread-indent-level)
2927       (while (>= n 0)
2928         (aset gnus-thread-indent-array n
2929               (make-string (* n gnus-thread-indent-level) ? ))
2930         (setq n (1- n))))))
2931
2932 (defun gnus-update-summary-mark-positions ()
2933   "Compute where the summary marks are to go."
2934   (save-excursion
2935     (when (gnus-buffer-exists-p gnus-summary-buffer)
2936       (set-buffer gnus-summary-buffer))
2937     (let ((gnus-replied-mark 129)
2938           (gnus-score-below-mark 130)
2939           (gnus-score-over-mark 130)
2940           (gnus-download-mark 131)
2941           (spec gnus-summary-line-format-spec)
2942           gnus-visual pos)
2943       (save-excursion
2944         (gnus-set-work-buffer)
2945         (let ((gnus-summary-line-format-spec spec)
2946               (gnus-newsgroup-downloadable '((0 . t))))
2947           (gnus-summary-insert-line
2948            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2949            0 nil 128 t nil "" nil 1)
2950           (goto-char (point-min))
2951           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2952                                              (- (point) 2)))))
2953           (goto-char (point-min))
2954           (push (cons 'replied (and (search-forward "\201" nil t)
2955                                     (- (point) 2)))
2956                 pos)
2957           (goto-char (point-min))
2958           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2959                 pos)
2960           (goto-char (point-min))
2961           (push (cons 'download
2962                       (and (search-forward "\203" nil t) (- (point) 2)))
2963                 pos)))
2964       (setq gnus-summary-mark-positions pos))))
2965
2966 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2967   "Insert a dummy root in the summary buffer."
2968   (beginning-of-line)
2969   (gnus-add-text-properties
2970    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2971    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2972
2973 (defun gnus-summary-extract-address-component (from)
2974   (or (car (funcall gnus-extract-address-components from))
2975       from))
2976
2977 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2978   (let ((mail-parse-charset gnus-newsgroup-charset)
2979         ; Is it really necessary to do this next part for each summary line?
2980         ; Luckily, doesn't seem to slow things down much.
2981         (mail-parse-ignored-charsets
2982          (save-excursion (set-buffer gnus-summary-buffer)
2983                          gnus-newsgroup-ignored-charsets)))
2984     (or
2985      (and gnus-ignored-from-addresses
2986           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2987           (let ((extra-headers (mail-header-extra header))
2988                 to
2989                 newsgroups)
2990             (cond
2991              ((setq to (cdr (assq 'To extra-headers)))
2992               (concat "-> "
2993                       (inline
2994                         (gnus-summary-extract-address-component
2995                          (funcall gnus-decode-encoded-word-function to)))))
2996              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2997               (concat "=> " newsgroups)))))
2998      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
2999
3000 (defun gnus-summary-insert-line (gnus-tmp-header
3001                                  gnus-tmp-level gnus-tmp-current
3002                                  gnus-tmp-unread gnus-tmp-replied
3003                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3004                                  &optional gnus-tmp-dummy gnus-tmp-score
3005                                  gnus-tmp-process)
3006   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3007          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3008          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3009          (gnus-tmp-score-char
3010           (if (or (null gnus-summary-default-score)
3011                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3012                       gnus-summary-zcore-fuzz))
3013               ?                         ;Whitespace
3014             (if (< gnus-tmp-score gnus-summary-default-score)
3015                 gnus-score-below-mark gnus-score-over-mark)))
3016          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3017          (gnus-tmp-replied
3018           (cond (gnus-tmp-process gnus-process-mark)
3019                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3020                  gnus-cached-mark)
3021                 (gnus-tmp-replied gnus-replied-mark)
3022                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3023                  gnus-forwarded-mark)
3024                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3025                  gnus-saved-mark)
3026                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3027                  gnus-recent-mark)
3028                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3029                  gnus-unseen-mark)
3030                 (t gnus-no-mark)))
3031          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3032          (gnus-tmp-name
3033           (cond
3034            ((string-match "<[^>]+> *$" gnus-tmp-from)
3035             (let ((beg (match-beginning 0)))
3036               (or (and (string-match "^\".+\"" gnus-tmp-from)
3037                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3038                   (substring gnus-tmp-from 0 beg))))
3039            ((string-match "(.+)" gnus-tmp-from)
3040             (substring gnus-tmp-from
3041                        (1+ (match-beginning 0)) (1- (match-end 0))))
3042            (t gnus-tmp-from)))
3043          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3044          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3045          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3046          (buffer-read-only nil))
3047     (when (string= gnus-tmp-name "")
3048       (setq gnus-tmp-name gnus-tmp-from))
3049     (unless (numberp gnus-tmp-lines)
3050       (setq gnus-tmp-lines -1))
3051     (if (= gnus-tmp-lines -1)
3052         (setq gnus-tmp-lines "?")
3053       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3054     (gnus-put-text-property
3055      (point)
3056      (progn (eval gnus-summary-line-format-spec) (point))
3057      'gnus-number gnus-tmp-number)
3058     (when (gnus-visual-p 'summary-highlight 'highlight)
3059       (forward-line -1)
3060       (gnus-run-hooks 'gnus-summary-update-hook)
3061       (forward-line 1))))
3062
3063 (defun gnus-summary-update-line (&optional dont-update)
3064   "Update summary line after change."
3065   (when (and gnus-summary-default-score
3066              (not gnus-summary-inhibit-highlight))
3067     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3068            (article (gnus-summary-article-number))
3069            (score (gnus-summary-article-score article)))
3070       (unless dont-update
3071         (if (and gnus-summary-mark-below
3072                  (< (gnus-summary-article-score)
3073                     gnus-summary-mark-below))
3074             ;; This article has a low score, so we mark it as read.
3075             (when (memq article gnus-newsgroup-unreads)
3076               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3077           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3078             ;; This article was previously marked as read on account
3079             ;; of a low score, but now it has risen, so we mark it as
3080             ;; unread.
3081             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3082         (gnus-summary-update-mark
3083          (if (or (null gnus-summary-default-score)
3084                  (<= (abs (- score gnus-summary-default-score))
3085                      gnus-summary-zcore-fuzz))
3086              ?                          ;Whitespace
3087            (if (< score gnus-summary-default-score)
3088                gnus-score-below-mark gnus-score-over-mark))
3089          'score))
3090       ;; Do visual highlighting.
3091       (when (gnus-visual-p 'summary-highlight 'highlight)
3092         (gnus-run-hooks 'gnus-summary-update-hook)))))
3093
3094 (defvar gnus-tmp-new-adopts nil)
3095
3096 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3097   "Return the number of articles in THREAD.
3098 This may be 0 in some cases -- if none of the articles in
3099 the thread are to be displayed."
3100   (let* ((number
3101          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3102           (cond
3103            ((not (listp thread))
3104             1)
3105            ((and (consp thread) (cdr thread))
3106             (apply
3107              '+ 1 (mapcar
3108                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3109            ((null thread)
3110             1)
3111            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3112             1)
3113            (t 0))))
3114     (when (and level (zerop level) gnus-tmp-new-adopts)
3115       (incf number
3116             (apply '+ (mapcar
3117                        'gnus-summary-number-of-articles-in-thread
3118                        gnus-tmp-new-adopts))))
3119     (if char
3120         (if (> number 1) gnus-not-empty-thread-mark
3121           gnus-empty-thread-mark)
3122       number)))
3123
3124 (defun gnus-summary-set-local-parameters (group)
3125   "Go through the local params of GROUP and set all variable specs in that list."
3126   (let ((params (gnus-group-find-parameter group))
3127         (vars '(quit-config))           ; Ignore quit-config.
3128         elem)
3129     (while params
3130       (setq elem (car params)
3131             params (cdr params))
3132       (and (consp elem)                 ; Has to be a cons.
3133            (consp (cdr elem))           ; The cdr has to be a list.
3134            (symbolp (car elem))         ; Has to be a symbol in there.
3135            (not (memq (car elem) vars))
3136            (ignore-errors               ; So we set it.
3137              (push (car elem) vars)
3138              (make-local-variable (car elem))
3139              (set (car elem) (eval (nth 1 elem))))))))
3140
3141 (defun gnus-summary-read-group (group &optional show-all no-article
3142                                       kill-buffer no-display backward
3143                                       select-articles)
3144   "Start reading news in newsgroup GROUP.
3145 If SHOW-ALL is non-nil, already read articles are also listed.
3146 If NO-ARTICLE is non-nil, no article is selected initially.
3147 If NO-DISPLAY, don't generate a summary buffer."
3148   (let (result)
3149     (while (and group
3150                 (null (setq result
3151                             (let ((gnus-auto-select-next nil))
3152                               (or (gnus-summary-read-group-1
3153                                    group show-all no-article
3154                                    kill-buffer no-display
3155                                    select-articles)
3156                                   (setq show-all nil
3157                                         select-articles nil)))))
3158                 (eq gnus-auto-select-next 'quietly))
3159       (set-buffer gnus-group-buffer)
3160       ;; The entry function called above goes to the next
3161       ;; group automatically, so we go two groups back
3162       ;; if we are searching for the previous group.
3163       (when backward
3164         (gnus-group-prev-unread-group 2))
3165       (if (not (equal group (gnus-group-group-name)))
3166           (setq group (gnus-group-group-name))
3167         (setq group nil)))
3168     result))
3169
3170 (defun gnus-summary-read-group-1 (group show-all no-article
3171                                         kill-buffer no-display
3172                                         &optional select-articles)
3173   ;; Killed foreign groups can't be entered.
3174   ;;  (when (and (not (gnus-group-native-p group))
3175   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3176   ;;    (error "Dead non-native groups can't be entered"))
3177   (gnus-message 5 "Retrieving newsgroup: %s..."
3178                 (gnus-group-decoded-name group))
3179   (let* ((new-group (gnus-summary-setup-buffer group))
3180          (quit-config (gnus-group-quit-config group))
3181          (did-select (and new-group (gnus-select-newsgroup
3182                                      group show-all select-articles))))
3183     (cond
3184      ;; This summary buffer exists already, so we just select it.
3185      ((not new-group)
3186       (gnus-set-global-variables)
3187       (when kill-buffer
3188         (gnus-kill-or-deaden-summary kill-buffer))
3189       (gnus-configure-windows 'summary 'force)
3190       (gnus-set-mode-line 'summary)
3191       (gnus-summary-position-point)
3192       (message "")
3193       t)
3194      ;; We couldn't select this group.
3195      ((null did-select)
3196       (when (and (eq major-mode 'gnus-summary-mode)
3197                  (not (equal (current-buffer) kill-buffer)))
3198         (kill-buffer (current-buffer))
3199         (if (not quit-config)
3200             (progn
3201               ;; Update the info -- marks might need to be removed,
3202               ;; for instance.
3203               (gnus-summary-update-info)
3204               (set-buffer gnus-group-buffer)
3205               (gnus-group-jump-to-group group)
3206               (gnus-group-next-unread-group 1))
3207           (gnus-handle-ephemeral-exit quit-config)))
3208       (let ((grpinfo (gnus-get-info group)))
3209         (if (null (gnus-info-read grpinfo))
3210             (gnus-message 3 "Group %s contains no messages"
3211                           (gnus-group-decoded-name group))
3212           (gnus-message 3 "Can't select group")))
3213       nil)
3214      ;; The user did a `C-g' while prompting for number of articles,
3215      ;; so we exit this group.
3216      ((eq did-select 'quit)
3217       (and (eq major-mode 'gnus-summary-mode)
3218            (not (equal (current-buffer) kill-buffer))
3219            (kill-buffer (current-buffer)))
3220       (when kill-buffer
3221         (gnus-kill-or-deaden-summary kill-buffer))
3222       (if (not quit-config)
3223           (progn
3224             (set-buffer gnus-group-buffer)
3225             (gnus-group-jump-to-group group)
3226             (gnus-group-next-unread-group 1)
3227             (gnus-configure-windows 'group 'force))
3228         (gnus-handle-ephemeral-exit quit-config))
3229       ;; Finally signal the quit.
3230       (signal 'quit nil))
3231      ;; The group was successfully selected.
3232      (t
3233       (gnus-set-global-variables)
3234       ;; Save the active value in effect when the group was entered.
3235       (setq gnus-newsgroup-active
3236             (gnus-copy-sequence
3237              (gnus-active gnus-newsgroup-name)))
3238       ;; You can change the summary buffer in some way with this hook.
3239       (gnus-run-hooks 'gnus-select-group-hook)
3240       (gnus-update-format-specifications
3241        nil 'summary 'summary-mode 'summary-dummy)
3242       (gnus-update-summary-mark-positions)
3243       ;; Do score processing.
3244       (when gnus-use-scoring
3245         (gnus-possibly-score-headers))
3246       ;; Check whether to fill in the gaps in the threads.
3247       (when gnus-build-sparse-threads
3248         (gnus-build-sparse-threads))
3249       ;; Find the initial limit.
3250       (if gnus-show-threads
3251           (if show-all
3252               (let ((gnus-newsgroup-dormant nil))
3253                 (gnus-summary-initial-limit show-all))
3254             (gnus-summary-initial-limit show-all))
3255         ;; When untreaded, all articles are always shown.
3256         (setq gnus-newsgroup-limit
3257               (mapcar
3258                (lambda (header) (mail-header-number header))
3259                gnus-newsgroup-headers)))
3260       ;; Generate the summary buffer.
3261       (unless no-display
3262         (gnus-summary-prepare))
3263       (when gnus-use-trees
3264         (gnus-tree-open group)
3265         (setq gnus-summary-highlight-line-function
3266               'gnus-tree-highlight-article))
3267       ;; If the summary buffer is empty, but there are some low-scored
3268       ;; articles or some excluded dormants, we include these in the
3269       ;; buffer.
3270       (when (and (zerop (buffer-size))
3271                  (not no-display))
3272         (cond (gnus-newsgroup-dormant
3273                (gnus-summary-limit-include-dormant))
3274               ((and gnus-newsgroup-scored show-all)
3275                (gnus-summary-limit-include-expunged t))))
3276       ;; Function `gnus-apply-kill-file' must be called in this hook.
3277       (gnus-run-hooks 'gnus-apply-kill-hook)
3278       (if (and (zerop (buffer-size))
3279                (not no-display))
3280           (progn
3281             ;; This newsgroup is empty.
3282             (gnus-summary-catchup-and-exit nil t)
3283             (gnus-message 6 "No unread news")
3284             (when kill-buffer
3285               (gnus-kill-or-deaden-summary kill-buffer))
3286             ;; Return nil from this function.
3287             nil)
3288         ;; Hide conversation thread subtrees.  We cannot do this in
3289         ;; gnus-summary-prepare-hook since kill processing may not
3290         ;; work with hidden articles.
3291         (gnus-summary-maybe-hide-threads)
3292         (when kill-buffer
3293           (gnus-kill-or-deaden-summary kill-buffer))
3294         (gnus-summary-auto-select-subject)
3295         ;; Show first unread article if requested.
3296         (if (and (not no-article)
3297                  (not no-display)
3298                  gnus-newsgroup-unreads
3299                  gnus-auto-select-first)
3300             (progn
3301               (gnus-configure-windows 'summary)
3302               (let ((art (gnus-summary-article-number)))
3303                 (unless (or (memq art gnus-newsgroup-undownloaded)
3304                             (memq art gnus-newsgroup-downloadable))
3305                   (gnus-summary-goto-article art))))
3306           ;; Don't select any articles.
3307           (gnus-summary-position-point)
3308           (gnus-configure-windows 'summary 'force)
3309           (gnus-set-mode-line 'summary))
3310         (when (get-buffer-window gnus-group-buffer t)
3311           ;; Gotta use windows, because recenter does weird stuff if
3312           ;; the current buffer ain't the displayed window.
3313           (let ((owin (selected-window)))
3314             (select-window (get-buffer-window gnus-group-buffer t))
3315             (when (gnus-group-goto-group group)
3316               (recenter))
3317             (select-window owin)))
3318         ;; Mark this buffer as "prepared".
3319         (setq gnus-newsgroup-prepared t)
3320         (gnus-run-hooks 'gnus-summary-prepared-hook)
3321         t)))))
3322
3323 (defun gnus-summary-auto-select-subject ()
3324   "Select the subject line on initial group entry."
3325   (goto-char (point-min))
3326   (cond
3327    ((eq gnus-auto-select-subject 'best)
3328     (gnus-summary-best-unread-subject))
3329    ((eq gnus-auto-select-subject 'unread)
3330     (gnus-summary-first-unread-subject))
3331    ((eq gnus-auto-select-subject 'unseen)
3332     (gnus-summary-first-unseen-subject))
3333    ((eq gnus-auto-select-subject 'unseen-or-unread)
3334     (gnus-summary-first-unseen-or-unread-subject))
3335    ((eq gnus-auto-select-subject 'first)
3336     ;; Do nothing.
3337     )
3338    ((gnus-functionp gnus-auto-select-subject)
3339     (funcall gnus-auto-select-subject))))
3340
3341 (defun gnus-summary-prepare ()
3342   "Generate the summary buffer."
3343   (interactive)
3344   (let ((buffer-read-only nil))
3345     (erase-buffer)
3346     (setq gnus-newsgroup-data nil
3347           gnus-newsgroup-data-reverse nil)
3348     (gnus-run-hooks 'gnus-summary-generate-hook)
3349     ;; Generate the buffer, either with threads or without.
3350     (when gnus-newsgroup-headers
3351       (gnus-summary-prepare-threads
3352        (if gnus-show-threads
3353            (gnus-sort-gathered-threads
3354             (funcall gnus-summary-thread-gathering-function
3355                      (gnus-sort-threads
3356                       (gnus-cut-threads (gnus-make-threads)))))
3357          ;; Unthreaded display.
3358          (gnus-sort-articles gnus-newsgroup-headers))))
3359     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3360     ;; Call hooks for modifying summary buffer.
3361     (goto-char (point-min))
3362     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3363
3364 (defsubst gnus-general-simplify-subject (subject)
3365   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3366   (setq subject
3367         (cond
3368          ;; Truncate the subject.
3369          (gnus-simplify-subject-functions
3370           (gnus-map-function gnus-simplify-subject-functions subject))
3371          ((numberp gnus-summary-gather-subject-limit)
3372           (setq subject (gnus-simplify-subject-re subject))
3373           (if (> (length subject) gnus-summary-gather-subject-limit)
3374               (substring subject 0 gnus-summary-gather-subject-limit)
3375             subject))
3376          ;; Fuzzily simplify it.
3377          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3378           (gnus-simplify-subject-fuzzy subject))
3379          ;; Just remove the leading "Re:".
3380          (t
3381           (gnus-simplify-subject-re subject))))
3382
3383   (if (and gnus-summary-gather-exclude-subject
3384            (string-match gnus-summary-gather-exclude-subject subject))
3385       nil                         ; This article shouldn't be gathered
3386     subject))
3387
3388 (defun gnus-summary-simplify-subject-query ()
3389   "Query where the respool algorithm would put this article."
3390   (interactive)
3391   (gnus-summary-select-article)
3392   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3393
3394 (defun gnus-gather-threads-by-subject (threads)
3395   "Gather threads by looking at Subject headers."
3396   (if (not gnus-summary-make-false-root)
3397       threads
3398     (let ((hashtb (gnus-make-hashtable 1024))
3399           (prev threads)
3400           (result threads)
3401           subject hthread whole-subject)
3402       (while threads
3403         (setq subject (gnus-general-simplify-subject
3404                        (setq whole-subject (mail-header-subject
3405                                             (caar threads)))))
3406         (when subject
3407           (if (setq hthread (gnus-gethash subject hashtb))
3408               (progn
3409                 ;; We enter a dummy root into the thread, if we
3410                 ;; haven't done that already.
3411                 (unless (stringp (caar hthread))
3412                   (setcar hthread (list whole-subject (car hthread))))
3413                 ;; We add this new gathered thread to this gathered
3414                 ;; thread.
3415                 (setcdr (car hthread)
3416                         (nconc (cdar hthread) (list (car threads))))
3417                 ;; Remove it from the list of threads.
3418                 (setcdr prev (cdr threads))
3419                 (setq threads prev))
3420             ;; Enter this thread into the hash table.
3421             (gnus-sethash subject threads hashtb)))
3422         (setq prev threads)
3423         (setq threads (cdr threads)))
3424       result)))
3425
3426 (defun gnus-gather-threads-by-references (threads)
3427   "Gather threads by looking at References headers."
3428   (let ((idhashtb (gnus-make-hashtable 1024))
3429         (thhashtb (gnus-make-hashtable 1024))
3430         (prev threads)
3431         (result threads)
3432         ids references id gthread gid entered ref)
3433     (while threads
3434       (when (setq references (mail-header-references (caar threads)))
3435         (setq id (mail-header-id (caar threads))
3436               ids (inline (gnus-split-references references))
3437               entered nil)
3438         (while (setq ref (pop ids))
3439           (setq ids (delete ref ids))
3440           (if (not (setq gid (gnus-gethash ref idhashtb)))
3441               (progn
3442                 (gnus-sethash ref id idhashtb)
3443                 (gnus-sethash id threads thhashtb))
3444             (setq gthread (gnus-gethash gid thhashtb))
3445             (unless entered
3446               ;; We enter a dummy root into the thread, if we
3447               ;; haven't done that already.
3448               (unless (stringp (caar gthread))
3449                 (setcar gthread (list (mail-header-subject (caar gthread))
3450                                       (car gthread))))
3451               ;; We add this new gathered thread to this gathered
3452               ;; thread.
3453               (setcdr (car gthread)
3454                       (nconc (cdar gthread) (list (car threads)))))
3455             ;; Add it into the thread hash table.
3456             (gnus-sethash id gthread thhashtb)
3457             (setq entered t)
3458             ;; Remove it from the list of threads.
3459             (setcdr prev (cdr threads))
3460             (setq threads prev))))
3461       (setq prev threads)
3462       (setq threads (cdr threads)))
3463     result))
3464
3465 (defun gnus-sort-gathered-threads (threads)
3466   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3467   (let ((result threads))
3468     (while threads
3469       (when (stringp (caar threads))
3470         (setcdr (car threads)
3471                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3472       (setq threads (cdr threads)))
3473     result))
3474
3475 (defun gnus-thread-loop-p (root thread)
3476   "Say whether ROOT is in THREAD."
3477   (let ((stack (list thread))
3478         (infloop 0)
3479         th)
3480     (while (setq thread (pop stack))
3481       (setq th (cdr thread))
3482       (while (and th
3483                   (not (eq (caar th) root)))
3484         (pop th))
3485       (if th
3486           ;; We have found a loop.
3487           (let (ref-dep)
3488             (setcdr thread (delq (car th) (cdr thread)))
3489             (if (boundp (setq ref-dep (intern "none"
3490                                               gnus-newsgroup-dependencies)))
3491                 (setcdr (symbol-value ref-dep)
3492                         (nconc (cdr (symbol-value ref-dep))
3493                                (list (car th))))
3494               (set ref-dep (list nil (car th))))
3495             (setq infloop 1
3496                   stack nil))
3497         ;; Push all the subthreads onto the stack.
3498         (push (cdr thread) stack)))
3499     infloop))
3500
3501 (defun gnus-make-threads ()
3502   "Go through the dependency hashtb and find the roots.  Return all threads."
3503   (let (threads)
3504     (while (catch 'infloop
3505              (mapatoms
3506               (lambda (refs)
3507                 ;; Deal with self-referencing References loops.
3508                 (when (and (car (symbol-value refs))
3509                            (not (zerop
3510                                  (apply
3511                                   '+
3512                                   (mapcar
3513                                    (lambda (thread)
3514                                      (gnus-thread-loop-p
3515                                       (car (symbol-value refs)) thread))
3516                                    (cdr (symbol-value refs)))))))
3517                   (setq threads nil)
3518                   (throw 'infloop t))
3519                 (unless (car (symbol-value refs))
3520              ;; These threads do not refer back to any other articles,
3521                   ;; so they're roots.
3522                   (setq threads (append (cdr (symbol-value refs)) threads))))
3523               gnus-newsgroup-dependencies)))
3524     threads))
3525
3526 ;; Build the thread tree.
3527 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3528   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3529
3530 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3531 if it was already present.
3532
3533 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3534 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3535 Message-IDs will be renamed be renamed to a unique Message-ID before
3536 being entered.
3537
3538 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3539   (let* ((id (mail-header-id header))
3540          (id-dep (and id (intern id dependencies)))
3541          ref ref-dep ref-header)
3542     ;; Enter this `header' in the `dependencies' table.
3543     (cond
3544      ((not id-dep)
3545       (setq header nil))
3546      ;; The first two cases do the normal part: enter a new `header'
3547      ;; in the `dependencies' table.
3548      ((not (boundp id-dep))
3549       (set id-dep (list header)))
3550      ((null (car (symbol-value id-dep)))
3551       (setcar (symbol-value id-dep) header))
3552
3553      ;; From here the `header' was already present in the
3554      ;; `dependencies' table.
3555      (force-new
3556       ;; Overrides an existing entry;
3557       ;; just set the header part of the entry.
3558       (setcar (symbol-value id-dep) header))
3559
3560      ;; Renames the existing `header' to a unique Message-ID.
3561      ((not gnus-summary-ignore-duplicates)
3562       ;; An article with this Message-ID has already been seen.
3563       ;; We rename the Message-ID.
3564       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3565            (list header))
3566       (mail-header-set-id header id))
3567
3568      ;; The last case ignores an existing entry, except it adds any
3569      ;; additional Xrefs (in case the two articles came from different
3570      ;; servers.
3571      ;; Also sets `header' to `nil' meaning that the `dependencies'
3572      ;; table was *not* modified.
3573      (t
3574       (mail-header-set-xref
3575        (car (symbol-value id-dep))
3576        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3577                    "")
3578                (or (mail-header-xref header) "")))
3579       (setq header nil)))
3580
3581     (when header
3582       ;; First check if that we are not creating a References loop.
3583       (setq ref (gnus-parent-id (mail-header-references header)))
3584       (while (and ref
3585                   (setq ref-dep (intern-soft ref dependencies))
3586                   (boundp ref-dep)
3587                   (setq ref-header (car (symbol-value ref-dep))))
3588         (if (string= id ref)
3589             ;; Yuk!  This is a reference loop.  Make the article be a
3590             ;; root article.
3591             (progn
3592               (mail-header-set-references (car (symbol-value id-dep)) "none")
3593               (setq ref nil))
3594           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3595       (setq ref (gnus-parent-id (mail-header-references header)))
3596       (setq ref-dep (intern (or ref "none") dependencies))
3597       (if (boundp ref-dep)
3598           (setcdr (symbol-value ref-dep)
3599                   (nconc (cdr (symbol-value ref-dep))
3600                          (list (symbol-value id-dep))))
3601         (set ref-dep (list nil (symbol-value id-dep)))))
3602     header))
3603
3604 (defun gnus-build-sparse-threads ()
3605   (let ((headers gnus-newsgroup-headers)
3606         (mail-parse-charset gnus-newsgroup-charset)
3607         (gnus-summary-ignore-duplicates t)
3608         header references generation relations
3609         subject child end new-child date)
3610     ;; First we create an alist of generations/relations, where
3611     ;; generations is how much we trust the relation, and the relation
3612     ;; is parent/child.
3613     (gnus-message 7 "Making sparse threads...")
3614     (save-excursion
3615       (nnheader-set-temp-buffer " *gnus sparse threads*")
3616       (while (setq header (pop headers))
3617         (when (and (setq references (mail-header-references header))
3618                    (not (string= references "")))
3619           (insert references)
3620           (setq child (mail-header-id header)
3621                 subject (mail-header-subject header)
3622                 date (mail-header-date header)
3623                 generation 0)
3624           (while (search-backward ">" nil t)
3625             (setq end (1+ (point)))
3626             (when (search-backward "<" nil t)
3627               (setq new-child (buffer-substring (point) end))
3628               (push (list (incf generation)
3629                           child (setq child new-child)
3630                           subject date)
3631                     relations)))
3632           (when child
3633             (push (list (1+ generation) child nil subject) relations))
3634           (erase-buffer)))
3635       (kill-buffer (current-buffer)))
3636     ;; Sort over trustworthiness.
3637     (mapcar
3638      (lambda (relation)
3639        (when (gnus-dependencies-add-header
3640               (make-full-mail-header
3641                gnus-reffed-article-number
3642                (nth 3 relation) "" (or (nth 4 relation) "")
3643                (nth 1 relation)
3644                (or (nth 2 relation) "") 0 0 "")
3645               gnus-newsgroup-dependencies nil)
3646          (push gnus-reffed-article-number gnus-newsgroup-limit)
3647          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3648          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3649                gnus-newsgroup-reads)
3650          (decf gnus-reffed-article-number)))
3651      (sort relations 'car-less-than-car))
3652     (gnus-message 7 "Making sparse threads...done")))
3653
3654 (defun gnus-build-old-threads ()
3655   ;; Look at all the articles that refer back to old articles, and
3656   ;; fetch the headers for the articles that aren't there.  This will
3657   ;; build complete threads - if the roots haven't been expired by the
3658   ;; server, that is.
3659   (let ((mail-parse-charset gnus-newsgroup-charset)
3660         id heads)
3661     (mapatoms
3662      (lambda (refs)
3663        (when (not (car (symbol-value refs)))
3664          (setq heads (cdr (symbol-value refs)))
3665          (while heads
3666            (if (memq (mail-header-number (caar heads))
3667                      gnus-newsgroup-dormant)
3668                (setq heads (cdr heads))
3669              (setq id (symbol-name refs))
3670              (while (and (setq id (gnus-build-get-header id))
3671                          (not (car (gnus-id-to-thread id)))))
3672              (setq heads nil)))))
3673      gnus-newsgroup-dependencies)))
3674
3675 ;; This function has to be called with point after the article number
3676 ;; on the beginning of the line.
3677 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3678   (let ((eol (gnus-point-at-eol))
3679         (buffer (current-buffer))
3680         header)
3681
3682     ;; overview: [num subject from date id refs chars lines misc]
3683     (unwind-protect
3684         (progn
3685           (narrow-to-region (point) eol)
3686           (unless (eobp)
3687             (forward-char))
3688
3689           (setq header
3690                 (make-full-mail-header
3691                  number                 ; number
3692                  (funcall gnus-decode-encoded-word-function
3693                           (nnheader-nov-field)) ; subject
3694                  (funcall gnus-decode-encoded-word-function
3695                           (nnheader-nov-field)) ; from
3696                  (nnheader-nov-field)   ; date
3697                  (nnheader-nov-read-message-id) ; id
3698                  (nnheader-nov-field)   ; refs
3699                  (nnheader-nov-read-integer) ; chars
3700                  (nnheader-nov-read-integer) ; lines
3701                  (unless (eobp)
3702                    (if (looking-at "Xref: ")
3703                        (goto-char (match-end 0)))
3704                    (nnheader-nov-field)) ; Xref
3705                  (nnheader-nov-parse-extra)))) ; extra
3706
3707       (widen))
3708
3709     (when gnus-alter-header-function
3710       (funcall gnus-alter-header-function header))
3711     (gnus-dependencies-add-header header dependencies force-new)))
3712
3713 (defun gnus-build-get-header (id)
3714   "Look through the buffer of NOV lines and find the header to ID.
3715 Enter this line into the dependencies hash table, and return
3716 the id of the parent article (if any)."
3717   (let ((deps gnus-newsgroup-dependencies)
3718         found header)
3719     (prog1
3720         (save-excursion
3721           (set-buffer nntp-server-buffer)
3722           (let ((case-fold-search nil))
3723             (goto-char (point-min))
3724             (while (and (not found)
3725                         (search-forward id nil t))
3726               (beginning-of-line)
3727               (setq found (looking-at
3728                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3729                                    (regexp-quote id))))
3730               (or found (beginning-of-line 2)))
3731             (when found
3732               (beginning-of-line)
3733               (and
3734                (setq header (gnus-nov-parse-line
3735                              (read (current-buffer)) deps))
3736                (gnus-parent-id (mail-header-references header))))))
3737       (when header
3738         (let ((number (mail-header-number header)))
3739           (push number gnus-newsgroup-limit)
3740           (push header gnus-newsgroup-headers)
3741           (if (memq number gnus-newsgroup-unselected)
3742               (progn
3743                 (push number gnus-newsgroup-unreads)
3744                 (setq gnus-newsgroup-unselected
3745                       (delq number gnus-newsgroup-unselected)))
3746             (push number gnus-newsgroup-ancient)))))))
3747
3748 (defun gnus-build-all-threads ()
3749   "Read all the headers."
3750   (let ((gnus-summary-ignore-duplicates t)
3751         (mail-parse-charset gnus-newsgroup-charset)
3752         (dependencies gnus-newsgroup-dependencies)
3753         header article)
3754     (save-excursion
3755       (set-buffer nntp-server-buffer)
3756       (let ((case-fold-search nil))
3757         (goto-char (point-min))
3758         (while (not (eobp))
3759           (ignore-errors
3760             (setq article (read (current-buffer))
3761                   header (gnus-nov-parse-line article dependencies)))
3762           (when header
3763             (save-excursion
3764               (set-buffer gnus-summary-buffer)
3765               (push header gnus-newsgroup-headers)
3766               (if (memq (setq article (mail-header-number header))
3767                         gnus-newsgroup-unselected)
3768                   (progn
3769                     (push article gnus-newsgroup-unreads)
3770                     (setq gnus-newsgroup-unselected
3771                           (delq article gnus-newsgroup-unselected)))
3772                 (push article gnus-newsgroup-ancient)))
3773             (forward-line 1)))))))
3774
3775 (defun gnus-summary-update-article-line (article header)
3776   "Update the line for ARTICLE using HEADERS."
3777   (let* ((id (mail-header-id header))
3778          (thread (gnus-id-to-thread id)))
3779     (unless thread
3780       (error "Article in no thread"))
3781     ;; Update the thread.
3782     (setcar thread header)
3783     (gnus-summary-goto-subject article)
3784     (let* ((datal (gnus-data-find-list article))
3785            (data (car datal))
3786            (length (when (cdr datal)
3787                      (- (gnus-data-pos data)
3788                         (gnus-data-pos (cadr datal)))))
3789            (buffer-read-only nil)
3790            (level (gnus-summary-thread-level)))
3791       (gnus-delete-line)
3792       (gnus-summary-insert-line
3793        header level nil (gnus-article-mark article)
3794        (memq article gnus-newsgroup-replied)
3795        (memq article gnus-newsgroup-expirable)
3796        ;; Only insert the Subject string when it's different
3797        ;; from the previous Subject string.
3798        (if (and
3799             gnus-show-threads
3800             (gnus-subject-equal
3801              (condition-case ()
3802                  (mail-header-subject
3803                   (gnus-data-header
3804                    (cadr
3805                     (gnus-data-find-list
3806                      article
3807                      (gnus-data-list t)))))
3808                ;; Error on the side of excessive subjects.
3809                (error ""))
3810              (mail-header-subject header)))
3811            ""
3812          (mail-header-subject header))
3813        nil (cdr (assq article gnus-newsgroup-scored))
3814        (memq article gnus-newsgroup-processable))
3815       (when length
3816         (gnus-data-update-list
3817          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3818
3819 (defun gnus-summary-update-article (article &optional iheader)
3820   "Update ARTICLE in the summary buffer."
3821   (set-buffer gnus-summary-buffer)
3822   (let* ((header (gnus-summary-article-header article))
3823          (id (mail-header-id header))
3824          (data (gnus-data-find article))
3825          (thread (gnus-id-to-thread id))
3826          (references (mail-header-references header))
3827          (parent
3828           (gnus-id-to-thread
3829            (or (gnus-parent-id
3830                 (when (and references
3831                            (not (equal "" references)))
3832                   references))
3833                "none")))
3834          (buffer-read-only nil)
3835          (old (car thread)))
3836     (when thread
3837       (unless iheader
3838         (setcar thread nil)
3839         (when parent
3840           (delq thread parent)))
3841       (if (gnus-summary-insert-subject id header)
3842        ;; Set the (possibly) new article number in the data structure.
3843           (gnus-data-set-number data (gnus-id-to-article id))
3844         (setcar thread old)
3845         nil))))
3846
3847 (defun gnus-rebuild-thread (id &optional line)
3848   "Rebuild the thread containing ID.
3849 If LINE, insert the rebuilt thread starting on line LINE."
3850   (let ((buffer-read-only nil)
3851         old-pos current thread data)
3852     (if (not gnus-show-threads)
3853         (setq thread (list (car (gnus-id-to-thread id))))
3854       ;; Get the thread this article is part of.
3855       (setq thread (gnus-remove-thread id)))
3856     (setq old-pos (gnus-point-at-bol))
3857     (setq current (save-excursion
3858                     (and (re-search-backward "[\r\n]" nil t)
3859                          (gnus-summary-article-number))))
3860     ;; If this is a gathered thread, we have to go some re-gathering.
3861     (when (stringp (car thread))
3862       (let ((subject (car thread))
3863             roots thr)
3864         (setq thread (cdr thread))
3865         (while thread
3866           (unless (memq (setq thr (gnus-id-to-thread
3867                                    (gnus-root-id
3868                                     (mail-header-id (caar thread)))))
3869                         roots)
3870             (push thr roots))
3871           (setq thread (cdr thread)))
3872         ;; We now have all (unique) roots.
3873         (if (= (length roots) 1)
3874             ;; All the loose roots are now one solid root.
3875             (setq thread (car roots))
3876           (setq thread (cons subject (gnus-sort-threads roots))))))
3877     (let (threads)
3878       ;; We then insert this thread into the summary buffer.
3879       (when line
3880         (goto-char (point-min))
3881         (forward-line (1- line)))
3882       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3883         (if gnus-show-threads
3884             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3885           (gnus-summary-prepare-unthreaded thread))
3886         (setq data (nreverse gnus-newsgroup-data))
3887         (setq threads gnus-newsgroup-threads))
3888       ;; We splice the new data into the data structure.
3889       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3890       ;;!!! then we want to insert at the beginning of the buffer.
3891       ;;!!! That happens to be true with Gnus now, but that may
3892       ;;!!! change in the future.  Perhaps.
3893       (gnus-data-enter-list
3894        (if line nil current) data (- (point) old-pos))
3895       (setq gnus-newsgroup-threads
3896             (nconc threads gnus-newsgroup-threads))
3897       (gnus-data-compute-positions))))
3898
3899 (defun gnus-number-to-header (number)
3900   "Return the header for article NUMBER."
3901   (let ((headers gnus-newsgroup-headers))
3902     (while (and headers
3903                 (not (= number (mail-header-number (car headers)))))
3904       (pop headers))
3905     (when headers
3906       (car headers))))
3907
3908 (defun gnus-parent-headers (in-headers &optional generation)
3909   "Return the headers of the GENERATIONeth parent of HEADERS."
3910   (unless generation
3911     (setq generation 1))
3912   (let ((parent t)
3913         (headers in-headers)
3914         references)
3915     (while (and parent
3916                 (not (zerop generation))
3917                 (setq references (mail-header-references headers)))
3918       (setq headers (if (and references
3919                              (setq parent (gnus-parent-id references)))
3920                         (car (gnus-id-to-thread parent))
3921                       nil))
3922       (decf generation))
3923     (and (not (eq headers in-headers))
3924          headers)))
3925
3926 (defun gnus-id-to-thread (id)
3927   "Return the (sub-)thread where ID appears."
3928   (gnus-gethash id gnus-newsgroup-dependencies))
3929
3930 (defun gnus-id-to-article (id)
3931   "Return the article number of ID."
3932   (let ((thread (gnus-id-to-thread id)))
3933     (when (and thread
3934                (car thread))
3935       (mail-header-number (car thread)))))
3936
3937 (defun gnus-id-to-header (id)
3938   "Return the article headers of ID."
3939   (car (gnus-id-to-thread id)))
3940
3941 (defun gnus-article-displayed-root-p (article)
3942   "Say whether ARTICLE is a root(ish) article."
3943   (let ((level (gnus-summary-thread-level article))
3944         (refs (mail-header-references  (gnus-summary-article-header article)))
3945         particle)
3946     (cond
3947      ((null level) nil)
3948      ((zerop level) t)
3949      ((null refs) t)
3950      ((null (gnus-parent-id refs)) t)
3951      ((and (= 1 level)
3952            (null (setq particle (gnus-id-to-article
3953                                  (gnus-parent-id refs))))
3954            (null (gnus-summary-thread-level particle)))))))
3955
3956 (defun gnus-root-id (id)
3957   "Return the id of the root of the thread where ID appears."
3958   (let (last-id prev)
3959     (while (and id (setq prev (car (gnus-id-to-thread id))))
3960       (setq last-id id
3961             id (gnus-parent-id (mail-header-references prev))))
3962     last-id))
3963
3964 (defun gnus-articles-in-thread (thread)
3965   "Return the list of articles in THREAD."
3966   (cons (mail-header-number (car thread))
3967         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3968
3969 (defun gnus-remove-thread (id &optional dont-remove)
3970   "Remove the thread that has ID in it."
3971   (let (headers thread last-id)
3972     ;; First go up in this thread until we find the root.
3973     (setq last-id (gnus-root-id id)
3974           headers (message-flatten-list (gnus-id-to-thread last-id)))
3975     ;; We have now found the real root of this thread.  It might have
3976     ;; been gathered into some loose thread, so we have to search
3977     ;; through the threads to find the thread we wanted.
3978     (let ((threads gnus-newsgroup-threads)
3979           sub)
3980       (while threads
3981         (setq sub (car threads))
3982         (if (stringp (car sub))
3983             ;; This is a gathered thread, so we look at the roots
3984             ;; below it to find whether this article is in this
3985             ;; gathered root.
3986             (progn
3987               (setq sub (cdr sub))
3988               (while sub
3989                 (when (member (caar sub) headers)
3990                   (setq thread (car threads)
3991                         threads nil
3992                         sub nil))
3993                 (setq sub (cdr sub))))
3994           ;; It's an ordinary thread, so we check it.
3995           (when (eq (car sub) (car headers))
3996             (setq thread sub
3997                   threads nil)))
3998         (setq threads (cdr threads)))
3999       ;; If this article is in no thread, then it's a root.
4000       (if thread
4001           (unless dont-remove
4002             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4003         (setq thread (gnus-id-to-thread last-id)))
4004       (when thread
4005         (prog1
4006             thread                      ; We return this thread.
4007           (unless dont-remove
4008             (if (stringp (car thread))
4009                 (progn
4010                   ;; If we use dummy roots, then we have to remove the
4011                   ;; dummy root as well.
4012                   (when (eq gnus-summary-make-false-root 'dummy)
4013                     ;; We go to the dummy root by going to
4014                     ;; the first sub-"thread", and then one line up.
4015                     (gnus-summary-goto-article
4016                      (mail-header-number (caadr thread)))
4017                     (forward-line -1)
4018                     (gnus-delete-line)
4019                     (gnus-data-compute-positions))
4020                   (setq thread (cdr thread))
4021                   (while thread
4022                     (gnus-remove-thread-1 (car thread))
4023                     (setq thread (cdr thread))))
4024               (gnus-remove-thread-1 thread))))))))
4025
4026 (defun gnus-remove-thread-1 (thread)
4027   "Remove the thread THREAD recursively."
4028   (let ((number (mail-header-number (pop thread)))
4029         d)
4030     (setq thread (reverse thread))
4031     (while thread
4032       (gnus-remove-thread-1 (pop thread)))
4033     (when (setq d (gnus-data-find number))
4034       (goto-char (gnus-data-pos d))
4035       (gnus-summary-show-thread)
4036       (gnus-data-remove
4037        number
4038        (- (gnus-point-at-bol)
4039           (prog1
4040               (1+ (gnus-point-at-eol))
4041             (gnus-delete-line)))))))
4042
4043 (defun gnus-sort-threads-1 (threads func)
4044   (sort (mapcar (lambda (thread)
4045                   (cons (car thread)
4046                         (and (cdr thread)
4047                              (gnus-sort-threads-1 (cdr thread) func))))
4048                 threads) func))
4049
4050 (defun gnus-sort-threads (threads)
4051   "Sort THREADS."
4052   (if (not gnus-thread-sort-functions)
4053       threads
4054     (gnus-message 8 "Sorting threads...")
4055     (prog1
4056         (gnus-sort-threads-1
4057          threads
4058          (gnus-make-sort-function gnus-thread-sort-functions))
4059       (gnus-message 8 "Sorting threads...done"))))
4060
4061 (defun gnus-sort-articles (articles)
4062   "Sort ARTICLES."
4063   (when gnus-article-sort-functions
4064     (gnus-message 7 "Sorting articles...")
4065     (prog1
4066         (setq gnus-newsgroup-headers
4067               (sort articles (gnus-make-sort-function
4068                               gnus-article-sort-functions)))
4069       (gnus-message 7 "Sorting articles...done"))))
4070
4071 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4072 (defmacro gnus-thread-header (thread)
4073   "Return header of first article in THREAD.
4074 Note that THREAD must never, ever be anything else than a variable -
4075 using some other form will lead to serious barfage."
4076   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4077   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4078   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4079         (vector thread) 2))
4080
4081 (defsubst gnus-article-sort-by-number (h1 h2)
4082   "Sort articles by article number."
4083   (< (mail-header-number h1)
4084      (mail-header-number h2)))
4085
4086 (defun gnus-thread-sort-by-number (h1 h2)
4087   "Sort threads by root article number."
4088   (gnus-article-sort-by-number
4089    (gnus-thread-header h1) (gnus-thread-header h2)))
4090
4091 (defsubst gnus-article-sort-by-lines (h1 h2)
4092   "Sort articles by article Lines header."
4093   (< (mail-header-lines h1)
4094      (mail-header-lines h2)))
4095
4096 (defun gnus-thread-sort-by-lines (h1 h2)
4097   "Sort threads by root article Lines header."
4098   (gnus-article-sort-by-lines
4099    (gnus-thread-header h1) (gnus-thread-header h2)))
4100
4101 (defsubst gnus-article-sort-by-chars (h1 h2)
4102   "Sort articles by octet length."
4103   (< (mail-header-chars h1)
4104      (mail-header-chars h2)))
4105
4106 (defun gnus-thread-sort-by-chars (h1 h2)
4107   "Sort threads by root article octet length."
4108   (gnus-article-sort-by-chars
4109    (gnus-thread-header h1) (gnus-thread-header h2)))
4110
4111 (defsubst gnus-article-sort-by-author (h1 h2)
4112   "Sort articles by root author."
4113   (string-lessp
4114    (let ((extract (funcall
4115                    gnus-extract-address-components
4116                    (mail-header-from h1))))
4117      (or (car extract) (cadr extract) ""))
4118    (let ((extract (funcall
4119                    gnus-extract-address-components
4120                    (mail-header-from h2))))
4121      (or (car extract) (cadr extract) ""))))
4122
4123 (defun gnus-thread-sort-by-author (h1 h2)
4124   "Sort threads by root author."
4125   (gnus-article-sort-by-author
4126    (gnus-thread-header h1)  (gnus-thread-header h2)))
4127
4128 (defsubst gnus-article-sort-by-subject (h1 h2)
4129   "Sort articles by root subject."
4130   (string-lessp
4131    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4132    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4133
4134 (defun gnus-thread-sort-by-subject (h1 h2)
4135   "Sort threads by root subject."
4136   (gnus-article-sort-by-subject
4137    (gnus-thread-header h1) (gnus-thread-header h2)))
4138
4139 (defsubst gnus-article-sort-by-date (h1 h2)
4140   "Sort articles by root article date."
4141   (time-less-p
4142    (gnus-date-get-time (mail-header-date h1))
4143    (gnus-date-get-time (mail-header-date h2))))
4144
4145 (defun gnus-thread-sort-by-date (h1 h2)
4146   "Sort threads by root article date."
4147   (gnus-article-sort-by-date
4148    (gnus-thread-header h1) (gnus-thread-header h2)))
4149
4150 (defsubst gnus-article-sort-by-score (h1 h2)
4151   "Sort articles by root article score.
4152 Unscored articles will be counted as having a score of zero."
4153   (> (or (cdr (assq (mail-header-number h1)
4154                     gnus-newsgroup-scored))
4155          gnus-summary-default-score 0)
4156      (or (cdr (assq (mail-header-number h2)
4157                     gnus-newsgroup-scored))
4158          gnus-summary-default-score 0)))
4159
4160 (defun gnus-thread-sort-by-score (h1 h2)
4161   "Sort threads by root article score."
4162   (gnus-article-sort-by-score
4163    (gnus-thread-header h1) (gnus-thread-header h2)))
4164
4165 (defun gnus-thread-sort-by-total-score (h1 h2)
4166   "Sort threads by the sum of all scores in the thread.
4167 Unscored articles will be counted as having a score of zero."
4168   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4169
4170 (defun gnus-thread-total-score (thread)
4171   ;; This function find the total score of THREAD.
4172   (cond
4173    ((null thread)
4174     0)
4175    ((consp thread)
4176     (if (stringp (car thread))
4177         (apply gnus-thread-score-function 0
4178                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4179       (gnus-thread-total-score-1 thread)))
4180    (t
4181     (gnus-thread-total-score-1 (list thread)))))
4182
4183 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4184   "Sort threads such that the thread with the most recently arrived article comes first."
4185   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4186
4187 (defun gnus-thread-highest-number (thread)
4188   "Return the highest article number in THREAD."
4189   (apply 'max (mapcar (lambda (header)
4190                         (mail-header-number header))
4191                       (message-flatten-list thread))))
4192
4193 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4194   "Sort threads such that the thread with the most recently dated article comes first."
4195   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4196
4197 (defun gnus-thread-latest-date (thread)
4198   "Return the highest article date in THREAD."
4199   (let ((previous-time 0))
4200     (apply 'max (mapcar
4201                  (lambda (header)
4202                    (setq previous-time
4203                          (time-to-seconds
4204                           (mail-header-parse-date
4205                            (condition-case ()
4206                                (mail-header-date header)
4207                              (error previous-time))))))
4208                  (sort
4209                   (message-flatten-list thread)
4210                   (lambda (h1 h2)
4211                     (< (mail-header-number h1)
4212                        (mail-header-number h2))))))))
4213
4214 (defun gnus-thread-total-score-1 (root)
4215   ;; This function find the total score of the thread below ROOT.
4216   (setq root (car root))
4217   (apply gnus-thread-score-function
4218          (or (append
4219               (mapcar 'gnus-thread-total-score
4220                       (cdr (gnus-id-to-thread (mail-header-id root))))
4221               (when (> (mail-header-number root) 0)
4222                 (list (or (cdr (assq (mail-header-number root)
4223                                      gnus-newsgroup-scored))
4224                           gnus-summary-default-score 0))))
4225              (list gnus-summary-default-score)
4226              '(0))))
4227
4228 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4229 (defvar gnus-tmp-prev-subject nil)
4230 (defvar gnus-tmp-false-parent nil)
4231 (defvar gnus-tmp-root-expunged nil)
4232 (defvar gnus-tmp-dummy-line nil)
4233
4234 (eval-when-compile (defvar gnus-tmp-header))
4235 (defun gnus-extra-header (type &optional header)
4236   "Return the extra header of TYPE."
4237   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4238       ""))
4239
4240 (defvar gnus-tmp-thread-tree-header-string "")
4241
4242 (defvar gnus-sum-thread-tree-root "> "
4243   "With %B spec, used for the root of a thread.
4244 If nil, use subject instead.")
4245 (defvar gnus-sum-thread-tree-single-indent ""
4246   "With %B spec, used for a thread with just one message.
4247 If nil, use subject instead.")
4248 (defvar gnus-sum-thread-tree-vertical "| "
4249   "With %B spec, used for drawing a vertical line.")
4250 (defvar gnus-sum-thread-tree-indent "  "
4251   "With %B spec, used for indenting.")
4252 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4253   "With %B spec, used for a leaf with brothers.")
4254 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4255   "With %B spec, used for a leaf without brothers.")
4256
4257 (defun gnus-summary-prepare-threads (threads)
4258   "Prepare summary buffer from THREADS and indentation LEVEL.
4259 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4260 or a straight list of headers."
4261   (gnus-message 7 "Generating summary...")
4262
4263   (setq gnus-newsgroup-threads threads)
4264   (beginning-of-line)
4265
4266   (let ((gnus-tmp-level 0)
4267         (default-score (or gnus-summary-default-score 0))
4268         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4269         thread number subject stack state gnus-tmp-gathered beg-match
4270         new-roots gnus-tmp-new-adopts thread-end
4271         gnus-tmp-header gnus-tmp-unread
4272         gnus-tmp-replied gnus-tmp-subject-or-nil
4273         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4274         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4275         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4276         tree-stack)
4277
4278     (setq gnus-tmp-prev-subject nil)
4279
4280     (if (vectorp (car threads))
4281         ;; If this is a straight (sic) list of headers, then a
4282         ;; threaded summary display isn't required, so we just create
4283         ;; an unthreaded one.
4284         (gnus-summary-prepare-unthreaded threads)
4285
4286       ;; Do the threaded display.
4287
4288       (while (or threads stack gnus-tmp-new-adopts new-roots)
4289
4290         (if (and (= gnus-tmp-level 0)
4291                  (or (not stack)
4292                      (= (caar stack) 0))
4293                  (not gnus-tmp-false-parent)
4294                  (or gnus-tmp-new-adopts new-roots))
4295             (if gnus-tmp-new-adopts
4296                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4297                       thread (list (car gnus-tmp-new-adopts))
4298                       gnus-tmp-header (caar thread)
4299                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4300               (when new-roots
4301                 (setq thread (list (car new-roots))
4302                       gnus-tmp-header (caar thread)
4303                       new-roots (cdr new-roots))))
4304
4305           (if threads
4306               ;; If there are some threads, we do them before the
4307               ;; threads on the stack.
4308               (setq thread threads
4309                     gnus-tmp-header (caar thread))
4310             ;; There were no current threads, so we pop something off
4311             ;; the stack.
4312             (setq state (car stack)
4313                   gnus-tmp-level (car state)
4314                   tree-stack (cadr state)
4315                   thread (caddr state)
4316                   stack (cdr stack)
4317                   gnus-tmp-header (caar thread))))
4318
4319         (setq gnus-tmp-false-parent nil)
4320         (setq gnus-tmp-root-expunged nil)
4321         (setq thread-end nil)
4322
4323         (if (stringp gnus-tmp-header)
4324             ;; The header is a dummy root.
4325             (cond
4326              ((eq gnus-summary-make-false-root 'adopt)
4327               ;; We let the first article adopt the rest.
4328               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4329                                                (cddar thread)))
4330               (setq gnus-tmp-gathered
4331                     (nconc (mapcar
4332                             (lambda (h) (mail-header-number (car h)))
4333                             (cddar thread))
4334                            gnus-tmp-gathered))
4335               (setq thread (cons (list (caar thread)
4336                                        (cadar thread))
4337                                  (cdr thread)))
4338               (setq gnus-tmp-level -1
4339                     gnus-tmp-false-parent t))
4340              ((eq gnus-summary-make-false-root 'empty)
4341               ;; We print adopted articles with empty subject fields.
4342               (setq gnus-tmp-gathered
4343                     (nconc (mapcar
4344                             (lambda (h) (mail-header-number (car h)))
4345                             (cddar thread))
4346                            gnus-tmp-gathered))
4347               (setq gnus-tmp-level -1))
4348              ((eq gnus-summary-make-false-root 'dummy)
4349               ;; We remember that we probably want to output a dummy
4350               ;; root.
4351               (setq gnus-tmp-dummy-line gnus-tmp-header)
4352               (setq gnus-tmp-prev-subject gnus-tmp-header))
4353              (t
4354               ;; We do not make a root for the gathered
4355               ;; sub-threads at all.
4356               (setq gnus-tmp-level -1)))
4357
4358           (setq number (mail-header-number gnus-tmp-header)
4359                 subject (mail-header-subject gnus-tmp-header))
4360
4361           (cond
4362            ;; If the thread has changed subject, we might want to make
4363            ;; this subthread into a root.
4364            ((and (null gnus-thread-ignore-subject)
4365                  (not (zerop gnus-tmp-level))
4366                  gnus-tmp-prev-subject
4367                  (not (inline
4368                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4369             (setq new-roots (nconc new-roots (list (car thread)))
4370                   thread-end t
4371                   gnus-tmp-header nil))
4372            ;; If the article lies outside the current limit,
4373            ;; then we do not display it.
4374            ((not (memq number gnus-newsgroup-limit))
4375             (setq gnus-tmp-gathered
4376                   (nconc (mapcar
4377                           (lambda (h) (mail-header-number (car h)))
4378                           (cdar thread))
4379                          gnus-tmp-gathered))
4380             (setq gnus-tmp-new-adopts (if (cdar thread)
4381                                           (append gnus-tmp-new-adopts
4382                                                   (cdar thread))
4383                                         gnus-tmp-new-adopts)
4384                   thread-end t
4385                   gnus-tmp-header nil)
4386             (when (zerop gnus-tmp-level)
4387               (setq gnus-tmp-root-expunged t)))
4388            ;; Perhaps this article is to be marked as read?
4389            ((and gnus-summary-mark-below
4390                  (< (or (cdr (assq number gnus-newsgroup-scored))
4391                         default-score)
4392                     gnus-summary-mark-below)
4393                  ;; Don't touch sparse articles.
4394                  (not (gnus-summary-article-sparse-p number))
4395                  (not (gnus-summary-article-ancient-p number)))
4396             (setq gnus-newsgroup-unreads
4397                   (delq number gnus-newsgroup-unreads))
4398             (if gnus-newsgroup-auto-expire
4399                 (push number gnus-newsgroup-expirable)
4400               (push (cons number gnus-low-score-mark)
4401                     gnus-newsgroup-reads))))
4402
4403           (when gnus-tmp-header
4404             ;; We may have an old dummy line to output before this
4405             ;; article.
4406             (when (and gnus-tmp-dummy-line
4407                        (gnus-subject-equal
4408                         gnus-tmp-dummy-line
4409                         (mail-header-subject gnus-tmp-header)))
4410               (gnus-summary-insert-dummy-line
4411                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4412               (setq gnus-tmp-dummy-line nil))
4413
4414             ;; Compute the mark.
4415             (setq gnus-tmp-unread (gnus-article-mark number))
4416
4417             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4418                                   gnus-tmp-header gnus-tmp-level)
4419                   gnus-newsgroup-data)
4420
4421             ;; Actually insert the line.
4422             (setq
4423              gnus-tmp-subject-or-nil
4424              (cond
4425               ((and gnus-thread-ignore-subject
4426                     gnus-tmp-prev-subject
4427                     (not (inline (gnus-subject-equal
4428                                   gnus-tmp-prev-subject subject))))
4429                subject)
4430               ((zerop gnus-tmp-level)
4431                (if (and (eq gnus-summary-make-false-root 'empty)
4432                         (memq number gnus-tmp-gathered)
4433                         gnus-tmp-prev-subject
4434                         (inline (gnus-subject-equal
4435                                  gnus-tmp-prev-subject subject)))
4436                    gnus-summary-same-subject
4437                  subject))
4438               (t gnus-summary-same-subject)))
4439             (if (and (eq gnus-summary-make-false-root 'adopt)
4440                      (= gnus-tmp-level 1)
4441                      (memq number gnus-tmp-gathered))
4442                 (setq gnus-tmp-opening-bracket ?\<
4443                       gnus-tmp-closing-bracket ?\>)
4444               (setq gnus-tmp-opening-bracket ?\[
4445                     gnus-tmp-closing-bracket ?\]))
4446             (setq
4447              gnus-tmp-indentation
4448              (aref gnus-thread-indent-array gnus-tmp-level)
4449              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4450              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4451                                 gnus-summary-default-score 0)
4452              gnus-tmp-score-char
4453              (if (or (null gnus-summary-default-score)
4454                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4455                          gnus-summary-zcore-fuzz))
4456                  ?                      ;Whitespace
4457                (if (< gnus-tmp-score gnus-summary-default-score)
4458                    gnus-score-below-mark gnus-score-over-mark))
4459              gnus-tmp-replied
4460              (cond ((memq number gnus-newsgroup-processable)
4461                     gnus-process-mark)
4462                    ((memq number gnus-newsgroup-cached)
4463                     gnus-cached-mark)
4464                    ((memq number gnus-newsgroup-replied)
4465                     gnus-replied-mark)
4466                    ((memq number gnus-newsgroup-forwarded)
4467                     gnus-forwarded-mark)
4468                    ((memq number gnus-newsgroup-saved)
4469                     gnus-saved-mark)
4470                    ((memq number gnus-newsgroup-recent)
4471                     gnus-recent-mark)
4472                    ((memq number gnus-newsgroup-unseen)
4473                     gnus-unseen-mark)
4474                    (t gnus-no-mark))
4475              gnus-tmp-from (mail-header-from gnus-tmp-header)
4476              gnus-tmp-name
4477              (cond
4478               ((string-match "<[^>]+> *$" gnus-tmp-from)
4479                (setq beg-match (match-beginning 0))
4480                (or (and (string-match "^\".+\"" gnus-tmp-from)
4481                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4482                    (substring gnus-tmp-from 0 beg-match)))
4483               ((string-match "(.+)" gnus-tmp-from)
4484                (substring gnus-tmp-from
4485                           (1+ (match-beginning 0)) (1- (match-end 0))))
4486               (t gnus-tmp-from))
4487              gnus-tmp-thread-tree-header-string
4488              (cond
4489               ((not gnus-show-threads) "")
4490               ((zerop gnus-tmp-level)
4491                (if (cdar thread)
4492                    (or gnus-sum-thread-tree-root subject)
4493                  (or gnus-sum-thread-tree-single-indent subject)))
4494               (t
4495                (concat (apply 'concat
4496                               (mapcar (lambda (item)
4497                                         (if (= item 1)
4498                                             gnus-sum-thread-tree-vertical
4499                                           gnus-sum-thread-tree-indent))
4500                                       (cdr (reverse tree-stack))))
4501                        (if (nth 1 thread)
4502                            gnus-sum-thread-tree-leaf-with-other
4503                          gnus-sum-thread-tree-single-leaf)))))
4504             (when (string= gnus-tmp-name "")
4505               (setq gnus-tmp-name gnus-tmp-from))
4506             (unless (numberp gnus-tmp-lines)
4507               (setq gnus-tmp-lines -1))
4508             (if (= gnus-tmp-lines -1)
4509                 (setq gnus-tmp-lines "?")
4510               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4511             (gnus-put-text-property
4512              (point)
4513              (progn (eval gnus-summary-line-format-spec) (point))
4514              'gnus-number number)
4515             (when gnus-visual-p
4516               (forward-line -1)
4517               (gnus-run-hooks 'gnus-summary-update-hook)
4518               (forward-line 1))
4519
4520             (setq gnus-tmp-prev-subject subject)))
4521
4522         (when (nth 1 thread)
4523           (push (list (max 0 gnus-tmp-level)
4524                       (copy-list tree-stack)
4525                       (nthcdr 1 thread))
4526                 stack))
4527         (push (if (nth 1 thread) 1 0) tree-stack)
4528         (incf gnus-tmp-level)
4529         (setq threads (if thread-end nil (cdar thread)))
4530         (unless threads
4531           (setq gnus-tmp-level 0)))))
4532   (gnus-message 7 "Generating summary...done"))
4533
4534 (defun gnus-summary-prepare-unthreaded (headers)
4535   "Generate an unthreaded summary buffer based on HEADERS."
4536   (let (header number mark)
4537
4538     (beginning-of-line)
4539
4540     (while headers
4541       ;; We may have to root out some bad articles...
4542       (when (memq (setq number (mail-header-number
4543                                 (setq header (pop headers))))
4544                   gnus-newsgroup-limit)
4545         ;; Mark article as read when it has a low score.
4546         (when (and gnus-summary-mark-below
4547                    (< (or (cdr (assq number gnus-newsgroup-scored))
4548                           gnus-summary-default-score 0)
4549                       gnus-summary-mark-below)
4550                    (not (gnus-summary-article-ancient-p number)))
4551           (setq gnus-newsgroup-unreads
4552                 (delq number gnus-newsgroup-unreads))
4553           (if gnus-newsgroup-auto-expire
4554               (push number gnus-newsgroup-expirable)
4555             (push (cons number gnus-low-score-mark)
4556                   gnus-newsgroup-reads)))
4557
4558         (setq mark (gnus-article-mark number))
4559         (push (gnus-data-make number mark (1+ (point)) header 0)
4560               gnus-newsgroup-data)
4561         (gnus-summary-insert-line
4562          header 0 number
4563          mark (memq number gnus-newsgroup-replied)
4564          (memq number gnus-newsgroup-expirable)
4565          (mail-header-subject header) nil
4566          (cdr (assq number gnus-newsgroup-scored))
4567          (memq number gnus-newsgroup-processable))))))
4568
4569 (defun gnus-summary-remove-list-identifiers ()
4570   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4571   (let ((regexp (if (consp gnus-list-identifiers)
4572                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4573                   gnus-list-identifiers))
4574         changed subject)
4575     (when regexp
4576       (dolist (header gnus-newsgroup-headers)
4577         (setq subject (mail-header-subject header)
4578               changed nil)
4579         (while (string-match
4580                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4581                 subject)
4582           (setq subject
4583                 (concat (substring subject 0 (match-beginning 2))
4584                         (substring subject (match-end 0)))
4585                 changed t))
4586         (when (and changed
4587                    (string-match
4588                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4589           (setq subject
4590                 (concat (substring subject 0 (match-beginning 1))
4591                         (substring subject (match-end 1)))))
4592         (when changed
4593           (mail-header-set-subject header subject))))))
4594
4595 (defun gnus-fetch-headers (articles)
4596   "Fetch headers of ARTICLES."
4597   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4598     (gnus-message 5 "Fetching headers for %s..." name)
4599     (prog1
4600         (if (eq 'nov
4601                 (setq gnus-headers-retrieved-by
4602                       (gnus-retrieve-headers
4603                        articles gnus-newsgroup-name
4604                        ;; We might want to fetch old headers, but
4605                        ;; not if there is only 1 article.
4606                        (and (or (and
4607                                  (not (eq gnus-fetch-old-headers 'some))
4608                                  (not (numberp gnus-fetch-old-headers)))
4609                                 (> (length articles) 1))
4610                             gnus-fetch-old-headers))))
4611             (gnus-get-newsgroup-headers-xover
4612              articles nil nil gnus-newsgroup-name t)
4613           (gnus-get-newsgroup-headers))
4614       (gnus-message 5 "Fetching headers for %s...done" name))))
4615
4616 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4617   "Select newsgroup GROUP.
4618 If READ-ALL is non-nil, all articles in the group are selected.
4619 If SELECT-ARTICLES, only select those articles from GROUP."
4620   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4621          ;;!!! Dirty hack; should be removed.
4622          (gnus-summary-ignore-duplicates
4623           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4624               t
4625             gnus-summary-ignore-duplicates))
4626          (info (nth 2 entry))
4627          articles fetched-articles cached)
4628
4629     (unless (gnus-check-server
4630              (set (make-local-variable 'gnus-current-select-method)
4631                   (gnus-find-method-for-group group)))
4632       (error "Couldn't open server"))
4633
4634     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4635         (gnus-activate-group group)     ; Or we can activate it...
4636         (progn                          ; Or we bug out.
4637           (when (equal major-mode 'gnus-summary-mode)
4638             (kill-buffer (current-buffer)))
4639           (error "Couldn't activate group %s: %s"
4640                  group (gnus-status-message group))))
4641
4642     (unless (gnus-request-group group t)
4643       (when (equal major-mode 'gnus-summary-mode)
4644         (kill-buffer (current-buffer)))
4645       (error "Couldn't request group %s: %s"
4646              group (gnus-status-message group)))
4647
4648     (setq gnus-newsgroup-name group
4649           gnus-newsgroup-unselected nil
4650           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4651
4652     (let ((display (gnus-group-find-parameter group 'display)))
4653       (setq gnus-newsgroup-display
4654             (cond
4655              ((not (zerop (or (car-safe read-all) 0)))
4656               ;; The user entered the group with C-u SPC/RET, let's show
4657               ;; all articles.
4658               'gnus-not-ignore)
4659              ((eq display 'all)
4660               'gnus-not-ignore)
4661              ((arrayp display)
4662               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4663              ((numberp display)
4664               ;; The following is probably the "correct" solution, but
4665               ;; it makes Gnus fetch all headers and then limit the
4666               ;; articles (which is slow), so instead we hack the
4667               ;; select-articles parameter instead. -- Simon Josefsson
4668               ;; <jas@kth.se>
4669               ;;
4670               ;; (gnus-byte-compile
4671               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4672               ;;                         display)))))
4673               (setq select-articles
4674                     (gnus-uncompress-range
4675                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4676                              (if (> tmp 0)
4677                                  tmp
4678                                1))
4679                            (cdr (gnus-active group)))))
4680               nil)
4681              (t
4682               nil))))
4683
4684     (gnus-summary-setup-default-charset)
4685
4686     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4687     (when (gnus-virtual-group-p group)
4688       (setq cached gnus-newsgroup-cached))
4689
4690     (setq gnus-newsgroup-unreads
4691           (gnus-set-difference
4692            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4693            gnus-newsgroup-dormant))
4694
4695     (setq gnus-newsgroup-processable nil)
4696
4697     (gnus-update-read-articles group gnus-newsgroup-unreads)
4698
4699     ;; Adjust and set lists of article marks.
4700     (when info
4701       (gnus-adjust-marked-articles info))
4702
4703     (if (setq articles select-articles)
4704         (setq gnus-newsgroup-unselected
4705               (gnus-sorted-intersection
4706                gnus-newsgroup-unreads
4707                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4708       (setq articles (gnus-articles-to-read group read-all)))
4709
4710     (cond
4711      ((null articles)
4712       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4713       'quit)
4714      ((eq articles 0) nil)
4715      (t
4716       ;; Init the dependencies hash table.
4717       (setq gnus-newsgroup-dependencies
4718             (gnus-make-hashtable (length articles)))
4719       (gnus-set-global-variables)
4720       ;; Retrieve the headers and read them in.
4721       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4722
4723       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4724       (when cached
4725         (setq gnus-newsgroup-cached cached))
4726
4727       ;; Suppress duplicates?
4728       (when gnus-suppress-duplicates
4729         (gnus-dup-suppress-articles))
4730
4731       ;; Set the initial limit.
4732       (setq gnus-newsgroup-limit (copy-sequence articles))
4733       ;; Remove canceled articles from the list of unread articles.
4734       (setq fetched-articles
4735             (mapcar (lambda (headers) (mail-header-number headers))
4736                     gnus-newsgroup-headers))
4737       (setq gnus-newsgroup-articles fetched-articles)
4738       (setq gnus-newsgroup-unreads
4739             (gnus-set-sorted-intersection
4740              gnus-newsgroup-unreads fetched-articles))
4741
4742       ;; The `seen' marks are treated specially.
4743       (if (not gnus-newsgroup-seen)
4744           (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4745         (dolist (article gnus-newsgroup-articles)
4746           (unless (gnus-member-of-range article gnus-newsgroup-seen)
4747             (push article gnus-newsgroup-unseen)))
4748         (setq gnus-newsgroup-unseen (nreverse gnus-newsgroup-unseen)))
4749
4750       ;; Removed marked articles that do not exist.
4751       (gnus-update-missing-marks
4752        (gnus-sorted-complement fetched-articles articles))
4753       ;; We might want to build some more threads first.
4754       (when (and gnus-fetch-old-headers
4755                  (eq gnus-headers-retrieved-by 'nov))
4756         (if (eq gnus-fetch-old-headers 'invisible)
4757             (gnus-build-all-threads)
4758           (gnus-build-old-threads)))
4759       ;; Let the Gnus agent mark articles as read.
4760       (when gnus-agent
4761         (gnus-agent-get-undownloaded-list))
4762       ;; Remove list identifiers from subject
4763       (when gnus-list-identifiers
4764         (gnus-summary-remove-list-identifiers))
4765       ;; Check whether auto-expire is to be done in this group.
4766       (setq gnus-newsgroup-auto-expire
4767             (gnus-group-auto-expirable-p group))
4768       ;; Set up the article buffer now, if necessary.
4769       (unless gnus-single-article-buffer
4770         (gnus-article-setup-buffer))
4771       ;; First and last article in this newsgroup.
4772       (when gnus-newsgroup-headers
4773         (setq gnus-newsgroup-begin
4774               (mail-header-number (car gnus-newsgroup-headers))
4775               gnus-newsgroup-end
4776               (mail-header-number
4777                (gnus-last-element gnus-newsgroup-headers))))
4778       ;; GROUP is successfully selected.
4779       (or gnus-newsgroup-headers t)))))
4780
4781 (defun gnus-summary-display-make-predicate (display)
4782   (require 'gnus-agent)
4783   (when (= (length display) 1)
4784     (setq display (car display)))
4785   (unless gnus-summary-display-cache
4786     (dolist (elem (append (list (cons 'read 'read)
4787                                 (cons 'unseen 'unseen))
4788                           gnus-article-mark-lists))
4789       (push (cons (cdr elem)
4790                   (gnus-byte-compile
4791                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4792             gnus-summary-display-cache)))
4793   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4794     (gnus-get-predicate display)))
4795
4796 ;; Uses the dynamically bound `number' variable.
4797 (defvar number)
4798 (defun gnus-article-marked-p (type &optional article)
4799   (let ((article (or article number)))
4800     (cond
4801      ((eq type 'tick)
4802       (memq article gnus-newsgroup-marked))
4803      ((eq type 'unsend)
4804       (memq article gnus-newsgroup-unsendable))
4805      ((eq type 'undownload)
4806       (memq article gnus-newsgroup-undownloaded))
4807      ((eq type 'download)
4808       (memq article gnus-newsgroup-downloadable))
4809      ((eq type 'unread)
4810       (memq article gnus-newsgroup-unreads))
4811      ((eq type 'read)
4812       (memq article gnus-newsgroup-reads))
4813      ((eq type 'dormant)
4814       (memq article gnus-newsgroup-dormant) )
4815      ((eq type 'expire)
4816       (memq article gnus-newsgroup-expirable))
4817      ((eq type 'reply)
4818       (memq article gnus-newsgroup-replied))
4819      ((eq type 'killed)
4820       (memq article gnus-newsgroup-killed))
4821      ((eq type 'bookmark)
4822       (assq article gnus-newsgroup-bookmarks))
4823      ((eq type 'score)
4824       (assq article gnus-newsgroup-scored))
4825      ((eq type 'save)
4826       (memq article gnus-newsgroup-saved))
4827      ((eq type 'cache)
4828       (memq article gnus-newsgroup-cached))
4829      ((eq type 'forward)
4830       (memq article gnus-newsgroup-forwarded))
4831      ((eq type 'seen)
4832       (not (memq article gnus-newsgroup-unseen)))
4833      ((eq type 'recent)
4834       (memq article gnus-newsgroup-recent))
4835      (t t))))
4836
4837 (defun gnus-articles-to-read (group &optional read-all)
4838   "Find out what articles the user wants to read."
4839   (let* ((articles
4840           ;; Select all articles if `read-all' is non-nil, or if there
4841           ;; are no unread articles.
4842           (if (or read-all
4843                   (and (zerop (length gnus-newsgroup-marked))
4844                        (zerop (length gnus-newsgroup-unreads)))
4845                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4846               ;; We want to select the headers for all the articles in
4847               ;; the group, so we select either all the active
4848               ;; articles in the group, or (if that's nil), the
4849               ;; articles in the cache.
4850               (or
4851                (gnus-uncompress-range (gnus-active group))
4852                (gnus-cache-articles-in-group group))
4853             ;; Select only the "normal" subset of articles.
4854             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4855                           (copy-sequence gnus-newsgroup-unreads))
4856                   '<)))
4857          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4858          (scored (length scored-list))
4859          (number (length articles))
4860          (marked (+ (length gnus-newsgroup-marked)
4861                     (length gnus-newsgroup-dormant)))
4862          (select
4863           (cond
4864            ((numberp read-all)
4865             read-all)
4866            (t
4867             (condition-case ()
4868                 (cond
4869                  ((and (or (<= scored marked) (= scored number))
4870                        (numberp gnus-large-newsgroup)
4871                        (> number gnus-large-newsgroup))
4872                   (let ((input
4873                          (read-string
4874                           (format
4875                            "How many articles from %s (default %d): "
4876                            (gnus-limit-string
4877                             (gnus-group-decoded-name gnus-newsgroup-name)
4878                             35)
4879                            number))))
4880                     (if (string-match "^[ \t]*$" input) number input)))
4881                  ((and (> scored marked) (< scored number)
4882                        (> (- scored number) 20))
4883                   (let ((input
4884                          (read-string
4885                           (format "%s %s (%d scored, %d total): "
4886                                   "How many articles from"
4887                                   (gnus-group-decoded-name group)
4888                                   scored number))))
4889                     (if (string-match "^[ \t]*$" input)
4890                         number input)))
4891                  (t number))
4892               (quit
4893                (message "Quit getting the articles to read")
4894                nil))))))
4895     (setq select (if (stringp select) (string-to-number select) select))
4896     (if (or (null select) (zerop select))
4897         select
4898       (if (and (not (zerop scored)) (<= (abs select) scored))
4899           (progn
4900             (setq articles (sort scored-list '<))
4901             (setq number (length articles)))
4902         (setq articles (copy-sequence articles)))
4903
4904       (when (< (abs select) number)
4905         (if (< select 0)
4906             ;; Select the N oldest articles.
4907             (setcdr (nthcdr (1- (abs select)) articles) nil)
4908           ;; Select the N most recent articles.
4909           (setq articles (nthcdr (- number select) articles))))
4910       (setq gnus-newsgroup-unselected
4911             (gnus-sorted-intersection
4912              gnus-newsgroup-unreads
4913              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4914       (when gnus-alter-articles-to-read-function
4915         (setq gnus-newsgroup-unreads
4916               (sort
4917                (funcall gnus-alter-articles-to-read-function
4918                         gnus-newsgroup-name gnus-newsgroup-unreads)
4919                '<)))
4920       articles)))
4921
4922 (defun gnus-killed-articles (killed articles)
4923   (let (out)
4924     (while articles
4925       (when (inline (gnus-member-of-range (car articles) killed))
4926         (push (car articles) out))
4927       (setq articles (cdr articles)))
4928     out))
4929
4930 (defun gnus-uncompress-marks (marks)
4931   "Uncompress the mark ranges in MARKS."
4932   (let ((uncompressed '(score bookmark))
4933         out)
4934     (while marks
4935       (if (memq (caar marks) uncompressed)
4936           (push (car marks) out)
4937         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4938       (setq marks (cdr marks)))
4939     out))
4940
4941 (defun gnus-article-mark-to-type (mark)
4942   "Return the type of MARK."
4943   (or (cadr (assq mark gnus-article-special-mark-lists))
4944       'list))
4945
4946 (defun gnus-article-unpropagatable-p (mark)
4947   "Return whether MARK should be propagated to backend."
4948   (memq mark gnus-article-unpropagated-mark-lists))
4949
4950 (defun gnus-adjust-marked-articles (info)
4951   "Set all article lists and remove all marks that are no longer valid."
4952   (let* ((marked-lists (gnus-info-marks info))
4953          (active (gnus-active (gnus-info-group info)))
4954          (min (car active))
4955          (max (cdr active))
4956          (types gnus-article-mark-lists)
4957          marks var articles article mark mark-type)
4958
4959     (dolist (marks marked-lists)
4960       (setq mark (car marks)
4961             mark-type (gnus-article-mark-to-type mark)
4962             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4963
4964       ;; We set the variable according to the type of the marks list,
4965       ;; and then adjust the marks to a subset of the active articles.
4966       (cond
4967        ;; Adjust "simple" lists.
4968        ((eq mark-type 'list)
4969         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4970         (when (memq mark '(tick dormant expire reply save))
4971           (while articles
4972             (when (or (< (setq article (pop articles)) min) (> article max))
4973               (set var (delq article (symbol-value var)))))))
4974        ;; Adjust assocs.
4975        ((eq mark-type 'tuple)
4976         (set var (setq articles (cdr marks)))
4977         (when (not (listp (cdr (symbol-value var))))
4978           (set var (list (symbol-value var))))
4979         (when (not (listp (cdr articles)))
4980           (setq articles (list articles)))
4981         (while articles
4982           (when (or (not (consp (setq article (pop articles))))
4983                     (< (car article) min)
4984                     (> (car article) max))
4985             (set var (delq article (symbol-value var))))))
4986        ;; Adjust ranges (sloppily).
4987        ((eq mark-type 'range)
4988         (cond
4989          ((eq mark 'seen)
4990           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
4991           ;; It should be (seen (NUM1 . NUM2)).
4992           (when (numberp (cddr marks))
4993             (setcdr marks (list (cdr marks))))
4994           (setq articles (cdr marks))
4995           (while (and articles
4996                       (or (and (consp (car articles))
4997                                (> min (cdar articles)))
4998                           (and (numberp (car articles))
4999                                (> min (car articles)))))
5000             (pop articles))
5001           (set var articles))))))))
5002
5003 (defun gnus-update-missing-marks (missing)
5004   "Go through the list of MISSING articles and remove them from the mark lists."
5005   (when missing
5006     (let (var m)
5007       ;; Go through all types.
5008       (dolist (elem gnus-article-mark-lists)
5009         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5010           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5011           (when (symbol-value var)
5012             ;; This list has articles.  So we delete all missing
5013             ;; articles from it.
5014             (setq m missing)
5015             (while m
5016               (set var (delq (pop m) (symbol-value var))))))))))
5017
5018 (defun gnus-update-marks ()
5019   "Enter the various lists of marked articles into the newsgroup info list."
5020   (let ((types gnus-article-mark-lists)
5021         (info (gnus-get-info gnus-newsgroup-name))
5022         type list newmarked symbol delta-marks)
5023     (when info
5024       ;; Add all marks lists to the list of marks lists.
5025       (while (setq type (pop types))
5026         (setq list (symbol-value
5027                     (setq symbol
5028                           (intern (format "gnus-newsgroup-%s" (car type))))))
5029
5030         (when list
5031           ;; Get rid of the entries of the articles that have the
5032           ;; default score.
5033           (when (and (eq (cdr type) 'score)
5034                      gnus-save-score
5035                      list)
5036             (let* ((arts list)
5037                    (prev (cons nil list))
5038                    (all prev))
5039               (while arts
5040                 (if (or (not (consp (car arts)))
5041                         (= (cdar arts) gnus-summary-default-score))
5042                     (setcdr prev (cdr arts))
5043                   (setq prev arts))
5044                 (setq arts (cdr arts)))
5045               (setq list (cdr all)))))
5046
5047         (when (eq (cdr type) 'seen)
5048           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5049
5050         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5051           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5052
5053         (when (and (gnus-check-backend-function
5054                     'request-set-mark gnus-newsgroup-name)
5055                    (not (gnus-article-unpropagatable-p (cdr type))))
5056           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5057                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5058                  (add (gnus-remove-from-range
5059                        (gnus-copy-sequence list) old)))
5060             (when add
5061               (push (list add 'add (list (cdr type))) delta-marks))
5062             (when del
5063               (push (list del 'del (list (cdr type))) delta-marks))))
5064
5065         (when list
5066           (push (cons (cdr type) list) newmarked)))
5067
5068       (when delta-marks
5069         (unless (gnus-check-group gnus-newsgroup-name)
5070           (error "Can't open server for %s" gnus-newsgroup-name))
5071         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5072
5073       ;; Enter these new marks into the info of the group.
5074       (if (nthcdr 3 info)
5075           (setcar (nthcdr 3 info) newmarked)
5076         ;; Add the marks lists to the end of the info.
5077         (when newmarked
5078           (setcdr (nthcdr 2 info) (list newmarked))))
5079
5080       ;; Cut off the end of the info if there's nothing else there.
5081       (let ((i 5))
5082         (while (and (> i 2)
5083                     (not (nth i info)))
5084           (when (nthcdr (decf i) info)
5085             (setcdr (nthcdr i info) nil)))))))
5086
5087 (defun gnus-set-mode-line (where)
5088   "Set the mode line of the article or summary buffers.
5089 If WHERE is `summary', the summary mode line format will be used."
5090   ;; Is this mode line one we keep updated?
5091   (when (and (memq where gnus-updated-mode-lines)
5092              (symbol-value
5093               (intern (format "gnus-%s-mode-line-format-spec" where))))
5094     (let (mode-string)
5095       (save-excursion
5096         ;; We evaluate this in the summary buffer since these
5097         ;; variables are buffer-local to that buffer.
5098         (set-buffer gnus-summary-buffer)
5099        ;; We bind all these variables that are used in the `eval' form
5100         ;; below.
5101         (let* ((mformat (symbol-value
5102                          (intern
5103                           (format "gnus-%s-mode-line-format-spec" where))))
5104                (gnus-tmp-group-name (gnus-group-decoded-name
5105                                      gnus-newsgroup-name))
5106                (gnus-tmp-article-number (or gnus-current-article 0))
5107                (gnus-tmp-unread gnus-newsgroup-unreads)
5108                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5109                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5110                (gnus-tmp-unread-and-unselected
5111                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5112                             (zerop gnus-tmp-unselected))
5113                        "")
5114                       ((zerop gnus-tmp-unselected)
5115                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5116                       (t (format "{%d(+%d) more}"
5117                                  gnus-tmp-unread-and-unticked
5118                                  gnus-tmp-unselected))))
5119                (gnus-tmp-subject
5120                 (if (and gnus-current-headers
5121                          (vectorp gnus-current-headers))
5122                     (gnus-mode-string-quote
5123                      (mail-header-subject gnus-current-headers))
5124                   ""))
5125                bufname-length max-len
5126                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5127           (setq mode-string (eval mformat))
5128           (setq bufname-length (if (string-match "%b" mode-string)
5129                                    (- (length
5130                                        (buffer-name
5131                                         (if (eq where 'summary)
5132                                             nil
5133                                           (get-buffer gnus-article-buffer))))
5134                                       2)
5135                                  0))
5136           (setq max-len (max 4 (if gnus-mode-non-string-length
5137                                    (- (window-width)
5138                                       gnus-mode-non-string-length
5139                                       bufname-length)
5140                                  (length mode-string))))
5141           ;; We might have to chop a bit of the string off...
5142           (when (> (length mode-string) max-len)
5143             (setq mode-string
5144                   (concat (truncate-string-to-width mode-string (- max-len 3))
5145                           "...")))
5146           ;; Pad the mode string a bit.
5147           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5148       ;; Update the mode line.
5149       (setq mode-line-buffer-identification
5150             (gnus-mode-line-buffer-identification (list mode-string)))
5151       (set-buffer-modified-p t))))
5152
5153 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5154   "Go through the HEADERS list and add all Xrefs to a hash table.
5155 The resulting hash table is returned, or nil if no Xrefs were found."
5156   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5157          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5158          (xref-hashtb (gnus-make-hashtable))
5159          start group entry number xrefs header)
5160     (while headers
5161       (setq header (pop headers))
5162       (when (and (setq xrefs (mail-header-xref header))
5163                  (not (memq (setq number (mail-header-number header))
5164                             unreads)))
5165         (setq start 0)
5166         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5167           (setq start (match-end 0))
5168           (setq group (if prefix
5169                           (concat prefix (substring xrefs (match-beginning 1)
5170                                                     (match-end 1)))
5171                         (substring xrefs (match-beginning 1) (match-end 1))))
5172           (setq number
5173                 (string-to-int (substring xrefs (match-beginning 2)
5174                                           (match-end 2))))
5175           (if (setq entry (gnus-gethash group xref-hashtb))
5176               (setcdr entry (cons number (cdr entry)))
5177             (gnus-sethash group (cons number nil) xref-hashtb)))))
5178     (and start xref-hashtb)))
5179
5180 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5181   "Look through all the headers and mark the Xrefs as read."
5182   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5183         name entry info xref-hashtb idlist method nth4)
5184     (save-excursion
5185       (set-buffer gnus-group-buffer)
5186       (when (setq xref-hashtb
5187                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5188         (mapatoms
5189          (lambda (group)
5190            (unless (string= from-newsgroup (setq name (symbol-name group)))
5191              (setq idlist (symbol-value group))
5192              ;; Dead groups are not updated.
5193              (and (prog1
5194                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5195                             info (nth 2 entry))
5196                     (when (stringp (setq nth4 (gnus-info-method info)))
5197                       (setq nth4 (gnus-server-to-method nth4))))
5198                   ;; Only do the xrefs if the group has the same
5199                   ;; select method as the group we have just read.
5200                   (or (gnus-methods-equal-p
5201                        nth4 (gnus-find-method-for-group from-newsgroup))
5202                       virtual
5203                       (equal nth4 (setq method (gnus-find-method-for-group
5204                                                 from-newsgroup)))
5205                       (and (equal (car nth4) (car method))
5206                            (equal (nth 1 nth4) (nth 1 method))))
5207                   gnus-use-cross-reference
5208                   (or (not (eq gnus-use-cross-reference t))
5209                       virtual
5210                       ;; Only do cross-references on subscribed
5211                       ;; groups, if that is what is wanted.
5212                       (<= (gnus-info-level info) gnus-level-subscribed))
5213                   (gnus-group-make-articles-read name idlist))))
5214          xref-hashtb)))))
5215
5216 (defun gnus-compute-read-articles (group articles)
5217   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5218          (info (nth 2 entry))
5219          (active (gnus-active group))
5220          ninfo)
5221     (when entry
5222       ;; First peel off all invalid article numbers.
5223       (when active
5224         (let ((ids articles)
5225               id first)
5226           (while (setq id (pop ids))
5227             (when (and first (> id (cdr active)))
5228               ;; We'll end up in this situation in one particular
5229               ;; obscure situation.  If you re-scan a group and get
5230               ;; a new article that is cross-posted to a different
5231               ;; group that has not been re-scanned, you might get
5232               ;; crossposted article that has a higher number than
5233               ;; Gnus believes possible.  So we re-activate this
5234               ;; group as well.  This might mean doing the
5235               ;; crossposting thingy will *increase* the number
5236               ;; of articles in some groups.  Tsk, tsk.
5237               (setq active (or (gnus-activate-group group) active)))
5238             (when (or (> id (cdr active))
5239                       (< id (car active)))
5240               (setq articles (delq id articles))))))
5241       ;; If the read list is nil, we init it.
5242       (if (and active
5243                (null (gnus-info-read info))
5244                (> (car active) 1))
5245           (setq ninfo (cons 1 (1- (car active))))
5246         (setq ninfo (gnus-info-read info)))
5247       ;; Then we add the read articles to the range.
5248       (gnus-add-to-range
5249        ninfo (setq articles (sort articles '<))))))
5250
5251 (defun gnus-group-make-articles-read (group articles)
5252   "Update the info of GROUP to say that ARTICLES are read."
5253   (let* ((num 0)
5254          (entry (gnus-gethash group gnus-newsrc-hashtb))
5255          (info (nth 2 entry))
5256          (active (gnus-active group))
5257          range)
5258     (when entry
5259       (setq range (gnus-compute-read-articles group articles))
5260       (save-excursion
5261         (set-buffer gnus-group-buffer)
5262         (gnus-undo-register
5263           `(progn
5264              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5265              (gnus-info-set-read ',info ',(gnus-info-read info))
5266              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5267              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5268              (gnus-group-update-group ,group t))))
5269       ;; Add the read articles to the range.
5270       (gnus-info-set-read info range)
5271       (gnus-request-set-mark group (list (list range 'add '(read))))
5272       ;; Then we have to re-compute how many unread
5273       ;; articles there are in this group.
5274       (when active
5275         (cond
5276          ((not range)
5277           (setq num (- (1+ (cdr active)) (car active))))
5278          ((not (listp (cdr range)))
5279           (setq num (- (cdr active) (- (1+ (cdr range))
5280                                        (car range)))))
5281          (t
5282           (while range
5283             (if (numberp (car range))
5284                 (setq num (1+ num))
5285               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5286             (setq range (cdr range)))
5287           (setq num (- (cdr active) num))))
5288         ;; Update the number of unread articles.
5289         (setcar entry num)
5290         ;; Update the group buffer.
5291         (gnus-group-update-group group t)))))
5292
5293 (defvar gnus-newsgroup-none-id 0)
5294
5295 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5296   (let ((cur nntp-server-buffer)
5297         (dependencies
5298          (or dependencies
5299              (save-excursion (set-buffer gnus-summary-buffer)
5300                              gnus-newsgroup-dependencies)))
5301         headers id end ref
5302         (mail-parse-charset gnus-newsgroup-charset)
5303         (mail-parse-ignored-charsets
5304          (save-excursion (condition-case nil
5305                              (set-buffer gnus-summary-buffer)
5306                            (error))
5307                          gnus-newsgroup-ignored-charsets)))
5308     (save-excursion
5309       (set-buffer nntp-server-buffer)
5310       ;; Translate all TAB characters into SPACE characters.
5311       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5312       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5313       (gnus-run-hooks 'gnus-parse-headers-hook)
5314       (let ((case-fold-search t)
5315             in-reply-to header p lines chars)
5316         (goto-char (point-min))
5317         ;; Search to the beginning of the next header.  Error messages
5318         ;; do not begin with 2 or 3.
5319         (while (re-search-forward "^[23][0-9]+ " nil t)
5320           (setq id nil
5321                 ref nil)
5322           ;; This implementation of this function, with nine
5323           ;; search-forwards instead of the one re-search-forward and
5324           ;; a case (which basically was the old function) is actually
5325           ;; about twice as fast, even though it looks messier.  You
5326           ;; can't have everything, I guess.  Speed and elegance
5327           ;; doesn't always go hand in hand.
5328           (setq
5329            header
5330            (vector
5331             ;; Number.
5332             (prog1
5333                 (read cur)
5334               (end-of-line)
5335               (setq p (point))
5336               (narrow-to-region (point)
5337                                 (or (and (search-forward "\n.\n" nil t)
5338                                          (- (point) 2))
5339                                     (point))))
5340             ;; Subject.
5341             (progn
5342               (goto-char p)
5343               (if (search-forward "\nsubject:" nil t)
5344                   (funcall gnus-decode-encoded-word-function
5345                            (nnheader-header-value))
5346                 "(none)"))
5347             ;; From.
5348             (progn
5349               (goto-char p)
5350               (if (search-forward "\nfrom:" nil t)
5351                   (funcall gnus-decode-encoded-word-function
5352                            (nnheader-header-value))
5353                 "(nobody)"))
5354             ;; Date.
5355             (progn
5356               (goto-char p)
5357               (if (search-forward "\ndate:" nil t)
5358                   (nnheader-header-value) ""))
5359             ;; Message-ID.
5360             (progn
5361               (goto-char p)
5362               (setq id (if (re-search-forward
5363                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5364                            ;; We do it this way to make sure the Message-ID
5365                            ;; is (somewhat) syntactically valid.
5366                            (buffer-substring (match-beginning 1)
5367                                              (match-end 1))
5368                          ;; If there was no message-id, we just fake one
5369                          ;; to make subsequent routines simpler.
5370                          (nnheader-generate-fake-message-id))))
5371             ;; References.
5372             (progn
5373               (goto-char p)
5374               (if (search-forward "\nreferences:" nil t)
5375                   (progn
5376                     (setq end (point))
5377                     (prog1
5378                         (nnheader-header-value)
5379                       (setq ref
5380                             (buffer-substring
5381                              (progn
5382                                (end-of-line)
5383                                (search-backward ">" end t)
5384                                (1+ (point)))
5385                              (progn
5386                                (search-backward "<" end t)
5387                                (point))))))
5388                 ;; Get the references from the in-reply-to header if there
5389                 ;; were no references and the in-reply-to header looks
5390                 ;; promising.
5391                 (if (and (search-forward "\nin-reply-to:" nil t)
5392                          (setq in-reply-to (nnheader-header-value))
5393                          (string-match "<[^>]+>" in-reply-to))
5394                     (let (ref2)
5395                       (setq ref (substring in-reply-to (match-beginning 0)
5396                                            (match-end 0)))
5397                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5398                         (setq ref2 (substring in-reply-to (match-beginning 0)
5399                                               (match-end 0)))
5400                         (when (> (length ref2) (length ref))
5401                           (setq ref ref2)))
5402                       ref)
5403                   (setq ref nil))))
5404             ;; Chars.
5405             (progn
5406               (goto-char p)
5407               (if (search-forward "\nchars: " nil t)
5408                   (if (numberp (setq chars (ignore-errors (read cur))))
5409                       chars -1)
5410                 -1))
5411             ;; Lines.
5412             (progn
5413               (goto-char p)
5414               (if (search-forward "\nlines: " nil t)
5415                   (if (numberp (setq lines (ignore-errors (read cur))))
5416                       lines -1)
5417                 -1))
5418             ;; Xref.
5419             (progn
5420               (goto-char p)
5421               (and (search-forward "\nxref:" nil t)
5422                    (nnheader-header-value)))
5423             ;; Extra.
5424             (when gnus-extra-headers
5425               (let ((extra gnus-extra-headers)
5426                     out)
5427                 (while extra
5428                   (goto-char p)
5429                   (when (search-forward
5430                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5431                     (push (cons (car extra) (nnheader-header-value))
5432                           out))
5433                   (pop extra))
5434                 out))))
5435           (when (equal id ref)
5436             (setq ref nil))
5437
5438           (when gnus-alter-header-function
5439             (funcall gnus-alter-header-function header)
5440             (setq id (mail-header-id header)
5441                   ref (gnus-parent-id (mail-header-references header))))
5442
5443           (when (setq header
5444                       (gnus-dependencies-add-header
5445                        header dependencies force-new))
5446             (push header headers))
5447           (goto-char (point-max))
5448           (widen))
5449         (nreverse headers)))))
5450
5451 ;; Goes through the xover lines and returns a list of vectors
5452 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5453                                                   force-new dependencies
5454                                                   group also-fetch-heads)
5455   "Parse the news overview data in the server buffer.
5456 Return a list of headers that match SEQUENCE (see
5457 `nntp-retrieve-headers')."
5458   ;; Get the Xref when the users reads the articles since most/some
5459   ;; NNTP servers do not include Xrefs when using XOVER.
5460   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5461   (let ((mail-parse-charset gnus-newsgroup-charset)
5462         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5463         (cur nntp-server-buffer)
5464         (dependencies (or dependencies gnus-newsgroup-dependencies))
5465         (allp (cond
5466                ((eq gnus-read-all-available-headers t)
5467                 t)
5468                ((stringp gnus-read-all-available-headers)
5469                 (string-match gnus-read-all-available-headers group))
5470                (t
5471                 nil)))
5472         number headers header)
5473     (save-excursion
5474       (set-buffer nntp-server-buffer)
5475       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5476       ;; Allow the user to mangle the headers before parsing them.
5477       (gnus-run-hooks 'gnus-parse-headers-hook)
5478       (goto-char (point-min))
5479       (while (not (eobp))
5480         (condition-case ()
5481             (while (and (or sequence allp)
5482                         (not (eobp)))
5483               (setq number (read cur))
5484               (when (not allp)
5485                 (while (and sequence
5486                             (< (car sequence) number))
5487                   (setq sequence (cdr sequence))))
5488               (when (and (or allp
5489                              (and sequence
5490                                   (eq number (car sequence))))
5491                          (progn
5492                            (setq sequence (cdr sequence))
5493                            (setq header (inline
5494                                           (gnus-nov-parse-line
5495                                            number dependencies force-new)))))
5496                 (push header headers))
5497               (forward-line 1))
5498           (error
5499            (gnus-error 4 "Strange nov line (%d)"
5500                        (count-lines (point-min) (point)))))
5501         (forward-line 1))
5502       ;; A common bug in inn is that if you have posted an article and
5503       ;; then retrieves the active file, it will answer correctly --
5504       ;; the new article is included.  However, a NOV entry for the
5505       ;; article may not have been generated yet, so this may fail.
5506       ;; We work around this problem by retrieving the last few
5507       ;; headers using HEAD.
5508       (if (or (not also-fetch-heads)
5509               (not sequence))
5510           ;; We (probably) got all the headers.
5511           (nreverse headers)
5512         (let ((gnus-nov-is-evil t))
5513           (nconc
5514            (nreverse headers)
5515            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5516              (gnus-get-newsgroup-headers))))))))
5517
5518 (defun gnus-article-get-xrefs ()
5519   "Fill in the Xref value in `gnus-current-headers', if necessary.
5520 This is meant to be called in `gnus-article-internal-prepare-hook'."
5521   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5522                                  gnus-current-headers)))
5523     (or (not gnus-use-cross-reference)
5524         (not headers)
5525         (and (mail-header-xref headers)
5526              (not (string= (mail-header-xref headers) "")))
5527         (let ((case-fold-search t)
5528               xref)
5529           (save-restriction
5530             (nnheader-narrow-to-headers)
5531             (goto-char (point-min))
5532             (when (or (and (not (eobp))
5533                            (eq (downcase (char-after)) ?x)
5534                            (looking-at "Xref:"))
5535                       (search-forward "\nXref:" nil t))
5536               (goto-char (1+ (match-end 0)))
5537               (setq xref (buffer-substring (point)
5538                                            (progn (end-of-line) (point))))
5539               (mail-header-set-xref headers xref)))))))
5540
5541 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5542   "Find article ID and insert the summary line for that article.
5543 OLD-HEADER can either be a header or a line number to insert
5544 the subject line on."
5545   (let* ((line (and (numberp old-header) old-header))
5546          (old-header (and (vectorp old-header) old-header))
5547          (header (cond ((and old-header use-old-header)
5548                         old-header)
5549                        ((and (numberp id)
5550                              (gnus-number-to-header id))
5551                         (gnus-number-to-header id))
5552                        (t
5553                         (gnus-read-header id))))
5554          (number (and (numberp id) id))
5555          d)
5556     (when header
5557       ;; Rebuild the thread that this article is part of and go to the
5558       ;; article we have fetched.
5559       (when (and (not gnus-show-threads)
5560                  old-header)
5561         (when (and number
5562                    (setq d (gnus-data-find (mail-header-number old-header))))
5563           (goto-char (gnus-data-pos d))
5564           (gnus-data-remove
5565            number
5566            (- (gnus-point-at-bol)
5567               (prog1
5568                   (1+ (gnus-point-at-eol))
5569                 (gnus-delete-line))))))
5570       (when old-header
5571         (mail-header-set-number header (mail-header-number old-header)))
5572       (setq gnus-newsgroup-sparse
5573             (delq (setq number (mail-header-number header))
5574                   gnus-newsgroup-sparse))
5575       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5576       (push number gnus-newsgroup-limit)
5577       (gnus-rebuild-thread (mail-header-id header) line)
5578       (gnus-summary-goto-subject number nil t))
5579     (when (and (numberp number)
5580                (> number 0))
5581       ;; We have to update the boundaries even if we can't fetch the
5582       ;; article if ID is a number -- so that the next `P' or `N'
5583       ;; command will fetch the previous (or next) article even
5584       ;; if the one we tried to fetch this time has been canceled.
5585       (when (> number gnus-newsgroup-end)
5586         (setq gnus-newsgroup-end number))
5587       (when (< number gnus-newsgroup-begin)
5588         (setq gnus-newsgroup-begin number))
5589       (setq gnus-newsgroup-unselected
5590             (delq number gnus-newsgroup-unselected)))
5591     ;; Report back a success?
5592     (and header (mail-header-number header))))
5593
5594 ;;; Process/prefix in the summary buffer
5595
5596 (defun gnus-summary-work-articles (n)
5597   "Return a list of articles to be worked upon.
5598 The prefix argument, the list of process marked articles, and the
5599 current article will be taken into consideration."
5600   (save-excursion
5601     (set-buffer gnus-summary-buffer)
5602     (cond
5603      (n
5604       ;; A numerical prefix has been given.
5605       (setq n (prefix-numeric-value n))
5606       (let ((backward (< n 0))
5607             (n (abs (prefix-numeric-value n)))
5608             articles article)
5609         (save-excursion
5610           (while
5611               (and (> n 0)
5612                    (push (setq article (gnus-summary-article-number))
5613                          articles)
5614                    (if backward
5615                        (gnus-summary-find-prev nil article)
5616                      (gnus-summary-find-next nil article)))
5617             (decf n)))
5618         (nreverse articles)))
5619      ((and (gnus-region-active-p) (mark))
5620       (message "region active")
5621       ;; Work on the region between point and mark.
5622       (let ((max (max (point) (mark)))
5623             articles article)
5624         (save-excursion
5625           (goto-char (min (min (point) (mark))))
5626           (while
5627               (and
5628                (push (setq article (gnus-summary-article-number)) articles)
5629                (gnus-summary-find-next nil article)
5630                (< (point) max)))
5631           (nreverse articles))))
5632      (gnus-newsgroup-processable
5633       ;; There are process-marked articles present.
5634       ;; Save current state.
5635       (gnus-summary-save-process-mark)
5636       ;; Return the list.
5637       (reverse gnus-newsgroup-processable))
5638      (t
5639       ;; Just return the current article.
5640       (list (gnus-summary-article-number))))))
5641
5642 (defmacro gnus-summary-iterate (arg &rest forms)
5643   "Iterate over the process/prefixed articles and do FORMS.
5644 ARG is the interactive prefix given to the command.  FORMS will be
5645 executed with point over the summary line of the articles."
5646   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5647     `(let ((,articles (gnus-summary-work-articles ,arg)))
5648        (while ,articles
5649          (gnus-summary-goto-subject (car ,articles))
5650          ,@forms
5651          (pop ,articles)))))
5652
5653 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5654 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5655
5656 (defun gnus-summary-save-process-mark ()
5657   "Push the current set of process marked articles on the stack."
5658   (interactive)
5659   (push (copy-sequence gnus-newsgroup-processable)
5660         gnus-newsgroup-process-stack))
5661
5662 (defun gnus-summary-kill-process-mark ()
5663   "Push the current set of process marked articles on the stack and unmark."
5664   (interactive)
5665   (gnus-summary-save-process-mark)
5666   (gnus-summary-unmark-all-processable))
5667
5668 (defun gnus-summary-yank-process-mark ()
5669   "Pop the last process mark state off the stack and restore it."
5670   (interactive)
5671   (unless gnus-newsgroup-process-stack
5672     (error "Empty mark stack"))
5673   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5674
5675 (defun gnus-summary-process-mark-set (set)
5676   "Make SET into the current process marked articles."
5677   (gnus-summary-unmark-all-processable)
5678   (while set
5679     (gnus-summary-set-process-mark (pop set))))
5680
5681 ;;; Searching and stuff
5682
5683 (defun gnus-summary-search-group (&optional backward use-level)
5684   "Search for next unread newsgroup.
5685 If optional argument BACKWARD is non-nil, search backward instead."
5686   (save-excursion
5687     (set-buffer gnus-group-buffer)
5688     (when (gnus-group-search-forward
5689            backward nil (if use-level (gnus-group-group-level) nil))
5690       (gnus-group-group-name))))
5691
5692 (defun gnus-summary-best-group (&optional exclude-group)
5693   "Find the name of the best unread group.
5694 If EXCLUDE-GROUP, do not go to this group."
5695   (save-excursion
5696     (set-buffer gnus-group-buffer)
5697     (save-excursion
5698       (gnus-group-best-unread-group exclude-group))))
5699
5700 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5701   (if backward (gnus-summary-find-prev)
5702     (let* ((dummy (gnus-summary-article-intangible-p))
5703            (article (or article (gnus-summary-article-number)))
5704            (arts (gnus-data-find-list article))
5705            result)
5706       (when (and (not dummy)
5707                  (or (not gnus-summary-check-current)
5708                      (not unread)
5709                      (not (gnus-data-unread-p (car arts)))))
5710         (setq arts (cdr arts)))
5711       (when (setq result
5712                   (if unread
5713                       (progn
5714                         (while arts
5715                           (when (or (and undownloaded
5716                                          (eq gnus-undownloaded-mark
5717                                              (gnus-data-mark (car arts))))
5718                                     (gnus-data-unread-p (car arts)))
5719                             (setq result (car arts)
5720                                   arts nil))
5721                           (setq arts (cdr arts)))
5722                         result)
5723                     (car arts)))
5724         (goto-char (gnus-data-pos result))
5725         (gnus-data-number result)))))
5726
5727 (defun gnus-summary-find-prev (&optional unread article)
5728   (let* ((eobp (eobp))
5729          (article (or article (gnus-summary-article-number)))
5730          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5731          result)
5732     (when (and (not eobp)
5733                (or (not gnus-summary-check-current)
5734                    (not unread)
5735                    (not (gnus-data-unread-p (car arts)))))
5736       (setq arts (cdr arts)))
5737     (when (setq result
5738                 (if unread
5739                     (progn
5740                       (while arts
5741                         (when (gnus-data-unread-p (car arts))
5742                           (setq result (car arts)
5743                                 arts nil))
5744                         (setq arts (cdr arts)))
5745                       result)
5746                   (car arts)))
5747       (goto-char (gnus-data-pos result))
5748       (gnus-data-number result))))
5749
5750 (defun gnus-summary-find-subject (subject &optional unread backward article)
5751   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5752          (article (or article (gnus-summary-article-number)))
5753          (articles (gnus-data-list backward))
5754          (arts (gnus-data-find-list article articles))
5755          result)
5756     (when (or (not gnus-summary-check-current)
5757               (not unread)
5758               (not (gnus-data-unread-p (car arts))))
5759       (setq arts (cdr arts)))
5760     (while arts
5761       (and (or (not unread)
5762                (gnus-data-unread-p (car arts)))
5763            (vectorp (gnus-data-header (car arts)))
5764            (gnus-subject-equal
5765             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5766            (setq result (car arts)
5767                  arts nil))
5768       (setq arts (cdr arts)))
5769     (and result
5770          (goto-char (gnus-data-pos result))
5771          (gnus-data-number result))))
5772
5773 (defun gnus-summary-search-forward (&optional unread subject backward)
5774   "Search forward for an article.
5775 If UNREAD, look for unread articles.  If SUBJECT, look for
5776 articles with that subject.  If BACKWARD, search backward instead."
5777   (cond (subject (gnus-summary-find-subject subject unread backward))
5778         (backward (gnus-summary-find-prev unread))
5779         (t (gnus-summary-find-next unread))))
5780
5781 (defun gnus-recenter (&optional n)
5782   "Center point in window and redisplay frame.
5783 Also do horizontal recentering."
5784   (interactive "P")
5785   (when (and gnus-auto-center-summary
5786              (not (eq gnus-auto-center-summary 'vertical)))
5787     (gnus-horizontal-recenter))
5788   (recenter n))
5789
5790 (defun gnus-summary-recenter ()
5791   "Center point in the summary window.
5792 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5793 displayed, no centering will be performed."
5794   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5795 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5796   (interactive)
5797   (let* ((top (cond ((< (window-height) 4) 0)
5798                     ((< (window-height) 7) 1)
5799                     (t (if (numberp gnus-auto-center-summary)
5800                            gnus-auto-center-summary
5801                          2))))
5802          (height (1- (window-height)))
5803          (bottom (save-excursion (goto-char (point-max))
5804                                  (forward-line (- height))
5805                                  (point)))
5806          (window (get-buffer-window (current-buffer))))
5807     ;; The user has to want it.
5808     (when gnus-auto-center-summary
5809       (when (get-buffer-window gnus-article-buffer)
5810         ;; Only do recentering when the article buffer is displayed,
5811       ;; Set the window start to either `bottom', which is the biggest
5812         ;; possible valid number, or the second line from the top,
5813         ;; whichever is the least.
5814         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5815           (if (> bottom top-pos)
5816               ;; Keep the second line from the top visible
5817               (set-window-start window top-pos t)
5818             ;; Try to keep the bottom line visible; if it's partially
5819             ;; obscured, either scroll one more line to make it fully
5820             ;; visible, or revert to using TOP-POS.
5821             (save-excursion
5822               (goto-char (point-max))
5823               (forward-line -1)
5824               (let ((last-line-start (point)))
5825                 (goto-char bottom)
5826                 (set-window-start window (point) t)
5827                 (when (not (pos-visible-in-window-p last-line-start window))
5828                   (forward-line 1)
5829                   (set-window-start window (min (point) top-pos) t)))))))
5830       ;; Do horizontal recentering while we're at it.
5831       (when (and (get-buffer-window (current-buffer) t)
5832                  (not (eq gnus-auto-center-summary 'vertical)))
5833         (let ((selected (selected-window)))
5834           (select-window (get-buffer-window (current-buffer) t))
5835           (gnus-summary-position-point)
5836           (gnus-horizontal-recenter)
5837           (select-window selected))))))
5838
5839 (defun gnus-summary-jump-to-group (newsgroup)
5840   "Move point to NEWSGROUP in group mode buffer."
5841   ;; Keep update point of group mode buffer if visible.
5842   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5843       (save-window-excursion
5844         ;; Take care of tree window mode.
5845         (when (get-buffer-window gnus-group-buffer)
5846           (pop-to-buffer gnus-group-buffer))
5847         (gnus-group-jump-to-group newsgroup))
5848     (save-excursion
5849       ;; Take care of tree window mode.
5850       (if (get-buffer-window gnus-group-buffer)
5851           (pop-to-buffer gnus-group-buffer)
5852         (set-buffer gnus-group-buffer))
5853       (gnus-group-jump-to-group newsgroup))))
5854
5855 ;; This function returns a list of article numbers based on the
5856 ;; difference between the ranges of read articles in this group and
5857 ;; the range of active articles.
5858 (defun gnus-list-of-unread-articles (group)
5859   (let* ((read (gnus-info-read (gnus-get-info group)))
5860          (active (or (gnus-active group) (gnus-activate-group group)))
5861          (last (cdr active))
5862          first nlast unread)
5863     ;; If none are read, then all are unread.
5864     (if (not read)
5865         (setq first (car active))
5866       ;; If the range of read articles is a single range, then the
5867       ;; first unread article is the article after the last read
5868       ;; article.  Sounds logical, doesn't it?
5869       (if (and (not (listp (cdr read)))
5870                (or (< (car read) (car active))
5871                    (progn (setq read (list read))
5872                           nil)))
5873           (setq first (max (car active) (1+ (cdr read))))
5874         ;; `read' is a list of ranges.
5875         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5876                                   (caar read)))
5877                   1)
5878           (setq first (car active)))
5879         (while read
5880           (when first
5881             (while (< first nlast)
5882               (push first unread)
5883               (setq first (1+ first))))
5884           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5885           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5886           (setq read (cdr read)))))
5887     ;; And add the last unread articles.
5888     (while (<= first last)
5889       (push first unread)
5890       (setq first (1+ first)))
5891     ;; Return the list of unread articles.
5892     (delq 0 (nreverse unread))))
5893
5894 (defun gnus-list-of-read-articles (group)
5895   "Return a list of unread, unticked and non-dormant articles."
5896   (let* ((info (gnus-get-info group))
5897          (marked (gnus-info-marks info))
5898          (active (gnus-active group)))
5899     (and info active
5900          (gnus-set-difference
5901           (gnus-sorted-complement
5902            (gnus-uncompress-range active)
5903            (gnus-list-of-unread-articles group))
5904           (append
5905            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5906            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5907
5908 ;; Various summary commands
5909
5910 (defun gnus-summary-select-article-buffer ()
5911   "Reconfigure windows to show article buffer."
5912   (interactive)
5913   (if (not (gnus-buffer-live-p gnus-article-buffer))
5914       (error "There is no article buffer for this summary buffer")
5915     (gnus-configure-windows 'article)
5916     (select-window (get-buffer-window gnus-article-buffer))))
5917
5918 (defun gnus-summary-universal-argument (arg)
5919   "Perform any operation on all articles that are process/prefixed."
5920   (interactive "P")
5921   (let ((articles (gnus-summary-work-articles arg))
5922         func article)
5923     (if (eq
5924          (setq
5925           func
5926           (key-binding
5927            (read-key-sequence
5928             (substitute-command-keys
5929              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5930          'undefined)
5931         (gnus-error 1 "Undefined key")
5932       (save-excursion
5933         (while articles
5934           (gnus-summary-goto-subject (setq article (pop articles)))
5935           (let (gnus-newsgroup-processable)
5936             (command-execute func))
5937           (gnus-summary-remove-process-mark article)))))
5938   (gnus-summary-position-point))
5939
5940 (defun gnus-summary-toggle-truncation (&optional arg)
5941   "Toggle truncation of summary lines.
5942 With arg, turn line truncation on iff arg is positive."
5943   (interactive "P")
5944   (setq truncate-lines
5945         (if (null arg) (not truncate-lines)
5946           (> (prefix-numeric-value arg) 0)))
5947   (redraw-display))
5948
5949 (defun gnus-summary-reselect-current-group (&optional all rescan)
5950   "Exit and then reselect the current newsgroup.
5951 The prefix argument ALL means to select all articles."
5952   (interactive "P")
5953   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5954     (error "Ephemeral groups can't be reselected"))
5955   (let ((current-subject (gnus-summary-article-number))
5956         (group gnus-newsgroup-name))
5957     (setq gnus-newsgroup-begin nil)
5958     (gnus-summary-exit)
5959     ;; We have to adjust the point of group mode buffer because
5960     ;; point was moved to the next unread newsgroup by exiting.
5961     (gnus-summary-jump-to-group group)
5962     (when rescan
5963       (save-excursion
5964         (gnus-group-get-new-news-this-group 1)))
5965     (gnus-group-read-group all t)
5966     (gnus-summary-goto-subject current-subject nil t)))
5967
5968 (defun gnus-summary-rescan-group (&optional all)
5969   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5970   (interactive "P")
5971   (gnus-summary-reselect-current-group all t))
5972
5973 (defun gnus-summary-update-info (&optional non-destructive)
5974   (save-excursion
5975     (let ((group gnus-newsgroup-name))
5976       (when group
5977         (when gnus-newsgroup-kill-headers
5978           (setq gnus-newsgroup-killed
5979                 (gnus-compress-sequence
5980                  (nconc
5981                   (gnus-set-sorted-intersection
5982                    (gnus-uncompress-range gnus-newsgroup-killed)
5983                    (setq gnus-newsgroup-unselected
5984                          (sort gnus-newsgroup-unselected '<)))
5985                   (setq gnus-newsgroup-unreads
5986                         (sort gnus-newsgroup-unreads '<)))
5987                  t)))
5988         (unless (listp (cdr gnus-newsgroup-killed))
5989           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5990         (let ((headers gnus-newsgroup-headers))
5991           ;; Set the new ranges of read articles.
5992           (save-excursion
5993             (set-buffer gnus-group-buffer)
5994             (gnus-undo-force-boundary))
5995           (gnus-update-read-articles
5996            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5997           ;; Set the current article marks.
5998           (let ((gnus-newsgroup-scored
5999                  (if (and (not gnus-save-score)
6000                           (not non-destructive))
6001                      nil
6002                    gnus-newsgroup-scored)))
6003             (save-excursion
6004               (gnus-update-marks)))
6005           ;; Do the cross-ref thing.
6006           (when gnus-use-cross-reference
6007             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6008           ;; Do not switch windows but change the buffer to work.
6009           (set-buffer gnus-group-buffer)
6010           (unless (gnus-ephemeral-group-p group)
6011             (gnus-group-update-group group)))))))
6012
6013 (defun gnus-summary-save-newsrc (&optional force)
6014   "Save the current number of read/marked articles in the dribble buffer.
6015 The dribble buffer will then be saved.
6016 If FORCE (the prefix), also save the .newsrc file(s)."
6017   (interactive "P")
6018   (gnus-summary-update-info t)
6019   (if force
6020       (gnus-save-newsrc-file)
6021     (gnus-dribble-save)))
6022
6023 (defun gnus-summary-exit (&optional temporary)
6024   "Exit reading current newsgroup, and then return to group selection mode.
6025 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6026   (interactive)
6027   (gnus-set-global-variables)
6028   (when (gnus-buffer-live-p gnus-article-buffer)
6029     (save-excursion
6030       (set-buffer gnus-article-buffer)
6031       (mm-destroy-parts gnus-article-mime-handles)
6032       ;; Set it to nil for safety reason.
6033       (setq gnus-article-mime-handle-alist nil)
6034       (setq gnus-article-mime-handles nil)))
6035   (gnus-kill-save-kill-buffer)
6036   (gnus-async-halt-prefetch)
6037   (let* ((group gnus-newsgroup-name)
6038          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6039          (mode major-mode)
6040          (group-point nil)
6041          (buf (current-buffer)))
6042     (unless quit-config
6043       ;; Do adaptive scoring, and possibly save score files.
6044       (when gnus-newsgroup-adaptive
6045         (gnus-score-adaptive))
6046       (when gnus-use-scoring
6047         (gnus-score-save)))
6048     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6049     ;; If we have several article buffers, we kill them at exit.
6050     (unless gnus-single-article-buffer
6051       (gnus-kill-buffer gnus-original-article-buffer)
6052       (setq gnus-article-current nil))
6053     (when gnus-use-cache
6054       (gnus-cache-possibly-remove-articles)
6055       (gnus-cache-save-buffers))
6056     (gnus-async-prefetch-remove-group group)
6057     (when gnus-suppress-duplicates
6058       (gnus-dup-enter-articles))
6059     (when gnus-use-trees
6060       (gnus-tree-close group))
6061     (when gnus-use-cache
6062       (gnus-cache-write-active))
6063     ;; Remove entries for this group.
6064     (nnmail-purge-split-history (gnus-group-real-name group))
6065     ;; Make all changes in this group permanent.
6066     (unless quit-config
6067       (gnus-run-hooks 'gnus-exit-group-hook)
6068       (gnus-summary-update-info))
6069     (gnus-close-group group)
6070     ;; Make sure where we were, and go to next newsgroup.
6071     (set-buffer gnus-group-buffer)
6072     (unless quit-config
6073       (gnus-group-jump-to-group group))
6074     (gnus-run-hooks 'gnus-summary-exit-hook)
6075     (unless (or quit-config
6076                 ;; If this group has disappeared from the summary
6077                 ;; buffer, don't skip forwards.
6078                 (not (string= group (gnus-group-group-name))))
6079       (gnus-group-next-unread-group 1))
6080     (setq group-point (point))
6081     (if temporary
6082         nil                             ;Nothing to do.
6083       ;; If we have several article buffers, we kill them at exit.
6084       (unless gnus-single-article-buffer
6085         (gnus-kill-buffer gnus-article-buffer)
6086         (gnus-kill-buffer gnus-original-article-buffer)
6087         (setq gnus-article-current nil))
6088       (set-buffer buf)
6089       (if (not gnus-kill-summary-on-exit)
6090           (progn
6091             (gnus-deaden-summary)
6092             (setq mode nil))
6093        ;; We set all buffer-local variables to nil.  It is unclear why
6094         ;; this is needed, but if we don't, buffer-local variables are
6095         ;; not garbage-collected, it seems.  This would the lead to en
6096         ;; ever-growing Emacs.
6097         (gnus-summary-clear-local-variables)
6098         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6099           (gnus-summary-clear-local-variables))
6100         (when (get-buffer gnus-article-buffer)
6101           (bury-buffer gnus-article-buffer))
6102         ;; We clear the global counterparts of the buffer-local
6103         ;; variables as well, just to be on the safe side.
6104         (set-buffer gnus-group-buffer)
6105         (gnus-summary-clear-local-variables)
6106         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6107           (gnus-summary-clear-local-variables)))
6108       (setq gnus-current-select-method gnus-select-method)
6109       (pop-to-buffer gnus-group-buffer)
6110       (if (not quit-config)
6111           (progn
6112             (goto-char group-point)
6113             (gnus-configure-windows 'group 'force))
6114         (gnus-handle-ephemeral-exit quit-config))
6115       ;; Return to group mode buffer.
6116       (when (eq mode 'gnus-summary-mode)
6117         (gnus-kill-buffer buf))
6118       ;; Clear the current group name.
6119       (unless quit-config
6120         (setq gnus-newsgroup-name nil)))))
6121
6122 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6123 (defun gnus-summary-exit-no-update (&optional no-questions)
6124   "Quit reading current newsgroup without updating read article info."
6125   (interactive)
6126   (let* ((group gnus-newsgroup-name)
6127          (quit-config (gnus-group-quit-config group)))
6128     (when (or no-questions
6129               gnus-expert-user
6130               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6131       (gnus-async-halt-prefetch)
6132       (mapcar 'funcall
6133               (delq 'gnus-summary-expire-articles
6134                     (copy-sequence gnus-summary-prepare-exit-hook)))
6135       (when (gnus-buffer-live-p gnus-article-buffer)
6136         (save-excursion
6137           (set-buffer gnus-article-buffer)
6138           (mm-destroy-parts gnus-article-mime-handles)
6139           ;; Set it to nil for safety reason.
6140           (setq gnus-article-mime-handle-alist nil)
6141           (setq gnus-article-mime-handles nil)))
6142       ;; If we have several article buffers, we kill them at exit.
6143       (unless gnus-single-article-buffer
6144         (gnus-kill-buffer gnus-article-buffer)
6145         (gnus-kill-buffer gnus-original-article-buffer)
6146         (setq gnus-article-current nil))
6147       (if (not gnus-kill-summary-on-exit)
6148           (gnus-deaden-summary)
6149         (gnus-close-group group)
6150         (gnus-summary-clear-local-variables)
6151         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6152           (gnus-summary-clear-local-variables))
6153         (set-buffer gnus-group-buffer)
6154         (gnus-summary-clear-local-variables)
6155         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6156           (gnus-summary-clear-local-variables))
6157         (when (get-buffer gnus-summary-buffer)
6158           (kill-buffer gnus-summary-buffer)))
6159       (unless gnus-single-article-buffer
6160         (setq gnus-article-current nil))
6161       (when gnus-use-trees
6162         (gnus-tree-close group))
6163       (gnus-async-prefetch-remove-group group)
6164       (when (get-buffer gnus-article-buffer)
6165         (bury-buffer gnus-article-buffer))
6166       ;; Return to the group buffer.
6167       (gnus-configure-windows 'group 'force)
6168       ;; Clear the current group name.
6169       (setq gnus-newsgroup-name nil)
6170       (when (equal (gnus-group-group-name) group)
6171         (gnus-group-next-unread-group 1))
6172       (when quit-config
6173         (gnus-handle-ephemeral-exit quit-config)))))
6174
6175 (defun gnus-handle-ephemeral-exit (quit-config)
6176   "Handle movement when leaving an ephemeral group.
6177 The state which existed when entering the ephemeral is reset."
6178   (if (not (buffer-name (car quit-config)))
6179       (gnus-configure-windows 'group 'force)
6180     (set-buffer (car quit-config))
6181     (cond ((eq major-mode 'gnus-summary-mode)
6182            (gnus-set-global-variables))
6183           ((eq major-mode 'gnus-article-mode)
6184            (save-excursion
6185              ;; The `gnus-summary-buffer' variable may point
6186              ;; to the old summary buffer when using a single
6187              ;; article buffer.
6188              (unless (gnus-buffer-live-p gnus-summary-buffer)
6189                (set-buffer gnus-group-buffer))
6190              (set-buffer gnus-summary-buffer)
6191              (gnus-set-global-variables))))
6192     (if (or (eq (cdr quit-config) 'article)
6193             (eq (cdr quit-config) 'pick))
6194         (progn
6195           ;; The current article may be from the ephemeral group
6196           ;; thus it is best that we reload this article
6197           (gnus-summary-show-article)
6198           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6199               (gnus-configure-windows 'pick 'force)
6200             (gnus-configure-windows (cdr quit-config) 'force)))
6201       (gnus-configure-windows (cdr quit-config) 'force))
6202     (when (eq major-mode 'gnus-summary-mode)
6203       (gnus-summary-next-subject 1 nil t)
6204       (gnus-summary-recenter)
6205       (gnus-summary-position-point))))
6206
6207 ;;; Dead summaries.
6208
6209 (defvar gnus-dead-summary-mode-map nil)
6210
6211 (unless gnus-dead-summary-mode-map
6212   (setq gnus-dead-summary-mode-map (make-keymap))
6213   (suppress-keymap gnus-dead-summary-mode-map)
6214   (substitute-key-definition
6215    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6216   (dolist (key '("\C-d" "\r" "\177" [delete]))
6217     (define-key gnus-dead-summary-mode-map
6218       key 'gnus-summary-wake-up-the-dead))
6219   (dolist (key '("q" "Q"))
6220     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6221
6222 (defvar gnus-dead-summary-mode nil
6223   "Minor mode for Gnus summary buffers.")
6224
6225 (defun gnus-dead-summary-mode (&optional arg)
6226   "Minor mode for Gnus summary buffers."
6227   (interactive "P")
6228   (when (eq major-mode 'gnus-summary-mode)
6229     (make-local-variable 'gnus-dead-summary-mode)
6230     (setq gnus-dead-summary-mode
6231           (if (null arg) (not gnus-dead-summary-mode)
6232             (> (prefix-numeric-value arg) 0)))
6233     (when gnus-dead-summary-mode
6234       (gnus-add-minor-mode
6235        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6236
6237 (defun gnus-deaden-summary ()
6238   "Make the current summary buffer into a dead summary buffer."
6239   ;; Kill any previous dead summary buffer.
6240   (when (and gnus-dead-summary
6241              (buffer-name gnus-dead-summary))
6242     (save-excursion
6243       (set-buffer gnus-dead-summary)
6244       (when gnus-dead-summary-mode
6245         (kill-buffer (current-buffer)))))
6246   ;; Make this the current dead summary.
6247   (setq gnus-dead-summary (current-buffer))
6248   (gnus-dead-summary-mode 1)
6249   (let ((name (buffer-name)))
6250     (when (string-match "Summary" name)
6251       (rename-buffer
6252        (concat (substring name 0 (match-beginning 0)) "Dead "
6253                (substring name (match-beginning 0)))
6254        t)
6255       (bury-buffer))))
6256
6257 (defun gnus-kill-or-deaden-summary (buffer)
6258   "Kill or deaden the summary BUFFER."
6259   (save-excursion
6260     (when (and (buffer-name buffer)
6261                (not gnus-single-article-buffer))
6262       (save-excursion
6263         (set-buffer buffer)
6264         (gnus-kill-buffer gnus-article-buffer)
6265         (gnus-kill-buffer gnus-original-article-buffer)))
6266     (cond
6267      ;; Kill the buffer.
6268      (gnus-kill-summary-on-exit
6269       (when (and gnus-use-trees
6270                  (gnus-buffer-exists-p buffer))
6271         (save-excursion
6272           (set-buffer buffer)
6273           (gnus-tree-close gnus-newsgroup-name)))
6274       (gnus-kill-buffer buffer))
6275      ;; Deaden the buffer.
6276      ((gnus-buffer-exists-p buffer)
6277       (save-excursion
6278         (set-buffer buffer)
6279         (gnus-deaden-summary))))))
6280
6281 (defun gnus-summary-wake-up-the-dead (&rest args)
6282   "Wake up the dead summary buffer."
6283   (interactive)
6284   (gnus-dead-summary-mode -1)
6285   (let ((name (buffer-name)))
6286     (when (string-match "Dead " name)
6287       (rename-buffer
6288        (concat (substring name 0 (match-beginning 0))
6289                (substring name (match-end 0)))
6290        t)))
6291   (gnus-message 3 "This dead summary is now alive again"))
6292
6293 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6294 (defun gnus-summary-fetch-faq (&optional faq-dir)
6295   "Fetch the FAQ for the current group.
6296 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6297 in."
6298   (interactive
6299    (list
6300     (when current-prefix-arg
6301       (completing-read
6302        "Faq dir: " (and (listp gnus-group-faq-directory)
6303                         (mapcar (lambda (file) (list file))
6304                                 gnus-group-faq-directory))))))
6305   (let (gnus-faq-buffer)
6306     (when (setq gnus-faq-buffer
6307                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6308       (gnus-configure-windows 'summary-faq))))
6309
6310 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6311 (defun gnus-summary-describe-group (&optional force)
6312   "Describe the current newsgroup."
6313   (interactive "P")
6314   (gnus-group-describe-group force gnus-newsgroup-name))
6315
6316 (defun gnus-summary-describe-briefly ()
6317   "Describe summary mode commands briefly."
6318   (interactive)
6319   (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")))
6320
6321 ;; Walking around group mode buffer from summary mode.
6322
6323 (defun gnus-summary-next-group (&optional no-article target-group backward)
6324   "Exit current newsgroup and then select next unread newsgroup.
6325 If prefix argument NO-ARTICLE is non-nil, no article is selected
6326 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6327 previous group instead."
6328   (interactive "P")
6329   ;; Stop pre-fetching.
6330   (gnus-async-halt-prefetch)
6331   (let ((current-group gnus-newsgroup-name)
6332         (current-buffer (current-buffer))
6333         entered)
6334    ;; First we semi-exit this group to update Xrefs and all variables.
6335     ;; We can't do a real exit, because the window conf must remain
6336     ;; the same in case the user is prompted for info, and we don't
6337     ;; want the window conf to change before that...
6338     (gnus-summary-exit t)
6339     (while (not entered)
6340       ;; Then we find what group we are supposed to enter.
6341       (set-buffer gnus-group-buffer)
6342       (gnus-group-jump-to-group current-group)
6343       (setq target-group
6344             (or target-group
6345                 (if (eq gnus-keep-same-level 'best)
6346                     (gnus-summary-best-group gnus-newsgroup-name)
6347                   (gnus-summary-search-group backward gnus-keep-same-level))))
6348       (if (not target-group)
6349           ;; There are no further groups, so we return to the group
6350           ;; buffer.
6351           (progn
6352             (gnus-message 5 "Returning to the group buffer")
6353             (setq entered t)
6354             (when (gnus-buffer-live-p current-buffer)
6355               (set-buffer current-buffer)
6356               (gnus-summary-exit))
6357             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6358         ;; We try to enter the target group.
6359         (gnus-group-jump-to-group target-group)
6360         (let ((unreads (gnus-group-group-unread)))
6361           (if (and (or (eq t unreads)
6362                        (and unreads (not (zerop unreads))))
6363                    (gnus-summary-read-group
6364                     target-group nil no-article
6365                     (and (buffer-name current-buffer) current-buffer)
6366                     nil backward))
6367               (setq entered t)
6368             (setq current-group target-group
6369                   target-group nil)))))))
6370
6371 (defun gnus-summary-prev-group (&optional no-article)
6372   "Exit current newsgroup and then select previous unread newsgroup.
6373 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6374   (interactive "P")
6375   (gnus-summary-next-group no-article nil t))
6376
6377 ;; Walking around summary lines.
6378
6379 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6380   "Go to the first unread subject.
6381 If UNREAD is non-nil, go to the first unread article.
6382 Returns the article selected or nil if there are no unread articles."
6383   (interactive "P")
6384   (prog1
6385       (cond
6386        ;; Empty summary.
6387        ((null gnus-newsgroup-data)
6388         (gnus-message 3 "No articles in the group")
6389         nil)
6390        ;; Pick the first article.
6391        ((not unread)
6392         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6393         (gnus-data-number (car gnus-newsgroup-data)))
6394        ;; No unread articles.
6395        ((null gnus-newsgroup-unreads)
6396         (gnus-message 3 "No more unread articles")
6397         nil)
6398        ;; Find the first unread article.
6399        (t
6400         (let ((data gnus-newsgroup-data))
6401           (while (and data
6402                       (and (not (and undownloaded
6403                                      (eq gnus-undownloaded-mark
6404                                          (gnus-data-mark (car data)))))
6405                            (if unseen
6406                                (or (not (memq
6407                                          (gnus-data-number (car data))
6408                                          gnus-newsgroup-unseen))
6409                                    (not (gnus-data-unread-p (car data))))
6410                              (not (gnus-data-unread-p (car data))))))
6411             (setq data (cdr data)))
6412           (when data
6413             (goto-char (gnus-data-pos (car data)))
6414             (gnus-data-number (car data))))))
6415     (gnus-summary-position-point)))
6416
6417 (defun gnus-summary-next-subject (n &optional unread dont-display)
6418   "Go to next N'th summary line.
6419 If N is negative, go to the previous N'th subject line.
6420 If UNREAD is non-nil, only unread articles are selected.
6421 The difference between N and the actual number of steps taken is
6422 returned."
6423   (interactive "p")
6424   (let ((backward (< n 0))
6425         (n (abs n)))
6426     (while (and (> n 0)
6427                 (if backward
6428                     (gnus-summary-find-prev unread)
6429                   (gnus-summary-find-next unread)))
6430       (unless (zerop (setq n (1- n)))
6431         (gnus-summary-show-thread)))
6432     (when (/= 0 n)
6433       (gnus-message 7 "No more%s articles"
6434                     (if unread " unread" "")))
6435     (unless dont-display
6436       (gnus-summary-recenter)
6437       (gnus-summary-position-point))
6438     n))
6439
6440 (defun gnus-summary-next-unread-subject (n)
6441   "Go to next N'th unread summary line."
6442   (interactive "p")
6443   (gnus-summary-next-subject n t))
6444
6445 (defun gnus-summary-prev-subject (n &optional unread)
6446   "Go to previous N'th summary line.
6447 If optional argument UNREAD is non-nil, only unread article is selected."
6448   (interactive "p")
6449   (gnus-summary-next-subject (- n) unread))
6450
6451 (defun gnus-summary-prev-unread-subject (n)
6452   "Go to previous N'th unread summary line."
6453   (interactive "p")
6454   (gnus-summary-next-subject (- n) t))
6455
6456 (defun gnus-summary-goto-subject (article &optional force silent)
6457   "Go the subject line of ARTICLE.
6458 If FORCE, also allow jumping to articles not currently shown."
6459   (interactive "nArticle number: ")
6460   (unless (numberp article)
6461     (error "Article %s is not a number" article))
6462   (let ((b (point))
6463         (data (gnus-data-find article)))
6464     ;; We read in the article if we have to.
6465     (and (not data)
6466          force
6467          (gnus-summary-insert-subject
6468           article
6469           (if (or (numberp force) (vectorp force)) force)
6470           t)
6471          (setq data (gnus-data-find article)))
6472     (goto-char b)
6473     (if (not data)
6474         (progn
6475           (unless silent
6476             (gnus-message 3 "Can't find article %d" article))
6477           nil)
6478       (let ((pt (gnus-data-pos data)))
6479         (goto-char pt)
6480         (gnus-summary-set-article-display-arrow pt))
6481       (gnus-summary-position-point)
6482       article)))
6483
6484 ;; Walking around summary lines with displaying articles.
6485
6486 (defun gnus-summary-expand-window (&optional arg)
6487   "Make the summary buffer take up the entire Emacs frame.
6488 Given a prefix, will force an `article' buffer configuration."
6489   (interactive "P")
6490   (if arg
6491       (gnus-configure-windows 'article 'force)
6492     (gnus-configure-windows 'summary 'force)))
6493
6494 (defun gnus-summary-display-article (article &optional all-header)
6495   "Display ARTICLE in article buffer."
6496   (when (gnus-buffer-live-p gnus-article-buffer)
6497     (with-current-buffer gnus-article-buffer
6498       (mm-enable-multibyte)))
6499   (gnus-set-global-variables)
6500   (when (gnus-buffer-live-p gnus-article-buffer)
6501     (with-current-buffer gnus-article-buffer
6502       (setq gnus-article-charset gnus-newsgroup-charset)
6503       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6504       (mm-enable-multibyte)))
6505   (if (null article)
6506       nil
6507     (prog1
6508         (if gnus-summary-display-article-function
6509             (funcall gnus-summary-display-article-function article all-header)
6510           (gnus-article-prepare article all-header))
6511       (gnus-run-hooks 'gnus-select-article-hook)
6512       (when (and gnus-current-article
6513                  (not (zerop gnus-current-article)))
6514         (gnus-summary-goto-subject gnus-current-article))
6515       (gnus-summary-recenter)
6516       (when (and gnus-use-trees gnus-show-threads)
6517         (gnus-possibly-generate-tree article)
6518         (gnus-highlight-selected-tree article))
6519       ;; Successfully display article.
6520       (gnus-article-set-window-start
6521        (cdr (assq article gnus-newsgroup-bookmarks))))))
6522
6523 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6524   "Select the current article.
6525 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6526 non-nil, the article will be re-fetched even if it already present in
6527 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6528 be displayed."
6529   ;; Make sure we are in the summary buffer to work around bbdb bug.
6530   (unless (eq major-mode 'gnus-summary-mode)
6531     (set-buffer gnus-summary-buffer))
6532   (let ((article (or article (gnus-summary-article-number)))
6533         (all-headers (not (not all-headers))) ;Must be t or nil.
6534         gnus-summary-display-article-function)
6535     (and (not pseudo)
6536          (gnus-summary-article-pseudo-p article)
6537          (error "This is a pseudo-article"))
6538     (save-excursion
6539       (set-buffer gnus-summary-buffer)
6540       (if (or (and gnus-single-article-buffer
6541                    (or (null gnus-current-article)
6542                        (null gnus-article-current)
6543                        (null (get-buffer gnus-article-buffer))
6544                        (not (eq article (cdr gnus-article-current)))
6545                        (not (equal (car gnus-article-current)
6546                                    gnus-newsgroup-name))))
6547               (and (not gnus-single-article-buffer)
6548                    (or (null gnus-current-article)
6549                        (not (eq gnus-current-article article))))
6550               force)
6551           ;; The requested article is different from the current article.
6552           (progn
6553             (gnus-summary-display-article article all-headers)
6554             (when (gnus-buffer-live-p gnus-article-buffer)
6555               (with-current-buffer gnus-article-buffer
6556                 (if (not gnus-article-decoded-p) ;; a local variable
6557                     (mm-disable-multibyte))))
6558 ;;; Hidden headers are not hidden text any more.
6559 ;;          (when (or all-headers gnus-show-all-headers)
6560 ;;            (gnus-article-show-all-headers))
6561             (gnus-article-set-window-start
6562              (cdr (assq article gnus-newsgroup-bookmarks)))
6563             article)
6564 ;;      (when (or all-headers gnus-show-all-headers)
6565 ;;        (gnus-article-show-all-headers))
6566         'old))))
6567
6568 (defun gnus-summary-force-verify-and-decrypt ()
6569   (interactive)
6570   (let ((mm-verify-option 'known)
6571         (mm-decrypt-option 'known))
6572     (gnus-summary-select-article nil 'force)))
6573
6574 (defun gnus-summary-set-current-mark (&optional current-mark)
6575   "Obsolete function."
6576   nil)
6577
6578 (defun gnus-summary-next-article (&optional unread subject backward push)
6579   "Select the next article.
6580 If UNREAD, only unread articles are selected.
6581 If SUBJECT, only articles with SUBJECT are selected.
6582 If BACKWARD, the previous article is selected instead of the next."
6583   (interactive "P")
6584   (cond
6585    ;; Is there such an article?
6586    ((and (gnus-summary-search-forward unread subject backward)
6587          (or (gnus-summary-display-article (gnus-summary-article-number))
6588              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6589     (gnus-summary-position-point))
6590    ;; If not, we try the first unread, if that is wanted.
6591    ((and subject
6592          gnus-auto-select-same
6593          (gnus-summary-first-unread-article))
6594     (gnus-summary-position-point)
6595     (gnus-message 6 "Wrapped"))
6596    ;; Try to get next/previous article not displayed in this group.
6597    ((and gnus-auto-extend-newsgroup
6598          (not unread) (not subject))
6599     (gnus-summary-goto-article
6600      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6601      nil (count-lines (point-min) (point))))
6602    ;; Go to next/previous group.
6603    (t
6604     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6605       (gnus-summary-jump-to-group gnus-newsgroup-name))
6606     (let ((cmd last-command-char)
6607           (point
6608            (save-excursion
6609              (set-buffer gnus-group-buffer)
6610              (point)))
6611           (group
6612            (if (eq gnus-keep-same-level 'best)
6613                (gnus-summary-best-group gnus-newsgroup-name)
6614              (gnus-summary-search-group backward gnus-keep-same-level))))
6615       ;; For some reason, the group window gets selected.  We change
6616       ;; it back.
6617       (select-window (get-buffer-window (current-buffer)))
6618       ;; Select next unread newsgroup automagically.
6619       (cond
6620        ((or (not gnus-auto-select-next)
6621             (not cmd))
6622         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6623        ((or (eq gnus-auto-select-next 'quietly)
6624             (and (eq gnus-auto-select-next 'slightly-quietly)
6625                  push)
6626             (and (eq gnus-auto-select-next 'almost-quietly)
6627                  (gnus-summary-last-article-p)))
6628         ;; Select quietly.
6629         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6630             (gnus-summary-exit)
6631           (gnus-message 7 "No more%s articles (%s)..."
6632                         (if unread " unread" "")
6633                         (if group (concat "selecting " group)
6634                           "exiting"))
6635           (gnus-summary-next-group nil group backward)))
6636        (t
6637         (when (gnus-key-press-event-p last-input-event)
6638           (gnus-summary-walk-group-buffer
6639            gnus-newsgroup-name cmd unread backward point))))))))
6640
6641 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6642   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6643                       (?\C-p (gnus-group-prev-unread-group 1))))
6644         (cursor-in-echo-area t)
6645         keve key group ended)
6646     (save-excursion
6647       (set-buffer gnus-group-buffer)
6648       (goto-char start)
6649       (setq group
6650             (if (eq gnus-keep-same-level 'best)
6651                 (gnus-summary-best-group gnus-newsgroup-name)
6652               (gnus-summary-search-group backward gnus-keep-same-level))))
6653     (while (not ended)
6654       (gnus-message
6655        5 "No more%s articles%s" (if unread " unread" "")
6656        (if (and group
6657                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6658            (format " (Type %s for %s [%s])"
6659                    (single-key-description cmd) group
6660                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6661          (format " (Type %s to exit %s)"
6662                  (single-key-description cmd)
6663                  gnus-newsgroup-name)))
6664       ;; Confirm auto selection.
6665       (setq key (car (setq keve (gnus-read-event-char))))
6666       (setq ended t)
6667       (cond
6668        ((assq key keystrokes)
6669         (let ((obuf (current-buffer)))
6670           (switch-to-buffer gnus-group-buffer)
6671           (when group
6672             (gnus-group-jump-to-group group))
6673           (eval (cadr (assq key keystrokes)))
6674           (setq group (gnus-group-group-name))
6675           (switch-to-buffer obuf))
6676         (setq ended nil))
6677        ((equal key cmd)
6678         (if (or (not group)
6679                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6680             (gnus-summary-exit)
6681           (gnus-summary-next-group nil group backward)))
6682        (t
6683         (push (cdr keve) unread-command-events))))))
6684
6685 (defun gnus-summary-next-unread-article ()
6686   "Select unread article after current one."
6687   (interactive)
6688   (gnus-summary-next-article
6689    (or (not (eq gnus-summary-goto-unread 'never))
6690        (gnus-summary-last-article-p (gnus-summary-article-number)))
6691    (and gnus-auto-select-same
6692         (gnus-summary-article-subject))))
6693
6694 (defun gnus-summary-prev-article (&optional unread subject)
6695   "Select the article after the current one.
6696 If UNREAD is non-nil, only unread articles are selected."
6697   (interactive "P")
6698   (gnus-summary-next-article unread subject t))
6699
6700 (defun gnus-summary-prev-unread-article ()
6701   "Select unread article before current one."
6702   (interactive)
6703   (gnus-summary-prev-article
6704    (or (not (eq gnus-summary-goto-unread 'never))
6705        (gnus-summary-first-article-p (gnus-summary-article-number)))
6706    (and gnus-auto-select-same
6707         (gnus-summary-article-subject))))
6708
6709 (defun gnus-summary-next-page (&optional lines circular)
6710   "Show next page of the selected article.
6711 If at the end of the current article, select the next article.
6712 LINES says how many lines should be scrolled up.
6713
6714 If CIRCULAR is non-nil, go to the start of the article instead of
6715 selecting the next article when reaching the end of the current
6716 article."
6717   (interactive "P")
6718   (setq gnus-summary-buffer (current-buffer))
6719   (gnus-set-global-variables)
6720   (let ((article (gnus-summary-article-number))
6721         (article-window (get-buffer-window gnus-article-buffer t))
6722         endp)
6723     ;; If the buffer is empty, we have no article.
6724     (unless article
6725       (error "No article to select"))
6726     (gnus-configure-windows 'article)
6727     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6728         (if (and (eq gnus-summary-goto-unread 'never)
6729                  (not (gnus-summary-last-article-p article)))
6730             (gnus-summary-next-article)
6731           (gnus-summary-next-unread-article))
6732       (if (or (null gnus-current-article)
6733               (null gnus-article-current)
6734               (/= article (cdr gnus-article-current))
6735               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6736           ;; Selected subject is different from current article's.
6737           (gnus-summary-display-article article)
6738         (when article-window
6739           (gnus-eval-in-buffer-window gnus-article-buffer
6740             (setq endp (gnus-article-next-page lines)))
6741           (when endp
6742             (cond (circular
6743                    (gnus-summary-beginning-of-article))
6744                   (lines
6745                    (gnus-message 3 "End of message"))
6746                   ((null lines)
6747                    (if (and (eq gnus-summary-goto-unread 'never)
6748                             (not (gnus-summary-last-article-p article)))
6749                        (gnus-summary-next-article)
6750                      (gnus-summary-next-unread-article))))))))
6751     (gnus-summary-recenter)
6752     (gnus-summary-position-point)))
6753
6754 (defun gnus-summary-prev-page (&optional lines move)
6755   "Show previous page of selected article.
6756 Argument LINES specifies lines to be scrolled down.
6757 If MOVE, move to the previous unread article if point is at
6758 the beginning of the buffer."
6759   (interactive "P")
6760   (let ((article (gnus-summary-article-number))
6761         (article-window (get-buffer-window gnus-article-buffer t))
6762         endp)
6763     (gnus-configure-windows 'article)
6764     (if (or (null gnus-current-article)
6765             (null gnus-article-current)
6766             (/= article (cdr gnus-article-current))
6767             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6768         ;; Selected subject is different from current article's.
6769         (gnus-summary-display-article article)
6770       (gnus-summary-recenter)
6771       (when article-window
6772         (gnus-eval-in-buffer-window gnus-article-buffer
6773           (setq endp (gnus-article-prev-page lines)))
6774         (when (and move endp)
6775           (cond (lines
6776                  (gnus-message 3 "Beginning of message"))
6777                 ((null lines)
6778                  (if (and (eq gnus-summary-goto-unread 'never)
6779                           (not (gnus-summary-first-article-p article)))
6780                      (gnus-summary-prev-article)
6781                    (gnus-summary-prev-unread-article))))))))
6782   (gnus-summary-position-point))
6783
6784 (defun gnus-summary-prev-page-or-article (&optional lines)
6785   "Show previous page of selected article.
6786 Argument LINES specifies lines to be scrolled down.
6787 If at the beginning of the article, go to the next article."
6788   (interactive "P")
6789   (gnus-summary-prev-page lines t))
6790
6791 (defun gnus-summary-scroll-up (lines)
6792   "Scroll up (or down) one line current article.
6793 Argument LINES specifies lines to be scrolled up (or down if negative)."
6794   (interactive "p")
6795   (gnus-configure-windows 'article)
6796   (gnus-summary-show-thread)
6797   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6798     (gnus-eval-in-buffer-window gnus-article-buffer
6799       (cond ((> lines 0)
6800              (when (gnus-article-next-page lines)
6801                (gnus-message 3 "End of message")))
6802             ((< lines 0)
6803              (gnus-article-prev-page (- lines))))))
6804   (gnus-summary-recenter)
6805   (gnus-summary-position-point))
6806
6807 (defun gnus-summary-scroll-down (lines)
6808   "Scroll down (or up) one line current article.
6809 Argument LINES specifies lines to be scrolled down (or up if negative)."
6810   (interactive "p")
6811   (gnus-summary-scroll-up (- lines)))
6812
6813 (defun gnus-summary-next-same-subject ()
6814   "Select next article which has the same subject as current one."
6815   (interactive)
6816   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6817
6818 (defun gnus-summary-prev-same-subject ()
6819   "Select previous article which has the same subject as current one."
6820   (interactive)
6821   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6822
6823 (defun gnus-summary-next-unread-same-subject ()
6824   "Select next unread article which has the same subject as current one."
6825   (interactive)
6826   (gnus-summary-next-article t (gnus-summary-article-subject)))
6827
6828 (defun gnus-summary-prev-unread-same-subject ()
6829   "Select previous unread article which has the same subject as current one."
6830   (interactive)
6831   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6832
6833 (defun gnus-summary-first-unread-article ()
6834   "Select the first unread article.
6835 Return nil if there are no unread articles."
6836   (interactive)
6837   (prog1
6838       (when (gnus-summary-first-subject t)
6839         (gnus-summary-show-thread)
6840         (gnus-summary-first-subject t)
6841         (gnus-summary-display-article (gnus-summary-article-number)))
6842     (gnus-summary-position-point)))
6843
6844 (defun gnus-summary-first-unread-subject ()
6845   "Place the point on the subject line of the first unread article.
6846 Return nil if there are no unread articles."
6847   (interactive)
6848   (prog1
6849       (when (gnus-summary-first-subject t)
6850         (gnus-summary-show-thread)
6851         (gnus-summary-first-subject t))
6852     (gnus-summary-position-point)))
6853
6854 (defun gnus-summary-first-unseen-subject ()
6855   "Place the point on the subject line of the first unseen article.
6856 Return nil if there are no unseen articles."
6857   (interactive)
6858   (prog1
6859       (when (gnus-summary-first-subject t t t)
6860         (gnus-summary-show-thread)
6861         (gnus-summary-first-subject t t t))
6862     (gnus-summary-position-point)))
6863
6864 (defun gnus-summary-first-unseen-or-unread-subject ()
6865   "Place the point on the subject line of the first unseen article.
6866 Return nil if there are no unseen articles."
6867   (interactive)
6868   (prog1
6869       (unless (when (gnus-summary-first-subject t t t)
6870                 (gnus-summary-show-thread)
6871                 (gnus-summary-first-subject t t t))
6872         (when (gnus-summary-first-subject t)
6873           (gnus-summary-show-thread)
6874           (gnus-summary-first-subject t)))
6875     (gnus-summary-position-point)))
6876
6877 (defun gnus-summary-first-article ()
6878   "Select the first article.
6879 Return nil if there are no articles."
6880   (interactive)
6881   (prog1
6882       (when (gnus-summary-first-subject)
6883         (gnus-summary-show-thread)
6884         (gnus-summary-first-subject)
6885         (gnus-summary-display-article (gnus-summary-article-number)))
6886     (gnus-summary-position-point)))
6887
6888 (defun gnus-summary-best-unread-article (&optional arg)
6889   "Select the unread article with the highest score.
6890 If given a prefix argument, select the next unread article that has a
6891 score higher than the default score."
6892   (interactive "P")
6893   (let ((article (if arg
6894                      (gnus-summary-better-unread-subject)
6895                    (gnus-summary-best-unread-subject))))
6896     (if article
6897         (gnus-summary-goto-article article)
6898       (error "No unread articles"))))
6899
6900 (defun gnus-summary-best-unread-subject ()
6901   "Select the unread subject with the highest score."
6902   (interactive)
6903   (let ((best -1000000)
6904         (data gnus-newsgroup-data)
6905         article score)
6906     (while data
6907       (and (gnus-data-unread-p (car data))
6908            (> (setq score
6909                     (gnus-summary-article-score (gnus-data-number (car data))))
6910               best)
6911            (setq best score
6912                  article (gnus-data-number (car data))))
6913       (setq data (cdr data)))
6914     (when article
6915       (gnus-summary-goto-subject article))
6916     (gnus-summary-position-point)
6917     article))
6918
6919 (defun gnus-summary-better-unread-subject ()
6920   "Select the first unread subject that has a score over the default score."
6921   (interactive)
6922   (let ((data gnus-newsgroup-data)
6923         article score)
6924     (while (and (setq article (gnus-data-number (car data)))
6925                 (or (gnus-data-read-p (car data))
6926                     (not (> (gnus-summary-article-score article)
6927                             gnus-summary-default-score))))
6928       (setq data (cdr data)))
6929     (when article
6930       (gnus-summary-goto-subject article))
6931     (gnus-summary-position-point)
6932     article))
6933
6934 (defun gnus-summary-last-subject ()
6935   "Go to the last displayed subject line in the group."
6936   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6937     (when article
6938       (gnus-summary-goto-subject article))))
6939
6940 (defun gnus-summary-goto-article (article &optional all-headers force)
6941   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6942 If ALL-HEADERS is non-nil, no header lines are hidden.
6943 If FORCE, go to the article even if it isn't displayed.  If FORCE
6944 is a number, it is the line the article is to be displayed on."
6945   (interactive
6946    (list
6947     (completing-read
6948      "Article number or Message-ID: "
6949      (mapcar (lambda (number) (list (int-to-string number)))
6950              gnus-newsgroup-limit))
6951     current-prefix-arg
6952     t))
6953   (prog1
6954       (if (and (stringp article)
6955                (string-match "@" article))
6956           (gnus-summary-refer-article article)
6957         (when (stringp article)
6958           (setq article (string-to-number article)))
6959         (if (gnus-summary-goto-subject article force)
6960             (gnus-summary-display-article article all-headers)
6961           (gnus-message 4 "Couldn't go to article %s" article) nil))
6962     (gnus-summary-position-point)))
6963
6964 (defun gnus-summary-goto-last-article ()
6965   "Go to the previously read article."
6966   (interactive)
6967   (prog1
6968       (when gnus-last-article
6969         (gnus-summary-goto-article gnus-last-article nil t))
6970     (gnus-summary-position-point)))
6971
6972 (defun gnus-summary-pop-article (number)
6973   "Pop one article off the history and go to the previous.
6974 NUMBER articles will be popped off."
6975   (interactive "p")
6976   (let (to)
6977     (setq gnus-newsgroup-history
6978           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6979     (if to
6980         (gnus-summary-goto-article (car to) nil t)
6981       (error "Article history empty")))
6982   (gnus-summary-position-point))
6983
6984 ;; Summary commands and functions for limiting the summary buffer.
6985
6986 (defun gnus-summary-limit-to-articles (n)
6987   "Limit the summary buffer to the next N articles.
6988 If not given a prefix, use the process marked articles instead."
6989   (interactive "P")
6990   (prog1
6991       (let ((articles (gnus-summary-work-articles n)))
6992         (setq gnus-newsgroup-processable nil)
6993         (gnus-summary-limit articles))
6994     (gnus-summary-position-point)))
6995
6996 (defun gnus-summary-pop-limit (&optional total)
6997   "Restore the previous limit.
6998 If given a prefix, remove all limits."
6999   (interactive "P")
7000   (when total
7001     (setq gnus-newsgroup-limits
7002           (list (mapcar (lambda (h) (mail-header-number h))
7003                         gnus-newsgroup-headers))))
7004   (unless gnus-newsgroup-limits
7005     (error "No limit to pop"))
7006   (prog1
7007       (gnus-summary-limit nil 'pop)
7008     (gnus-summary-position-point)))
7009
7010 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7011   "Limit the summary buffer to articles that have subjects that match a regexp.
7012 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7013   (interactive
7014    (list (read-string (if current-prefix-arg
7015                           "Exclude subject (regexp): "
7016                         "Limit to subject (regexp): "))
7017          nil current-prefix-arg))
7018   (unless header
7019     (setq header "subject"))
7020   (when (not (equal "" subject))
7021     (prog1
7022         (let ((articles (gnus-summary-find-matching
7023                          (or header "subject") subject 'all nil nil
7024                          not-matching)))
7025           (unless articles
7026             (error "Found no matches for \"%s\"" subject))
7027           (gnus-summary-limit articles))
7028       (gnus-summary-position-point))))
7029
7030 (defun gnus-summary-limit-to-author (from &optional not-matching)
7031   "Limit the summary buffer to articles that have authors that match a regexp.
7032 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7033   (interactive
7034    (list (read-string (if current-prefix-arg
7035                           "Exclude author (regexp): "
7036                         "Limit to author (regexp): "))
7037          current-prefix-arg))
7038   (gnus-summary-limit-to-subject from "from" not-matching))
7039
7040 (defun gnus-summary-limit-to-age (age &optional younger-p)
7041   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7042 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7043 articles that are younger than AGE days."
7044   (interactive
7045    (let ((younger current-prefix-arg)
7046          (days-got nil)
7047          days)
7048      (while (not days-got)
7049        (setq days (if younger
7050                       (read-string "Limit to articles within (in days): ")
7051                     (read-string "Limit to articles older than (in days): ")))
7052        (when (> (length days) 0)
7053          (setq days (read days)))
7054        (if (numberp days)
7055            (progn 
7056              (setq days-got t)
7057              (if (< days 0)
7058                  (progn 
7059                    (setq younger (not younger))
7060                    (setq days (* days -1)))))
7061          (message "Please enter a number.")
7062          (sleep-for 1)))
7063      (list days younger)))
7064   (prog1
7065       (let ((data gnus-newsgroup-data)
7066             (cutoff (days-to-time age))
7067             articles d date is-younger)
7068         (while (setq d (pop data))
7069           (when (and (vectorp (gnus-data-header d))
7070                      (setq date (mail-header-date (gnus-data-header d))))
7071             (setq is-younger (time-less-p
7072                               (time-since (condition-case ()
7073                                               (date-to-time date)
7074                                             (error '(0 0))))
7075                               cutoff))
7076             (when (if younger-p
7077                       is-younger
7078                     (not is-younger))
7079               (push (gnus-data-number d) articles))))
7080         (gnus-summary-limit (nreverse articles)))
7081     (gnus-summary-position-point)))
7082
7083 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7084   "Limit the summary buffer to articles that match an 'extra' header."
7085   (interactive
7086    (let ((header
7087           (intern
7088            (gnus-completing-read-with-default
7089             (symbol-name (car gnus-extra-headers))
7090             (if current-prefix-arg
7091                 "Exclude extra header:"
7092               "Limit extra header:")
7093             (mapcar (lambda (x)
7094                       (cons (symbol-name x) x))
7095                     gnus-extra-headers)
7096             nil
7097             t))))
7098      (list header
7099            (read-string (format "%s header %s (regexp): "
7100                                 (if current-prefix-arg "Exclude" "Limit to")
7101                                 header))
7102            current-prefix-arg)))
7103   (when (not (equal "" regexp))
7104     (prog1
7105         (let ((articles (gnus-summary-find-matching
7106                          (cons 'extra header) regexp 'all nil nil
7107                          not-matching)))
7108           (unless articles
7109             (error "Found no matches for \"%s\"" regexp))
7110           (gnus-summary-limit articles))
7111       (gnus-summary-position-point))))
7112
7113 (defun gnus-summary-limit-to-display-predicate ()
7114   "Limit the summary buffer to the predicated in the `display' group parameter."
7115   (interactive)
7116   (unless gnus-newsgroup-display
7117     (error "There is no `display' group parameter"))
7118   (let (articles)
7119     (dolist (number gnus-newsgroup-articles)
7120       (when (funcall gnus-newsgroup-display)
7121         (push number articles)))
7122     (gnus-summary-limit articles))
7123   (gnus-summary-position-point))
7124
7125 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7126 (make-obsolete
7127  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7128
7129 (defun gnus-summary-limit-to-unread (&optional all)
7130   "Limit the summary buffer to articles that are not marked as read.
7131 If ALL is non-nil, limit strictly to unread articles."
7132   (interactive "P")
7133   (if all
7134       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7135     (gnus-summary-limit-to-marks
7136      ;; Concat all the marks that say that an article is read and have
7137      ;; those removed.
7138      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7139            gnus-killed-mark gnus-kill-file-mark
7140            gnus-low-score-mark gnus-expirable-mark
7141            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7142            gnus-duplicate-mark gnus-souped-mark)
7143      'reverse)))
7144
7145 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7146 (make-obsolete 'gnus-summary-delete-marked-with
7147                'gnus-summary-limit-exlude-marks)
7148
7149 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7150   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7151 If REVERSE, limit the summary buffer to articles that are marked
7152 with MARKS.  MARKS can either be a string of marks or a list of marks.
7153 Returns how many articles were removed."
7154   (interactive "sMarks: ")
7155   (gnus-summary-limit-to-marks marks t))
7156
7157 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7158   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7159 If REVERSE (the prefix), limit the summary buffer to articles that are
7160 not marked with MARKS.  MARKS can either be a string of marks or a
7161 list of marks.
7162 Returns how many articles were removed."
7163   (interactive "sMarks: \nP")
7164   (prog1
7165       (let ((data gnus-newsgroup-data)
7166             (marks (if (listp marks) marks
7167                      (append marks nil))) ; Transform to list.
7168             articles)
7169         (while data
7170           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7171                   (memq (gnus-data-mark (car data)) marks))
7172             (push (gnus-data-number (car data)) articles))
7173           (setq data (cdr data)))
7174         (gnus-summary-limit articles))
7175     (gnus-summary-position-point)))
7176
7177 (defun gnus-summary-limit-to-score (score)
7178   "Limit to articles with score at or above SCORE."
7179   (interactive "NLimit to articles with score of at least: ")
7180   (let ((data gnus-newsgroup-data)
7181         articles)
7182     (while data
7183       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7184                 score)
7185         (push (gnus-data-number (car data)) articles))
7186       (setq data (cdr data)))
7187     (prog1
7188         (gnus-summary-limit articles)
7189       (gnus-summary-position-point))))
7190
7191 (defun gnus-summary-limit-include-thread (id)
7192   "Display all the hidden articles that is in the thread with ID in it.
7193 When called interactively, ID is the Message-ID of the current
7194 article."
7195   (interactive (list (mail-header-id (gnus-summary-article-header))))
7196   (let ((articles (gnus-articles-in-thread
7197                    (gnus-id-to-thread (gnus-root-id id)))))
7198     (prog1
7199         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7200       (gnus-summary-limit-include-matching-articles
7201        "subject"
7202        (regexp-quote (gnus-simplify-subject-re
7203                       (mail-header-subject (gnus-id-to-header id)))))
7204       (gnus-summary-position-point))))
7205
7206 (defun gnus-summary-limit-include-matching-articles (header regexp)
7207   "Display all the hidden articles that have HEADERs that match REGEXP."
7208   (interactive (list (read-string "Match on header: ")
7209                      (read-string "Regexp: ")))
7210   (let ((articles (gnus-find-matching-articles header regexp)))
7211     (prog1
7212         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7213       (gnus-summary-position-point))))
7214
7215 (defun gnus-summary-limit-include-dormant ()
7216   "Display all the hidden articles that are marked as dormant.
7217 Note that this command only works on a subset of the articles currently
7218 fetched for this group."
7219   (interactive)
7220   (unless gnus-newsgroup-dormant
7221     (error "There are no dormant articles in this group"))
7222   (prog1
7223       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7224     (gnus-summary-position-point)))
7225
7226 (defun gnus-summary-limit-exclude-dormant ()
7227   "Hide all dormant articles."
7228   (interactive)
7229   (prog1
7230       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7231     (gnus-summary-position-point)))
7232
7233 (defun gnus-summary-limit-exclude-childless-dormant ()
7234   "Hide all dormant articles that have no children."
7235   (interactive)
7236   (let ((data (gnus-data-list t))
7237         articles d children)
7238     ;; Find all articles that are either not dormant or have
7239     ;; children.
7240     (while (setq d (pop data))
7241       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7242                 (and (setq children
7243                            (gnus-article-children (gnus-data-number d)))
7244                      (let (found)
7245                        (while children
7246                          (when (memq (car children) articles)
7247                            (setq children nil
7248                                  found t))
7249                          (pop children))
7250                        found)))
7251         (push (gnus-data-number d) articles)))
7252     ;; Do the limiting.
7253     (prog1
7254         (gnus-summary-limit articles)
7255       (gnus-summary-position-point))))
7256
7257 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7258   "Mark all unread excluded articles as read.
7259 If ALL, mark even excluded ticked and dormants as read."
7260   (interactive "P")
7261   (let ((articles (gnus-sorted-complement
7262                    (sort
7263                     (mapcar (lambda (h) (mail-header-number h))
7264                             gnus-newsgroup-headers)
7265                     '<)
7266                    (sort gnus-newsgroup-limit '<)))
7267         article)
7268     (setq gnus-newsgroup-unreads
7269           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7270     (if all
7271         (setq gnus-newsgroup-dormant nil
7272               gnus-newsgroup-marked nil
7273               gnus-newsgroup-reads
7274               (nconc
7275                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7276                gnus-newsgroup-reads))
7277       (while (setq article (pop articles))
7278         (unless (or (memq article gnus-newsgroup-dormant)
7279                     (memq article gnus-newsgroup-marked))
7280           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7281
7282 (defun gnus-summary-limit (articles &optional pop)
7283   (if pop
7284       ;; We pop the previous limit off the stack and use that.
7285       (setq articles (car gnus-newsgroup-limits)
7286             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7287     ;; We use the new limit, so we push the old limit on the stack.
7288     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7289   ;; Set the limit.
7290   (setq gnus-newsgroup-limit articles)
7291   (let ((total (length gnus-newsgroup-data))
7292         (data (gnus-data-find-list (gnus-summary-article-number)))
7293         (gnus-summary-mark-below nil)   ; Inhibit this.
7294         found)
7295     ;; This will do all the work of generating the new summary buffer
7296     ;; according to the new limit.
7297     (gnus-summary-prepare)
7298     ;; Hide any threads, possibly.
7299     (gnus-summary-maybe-hide-threads)
7300     ;; Try to return to the article you were at, or one in the
7301     ;; neighborhood.
7302     (when data
7303       ;; We try to find some article after the current one.
7304       (while data
7305         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7306           (setq data nil
7307                 found t))
7308         (setq data (cdr data))))
7309     (unless found
7310       ;; If there is no data, that means that we were after the last
7311       ;; article.  The same goes when we can't find any articles
7312       ;; after the current one.
7313       (goto-char (point-max))
7314       (gnus-summary-find-prev))
7315     (gnus-set-mode-line 'summary)
7316     ;; We return how many articles were removed from the summary
7317     ;; buffer as a result of the new limit.
7318     (- total (length gnus-newsgroup-data))))
7319
7320 (defsubst gnus-invisible-cut-children (threads)
7321   (let ((num 0))
7322     (while threads
7323       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7324         (incf num))
7325       (pop threads))
7326     (< num 2)))
7327
7328 (defsubst gnus-cut-thread (thread)
7329   "Go forwards in the thread until we find an article that we want to display."
7330   (when (or (eq gnus-fetch-old-headers 'some)
7331             (eq gnus-fetch-old-headers 'invisible)
7332             (numberp gnus-fetch-old-headers)
7333             (eq gnus-build-sparse-threads 'some)
7334             (eq gnus-build-sparse-threads 'more))
7335     ;; Deal with old-fetched headers and sparse threads.
7336     (while (and
7337             thread
7338             (or
7339              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7340              (gnus-summary-article-ancient-p
7341               (mail-header-number (car thread))))
7342             (if (or (<= (length (cdr thread)) 1)
7343                     (eq gnus-fetch-old-headers 'invisible))
7344                 (setq gnus-newsgroup-limit
7345                       (delq (mail-header-number (car thread))
7346                             gnus-newsgroup-limit)
7347                       thread (cadr thread))
7348               (when (gnus-invisible-cut-children (cdr thread))
7349                 (let ((th (cdr thread)))
7350                   (while th
7351                     (if (memq (mail-header-number (caar th))
7352                               gnus-newsgroup-limit)
7353                         (setq thread (car th)
7354                               th nil)
7355                       (setq th (cdr th))))))))))
7356   thread)
7357
7358 (defun gnus-cut-threads (threads)
7359   "Cut off all uninteresting articles from the beginning of threads."
7360   (when (or (eq gnus-fetch-old-headers 'some)
7361             (eq gnus-fetch-old-headers 'invisible)
7362             (numberp gnus-fetch-old-headers)
7363             (eq gnus-build-sparse-threads 'some)
7364             (eq gnus-build-sparse-threads 'more))
7365     (let ((th threads))
7366       (while th
7367         (setcar th (gnus-cut-thread (car th)))
7368         (setq th (cdr th)))))
7369   ;; Remove nixed out threads.
7370   (delq nil threads))
7371
7372 (defun gnus-summary-initial-limit (&optional show-if-empty)
7373   "Figure out what the initial limit is supposed to be on group entry.
7374 This entails weeding out unwanted dormants, low-scored articles,
7375 fetch-old-headers verbiage, and so on."
7376   ;; Most groups have nothing to remove.
7377   (if (or gnus-inhibit-limiting
7378           (and (null gnus-newsgroup-dormant)
7379                (eq gnus-newsgroup-display 'gnus-not-ignore)
7380                (not (eq gnus-fetch-old-headers 'some))
7381                (not (numberp gnus-fetch-old-headers))
7382                (not (eq gnus-fetch-old-headers 'invisible))
7383                (null gnus-summary-expunge-below)
7384                (not (eq gnus-build-sparse-threads 'some))
7385                (not (eq gnus-build-sparse-threads 'more))
7386                (null gnus-thread-expunge-below)
7387                (not gnus-use-nocem)))
7388       ()                                ; Do nothing.
7389     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7390     (setq gnus-newsgroup-limit nil)
7391     (mapatoms
7392      (lambda (node)
7393        (unless (car (symbol-value node))
7394          ;; These threads have no parents -- they are roots.
7395          (let ((nodes (cdr (symbol-value node)))
7396                thread)
7397            (while nodes
7398              (if (and gnus-thread-expunge-below
7399                       (< (gnus-thread-total-score (car nodes))
7400                          gnus-thread-expunge-below))
7401                  (gnus-expunge-thread (pop nodes))
7402                (setq thread (pop nodes))
7403                (gnus-summary-limit-children thread))))))
7404      gnus-newsgroup-dependencies)
7405     ;; If this limitation resulted in an empty group, we might
7406     ;; pop the previous limit and use it instead.
7407     (when (and (not gnus-newsgroup-limit)
7408                show-if-empty)
7409       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7410     gnus-newsgroup-limit))
7411
7412 (defun gnus-summary-limit-children (thread)
7413   "Return 1 if this subthread is visible and 0 if it is not."
7414   ;; First we get the number of visible children to this thread.  This
7415   ;; is done by recursing down the thread using this function, so this
7416   ;; will really go down to a leaf article first, before slowly
7417   ;; working its way up towards the root.
7418   (when thread
7419     (let ((children
7420            (if (cdr thread)
7421                (apply '+ (mapcar 'gnus-summary-limit-children
7422                                  (cdr thread)))
7423              0))
7424           (number (mail-header-number (car thread)))
7425           score)
7426       (if (and
7427            (not (memq number gnus-newsgroup-marked))
7428            (or
7429             ;; If this article is dormant and has absolutely no visible
7430             ;; children, then this article isn't visible.
7431             (and (memq number gnus-newsgroup-dormant)
7432                  (zerop children))
7433             ;; If this is "fetch-old-headered" and there is no
7434             ;; visible children, then we don't want this article.
7435             (and (or (eq gnus-fetch-old-headers 'some)
7436                      (numberp gnus-fetch-old-headers))
7437                  (gnus-summary-article-ancient-p number)
7438                  (zerop children))
7439             ;; If this is "fetch-old-headered" and `invisible', then
7440             ;; we don't want this article.
7441             (and (eq gnus-fetch-old-headers 'invisible)
7442                  (gnus-summary-article-ancient-p number))
7443             ;; If this is a sparsely inserted article with no children,
7444             ;; we don't want it.
7445             (and (eq gnus-build-sparse-threads 'some)
7446                  (gnus-summary-article-sparse-p number)
7447                  (zerop children))
7448             ;; If we use expunging, and this article is really
7449             ;; low-scored, then we don't want this article.
7450             (when (and gnus-summary-expunge-below
7451                        (< (setq score
7452                                 (or (cdr (assq number gnus-newsgroup-scored))
7453                                     gnus-summary-default-score))
7454                           gnus-summary-expunge-below))
7455               ;; We increase the expunge-tally here, but that has
7456               ;; nothing to do with the limits, really.
7457               (incf gnus-newsgroup-expunged-tally)
7458               ;; We also mark as read here, if that's wanted.
7459               (when (and gnus-summary-mark-below
7460                          (< score gnus-summary-mark-below))
7461                 (setq gnus-newsgroup-unreads
7462                       (delq number gnus-newsgroup-unreads))
7463                 (if gnus-newsgroup-auto-expire
7464                     (push number gnus-newsgroup-expirable)
7465                   (push (cons number gnus-low-score-mark)
7466                         gnus-newsgroup-reads)))
7467               t)
7468             ;; Do the `display' group parameter.
7469             (and gnus-newsgroup-display
7470                  (not (funcall gnus-newsgroup-display)))
7471             ;; Check NoCeM things.
7472             (if (and gnus-use-nocem
7473                      (gnus-nocem-unwanted-article-p
7474                       (mail-header-id (car thread))))
7475                 (progn
7476                   (setq gnus-newsgroup-unreads
7477                         (delq number gnus-newsgroup-unreads))
7478                   t))))
7479           ;; Nope, invisible article.
7480           0
7481         ;; Ok, this article is to be visible, so we add it to the limit
7482         ;; and return 1.
7483         (push number gnus-newsgroup-limit)
7484         1))))
7485
7486 (defun gnus-expunge-thread (thread)
7487   "Mark all articles in THREAD as read."
7488   (let* ((number (mail-header-number (car thread))))
7489     (incf gnus-newsgroup-expunged-tally)
7490     ;; We also mark as read here, if that's wanted.
7491     (setq gnus-newsgroup-unreads
7492           (delq number gnus-newsgroup-unreads))
7493     (if gnus-newsgroup-auto-expire
7494         (push number gnus-newsgroup-expirable)
7495       (push (cons number gnus-low-score-mark)
7496             gnus-newsgroup-reads)))
7497   ;; Go recursively through all subthreads.
7498   (mapcar 'gnus-expunge-thread (cdr thread)))
7499
7500 ;; Summary article oriented commands
7501
7502 (defun gnus-summary-refer-parent-article (n)
7503   "Refer parent article N times.
7504 If N is negative, go to ancestor -N instead.
7505 The difference between N and the number of articles fetched is returned."
7506   (interactive "p")
7507   (let ((skip 1)
7508         error header ref)
7509     (when (not (natnump n))
7510       (setq skip (abs n)
7511             n 1))
7512     (while (and (> n 0)
7513                 (not error))
7514       (setq header (gnus-summary-article-header))
7515       (if (and (eq (mail-header-number header)
7516                    (cdr gnus-article-current))
7517                (equal gnus-newsgroup-name
7518                       (car gnus-article-current)))
7519           ;; If we try to find the parent of the currently
7520           ;; displayed article, then we take a look at the actual
7521           ;; References header, since this is slightly more
7522           ;; reliable than the References field we got from the
7523           ;; server.
7524           (save-excursion
7525             (set-buffer gnus-original-article-buffer)
7526             (nnheader-narrow-to-headers)
7527             (unless (setq ref (message-fetch-field "references"))
7528               (setq ref (message-fetch-field "in-reply-to")))
7529             (widen))
7530         (setq ref
7531               ;; It's not the current article, so we take a bet on
7532               ;; the value we got from the server.
7533               (mail-header-references header)))
7534       (if (and ref
7535                (not (equal ref "")))
7536           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7537             (gnus-message 1 "Couldn't find parent"))
7538         (gnus-message 1 "No references in article %d"
7539                       (gnus-summary-article-number))
7540         (setq error t))
7541       (decf n))
7542     (gnus-summary-position-point)
7543     n))
7544
7545 (defun gnus-summary-refer-references ()
7546   "Fetch all articles mentioned in the References header.
7547 Return the number of articles fetched."
7548   (interactive)
7549   (let ((ref (mail-header-references (gnus-summary-article-header)))
7550         (current (gnus-summary-article-number))
7551         (n 0))
7552     (if (or (not ref)
7553             (equal ref ""))
7554         (error "No References in the current article")
7555       ;; For each Message-ID in the References header...
7556       (while (string-match "<[^>]*>" ref)
7557         (incf n)
7558         ;; ... fetch that article.
7559         (gnus-summary-refer-article
7560          (prog1 (match-string 0 ref)
7561            (setq ref (substring ref (match-end 0))))))
7562       (gnus-summary-goto-subject current)
7563       (gnus-summary-position-point)
7564       n)))
7565
7566 (defun gnus-summary-refer-thread (&optional limit)
7567   "Fetch all articles in the current thread.
7568 If LIMIT (the numerical prefix), fetch that many old headers instead
7569 of what's specified by the `gnus-refer-thread-limit' variable."
7570   (interactive "P")
7571   (let ((id (mail-header-id (gnus-summary-article-header)))
7572         (limit (if limit (prefix-numeric-value limit)
7573                  gnus-refer-thread-limit)))
7574     ;; We want to fetch LIMIT *old* headers, but we also have to
7575     ;; re-fetch all the headers in the current buffer, because many of
7576     ;; them may be undisplayed.  So we adjust LIMIT.
7577     (when (numberp limit)
7578       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7579     (unless (eq gnus-fetch-old-headers 'invisible)
7580       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7581       ;; Retrieve the headers and read them in.
7582       (if (eq (gnus-retrieve-headers
7583                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7584               'nov)
7585           (gnus-build-all-threads)
7586         (error "Can't fetch thread from backends that don't support NOV"))
7587       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7588     (gnus-summary-limit-include-thread id)))
7589
7590 (defun gnus-summary-refer-article (message-id)
7591   "Fetch an article specified by MESSAGE-ID."
7592   (interactive "sMessage-ID: ")
7593   (when (and (stringp message-id)
7594              (not (zerop (length message-id))))
7595     ;; Construct the correct Message-ID if necessary.
7596     ;; Suggested by tale@pawl.rpi.edu.
7597     (unless (string-match "^<" message-id)
7598       (setq message-id (concat "<" message-id)))
7599     (unless (string-match ">$" message-id)
7600       (setq message-id (concat message-id ">")))
7601     (let* ((header (gnus-id-to-header message-id))
7602            (sparse (and header
7603                         (gnus-summary-article-sparse-p
7604                          (mail-header-number header))
7605                         (memq (mail-header-number header)
7606                               gnus-newsgroup-limit)))
7607            number)
7608       (cond
7609        ;; If the article is present in the buffer we just go to it.
7610        ((and header
7611              (or (not (gnus-summary-article-sparse-p
7612                        (mail-header-number header)))
7613                  sparse))
7614         (prog1
7615             (gnus-summary-goto-article
7616              (mail-header-number header) nil t)
7617           (when sparse
7618             (gnus-summary-update-article (mail-header-number header)))))
7619        (t
7620         ;; We fetch the article.
7621         (catch 'found
7622           (dolist (gnus-override-method (gnus-refer-article-methods))
7623             (gnus-check-server gnus-override-method)
7624             ;; Fetch the header, and display the article.
7625             (when (setq number (gnus-summary-insert-subject message-id))
7626               (gnus-summary-select-article nil nil nil number)
7627               (throw 'found t)))
7628           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7629
7630 (defun gnus-refer-article-methods ()
7631   "Return a list of referrable methods."
7632   (cond
7633    ;; No method, so we default to current and native.
7634    ((null gnus-refer-article-method)
7635     (list gnus-current-select-method gnus-select-method))
7636    ;; Current.
7637    ((eq 'current gnus-refer-article-method)
7638     (list gnus-current-select-method))
7639    ;; List of select methods.
7640    ((not (and (symbolp (car gnus-refer-article-method))
7641               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7642     (let (out)
7643       (dolist (method gnus-refer-article-method)
7644         (push (if (eq 'current method)
7645                   gnus-current-select-method
7646                 method)
7647               out))
7648       (nreverse out)))
7649    ;; One single select method.
7650    (t
7651     (list gnus-refer-article-method))))
7652
7653 (defun gnus-summary-edit-parameters ()
7654   "Edit the group parameters of the current group."
7655   (interactive)
7656   (gnus-group-edit-group gnus-newsgroup-name 'params))
7657
7658 (defun gnus-summary-customize-parameters ()
7659   "Customize the group parameters of the current group."
7660   (interactive)
7661   (gnus-group-customize gnus-newsgroup-name))
7662
7663 (defun gnus-summary-enter-digest-group (&optional force)
7664   "Enter an nndoc group based on the current article.
7665 If FORCE, force a digest interpretation.  If not, try
7666 to guess what the document format is."
7667   (interactive "P")
7668   (let ((conf gnus-current-window-configuration))
7669     (save-excursion
7670       (gnus-summary-select-article))
7671     (setq gnus-current-window-configuration conf)
7672     (let* ((name (format "%s-%d"
7673                          (gnus-group-prefixed-name
7674                           gnus-newsgroup-name (list 'nndoc ""))
7675                          (save-excursion
7676                            (set-buffer gnus-summary-buffer)
7677                            gnus-current-article)))
7678            (ogroup gnus-newsgroup-name)
7679            (params (append (gnus-info-params (gnus-get-info ogroup))
7680                            (list (cons 'to-group ogroup))
7681                            (list (cons 'save-article-group ogroup))))
7682            (case-fold-search t)
7683            (buf (current-buffer))
7684            dig to-address)
7685       (save-excursion
7686         (set-buffer gnus-original-article-buffer)
7687         ;; Have the digest group inherit the main mail address of
7688         ;; the parent article.
7689         (when (setq to-address (or (message-fetch-field "reply-to")
7690                                    (message-fetch-field "from")))
7691           (setq params (append
7692                         (list (cons 'to-address
7693                                     (funcall gnus-decode-encoded-word-function
7694                                              to-address))))))
7695         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7696         (insert-buffer-substring gnus-original-article-buffer)
7697         ;; Remove lines that may lead nndoc to misinterpret the
7698         ;; document type.
7699         (narrow-to-region
7700          (goto-char (point-min))
7701          (or (search-forward "\n\n" nil t) (point)))
7702         (goto-char (point-min))
7703         (delete-matching-lines "^Path:\\|^From ")
7704         (widen))
7705       (unwind-protect
7706           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7707                     (gnus-newsgroup-ephemeral-ignored-charsets
7708                      gnus-newsgroup-ignored-charsets))
7709                 (gnus-group-read-ephemeral-group
7710                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7711                               (nndoc-article-type
7712                                ,(if force 'mbox 'guess)))
7713                  t nil nil nil
7714                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7715                                                         "ADAPT")))))
7716               ;; Make all postings to this group go to the parent group.
7717               (nconc (gnus-info-params (gnus-get-info name))
7718                      params)
7719             ;; Couldn't select this doc group.
7720             (switch-to-buffer buf)
7721             (gnus-set-global-variables)
7722             (gnus-configure-windows 'summary)
7723             (gnus-message 3 "Article couldn't be entered?"))
7724         (kill-buffer dig)))))
7725
7726 (defun gnus-summary-read-document (n)
7727   "Open a new group based on the current article(s).
7728 This will allow you to read digests and other similar
7729 documents as newsgroups.
7730 Obeys the standard process/prefix convention."
7731   (interactive "P")
7732   (let* ((articles (gnus-summary-work-articles n))
7733          (ogroup gnus-newsgroup-name)
7734          (params (append (gnus-info-params (gnus-get-info ogroup))
7735                          (list (cons 'to-group ogroup))))
7736          article group egroup groups vgroup)
7737     (while (setq article (pop articles))
7738       (setq group (format "%s-%d" gnus-newsgroup-name article))
7739       (gnus-summary-remove-process-mark article)
7740       (when (gnus-summary-display-article article)
7741         (save-excursion
7742           (with-temp-buffer
7743             (insert-buffer-substring gnus-original-article-buffer)
7744             ;; Remove some headers that may lead nndoc to make
7745             ;; the wrong guess.
7746             (message-narrow-to-head)
7747             (goto-char (point-min))
7748             (delete-matching-lines "^\\(Path\\):\\|^From ")
7749             (widen)
7750             (if (setq egroup
7751                       (gnus-group-read-ephemeral-group
7752                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7753                                      (nndoc-article-type guess))
7754                        t nil t))
7755                 (progn
7756             ;; Make all postings to this group go to the parent group.
7757                   (nconc (gnus-info-params (gnus-get-info egroup))
7758                          params)
7759                   (push egroup groups))
7760               ;; Couldn't select this doc group.
7761               (gnus-error 3 "Article couldn't be entered"))))))
7762     ;; Now we have selected all the documents.
7763     (cond
7764      ((not groups)
7765       (error "None of the articles could be interpreted as documents"))
7766      ((gnus-group-read-ephemeral-group
7767        (setq vgroup (format
7768                      "nnvirtual:%s-%s" gnus-newsgroup-name
7769                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7770        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7771        t
7772        (cons (current-buffer) 'summary)))
7773      (t
7774       (error "Couldn't select virtual nndoc group")))))
7775
7776 (defun gnus-summary-isearch-article (&optional regexp-p)
7777   "Do incremental search forward on the current article.
7778 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7779   (interactive "P")
7780   (gnus-summary-select-article)
7781   (gnus-configure-windows 'article)
7782   (gnus-eval-in-buffer-window gnus-article-buffer
7783     (save-restriction
7784       (widen)
7785       (isearch-forward regexp-p))))
7786
7787 (defun gnus-summary-search-article-forward (regexp &optional backward)
7788   "Search for an article containing REGEXP forward.
7789 If BACKWARD, search backward instead."
7790   (interactive
7791    (list (read-string
7792           (format "Search article %s (regexp%s): "
7793                   (if current-prefix-arg "backward" "forward")
7794                   (if gnus-last-search-regexp
7795                       (concat ", default " gnus-last-search-regexp)
7796                     "")))
7797          current-prefix-arg))
7798   (if (string-equal regexp "")
7799       (setq regexp (or gnus-last-search-regexp ""))
7800     (setq gnus-last-search-regexp regexp)
7801     (setq gnus-article-before-search gnus-current-article))
7802   ;; Intentionally set gnus-last-article.
7803   (setq gnus-last-article gnus-article-before-search)
7804   (let ((gnus-last-article gnus-last-article))
7805     (if (gnus-summary-search-article regexp backward)
7806         (gnus-summary-show-thread)
7807       (error "Search failed: \"%s\"" regexp))))
7808
7809 (defun gnus-summary-search-article-backward (regexp)
7810   "Search for an article containing REGEXP backward."
7811   (interactive
7812    (list (read-string
7813           (format "Search article backward (regexp%s): "
7814                   (if gnus-last-search-regexp
7815                       (concat ", default " gnus-last-search-regexp)
7816                     "")))))
7817   (gnus-summary-search-article-forward regexp 'backward))
7818
7819 (defun gnus-summary-search-article (regexp &optional backward)
7820   "Search for an article containing REGEXP.
7821 Optional argument BACKWARD means do search for backward.
7822 `gnus-select-article-hook' is not called during the search."
7823   ;; We have to require this here to make sure that the following
7824   ;; dynamic binding isn't shadowed by autoloading.
7825   (require 'gnus-async)
7826   (require 'gnus-art)
7827   (let ((gnus-select-article-hook nil)  ;Disable hook.
7828         (gnus-article-prepare-hook nil)
7829         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7830         (gnus-use-article-prefetch nil)
7831         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7832         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7833         (sum (current-buffer))
7834         (gnus-display-mime-function nil)
7835         (found nil)
7836         point)
7837     (gnus-save-hidden-threads
7838       (gnus-summary-select-article)
7839       (set-buffer gnus-article-buffer)
7840       (goto-char (window-point (get-buffer-window (current-buffer))))
7841       (when backward
7842         (forward-line -1))
7843       (while (not found)
7844         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7845         (if (if backward
7846                 (re-search-backward regexp nil t)
7847               (re-search-forward regexp nil t))
7848             ;; We found the regexp.
7849             (progn
7850               (setq found 'found)
7851               (beginning-of-line)
7852               (set-window-start
7853                (get-buffer-window (current-buffer))
7854                (point))
7855               (forward-line 1)
7856               (set-window-point
7857                (get-buffer-window (current-buffer))
7858                (point))
7859               (set-buffer sum)
7860               (setq point (point)))
7861           ;; We didn't find it, so we go to the next article.
7862           (set-buffer sum)
7863           (setq found 'not)
7864           (while (eq found 'not)
7865             (if (not (if backward (gnus-summary-find-prev)
7866                        (gnus-summary-find-next)))
7867                 ;; No more articles.
7868                 (setq found t)
7869               ;; Select the next article and adjust point.
7870               (unless (gnus-summary-article-sparse-p
7871                        (gnus-summary-article-number))
7872                 (setq found nil)
7873                 (gnus-summary-select-article)
7874                 (set-buffer gnus-article-buffer)
7875                 (widen)
7876                 (goto-char (if backward (point-max) (point-min))))))))
7877       (gnus-message 7 ""))
7878     ;; Return whether we found the regexp.
7879     (when (eq found 'found)
7880       (goto-char point)
7881       (gnus-summary-show-thread)
7882       (gnus-summary-goto-subject gnus-current-article)
7883       (gnus-summary-position-point)
7884       t)))
7885
7886 (defun gnus-find-matching-articles (header regexp)
7887   "Return a list of all articles that match REGEXP on HEADER.
7888 This search includes all articles in the current group that Gnus has
7889 fetched headers for, whether they are displayed or not."
7890   (let ((articles nil)
7891         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7892         (case-fold-search t))
7893     (dolist (header gnus-newsgroup-headers)
7894       (when (string-match regexp (funcall func header))
7895         (push (mail-header-number header) articles)))
7896     (nreverse articles)))
7897
7898 (defun gnus-summary-find-matching (header regexp &optional backward unread
7899                                           not-case-fold not-matching)
7900   "Return a list of all articles that match REGEXP on HEADER.
7901 The search stars on the current article and goes forwards unless
7902 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7903 If UNREAD is non-nil, only unread articles will
7904 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7905 in the comparisons. If NOT-MATCHING, return a list of all articles that
7906 not match REGEXP on HEADER."
7907   (let ((case-fold-search (not not-case-fold))
7908         articles d func)
7909     (if (consp header)
7910         (if (eq (car header) 'extra)
7911             (setq func
7912                   `(lambda (h)
7913                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7914                          "")))
7915           (error "%s is an invalid header" header))
7916       (unless (fboundp (intern (concat "mail-header-" header)))
7917         (error "%s is not a valid header" header))
7918       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7919     (dolist (d (if (eq backward 'all)
7920                    gnus-newsgroup-data
7921                  (gnus-data-find-list
7922                   (gnus-summary-article-number)
7923                   (gnus-data-list backward))))
7924       (when (and (or (not unread)       ; We want all articles...
7925                      (gnus-data-unread-p d)) ; Or just unreads.
7926                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7927                  (if not-matching
7928                      (not (string-match
7929                            regexp
7930                            (funcall func (gnus-data-header d))))
7931                    (string-match regexp
7932                                  (funcall func (gnus-data-header d)))))
7933         (push (gnus-data-number d) articles))) ; Success!
7934     (nreverse articles)))
7935
7936 (defun gnus-summary-execute-command (header regexp command &optional backward)
7937   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7938 If HEADER is an empty string (or nil), the match is done on the entire
7939 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7940   (interactive
7941    (list (let ((completion-ignore-case t))
7942            (completing-read
7943             "Header name: "
7944             (mapcar (lambda (header) (list (format "%s" header)))
7945                     (append
7946                      '("Number" "Subject" "From" "Lines" "Date"
7947                        "Message-ID" "Xref" "References" "Body")
7948                      gnus-extra-headers))
7949             nil 'require-match))
7950          (read-string "Regexp: ")
7951          (read-key-sequence "Command: ")
7952          current-prefix-arg))
7953   (when (equal header "Body")
7954     (setq header ""))
7955   ;; Hidden thread subtrees must be searched as well.
7956   (gnus-summary-show-all-threads)
7957   ;; We don't want to change current point nor window configuration.
7958   (save-excursion
7959     (save-window-excursion
7960       (gnus-message 6 "Executing %s..." (key-description command))
7961 ;; We'd like to execute COMMAND interactively so as to give arguments.
7962       (gnus-execute header regexp
7963                     `(call-interactively ',(key-binding command))
7964                     backward)
7965       (gnus-message 6 "Executing %s...done" (key-description command)))))
7966
7967 (defun gnus-summary-beginning-of-article ()
7968   "Scroll the article back to the beginning."
7969   (interactive)
7970   (gnus-summary-select-article)
7971   (gnus-configure-windows 'article)
7972   (gnus-eval-in-buffer-window gnus-article-buffer
7973     (widen)
7974     (goto-char (point-min))
7975     (when gnus-page-broken
7976       (gnus-narrow-to-page))))
7977
7978 (defun gnus-summary-end-of-article ()
7979   "Scroll to the end of the article."
7980   (interactive)
7981   (gnus-summary-select-article)
7982   (gnus-configure-windows 'article)
7983   (gnus-eval-in-buffer-window gnus-article-buffer
7984     (widen)
7985     (goto-char (point-max))
7986     (recenter -3)
7987     (when gnus-page-broken
7988       (gnus-narrow-to-page))))
7989
7990 (defun gnus-summary-print-truncate-and-quote (string &optional len)
7991   "Truncate to LEN and quote all \"(\"'s in STRING."
7992   (gnus-replace-in-string (if (and len (> (length string) len))
7993                               (substring string 0 len)
7994                             string)
7995                           "[()]" "\\\\\\&"))
7996
7997 (defun gnus-summary-print-article (&optional filename n)
7998   "Generate and print a PostScript image of the N next (mail) articles.
7999
8000 If N is negative, print the N previous articles.  If N is nil and articles
8001 have been marked with the process mark, print these instead.
8002
8003 If the optional first argument FILENAME is nil, send the image to the
8004 printer.  If FILENAME is a string, save the PostScript image in a file with
8005 that name.  If FILENAME is a number, prompt the user for the name of the file
8006 to save in."
8007   (interactive (list (ps-print-preprint current-prefix-arg)))
8008   (dolist (article (gnus-summary-work-articles n))
8009     (gnus-summary-select-article nil nil 'pseudo article)
8010     (gnus-eval-in-buffer-window gnus-article-buffer
8011       (gnus-print-buffer))
8012     (gnus-summary-remove-process-mark article))
8013   (ps-despool filename))
8014
8015 (defun gnus-print-buffer ()
8016   (let ((buffer (generate-new-buffer " *print*")))
8017     (unwind-protect
8018         (progn
8019           (copy-to-buffer buffer (point-min) (point-max))
8020           (set-buffer buffer)
8021           (gnus-article-delete-invisible-text)
8022           (when (gnus-visual-p 'article-highlight 'highlight)
8023             ;; Copy-to-buffer doesn't copy overlay.  So redo
8024             ;; highlight.
8025             (let ((gnus-article-buffer buffer))
8026               (gnus-article-highlight-citation t)
8027               (gnus-article-highlight-signature)))
8028           (let ((ps-left-header
8029                  (list
8030                   (concat "("
8031                           (gnus-summary-print-truncate-and-quote
8032                            (mail-header-subject gnus-current-headers)
8033                            66) ")")
8034                   (concat "("
8035                           (gnus-summary-print-truncate-and-quote
8036                            (mail-header-from gnus-current-headers)
8037                            45) ")")))
8038                 (ps-right-header
8039                  (list
8040                   "/pagenumberstring load"
8041                   (concat "("
8042                           (mail-header-date gnus-current-headers) ")"))))
8043             (gnus-run-hooks 'gnus-ps-print-hook)
8044             (save-excursion
8045               (if window-system
8046                   (ps-spool-buffer-with-faces)
8047                 (ps-spool-buffer)))))
8048       (kill-buffer buffer))))
8049
8050 (defun gnus-summary-show-article (&optional arg)
8051   "Force redisplaying of the current article.
8052 If ARG (the prefix) is a number, show the article with the charset
8053 defined in `gnus-summary-show-article-charset-alist', or the charset
8054 input.
8055 If ARG (the prefix) is non-nil and not a number, show the raw article
8056 without any article massaging functions being run.  Normally, the key strokes 
8057 are `C-u g'."
8058   (interactive "P")
8059   (cond
8060    ((numberp arg)
8061     (gnus-summary-show-article t)
8062     (let ((gnus-newsgroup-charset
8063            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8064                (mm-read-coding-system
8065                 "View as charset: " ;; actually it is coding system.
8066                 (save-excursion
8067                   (set-buffer gnus-article-buffer)
8068                   (mm-detect-coding-region (point) (point-max))))))
8069           (gnus-newsgroup-ignored-charsets 'gnus-all))
8070       (gnus-summary-select-article nil 'force)
8071       (let ((deps gnus-newsgroup-dependencies)
8072             head header lines)
8073         (save-excursion
8074           (set-buffer gnus-original-article-buffer)
8075           (save-restriction
8076             (message-narrow-to-head)
8077             (setq head (buffer-string))
8078             (goto-char (point-min))
8079             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8080               (goto-char (point-max))
8081               (widen)
8082               (setq lines (1- (count-lines (point) (point-max))))))
8083           (with-temp-buffer
8084             (insert (format "211 %d Article retrieved.\n"
8085                             (cdr gnus-article-current)))
8086             (insert head)
8087             (if lines (insert (format "Lines: %d\n" lines)))
8088             (insert ".\n")
8089             (let ((nntp-server-buffer (current-buffer)))
8090               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8091         (gnus-data-set-header
8092          (gnus-data-find (cdr gnus-article-current))
8093          header)
8094         (gnus-summary-update-article-line
8095          (cdr gnus-article-current) header)
8096         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8097           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8098    ((not arg)
8099     ;; Select the article the normal way.
8100     (gnus-summary-select-article nil 'force))
8101    (t
8102     ;; We have to require this here to make sure that the following
8103     ;; dynamic binding isn't shadowed by autoloading.
8104     (require 'gnus-async)
8105     (require 'gnus-art)
8106     ;; Bind the article treatment functions to nil.
8107     (let ((gnus-have-all-headers t)
8108           gnus-article-prepare-hook
8109           gnus-article-decode-hook
8110           gnus-display-mime-function
8111           gnus-break-pages)
8112       ;; Destroy any MIME parts.
8113       (when (gnus-buffer-live-p gnus-article-buffer)
8114         (save-excursion
8115           (set-buffer gnus-article-buffer)
8116           (mm-destroy-parts gnus-article-mime-handles)
8117           ;; Set it to nil for safety reason.
8118           (setq gnus-article-mime-handle-alist nil)
8119           (setq gnus-article-mime-handles nil)))
8120       (gnus-summary-select-article nil 'force))))
8121   (gnus-summary-goto-subject gnus-current-article)
8122   (gnus-summary-position-point))
8123
8124 (defun gnus-summary-show-raw-article ()
8125   "Show the raw article without any article massaging functions being run."
8126   (interactive)
8127   (gnus-summary-show-article t))
8128
8129 (defun gnus-summary-verbose-headers (&optional arg)
8130   "Toggle permanent full header display.
8131 If ARG is a positive number, turn header display on.
8132 If ARG is a negative number, turn header display off."
8133   (interactive "P")
8134   (setq gnus-show-all-headers
8135         (cond ((or (not (numberp arg))
8136                    (zerop arg))
8137                (not gnus-show-all-headers))
8138               ((natnump arg)
8139                t)))
8140   (gnus-summary-show-article))
8141
8142 (defun gnus-summary-toggle-header (&optional arg)
8143   "Show the headers if they are hidden, or hide them if they are shown.
8144 If ARG is a positive number, show the entire header.
8145 If ARG is a negative number, hide the unwanted header lines."
8146   (interactive "P")
8147   (save-excursion
8148     (set-buffer gnus-article-buffer)
8149     (save-restriction
8150       (let* ((buffer-read-only nil)
8151              (inhibit-point-motion-hooks t)
8152              hidden e)
8153         (setq hidden
8154               (if (numberp arg)
8155                   (>= arg 0)
8156                 (save-restriction
8157                   (article-narrow-to-head)
8158                   (gnus-article-hidden-text-p 'headers))))
8159         (goto-char (point-min))
8160         (when (search-forward "\n\n" nil t)
8161           (delete-region (point-min) (1- (point))))
8162         (goto-char (point-min))
8163         (save-excursion
8164           (set-buffer gnus-original-article-buffer)
8165           (goto-char (point-min))
8166           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8167         (insert-buffer-substring gnus-original-article-buffer 1 e)
8168         (save-restriction
8169           (narrow-to-region (point-min) (point))
8170           (article-decode-encoded-words)
8171           (if  hidden
8172               (let ((gnus-treat-hide-headers nil)
8173                     (gnus-treat-hide-boring-headers nil))
8174                 (gnus-delete-wash-type 'headers)
8175                 (gnus-treat-article 'head))
8176             (gnus-treat-article 'head)))
8177         (gnus-set-mode-line 'article)))))
8178
8179 (defun gnus-summary-show-all-headers ()
8180   "Make all header lines visible."
8181   (interactive)
8182   (gnus-summary-toggle-header 1))
8183
8184 (defun gnus-summary-caesar-message (&optional arg)
8185   "Caesar rotate the current article by 13.
8186 The numerical prefix specifies how many places to rotate each letter
8187 forward."
8188   (interactive "P")
8189   (gnus-summary-select-article)
8190   (let ((mail-header-separator ""))
8191     (gnus-eval-in-buffer-window gnus-article-buffer
8192       (save-restriction
8193         (widen)
8194         (let ((start (window-start))
8195               buffer-read-only)
8196           (message-caesar-buffer-body arg)
8197           (set-window-start (get-buffer-window (current-buffer)) start))))))
8198
8199 (defun gnus-summary-stop-page-breaking ()
8200   "Stop page breaking in the current article."
8201   (interactive)
8202   (gnus-summary-select-article)
8203   (gnus-eval-in-buffer-window gnus-article-buffer
8204     (widen)
8205     (when (gnus-visual-p 'page-marker)
8206       (let ((buffer-read-only nil))
8207         (gnus-remove-text-with-property 'gnus-prev)
8208         (gnus-remove-text-with-property 'gnus-next))
8209       (setq gnus-page-broken nil))))
8210
8211 (defun gnus-summary-move-article (&optional n to-newsgroup
8212                                             select-method action)
8213   "Move the current article to a different newsgroup.
8214 If N is a positive number, move the N next articles.
8215 If N is a negative number, move the N previous articles.
8216 If N is nil and any articles have been marked with the process mark,
8217 move those articles instead.
8218 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8219 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8220 re-spool using this method.
8221
8222 For this function to work, both the current newsgroup and the
8223 newsgroup that you want to move to have to support the `request-move'
8224 and `request-accept' functions.
8225
8226 ACTION can be either `move' (the default), `crosspost' or `copy'."
8227   (interactive "P")
8228   (unless action
8229     (setq action 'move))
8230   ;; Check whether the source group supports the required functions.
8231   (cond ((and (eq action 'move)
8232               (not (gnus-check-backend-function
8233                     'request-move-article gnus-newsgroup-name)))
8234          (error "The current group does not support article moving"))
8235         ((and (eq action 'crosspost)
8236               (not (gnus-check-backend-function
8237                     'request-replace-article gnus-newsgroup-name)))
8238          (error "The current group does not support article editing")))
8239   (let ((articles (gnus-summary-work-articles n))
8240         (prefix (if (gnus-check-backend-function
8241                      'request-move-article gnus-newsgroup-name)
8242                     (gnus-group-real-prefix gnus-newsgroup-name)
8243                   ""))
8244         (names '((move "Move" "Moving")
8245                  (copy "Copy" "Copying")
8246                  (crosspost "Crosspost" "Crossposting")))
8247         (copy-buf (save-excursion
8248                     (nnheader-set-temp-buffer " *copy article*")))
8249         art-group to-method new-xref article to-groups)
8250     (unless (assq action names)
8251       (error "Unknown action %s" action))
8252     ;; We have to select an article to give
8253     ;; `gnus-read-move-group-name' an opportunity to suggest an
8254     ;; appropriate default.
8255     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8256       (let ((gnus-display-mime-function nil)
8257             (gnus-article-prepare-hook nil))
8258         (gnus-summary-select-article nil nil nil (car articles))))
8259     ;; Read the newsgroup name.
8260     (when (and (not to-newsgroup)
8261                (not select-method))
8262       (setq to-newsgroup
8263             (gnus-read-move-group-name
8264              (cadr (assq action names))
8265              (symbol-value (intern (format "gnus-current-%s-group" action)))
8266              articles prefix))
8267       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8268     (setq to-method (or select-method
8269                         (gnus-server-to-method
8270                          (gnus-group-method to-newsgroup))))
8271     ;; Check the method we are to move this article to...
8272     (unless (gnus-check-backend-function
8273              'request-accept-article (car to-method))
8274       (error "%s does not support article copying" (car to-method)))
8275     (unless (gnus-check-server to-method)
8276       (error "Can't open server %s" (car to-method)))
8277     (gnus-message 6 "%s to %s: %s..."
8278                   (caddr (assq action names))
8279                   (or (car select-method) to-newsgroup) articles)
8280     (while articles
8281       (setq article (pop articles))
8282       (setq
8283        art-group
8284        (cond
8285         ;; Move the article.
8286         ((eq action 'move)
8287          ;; Remove this article from future suppression.
8288          (gnus-dup-unsuppress-article article)
8289          (gnus-request-move-article
8290           article                       ; Article to move
8291           gnus-newsgroup-name           ; From newsgroup
8292           (nth 1 (gnus-find-method-for-group
8293                   gnus-newsgroup-name)) ; Server
8294           (list 'gnus-request-accept-article
8295                 to-newsgroup (list 'quote select-method)
8296                 (not articles) t)       ; Accept form
8297           (not articles)))              ; Only save nov last time
8298         ;; Copy the article.
8299         ((eq action 'copy)
8300          (save-excursion
8301            (set-buffer copy-buf)
8302            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8303              (gnus-request-accept-article
8304               to-newsgroup select-method (not articles) t))))
8305         ;; Crosspost the article.
8306         ((eq action 'crosspost)
8307          (let ((xref (message-tokenize-header
8308                       (mail-header-xref (gnus-summary-article-header article))
8309                       " ")))
8310            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8311                                   ":" (number-to-string article)))
8312            (unless xref
8313              (setq xref (list (system-name))))
8314            (setq new-xref
8315                  (concat
8316                   (mapconcat 'identity
8317                              (delete "Xref:" (delete new-xref xref))
8318                              " ")
8319                   " " new-xref))
8320            (save-excursion
8321              (set-buffer copy-buf)
8322              ;; First put the article in the destination group.
8323              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8324              (when (consp (setq art-group
8325                                 (gnus-request-accept-article
8326                                  to-newsgroup select-method (not articles))))
8327                (setq new-xref (concat new-xref " " (car art-group)
8328                                       ":"
8329                                       (number-to-string (cdr art-group))))
8330                ;; Now we have the new Xrefs header, so we insert
8331                ;; it and replace the new article.
8332                (nnheader-replace-header "Xref" new-xref)
8333                (gnus-request-replace-article
8334                 (cdr art-group) to-newsgroup (current-buffer))
8335                art-group))))))
8336       (cond
8337        ((not art-group)
8338         (gnus-message 1 "Couldn't %s article %s: %s"
8339                       (cadr (assq action names)) article
8340                       (nnheader-get-report (car to-method))))
8341        ((eq art-group 'junk)
8342         (when (eq action 'move)
8343           (gnus-summary-mark-article article gnus-canceled-mark)
8344           (gnus-message 4 "Deleted article %s" article)))
8345        (t
8346         (let* ((pto-group (gnus-group-prefixed-name
8347                            (car art-group) to-method))
8348                (entry
8349                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8350                (info (nth 2 entry))
8351                (to-group (gnus-info-group info))
8352                to-marks)
8353           ;; Update the group that has been moved to.
8354           (when (and info
8355                      (memq action '(move copy)))
8356             (unless (member to-group to-groups)
8357               (push to-group to-groups))
8358
8359             (unless (memq article gnus-newsgroup-unreads)
8360               (push 'read to-marks)
8361               (gnus-info-set-read
8362                info (gnus-add-to-range (gnus-info-read info)
8363                                        (list (cdr art-group)))))
8364
8365             ;; See whether the article is to be put in the cache.
8366             (let ((marks gnus-article-mark-lists)
8367                   (to-article (cdr art-group)))
8368
8369               ;; Enter the article into the cache in the new group,
8370               ;; if that is required.
8371               (when gnus-use-cache
8372                 (gnus-cache-possibly-enter-article
8373                  to-group to-article
8374                  (memq article gnus-newsgroup-marked)
8375                  (memq article gnus-newsgroup-dormant)
8376                  (memq article gnus-newsgroup-unreads)))
8377
8378               (when gnus-preserve-marks
8379                 ;; Copy any marks over to the new group.
8380                 (when (and (equal to-group gnus-newsgroup-name)
8381                            (not (memq article gnus-newsgroup-unreads)))
8382                   ;; Mark this article as read in this group.
8383                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8384                   (setcdr (gnus-active to-group) to-article)
8385                   (setcdr gnus-newsgroup-active to-article))
8386
8387                 (while marks
8388                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8389                     (when (memq article (symbol-value
8390                                          (intern (format "gnus-newsgroup-%s"
8391                                                          (caar marks)))))
8392                       (push (cdar marks) to-marks)
8393                       ;; If the other group is the same as this group,
8394                       ;; then we have to add the mark to the list.
8395                       (when (equal to-group gnus-newsgroup-name)
8396                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8397                              (cons to-article
8398                                    (symbol-value
8399                                     (intern (format "gnus-newsgroup-%s"
8400                                                     (caar marks)))))))
8401                       ;; Copy the marks to other group.
8402                       (gnus-add-marked-articles
8403                        to-group (cdar marks) (list to-article) info)))
8404                   (setq marks (cdr marks)))
8405
8406                 (gnus-request-set-mark to-group (list (list (list to-article)
8407                                                             'add
8408                                                             to-marks))))
8409
8410               (gnus-dribble-enter
8411                (concat "(gnus-group-set-info '"
8412                        (gnus-prin1-to-string (gnus-get-info to-group))
8413                        ")"))))
8414
8415           ;; Update the Xref header in this article to point to
8416           ;; the new crossposted article we have just created.
8417           (when (eq action 'crosspost)
8418             (save-excursion
8419               (set-buffer copy-buf)
8420               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8421               (nnheader-replace-header "Xref" new-xref)
8422               (gnus-request-replace-article
8423                article gnus-newsgroup-name (current-buffer)))))
8424
8425         ;;;!!!Why is this necessary?
8426         (set-buffer gnus-summary-buffer)
8427
8428         (gnus-summary-goto-subject article)
8429         (when (eq action 'move)
8430           (gnus-summary-mark-article article gnus-canceled-mark))))
8431       (gnus-summary-remove-process-mark article))
8432     ;; Re-activate all groups that have been moved to.
8433     (save-excursion
8434       (set-buffer gnus-group-buffer)
8435       (let ((gnus-group-marked to-groups))
8436         (gnus-group-get-new-news-this-group nil t)))
8437
8438     (gnus-kill-buffer copy-buf)
8439     (gnus-summary-position-point)
8440     (gnus-set-mode-line 'summary)))
8441
8442 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8443   "Move the current article to a different newsgroup.
8444 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8445 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8446 re-spool using this method."
8447   (interactive "P")
8448   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8449
8450 (defun gnus-summary-crosspost-article (&optional n)
8451   "Crosspost the current article to some other group."
8452   (interactive "P")
8453   (gnus-summary-move-article n nil nil 'crosspost))
8454
8455 (defcustom gnus-summary-respool-default-method nil
8456   "Default method type for respooling an article.
8457 If nil, use to the current newsgroup method."
8458   :type 'symbol
8459   :group 'gnus-summary-mail)
8460
8461 (defun gnus-summary-respool-article (&optional n method)
8462   "Respool the current article.
8463 The article will be squeezed through the mail spooling process again,
8464 which means that it will be put in some mail newsgroup or other
8465 depending on `nnmail-split-methods'.
8466 If N is a positive number, respool the N next articles.
8467 If N is a negative number, respool the N previous articles.
8468 If N is nil and any articles have been marked with the process mark,
8469 respool those articles instead.
8470
8471 Respooling can be done both from mail groups and \"real\" newsgroups.
8472 In the former case, the articles in question will be moved from the
8473 current group into whatever groups they are destined to.  In the
8474 latter case, they will be copied into the relevant groups."
8475   (interactive
8476    (list current-prefix-arg
8477          (let* ((methods (gnus-methods-using 'respool))
8478                 (methname
8479                  (symbol-name (or gnus-summary-respool-default-method
8480                                   (car (gnus-find-method-for-group
8481                                         gnus-newsgroup-name)))))
8482                 (method
8483                  (gnus-completing-read-with-default
8484                   methname "What backend do you want to use when respooling?"
8485                   methods nil t nil 'gnus-mail-method-history))
8486                 ms)
8487            (cond
8488             ((zerop (length (setq ms (gnus-servers-using-backend
8489                                       (intern method)))))
8490              (list (intern method) ""))
8491             ((= 1 (length ms))
8492              (car ms))
8493             (t
8494              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8495                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8496                            ms-alist))))))))
8497   (unless method
8498     (error "No method given for respooling"))
8499   (if (assoc (symbol-name
8500               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8501              (gnus-methods-using 'respool))
8502       (gnus-summary-move-article n nil method)
8503     (gnus-summary-copy-article n nil method)))
8504
8505 (defun gnus-summary-import-article (file &optional edit)
8506   "Import an arbitrary file into a mail newsgroup."
8507   (interactive "fImport file: \nP")
8508   (let ((group gnus-newsgroup-name)
8509         (now (current-time))
8510         atts lines group-art)
8511     (unless (gnus-check-backend-function 'request-accept-article group)
8512       (error "%s does not support article importing" group))
8513     (or (file-readable-p file)
8514         (not (file-regular-p file))
8515         (error "Can't read %s" file))
8516     (save-excursion
8517       (set-buffer (gnus-get-buffer-create " *import file*"))
8518       (erase-buffer)
8519       (nnheader-insert-file-contents file)
8520       (goto-char (point-min))
8521       (if (nnheader-article-p)
8522           (save-restriction
8523             (goto-char (point-min))
8524             (search-forward "\n\n" nil t)
8525             (narrow-to-region (point-min) (1- (point)))
8526             (goto-char (point-min))
8527             (unless (re-search-forward "^date:" nil t)
8528               (goto-char (point-max))
8529               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8530        ;; This doesn't look like an article, so we fudge some headers.
8531         (setq atts (file-attributes file)
8532               lines (count-lines (point-min) (point-max)))
8533         (insert "From: " (read-string "From: ") "\n"
8534                 "Subject: " (read-string "Subject: ") "\n"
8535                 "Date: " (message-make-date (nth 5 atts)) "\n"
8536                 "Message-ID: " (message-make-message-id) "\n"
8537                 "Lines: " (int-to-string lines) "\n"
8538                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8539       (setq group-art (gnus-request-accept-article group nil t))
8540       (kill-buffer (current-buffer)))
8541     (setq gnus-newsgroup-active (gnus-activate-group group))
8542     (forward-line 1)
8543     (gnus-summary-goto-article (cdr group-art) nil t)
8544     (when edit
8545       (gnus-summary-edit-article))))
8546
8547 (defun gnus-summary-create-article ()
8548   "Create an article in a mail newsgroup."
8549   (interactive)
8550   (let ((group gnus-newsgroup-name)
8551         (now (current-time))
8552         group-art)
8553     (unless (gnus-check-backend-function 'request-accept-article group)
8554       (error "%s does not support article importing" group))
8555     (save-excursion
8556       (set-buffer (gnus-get-buffer-create " *import file*"))
8557       (erase-buffer)
8558       (goto-char (point-min))
8559       ;; This doesn't look like an article, so we fudge some headers.
8560       (insert "From: " (read-string "From: ") "\n"
8561               "Subject: " (read-string "Subject: ") "\n"
8562               "Date: " (message-make-date now) "\n"
8563               "Message-ID: " (message-make-message-id) "\n")
8564       (setq group-art (gnus-request-accept-article group nil t))
8565       (kill-buffer (current-buffer)))
8566     (setq gnus-newsgroup-active (gnus-activate-group group))
8567     (forward-line 1)
8568     (gnus-summary-goto-article (cdr group-art) nil t)
8569     (gnus-summary-edit-article)))
8570
8571 (defun gnus-summary-article-posted-p ()
8572   "Say whether the current (mail) article is available from news as well.
8573 This will be the case if the article has both been mailed and posted."
8574   (interactive)
8575   (let ((id (mail-header-references (gnus-summary-article-header)))
8576         (gnus-override-method (car (gnus-refer-article-methods))))
8577     (if (gnus-request-head id "")
8578         (gnus-message 2 "The current message was found on %s"
8579                       gnus-override-method)
8580       (gnus-message 2 "The current message couldn't be found on %s"
8581                     gnus-override-method)
8582       nil)))
8583
8584 (defun gnus-summary-expire-articles (&optional now)
8585   "Expire all articles that are marked as expirable in the current group."
8586   (interactive)
8587   (when (gnus-check-backend-function
8588          'request-expire-articles gnus-newsgroup-name)
8589     ;; This backend supports expiry.
8590     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8591            (expirable (if total
8592                           (progn
8593                             ;; We need to update the info for
8594                             ;; this group for `gnus-list-of-read-articles'
8595                             ;; to give us the right answer.
8596                             (gnus-run-hooks 'gnus-exit-group-hook)
8597                             (gnus-summary-update-info)
8598                             (gnus-list-of-read-articles gnus-newsgroup-name))
8599                         (setq gnus-newsgroup-expirable
8600                               (sort gnus-newsgroup-expirable '<))))
8601            (expiry-wait (if now 'immediate
8602                           (gnus-group-find-parameter
8603                            gnus-newsgroup-name 'expiry-wait)))
8604            (nnmail-expiry-target
8605             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8606                 nnmail-expiry-target))
8607            es)
8608       (when expirable
8609         ;; There are expirable articles in this group, so we run them
8610         ;; through the expiry process.
8611         (gnus-message 6 "Expiring articles...")
8612         (unless (gnus-check-group gnus-newsgroup-name)
8613           (error "Can't open server for %s" gnus-newsgroup-name))
8614         ;; The list of articles that weren't expired is returned.
8615         (save-excursion
8616           (if expiry-wait
8617               (let ((nnmail-expiry-wait-function nil)
8618                     (nnmail-expiry-wait expiry-wait))
8619                 (setq es (gnus-request-expire-articles
8620                           expirable gnus-newsgroup-name)))
8621             (setq es (gnus-request-expire-articles
8622                       expirable gnus-newsgroup-name)))
8623           (unless total
8624             (setq gnus-newsgroup-expirable es))
8625           ;; We go through the old list of expirable, and mark all
8626           ;; really expired articles as nonexistent.
8627           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8628             (let ((gnus-use-cache nil))
8629               (while expirable
8630                 (unless (memq (car expirable) es)
8631                   (when (gnus-data-find (car expirable))
8632                     (gnus-summary-mark-article
8633                      (car expirable) gnus-canceled-mark)))
8634                 (setq expirable (cdr expirable))))))
8635         (gnus-message 6 "Expiring articles...done")))))
8636
8637 (defun gnus-summary-expire-articles-now ()
8638   "Expunge all expirable articles in the current group.
8639 This means that *all* articles that are marked as expirable will be
8640 deleted forever, right now."
8641   (interactive)
8642   (or gnus-expert-user
8643       (gnus-yes-or-no-p
8644        "Are you really, really, really sure you want to delete all these messages? ")
8645       (error "Phew!"))
8646   (gnus-summary-expire-articles t))
8647
8648 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8649 (defun gnus-summary-delete-article (&optional n)
8650   "Delete the N next (mail) articles.
8651 This command actually deletes articles.  This is not a marking
8652 command.  The article will disappear forever from your life, never to
8653 return.
8654 If N is negative, delete backwards.
8655 If N is nil and articles have been marked with the process mark,
8656 delete these instead."
8657   (interactive "P")
8658   (unless (gnus-check-backend-function 'request-expire-articles
8659                                        gnus-newsgroup-name)
8660     (error "The current newsgroup does not support article deletion"))
8661   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8662     (error "Couldn't open server"))
8663   ;; Compute the list of articles to delete.
8664   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8665         not-deleted)
8666     (if (and gnus-novice-user
8667              (not (gnus-yes-or-no-p
8668                    (format "Do you really want to delete %s forever? "
8669                            (if (> (length articles) 1)
8670                                (format "these %s articles" (length articles))
8671                              "this article")))))
8672         ()
8673       ;; Delete the articles.
8674       (setq not-deleted (gnus-request-expire-articles
8675                          articles gnus-newsgroup-name 'force))
8676       (while articles
8677         (gnus-summary-remove-process-mark (car articles))
8678         ;; The backend might not have been able to delete the article
8679         ;; after all.
8680         (unless (memq (car articles) not-deleted)
8681           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8682         (setq articles (cdr articles)))
8683       (when not-deleted
8684         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8685     (gnus-summary-position-point)
8686     (gnus-set-mode-line 'summary)
8687     not-deleted))
8688
8689 (defun gnus-summary-edit-article (&optional arg)
8690   "Edit the current article.
8691 This will have permanent effect only in mail groups.
8692 If ARG is nil, edit the decoded articles.
8693 If ARG is 1, edit the raw articles.
8694 If ARG is 2, edit the raw articles even in read-only groups.
8695 If ARG is 3, edit the articles with the current handles.
8696 Otherwise, allow editing of articles even in read-only
8697 groups."
8698   (interactive "P")
8699   (let (force raw current-handles)
8700     (cond
8701      ((null arg))
8702      ((eq arg 1)
8703       (setq raw t))
8704      ((eq arg 2)
8705       (setq raw t
8706             force t))
8707      ((eq arg 3)
8708       (setq current-handles
8709             (and (gnus-buffer-live-p gnus-article-buffer)
8710                  (with-current-buffer gnus-article-buffer
8711                    (prog1
8712                        gnus-article-mime-handles
8713                      (setq gnus-article-mime-handles nil))))))
8714      (t
8715       (setq force t)))
8716     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8717       (error "Can't edit the raw article in group nndraft:drafts"))
8718     (save-excursion
8719       (set-buffer gnus-summary-buffer)
8720       (let ((mail-parse-charset gnus-newsgroup-charset)
8721             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8722         (gnus-set-global-variables)
8723         (when (and (not force)
8724                    (gnus-group-read-only-p))
8725           (error "The current newsgroup does not support article editing"))
8726         (gnus-summary-show-article t)
8727         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8728           (with-current-buffer gnus-article-buffer
8729             (mm-enable-multibyte)))
8730         (if (equal gnus-newsgroup-name "nndraft:drafts")
8731             (setq raw t))
8732         (gnus-article-edit-article
8733          (if raw 'ignore
8734            `(lambda ()
8735               (let ((mbl mml-buffer-list))
8736                 (setq mml-buffer-list nil)
8737                 (mime-to-mml ,'current-handles)
8738                 (let ((mbl1 mml-buffer-list))
8739                   (setq mml-buffer-list mbl)
8740                   (set (make-local-variable 'mml-buffer-list) mbl1))
8741                 (make-local-hook 'kill-buffer-hook)
8742                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8743          `(lambda (no-highlight)
8744             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8745                   (message-options message-options)
8746                   (message-options-set-recipient)
8747                   (mail-parse-ignored-charsets
8748                    ',gnus-newsgroup-ignored-charsets))
8749               ,(if (not raw) '(progn
8750                                 (mml-to-mime)
8751                                 (mml-destroy-buffers)
8752                                 (remove-hook 'kill-buffer-hook
8753                                              'mml-destroy-buffers t)
8754                                 (kill-local-variable 'mml-buffer-list)))
8755               (gnus-summary-edit-article-done
8756                ,(or (mail-header-references gnus-current-headers) "")
8757                ,(gnus-group-read-only-p)
8758                ,gnus-summary-buffer no-highlight))))))))
8759
8760 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8761
8762 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8763                                                  no-highlight)
8764   "Make edits to the current article permanent."
8765   (interactive)
8766   (save-excursion
8767    ;; The buffer restriction contains the entire article if it exists.
8768     (when (article-goto-body)
8769       (let ((lines (count-lines (point) (point-max)))
8770             (length (- (point-max) (point)))
8771             (case-fold-search t)
8772             (body (copy-marker (point))))
8773         (goto-char (point-min))
8774         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8775           (delete-region (match-beginning 1) (match-end 1))
8776           (insert (number-to-string length)))
8777         (goto-char (point-min))
8778         (when (re-search-forward
8779                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8780           (delete-region (match-beginning 1) (match-end 1))
8781           (insert (number-to-string length)))
8782         (goto-char (point-min))
8783         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8784           (delete-region (match-beginning 1) (match-end 1))
8785           (insert (number-to-string lines))))))
8786   ;; Replace the article.
8787   (let ((buf (current-buffer)))
8788     (with-temp-buffer
8789       (insert-buffer-substring buf)
8790
8791       (if (and (not read-only)
8792                (not (gnus-request-replace-article
8793                      (cdr gnus-article-current) (car gnus-article-current)
8794                      (current-buffer) t)))
8795           (error "Couldn't replace article")
8796         ;; Update the summary buffer.
8797         (if (and references
8798                  (equal (message-tokenize-header references " ")
8799                         (message-tokenize-header
8800                          (or (message-fetch-field "references") "") " ")))
8801             ;; We only have to update this line.
8802             (save-excursion
8803               (save-restriction
8804                 (message-narrow-to-head)
8805                 (let ((head (buffer-string))
8806                       header)
8807                   (with-temp-buffer
8808                     (insert (format "211 %d Article retrieved.\n"
8809                                     (cdr gnus-article-current)))
8810                     (insert head)
8811                     (insert ".\n")
8812                     (let ((nntp-server-buffer (current-buffer)))
8813                       (setq header (car (gnus-get-newsgroup-headers
8814                                          (save-excursion
8815                                            (set-buffer gnus-summary-buffer)
8816                                            gnus-newsgroup-dependencies)
8817                                          t))))
8818                     (save-excursion
8819                       (set-buffer gnus-summary-buffer)
8820                       (gnus-data-set-header
8821                        (gnus-data-find (cdr gnus-article-current))
8822                        header)
8823                       (gnus-summary-update-article-line
8824                        (cdr gnus-article-current) header)
8825                       (if (gnus-summary-goto-subject
8826                            (cdr gnus-article-current) nil t)
8827                           (gnus-summary-update-secondary-mark
8828                            (cdr gnus-article-current))))))))
8829           ;; Update threads.
8830           (set-buffer (or buffer gnus-summary-buffer))
8831           (gnus-summary-update-article (cdr gnus-article-current))
8832           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8833               (gnus-summary-update-secondary-mark
8834                (cdr gnus-article-current))))
8835         ;; Prettify the article buffer again.
8836         (unless no-highlight
8837           (save-excursion
8838             (set-buffer gnus-article-buffer)
8839             ;;;!!! Fix this -- article should be rehighlighted.
8840             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8841             (set-buffer gnus-original-article-buffer)
8842             (gnus-request-article
8843              (cdr gnus-article-current)
8844              (car gnus-article-current) (current-buffer))))
8845         ;; Prettify the summary buffer line.
8846         (when (gnus-visual-p 'summary-highlight 'highlight)
8847           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8848
8849 (defun gnus-summary-edit-wash (key)
8850   "Perform editing command KEY in the article buffer."
8851   (interactive
8852    (list
8853     (progn
8854       (message "%s" (concat (this-command-keys) "- "))
8855       (read-char))))
8856   (message "")
8857   (gnus-summary-edit-article)
8858   (execute-kbd-macro (concat (this-command-keys) key))
8859   (gnus-article-edit-done))
8860
8861 ;;; Respooling
8862
8863 (defun gnus-summary-respool-query (&optional silent trace)
8864   "Query where the respool algorithm would put this article."
8865   (interactive)
8866   (let (gnus-mark-article-hook)
8867     (gnus-summary-select-article)
8868     (save-excursion
8869       (set-buffer gnus-original-article-buffer)
8870       (save-restriction
8871         (message-narrow-to-head)
8872         (let ((groups (nnmail-article-group 'identity trace)))
8873           (unless silent
8874             (if groups
8875                 (message "This message would go to %s"
8876                          (mapconcat 'car groups ", "))
8877               (message "This message would go to no groups"))
8878             groups))))))
8879
8880 (defun gnus-summary-respool-trace ()
8881   "Trace where the respool algorithm would put this article.
8882 Display a buffer showing all fancy splitting patterns which matched."
8883   (interactive)
8884   (gnus-summary-respool-query nil t))
8885
8886 ;; Summary marking commands.
8887
8888 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8889   "Mark articles which has the same subject as read, and then select the next.
8890 If UNMARK is positive, remove any kind of mark.
8891 If UNMARK is negative, tick articles."
8892   (interactive "P")
8893   (when unmark
8894     (setq unmark (prefix-numeric-value unmark)))
8895   (let ((count
8896          (gnus-summary-mark-same-subject
8897           (gnus-summary-article-subject) unmark)))
8898     ;; Select next unread article.  If auto-select-same mode, should
8899     ;; select the first unread article.
8900     (gnus-summary-next-article t (and gnus-auto-select-same
8901                                       (gnus-summary-article-subject)))
8902     (gnus-message 7 "%d article%s marked as %s"
8903                   count (if (= count 1) " is" "s are")
8904                   (if unmark "unread" "read"))))
8905
8906 (defun gnus-summary-kill-same-subject (&optional unmark)
8907   "Mark articles which has the same subject as read.
8908 If UNMARK is positive, remove any kind of mark.
8909 If UNMARK is negative, tick articles."
8910   (interactive "P")
8911   (when unmark
8912     (setq unmark (prefix-numeric-value unmark)))
8913   (let ((count
8914          (gnus-summary-mark-same-subject
8915           (gnus-summary-article-subject) unmark)))
8916     ;; If marked as read, go to next unread subject.
8917     (when (null unmark)
8918       ;; Go to next unread subject.
8919       (gnus-summary-next-subject 1 t))
8920     (gnus-message 7 "%d articles are marked as %s"
8921                   count (if unmark "unread" "read"))))
8922
8923 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8924   "Mark articles with same SUBJECT as read, and return marked number.
8925 If optional argument UNMARK is positive, remove any kinds of marks.
8926 If optional argument UNMARK is negative, mark articles as unread instead."
8927   (let ((count 1))
8928     (save-excursion
8929       (cond
8930        ((null unmark)                   ; Mark as read.
8931         (while (and
8932                 (progn
8933                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8934                   (gnus-summary-show-thread) t)
8935                 (gnus-summary-find-subject subject))
8936           (setq count (1+ count))))
8937        ((> unmark 0)                    ; Tick.
8938         (while (and
8939                 (progn
8940                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8941                   (gnus-summary-show-thread) t)
8942                 (gnus-summary-find-subject subject))
8943           (setq count (1+ count))))
8944        (t                               ; Mark as unread.
8945         (while (and
8946                 (progn
8947                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8948                   (gnus-summary-show-thread) t)
8949                 (gnus-summary-find-subject subject))
8950           (setq count (1+ count)))))
8951       (gnus-set-mode-line 'summary)
8952       ;; Return the number of marked articles.
8953       count)))
8954
8955 (defun gnus-summary-mark-as-processable (n &optional unmark)
8956   "Set the process mark on the next N articles.
8957 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8958 the process mark instead.  The difference between N and the actual
8959 number of articles marked is returned."
8960   (interactive "P")
8961   (if (and (null n) (gnus-region-active-p))
8962       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8963     (setq n (prefix-numeric-value n))
8964     (let ((backward (< n 0))
8965           (n (abs n)))
8966       (while (and
8967               (> n 0)
8968               (if unmark
8969                   (gnus-summary-remove-process-mark
8970                    (gnus-summary-article-number))
8971                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8972               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8973         (setq n (1- n)))
8974       (when (/= 0 n)
8975         (gnus-message 7 "No more articles"))
8976       (gnus-summary-recenter)
8977       (gnus-summary-position-point)
8978       n)))
8979
8980 (defun gnus-summary-unmark-as-processable (n)
8981   "Remove the process mark from the next N articles.
8982 If N is negative, unmark backward instead.  The difference between N and
8983 the actual number of articles unmarked is returned."
8984   (interactive "P")
8985   (gnus-summary-mark-as-processable n t))
8986
8987 (defun gnus-summary-unmark-all-processable ()
8988   "Remove the process mark from all articles."
8989   (interactive)
8990   (save-excursion
8991     (while gnus-newsgroup-processable
8992       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8993   (gnus-summary-position-point))
8994
8995 (defun gnus-summary-add-mark (article type)
8996   "Mark ARTICLE with a mark of TYPE."
8997   (let ((vtype (car (assq type gnus-article-mark-lists)))
8998         var)
8999     (if (not vtype)
9000         (error "No such mark type: %s" type)
9001       (setq var (intern (format "gnus-newsgroup-%s" type)))
9002       (set var (cons article (symbol-value var)))
9003       (if (memq type '(processable cached replied forwarded recent saved))
9004           (gnus-summary-update-secondary-mark article)
9005         ;;; !!! This is bogus.  We should find out what primary
9006         ;;; !!! mark we want to set.
9007         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9008
9009 (defun gnus-summary-mark-as-expirable (n)
9010   "Mark N articles forward as expirable.
9011 If N is negative, mark backward instead.  The difference between N and
9012 the actual number of articles marked is returned."
9013   (interactive "p")
9014   (gnus-summary-mark-forward n gnus-expirable-mark))
9015
9016 (defun gnus-summary-mark-article-as-replied (article)
9017   "Mark ARTICLE as replied to and update the summary line.
9018 ARTICLE can also be a list of articles."
9019   (interactive (list (gnus-summary-article-number)))
9020   (let ((articles (if (listp article) article (list article))))
9021     (dolist (article articles)
9022       (push article gnus-newsgroup-replied)
9023       (let ((buffer-read-only nil))
9024         (when (gnus-summary-goto-subject article nil t)
9025           (gnus-summary-update-secondary-mark article))))))
9026
9027 (defun gnus-summary-mark-article-as-forwarded (article)
9028   "Mark ARTICLE as forwarded and update the summary line.
9029 ARTICLE can also be a list of articles."
9030   (let ((articles (if (listp article) article (list article))))
9031     (dolist (article articles)
9032       (push article gnus-newsgroup-forwarded)
9033       (let ((buffer-read-only nil))
9034         (when (gnus-summary-goto-subject article nil t)
9035           (gnus-summary-update-secondary-mark article))))))
9036
9037 (defun gnus-summary-set-bookmark (article)
9038   "Set a bookmark in current article."
9039   (interactive (list (gnus-summary-article-number)))
9040   (when (or (not (get-buffer gnus-article-buffer))
9041             (not gnus-current-article)
9042             (not gnus-article-current)
9043             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9044     (error "No current article selected"))
9045   ;; Remove old bookmark, if one exists.
9046   (let ((old (assq article gnus-newsgroup-bookmarks)))
9047     (when old
9048       (setq gnus-newsgroup-bookmarks
9049             (delq old gnus-newsgroup-bookmarks))))
9050   ;; Set the new bookmark, which is on the form
9051   ;; (article-number . line-number-in-body).
9052   (push
9053    (cons article
9054          (save-excursion
9055            (set-buffer gnus-article-buffer)
9056            (count-lines
9057             (min (point)
9058                  (save-excursion
9059                    (goto-char (point-min))
9060                    (search-forward "\n\n" nil t)
9061                    (point)))
9062             (point))))
9063    gnus-newsgroup-bookmarks)
9064   (gnus-message 6 "A bookmark has been added to the current article."))
9065
9066 (defun gnus-summary-remove-bookmark (article)
9067   "Remove the bookmark from the current article."
9068   (interactive (list (gnus-summary-article-number)))
9069   ;; Remove old bookmark, if one exists.
9070   (let ((old (assq article gnus-newsgroup-bookmarks)))
9071     (if old
9072         (progn
9073           (setq gnus-newsgroup-bookmarks
9074                 (delq old gnus-newsgroup-bookmarks))
9075           (gnus-message 6 "Removed bookmark."))
9076       (gnus-message 6 "No bookmark in current article."))))
9077
9078 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9079 (defun gnus-summary-mark-as-dormant (n)
9080   "Mark N articles forward as dormant.
9081 If N is negative, mark backward instead.  The difference between N and
9082 the actual number of articles marked is returned."
9083   (interactive "p")
9084   (gnus-summary-mark-forward n gnus-dormant-mark))
9085
9086 (defun gnus-summary-set-process-mark (article)
9087   "Set the process mark on ARTICLE and update the summary line."
9088   (setq gnus-newsgroup-processable
9089         (cons article
9090               (delq article gnus-newsgroup-processable)))
9091   (when (gnus-summary-goto-subject article)
9092     (gnus-summary-show-thread)
9093     (gnus-summary-goto-subject article)
9094     (gnus-summary-update-secondary-mark article)))
9095
9096 (defun gnus-summary-remove-process-mark (article)
9097   "Remove the process mark from ARTICLE and update the summary line."
9098   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9099   (when (gnus-summary-goto-subject article)
9100     (gnus-summary-show-thread)
9101     (gnus-summary-goto-subject article)
9102     (gnus-summary-update-secondary-mark article)))
9103
9104 (defun gnus-summary-set-saved-mark (article)
9105   "Set the process mark on ARTICLE and update the summary line."
9106   (push article gnus-newsgroup-saved)
9107   (when (gnus-summary-goto-subject article)
9108     (gnus-summary-update-secondary-mark article)))
9109
9110 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9111   "Mark N articles as read forwards.
9112 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9113 The difference between N and the actual number of articles marked is
9114 returned.
9115 Iff NO-EXPIRE, auto-expiry will be inhibited."
9116   (interactive "p")
9117   (gnus-summary-show-thread)
9118   (let ((backward (< n 0))
9119         (gnus-summary-goto-unread
9120          (and gnus-summary-goto-unread
9121               (not (eq gnus-summary-goto-unread 'never))
9122               (not (memq mark (list gnus-unread-mark
9123                                     gnus-ticked-mark gnus-dormant-mark)))))
9124         (n (abs n))
9125         (mark (or mark gnus-del-mark)))
9126     (while (and (> n 0)
9127                 (gnus-summary-mark-article nil mark no-expire)
9128                 (zerop (gnus-summary-next-subject
9129                         (if backward -1 1)
9130                         (and gnus-summary-goto-unread
9131                              (not (eq gnus-summary-goto-unread 'never)))
9132                         t)))
9133       (setq n (1- n)))
9134     (when (/= 0 n)
9135       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9136     (gnus-summary-recenter)
9137     (gnus-summary-position-point)
9138     (gnus-set-mode-line 'summary)
9139     n))
9140
9141 (defun gnus-summary-mark-article-as-read (mark)
9142   "Mark the current article quickly as read with MARK."
9143   (let ((article (gnus-summary-article-number)))
9144     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9145     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9146     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9147     (push (cons article mark) gnus-newsgroup-reads)
9148     ;; Possibly remove from cache, if that is used.
9149     (when gnus-use-cache
9150       (gnus-cache-enter-remove-article article))
9151     ;; Allow the backend to change the mark.
9152     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9153     ;; Check for auto-expiry.
9154     (when (and gnus-newsgroup-auto-expire
9155                (memq mark gnus-auto-expirable-marks))
9156       (setq mark gnus-expirable-mark)
9157       ;; Let the backend know about the mark change.
9158       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9159       (push article gnus-newsgroup-expirable))
9160     ;; Set the mark in the buffer.
9161     (gnus-summary-update-mark mark 'unread)
9162     t))
9163
9164 (defun gnus-summary-mark-article-as-unread (mark)
9165   "Mark the current article quickly as unread with MARK."
9166   (let* ((article (gnus-summary-article-number))
9167          (old-mark (gnus-summary-article-mark article)))
9168     ;; Allow the backend to change the mark.
9169     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9170     (if (eq mark old-mark)
9171         t
9172       (if (<= article 0)
9173           (progn
9174             (gnus-error 1 "Can't mark negative article numbers")
9175             nil)
9176         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9177         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9178         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9179         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9180         (cond ((= mark gnus-ticked-mark)
9181                (push article gnus-newsgroup-marked))
9182               ((= mark gnus-dormant-mark)
9183                (push article gnus-newsgroup-dormant))
9184               (t
9185                (push article gnus-newsgroup-unreads)))
9186         (gnus-pull article gnus-newsgroup-reads)
9187
9188         ;; See whether the article is to be put in the cache.
9189         (and gnus-use-cache
9190              (vectorp (gnus-summary-article-header article))
9191              (save-excursion
9192                (gnus-cache-possibly-enter-article
9193                 gnus-newsgroup-name article
9194                 (= mark gnus-ticked-mark)
9195                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9196
9197         ;; Fix the mark.
9198         (gnus-summary-update-mark mark 'unread)
9199         t))))
9200
9201 (defun gnus-summary-mark-article (&optional article mark no-expire)
9202   "Mark ARTICLE with MARK.  MARK can be any character.
9203 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9204 `??' (dormant) and `?E' (expirable).
9205 If MARK is nil, then the default character `?r' is used.
9206 If ARTICLE is nil, then the article on the current line will be
9207 marked.
9208 Iff NO-EXPIRE, auto-expiry will be inhibited."
9209   ;; The mark might be a string.
9210   (when (stringp mark)
9211     (setq mark (aref mark 0)))
9212   ;; If no mark is given, then we check auto-expiring.
9213   (when (null mark)
9214     (setq mark gnus-del-mark))
9215   (when (and (not no-expire)
9216              gnus-newsgroup-auto-expire
9217              (memq mark gnus-auto-expirable-marks))
9218     (setq mark gnus-expirable-mark))
9219   (let ((article (or article (gnus-summary-article-number)))
9220         (old-mark (gnus-summary-article-mark article)))
9221     ;; Allow the backend to change the mark.
9222     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9223     (if (eq mark old-mark)
9224         t
9225       (unless article
9226         (error "No article on current line"))
9227       (if (not (if (or (= mark gnus-unread-mark)
9228                        (= mark gnus-ticked-mark)
9229                        (= mark gnus-dormant-mark))
9230                    (gnus-mark-article-as-unread article mark)
9231                  (gnus-mark-article-as-read article mark)))
9232           t
9233         ;; See whether the article is to be put in the cache.
9234         (and gnus-use-cache
9235              (not (= mark gnus-canceled-mark))
9236              (vectorp (gnus-summary-article-header article))
9237              (save-excursion
9238                (gnus-cache-possibly-enter-article
9239                 gnus-newsgroup-name article
9240                 (= mark gnus-ticked-mark)
9241                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9242
9243         (when (gnus-summary-goto-subject article nil t)
9244           (let ((buffer-read-only nil))
9245             (gnus-summary-show-thread)
9246             ;; Fix the mark.
9247             (gnus-summary-update-mark mark 'unread)
9248             t))))))
9249
9250 (defun gnus-summary-update-secondary-mark (article)
9251   "Update the secondary (read, process, cache) mark."
9252   (gnus-summary-update-mark
9253    (cond ((memq article gnus-newsgroup-processable)
9254           gnus-process-mark)
9255          ((memq article gnus-newsgroup-cached)
9256           gnus-cached-mark)
9257          ((memq article gnus-newsgroup-replied)
9258           gnus-replied-mark)
9259          ((memq article gnus-newsgroup-forwarded)
9260           gnus-forwarded-mark)
9261          ((memq article gnus-newsgroup-saved)
9262           gnus-saved-mark)
9263          ((memq article gnus-newsgroup-recent)
9264           gnus-recent-mark)
9265          ((memq article gnus-newsgroup-unseen)
9266           gnus-unseen-mark)
9267          (t gnus-no-mark))
9268    'replied)
9269   (when (gnus-visual-p 'summary-highlight 'highlight)
9270     (gnus-run-hooks 'gnus-summary-update-hook))
9271   t)
9272
9273 (defun gnus-summary-update-mark (mark type)
9274   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9275         (buffer-read-only nil))
9276     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9277     (when forward
9278       (when (looking-at "\r")
9279         (incf forward))
9280       (when (<= (+ forward (point)) (point-max))
9281         ;; Go to the right position on the line.
9282         (goto-char (+ forward (point)))
9283         ;; Replace the old mark with the new mark.
9284         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9285         ;; Optionally update the marks by some user rule.
9286         (when (eq type 'unread)
9287           (gnus-data-set-mark
9288            (gnus-data-find (gnus-summary-article-number)) mark)
9289           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9290
9291 (defun gnus-mark-article-as-read (article &optional mark)
9292   "Enter ARTICLE in the pertinent lists and remove it from others."
9293   ;; Make the article expirable.
9294   (let ((mark (or mark gnus-del-mark)))
9295     (if (= mark gnus-expirable-mark)
9296         (push article gnus-newsgroup-expirable)
9297       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9298     ;; Remove from unread and marked lists.
9299     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9300     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9301     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9302     (push (cons article mark) gnus-newsgroup-reads)
9303     ;; Possibly remove from cache, if that is used.
9304     (when gnus-use-cache
9305       (gnus-cache-enter-remove-article article))
9306     t))
9307
9308 (defun gnus-mark-article-as-unread (article &optional mark)
9309   "Enter ARTICLE in the pertinent lists and remove it from others."
9310   (let ((mark (or mark gnus-ticked-mark)))
9311     (if (<= article 0)
9312         (progn
9313           (gnus-error 1 "Can't mark negative article numbers")
9314           nil)
9315       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9316             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9317             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9318             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9319
9320       ;; Unsuppress duplicates?
9321       (when gnus-suppress-duplicates
9322         (gnus-dup-unsuppress-article article))
9323
9324       (cond ((= mark gnus-ticked-mark)
9325              (push article gnus-newsgroup-marked))
9326             ((= mark gnus-dormant-mark)
9327              (push article gnus-newsgroup-dormant))
9328             (t
9329              (push article gnus-newsgroup-unreads)))
9330       (gnus-pull article gnus-newsgroup-reads)
9331       t)))
9332
9333 (defalias 'gnus-summary-mark-as-unread-forward
9334   'gnus-summary-tick-article-forward)
9335 (make-obsolete 'gnus-summary-mark-as-unread-forward
9336                'gnus-summary-tick-article-forward)
9337 (defun gnus-summary-tick-article-forward (n)
9338   "Tick N articles forwards.
9339 If N is negative, tick backwards instead.
9340 The difference between N and the number of articles ticked is returned."
9341   (interactive "p")
9342   (gnus-summary-mark-forward n gnus-ticked-mark))
9343
9344 (defalias 'gnus-summary-mark-as-unread-backward
9345   'gnus-summary-tick-article-backward)
9346 (make-obsolete 'gnus-summary-mark-as-unread-backward
9347                'gnus-summary-tick-article-backward)
9348 (defun gnus-summary-tick-article-backward (n)
9349   "Tick N articles backwards.
9350 The difference between N and the number of articles ticked is returned."
9351   (interactive "p")
9352   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9353
9354 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9355 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9356 (defun gnus-summary-tick-article (&optional article clear-mark)
9357   "Mark current article as unread.
9358 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9359 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9360   (interactive)
9361   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9362                                        gnus-ticked-mark)))
9363
9364 (defun gnus-summary-mark-as-read-forward (n)
9365   "Mark N articles as read forwards.
9366 If N is negative, mark backwards instead.
9367 The difference between N and the actual number of articles marked is
9368 returned."
9369   (interactive "p")
9370   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9371
9372 (defun gnus-summary-mark-as-read-backward (n)
9373   "Mark the N articles as read backwards.
9374 The difference between N and the actual number of articles marked is
9375 returned."
9376   (interactive "p")
9377   (gnus-summary-mark-forward
9378    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9379
9380 (defun gnus-summary-mark-as-read (&optional article mark)
9381   "Mark current article as read.
9382 ARTICLE specifies the article to be marked as read.
9383 MARK specifies a string to be inserted at the beginning of the line."
9384   (gnus-summary-mark-article article mark))
9385
9386 (defun gnus-summary-clear-mark-forward (n)
9387   "Clear marks from N articles forward.
9388 If N is negative, clear backward instead.
9389 The difference between N and the number of marks cleared is returned."
9390   (interactive "p")
9391   (gnus-summary-mark-forward n gnus-unread-mark))
9392
9393 (defun gnus-summary-clear-mark-backward (n)
9394   "Clear marks from N articles backward.
9395 The difference between N and the number of marks cleared is returned."
9396   (interactive "p")
9397   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9398
9399 (defun gnus-summary-mark-unread-as-read ()
9400   "Intended to be used by `gnus-summary-mark-article-hook'."
9401   (when (memq gnus-current-article gnus-newsgroup-unreads)
9402     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9403
9404 (defun gnus-summary-mark-read-and-unread-as-read ()
9405   "Intended to be used by `gnus-summary-mark-article-hook'."
9406   (let ((mark (gnus-summary-article-mark)))
9407     (when (or (gnus-unread-mark-p mark)
9408               (gnus-read-mark-p mark))
9409       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9410
9411 (defun gnus-summary-mark-unread-as-ticked ()
9412   "Intended to be used by `gnus-summary-mark-article-hook'."
9413   (when (memq gnus-current-article gnus-newsgroup-unreads)
9414     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9415
9416 (defun gnus-summary-mark-region-as-read (point mark all)
9417   "Mark all unread articles between point and mark as read.
9418 If given a prefix, mark all articles between point and mark as read,
9419 even ticked and dormant ones."
9420   (interactive "r\nP")
9421   (save-excursion
9422     (let (article)
9423       (goto-char point)
9424       (beginning-of-line)
9425       (while (and
9426               (< (point) mark)
9427               (progn
9428                 (when (or all
9429                           (memq (setq article (gnus-summary-article-number))
9430                                 gnus-newsgroup-unreads))
9431                   (gnus-summary-mark-article article gnus-del-mark))
9432                 t)
9433               (gnus-summary-find-next))))))
9434
9435 (defun gnus-summary-mark-below (score mark)
9436   "Mark articles with score less than SCORE with MARK."
9437   (interactive "P\ncMark: ")
9438   (setq score (if score
9439                   (prefix-numeric-value score)
9440                 (or gnus-summary-default-score 0)))
9441   (save-excursion
9442     (set-buffer gnus-summary-buffer)
9443     (goto-char (point-min))
9444     (while
9445         (progn
9446           (and (< (gnus-summary-article-score) score)
9447                (gnus-summary-mark-article nil mark))
9448           (gnus-summary-find-next)))))
9449
9450 (defun gnus-summary-kill-below (&optional score)
9451   "Mark articles with score below SCORE as read."
9452   (interactive "P")
9453   (gnus-summary-mark-below score gnus-killed-mark))
9454
9455 (defun gnus-summary-clear-above (&optional score)
9456   "Clear all marks from articles with score above SCORE."
9457   (interactive "P")
9458   (gnus-summary-mark-above score gnus-unread-mark))
9459
9460 (defun gnus-summary-tick-above (&optional score)
9461   "Tick all articles with score above SCORE."
9462   (interactive "P")
9463   (gnus-summary-mark-above score gnus-ticked-mark))
9464
9465 (defun gnus-summary-mark-above (score mark)
9466   "Mark articles with score over SCORE with MARK."
9467   (interactive "P\ncMark: ")
9468   (setq score (if score
9469                   (prefix-numeric-value score)
9470                 (or gnus-summary-default-score 0)))
9471   (save-excursion
9472     (set-buffer gnus-summary-buffer)
9473     (goto-char (point-min))
9474     (while (and (progn
9475                   (when (> (gnus-summary-article-score) score)
9476                     (gnus-summary-mark-article nil mark))
9477                   t)
9478                 (gnus-summary-find-next)))))
9479
9480 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9481 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9482 (defun gnus-summary-limit-include-expunged (&optional no-error)
9483   "Display all the hidden articles that were expunged for low scores."
9484   (interactive)
9485   (let ((buffer-read-only nil))
9486     (let ((scored gnus-newsgroup-scored)
9487           headers h)
9488       (while scored
9489         (unless (gnus-summary-article-header (caar scored))
9490           (and (setq h (gnus-number-to-header (caar scored)))
9491                (< (cdar scored) gnus-summary-expunge-below)
9492                (push h headers)))
9493         (setq scored (cdr scored)))
9494       (if (not headers)
9495           (when (not no-error)
9496             (error "No expunged articles hidden"))
9497         (goto-char (point-min))
9498         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9499         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9500         (mapcar (lambda (x) (push (mail-header-number x)
9501                                   gnus-newsgroup-limit))
9502                 headers)
9503         (gnus-summary-prepare-unthreaded (nreverse headers))
9504         (goto-char (point-min))
9505         (gnus-summary-position-point)
9506         t))))
9507
9508 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9509   "Mark all unread articles in this newsgroup as read.
9510 If prefix argument ALL is non-nil, ticked and dormant articles will
9511 also be marked as read.
9512 If QUIETLY is non-nil, no questions will be asked.
9513 If TO-HERE is non-nil, it should be a point in the buffer.  All
9514 articles before (after, if REVERSE is set) this point will be marked as read.
9515 Note that this function will only catch up the unread article
9516 in the current summary buffer limitation.
9517 The number of articles marked as read is returned."
9518   (interactive "P")
9519   (prog1
9520       (save-excursion
9521         (when (or quietly
9522                   (not gnus-interactive-catchup) ;Without confirmation?
9523                   gnus-expert-user
9524                   (gnus-y-or-n-p
9525                    (if all
9526                        "Mark absolutely all articles as read? "
9527                      "Mark all unread articles as read? ")))
9528           (if (and not-mark
9529                    (not gnus-newsgroup-adaptive)
9530                    (not gnus-newsgroup-auto-expire)
9531                    (not gnus-suppress-duplicates)
9532                    (or (not gnus-use-cache)
9533                        (eq gnus-use-cache 'passive)))
9534               (progn
9535                 (when all
9536                   (setq gnus-newsgroup-marked nil
9537                         gnus-newsgroup-dormant nil))
9538                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9539             ;; We actually mark all articles as canceled, which we
9540             ;; have to do when using auto-expiry or adaptive scoring.
9541             (gnus-summary-show-all-threads)
9542             (if (and to-here reverse)
9543                 (progn
9544                   (goto-char to-here)
9545                   (while (and
9546                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9547                           (gnus-summary-find-next (not all) nil nil t))))
9548               (when (gnus-summary-first-subject (not all) t)
9549                 (while (and
9550                         (if to-here (< (point) to-here) t)
9551                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9552                         (gnus-summary-find-next (not all) nil nil t)))))
9553             (gnus-set-mode-line 'summary))
9554           t))
9555     (gnus-summary-position-point)))
9556
9557 (defun gnus-summary-catchup-to-here (&optional all)
9558   "Mark all unticked articles before the current one as read.
9559 If ALL is non-nil, also mark ticked and dormant articles as read."
9560   (interactive "P")
9561   (save-excursion
9562     (gnus-save-hidden-threads
9563       (let ((beg (point)))
9564         ;; We check that there are unread articles.
9565         (when (or all (gnus-summary-find-prev))
9566           (gnus-summary-catchup all t beg)))))
9567   (gnus-summary-position-point))
9568
9569 (defun gnus-summary-catchup-from-here (&optional all)
9570   "Mark all unticked articles after the current one as read.
9571 If ALL is non-nil, also mark ticked and dormant articles as read."
9572   (interactive "P")
9573   (save-excursion
9574     (gnus-save-hidden-threads
9575       (let ((beg (point)))
9576         ;; We check that there are unread articles.
9577         (when (or all (gnus-summary-find-next))
9578           (gnus-summary-catchup all t beg nil t)))))
9579
9580   (gnus-summary-position-point))
9581 (defun gnus-summary-catchup-all (&optional quietly)
9582   "Mark all articles in this newsgroup as read."
9583   (interactive "P")
9584   (gnus-summary-catchup t quietly))
9585
9586 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9587   "Mark all unread articles in this group as read, then exit.
9588 If prefix argument ALL is non-nil, all articles are marked as read.
9589 If QUIETLY is non-nil, no questions will be asked."
9590   (interactive "P")
9591   (when (gnus-summary-catchup all quietly nil 'fast)
9592     ;; Select next newsgroup or exit.
9593     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9594              (eq gnus-auto-select-next 'quietly))
9595         (gnus-summary-next-group nil)
9596       (gnus-summary-exit))))
9597
9598 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9599   "Mark all articles in this newsgroup as read, and then exit."
9600   (interactive "P")
9601   (gnus-summary-catchup-and-exit t quietly))
9602
9603 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9604   "Mark all articles in this group as read and select the next group.
9605 If given a prefix, mark all articles, unread as well as ticked, as
9606 read."
9607   (interactive "P")
9608   (save-excursion
9609     (gnus-summary-catchup all))
9610   (gnus-summary-next-group))
9611
9612 ;;;
9613 ;;; with article
9614 ;;;
9615
9616 (defmacro gnus-with-article (article &rest forms)
9617   "Select ARTICLE and perform FORMS in the original article buffer.
9618 Then replace the article with the result."
9619   `(progn
9620      ;; We don't want the article to be marked as read.
9621      (let (gnus-mark-article-hook)
9622        (gnus-summary-select-article t t nil ,article))
9623      (set-buffer gnus-original-article-buffer)
9624      ,@forms
9625      (if (not (gnus-check-backend-function
9626                'request-replace-article (car gnus-article-current)))
9627          (gnus-message 5 "Read-only group; not replacing")
9628        (unless (gnus-request-replace-article
9629                 ,article (car gnus-article-current)
9630                 (current-buffer) t)
9631          (error "Couldn't replace article")))
9632      ;; The cache and backlog have to be flushed somewhat.
9633      (when gnus-keep-backlog
9634        (gnus-backlog-remove-article
9635         (car gnus-article-current) (cdr gnus-article-current)))
9636      (when gnus-use-cache
9637        (gnus-cache-update-article
9638         (car gnus-article-current) (cdr gnus-article-current)))))
9639
9640 (put 'gnus-with-article 'lisp-indent-function 1)
9641 (put 'gnus-with-article 'edebug-form-spec '(form body))
9642
9643 ;; Thread-based commands.
9644
9645 (defun gnus-summary-articles-in-thread (&optional article)
9646   "Return a list of all articles in the current thread.
9647 If ARTICLE is non-nil, return all articles in the thread that starts
9648 with that article."
9649   (let* ((article (or article (gnus-summary-article-number)))
9650          (data (gnus-data-find-list article))
9651          (top-level (gnus-data-level (car data)))
9652          (top-subject
9653           (cond ((null gnus-thread-operation-ignore-subject)
9654                  (gnus-simplify-subject-re
9655                   (mail-header-subject (gnus-data-header (car data)))))
9656                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9657                  (gnus-simplify-subject-fuzzy
9658                   (mail-header-subject (gnus-data-header (car data)))))
9659                 (t nil)))
9660          (end-point (save-excursion
9661                       (if (gnus-summary-go-to-next-thread)
9662                           (point) (point-max))))
9663          articles)
9664     (while (and data
9665                 (< (gnus-data-pos (car data)) end-point))
9666       (when (or (not top-subject)
9667                 (string= top-subject
9668                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9669                              (gnus-simplify-subject-fuzzy
9670                               (mail-header-subject
9671                                (gnus-data-header (car data))))
9672                            (gnus-simplify-subject-re
9673                             (mail-header-subject
9674                              (gnus-data-header (car data)))))))
9675         (push (gnus-data-number (car data)) articles))
9676       (unless (and (setq data (cdr data))
9677                    (> (gnus-data-level (car data)) top-level))
9678         (setq data nil)))
9679     ;; Return the list of articles.
9680     (nreverse articles)))
9681
9682 (defun gnus-summary-rethread-current ()
9683   "Rethread the thread the current article is part of."
9684   (interactive)
9685   (let* ((gnus-show-threads t)
9686          (article (gnus-summary-article-number))
9687          (id (mail-header-id (gnus-summary-article-header)))
9688          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9689     (unless id
9690       (error "No article on the current line"))
9691     (gnus-rebuild-thread id)
9692     (gnus-summary-goto-subject article)))
9693
9694 (defun gnus-summary-reparent-thread ()
9695   "Make the current article child of the marked (or previous) article.
9696
9697 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9698 is non-nil or the Subject: of both articles are the same."
9699   (interactive)
9700   (unless (not (gnus-group-read-only-p))
9701     (error "The current newsgroup does not support article editing"))
9702   (unless (<= (length gnus-newsgroup-processable) 1)
9703     (error "No more than one article may be marked"))
9704   (save-window-excursion
9705     (let ((gnus-article-buffer " *reparent*")
9706           (current-article (gnus-summary-article-number))
9707           ;; First grab the marked article, otherwise one line up.
9708           (parent-article (if (not (null gnus-newsgroup-processable))
9709                               (car gnus-newsgroup-processable)
9710                             (save-excursion
9711                               (if (eq (forward-line -1) 0)
9712                                   (gnus-summary-article-number)
9713                                 (error "Beginning of summary buffer"))))))
9714       (unless (not (eq current-article parent-article))
9715         (error "An article may not be self-referential"))
9716       (let ((message-id (mail-header-id
9717                          (gnus-summary-article-header parent-article))))
9718         (unless (and message-id (not (equal message-id "")))
9719           (error "No message-id in desired parent"))
9720         (gnus-with-article current-article
9721           (save-restriction
9722             (goto-char (point-min))
9723             (message-narrow-to-head)
9724             (if (re-search-forward "^References: " nil t)
9725                 (progn
9726                   (re-search-forward "^[^ \t]" nil t)
9727                   (forward-line -1)
9728                   (end-of-line)
9729                   (insert " " message-id))
9730               (insert "References: " message-id "\n"))))
9731         (set-buffer gnus-summary-buffer)
9732         (gnus-summary-unmark-all-processable)
9733         (gnus-summary-update-article current-article)
9734         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9735             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9736         (gnus-summary-rethread-current)
9737         (gnus-message 3 "Article %d is now the child of article %d"
9738                       current-article parent-article)))))
9739
9740 (defun gnus-summary-toggle-threads (&optional arg)
9741   "Toggle showing conversation threads.
9742 If ARG is positive number, turn showing conversation threads on."
9743   (interactive "P")
9744   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9745     (setq gnus-show-threads
9746           (if (null arg) (not gnus-show-threads)
9747             (> (prefix-numeric-value arg) 0)))
9748     (gnus-summary-prepare)
9749     (gnus-summary-goto-subject current)
9750     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9751     (gnus-summary-position-point)))
9752
9753 (defun gnus-summary-show-all-threads ()
9754   "Show all threads."
9755   (interactive)
9756   (save-excursion
9757     (let ((buffer-read-only nil))
9758       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9759   (gnus-summary-position-point))
9760
9761 (defun gnus-summary-show-thread ()
9762   "Show thread subtrees.
9763 Returns nil if no thread was there to be shown."
9764   (interactive)
9765   (let ((buffer-read-only nil)
9766         (orig (point))
9767         ;; first goto end then to beg, to have point at beg after let
9768         (end (progn (end-of-line) (point)))
9769         (beg (progn (beginning-of-line) (point))))
9770     (prog1
9771         ;; Any hidden lines here?
9772         (search-forward "\r" end t)
9773       (subst-char-in-region beg end ?\^M ?\n t)
9774       (goto-char orig)
9775       (gnus-summary-position-point))))
9776
9777 (defun gnus-summary-maybe-hide-threads ()
9778   "If requested, hide the threads that should be hidden."
9779   (when (and gnus-show-threads
9780              gnus-thread-hide-subtree)
9781     (gnus-summary-hide-all-threads
9782      (if (or (consp gnus-thread-hide-subtree)
9783              (gnus-functionp gnus-thread-hide-subtree))
9784          (gnus-make-predicate gnus-thread-hide-subtree)
9785        nil))))
9786
9787 ;;; Hiding predicates.
9788
9789 (defun gnus-article-unread-p (header)
9790   (memq (mail-header-number header) gnus-newsgroup-unreads))
9791
9792 (defun gnus-article-unseen-p (header)
9793   (memq (mail-header-number header) gnus-newsgroup-unseen))
9794
9795 (defun gnus-map-articles (predicate articles)
9796   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9797   (apply 'gnus-or (mapcar predicate
9798                           (mapcar 'gnus-summary-article-header articles))))
9799
9800 (defun gnus-summary-hide-all-threads (&optional predicate)
9801   "Hide all thread subtrees.
9802 If PREDICATE is supplied, threads that satisfy this predicate
9803 will not be hidden."
9804   (interactive)
9805   (save-excursion
9806     (goto-char (point-min))
9807     (let ((end nil))
9808       (while (not end)
9809         (when (or (not predicate)
9810                   (gnus-map-articles
9811                    predicate (gnus-summary-article-children)))
9812             (gnus-summary-hide-thread))
9813         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9814   (gnus-summary-position-point))
9815
9816 (defun gnus-summary-hide-thread ()
9817   "Hide thread subtrees.
9818 If PREDICATE is supplied, threads that satisfy this predicate
9819 will not be hidden.
9820 Returns nil if no threads were there to be hidden."
9821   (interactive)
9822   (let ((buffer-read-only nil)
9823         (start (point))
9824         (article (gnus-summary-article-number)))
9825     (goto-char start)
9826     ;; Go forward until either the buffer ends or the subthread
9827     ;; ends.
9828     (when (and (not (eobp))
9829                (or (zerop (gnus-summary-next-thread 1 t))
9830                    (goto-char (point-max))))
9831       (prog1
9832           (if (and (> (point) start)
9833                    (search-backward "\n" start t))
9834               (progn
9835                 (subst-char-in-region start (point) ?\n ?\^M)
9836                 (gnus-summary-goto-subject article))
9837             (goto-char start)
9838             nil)))))
9839
9840 (defun gnus-summary-go-to-next-thread (&optional previous)
9841   "Go to the same level (or less) next thread.
9842 If PREVIOUS is non-nil, go to previous thread instead.
9843 Return the article number moved to, or nil if moving was impossible."
9844   (let ((level (gnus-summary-thread-level))
9845         (way (if previous -1 1))
9846         (beg (point)))
9847     (forward-line way)
9848     (while (and (not (eobp))
9849                 (< level (gnus-summary-thread-level)))
9850       (forward-line way))
9851     (if (eobp)
9852         (progn
9853           (goto-char beg)
9854           nil)
9855       (setq beg (point))
9856       (prog1
9857           (gnus-summary-article-number)
9858         (goto-char beg)))))
9859
9860 (defun gnus-summary-next-thread (n &optional silent)
9861   "Go to the same level next N'th thread.
9862 If N is negative, search backward instead.
9863 Returns the difference between N and the number of skips actually
9864 done.
9865
9866 If SILENT, don't output messages."
9867   (interactive "p")
9868   (let ((backward (< n 0))
9869         (n (abs n)))
9870     (while (and (> n 0)
9871                 (gnus-summary-go-to-next-thread backward))
9872       (decf n))
9873     (unless silent
9874       (gnus-summary-position-point))
9875     (when (and (not silent) (/= 0 n))
9876       (gnus-message 7 "No more threads"))
9877     n))
9878
9879 (defun gnus-summary-prev-thread (n)
9880   "Go to the same level previous N'th thread.
9881 Returns the difference between N and the number of skips actually
9882 done."
9883   (interactive "p")
9884   (gnus-summary-next-thread (- n)))
9885
9886 (defun gnus-summary-go-down-thread ()
9887   "Go down one level in the current thread."
9888   (let ((children (gnus-summary-article-children)))
9889     (when children
9890       (gnus-summary-goto-subject (car children)))))
9891
9892 (defun gnus-summary-go-up-thread ()
9893   "Go up one level in the current thread."
9894   (let ((parent (gnus-summary-article-parent)))
9895     (when parent
9896       (gnus-summary-goto-subject parent))))
9897
9898 (defun gnus-summary-down-thread (n)
9899   "Go down thread N steps.
9900 If N is negative, go up instead.
9901 Returns the difference between N and how many steps down that were
9902 taken."
9903   (interactive "p")
9904   (let ((up (< n 0))
9905         (n (abs n)))
9906     (while (and (> n 0)
9907                 (if up (gnus-summary-go-up-thread)
9908                   (gnus-summary-go-down-thread)))
9909       (setq n (1- n)))
9910     (gnus-summary-position-point)
9911     (when (/= 0 n)
9912       (gnus-message 7 "Can't go further"))
9913     n))
9914
9915 (defun gnus-summary-up-thread (n)
9916   "Go up thread N steps.
9917 If N is negative, go down instead.
9918 Returns the difference between N and how many steps down that were
9919 taken."
9920   (interactive "p")
9921   (gnus-summary-down-thread (- n)))
9922
9923 (defun gnus-summary-top-thread ()
9924   "Go to the top of the thread."
9925   (interactive)
9926   (while (gnus-summary-go-up-thread))
9927   (gnus-summary-article-number))
9928
9929 (defun gnus-summary-kill-thread (&optional unmark)
9930   "Mark articles under current thread as read.
9931 If the prefix argument is positive, remove any kinds of marks.
9932 If the prefix argument is negative, tick articles instead."
9933   (interactive "P")
9934   (when unmark
9935     (setq unmark (prefix-numeric-value unmark)))
9936   (let ((articles (gnus-summary-articles-in-thread)))
9937     (save-excursion
9938       ;; Expand the thread.
9939       (gnus-summary-show-thread)
9940       ;; Mark all the articles.
9941       (while articles
9942         (gnus-summary-goto-subject (car articles))
9943         (cond ((null unmark)
9944                (gnus-summary-mark-article-as-read gnus-killed-mark))
9945               ((> unmark 0)
9946                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9947               (t
9948                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9949         (setq articles (cdr articles))))
9950     ;; Hide killed subtrees.
9951     (and (null unmark)
9952          gnus-thread-hide-killed
9953          (gnus-summary-hide-thread))
9954     ;; If marked as read, go to next unread subject.
9955     (when (null unmark)
9956       ;; Go to next unread subject.
9957       (gnus-summary-next-subject 1 t)))
9958   (gnus-set-mode-line 'summary))
9959
9960 ;; Summary sorting commands
9961
9962 (defun gnus-summary-sort-by-number (&optional reverse)
9963   "Sort the summary buffer by article number.
9964 Argument REVERSE means reverse order."
9965   (interactive "P")
9966   (gnus-summary-sort 'number reverse))
9967
9968 (defun gnus-summary-sort-by-author (&optional reverse)
9969   "Sort the summary buffer by author name alphabetically.
9970 If `case-fold-search' is non-nil, case of letters is ignored.
9971 Argument REVERSE means reverse order."
9972   (interactive "P")
9973   (gnus-summary-sort 'author reverse))
9974
9975 (defun gnus-summary-sort-by-subject (&optional reverse)
9976   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9977 If `case-fold-search' is non-nil, case of letters is ignored.
9978 Argument REVERSE means reverse order."
9979   (interactive "P")
9980   (gnus-summary-sort 'subject reverse))
9981
9982 (defun gnus-summary-sort-by-date (&optional reverse)
9983   "Sort the summary buffer by date.
9984 Argument REVERSE means reverse order."
9985   (interactive "P")
9986   (gnus-summary-sort 'date reverse))
9987
9988 (defun gnus-summary-sort-by-score (&optional reverse)
9989   "Sort the summary buffer by score.
9990 Argument REVERSE means reverse order."
9991   (interactive "P")
9992   (gnus-summary-sort 'score reverse))
9993
9994 (defun gnus-summary-sort-by-lines (&optional reverse)
9995   "Sort the summary buffer by the number of lines.
9996 Argument REVERSE means reverse order."
9997   (interactive "P")
9998   (gnus-summary-sort 'lines reverse))
9999
10000 (defun gnus-summary-sort-by-chars (&optional reverse)
10001   "Sort the summary buffer by article length.
10002 Argument REVERSE means reverse order."
10003   (interactive "P")
10004   (gnus-summary-sort 'chars reverse))
10005
10006 (defun gnus-summary-sort-by-original (&optional reverse)
10007   "Sort the summary buffer using the default sorting method.
10008 Argument REVERSE means reverse order."
10009   (interactive "P")
10010   (let* ((buffer-read-only)
10011          (gnus-summary-prepare-hook nil))
10012     ;; We do the sorting by regenerating the threads.
10013     (gnus-summary-prepare)
10014     ;; Hide subthreads if needed.
10015     (gnus-summary-maybe-hide-threads)))
10016
10017 (defun gnus-summary-sort (predicate reverse)
10018   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10019   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10020          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10021          (gnus-thread-sort-functions
10022           (if (not reverse)
10023               thread
10024             `(lambda (t1 t2)
10025                (,thread t2 t1))))
10026          (gnus-sort-gathered-threads-function
10027           gnus-thread-sort-functions)
10028          (gnus-article-sort-functions
10029           (if (not reverse)
10030               article
10031             `(lambda (t1 t2)
10032                (,article t2 t1))))
10033          (buffer-read-only)
10034          (gnus-summary-prepare-hook nil))
10035     ;; We do the sorting by regenerating the threads.
10036     (gnus-summary-prepare)
10037     ;; Hide subthreads if needed.
10038     (gnus-summary-maybe-hide-threads)))
10039
10040 ;; Summary saving commands.
10041
10042 (defun gnus-summary-save-article (&optional n not-saved)
10043   "Save the current article using the default saver function.
10044 If N is a positive number, save the N next articles.
10045 If N is a negative number, save the N previous articles.
10046 If N is nil and any articles have been marked with the process mark,
10047 save those articles instead.
10048 The variable `gnus-default-article-saver' specifies the saver function."
10049   (interactive "P")
10050   (let* ((articles (gnus-summary-work-articles n))
10051          (save-buffer (save-excursion
10052                         (nnheader-set-temp-buffer " *Gnus Save*")))
10053          (num (length articles))
10054          header file)
10055     (dolist (article articles)
10056       (setq header (gnus-summary-article-header article))
10057       (if (not (vectorp header))
10058           ;; This is a pseudo-article.
10059           (if (assq 'name header)
10060               (gnus-copy-file (cdr (assq 'name header)))
10061             (gnus-message 1 "Article %d is unsaveable" article))
10062         ;; This is a real article.
10063         (save-window-excursion
10064           (let ((gnus-display-mime-function nil)
10065                 (gnus-article-prepare-hook nil))
10066             (gnus-summary-select-article t nil nil article)))
10067         (save-excursion
10068           (set-buffer save-buffer)
10069           (erase-buffer)
10070           (insert-buffer-substring gnus-original-article-buffer))
10071         (setq file (gnus-article-save save-buffer file num))
10072         (gnus-summary-remove-process-mark article)
10073         (unless not-saved
10074           (gnus-summary-set-saved-mark article))))
10075     (gnus-kill-buffer save-buffer)
10076     (gnus-summary-position-point)
10077     (gnus-set-mode-line 'summary)
10078     n))
10079
10080 (defun gnus-summary-pipe-output (&optional arg)
10081   "Pipe the current article to a subprocess.
10082 If N is a positive number, pipe the N next articles.
10083 If N is a negative number, pipe the N previous articles.
10084 If N is nil and any articles have been marked with the process mark,
10085 pipe those articles instead."
10086   (interactive "P")
10087   (require 'gnus-art)
10088   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10089     (gnus-summary-save-article arg t))
10090   (let ((buffer (get-buffer "*Shell Command Output*")))
10091     (if (and buffer
10092              (with-current-buffer buffer (> (point-max) (point-min))))
10093         (gnus-configure-windows 'pipe))))
10094
10095 (defun gnus-summary-save-article-mail (&optional arg)
10096   "Append the current article to an mail file.
10097 If N is a positive number, save the N next articles.
10098 If N is a negative number, save the N previous articles.
10099 If N is nil and any articles have been marked with the process mark,
10100 save those articles instead."
10101   (interactive "P")
10102   (require 'gnus-art)
10103   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10104     (gnus-summary-save-article arg)))
10105
10106 (defun gnus-summary-save-article-rmail (&optional arg)
10107   "Append the current article to an rmail file.
10108 If N is a positive number, save the N next articles.
10109 If N is a negative number, save the N previous articles.
10110 If N is nil and any articles have been marked with the process mark,
10111 save those articles instead."
10112   (interactive "P")
10113   (require 'gnus-art)
10114   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10115     (gnus-summary-save-article arg)))
10116
10117 (defun gnus-summary-save-article-file (&optional arg)
10118   "Append the current article to a file.
10119 If N is a positive number, save the N next articles.
10120 If N is a negative number, save the N previous articles.
10121 If N is nil and any articles have been marked with the process mark,
10122 save those articles instead."
10123   (interactive "P")
10124   (require 'gnus-art)
10125   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10126     (gnus-summary-save-article arg)))
10127
10128 (defun gnus-summary-write-article-file (&optional arg)
10129   "Write the current article to a file, deleting the previous file.
10130 If N is a positive number, save the N next articles.
10131 If N is a negative number, save the N previous articles.
10132 If N is nil and any articles have been marked with the process mark,
10133 save those articles instead."
10134   (interactive "P")
10135   (require 'gnus-art)
10136   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10137     (gnus-summary-save-article arg)))
10138
10139 (defun gnus-summary-save-article-body-file (&optional arg)
10140   "Append the current article body to a file.
10141 If N is a positive number, save the N next articles.
10142 If N is a negative number, save the N previous articles.
10143 If N is nil and any articles have been marked with the process mark,
10144 save those articles instead."
10145   (interactive "P")
10146   (require 'gnus-art)
10147   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10148     (gnus-summary-save-article arg)))
10149
10150 (defun gnus-summary-muttprint (&optional arg)
10151   "Print the current article using Muttprint.
10152 If N is a positive number, save the N next articles.
10153 If N is a negative number, save the N previous articles.
10154 If N is nil and any articles have been marked with the process mark,
10155 save those articles instead."
10156   (interactive "P")
10157   (require 'gnus-art)
10158   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10159     (gnus-summary-save-article arg t)))
10160
10161 (defun gnus-summary-pipe-message (program)
10162   "Pipe the current article through PROGRAM."
10163   (interactive "sProgram: ")
10164   (gnus-summary-select-article)
10165   (let ((mail-header-separator ""))
10166     (gnus-eval-in-buffer-window gnus-article-buffer
10167       (save-restriction
10168         (widen)
10169         (let ((start (window-start))
10170               buffer-read-only)
10171           (message-pipe-buffer-body program)
10172           (set-window-start (get-buffer-window (current-buffer)) start))))))
10173
10174 (defun gnus-get-split-value (methods)
10175   "Return a value based on the split METHODS."
10176   (let (split-name method result match)
10177     (when methods
10178       (save-excursion
10179         (set-buffer gnus-original-article-buffer)
10180         (save-restriction
10181           (nnheader-narrow-to-headers)
10182           (while (and methods (not split-name))
10183             (goto-char (point-min))
10184             (setq method (pop methods))
10185             (setq match (car method))
10186             (when (cond
10187                    ((stringp match)
10188                     ;; Regular expression.
10189                     (ignore-errors
10190                       (re-search-forward match nil t)))
10191                    ((gnus-functionp match)
10192                     ;; Function.
10193                     (save-restriction
10194                       (widen)
10195                       (setq result (funcall match gnus-newsgroup-name))))
10196                    ((consp match)
10197                     ;; Form.
10198                     (save-restriction
10199                       (widen)
10200                       (setq result (eval match)))))
10201               (setq split-name (cdr method))
10202               (cond ((stringp result)
10203                      (push (expand-file-name
10204                             result gnus-article-save-directory)
10205                            split-name))
10206                     ((consp result)
10207                      (setq split-name (append result split-name)))))))))
10208     (nreverse split-name)))
10209
10210 (defun gnus-valid-move-group-p (group)
10211   (and (boundp group)
10212        (symbol-name group)
10213        (symbol-value group)
10214        (gnus-get-function (gnus-find-method-for-group
10215                            (symbol-name group)) 'request-accept-article t)))
10216
10217 (defun gnus-read-move-group-name (prompt default articles prefix)
10218   "Read a group name."
10219   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10220          (minibuffer-confirm-incomplete nil) ; XEmacs
10221          (prom
10222           (format "%s %s to:"
10223                   prompt
10224                   (if (> (length articles) 1)
10225                       (format "these %d articles" (length articles))
10226                     "this article")))
10227          (to-newsgroup
10228           (cond
10229            ((null split-name)
10230             (gnus-completing-read-with-default
10231              default prom
10232              gnus-active-hashtb
10233              'gnus-valid-move-group-p
10234              nil prefix
10235              'gnus-group-history))
10236            ((= 1 (length split-name))
10237             (gnus-completing-read-with-default
10238              (car split-name) prom
10239              gnus-active-hashtb
10240              'gnus-valid-move-group-p
10241              nil nil
10242              'gnus-group-history))
10243            (t
10244             (gnus-completing-read-with-default
10245              nil prom
10246              (mapcar (lambda (el) (list el))
10247                      (nreverse split-name))
10248              nil nil nil
10249              'gnus-group-history))))
10250          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10251     (when to-newsgroup
10252       (if (or (string= to-newsgroup "")
10253               (string= to-newsgroup prefix))
10254           (setq to-newsgroup default))
10255       (unless to-newsgroup
10256         (error "No group name entered"))
10257       (or (gnus-active to-newsgroup)
10258           (gnus-activate-group to-newsgroup nil nil to-method)
10259           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10260                                      to-newsgroup))
10261               (or (and (gnus-request-create-group to-newsgroup to-method)
10262                        (gnus-activate-group
10263                         to-newsgroup nil nil to-method)
10264                        (gnus-subscribe-group to-newsgroup))
10265                   (error "Couldn't create group %s" to-newsgroup)))
10266           (error "No such group: %s" to-newsgroup)))
10267     to-newsgroup))
10268
10269 (defun gnus-summary-save-parts (type dir n &optional reverse)
10270   "Save parts matching TYPE to DIR.
10271 If REVERSE, save parts that do not match TYPE."
10272   (interactive
10273    (list (read-string "Save parts of type: "
10274                       (or (car gnus-summary-save-parts-type-history)
10275                           gnus-summary-save-parts-default-mime)
10276                       'gnus-summary-save-parts-type-history)
10277          (setq gnus-summary-save-parts-last-directory
10278                (read-file-name "Save to directory: "
10279                                gnus-summary-save-parts-last-directory
10280                                nil t))
10281          current-prefix-arg))
10282   (gnus-summary-iterate n
10283     (let ((gnus-display-mime-function nil)
10284           (gnus-inhibit-treatment t))
10285       (gnus-summary-select-article))
10286     (save-excursion
10287       (set-buffer gnus-article-buffer)
10288       (let ((handles (or gnus-article-mime-handles
10289                          (mm-dissect-buffer) (mm-uu-dissect))))
10290         (when handles
10291           (gnus-summary-save-parts-1 type dir handles reverse)
10292           (unless gnus-article-mime-handles ;; Don't destroy this case.
10293             (mm-destroy-parts handles)))))))
10294
10295 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10296   (if (stringp (car handle))
10297       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10298               (cdr handle))
10299     (when (if reverse
10300               (not (string-match type (mm-handle-media-type handle)))
10301             (string-match type (mm-handle-media-type handle)))
10302       (let ((file (expand-file-name
10303                    (file-name-nondirectory
10304                     (or
10305                      (mail-content-type-get
10306                       (mm-handle-disposition handle) 'filename)
10307                      (concat gnus-newsgroup-name
10308                              "." (number-to-string
10309                                   (cdr gnus-article-current)))))
10310                    dir)))
10311         (unless (file-exists-p file)
10312           (mm-save-part-to-file handle file))))))
10313
10314 ;; Summary extract commands
10315
10316 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10317   (let ((buffer-read-only nil)
10318         (article (gnus-summary-article-number))
10319         after-article b e)
10320     (unless (gnus-summary-goto-subject article)
10321       (error "No such article: %d" article))
10322     (gnus-summary-position-point)
10323     ;; If all commands are to be bunched up on one line, we collect
10324     ;; them here.
10325     (unless gnus-view-pseudos-separately
10326       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10327             files action)
10328         (while ps
10329           (setq action (cdr (assq 'action (car ps))))
10330           (setq files (list (cdr (assq 'name (car ps)))))
10331           (while (and ps (cdr ps)
10332                       (string= (or action "1")
10333                                (or (cdr (assq 'action (cadr ps))) "2")))
10334             (push (cdr (assq 'name (cadr ps))) files)
10335             (setcdr ps (cddr ps)))
10336           (when files
10337             (when (not (string-match "%s" action))
10338               (push " " files))
10339             (push " " files)
10340             (when (assq 'execute (car ps))
10341               (setcdr (assq 'execute (car ps))
10342                       (funcall (if (string-match "%s" action)
10343                                    'format 'concat)
10344                                action
10345                                (mapconcat
10346                                 (lambda (f)
10347                                   (if (equal f " ")
10348                                       f
10349                                     (mm-quote-arg f)))
10350                                 files " ")))))
10351           (setq ps (cdr ps)))))
10352     (if (and gnus-view-pseudos (not not-view))
10353         (while pslist
10354           (when (assq 'execute (car pslist))
10355             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10356                                   (eq gnus-view-pseudos 'not-confirm)))
10357           (setq pslist (cdr pslist)))
10358       (save-excursion
10359         (while pslist
10360           (setq after-article (or (cdr (assq 'article (car pslist)))
10361                                   (gnus-summary-article-number)))
10362           (gnus-summary-goto-subject after-article)
10363           (forward-line 1)
10364           (setq b (point))
10365           (insert "    " (file-name-nondirectory
10366                           (cdr (assq 'name (car pslist))))
10367                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10368           (setq e (point))
10369           (forward-line -1)             ; back to `b'
10370           (gnus-add-text-properties
10371            b (1- e) (list 'gnus-number gnus-reffed-article-number
10372                           gnus-mouse-face-prop gnus-mouse-face))
10373           (gnus-data-enter
10374            after-article gnus-reffed-article-number
10375            gnus-unread-mark b (car pslist) 0 (- e b))
10376           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10377           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10378           (setq pslist (cdr pslist)))))))
10379
10380 (defun gnus-pseudos< (p1 p2)
10381   (let ((c1 (cdr (assq 'action p1)))
10382         (c2 (cdr (assq 'action p2))))
10383     (and c1 c2 (string< c1 c2))))
10384
10385 (defun gnus-request-pseudo-article (props)
10386   (cond ((assq 'execute props)
10387          (gnus-execute-command (cdr (assq 'execute props)))))
10388   (let ((gnus-current-article (gnus-summary-article-number)))
10389     (gnus-run-hooks 'gnus-mark-article-hook)))
10390
10391 (defun gnus-execute-command (command &optional automatic)
10392   (save-excursion
10393     (gnus-article-setup-buffer)
10394     (set-buffer gnus-article-buffer)
10395     (setq buffer-read-only nil)
10396     (let ((command (if automatic command
10397                      (read-string "Command: " (cons command 0)))))
10398       (erase-buffer)
10399       (insert "$ " command "\n\n")
10400       (if gnus-view-pseudo-asynchronously
10401           (start-process "gnus-execute" (current-buffer) shell-file-name
10402                          shell-command-switch command)
10403         (call-process shell-file-name nil t nil
10404                       shell-command-switch command)))))
10405
10406 ;; Summary kill commands.
10407
10408 (defun gnus-summary-edit-global-kill (article)
10409   "Edit the \"global\" kill file."
10410   (interactive (list (gnus-summary-article-number)))
10411   (gnus-group-edit-global-kill article))
10412
10413 (defun gnus-summary-edit-local-kill ()
10414   "Edit a local kill file applied to the current newsgroup."
10415   (interactive)
10416   (setq gnus-current-headers (gnus-summary-article-header))
10417   (gnus-group-edit-local-kill
10418    (gnus-summary-article-number) gnus-newsgroup-name))
10419
10420 ;;; Header reading.
10421
10422 (defun gnus-read-header (id &optional header)
10423   "Read the headers of article ID and enter them into the Gnus system."
10424   (let ((group gnus-newsgroup-name)
10425         (gnus-override-method
10426          (or
10427           gnus-override-method
10428           (and (gnus-news-group-p gnus-newsgroup-name)
10429                (car (gnus-refer-article-methods)))))
10430         where)
10431     ;; First we check to see whether the header in question is already
10432     ;; fetched.
10433     (if (stringp id)
10434         ;; This is a Message-ID.
10435         (setq header (or header (gnus-id-to-header id)))
10436       ;; This is an article number.
10437       (setq header (or header (gnus-summary-article-header id))))
10438     (if (and header
10439              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10440         ;; We have found the header.
10441         header
10442       ;; If this is a sparse article, we have to nix out its
10443       ;; previous entry in the thread hashtb.
10444       (when (and header
10445                  (gnus-summary-article-sparse-p (mail-header-number header)))
10446         (let* ((parent (gnus-parent-id (mail-header-references header)))
10447                (thread (and parent (gnus-id-to-thread parent))))
10448           (when thread
10449             (delq (assq header thread) thread))))
10450       ;; We have to really fetch the header to this article.
10451       (save-excursion
10452         (set-buffer nntp-server-buffer)
10453         (when (setq where (gnus-request-head id group))
10454           (nnheader-fold-continuation-lines)
10455           (goto-char (point-max))
10456           (insert ".\n")
10457           (goto-char (point-min))
10458           (insert "211 ")
10459           (princ (cond
10460                   ((numberp id) id)
10461                   ((cdr where) (cdr where))
10462                   (header (mail-header-number header))
10463                   (t gnus-reffed-article-number))
10464                  (current-buffer))
10465           (insert " Article retrieved.\n"))
10466         (if (or (not where)
10467                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10468             ()                          ; Malformed head.
10469           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10470             (when (and (stringp id)
10471                        (not (string= (gnus-group-real-name group)
10472                                      (car where))))
10473               ;; If we fetched by Message-ID and the article came
10474               ;; from a different group, we fudge some bogus article
10475               ;; numbers for this article.
10476               (mail-header-set-number header gnus-reffed-article-number))
10477             (save-excursion
10478               (set-buffer gnus-summary-buffer)
10479               (decf gnus-reffed-article-number)
10480               (gnus-remove-header (mail-header-number header))
10481               (push header gnus-newsgroup-headers)
10482               (setq gnus-current-headers header)
10483               (push (mail-header-number header) gnus-newsgroup-limit)))
10484           header)))))
10485
10486 (defun gnus-remove-header (number)
10487   "Remove header NUMBER from `gnus-newsgroup-headers'."
10488   (if (and gnus-newsgroup-headers
10489            (= number (mail-header-number (car gnus-newsgroup-headers))))
10490       (pop gnus-newsgroup-headers)
10491     (let ((headers gnus-newsgroup-headers))
10492       (while (and (cdr headers)
10493                   (not (= number (mail-header-number (cadr headers)))))
10494         (pop headers))
10495       (when (cdr headers)
10496         (setcdr headers (cddr headers))))))
10497
10498 ;;;
10499 ;;; summary highlights
10500 ;;;
10501
10502 (defun gnus-highlight-selected-summary ()
10503   "Highlight selected article in summary buffer."
10504   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10505   (when gnus-summary-selected-face
10506     (save-excursion
10507       (let* ((beg (progn (beginning-of-line) (point)))
10508              (end (progn (end-of-line) (point)))
10509              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10510              (from (if (get-text-property beg gnus-mouse-face-prop)
10511                        beg
10512                      (or (next-single-property-change
10513                           beg gnus-mouse-face-prop nil end)
10514                          beg)))
10515              (to
10516               (if (= from end)
10517                   (- from 2)
10518                 (or (next-single-property-change
10519                      from gnus-mouse-face-prop nil end)
10520                     end))))
10521         ;; If no mouse-face prop on line we will have to = from = end,
10522         ;; so we highlight the entire line instead.
10523         (when (= (+ to 2) from)
10524           (setq from beg)
10525           (setq to end))
10526         (if gnus-newsgroup-selected-overlay
10527             ;; Move old overlay.
10528             (gnus-move-overlay
10529              gnus-newsgroup-selected-overlay from to (current-buffer))
10530           ;; Create new overlay.
10531           (gnus-overlay-put
10532            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10533            'face gnus-summary-selected-face))))))
10534
10535 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10536 (defun gnus-summary-highlight-line ()
10537   "Highlight current line according to `gnus-summary-highlight'."
10538   (let* ((list gnus-summary-highlight)
10539          (p (point))
10540          (end (progn (end-of-line) (point)))
10541          ;; now find out where the line starts and leave point there.
10542          (beg (progn (beginning-of-line) (point)))
10543          (article (gnus-summary-article-number))
10544          (score (or (cdr (assq (or article gnus-current-article)
10545                                gnus-newsgroup-scored))
10546                     gnus-summary-default-score 0))
10547          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10548          (inhibit-read-only t))
10549     ;; Eval the cars of the lists until we find a match.
10550     (let ((default gnus-summary-default-score)
10551           (default-high gnus-summary-default-high-score)
10552           (default-low gnus-summary-default-low-score))
10553       (while (and list
10554                   (not (eval (caar list))))
10555         (setq list (cdr list))))
10556     (let ((face (cdar list)))
10557       (unless (eq face (get-text-property beg 'face))
10558         (gnus-put-text-property-excluding-characters-with-faces
10559          beg end 'face
10560          (setq face (if (boundp face) (symbol-value face) face)))
10561         (when gnus-summary-highlight-line-function
10562           (funcall gnus-summary-highlight-line-function article face))))
10563     (goto-char p)))
10564
10565 (defun gnus-update-read-articles (group unread &optional compute)
10566   "Update the list of read articles in GROUP."
10567   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10568          (entry (gnus-gethash group gnus-newsrc-hashtb))
10569          (info (nth 2 entry))
10570          (prev 1)
10571          (unread (sort (copy-sequence unread) '<))
10572          read)
10573     (if (or (not info) (not active))
10574         ;; There is no info on this group if it was, in fact,
10575         ;; killed.  Gnus stores no information on killed groups, so
10576         ;; there's nothing to be done.
10577         ;; One could store the information somewhere temporarily,
10578         ;; perhaps...  Hmmm...
10579         ()
10580       ;; Remove any negative articles numbers.
10581       (while (and unread (< (car unread) 0))
10582         (setq unread (cdr unread)))
10583       ;; Remove any expired article numbers
10584       (while (and unread (< (car unread) (car active)))
10585         (setq unread (cdr unread)))
10586       ;; Compute the ranges of read articles by looking at the list of
10587       ;; unread articles.
10588       (while unread
10589         (when (/= (car unread) prev)
10590           (push (if (= prev (1- (car unread))) prev
10591                   (cons prev (1- (car unread))))
10592                 read))
10593         (setq prev (1+ (car unread)))
10594         (setq unread (cdr unread)))
10595       (when (<= prev (cdr active))
10596         (push (cons prev (cdr active)) read))
10597       (setq read (if (> (length read) 1) (nreverse read) read))
10598       (if compute
10599           read
10600         (save-excursion
10601           (let (setmarkundo)
10602             ;; Propagate the read marks to the backend.
10603             (when (gnus-check-backend-function 'request-set-mark group)
10604               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10605                     (add (gnus-remove-from-range read (gnus-info-read info))))
10606                 (when (or add del)
10607                   (unless (gnus-check-group group)
10608                     (error "Can't open server for %s" group))
10609                   (gnus-request-set-mark
10610                    group (delq nil (list (if add (list add 'add '(read)))
10611                                          (if del (list del 'del '(read))))))
10612                   (setq setmarkundo
10613                         `(gnus-request-set-mark
10614                           ,group
10615                           ',(delq nil (list
10616                                        (if del (list del 'add '(read)))
10617                                        (if add (list add 'del '(read))))))))))
10618             (set-buffer gnus-group-buffer)
10619             (gnus-undo-register
10620               `(progn
10621                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10622                  (gnus-info-set-read ',info ',(gnus-info-read info))
10623                  (gnus-get-unread-articles-in-group ',info
10624                                                     (gnus-active ,group))
10625                  (gnus-group-update-group ,group t)
10626                  ,setmarkundo))))
10627         ;; Enter this list into the group info.
10628         (gnus-info-set-read info read)
10629         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10630         (gnus-get-unread-articles-in-group info (gnus-active group))
10631         t))))
10632
10633 (defun gnus-offer-save-summaries ()
10634   "Offer to save all active summary buffers."
10635   (let (buffers)
10636     ;; Go through all buffers and find all summaries.
10637     (dolist (buffer (buffer-list))
10638       (when (and (setq buffer (buffer-name buffer))
10639                  (string-match "Summary" buffer)
10640                  (save-excursion
10641                    (set-buffer buffer)
10642                    ;; We check that this is, indeed, a summary buffer.
10643                    (and (eq major-mode 'gnus-summary-mode)
10644                         ;; Also make sure this isn't bogus.
10645                         gnus-newsgroup-prepared
10646                         ;; Also make sure that this isn't a
10647                         ;; dead summary buffer.
10648                         (not gnus-dead-summary-mode))))
10649         (push buffer buffers)))
10650     ;; Go through all these summary buffers and offer to save them.
10651     (when buffers
10652       (save-excursion
10653         (map-y-or-n-p
10654          "Update summary buffer %s? "
10655          (lambda (buf)
10656            (switch-to-buffer buf)
10657            (gnus-summary-exit))
10658          buffers)))))
10659
10660 (defun gnus-summary-setup-default-charset ()
10661   "Setup newsgroup default charset."
10662   (if (equal gnus-newsgroup-name "nndraft:drafts")
10663       (setq gnus-newsgroup-charset nil)
10664     (let* ((ignored-charsets
10665             (or gnus-newsgroup-ephemeral-ignored-charsets
10666                 (append
10667                  (and gnus-newsgroup-name
10668                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10669                  gnus-newsgroup-ignored-charsets))))
10670       (setq gnus-newsgroup-charset
10671             (or gnus-newsgroup-ephemeral-charset
10672                 (and gnus-newsgroup-name
10673                      (gnus-parameter-charset gnus-newsgroup-name))
10674                 gnus-default-charset))
10675       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10676            ignored-charsets))))
10677
10678 ;;;
10679 ;;; Mime Commands
10680 ;;;
10681
10682 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10683   "Display the current article buffer fully MIME-buttonized.
10684 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10685 treated as multipart/mixed."
10686   (interactive "P")
10687   (require 'gnus-art)
10688   (let ((gnus-unbuttonized-mime-types nil)
10689         (gnus-mime-display-multipart-as-mixed show-all-parts))
10690     (gnus-summary-show-article)))
10691
10692 (defun gnus-summary-repair-multipart (article)
10693   "Add a Content-Type header to a multipart article without one."
10694   (interactive (list (gnus-summary-article-number)))
10695   (gnus-with-article article
10696     (message-narrow-to-head)
10697     (message-remove-header "Mime-Version")
10698     (goto-char (point-max))
10699     (insert "Mime-Version: 1.0\n")
10700     (widen)
10701     (when (search-forward "\n--" nil t)
10702       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10703         (message-narrow-to-head)
10704         (message-remove-header "Content-Type")
10705         (goto-char (point-max))
10706         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10707                         separator))
10708         (widen))))
10709   (let (gnus-mark-article-hook)
10710     (gnus-summary-select-article t t nil article)))
10711
10712 (defun gnus-summary-toggle-display-buttonized ()
10713   "Toggle the buttonizing of the article buffer."
10714   (interactive)
10715   (require 'gnus-art)
10716   (if (setq gnus-inhibit-mime-unbuttonizing
10717             (not gnus-inhibit-mime-unbuttonizing))
10718       (let ((gnus-unbuttonized-mime-types nil))
10719         (gnus-summary-show-article))
10720     (gnus-summary-show-article)))
10721
10722 ;;;
10723 ;;; Generic summary marking commands
10724 ;;;
10725
10726 (defvar gnus-summary-marking-alist
10727   '((read gnus-del-mark "d")
10728     (unread gnus-unread-mark "u")
10729     (ticked gnus-ticked-mark "!")
10730     (dormant gnus-dormant-mark "?")
10731     (expirable gnus-expirable-mark "e"))
10732   "An alist of names/marks/keystrokes.")
10733
10734 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10735 (defvar gnus-summary-mark-map)
10736
10737 (defun gnus-summary-make-all-marking-commands ()
10738   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10739   (dolist (elem gnus-summary-marking-alist)
10740     (apply 'gnus-summary-make-marking-command elem)))
10741
10742 (defun gnus-summary-make-marking-command (name mark keystroke)
10743   (let ((map (make-sparse-keymap)))
10744     (define-key gnus-summary-generic-mark-map keystroke map)
10745     (dolist (lway `((next "next" next nil "n")
10746                     (next-unread "next unread" next t "N")
10747                     (prev "previous" prev nil "p")
10748                     (prev-unread "previous unread" prev t "P")
10749                     (nomove "" nil nil ,keystroke)))
10750       (let ((func (gnus-summary-make-marking-command-1
10751                    mark (car lway) lway name)))
10752         (setq func (eval func))
10753         (define-key map (nth 4 lway) func)))))
10754
10755 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10756   `(defun ,(intern
10757             (format "gnus-summary-put-mark-as-%s%s"
10758                     name (if (eq way 'nomove)
10759                              ""
10760                            (concat "-" (symbol-name way)))))
10761      (n)
10762      ,(format
10763        "Mark the current article as %s%s.
10764 If N, the prefix, then repeat N times.
10765 If N is negative, move in reverse order.
10766 The difference between N and the actual number of articles marked is
10767 returned."
10768        name (cadr lway))
10769      (interactive "p")
10770      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10771
10772 (defun gnus-summary-generic-mark (n mark move unread)
10773   "Mark N articles with MARK."
10774   (unless (eq major-mode 'gnus-summary-mode)
10775     (error "This command can only be used in the summary buffer"))
10776   (gnus-summary-show-thread)
10777   (let ((nummove
10778          (cond
10779           ((eq move 'next) 1)
10780           ((eq move 'prev) -1)
10781           (t 0))))
10782     (if (zerop nummove)
10783         (setq n 1)
10784       (when (< n 0)
10785         (setq n (abs n)
10786               nummove (* -1 nummove))))
10787     (while (and (> n 0)
10788                 (gnus-summary-mark-article nil mark)
10789                 (zerop (gnus-summary-next-subject nummove unread t)))
10790       (setq n (1- n)))
10791     (when (/= 0 n)
10792       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10793     (gnus-summary-recenter)
10794     (gnus-summary-position-point)
10795     (gnus-set-mode-line 'summary)
10796     n))
10797
10798 (defun gnus-summary-insert-articles (articles)
10799   (when (setq articles
10800               (gnus-set-difference articles
10801                                    (mapcar (lambda (h) (mail-header-number h))
10802                                            gnus-newsgroup-headers)))
10803     (setq gnus-newsgroup-headers
10804           (merge 'list
10805                  gnus-newsgroup-headers
10806                  (gnus-fetch-headers articles)
10807                  'gnus-article-sort-by-number))
10808     ;; Suppress duplicates?
10809     (when gnus-suppress-duplicates
10810       (gnus-dup-suppress-articles))
10811
10812     ;; We might want to build some more threads first.
10813     (when (and gnus-fetch-old-headers
10814                (eq gnus-headers-retrieved-by 'nov))
10815       (if (eq gnus-fetch-old-headers 'invisible)
10816           (gnus-build-all-threads)
10817         (gnus-build-old-threads)))
10818     ;; Let the Gnus agent mark articles as read.
10819     (when gnus-agent
10820       (gnus-agent-get-undownloaded-list))
10821     ;; Remove list identifiers from subject
10822     (when gnus-list-identifiers
10823       (gnus-summary-remove-list-identifiers))
10824     ;; First and last article in this newsgroup.
10825     (when gnus-newsgroup-headers
10826       (setq gnus-newsgroup-begin
10827             (mail-header-number (car gnus-newsgroup-headers))
10828             gnus-newsgroup-end
10829             (mail-header-number
10830              (gnus-last-element gnus-newsgroup-headers))))
10831     (when gnus-use-scoring
10832       (gnus-possibly-score-headers))))
10833
10834 (defun gnus-summary-insert-old-articles (&optional all)
10835   "Insert all old articles in this group.
10836 If ALL is non-nil, already read articles become readable.
10837 If ALL is a number, fetch this number of articles."
10838   (interactive "P")
10839   (prog1
10840       (let ((old (mapcar 'car gnus-newsgroup-data))
10841             (i (car gnus-newsgroup-active))
10842             older len)
10843         (while (<= i (cdr gnus-newsgroup-active))
10844           (or (memq i old) (push i older))
10845           (incf i))
10846         (setq len (length older))
10847         (cond
10848          ((null older) nil)
10849          ((numberp all)
10850           (if (< all len)
10851               (setq older (subseq older 0 all))))
10852          (all nil)
10853          (t
10854           (if (and (numberp gnus-large-newsgroup)
10855                    (> len gnus-large-newsgroup))
10856               (let ((input
10857                      (read-string
10858                       (format
10859                        "How many articles from %s (default %d): "
10860                        (gnus-limit-string
10861                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10862                        len))))
10863                 (unless (string-match "^[ \t]*$" input)
10864                   (setq all (string-to-number input))
10865                   (if (< all len)
10866                       (setq older (subseq older 0 all))))))))
10867         (if (not older)
10868             (message "No old news.")
10869           (let ((gnus-fetch-old-headers t))
10870             (gnus-summary-insert-articles older))
10871           (gnus-summary-limit (gnus-union older old))))
10872     (gnus-summary-position-point)))
10873
10874 (defun gnus-summary-insert-new-articles ()
10875   "Insert all new articles in this group."
10876   (interactive)
10877   (prog1
10878       (let ((old (mapcar 'car gnus-newsgroup-data))
10879             (old-active gnus-newsgroup-active)
10880             (nnmail-fetched-sources (list t))
10881             i new)
10882         (setq gnus-newsgroup-active
10883               (gnus-activate-group gnus-newsgroup-name 'scan))
10884         (setq i (1+ (cdr old-active)))
10885         (while (<= i (cdr gnus-newsgroup-active))
10886           (push i new)
10887           (incf i))
10888         (if (not new)
10889             (message "No gnus is bad news.")
10890           (setq new (nreverse new))
10891           (gnus-summary-insert-articles new)
10892           (setq gnus-newsgroup-unreads
10893                 (append gnus-newsgroup-unreads new))
10894           (gnus-summary-limit (gnus-union old new))))
10895     (gnus-summary-position-point)))
10896
10897 (gnus-summary-make-all-marking-commands)
10898
10899 (gnus-ems-redefine)
10900
10901 (provide 'gnus-sum)
10902
10903 (run-hooks 'gnus-sum-load-hook)
10904
10905 ;;; gnus-sum.el ends here