* nnheader.el (nnheader-nov-skip-field): Use skip-chars-forward,
[gnus] / 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 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-extract-message-id-from-in-reply-to (string)
3605   (if (string-match "<[^>]+>" string)
3606       (substring string (match-beginning 0) (match-end 0))
3607     nil))
3608
3609 (defun gnus-build-sparse-threads ()
3610   (let ((headers gnus-newsgroup-headers)
3611         (mail-parse-charset gnus-newsgroup-charset)
3612         (gnus-summary-ignore-duplicates t)
3613         header references generation relations
3614         subject child end new-child date)
3615     ;; First we create an alist of generations/relations, where
3616     ;; generations is how much we trust the relation, and the relation
3617     ;; is parent/child.
3618     (gnus-message 7 "Making sparse threads...")
3619     (save-excursion
3620       (nnheader-set-temp-buffer " *gnus sparse threads*")
3621       (while (setq header (pop headers))
3622         (when (and (setq references (mail-header-references header))
3623                    (not (string= references "")))
3624           (insert references)
3625           (setq child (mail-header-id header)
3626                 subject (mail-header-subject header)
3627                 date (mail-header-date header)
3628                 generation 0)
3629           (while (search-backward ">" nil t)
3630             (setq end (1+ (point)))
3631             (when (search-backward "<" nil t)
3632               (setq new-child (buffer-substring (point) end))
3633               (push (list (incf generation)
3634                           child (setq child new-child)
3635                           subject date)
3636                     relations)))
3637           (when child
3638             (push (list (1+ generation) child nil subject) relations))
3639           (erase-buffer)))
3640       (kill-buffer (current-buffer)))
3641     ;; Sort over trustworthiness.
3642     (mapcar
3643      (lambda (relation)
3644        (when (gnus-dependencies-add-header
3645               (make-full-mail-header
3646                gnus-reffed-article-number
3647                (nth 3 relation) "" (or (nth 4 relation) "")
3648                (nth 1 relation)
3649                (or (nth 2 relation) "") 0 0 "")
3650               gnus-newsgroup-dependencies nil)
3651          (push gnus-reffed-article-number gnus-newsgroup-limit)
3652          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3653          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3654                gnus-newsgroup-reads)
3655          (decf gnus-reffed-article-number)))
3656      (sort relations 'car-less-than-car))
3657     (gnus-message 7 "Making sparse threads...done")))
3658
3659 (defun gnus-build-old-threads ()
3660   ;; Look at all the articles that refer back to old articles, and
3661   ;; fetch the headers for the articles that aren't there.  This will
3662   ;; build complete threads - if the roots haven't been expired by the
3663   ;; server, that is.
3664   (let ((mail-parse-charset gnus-newsgroup-charset)
3665         id heads)
3666     (mapatoms
3667      (lambda (refs)
3668        (when (not (car (symbol-value refs)))
3669          (setq heads (cdr (symbol-value refs)))
3670          (while heads
3671            (if (memq (mail-header-number (caar heads))
3672                      gnus-newsgroup-dormant)
3673                (setq heads (cdr heads))
3674              (setq id (symbol-name refs))
3675              (while (and (setq id (gnus-build-get-header id))
3676                          (not (car (gnus-id-to-thread id)))))
3677              (setq heads nil)))))
3678      gnus-newsgroup-dependencies)))
3679
3680 ;; This function has to be called with point after the article number
3681 ;; on the beginning of the line.
3682 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3683   (let ((eol (gnus-point-at-eol))
3684         (buffer (current-buffer))
3685         header references in-reply-to)
3686
3687     ;; overview: [num subject from date id refs chars lines misc]
3688     (unwind-protect
3689         (progn
3690           (narrow-to-region (point) eol)
3691           (unless (eobp)
3692             (forward-char))
3693
3694           (setq header
3695                 (make-full-mail-header
3696                  number                 ; number
3697                  (funcall gnus-decode-encoded-word-function
3698                           (nnheader-nov-field)) ; subject
3699                  (funcall gnus-decode-encoded-word-function
3700                           (nnheader-nov-field)) ; from
3701                  (nnheader-nov-field)   ; date
3702                  (nnheader-nov-read-message-id) ; id
3703                  (setq references (nnheader-nov-field)) ; refs
3704                  (nnheader-nov-read-integer) ; chars
3705                  (nnheader-nov-read-integer) ; lines
3706                  (unless (eobp)
3707                    (if (looking-at "Xref: ")
3708                        (goto-char (match-end 0)))
3709                    (nnheader-nov-field)) ; Xref
3710                  (nnheader-nov-parse-extra)))) ; extra
3711
3712       (widen))
3713
3714     (when (and (string= references "")
3715                (setq in-reply-to (mail-header-extra header))
3716                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3717       (mail-header-set-references
3718        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3719
3720     (when gnus-alter-header-function
3721       (funcall gnus-alter-header-function header))
3722     (gnus-dependencies-add-header header dependencies force-new)))
3723
3724 (defun gnus-build-get-header (id)
3725   "Look through the buffer of NOV lines and find the header to ID.
3726 Enter this line into the dependencies hash table, and return
3727 the id of the parent article (if any)."
3728   (let ((deps gnus-newsgroup-dependencies)
3729         found header)
3730     (prog1
3731         (save-excursion
3732           (set-buffer nntp-server-buffer)
3733           (let ((case-fold-search nil))
3734             (goto-char (point-min))
3735             (while (and (not found)
3736                         (search-forward id nil t))
3737               (beginning-of-line)
3738               (setq found (looking-at
3739                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3740                                    (regexp-quote id))))
3741               (or found (beginning-of-line 2)))
3742             (when found
3743               (beginning-of-line)
3744               (and
3745                (setq header (gnus-nov-parse-line
3746                              (read (current-buffer)) deps))
3747                (gnus-parent-id (mail-header-references header))))))
3748       (when header
3749         (let ((number (mail-header-number header)))
3750           (push number gnus-newsgroup-limit)
3751           (push header gnus-newsgroup-headers)
3752           (if (memq number gnus-newsgroup-unselected)
3753               (progn
3754                 (push number gnus-newsgroup-unreads)
3755                 (setq gnus-newsgroup-unselected
3756                       (delq number gnus-newsgroup-unselected)))
3757             (push number gnus-newsgroup-ancient)))))))
3758
3759 (defun gnus-build-all-threads ()
3760   "Read all the headers."
3761   (let ((gnus-summary-ignore-duplicates t)
3762         (mail-parse-charset gnus-newsgroup-charset)
3763         (dependencies gnus-newsgroup-dependencies)
3764         header article)
3765     (save-excursion
3766       (set-buffer nntp-server-buffer)
3767       (let ((case-fold-search nil))
3768         (goto-char (point-min))
3769         (while (not (eobp))
3770           (ignore-errors
3771             (setq article (read (current-buffer))
3772                   header (gnus-nov-parse-line article dependencies)))
3773           (when header
3774             (save-excursion
3775               (set-buffer gnus-summary-buffer)
3776               (push header gnus-newsgroup-headers)
3777               (if (memq (setq article (mail-header-number header))
3778                         gnus-newsgroup-unselected)
3779                   (progn
3780                     (push article gnus-newsgroup-unreads)
3781                     (setq gnus-newsgroup-unselected
3782                           (delq article gnus-newsgroup-unselected)))
3783                 (push article gnus-newsgroup-ancient)))
3784             (forward-line 1)))))))
3785
3786 (defun gnus-summary-update-article-line (article header)
3787   "Update the line for ARTICLE using HEADERS."
3788   (let* ((id (mail-header-id header))
3789          (thread (gnus-id-to-thread id)))
3790     (unless thread
3791       (error "Article in no thread"))
3792     ;; Update the thread.
3793     (setcar thread header)
3794     (gnus-summary-goto-subject article)
3795     (let* ((datal (gnus-data-find-list article))
3796            (data (car datal))
3797            (length (when (cdr datal)
3798                      (- (gnus-data-pos data)
3799                         (gnus-data-pos (cadr datal)))))
3800            (buffer-read-only nil)
3801            (level (gnus-summary-thread-level)))
3802       (gnus-delete-line)
3803       (gnus-summary-insert-line
3804        header level nil (gnus-article-mark article)
3805        (memq article gnus-newsgroup-replied)
3806        (memq article gnus-newsgroup-expirable)
3807        ;; Only insert the Subject string when it's different
3808        ;; from the previous Subject string.
3809        (if (and
3810             gnus-show-threads
3811             (gnus-subject-equal
3812              (condition-case ()
3813                  (mail-header-subject
3814                   (gnus-data-header
3815                    (cadr
3816                     (gnus-data-find-list
3817                      article
3818                      (gnus-data-list t)))))
3819                ;; Error on the side of excessive subjects.
3820                (error ""))
3821              (mail-header-subject header)))
3822            ""
3823          (mail-header-subject header))
3824        nil (cdr (assq article gnus-newsgroup-scored))
3825        (memq article gnus-newsgroup-processable))
3826       (when length
3827         (gnus-data-update-list
3828          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3829
3830 (defun gnus-summary-update-article (article &optional iheader)
3831   "Update ARTICLE in the summary buffer."
3832   (set-buffer gnus-summary-buffer)
3833   (let* ((header (gnus-summary-article-header article))
3834          (id (mail-header-id header))
3835          (data (gnus-data-find article))
3836          (thread (gnus-id-to-thread id))
3837          (references (mail-header-references header))
3838          (parent
3839           (gnus-id-to-thread
3840            (or (gnus-parent-id
3841                 (when (and references
3842                            (not (equal "" references)))
3843                   references))
3844                "none")))
3845          (buffer-read-only nil)
3846          (old (car thread)))
3847     (when thread
3848       (unless iheader
3849         (setcar thread nil)
3850         (when parent
3851           (delq thread parent)))
3852       (if (gnus-summary-insert-subject id header)
3853        ;; Set the (possibly) new article number in the data structure.
3854           (gnus-data-set-number data (gnus-id-to-article id))
3855         (setcar thread old)
3856         nil))))
3857
3858 (defun gnus-rebuild-thread (id &optional line)
3859   "Rebuild the thread containing ID.
3860 If LINE, insert the rebuilt thread starting on line LINE."
3861   (let ((buffer-read-only nil)
3862         old-pos current thread data)
3863     (if (not gnus-show-threads)
3864         (setq thread (list (car (gnus-id-to-thread id))))
3865       ;; Get the thread this article is part of.
3866       (setq thread (gnus-remove-thread id)))
3867     (setq old-pos (gnus-point-at-bol))
3868     (setq current (save-excursion
3869                     (and (re-search-backward "[\r\n]" nil t)
3870                          (gnus-summary-article-number))))
3871     ;; If this is a gathered thread, we have to go some re-gathering.
3872     (when (stringp (car thread))
3873       (let ((subject (car thread))
3874             roots thr)
3875         (setq thread (cdr thread))
3876         (while thread
3877           (unless (memq (setq thr (gnus-id-to-thread
3878                                    (gnus-root-id
3879                                     (mail-header-id (caar thread)))))
3880                         roots)
3881             (push thr roots))
3882           (setq thread (cdr thread)))
3883         ;; We now have all (unique) roots.
3884         (if (= (length roots) 1)
3885             ;; All the loose roots are now one solid root.
3886             (setq thread (car roots))
3887           (setq thread (cons subject (gnus-sort-threads roots))))))
3888     (let (threads)
3889       ;; We then insert this thread into the summary buffer.
3890       (when line
3891         (goto-char (point-min))
3892         (forward-line (1- line)))
3893       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3894         (if gnus-show-threads
3895             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3896           (gnus-summary-prepare-unthreaded thread))
3897         (setq data (nreverse gnus-newsgroup-data))
3898         (setq threads gnus-newsgroup-threads))
3899       ;; We splice the new data into the data structure.
3900       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3901       ;;!!! then we want to insert at the beginning of the buffer.
3902       ;;!!! That happens to be true with Gnus now, but that may
3903       ;;!!! change in the future.  Perhaps.
3904       (gnus-data-enter-list
3905        (if line nil current) data (- (point) old-pos))
3906       (setq gnus-newsgroup-threads
3907             (nconc threads gnus-newsgroup-threads))
3908       (gnus-data-compute-positions))))
3909
3910 (defun gnus-number-to-header (number)
3911   "Return the header for article NUMBER."
3912   (let ((headers gnus-newsgroup-headers))
3913     (while (and headers
3914                 (not (= number (mail-header-number (car headers)))))
3915       (pop headers))
3916     (when headers
3917       (car headers))))
3918
3919 (defun gnus-parent-headers (in-headers &optional generation)
3920   "Return the headers of the GENERATIONeth parent of HEADERS."
3921   (unless generation
3922     (setq generation 1))
3923   (let ((parent t)
3924         (headers in-headers)
3925         references)
3926     (while (and parent
3927                 (not (zerop generation))
3928                 (setq references (mail-header-references headers)))
3929       (setq headers (if (and references
3930                              (setq parent (gnus-parent-id references)))
3931                         (car (gnus-id-to-thread parent))
3932                       nil))
3933       (decf generation))
3934     (and (not (eq headers in-headers))
3935          headers)))
3936
3937 (defun gnus-id-to-thread (id)
3938   "Return the (sub-)thread where ID appears."
3939   (gnus-gethash id gnus-newsgroup-dependencies))
3940
3941 (defun gnus-id-to-article (id)
3942   "Return the article number of ID."
3943   (let ((thread (gnus-id-to-thread id)))
3944     (when (and thread
3945                (car thread))
3946       (mail-header-number (car thread)))))
3947
3948 (defun gnus-id-to-header (id)
3949   "Return the article headers of ID."
3950   (car (gnus-id-to-thread id)))
3951
3952 (defun gnus-article-displayed-root-p (article)
3953   "Say whether ARTICLE is a root(ish) article."
3954   (let ((level (gnus-summary-thread-level article))
3955         (refs (mail-header-references  (gnus-summary-article-header article)))
3956         particle)
3957     (cond
3958      ((null level) nil)
3959      ((zerop level) t)
3960      ((null refs) t)
3961      ((null (gnus-parent-id refs)) t)
3962      ((and (= 1 level)
3963            (null (setq particle (gnus-id-to-article
3964                                  (gnus-parent-id refs))))
3965            (null (gnus-summary-thread-level particle)))))))
3966
3967 (defun gnus-root-id (id)
3968   "Return the id of the root of the thread where ID appears."
3969   (let (last-id prev)
3970     (while (and id (setq prev (car (gnus-id-to-thread id))))
3971       (setq last-id id
3972             id (gnus-parent-id (mail-header-references prev))))
3973     last-id))
3974
3975 (defun gnus-articles-in-thread (thread)
3976   "Return the list of articles in THREAD."
3977   (cons (mail-header-number (car thread))
3978         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3979
3980 (defun gnus-remove-thread (id &optional dont-remove)
3981   "Remove the thread that has ID in it."
3982   (let (headers thread last-id)
3983     ;; First go up in this thread until we find the root.
3984     (setq last-id (gnus-root-id id)
3985           headers (message-flatten-list (gnus-id-to-thread last-id)))
3986     ;; We have now found the real root of this thread.  It might have
3987     ;; been gathered into some loose thread, so we have to search
3988     ;; through the threads to find the thread we wanted.
3989     (let ((threads gnus-newsgroup-threads)
3990           sub)
3991       (while threads
3992         (setq sub (car threads))
3993         (if (stringp (car sub))
3994             ;; This is a gathered thread, so we look at the roots
3995             ;; below it to find whether this article is in this
3996             ;; gathered root.
3997             (progn
3998               (setq sub (cdr sub))
3999               (while sub
4000                 (when (member (caar sub) headers)
4001                   (setq thread (car threads)
4002                         threads nil
4003                         sub nil))
4004                 (setq sub (cdr sub))))
4005           ;; It's an ordinary thread, so we check it.
4006           (when (eq (car sub) (car headers))
4007             (setq thread sub
4008                   threads nil)))
4009         (setq threads (cdr threads)))
4010       ;; If this article is in no thread, then it's a root.
4011       (if thread
4012           (unless dont-remove
4013             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4014         (setq thread (gnus-id-to-thread last-id)))
4015       (when thread
4016         (prog1
4017             thread                      ; We return this thread.
4018           (unless dont-remove
4019             (if (stringp (car thread))
4020                 (progn
4021                   ;; If we use dummy roots, then we have to remove the
4022                   ;; dummy root as well.
4023                   (when (eq gnus-summary-make-false-root 'dummy)
4024                     ;; We go to the dummy root by going to
4025                     ;; the first sub-"thread", and then one line up.
4026                     (gnus-summary-goto-article
4027                      (mail-header-number (caadr thread)))
4028                     (forward-line -1)
4029                     (gnus-delete-line)
4030                     (gnus-data-compute-positions))
4031                   (setq thread (cdr thread))
4032                   (while thread
4033                     (gnus-remove-thread-1 (car thread))
4034                     (setq thread (cdr thread))))
4035               (gnus-remove-thread-1 thread))))))))
4036
4037 (defun gnus-remove-thread-1 (thread)
4038   "Remove the thread THREAD recursively."
4039   (let ((number (mail-header-number (pop thread)))
4040         d)
4041     (setq thread (reverse thread))
4042     (while thread
4043       (gnus-remove-thread-1 (pop thread)))
4044     (when (setq d (gnus-data-find number))
4045       (goto-char (gnus-data-pos d))
4046       (gnus-summary-show-thread)
4047       (gnus-data-remove
4048        number
4049        (- (gnus-point-at-bol)
4050           (prog1
4051               (1+ (gnus-point-at-eol))
4052             (gnus-delete-line)))))))
4053
4054 (defun gnus-sort-threads-1 (threads func)
4055   (sort (mapcar (lambda (thread)
4056                   (cons (car thread)
4057                         (and (cdr thread)
4058                              (gnus-sort-threads-1 (cdr thread) func))))
4059                 threads) func))
4060
4061 (defun gnus-sort-threads (threads)
4062   "Sort THREADS."
4063   (if (not gnus-thread-sort-functions)
4064       threads
4065     (gnus-message 8 "Sorting threads...")
4066     (prog1
4067         (gnus-sort-threads-1
4068          threads
4069          (gnus-make-sort-function gnus-thread-sort-functions))
4070       (gnus-message 8 "Sorting threads...done"))))
4071
4072 (defun gnus-sort-articles (articles)
4073   "Sort ARTICLES."
4074   (when gnus-article-sort-functions
4075     (gnus-message 7 "Sorting articles...")
4076     (prog1
4077         (setq gnus-newsgroup-headers
4078               (sort articles (gnus-make-sort-function
4079                               gnus-article-sort-functions)))
4080       (gnus-message 7 "Sorting articles...done"))))
4081
4082 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4083 (defmacro gnus-thread-header (thread)
4084   "Return header of first article in THREAD.
4085 Note that THREAD must never, ever be anything else than a variable -
4086 using some other form will lead to serious barfage."
4087   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4088   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4089   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4090         (vector thread) 2))
4091
4092 (defsubst gnus-article-sort-by-number (h1 h2)
4093   "Sort articles by article number."
4094   (< (mail-header-number h1)
4095      (mail-header-number h2)))
4096
4097 (defun gnus-thread-sort-by-number (h1 h2)
4098   "Sort threads by root article number."
4099   (gnus-article-sort-by-number
4100    (gnus-thread-header h1) (gnus-thread-header h2)))
4101
4102 (defsubst gnus-article-sort-by-lines (h1 h2)
4103   "Sort articles by article Lines header."
4104   (< (mail-header-lines h1)
4105      (mail-header-lines h2)))
4106
4107 (defun gnus-thread-sort-by-lines (h1 h2)
4108   "Sort threads by root article Lines header."
4109   (gnus-article-sort-by-lines
4110    (gnus-thread-header h1) (gnus-thread-header h2)))
4111
4112 (defsubst gnus-article-sort-by-chars (h1 h2)
4113   "Sort articles by octet length."
4114   (< (mail-header-chars h1)
4115      (mail-header-chars h2)))
4116
4117 (defun gnus-thread-sort-by-chars (h1 h2)
4118   "Sort threads by root article octet length."
4119   (gnus-article-sort-by-chars
4120    (gnus-thread-header h1) (gnus-thread-header h2)))
4121
4122 (defsubst gnus-article-sort-by-author (h1 h2)
4123   "Sort articles by root author."
4124   (string-lessp
4125    (let ((extract (funcall
4126                    gnus-extract-address-components
4127                    (mail-header-from h1))))
4128      (or (car extract) (cadr extract) ""))
4129    (let ((extract (funcall
4130                    gnus-extract-address-components
4131                    (mail-header-from h2))))
4132      (or (car extract) (cadr extract) ""))))
4133
4134 (defun gnus-thread-sort-by-author (h1 h2)
4135   "Sort threads by root author."
4136   (gnus-article-sort-by-author
4137    (gnus-thread-header h1)  (gnus-thread-header h2)))
4138
4139 (defsubst gnus-article-sort-by-subject (h1 h2)
4140   "Sort articles by root subject."
4141   (string-lessp
4142    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4143    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4144
4145 (defun gnus-thread-sort-by-subject (h1 h2)
4146   "Sort threads by root subject."
4147   (gnus-article-sort-by-subject
4148    (gnus-thread-header h1) (gnus-thread-header h2)))
4149
4150 (defsubst gnus-article-sort-by-date (h1 h2)
4151   "Sort articles by root article date."
4152   (time-less-p
4153    (gnus-date-get-time (mail-header-date h1))
4154    (gnus-date-get-time (mail-header-date h2))))
4155
4156 (defun gnus-thread-sort-by-date (h1 h2)
4157   "Sort threads by root article date."
4158   (gnus-article-sort-by-date
4159    (gnus-thread-header h1) (gnus-thread-header h2)))
4160
4161 (defsubst gnus-article-sort-by-score (h1 h2)
4162   "Sort articles by root article score.
4163 Unscored articles will be counted as having a score of zero."
4164   (> (or (cdr (assq (mail-header-number h1)
4165                     gnus-newsgroup-scored))
4166          gnus-summary-default-score 0)
4167      (or (cdr (assq (mail-header-number h2)
4168                     gnus-newsgroup-scored))
4169          gnus-summary-default-score 0)))
4170
4171 (defun gnus-thread-sort-by-score (h1 h2)
4172   "Sort threads by root article score."
4173   (gnus-article-sort-by-score
4174    (gnus-thread-header h1) (gnus-thread-header h2)))
4175
4176 (defun gnus-thread-sort-by-total-score (h1 h2)
4177   "Sort threads by the sum of all scores in the thread.
4178 Unscored articles will be counted as having a score of zero."
4179   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4180
4181 (defun gnus-thread-total-score (thread)
4182   ;; This function find the total score of THREAD.
4183   (cond
4184    ((null thread)
4185     0)
4186    ((consp thread)
4187     (if (stringp (car thread))
4188         (apply gnus-thread-score-function 0
4189                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4190       (gnus-thread-total-score-1 thread)))
4191    (t
4192     (gnus-thread-total-score-1 (list thread)))))
4193
4194 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4195   "Sort threads such that the thread with the most recently arrived article comes first."
4196   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4197
4198 (defun gnus-thread-highest-number (thread)
4199   "Return the highest article number in THREAD."
4200   (apply 'max (mapcar (lambda (header)
4201                         (mail-header-number header))
4202                       (message-flatten-list thread))))
4203
4204 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4205   "Sort threads such that the thread with the most recently dated article comes first."
4206   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4207
4208 (defun gnus-thread-latest-date (thread)
4209   "Return the highest article date in THREAD."
4210   (let ((previous-time 0))
4211     (apply 'max (mapcar
4212                  (lambda (header)
4213                    (setq previous-time
4214                          (time-to-seconds
4215                           (mail-header-parse-date
4216                            (condition-case ()
4217                                (mail-header-date header)
4218                              (error previous-time))))))
4219                  (sort
4220                   (message-flatten-list thread)
4221                   (lambda (h1 h2)
4222                     (< (mail-header-number h1)
4223                        (mail-header-number h2))))))))
4224
4225 (defun gnus-thread-total-score-1 (root)
4226   ;; This function find the total score of the thread below ROOT.
4227   (setq root (car root))
4228   (apply gnus-thread-score-function
4229          (or (append
4230               (mapcar 'gnus-thread-total-score
4231                       (cdr (gnus-id-to-thread (mail-header-id root))))
4232               (when (> (mail-header-number root) 0)
4233                 (list (or (cdr (assq (mail-header-number root)
4234                                      gnus-newsgroup-scored))
4235                           gnus-summary-default-score 0))))
4236              (list gnus-summary-default-score)
4237              '(0))))
4238
4239 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4240 (defvar gnus-tmp-prev-subject nil)
4241 (defvar gnus-tmp-false-parent nil)
4242 (defvar gnus-tmp-root-expunged nil)
4243 (defvar gnus-tmp-dummy-line nil)
4244
4245 (eval-when-compile (defvar gnus-tmp-header))
4246 (defun gnus-extra-header (type &optional header)
4247   "Return the extra header of TYPE."
4248   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4249       ""))
4250
4251 (defvar gnus-tmp-thread-tree-header-string "")
4252
4253 (defvar gnus-sum-thread-tree-root "> "
4254   "With %B spec, used for the root of a thread.
4255 If nil, use subject instead.")
4256 (defvar gnus-sum-thread-tree-single-indent ""
4257   "With %B spec, used for a thread with just one message.
4258 If nil, use subject instead.")
4259 (defvar gnus-sum-thread-tree-vertical "| "
4260   "With %B spec, used for drawing a vertical line.")
4261 (defvar gnus-sum-thread-tree-indent "  "
4262   "With %B spec, used for indenting.")
4263 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4264   "With %B spec, used for a leaf with brothers.")
4265 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4266   "With %B spec, used for a leaf without brothers.")
4267
4268 (defun gnus-summary-prepare-threads (threads)
4269   "Prepare summary buffer from THREADS and indentation LEVEL.
4270 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4271 or a straight list of headers."
4272   (gnus-message 7 "Generating summary...")
4273
4274   (setq gnus-newsgroup-threads threads)
4275   (beginning-of-line)
4276
4277   (let ((gnus-tmp-level 0)
4278         (default-score (or gnus-summary-default-score 0))
4279         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4280         thread number subject stack state gnus-tmp-gathered beg-match
4281         new-roots gnus-tmp-new-adopts thread-end
4282         gnus-tmp-header gnus-tmp-unread
4283         gnus-tmp-replied gnus-tmp-subject-or-nil
4284         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4285         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4286         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4287         tree-stack)
4288
4289     (setq gnus-tmp-prev-subject nil)
4290
4291     (if (vectorp (car threads))
4292         ;; If this is a straight (sic) list of headers, then a
4293         ;; threaded summary display isn't required, so we just create
4294         ;; an unthreaded one.
4295         (gnus-summary-prepare-unthreaded threads)
4296
4297       ;; Do the threaded display.
4298
4299       (while (or threads stack gnus-tmp-new-adopts new-roots)
4300
4301         (if (and (= gnus-tmp-level 0)
4302                  (or (not stack)
4303                      (= (caar stack) 0))
4304                  (not gnus-tmp-false-parent)
4305                  (or gnus-tmp-new-adopts new-roots))
4306             (if gnus-tmp-new-adopts
4307                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4308                       thread (list (car gnus-tmp-new-adopts))
4309                       gnus-tmp-header (caar thread)
4310                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4311               (when new-roots
4312                 (setq thread (list (car new-roots))
4313                       gnus-tmp-header (caar thread)
4314                       new-roots (cdr new-roots))))
4315
4316           (if threads
4317               ;; If there are some threads, we do them before the
4318               ;; threads on the stack.
4319               (setq thread threads
4320                     gnus-tmp-header (caar thread))
4321             ;; There were no current threads, so we pop something off
4322             ;; the stack.
4323             (setq state (car stack)
4324                   gnus-tmp-level (car state)
4325                   tree-stack (cadr state)
4326                   thread (caddr state)
4327                   stack (cdr stack)
4328                   gnus-tmp-header (caar thread))))
4329
4330         (setq gnus-tmp-false-parent nil)
4331         (setq gnus-tmp-root-expunged nil)
4332         (setq thread-end nil)
4333
4334         (if (stringp gnus-tmp-header)
4335             ;; The header is a dummy root.
4336             (cond
4337              ((eq gnus-summary-make-false-root 'adopt)
4338               ;; We let the first article adopt the rest.
4339               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4340                                                (cddar thread)))
4341               (setq gnus-tmp-gathered
4342                     (nconc (mapcar
4343                             (lambda (h) (mail-header-number (car h)))
4344                             (cddar thread))
4345                            gnus-tmp-gathered))
4346               (setq thread (cons (list (caar thread)
4347                                        (cadar thread))
4348                                  (cdr thread)))
4349               (setq gnus-tmp-level -1
4350                     gnus-tmp-false-parent t))
4351              ((eq gnus-summary-make-false-root 'empty)
4352               ;; We print adopted articles with empty subject fields.
4353               (setq gnus-tmp-gathered
4354                     (nconc (mapcar
4355                             (lambda (h) (mail-header-number (car h)))
4356                             (cddar thread))
4357                            gnus-tmp-gathered))
4358               (setq gnus-tmp-level -1))
4359              ((eq gnus-summary-make-false-root 'dummy)
4360               ;; We remember that we probably want to output a dummy
4361               ;; root.
4362               (setq gnus-tmp-dummy-line gnus-tmp-header)
4363               (setq gnus-tmp-prev-subject gnus-tmp-header))
4364              (t
4365               ;; We do not make a root for the gathered
4366               ;; sub-threads at all.
4367               (setq gnus-tmp-level -1)))
4368
4369           (setq number (mail-header-number gnus-tmp-header)
4370                 subject (mail-header-subject gnus-tmp-header))
4371
4372           (cond
4373            ;; If the thread has changed subject, we might want to make
4374            ;; this subthread into a root.
4375            ((and (null gnus-thread-ignore-subject)
4376                  (not (zerop gnus-tmp-level))
4377                  gnus-tmp-prev-subject
4378                  (not (inline
4379                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4380             (setq new-roots (nconc new-roots (list (car thread)))
4381                   thread-end t
4382                   gnus-tmp-header nil))
4383            ;; If the article lies outside the current limit,
4384            ;; then we do not display it.
4385            ((not (memq number gnus-newsgroup-limit))
4386             (setq gnus-tmp-gathered
4387                   (nconc (mapcar
4388                           (lambda (h) (mail-header-number (car h)))
4389                           (cdar thread))
4390                          gnus-tmp-gathered))
4391             (setq gnus-tmp-new-adopts (if (cdar thread)
4392                                           (append gnus-tmp-new-adopts
4393                                                   (cdar thread))
4394                                         gnus-tmp-new-adopts)
4395                   thread-end t
4396                   gnus-tmp-header nil)
4397             (when (zerop gnus-tmp-level)
4398               (setq gnus-tmp-root-expunged t)))
4399            ;; Perhaps this article is to be marked as read?
4400            ((and gnus-summary-mark-below
4401                  (< (or (cdr (assq number gnus-newsgroup-scored))
4402                         default-score)
4403                     gnus-summary-mark-below)
4404                  ;; Don't touch sparse articles.
4405                  (not (gnus-summary-article-sparse-p number))
4406                  (not (gnus-summary-article-ancient-p number)))
4407             (setq gnus-newsgroup-unreads
4408                   (delq number gnus-newsgroup-unreads))
4409             (if gnus-newsgroup-auto-expire
4410                 (push number gnus-newsgroup-expirable)
4411               (push (cons number gnus-low-score-mark)
4412                     gnus-newsgroup-reads))))
4413
4414           (when gnus-tmp-header
4415             ;; We may have an old dummy line to output before this
4416             ;; article.
4417             (when (and gnus-tmp-dummy-line
4418                        (gnus-subject-equal
4419                         gnus-tmp-dummy-line
4420                         (mail-header-subject gnus-tmp-header)))
4421               (gnus-summary-insert-dummy-line
4422                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4423               (setq gnus-tmp-dummy-line nil))
4424
4425             ;; Compute the mark.
4426             (setq gnus-tmp-unread (gnus-article-mark number))
4427
4428             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4429                                   gnus-tmp-header gnus-tmp-level)
4430                   gnus-newsgroup-data)
4431
4432             ;; Actually insert the line.
4433             (setq
4434              gnus-tmp-subject-or-nil
4435              (cond
4436               ((and gnus-thread-ignore-subject
4437                     gnus-tmp-prev-subject
4438                     (not (inline (gnus-subject-equal
4439                                   gnus-tmp-prev-subject subject))))
4440                subject)
4441               ((zerop gnus-tmp-level)
4442                (if (and (eq gnus-summary-make-false-root 'empty)
4443                         (memq number gnus-tmp-gathered)
4444                         gnus-tmp-prev-subject
4445                         (inline (gnus-subject-equal
4446                                  gnus-tmp-prev-subject subject)))
4447                    gnus-summary-same-subject
4448                  subject))
4449               (t gnus-summary-same-subject)))
4450             (if (and (eq gnus-summary-make-false-root 'adopt)
4451                      (= gnus-tmp-level 1)
4452                      (memq number gnus-tmp-gathered))
4453                 (setq gnus-tmp-opening-bracket ?\<
4454                       gnus-tmp-closing-bracket ?\>)
4455               (setq gnus-tmp-opening-bracket ?\[
4456                     gnus-tmp-closing-bracket ?\]))
4457             (setq
4458              gnus-tmp-indentation
4459              (aref gnus-thread-indent-array gnus-tmp-level)
4460              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4461              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4462                                 gnus-summary-default-score 0)
4463              gnus-tmp-score-char
4464              (if (or (null gnus-summary-default-score)
4465                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4466                          gnus-summary-zcore-fuzz))
4467                  ?                      ;Whitespace
4468                (if (< gnus-tmp-score gnus-summary-default-score)
4469                    gnus-score-below-mark gnus-score-over-mark))
4470              gnus-tmp-replied
4471              (cond ((memq number gnus-newsgroup-processable)
4472                     gnus-process-mark)
4473                    ((memq number gnus-newsgroup-cached)
4474                     gnus-cached-mark)
4475                    ((memq number gnus-newsgroup-replied)
4476                     gnus-replied-mark)
4477                    ((memq number gnus-newsgroup-forwarded)
4478                     gnus-forwarded-mark)
4479                    ((memq number gnus-newsgroup-saved)
4480                     gnus-saved-mark)
4481                    ((memq number gnus-newsgroup-recent)
4482                     gnus-recent-mark)
4483                    ((memq number gnus-newsgroup-unseen)
4484                     gnus-unseen-mark)
4485                    (t gnus-no-mark))
4486              gnus-tmp-from (mail-header-from gnus-tmp-header)
4487              gnus-tmp-name
4488              (cond
4489               ((string-match "<[^>]+> *$" gnus-tmp-from)
4490                (setq beg-match (match-beginning 0))
4491                (or (and (string-match "^\".+\"" gnus-tmp-from)
4492                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4493                    (substring gnus-tmp-from 0 beg-match)))
4494               ((string-match "(.+)" gnus-tmp-from)
4495                (substring gnus-tmp-from
4496                           (1+ (match-beginning 0)) (1- (match-end 0))))
4497               (t gnus-tmp-from))
4498              gnus-tmp-thread-tree-header-string
4499              (cond
4500               ((not gnus-show-threads) "")
4501               ((zerop gnus-tmp-level)
4502                (if (cdar thread)
4503                    (or gnus-sum-thread-tree-root subject)
4504                  (or gnus-sum-thread-tree-single-indent subject)))
4505               (t
4506                (concat (apply 'concat
4507                               (mapcar (lambda (item)
4508                                         (if (= item 1)
4509                                             gnus-sum-thread-tree-vertical
4510                                           gnus-sum-thread-tree-indent))
4511                                       (cdr (reverse tree-stack))))
4512                        (if (nth 1 thread)
4513                            gnus-sum-thread-tree-leaf-with-other
4514                          gnus-sum-thread-tree-single-leaf)))))
4515             (when (string= gnus-tmp-name "")
4516               (setq gnus-tmp-name gnus-tmp-from))
4517             (unless (numberp gnus-tmp-lines)
4518               (setq gnus-tmp-lines -1))
4519             (if (= gnus-tmp-lines -1)
4520                 (setq gnus-tmp-lines "?")
4521               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4522             (gnus-put-text-property
4523              (point)
4524              (progn (eval gnus-summary-line-format-spec) (point))
4525              'gnus-number number)
4526             (when gnus-visual-p
4527               (forward-line -1)
4528               (gnus-run-hooks 'gnus-summary-update-hook)
4529               (forward-line 1))
4530
4531             (setq gnus-tmp-prev-subject subject)))
4532
4533         (when (nth 1 thread)
4534           (push (list (max 0 gnus-tmp-level)
4535                       (copy-list tree-stack)
4536                       (nthcdr 1 thread))
4537                 stack))
4538         (push (if (nth 1 thread) 1 0) tree-stack)
4539         (incf gnus-tmp-level)
4540         (setq threads (if thread-end nil (cdar thread)))
4541         (unless threads
4542           (setq gnus-tmp-level 0)))))
4543   (gnus-message 7 "Generating summary...done"))
4544
4545 (defun gnus-summary-prepare-unthreaded (headers)
4546   "Generate an unthreaded summary buffer based on HEADERS."
4547   (let (header number mark)
4548
4549     (beginning-of-line)
4550
4551     (while headers
4552       ;; We may have to root out some bad articles...
4553       (when (memq (setq number (mail-header-number
4554                                 (setq header (pop headers))))
4555                   gnus-newsgroup-limit)
4556         ;; Mark article as read when it has a low score.
4557         (when (and gnus-summary-mark-below
4558                    (< (or (cdr (assq number gnus-newsgroup-scored))
4559                           gnus-summary-default-score 0)
4560                       gnus-summary-mark-below)
4561                    (not (gnus-summary-article-ancient-p number)))
4562           (setq gnus-newsgroup-unreads
4563                 (delq number gnus-newsgroup-unreads))
4564           (if gnus-newsgroup-auto-expire
4565               (push number gnus-newsgroup-expirable)
4566             (push (cons number gnus-low-score-mark)
4567                   gnus-newsgroup-reads)))
4568
4569         (setq mark (gnus-article-mark number))
4570         (push (gnus-data-make number mark (1+ (point)) header 0)
4571               gnus-newsgroup-data)
4572         (gnus-summary-insert-line
4573          header 0 number
4574          mark (memq number gnus-newsgroup-replied)
4575          (memq number gnus-newsgroup-expirable)
4576          (mail-header-subject header) nil
4577          (cdr (assq number gnus-newsgroup-scored))
4578          (memq number gnus-newsgroup-processable))))))
4579
4580 (defun gnus-summary-remove-list-identifiers ()
4581   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4582   (let ((regexp (if (consp gnus-list-identifiers)
4583                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4584                   gnus-list-identifiers))
4585         changed subject)
4586     (when regexp
4587       (dolist (header gnus-newsgroup-headers)
4588         (setq subject (mail-header-subject header)
4589               changed nil)
4590         (while (string-match
4591                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4592                 subject)
4593           (setq subject
4594                 (concat (substring subject 0 (match-beginning 2))
4595                         (substring subject (match-end 0)))
4596                 changed t))
4597         (when (and changed
4598                    (string-match
4599                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4600           (setq subject
4601                 (concat (substring subject 0 (match-beginning 1))
4602                         (substring subject (match-end 1)))))
4603         (when changed
4604           (mail-header-set-subject header subject))))))
4605
4606 (defun gnus-fetch-headers (articles)
4607   "Fetch headers of ARTICLES."
4608   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4609     (gnus-message 5 "Fetching headers for %s..." name)
4610     (prog1
4611         (if (eq 'nov
4612                 (setq gnus-headers-retrieved-by
4613                       (gnus-retrieve-headers
4614                        articles gnus-newsgroup-name
4615                        ;; We might want to fetch old headers, but
4616                        ;; not if there is only 1 article.
4617                        (and (or (and
4618                                  (not (eq gnus-fetch-old-headers 'some))
4619                                  (not (numberp gnus-fetch-old-headers)))
4620                                 (> (length articles) 1))
4621                             gnus-fetch-old-headers))))
4622             (gnus-get-newsgroup-headers-xover
4623              articles nil nil gnus-newsgroup-name t)
4624           (gnus-get-newsgroup-headers))
4625       (gnus-message 5 "Fetching headers for %s...done" name))))
4626
4627 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4628   "Select newsgroup GROUP.
4629 If READ-ALL is non-nil, all articles in the group are selected.
4630 If SELECT-ARTICLES, only select those articles from GROUP."
4631   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4632          ;;!!! Dirty hack; should be removed.
4633          (gnus-summary-ignore-duplicates
4634           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4635               t
4636             gnus-summary-ignore-duplicates))
4637          (info (nth 2 entry))
4638          articles fetched-articles cached)
4639
4640     (unless (gnus-check-server
4641              (set (make-local-variable 'gnus-current-select-method)
4642                   (gnus-find-method-for-group group)))
4643       (error "Couldn't open server"))
4644
4645     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4646         (gnus-activate-group group)     ; Or we can activate it...
4647         (progn                          ; Or we bug out.
4648           (when (equal major-mode 'gnus-summary-mode)
4649             (kill-buffer (current-buffer)))
4650           (error "Couldn't activate group %s: %s"
4651                  group (gnus-status-message group))))
4652
4653     (unless (gnus-request-group group t)
4654       (when (equal major-mode 'gnus-summary-mode)
4655         (kill-buffer (current-buffer)))
4656       (error "Couldn't request group %s: %s"
4657              group (gnus-status-message group)))
4658
4659     (setq gnus-newsgroup-name group
4660           gnus-newsgroup-unselected nil
4661           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4662
4663     (let ((display (gnus-group-find-parameter group 'display)))
4664       (setq gnus-newsgroup-display
4665             (cond
4666              ((not (zerop (or (car-safe read-all) 0)))
4667               ;; The user entered the group with C-u SPC/RET, let's show
4668               ;; all articles.
4669               'gnus-not-ignore)
4670              ((eq display 'all)
4671               'gnus-not-ignore)
4672              ((arrayp display)
4673               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4674              ((numberp display)
4675               ;; The following is probably the "correct" solution, but
4676               ;; it makes Gnus fetch all headers and then limit the
4677               ;; articles (which is slow), so instead we hack the
4678               ;; select-articles parameter instead. -- Simon Josefsson
4679               ;; <jas@kth.se>
4680               ;;
4681               ;; (gnus-byte-compile
4682               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4683               ;;                         display)))))
4684               (setq select-articles
4685                     (gnus-uncompress-range
4686                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4687                              (if (> tmp 0)
4688                                  tmp
4689                                1))
4690                            (cdr (gnus-active group)))))
4691               nil)
4692              (t
4693               nil))))
4694
4695     (gnus-summary-setup-default-charset)
4696
4697     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4698     (when (gnus-virtual-group-p group)
4699       (setq cached gnus-newsgroup-cached))
4700
4701     (setq gnus-newsgroup-unreads
4702           (gnus-set-difference
4703            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4704            gnus-newsgroup-dormant))
4705
4706     (setq gnus-newsgroup-processable nil)
4707
4708     (gnus-update-read-articles group gnus-newsgroup-unreads)
4709
4710     ;; Adjust and set lists of article marks.
4711     (when info
4712       (gnus-adjust-marked-articles info))
4713
4714     (if (setq articles select-articles)
4715         (setq gnus-newsgroup-unselected
4716               (gnus-sorted-intersection
4717                gnus-newsgroup-unreads
4718                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4719       (setq articles (gnus-articles-to-read group read-all)))
4720
4721     (cond
4722      ((null articles)
4723       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4724       'quit)
4725      ((eq articles 0) nil)
4726      (t
4727       ;; Init the dependencies hash table.
4728       (setq gnus-newsgroup-dependencies
4729             (gnus-make-hashtable (length articles)))
4730       (gnus-set-global-variables)
4731       ;; Retrieve the headers and read them in.
4732       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4733
4734       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4735       (when cached
4736         (setq gnus-newsgroup-cached cached))
4737
4738       ;; Suppress duplicates?
4739       (when gnus-suppress-duplicates
4740         (gnus-dup-suppress-articles))
4741
4742       ;; Set the initial limit.
4743       (setq gnus-newsgroup-limit (copy-sequence articles))
4744       ;; Remove canceled articles from the list of unread articles.
4745       (setq fetched-articles
4746             (mapcar (lambda (headers) (mail-header-number headers))
4747                     gnus-newsgroup-headers))
4748       (setq gnus-newsgroup-articles fetched-articles)
4749       (setq gnus-newsgroup-unreads
4750             (gnus-set-sorted-intersection
4751              gnus-newsgroup-unreads fetched-articles))
4752       (gnus-compute-unseen-list)
4753
4754       ;; Removed marked articles that do not exist.
4755       (gnus-update-missing-marks
4756        (gnus-sorted-complement fetched-articles articles))
4757       ;; We might want to build some more threads first.
4758       (when (and gnus-fetch-old-headers
4759                  (eq gnus-headers-retrieved-by 'nov))
4760         (if (eq gnus-fetch-old-headers 'invisible)
4761             (gnus-build-all-threads)
4762           (gnus-build-old-threads)))
4763       ;; Let the Gnus agent mark articles as read.
4764       (when gnus-agent
4765         (gnus-agent-get-undownloaded-list))
4766       ;; Remove list identifiers from subject
4767       (when gnus-list-identifiers
4768         (gnus-summary-remove-list-identifiers))
4769       ;; Check whether auto-expire is to be done in this group.
4770       (setq gnus-newsgroup-auto-expire
4771             (gnus-group-auto-expirable-p group))
4772       ;; Set up the article buffer now, if necessary.
4773       (unless gnus-single-article-buffer
4774         (gnus-article-setup-buffer))
4775       ;; First and last article in this newsgroup.
4776       (when gnus-newsgroup-headers
4777         (setq gnus-newsgroup-begin
4778               (mail-header-number (car gnus-newsgroup-headers))
4779               gnus-newsgroup-end
4780               (mail-header-number
4781                (gnus-last-element gnus-newsgroup-headers))))
4782       ;; GROUP is successfully selected.
4783       (or gnus-newsgroup-headers t)))))
4784
4785 (defun gnus-compute-unseen-list ()
4786   ;; The `seen' marks are treated specially.
4787   (if (not gnus-newsgroup-seen)
4788       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4789     (setq gnus-newsgroup-unseen
4790           (gnus-inverse-list-range-intersection
4791            gnus-newsgroup-articles gnus-newsgroup-seen))))
4792
4793 (defun gnus-summary-display-make-predicate (display)
4794   (require 'gnus-agent)
4795   (when (= (length display) 1)
4796     (setq display (car display)))
4797   (unless gnus-summary-display-cache
4798     (dolist (elem (append (list (cons 'read 'read)
4799                                 (cons 'unseen 'unseen))
4800                           gnus-article-mark-lists))
4801       (push (cons (cdr elem)
4802                   (gnus-byte-compile
4803                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4804             gnus-summary-display-cache)))
4805   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4806     (gnus-get-predicate display)))
4807
4808 ;; Uses the dynamically bound `number' variable.
4809 (defvar number)
4810 (defun gnus-article-marked-p (type &optional article)
4811   (let ((article (or article number)))
4812     (cond
4813      ((eq type 'tick)
4814       (memq article gnus-newsgroup-marked))
4815      ((eq type 'unsend)
4816       (memq article gnus-newsgroup-unsendable))
4817      ((eq type 'undownload)
4818       (memq article gnus-newsgroup-undownloaded))
4819      ((eq type 'download)
4820       (memq article gnus-newsgroup-downloadable))
4821      ((eq type 'unread)
4822       (memq article gnus-newsgroup-unreads))
4823      ((eq type 'read)
4824       (memq article gnus-newsgroup-reads))
4825      ((eq type 'dormant)
4826       (memq article gnus-newsgroup-dormant) )
4827      ((eq type 'expire)
4828       (memq article gnus-newsgroup-expirable))
4829      ((eq type 'reply)
4830       (memq article gnus-newsgroup-replied))
4831      ((eq type 'killed)
4832       (memq article gnus-newsgroup-killed))
4833      ((eq type 'bookmark)
4834       (assq article gnus-newsgroup-bookmarks))
4835      ((eq type 'score)
4836       (assq article gnus-newsgroup-scored))
4837      ((eq type 'save)
4838       (memq article gnus-newsgroup-saved))
4839      ((eq type 'cache)
4840       (memq article gnus-newsgroup-cached))
4841      ((eq type 'forward)
4842       (memq article gnus-newsgroup-forwarded))
4843      ((eq type 'seen)
4844       (not (memq article gnus-newsgroup-unseen)))
4845      ((eq type 'recent)
4846       (memq article gnus-newsgroup-recent))
4847      (t t))))
4848
4849 (defun gnus-articles-to-read (group &optional read-all)
4850   "Find out what articles the user wants to read."
4851   (let* ((articles
4852           ;; Select all articles if `read-all' is non-nil, or if there
4853           ;; are no unread articles.
4854           (if (or read-all
4855                   (and (zerop (length gnus-newsgroup-marked))
4856                        (zerop (length gnus-newsgroup-unreads)))
4857                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4858               ;; We want to select the headers for all the articles in
4859               ;; the group, so we select either all the active
4860               ;; articles in the group, or (if that's nil), the
4861               ;; articles in the cache.
4862               (or
4863                (gnus-uncompress-range (gnus-active group))
4864                (gnus-cache-articles-in-group group))
4865             ;; Select only the "normal" subset of articles.
4866             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4867                           (copy-sequence gnus-newsgroup-unreads))
4868                   '<)))
4869          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4870          (scored (length scored-list))
4871          (number (length articles))
4872          (marked (+ (length gnus-newsgroup-marked)
4873                     (length gnus-newsgroup-dormant)))
4874          (select
4875           (cond
4876            ((numberp read-all)
4877             read-all)
4878            (t
4879             (condition-case ()
4880                 (cond
4881                  ((and (or (<= scored marked) (= scored number))
4882                        (numberp gnus-large-newsgroup)
4883                        (> number gnus-large-newsgroup))
4884                   (let ((input
4885                          (read-string
4886                           (format
4887                            "How many articles from %s (default %d): "
4888                            (gnus-limit-string
4889                             (gnus-group-decoded-name gnus-newsgroup-name)
4890                             35)
4891                            number))))
4892                     (if (string-match "^[ \t]*$" input) number input)))
4893                  ((and (> scored marked) (< scored number)
4894                        (> (- scored number) 20))
4895                   (let ((input
4896                          (read-string
4897                           (format "%s %s (%d scored, %d total): "
4898                                   "How many articles from"
4899                                   (gnus-group-decoded-name group)
4900                                   scored number))))
4901                     (if (string-match "^[ \t]*$" input)
4902                         number input)))
4903                  (t number))
4904               (quit
4905                (message "Quit getting the articles to read")
4906                nil))))))
4907     (setq select (if (stringp select) (string-to-number select) select))
4908     (if (or (null select) (zerop select))
4909         select
4910       (if (and (not (zerop scored)) (<= (abs select) scored))
4911           (progn
4912             (setq articles (sort scored-list '<))
4913             (setq number (length articles)))
4914         (setq articles (copy-sequence articles)))
4915
4916       (when (< (abs select) number)
4917         (if (< select 0)
4918             ;; Select the N oldest articles.
4919             (setcdr (nthcdr (1- (abs select)) articles) nil)
4920           ;; Select the N most recent articles.
4921           (setq articles (nthcdr (- number select) articles))))
4922       (setq gnus-newsgroup-unselected
4923             (gnus-sorted-intersection
4924              gnus-newsgroup-unreads
4925              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4926       (when gnus-alter-articles-to-read-function
4927         (setq gnus-newsgroup-unreads
4928               (sort
4929                (funcall gnus-alter-articles-to-read-function
4930                         gnus-newsgroup-name gnus-newsgroup-unreads)
4931                '<)))
4932       articles)))
4933
4934 (defun gnus-killed-articles (killed articles)
4935   (let (out)
4936     (while articles
4937       (when (inline (gnus-member-of-range (car articles) killed))
4938         (push (car articles) out))
4939       (setq articles (cdr articles)))
4940     out))
4941
4942 (defun gnus-uncompress-marks (marks)
4943   "Uncompress the mark ranges in MARKS."
4944   (let ((uncompressed '(score bookmark))
4945         out)
4946     (while marks
4947       (if (memq (caar marks) uncompressed)
4948           (push (car marks) out)
4949         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4950       (setq marks (cdr marks)))
4951     out))
4952
4953 (defun gnus-article-mark-to-type (mark)
4954   "Return the type of MARK."
4955   (or (cadr (assq mark gnus-article-special-mark-lists))
4956       'list))
4957
4958 (defun gnus-article-unpropagatable-p (mark)
4959   "Return whether MARK should be propagated to backend."
4960   (memq mark gnus-article-unpropagated-mark-lists))
4961
4962 (defun gnus-adjust-marked-articles (info)
4963   "Set all article lists and remove all marks that are no longer valid."
4964   (let* ((marked-lists (gnus-info-marks info))
4965          (active (gnus-active (gnus-info-group info)))
4966          (min (car active))
4967          (max (cdr active))
4968          (types gnus-article-mark-lists)
4969          marks var articles article mark mark-type)
4970
4971     (dolist (marks marked-lists)
4972       (setq mark (car marks)
4973             mark-type (gnus-article-mark-to-type mark)
4974             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4975
4976       ;; We set the variable according to the type of the marks list,
4977       ;; and then adjust the marks to a subset of the active articles.
4978       (cond
4979        ;; Adjust "simple" lists.
4980        ((eq mark-type 'list)
4981         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4982         (when (memq mark '(tick dormant expire reply save))
4983           (while articles
4984             (when (or (< (setq article (pop articles)) min) (> article max))
4985               (set var (delq article (symbol-value var)))))))
4986        ;; Adjust assocs.
4987        ((eq mark-type 'tuple)
4988         (set var (setq articles (cdr marks)))
4989         (when (not (listp (cdr (symbol-value var))))
4990           (set var (list (symbol-value var))))
4991         (when (not (listp (cdr articles)))
4992           (setq articles (list articles)))
4993         (while articles
4994           (when (or (not (consp (setq article (pop articles))))
4995                     (< (car article) min)
4996                     (> (car article) max))
4997             (set var (delq article (symbol-value var))))))
4998        ;; Adjust ranges (sloppily).
4999        ((eq mark-type 'range)
5000         (cond
5001          ((eq mark 'seen)
5002           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5003           ;; It should be (seen (NUM1 . NUM2)).
5004           (when (numberp (cddr marks))
5005             (setcdr marks (list (cdr marks))))
5006           (setq articles (cdr marks))
5007           (while (and articles
5008                       (or (and (consp (car articles))
5009                                (> min (cdar articles)))
5010                           (and (numberp (car articles))
5011                                (> min (car articles)))))
5012             (pop articles))
5013           (set var articles))))))))
5014
5015 (defun gnus-update-missing-marks (missing)
5016   "Go through the list of MISSING articles and remove them from the mark lists."
5017   (when missing
5018     (let (var m)
5019       ;; Go through all types.
5020       (dolist (elem gnus-article-mark-lists)
5021         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5022           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5023           (when (symbol-value var)
5024             ;; This list has articles.  So we delete all missing
5025             ;; articles from it.
5026             (setq m missing)
5027             (while m
5028               (set var (delq (pop m) (symbol-value var))))))))))
5029
5030 (defun gnus-update-marks ()
5031   "Enter the various lists of marked articles into the newsgroup info list."
5032   (let ((types gnus-article-mark-lists)
5033         (info (gnus-get-info gnus-newsgroup-name))
5034         type list newmarked symbol delta-marks)
5035     (when info
5036       ;; Add all marks lists to the list of marks lists.
5037       (while (setq type (pop types))
5038         (setq list (symbol-value
5039                     (setq symbol
5040                           (intern (format "gnus-newsgroup-%s" (car type))))))
5041
5042         (when list
5043           ;; Get rid of the entries of the articles that have the
5044           ;; default score.
5045           (when (and (eq (cdr type) 'score)
5046                      gnus-save-score
5047                      list)
5048             (let* ((arts list)
5049                    (prev (cons nil list))
5050                    (all prev))
5051               (while arts
5052                 (if (or (not (consp (car arts)))
5053                         (= (cdar arts) gnus-summary-default-score))
5054                     (setcdr prev (cdr arts))
5055                   (setq prev arts))
5056                 (setq arts (cdr arts)))
5057               (setq list (cdr all)))))
5058
5059         (when (eq (cdr type) 'seen)
5060           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5061
5062         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5063           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5064
5065         (when (and (gnus-check-backend-function
5066                     'request-set-mark gnus-newsgroup-name)
5067                    (not (gnus-article-unpropagatable-p (cdr type))))
5068           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5069                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5070                  (add (gnus-remove-from-range
5071                        (gnus-copy-sequence list) old)))
5072             (when add
5073               (push (list add 'add (list (cdr type))) delta-marks))
5074             (when del
5075               (push (list del 'del (list (cdr type))) delta-marks))))
5076
5077         (when list
5078           (push (cons (cdr type) list) newmarked)))
5079
5080       (when delta-marks
5081         (unless (gnus-check-group gnus-newsgroup-name)
5082           (error "Can't open server for %s" gnus-newsgroup-name))
5083         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5084
5085       ;; Enter these new marks into the info of the group.
5086       (if (nthcdr 3 info)
5087           (setcar (nthcdr 3 info) newmarked)
5088         ;; Add the marks lists to the end of the info.
5089         (when newmarked
5090           (setcdr (nthcdr 2 info) (list newmarked))))
5091
5092       ;; Cut off the end of the info if there's nothing else there.
5093       (let ((i 5))
5094         (while (and (> i 2)
5095                     (not (nth i info)))
5096           (when (nthcdr (decf i) info)
5097             (setcdr (nthcdr i info) nil)))))))
5098
5099 (defun gnus-set-mode-line (where)
5100   "Set the mode line of the article or summary buffers.
5101 If WHERE is `summary', the summary mode line format will be used."
5102   ;; Is this mode line one we keep updated?
5103   (when (and (memq where gnus-updated-mode-lines)
5104              (symbol-value
5105               (intern (format "gnus-%s-mode-line-format-spec" where))))
5106     (let (mode-string)
5107       (save-excursion
5108         ;; We evaluate this in the summary buffer since these
5109         ;; variables are buffer-local to that buffer.
5110         (set-buffer gnus-summary-buffer)
5111        ;; We bind all these variables that are used in the `eval' form
5112         ;; below.
5113         (let* ((mformat (symbol-value
5114                          (intern
5115                           (format "gnus-%s-mode-line-format-spec" where))))
5116                (gnus-tmp-group-name (gnus-group-decoded-name
5117                                      gnus-newsgroup-name))
5118                (gnus-tmp-article-number (or gnus-current-article 0))
5119                (gnus-tmp-unread gnus-newsgroup-unreads)
5120                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5121                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5122                (gnus-tmp-unread-and-unselected
5123                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5124                             (zerop gnus-tmp-unselected))
5125                        "")
5126                       ((zerop gnus-tmp-unselected)
5127                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5128                       (t (format "{%d(+%d) more}"
5129                                  gnus-tmp-unread-and-unticked
5130                                  gnus-tmp-unselected))))
5131                (gnus-tmp-subject
5132                 (if (and gnus-current-headers
5133                          (vectorp gnus-current-headers))
5134                     (gnus-mode-string-quote
5135                      (mail-header-subject gnus-current-headers))
5136                   ""))
5137                bufname-length max-len
5138                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5139           (setq mode-string (eval mformat))
5140           (setq bufname-length (if (string-match "%b" mode-string)
5141                                    (- (length
5142                                        (buffer-name
5143                                         (if (eq where 'summary)
5144                                             nil
5145                                           (get-buffer gnus-article-buffer))))
5146                                       2)
5147                                  0))
5148           (setq max-len (max 4 (if gnus-mode-non-string-length
5149                                    (- (window-width)
5150                                       gnus-mode-non-string-length
5151                                       bufname-length)
5152                                  (length mode-string))))
5153           ;; We might have to chop a bit of the string off...
5154           (when (> (length mode-string) max-len)
5155             (setq mode-string
5156                   (concat (truncate-string-to-width mode-string (- max-len 3))
5157                           "...")))
5158           ;; Pad the mode string a bit.
5159           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5160       ;; Update the mode line.
5161       (setq mode-line-buffer-identification
5162             (gnus-mode-line-buffer-identification (list mode-string)))
5163       (set-buffer-modified-p t))))
5164
5165 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5166   "Go through the HEADERS list and add all Xrefs to a hash table.
5167 The resulting hash table is returned, or nil if no Xrefs were found."
5168   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5169          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5170          (xref-hashtb (gnus-make-hashtable))
5171          start group entry number xrefs header)
5172     (while headers
5173       (setq header (pop headers))
5174       (when (and (setq xrefs (mail-header-xref header))
5175                  (not (memq (setq number (mail-header-number header))
5176                             unreads)))
5177         (setq start 0)
5178         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5179           (setq start (match-end 0))
5180           (setq group (if prefix
5181                           (concat prefix (substring xrefs (match-beginning 1)
5182                                                     (match-end 1)))
5183                         (substring xrefs (match-beginning 1) (match-end 1))))
5184           (setq number
5185                 (string-to-int (substring xrefs (match-beginning 2)
5186                                           (match-end 2))))
5187           (if (setq entry (gnus-gethash group xref-hashtb))
5188               (setcdr entry (cons number (cdr entry)))
5189             (gnus-sethash group (cons number nil) xref-hashtb)))))
5190     (and start xref-hashtb)))
5191
5192 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5193   "Look through all the headers and mark the Xrefs as read."
5194   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5195         name entry info xref-hashtb idlist method nth4)
5196     (save-excursion
5197       (set-buffer gnus-group-buffer)
5198       (when (setq xref-hashtb
5199                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5200         (mapatoms
5201          (lambda (group)
5202            (unless (string= from-newsgroup (setq name (symbol-name group)))
5203              (setq idlist (symbol-value group))
5204              ;; Dead groups are not updated.
5205              (and (prog1
5206                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5207                             info (nth 2 entry))
5208                     (when (stringp (setq nth4 (gnus-info-method info)))
5209                       (setq nth4 (gnus-server-to-method nth4))))
5210                   ;; Only do the xrefs if the group has the same
5211                   ;; select method as the group we have just read.
5212                   (or (gnus-methods-equal-p
5213                        nth4 (gnus-find-method-for-group from-newsgroup))
5214                       virtual
5215                       (equal nth4 (setq method (gnus-find-method-for-group
5216                                                 from-newsgroup)))
5217                       (and (equal (car nth4) (car method))
5218                            (equal (nth 1 nth4) (nth 1 method))))
5219                   gnus-use-cross-reference
5220                   (or (not (eq gnus-use-cross-reference t))
5221                       virtual
5222                       ;; Only do cross-references on subscribed
5223                       ;; groups, if that is what is wanted.
5224                       (<= (gnus-info-level info) gnus-level-subscribed))
5225                   (gnus-group-make-articles-read name idlist))))
5226          xref-hashtb)))))
5227
5228 (defun gnus-compute-read-articles (group articles)
5229   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5230          (info (nth 2 entry))
5231          (active (gnus-active group))
5232          ninfo)
5233     (when entry
5234       ;; First peel off all invalid article numbers.
5235       (when active
5236         (let ((ids articles)
5237               id first)
5238           (while (setq id (pop ids))
5239             (when (and first (> id (cdr active)))
5240               ;; We'll end up in this situation in one particular
5241               ;; obscure situation.  If you re-scan a group and get
5242               ;; a new article that is cross-posted to a different
5243               ;; group that has not been re-scanned, you might get
5244               ;; crossposted article that has a higher number than
5245               ;; Gnus believes possible.  So we re-activate this
5246               ;; group as well.  This might mean doing the
5247               ;; crossposting thingy will *increase* the number
5248               ;; of articles in some groups.  Tsk, tsk.
5249               (setq active (or (gnus-activate-group group) active)))
5250             (when (or (> id (cdr active))
5251                       (< id (car active)))
5252               (setq articles (delq id articles))))))
5253       ;; If the read list is nil, we init it.
5254       (if (and active
5255                (null (gnus-info-read info))
5256                (> (car active) 1))
5257           (setq ninfo (cons 1 (1- (car active))))
5258         (setq ninfo (gnus-info-read info)))
5259       ;; Then we add the read articles to the range.
5260       (gnus-add-to-range
5261        ninfo (setq articles (sort articles '<))))))
5262
5263 (defun gnus-group-make-articles-read (group articles)
5264   "Update the info of GROUP to say that ARTICLES are read."
5265   (let* ((num 0)
5266          (entry (gnus-gethash group gnus-newsrc-hashtb))
5267          (info (nth 2 entry))
5268          (active (gnus-active group))
5269          range)
5270     (when entry
5271       (setq range (gnus-compute-read-articles group articles))
5272       (save-excursion
5273         (set-buffer gnus-group-buffer)
5274         (gnus-undo-register
5275           `(progn
5276              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5277              (gnus-info-set-read ',info ',(gnus-info-read info))
5278              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5279              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5280              (gnus-group-update-group ,group t))))
5281       ;; Add the read articles to the range.
5282       (gnus-info-set-read info range)
5283       (gnus-request-set-mark group (list (list range 'add '(read))))
5284       ;; Then we have to re-compute how many unread
5285       ;; articles there are in this group.
5286       (when active
5287         (cond
5288          ((not range)
5289           (setq num (- (1+ (cdr active)) (car active))))
5290          ((not (listp (cdr range)))
5291           (setq num (- (cdr active) (- (1+ (cdr range))
5292                                        (car range)))))
5293          (t
5294           (while range
5295             (if (numberp (car range))
5296                 (setq num (1+ num))
5297               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5298             (setq range (cdr range)))
5299           (setq num (- (cdr active) num))))
5300         ;; Update the number of unread articles.
5301         (setcar entry num)
5302         ;; Update the group buffer.
5303         (gnus-group-update-group group t)))))
5304
5305 (defvar gnus-newsgroup-none-id 0)
5306
5307 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5308   (let ((cur nntp-server-buffer)
5309         (dependencies
5310          (or dependencies
5311              (save-excursion (set-buffer gnus-summary-buffer)
5312                              gnus-newsgroup-dependencies)))
5313         headers id end ref
5314         (mail-parse-charset gnus-newsgroup-charset)
5315         (mail-parse-ignored-charsets
5316          (save-excursion (condition-case nil
5317                              (set-buffer gnus-summary-buffer)
5318                            (error))
5319                          gnus-newsgroup-ignored-charsets)))
5320     (save-excursion
5321       (set-buffer nntp-server-buffer)
5322       ;; Translate all TAB characters into SPACE characters.
5323       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5324       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5325       (gnus-run-hooks 'gnus-parse-headers-hook)
5326       (let ((case-fold-search t)
5327             in-reply-to header p lines chars)
5328         (goto-char (point-min))
5329         ;; Search to the beginning of the next header.  Error messages
5330         ;; do not begin with 2 or 3.
5331         (while (re-search-forward "^[23][0-9]+ " nil t)
5332           (setq id nil
5333                 ref nil)
5334           ;; This implementation of this function, with nine
5335           ;; search-forwards instead of the one re-search-forward and
5336           ;; a case (which basically was the old function) is actually
5337           ;; about twice as fast, even though it looks messier.  You
5338           ;; can't have everything, I guess.  Speed and elegance
5339           ;; doesn't always go hand in hand.
5340           (setq
5341            header
5342            (vector
5343             ;; Number.
5344             (prog1
5345                 (read cur)
5346               (end-of-line)
5347               (setq p (point))
5348               (narrow-to-region (point)
5349                                 (or (and (search-forward "\n.\n" nil t)
5350                                          (- (point) 2))
5351                                     (point))))
5352             ;; Subject.
5353             (progn
5354               (goto-char p)
5355               (if (search-forward "\nsubject:" nil t)
5356                   (funcall gnus-decode-encoded-word-function
5357                            (nnheader-header-value))
5358                 "(none)"))
5359             ;; From.
5360             (progn
5361               (goto-char p)
5362               (if (search-forward "\nfrom:" nil t)
5363                   (funcall gnus-decode-encoded-word-function
5364                            (nnheader-header-value))
5365                 "(nobody)"))
5366             ;; Date.
5367             (progn
5368               (goto-char p)
5369               (if (search-forward "\ndate:" nil t)
5370                   (nnheader-header-value) ""))
5371             ;; Message-ID.
5372             (progn
5373               (goto-char p)
5374               (setq id (if (re-search-forward
5375                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5376                            ;; We do it this way to make sure the Message-ID
5377                            ;; is (somewhat) syntactically valid.
5378                            (buffer-substring (match-beginning 1)
5379                                              (match-end 1))
5380                          ;; If there was no message-id, we just fake one
5381                          ;; to make subsequent routines simpler.
5382                          (nnheader-generate-fake-message-id))))
5383             ;; References.
5384             (progn
5385               (goto-char p)
5386               (if (search-forward "\nreferences:" nil t)
5387                   (progn
5388                     (setq end (point))
5389                     (prog1
5390                         (nnheader-header-value)
5391                       (setq ref
5392                             (buffer-substring
5393                              (progn
5394                                (end-of-line)
5395                                (search-backward ">" end t)
5396                                (1+ (point)))
5397                              (progn
5398                                (search-backward "<" end t)
5399                                (point))))))
5400                 ;; Get the references from the in-reply-to header if there
5401                 ;; were no references and the in-reply-to header looks
5402                 ;; promising.
5403                 (if (and (search-forward "\nin-reply-to:" nil t)
5404                          (setq in-reply-to (nnheader-header-value))
5405                          (string-match "<[^>]+>" in-reply-to))
5406                     (let (ref2)
5407                       (setq ref (substring in-reply-to (match-beginning 0)
5408                                            (match-end 0)))
5409                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5410                         (setq ref2 (substring in-reply-to (match-beginning 0)
5411                                               (match-end 0)))
5412                         (when (> (length ref2) (length ref))
5413                           (setq ref ref2)))
5414                       ref)
5415                   (setq ref nil))))
5416             ;; Chars.
5417             (progn
5418               (goto-char p)
5419               (if (search-forward "\nchars: " nil t)
5420                   (if (numberp (setq chars (ignore-errors (read cur))))
5421                       chars -1)
5422                 -1))
5423             ;; Lines.
5424             (progn
5425               (goto-char p)
5426               (if (search-forward "\nlines: " nil t)
5427                   (if (numberp (setq lines (ignore-errors (read cur))))
5428                       lines -1)
5429                 -1))
5430             ;; Xref.
5431             (progn
5432               (goto-char p)
5433               (and (search-forward "\nxref:" nil t)
5434                    (nnheader-header-value)))
5435             ;; Extra.
5436             (when gnus-extra-headers
5437               (let ((extra gnus-extra-headers)
5438                     out)
5439                 (while extra
5440                   (goto-char p)
5441                   (when (search-forward
5442                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5443                     (push (cons (car extra) (nnheader-header-value))
5444                           out))
5445                   (pop extra))
5446                 out))))
5447           (when (equal id ref)
5448             (setq ref nil))
5449
5450           (when gnus-alter-header-function
5451             (funcall gnus-alter-header-function header)
5452             (setq id (mail-header-id header)
5453                   ref (gnus-parent-id (mail-header-references header))))
5454
5455           (when (setq header
5456                       (gnus-dependencies-add-header
5457                        header dependencies force-new))
5458             (push header headers))
5459           (goto-char (point-max))
5460           (widen))
5461         (nreverse headers)))))
5462
5463 ;; Goes through the xover lines and returns a list of vectors
5464 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5465                                                   force-new dependencies
5466                                                   group also-fetch-heads)
5467   "Parse the news overview data in the server buffer.
5468 Return a list of headers that match SEQUENCE (see
5469 `nntp-retrieve-headers')."
5470   ;; Get the Xref when the users reads the articles since most/some
5471   ;; NNTP servers do not include Xrefs when using XOVER.
5472   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5473   (let ((mail-parse-charset gnus-newsgroup-charset)
5474         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5475         (cur nntp-server-buffer)
5476         (dependencies (or dependencies gnus-newsgroup-dependencies))
5477         (allp (cond
5478                ((eq gnus-read-all-available-headers t)
5479                 t)
5480                ((stringp gnus-read-all-available-headers)
5481                 (string-match gnus-read-all-available-headers group))
5482                (t
5483                 nil)))
5484         number headers header)
5485     (save-excursion
5486       (set-buffer nntp-server-buffer)
5487       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5488       ;; Allow the user to mangle the headers before parsing them.
5489       (gnus-run-hooks 'gnus-parse-headers-hook)
5490       (goto-char (point-min))
5491       (gnus-parse-without-error
5492         (while (and (or sequence allp)
5493                     (not (eobp)))
5494           (setq number (read cur))
5495           (when (not allp)
5496             (while (and sequence
5497                         (< (car sequence) number))
5498               (setq sequence (cdr sequence))))
5499           (when (and (or allp
5500                          (and sequence
5501                               (eq number (car sequence))))
5502                      (progn
5503                        (setq sequence (cdr sequence))
5504                        (setq header (inline
5505                                       (gnus-nov-parse-line
5506                                        number dependencies force-new)))))
5507             (push header headers))
5508           (forward-line 1)))
5509       ;; A common bug in inn is that if you have posted an article and
5510       ;; then retrieves the active file, it will answer correctly --
5511       ;; the new article is included.  However, a NOV entry for the
5512       ;; article may not have been generated yet, so this may fail.
5513       ;; We work around this problem by retrieving the last few
5514       ;; headers using HEAD.
5515       (if (or (not also-fetch-heads)
5516               (not sequence))
5517           ;; We (probably) got all the headers.
5518           (nreverse headers)
5519         (let ((gnus-nov-is-evil t))
5520           (nconc
5521            (nreverse headers)
5522            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5523              (gnus-get-newsgroup-headers))))))))
5524
5525 (defun gnus-article-get-xrefs ()
5526   "Fill in the Xref value in `gnus-current-headers', if necessary.
5527 This is meant to be called in `gnus-article-internal-prepare-hook'."
5528   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5529                                  gnus-current-headers)))
5530     (or (not gnus-use-cross-reference)
5531         (not headers)
5532         (and (mail-header-xref headers)
5533              (not (string= (mail-header-xref headers) "")))
5534         (let ((case-fold-search t)
5535               xref)
5536           (save-restriction
5537             (nnheader-narrow-to-headers)
5538             (goto-char (point-min))
5539             (when (or (and (not (eobp))
5540                            (eq (downcase (char-after)) ?x)
5541                            (looking-at "Xref:"))
5542                       (search-forward "\nXref:" nil t))
5543               (goto-char (1+ (match-end 0)))
5544               (setq xref (buffer-substring (point)
5545                                            (progn (end-of-line) (point))))
5546               (mail-header-set-xref headers xref)))))))
5547
5548 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5549   "Find article ID and insert the summary line for that article.
5550 OLD-HEADER can either be a header or a line number to insert
5551 the subject line on."
5552   (let* ((line (and (numberp old-header) old-header))
5553          (old-header (and (vectorp old-header) old-header))
5554          (header (cond ((and old-header use-old-header)
5555                         old-header)
5556                        ((and (numberp id)
5557                              (gnus-number-to-header id))
5558                         (gnus-number-to-header id))
5559                        (t
5560                         (gnus-read-header id))))
5561          (number (and (numberp id) id))
5562          d)
5563     (when header
5564       ;; Rebuild the thread that this article is part of and go to the
5565       ;; article we have fetched.
5566       (when (and (not gnus-show-threads)
5567                  old-header)
5568         (when (and number
5569                    (setq d (gnus-data-find (mail-header-number old-header))))
5570           (goto-char (gnus-data-pos d))
5571           (gnus-data-remove
5572            number
5573            (- (gnus-point-at-bol)
5574               (prog1
5575                   (1+ (gnus-point-at-eol))
5576                 (gnus-delete-line))))))
5577       (when old-header
5578         (mail-header-set-number header (mail-header-number old-header)))
5579       (setq gnus-newsgroup-sparse
5580             (delq (setq number (mail-header-number header))
5581                   gnus-newsgroup-sparse))
5582       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5583       (push number gnus-newsgroup-limit)
5584       (gnus-rebuild-thread (mail-header-id header) line)
5585       (gnus-summary-goto-subject number nil t))
5586     (when (and (numberp number)
5587                (> number 0))
5588       ;; We have to update the boundaries even if we can't fetch the
5589       ;; article if ID is a number -- so that the next `P' or `N'
5590       ;; command will fetch the previous (or next) article even
5591       ;; if the one we tried to fetch this time has been canceled.
5592       (when (> number gnus-newsgroup-end)
5593         (setq gnus-newsgroup-end number))
5594       (when (< number gnus-newsgroup-begin)
5595         (setq gnus-newsgroup-begin number))
5596       (setq gnus-newsgroup-unselected
5597             (delq number gnus-newsgroup-unselected)))
5598     ;; Report back a success?
5599     (and header (mail-header-number header))))
5600
5601 ;;; Process/prefix in the summary buffer
5602
5603 (defun gnus-summary-work-articles (n)
5604   "Return a list of articles to be worked upon.
5605 The prefix argument, the list of process marked articles, and the
5606 current article will be taken into consideration."
5607   (save-excursion
5608     (set-buffer gnus-summary-buffer)
5609     (cond
5610      (n
5611       ;; A numerical prefix has been given.
5612       (setq n (prefix-numeric-value n))
5613       (let ((backward (< n 0))
5614             (n (abs (prefix-numeric-value n)))
5615             articles article)
5616         (save-excursion
5617           (while
5618               (and (> n 0)
5619                    (push (setq article (gnus-summary-article-number))
5620                          articles)
5621                    (if backward
5622                        (gnus-summary-find-prev nil article)
5623                      (gnus-summary-find-next nil article)))
5624             (decf n)))
5625         (nreverse articles)))
5626      ((and (gnus-region-active-p) (mark))
5627       (message "region active")
5628       ;; Work on the region between point and mark.
5629       (let ((max (max (point) (mark)))
5630             articles article)
5631         (save-excursion
5632           (goto-char (min (min (point) (mark))))
5633           (while
5634               (and
5635                (push (setq article (gnus-summary-article-number)) articles)
5636                (gnus-summary-find-next nil article)
5637                (< (point) max)))
5638           (nreverse articles))))
5639      (gnus-newsgroup-processable
5640       ;; There are process-marked articles present.
5641       ;; Save current state.
5642       (gnus-summary-save-process-mark)
5643       ;; Return the list.
5644       (reverse gnus-newsgroup-processable))
5645      (t
5646       ;; Just return the current article.
5647       (list (gnus-summary-article-number))))))
5648
5649 (defmacro gnus-summary-iterate (arg &rest forms)
5650   "Iterate over the process/prefixed articles and do FORMS.
5651 ARG is the interactive prefix given to the command.  FORMS will be
5652 executed with point over the summary line of the articles."
5653   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5654     `(let ((,articles (gnus-summary-work-articles ,arg)))
5655        (while ,articles
5656          (gnus-summary-goto-subject (car ,articles))
5657          ,@forms
5658          (pop ,articles)))))
5659
5660 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5661 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5662
5663 (defun gnus-summary-save-process-mark ()
5664   "Push the current set of process marked articles on the stack."
5665   (interactive)
5666   (push (copy-sequence gnus-newsgroup-processable)
5667         gnus-newsgroup-process-stack))
5668
5669 (defun gnus-summary-kill-process-mark ()
5670   "Push the current set of process marked articles on the stack and unmark."
5671   (interactive)
5672   (gnus-summary-save-process-mark)
5673   (gnus-summary-unmark-all-processable))
5674
5675 (defun gnus-summary-yank-process-mark ()
5676   "Pop the last process mark state off the stack and restore it."
5677   (interactive)
5678   (unless gnus-newsgroup-process-stack
5679     (error "Empty mark stack"))
5680   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5681
5682 (defun gnus-summary-process-mark-set (set)
5683   "Make SET into the current process marked articles."
5684   (gnus-summary-unmark-all-processable)
5685   (while set
5686     (gnus-summary-set-process-mark (pop set))))
5687
5688 ;;; Searching and stuff
5689
5690 (defun gnus-summary-search-group (&optional backward use-level)
5691   "Search for next unread newsgroup.
5692 If optional argument BACKWARD is non-nil, search backward instead."
5693   (save-excursion
5694     (set-buffer gnus-group-buffer)
5695     (when (gnus-group-search-forward
5696            backward nil (if use-level (gnus-group-group-level) nil))
5697       (gnus-group-group-name))))
5698
5699 (defun gnus-summary-best-group (&optional exclude-group)
5700   "Find the name of the best unread group.
5701 If EXCLUDE-GROUP, do not go to this group."
5702   (save-excursion
5703     (set-buffer gnus-group-buffer)
5704     (save-excursion
5705       (gnus-group-best-unread-group exclude-group))))
5706
5707 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5708   (if backward (gnus-summary-find-prev)
5709     (let* ((dummy (gnus-summary-article-intangible-p))
5710            (article (or article (gnus-summary-article-number)))
5711            (arts (gnus-data-find-list article))
5712            result)
5713       (when (and (not dummy)
5714                  (or (not gnus-summary-check-current)
5715                      (not unread)
5716                      (not (gnus-data-unread-p (car arts)))))
5717         (setq arts (cdr arts)))
5718       (when (setq result
5719                   (if unread
5720                       (progn
5721                         (while arts
5722                           (when (or (and undownloaded
5723                                          (eq gnus-undownloaded-mark
5724                                              (gnus-data-mark (car arts))))
5725                                     (gnus-data-unread-p (car arts)))
5726                             (setq result (car arts)
5727                                   arts nil))
5728                           (setq arts (cdr arts)))
5729                         result)
5730                     (car arts)))
5731         (goto-char (gnus-data-pos result))
5732         (gnus-data-number result)))))
5733
5734 (defun gnus-summary-find-prev (&optional unread article)
5735   (let* ((eobp (eobp))
5736          (article (or article (gnus-summary-article-number)))
5737          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5738          result)
5739     (when (and (not eobp)
5740                (or (not gnus-summary-check-current)
5741                    (not unread)
5742                    (not (gnus-data-unread-p (car arts)))))
5743       (setq arts (cdr arts)))
5744     (when (setq result
5745                 (if unread
5746                     (progn
5747                       (while arts
5748                         (when (gnus-data-unread-p (car arts))
5749                           (setq result (car arts)
5750                                 arts nil))
5751                         (setq arts (cdr arts)))
5752                       result)
5753                   (car arts)))
5754       (goto-char (gnus-data-pos result))
5755       (gnus-data-number result))))
5756
5757 (defun gnus-summary-find-subject (subject &optional unread backward article)
5758   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5759          (article (or article (gnus-summary-article-number)))
5760          (articles (gnus-data-list backward))
5761          (arts (gnus-data-find-list article articles))
5762          result)
5763     (when (or (not gnus-summary-check-current)
5764               (not unread)
5765               (not (gnus-data-unread-p (car arts))))
5766       (setq arts (cdr arts)))
5767     (while arts
5768       (and (or (not unread)
5769                (gnus-data-unread-p (car arts)))
5770            (vectorp (gnus-data-header (car arts)))
5771            (gnus-subject-equal
5772             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5773            (setq result (car arts)
5774                  arts nil))
5775       (setq arts (cdr arts)))
5776     (and result
5777          (goto-char (gnus-data-pos result))
5778          (gnus-data-number result))))
5779
5780 (defun gnus-summary-search-forward (&optional unread subject backward)
5781   "Search forward for an article.
5782 If UNREAD, look for unread articles.  If SUBJECT, look for
5783 articles with that subject.  If BACKWARD, search backward instead."
5784   (cond (subject (gnus-summary-find-subject subject unread backward))
5785         (backward (gnus-summary-find-prev unread))
5786         (t (gnus-summary-find-next unread))))
5787
5788 (defun gnus-recenter (&optional n)
5789   "Center point in window and redisplay frame.
5790 Also do horizontal recentering."
5791   (interactive "P")
5792   (when (and gnus-auto-center-summary
5793              (not (eq gnus-auto-center-summary 'vertical)))
5794     (gnus-horizontal-recenter))
5795   (recenter n))
5796
5797 (defun gnus-summary-recenter ()
5798   "Center point in the summary window.
5799 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5800 displayed, no centering will be performed."
5801   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5802 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5803   (interactive)
5804   (let* ((top (cond ((< (window-height) 4) 0)
5805                     ((< (window-height) 7) 1)
5806                     (t (if (numberp gnus-auto-center-summary)
5807                            gnus-auto-center-summary
5808                          2))))
5809          (height (1- (window-height)))
5810          (bottom (save-excursion (goto-char (point-max))
5811                                  (forward-line (- height))
5812                                  (point)))
5813          (window (get-buffer-window (current-buffer))))
5814     ;; The user has to want it.
5815     (when gnus-auto-center-summary
5816       (when (get-buffer-window gnus-article-buffer)
5817         ;; Only do recentering when the article buffer is displayed,
5818       ;; Set the window start to either `bottom', which is the biggest
5819         ;; possible valid number, or the second line from the top,
5820         ;; whichever is the least.
5821         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5822           (if (> bottom top-pos)
5823               ;; Keep the second line from the top visible
5824               (set-window-start window top-pos t)
5825             ;; Try to keep the bottom line visible; if it's partially
5826             ;; obscured, either scroll one more line to make it fully
5827             ;; visible, or revert to using TOP-POS.
5828             (save-excursion
5829               (goto-char (point-max))
5830               (forward-line -1)
5831               (let ((last-line-start (point)))
5832                 (goto-char bottom)
5833                 (set-window-start window (point) t)
5834                 (when (not (pos-visible-in-window-p last-line-start window))
5835                   (forward-line 1)
5836                   (set-window-start window (min (point) top-pos) t)))))))
5837       ;; Do horizontal recentering while we're at it.
5838       (when (and (get-buffer-window (current-buffer) t)
5839                  (not (eq gnus-auto-center-summary 'vertical)))
5840         (let ((selected (selected-window)))
5841           (select-window (get-buffer-window (current-buffer) t))
5842           (gnus-summary-position-point)
5843           (gnus-horizontal-recenter)
5844           (select-window selected))))))
5845
5846 (defun gnus-summary-jump-to-group (newsgroup)
5847   "Move point to NEWSGROUP in group mode buffer."
5848   ;; Keep update point of group mode buffer if visible.
5849   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5850       (save-window-excursion
5851         ;; Take care of tree window mode.
5852         (when (get-buffer-window gnus-group-buffer)
5853           (pop-to-buffer gnus-group-buffer))
5854         (gnus-group-jump-to-group newsgroup))
5855     (save-excursion
5856       ;; Take care of tree window mode.
5857       (if (get-buffer-window gnus-group-buffer)
5858           (pop-to-buffer gnus-group-buffer)
5859         (set-buffer gnus-group-buffer))
5860       (gnus-group-jump-to-group newsgroup))))
5861
5862 ;; This function returns a list of article numbers based on the
5863 ;; difference between the ranges of read articles in this group and
5864 ;; the range of active articles.
5865 (defun gnus-list-of-unread-articles (group)
5866   (let* ((read (gnus-info-read (gnus-get-info group)))
5867          (active (or (gnus-active group) (gnus-activate-group group)))
5868          (last (cdr active))
5869          first nlast unread)
5870     ;; If none are read, then all are unread.
5871     (if (not read)
5872         (setq first (car active))
5873       ;; If the range of read articles is a single range, then the
5874       ;; first unread article is the article after the last read
5875       ;; article.  Sounds logical, doesn't it?
5876       (if (and (not (listp (cdr read)))
5877                (or (< (car read) (car active))
5878                    (progn (setq read (list read))
5879                           nil)))
5880           (setq first (max (car active) (1+ (cdr read))))
5881         ;; `read' is a list of ranges.
5882         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5883                                   (caar read)))
5884                   1)
5885           (setq first (car active)))
5886         (while read
5887           (when first
5888             (while (< first nlast)
5889               (push first unread)
5890               (setq first (1+ first))))
5891           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5892           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5893           (setq read (cdr read)))))
5894     ;; And add the last unread articles.
5895     (while (<= first last)
5896       (push first unread)
5897       (setq first (1+ first)))
5898     ;; Return the list of unread articles.
5899     (delq 0 (nreverse unread))))
5900
5901 (defun gnus-list-of-read-articles (group)
5902   "Return a list of unread, unticked and non-dormant articles."
5903   (let* ((info (gnus-get-info group))
5904          (marked (gnus-info-marks info))
5905          (active (gnus-active group)))
5906     (and info active
5907          (gnus-set-difference
5908           (gnus-sorted-complement
5909            (gnus-uncompress-range active)
5910            (gnus-list-of-unread-articles group))
5911           (append
5912            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5913            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5914
5915 ;; Various summary commands
5916
5917 (defun gnus-summary-select-article-buffer ()
5918   "Reconfigure windows to show article buffer."
5919   (interactive)
5920   (if (not (gnus-buffer-live-p gnus-article-buffer))
5921       (error "There is no article buffer for this summary buffer")
5922     (gnus-configure-windows 'article)
5923     (select-window (get-buffer-window gnus-article-buffer))))
5924
5925 (defun gnus-summary-universal-argument (arg)
5926   "Perform any operation on all articles that are process/prefixed."
5927   (interactive "P")
5928   (let ((articles (gnus-summary-work-articles arg))
5929         func article)
5930     (if (eq
5931          (setq
5932           func
5933           (key-binding
5934            (read-key-sequence
5935             (substitute-command-keys
5936              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5937          'undefined)
5938         (gnus-error 1 "Undefined key")
5939       (save-excursion
5940         (while articles
5941           (gnus-summary-goto-subject (setq article (pop articles)))
5942           (let (gnus-newsgroup-processable)
5943             (command-execute func))
5944           (gnus-summary-remove-process-mark article)))))
5945   (gnus-summary-position-point))
5946
5947 (defun gnus-summary-toggle-truncation (&optional arg)
5948   "Toggle truncation of summary lines.
5949 With arg, turn line truncation on iff arg is positive."
5950   (interactive "P")
5951   (setq truncate-lines
5952         (if (null arg) (not truncate-lines)
5953           (> (prefix-numeric-value arg) 0)))
5954   (redraw-display))
5955
5956 (defun gnus-summary-reselect-current-group (&optional all rescan)
5957   "Exit and then reselect the current newsgroup.
5958 The prefix argument ALL means to select all articles."
5959   (interactive "P")
5960   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5961     (error "Ephemeral groups can't be reselected"))
5962   (let ((current-subject (gnus-summary-article-number))
5963         (group gnus-newsgroup-name))
5964     (setq gnus-newsgroup-begin nil)
5965     (gnus-summary-exit)
5966     ;; We have to adjust the point of group mode buffer because
5967     ;; point was moved to the next unread newsgroup by exiting.
5968     (gnus-summary-jump-to-group group)
5969     (when rescan
5970       (save-excursion
5971         (gnus-group-get-new-news-this-group 1)))
5972     (gnus-group-read-group all t)
5973     (gnus-summary-goto-subject current-subject nil t)))
5974
5975 (defun gnus-summary-rescan-group (&optional all)
5976   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5977   (interactive "P")
5978   (gnus-summary-reselect-current-group all t))
5979
5980 (defun gnus-summary-update-info (&optional non-destructive)
5981   (save-excursion
5982     (let ((group gnus-newsgroup-name))
5983       (when group
5984         (when gnus-newsgroup-kill-headers
5985           (setq gnus-newsgroup-killed
5986                 (gnus-compress-sequence
5987                  (gnus-sorted-union
5988                   (gnus-list-range-intersection
5989                    (setq gnus-newsgroup-unselected
5990                          (sort gnus-newsgroup-unselected '<))
5991                    gnus-newsgroup-killed)
5992                   (setq gnus-newsgroup-unreads
5993                         (sort gnus-newsgroup-unreads '<)))
5994                  t)))
5995         (unless (listp (cdr gnus-newsgroup-killed))
5996           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5997         (let ((headers gnus-newsgroup-headers))
5998           ;; Set the new ranges of read articles.
5999           (save-excursion
6000             (set-buffer gnus-group-buffer)
6001             (gnus-undo-force-boundary))
6002           (gnus-update-read-articles
6003            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
6004           ;; Set the current article marks.
6005           (let ((gnus-newsgroup-scored
6006                  (if (and (not gnus-save-score)
6007                           (not non-destructive))
6008                      nil
6009                    gnus-newsgroup-scored)))
6010             (save-excursion
6011               (gnus-update-marks)))
6012           ;; Do the cross-ref thing.
6013           (when gnus-use-cross-reference
6014             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6015           ;; Do not switch windows but change the buffer to work.
6016           (set-buffer gnus-group-buffer)
6017           (unless (gnus-ephemeral-group-p group)
6018             (gnus-group-update-group group)))))))
6019
6020 (defun gnus-summary-save-newsrc (&optional force)
6021   "Save the current number of read/marked articles in the dribble buffer.
6022 The dribble buffer will then be saved.
6023 If FORCE (the prefix), also save the .newsrc file(s)."
6024   (interactive "P")
6025   (gnus-summary-update-info t)
6026   (if force
6027       (gnus-save-newsrc-file)
6028     (gnus-dribble-save)))
6029
6030 (defun gnus-summary-exit (&optional temporary)
6031   "Exit reading current newsgroup, and then return to group selection mode.
6032 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6033   (interactive)
6034   (gnus-set-global-variables)
6035   (when (gnus-buffer-live-p gnus-article-buffer)
6036     (save-excursion
6037       (set-buffer gnus-article-buffer)
6038       (mm-destroy-parts gnus-article-mime-handles)
6039       ;; Set it to nil for safety reason.
6040       (setq gnus-article-mime-handle-alist nil)
6041       (setq gnus-article-mime-handles nil)))
6042   (gnus-kill-save-kill-buffer)
6043   (gnus-async-halt-prefetch)
6044   (let* ((group gnus-newsgroup-name)
6045          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6046          (mode major-mode)
6047          (group-point nil)
6048          (buf (current-buffer)))
6049     (unless quit-config
6050       ;; Do adaptive scoring, and possibly save score files.
6051       (when gnus-newsgroup-adaptive
6052         (gnus-score-adaptive))
6053       (when gnus-use-scoring
6054         (gnus-score-save)))
6055     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6056     ;; If we have several article buffers, we kill them at exit.
6057     (unless gnus-single-article-buffer
6058       (gnus-kill-buffer gnus-original-article-buffer)
6059       (setq gnus-article-current nil))
6060     (when gnus-use-cache
6061       (gnus-cache-possibly-remove-articles)
6062       (gnus-cache-save-buffers))
6063     (gnus-async-prefetch-remove-group group)
6064     (when gnus-suppress-duplicates
6065       (gnus-dup-enter-articles))
6066     (when gnus-use-trees
6067       (gnus-tree-close group))
6068     (when gnus-use-cache
6069       (gnus-cache-write-active))
6070     ;; Remove entries for this group.
6071     (nnmail-purge-split-history (gnus-group-real-name group))
6072     ;; Make all changes in this group permanent.
6073     (unless quit-config
6074       (gnus-run-hooks 'gnus-exit-group-hook)
6075       (gnus-summary-update-info))
6076     (gnus-close-group group)
6077     ;; Make sure where we were, and go to next newsgroup.
6078     (set-buffer gnus-group-buffer)
6079     (unless quit-config
6080       (gnus-group-jump-to-group group))
6081     (gnus-run-hooks 'gnus-summary-exit-hook)
6082     (unless (or quit-config
6083                 ;; If this group has disappeared from the summary
6084                 ;; buffer, don't skip forwards.
6085                 (not (string= group (gnus-group-group-name))))
6086       (gnus-group-next-unread-group 1))
6087     (setq group-point (point))
6088     (if temporary
6089         nil                             ;Nothing to do.
6090       ;; If we have several article buffers, we kill them at exit.
6091       (unless gnus-single-article-buffer
6092         (gnus-kill-buffer gnus-article-buffer)
6093         (gnus-kill-buffer gnus-original-article-buffer)
6094         (setq gnus-article-current nil))
6095       (set-buffer buf)
6096       (if (not gnus-kill-summary-on-exit)
6097           (progn
6098             (gnus-deaden-summary)
6099             (setq mode nil))
6100        ;; We set all buffer-local variables to nil.  It is unclear why
6101         ;; this is needed, but if we don't, buffer-local variables are
6102         ;; not garbage-collected, it seems.  This would the lead to en
6103         ;; ever-growing Emacs.
6104         (gnus-summary-clear-local-variables)
6105         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6106           (gnus-summary-clear-local-variables))
6107         (when (get-buffer gnus-article-buffer)
6108           (bury-buffer gnus-article-buffer))
6109         ;; We clear the global counterparts of the buffer-local
6110         ;; variables as well, just to be on the safe side.
6111         (set-buffer gnus-group-buffer)
6112         (gnus-summary-clear-local-variables)
6113         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6114           (gnus-summary-clear-local-variables)))
6115       (setq gnus-current-select-method gnus-select-method)
6116       (pop-to-buffer gnus-group-buffer)
6117       (if (not quit-config)
6118           (progn
6119             (goto-char group-point)
6120             (gnus-configure-windows 'group 'force))
6121         (gnus-handle-ephemeral-exit quit-config))
6122       ;; Return to group mode buffer.
6123       (when (eq mode 'gnus-summary-mode)
6124         (gnus-kill-buffer buf))
6125       ;; Clear the current group name.
6126       (unless quit-config
6127         (setq gnus-newsgroup-name nil)))))
6128
6129 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6130 (defun gnus-summary-exit-no-update (&optional no-questions)
6131   "Quit reading current newsgroup without updating read article info."
6132   (interactive)
6133   (let* ((group gnus-newsgroup-name)
6134          (quit-config (gnus-group-quit-config group)))
6135     (when (or no-questions
6136               gnus-expert-user
6137               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6138       (gnus-async-halt-prefetch)
6139       (mapcar 'funcall
6140               (delq 'gnus-summary-expire-articles
6141                     (copy-sequence gnus-summary-prepare-exit-hook)))
6142       (when (gnus-buffer-live-p gnus-article-buffer)
6143         (save-excursion
6144           (set-buffer gnus-article-buffer)
6145           (mm-destroy-parts gnus-article-mime-handles)
6146           ;; Set it to nil for safety reason.
6147           (setq gnus-article-mime-handle-alist nil)
6148           (setq gnus-article-mime-handles nil)))
6149       ;; If we have several article buffers, we kill them at exit.
6150       (unless gnus-single-article-buffer
6151         (gnus-kill-buffer gnus-article-buffer)
6152         (gnus-kill-buffer gnus-original-article-buffer)
6153         (setq gnus-article-current nil))
6154       (if (not gnus-kill-summary-on-exit)
6155           (gnus-deaden-summary)
6156         (gnus-close-group group)
6157         (gnus-summary-clear-local-variables)
6158         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6159           (gnus-summary-clear-local-variables))
6160         (set-buffer gnus-group-buffer)
6161         (gnus-summary-clear-local-variables)
6162         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6163           (gnus-summary-clear-local-variables))
6164         (when (get-buffer gnus-summary-buffer)
6165           (kill-buffer gnus-summary-buffer)))
6166       (unless gnus-single-article-buffer
6167         (setq gnus-article-current nil))
6168       (when gnus-use-trees
6169         (gnus-tree-close group))
6170       (gnus-async-prefetch-remove-group group)
6171       (when (get-buffer gnus-article-buffer)
6172         (bury-buffer gnus-article-buffer))
6173       ;; Return to the group buffer.
6174       (gnus-configure-windows 'group 'force)
6175       ;; Clear the current group name.
6176       (setq gnus-newsgroup-name nil)
6177       (when (equal (gnus-group-group-name) group)
6178         (gnus-group-next-unread-group 1))
6179       (when quit-config
6180         (gnus-handle-ephemeral-exit quit-config)))))
6181
6182 (defun gnus-handle-ephemeral-exit (quit-config)
6183   "Handle movement when leaving an ephemeral group.
6184 The state which existed when entering the ephemeral is reset."
6185   (if (not (buffer-name (car quit-config)))
6186       (gnus-configure-windows 'group 'force)
6187     (set-buffer (car quit-config))
6188     (cond ((eq major-mode 'gnus-summary-mode)
6189            (gnus-set-global-variables))
6190           ((eq major-mode 'gnus-article-mode)
6191            (save-excursion
6192              ;; The `gnus-summary-buffer' variable may point
6193              ;; to the old summary buffer when using a single
6194              ;; article buffer.
6195              (unless (gnus-buffer-live-p gnus-summary-buffer)
6196                (set-buffer gnus-group-buffer))
6197              (set-buffer gnus-summary-buffer)
6198              (gnus-set-global-variables))))
6199     (if (or (eq (cdr quit-config) 'article)
6200             (eq (cdr quit-config) 'pick))
6201         (progn
6202           ;; The current article may be from the ephemeral group
6203           ;; thus it is best that we reload this article
6204           (gnus-summary-show-article)
6205           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6206               (gnus-configure-windows 'pick 'force)
6207             (gnus-configure-windows (cdr quit-config) 'force)))
6208       (gnus-configure-windows (cdr quit-config) 'force))
6209     (when (eq major-mode 'gnus-summary-mode)
6210       (gnus-summary-next-subject 1 nil t)
6211       (gnus-summary-recenter)
6212       (gnus-summary-position-point))))
6213
6214 ;;; Dead summaries.
6215
6216 (defvar gnus-dead-summary-mode-map nil)
6217
6218 (unless gnus-dead-summary-mode-map
6219   (setq gnus-dead-summary-mode-map (make-keymap))
6220   (suppress-keymap gnus-dead-summary-mode-map)
6221   (substitute-key-definition
6222    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6223   (dolist (key '("\C-d" "\r" "\177" [delete]))
6224     (define-key gnus-dead-summary-mode-map
6225       key 'gnus-summary-wake-up-the-dead))
6226   (dolist (key '("q" "Q"))
6227     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6228
6229 (defvar gnus-dead-summary-mode nil
6230   "Minor mode for Gnus summary buffers.")
6231
6232 (defun gnus-dead-summary-mode (&optional arg)
6233   "Minor mode for Gnus summary buffers."
6234   (interactive "P")
6235   (when (eq major-mode 'gnus-summary-mode)
6236     (make-local-variable 'gnus-dead-summary-mode)
6237     (setq gnus-dead-summary-mode
6238           (if (null arg) (not gnus-dead-summary-mode)
6239             (> (prefix-numeric-value arg) 0)))
6240     (when gnus-dead-summary-mode
6241       (gnus-add-minor-mode
6242        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6243
6244 (defun gnus-deaden-summary ()
6245   "Make the current summary buffer into a dead summary buffer."
6246   ;; Kill any previous dead summary buffer.
6247   (when (and gnus-dead-summary
6248              (buffer-name gnus-dead-summary))
6249     (save-excursion
6250       (set-buffer gnus-dead-summary)
6251       (when gnus-dead-summary-mode
6252         (kill-buffer (current-buffer)))))
6253   ;; Make this the current dead summary.
6254   (setq gnus-dead-summary (current-buffer))
6255   (gnus-dead-summary-mode 1)
6256   (let ((name (buffer-name)))
6257     (when (string-match "Summary" name)
6258       (rename-buffer
6259        (concat (substring name 0 (match-beginning 0)) "Dead "
6260                (substring name (match-beginning 0)))
6261        t)
6262       (bury-buffer))))
6263
6264 (defun gnus-kill-or-deaden-summary (buffer)
6265   "Kill or deaden the summary BUFFER."
6266   (save-excursion
6267     (when (and (buffer-name buffer)
6268                (not gnus-single-article-buffer))
6269       (save-excursion
6270         (set-buffer buffer)
6271         (gnus-kill-buffer gnus-article-buffer)
6272         (gnus-kill-buffer gnus-original-article-buffer)))
6273     (cond
6274      ;; Kill the buffer.
6275      (gnus-kill-summary-on-exit
6276       (when (and gnus-use-trees
6277                  (gnus-buffer-exists-p buffer))
6278         (save-excursion
6279           (set-buffer buffer)
6280           (gnus-tree-close gnus-newsgroup-name)))
6281       (gnus-kill-buffer buffer))
6282      ;; Deaden the buffer.
6283      ((gnus-buffer-exists-p buffer)
6284       (save-excursion
6285         (set-buffer buffer)
6286         (gnus-deaden-summary))))))
6287
6288 (defun gnus-summary-wake-up-the-dead (&rest args)
6289   "Wake up the dead summary buffer."
6290   (interactive)
6291   (gnus-dead-summary-mode -1)
6292   (let ((name (buffer-name)))
6293     (when (string-match "Dead " name)
6294       (rename-buffer
6295        (concat (substring name 0 (match-beginning 0))
6296                (substring name (match-end 0)))
6297        t)))
6298   (gnus-message 3 "This dead summary is now alive again"))
6299
6300 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6301 (defun gnus-summary-fetch-faq (&optional faq-dir)
6302   "Fetch the FAQ for the current group.
6303 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6304 in."
6305   (interactive
6306    (list
6307     (when current-prefix-arg
6308       (completing-read
6309        "Faq dir: " (and (listp gnus-group-faq-directory)
6310                         (mapcar (lambda (file) (list file))
6311                                 gnus-group-faq-directory))))))
6312   (let (gnus-faq-buffer)
6313     (when (setq gnus-faq-buffer
6314                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6315       (gnus-configure-windows 'summary-faq))))
6316
6317 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6318 (defun gnus-summary-describe-group (&optional force)
6319   "Describe the current newsgroup."
6320   (interactive "P")
6321   (gnus-group-describe-group force gnus-newsgroup-name))
6322
6323 (defun gnus-summary-describe-briefly ()
6324   "Describe summary mode commands briefly."
6325   (interactive)
6326   (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")))
6327
6328 ;; Walking around group mode buffer from summary mode.
6329
6330 (defun gnus-summary-next-group (&optional no-article target-group backward)
6331   "Exit current newsgroup and then select next unread newsgroup.
6332 If prefix argument NO-ARTICLE is non-nil, no article is selected
6333 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6334 previous group instead."
6335   (interactive "P")
6336   ;; Stop pre-fetching.
6337   (gnus-async-halt-prefetch)
6338   (let ((current-group gnus-newsgroup-name)
6339         (current-buffer (current-buffer))
6340         entered)
6341    ;; First we semi-exit this group to update Xrefs and all variables.
6342     ;; We can't do a real exit, because the window conf must remain
6343     ;; the same in case the user is prompted for info, and we don't
6344     ;; want the window conf to change before that...
6345     (gnus-summary-exit t)
6346     (while (not entered)
6347       ;; Then we find what group we are supposed to enter.
6348       (set-buffer gnus-group-buffer)
6349       (gnus-group-jump-to-group current-group)
6350       (setq target-group
6351             (or target-group
6352                 (if (eq gnus-keep-same-level 'best)
6353                     (gnus-summary-best-group gnus-newsgroup-name)
6354                   (gnus-summary-search-group backward gnus-keep-same-level))))
6355       (if (not target-group)
6356           ;; There are no further groups, so we return to the group
6357           ;; buffer.
6358           (progn
6359             (gnus-message 5 "Returning to the group buffer")
6360             (setq entered t)
6361             (when (gnus-buffer-live-p current-buffer)
6362               (set-buffer current-buffer)
6363               (gnus-summary-exit))
6364             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6365         ;; We try to enter the target group.
6366         (gnus-group-jump-to-group target-group)
6367         (let ((unreads (gnus-group-group-unread)))
6368           (if (and (or (eq t unreads)
6369                        (and unreads (not (zerop unreads))))
6370                    (gnus-summary-read-group
6371                     target-group nil no-article
6372                     (and (buffer-name current-buffer) current-buffer)
6373                     nil backward))
6374               (setq entered t)
6375             (setq current-group target-group
6376                   target-group nil)))))))
6377
6378 (defun gnus-summary-prev-group (&optional no-article)
6379   "Exit current newsgroup and then select previous unread newsgroup.
6380 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6381   (interactive "P")
6382   (gnus-summary-next-group no-article nil t))
6383
6384 ;; Walking around summary lines.
6385
6386 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6387   "Go to the first unread subject.
6388 If UNREAD is non-nil, go to the first unread article.
6389 Returns the article selected or nil if there are no unread articles."
6390   (interactive "P")
6391   (prog1
6392       (cond
6393        ;; Empty summary.
6394        ((null gnus-newsgroup-data)
6395         (gnus-message 3 "No articles in the group")
6396         nil)
6397        ;; Pick the first article.
6398        ((not unread)
6399         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6400         (gnus-data-number (car gnus-newsgroup-data)))
6401        ;; No unread articles.
6402        ((null gnus-newsgroup-unreads)
6403         (gnus-message 3 "No more unread articles")
6404         nil)
6405        ;; Find the first unread article.
6406        (t
6407         (let ((data gnus-newsgroup-data))
6408           (while (and data
6409                       (and (not (and undownloaded
6410                                      (eq gnus-undownloaded-mark
6411                                          (gnus-data-mark (car data)))))
6412                            (if unseen
6413                                (or (not (memq
6414                                          (gnus-data-number (car data))
6415                                          gnus-newsgroup-unseen))
6416                                    (not (gnus-data-unread-p (car data))))
6417                              (not (gnus-data-unread-p (car data))))))
6418             (setq data (cdr data)))
6419           (when data
6420             (goto-char (gnus-data-pos (car data)))
6421             (gnus-data-number (car data))))))
6422     (gnus-summary-position-point)))
6423
6424 (defun gnus-summary-next-subject (n &optional unread dont-display)
6425   "Go to next N'th summary line.
6426 If N is negative, go to the previous N'th subject line.
6427 If UNREAD is non-nil, only unread articles are selected.
6428 The difference between N and the actual number of steps taken is
6429 returned."
6430   (interactive "p")
6431   (let ((backward (< n 0))
6432         (n (abs n)))
6433     (while (and (> n 0)
6434                 (if backward
6435                     (gnus-summary-find-prev unread)
6436                   (gnus-summary-find-next unread)))
6437       (unless (zerop (setq n (1- n)))
6438         (gnus-summary-show-thread)))
6439     (when (/= 0 n)
6440       (gnus-message 7 "No more%s articles"
6441                     (if unread " unread" "")))
6442     (unless dont-display
6443       (gnus-summary-recenter)
6444       (gnus-summary-position-point))
6445     n))
6446
6447 (defun gnus-summary-next-unread-subject (n)
6448   "Go to next N'th unread summary line."
6449   (interactive "p")
6450   (gnus-summary-next-subject n t))
6451
6452 (defun gnus-summary-prev-subject (n &optional unread)
6453   "Go to previous N'th summary line.
6454 If optional argument UNREAD is non-nil, only unread article is selected."
6455   (interactive "p")
6456   (gnus-summary-next-subject (- n) unread))
6457
6458 (defun gnus-summary-prev-unread-subject (n)
6459   "Go to previous N'th unread summary line."
6460   (interactive "p")
6461   (gnus-summary-next-subject (- n) t))
6462
6463 (defun gnus-summary-goto-subject (article &optional force silent)
6464   "Go the subject line of ARTICLE.
6465 If FORCE, also allow jumping to articles not currently shown."
6466   (interactive "nArticle number: ")
6467   (unless (numberp article)
6468     (error "Article %s is not a number" article))
6469   (let ((b (point))
6470         (data (gnus-data-find article)))
6471     ;; We read in the article if we have to.
6472     (and (not data)
6473          force
6474          (gnus-summary-insert-subject
6475           article
6476           (if (or (numberp force) (vectorp force)) force)
6477           t)
6478          (setq data (gnus-data-find article)))
6479     (goto-char b)
6480     (if (not data)
6481         (progn
6482           (unless silent
6483             (gnus-message 3 "Can't find article %d" article))
6484           nil)
6485       (let ((pt (gnus-data-pos data)))
6486         (goto-char pt)
6487         (gnus-summary-set-article-display-arrow pt))
6488       (gnus-summary-position-point)
6489       article)))
6490
6491 ;; Walking around summary lines with displaying articles.
6492
6493 (defun gnus-summary-expand-window (&optional arg)
6494   "Make the summary buffer take up the entire Emacs frame.
6495 Given a prefix, will force an `article' buffer configuration."
6496   (interactive "P")
6497   (if arg
6498       (gnus-configure-windows 'article 'force)
6499     (gnus-configure-windows 'summary 'force)))
6500
6501 (defun gnus-summary-display-article (article &optional all-header)
6502   "Display ARTICLE in article buffer."
6503   (when (gnus-buffer-live-p gnus-article-buffer)
6504     (with-current-buffer gnus-article-buffer
6505       (mm-enable-multibyte)))
6506   (gnus-set-global-variables)
6507   (when (gnus-buffer-live-p gnus-article-buffer)
6508     (with-current-buffer gnus-article-buffer
6509       (setq gnus-article-charset gnus-newsgroup-charset)
6510       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6511       (mm-enable-multibyte)))
6512   (if (null article)
6513       nil
6514     (prog1
6515         (if gnus-summary-display-article-function
6516             (funcall gnus-summary-display-article-function article all-header)
6517           (gnus-article-prepare article all-header))
6518       (gnus-run-hooks 'gnus-select-article-hook)
6519       (when (and gnus-current-article
6520                  (not (zerop gnus-current-article)))
6521         (gnus-summary-goto-subject gnus-current-article))
6522       (gnus-summary-recenter)
6523       (when (and gnus-use-trees gnus-show-threads)
6524         (gnus-possibly-generate-tree article)
6525         (gnus-highlight-selected-tree article))
6526       ;; Successfully display article.
6527       (gnus-article-set-window-start
6528        (cdr (assq article gnus-newsgroup-bookmarks))))))
6529
6530 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6531   "Select the current article.
6532 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6533 non-nil, the article will be re-fetched even if it already present in
6534 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6535 be displayed."
6536   ;; Make sure we are in the summary buffer to work around bbdb bug.
6537   (unless (eq major-mode 'gnus-summary-mode)
6538     (set-buffer gnus-summary-buffer))
6539   (let ((article (or article (gnus-summary-article-number)))
6540         (all-headers (not (not all-headers))) ;Must be t or nil.
6541         gnus-summary-display-article-function)
6542     (and (not pseudo)
6543          (gnus-summary-article-pseudo-p article)
6544          (error "This is a pseudo-article"))
6545     (save-excursion
6546       (set-buffer gnus-summary-buffer)
6547       (if (or (and gnus-single-article-buffer
6548                    (or (null gnus-current-article)
6549                        (null gnus-article-current)
6550                        (null (get-buffer gnus-article-buffer))
6551                        (not (eq article (cdr gnus-article-current)))
6552                        (not (equal (car gnus-article-current)
6553                                    gnus-newsgroup-name))))
6554               (and (not gnus-single-article-buffer)
6555                    (or (null gnus-current-article)
6556                        (not (eq gnus-current-article article))))
6557               force)
6558           ;; The requested article is different from the current article.
6559           (progn
6560             (gnus-summary-display-article article all-headers)
6561             (when (gnus-buffer-live-p gnus-article-buffer)
6562               (with-current-buffer gnus-article-buffer
6563                 (if (not gnus-article-decoded-p) ;; a local variable
6564                     (mm-disable-multibyte))))
6565 ;;; Hidden headers are not hidden text any more.
6566 ;;          (when (or all-headers gnus-show-all-headers)
6567 ;;            (gnus-article-show-all-headers))
6568             (gnus-article-set-window-start
6569              (cdr (assq article gnus-newsgroup-bookmarks)))
6570             article)
6571 ;;      (when (or all-headers gnus-show-all-headers)
6572 ;;        (gnus-article-show-all-headers))
6573         'old))))
6574
6575 (defun gnus-summary-force-verify-and-decrypt ()
6576   (interactive)
6577   (let ((mm-verify-option 'known)
6578         (mm-decrypt-option 'known))
6579     (gnus-summary-select-article nil 'force)))
6580
6581 (defun gnus-summary-set-current-mark (&optional current-mark)
6582   "Obsolete function."
6583   nil)
6584
6585 (defun gnus-summary-next-article (&optional unread subject backward push)
6586   "Select the next article.
6587 If UNREAD, only unread articles are selected.
6588 If SUBJECT, only articles with SUBJECT are selected.
6589 If BACKWARD, the previous article is selected instead of the next."
6590   (interactive "P")
6591   (cond
6592    ;; Is there such an article?
6593    ((and (gnus-summary-search-forward unread subject backward)
6594          (or (gnus-summary-display-article (gnus-summary-article-number))
6595              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6596     (gnus-summary-position-point))
6597    ;; If not, we try the first unread, if that is wanted.
6598    ((and subject
6599          gnus-auto-select-same
6600          (gnus-summary-first-unread-article))
6601     (gnus-summary-position-point)
6602     (gnus-message 6 "Wrapped"))
6603    ;; Try to get next/previous article not displayed in this group.
6604    ((and gnus-auto-extend-newsgroup
6605          (not unread) (not subject))
6606     (gnus-summary-goto-article
6607      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6608      nil (count-lines (point-min) (point))))
6609    ;; Go to next/previous group.
6610    (t
6611     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6612       (gnus-summary-jump-to-group gnus-newsgroup-name))
6613     (let ((cmd last-command-char)
6614           (point
6615            (save-excursion
6616              (set-buffer gnus-group-buffer)
6617              (point)))
6618           (group
6619            (if (eq gnus-keep-same-level 'best)
6620                (gnus-summary-best-group gnus-newsgroup-name)
6621              (gnus-summary-search-group backward gnus-keep-same-level))))
6622       ;; For some reason, the group window gets selected.  We change
6623       ;; it back.
6624       (select-window (get-buffer-window (current-buffer)))
6625       ;; Select next unread newsgroup automagically.
6626       (cond
6627        ((or (not gnus-auto-select-next)
6628             (not cmd))
6629         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6630        ((or (eq gnus-auto-select-next 'quietly)
6631             (and (eq gnus-auto-select-next 'slightly-quietly)
6632                  push)
6633             (and (eq gnus-auto-select-next 'almost-quietly)
6634                  (gnus-summary-last-article-p)))
6635         ;; Select quietly.
6636         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6637             (gnus-summary-exit)
6638           (gnus-message 7 "No more%s articles (%s)..."
6639                         (if unread " unread" "")
6640                         (if group (concat "selecting " group)
6641                           "exiting"))
6642           (gnus-summary-next-group nil group backward)))
6643        (t
6644         (when (gnus-key-press-event-p last-input-event)
6645           (gnus-summary-walk-group-buffer
6646            gnus-newsgroup-name cmd unread backward point))))))))
6647
6648 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6649   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6650                       (?\C-p (gnus-group-prev-unread-group 1))))
6651         (cursor-in-echo-area t)
6652         keve key group ended)
6653     (save-excursion
6654       (set-buffer gnus-group-buffer)
6655       (goto-char start)
6656       (setq group
6657             (if (eq gnus-keep-same-level 'best)
6658                 (gnus-summary-best-group gnus-newsgroup-name)
6659               (gnus-summary-search-group backward gnus-keep-same-level))))
6660     (while (not ended)
6661       (gnus-message
6662        5 "No more%s articles%s" (if unread " unread" "")
6663        (if (and group
6664                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6665            (format " (Type %s for %s [%s])"
6666                    (single-key-description cmd) group
6667                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6668          (format " (Type %s to exit %s)"
6669                  (single-key-description cmd)
6670                  gnus-newsgroup-name)))
6671       ;; Confirm auto selection.
6672       (setq key (car (setq keve (gnus-read-event-char))))
6673       (setq ended t)
6674       (cond
6675        ((assq key keystrokes)
6676         (let ((obuf (current-buffer)))
6677           (switch-to-buffer gnus-group-buffer)
6678           (when group
6679             (gnus-group-jump-to-group group))
6680           (eval (cadr (assq key keystrokes)))
6681           (setq group (gnus-group-group-name))
6682           (switch-to-buffer obuf))
6683         (setq ended nil))
6684        ((equal key cmd)
6685         (if (or (not group)
6686                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6687             (gnus-summary-exit)
6688           (gnus-summary-next-group nil group backward)))
6689        (t
6690         (push (cdr keve) unread-command-events))))))
6691
6692 (defun gnus-summary-next-unread-article ()
6693   "Select unread article after current one."
6694   (interactive)
6695   (gnus-summary-next-article
6696    (or (not (eq gnus-summary-goto-unread 'never))
6697        (gnus-summary-last-article-p (gnus-summary-article-number)))
6698    (and gnus-auto-select-same
6699         (gnus-summary-article-subject))))
6700
6701 (defun gnus-summary-prev-article (&optional unread subject)
6702   "Select the article after the current one.
6703 If UNREAD is non-nil, only unread articles are selected."
6704   (interactive "P")
6705   (gnus-summary-next-article unread subject t))
6706
6707 (defun gnus-summary-prev-unread-article ()
6708   "Select unread article before current one."
6709   (interactive)
6710   (gnus-summary-prev-article
6711    (or (not (eq gnus-summary-goto-unread 'never))
6712        (gnus-summary-first-article-p (gnus-summary-article-number)))
6713    (and gnus-auto-select-same
6714         (gnus-summary-article-subject))))
6715
6716 (defun gnus-summary-next-page (&optional lines circular)
6717   "Show next page of the selected article.
6718 If at the end of the current article, select the next article.
6719 LINES says how many lines should be scrolled up.
6720
6721 If CIRCULAR is non-nil, go to the start of the article instead of
6722 selecting the next article when reaching the end of the current
6723 article."
6724   (interactive "P")
6725   (setq gnus-summary-buffer (current-buffer))
6726   (gnus-set-global-variables)
6727   (let ((article (gnus-summary-article-number))
6728         (article-window (get-buffer-window gnus-article-buffer t))
6729         endp)
6730     ;; If the buffer is empty, we have no article.
6731     (unless article
6732       (error "No article to select"))
6733     (gnus-configure-windows 'article)
6734     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6735         (if (and (eq gnus-summary-goto-unread 'never)
6736                  (not (gnus-summary-last-article-p article)))
6737             (gnus-summary-next-article)
6738           (gnus-summary-next-unread-article))
6739       (if (or (null gnus-current-article)
6740               (null gnus-article-current)
6741               (/= article (cdr gnus-article-current))
6742               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6743           ;; Selected subject is different from current article's.
6744           (gnus-summary-display-article article)
6745         (when article-window
6746           (gnus-eval-in-buffer-window gnus-article-buffer
6747             (setq endp (gnus-article-next-page lines)))
6748           (when endp
6749             (cond (circular
6750                    (gnus-summary-beginning-of-article))
6751                   (lines
6752                    (gnus-message 3 "End of message"))
6753                   ((null lines)
6754                    (if (and (eq gnus-summary-goto-unread 'never)
6755                             (not (gnus-summary-last-article-p article)))
6756                        (gnus-summary-next-article)
6757                      (gnus-summary-next-unread-article))))))))
6758     (gnus-summary-recenter)
6759     (gnus-summary-position-point)))
6760
6761 (defun gnus-summary-prev-page (&optional lines move)
6762   "Show previous page of selected article.
6763 Argument LINES specifies lines to be scrolled down.
6764 If MOVE, move to the previous unread article if point is at
6765 the beginning of the buffer."
6766   (interactive "P")
6767   (let ((article (gnus-summary-article-number))
6768         (article-window (get-buffer-window gnus-article-buffer t))
6769         endp)
6770     (gnus-configure-windows 'article)
6771     (if (or (null gnus-current-article)
6772             (null gnus-article-current)
6773             (/= article (cdr gnus-article-current))
6774             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6775         ;; Selected subject is different from current article's.
6776         (gnus-summary-display-article article)
6777       (gnus-summary-recenter)
6778       (when article-window
6779         (gnus-eval-in-buffer-window gnus-article-buffer
6780           (setq endp (gnus-article-prev-page lines)))
6781         (when (and move endp)
6782           (cond (lines
6783                  (gnus-message 3 "Beginning of message"))
6784                 ((null lines)
6785                  (if (and (eq gnus-summary-goto-unread 'never)
6786                           (not (gnus-summary-first-article-p article)))
6787                      (gnus-summary-prev-article)
6788                    (gnus-summary-prev-unread-article))))))))
6789   (gnus-summary-position-point))
6790
6791 (defun gnus-summary-prev-page-or-article (&optional lines)
6792   "Show previous page of selected article.
6793 Argument LINES specifies lines to be scrolled down.
6794 If at the beginning of the article, go to the next article."
6795   (interactive "P")
6796   (gnus-summary-prev-page lines t))
6797
6798 (defun gnus-summary-scroll-up (lines)
6799   "Scroll up (or down) one line current article.
6800 Argument LINES specifies lines to be scrolled up (or down if negative)."
6801   (interactive "p")
6802   (gnus-configure-windows 'article)
6803   (gnus-summary-show-thread)
6804   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6805     (gnus-eval-in-buffer-window gnus-article-buffer
6806       (cond ((> lines 0)
6807              (when (gnus-article-next-page lines)
6808                (gnus-message 3 "End of message")))
6809             ((< lines 0)
6810              (gnus-article-prev-page (- lines))))))
6811   (gnus-summary-recenter)
6812   (gnus-summary-position-point))
6813
6814 (defun gnus-summary-scroll-down (lines)
6815   "Scroll down (or up) one line current article.
6816 Argument LINES specifies lines to be scrolled down (or up if negative)."
6817   (interactive "p")
6818   (gnus-summary-scroll-up (- lines)))
6819
6820 (defun gnus-summary-next-same-subject ()
6821   "Select next article which has the same subject as current one."
6822   (interactive)
6823   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6824
6825 (defun gnus-summary-prev-same-subject ()
6826   "Select previous article which has the same subject as current one."
6827   (interactive)
6828   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6829
6830 (defun gnus-summary-next-unread-same-subject ()
6831   "Select next unread article which has the same subject as current one."
6832   (interactive)
6833   (gnus-summary-next-article t (gnus-summary-article-subject)))
6834
6835 (defun gnus-summary-prev-unread-same-subject ()
6836   "Select previous unread article which has the same subject as current one."
6837   (interactive)
6838   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6839
6840 (defun gnus-summary-first-unread-article ()
6841   "Select the first unread article.
6842 Return nil if there are no unread articles."
6843   (interactive)
6844   (prog1
6845       (when (gnus-summary-first-subject t)
6846         (gnus-summary-show-thread)
6847         (gnus-summary-first-subject t)
6848         (gnus-summary-display-article (gnus-summary-article-number)))
6849     (gnus-summary-position-point)))
6850
6851 (defun gnus-summary-first-unread-subject ()
6852   "Place the point on the subject line of the first unread article.
6853 Return nil if there are no unread articles."
6854   (interactive)
6855   (prog1
6856       (when (gnus-summary-first-subject t)
6857         (gnus-summary-show-thread)
6858         (gnus-summary-first-subject t))
6859     (gnus-summary-position-point)))
6860
6861 (defun gnus-summary-first-unseen-subject ()
6862   "Place the point on the subject line of the first unseen article.
6863 Return nil if there are no unseen articles."
6864   (interactive)
6865   (prog1
6866       (when (gnus-summary-first-subject t t t)
6867         (gnus-summary-show-thread)
6868         (gnus-summary-first-subject t t t))
6869     (gnus-summary-position-point)))
6870
6871 (defun gnus-summary-first-unseen-or-unread-subject ()
6872   "Place the point on the subject line of the first unseen article.
6873 Return nil if there are no unseen articles."
6874   (interactive)
6875   (prog1
6876       (unless (when (gnus-summary-first-subject t t t)
6877                 (gnus-summary-show-thread)
6878                 (gnus-summary-first-subject t t t))
6879         (when (gnus-summary-first-subject t)
6880           (gnus-summary-show-thread)
6881           (gnus-summary-first-subject t)))
6882     (gnus-summary-position-point)))
6883
6884 (defun gnus-summary-first-article ()
6885   "Select the first article.
6886 Return nil if there are no articles."
6887   (interactive)
6888   (prog1
6889       (when (gnus-summary-first-subject)
6890         (gnus-summary-show-thread)
6891         (gnus-summary-first-subject)
6892         (gnus-summary-display-article (gnus-summary-article-number)))
6893     (gnus-summary-position-point)))
6894
6895 (defun gnus-summary-best-unread-article (&optional arg)
6896   "Select the unread article with the highest score.
6897 If given a prefix argument, select the next unread article that has a
6898 score higher than the default score."
6899   (interactive "P")
6900   (let ((article (if arg
6901                      (gnus-summary-better-unread-subject)
6902                    (gnus-summary-best-unread-subject))))
6903     (if article
6904         (gnus-summary-goto-article article)
6905       (error "No unread articles"))))
6906
6907 (defun gnus-summary-best-unread-subject ()
6908   "Select the unread subject with the highest score."
6909   (interactive)
6910   (let ((best -1000000)
6911         (data gnus-newsgroup-data)
6912         article score)
6913     (while data
6914       (and (gnus-data-unread-p (car data))
6915            (> (setq score
6916                     (gnus-summary-article-score (gnus-data-number (car data))))
6917               best)
6918            (setq best score
6919                  article (gnus-data-number (car data))))
6920       (setq data (cdr data)))
6921     (when article
6922       (gnus-summary-goto-subject article))
6923     (gnus-summary-position-point)
6924     article))
6925
6926 (defun gnus-summary-better-unread-subject ()
6927   "Select the first unread subject that has a score over the default score."
6928   (interactive)
6929   (let ((data gnus-newsgroup-data)
6930         article score)
6931     (while (and (setq article (gnus-data-number (car data)))
6932                 (or (gnus-data-read-p (car data))
6933                     (not (> (gnus-summary-article-score article)
6934                             gnus-summary-default-score))))
6935       (setq data (cdr data)))
6936     (when article
6937       (gnus-summary-goto-subject article))
6938     (gnus-summary-position-point)
6939     article))
6940
6941 (defun gnus-summary-last-subject ()
6942   "Go to the last displayed subject line in the group."
6943   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6944     (when article
6945       (gnus-summary-goto-subject article))))
6946
6947 (defun gnus-summary-goto-article (article &optional all-headers force)
6948   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6949 If ALL-HEADERS is non-nil, no header lines are hidden.
6950 If FORCE, go to the article even if it isn't displayed.  If FORCE
6951 is a number, it is the line the article is to be displayed on."
6952   (interactive
6953    (list
6954     (completing-read
6955      "Article number or Message-ID: "
6956      (mapcar (lambda (number) (list (int-to-string number)))
6957              gnus-newsgroup-limit))
6958     current-prefix-arg
6959     t))
6960   (prog1
6961       (if (and (stringp article)
6962                (string-match "@" article))
6963           (gnus-summary-refer-article article)
6964         (when (stringp article)
6965           (setq article (string-to-number article)))
6966         (if (gnus-summary-goto-subject article force)
6967             (gnus-summary-display-article article all-headers)
6968           (gnus-message 4 "Couldn't go to article %s" article) nil))
6969     (gnus-summary-position-point)))
6970
6971 (defun gnus-summary-goto-last-article ()
6972   "Go to the previously read article."
6973   (interactive)
6974   (prog1
6975       (when gnus-last-article
6976         (gnus-summary-goto-article gnus-last-article nil t))
6977     (gnus-summary-position-point)))
6978
6979 (defun gnus-summary-pop-article (number)
6980   "Pop one article off the history and go to the previous.
6981 NUMBER articles will be popped off."
6982   (interactive "p")
6983   (let (to)
6984     (setq gnus-newsgroup-history
6985           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6986     (if to
6987         (gnus-summary-goto-article (car to) nil t)
6988       (error "Article history empty")))
6989   (gnus-summary-position-point))
6990
6991 ;; Summary commands and functions for limiting the summary buffer.
6992
6993 (defun gnus-summary-limit-to-articles (n)
6994   "Limit the summary buffer to the next N articles.
6995 If not given a prefix, use the process marked articles instead."
6996   (interactive "P")
6997   (prog1
6998       (let ((articles (gnus-summary-work-articles n)))
6999         (setq gnus-newsgroup-processable nil)
7000         (gnus-summary-limit articles))
7001     (gnus-summary-position-point)))
7002
7003 (defun gnus-summary-pop-limit (&optional total)
7004   "Restore the previous limit.
7005 If given a prefix, remove all limits."
7006   (interactive "P")
7007   (when total
7008     (setq gnus-newsgroup-limits
7009           (list (mapcar (lambda (h) (mail-header-number h))
7010                         gnus-newsgroup-headers))))
7011   (unless gnus-newsgroup-limits
7012     (error "No limit to pop"))
7013   (prog1
7014       (gnus-summary-limit nil 'pop)
7015     (gnus-summary-position-point)))
7016
7017 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7018   "Limit the summary buffer to articles that have subjects that match a regexp.
7019 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7020   (interactive
7021    (list (read-string (if current-prefix-arg
7022                           "Exclude subject (regexp): "
7023                         "Limit to subject (regexp): "))
7024          nil current-prefix-arg))
7025   (unless header
7026     (setq header "subject"))
7027   (when (not (equal "" subject))
7028     (prog1
7029         (let ((articles (gnus-summary-find-matching
7030                          (or header "subject") subject 'all nil nil
7031                          not-matching)))
7032           (unless articles
7033             (error "Found no matches for \"%s\"" subject))
7034           (gnus-summary-limit articles))
7035       (gnus-summary-position-point))))
7036
7037 (defun gnus-summary-limit-to-author (from &optional not-matching)
7038   "Limit the summary buffer to articles that have authors that match a regexp.
7039 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7040   (interactive
7041    (list (read-string (if current-prefix-arg
7042                           "Exclude author (regexp): "
7043                         "Limit to author (regexp): "))
7044          current-prefix-arg))
7045   (gnus-summary-limit-to-subject from "from" not-matching))
7046
7047 (defun gnus-summary-limit-to-age (age &optional younger-p)
7048   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7049 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7050 articles that are younger than AGE days."
7051   (interactive
7052    (let ((younger current-prefix-arg)
7053          (days-got nil)
7054          days)
7055      (while (not days-got)
7056        (setq days (if younger
7057                       (read-string "Limit to articles within (in days): ")
7058                     (read-string "Limit to articles older than (in days): ")))
7059        (when (> (length days) 0)
7060          (setq days (read days)))
7061        (if (numberp days)
7062            (progn 
7063              (setq days-got t)
7064              (if (< days 0)
7065                  (progn 
7066                    (setq younger (not younger))
7067                    (setq days (* days -1)))))
7068          (message "Please enter a number.")
7069          (sleep-for 1)))
7070      (list days younger)))
7071   (prog1
7072       (let ((data gnus-newsgroup-data)
7073             (cutoff (days-to-time age))
7074             articles d date is-younger)
7075         (while (setq d (pop data))
7076           (when (and (vectorp (gnus-data-header d))
7077                      (setq date (mail-header-date (gnus-data-header d))))
7078             (setq is-younger (time-less-p
7079                               (time-since (condition-case ()
7080                                               (date-to-time date)
7081                                             (error '(0 0))))
7082                               cutoff))
7083             (when (if younger-p
7084                       is-younger
7085                     (not is-younger))
7086               (push (gnus-data-number d) articles))))
7087         (gnus-summary-limit (nreverse articles)))
7088     (gnus-summary-position-point)))
7089
7090 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7091   "Limit the summary buffer to articles that match an 'extra' header."
7092   (interactive
7093    (let ((header
7094           (intern
7095            (gnus-completing-read-with-default
7096             (symbol-name (car gnus-extra-headers))
7097             (if current-prefix-arg
7098                 "Exclude extra header:"
7099               "Limit extra header:")
7100             (mapcar (lambda (x)
7101                       (cons (symbol-name x) x))
7102                     gnus-extra-headers)
7103             nil
7104             t))))
7105      (list header
7106            (read-string (format "%s header %s (regexp): "
7107                                 (if current-prefix-arg "Exclude" "Limit to")
7108                                 header))
7109            current-prefix-arg)))
7110   (when (not (equal "" regexp))
7111     (prog1
7112         (let ((articles (gnus-summary-find-matching
7113                          (cons 'extra header) regexp 'all nil nil
7114                          not-matching)))
7115           (unless articles
7116             (error "Found no matches for \"%s\"" regexp))
7117           (gnus-summary-limit articles))
7118       (gnus-summary-position-point))))
7119
7120 (defun gnus-summary-limit-to-display-predicate ()
7121   "Limit the summary buffer to the predicated in the `display' group parameter."
7122   (interactive)
7123   (unless gnus-newsgroup-display
7124     (error "There is no `display' group parameter"))
7125   (let (articles)
7126     (dolist (number gnus-newsgroup-articles)
7127       (when (funcall gnus-newsgroup-display)
7128         (push number articles)))
7129     (gnus-summary-limit articles))
7130   (gnus-summary-position-point))
7131
7132 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7133 (make-obsolete
7134  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7135
7136 (defun gnus-summary-limit-to-unread (&optional all)
7137   "Limit the summary buffer to articles that are not marked as read.
7138 If ALL is non-nil, limit strictly to unread articles."
7139   (interactive "P")
7140   (if all
7141       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7142     (gnus-summary-limit-to-marks
7143      ;; Concat all the marks that say that an article is read and have
7144      ;; those removed.
7145      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7146            gnus-killed-mark gnus-kill-file-mark
7147            gnus-low-score-mark gnus-expirable-mark
7148            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7149            gnus-duplicate-mark gnus-souped-mark)
7150      'reverse)))
7151
7152 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7153 (make-obsolete 'gnus-summary-delete-marked-with
7154                'gnus-summary-limit-exlude-marks)
7155
7156 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7157   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7158 If REVERSE, limit the summary buffer to articles that are marked
7159 with MARKS.  MARKS can either be a string of marks or a list of marks.
7160 Returns how many articles were removed."
7161   (interactive "sMarks: ")
7162   (gnus-summary-limit-to-marks marks t))
7163
7164 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7165   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7166 If REVERSE (the prefix), limit the summary buffer to articles that are
7167 not marked with MARKS.  MARKS can either be a string of marks or a
7168 list of marks.
7169 Returns how many articles were removed."
7170   (interactive "sMarks: \nP")
7171   (prog1
7172       (let ((data gnus-newsgroup-data)
7173             (marks (if (listp marks) marks
7174                      (append marks nil))) ; Transform to list.
7175             articles)
7176         (while data
7177           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7178                   (memq (gnus-data-mark (car data)) marks))
7179             (push (gnus-data-number (car data)) articles))
7180           (setq data (cdr data)))
7181         (gnus-summary-limit articles))
7182     (gnus-summary-position-point)))
7183
7184 (defun gnus-summary-limit-to-score (score)
7185   "Limit to articles with score at or above SCORE."
7186   (interactive "NLimit to articles with score of at least: ")
7187   (let ((data gnus-newsgroup-data)
7188         articles)
7189     (while data
7190       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7191                 score)
7192         (push (gnus-data-number (car data)) articles))
7193       (setq data (cdr data)))
7194     (prog1
7195         (gnus-summary-limit articles)
7196       (gnus-summary-position-point))))
7197
7198 (defun gnus-summary-limit-include-thread (id)
7199   "Display all the hidden articles that is in the thread with ID in it.
7200 When called interactively, ID is the Message-ID of the current
7201 article."
7202   (interactive (list (mail-header-id (gnus-summary-article-header))))
7203   (let ((articles (gnus-articles-in-thread
7204                    (gnus-id-to-thread (gnus-root-id id)))))
7205     (prog1
7206         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7207       (gnus-summary-limit-include-matching-articles
7208        "subject"
7209        (regexp-quote (gnus-simplify-subject-re
7210                       (mail-header-subject (gnus-id-to-header id)))))
7211       (gnus-summary-position-point))))
7212
7213 (defun gnus-summary-limit-include-matching-articles (header regexp)
7214   "Display all the hidden articles that have HEADERs that match REGEXP."
7215   (interactive (list (read-string "Match on header: ")
7216                      (read-string "Regexp: ")))
7217   (let ((articles (gnus-find-matching-articles header regexp)))
7218     (prog1
7219         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7220       (gnus-summary-position-point))))
7221
7222 (defun gnus-summary-limit-include-dormant ()
7223   "Display all the hidden articles that are marked as dormant.
7224 Note that this command only works on a subset of the articles currently
7225 fetched for this group."
7226   (interactive)
7227   (unless gnus-newsgroup-dormant
7228     (error "There are no dormant articles in this group"))
7229   (prog1
7230       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7231     (gnus-summary-position-point)))
7232
7233 (defun gnus-summary-limit-exclude-dormant ()
7234   "Hide all dormant articles."
7235   (interactive)
7236   (prog1
7237       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7238     (gnus-summary-position-point)))
7239
7240 (defun gnus-summary-limit-exclude-childless-dormant ()
7241   "Hide all dormant articles that have no children."
7242   (interactive)
7243   (let ((data (gnus-data-list t))
7244         articles d children)
7245     ;; Find all articles that are either not dormant or have
7246     ;; children.
7247     (while (setq d (pop data))
7248       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7249                 (and (setq children
7250                            (gnus-article-children (gnus-data-number d)))
7251                      (let (found)
7252                        (while children
7253                          (when (memq (car children) articles)
7254                            (setq children nil
7255                                  found t))
7256                          (pop children))
7257                        found)))
7258         (push (gnus-data-number d) articles)))
7259     ;; Do the limiting.
7260     (prog1
7261         (gnus-summary-limit articles)
7262       (gnus-summary-position-point))))
7263
7264 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7265   "Mark all unread excluded articles as read.
7266 If ALL, mark even excluded ticked and dormants as read."
7267   (interactive "P")
7268   (let ((articles (gnus-sorted-complement
7269                    (sort
7270                     (mapcar (lambda (h) (mail-header-number h))
7271                             gnus-newsgroup-headers)
7272                     '<)
7273                    (sort gnus-newsgroup-limit '<)))
7274         article)
7275     (setq gnus-newsgroup-unreads
7276           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7277     (if all
7278         (setq gnus-newsgroup-dormant nil
7279               gnus-newsgroup-marked nil
7280               gnus-newsgroup-reads
7281               (nconc
7282                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7283                gnus-newsgroup-reads))
7284       (while (setq article (pop articles))
7285         (unless (or (memq article gnus-newsgroup-dormant)
7286                     (memq article gnus-newsgroup-marked))
7287           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7288
7289 (defun gnus-summary-limit (articles &optional pop)
7290   (if pop
7291       ;; We pop the previous limit off the stack and use that.
7292       (setq articles (car gnus-newsgroup-limits)
7293             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7294     ;; We use the new limit, so we push the old limit on the stack.
7295     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7296   ;; Set the limit.
7297   (setq gnus-newsgroup-limit articles)
7298   (let ((total (length gnus-newsgroup-data))
7299         (data (gnus-data-find-list (gnus-summary-article-number)))
7300         (gnus-summary-mark-below nil)   ; Inhibit this.
7301         found)
7302     ;; This will do all the work of generating the new summary buffer
7303     ;; according to the new limit.
7304     (gnus-summary-prepare)
7305     ;; Hide any threads, possibly.
7306     (gnus-summary-maybe-hide-threads)
7307     ;; Try to return to the article you were at, or one in the
7308     ;; neighborhood.
7309     (when data
7310       ;; We try to find some article after the current one.
7311       (while data
7312         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7313           (setq data nil
7314                 found t))
7315         (setq data (cdr data))))
7316     (unless found
7317       ;; If there is no data, that means that we were after the last
7318       ;; article.  The same goes when we can't find any articles
7319       ;; after the current one.
7320       (goto-char (point-max))
7321       (gnus-summary-find-prev))
7322     (gnus-set-mode-line 'summary)
7323     ;; We return how many articles were removed from the summary
7324     ;; buffer as a result of the new limit.
7325     (- total (length gnus-newsgroup-data))))
7326
7327 (defsubst gnus-invisible-cut-children (threads)
7328   (let ((num 0))
7329     (while threads
7330       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7331         (incf num))
7332       (pop threads))
7333     (< num 2)))
7334
7335 (defsubst gnus-cut-thread (thread)
7336   "Go forwards in the thread until we find an article that we want to display."
7337   (when (or (eq gnus-fetch-old-headers 'some)
7338             (eq gnus-fetch-old-headers 'invisible)
7339             (numberp gnus-fetch-old-headers)
7340             (eq gnus-build-sparse-threads 'some)
7341             (eq gnus-build-sparse-threads 'more))
7342     ;; Deal with old-fetched headers and sparse threads.
7343     (while (and
7344             thread
7345             (or
7346              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7347              (gnus-summary-article-ancient-p
7348               (mail-header-number (car thread))))
7349             (if (or (<= (length (cdr thread)) 1)
7350                     (eq gnus-fetch-old-headers 'invisible))
7351                 (setq gnus-newsgroup-limit
7352                       (delq (mail-header-number (car thread))
7353                             gnus-newsgroup-limit)
7354                       thread (cadr thread))
7355               (when (gnus-invisible-cut-children (cdr thread))
7356                 (let ((th (cdr thread)))
7357                   (while th
7358                     (if (memq (mail-header-number (caar th))
7359                               gnus-newsgroup-limit)
7360                         (setq thread (car th)
7361                               th nil)
7362                       (setq th (cdr th))))))))))
7363   thread)
7364
7365 (defun gnus-cut-threads (threads)
7366   "Cut off all uninteresting articles from the beginning of threads."
7367   (when (or (eq gnus-fetch-old-headers 'some)
7368             (eq gnus-fetch-old-headers 'invisible)
7369             (numberp gnus-fetch-old-headers)
7370             (eq gnus-build-sparse-threads 'some)
7371             (eq gnus-build-sparse-threads 'more))
7372     (let ((th threads))
7373       (while th
7374         (setcar th (gnus-cut-thread (car th)))
7375         (setq th (cdr th)))))
7376   ;; Remove nixed out threads.
7377   (delq nil threads))
7378
7379 (defun gnus-summary-initial-limit (&optional show-if-empty)
7380   "Figure out what the initial limit is supposed to be on group entry.
7381 This entails weeding out unwanted dormants, low-scored articles,
7382 fetch-old-headers verbiage, and so on."
7383   ;; Most groups have nothing to remove.
7384   (if (or gnus-inhibit-limiting
7385           (and (null gnus-newsgroup-dormant)
7386                (eq gnus-newsgroup-display 'gnus-not-ignore)
7387                (not (eq gnus-fetch-old-headers 'some))
7388                (not (numberp gnus-fetch-old-headers))
7389                (not (eq gnus-fetch-old-headers 'invisible))
7390                (null gnus-summary-expunge-below)
7391                (not (eq gnus-build-sparse-threads 'some))
7392                (not (eq gnus-build-sparse-threads 'more))
7393                (null gnus-thread-expunge-below)
7394                (not gnus-use-nocem)))
7395       ()                                ; Do nothing.
7396     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7397     (setq gnus-newsgroup-limit nil)
7398     (mapatoms
7399      (lambda (node)
7400        (unless (car (symbol-value node))
7401          ;; These threads have no parents -- they are roots.
7402          (let ((nodes (cdr (symbol-value node)))
7403                thread)
7404            (while nodes
7405              (if (and gnus-thread-expunge-below
7406                       (< (gnus-thread-total-score (car nodes))
7407                          gnus-thread-expunge-below))
7408                  (gnus-expunge-thread (pop nodes))
7409                (setq thread (pop nodes))
7410                (gnus-summary-limit-children thread))))))
7411      gnus-newsgroup-dependencies)
7412     ;; If this limitation resulted in an empty group, we might
7413     ;; pop the previous limit and use it instead.
7414     (when (and (not gnus-newsgroup-limit)
7415                show-if-empty)
7416       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7417     gnus-newsgroup-limit))
7418
7419 (defun gnus-summary-limit-children (thread)
7420   "Return 1 if this subthread is visible and 0 if it is not."
7421   ;; First we get the number of visible children to this thread.  This
7422   ;; is done by recursing down the thread using this function, so this
7423   ;; will really go down to a leaf article first, before slowly
7424   ;; working its way up towards the root.
7425   (when thread
7426     (let ((children
7427            (if (cdr thread)
7428                (apply '+ (mapcar 'gnus-summary-limit-children
7429                                  (cdr thread)))
7430              0))
7431           (number (mail-header-number (car thread)))
7432           score)
7433       (if (and
7434            (not (memq number gnus-newsgroup-marked))
7435            (or
7436             ;; If this article is dormant and has absolutely no visible
7437             ;; children, then this article isn't visible.
7438             (and (memq number gnus-newsgroup-dormant)
7439                  (zerop children))
7440             ;; If this is "fetch-old-headered" and there is no
7441             ;; visible children, then we don't want this article.
7442             (and (or (eq gnus-fetch-old-headers 'some)
7443                      (numberp gnus-fetch-old-headers))
7444                  (gnus-summary-article-ancient-p number)
7445                  (zerop children))
7446             ;; If this is "fetch-old-headered" and `invisible', then
7447             ;; we don't want this article.
7448             (and (eq gnus-fetch-old-headers 'invisible)
7449                  (gnus-summary-article-ancient-p number))
7450             ;; If this is a sparsely inserted article with no children,
7451             ;; we don't want it.
7452             (and (eq gnus-build-sparse-threads 'some)
7453                  (gnus-summary-article-sparse-p number)
7454                  (zerop children))
7455             ;; If we use expunging, and this article is really
7456             ;; low-scored, then we don't want this article.
7457             (when (and gnus-summary-expunge-below
7458                        (< (setq score
7459                                 (or (cdr (assq number gnus-newsgroup-scored))
7460                                     gnus-summary-default-score))
7461                           gnus-summary-expunge-below))
7462               ;; We increase the expunge-tally here, but that has
7463               ;; nothing to do with the limits, really.
7464               (incf gnus-newsgroup-expunged-tally)
7465               ;; We also mark as read here, if that's wanted.
7466               (when (and gnus-summary-mark-below
7467                          (< score gnus-summary-mark-below))
7468                 (setq gnus-newsgroup-unreads
7469                       (delq number gnus-newsgroup-unreads))
7470                 (if gnus-newsgroup-auto-expire
7471                     (push number gnus-newsgroup-expirable)
7472                   (push (cons number gnus-low-score-mark)
7473                         gnus-newsgroup-reads)))
7474               t)
7475             ;; Do the `display' group parameter.
7476             (and gnus-newsgroup-display
7477                  (not (funcall gnus-newsgroup-display)))
7478             ;; Check NoCeM things.
7479             (if (and gnus-use-nocem
7480                      (gnus-nocem-unwanted-article-p
7481                       (mail-header-id (car thread))))
7482                 (progn
7483                   (setq gnus-newsgroup-unreads
7484                         (delq number gnus-newsgroup-unreads))
7485                   t))))
7486           ;; Nope, invisible article.
7487           0
7488         ;; Ok, this article is to be visible, so we add it to the limit
7489         ;; and return 1.
7490         (push number gnus-newsgroup-limit)
7491         1))))
7492
7493 (defun gnus-expunge-thread (thread)
7494   "Mark all articles in THREAD as read."
7495   (let* ((number (mail-header-number (car thread))))
7496     (incf gnus-newsgroup-expunged-tally)
7497     ;; We also mark as read here, if that's wanted.
7498     (setq gnus-newsgroup-unreads
7499           (delq number gnus-newsgroup-unreads))
7500     (if gnus-newsgroup-auto-expire
7501         (push number gnus-newsgroup-expirable)
7502       (push (cons number gnus-low-score-mark)
7503             gnus-newsgroup-reads)))
7504   ;; Go recursively through all subthreads.
7505   (mapcar 'gnus-expunge-thread (cdr thread)))
7506
7507 ;; Summary article oriented commands
7508
7509 (defun gnus-summary-refer-parent-article (n)
7510   "Refer parent article N times.
7511 If N is negative, go to ancestor -N instead.
7512 The difference between N and the number of articles fetched is returned."
7513   (interactive "p")
7514   (let ((skip 1)
7515         error header ref)
7516     (when (not (natnump n))
7517       (setq skip (abs n)
7518             n 1))
7519     (while (and (> n 0)
7520                 (not error))
7521       (setq header (gnus-summary-article-header))
7522       (if (and (eq (mail-header-number header)
7523                    (cdr gnus-article-current))
7524                (equal gnus-newsgroup-name
7525                       (car gnus-article-current)))
7526           ;; If we try to find the parent of the currently
7527           ;; displayed article, then we take a look at the actual
7528           ;; References header, since this is slightly more
7529           ;; reliable than the References field we got from the
7530           ;; server.
7531           (save-excursion
7532             (set-buffer gnus-original-article-buffer)
7533             (nnheader-narrow-to-headers)
7534             (unless (setq ref (message-fetch-field "references"))
7535               (setq ref (message-fetch-field "in-reply-to")))
7536             (widen))
7537         (setq ref
7538               ;; It's not the current article, so we take a bet on
7539               ;; the value we got from the server.
7540               (mail-header-references header)))
7541       (if (and ref
7542                (not (equal ref "")))
7543           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7544             (gnus-message 1 "Couldn't find parent"))
7545         (gnus-message 1 "No references in article %d"
7546                       (gnus-summary-article-number))
7547         (setq error t))
7548       (decf n))
7549     (gnus-summary-position-point)
7550     n))
7551
7552 (defun gnus-summary-refer-references ()
7553   "Fetch all articles mentioned in the References header.
7554 Return the number of articles fetched."
7555   (interactive)
7556   (let ((ref (mail-header-references (gnus-summary-article-header)))
7557         (current (gnus-summary-article-number))
7558         (n 0))
7559     (if (or (not ref)
7560             (equal ref ""))
7561         (error "No References in the current article")
7562       ;; For each Message-ID in the References header...
7563       (while (string-match "<[^>]*>" ref)
7564         (incf n)
7565         ;; ... fetch that article.
7566         (gnus-summary-refer-article
7567          (prog1 (match-string 0 ref)
7568            (setq ref (substring ref (match-end 0))))))
7569       (gnus-summary-goto-subject current)
7570       (gnus-summary-position-point)
7571       n)))
7572
7573 (defun gnus-summary-refer-thread (&optional limit)
7574   "Fetch all articles in the current thread.
7575 If LIMIT (the numerical prefix), fetch that many old headers instead
7576 of what's specified by the `gnus-refer-thread-limit' variable."
7577   (interactive "P")
7578   (let ((id (mail-header-id (gnus-summary-article-header)))
7579         (limit (if limit (prefix-numeric-value limit)
7580                  gnus-refer-thread-limit)))
7581     ;; We want to fetch LIMIT *old* headers, but we also have to
7582     ;; re-fetch all the headers in the current buffer, because many of
7583     ;; them may be undisplayed.  So we adjust LIMIT.
7584     (when (numberp limit)
7585       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7586     (unless (eq gnus-fetch-old-headers 'invisible)
7587       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7588       ;; Retrieve the headers and read them in.
7589       (if (eq (gnus-retrieve-headers
7590                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7591               'nov)
7592           (gnus-build-all-threads)
7593         (error "Can't fetch thread from backends that don't support NOV"))
7594       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7595     (gnus-summary-limit-include-thread id)))
7596
7597 (defun gnus-summary-refer-article (message-id)
7598   "Fetch an article specified by MESSAGE-ID."
7599   (interactive "sMessage-ID: ")
7600   (when (and (stringp message-id)
7601              (not (zerop (length message-id))))
7602     ;; Construct the correct Message-ID if necessary.
7603     ;; Suggested by tale@pawl.rpi.edu.
7604     (unless (string-match "^<" message-id)
7605       (setq message-id (concat "<" message-id)))
7606     (unless (string-match ">$" message-id)
7607       (setq message-id (concat message-id ">")))
7608     (let* ((header (gnus-id-to-header message-id))
7609            (sparse (and header
7610                         (gnus-summary-article-sparse-p
7611                          (mail-header-number header))
7612                         (memq (mail-header-number header)
7613                               gnus-newsgroup-limit)))
7614            number)
7615       (cond
7616        ;; If the article is present in the buffer we just go to it.
7617        ((and header
7618              (or (not (gnus-summary-article-sparse-p
7619                        (mail-header-number header)))
7620                  sparse))
7621         (prog1
7622             (gnus-summary-goto-article
7623              (mail-header-number header) nil t)
7624           (when sparse
7625             (gnus-summary-update-article (mail-header-number header)))))
7626        (t
7627         ;; We fetch the article.
7628         (catch 'found
7629           (dolist (gnus-override-method (gnus-refer-article-methods))
7630             (gnus-check-server gnus-override-method)
7631             ;; Fetch the header, and display the article.
7632             (when (setq number (gnus-summary-insert-subject message-id))
7633               (gnus-summary-select-article nil nil nil number)
7634               (throw 'found t)))
7635           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7636
7637 (defun gnus-refer-article-methods ()
7638   "Return a list of referrable methods."
7639   (cond
7640    ;; No method, so we default to current and native.
7641    ((null gnus-refer-article-method)
7642     (list gnus-current-select-method gnus-select-method))
7643    ;; Current.
7644    ((eq 'current gnus-refer-article-method)
7645     (list gnus-current-select-method))
7646    ;; List of select methods.
7647    ((not (and (symbolp (car gnus-refer-article-method))
7648               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7649     (let (out)
7650       (dolist (method gnus-refer-article-method)
7651         (push (if (eq 'current method)
7652                   gnus-current-select-method
7653                 method)
7654               out))
7655       (nreverse out)))
7656    ;; One single select method.
7657    (t
7658     (list gnus-refer-article-method))))
7659
7660 (defun gnus-summary-edit-parameters ()
7661   "Edit the group parameters of the current group."
7662   (interactive)
7663   (gnus-group-edit-group gnus-newsgroup-name 'params))
7664
7665 (defun gnus-summary-customize-parameters ()
7666   "Customize the group parameters of the current group."
7667   (interactive)
7668   (gnus-group-customize gnus-newsgroup-name))
7669
7670 (defun gnus-summary-enter-digest-group (&optional force)
7671   "Enter an nndoc group based on the current article.
7672 If FORCE, force a digest interpretation.  If not, try
7673 to guess what the document format is."
7674   (interactive "P")
7675   (let ((conf gnus-current-window-configuration))
7676     (save-excursion
7677       (gnus-summary-select-article))
7678     (setq gnus-current-window-configuration conf)
7679     (let* ((name (format "%s-%d"
7680                          (gnus-group-prefixed-name
7681                           gnus-newsgroup-name (list 'nndoc ""))
7682                          (save-excursion
7683                            (set-buffer gnus-summary-buffer)
7684                            gnus-current-article)))
7685            (ogroup gnus-newsgroup-name)
7686            (params (append (gnus-info-params (gnus-get-info ogroup))
7687                            (list (cons 'to-group ogroup))
7688                            (list (cons 'save-article-group ogroup))))
7689            (case-fold-search t)
7690            (buf (current-buffer))
7691            dig to-address)
7692       (save-excursion
7693         (set-buffer gnus-original-article-buffer)
7694         ;; Have the digest group inherit the main mail address of
7695         ;; the parent article.
7696         (when (setq to-address (or (message-fetch-field "reply-to")
7697                                    (message-fetch-field "from")))
7698           (setq params (append
7699                         (list (cons 'to-address
7700                                     (funcall gnus-decode-encoded-word-function
7701                                              to-address))))))
7702         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7703         (insert-buffer-substring gnus-original-article-buffer)
7704         ;; Remove lines that may lead nndoc to misinterpret the
7705         ;; document type.
7706         (narrow-to-region
7707          (goto-char (point-min))
7708          (or (search-forward "\n\n" nil t) (point)))
7709         (goto-char (point-min))
7710         (delete-matching-lines "^Path:\\|^From ")
7711         (widen))
7712       (unwind-protect
7713           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7714                     (gnus-newsgroup-ephemeral-ignored-charsets
7715                      gnus-newsgroup-ignored-charsets))
7716                 (gnus-group-read-ephemeral-group
7717                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7718                               (nndoc-article-type
7719                                ,(if force 'mbox 'guess)))
7720                  t nil nil nil
7721                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7722                                                         "ADAPT")))))
7723               ;; Make all postings to this group go to the parent group.
7724               (nconc (gnus-info-params (gnus-get-info name))
7725                      params)
7726             ;; Couldn't select this doc group.
7727             (switch-to-buffer buf)
7728             (gnus-set-global-variables)
7729             (gnus-configure-windows 'summary)
7730             (gnus-message 3 "Article couldn't be entered?"))
7731         (kill-buffer dig)))))
7732
7733 (defun gnus-summary-read-document (n)
7734   "Open a new group based on the current article(s).
7735 This will allow you to read digests and other similar
7736 documents as newsgroups.
7737 Obeys the standard process/prefix convention."
7738   (interactive "P")
7739   (let* ((articles (gnus-summary-work-articles n))
7740          (ogroup gnus-newsgroup-name)
7741          (params (append (gnus-info-params (gnus-get-info ogroup))
7742                          (list (cons 'to-group ogroup))))
7743          article group egroup groups vgroup)
7744     (while (setq article (pop articles))
7745       (setq group (format "%s-%d" gnus-newsgroup-name article))
7746       (gnus-summary-remove-process-mark article)
7747       (when (gnus-summary-display-article article)
7748         (save-excursion
7749           (with-temp-buffer
7750             (insert-buffer-substring gnus-original-article-buffer)
7751             ;; Remove some headers that may lead nndoc to make
7752             ;; the wrong guess.
7753             (message-narrow-to-head)
7754             (goto-char (point-min))
7755             (delete-matching-lines "^\\(Path\\):\\|^From ")
7756             (widen)
7757             (if (setq egroup
7758                       (gnus-group-read-ephemeral-group
7759                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7760                                      (nndoc-article-type guess))
7761                        t nil t))
7762                 (progn
7763             ;; Make all postings to this group go to the parent group.
7764                   (nconc (gnus-info-params (gnus-get-info egroup))
7765                          params)
7766                   (push egroup groups))
7767               ;; Couldn't select this doc group.
7768               (gnus-error 3 "Article couldn't be entered"))))))
7769     ;; Now we have selected all the documents.
7770     (cond
7771      ((not groups)
7772       (error "None of the articles could be interpreted as documents"))
7773      ((gnus-group-read-ephemeral-group
7774        (setq vgroup (format
7775                      "nnvirtual:%s-%s" gnus-newsgroup-name
7776                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7777        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7778        t
7779        (cons (current-buffer) 'summary)))
7780      (t
7781       (error "Couldn't select virtual nndoc group")))))
7782
7783 (defun gnus-summary-isearch-article (&optional regexp-p)
7784   "Do incremental search forward on the current article.
7785 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7786   (interactive "P")
7787   (gnus-summary-select-article)
7788   (gnus-configure-windows 'article)
7789   (gnus-eval-in-buffer-window gnus-article-buffer
7790     (save-restriction
7791       (widen)
7792       (isearch-forward regexp-p))))
7793
7794 (defun gnus-summary-search-article-forward (regexp &optional backward)
7795   "Search for an article containing REGEXP forward.
7796 If BACKWARD, search backward instead."
7797   (interactive
7798    (list (read-string
7799           (format "Search article %s (regexp%s): "
7800                   (if current-prefix-arg "backward" "forward")
7801                   (if gnus-last-search-regexp
7802                       (concat ", default " gnus-last-search-regexp)
7803                     "")))
7804          current-prefix-arg))
7805   (if (string-equal regexp "")
7806       (setq regexp (or gnus-last-search-regexp ""))
7807     (setq gnus-last-search-regexp regexp)
7808     (setq gnus-article-before-search gnus-current-article))
7809   ;; Intentionally set gnus-last-article.
7810   (setq gnus-last-article gnus-article-before-search)
7811   (let ((gnus-last-article gnus-last-article))
7812     (if (gnus-summary-search-article regexp backward)
7813         (gnus-summary-show-thread)
7814       (error "Search failed: \"%s\"" regexp))))
7815
7816 (defun gnus-summary-search-article-backward (regexp)
7817   "Search for an article containing REGEXP backward."
7818   (interactive
7819    (list (read-string
7820           (format "Search article backward (regexp%s): "
7821                   (if gnus-last-search-regexp
7822                       (concat ", default " gnus-last-search-regexp)
7823                     "")))))
7824   (gnus-summary-search-article-forward regexp 'backward))
7825
7826 (defun gnus-summary-search-article (regexp &optional backward)
7827   "Search for an article containing REGEXP.
7828 Optional argument BACKWARD means do search for backward.
7829 `gnus-select-article-hook' is not called during the search."
7830   ;; We have to require this here to make sure that the following
7831   ;; dynamic binding isn't shadowed by autoloading.
7832   (require 'gnus-async)
7833   (require 'gnus-art)
7834   (let ((gnus-select-article-hook nil)  ;Disable hook.
7835         (gnus-article-prepare-hook nil)
7836         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7837         (gnus-use-article-prefetch nil)
7838         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7839         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7840         (sum (current-buffer))
7841         (gnus-display-mime-function nil)
7842         (found nil)
7843         point)
7844     (gnus-save-hidden-threads
7845       (gnus-summary-select-article)
7846       (set-buffer gnus-article-buffer)
7847       (goto-char (window-point (get-buffer-window (current-buffer))))
7848       (when backward
7849         (forward-line -1))
7850       (while (not found)
7851         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7852         (if (if backward
7853                 (re-search-backward regexp nil t)
7854               (re-search-forward regexp nil t))
7855             ;; We found the regexp.
7856             (progn
7857               (setq found 'found)
7858               (beginning-of-line)
7859               (set-window-start
7860                (get-buffer-window (current-buffer))
7861                (point))
7862               (forward-line 1)
7863               (set-window-point
7864                (get-buffer-window (current-buffer))
7865                (point))
7866               (set-buffer sum)
7867               (setq point (point)))
7868           ;; We didn't find it, so we go to the next article.
7869           (set-buffer sum)
7870           (setq found 'not)
7871           (while (eq found 'not)
7872             (if (not (if backward (gnus-summary-find-prev)
7873                        (gnus-summary-find-next)))
7874                 ;; No more articles.
7875                 (setq found t)
7876               ;; Select the next article and adjust point.
7877               (unless (gnus-summary-article-sparse-p
7878                        (gnus-summary-article-number))
7879                 (setq found nil)
7880                 (gnus-summary-select-article)
7881                 (set-buffer gnus-article-buffer)
7882                 (widen)
7883                 (goto-char (if backward (point-max) (point-min))))))))
7884       (gnus-message 7 ""))
7885     ;; Return whether we found the regexp.
7886     (when (eq found 'found)
7887       (goto-char point)
7888       (gnus-summary-show-thread)
7889       (gnus-summary-goto-subject gnus-current-article)
7890       (gnus-summary-position-point)
7891       t)))
7892
7893 (defun gnus-find-matching-articles (header regexp)
7894   "Return a list of all articles that match REGEXP on HEADER.
7895 This search includes all articles in the current group that Gnus has
7896 fetched headers for, whether they are displayed or not."
7897   (let ((articles nil)
7898         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7899         (case-fold-search t))
7900     (dolist (header gnus-newsgroup-headers)
7901       (when (string-match regexp (funcall func header))
7902         (push (mail-header-number header) articles)))
7903     (nreverse articles)))
7904
7905 (defun gnus-summary-find-matching (header regexp &optional backward unread
7906                                           not-case-fold not-matching)
7907   "Return a list of all articles that match REGEXP on HEADER.
7908 The search stars on the current article and goes forwards unless
7909 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7910 If UNREAD is non-nil, only unread articles will
7911 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7912 in the comparisons. If NOT-MATCHING, return a list of all articles that
7913 not match REGEXP on HEADER."
7914   (let ((case-fold-search (not not-case-fold))
7915         articles d func)
7916     (if (consp header)
7917         (if (eq (car header) 'extra)
7918             (setq func
7919                   `(lambda (h)
7920                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7921                          "")))
7922           (error "%s is an invalid header" header))
7923       (unless (fboundp (intern (concat "mail-header-" header)))
7924         (error "%s is not a valid header" header))
7925       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7926     (dolist (d (if (eq backward 'all)
7927                    gnus-newsgroup-data
7928                  (gnus-data-find-list
7929                   (gnus-summary-article-number)
7930                   (gnus-data-list backward))))
7931       (when (and (or (not unread)       ; We want all articles...
7932                      (gnus-data-unread-p d)) ; Or just unreads.
7933                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7934                  (if not-matching
7935                      (not (string-match
7936                            regexp
7937                            (funcall func (gnus-data-header d))))
7938                    (string-match regexp
7939                                  (funcall func (gnus-data-header d)))))
7940         (push (gnus-data-number d) articles))) ; Success!
7941     (nreverse articles)))
7942
7943 (defun gnus-summary-execute-command (header regexp command &optional backward)
7944   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7945 If HEADER is an empty string (or nil), the match is done on the entire
7946 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7947   (interactive
7948    (list (let ((completion-ignore-case t))
7949            (completing-read
7950             "Header name: "
7951             (mapcar (lambda (header) (list (format "%s" header)))
7952                     (append
7953                      '("Number" "Subject" "From" "Lines" "Date"
7954                        "Message-ID" "Xref" "References" "Body")
7955                      gnus-extra-headers))
7956             nil 'require-match))
7957          (read-string "Regexp: ")
7958          (read-key-sequence "Command: ")
7959          current-prefix-arg))
7960   (when (equal header "Body")
7961     (setq header ""))
7962   ;; Hidden thread subtrees must be searched as well.
7963   (gnus-summary-show-all-threads)
7964   ;; We don't want to change current point nor window configuration.
7965   (save-excursion
7966     (save-window-excursion
7967       (gnus-message 6 "Executing %s..." (key-description command))
7968 ;; We'd like to execute COMMAND interactively so as to give arguments.
7969       (gnus-execute header regexp
7970                     `(call-interactively ',(key-binding command))
7971                     backward)
7972       (gnus-message 6 "Executing %s...done" (key-description command)))))
7973
7974 (defun gnus-summary-beginning-of-article ()
7975   "Scroll the article back to the beginning."
7976   (interactive)
7977   (gnus-summary-select-article)
7978   (gnus-configure-windows 'article)
7979   (gnus-eval-in-buffer-window gnus-article-buffer
7980     (widen)
7981     (goto-char (point-min))
7982     (when gnus-page-broken
7983       (gnus-narrow-to-page))))
7984
7985 (defun gnus-summary-end-of-article ()
7986   "Scroll to the end of the article."
7987   (interactive)
7988   (gnus-summary-select-article)
7989   (gnus-configure-windows 'article)
7990   (gnus-eval-in-buffer-window gnus-article-buffer
7991     (widen)
7992     (goto-char (point-max))
7993     (recenter -3)
7994     (when gnus-page-broken
7995       (gnus-narrow-to-page))))
7996
7997 (defun gnus-summary-print-truncate-and-quote (string &optional len)
7998   "Truncate to LEN and quote all \"(\"'s in STRING."
7999   (gnus-replace-in-string (if (and len (> (length string) len))
8000                               (substring string 0 len)
8001                             string)
8002                           "[()]" "\\\\\\&"))
8003
8004 (defun gnus-summary-print-article (&optional filename n)
8005   "Generate and print a PostScript image of the N next (mail) articles.
8006
8007 If N is negative, print the N previous articles.  If N is nil and articles
8008 have been marked with the process mark, print these instead.
8009
8010 If the optional first argument FILENAME is nil, send the image to the
8011 printer.  If FILENAME is a string, save the PostScript image in a file with
8012 that name.  If FILENAME is a number, prompt the user for the name of the file
8013 to save in."
8014   (interactive (list (ps-print-preprint current-prefix-arg)))
8015   (dolist (article (gnus-summary-work-articles n))
8016     (gnus-summary-select-article nil nil 'pseudo article)
8017     (gnus-eval-in-buffer-window gnus-article-buffer
8018       (gnus-print-buffer))
8019     (gnus-summary-remove-process-mark article))
8020   (ps-despool filename))
8021
8022 (defun gnus-print-buffer ()
8023   (let ((buffer (generate-new-buffer " *print*")))
8024     (unwind-protect
8025         (progn
8026           (copy-to-buffer buffer (point-min) (point-max))
8027           (set-buffer buffer)
8028           (gnus-article-delete-invisible-text)
8029           (when (gnus-visual-p 'article-highlight 'highlight)
8030             ;; Copy-to-buffer doesn't copy overlay.  So redo
8031             ;; highlight.
8032             (let ((gnus-article-buffer buffer))
8033               (gnus-article-highlight-citation t)
8034               (gnus-article-highlight-signature)))
8035           (let ((ps-left-header
8036                  (list
8037                   (concat "("
8038                           (gnus-summary-print-truncate-and-quote
8039                            (mail-header-subject gnus-current-headers)
8040                            66) ")")
8041                   (concat "("
8042                           (gnus-summary-print-truncate-and-quote
8043                            (mail-header-from gnus-current-headers)
8044                            45) ")")))
8045                 (ps-right-header
8046                  (list
8047                   "/pagenumberstring load"
8048                   (concat "("
8049                           (mail-header-date gnus-current-headers) ")"))))
8050             (gnus-run-hooks 'gnus-ps-print-hook)
8051             (save-excursion
8052               (if window-system
8053                   (ps-spool-buffer-with-faces)
8054                 (ps-spool-buffer)))))
8055       (kill-buffer buffer))))
8056
8057 (defun gnus-summary-show-article (&optional arg)
8058   "Force redisplaying of the current article.
8059 If ARG (the prefix) is a number, show the article with the charset
8060 defined in `gnus-summary-show-article-charset-alist', or the charset
8061 input.
8062 If ARG (the prefix) is non-nil and not a number, show the raw article
8063 without any article massaging functions being run.  Normally, the key strokes 
8064 are `C-u g'."
8065   (interactive "P")
8066   (cond
8067    ((numberp arg)
8068     (gnus-summary-show-article t)
8069     (let ((gnus-newsgroup-charset
8070            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8071                (mm-read-coding-system
8072                 "View as charset: " ;; actually it is coding system.
8073                 (save-excursion
8074                   (set-buffer gnus-article-buffer)
8075                   (mm-detect-coding-region (point) (point-max))))))
8076           (gnus-newsgroup-ignored-charsets 'gnus-all))
8077       (gnus-summary-select-article nil 'force)
8078       (let ((deps gnus-newsgroup-dependencies)
8079             head header lines)
8080         (save-excursion
8081           (set-buffer gnus-original-article-buffer)
8082           (save-restriction
8083             (message-narrow-to-head)
8084             (setq head (buffer-string))
8085             (goto-char (point-min))
8086             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8087               (goto-char (point-max))
8088               (widen)
8089               (setq lines (1- (count-lines (point) (point-max))))))
8090           (with-temp-buffer
8091             (insert (format "211 %d Article retrieved.\n"
8092                             (cdr gnus-article-current)))
8093             (insert head)
8094             (if lines (insert (format "Lines: %d\n" lines)))
8095             (insert ".\n")
8096             (let ((nntp-server-buffer (current-buffer)))
8097               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8098         (gnus-data-set-header
8099          (gnus-data-find (cdr gnus-article-current))
8100          header)
8101         (gnus-summary-update-article-line
8102          (cdr gnus-article-current) header)
8103         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8104           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8105    ((not arg)
8106     ;; Select the article the normal way.
8107     (gnus-summary-select-article nil 'force))
8108    (t
8109     ;; We have to require this here to make sure that the following
8110     ;; dynamic binding isn't shadowed by autoloading.
8111     (require 'gnus-async)
8112     (require 'gnus-art)
8113     ;; Bind the article treatment functions to nil.
8114     (let ((gnus-have-all-headers t)
8115           gnus-article-prepare-hook
8116           gnus-article-decode-hook
8117           gnus-display-mime-function
8118           gnus-break-pages)
8119       ;; Destroy any MIME parts.
8120       (when (gnus-buffer-live-p gnus-article-buffer)
8121         (save-excursion
8122           (set-buffer gnus-article-buffer)
8123           (mm-destroy-parts gnus-article-mime-handles)
8124           ;; Set it to nil for safety reason.
8125           (setq gnus-article-mime-handle-alist nil)
8126           (setq gnus-article-mime-handles nil)))
8127       (gnus-summary-select-article nil 'force))))
8128   (gnus-summary-goto-subject gnus-current-article)
8129   (gnus-summary-position-point))
8130
8131 (defun gnus-summary-show-raw-article ()
8132   "Show the raw article without any article massaging functions being run."
8133   (interactive)
8134   (gnus-summary-show-article t))
8135
8136 (defun gnus-summary-verbose-headers (&optional arg)
8137   "Toggle permanent full header display.
8138 If ARG is a positive number, turn header display on.
8139 If ARG is a negative number, turn header display off."
8140   (interactive "P")
8141   (setq gnus-show-all-headers
8142         (cond ((or (not (numberp arg))
8143                    (zerop arg))
8144                (not gnus-show-all-headers))
8145               ((natnump arg)
8146                t)))
8147   (gnus-summary-show-article))
8148
8149 (defun gnus-summary-toggle-header (&optional arg)
8150   "Show the headers if they are hidden, or hide them if they are shown.
8151 If ARG is a positive number, show the entire header.
8152 If ARG is a negative number, hide the unwanted header lines."
8153   (interactive "P")
8154   (save-excursion
8155     (set-buffer gnus-article-buffer)
8156     (save-restriction
8157       (let* ((buffer-read-only nil)
8158              (inhibit-point-motion-hooks t)
8159              hidden e)
8160         (setq hidden
8161               (if (numberp arg)
8162                   (>= arg 0)
8163                 (save-restriction
8164                   (article-narrow-to-head)
8165                   (gnus-article-hidden-text-p 'headers))))
8166         (goto-char (point-min))
8167         (when (search-forward "\n\n" nil t)
8168           (delete-region (point-min) (1- (point))))
8169         (goto-char (point-min))
8170         (save-excursion
8171           (set-buffer gnus-original-article-buffer)
8172           (goto-char (point-min))
8173           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8174         (insert-buffer-substring gnus-original-article-buffer 1 e)
8175         (save-restriction
8176           (narrow-to-region (point-min) (point))
8177           (article-decode-encoded-words)
8178           (if  hidden
8179               (let ((gnus-treat-hide-headers nil)
8180                     (gnus-treat-hide-boring-headers nil))
8181                 (gnus-delete-wash-type 'headers)
8182                 (gnus-treat-article 'head))
8183             (gnus-treat-article 'head)))
8184         (gnus-set-mode-line 'article)))))
8185
8186 (defun gnus-summary-show-all-headers ()
8187   "Make all header lines visible."
8188   (interactive)
8189   (gnus-summary-toggle-header 1))
8190
8191 (defun gnus-summary-caesar-message (&optional arg)
8192   "Caesar rotate the current article by 13.
8193 The numerical prefix specifies how many places to rotate each letter
8194 forward."
8195   (interactive "P")
8196   (gnus-summary-select-article)
8197   (let ((mail-header-separator ""))
8198     (gnus-eval-in-buffer-window gnus-article-buffer
8199       (save-restriction
8200         (widen)
8201         (let ((start (window-start))
8202               buffer-read-only)
8203           (message-caesar-buffer-body arg)
8204           (set-window-start (get-buffer-window (current-buffer)) start))))))
8205
8206 (defun gnus-summary-stop-page-breaking ()
8207   "Stop page breaking in the current article."
8208   (interactive)
8209   (gnus-summary-select-article)
8210   (gnus-eval-in-buffer-window gnus-article-buffer
8211     (widen)
8212     (when (gnus-visual-p 'page-marker)
8213       (let ((buffer-read-only nil))
8214         (gnus-remove-text-with-property 'gnus-prev)
8215         (gnus-remove-text-with-property 'gnus-next))
8216       (setq gnus-page-broken nil))))
8217
8218 (defun gnus-summary-move-article (&optional n to-newsgroup
8219                                             select-method action)
8220   "Move the current article to a different newsgroup.
8221 If N is a positive number, move the N next articles.
8222 If N is a negative number, move the N previous articles.
8223 If N is nil and any articles have been marked with the process mark,
8224 move those articles instead.
8225 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8226 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8227 re-spool using this method.
8228
8229 For this function to work, both the current newsgroup and the
8230 newsgroup that you want to move to have to support the `request-move'
8231 and `request-accept' functions.
8232
8233 ACTION can be either `move' (the default), `crosspost' or `copy'."
8234   (interactive "P")
8235   (unless action
8236     (setq action 'move))
8237   ;; Check whether the source group supports the required functions.
8238   (cond ((and (eq action 'move)
8239               (not (gnus-check-backend-function
8240                     'request-move-article gnus-newsgroup-name)))
8241          (error "The current group does not support article moving"))
8242         ((and (eq action 'crosspost)
8243               (not (gnus-check-backend-function
8244                     'request-replace-article gnus-newsgroup-name)))
8245          (error "The current group does not support article editing")))
8246   (let ((articles (gnus-summary-work-articles n))
8247         (prefix (if (gnus-check-backend-function
8248                      'request-move-article gnus-newsgroup-name)
8249                     (gnus-group-real-prefix gnus-newsgroup-name)
8250                   ""))
8251         (names '((move "Move" "Moving")
8252                  (copy "Copy" "Copying")
8253                  (crosspost "Crosspost" "Crossposting")))
8254         (copy-buf (save-excursion
8255                     (nnheader-set-temp-buffer " *copy article*")))
8256         art-group to-method new-xref article to-groups)
8257     (unless (assq action names)
8258       (error "Unknown action %s" action))
8259     ;; We have to select an article to give
8260     ;; `gnus-read-move-group-name' an opportunity to suggest an
8261     ;; appropriate default.
8262     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8263       (let ((gnus-display-mime-function nil)
8264             (gnus-article-prepare-hook nil))
8265         (gnus-summary-select-article nil nil nil (car articles))))
8266     ;; Read the newsgroup name.
8267     (when (and (not to-newsgroup)
8268                (not select-method))
8269       (setq to-newsgroup
8270             (gnus-read-move-group-name
8271              (cadr (assq action names))
8272              (symbol-value (intern (format "gnus-current-%s-group" action)))
8273              articles prefix))
8274       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8275     (setq to-method (or select-method
8276                         (gnus-server-to-method
8277                          (gnus-group-method to-newsgroup))))
8278     ;; Check the method we are to move this article to...
8279     (unless (gnus-check-backend-function
8280              'request-accept-article (car to-method))
8281       (error "%s does not support article copying" (car to-method)))
8282     (unless (gnus-check-server to-method)
8283       (error "Can't open server %s" (car to-method)))
8284     (gnus-message 6 "%s to %s: %s..."
8285                   (caddr (assq action names))
8286                   (or (car select-method) to-newsgroup) articles)
8287     (while articles
8288       (setq article (pop articles))
8289       (setq
8290        art-group
8291        (cond
8292         ;; Move the article.
8293         ((eq action 'move)
8294          ;; Remove this article from future suppression.
8295          (gnus-dup-unsuppress-article article)
8296          (gnus-request-move-article
8297           article                       ; Article to move
8298           gnus-newsgroup-name           ; From newsgroup
8299           (nth 1 (gnus-find-method-for-group
8300                   gnus-newsgroup-name)) ; Server
8301           (list 'gnus-request-accept-article
8302                 to-newsgroup (list 'quote select-method)
8303                 (not articles) t)       ; Accept form
8304           (not articles)))              ; Only save nov last time
8305         ;; Copy the article.
8306         ((eq action 'copy)
8307          (save-excursion
8308            (set-buffer copy-buf)
8309            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8310              (gnus-request-accept-article
8311               to-newsgroup select-method (not articles) t))))
8312         ;; Crosspost the article.
8313         ((eq action 'crosspost)
8314          (let ((xref (message-tokenize-header
8315                       (mail-header-xref (gnus-summary-article-header article))
8316                       " ")))
8317            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8318                                   ":" (number-to-string article)))
8319            (unless xref
8320              (setq xref (list (system-name))))
8321            (setq new-xref
8322                  (concat
8323                   (mapconcat 'identity
8324                              (delete "Xref:" (delete new-xref xref))
8325                              " ")
8326                   " " new-xref))
8327            (save-excursion
8328              (set-buffer copy-buf)
8329              ;; First put the article in the destination group.
8330              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8331              (when (consp (setq art-group
8332                                 (gnus-request-accept-article
8333                                  to-newsgroup select-method (not articles))))
8334                (setq new-xref (concat new-xref " " (car art-group)
8335                                       ":"
8336                                       (number-to-string (cdr art-group))))
8337                ;; Now we have the new Xrefs header, so we insert
8338                ;; it and replace the new article.
8339                (nnheader-replace-header "Xref" new-xref)
8340                (gnus-request-replace-article
8341                 (cdr art-group) to-newsgroup (current-buffer))
8342                art-group))))))
8343       (cond
8344        ((not art-group)
8345         (gnus-message 1 "Couldn't %s article %s: %s"
8346                       (cadr (assq action names)) article
8347                       (nnheader-get-report (car to-method))))
8348        ((eq art-group 'junk)
8349         (when (eq action 'move)
8350           (gnus-summary-mark-article article gnus-canceled-mark)
8351           (gnus-message 4 "Deleted article %s" article)))
8352        (t
8353         (let* ((pto-group (gnus-group-prefixed-name
8354                            (car art-group) to-method))
8355                (entry
8356                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8357                (info (nth 2 entry))
8358                (to-group (gnus-info-group info))
8359                to-marks)
8360           ;; Update the group that has been moved to.
8361           (when (and info
8362                      (memq action '(move copy)))
8363             (unless (member to-group to-groups)
8364               (push to-group to-groups))
8365
8366             (unless (memq article gnus-newsgroup-unreads)
8367               (push 'read to-marks)
8368               (gnus-info-set-read
8369                info (gnus-add-to-range (gnus-info-read info)
8370                                        (list (cdr art-group)))))
8371
8372             ;; See whether the article is to be put in the cache.
8373             (let ((marks gnus-article-mark-lists)
8374                   (to-article (cdr art-group)))
8375
8376               ;; Enter the article into the cache in the new group,
8377               ;; if that is required.
8378               (when gnus-use-cache
8379                 (gnus-cache-possibly-enter-article
8380                  to-group to-article
8381                  (memq article gnus-newsgroup-marked)
8382                  (memq article gnus-newsgroup-dormant)
8383                  (memq article gnus-newsgroup-unreads)))
8384
8385               (when gnus-preserve-marks
8386                 ;; Copy any marks over to the new group.
8387                 (when (and (equal to-group gnus-newsgroup-name)
8388                            (not (memq article gnus-newsgroup-unreads)))
8389                   ;; Mark this article as read in this group.
8390                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8391                   (setcdr (gnus-active to-group) to-article)
8392                   (setcdr gnus-newsgroup-active to-article))
8393
8394                 (while marks
8395                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8396                     (when (memq article (symbol-value
8397                                          (intern (format "gnus-newsgroup-%s"
8398                                                          (caar marks)))))
8399                       (push (cdar marks) to-marks)
8400                       ;; If the other group is the same as this group,
8401                       ;; then we have to add the mark to the list.
8402                       (when (equal to-group gnus-newsgroup-name)
8403                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8404                              (cons to-article
8405                                    (symbol-value
8406                                     (intern (format "gnus-newsgroup-%s"
8407                                                     (caar marks)))))))
8408                       ;; Copy the marks to other group.
8409                       (gnus-add-marked-articles
8410                        to-group (cdar marks) (list to-article) info)))
8411                   (setq marks (cdr marks)))
8412
8413                 (gnus-request-set-mark to-group (list (list (list to-article)
8414                                                             'add
8415                                                             to-marks))))
8416
8417               (gnus-dribble-enter
8418                (concat "(gnus-group-set-info '"
8419                        (gnus-prin1-to-string (gnus-get-info to-group))
8420                        ")"))))
8421
8422           ;; Update the Xref header in this article to point to
8423           ;; the new crossposted article we have just created.
8424           (when (eq action 'crosspost)
8425             (save-excursion
8426               (set-buffer copy-buf)
8427               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8428               (nnheader-replace-header "Xref" new-xref)
8429               (gnus-request-replace-article
8430                article gnus-newsgroup-name (current-buffer)))))
8431
8432         ;;;!!!Why is this necessary?
8433         (set-buffer gnus-summary-buffer)
8434
8435         (gnus-summary-goto-subject article)
8436         (when (eq action 'move)
8437           (gnus-summary-mark-article article gnus-canceled-mark))))
8438       (gnus-summary-remove-process-mark article))
8439     ;; Re-activate all groups that have been moved to.
8440     (save-excursion
8441       (set-buffer gnus-group-buffer)
8442       (let ((gnus-group-marked to-groups))
8443         (gnus-group-get-new-news-this-group nil t)))
8444
8445     (gnus-kill-buffer copy-buf)
8446     (gnus-summary-position-point)
8447     (gnus-set-mode-line 'summary)))
8448
8449 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8450   "Move the current article to a different newsgroup.
8451 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8452 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8453 re-spool using this method."
8454   (interactive "P")
8455   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8456
8457 (defun gnus-summary-crosspost-article (&optional n)
8458   "Crosspost the current article to some other group."
8459   (interactive "P")
8460   (gnus-summary-move-article n nil nil 'crosspost))
8461
8462 (defcustom gnus-summary-respool-default-method nil
8463   "Default method type for respooling an article.
8464 If nil, use to the current newsgroup method."
8465   :type 'symbol
8466   :group 'gnus-summary-mail)
8467
8468 (defun gnus-summary-respool-article (&optional n method)
8469   "Respool the current article.
8470 The article will be squeezed through the mail spooling process again,
8471 which means that it will be put in some mail newsgroup or other
8472 depending on `nnmail-split-methods'.
8473 If N is a positive number, respool the N next articles.
8474 If N is a negative number, respool the N previous articles.
8475 If N is nil and any articles have been marked with the process mark,
8476 respool those articles instead.
8477
8478 Respooling can be done both from mail groups and \"real\" newsgroups.
8479 In the former case, the articles in question will be moved from the
8480 current group into whatever groups they are destined to.  In the
8481 latter case, they will be copied into the relevant groups."
8482   (interactive
8483    (list current-prefix-arg
8484          (let* ((methods (gnus-methods-using 'respool))
8485                 (methname
8486                  (symbol-name (or gnus-summary-respool-default-method
8487                                   (car (gnus-find-method-for-group
8488                                         gnus-newsgroup-name)))))
8489                 (method
8490                  (gnus-completing-read-with-default
8491                   methname "What backend do you want to use when respooling?"
8492                   methods nil t nil 'gnus-mail-method-history))
8493                 ms)
8494            (cond
8495             ((zerop (length (setq ms (gnus-servers-using-backend
8496                                       (intern method)))))
8497              (list (intern method) ""))
8498             ((= 1 (length ms))
8499              (car ms))
8500             (t
8501              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8502                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8503                            ms-alist))))))))
8504   (unless method
8505     (error "No method given for respooling"))
8506   (if (assoc (symbol-name
8507               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8508              (gnus-methods-using 'respool))
8509       (gnus-summary-move-article n nil method)
8510     (gnus-summary-copy-article n nil method)))
8511
8512 (defun gnus-summary-import-article (file &optional edit)
8513   "Import an arbitrary file into a mail newsgroup."
8514   (interactive "fImport file: \nP")
8515   (let ((group gnus-newsgroup-name)
8516         (now (current-time))
8517         atts lines group-art)
8518     (unless (gnus-check-backend-function 'request-accept-article group)
8519       (error "%s does not support article importing" group))
8520     (or (file-readable-p file)
8521         (not (file-regular-p file))
8522         (error "Can't read %s" file))
8523     (save-excursion
8524       (set-buffer (gnus-get-buffer-create " *import file*"))
8525       (erase-buffer)
8526       (nnheader-insert-file-contents file)
8527       (goto-char (point-min))
8528       (if (nnheader-article-p)
8529           (save-restriction
8530             (goto-char (point-min))
8531             (search-forward "\n\n" nil t)
8532             (narrow-to-region (point-min) (1- (point)))
8533             (goto-char (point-min))
8534             (unless (re-search-forward "^date:" nil t)
8535               (goto-char (point-max))
8536               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8537        ;; This doesn't look like an article, so we fudge some headers.
8538         (setq atts (file-attributes file)
8539               lines (count-lines (point-min) (point-max)))
8540         (insert "From: " (read-string "From: ") "\n"
8541                 "Subject: " (read-string "Subject: ") "\n"
8542                 "Date: " (message-make-date (nth 5 atts)) "\n"
8543                 "Message-ID: " (message-make-message-id) "\n"
8544                 "Lines: " (int-to-string lines) "\n"
8545                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8546       (setq group-art (gnus-request-accept-article group nil t))
8547       (kill-buffer (current-buffer)))
8548     (setq gnus-newsgroup-active (gnus-activate-group group))
8549     (forward-line 1)
8550     (gnus-summary-goto-article (cdr group-art) nil t)
8551     (when edit
8552       (gnus-summary-edit-article))))
8553
8554 (defun gnus-summary-create-article ()
8555   "Create an article in a mail newsgroup."
8556   (interactive)
8557   (let ((group gnus-newsgroup-name)
8558         (now (current-time))
8559         group-art)
8560     (unless (gnus-check-backend-function 'request-accept-article group)
8561       (error "%s does not support article importing" group))
8562     (save-excursion
8563       (set-buffer (gnus-get-buffer-create " *import file*"))
8564       (erase-buffer)
8565       (goto-char (point-min))
8566       ;; This doesn't look like an article, so we fudge some headers.
8567       (insert "From: " (read-string "From: ") "\n"
8568               "Subject: " (read-string "Subject: ") "\n"
8569               "Date: " (message-make-date now) "\n"
8570               "Message-ID: " (message-make-message-id) "\n")
8571       (setq group-art (gnus-request-accept-article group nil t))
8572       (kill-buffer (current-buffer)))
8573     (setq gnus-newsgroup-active (gnus-activate-group group))
8574     (forward-line 1)
8575     (gnus-summary-goto-article (cdr group-art) nil t)
8576     (gnus-summary-edit-article)))
8577
8578 (defun gnus-summary-article-posted-p ()
8579   "Say whether the current (mail) article is available from news as well.
8580 This will be the case if the article has both been mailed and posted."
8581   (interactive)
8582   (let ((id (mail-header-references (gnus-summary-article-header)))
8583         (gnus-override-method (car (gnus-refer-article-methods))))
8584     (if (gnus-request-head id "")
8585         (gnus-message 2 "The current message was found on %s"
8586                       gnus-override-method)
8587       (gnus-message 2 "The current message couldn't be found on %s"
8588                     gnus-override-method)
8589       nil)))
8590
8591 (defun gnus-summary-expire-articles (&optional now)
8592   "Expire all articles that are marked as expirable in the current group."
8593   (interactive)
8594   (when (gnus-check-backend-function
8595          'request-expire-articles gnus-newsgroup-name)
8596     ;; This backend supports expiry.
8597     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8598            (expirable (if total
8599                           (progn
8600                             ;; We need to update the info for
8601                             ;; this group for `gnus-list-of-read-articles'
8602                             ;; to give us the right answer.
8603                             (gnus-run-hooks 'gnus-exit-group-hook)
8604                             (gnus-summary-update-info)
8605                             (gnus-list-of-read-articles gnus-newsgroup-name))
8606                         (setq gnus-newsgroup-expirable
8607                               (sort gnus-newsgroup-expirable '<))))
8608            (expiry-wait (if now 'immediate
8609                           (gnus-group-find-parameter
8610                            gnus-newsgroup-name 'expiry-wait)))
8611            (nnmail-expiry-target
8612             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8613                 nnmail-expiry-target))
8614            es)
8615       (when expirable
8616         ;; There are expirable articles in this group, so we run them
8617         ;; through the expiry process.
8618         (gnus-message 6 "Expiring articles...")
8619         (unless (gnus-check-group gnus-newsgroup-name)
8620           (error "Can't open server for %s" gnus-newsgroup-name))
8621         ;; The list of articles that weren't expired is returned.
8622         (save-excursion
8623           (if expiry-wait
8624               (let ((nnmail-expiry-wait-function nil)
8625                     (nnmail-expiry-wait expiry-wait))
8626                 (setq es (gnus-request-expire-articles
8627                           expirable gnus-newsgroup-name)))
8628             (setq es (gnus-request-expire-articles
8629                       expirable gnus-newsgroup-name)))
8630           (unless total
8631             (setq gnus-newsgroup-expirable es))
8632           ;; We go through the old list of expirable, and mark all
8633           ;; really expired articles as nonexistent.
8634           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8635             (let ((gnus-use-cache nil))
8636               (dolist (article expirable)
8637                 (when (and (not (memq article es))
8638                            (gnus-data-find article))
8639                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8640         (gnus-message 6 "Expiring articles...done")))))
8641
8642 (defun gnus-summary-expire-articles-now ()
8643   "Expunge all expirable articles in the current group.
8644 This means that *all* articles that are marked as expirable will be
8645 deleted forever, right now."
8646   (interactive)
8647   (or gnus-expert-user
8648       (gnus-yes-or-no-p
8649        "Are you really, really, really sure you want to delete all these messages? ")
8650       (error "Phew!"))
8651   (gnus-summary-expire-articles t))
8652
8653 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8654 (defun gnus-summary-delete-article (&optional n)
8655   "Delete the N next (mail) articles.
8656 This command actually deletes articles.  This is not a marking
8657 command.  The article will disappear forever from your life, never to
8658 return.
8659 If N is negative, delete backwards.
8660 If N is nil and articles have been marked with the process mark,
8661 delete these instead."
8662   (interactive "P")
8663   (unless (gnus-check-backend-function 'request-expire-articles
8664                                        gnus-newsgroup-name)
8665     (error "The current newsgroup does not support article deletion"))
8666   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8667     (error "Couldn't open server"))
8668   ;; Compute the list of articles to delete.
8669   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8670         not-deleted)
8671     (if (and gnus-novice-user
8672              (not (gnus-yes-or-no-p
8673                    (format "Do you really want to delete %s forever? "
8674                            (if (> (length articles) 1)
8675                                (format "these %s articles" (length articles))
8676                              "this article")))))
8677         ()
8678       ;; Delete the articles.
8679       (setq not-deleted (gnus-request-expire-articles
8680                          articles gnus-newsgroup-name 'force))
8681       (while articles
8682         (gnus-summary-remove-process-mark (car articles))
8683         ;; The backend might not have been able to delete the article
8684         ;; after all.
8685         (unless (memq (car articles) not-deleted)
8686           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8687         (setq articles (cdr articles)))
8688       (when not-deleted
8689         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8690     (gnus-summary-position-point)
8691     (gnus-set-mode-line 'summary)
8692     not-deleted))
8693
8694 (defun gnus-summary-edit-article (&optional arg)
8695   "Edit the current article.
8696 This will have permanent effect only in mail groups.
8697 If ARG is nil, edit the decoded articles.
8698 If ARG is 1, edit the raw articles.
8699 If ARG is 2, edit the raw articles even in read-only groups.
8700 If ARG is 3, edit the articles with the current handles.
8701 Otherwise, allow editing of articles even in read-only
8702 groups."
8703   (interactive "P")
8704   (let (force raw current-handles)
8705     (cond
8706      ((null arg))
8707      ((eq arg 1)
8708       (setq raw t))
8709      ((eq arg 2)
8710       (setq raw t
8711             force t))
8712      ((eq arg 3)
8713       (setq current-handles
8714             (and (gnus-buffer-live-p gnus-article-buffer)
8715                  (with-current-buffer gnus-article-buffer
8716                    (prog1
8717                        gnus-article-mime-handles
8718                      (setq gnus-article-mime-handles nil))))))
8719      (t
8720       (setq force t)))
8721     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8722       (error "Can't edit the raw article in group nndraft:drafts"))
8723     (save-excursion
8724       (set-buffer gnus-summary-buffer)
8725       (let ((mail-parse-charset gnus-newsgroup-charset)
8726             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8727         (gnus-set-global-variables)
8728         (when (and (not force)
8729                    (gnus-group-read-only-p))
8730           (error "The current newsgroup does not support article editing"))
8731         (gnus-summary-show-article t)
8732         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8733           (with-current-buffer gnus-article-buffer
8734             (mm-enable-multibyte)))
8735         (if (equal gnus-newsgroup-name "nndraft:drafts")
8736             (setq raw t))
8737         (gnus-article-edit-article
8738          (if raw 'ignore
8739            `(lambda ()
8740               (let ((mbl mml-buffer-list))
8741                 (setq mml-buffer-list nil)
8742                 (mime-to-mml ,'current-handles)
8743                 (let ((mbl1 mml-buffer-list))
8744                   (setq mml-buffer-list mbl)
8745                   (set (make-local-variable 'mml-buffer-list) mbl1))
8746                 (make-local-hook 'kill-buffer-hook)
8747                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8748          `(lambda (no-highlight)
8749             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8750                   (message-options message-options)
8751                   (message-options-set-recipient)
8752                   (mail-parse-ignored-charsets
8753                    ',gnus-newsgroup-ignored-charsets))
8754               ,(if (not raw) '(progn
8755                                 (mml-to-mime)
8756                                 (mml-destroy-buffers)
8757                                 (remove-hook 'kill-buffer-hook
8758                                              'mml-destroy-buffers t)
8759                                 (kill-local-variable 'mml-buffer-list)))
8760               (gnus-summary-edit-article-done
8761                ,(or (mail-header-references gnus-current-headers) "")
8762                ,(gnus-group-read-only-p)
8763                ,gnus-summary-buffer no-highlight))))))))
8764
8765 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8766
8767 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8768                                                  no-highlight)
8769   "Make edits to the current article permanent."
8770   (interactive)
8771   (save-excursion
8772    ;; The buffer restriction contains the entire article if it exists.
8773     (when (article-goto-body)
8774       (let ((lines (count-lines (point) (point-max)))
8775             (length (- (point-max) (point)))
8776             (case-fold-search t)
8777             (body (copy-marker (point))))
8778         (goto-char (point-min))
8779         (when (re-search-forward "^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
8784                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8785           (delete-region (match-beginning 1) (match-end 1))
8786           (insert (number-to-string length)))
8787         (goto-char (point-min))
8788         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8789           (delete-region (match-beginning 1) (match-end 1))
8790           (insert (number-to-string lines))))))
8791   ;; Replace the article.
8792   (let ((buf (current-buffer)))
8793     (with-temp-buffer
8794       (insert-buffer-substring buf)
8795
8796       (if (and (not read-only)
8797                (not (gnus-request-replace-article
8798                      (cdr gnus-article-current) (car gnus-article-current)
8799                      (current-buffer) t)))
8800           (error "Couldn't replace article")
8801         ;; Update the summary buffer.
8802         (if (and references
8803                  (equal (message-tokenize-header references " ")
8804                         (message-tokenize-header
8805                          (or (message-fetch-field "references") "") " ")))
8806             ;; We only have to update this line.
8807             (save-excursion
8808               (save-restriction
8809                 (message-narrow-to-head)
8810                 (let ((head (buffer-string))
8811                       header)
8812                   (with-temp-buffer
8813                     (insert (format "211 %d Article retrieved.\n"
8814                                     (cdr gnus-article-current)))
8815                     (insert head)
8816                     (insert ".\n")
8817                     (let ((nntp-server-buffer (current-buffer)))
8818                       (setq header (car (gnus-get-newsgroup-headers
8819                                          (save-excursion
8820                                            (set-buffer gnus-summary-buffer)
8821                                            gnus-newsgroup-dependencies)
8822                                          t))))
8823                     (save-excursion
8824                       (set-buffer gnus-summary-buffer)
8825                       (gnus-data-set-header
8826                        (gnus-data-find (cdr gnus-article-current))
8827                        header)
8828                       (gnus-summary-update-article-line
8829                        (cdr gnus-article-current) header)
8830                       (if (gnus-summary-goto-subject
8831                            (cdr gnus-article-current) nil t)
8832                           (gnus-summary-update-secondary-mark
8833                            (cdr gnus-article-current))))))))
8834           ;; Update threads.
8835           (set-buffer (or buffer gnus-summary-buffer))
8836           (gnus-summary-update-article (cdr gnus-article-current))
8837           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8838               (gnus-summary-update-secondary-mark
8839                (cdr gnus-article-current))))
8840         ;; Prettify the article buffer again.
8841         (unless no-highlight
8842           (save-excursion
8843             (set-buffer gnus-article-buffer)
8844             ;;;!!! Fix this -- article should be rehighlighted.
8845             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8846             (set-buffer gnus-original-article-buffer)
8847             (gnus-request-article
8848              (cdr gnus-article-current)
8849              (car gnus-article-current) (current-buffer))))
8850         ;; Prettify the summary buffer line.
8851         (when (gnus-visual-p 'summary-highlight 'highlight)
8852           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8853
8854 (defun gnus-summary-edit-wash (key)
8855   "Perform editing command KEY in the article buffer."
8856   (interactive
8857    (list
8858     (progn
8859       (message "%s" (concat (this-command-keys) "- "))
8860       (read-char))))
8861   (message "")
8862   (gnus-summary-edit-article)
8863   (execute-kbd-macro (concat (this-command-keys) key))
8864   (gnus-article-edit-done))
8865
8866 ;;; Respooling
8867
8868 (defun gnus-summary-respool-query (&optional silent trace)
8869   "Query where the respool algorithm would put this article."
8870   (interactive)
8871   (let (gnus-mark-article-hook)
8872     (gnus-summary-select-article)
8873     (save-excursion
8874       (set-buffer gnus-original-article-buffer)
8875       (save-restriction
8876         (message-narrow-to-head)
8877         (let ((groups (nnmail-article-group 'identity trace)))
8878           (unless silent
8879             (if groups
8880                 (message "This message would go to %s"
8881                          (mapconcat 'car groups ", "))
8882               (message "This message would go to no groups"))
8883             groups))))))
8884
8885 (defun gnus-summary-respool-trace ()
8886   "Trace where the respool algorithm would put this article.
8887 Display a buffer showing all fancy splitting patterns which matched."
8888   (interactive)
8889   (gnus-summary-respool-query nil t))
8890
8891 ;; Summary marking commands.
8892
8893 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8894   "Mark articles which has the same subject as read, and then select the next.
8895 If UNMARK is positive, remove any kind of mark.
8896 If UNMARK is negative, tick articles."
8897   (interactive "P")
8898   (when unmark
8899     (setq unmark (prefix-numeric-value unmark)))
8900   (let ((count
8901          (gnus-summary-mark-same-subject
8902           (gnus-summary-article-subject) unmark)))
8903     ;; Select next unread article.  If auto-select-same mode, should
8904     ;; select the first unread article.
8905     (gnus-summary-next-article t (and gnus-auto-select-same
8906                                       (gnus-summary-article-subject)))
8907     (gnus-message 7 "%d article%s marked as %s"
8908                   count (if (= count 1) " is" "s are")
8909                   (if unmark "unread" "read"))))
8910
8911 (defun gnus-summary-kill-same-subject (&optional unmark)
8912   "Mark articles which has the same subject as read.
8913 If UNMARK is positive, remove any kind of mark.
8914 If UNMARK is negative, tick articles."
8915   (interactive "P")
8916   (when unmark
8917     (setq unmark (prefix-numeric-value unmark)))
8918   (let ((count
8919          (gnus-summary-mark-same-subject
8920           (gnus-summary-article-subject) unmark)))
8921     ;; If marked as read, go to next unread subject.
8922     (when (null unmark)
8923       ;; Go to next unread subject.
8924       (gnus-summary-next-subject 1 t))
8925     (gnus-message 7 "%d articles are marked as %s"
8926                   count (if unmark "unread" "read"))))
8927
8928 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8929   "Mark articles with same SUBJECT as read, and return marked number.
8930 If optional argument UNMARK is positive, remove any kinds of marks.
8931 If optional argument UNMARK is negative, mark articles as unread instead."
8932   (let ((count 1))
8933     (save-excursion
8934       (cond
8935        ((null unmark)                   ; Mark as read.
8936         (while (and
8937                 (progn
8938                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8939                   (gnus-summary-show-thread) t)
8940                 (gnus-summary-find-subject subject))
8941           (setq count (1+ count))))
8942        ((> unmark 0)                    ; Tick.
8943         (while (and
8944                 (progn
8945                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8946                   (gnus-summary-show-thread) t)
8947                 (gnus-summary-find-subject subject))
8948           (setq count (1+ count))))
8949        (t                               ; Mark as unread.
8950         (while (and
8951                 (progn
8952                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8953                   (gnus-summary-show-thread) t)
8954                 (gnus-summary-find-subject subject))
8955           (setq count (1+ count)))))
8956       (gnus-set-mode-line 'summary)
8957       ;; Return the number of marked articles.
8958       count)))
8959
8960 (defun gnus-summary-mark-as-processable (n &optional unmark)
8961   "Set the process mark on the next N articles.
8962 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8963 the process mark instead.  The difference between N and the actual
8964 number of articles marked is returned."
8965   (interactive "P")
8966   (if (and (null n) (gnus-region-active-p))
8967       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8968     (setq n (prefix-numeric-value n))
8969     (let ((backward (< n 0))
8970           (n (abs n)))
8971       (while (and
8972               (> n 0)
8973               (if unmark
8974                   (gnus-summary-remove-process-mark
8975                    (gnus-summary-article-number))
8976                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8977               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8978         (setq n (1- n)))
8979       (when (/= 0 n)
8980         (gnus-message 7 "No more articles"))
8981       (gnus-summary-recenter)
8982       (gnus-summary-position-point)
8983       n)))
8984
8985 (defun gnus-summary-unmark-as-processable (n)
8986   "Remove the process mark from the next N articles.
8987 If N is negative, unmark backward instead.  The difference between N and
8988 the actual number of articles unmarked is returned."
8989   (interactive "P")
8990   (gnus-summary-mark-as-processable n t))
8991
8992 (defun gnus-summary-unmark-all-processable ()
8993   "Remove the process mark from all articles."
8994   (interactive)
8995   (save-excursion
8996     (while gnus-newsgroup-processable
8997       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8998   (gnus-summary-position-point))
8999
9000 (defun gnus-summary-add-mark (article type)
9001   "Mark ARTICLE with a mark of TYPE."
9002   (let ((vtype (car (assq type gnus-article-mark-lists)))
9003         var)
9004     (if (not vtype)
9005         (error "No such mark type: %s" type)
9006       (setq var (intern (format "gnus-newsgroup-%s" type)))
9007       (set var (cons article (symbol-value var)))
9008       (if (memq type '(processable cached replied forwarded recent saved))
9009           (gnus-summary-update-secondary-mark article)
9010         ;;; !!! This is bogus.  We should find out what primary
9011         ;;; !!! mark we want to set.
9012         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9013
9014 (defun gnus-summary-mark-as-expirable (n)
9015   "Mark N articles forward as expirable.
9016 If N is negative, mark backward instead.  The difference between N and
9017 the actual number of articles marked is returned."
9018   (interactive "p")
9019   (gnus-summary-mark-forward n gnus-expirable-mark))
9020
9021 (defun gnus-summary-mark-article-as-replied (article)
9022   "Mark ARTICLE as replied to and update the summary line.
9023 ARTICLE can also be a list of articles."
9024   (interactive (list (gnus-summary-article-number)))
9025   (let ((articles (if (listp article) article (list article))))
9026     (dolist (article articles)
9027       (push article gnus-newsgroup-replied)
9028       (let ((buffer-read-only nil))
9029         (when (gnus-summary-goto-subject article nil t)
9030           (gnus-summary-update-secondary-mark article))))))
9031
9032 (defun gnus-summary-mark-article-as-forwarded (article)
9033   "Mark ARTICLE as forwarded and update the summary line.
9034 ARTICLE can also be a list of articles."
9035   (let ((articles (if (listp article) article (list article))))
9036     (dolist (article articles)
9037       (push article gnus-newsgroup-forwarded)
9038       (let ((buffer-read-only nil))
9039         (when (gnus-summary-goto-subject article nil t)
9040           (gnus-summary-update-secondary-mark article))))))
9041
9042 (defun gnus-summary-set-bookmark (article)
9043   "Set a bookmark in current article."
9044   (interactive (list (gnus-summary-article-number)))
9045   (when (or (not (get-buffer gnus-article-buffer))
9046             (not gnus-current-article)
9047             (not gnus-article-current)
9048             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9049     (error "No current article selected"))
9050   ;; Remove old bookmark, if one exists.
9051   (let ((old (assq article gnus-newsgroup-bookmarks)))
9052     (when old
9053       (setq gnus-newsgroup-bookmarks
9054             (delq old gnus-newsgroup-bookmarks))))
9055   ;; Set the new bookmark, which is on the form
9056   ;; (article-number . line-number-in-body).
9057   (push
9058    (cons article
9059          (save-excursion
9060            (set-buffer gnus-article-buffer)
9061            (count-lines
9062             (min (point)
9063                  (save-excursion
9064                    (goto-char (point-min))
9065                    (search-forward "\n\n" nil t)
9066                    (point)))
9067             (point))))
9068    gnus-newsgroup-bookmarks)
9069   (gnus-message 6 "A bookmark has been added to the current article."))
9070
9071 (defun gnus-summary-remove-bookmark (article)
9072   "Remove the bookmark from the current article."
9073   (interactive (list (gnus-summary-article-number)))
9074   ;; Remove old bookmark, if one exists.
9075   (let ((old (assq article gnus-newsgroup-bookmarks)))
9076     (if old
9077         (progn
9078           (setq gnus-newsgroup-bookmarks
9079                 (delq old gnus-newsgroup-bookmarks))
9080           (gnus-message 6 "Removed bookmark."))
9081       (gnus-message 6 "No bookmark in current article."))))
9082
9083 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9084 (defun gnus-summary-mark-as-dormant (n)
9085   "Mark N articles forward as dormant.
9086 If N is negative, mark backward instead.  The difference between N and
9087 the actual number of articles marked is returned."
9088   (interactive "p")
9089   (gnus-summary-mark-forward n gnus-dormant-mark))
9090
9091 (defun gnus-summary-set-process-mark (article)
9092   "Set the process mark on ARTICLE and update the summary line."
9093   (setq gnus-newsgroup-processable
9094         (cons article
9095               (delq article gnus-newsgroup-processable)))
9096   (when (gnus-summary-goto-subject article)
9097     (gnus-summary-show-thread)
9098     (gnus-summary-goto-subject article)
9099     (gnus-summary-update-secondary-mark article)))
9100
9101 (defun gnus-summary-remove-process-mark (article)
9102   "Remove the process mark from ARTICLE and update the summary line."
9103   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9104   (when (gnus-summary-goto-subject article)
9105     (gnus-summary-show-thread)
9106     (gnus-summary-goto-subject article)
9107     (gnus-summary-update-secondary-mark article)))
9108
9109 (defun gnus-summary-set-saved-mark (article)
9110   "Set the process mark on ARTICLE and update the summary line."
9111   (push article gnus-newsgroup-saved)
9112   (when (gnus-summary-goto-subject article)
9113     (gnus-summary-update-secondary-mark article)))
9114
9115 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9116   "Mark N articles as read forwards.
9117 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9118 The difference between N and the actual number of articles marked is
9119 returned.
9120 Iff NO-EXPIRE, auto-expiry will be inhibited."
9121   (interactive "p")
9122   (gnus-summary-show-thread)
9123   (let ((backward (< n 0))
9124         (gnus-summary-goto-unread
9125          (and gnus-summary-goto-unread
9126               (not (eq gnus-summary-goto-unread 'never))
9127               (not (memq mark (list gnus-unread-mark
9128                                     gnus-ticked-mark gnus-dormant-mark)))))
9129         (n (abs n))
9130         (mark (or mark gnus-del-mark)))
9131     (while (and (> n 0)
9132                 (gnus-summary-mark-article nil mark no-expire)
9133                 (zerop (gnus-summary-next-subject
9134                         (if backward -1 1)
9135                         (and gnus-summary-goto-unread
9136                              (not (eq gnus-summary-goto-unread 'never)))
9137                         t)))
9138       (setq n (1- n)))
9139     (when (/= 0 n)
9140       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9141     (gnus-summary-recenter)
9142     (gnus-summary-position-point)
9143     (gnus-set-mode-line 'summary)
9144     n))
9145
9146 (defun gnus-summary-mark-article-as-read (mark)
9147   "Mark the current article quickly as read with MARK."
9148   (let ((article (gnus-summary-article-number)))
9149     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9150     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9151     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9152     (push (cons article mark) gnus-newsgroup-reads)
9153     ;; Possibly remove from cache, if that is used.
9154     (when gnus-use-cache
9155       (gnus-cache-enter-remove-article article))
9156     ;; Allow the backend to change the mark.
9157     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9158     ;; Check for auto-expiry.
9159     (when (and gnus-newsgroup-auto-expire
9160                (memq mark gnus-auto-expirable-marks))
9161       (setq mark gnus-expirable-mark)
9162       ;; Let the backend know about the mark change.
9163       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9164       (push article gnus-newsgroup-expirable))
9165     ;; Set the mark in the buffer.
9166     (gnus-summary-update-mark mark 'unread)
9167     t))
9168
9169 (defun gnus-summary-mark-article-as-unread (mark)
9170   "Mark the current article quickly as unread with MARK."
9171   (let* ((article (gnus-summary-article-number))
9172          (old-mark (gnus-summary-article-mark article)))
9173     ;; Allow the backend to change the mark.
9174     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9175     (if (eq mark old-mark)
9176         t
9177       (if (<= article 0)
9178           (progn
9179             (gnus-error 1 "Can't mark negative article numbers")
9180             nil)
9181         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9182         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9183         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9184         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9185         (cond ((= mark gnus-ticked-mark)
9186                (push article gnus-newsgroup-marked))
9187               ((= mark gnus-dormant-mark)
9188                (push article gnus-newsgroup-dormant))
9189               (t
9190                (push article gnus-newsgroup-unreads)))
9191         (gnus-pull article gnus-newsgroup-reads)
9192
9193         ;; See whether the article is to be put in the cache.
9194         (and gnus-use-cache
9195              (vectorp (gnus-summary-article-header article))
9196              (save-excursion
9197                (gnus-cache-possibly-enter-article
9198                 gnus-newsgroup-name article
9199                 (= mark gnus-ticked-mark)
9200                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9201
9202         ;; Fix the mark.
9203         (gnus-summary-update-mark mark 'unread)
9204         t))))
9205
9206 (defun gnus-summary-mark-article (&optional article mark no-expire)
9207   "Mark ARTICLE with MARK.  MARK can be any character.
9208 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9209 `??' (dormant) and `?E' (expirable).
9210 If MARK is nil, then the default character `?r' is used.
9211 If ARTICLE is nil, then the article on the current line will be
9212 marked.
9213 Iff NO-EXPIRE, auto-expiry will be inhibited."
9214   ;; The mark might be a string.
9215   (when (stringp mark)
9216     (setq mark (aref mark 0)))
9217   ;; If no mark is given, then we check auto-expiring.
9218   (when (null mark)
9219     (setq mark gnus-del-mark))
9220   (when (and (not no-expire)
9221              gnus-newsgroup-auto-expire
9222              (memq mark gnus-auto-expirable-marks))
9223     (setq mark gnus-expirable-mark))
9224   (let ((article (or article (gnus-summary-article-number)))
9225         (old-mark (gnus-summary-article-mark article)))
9226     ;; Allow the backend to change the mark.
9227     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9228     (if (eq mark old-mark)
9229         t
9230       (unless article
9231         (error "No article on current line"))
9232       (if (not (if (or (= mark gnus-unread-mark)
9233                        (= mark gnus-ticked-mark)
9234                        (= mark gnus-dormant-mark))
9235                    (gnus-mark-article-as-unread article mark)
9236                  (gnus-mark-article-as-read article mark)))
9237           t
9238         ;; See whether the article is to be put in the cache.
9239         (and gnus-use-cache
9240              (not (= mark gnus-canceled-mark))
9241              (vectorp (gnus-summary-article-header article))
9242              (save-excursion
9243                (gnus-cache-possibly-enter-article
9244                 gnus-newsgroup-name article
9245                 (= mark gnus-ticked-mark)
9246                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9247
9248         (when (gnus-summary-goto-subject article nil t)
9249           (let ((buffer-read-only nil))
9250             (gnus-summary-show-thread)
9251             ;; Fix the mark.
9252             (gnus-summary-update-mark mark 'unread)
9253             t))))))
9254
9255 (defun gnus-summary-update-secondary-mark (article)
9256   "Update the secondary (read, process, cache) mark."
9257   (gnus-summary-update-mark
9258    (cond ((memq article gnus-newsgroup-processable)
9259           gnus-process-mark)
9260          ((memq article gnus-newsgroup-cached)
9261           gnus-cached-mark)
9262          ((memq article gnus-newsgroup-replied)
9263           gnus-replied-mark)
9264          ((memq article gnus-newsgroup-forwarded)
9265           gnus-forwarded-mark)
9266          ((memq article gnus-newsgroup-saved)
9267           gnus-saved-mark)
9268          ((memq article gnus-newsgroup-recent)
9269           gnus-recent-mark)
9270          ((memq article gnus-newsgroup-unseen)
9271           gnus-unseen-mark)
9272          (t gnus-no-mark))
9273    'replied)
9274   (when (gnus-visual-p 'summary-highlight 'highlight)
9275     (gnus-run-hooks 'gnus-summary-update-hook))
9276   t)
9277
9278 (defun gnus-summary-update-mark (mark type)
9279   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9280         (buffer-read-only nil))
9281     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9282     (when forward
9283       (when (looking-at "\r")
9284         (incf forward))
9285       (when (<= (+ forward (point)) (point-max))
9286         ;; Go to the right position on the line.
9287         (goto-char (+ forward (point)))
9288         ;; Replace the old mark with the new mark.
9289         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9290         ;; Optionally update the marks by some user rule.
9291         (when (eq type 'unread)
9292           (gnus-data-set-mark
9293            (gnus-data-find (gnus-summary-article-number)) mark)
9294           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9295
9296 (defun gnus-mark-article-as-read (article &optional mark)
9297   "Enter ARTICLE in the pertinent lists and remove it from others."
9298   ;; Make the article expirable.
9299   (let ((mark (or mark gnus-del-mark)))
9300     (if (= mark gnus-expirable-mark)
9301         (push article gnus-newsgroup-expirable)
9302       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9303     ;; Remove from unread and marked lists.
9304     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9305     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9306     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9307     (push (cons article mark) gnus-newsgroup-reads)
9308     ;; Possibly remove from cache, if that is used.
9309     (when gnus-use-cache
9310       (gnus-cache-enter-remove-article article))
9311     t))
9312
9313 (defun gnus-mark-article-as-unread (article &optional mark)
9314   "Enter ARTICLE in the pertinent lists and remove it from others."
9315   (let ((mark (or mark gnus-ticked-mark)))
9316     (if (<= article 0)
9317         (progn
9318           (gnus-error 1 "Can't mark negative article numbers")
9319           nil)
9320       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9321             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9322             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9323             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9324
9325       ;; Unsuppress duplicates?
9326       (when gnus-suppress-duplicates
9327         (gnus-dup-unsuppress-article article))
9328
9329       (cond ((= mark gnus-ticked-mark)
9330              (push article gnus-newsgroup-marked))
9331             ((= mark gnus-dormant-mark)
9332              (push article gnus-newsgroup-dormant))
9333             (t
9334              (push article gnus-newsgroup-unreads)))
9335       (gnus-pull article gnus-newsgroup-reads)
9336       t)))
9337
9338 (defalias 'gnus-summary-mark-as-unread-forward
9339   'gnus-summary-tick-article-forward)
9340 (make-obsolete 'gnus-summary-mark-as-unread-forward
9341                'gnus-summary-tick-article-forward)
9342 (defun gnus-summary-tick-article-forward (n)
9343   "Tick N articles forwards.
9344 If N is negative, tick backwards instead.
9345 The difference between N and the number of articles ticked is returned."
9346   (interactive "p")
9347   (gnus-summary-mark-forward n gnus-ticked-mark))
9348
9349 (defalias 'gnus-summary-mark-as-unread-backward
9350   'gnus-summary-tick-article-backward)
9351 (make-obsolete 'gnus-summary-mark-as-unread-backward
9352                'gnus-summary-tick-article-backward)
9353 (defun gnus-summary-tick-article-backward (n)
9354   "Tick N articles backwards.
9355 The difference between N and the number of articles ticked is returned."
9356   (interactive "p")
9357   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9358
9359 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9360 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9361 (defun gnus-summary-tick-article (&optional article clear-mark)
9362   "Mark current article as unread.
9363 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9364 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9365   (interactive)
9366   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9367                                        gnus-ticked-mark)))
9368
9369 (defun gnus-summary-mark-as-read-forward (n)
9370   "Mark N articles as read forwards.
9371 If N is negative, mark backwards instead.
9372 The difference between N and the actual number of articles marked is
9373 returned."
9374   (interactive "p")
9375   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9376
9377 (defun gnus-summary-mark-as-read-backward (n)
9378   "Mark the N articles as read backwards.
9379 The difference between N and the actual number of articles marked is
9380 returned."
9381   (interactive "p")
9382   (gnus-summary-mark-forward
9383    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9384
9385 (defun gnus-summary-mark-as-read (&optional article mark)
9386   "Mark current article as read.
9387 ARTICLE specifies the article to be marked as read.
9388 MARK specifies a string to be inserted at the beginning of the line."
9389   (gnus-summary-mark-article article mark))
9390
9391 (defun gnus-summary-clear-mark-forward (n)
9392   "Clear marks from N articles forward.
9393 If N is negative, clear backward instead.
9394 The difference between N and the number of marks cleared is returned."
9395   (interactive "p")
9396   (gnus-summary-mark-forward n gnus-unread-mark))
9397
9398 (defun gnus-summary-clear-mark-backward (n)
9399   "Clear marks from N articles backward.
9400 The difference between N and the number of marks cleared is returned."
9401   (interactive "p")
9402   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9403
9404 (defun gnus-summary-mark-unread-as-read ()
9405   "Intended to be used by `gnus-summary-mark-article-hook'."
9406   (when (memq gnus-current-article gnus-newsgroup-unreads)
9407     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9408
9409 (defun gnus-summary-mark-read-and-unread-as-read ()
9410   "Intended to be used by `gnus-summary-mark-article-hook'."
9411   (let ((mark (gnus-summary-article-mark)))
9412     (when (or (gnus-unread-mark-p mark)
9413               (gnus-read-mark-p mark))
9414       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9415
9416 (defun gnus-summary-mark-unread-as-ticked ()
9417   "Intended to be used by `gnus-summary-mark-article-hook'."
9418   (when (memq gnus-current-article gnus-newsgroup-unreads)
9419     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9420
9421 (defun gnus-summary-mark-region-as-read (point mark all)
9422   "Mark all unread articles between point and mark as read.
9423 If given a prefix, mark all articles between point and mark as read,
9424 even ticked and dormant ones."
9425   (interactive "r\nP")
9426   (save-excursion
9427     (let (article)
9428       (goto-char point)
9429       (beginning-of-line)
9430       (while (and
9431               (< (point) mark)
9432               (progn
9433                 (when (or all
9434                           (memq (setq article (gnus-summary-article-number))
9435                                 gnus-newsgroup-unreads))
9436                   (gnus-summary-mark-article article gnus-del-mark))
9437                 t)
9438               (gnus-summary-find-next))))))
9439
9440 (defun gnus-summary-mark-below (score mark)
9441   "Mark articles with score less than SCORE with MARK."
9442   (interactive "P\ncMark: ")
9443   (setq score (if score
9444                   (prefix-numeric-value score)
9445                 (or gnus-summary-default-score 0)))
9446   (save-excursion
9447     (set-buffer gnus-summary-buffer)
9448     (goto-char (point-min))
9449     (while
9450         (progn
9451           (and (< (gnus-summary-article-score) score)
9452                (gnus-summary-mark-article nil mark))
9453           (gnus-summary-find-next)))))
9454
9455 (defun gnus-summary-kill-below (&optional score)
9456   "Mark articles with score below SCORE as read."
9457   (interactive "P")
9458   (gnus-summary-mark-below score gnus-killed-mark))
9459
9460 (defun gnus-summary-clear-above (&optional score)
9461   "Clear all marks from articles with score above SCORE."
9462   (interactive "P")
9463   (gnus-summary-mark-above score gnus-unread-mark))
9464
9465 (defun gnus-summary-tick-above (&optional score)
9466   "Tick all articles with score above SCORE."
9467   (interactive "P")
9468   (gnus-summary-mark-above score gnus-ticked-mark))
9469
9470 (defun gnus-summary-mark-above (score mark)
9471   "Mark articles with score over SCORE with MARK."
9472   (interactive "P\ncMark: ")
9473   (setq score (if score
9474                   (prefix-numeric-value score)
9475                 (or gnus-summary-default-score 0)))
9476   (save-excursion
9477     (set-buffer gnus-summary-buffer)
9478     (goto-char (point-min))
9479     (while (and (progn
9480                   (when (> (gnus-summary-article-score) score)
9481                     (gnus-summary-mark-article nil mark))
9482                   t)
9483                 (gnus-summary-find-next)))))
9484
9485 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9486 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9487 (defun gnus-summary-limit-include-expunged (&optional no-error)
9488   "Display all the hidden articles that were expunged for low scores."
9489   (interactive)
9490   (let ((buffer-read-only nil))
9491     (let ((scored gnus-newsgroup-scored)
9492           headers h)
9493       (while scored
9494         (unless (gnus-summary-article-header (caar scored))
9495           (and (setq h (gnus-number-to-header (caar scored)))
9496                (< (cdar scored) gnus-summary-expunge-below)
9497                (push h headers)))
9498         (setq scored (cdr scored)))
9499       (if (not headers)
9500           (when (not no-error)
9501             (error "No expunged articles hidden"))
9502         (goto-char (point-min))
9503         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9504         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9505         (mapcar (lambda (x) (push (mail-header-number x)
9506                                   gnus-newsgroup-limit))
9507                 headers)
9508         (gnus-summary-prepare-unthreaded (nreverse headers))
9509         (goto-char (point-min))
9510         (gnus-summary-position-point)
9511         t))))
9512
9513 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9514   "Mark all unread articles in this newsgroup as read.
9515 If prefix argument ALL is non-nil, ticked and dormant articles will
9516 also be marked as read.
9517 If QUIETLY is non-nil, no questions will be asked.
9518 If TO-HERE is non-nil, it should be a point in the buffer.  All
9519 articles before (after, if REVERSE is set) this point will be marked as read.
9520 Note that this function will only catch up the unread article
9521 in the current summary buffer limitation.
9522 The number of articles marked as read is returned."
9523   (interactive "P")
9524   (prog1
9525       (save-excursion
9526         (when (or quietly
9527                   (not gnus-interactive-catchup) ;Without confirmation?
9528                   gnus-expert-user
9529                   (gnus-y-or-n-p
9530                    (if all
9531                        "Mark absolutely all articles as read? "
9532                      "Mark all unread articles as read? ")))
9533           (if (and not-mark
9534                    (not gnus-newsgroup-adaptive)
9535                    (not gnus-newsgroup-auto-expire)
9536                    (not gnus-suppress-duplicates)
9537                    (or (not gnus-use-cache)
9538                        (eq gnus-use-cache 'passive)))
9539               (progn
9540                 (when all
9541                   (setq gnus-newsgroup-marked nil
9542                         gnus-newsgroup-dormant nil))
9543                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9544             ;; We actually mark all articles as canceled, which we
9545             ;; have to do when using auto-expiry or adaptive scoring.
9546             (gnus-summary-show-all-threads)
9547             (if (and to-here reverse)
9548                 (progn
9549                   (goto-char to-here)
9550                   (while (and
9551                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9552                           (gnus-summary-find-next (not all) nil nil t))))
9553               (when (gnus-summary-first-subject (not all) t)
9554                 (while (and
9555                         (if to-here (< (point) to-here) t)
9556                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9557                         (gnus-summary-find-next (not all) nil nil t)))))
9558             (gnus-set-mode-line 'summary))
9559           t))
9560     (gnus-summary-position-point)))
9561
9562 (defun gnus-summary-catchup-to-here (&optional all)
9563   "Mark all unticked articles before the current one as read.
9564 If ALL is non-nil, also mark ticked and dormant articles as read."
9565   (interactive "P")
9566   (save-excursion
9567     (gnus-save-hidden-threads
9568       (let ((beg (point)))
9569         ;; We check that there are unread articles.
9570         (when (or all (gnus-summary-find-prev))
9571           (gnus-summary-catchup all t beg)))))
9572   (gnus-summary-position-point))
9573
9574 (defun gnus-summary-catchup-from-here (&optional all)
9575   "Mark all unticked articles after the current one as read.
9576 If ALL is non-nil, also mark ticked and dormant articles as read."
9577   (interactive "P")
9578   (save-excursion
9579     (gnus-save-hidden-threads
9580       (let ((beg (point)))
9581         ;; We check that there are unread articles.
9582         (when (or all (gnus-summary-find-next))
9583           (gnus-summary-catchup all t beg nil t)))))
9584
9585   (gnus-summary-position-point))
9586 (defun gnus-summary-catchup-all (&optional quietly)
9587   "Mark all articles in this newsgroup as read."
9588   (interactive "P")
9589   (gnus-summary-catchup t quietly))
9590
9591 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9592   "Mark all unread articles in this group as read, then exit.
9593 If prefix argument ALL is non-nil, all articles are marked as read.
9594 If QUIETLY is non-nil, no questions will be asked."
9595   (interactive "P")
9596   (when (gnus-summary-catchup all quietly nil 'fast)
9597     ;; Select next newsgroup or exit.
9598     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9599              (eq gnus-auto-select-next 'quietly))
9600         (gnus-summary-next-group nil)
9601       (gnus-summary-exit))))
9602
9603 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9604   "Mark all articles in this newsgroup as read, and then exit."
9605   (interactive "P")
9606   (gnus-summary-catchup-and-exit t quietly))
9607
9608 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9609   "Mark all articles in this group as read and select the next group.
9610 If given a prefix, mark all articles, unread as well as ticked, as
9611 read."
9612   (interactive "P")
9613   (save-excursion
9614     (gnus-summary-catchup all))
9615   (gnus-summary-next-group))
9616
9617 ;;;
9618 ;;; with article
9619 ;;;
9620
9621 (defmacro gnus-with-article (article &rest forms)
9622   "Select ARTICLE and perform FORMS in the original article buffer.
9623 Then replace the article with the result."
9624   `(progn
9625      ;; We don't want the article to be marked as read.
9626      (let (gnus-mark-article-hook)
9627        (gnus-summary-select-article t t nil ,article))
9628      (set-buffer gnus-original-article-buffer)
9629      ,@forms
9630      (if (not (gnus-check-backend-function
9631                'request-replace-article (car gnus-article-current)))
9632          (gnus-message 5 "Read-only group; not replacing")
9633        (unless (gnus-request-replace-article
9634                 ,article (car gnus-article-current)
9635                 (current-buffer) t)
9636          (error "Couldn't replace article")))
9637      ;; The cache and backlog have to be flushed somewhat.
9638      (when gnus-keep-backlog
9639        (gnus-backlog-remove-article
9640         (car gnus-article-current) (cdr gnus-article-current)))
9641      (when gnus-use-cache
9642        (gnus-cache-update-article
9643         (car gnus-article-current) (cdr gnus-article-current)))))
9644
9645 (put 'gnus-with-article 'lisp-indent-function 1)
9646 (put 'gnus-with-article 'edebug-form-spec '(form body))
9647
9648 ;; Thread-based commands.
9649
9650 (defun gnus-summary-articles-in-thread (&optional article)
9651   "Return a list of all articles in the current thread.
9652 If ARTICLE is non-nil, return all articles in the thread that starts
9653 with that article."
9654   (let* ((article (or article (gnus-summary-article-number)))
9655          (data (gnus-data-find-list article))
9656          (top-level (gnus-data-level (car data)))
9657          (top-subject
9658           (cond ((null gnus-thread-operation-ignore-subject)
9659                  (gnus-simplify-subject-re
9660                   (mail-header-subject (gnus-data-header (car data)))))
9661                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9662                  (gnus-simplify-subject-fuzzy
9663                   (mail-header-subject (gnus-data-header (car data)))))
9664                 (t nil)))
9665          (end-point (save-excursion
9666                       (if (gnus-summary-go-to-next-thread)
9667                           (point) (point-max))))
9668          articles)
9669     (while (and data
9670                 (< (gnus-data-pos (car data)) end-point))
9671       (when (or (not top-subject)
9672                 (string= top-subject
9673                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9674                              (gnus-simplify-subject-fuzzy
9675                               (mail-header-subject
9676                                (gnus-data-header (car data))))
9677                            (gnus-simplify-subject-re
9678                             (mail-header-subject
9679                              (gnus-data-header (car data)))))))
9680         (push (gnus-data-number (car data)) articles))
9681       (unless (and (setq data (cdr data))
9682                    (> (gnus-data-level (car data)) top-level))
9683         (setq data nil)))
9684     ;; Return the list of articles.
9685     (nreverse articles)))
9686
9687 (defun gnus-summary-rethread-current ()
9688   "Rethread the thread the current article is part of."
9689   (interactive)
9690   (let* ((gnus-show-threads t)
9691          (article (gnus-summary-article-number))
9692          (id (mail-header-id (gnus-summary-article-header)))
9693          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9694     (unless id
9695       (error "No article on the current line"))
9696     (gnus-rebuild-thread id)
9697     (gnus-summary-goto-subject article)))
9698
9699 (defun gnus-summary-reparent-thread ()
9700   "Make the current article child of the marked (or previous) article.
9701
9702 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9703 is non-nil or the Subject: of both articles are the same."
9704   (interactive)
9705   (unless (not (gnus-group-read-only-p))
9706     (error "The current newsgroup does not support article editing"))
9707   (unless (<= (length gnus-newsgroup-processable) 1)
9708     (error "No more than one article may be marked"))
9709   (save-window-excursion
9710     (let ((gnus-article-buffer " *reparent*")
9711           (current-article (gnus-summary-article-number))
9712           ;; First grab the marked article, otherwise one line up.
9713           (parent-article (if (not (null gnus-newsgroup-processable))
9714                               (car gnus-newsgroup-processable)
9715                             (save-excursion
9716                               (if (eq (forward-line -1) 0)
9717                                   (gnus-summary-article-number)
9718                                 (error "Beginning of summary buffer"))))))
9719       (unless (not (eq current-article parent-article))
9720         (error "An article may not be self-referential"))
9721       (let ((message-id (mail-header-id
9722                          (gnus-summary-article-header parent-article))))
9723         (unless (and message-id (not (equal message-id "")))
9724           (error "No message-id in desired parent"))
9725         (gnus-with-article current-article
9726           (save-restriction
9727             (goto-char (point-min))
9728             (message-narrow-to-head)
9729             (if (re-search-forward "^References: " nil t)
9730                 (progn
9731                   (re-search-forward "^[^ \t]" nil t)
9732                   (forward-line -1)
9733                   (end-of-line)
9734                   (insert " " message-id))
9735               (insert "References: " message-id "\n"))))
9736         (set-buffer gnus-summary-buffer)
9737         (gnus-summary-unmark-all-processable)
9738         (gnus-summary-update-article current-article)
9739         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9740             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9741         (gnus-summary-rethread-current)
9742         (gnus-message 3 "Article %d is now the child of article %d"
9743                       current-article parent-article)))))
9744
9745 (defun gnus-summary-toggle-threads (&optional arg)
9746   "Toggle showing conversation threads.
9747 If ARG is positive number, turn showing conversation threads on."
9748   (interactive "P")
9749   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9750     (setq gnus-show-threads
9751           (if (null arg) (not gnus-show-threads)
9752             (> (prefix-numeric-value arg) 0)))
9753     (gnus-summary-prepare)
9754     (gnus-summary-goto-subject current)
9755     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9756     (gnus-summary-position-point)))
9757
9758 (defun gnus-summary-show-all-threads ()
9759   "Show all threads."
9760   (interactive)
9761   (save-excursion
9762     (let ((buffer-read-only nil))
9763       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9764   (gnus-summary-position-point))
9765
9766 (defun gnus-summary-show-thread ()
9767   "Show thread subtrees.
9768 Returns nil if no thread was there to be shown."
9769   (interactive)
9770   (let ((buffer-read-only nil)
9771         (orig (point))
9772         ;; first goto end then to beg, to have point at beg after let
9773         (end (progn (end-of-line) (point)))
9774         (beg (progn (beginning-of-line) (point))))
9775     (prog1
9776         ;; Any hidden lines here?
9777         (search-forward "\r" end t)
9778       (subst-char-in-region beg end ?\^M ?\n t)
9779       (goto-char orig)
9780       (gnus-summary-position-point))))
9781
9782 (defun gnus-summary-maybe-hide-threads ()
9783   "If requested, hide the threads that should be hidden."
9784   (when (and gnus-show-threads
9785              gnus-thread-hide-subtree)
9786     (gnus-summary-hide-all-threads
9787      (if (or (consp gnus-thread-hide-subtree)
9788              (gnus-functionp gnus-thread-hide-subtree))
9789          (gnus-make-predicate gnus-thread-hide-subtree)
9790        nil))))
9791
9792 ;;; Hiding predicates.
9793
9794 (defun gnus-article-unread-p (header)
9795   (memq (mail-header-number header) gnus-newsgroup-unreads))
9796
9797 (defun gnus-article-unseen-p (header)
9798   (memq (mail-header-number header) gnus-newsgroup-unseen))
9799
9800 (defun gnus-map-articles (predicate articles)
9801   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9802   (apply 'gnus-or (mapcar predicate
9803                           (mapcar 'gnus-summary-article-header articles))))
9804
9805 (defun gnus-summary-hide-all-threads (&optional predicate)
9806   "Hide all thread subtrees.
9807 If PREDICATE is supplied, threads that satisfy this predicate
9808 will not be hidden."
9809   (interactive)
9810   (save-excursion
9811     (goto-char (point-min))
9812     (let ((end nil))
9813       (while (not end)
9814         (when (or (not predicate)
9815                   (gnus-map-articles
9816                    predicate (gnus-summary-article-children)))
9817             (gnus-summary-hide-thread))
9818         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9819   (gnus-summary-position-point))
9820
9821 (defun gnus-summary-hide-thread ()
9822   "Hide thread subtrees.
9823 If PREDICATE is supplied, threads that satisfy this predicate
9824 will not be hidden.
9825 Returns nil if no threads were there to be hidden."
9826   (interactive)
9827   (let ((buffer-read-only nil)
9828         (start (point))
9829         (article (gnus-summary-article-number)))
9830     (goto-char start)
9831     ;; Go forward until either the buffer ends or the subthread
9832     ;; ends.
9833     (when (and (not (eobp))
9834                (or (zerop (gnus-summary-next-thread 1 t))
9835                    (goto-char (point-max))))
9836       (prog1
9837           (if (and (> (point) start)
9838                    (search-backward "\n" start t))
9839               (progn
9840                 (subst-char-in-region start (point) ?\n ?\^M)
9841                 (gnus-summary-goto-subject article))
9842             (goto-char start)
9843             nil)))))
9844
9845 (defun gnus-summary-go-to-next-thread (&optional previous)
9846   "Go to the same level (or less) next thread.
9847 If PREVIOUS is non-nil, go to previous thread instead.
9848 Return the article number moved to, or nil if moving was impossible."
9849   (let ((level (gnus-summary-thread-level))
9850         (way (if previous -1 1))
9851         (beg (point)))
9852     (forward-line way)
9853     (while (and (not (eobp))
9854                 (< level (gnus-summary-thread-level)))
9855       (forward-line way))
9856     (if (eobp)
9857         (progn
9858           (goto-char beg)
9859           nil)
9860       (setq beg (point))
9861       (prog1
9862           (gnus-summary-article-number)
9863         (goto-char beg)))))
9864
9865 (defun gnus-summary-next-thread (n &optional silent)
9866   "Go to the same level next N'th thread.
9867 If N is negative, search backward instead.
9868 Returns the difference between N and the number of skips actually
9869 done.
9870
9871 If SILENT, don't output messages."
9872   (interactive "p")
9873   (let ((backward (< n 0))
9874         (n (abs n)))
9875     (while (and (> n 0)
9876                 (gnus-summary-go-to-next-thread backward))
9877       (decf n))
9878     (unless silent
9879       (gnus-summary-position-point))
9880     (when (and (not silent) (/= 0 n))
9881       (gnus-message 7 "No more threads"))
9882     n))
9883
9884 (defun gnus-summary-prev-thread (n)
9885   "Go to the same level previous N'th thread.
9886 Returns the difference between N and the number of skips actually
9887 done."
9888   (interactive "p")
9889   (gnus-summary-next-thread (- n)))
9890
9891 (defun gnus-summary-go-down-thread ()
9892   "Go down one level in the current thread."
9893   (let ((children (gnus-summary-article-children)))
9894     (when children
9895       (gnus-summary-goto-subject (car children)))))
9896
9897 (defun gnus-summary-go-up-thread ()
9898   "Go up one level in the current thread."
9899   (let ((parent (gnus-summary-article-parent)))
9900     (when parent
9901       (gnus-summary-goto-subject parent))))
9902
9903 (defun gnus-summary-down-thread (n)
9904   "Go down thread N steps.
9905 If N is negative, go up instead.
9906 Returns the difference between N and how many steps down that were
9907 taken."
9908   (interactive "p")
9909   (let ((up (< n 0))
9910         (n (abs n)))
9911     (while (and (> n 0)
9912                 (if up (gnus-summary-go-up-thread)
9913                   (gnus-summary-go-down-thread)))
9914       (setq n (1- n)))
9915     (gnus-summary-position-point)
9916     (when (/= 0 n)
9917       (gnus-message 7 "Can't go further"))
9918     n))
9919
9920 (defun gnus-summary-up-thread (n)
9921   "Go up thread N steps.
9922 If N is negative, go down instead.
9923 Returns the difference between N and how many steps down that were
9924 taken."
9925   (interactive "p")
9926   (gnus-summary-down-thread (- n)))
9927
9928 (defun gnus-summary-top-thread ()
9929   "Go to the top of the thread."
9930   (interactive)
9931   (while (gnus-summary-go-up-thread))
9932   (gnus-summary-article-number))
9933
9934 (defun gnus-summary-kill-thread (&optional unmark)
9935   "Mark articles under current thread as read.
9936 If the prefix argument is positive, remove any kinds of marks.
9937 If the prefix argument is negative, tick articles instead."
9938   (interactive "P")
9939   (when unmark
9940     (setq unmark (prefix-numeric-value unmark)))
9941   (let ((articles (gnus-summary-articles-in-thread)))
9942     (save-excursion
9943       ;; Expand the thread.
9944       (gnus-summary-show-thread)
9945       ;; Mark all the articles.
9946       (while articles
9947         (gnus-summary-goto-subject (car articles))
9948         (cond ((null unmark)
9949                (gnus-summary-mark-article-as-read gnus-killed-mark))
9950               ((> unmark 0)
9951                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9952               (t
9953                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9954         (setq articles (cdr articles))))
9955     ;; Hide killed subtrees.
9956     (and (null unmark)
9957          gnus-thread-hide-killed
9958          (gnus-summary-hide-thread))
9959     ;; If marked as read, go to next unread subject.
9960     (when (null unmark)
9961       ;; Go to next unread subject.
9962       (gnus-summary-next-subject 1 t)))
9963   (gnus-set-mode-line 'summary))
9964
9965 ;; Summary sorting commands
9966
9967 (defun gnus-summary-sort-by-number (&optional reverse)
9968   "Sort the summary buffer by article number.
9969 Argument REVERSE means reverse order."
9970   (interactive "P")
9971   (gnus-summary-sort 'number reverse))
9972
9973 (defun gnus-summary-sort-by-author (&optional reverse)
9974   "Sort the summary buffer by author name alphabetically.
9975 If `case-fold-search' is non-nil, case of letters is ignored.
9976 Argument REVERSE means reverse order."
9977   (interactive "P")
9978   (gnus-summary-sort 'author reverse))
9979
9980 (defun gnus-summary-sort-by-subject (&optional reverse)
9981   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9982 If `case-fold-search' is non-nil, case of letters is ignored.
9983 Argument REVERSE means reverse order."
9984   (interactive "P")
9985   (gnus-summary-sort 'subject reverse))
9986
9987 (defun gnus-summary-sort-by-date (&optional reverse)
9988   "Sort the summary buffer by date.
9989 Argument REVERSE means reverse order."
9990   (interactive "P")
9991   (gnus-summary-sort 'date reverse))
9992
9993 (defun gnus-summary-sort-by-score (&optional reverse)
9994   "Sort the summary buffer by score.
9995 Argument REVERSE means reverse order."
9996   (interactive "P")
9997   (gnus-summary-sort 'score reverse))
9998
9999 (defun gnus-summary-sort-by-lines (&optional reverse)
10000   "Sort the summary buffer by the number of lines.
10001 Argument REVERSE means reverse order."
10002   (interactive "P")
10003   (gnus-summary-sort 'lines reverse))
10004
10005 (defun gnus-summary-sort-by-chars (&optional reverse)
10006   "Sort the summary buffer by article length.
10007 Argument REVERSE means reverse order."
10008   (interactive "P")
10009   (gnus-summary-sort 'chars reverse))
10010
10011 (defun gnus-summary-sort-by-original (&optional reverse)
10012   "Sort the summary buffer using the default sorting method.
10013 Argument REVERSE means reverse order."
10014   (interactive "P")
10015   (let* ((buffer-read-only)
10016          (gnus-summary-prepare-hook nil))
10017     ;; We do the sorting by regenerating the threads.
10018     (gnus-summary-prepare)
10019     ;; Hide subthreads if needed.
10020     (gnus-summary-maybe-hide-threads)))
10021
10022 (defun gnus-summary-sort (predicate reverse)
10023   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10024   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10025          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10026          (gnus-thread-sort-functions
10027           (if (not reverse)
10028               thread
10029             `(lambda (t1 t2)
10030                (,thread t2 t1))))
10031          (gnus-sort-gathered-threads-function
10032           gnus-thread-sort-functions)
10033          (gnus-article-sort-functions
10034           (if (not reverse)
10035               article
10036             `(lambda (t1 t2)
10037                (,article t2 t1))))
10038          (buffer-read-only)
10039          (gnus-summary-prepare-hook nil))
10040     ;; We do the sorting by regenerating the threads.
10041     (gnus-summary-prepare)
10042     ;; Hide subthreads if needed.
10043     (gnus-summary-maybe-hide-threads)))
10044
10045 ;; Summary saving commands.
10046
10047 (defun gnus-summary-save-article (&optional n not-saved)
10048   "Save the current article using the default saver function.
10049 If N is a positive number, save the N next articles.
10050 If N is a negative number, save the N previous articles.
10051 If N is nil and any articles have been marked with the process mark,
10052 save those articles instead.
10053 The variable `gnus-default-article-saver' specifies the saver function."
10054   (interactive "P")
10055   (let* ((articles (gnus-summary-work-articles n))
10056          (save-buffer (save-excursion
10057                         (nnheader-set-temp-buffer " *Gnus Save*")))
10058          (num (length articles))
10059          header file)
10060     (dolist (article articles)
10061       (setq header (gnus-summary-article-header article))
10062       (if (not (vectorp header))
10063           ;; This is a pseudo-article.
10064           (if (assq 'name header)
10065               (gnus-copy-file (cdr (assq 'name header)))
10066             (gnus-message 1 "Article %d is unsaveable" article))
10067         ;; This is a real article.
10068         (save-window-excursion
10069           (let ((gnus-display-mime-function nil)
10070                 (gnus-article-prepare-hook nil))
10071             (gnus-summary-select-article t nil nil article)))
10072         (save-excursion
10073           (set-buffer save-buffer)
10074           (erase-buffer)
10075           (insert-buffer-substring gnus-original-article-buffer))
10076         (setq file (gnus-article-save save-buffer file num))
10077         (gnus-summary-remove-process-mark article)
10078         (unless not-saved
10079           (gnus-summary-set-saved-mark article))))
10080     (gnus-kill-buffer save-buffer)
10081     (gnus-summary-position-point)
10082     (gnus-set-mode-line 'summary)
10083     n))
10084
10085 (defun gnus-summary-pipe-output (&optional arg)
10086   "Pipe the current article to a subprocess.
10087 If N is a positive number, pipe the N next articles.
10088 If N is a negative number, pipe the N previous articles.
10089 If N is nil and any articles have been marked with the process mark,
10090 pipe those articles instead."
10091   (interactive "P")
10092   (require 'gnus-art)
10093   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10094     (gnus-summary-save-article arg t))
10095   (let ((buffer (get-buffer "*Shell Command Output*")))
10096     (if (and buffer
10097              (with-current-buffer buffer (> (point-max) (point-min))))
10098         (gnus-configure-windows 'pipe))))
10099
10100 (defun gnus-summary-save-article-mail (&optional arg)
10101   "Append the current article to an mail file.
10102 If N is a positive number, save the N next articles.
10103 If N is a negative number, save the N previous articles.
10104 If N is nil and any articles have been marked with the process mark,
10105 save those articles instead."
10106   (interactive "P")
10107   (require 'gnus-art)
10108   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10109     (gnus-summary-save-article arg)))
10110
10111 (defun gnus-summary-save-article-rmail (&optional arg)
10112   "Append the current article to an rmail file.
10113 If N is a positive number, save the N next articles.
10114 If N is a negative number, save the N previous articles.
10115 If N is nil and any articles have been marked with the process mark,
10116 save those articles instead."
10117   (interactive "P")
10118   (require 'gnus-art)
10119   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10120     (gnus-summary-save-article arg)))
10121
10122 (defun gnus-summary-save-article-file (&optional arg)
10123   "Append the current article to a file.
10124 If N is a positive number, save the N next articles.
10125 If N is a negative number, save the N previous articles.
10126 If N is nil and any articles have been marked with the process mark,
10127 save those articles instead."
10128   (interactive "P")
10129   (require 'gnus-art)
10130   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10131     (gnus-summary-save-article arg)))
10132
10133 (defun gnus-summary-write-article-file (&optional arg)
10134   "Write the current article to a file, deleting the previous file.
10135 If N is a positive number, save the N next articles.
10136 If N is a negative number, save the N previous articles.
10137 If N is nil and any articles have been marked with the process mark,
10138 save those articles instead."
10139   (interactive "P")
10140   (require 'gnus-art)
10141   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10142     (gnus-summary-save-article arg)))
10143
10144 (defun gnus-summary-save-article-body-file (&optional arg)
10145   "Append the current article body to a file.
10146 If N is a positive number, save the N next articles.
10147 If N is a negative number, save the N previous articles.
10148 If N is nil and any articles have been marked with the process mark,
10149 save those articles instead."
10150   (interactive "P")
10151   (require 'gnus-art)
10152   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10153     (gnus-summary-save-article arg)))
10154
10155 (defun gnus-summary-muttprint (&optional arg)
10156   "Print the current article using Muttprint.
10157 If N is a positive number, save the N next articles.
10158 If N is a negative number, save the N previous articles.
10159 If N is nil and any articles have been marked with the process mark,
10160 save those articles instead."
10161   (interactive "P")
10162   (require 'gnus-art)
10163   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10164     (gnus-summary-save-article arg t)))
10165
10166 (defun gnus-summary-pipe-message (program)
10167   "Pipe the current article through PROGRAM."
10168   (interactive "sProgram: ")
10169   (gnus-summary-select-article)
10170   (let ((mail-header-separator ""))
10171     (gnus-eval-in-buffer-window gnus-article-buffer
10172       (save-restriction
10173         (widen)
10174         (let ((start (window-start))
10175               buffer-read-only)
10176           (message-pipe-buffer-body program)
10177           (set-window-start (get-buffer-window (current-buffer)) start))))))
10178
10179 (defun gnus-get-split-value (methods)
10180   "Return a value based on the split METHODS."
10181   (let (split-name method result match)
10182     (when methods
10183       (save-excursion
10184         (set-buffer gnus-original-article-buffer)
10185         (save-restriction
10186           (nnheader-narrow-to-headers)
10187           (while (and methods (not split-name))
10188             (goto-char (point-min))
10189             (setq method (pop methods))
10190             (setq match (car method))
10191             (when (cond
10192                    ((stringp match)
10193                     ;; Regular expression.
10194                     (ignore-errors
10195                       (re-search-forward match nil t)))
10196                    ((gnus-functionp match)
10197                     ;; Function.
10198                     (save-restriction
10199                       (widen)
10200                       (setq result (funcall match gnus-newsgroup-name))))
10201                    ((consp match)
10202                     ;; Form.
10203                     (save-restriction
10204                       (widen)
10205                       (setq result (eval match)))))
10206               (setq split-name (cdr method))
10207               (cond ((stringp result)
10208                      (push (expand-file-name
10209                             result gnus-article-save-directory)
10210                            split-name))
10211                     ((consp result)
10212                      (setq split-name (append result split-name)))))))))
10213     (nreverse split-name)))
10214
10215 (defun gnus-valid-move-group-p (group)
10216   (and (boundp group)
10217        (symbol-name group)
10218        (symbol-value group)
10219        (gnus-get-function (gnus-find-method-for-group
10220                            (symbol-name group)) 'request-accept-article t)))
10221
10222 (defun gnus-read-move-group-name (prompt default articles prefix)
10223   "Read a group name."
10224   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10225          (minibuffer-confirm-incomplete nil) ; XEmacs
10226          (prom
10227           (format "%s %s to:"
10228                   prompt
10229                   (if (> (length articles) 1)
10230                       (format "these %d articles" (length articles))
10231                     "this article")))
10232          (to-newsgroup
10233           (cond
10234            ((null split-name)
10235             (gnus-completing-read-with-default
10236              default prom
10237              gnus-active-hashtb
10238              'gnus-valid-move-group-p
10239              nil prefix
10240              'gnus-group-history))
10241            ((= 1 (length split-name))
10242             (gnus-completing-read-with-default
10243              (car split-name) prom
10244              gnus-active-hashtb
10245              'gnus-valid-move-group-p
10246              nil nil
10247              'gnus-group-history))
10248            (t
10249             (gnus-completing-read-with-default
10250              nil prom
10251              (mapcar (lambda (el) (list el))
10252                      (nreverse split-name))
10253              nil nil nil
10254              'gnus-group-history))))
10255          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10256     (when to-newsgroup
10257       (if (or (string= to-newsgroup "")
10258               (string= to-newsgroup prefix))
10259           (setq to-newsgroup default))
10260       (unless to-newsgroup
10261         (error "No group name entered"))
10262       (or (gnus-active to-newsgroup)
10263           (gnus-activate-group to-newsgroup nil nil to-method)
10264           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10265                                      to-newsgroup))
10266               (or (and (gnus-request-create-group to-newsgroup to-method)
10267                        (gnus-activate-group
10268                         to-newsgroup nil nil to-method)
10269                        (gnus-subscribe-group to-newsgroup))
10270                   (error "Couldn't create group %s" to-newsgroup)))
10271           (error "No such group: %s" to-newsgroup)))
10272     to-newsgroup))
10273
10274 (defun gnus-summary-save-parts (type dir n &optional reverse)
10275   "Save parts matching TYPE to DIR.
10276 If REVERSE, save parts that do not match TYPE."
10277   (interactive
10278    (list (read-string "Save parts of type: "
10279                       (or (car gnus-summary-save-parts-type-history)
10280                           gnus-summary-save-parts-default-mime)
10281                       'gnus-summary-save-parts-type-history)
10282          (setq gnus-summary-save-parts-last-directory
10283                (read-file-name "Save to directory: "
10284                                gnus-summary-save-parts-last-directory
10285                                nil t))
10286          current-prefix-arg))
10287   (gnus-summary-iterate n
10288     (let ((gnus-display-mime-function nil)
10289           (gnus-inhibit-treatment t))
10290       (gnus-summary-select-article))
10291     (save-excursion
10292       (set-buffer gnus-article-buffer)
10293       (let ((handles (or gnus-article-mime-handles
10294                          (mm-dissect-buffer) (mm-uu-dissect))))
10295         (when handles
10296           (gnus-summary-save-parts-1 type dir handles reverse)
10297           (unless gnus-article-mime-handles ;; Don't destroy this case.
10298             (mm-destroy-parts handles)))))))
10299
10300 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10301   (if (stringp (car handle))
10302       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10303               (cdr handle))
10304     (when (if reverse
10305               (not (string-match type (mm-handle-media-type handle)))
10306             (string-match type (mm-handle-media-type handle)))
10307       (let ((file (expand-file-name
10308                    (file-name-nondirectory
10309                     (or
10310                      (mail-content-type-get
10311                       (mm-handle-disposition handle) 'filename)
10312                      (concat gnus-newsgroup-name
10313                              "." (number-to-string
10314                                   (cdr gnus-article-current)))))
10315                    dir)))
10316         (unless (file-exists-p file)
10317           (mm-save-part-to-file handle file))))))
10318
10319 ;; Summary extract commands
10320
10321 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10322   (let ((buffer-read-only nil)
10323         (article (gnus-summary-article-number))
10324         after-article b e)
10325     (unless (gnus-summary-goto-subject article)
10326       (error "No such article: %d" article))
10327     (gnus-summary-position-point)
10328     ;; If all commands are to be bunched up on one line, we collect
10329     ;; them here.
10330     (unless gnus-view-pseudos-separately
10331       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10332             files action)
10333         (while ps
10334           (setq action (cdr (assq 'action (car ps))))
10335           (setq files (list (cdr (assq 'name (car ps)))))
10336           (while (and ps (cdr ps)
10337                       (string= (or action "1")
10338                                (or (cdr (assq 'action (cadr ps))) "2")))
10339             (push (cdr (assq 'name (cadr ps))) files)
10340             (setcdr ps (cddr ps)))
10341           (when files
10342             (when (not (string-match "%s" action))
10343               (push " " files))
10344             (push " " files)
10345             (when (assq 'execute (car ps))
10346               (setcdr (assq 'execute (car ps))
10347                       (funcall (if (string-match "%s" action)
10348                                    'format 'concat)
10349                                action
10350                                (mapconcat
10351                                 (lambda (f)
10352                                   (if (equal f " ")
10353                                       f
10354                                     (mm-quote-arg f)))
10355                                 files " ")))))
10356           (setq ps (cdr ps)))))
10357     (if (and gnus-view-pseudos (not not-view))
10358         (while pslist
10359           (when (assq 'execute (car pslist))
10360             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10361                                   (eq gnus-view-pseudos 'not-confirm)))
10362           (setq pslist (cdr pslist)))
10363       (save-excursion
10364         (while pslist
10365           (setq after-article (or (cdr (assq 'article (car pslist)))
10366                                   (gnus-summary-article-number)))
10367           (gnus-summary-goto-subject after-article)
10368           (forward-line 1)
10369           (setq b (point))
10370           (insert "    " (file-name-nondirectory
10371                           (cdr (assq 'name (car pslist))))
10372                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10373           (setq e (point))
10374           (forward-line -1)             ; back to `b'
10375           (gnus-add-text-properties
10376            b (1- e) (list 'gnus-number gnus-reffed-article-number
10377                           gnus-mouse-face-prop gnus-mouse-face))
10378           (gnus-data-enter
10379            after-article gnus-reffed-article-number
10380            gnus-unread-mark b (car pslist) 0 (- e b))
10381           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10382           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10383           (setq pslist (cdr pslist)))))))
10384
10385 (defun gnus-pseudos< (p1 p2)
10386   (let ((c1 (cdr (assq 'action p1)))
10387         (c2 (cdr (assq 'action p2))))
10388     (and c1 c2 (string< c1 c2))))
10389
10390 (defun gnus-request-pseudo-article (props)
10391   (cond ((assq 'execute props)
10392          (gnus-execute-command (cdr (assq 'execute props)))))
10393   (let ((gnus-current-article (gnus-summary-article-number)))
10394     (gnus-run-hooks 'gnus-mark-article-hook)))
10395
10396 (defun gnus-execute-command (command &optional automatic)
10397   (save-excursion
10398     (gnus-article-setup-buffer)
10399     (set-buffer gnus-article-buffer)
10400     (setq buffer-read-only nil)
10401     (let ((command (if automatic command
10402                      (read-string "Command: " (cons command 0)))))
10403       (erase-buffer)
10404       (insert "$ " command "\n\n")
10405       (if gnus-view-pseudo-asynchronously
10406           (start-process "gnus-execute" (current-buffer) shell-file-name
10407                          shell-command-switch command)
10408         (call-process shell-file-name nil t nil
10409                       shell-command-switch command)))))
10410
10411 ;; Summary kill commands.
10412
10413 (defun gnus-summary-edit-global-kill (article)
10414   "Edit the \"global\" kill file."
10415   (interactive (list (gnus-summary-article-number)))
10416   (gnus-group-edit-global-kill article))
10417
10418 (defun gnus-summary-edit-local-kill ()
10419   "Edit a local kill file applied to the current newsgroup."
10420   (interactive)
10421   (setq gnus-current-headers (gnus-summary-article-header))
10422   (gnus-group-edit-local-kill
10423    (gnus-summary-article-number) gnus-newsgroup-name))
10424
10425 ;;; Header reading.
10426
10427 (defun gnus-read-header (id &optional header)
10428   "Read the headers of article ID and enter them into the Gnus system."
10429   (let ((group gnus-newsgroup-name)
10430         (gnus-override-method
10431          (or
10432           gnus-override-method
10433           (and (gnus-news-group-p gnus-newsgroup-name)
10434                (car (gnus-refer-article-methods)))))
10435         where)
10436     ;; First we check to see whether the header in question is already
10437     ;; fetched.
10438     (if (stringp id)
10439         ;; This is a Message-ID.
10440         (setq header (or header (gnus-id-to-header id)))
10441       ;; This is an article number.
10442       (setq header (or header (gnus-summary-article-header id))))
10443     (if (and header
10444              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10445         ;; We have found the header.
10446         header
10447       ;; If this is a sparse article, we have to nix out its
10448       ;; previous entry in the thread hashtb.
10449       (when (and header
10450                  (gnus-summary-article-sparse-p (mail-header-number header)))
10451         (let* ((parent (gnus-parent-id (mail-header-references header)))
10452                (thread (and parent (gnus-id-to-thread parent))))
10453           (when thread
10454             (delq (assq header thread) thread))))
10455       ;; We have to really fetch the header to this article.
10456       (save-excursion
10457         (set-buffer nntp-server-buffer)
10458         (when (setq where (gnus-request-head id group))
10459           (nnheader-fold-continuation-lines)
10460           (goto-char (point-max))
10461           (insert ".\n")
10462           (goto-char (point-min))
10463           (insert "211 ")
10464           (princ (cond
10465                   ((numberp id) id)
10466                   ((cdr where) (cdr where))
10467                   (header (mail-header-number header))
10468                   (t gnus-reffed-article-number))
10469                  (current-buffer))
10470           (insert " Article retrieved.\n"))
10471         (if (or (not where)
10472                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10473             ()                          ; Malformed head.
10474           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10475             (when (and (stringp id)
10476                        (not (string= (gnus-group-real-name group)
10477                                      (car where))))
10478               ;; If we fetched by Message-ID and the article came
10479               ;; from a different group, we fudge some bogus article
10480               ;; numbers for this article.
10481               (mail-header-set-number header gnus-reffed-article-number))
10482             (save-excursion
10483               (set-buffer gnus-summary-buffer)
10484               (decf gnus-reffed-article-number)
10485               (gnus-remove-header (mail-header-number header))
10486               (push header gnus-newsgroup-headers)
10487               (setq gnus-current-headers header)
10488               (push (mail-header-number header) gnus-newsgroup-limit)))
10489           header)))))
10490
10491 (defun gnus-remove-header (number)
10492   "Remove header NUMBER from `gnus-newsgroup-headers'."
10493   (if (and gnus-newsgroup-headers
10494            (= number (mail-header-number (car gnus-newsgroup-headers))))
10495       (pop gnus-newsgroup-headers)
10496     (let ((headers gnus-newsgroup-headers))
10497       (while (and (cdr headers)
10498                   (not (= number (mail-header-number (cadr headers)))))
10499         (pop headers))
10500       (when (cdr headers)
10501         (setcdr headers (cddr headers))))))
10502
10503 ;;;
10504 ;;; summary highlights
10505 ;;;
10506
10507 (defun gnus-highlight-selected-summary ()
10508   "Highlight selected article in summary buffer."
10509   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10510   (when gnus-summary-selected-face
10511     (save-excursion
10512       (let* ((beg (progn (beginning-of-line) (point)))
10513              (end (progn (end-of-line) (point)))
10514              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10515              (from (if (get-text-property beg gnus-mouse-face-prop)
10516                        beg
10517                      (or (next-single-property-change
10518                           beg gnus-mouse-face-prop nil end)
10519                          beg)))
10520              (to
10521               (if (= from end)
10522                   (- from 2)
10523                 (or (next-single-property-change
10524                      from gnus-mouse-face-prop nil end)
10525                     end))))
10526         ;; If no mouse-face prop on line we will have to = from = end,
10527         ;; so we highlight the entire line instead.
10528         (when (= (+ to 2) from)
10529           (setq from beg)
10530           (setq to end))
10531         (if gnus-newsgroup-selected-overlay
10532             ;; Move old overlay.
10533             (gnus-move-overlay
10534              gnus-newsgroup-selected-overlay from to (current-buffer))
10535           ;; Create new overlay.
10536           (gnus-overlay-put
10537            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10538            'face gnus-summary-selected-face))))))
10539
10540 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10541 (defun gnus-summary-highlight-line ()
10542   "Highlight current line according to `gnus-summary-highlight'."
10543   (let* ((list gnus-summary-highlight)
10544          (p (point))
10545          (end (progn (end-of-line) (point)))
10546          ;; now find out where the line starts and leave point there.
10547          (beg (progn (beginning-of-line) (point)))
10548          (article (gnus-summary-article-number))
10549          (score (or (cdr (assq (or article gnus-current-article)
10550                                gnus-newsgroup-scored))
10551                     gnus-summary-default-score 0))
10552          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10553          (inhibit-read-only t))
10554     ;; Eval the cars of the lists until we find a match.
10555     (let ((default gnus-summary-default-score)
10556           (default-high gnus-summary-default-high-score)
10557           (default-low gnus-summary-default-low-score))
10558       (while (and list
10559                   (not (eval (caar list))))
10560         (setq list (cdr list))))
10561     (let ((face (cdar list)))
10562       (unless (eq face (get-text-property beg 'face))
10563         (gnus-put-text-property-excluding-characters-with-faces
10564          beg end 'face
10565          (setq face (if (boundp face) (symbol-value face) face)))
10566         (when gnus-summary-highlight-line-function
10567           (funcall gnus-summary-highlight-line-function article face))))
10568     (goto-char p)))
10569
10570 (defun gnus-update-read-articles (group unread &optional compute)
10571   "Update the list of read articles in GROUP."
10572   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10573          (entry (gnus-gethash group gnus-newsrc-hashtb))
10574          (info (nth 2 entry))
10575          (prev 1)
10576          (unread (sort (copy-sequence unread) '<))
10577          read)
10578     (if (or (not info) (not active))
10579         ;; There is no info on this group if it was, in fact,
10580         ;; killed.  Gnus stores no information on killed groups, so
10581         ;; there's nothing to be done.
10582         ;; One could store the information somewhere temporarily,
10583         ;; perhaps...  Hmmm...
10584         ()
10585       ;; Remove any negative articles numbers.
10586       (while (and unread (< (car unread) 0))
10587         (setq unread (cdr unread)))
10588       ;; Remove any expired article numbers
10589       (while (and unread (< (car unread) (car active)))
10590         (setq unread (cdr unread)))
10591       ;; Compute the ranges of read articles by looking at the list of
10592       ;; unread articles.
10593       (while unread
10594         (when (/= (car unread) prev)
10595           (push (if (= prev (1- (car unread))) prev
10596                   (cons prev (1- (car unread))))
10597                 read))
10598         (setq prev (1+ (car unread)))
10599         (setq unread (cdr unread)))
10600       (when (<= prev (cdr active))
10601         (push (cons prev (cdr active)) read))
10602       (setq read (if (> (length read) 1) (nreverse read) read))
10603       (if compute
10604           read
10605         (save-excursion
10606           (let (setmarkundo)
10607             ;; Propagate the read marks to the backend.
10608             (when (gnus-check-backend-function 'request-set-mark group)
10609               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10610                     (add (gnus-remove-from-range read (gnus-info-read info))))
10611                 (when (or add del)
10612                   (unless (gnus-check-group group)
10613                     (error "Can't open server for %s" group))
10614                   (gnus-request-set-mark
10615                    group (delq nil (list (if add (list add 'add '(read)))
10616                                          (if del (list del 'del '(read))))))
10617                   (setq setmarkundo
10618                         `(gnus-request-set-mark
10619                           ,group
10620                           ',(delq nil (list
10621                                        (if del (list del 'add '(read)))
10622                                        (if add (list add 'del '(read))))))))))
10623             (set-buffer gnus-group-buffer)
10624             (gnus-undo-register
10625               `(progn
10626                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10627                  (gnus-info-set-read ',info ',(gnus-info-read info))
10628                  (gnus-get-unread-articles-in-group ',info
10629                                                     (gnus-active ,group))
10630                  (gnus-group-update-group ,group t)
10631                  ,setmarkundo))))
10632         ;; Enter this list into the group info.
10633         (gnus-info-set-read info read)
10634         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10635         (gnus-get-unread-articles-in-group info (gnus-active group))
10636         t))))
10637
10638 (defun gnus-offer-save-summaries ()
10639   "Offer to save all active summary buffers."
10640   (let (buffers)
10641     ;; Go through all buffers and find all summaries.
10642     (dolist (buffer (buffer-list))
10643       (when (and (setq buffer (buffer-name buffer))
10644                  (string-match "Summary" buffer)
10645                  (save-excursion
10646                    (set-buffer buffer)
10647                    ;; We check that this is, indeed, a summary buffer.
10648                    (and (eq major-mode 'gnus-summary-mode)
10649                         ;; Also make sure this isn't bogus.
10650                         gnus-newsgroup-prepared
10651                         ;; Also make sure that this isn't a
10652                         ;; dead summary buffer.
10653                         (not gnus-dead-summary-mode))))
10654         (push buffer buffers)))
10655     ;; Go through all these summary buffers and offer to save them.
10656     (when buffers
10657       (save-excursion
10658         (map-y-or-n-p
10659          "Update summary buffer %s? "
10660          (lambda (buf)
10661            (switch-to-buffer buf)
10662            (gnus-summary-exit))
10663          buffers)))))
10664
10665 (defun gnus-summary-setup-default-charset ()
10666   "Setup newsgroup default charset."
10667   (if (equal gnus-newsgroup-name "nndraft:drafts")
10668       (setq gnus-newsgroup-charset nil)
10669     (let* ((ignored-charsets
10670             (or gnus-newsgroup-ephemeral-ignored-charsets
10671                 (append
10672                  (and gnus-newsgroup-name
10673                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10674                  gnus-newsgroup-ignored-charsets))))
10675       (setq gnus-newsgroup-charset
10676             (or gnus-newsgroup-ephemeral-charset
10677                 (and gnus-newsgroup-name
10678                      (gnus-parameter-charset gnus-newsgroup-name))
10679                 gnus-default-charset))
10680       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10681            ignored-charsets))))
10682
10683 ;;;
10684 ;;; Mime Commands
10685 ;;;
10686
10687 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10688   "Display the current article buffer fully MIME-buttonized.
10689 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10690 treated as multipart/mixed."
10691   (interactive "P")
10692   (require 'gnus-art)
10693   (let ((gnus-unbuttonized-mime-types nil)
10694         (gnus-mime-display-multipart-as-mixed show-all-parts))
10695     (gnus-summary-show-article)))
10696
10697 (defun gnus-summary-repair-multipart (article)
10698   "Add a Content-Type header to a multipart article without one."
10699   (interactive (list (gnus-summary-article-number)))
10700   (gnus-with-article article
10701     (message-narrow-to-head)
10702     (message-remove-header "Mime-Version")
10703     (goto-char (point-max))
10704     (insert "Mime-Version: 1.0\n")
10705     (widen)
10706     (when (search-forward "\n--" nil t)
10707       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10708         (message-narrow-to-head)
10709         (message-remove-header "Content-Type")
10710         (goto-char (point-max))
10711         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10712                         separator))
10713         (widen))))
10714   (let (gnus-mark-article-hook)
10715     (gnus-summary-select-article t t nil article)))
10716
10717 (defun gnus-summary-toggle-display-buttonized ()
10718   "Toggle the buttonizing of the article buffer."
10719   (interactive)
10720   (require 'gnus-art)
10721   (if (setq gnus-inhibit-mime-unbuttonizing
10722             (not gnus-inhibit-mime-unbuttonizing))
10723       (let ((gnus-unbuttonized-mime-types nil))
10724         (gnus-summary-show-article))
10725     (gnus-summary-show-article)))
10726
10727 ;;;
10728 ;;; Generic summary marking commands
10729 ;;;
10730
10731 (defvar gnus-summary-marking-alist
10732   '((read gnus-del-mark "d")
10733     (unread gnus-unread-mark "u")
10734     (ticked gnus-ticked-mark "!")
10735     (dormant gnus-dormant-mark "?")
10736     (expirable gnus-expirable-mark "e"))
10737   "An alist of names/marks/keystrokes.")
10738
10739 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10740 (defvar gnus-summary-mark-map)
10741
10742 (defun gnus-summary-make-all-marking-commands ()
10743   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10744   (dolist (elem gnus-summary-marking-alist)
10745     (apply 'gnus-summary-make-marking-command elem)))
10746
10747 (defun gnus-summary-make-marking-command (name mark keystroke)
10748   (let ((map (make-sparse-keymap)))
10749     (define-key gnus-summary-generic-mark-map keystroke map)
10750     (dolist (lway `((next "next" next nil "n")
10751                     (next-unread "next unread" next t "N")
10752                     (prev "previous" prev nil "p")
10753                     (prev-unread "previous unread" prev t "P")
10754                     (nomove "" nil nil ,keystroke)))
10755       (let ((func (gnus-summary-make-marking-command-1
10756                    mark (car lway) lway name)))
10757         (setq func (eval func))
10758         (define-key map (nth 4 lway) func)))))
10759
10760 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10761   `(defun ,(intern
10762             (format "gnus-summary-put-mark-as-%s%s"
10763                     name (if (eq way 'nomove)
10764                              ""
10765                            (concat "-" (symbol-name way)))))
10766      (n)
10767      ,(format
10768        "Mark the current article as %s%s.
10769 If N, the prefix, then repeat N times.
10770 If N is negative, move in reverse order.
10771 The difference between N and the actual number of articles marked is
10772 returned."
10773        name (cadr lway))
10774      (interactive "p")
10775      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10776
10777 (defun gnus-summary-generic-mark (n mark move unread)
10778   "Mark N articles with MARK."
10779   (unless (eq major-mode 'gnus-summary-mode)
10780     (error "This command can only be used in the summary buffer"))
10781   (gnus-summary-show-thread)
10782   (let ((nummove
10783          (cond
10784           ((eq move 'next) 1)
10785           ((eq move 'prev) -1)
10786           (t 0))))
10787     (if (zerop nummove)
10788         (setq n 1)
10789       (when (< n 0)
10790         (setq n (abs n)
10791               nummove (* -1 nummove))))
10792     (while (and (> n 0)
10793                 (gnus-summary-mark-article nil mark)
10794                 (zerop (gnus-summary-next-subject nummove unread t)))
10795       (setq n (1- n)))
10796     (when (/= 0 n)
10797       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10798     (gnus-summary-recenter)
10799     (gnus-summary-position-point)
10800     (gnus-set-mode-line 'summary)
10801     n))
10802
10803 (defun gnus-summary-insert-articles (articles)
10804   (when (setq articles
10805               (gnus-set-difference articles
10806                                    (mapcar (lambda (h) (mail-header-number h))
10807                                            gnus-newsgroup-headers)))
10808     (setq gnus-newsgroup-headers
10809           (merge 'list
10810                  gnus-newsgroup-headers
10811                  (gnus-fetch-headers articles)
10812                  'gnus-article-sort-by-number))
10813     ;; Suppress duplicates?
10814     (when gnus-suppress-duplicates
10815       (gnus-dup-suppress-articles))
10816
10817     ;; We might want to build some more threads first.
10818     (when (and gnus-fetch-old-headers
10819                (eq gnus-headers-retrieved-by 'nov))
10820       (if (eq gnus-fetch-old-headers 'invisible)
10821           (gnus-build-all-threads)
10822         (gnus-build-old-threads)))
10823     ;; Let the Gnus agent mark articles as read.
10824     (when gnus-agent
10825       (gnus-agent-get-undownloaded-list))
10826     ;; Remove list identifiers from subject
10827     (when gnus-list-identifiers
10828       (gnus-summary-remove-list-identifiers))
10829     ;; First and last article in this newsgroup.
10830     (when gnus-newsgroup-headers
10831       (setq gnus-newsgroup-begin
10832             (mail-header-number (car gnus-newsgroup-headers))
10833             gnus-newsgroup-end
10834             (mail-header-number
10835              (gnus-last-element gnus-newsgroup-headers))))
10836     (when gnus-use-scoring
10837       (gnus-possibly-score-headers))))
10838
10839 (defun gnus-summary-insert-old-articles (&optional all)
10840   "Insert all old articles in this group.
10841 If ALL is non-nil, already read articles become readable.
10842 If ALL is a number, fetch this number of articles."
10843   (interactive "P")
10844   (prog1
10845       (let ((old (mapcar 'car gnus-newsgroup-data))
10846             (i (car gnus-newsgroup-active))
10847             older len)
10848         (while (<= i (cdr gnus-newsgroup-active))
10849           (or (memq i old) (push i older))
10850           (incf i))
10851         (setq len (length older))
10852         (cond
10853          ((null older) nil)
10854          ((numberp all)
10855           (if (< all len)
10856               (setq older (subseq older 0 all))))
10857          (all nil)
10858          (t
10859           (if (and (numberp gnus-large-newsgroup)
10860                    (> len gnus-large-newsgroup))
10861               (let ((input
10862                      (read-string
10863                       (format
10864                        "How many articles from %s (default %d): "
10865                        (gnus-limit-string
10866                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10867                        len))))
10868                 (unless (string-match "^[ \t]*$" input)
10869                   (setq all (string-to-number input))
10870                   (if (< all len)
10871                       (setq older (subseq older 0 all))))))))
10872         (if (not older)
10873             (message "No old news.")
10874           (let ((gnus-fetch-old-headers t))
10875             (gnus-summary-insert-articles older))
10876           (gnus-summary-limit (gnus-union older old))))
10877     (gnus-summary-position-point)))
10878
10879 (defun gnus-summary-insert-new-articles ()
10880   "Insert all new articles in this group."
10881   (interactive)
10882   (prog1
10883       (let ((old (mapcar 'car gnus-newsgroup-data))
10884             (old-active gnus-newsgroup-active)
10885             (nnmail-fetched-sources (list t))
10886             i new)
10887         (setq gnus-newsgroup-active
10888               (gnus-activate-group gnus-newsgroup-name 'scan))
10889         (setq i (1+ (cdr old-active)))
10890         (while (<= i (cdr gnus-newsgroup-active))
10891           (push i new)
10892           (incf i))
10893         (if (not new)
10894             (message "No gnus is bad news.")
10895           (setq new (nreverse new))
10896           (gnus-summary-insert-articles new)
10897           (setq gnus-newsgroup-unreads
10898                 (append gnus-newsgroup-unreads new))
10899           (gnus-summary-limit (gnus-union old new))))
10900     (gnus-summary-position-point)))
10901
10902 (gnus-summary-make-all-marking-commands)
10903
10904 (gnus-ems-redefine)
10905
10906 (provide 'gnus-sum)
10907
10908 (run-hooks 'gnus-sum-load-hook)
10909
10910 ;;; gnus-sum.el ends here