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