* gnus-sum.el (gnus-summary-exit): Bind gnus-group-is-exiting-p.
[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 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-make-false-root-always t
113   "Always make a false dummy root."
114   :group 'gnus-thread
115   :type 'boolean)
116
117 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
118   "*A regexp to match subjects to be excluded from loose thread gathering.
119 As loose thread gathering is done on subjects only, that means that
120 there can be many false gatherings performed.  By rooting out certain
121 common subjects, gathering might become saner."
122   :group 'gnus-thread
123   :type 'regexp)
124
125 (defcustom gnus-summary-gather-subject-limit nil
126   "*Maximum length of subject comparisons when gathering loose threads.
127 Use nil to compare full subjects.  Setting this variable to a low
128 number will help gather threads that have been corrupted by
129 newsreaders chopping off subject lines, but it might also mean that
130 unrelated articles that have subject that happen to begin with the
131 same few characters will be incorrectly gathered.
132
133 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
134 comparing subjects."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  (const fuzzy)
138                  (sexp :menu-tag "on" t)))
139
140 (defcustom gnus-simplify-subject-functions nil
141   "List of functions taking a string argument that simplify subjects.
142 The functions are applied recursively.
143
144 Useful functions to put in this list include:
145 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
146 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
147   :group 'gnus-thread
148   :type '(repeat function))
149
150 (defcustom gnus-simplify-ignored-prefixes nil
151   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  regexp))
155
156 (defcustom gnus-build-sparse-threads nil
157   "*If non-nil, fill in the gaps in threads.
158 If `some', only fill in the gaps that are needed to tie loose threads
159 together.  If `more', fill in all leaf nodes that Gnus can find.  If
160 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
161   :group 'gnus-thread
162   :type '(choice (const :tag "off" nil)
163                  (const some)
164                  (const more)
165                  (sexp :menu-tag "all" t)))
166
167 (defcustom gnus-summary-thread-gathering-function
168   'gnus-gather-threads-by-subject
169   "*Function used for gathering loose threads.
170 There are two pre-defined functions: `gnus-gather-threads-by-subject',
171 which only takes Subjects into consideration; and
172 `gnus-gather-threads-by-references', which compared the References
173 headers of the articles to find matches."
174   :group 'gnus-thread
175   :type '(radio (function-item gnus-gather-threads-by-subject)
176                 (function-item gnus-gather-threads-by-references)
177                 (function :tag "other")))
178
179 (defcustom gnus-summary-same-subject ""
180   "*String indicating that the current article has the same subject as the previous.
181 This variable will only be used if the value of
182 `gnus-summary-make-false-root' is `empty'."
183   :group 'gnus-summary-format
184   :type 'string)
185
186 (defcustom gnus-summary-goto-unread t
187   "*If t, many commands will go to the next unread article.
188 This applies to marking commands as well as other commands that
189 \"naturally\" select the next article, like, for instance, `SPC' at
190 the end of an article.
191
192 If nil, the marking commands do NOT go to the next unread article
193 \(they go to the next article instead).  If `never', commands that
194 usually go to the next unread article, will go to the next article,
195 whether it is read or not."
196   :group 'gnus-summary-marks
197   :link '(custom-manual "(gnus)Setting Marks")
198   :type '(choice (const :tag "off" nil)
199                  (const never)
200                  (sexp :menu-tag "on" t)))
201
202 (defcustom gnus-summary-default-score 0
203   "*Default article score level.
204 All scores generated by the score files will be added to this score.
205 If this variable is nil, scoring will be disabled."
206   :group 'gnus-score-default
207   :type '(choice (const :tag "disable")
208                  integer))
209
210 (defcustom gnus-summary-default-high-score 0
211   "*Default threshold for a high scored article.
212 An article will be highlighted as high scored if its score is greater
213 than this score."
214   :group 'gnus-score-default
215   :type 'integer)
216
217 (defcustom gnus-summary-default-low-score 0
218   "*Default threshold for a low scored article.
219 An article will be highlighted as low scored if its score is smaller
220 than this score."
221   :group 'gnus-score-default
222   :type 'integer)
223
224 (defcustom gnus-summary-zcore-fuzz 0
225   "*Fuzziness factor for the zcore in the summary buffer.
226 Articles with scores closer than this to `gnus-summary-default-score'
227 will not be marked."
228   :group 'gnus-summary-format
229   :type 'integer)
230
231 (defcustom gnus-simplify-subject-fuzzy-regexp nil
232   "*Strings to be removed when doing fuzzy matches.
233 This can either be a regular expression or list of regular expressions
234 that will be removed from subject strings if fuzzy subject
235 simplification is selected."
236   :group 'gnus-thread
237   :type '(repeat regexp))
238
239 (defcustom gnus-show-threads t
240   "*If non-nil, display threads in summary mode."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-subtree nil
245   "*If non-nil, hide all threads initially.
246 This can be a predicate specifier which says which threads to hide.
247 If threads are hidden, you have to run the command
248 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
249 to expose hidden threads."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-hide-killed t
254   "*If non-nil, hide killed threads automatically."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-ignore-subject t
259   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
260 If nil, articles that have different subjects from their parents will
261 start separate threads."
262   :group 'gnus-thread
263   :type 'boolean)
264
265 (defcustom gnus-thread-operation-ignore-subject t
266   "*If non-nil, subjects will be ignored when doing thread commands.
267 This affects commands like `gnus-summary-kill-thread' and
268 `gnus-summary-lower-thread'.
269
270 If this variable is nil, articles in the same thread with different
271 subjects will not be included in the operation in question.  If this
272 variable is `fuzzy', only articles that have subjects that are fuzzily
273 equal will be included."
274   :group 'gnus-thread
275   :type '(choice (const :tag "off" nil)
276                  (const fuzzy)
277                  (sexp :tag "on" t)))
278
279 (defcustom gnus-thread-indent-level 4
280   "*Number that says how much each sub-thread should be indented."
281   :group 'gnus-thread
282   :type 'integer)
283
284 (defcustom gnus-auto-extend-newsgroup t
285   "*If non-nil, extend newsgroup forward and backward when requested."
286   :group 'gnus-summary-choose
287   :type 'boolean)
288
289 (defcustom gnus-auto-select-first t
290   "*If non-nil, select the article under point.
291 Which article this is is controlled by the `gnus-auto-select-subject'
292 variable.
293
294 If you want to prevent automatic selection of articles in some
295 newsgroups, set the variable to nil in `gnus-select-group-hook'."
296   :group 'gnus-group-select
297   :type '(choice (const :tag "none" nil)
298                  (sexp :menu-tag "first" t)))
299
300 (defcustom gnus-auto-select-subject 'unread
301   "*Says what subject to place under point when entering a group.
302
303 This variable can either be the symbols `first' (place point on the
304 first subject), `unread' (place point on the subject line of the first
305 unread article), `best' (place point on the subject line of the
306 higest-scored article), `unseen' (place point on the subject line of
307 the first unseen article), 'unseen-or-unread' (place point on the subject
308 line of the first unseen article or, if all article have been seen, on the
309 subject line of the first unread article), or a function to be called to
310 place point on some subject line."
311   :group 'gnus-group-select
312   :type '(choice (const best)
313                  (const unread)
314                  (const first)
315                  (const unseen)
316                  (const unseen-or-unread)))
317
318 (defcustom gnus-auto-select-next t
319   "*If non-nil, offer to go to the next group from the end of the previous.
320 If the value is t and the next newsgroup is empty, Gnus will exit
321 summary mode and go back to group mode.  If the value is neither nil
322 nor t, Gnus will select the following unread newsgroup.  In
323 particular, if the value is the symbol `quietly', the next unread
324 newsgroup will be selected without any confirmation, and if it is
325 `almost-quietly', the next group will be selected without any
326 confirmation if you are located on the last article in the group.
327 Finally, if this variable is `slightly-quietly', the `Z n' command
328 will go to the next group without confirmation."
329   :group 'gnus-summary-maneuvering
330   :type '(choice (const :tag "off" nil)
331                  (const quietly)
332                  (const almost-quietly)
333                  (const slightly-quietly)
334                  (sexp :menu-tag "on" t)))
335
336 (defcustom gnus-auto-select-same nil
337   "*If non-nil, select the next article with the same subject.
338 If there are no more articles with the same subject, go to
339 the first unread article."
340   :group 'gnus-summary-maneuvering
341   :type 'boolean)
342
343 (defcustom gnus-summary-check-current nil
344   "*If non-nil, consider the current article when moving.
345 The \"unread\" movement commands will stay on the same line if the
346 current article is unread."
347   :group 'gnus-summary-maneuvering
348   :type 'boolean)
349
350 (defcustom gnus-auto-center-summary t
351   "*If non-nil, always center the current summary buffer.
352 In particular, if `vertical' do only vertical recentering.  If non-nil
353 and non-`vertical', do both horizontal and vertical recentering."
354   :group 'gnus-summary-maneuvering
355   :type '(choice (const :tag "none" nil)
356                  (const vertical)
357                  (integer :tag "height")
358                  (sexp :menu-tag "both" t)))
359
360 (defcustom gnus-show-all-headers nil
361   "*If non-nil, don't hide any headers."
362   :group 'gnus-article-hiding
363   :group 'gnus-article-headers
364   :type 'boolean)
365
366 (defcustom gnus-summary-ignore-duplicates nil
367   "*If non-nil, ignore articles with identical Message-ID headers."
368   :group 'gnus-summary
369   :type 'boolean)
370
371 (defcustom gnus-single-article-buffer t
372   "*If non-nil, display all articles in the same buffer.
373 If nil, each group will get its own article buffer."
374   :group 'gnus-article-various
375   :type 'boolean)
376
377 (defcustom gnus-break-pages t
378   "*If non-nil, do page breaking on articles.
379 The page delimiter is specified by the `gnus-page-delimiter'
380 variable."
381   :group 'gnus-article-various
382   :type 'boolean)
383
384 (defcustom gnus-move-split-methods nil
385   "*Variable used to suggest where articles are to be moved to.
386 It uses the same syntax as the `gnus-split-methods' variable.
387 However, whereas `gnus-split-methods' specifies file names as targets,
388 this variable specifies group names."
389   :group 'gnus-summary-mail
390   :type '(repeat (choice (list :value (fun) function)
391                          (cons :value ("" "") regexp (repeat string))
392                          (sexp :value nil))))
393
394 (defcustom gnus-unread-mark ?           ;Whitespace
395   "*Mark used for unread articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-ticked-mark ?!
400   "*Mark used for ticked articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-dormant-mark ??
405   "*Mark used for dormant articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-del-mark ?r
410   "*Mark used for del'd articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-read-mark ?R
415   "*Mark used for read articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-expirable-mark ?E
420   "*Mark used for expirable articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-killed-mark ?K
425   "*Mark used for killed articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-spam-mark ?H
430   "*Mark used for spam articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-souped-mark ?F
435   "*Mark used for souped articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-kill-file-mark ?X
440   "*Mark used for articles killed by kill files."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-low-score-mark ?Y
445   "*Mark used for articles with a low score."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-catchup-mark ?C
450   "*Mark used for articles that are caught up."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-replied-mark ?A
455   "*Mark used for articles that have been replied to."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-forwarded-mark ?F
460   "*Mark used for articles that have been forwarded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-recent-mark ?N
465   "*Mark used for articles that are recent."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-cached-mark ?*
470   "*Mark used for articles that are in the cache."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-saved-mark ?S
475   "*Mark used for articles that have been saved."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-unseen-mark ?.
480   "*Mark used for articles that haven't been seen."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-no-mark ?               ;Whitespace
485   "*Mark used for articles that have no other secondary mark."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-ancient-mark ?O
490   "*Mark used for ancient articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-sparse-mark ?Q
495   "*Mark used for sparsely reffed articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-canceled-mark ?G
500   "*Mark used for canceled articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-duplicate-mark ?M
505   "*Mark used for duplicate articles."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-undownloaded-mark ?-
510   "*Mark used for articles that weren't downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-downloaded-mark ?+
515   "*Mark used for articles that were downloaded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-downloadable-mark ?%
520   "*Mark used for articles that are to be downloaded."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-unsendable-mark ?=
525   "*Mark used for articles that won't be sent."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-score-over-mark ?+
530   "*Score mark used for articles with high scores."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-score-below-mark ?-
535   "*Score mark used for articles with low scores."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-empty-thread-mark ?     ;Whitespace
540   "*There is no thread under the article."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-not-empty-thread-mark ?=
545   "*There is a thread under the article."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-view-pseudo-asynchronously nil
550   "*If non-nil, Gnus will view pseudo-articles asynchronously."
551   :group 'gnus-extract-view
552   :type 'boolean)
553
554 (defcustom gnus-auto-expirable-marks
555   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
556         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
557         gnus-souped-mark gnus-duplicate-mark)
558   "*The list of marks converted into expiration if a group is auto-expirable."
559   :version "21.1"
560   :group 'gnus-summary
561   :type '(repeat character))
562
563 (defcustom gnus-inhibit-user-auto-expire t
564   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
565   :version "21.1"
566   :group 'gnus-summary
567   :type 'boolean)
568
569 (defcustom gnus-view-pseudos nil
570   "*If `automatic', pseudo-articles will be viewed automatically.
571 If `not-confirm', pseudos will be viewed automatically, and the user
572 will not be asked to confirm the command."
573   :group 'gnus-extract-view
574   :type '(choice (const :tag "off" nil)
575                  (const automatic)
576                  (const not-confirm)))
577
578 (defcustom gnus-view-pseudos-separately t
579   "*If non-nil, one pseudo-article will be created for each file to be viewed.
580 If nil, all files that use the same viewing command will be given as a
581 list of parameters to that command."
582   :group 'gnus-extract-view
583   :type 'boolean)
584
585 (defcustom gnus-insert-pseudo-articles t
586   "*If non-nil, insert pseudo-articles when decoding articles."
587   :group 'gnus-extract-view
588   :type 'boolean)
589
590 (defcustom gnus-summary-dummy-line-format
591   "   %(:                             :%) %S\n"
592   "*The format specification for the dummy roots in the summary buffer.
593 It works along the same lines as a normal formatting string,
594 with some simple extensions.
595
596 %S  The subject
597
598 General format specifiers can also be used.
599 See `(gnus)Formatting Variables'."
600   :link '(custom-manual "(gnus)Formatting Variables")
601   :group 'gnus-threading
602   :type 'string)
603
604 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
605   "*The format specification for the summary mode line.
606 It works along the same lines as a normal formatting string,
607 with some simple extensions:
608
609 %G  Group name
610 %p  Unprefixed group name
611 %A  Current article number
612 %z  Current article score
613 %V  Gnus version
614 %U  Number of unread articles in the group
615 %e  Number of unselected articles in the group
616 %Z  A string with unread/unselected article counts
617 %g  Shortish group name
618 %S  Subject of the current article
619 %u  User-defined spec
620 %s  Current score file name
621 %d  Number of dormant articles
622 %r  Number of articles that have been marked as read in this session
623 %E  Number of articles expunged by the score files"
624   :group 'gnus-summary-format
625   :type 'string)
626
627 (defcustom gnus-list-identifiers nil
628   "Regexp that matches list identifiers to be removed from subject.
629 This can also be a list of regexps."
630   :version "21.1"
631   :group 'gnus-summary-format
632   :group 'gnus-article-hiding
633   :type '(choice (const :tag "none" nil)
634                  (regexp :value ".*")
635                  (repeat :value (".*") regexp)))
636
637 (defcustom gnus-summary-mark-below 0
638   "*Mark all articles with a score below this variable as read.
639 This variable is local to each summary buffer and usually set by the
640 score file."
641   :group 'gnus-score-default
642   :type 'integer)
643
644 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
645   "*List of functions used for sorting articles in the summary buffer.
646
647 Each function takes two articles and returns non-nil if the first
648 article should be sorted before the other.  If you use more than one
649 function, the primary sort function should be the last.  You should
650 probably always include `gnus-article-sort-by-number' in the list of
651 sorting functions -- preferably first.  Also note that sorting by date
652 is often much slower than sorting by number, and the sorting order is
653 very similar.  (Sorting by date means sorting by the time the message
654 was sent, sorting by number means sorting by arrival time.)
655
656 Ready-made functions include `gnus-article-sort-by-number',
657 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
658 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
659 and `gnus-article-sort-by-score'.
660
661 When threading is turned on, the variable `gnus-thread-sort-functions'
662 controls how articles are sorted."
663   :group 'gnus-summary-sort
664   :type '(repeat (choice (function-item gnus-article-sort-by-number)
665                          (function-item gnus-article-sort-by-author)
666                          (function-item gnus-article-sort-by-subject)
667                          (function-item gnus-article-sort-by-date)
668                          (function-item gnus-article-sort-by-score)
669                          (function-item gnus-article-sort-by-random)
670                          (function :tag "other"))))
671
672 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
673   "*List of functions used for sorting threads in the summary buffer.
674 By default, threads are sorted by article number.
675
676 Each function takes two threads and returns non-nil if the first
677 thread should be sorted before the other.  If you use more than one
678 function, the primary sort function should be the last.  You should
679 probably always include `gnus-thread-sort-by-number' in the list of
680 sorting functions -- preferably first.  Also note that sorting by date
681 is often much slower than sorting by number, and the sorting order is
682 very similar.  (Sorting by date means sorting by the time the message
683 was sent, sorting by number means sorting by arrival time.)
684
685 Ready-made functions include `gnus-thread-sort-by-number',
686 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
687 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
688 `gnus-thread-sort-by-most-recent-number',
689 `gnus-thread-sort-by-most-recent-date',
690 `gnus-thread-sort-by-random', and
691 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
692
693 When threading is turned off, the variable
694 `gnus-article-sort-functions' controls how articles are sorted."
695   :group 'gnus-summary-sort
696   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
697                          (function-item gnus-thread-sort-by-author)
698                          (function-item gnus-thread-sort-by-subject)
699                          (function-item gnus-thread-sort-by-date)
700                          (function-item gnus-thread-sort-by-score)
701                          (function-item gnus-thread-sort-by-total-score)
702                          (function-item gnus-thread-sort-by-random)
703                          (function :tag "other"))))
704
705 (defcustom gnus-thread-score-function '+
706   "*Function used for calculating the total score of a thread.
707
708 The function is called with the scores of the article and each
709 subthread and should then return the score of the thread.
710
711 Some functions you can use are `+', `max', or `min'."
712   :group 'gnus-summary-sort
713   :type 'function)
714
715 (defcustom gnus-summary-expunge-below nil
716   "All articles that have a score less than this variable will be expunged.
717 This variable is local to the summary buffers."
718   :group 'gnus-score-default
719   :type '(choice (const :tag "off" nil)
720                  integer))
721
722 (defcustom gnus-thread-expunge-below nil
723   "All threads that have a total score less than this variable will be expunged.
724 See `gnus-thread-score-function' for en explanation of what a
725 \"thread score\" is.
726
727 This variable is local to the summary buffers."
728   :group 'gnus-threading
729   :group 'gnus-score-default
730   :type '(choice (const :tag "off" nil)
731                  integer))
732
733 (defcustom gnus-summary-mode-hook nil
734   "*A hook for Gnus summary mode.
735 This hook is run before any variables are set in the summary buffer."
736   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
737   :group 'gnus-summary-various
738   :type 'hook)
739
740 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
741 (when (featurep 'xemacs)
742   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
743   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
744   (add-hook 'gnus-summary-mode-hook
745             'gnus-xmas-switch-horizontal-scrollbar-off))
746
747 (defcustom gnus-summary-menu-hook nil
748   "*Hook run after the creation of the summary mode menu."
749   :group 'gnus-summary-visual
750   :type 'hook)
751
752 (defcustom gnus-summary-exit-hook nil
753   "*A hook called on exit from the summary buffer.
754 It will be called with point in the group buffer."
755   :group 'gnus-summary-exit
756   :type 'hook)
757
758 (defcustom gnus-summary-prepare-hook nil
759   "*A hook called after the summary buffer has been generated.
760 If you want to modify the summary buffer, you can use this hook."
761   :group 'gnus-summary-various
762   :type 'hook)
763
764 (defcustom gnus-summary-prepared-hook nil
765   "*A hook called as the last thing after the summary buffer has been generated."
766   :group 'gnus-summary-various
767   :type 'hook)
768
769 (defcustom gnus-summary-generate-hook nil
770   "*A hook run just before generating the summary buffer.
771 This hook is commonly used to customize threading variables and the
772 like."
773   :group 'gnus-summary-various
774   :type 'hook)
775
776 (defcustom gnus-select-group-hook nil
777   "*A hook called when a newsgroup is selected.
778
779 If you'd like to simplify subjects like the
780 `gnus-summary-next-same-subject' command does, you can use the
781 following hook:
782
783  (add-hook gnus-select-group-hook
784            (lambda ()
785              (mapcar (lambda (header)
786                        (mail-header-set-subject
787                         header
788                         (gnus-simplify-subject
789                          (mail-header-subject header) 're-only)))
790                      gnus-newsgroup-headers)))"
791   :group 'gnus-group-select
792   :type 'hook)
793
794 (defcustom gnus-select-article-hook nil
795   "*A hook called when an article is selected."
796   :group 'gnus-summary-choose
797   :type 'hook)
798
799 (defcustom gnus-visual-mark-article-hook
800   (list 'gnus-highlight-selected-summary)
801   "*Hook run after selecting an article in the summary buffer.
802 It is meant to be used for highlighting the article in some way.  It
803 is not run if `gnus-visual' is nil."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-parse-headers-hook nil
808   "*A hook called before parsing the headers."
809   :group 'gnus-various
810   :type 'hook)
811
812 (defcustom gnus-exit-group-hook nil
813   "*A hook called when exiting summary mode.
814 This hook is not called from the non-updating exit commands like `Q'."
815   :group 'gnus-various
816   :type 'hook)
817
818 (defcustom gnus-summary-update-hook
819   (list 'gnus-summary-highlight-line)
820   "*A hook called when a summary line is changed.
821 The hook will not be called if `gnus-visual' is nil.
822
823 The default function `gnus-summary-highlight-line' will
824 highlight the line according to the `gnus-summary-highlight'
825 variable."
826   :group 'gnus-summary-visual
827   :type 'hook)
828
829 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
830   "*A hook called when an article is selected for the first time.
831 The hook is intended to mark an article as read (or unread)
832 automatically when it is selected."
833   :group 'gnus-summary-choose
834   :type 'hook)
835
836 (defcustom gnus-group-no-more-groups-hook nil
837   "*A hook run when returning to group mode having no more (unread) groups."
838   :group 'gnus-group-select
839   :type 'hook)
840
841 (defcustom gnus-ps-print-hook nil
842   "*A hook run before ps-printing something from Gnus."
843   :group 'gnus-summary
844   :type 'hook)
845
846 (defcustom gnus-summary-display-arrow
847   (and (fboundp 'display-graphic-p)
848        (display-graphic-p))
849   "*If non-nil, display an arrow highlighting the current article."
850   :version "21.1"
851   :group 'gnus-summary
852   :type 'boolean)
853
854 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
855   "Face used for highlighting the current article in the summary buffer."
856   :group 'gnus-summary-visual
857   :type 'face)
858
859 (defvar gnus-tmp-downloaded nil)
860
861 (defcustom gnus-summary-highlight
862   '(((eq mark gnus-canceled-mark)
863      . gnus-summary-cancelled-face)
864     ((and uncached (> score default-high))
865      . gnus-summary-high-uncached-face)
866     ((and uncached (< score default-low))
867      . gnus-summary-low-uncached-face)
868     (uncached
869      . gnus-summary-normal-uncached-face)
870     ((and (> score default-high)
871           (or (eq mark gnus-dormant-mark)
872               (eq mark gnus-ticked-mark)))
873      . gnus-summary-high-ticked-face)
874     ((and (< score default-low)
875           (or (eq mark gnus-dormant-mark)
876               (eq mark gnus-ticked-mark)))
877      . gnus-summary-low-ticked-face)
878     ((or (eq mark gnus-dormant-mark)
879          (eq mark gnus-ticked-mark))
880      . gnus-summary-normal-ticked-face)
881     ((and (> score default-high) (eq mark gnus-ancient-mark))
882      . gnus-summary-high-ancient-face)
883     ((and (< score default-low) (eq mark gnus-ancient-mark))
884      . gnus-summary-low-ancient-face)
885     ((eq mark gnus-ancient-mark)
886      . gnus-summary-normal-ancient-face)
887     ((and (> score default-high) (eq mark gnus-unread-mark))
888      . gnus-summary-high-unread-face)
889     ((and (< score default-low) (eq mark gnus-unread-mark))
890      . gnus-summary-low-unread-face)
891     ((eq mark gnus-unread-mark)
892      . gnus-summary-normal-unread-face)
893     ((> score default-high)
894      . gnus-summary-high-read-face)
895     ((< score default-low)
896      . gnus-summary-low-read-face)
897     (t
898      . gnus-summary-normal-read-face))
899   "*Controls the highlighting of summary buffer lines.
900
901 A list of (FORM . FACE) pairs.  When deciding how a a particular
902 summary line should be displayed, each form is evaluated.  The content
903 of the face field after the first true form is used.  You can change
904 how those summary lines are displayed, by editing the face field.
905
906 You can use the following variables in the FORM field.
907
908 score:        The article's score
909 default:      The default article score.
910 default-high: The default score for high scored articles.
911 default-low:  The default score for low scored articles.
912 below:        The score below which articles are automatically marked as read.
913 mark:         The articles mark."
914   :group 'gnus-summary-visual
915   :type '(repeat (cons (sexp :tag "Form" nil)
916                        face)))
917
918 (defcustom gnus-alter-header-function nil
919   "Function called to allow alteration of article header structures.
920 The function is called with one parameter, the article header vector,
921 which it may alter in any way.")
922
923 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
924   "Variable that says which function should be used to decode a string with encoded words.")
925
926 (defcustom gnus-extra-headers '(To Newsgroups)
927   "*Extra headers to parse."
928   :version "21.1"
929   :group 'gnus-summary
930   :type '(repeat symbol))
931
932 (defcustom gnus-ignored-from-addresses
933   (and user-mail-address (regexp-quote user-mail-address))
934   "*Regexp of From headers that may be suppressed in favor of To headers."
935   :version "21.1"
936   :group 'gnus-summary
937   :type 'regexp)
938
939 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
940   "List of charsets that should be ignored.
941 When these charsets are used in the \"charset\" parameter, the
942 default charset will be used instead."
943   :version "21.1"
944   :type '(repeat symbol)
945   :group 'gnus-charset)
946
947 (gnus-define-group-parameter
948  ignored-charsets
949  :type list
950  :function-document
951  "Return the ignored charsets of GROUP."
952  :variable gnus-group-ignored-charsets-alist
953  :variable-default
954  '(("alt\\.chinese\\.text" iso-8859-1))
955  :variable-document
956  "Alist of regexps (to match group names) and charsets that should be ignored.
957 When these charsets are used in the \"charset\" parameter, the
958 default charset will be used instead."
959  :variable-group gnus-charset
960  :variable-type '(repeat (cons (regexp :tag "Group")
961                                (repeat symbol)))
962  :parameter-type '(choice :tag "Ignored charsets"
963                           :value nil
964                           (repeat (symbol)))
965  :parameter-document       "\
966 List of charsets that should be ignored.
967
968 When these charsets are used in the \"charset\" parameter, the
969 default charset will be used instead.")
970
971 (defcustom gnus-group-highlight-words-alist nil
972   "Alist of group regexps and highlight regexps.
973 This variable uses the same syntax as `gnus-emphasis-alist'."
974   :version "21.1"
975   :type '(repeat (cons (regexp :tag "Group")
976                        (repeat (list (regexp :tag "Highlight regexp")
977                                      (number :tag "Group for entire word" 0)
978                                      (number :tag "Group for displayed part" 0)
979                                      (symbol :tag "Face"
980                                              gnus-emphasis-highlight-words)))))
981   :group 'gnus-summary-visual)
982
983 (defcustom gnus-summary-show-article-charset-alist
984   nil
985   "Alist of number and charset.
986 The article will be shown with the charset corresponding to the
987 numbered argument.
988 For example: ((1 . cn-gb-2312) (2 . big5))."
989   :version "21.1"
990   :type '(repeat (cons (number :tag "Argument" 1)
991                        (symbol :tag "Charset")))
992   :group 'gnus-charset)
993
994 (defcustom gnus-preserve-marks t
995   "Whether marks are preserved when moving, copying and respooling messages."
996   :version "21.1"
997   :type 'boolean
998   :group 'gnus-summary-marks)
999
1000 (defcustom gnus-alter-articles-to-read-function nil
1001   "Function to be called to alter the list of articles to be selected."
1002   :type '(choice (const nil) function)
1003   :group 'gnus-summary)
1004
1005 (defcustom gnus-orphan-score nil
1006   "*All orphans get this score added.  Set in the score file."
1007   :group 'gnus-score-default
1008   :type '(choice (const nil)
1009                  integer))
1010
1011 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1012   "*A regexp to match MIME parts when saving multiple parts of a message
1013 with gnus-summary-save-parts (X m). This regexp will be used by default
1014 when prompting the user for which type of files to save."
1015   :group 'gnus-summary
1016   :type 'regexp)
1017
1018 (defcustom gnus-read-all-available-headers nil
1019   "Whether Gnus should parse all headers made available to it.
1020 This is mostly relevant for slow backends where the user may
1021 wish to widen the summary buffer to include all headers
1022 that were fetched.  Say, for nnultimate groups."
1023   :group 'gnus-summary
1024   :type '(choice boolean regexp))
1025
1026 (defcustom gnus-summary-muttprint-program "muttprint"
1027   "Command (and optional arguments) used to run Muttprint."
1028   :version "21.3"
1029   :group 'gnus-summary
1030   :type 'string)
1031
1032 (defcustom gnus-article-loose-mime nil
1033   "If non-nil, don't require MIME-Version header.
1034 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1035 supply the MIME-Version header or deliberately strip it From the mail.
1036 Set it to non-nil, Gnus will treat some articles as MIME even if
1037 the MIME-Version header is missed."
1038   :version "21.3"
1039   :type 'boolean
1040   :group 'gnus-article)
1041
1042 ;;; Internal variables
1043
1044 (defvar gnus-summary-display-cache nil)
1045 (defvar gnus-article-mime-handles nil)
1046 (defvar gnus-article-decoded-p nil)
1047 (defvar gnus-article-charset nil)
1048 (defvar gnus-article-ignored-charsets nil)
1049 (defvar gnus-scores-exclude-files nil)
1050 (defvar gnus-page-broken nil)
1051
1052 (defvar gnus-original-article nil)
1053 (defvar gnus-article-internal-prepare-hook nil)
1054 (defvar gnus-newsgroup-process-stack nil)
1055
1056 (defvar gnus-thread-indent-array nil)
1057 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1058 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1059   "Function called to sort the articles within a thread after it has been gathered together.")
1060
1061 (defvar gnus-summary-save-parts-type-history nil)
1062 (defvar gnus-summary-save-parts-last-directory nil)
1063
1064 ;; Avoid highlighting in kill files.
1065 (defvar gnus-summary-inhibit-highlight nil)
1066 (defvar gnus-newsgroup-selected-overlay nil)
1067 (defvar gnus-inhibit-limiting nil)
1068 (defvar gnus-newsgroup-adaptive-score-file nil)
1069 (defvar gnus-current-score-file nil)
1070 (defvar gnus-current-move-group nil)
1071 (defvar gnus-current-copy-group nil)
1072 (defvar gnus-current-crosspost-group nil)
1073 (defvar gnus-newsgroup-display nil)
1074
1075 (defvar gnus-newsgroup-dependencies nil)
1076 (defvar gnus-newsgroup-adaptive nil)
1077 (defvar gnus-summary-display-article-function nil)
1078 (defvar gnus-summary-highlight-line-function nil
1079   "Function called after highlighting a summary line.")
1080
1081 (defvar gnus-summary-line-format-alist
1082   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1083     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1084     (?s gnus-tmp-subject-or-nil ?s)
1085     (?n gnus-tmp-name ?s)
1086     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1087         ?s)
1088     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1089             gnus-tmp-from) ?s)
1090     (?F gnus-tmp-from ?s)
1091     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1092     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1093     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1094     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1095     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1096     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1097     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1098     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1099     (?L gnus-tmp-lines ?s)
1100     (?O gnus-tmp-downloaded ?c)
1101     (?I gnus-tmp-indentation ?s)
1102     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1103     (?R gnus-tmp-replied ?c)
1104     (?\[ gnus-tmp-opening-bracket ?c)
1105     (?\] gnus-tmp-closing-bracket ?c)
1106     (?\> (make-string gnus-tmp-level ? ) ?s)
1107     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1108     (?i gnus-tmp-score ?d)
1109     (?z gnus-tmp-score-char ?c)
1110     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1111     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1112     (?U gnus-tmp-unread ?c)
1113     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1114         ?s)
1115     (?t (gnus-summary-number-of-articles-in-thread
1116          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1117         ?d)
1118     (?e (gnus-summary-number-of-articles-in-thread
1119          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1120         ?c)
1121     (?u gnus-tmp-user-defined ?s)
1122     (?P (gnus-pick-line-number) ?d)
1123     (?B gnus-tmp-thread-tree-header-string ?s)
1124     (user-date (gnus-user-date
1125                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1126   "An alist of format specifications that can appear in summary lines.
1127 These are paired with what variables they correspond with, along with
1128 the type of the variable (string, integer, character, etc).")
1129
1130 (defvar gnus-summary-dummy-line-format-alist
1131   `((?S gnus-tmp-subject ?s)
1132     (?N gnus-tmp-number ?d)
1133     (?u gnus-tmp-user-defined ?s)))
1134
1135 (defvar gnus-summary-mode-line-format-alist
1136   `((?G gnus-tmp-group-name ?s)
1137     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1138     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1139     (?A gnus-tmp-article-number ?d)
1140     (?Z gnus-tmp-unread-and-unselected ?s)
1141     (?V gnus-version ?s)
1142     (?U gnus-tmp-unread-and-unticked ?d)
1143     (?S gnus-tmp-subject ?s)
1144     (?e gnus-tmp-unselected ?d)
1145     (?u gnus-tmp-user-defined ?s)
1146     (?d (length gnus-newsgroup-dormant) ?d)
1147     (?t (length gnus-newsgroup-marked) ?d)
1148     (?h (length gnus-newsgroup-spam-marked) ?d)
1149     (?r (length gnus-newsgroup-reads) ?d)
1150     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1151     (?E gnus-newsgroup-expunged-tally ?d)
1152     (?s (gnus-current-score-file-nondirectory) ?s)))
1153
1154 (defvar gnus-last-search-regexp nil
1155   "Default regexp for article search command.")
1156
1157 (defvar gnus-last-shell-command nil
1158   "Default shell command on article.")
1159
1160 (defvar gnus-newsgroup-agentized nil
1161   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1162 (defvar gnus-newsgroup-begin nil)
1163 (defvar gnus-newsgroup-end nil)
1164 (defvar gnus-newsgroup-last-rmail nil)
1165 (defvar gnus-newsgroup-last-mail nil)
1166 (defvar gnus-newsgroup-last-folder nil)
1167 (defvar gnus-newsgroup-last-file nil)
1168 (defvar gnus-newsgroup-auto-expire nil)
1169 (defvar gnus-newsgroup-active nil)
1170
1171 (defvar gnus-newsgroup-data nil)
1172 (defvar gnus-newsgroup-data-reverse nil)
1173 (defvar gnus-newsgroup-limit nil)
1174 (defvar gnus-newsgroup-limits nil)
1175
1176 (defvar gnus-newsgroup-unreads nil
1177   "Sorted list of unread articles in the current newsgroup.")
1178
1179 (defvar gnus-newsgroup-unselected nil
1180   "Sorted list of unselected unread articles in the current newsgroup.")
1181
1182 (defvar gnus-newsgroup-reads nil
1183   "Alist of read articles and article marks in the current newsgroup.")
1184
1185 (defvar gnus-newsgroup-expunged-tally nil)
1186
1187 (defvar gnus-newsgroup-marked nil
1188   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1189
1190 (defvar gnus-newsgroup-spam-marked nil
1191   "List of ranges of articles that have been marked as spam.")
1192
1193 (defvar gnus-newsgroup-killed nil
1194   "List of ranges of articles that have been through the scoring process.")
1195
1196 (defvar gnus-newsgroup-cached nil
1197   "Sorted list of articles that come from the article cache.")
1198
1199 (defvar gnus-newsgroup-saved nil
1200   "List of articles that have been saved.")
1201
1202 (defvar gnus-newsgroup-kill-headers nil)
1203
1204 (defvar gnus-newsgroup-replied nil
1205   "List of articles that have been replied to in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-forwarded nil
1208   "List of articles that have been forwarded in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-recent nil
1211   "List of articles that have are recent in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-expirable nil
1214   "Sorted list of articles in the current newsgroup that can be expired.")
1215
1216 (defvar gnus-newsgroup-processable nil
1217   "List of articles in the current newsgroup that can be processed.")
1218
1219 (defvar gnus-newsgroup-downloadable nil
1220   "Sorted list of articles in the current newsgroup that can be processed.")
1221
1222 (defvar gnus-newsgroup-undownloaded nil
1223   "List of articles in the current newsgroup that haven't been downloaded..")
1224
1225 (defvar gnus-newsgroup-unsendable nil
1226   "List of articles in the current newsgroup that won't be sent.")
1227
1228 (defvar gnus-newsgroup-bookmarks nil
1229   "List of articles in the current newsgroup that have bookmarks.")
1230
1231 (defvar gnus-newsgroup-dormant nil
1232   "Sorted list of dormant articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-unseen nil
1235   "List of unseen articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-seen nil
1238   "Range of seen articles in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-articles nil
1241   "List of articles in the current newsgroup.")
1242
1243 (defvar gnus-newsgroup-scored nil
1244   "List of scored articles in the current newsgroup.")
1245
1246 (defvar gnus-newsgroup-headers nil
1247   "List of article headers in the current newsgroup.")
1248
1249 (defvar gnus-newsgroup-threads nil)
1250
1251 (defvar gnus-newsgroup-prepared nil
1252   "Whether the current group has been prepared properly.")
1253
1254 (defvar gnus-newsgroup-ancient nil
1255   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1256
1257 (defvar gnus-newsgroup-sparse nil)
1258
1259 (defvar gnus-current-article nil)
1260 (defvar gnus-article-current nil)
1261 (defvar gnus-current-headers nil)
1262 (defvar gnus-have-all-headers nil)
1263 (defvar gnus-last-article nil)
1264 (defvar gnus-newsgroup-history nil)
1265 (defvar gnus-newsgroup-charset nil)
1266 (defvar gnus-newsgroup-ephemeral-charset nil)
1267 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1268
1269 (defvar gnus-article-before-search nil)
1270
1271 (defconst gnus-summary-local-variables
1272   '(gnus-newsgroup-name
1273     gnus-newsgroup-begin gnus-newsgroup-end
1274     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1275     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1276     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1277     gnus-newsgroup-unselected gnus-newsgroup-marked
1278     gnus-newsgroup-spam-marked
1279     gnus-newsgroup-reads gnus-newsgroup-saved
1280     gnus-newsgroup-replied gnus-newsgroup-forwarded
1281     gnus-newsgroup-recent
1282     gnus-newsgroup-expirable
1283     gnus-newsgroup-processable gnus-newsgroup-killed
1284     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1285     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1286     gnus-newsgroup-seen gnus-newsgroup-articles
1287     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1288     gnus-newsgroup-headers gnus-newsgroup-threads
1289     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1290     gnus-current-article gnus-current-headers gnus-have-all-headers
1291     gnus-last-article gnus-article-internal-prepare-hook
1292     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1293     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1294     gnus-thread-expunge-below
1295     gnus-score-alist gnus-current-score-file
1296     (gnus-summary-expunge-below . global)
1297     (gnus-summary-mark-below . global)
1298     (gnus-orphan-score . global)
1299     gnus-newsgroup-active gnus-scores-exclude-files
1300     gnus-newsgroup-history gnus-newsgroup-ancient
1301     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1302     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1303     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1304     (gnus-newsgroup-expunged-tally . 0)
1305     gnus-cache-removable-articles gnus-newsgroup-cached
1306     gnus-newsgroup-data gnus-newsgroup-data-reverse
1307     gnus-newsgroup-limit gnus-newsgroup-limits
1308     gnus-newsgroup-charset gnus-newsgroup-display)
1309   "Variables that are buffer-local to the summary buffers.")
1310
1311 (defvar gnus-newsgroup-variables nil
1312   "A list of variables that have separate values in different newsgroups.
1313 A list of newsgroup (summary buffer) local variables, or cons of
1314 variables and their default values (when the default values are not
1315 nil), that should be made global while the summary buffer is active.
1316 These variables can be used to set variables in the group parameters
1317 while still allowing them to affect operations done in other
1318 buffers. For example:
1319
1320 \(setq gnus-newsgroup-variables
1321      '(message-use-followup-to
1322        (gnus-visible-headers .
1323          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1324 ")
1325
1326 ;; Byte-compiler warning.
1327 (eval-when-compile (defvar gnus-article-mode-map))
1328
1329 ;; MIME stuff.
1330
1331 (defvar gnus-decode-encoded-word-methods
1332   '(mail-decode-encoded-word-string)
1333   "List of methods used to decode encoded words.
1334
1335 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1336 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1337 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1338 whose names match REGEXP.
1339
1340 For example:
1341 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1342  mail-decode-encoded-word-string
1343  (\"chinese\" . rfc1843-decode-string))")
1344
1345 (defvar gnus-decode-encoded-word-methods-cache nil)
1346
1347 (defun gnus-multi-decode-encoded-word-string (string)
1348   "Apply the functions from `gnus-encoded-word-methods' that match."
1349   (unless (and gnus-decode-encoded-word-methods-cache
1350                (eq gnus-newsgroup-name
1351                    (car gnus-decode-encoded-word-methods-cache)))
1352     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1353     (mapcar (lambda (x)
1354               (if (symbolp x)
1355                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1356                 (if (and gnus-newsgroup-name
1357                          (string-match (car x) gnus-newsgroup-name))
1358                     (nconc gnus-decode-encoded-word-methods-cache
1359                            (list (cdr x))))))
1360             gnus-decode-encoded-word-methods))
1361   (let ((xlist gnus-decode-encoded-word-methods-cache))
1362     (pop xlist)
1363     (while xlist
1364       (setq string (funcall (pop xlist) string))))
1365   string)
1366
1367 ;; Subject simplification.
1368
1369 (defun gnus-simplify-whitespace (str)
1370   "Remove excessive whitespace from STR."
1371   (let ((mystr str))
1372     ;; Multiple spaces.
1373     (while (string-match "[ \t][ \t]+" mystr)
1374       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1375                           " "
1376                           (substring mystr (match-end 0)))))
1377     ;; Leading spaces.
1378     (when (string-match "^[ \t]+" mystr)
1379       (setq mystr (substring mystr (match-end 0))))
1380     ;; Trailing spaces.
1381     (when (string-match "[ \t]+$" mystr)
1382       (setq mystr (substring mystr 0 (match-beginning 0))))
1383     mystr))
1384
1385 (defun gnus-simplify-all-whitespace (str)
1386   "Remove all whitespace from STR."
1387   (let ((mystr str))
1388     (while (string-match "[ \t\n]+" mystr)
1389       (setq mystr (replace-match "" nil nil mystr)))
1390     mystr))
1391
1392 (defsubst gnus-simplify-subject-re (subject)
1393   "Remove \"Re:\" from subject lines."
1394   (if (string-match message-subject-re-regexp subject)
1395       (substring subject (match-end 0))
1396     subject))
1397
1398 (defun gnus-simplify-subject (subject &optional re-only)
1399   "Remove `Re:' and words in parentheses.
1400 If RE-ONLY is non-nil, strip leading `Re:'s only."
1401   (let ((case-fold-search t))           ;Ignore case.
1402     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1403     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1404       (setq subject (substring subject (match-end 0))))
1405     ;; Remove uninteresting prefixes.
1406     (when (and (not re-only)
1407                gnus-simplify-ignored-prefixes
1408                (string-match gnus-simplify-ignored-prefixes subject))
1409       (setq subject (substring subject (match-end 0))))
1410     ;; Remove words in parentheses from end.
1411     (unless re-only
1412       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1413         (setq subject (substring subject 0 (match-beginning 0)))))
1414     ;; Return subject string.
1415     subject))
1416
1417 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1418 ;; all whitespace.
1419 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1420   (goto-char (point-min))
1421   (while (re-search-forward regexp nil t)
1422     (replace-match (or newtext ""))))
1423
1424 (defun gnus-simplify-buffer-fuzzy ()
1425   "Simplify string in the buffer fuzzily.
1426 The string in the accessible portion of the current buffer is simplified.
1427 It is assumed to be a single-line subject.
1428 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1429 matter is removed.  Additional things can be deleted by setting
1430 `gnus-simplify-subject-fuzzy-regexp'."
1431   (let ((case-fold-search t)
1432         (modified-tick))
1433     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1434
1435     (while (not (eq modified-tick (buffer-modified-tick)))
1436       (setq modified-tick (buffer-modified-tick))
1437       (cond
1438        ((listp gnus-simplify-subject-fuzzy-regexp)
1439         (mapcar 'gnus-simplify-buffer-fuzzy-step
1440                 gnus-simplify-subject-fuzzy-regexp))
1441        (gnus-simplify-subject-fuzzy-regexp
1442         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1443       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1444       (gnus-simplify-buffer-fuzzy-step
1445        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1446       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1447
1448     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1449     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1450     (gnus-simplify-buffer-fuzzy-step " $")
1451     (gnus-simplify-buffer-fuzzy-step "^ +")))
1452
1453 (defun gnus-simplify-subject-fuzzy (subject)
1454   "Simplify a subject string fuzzily.
1455 See `gnus-simplify-buffer-fuzzy' for details."
1456   (save-excursion
1457     (gnus-set-work-buffer)
1458     (let ((case-fold-search t))
1459       ;; Remove uninteresting prefixes.
1460       (when (and gnus-simplify-ignored-prefixes
1461                  (string-match gnus-simplify-ignored-prefixes subject))
1462         (setq subject (substring subject (match-end 0))))
1463       (insert subject)
1464       (inline (gnus-simplify-buffer-fuzzy))
1465       (buffer-string))))
1466
1467 (defsubst gnus-simplify-subject-fully (subject)
1468   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1469   (cond
1470    (gnus-simplify-subject-functions
1471     (gnus-map-function gnus-simplify-subject-functions subject))
1472    ((null gnus-summary-gather-subject-limit)
1473     (gnus-simplify-subject-re subject))
1474    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1475     (gnus-simplify-subject-fuzzy subject))
1476    ((numberp gnus-summary-gather-subject-limit)
1477     (gnus-limit-string (gnus-simplify-subject-re subject)
1478                        gnus-summary-gather-subject-limit))
1479    (t
1480     subject)))
1481
1482 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1483   "Check whether two subjects are equal.
1484 If optional argument simple-first is t, first argument is already
1485 simplified."
1486   (cond
1487    ((null simple-first)
1488     (equal (gnus-simplify-subject-fully s1)
1489            (gnus-simplify-subject-fully s2)))
1490    (t
1491     (equal s1
1492            (gnus-simplify-subject-fully s2)))))
1493
1494 (defun gnus-summary-bubble-group ()
1495   "Increase the score of the current group.
1496 This is a handy function to add to `gnus-summary-exit-hook' to
1497 increase the score of each group you read."
1498   (gnus-group-add-score gnus-newsgroup-name))
1499
1500 \f
1501 ;;;
1502 ;;; Gnus summary mode
1503 ;;;
1504
1505 (put 'gnus-summary-mode 'mode-class 'special)
1506
1507 (defvar gnus-article-commands-menu)
1508
1509 (when t
1510   ;; Non-orthogonal keys
1511
1512   (gnus-define-keys gnus-summary-mode-map
1513     " " gnus-summary-next-page
1514     "\177" gnus-summary-prev-page
1515     [delete] gnus-summary-prev-page
1516     [backspace] gnus-summary-prev-page
1517     "\r" gnus-summary-scroll-up
1518     "\M-\r" gnus-summary-scroll-down
1519     "n" gnus-summary-next-unread-article
1520     "p" gnus-summary-prev-unread-article
1521     "N" gnus-summary-next-article
1522     "P" gnus-summary-prev-article
1523     "\M-\C-n" gnus-summary-next-same-subject
1524     "\M-\C-p" gnus-summary-prev-same-subject
1525     "\M-n" gnus-summary-next-unread-subject
1526     "\M-p" gnus-summary-prev-unread-subject
1527     "." gnus-summary-first-unread-article
1528     "," gnus-summary-best-unread-article
1529     "\M-s" gnus-summary-search-article-forward
1530     "\M-r" gnus-summary-search-article-backward
1531     "<" gnus-summary-beginning-of-article
1532     ">" gnus-summary-end-of-article
1533     "j" gnus-summary-goto-article
1534     "^" gnus-summary-refer-parent-article
1535     "\M-^" gnus-summary-refer-article
1536     "u" gnus-summary-tick-article-forward
1537     "!" gnus-summary-tick-article-forward
1538     "U" gnus-summary-tick-article-backward
1539     "d" gnus-summary-mark-as-read-forward
1540     "D" gnus-summary-mark-as-read-backward
1541     "E" gnus-summary-mark-as-expirable
1542     "\M-u" gnus-summary-clear-mark-forward
1543     "\M-U" gnus-summary-clear-mark-backward
1544     "k" gnus-summary-kill-same-subject-and-select
1545     "\C-k" gnus-summary-kill-same-subject
1546     "\M-\C-k" gnus-summary-kill-thread
1547     "\M-\C-l" gnus-summary-lower-thread
1548     "e" gnus-summary-edit-article
1549     "#" gnus-summary-mark-as-processable
1550     "\M-#" gnus-summary-unmark-as-processable
1551     "\M-\C-t" gnus-summary-toggle-threads
1552     "\M-\C-s" gnus-summary-show-thread
1553     "\M-\C-h" gnus-summary-hide-thread
1554     "\M-\C-f" gnus-summary-next-thread
1555     "\M-\C-b" gnus-summary-prev-thread
1556     [(meta down)] gnus-summary-next-thread
1557     [(meta up)] gnus-summary-prev-thread
1558     "\M-\C-u" gnus-summary-up-thread
1559     "\M-\C-d" gnus-summary-down-thread
1560     "&" gnus-summary-execute-command
1561     "c" gnus-summary-catchup-and-exit
1562     "\C-w" gnus-summary-mark-region-as-read
1563     "\C-t" gnus-summary-toggle-truncation
1564     "?" gnus-summary-mark-as-dormant
1565     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1566     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1567     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1568     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1569     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1570     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1571     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1572     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1573     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1574     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1575     "=" gnus-summary-expand-window
1576     "\C-x\C-s" gnus-summary-reselect-current-group
1577     "\M-g" gnus-summary-rescan-group
1578     "w" gnus-summary-stop-page-breaking
1579     "\C-c\C-r" gnus-summary-caesar-message
1580     "f" gnus-summary-followup
1581     "F" gnus-summary-followup-with-original
1582     "C" gnus-summary-cancel-article
1583     "r" gnus-summary-reply
1584     "R" gnus-summary-reply-with-original
1585     "\C-c\C-f" gnus-summary-mail-forward
1586     "o" gnus-summary-save-article
1587     "\C-o" gnus-summary-save-article-mail
1588     "|" gnus-summary-pipe-output
1589     "\M-k" gnus-summary-edit-local-kill
1590     "\M-K" gnus-summary-edit-global-kill
1591     ;; "V" gnus-version
1592     "\C-c\C-d" gnus-summary-describe-group
1593     "q" gnus-summary-exit
1594     "Q" gnus-summary-exit-no-update
1595     "\C-c\C-i" gnus-info-find-node
1596     gnus-mouse-2 gnus-mouse-pick-article
1597     "m" gnus-summary-mail-other-window
1598     "a" gnus-summary-post-news
1599     "i" gnus-summary-news-other-window
1600     "x" gnus-summary-limit-to-unread
1601     "s" gnus-summary-isearch-article
1602     "t" gnus-summary-toggle-header
1603     "g" gnus-summary-show-article
1604     "l" gnus-summary-goto-last-article
1605     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1606     "\C-d" gnus-summary-enter-digest-group
1607     "\M-\C-d" gnus-summary-read-document
1608     "\M-\C-e" gnus-summary-edit-parameters
1609     "\M-\C-a" gnus-summary-customize-parameters
1610     "\C-c\C-b" gnus-bug
1611     "*" gnus-cache-enter-article
1612     "\M-*" gnus-cache-remove-article
1613     "\M-&" gnus-summary-universal-argument
1614     "\C-l" gnus-recenter
1615     "I" gnus-summary-increase-score
1616     "L" gnus-summary-lower-score
1617     "\M-i" gnus-symbolic-argument
1618     "h" gnus-summary-select-article-buffer
1619
1620     "b" gnus-article-view-part
1621     "\M-t" gnus-summary-toggle-display-buttonized
1622
1623     "V" gnus-summary-score-map
1624     "X" gnus-uu-extract-map
1625     "S" gnus-summary-send-map)
1626
1627   ;; Sort of orthogonal keymap
1628   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1629     "t" gnus-summary-tick-article-forward
1630     "!" gnus-summary-tick-article-forward
1631     "d" gnus-summary-mark-as-read-forward
1632     "r" gnus-summary-mark-as-read-forward
1633     "c" gnus-summary-clear-mark-forward
1634     " " gnus-summary-clear-mark-forward
1635     "e" gnus-summary-mark-as-expirable
1636     "x" gnus-summary-mark-as-expirable
1637     "?" gnus-summary-mark-as-dormant
1638     "b" gnus-summary-set-bookmark
1639     "B" gnus-summary-remove-bookmark
1640     "#" gnus-summary-mark-as-processable
1641     "\M-#" gnus-summary-unmark-as-processable
1642     "S" gnus-summary-limit-include-expunged
1643     "C" gnus-summary-catchup
1644     "H" gnus-summary-catchup-to-here
1645     "h" gnus-summary-catchup-from-here
1646     "\C-c" gnus-summary-catchup-all
1647     "k" gnus-summary-kill-same-subject-and-select
1648     "K" gnus-summary-kill-same-subject
1649     "P" gnus-uu-mark-map)
1650
1651   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1652     "c" gnus-summary-clear-above
1653     "u" gnus-summary-tick-above
1654     "m" gnus-summary-mark-above
1655     "k" gnus-summary-kill-below)
1656
1657   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1658     "/" gnus-summary-limit-to-subject
1659     "n" gnus-summary-limit-to-articles
1660     "w" gnus-summary-pop-limit
1661     "s" gnus-summary-limit-to-subject
1662     "a" gnus-summary-limit-to-author
1663     "u" gnus-summary-limit-to-unread
1664     "m" gnus-summary-limit-to-marks
1665     "M" gnus-summary-limit-exclude-marks
1666     "v" gnus-summary-limit-to-score
1667     "*" gnus-summary-limit-include-cached
1668     "D" gnus-summary-limit-include-dormant
1669     "T" gnus-summary-limit-include-thread
1670     "d" gnus-summary-limit-exclude-dormant
1671     "t" gnus-summary-limit-to-age
1672     "." gnus-summary-limit-to-unseen
1673     "x" gnus-summary-limit-to-extra
1674     "p" gnus-summary-limit-to-display-predicate
1675     "E" gnus-summary-limit-include-expunged
1676     "c" gnus-summary-limit-exclude-childless-dormant
1677     "C" gnus-summary-limit-mark-excluded-as-read
1678     "o" gnus-summary-insert-old-articles
1679     "N" gnus-summary-insert-new-articles)
1680
1681   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1682     "n" gnus-summary-next-unread-article
1683     "p" gnus-summary-prev-unread-article
1684     "N" gnus-summary-next-article
1685     "P" gnus-summary-prev-article
1686     "\C-n" gnus-summary-next-same-subject
1687     "\C-p" gnus-summary-prev-same-subject
1688     "\M-n" gnus-summary-next-unread-subject
1689     "\M-p" gnus-summary-prev-unread-subject
1690     "f" gnus-summary-first-unread-article
1691     "b" gnus-summary-best-unread-article
1692     "j" gnus-summary-goto-article
1693     "g" gnus-summary-goto-subject
1694     "l" gnus-summary-goto-last-article
1695     "o" gnus-summary-pop-article)
1696
1697   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1698     "k" gnus-summary-kill-thread
1699     "l" gnus-summary-lower-thread
1700     "i" gnus-summary-raise-thread
1701     "T" gnus-summary-toggle-threads
1702     "t" gnus-summary-rethread-current
1703     "^" gnus-summary-reparent-thread
1704     "s" gnus-summary-show-thread
1705     "S" gnus-summary-show-all-threads
1706     "h" gnus-summary-hide-thread
1707     "H" gnus-summary-hide-all-threads
1708     "n" gnus-summary-next-thread
1709     "p" gnus-summary-prev-thread
1710     "u" gnus-summary-up-thread
1711     "o" gnus-summary-top-thread
1712     "d" gnus-summary-down-thread
1713     "#" gnus-uu-mark-thread
1714     "\M-#" gnus-uu-unmark-thread)
1715
1716   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1717     "g" gnus-summary-prepare
1718     "c" gnus-summary-insert-cached-articles
1719     "d" gnus-summary-insert-dormant-articles)
1720
1721   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1722     "c" gnus-summary-catchup-and-exit
1723     "C" gnus-summary-catchup-all-and-exit
1724     "E" gnus-summary-exit-no-update
1725     "Q" gnus-summary-exit
1726     "Z" gnus-summary-exit
1727     "n" gnus-summary-catchup-and-goto-next-group
1728     "R" gnus-summary-reselect-current-group
1729     "G" gnus-summary-rescan-group
1730     "N" gnus-summary-next-group
1731     "s" gnus-summary-save-newsrc
1732     "P" gnus-summary-prev-group)
1733
1734   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1735     " " gnus-summary-next-page
1736     "n" gnus-summary-next-page
1737     "\177" gnus-summary-prev-page
1738     [delete] gnus-summary-prev-page
1739     "p" gnus-summary-prev-page
1740     "\r" gnus-summary-scroll-up
1741     "\M-\r" gnus-summary-scroll-down
1742     "<" gnus-summary-beginning-of-article
1743     ">" gnus-summary-end-of-article
1744     "b" gnus-summary-beginning-of-article
1745     "e" gnus-summary-end-of-article
1746     "^" gnus-summary-refer-parent-article
1747     "r" gnus-summary-refer-parent-article
1748     "D" gnus-summary-enter-digest-group
1749     "R" gnus-summary-refer-references
1750     "T" gnus-summary-refer-thread
1751     "g" gnus-summary-show-article
1752     "s" gnus-summary-isearch-article
1753     "P" gnus-summary-print-article
1754     "M" gnus-mailing-list-insinuate
1755     "t" gnus-article-babel)
1756
1757   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1758     "b" gnus-article-add-buttons
1759     "B" gnus-article-add-buttons-to-head
1760     "o" gnus-article-treat-overstrike
1761     "e" gnus-article-emphasize
1762     "w" gnus-article-fill-cited-article
1763     "Q" gnus-article-fill-long-lines
1764     "C" gnus-article-capitalize-sentences
1765     "c" gnus-article-remove-cr
1766     "q" gnus-article-de-quoted-unreadable
1767     "6" gnus-article-de-base64-unreadable
1768     "Z" gnus-article-decode-HZ
1769     "h" gnus-article-wash-html
1770     "u" gnus-article-unsplit-urls
1771     "s" gnus-summary-force-verify-and-decrypt
1772     "f" gnus-article-display-x-face
1773     "l" gnus-summary-stop-page-breaking
1774     "r" gnus-summary-caesar-message
1775     "m" gnus-summary-morse-message
1776     "t" gnus-summary-toggle-header
1777     "g" gnus-treat-smiley
1778     "v" gnus-summary-verbose-headers
1779     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1780     "p" gnus-article-verify-x-pgp-sig
1781     "d" gnus-article-treat-dumbquotes
1782     "k" gnus-article-outlook-deuglify-article)
1783
1784   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1785     "a" gnus-article-hide
1786     "h" gnus-article-hide-headers
1787     "b" gnus-article-hide-boring-headers
1788     "s" gnus-article-hide-signature
1789     "c" gnus-article-hide-citation
1790     "C" gnus-article-hide-citation-in-followups
1791     "l" gnus-article-hide-list-identifiers
1792     "p" gnus-article-hide-pgp
1793     "B" gnus-article-strip-banner
1794     "P" gnus-article-hide-pem
1795     "\C-c" gnus-article-hide-citation-maybe)
1796
1797   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1798     "a" gnus-article-highlight
1799     "h" gnus-article-highlight-headers
1800     "c" gnus-article-highlight-citation
1801     "s" gnus-article-highlight-signature)
1802
1803   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1804     "f" gnus-article-treat-fold-headers
1805     "u" gnus-article-treat-unfold-headers
1806     "n" gnus-article-treat-fold-newsgroups)
1807
1808   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1809     "x" gnus-article-display-x-face
1810     "s" gnus-treat-smiley
1811     "D" gnus-article-remove-images
1812     "f" gnus-treat-from-picon
1813     "m" gnus-treat-mail-picon
1814     "n" gnus-treat-newsgroups-picon)
1815
1816   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1817     "w" gnus-article-decode-mime-words
1818     "c" gnus-article-decode-charset
1819     "v" gnus-mime-view-all-parts
1820     "b" gnus-article-view-part)
1821
1822   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1823     "z" gnus-article-date-ut
1824     "u" gnus-article-date-ut
1825     "l" gnus-article-date-local
1826     "p" gnus-article-date-english
1827     "e" gnus-article-date-lapsed
1828     "o" gnus-article-date-original
1829     "i" gnus-article-date-iso8601
1830     "s" gnus-article-date-user)
1831
1832   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1833     "t" gnus-article-remove-trailing-blank-lines
1834     "l" gnus-article-strip-leading-blank-lines
1835     "m" gnus-article-strip-multiple-blank-lines
1836     "a" gnus-article-strip-blank-lines
1837     "A" gnus-article-strip-all-blank-lines
1838     "s" gnus-article-strip-leading-space
1839     "e" gnus-article-strip-trailing-space
1840     "w" gnus-article-remove-leading-whitespace)
1841
1842   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1843     "v" gnus-version
1844     "f" gnus-summary-fetch-faq
1845     "d" gnus-summary-describe-group
1846     "h" gnus-summary-describe-briefly
1847     "i" gnus-info-find-node
1848     "c" gnus-group-fetch-charter
1849     "C" gnus-group-fetch-control)
1850
1851   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1852     "e" gnus-summary-expire-articles
1853     "\M-\C-e" gnus-summary-expire-articles-now
1854     "\177" gnus-summary-delete-article
1855     [delete] gnus-summary-delete-article
1856     [backspace] gnus-summary-delete-article
1857     "m" gnus-summary-move-article
1858     "r" gnus-summary-respool-article
1859     "w" gnus-summary-edit-article
1860     "c" gnus-summary-copy-article
1861     "B" gnus-summary-crosspost-article
1862     "q" gnus-summary-respool-query
1863     "t" gnus-summary-respool-trace
1864     "i" gnus-summary-import-article
1865     "I" gnus-summary-create-article
1866     "p" gnus-summary-article-posted-p)
1867
1868   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1869     "o" gnus-summary-save-article
1870     "m" gnus-summary-save-article-mail
1871     "F" gnus-summary-write-article-file
1872     "r" gnus-summary-save-article-rmail
1873     "f" gnus-summary-save-article-file
1874     "b" gnus-summary-save-article-body-file
1875     "h" gnus-summary-save-article-folder
1876     "v" gnus-summary-save-article-vm
1877     "p" gnus-summary-pipe-output
1878     "P" gnus-summary-muttprint
1879     "s" gnus-soup-add-article)
1880
1881   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1882     "b" gnus-summary-display-buttonized
1883     "m" gnus-summary-repair-multipart
1884     "v" gnus-article-view-part
1885     "o" gnus-article-save-part
1886     "c" gnus-article-copy-part
1887     "C" gnus-article-view-part-as-charset
1888     "e" gnus-article-view-part-externally
1889     "E" gnus-article-encrypt-body
1890     "i" gnus-article-inline-part
1891     "|" gnus-article-pipe-part)
1892
1893   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1894     "p" gnus-summary-mark-as-processable
1895     "u" gnus-summary-unmark-as-processable
1896     "U" gnus-summary-unmark-all-processable
1897     "v" gnus-uu-mark-over
1898     "s" gnus-uu-mark-series
1899     "r" gnus-uu-mark-region
1900     "g" gnus-uu-unmark-region
1901     "R" gnus-uu-mark-by-regexp
1902     "G" gnus-uu-unmark-by-regexp
1903     "t" gnus-uu-mark-thread
1904     "T" gnus-uu-unmark-thread
1905     "a" gnus-uu-mark-all
1906     "b" gnus-uu-mark-buffer
1907     "S" gnus-uu-mark-sparse
1908     "k" gnus-summary-kill-process-mark
1909     "y" gnus-summary-yank-process-mark
1910     "w" gnus-summary-save-process-mark
1911     "i" gnus-uu-invert-processable)
1912
1913   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1914     ;;"x" gnus-uu-extract-any
1915     "m" gnus-summary-save-parts
1916     "u" gnus-uu-decode-uu
1917     "U" gnus-uu-decode-uu-and-save
1918     "s" gnus-uu-decode-unshar
1919     "S" gnus-uu-decode-unshar-and-save
1920     "o" gnus-uu-decode-save
1921     "O" gnus-uu-decode-save
1922     "b" gnus-uu-decode-binhex
1923     "B" gnus-uu-decode-binhex
1924     "p" gnus-uu-decode-postscript
1925     "P" gnus-uu-decode-postscript-and-save)
1926
1927   (gnus-define-keys
1928       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1929     "u" gnus-uu-decode-uu-view
1930     "U" gnus-uu-decode-uu-and-save-view
1931     "s" gnus-uu-decode-unshar-view
1932     "S" gnus-uu-decode-unshar-and-save-view
1933     "o" gnus-uu-decode-save-view
1934     "O" gnus-uu-decode-save-view
1935     "b" gnus-uu-decode-binhex-view
1936     "B" gnus-uu-decode-binhex-view
1937     "p" gnus-uu-decode-postscript-view
1938     "P" gnus-uu-decode-postscript-and-save-view))
1939
1940 (defvar gnus-article-post-menu nil)
1941
1942 (defconst gnus-summary-menu-maxlen 20)
1943
1944 (defun gnus-summary-menu-split (menu)
1945   ;; If we have lots of elements, divide them into groups of 20
1946   ;; and make a pane (or submenu) for each one.
1947   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1948       (let ((menu menu) sublists next
1949             (i 1))
1950         (while menu
1951           ;; Pull off the next gnus-summary-menu-maxlen elements
1952           ;; and make them the next element of sublist.
1953           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1954           (if next
1955               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1956                       nil))
1957           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1958                                              (aref (car (last menu)) 0)) menu)
1959                                sublists))
1960           (setq i (1+ i))
1961           (setq menu next))
1962         (nreverse sublists))
1963     ;; Few elements--put them all in one pane.
1964     menu))
1965
1966 (defun gnus-summary-make-menu-bar ()
1967   (gnus-turn-off-edit-menu 'summary)
1968
1969   (unless (boundp 'gnus-summary-misc-menu)
1970
1971     (easy-menu-define
1972       gnus-summary-kill-menu gnus-summary-mode-map ""
1973       (cons
1974        "Score"
1975        (nconc
1976         (list
1977          ["Customize" gnus-score-customize t])
1978         (gnus-make-score-map 'increase)
1979         (gnus-make-score-map 'lower)
1980         '(("Mark"
1981            ["Kill below" gnus-summary-kill-below t]
1982            ["Mark above" gnus-summary-mark-above t]
1983            ["Tick above" gnus-summary-tick-above t]
1984            ["Clear above" gnus-summary-clear-above t])
1985           ["Current score" gnus-summary-current-score t]
1986           ["Set score" gnus-summary-set-score t]
1987           ["Switch current score file..." gnus-score-change-score-file t]
1988           ["Set mark below..." gnus-score-set-mark-below t]
1989           ["Set expunge below..." gnus-score-set-expunge-below t]
1990           ["Edit current score file" gnus-score-edit-current-scores t]
1991           ["Edit score file" gnus-score-edit-file t]
1992           ["Trace score" gnus-score-find-trace t]
1993           ["Find words" gnus-score-find-favourite-words t]
1994           ["Rescore buffer" gnus-summary-rescore t]
1995           ["Increase score..." gnus-summary-increase-score t]
1996           ["Lower score..." gnus-summary-lower-score t]))))
1997
1998     ;; Define both the Article menu in the summary buffer and the
1999     ;; equivalent Commands menu in the article buffer here for
2000     ;; consistency.
2001     (let ((innards
2002            `(("Hide"
2003               ["All" gnus-article-hide t]
2004               ["Headers" gnus-article-hide-headers t]
2005               ["Signature" gnus-article-hide-signature t]
2006               ["Citation" gnus-article-hide-citation t]
2007               ["List identifiers" gnus-article-hide-list-identifiers t]
2008               ["PGP" gnus-article-hide-pgp t]
2009               ["Banner" gnus-article-strip-banner t]
2010               ["Boring headers" gnus-article-hide-boring-headers t])
2011              ("Highlight"
2012               ["All" gnus-article-highlight t]
2013               ["Headers" gnus-article-highlight-headers t]
2014               ["Signature" gnus-article-highlight-signature t]
2015               ["Citation" gnus-article-highlight-citation t])
2016              ("MIME"
2017               ["Words" gnus-article-decode-mime-words t]
2018               ["Charset" gnus-article-decode-charset t]
2019               ["QP" gnus-article-de-quoted-unreadable t]
2020               ["Base64" gnus-article-de-base64-unreadable t]
2021               ["View MIME buttons" gnus-summary-display-buttonized t]
2022               ["View all" gnus-mime-view-all-parts t]
2023               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2024               ["Encrypt body" gnus-article-encrypt-body t]
2025               ["Extract all parts" gnus-summary-save-parts t])
2026              ("Date"
2027               ["Local" gnus-article-date-local t]
2028               ["ISO8601" gnus-article-date-iso8601 t]
2029               ["UT" gnus-article-date-ut t]
2030               ["Original" gnus-article-date-original t]
2031               ["Lapsed" gnus-article-date-lapsed t]
2032               ["User-defined" gnus-article-date-user t])
2033              ("Display"
2034               ["Remove images" gnus-article-remove-images t]
2035               ["Toggle smiley" gnus-treat-smiley t]
2036               ["Show X-Face" gnus-article-display-x-face t]
2037               ["Show picons in From" gnus-treat-from-picon t]
2038               ["Show picons in mail headers" gnus-treat-mail-picon t]
2039               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2040               ("View as different encoding"
2041                ,@(gnus-summary-menu-split
2042                   (mapcar
2043                    (lambda (cs)
2044                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2045                      ;; forms for menu commands, we should provide intern'ed
2046                      ;; function symbols.
2047                      (let ((command (intern (format "\
2048 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2049                        (fset command
2050                              `(lambda ()
2051                                 (interactive)
2052                                 (let ((gnus-summary-show-article-charset-alist
2053                                        '((1 . ,cs))))
2054                                   (gnus-summary-show-article 1))))
2055                        `[,(symbol-name cs) ,command t]))
2056                    (sort (if (fboundp 'coding-system-list)
2057                              (coding-system-list)
2058                            (mapcar 'car mm-mime-mule-charset-alist))
2059                          'string<)))))
2060              ("Washing"
2061               ("Remove Blanks"
2062                ["Leading" gnus-article-strip-leading-blank-lines t]
2063                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2064                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2065                ["All of the above" gnus-article-strip-blank-lines t]
2066                ["All" gnus-article-strip-all-blank-lines t]
2067                ["Leading space" gnus-article-strip-leading-space t]
2068                ["Trailing space" gnus-article-strip-trailing-space t]
2069                ["Leading space in headers"
2070                 gnus-article-remove-leading-whitespace t])
2071               ["Overstrike" gnus-article-treat-overstrike t]
2072               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2073               ["Emphasis" gnus-article-emphasize t]
2074               ["Word wrap" gnus-article-fill-cited-article t]
2075               ["Fill long lines" gnus-article-fill-long-lines t]
2076               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2077               ["CR" gnus-article-remove-cr t]
2078               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2079               ["Base64" gnus-article-de-base64-unreadable t]
2080               ["Rot 13" gnus-summary-caesar-message
2081                ,@(if (featurep 'xemacs) '(t)
2082                    '(:help "\"Caesar rotate\" article by 13"))]
2083               ["Morse decode" gnus-summary-morse-message t]
2084               ["Unix pipe..." gnus-summary-pipe-message t]
2085               ["Add buttons" gnus-article-add-buttons t]
2086               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2087               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2088               ["Verbose header" gnus-summary-verbose-headers t]
2089               ["Toggle header" gnus-summary-toggle-header t]
2090               ["Unfold headers" gnus-article-treat-unfold-headers t]
2091               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2092               ["Html" gnus-article-wash-html t]
2093               ["URLs" gnus-article-unsplit-urls t]
2094               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2095               ["HZ" gnus-article-decode-HZ t]
2096               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2097               )
2098              ("Output"
2099               ["Save in default format" gnus-summary-save-article
2100                ,@(if (featurep 'xemacs) '(t)
2101                    '(:help "Save article using default method"))]
2102               ["Save in file" gnus-summary-save-article-file
2103                ,@(if (featurep 'xemacs) '(t)
2104                    '(:help "Save article in file"))]
2105               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2106               ["Save in MH folder" gnus-summary-save-article-folder t]
2107               ["Save in VM folder" gnus-summary-save-article-vm t]
2108               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2109               ["Save body in file" gnus-summary-save-article-body-file t]
2110               ["Pipe through a filter" gnus-summary-pipe-output t]
2111               ["Add to SOUP packet" gnus-soup-add-article t]
2112               ["Print with Muttprint" gnus-summary-muttprint t]
2113               ["Print" gnus-summary-print-article t])
2114              ("Backend"
2115               ["Respool article..." gnus-summary-respool-article t]
2116               ["Move article..." gnus-summary-move-article
2117                (gnus-check-backend-function
2118                 'request-move-article gnus-newsgroup-name)]
2119               ["Copy article..." gnus-summary-copy-article t]
2120               ["Crosspost article..." gnus-summary-crosspost-article
2121                (gnus-check-backend-function
2122                 'request-replace-article gnus-newsgroup-name)]
2123               ["Import file..." gnus-summary-import-article t]
2124               ["Create article..." gnus-summary-create-article t]
2125               ["Check if posted" gnus-summary-article-posted-p t]
2126               ["Edit article" gnus-summary-edit-article
2127                (not (gnus-group-read-only-p))]
2128               ["Delete article" gnus-summary-delete-article
2129                (gnus-check-backend-function
2130                 'request-expire-articles gnus-newsgroup-name)]
2131               ["Query respool" gnus-summary-respool-query t]
2132               ["Trace respool" gnus-summary-respool-trace t]
2133               ["Delete expirable articles" gnus-summary-expire-articles-now
2134                (gnus-check-backend-function
2135                 'request-expire-articles gnus-newsgroup-name)])
2136              ("Extract"
2137               ["Uudecode" gnus-uu-decode-uu
2138                ,@(if (featurep 'xemacs) '(t)
2139                    '(:help "Decode uuencoded article(s)"))]
2140               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2141               ["Unshar" gnus-uu-decode-unshar t]
2142               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2143               ["Save" gnus-uu-decode-save t]
2144               ["Binhex" gnus-uu-decode-binhex t]
2145               ["Postscript" gnus-uu-decode-postscript t]
2146               ["All MIME parts" gnus-summary-save-parts t])
2147              ("Cache"
2148               ["Enter article" gnus-cache-enter-article t]
2149               ["Remove article" gnus-cache-remove-article t])
2150              ["Translate" gnus-article-babel t]
2151              ["Select article buffer" gnus-summary-select-article-buffer t]
2152              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2153              ["Isearch article..." gnus-summary-isearch-article t]
2154              ["Beginning of the article" gnus-summary-beginning-of-article t]
2155              ["End of the article" gnus-summary-end-of-article t]
2156              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2157              ["Fetch referenced articles" gnus-summary-refer-references t]
2158              ["Fetch current thread" gnus-summary-refer-thread t]
2159              ["Fetch article with id..." gnus-summary-refer-article t]
2160              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2161              ["Redisplay" gnus-summary-show-article t]
2162              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2163       (easy-menu-define
2164         gnus-summary-article-menu gnus-summary-mode-map ""
2165         (cons "Article" innards))
2166
2167       (if (not (keymapp gnus-summary-article-menu))
2168           (easy-menu-define
2169             gnus-article-commands-menu gnus-article-mode-map ""
2170             (cons "Commands" innards))
2171         ;; in Emacs, don't share menu.
2172         (setq gnus-article-commands-menu
2173               (copy-keymap gnus-summary-article-menu))
2174         (define-key gnus-article-mode-map [menu-bar commands]
2175           (cons "Commands" gnus-article-commands-menu))))
2176
2177     (easy-menu-define
2178       gnus-summary-thread-menu gnus-summary-mode-map ""
2179       '("Threads"
2180         ["Toggle threading" gnus-summary-toggle-threads t]
2181         ["Hide threads" gnus-summary-hide-all-threads t]
2182         ["Show threads" gnus-summary-show-all-threads t]
2183         ["Hide thread" gnus-summary-hide-thread t]
2184         ["Show thread" gnus-summary-show-thread t]
2185         ["Go to next thread" gnus-summary-next-thread t]
2186         ["Go to previous thread" gnus-summary-prev-thread t]
2187         ["Go down thread" gnus-summary-down-thread t]
2188         ["Go up thread" gnus-summary-up-thread t]
2189         ["Top of thread" gnus-summary-top-thread t]
2190         ["Mark thread as read" gnus-summary-kill-thread t]
2191         ["Lower thread score" gnus-summary-lower-thread t]
2192         ["Raise thread score" gnus-summary-raise-thread t]
2193         ["Rethread current" gnus-summary-rethread-current t]))
2194
2195     (easy-menu-define
2196       gnus-summary-post-menu gnus-summary-mode-map ""
2197       `("Post"
2198         ["Send a message (mail or news)" gnus-summary-post-news
2199          ,@(if (featurep 'xemacs) '(t)
2200              '(:help "Post an article"))]
2201         ["Followup" gnus-summary-followup
2202          ,@(if (featurep 'xemacs) '(t)
2203              '(:help "Post followup to this article"))]
2204         ["Followup and yank" gnus-summary-followup-with-original
2205          ,@(if (featurep 'xemacs) '(t)
2206              '(:help "Post followup to this article, quoting its contents"))]
2207         ["Supersede article" gnus-summary-supersede-article t]
2208         ["Cancel article" gnus-summary-cancel-article
2209          ,@(if (featurep 'xemacs) '(t)
2210              '(:help "Cancel an article you posted"))]
2211         ["Reply" gnus-summary-reply t]
2212         ["Reply and yank" gnus-summary-reply-with-original t]
2213         ["Wide reply" gnus-summary-wide-reply t]
2214         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2215          ,@(if (featurep 'xemacs) '(t)
2216              '(:help "Mail a reply, quoting this article"))]
2217         ["Very wide reply" gnus-summary-very-wide-reply t]
2218         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2219          ,@(if (featurep 'xemacs) '(t)
2220              '(:help "Mail a very wide reply, quoting this article"))]
2221         ["Mail forward" gnus-summary-mail-forward t]
2222         ["Post forward" gnus-summary-post-forward t]
2223         ["Digest and mail" gnus-uu-digest-mail-forward t]
2224         ["Digest and post" gnus-uu-digest-post-forward t]
2225         ["Resend message" gnus-summary-resend-message t]
2226         ["Resend message edit" gnus-summary-resend-message-edit t]
2227         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2228         ["Send a mail" gnus-summary-mail-other-window t]
2229         ["Create a local message" gnus-summary-news-other-window t]
2230         ["Uuencode and post" gnus-uu-post-news
2231          ,@(if (featurep 'xemacs) '(t)
2232              '(:help "Post a uuencoded article"))]
2233         ["Followup via news" gnus-summary-followup-to-mail t]
2234         ["Followup via news and yank"
2235          gnus-summary-followup-to-mail-with-original t]
2236         ;;("Draft"
2237         ;;["Send" gnus-summary-send-draft t]
2238         ;;["Send bounced" gnus-resend-bounced-mail t])
2239         ))
2240
2241     (cond
2242      ((not (keymapp gnus-summary-post-menu))
2243       (setq gnus-article-post-menu gnus-summary-post-menu))
2244      ((not gnus-article-post-menu)
2245       ;; Don't share post menu.
2246       (setq gnus-article-post-menu
2247             (copy-keymap gnus-summary-post-menu))))
2248     (define-key gnus-article-mode-map [menu-bar post]
2249       (cons "Post" gnus-article-post-menu))
2250
2251     (easy-menu-define
2252       gnus-summary-misc-menu gnus-summary-mode-map ""
2253       `("Gnus"
2254         ("Mark Read"
2255          ["Mark as read" gnus-summary-mark-as-read-forward t]
2256          ["Mark same subject and select"
2257           gnus-summary-kill-same-subject-and-select t]
2258          ["Mark same subject" gnus-summary-kill-same-subject t]
2259          ["Catchup" gnus-summary-catchup
2260           ,@(if (featurep 'xemacs) '(t)
2261               '(:help "Mark unread articles in this group as read"))]
2262          ["Catchup all" gnus-summary-catchup-all t]
2263          ["Catchup to here" gnus-summary-catchup-to-here t]
2264          ["Catchup from here" gnus-summary-catchup-from-here t]
2265          ["Catchup region" gnus-summary-mark-region-as-read t]
2266          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2267         ("Mark Various"
2268          ["Tick" gnus-summary-tick-article-forward t]
2269          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2270          ["Remove marks" gnus-summary-clear-mark-forward t]
2271          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2272          ["Set bookmark" gnus-summary-set-bookmark t]
2273          ["Remove bookmark" gnus-summary-remove-bookmark t])
2274         ("Limit to"
2275          ["Marks..." gnus-summary-limit-to-marks t]
2276          ["Subject..." gnus-summary-limit-to-subject t]
2277          ["Author..." gnus-summary-limit-to-author t]
2278          ["Age..." gnus-summary-limit-to-age t]
2279          ["Extra..." gnus-summary-limit-to-extra t]
2280          ["Score..." gnus-summary-limit-to-score t]
2281          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2282          ["Unread" gnus-summary-limit-to-unread t]
2283          ["Unseen" gnus-summary-limit-to-unseen t]
2284          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2285          ["Next articles" gnus-summary-limit-to-articles t]
2286          ["Pop limit" gnus-summary-pop-limit t]
2287          ["Show dormant" gnus-summary-limit-include-dormant t]
2288          ["Hide childless dormant"
2289           gnus-summary-limit-exclude-childless-dormant t]
2290          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2291          ["Hide marked" gnus-summary-limit-exclude-marks t]
2292          ["Show expunged" gnus-summary-limit-include-expunged t])
2293         ("Process Mark"
2294          ["Set mark" gnus-summary-mark-as-processable t]
2295          ["Remove mark" gnus-summary-unmark-as-processable t]
2296          ["Remove all marks" gnus-summary-unmark-all-processable t]
2297          ["Mark above" gnus-uu-mark-over t]
2298          ["Mark series" gnus-uu-mark-series t]
2299          ["Mark region" gnus-uu-mark-region t]
2300          ["Unmark region" gnus-uu-unmark-region t]
2301          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2302          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2303          ["Mark all" gnus-uu-mark-all t]
2304          ["Mark buffer" gnus-uu-mark-buffer t]
2305          ["Mark sparse" gnus-uu-mark-sparse t]
2306          ["Mark thread" gnus-uu-mark-thread t]
2307          ["Unmark thread" gnus-uu-unmark-thread t]
2308          ("Process Mark Sets"
2309           ["Kill" gnus-summary-kill-process-mark t]
2310           ["Yank" gnus-summary-yank-process-mark
2311            gnus-newsgroup-process-stack]
2312           ["Save" gnus-summary-save-process-mark t]))
2313         ("Scroll article"
2314          ["Page forward" gnus-summary-next-page
2315           ,@(if (featurep 'xemacs) '(t)
2316               '(:help "Show next page of article"))]
2317          ["Page backward" gnus-summary-prev-page
2318           ,@(if (featurep 'xemacs) '(t)
2319               '(:help "Show previous page of article"))]
2320          ["Line forward" gnus-summary-scroll-up t])
2321         ("Move"
2322          ["Next unread article" gnus-summary-next-unread-article t]
2323          ["Previous unread article" gnus-summary-prev-unread-article t]
2324          ["Next article" gnus-summary-next-article t]
2325          ["Previous article" gnus-summary-prev-article t]
2326          ["Next unread subject" gnus-summary-next-unread-subject t]
2327          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2328          ["Next article same subject" gnus-summary-next-same-subject t]
2329          ["Previous article same subject" gnus-summary-prev-same-subject t]
2330          ["First unread article" gnus-summary-first-unread-article t]
2331          ["Best unread article" gnus-summary-best-unread-article t]
2332          ["Go to subject number..." gnus-summary-goto-subject t]
2333          ["Go to article number..." gnus-summary-goto-article t]
2334          ["Go to the last article" gnus-summary-goto-last-article t]
2335          ["Pop article off history" gnus-summary-pop-article t])
2336         ("Sort"
2337          ["Sort by number" gnus-summary-sort-by-number t]
2338          ["Sort by author" gnus-summary-sort-by-author t]
2339          ["Sort by subject" gnus-summary-sort-by-subject t]
2340          ["Sort by date" gnus-summary-sort-by-date t]
2341          ["Sort by score" gnus-summary-sort-by-score t]
2342          ["Sort by lines" gnus-summary-sort-by-lines t]
2343          ["Sort by characters" gnus-summary-sort-by-chars t]
2344          ["Randomize" gnus-summary-sort-by-random t]
2345          ["Original sort" gnus-summary-sort-by-original t])
2346         ("Help"
2347          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2348          ["Describe group" gnus-summary-describe-group t]
2349          ["Fetch charter" gnus-group-fetch-charter
2350           ,@(if (featurep 'xemacs) nil
2351               '(:help "Display the charter of the current group"))]
2352          ["Fetch control message" gnus-group-fetch-control
2353           ,@(if (featurep 'xemacs) nil
2354               '(:help "Display the archived control message for the current group"))]
2355          ["Read manual" gnus-info-find-node t])
2356         ("Modes"
2357          ["Pick and read" gnus-pick-mode t]
2358          ["Binary" gnus-binary-mode t])
2359         ("Regeneration"
2360          ["Regenerate" gnus-summary-prepare t]
2361          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2362          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2363          ["Toggle threading" gnus-summary-toggle-threads t])
2364         ["See old articles" gnus-summary-insert-old-articles t]
2365         ["See new articles" gnus-summary-insert-new-articles t]
2366         ["Filter articles..." gnus-summary-execute-command t]
2367         ["Run command on subjects..." gnus-summary-universal-argument t]
2368         ["Search articles forward..." gnus-summary-search-article-forward t]
2369         ["Search articles backward..." gnus-summary-search-article-backward t]
2370         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2371         ["Expand window" gnus-summary-expand-window t]
2372         ["Expire expirable articles" gnus-summary-expire-articles
2373          (gnus-check-backend-function
2374           'request-expire-articles gnus-newsgroup-name)]
2375         ["Edit local kill file" gnus-summary-edit-local-kill t]
2376         ["Edit main kill file" gnus-summary-edit-global-kill t]
2377         ["Edit group parameters" gnus-summary-edit-parameters t]
2378         ["Customize group parameters" gnus-summary-customize-parameters t]
2379         ["Send a bug report" gnus-bug t]
2380         ("Exit"
2381          ["Catchup and exit" gnus-summary-catchup-and-exit
2382           ,@(if (featurep 'xemacs) '(t)
2383               '(:help "Mark unread articles in this group as read, then exit"))]
2384          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2385          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2386          ["Exit group" gnus-summary-exit
2387           ,@(if (featurep 'xemacs) '(t)
2388               '(:help "Exit current group, return to group selection mode"))]
2389          ["Exit group without updating" gnus-summary-exit-no-update t]
2390          ["Exit and goto next group" gnus-summary-next-group t]
2391          ["Exit and goto prev group" gnus-summary-prev-group t]
2392          ["Reselect group" gnus-summary-reselect-current-group t]
2393          ["Rescan group" gnus-summary-rescan-group t]
2394          ["Update dribble" gnus-summary-save-newsrc t])))
2395
2396     (gnus-run-hooks 'gnus-summary-menu-hook)))
2397
2398 (defvar gnus-summary-tool-bar-map nil)
2399
2400 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2401 (defun gnus-summary-make-tool-bar ()
2402   (if (and (fboundp 'tool-bar-add-item-from-menu)
2403            (default-value 'tool-bar-mode)
2404            (not gnus-summary-tool-bar-map))
2405       (setq gnus-summary-tool-bar-map
2406             (let ((tool-bar-map (make-sparse-keymap))
2407                   (load-path (mm-image-load-path)))
2408               (tool-bar-add-item-from-menu
2409                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2410               (tool-bar-add-item-from-menu
2411                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2412               (tool-bar-add-item-from-menu
2413                'gnus-summary-post-news "post" gnus-summary-mode-map)
2414               (tool-bar-add-item-from-menu
2415                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2416               (tool-bar-add-item-from-menu
2417                'gnus-summary-followup "followup" gnus-summary-mode-map)
2418               (tool-bar-add-item-from-menu
2419                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2420               (tool-bar-add-item-from-menu
2421                'gnus-summary-reply "reply" gnus-summary-mode-map)
2422               (tool-bar-add-item-from-menu
2423                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2424               (tool-bar-add-item-from-menu
2425                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2426               (tool-bar-add-item-from-menu
2427                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2428               (tool-bar-add-item-from-menu
2429                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2430               (tool-bar-add-item-from-menu
2431                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2432               (tool-bar-add-item-from-menu
2433                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2434               (tool-bar-add-item-from-menu
2435                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2436               (tool-bar-add-item-from-menu
2437                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2438               tool-bar-map)))
2439   (if gnus-summary-tool-bar-map
2440       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2441
2442 (defun gnus-score-set-default (var value)
2443   "A version of set that updates the GNU Emacs menu-bar."
2444   (set var value)
2445   ;; It is the message that forces the active status to be updated.
2446   (message ""))
2447
2448 (defun gnus-make-score-map (type)
2449   "Make a summary score map of type TYPE."
2450   (if t
2451       nil
2452     (let ((headers '(("author" "from" string)
2453                      ("subject" "subject" string)
2454                      ("article body" "body" string)
2455                      ("article head" "head" string)
2456                      ("xref" "xref" string)
2457                      ("extra header" "extra" string)
2458                      ("lines" "lines" number)
2459                      ("followups to author" "followup" string)))
2460           (types '((number ("less than" <)
2461                            ("greater than" >)
2462                            ("equal" =))
2463                    (string ("substring" s)
2464                            ("exact string" e)
2465                            ("fuzzy string" f)
2466                            ("regexp" r))))
2467           (perms '(("temporary" (current-time-string))
2468                    ("permanent" nil)
2469                    ("immediate" now)))
2470           header)
2471       (list
2472        (apply
2473         'nconc
2474         (list
2475          (if (eq type 'lower)
2476              "Lower score"
2477            "Increase score"))
2478         (let (outh)
2479           (while headers
2480             (setq header (car headers))
2481             (setq outh
2482                   (cons
2483                    (apply
2484                     'nconc
2485                     (list (car header))
2486                     (let ((ts (cdr (assoc (nth 2 header) types)))
2487                           outt)
2488                       (while ts
2489                         (setq outt
2490                               (cons
2491                                (apply
2492                                 'nconc
2493                                 (list (caar ts))
2494                                 (let ((ps perms)
2495                                       outp)
2496                                   (while ps
2497                                     (setq outp
2498                                           (cons
2499                                            (vector
2500                                             (caar ps)
2501                                             (list
2502                                              'gnus-summary-score-entry
2503                                              (nth 1 header)
2504                                              (if (or (string= (nth 1 header)
2505                                                               "head")
2506                                                      (string= (nth 1 header)
2507                                                               "body"))
2508                                                  ""
2509                                                (list 'gnus-summary-header
2510                                                      (nth 1 header)))
2511                                              (list 'quote (nth 1 (car ts)))
2512                                              (list 'gnus-score-delta-default
2513                                                    nil)
2514                                              (nth 1 (car ps))
2515                                              t)
2516                                             t)
2517                                            outp))
2518                                     (setq ps (cdr ps)))
2519                                   (list (nreverse outp))))
2520                                outt))
2521                         (setq ts (cdr ts)))
2522                       (list (nreverse outt))))
2523                    outh))
2524             (setq headers (cdr headers)))
2525           (list (nreverse outh))))))))
2526
2527 \f
2528
2529 (defun gnus-summary-mode (&optional group)
2530   "Major mode for reading articles.
2531
2532 All normal editing commands are switched off.
2533 \\<gnus-summary-mode-map>
2534 Each line in this buffer represents one article.  To read an
2535 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2536 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2537 respectively.
2538
2539 You can also post articles and send mail from this buffer.  To
2540 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2541 of an article, type `\\[gnus-summary-reply]'.
2542
2543 There are approx. one gazillion commands you can execute in this
2544 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2545
2546 The following commands are available:
2547
2548 \\{gnus-summary-mode-map}"
2549   (interactive)
2550   (kill-all-local-variables)
2551   (when (gnus-visual-p 'summary-menu 'menu)
2552     (gnus-summary-make-menu-bar)
2553     (gnus-summary-make-tool-bar))
2554   (gnus-summary-make-local-variables)
2555   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2556     (gnus-summary-make-local-variables))
2557   (gnus-make-thread-indent-array)
2558   (gnus-simplify-mode-line)
2559   (setq major-mode 'gnus-summary-mode)
2560   (setq mode-name "Summary")
2561   (make-local-variable 'minor-mode-alist)
2562   (use-local-map gnus-summary-mode-map)
2563   (buffer-disable-undo)
2564   (setq buffer-read-only t)             ;Disable modification
2565   (setq truncate-lines t)
2566   (setq selective-display t)
2567   (setq selective-display-ellipses t)   ;Display `...'
2568   (gnus-summary-set-display-table)
2569   (gnus-set-default-directory)
2570   (setq gnus-newsgroup-name group)
2571   (make-local-variable 'gnus-summary-line-format)
2572   (make-local-variable 'gnus-summary-line-format-spec)
2573   (make-local-variable 'gnus-summary-dummy-line-format)
2574   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2575   (make-local-variable 'gnus-summary-mark-positions)
2576   (make-local-hook 'pre-command-hook)
2577   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2578   (gnus-run-hooks 'gnus-summary-mode-hook)
2579   (turn-on-gnus-mailing-list-mode)
2580   (mm-enable-multibyte)
2581   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2582   (gnus-update-summary-mark-positions))
2583
2584 (defun gnus-summary-make-local-variables ()
2585   "Make all the local summary buffer variables."
2586   (let (global)
2587     (dolist (local gnus-summary-local-variables)
2588       (if (consp local)
2589           (progn
2590             (if (eq (cdr local) 'global)
2591                 ;; Copy the global value of the variable.
2592                 (setq global (symbol-value (car local)))
2593               ;; Use the value from the list.
2594               (setq global (eval (cdr local))))
2595             (set (make-local-variable (car local)) global))
2596         ;; Simple nil-valued local variable.
2597         (set (make-local-variable local) nil)))))
2598
2599 (defun gnus-summary-clear-local-variables ()
2600   (let ((locals gnus-summary-local-variables))
2601     (while locals
2602       (if (consp (car locals))
2603           (and (vectorp (caar locals))
2604                (set (caar locals) nil))
2605         (and (vectorp (car locals))
2606              (set (car locals) nil)))
2607       (setq locals (cdr locals)))))
2608
2609 ;; Summary data functions.
2610
2611 (defmacro gnus-data-number (data)
2612   `(car ,data))
2613
2614 (defmacro gnus-data-set-number (data number)
2615   `(setcar ,data ,number))
2616
2617 (defmacro gnus-data-mark (data)
2618   `(nth 1 ,data))
2619
2620 (defmacro gnus-data-set-mark (data mark)
2621   `(setcar (nthcdr 1 ,data) ,mark))
2622
2623 (defmacro gnus-data-pos (data)
2624   `(nth 2 ,data))
2625
2626 (defmacro gnus-data-set-pos (data pos)
2627   `(setcar (nthcdr 2 ,data) ,pos))
2628
2629 (defmacro gnus-data-header (data)
2630   `(nth 3 ,data))
2631
2632 (defmacro gnus-data-set-header (data header)
2633   `(setf (nth 3 ,data) ,header))
2634
2635 (defmacro gnus-data-level (data)
2636   `(nth 4 ,data))
2637
2638 (defmacro gnus-data-unread-p (data)
2639   `(= (nth 1 ,data) gnus-unread-mark))
2640
2641 (defmacro gnus-data-read-p (data)
2642   `(/= (nth 1 ,data) gnus-unread-mark))
2643
2644 (defmacro gnus-data-pseudo-p (data)
2645   `(consp (nth 3 ,data)))
2646
2647 (defmacro gnus-data-find (number)
2648   `(assq ,number gnus-newsgroup-data))
2649
2650 (defmacro gnus-data-find-list (number &optional data)
2651   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2652      (memq (assq ,number bdata)
2653            bdata)))
2654
2655 (defmacro gnus-data-make (number mark pos header level)
2656   `(list ,number ,mark ,pos ,header ,level))
2657
2658 (defun gnus-data-enter (after-article number mark pos header level offset)
2659   (let ((data (gnus-data-find-list after-article)))
2660     (unless data
2661       (error "No such article: %d" after-article))
2662     (setcdr data (cons (gnus-data-make number mark pos header level)
2663                        (cdr data)))
2664     (setq gnus-newsgroup-data-reverse nil)
2665     (gnus-data-update-list (cddr data) offset)))
2666
2667 (defun gnus-data-enter-list (after-article list &optional offset)
2668   (when list
2669     (let ((data (and after-article (gnus-data-find-list after-article)))
2670           (ilist list))
2671       (if (not (or data
2672                    after-article))
2673           (let ((odata gnus-newsgroup-data))
2674             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2675             (when offset
2676               (gnus-data-update-list odata offset)))
2677       ;; Find the last element in the list to be spliced into the main
2678         ;; list.
2679         (while (cdr list)
2680           (setq list (cdr list)))
2681         (if (not data)
2682             (progn
2683               (setcdr list gnus-newsgroup-data)
2684               (setq gnus-newsgroup-data ilist)
2685               (when offset
2686                 (gnus-data-update-list (cdr list) offset)))
2687           (setcdr list (cdr data))
2688           (setcdr data ilist)
2689           (when offset
2690             (gnus-data-update-list (cdr list) offset))))
2691       (setq gnus-newsgroup-data-reverse nil))))
2692
2693 (defun gnus-data-remove (article &optional offset)
2694   (let ((data gnus-newsgroup-data))
2695     (if (= (gnus-data-number (car data)) article)
2696         (progn
2697           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2698                 gnus-newsgroup-data-reverse nil)
2699           (when offset
2700             (gnus-data-update-list gnus-newsgroup-data offset)))
2701       (while (cdr data)
2702         (when (= (gnus-data-number (cadr data)) article)
2703           (setcdr data (cddr data))
2704           (when offset
2705             (gnus-data-update-list (cdr data) offset))
2706           (setq data nil
2707                 gnus-newsgroup-data-reverse nil))
2708         (setq data (cdr data))))))
2709
2710 (defmacro gnus-data-list (backward)
2711   `(if ,backward
2712        (or gnus-newsgroup-data-reverse
2713            (setq gnus-newsgroup-data-reverse
2714                  (reverse gnus-newsgroup-data)))
2715      gnus-newsgroup-data))
2716
2717 (defun gnus-data-update-list (data offset)
2718   "Add OFFSET to the POS of all data entries in DATA."
2719   (setq gnus-newsgroup-data-reverse nil)
2720   (while data
2721     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2722     (setq data (cdr data))))
2723
2724 (defun gnus-summary-article-pseudo-p (article)
2725   "Say whether this article is a pseudo article or not."
2726   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2727
2728 (defmacro gnus-summary-article-sparse-p (article)
2729   "Say whether this article is a sparse article or not."
2730   `(memq ,article gnus-newsgroup-sparse))
2731
2732 (defmacro gnus-summary-article-ancient-p (article)
2733   "Say whether this article is a sparse article or not."
2734   `(memq ,article gnus-newsgroup-ancient))
2735
2736 (defun gnus-article-parent-p (number)
2737   "Say whether this article is a parent or not."
2738   (let ((data (gnus-data-find-list number)))
2739     (and (cdr data)              ; There has to be an article after...
2740          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2741             (gnus-data-level (nth 1 data))))))
2742
2743 (defun gnus-article-children (number)
2744   "Return a list of all children to NUMBER."
2745   (let* ((data (gnus-data-find-list number))
2746          (level (gnus-data-level (car data)))
2747          children)
2748     (setq data (cdr data))
2749     (while (and data
2750                 (= (gnus-data-level (car data)) (1+ level)))
2751       (push (gnus-data-number (car data)) children)
2752       (setq data (cdr data)))
2753     children))
2754
2755 (defmacro gnus-summary-skip-intangible ()
2756   "If the current article is intangible, then jump to a different article."
2757   '(let ((to (get-text-property (point) 'gnus-intangible)))
2758      (and to (gnus-summary-goto-subject to))))
2759
2760 (defmacro gnus-summary-article-intangible-p ()
2761   "Say whether this article is intangible or not."
2762   '(get-text-property (point) 'gnus-intangible))
2763
2764 (defun gnus-article-read-p (article)
2765   "Say whether ARTICLE is read or not."
2766   (not (or (memq article gnus-newsgroup-marked)
2767            (memq article gnus-newsgroup-spam-marked)
2768            (memq article gnus-newsgroup-unreads)
2769            (memq article gnus-newsgroup-unselected)
2770            (memq article gnus-newsgroup-dormant))))
2771
2772 ;; Some summary mode macros.
2773
2774 (defmacro gnus-summary-article-number ()
2775   "The article number of the article on the current line.
2776 If there isn't an article number here, then we return the current
2777 article number."
2778   '(progn
2779      (gnus-summary-skip-intangible)
2780      (or (get-text-property (point) 'gnus-number)
2781          (gnus-summary-last-subject))))
2782
2783 (defmacro gnus-summary-article-header (&optional number)
2784   "Return the header of article NUMBER."
2785   `(gnus-data-header (gnus-data-find
2786                       ,(or number '(gnus-summary-article-number)))))
2787
2788 (defmacro gnus-summary-thread-level (&optional number)
2789   "Return the level of thread that starts with article NUMBER."
2790   `(if (and (eq gnus-summary-make-false-root 'dummy)
2791             (get-text-property (point) 'gnus-intangible))
2792        0
2793      (gnus-data-level (gnus-data-find
2794                        ,(or number '(gnus-summary-article-number))))))
2795
2796 (defmacro gnus-summary-article-mark (&optional number)
2797   "Return the mark of article NUMBER."
2798   `(gnus-data-mark (gnus-data-find
2799                     ,(or number '(gnus-summary-article-number)))))
2800
2801 (defmacro gnus-summary-article-pos (&optional number)
2802   "Return the position of the line of article NUMBER."
2803   `(gnus-data-pos (gnus-data-find
2804                    ,(or number '(gnus-summary-article-number)))))
2805
2806 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2807 (defmacro gnus-summary-article-subject (&optional number)
2808   "Return current subject string or nil if nothing."
2809   `(let ((headers
2810           ,(if number
2811                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2812              '(gnus-data-header (assq (gnus-summary-article-number)
2813                                       gnus-newsgroup-data)))))
2814      (and headers
2815           (vectorp headers)
2816           (mail-header-subject headers))))
2817
2818 (defmacro gnus-summary-article-score (&optional number)
2819   "Return current article score."
2820   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2821                   gnus-newsgroup-scored))
2822        gnus-summary-default-score 0))
2823
2824 (defun gnus-summary-article-children (&optional number)
2825   "Return a list of article numbers that are children of article NUMBER."
2826   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2827          (level (gnus-data-level (car data)))
2828          l children)
2829     (while (and (setq data (cdr data))
2830                 (> (setq l (gnus-data-level (car data))) level))
2831       (and (= (1+ level) l)
2832            (push (gnus-data-number (car data))
2833                  children)))
2834     (nreverse children)))
2835
2836 (defun gnus-summary-article-parent (&optional number)
2837   "Return the article number of the parent of article NUMBER."
2838   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2839                                     (gnus-data-list t)))
2840          (level (gnus-data-level (car data))))
2841     (if (zerop level)
2842         ()                              ; This is a root.
2843       ;; We search until we find an article with a level less than
2844       ;; this one.  That function has to be the parent.
2845       (while (and (setq data (cdr data))
2846                   (not (< (gnus-data-level (car data)) level))))
2847       (and data (gnus-data-number (car data))))))
2848
2849 (defun gnus-unread-mark-p (mark)
2850   "Say whether MARK is the unread mark."
2851   (= mark gnus-unread-mark))
2852
2853 (defun gnus-read-mark-p (mark)
2854   "Say whether MARK is one of the marks that mark as read.
2855 This is all marks except unread, ticked, dormant, and expirable."
2856   (not (or (= mark gnus-unread-mark)
2857            (= mark gnus-ticked-mark)
2858            (= mark gnus-dormant-mark)
2859            (= mark gnus-expirable-mark))))
2860
2861 (defmacro gnus-article-mark (number)
2862   "Return the MARK of article NUMBER.
2863 This macro should only be used when computing the mark the \"first\"
2864 time; i.e., when generating the summary lines.  After that,
2865 `gnus-summary-article-mark' should be used to examine the
2866 marks of articles."
2867   `(cond
2868     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2869     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2870     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2871     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2872     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2873     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2874     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2875     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2876            gnus-ancient-mark))))
2877
2878 ;; Saving hidden threads.
2879
2880 (defmacro gnus-save-hidden-threads (&rest forms)
2881   "Save hidden threads, eval FORMS, and restore the hidden threads."
2882   (let ((config (make-symbol "config")))
2883     `(let ((,config (gnus-hidden-threads-configuration)))
2884        (unwind-protect
2885            (save-excursion
2886              ,@forms)
2887          (gnus-restore-hidden-threads-configuration ,config)))))
2888 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2889 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2890
2891 (defun gnus-data-compute-positions ()
2892   "Compute the positions of all articles."
2893   (setq gnus-newsgroup-data-reverse nil)
2894   (let ((data gnus-newsgroup-data))
2895     (save-excursion
2896       (gnus-save-hidden-threads
2897         (gnus-summary-show-all-threads)
2898         (goto-char (point-min))
2899         (while data
2900           (while (get-text-property (point) 'gnus-intangible)
2901             (forward-line 1))
2902           (gnus-data-set-pos (car data) (+ (point) 3))
2903           (setq data (cdr data))
2904           (forward-line 1))))))
2905
2906 (defun gnus-hidden-threads-configuration ()
2907   "Return the current hidden threads configuration."
2908   (save-excursion
2909     (let (config)
2910       (goto-char (point-min))
2911       (while (search-forward "\r" nil t)
2912         (push (1- (point)) config))
2913       config)))
2914
2915 (defun gnus-restore-hidden-threads-configuration (config)
2916   "Restore hidden threads configuration from CONFIG."
2917   (save-excursion
2918     (let (point buffer-read-only)
2919       (while (setq point (pop config))
2920         (when (and (< point (point-max))
2921                    (goto-char point)
2922                    (eq (char-after) ?\n))
2923           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2924
2925 ;; Various summary mode internalish functions.
2926
2927 (defun gnus-mouse-pick-article (e)
2928   (interactive "e")
2929   (mouse-set-point e)
2930   (gnus-summary-next-page nil t))
2931
2932 (defun gnus-summary-set-display-table ()
2933   "Change the display table.
2934 Odd characters have a tendency to mess
2935 up nicely formatted displays - we make all possible glyphs
2936 display only a single character."
2937
2938   ;; We start from the standard display table, if any.
2939   (let ((table (or (copy-sequence standard-display-table)
2940                    (make-display-table)))
2941         (i 32))
2942     ;; Nix out all the control chars...
2943     (while (>= (setq i (1- i)) 0)
2944       (aset table i [??]))
2945    ;; ... but not newline and cr, of course.  (cr is necessary for the
2946     ;; selective display).
2947     (aset table ?\n nil)
2948     (aset table ?\r nil)
2949     ;; We keep TAB as well.
2950     (aset table ?\t nil)
2951     ;; We nix out any glyphs over 126 that are not set already.
2952     (let ((i 256))
2953       (while (>= (setq i (1- i)) 127)
2954         ;; Only modify if the entry is nil.
2955         (unless (aref table i)
2956           (aset table i [??]))))
2957     (setq buffer-display-table table)))
2958
2959 (defun gnus-summary-set-article-display-arrow (pos)
2960   "Update the overlay arrow to point to line at position POS."
2961   (when (and gnus-summary-display-arrow
2962              (boundp 'overlay-arrow-position)
2963              (boundp 'overlay-arrow-string))
2964     (save-excursion
2965       (goto-char pos)
2966       (beginning-of-line)
2967       (unless overlay-arrow-position
2968         (setq overlay-arrow-position (make-marker)))
2969       (setq overlay-arrow-string "=>"
2970             overlay-arrow-position (set-marker overlay-arrow-position
2971                                                (point)
2972                                                (current-buffer))))))
2973
2974 (defun gnus-summary-buffer-name (group)
2975   "Return the summary buffer name of GROUP."
2976   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2977
2978 (defun gnus-summary-setup-buffer (group)
2979   "Initialize summary buffer."
2980   (let ((buffer (gnus-summary-buffer-name group))
2981         (dead-name (concat "*Dead Summary "
2982                            (gnus-group-decoded-name group) "*")))
2983     ;; If a dead summary buffer exists, we kill it.
2984     (when (gnus-buffer-live-p dead-name)
2985       (gnus-kill-buffer dead-name))
2986     (if (get-buffer buffer)
2987         (progn
2988           (set-buffer buffer)
2989           (setq gnus-summary-buffer (current-buffer))
2990           (not gnus-newsgroup-prepared))
2991       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2992       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2993       (gnus-summary-mode group)
2994       (when gnus-carpal
2995         (gnus-carpal-setup-buffer 'summary))
2996       (unless gnus-single-article-buffer
2997         (make-local-variable 'gnus-article-buffer)
2998         (make-local-variable 'gnus-article-current)
2999         (make-local-variable 'gnus-original-article-buffer))
3000       (setq gnus-newsgroup-name group)
3001       ;; Set any local variables in the group parameters.
3002       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3003       t)))
3004
3005 (defun gnus-set-global-variables ()
3006   "Set the global equivalents of the buffer-local variables.
3007 They are set to the latest values they had.  These reflect the summary
3008 buffer that was in action when the last article was fetched."
3009   (when (eq major-mode 'gnus-summary-mode)
3010     (setq gnus-summary-buffer (current-buffer))
3011     (let ((name gnus-newsgroup-name)
3012           (marked gnus-newsgroup-marked)
3013           (spam gnus-newsgroup-spam-marked)
3014           (unread gnus-newsgroup-unreads)
3015           (headers gnus-current-headers)
3016           (data gnus-newsgroup-data)
3017           (summary gnus-summary-buffer)
3018           (article-buffer gnus-article-buffer)
3019           (original gnus-original-article-buffer)
3020           (gac gnus-article-current)
3021           (reffed gnus-reffed-article-number)
3022           (score-file gnus-current-score-file)
3023           (default-charset gnus-newsgroup-charset)
3024           vlist)
3025       (let ((locals gnus-newsgroup-variables))
3026         (while locals
3027           (if (consp (car locals))
3028               (push (eval (caar locals)) vlist)
3029             (push (eval (car locals)) vlist))
3030           (setq locals (cdr locals)))
3031         (setq vlist (nreverse vlist)))
3032       (save-excursion
3033         (set-buffer gnus-group-buffer)
3034         (setq gnus-newsgroup-name name
3035               gnus-newsgroup-marked marked
3036               gnus-newsgroup-spam-marked spam
3037               gnus-newsgroup-unreads unread
3038               gnus-current-headers headers
3039               gnus-newsgroup-data data
3040               gnus-article-current gac
3041               gnus-summary-buffer summary
3042               gnus-article-buffer article-buffer
3043               gnus-original-article-buffer original
3044               gnus-reffed-article-number reffed
3045               gnus-current-score-file score-file
3046               gnus-newsgroup-charset default-charset)
3047         (let ((locals gnus-newsgroup-variables))
3048           (while locals
3049             (if (consp (car locals))
3050                 (set (caar locals) (pop vlist))
3051               (set (car locals) (pop vlist)))
3052             (setq locals (cdr locals))))
3053         ;; The article buffer also has local variables.
3054         (when (gnus-buffer-live-p gnus-article-buffer)
3055           (set-buffer gnus-article-buffer)
3056           (setq gnus-summary-buffer summary))))))
3057
3058 (defun gnus-summary-article-unread-p (article)
3059   "Say whether ARTICLE is unread or not."
3060   (memq article gnus-newsgroup-unreads))
3061
3062 (defun gnus-summary-first-article-p (&optional article)
3063   "Return whether ARTICLE is the first article in the buffer."
3064   (if (not (setq article (or article (gnus-summary-article-number))))
3065       nil
3066     (eq article (caar gnus-newsgroup-data))))
3067
3068 (defun gnus-summary-last-article-p (&optional article)
3069   "Return whether ARTICLE is the last article in the buffer."
3070   (if (not (setq article (or article (gnus-summary-article-number))))
3071       ;; All non-existent numbers are the last article.  :-)
3072       t
3073     (not (cdr (gnus-data-find-list article)))))
3074
3075 (defun gnus-make-thread-indent-array ()
3076   (let ((n 200))
3077     (unless (and gnus-thread-indent-array
3078                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3079       (setq gnus-thread-indent-array (make-vector 201 "")
3080             gnus-thread-indent-array-level gnus-thread-indent-level)
3081       (while (>= n 0)
3082         (aset gnus-thread-indent-array n
3083               (make-string (* n gnus-thread-indent-level) ? ))
3084         (setq n (1- n))))))
3085
3086 (defun gnus-update-summary-mark-positions ()
3087   "Compute where the summary marks are to go."
3088   (save-excursion
3089     (when (gnus-buffer-exists-p gnus-summary-buffer)
3090       (set-buffer gnus-summary-buffer))
3091     (let ((gnus-replied-mark 129)
3092           (gnus-score-below-mark 130)
3093           (gnus-score-over-mark 130)
3094           (gnus-downloaded-mark 131)
3095           (spec gnus-summary-line-format-spec)
3096           gnus-visual pos)
3097       (save-excursion
3098         (gnus-set-work-buffer)
3099         (let ((gnus-summary-line-format-spec spec)
3100               (gnus-newsgroup-downloadable '(0)))
3101           (gnus-summary-insert-line
3102            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3103            0 nil nil 128 t nil "" nil 1)
3104           (goto-char (point-min))
3105           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3106                                              (- (point) (point-min) 1)))))
3107           (goto-char (point-min))
3108           (push (cons 'replied (and (search-forward "\201" nil t)
3109                                     (- (point) (point-min) 1)))
3110                 pos)
3111           (goto-char (point-min))
3112           (push (cons 'score (and (search-forward "\202" nil t)
3113                                   (- (point) (point-min) 1)))
3114                 pos)
3115           (goto-char (point-min))
3116           (push (cons 'download
3117                       (and (search-forward "\203" nil t)
3118                            (- (point) (point-min) 1)))
3119                 pos)))
3120       (setq gnus-summary-mark-positions pos))))
3121
3122 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3123   "Insert a dummy root in the summary buffer."
3124   (beginning-of-line)
3125   (gnus-add-text-properties
3126    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3127    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3128
3129 (defun gnus-summary-extract-address-component (from)
3130   (or (car (funcall gnus-extract-address-components from))
3131       from))
3132
3133 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3134   (let ((mail-parse-charset gnus-newsgroup-charset)
3135         ; Is it really necessary to do this next part for each summary line?
3136         ; Luckily, doesn't seem to slow things down much.
3137         (mail-parse-ignored-charsets
3138          (save-excursion (set-buffer gnus-summary-buffer)
3139                          gnus-newsgroup-ignored-charsets)))
3140     (or
3141      (and gnus-ignored-from-addresses
3142           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3143           (let ((extra-headers (mail-header-extra header))
3144                 to
3145                 newsgroups)
3146             (cond
3147              ((setq to (cdr (assq 'To extra-headers)))
3148               (concat "-> "
3149                       (inline
3150                         (gnus-summary-extract-address-component
3151                          (funcall gnus-decode-encoded-word-function to)))))
3152              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3153               (concat "=> " newsgroups)))))
3154      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3155
3156 (defun gnus-summary-insert-line (gnus-tmp-header
3157                                  gnus-tmp-level gnus-tmp-current
3158                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3159                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3160                                  &optional gnus-tmp-dummy gnus-tmp-score
3161                                  gnus-tmp-process)
3162   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3163          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3164          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3165          (gnus-tmp-score-char
3166           (if (or (null gnus-summary-default-score)
3167                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3168                       gnus-summary-zcore-fuzz))
3169               ?                         ;Whitespace
3170             (if (< gnus-tmp-score gnus-summary-default-score)
3171                 gnus-score-below-mark gnus-score-over-mark)))
3172          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3173          (gnus-tmp-replied
3174           (cond (gnus-tmp-process gnus-process-mark)
3175                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3176                  gnus-cached-mark)
3177                 (gnus-tmp-replied gnus-replied-mark)
3178                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3179                  gnus-forwarded-mark)
3180                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3181                  gnus-saved-mark)
3182                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3183                  gnus-recent-mark)
3184                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3185                  gnus-unseen-mark)
3186                 (t gnus-no-mark)))
3187          (gnus-tmp-downloaded
3188           (cond (undownloaded 
3189                  gnus-undownloaded-mark)
3190                 (gnus-newsgroup-agentized
3191                  gnus-downloaded-mark)
3192                 (t
3193                  gnus-no-mark)))
3194          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3195          (gnus-tmp-name
3196           (cond
3197            ((string-match "<[^>]+> *$" gnus-tmp-from)
3198             (let ((beg (match-beginning 0)))
3199               (or (and (string-match "^\".+\"" gnus-tmp-from)
3200                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3201                   (substring gnus-tmp-from 0 beg))))
3202            ((string-match "(.+)" gnus-tmp-from)
3203             (substring gnus-tmp-from
3204                        (1+ (match-beginning 0)) (1- (match-end 0))))
3205            (t gnus-tmp-from)))
3206          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3207          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3208          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3209          (buffer-read-only nil))
3210     (when (string= gnus-tmp-name "")
3211       (setq gnus-tmp-name gnus-tmp-from))
3212     (unless (numberp gnus-tmp-lines)
3213       (setq gnus-tmp-lines -1))
3214     (if (= gnus-tmp-lines -1)
3215         (setq gnus-tmp-lines "?")
3216       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3217     (gnus-put-text-property
3218      (point)
3219      (progn (eval gnus-summary-line-format-spec) (point))
3220      'gnus-number gnus-tmp-number)
3221     (when (gnus-visual-p 'summary-highlight 'highlight)
3222       (forward-line -1)
3223       (gnus-run-hooks 'gnus-summary-update-hook)
3224       (forward-line 1))))
3225
3226 (defun gnus-summary-update-line (&optional dont-update)
3227   "Update summary line after change."
3228   (when (and gnus-summary-default-score
3229              (not gnus-summary-inhibit-highlight))
3230     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3231            (article (gnus-summary-article-number))
3232            (score (gnus-summary-article-score article)))
3233       (unless dont-update
3234         (if (and gnus-summary-mark-below
3235                  (< (gnus-summary-article-score)
3236                     gnus-summary-mark-below))
3237             ;; This article has a low score, so we mark it as read.
3238             (when (memq article gnus-newsgroup-unreads)
3239               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3240           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3241             ;; This article was previously marked as read on account
3242             ;; of a low score, but now it has risen, so we mark it as
3243             ;; unread.
3244             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3245         (gnus-summary-update-mark
3246          (if (or (null gnus-summary-default-score)
3247                  (<= (abs (- score gnus-summary-default-score))
3248                      gnus-summary-zcore-fuzz))
3249              ?                          ;Whitespace
3250            (if (< score gnus-summary-default-score)
3251                gnus-score-below-mark gnus-score-over-mark))
3252          'score))
3253       ;; Do visual highlighting.
3254       (when (gnus-visual-p 'summary-highlight 'highlight)
3255         (gnus-run-hooks 'gnus-summary-update-hook)))))
3256
3257 (defvar gnus-tmp-new-adopts nil)
3258
3259 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3260   "Return the number of articles in THREAD.
3261 This may be 0 in some cases -- if none of the articles in
3262 the thread are to be displayed."
3263   (let* ((number
3264          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3265           (cond
3266            ((not (listp thread))
3267             1)
3268            ((and (consp thread) (cdr thread))
3269             (apply
3270              '+ 1 (mapcar
3271                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3272            ((null thread)
3273             1)
3274            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3275             1)
3276            (t 0))))
3277     (when (and level (zerop level) gnus-tmp-new-adopts)
3278       (incf number
3279             (apply '+ (mapcar
3280                        'gnus-summary-number-of-articles-in-thread
3281                        gnus-tmp-new-adopts))))
3282     (if char
3283         (if (> number 1) gnus-not-empty-thread-mark
3284           gnus-empty-thread-mark)
3285       number)))
3286
3287 (defsubst gnus-summary-line-message-size (head)
3288   "Return pretty-printed version of message size.
3289 This function is intended to be used in
3290 `gnus-summary-line-format-alist', which see."
3291   (let ((c (or (mail-header-chars head) -1)))
3292     (cond ((< c 0) "n/a")               ; chars not available
3293           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3294           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3295           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3296           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3297
3298
3299 (defun gnus-summary-set-local-parameters (group)
3300   "Go through the local params of GROUP and set all variable specs in that list."
3301   (let ((params (gnus-group-find-parameter group))
3302         (vars '(quit-config))           ; Ignore quit-config.
3303         elem)
3304     (while params
3305       (setq elem (car params)
3306             params (cdr params))
3307       (and (consp elem)                 ; Has to be a cons.
3308            (consp (cdr elem))           ; The cdr has to be a list.
3309            (symbolp (car elem))         ; Has to be a symbol in there.
3310            (not (memq (car elem) vars))
3311            (ignore-errors               ; So we set it.
3312              (push (car elem) vars)
3313              (make-local-variable (car elem))
3314              (set (car elem) (eval (nth 1 elem))))))))
3315
3316 (defun gnus-summary-read-group (group &optional show-all no-article
3317                                       kill-buffer no-display backward
3318                                       select-articles)
3319   "Start reading news in newsgroup GROUP.
3320 If SHOW-ALL is non-nil, already read articles are also listed.
3321 If NO-ARTICLE is non-nil, no article is selected initially.
3322 If NO-DISPLAY, don't generate a summary buffer."
3323   (let (result)
3324     (while (and group
3325                 (null (setq result
3326                             (let ((gnus-auto-select-next nil))
3327                               (or (gnus-summary-read-group-1
3328                                    group show-all no-article
3329                                    kill-buffer no-display
3330                                    select-articles)
3331                                   (setq show-all nil
3332                                         select-articles nil)))))
3333                 (eq gnus-auto-select-next 'quietly))
3334       (set-buffer gnus-group-buffer)
3335       ;; The entry function called above goes to the next
3336       ;; group automatically, so we go two groups back
3337       ;; if we are searching for the previous group.
3338       (when backward
3339         (gnus-group-prev-unread-group 2))
3340       (if (not (equal group (gnus-group-group-name)))
3341           (setq group (gnus-group-group-name))
3342         (setq group nil)))
3343     result))
3344
3345 (defun gnus-summary-read-group-1 (group show-all no-article
3346                                         kill-buffer no-display
3347                                         &optional select-articles)
3348   ;; Killed foreign groups can't be entered.
3349   ;;  (when (and (not (gnus-group-native-p group))
3350   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3351   ;;    (error "Dead non-native groups can't be entered"))
3352   (gnus-message 5 "Retrieving newsgroup: %s..."
3353                 (gnus-group-decoded-name group))
3354   (let* ((new-group (gnus-summary-setup-buffer group))
3355          (quit-config (gnus-group-quit-config group))
3356          (did-select (and new-group (gnus-select-newsgroup
3357                                      group show-all select-articles))))
3358     (cond
3359      ;; This summary buffer exists already, so we just select it.
3360      ((not new-group)
3361       (gnus-set-global-variables)
3362       (when kill-buffer
3363         (gnus-kill-or-deaden-summary kill-buffer))
3364       (gnus-configure-windows 'summary 'force)
3365       (gnus-set-mode-line 'summary)
3366       (gnus-summary-position-point)
3367       (message "")
3368       t)
3369      ;; We couldn't select this group.
3370      ((null did-select)
3371       (when (and (eq major-mode 'gnus-summary-mode)
3372                  (not (equal (current-buffer) kill-buffer)))
3373         (kill-buffer (current-buffer))
3374         (if (not quit-config)
3375             (progn
3376               ;; Update the info -- marks might need to be removed,
3377               ;; for instance.
3378               (gnus-summary-update-info)
3379               (set-buffer gnus-group-buffer)
3380               (gnus-group-jump-to-group group)
3381               (gnus-group-next-unread-group 1))
3382           (gnus-handle-ephemeral-exit quit-config)))
3383       (let ((grpinfo (gnus-get-info group)))
3384         (if (null (gnus-info-read grpinfo))
3385             (gnus-message 3 "Group %s contains no messages"
3386                           (gnus-group-decoded-name group))
3387           (gnus-message 3 "Can't select group")))
3388       nil)
3389      ;; The user did a `C-g' while prompting for number of articles,
3390      ;; so we exit this group.
3391      ((eq did-select 'quit)
3392       (and (eq major-mode 'gnus-summary-mode)
3393            (not (equal (current-buffer) kill-buffer))
3394            (kill-buffer (current-buffer)))
3395       (when kill-buffer
3396         (gnus-kill-or-deaden-summary kill-buffer))
3397       (if (not quit-config)
3398           (progn
3399             (set-buffer gnus-group-buffer)
3400             (gnus-group-jump-to-group group)
3401             (gnus-group-next-unread-group 1)
3402             (gnus-configure-windows 'group 'force))
3403         (gnus-handle-ephemeral-exit quit-config))
3404       ;; Finally signal the quit.
3405       (signal 'quit nil))
3406      ;; The group was successfully selected.
3407      (t
3408       (gnus-set-global-variables)
3409       ;; Save the active value in effect when the group was entered.
3410       (setq gnus-newsgroup-active
3411             (gnus-copy-sequence
3412              (gnus-active gnus-newsgroup-name)))
3413       ;; You can change the summary buffer in some way with this hook.
3414       (gnus-run-hooks 'gnus-select-group-hook)
3415       (gnus-update-format-specifications
3416        nil 'summary 'summary-mode 'summary-dummy)
3417       (gnus-update-summary-mark-positions)
3418       ;; Do score processing.
3419       (when gnus-use-scoring
3420         (gnus-possibly-score-headers))
3421       ;; Check whether to fill in the gaps in the threads.
3422       (when gnus-build-sparse-threads
3423         (gnus-build-sparse-threads))
3424       ;; Find the initial limit.
3425       (if gnus-show-threads
3426           (if show-all
3427               (let ((gnus-newsgroup-dormant nil))
3428                 (gnus-summary-initial-limit show-all))
3429             (gnus-summary-initial-limit show-all))
3430         ;; When unthreaded, all articles are always shown.
3431         (setq gnus-newsgroup-limit
3432               (mapcar
3433                (lambda (header) (mail-header-number header))
3434                gnus-newsgroup-headers)))
3435       ;; Generate the summary buffer.
3436       (unless no-display
3437         (gnus-summary-prepare))
3438       (when gnus-use-trees
3439         (gnus-tree-open group)
3440         (setq gnus-summary-highlight-line-function
3441               'gnus-tree-highlight-article))
3442       ;; If the summary buffer is empty, but there are some low-scored
3443       ;; articles or some excluded dormants, we include these in the
3444       ;; buffer.
3445       (when (and (zerop (buffer-size))
3446                  (not no-display))
3447         (cond (gnus-newsgroup-dormant
3448                (gnus-summary-limit-include-dormant))
3449               ((and gnus-newsgroup-scored show-all)
3450                (gnus-summary-limit-include-expunged t))))
3451       ;; Function `gnus-apply-kill-file' must be called in this hook.
3452       (gnus-run-hooks 'gnus-apply-kill-hook)
3453       (if (and (zerop (buffer-size))
3454                (not no-display))
3455           (progn
3456             ;; This newsgroup is empty.
3457             (gnus-summary-catchup-and-exit nil t)
3458             (gnus-message 6 "No unread news")
3459             (when kill-buffer
3460               (gnus-kill-or-deaden-summary kill-buffer))
3461             ;; Return nil from this function.
3462             nil)
3463         ;; Hide conversation thread subtrees.  We cannot do this in
3464         ;; gnus-summary-prepare-hook since kill processing may not
3465         ;; work with hidden articles.
3466         (gnus-summary-maybe-hide-threads)
3467         (when kill-buffer
3468           (gnus-kill-or-deaden-summary kill-buffer))
3469         (gnus-summary-auto-select-subject)
3470         ;; Show first unread article if requested.
3471         (if (and (not no-article)
3472                  (not no-display)
3473                  gnus-newsgroup-unreads
3474                  gnus-auto-select-first)
3475             (progn
3476               (gnus-configure-windows 'summary)
3477               (let ((art (gnus-summary-article-number)))
3478                 (unless (or (memq art gnus-newsgroup-undownloaded)
3479                             (memq art gnus-newsgroup-downloadable))
3480                   (gnus-summary-goto-article art))))
3481           ;; Don't select any articles.
3482           (gnus-summary-position-point)
3483           (gnus-configure-windows 'summary 'force)
3484           (gnus-set-mode-line 'summary))
3485         (when (get-buffer-window gnus-group-buffer t)
3486           ;; Gotta use windows, because recenter does weird stuff if
3487           ;; the current buffer ain't the displayed window.
3488           (let ((owin (selected-window)))
3489             (select-window (get-buffer-window gnus-group-buffer t))
3490             (when (gnus-group-goto-group group)
3491               (recenter))
3492             (select-window owin)))
3493         ;; Mark this buffer as "prepared".
3494         (setq gnus-newsgroup-prepared t)
3495         (gnus-run-hooks 'gnus-summary-prepared-hook)
3496         (gnus-group-update-group group)
3497         t)))))
3498
3499 (defun gnus-summary-auto-select-subject ()
3500   "Select the subject line on initial group entry."
3501   (goto-char (point-min))
3502   (cond
3503    ((eq gnus-auto-select-subject 'best)
3504     (gnus-summary-best-unread-subject))
3505    ((eq gnus-auto-select-subject 'unread)
3506     (gnus-summary-first-unread-subject))
3507    ((eq gnus-auto-select-subject 'unseen)
3508     (gnus-summary-first-unseen-subject))
3509    ((eq gnus-auto-select-subject 'unseen-or-unread)
3510     (gnus-summary-first-unseen-or-unread-subject))
3511    ((eq gnus-auto-select-subject 'first)
3512     ;; Do nothing.
3513     )
3514    ((gnus-functionp gnus-auto-select-subject)
3515     (funcall gnus-auto-select-subject))))
3516
3517 (defun gnus-summary-prepare ()
3518   "Generate the summary buffer."
3519   (interactive)
3520   (let ((buffer-read-only nil))
3521     (erase-buffer)
3522     (setq gnus-newsgroup-data nil
3523           gnus-newsgroup-data-reverse nil)
3524     (gnus-run-hooks 'gnus-summary-generate-hook)
3525     ;; Generate the buffer, either with threads or without.
3526     (when gnus-newsgroup-headers
3527       (gnus-summary-prepare-threads
3528        (if gnus-show-threads
3529            (gnus-sort-gathered-threads
3530             (funcall gnus-summary-thread-gathering-function
3531                      (gnus-sort-threads
3532                       (gnus-cut-threads (gnus-make-threads)))))
3533          ;; Unthreaded display.
3534          (gnus-sort-articles gnus-newsgroup-headers))))
3535     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3536     ;; Call hooks for modifying summary buffer.
3537     (goto-char (point-min))
3538     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3539
3540 (defsubst gnus-general-simplify-subject (subject)
3541   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3542   (setq subject
3543         (cond
3544          ;; Truncate the subject.
3545          (gnus-simplify-subject-functions
3546           (gnus-map-function gnus-simplify-subject-functions subject))
3547          ((numberp gnus-summary-gather-subject-limit)
3548           (setq subject (gnus-simplify-subject-re subject))
3549           (if (> (length subject) gnus-summary-gather-subject-limit)
3550               (substring subject 0 gnus-summary-gather-subject-limit)
3551             subject))
3552          ;; Fuzzily simplify it.
3553          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3554           (gnus-simplify-subject-fuzzy subject))
3555          ;; Just remove the leading "Re:".
3556          (t
3557           (gnus-simplify-subject-re subject))))
3558
3559   (if (and gnus-summary-gather-exclude-subject
3560            (string-match gnus-summary-gather-exclude-subject subject))
3561       nil                         ; This article shouldn't be gathered
3562     subject))
3563
3564 (defun gnus-summary-simplify-subject-query ()
3565   "Query where the respool algorithm would put this article."
3566   (interactive)
3567   (gnus-summary-select-article)
3568   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3569
3570 (defun gnus-gather-threads-by-subject (threads)
3571   "Gather threads by looking at Subject headers."
3572   (if (not gnus-summary-make-false-root)
3573       threads
3574     (let ((hashtb (gnus-make-hashtable 1024))
3575           (prev threads)
3576           (result threads)
3577           subject hthread whole-subject)
3578       (while threads
3579         (setq subject (gnus-general-simplify-subject
3580                        (setq whole-subject (mail-header-subject
3581                                             (caar threads)))))
3582         (when subject
3583           (if (setq hthread (gnus-gethash subject hashtb))
3584               (progn
3585                 ;; We enter a dummy root into the thread, if we
3586                 ;; haven't done that already.
3587                 (unless (stringp (caar hthread))
3588                   (setcar hthread (list whole-subject (car hthread))))
3589                 ;; We add this new gathered thread to this gathered
3590                 ;; thread.
3591                 (setcdr (car hthread)
3592                         (nconc (cdar hthread) (list (car threads))))
3593                 ;; Remove it from the list of threads.
3594                 (setcdr prev (cdr threads))
3595                 (setq threads prev))
3596             ;; Enter this thread into the hash table.
3597             (gnus-sethash subject
3598                           (if gnus-summary-make-false-root-always
3599                               (progn
3600                                 ;; If you want a dummy root above all
3601                                 ;; threads...
3602                                 (setcar threads (list whole-subject
3603                                                       (car threads)))
3604                                 threads)
3605                             threads)
3606                           hashtb)))
3607         (setq prev threads)
3608         (setq threads (cdr threads)))
3609       result)))
3610
3611 (defun gnus-gather-threads-by-references (threads)
3612   "Gather threads by looking at References headers."
3613   (let ((idhashtb (gnus-make-hashtable 1024))
3614         (thhashtb (gnus-make-hashtable 1024))
3615         (prev threads)
3616         (result threads)
3617         ids references id gthread gid entered ref)
3618     (while threads
3619       (when (setq references (mail-header-references (caar threads)))
3620         (setq id (mail-header-id (caar threads))
3621               ids (inline (gnus-split-references references))
3622               entered nil)
3623         (while (setq ref (pop ids))
3624           (setq ids (delete ref ids))
3625           (if (not (setq gid (gnus-gethash ref idhashtb)))
3626               (progn
3627                 (gnus-sethash ref id idhashtb)
3628                 (gnus-sethash id threads thhashtb))
3629             (setq gthread (gnus-gethash gid thhashtb))
3630             (unless entered
3631               ;; We enter a dummy root into the thread, if we
3632               ;; haven't done that already.
3633               (unless (stringp (caar gthread))
3634                 (setcar gthread (list (mail-header-subject (caar gthread))
3635                                       (car gthread))))
3636               ;; We add this new gathered thread to this gathered
3637               ;; thread.
3638               (setcdr (car gthread)
3639                       (nconc (cdar gthread) (list (car threads)))))
3640             ;; Add it into the thread hash table.
3641             (gnus-sethash id gthread thhashtb)
3642             (setq entered t)
3643             ;; Remove it from the list of threads.
3644             (setcdr prev (cdr threads))
3645             (setq threads prev))))
3646       (setq prev threads)
3647       (setq threads (cdr threads)))
3648     result))
3649
3650 (defun gnus-sort-gathered-threads (threads)
3651   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3652   (let ((result threads))
3653     (while threads
3654       (when (stringp (caar threads))
3655         (setcdr (car threads)
3656                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3657       (setq threads (cdr threads)))
3658     result))
3659
3660 (defun gnus-thread-loop-p (root thread)
3661   "Say whether ROOT is in THREAD."
3662   (let ((stack (list thread))
3663         (infloop 0)
3664         th)
3665     (while (setq thread (pop stack))
3666       (setq th (cdr thread))
3667       (while (and th
3668                   (not (eq (caar th) root)))
3669         (pop th))
3670       (if th
3671           ;; We have found a loop.
3672           (let (ref-dep)
3673             (setcdr thread (delq (car th) (cdr thread)))
3674             (if (boundp (setq ref-dep (intern "none"
3675                                               gnus-newsgroup-dependencies)))
3676                 (setcdr (symbol-value ref-dep)
3677                         (nconc (cdr (symbol-value ref-dep))
3678                                (list (car th))))
3679               (set ref-dep (list nil (car th))))
3680             (setq infloop 1
3681                   stack nil))
3682         ;; Push all the subthreads onto the stack.
3683         (push (cdr thread) stack)))
3684     infloop))
3685
3686 (defun gnus-make-threads ()
3687   "Go through the dependency hashtb and find the roots.  Return all threads."
3688   (let (threads)
3689     (while (catch 'infloop
3690              (mapatoms
3691               (lambda (refs)
3692                 ;; Deal with self-referencing References loops.
3693                 (when (and (car (symbol-value refs))
3694                            (not (zerop
3695                                  (apply
3696                                   '+
3697                                   (mapcar
3698                                    (lambda (thread)
3699                                      (gnus-thread-loop-p
3700                                       (car (symbol-value refs)) thread))
3701                                    (cdr (symbol-value refs)))))))
3702                   (setq threads nil)
3703                   (throw 'infloop t))
3704                 (unless (car (symbol-value refs))
3705                   ;; These threads do not refer back to any other
3706                   ;; articles, so they're roots.
3707                   (setq threads (append (cdr (symbol-value refs)) threads))))
3708               gnus-newsgroup-dependencies)))
3709     threads))
3710
3711 ;; Build the thread tree.
3712 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3713   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3714
3715 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3716 if it was already present.
3717
3718 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3719 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3720 Message-IDs will be renamed to a unique Message-ID before being
3721 entered.
3722
3723 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3724   (let* ((id (mail-header-id header))
3725          (id-dep (and id (intern id dependencies)))
3726          parent-id ref ref-dep ref-header replaced)
3727     ;; Enter this `header' in the `dependencies' table.
3728     (cond
3729      ((not id-dep)
3730       (setq header nil))
3731      ;; The first two cases do the normal part: enter a new `header'
3732      ;; in the `dependencies' table.
3733      ((not (boundp id-dep))
3734       (set id-dep (list header)))
3735      ((null (car (symbol-value id-dep)))
3736       (setcar (symbol-value id-dep) header))
3737
3738      ;; From here the `header' was already present in the
3739      ;; `dependencies' table.
3740      (force-new
3741       ;; Overrides an existing entry;
3742       ;; just set the header part of the entry.
3743       (setcar (symbol-value id-dep) header)
3744       (setq replaced t))
3745
3746      ;; Renames the existing `header' to a unique Message-ID.
3747      ((not gnus-summary-ignore-duplicates)
3748       ;; An article with this Message-ID has already been seen.
3749       ;; We rename the Message-ID.
3750       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3751            (list header))
3752       (mail-header-set-id header id))
3753
3754      ;; The last case ignores an existing entry, except it adds any
3755      ;; additional Xrefs (in case the two articles came from different
3756      ;; servers.
3757      ;; Also sets `header' to `nil' meaning that the `dependencies'
3758      ;; table was *not* modified.
3759      (t
3760       (mail-header-set-xref
3761        (car (symbol-value id-dep))
3762        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3763                    "")
3764                (or (mail-header-xref header) "")))
3765       (setq header nil)))
3766
3767     (when (and header (not replaced))
3768       ;; First check that we are not creating a References loop.
3769       (setq parent-id (gnus-parent-id (mail-header-references header)))
3770       (setq ref parent-id)
3771       (while (and ref
3772                   (setq ref-dep (intern-soft ref dependencies))
3773                   (boundp ref-dep)
3774                   (setq ref-header (car (symbol-value ref-dep))))
3775         (if (string= id ref)
3776             ;; Yuk!  This is a reference loop.  Make the article be a
3777             ;; root article.
3778             (progn
3779               (mail-header-set-references (car (symbol-value id-dep)) "none")
3780               (setq ref nil)
3781               (setq parent-id nil))
3782           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3783       (setq ref-dep (intern (or parent-id "none") dependencies))
3784       (if (boundp ref-dep)
3785           (setcdr (symbol-value ref-dep)
3786                   (nconc (cdr (symbol-value ref-dep))
3787                          (list (symbol-value id-dep))))
3788         (set ref-dep (list nil (symbol-value id-dep)))))
3789     header))
3790
3791 (defun gnus-extract-message-id-from-in-reply-to (string)
3792   (if (string-match "<[^>]+>" string)
3793       (substring string (match-beginning 0) (match-end 0))
3794     nil))
3795
3796 (defun gnus-build-sparse-threads ()
3797   (let ((headers gnus-newsgroup-headers)
3798         (mail-parse-charset gnus-newsgroup-charset)
3799         (gnus-summary-ignore-duplicates t)
3800         header references generation relations
3801         subject child end new-child date)
3802     ;; First we create an alist of generations/relations, where
3803     ;; generations is how much we trust the relation, and the relation
3804     ;; is parent/child.
3805     (gnus-message 7 "Making sparse threads...")
3806     (save-excursion
3807       (nnheader-set-temp-buffer " *gnus sparse threads*")
3808       (while (setq header (pop headers))
3809         (when (and (setq references (mail-header-references header))
3810                    (not (string= references "")))
3811           (insert references)
3812           (setq child (mail-header-id header)
3813                 subject (mail-header-subject header)
3814                 date (mail-header-date header)
3815                 generation 0)
3816           (while (search-backward ">" nil t)
3817             (setq end (1+ (point)))
3818             (when (search-backward "<" nil t)
3819               (setq new-child (buffer-substring (point) end))
3820               (push (list (incf generation)
3821                           child (setq child new-child)
3822                           subject date)
3823                     relations)))
3824           (when child
3825             (push (list (1+ generation) child nil subject) relations))
3826           (erase-buffer)))
3827       (kill-buffer (current-buffer)))
3828     ;; Sort over trustworthiness.
3829     (mapcar
3830      (lambda (relation)
3831        (when (gnus-dependencies-add-header
3832               (make-full-mail-header
3833                gnus-reffed-article-number
3834                (nth 3 relation) "" (or (nth 4 relation) "")
3835                (nth 1 relation)
3836                (or (nth 2 relation) "") 0 0 "")
3837               gnus-newsgroup-dependencies nil)
3838          (push gnus-reffed-article-number gnus-newsgroup-limit)
3839          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3840          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3841                gnus-newsgroup-reads)
3842          (decf gnus-reffed-article-number)))
3843      (sort relations 'car-less-than-car))
3844     (gnus-message 7 "Making sparse threads...done")))
3845
3846 (defun gnus-build-old-threads ()
3847   ;; Look at all the articles that refer back to old articles, and
3848   ;; fetch the headers for the articles that aren't there.  This will
3849   ;; build complete threads - if the roots haven't been expired by the
3850   ;; server, that is.
3851   (let ((mail-parse-charset gnus-newsgroup-charset)
3852         id heads)
3853     (mapatoms
3854      (lambda (refs)
3855        (when (not (car (symbol-value refs)))
3856          (setq heads (cdr (symbol-value refs)))
3857          (while heads
3858            (if (memq (mail-header-number (caar heads))
3859                      gnus-newsgroup-dormant)
3860                (setq heads (cdr heads))
3861              (setq id (symbol-name refs))
3862              (while (and (setq id (gnus-build-get-header id))
3863                          (not (car (gnus-id-to-thread id)))))
3864              (setq heads nil)))))
3865      gnus-newsgroup-dependencies)))
3866
3867 ;; This function has to be called with point after the article number
3868 ;; on the beginning of the line.
3869 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3870   (let ((eol (gnus-point-at-eol))
3871         (buffer (current-buffer))
3872         header references in-reply-to)
3873
3874     ;; overview: [num subject from date id refs chars lines misc]
3875     (unwind-protect
3876         (let (x)
3877           (narrow-to-region (point) eol)
3878           (unless (eobp)
3879             (forward-char))
3880
3881           (setq header
3882                 (make-full-mail-header
3883                  number                 ; number
3884                  (condition-case ()     ; subject
3885                      (funcall gnus-decode-encoded-word-function
3886                               (setq x (nnheader-nov-field)))
3887                    (error x))
3888                  (condition-case ()     ; from
3889                      (funcall gnus-decode-encoded-word-function
3890                               (setq x (nnheader-nov-field)))
3891                    (error x))
3892                  (nnheader-nov-field)   ; date
3893                  (nnheader-nov-read-message-id) ; id
3894                  (setq references (nnheader-nov-field)) ; refs
3895                  (nnheader-nov-read-integer) ; chars
3896                  (nnheader-nov-read-integer) ; lines
3897                  (unless (eobp)
3898                    (if (looking-at "Xref: ")
3899                        (goto-char (match-end 0)))
3900                    (nnheader-nov-field)) ; Xref
3901                  (nnheader-nov-parse-extra)))) ; extra
3902
3903       (widen))
3904
3905     (when (and (string= references "")
3906                (setq in-reply-to (mail-header-extra header))
3907                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3908       (mail-header-set-references
3909        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3910
3911     (when gnus-alter-header-function
3912       (funcall gnus-alter-header-function header))
3913     (gnus-dependencies-add-header header dependencies force-new)))
3914
3915 (defun gnus-build-get-header (id)
3916   "Look through the buffer of NOV lines and find the header to ID.
3917 Enter this line into the dependencies hash table, and return
3918 the id of the parent article (if any)."
3919   (let ((deps gnus-newsgroup-dependencies)
3920         found header)
3921     (prog1
3922         (save-excursion
3923           (set-buffer nntp-server-buffer)
3924           (let ((case-fold-search nil))
3925             (goto-char (point-min))
3926             (while (and (not found)
3927                         (search-forward id nil t))
3928               (beginning-of-line)
3929               (setq found (looking-at
3930                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3931                                    (regexp-quote id))))
3932               (or found (beginning-of-line 2)))
3933             (when found
3934               (beginning-of-line)
3935               (and
3936                (setq header (gnus-nov-parse-line
3937                              (read (current-buffer)) deps))
3938                (gnus-parent-id (mail-header-references header))))))
3939       (when header
3940         (let ((number (mail-header-number header)))
3941           (push number gnus-newsgroup-limit)
3942           (push header gnus-newsgroup-headers)
3943           (if (memq number gnus-newsgroup-unselected)
3944               (progn
3945                 (setq gnus-newsgroup-unreads
3946                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3947                                                number))
3948                 (setq gnus-newsgroup-unselected
3949                       (delq number gnus-newsgroup-unselected)))
3950             (push number gnus-newsgroup-ancient)))))))
3951
3952 (defun gnus-build-all-threads ()
3953   "Read all the headers."
3954   (let ((gnus-summary-ignore-duplicates t)
3955         (mail-parse-charset gnus-newsgroup-charset)
3956         (dependencies gnus-newsgroup-dependencies)
3957         header article)
3958     (save-excursion
3959       (set-buffer nntp-server-buffer)
3960       (let ((case-fold-search nil))
3961         (goto-char (point-min))
3962         (while (not (eobp))
3963           (ignore-errors
3964             (setq article (read (current-buffer))
3965                   header (gnus-nov-parse-line article dependencies)))
3966           (when header
3967             (save-excursion
3968               (set-buffer gnus-summary-buffer)
3969               (push header gnus-newsgroup-headers)
3970               (if (memq (setq article (mail-header-number header))
3971                         gnus-newsgroup-unselected)
3972                   (progn
3973                     (setq gnus-newsgroup-unreads
3974                           (gnus-add-to-sorted-list
3975                            gnus-newsgroup-unreads article))
3976                     (setq gnus-newsgroup-unselected
3977                           (delq article gnus-newsgroup-unselected)))
3978                 (push article gnus-newsgroup-ancient)))
3979             (forward-line 1)))))))
3980
3981 (defun gnus-summary-update-article-line (article header)
3982   "Update the line for ARTICLE using HEADERS."
3983   (let* ((id (mail-header-id header))
3984          (thread (gnus-id-to-thread id)))
3985     (unless thread
3986       (error "Article in no thread"))
3987     ;; Update the thread.
3988     (setcar thread header)
3989     (gnus-summary-goto-subject article)
3990     (let* ((datal (gnus-data-find-list article))
3991            (data (car datal))
3992            (length (when (cdr datal)
3993                      (- (gnus-data-pos data)
3994                         (gnus-data-pos (cadr datal)))))
3995            (buffer-read-only nil)
3996            (level (gnus-summary-thread-level)))
3997       (gnus-delete-line)
3998       (gnus-summary-insert-line
3999        header level nil 
4000        (memq article gnus-newsgroup-undownloaded)
4001        (gnus-article-mark article)
4002        (memq article gnus-newsgroup-replied)
4003        (memq article gnus-newsgroup-expirable)
4004        ;; Only insert the Subject string when it's different
4005        ;; from the previous Subject string.
4006        (if (and
4007             gnus-show-threads
4008             (gnus-subject-equal
4009              (condition-case ()
4010                  (mail-header-subject
4011                   (gnus-data-header
4012                    (cadr
4013                     (gnus-data-find-list
4014                      article
4015                      (gnus-data-list t)))))
4016                ;; Error on the side of excessive subjects.
4017                (error ""))
4018              (mail-header-subject header)))
4019            ""
4020          (mail-header-subject header))
4021        nil (cdr (assq article gnus-newsgroup-scored))
4022        (memq article gnus-newsgroup-processable))
4023       (when length
4024         (gnus-data-update-list
4025          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4026
4027 (defun gnus-summary-update-article (article &optional iheader)
4028   "Update ARTICLE in the summary buffer."
4029   (set-buffer gnus-summary-buffer)
4030   (let* ((header (gnus-summary-article-header article))
4031          (id (mail-header-id header))
4032          (data (gnus-data-find article))
4033          (thread (gnus-id-to-thread id))
4034          (references (mail-header-references header))
4035          (parent
4036           (gnus-id-to-thread
4037            (or (gnus-parent-id
4038                 (when (and references
4039                            (not (equal "" references)))
4040                   references))
4041                "none")))
4042          (buffer-read-only nil)
4043          (old (car thread)))
4044     (when thread
4045       (unless iheader
4046         (setcar thread nil)
4047         (when parent
4048           (delq thread parent)))
4049       (if (gnus-summary-insert-subject id header)
4050           ;; Set the (possibly) new article number in the data structure.
4051           (gnus-data-set-number data (gnus-id-to-article id))
4052         (setcar thread old)
4053         nil))))
4054
4055 (defun gnus-rebuild-thread (id &optional line)
4056   "Rebuild the thread containing ID.
4057 If LINE, insert the rebuilt thread starting on line LINE."
4058   (let ((buffer-read-only nil)
4059         old-pos current thread data)
4060     (if (not gnus-show-threads)
4061         (setq thread (list (car (gnus-id-to-thread id))))
4062       ;; Get the thread this article is part of.
4063       (setq thread (gnus-remove-thread id)))
4064     (setq old-pos (gnus-point-at-bol))
4065     (setq current (save-excursion
4066                     (and (re-search-backward "[\r\n]" nil t)
4067                          (gnus-summary-article-number))))
4068     ;; If this is a gathered thread, we have to go some re-gathering.
4069     (when (stringp (car thread))
4070       (let ((subject (car thread))
4071             roots thr)
4072         (setq thread (cdr thread))
4073         (while thread
4074           (unless (memq (setq thr (gnus-id-to-thread
4075                                    (gnus-root-id
4076                                     (mail-header-id (caar thread)))))
4077                         roots)
4078             (push thr roots))
4079           (setq thread (cdr thread)))
4080         ;; We now have all (unique) roots.
4081         (if (= (length roots) 1)
4082             ;; All the loose roots are now one solid root.
4083             (setq thread (car roots))
4084           (setq thread (cons subject (gnus-sort-threads roots))))))
4085     (let (threads)
4086       ;; We then insert this thread into the summary buffer.
4087       (when line
4088         (goto-char (point-min))
4089         (forward-line (1- line)))
4090       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4091         (if gnus-show-threads
4092             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4093           (gnus-summary-prepare-unthreaded thread))
4094         (setq data (nreverse gnus-newsgroup-data))
4095         (setq threads gnus-newsgroup-threads))
4096       ;; We splice the new data into the data structure.
4097       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4098       ;;!!! then we want to insert at the beginning of the buffer.
4099       ;;!!! That happens to be true with Gnus now, but that may
4100       ;;!!! change in the future.  Perhaps.
4101       (gnus-data-enter-list
4102        (if line nil current) data (- (point) old-pos))
4103       (setq gnus-newsgroup-threads
4104             (nconc threads gnus-newsgroup-threads))
4105       (gnus-data-compute-positions))))
4106
4107 (defun gnus-number-to-header (number)
4108   "Return the header for article NUMBER."
4109   (let ((headers gnus-newsgroup-headers))
4110     (while (and headers
4111                 (not (= number (mail-header-number (car headers)))))
4112       (pop headers))
4113     (when headers
4114       (car headers))))
4115
4116 (defun gnus-parent-headers (in-headers &optional generation)
4117   "Return the headers of the GENERATIONeth parent of HEADERS."
4118   (unless generation
4119     (setq generation 1))
4120   (let ((parent t)
4121         (headers in-headers)
4122         references)
4123     (while (and parent
4124                 (not (zerop generation))
4125                 (setq references (mail-header-references headers)))
4126       (setq headers (if (and references
4127                              (setq parent (gnus-parent-id references)))
4128                         (car (gnus-id-to-thread parent))
4129                       nil))
4130       (decf generation))
4131     (and (not (eq headers in-headers))
4132          headers)))
4133
4134 (defun gnus-id-to-thread (id)
4135   "Return the (sub-)thread where ID appears."
4136   (gnus-gethash id gnus-newsgroup-dependencies))
4137
4138 (defun gnus-id-to-article (id)
4139   "Return the article number of ID."
4140   (let ((thread (gnus-id-to-thread id)))
4141     (when (and thread
4142                (car thread))
4143       (mail-header-number (car thread)))))
4144
4145 (defun gnus-id-to-header (id)
4146   "Return the article headers of ID."
4147   (car (gnus-id-to-thread id)))
4148
4149 (defun gnus-article-displayed-root-p (article)
4150   "Say whether ARTICLE is a root(ish) article."
4151   (let ((level (gnus-summary-thread-level article))
4152         (refs (mail-header-references  (gnus-summary-article-header article)))
4153         particle)
4154     (cond
4155      ((null level) nil)
4156      ((zerop level) t)
4157      ((null refs) t)
4158      ((null (gnus-parent-id refs)) t)
4159      ((and (= 1 level)
4160            (null (setq particle (gnus-id-to-article
4161                                  (gnus-parent-id refs))))
4162            (null (gnus-summary-thread-level particle)))))))
4163
4164 (defun gnus-root-id (id)
4165   "Return the id of the root of the thread where ID appears."
4166   (let (last-id prev)
4167     (while (and id (setq prev (car (gnus-id-to-thread id))))
4168       (setq last-id id
4169             id (gnus-parent-id (mail-header-references prev))))
4170     last-id))
4171
4172 (defun gnus-articles-in-thread (thread)
4173   "Return the list of articles in THREAD."
4174   (cons (mail-header-number (car thread))
4175         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4176
4177 (defun gnus-remove-thread (id &optional dont-remove)
4178   "Remove the thread that has ID in it."
4179   (let (headers thread last-id)
4180     ;; First go up in this thread until we find the root.
4181     (setq last-id (gnus-root-id id)
4182           headers (message-flatten-list (gnus-id-to-thread last-id)))
4183     ;; We have now found the real root of this thread.  It might have
4184     ;; been gathered into some loose thread, so we have to search
4185     ;; through the threads to find the thread we wanted.
4186     (let ((threads gnus-newsgroup-threads)
4187           sub)
4188       (while threads
4189         (setq sub (car threads))
4190         (if (stringp (car sub))
4191             ;; This is a gathered thread, so we look at the roots
4192             ;; below it to find whether this article is in this
4193             ;; gathered root.
4194             (progn
4195               (setq sub (cdr sub))
4196               (while sub
4197                 (when (member (caar sub) headers)
4198                   (setq thread (car threads)
4199                         threads nil
4200                         sub nil))
4201                 (setq sub (cdr sub))))
4202           ;; It's an ordinary thread, so we check it.
4203           (when (eq (car sub) (car headers))
4204             (setq thread sub
4205                   threads nil)))
4206         (setq threads (cdr threads)))
4207       ;; If this article is in no thread, then it's a root.
4208       (if thread
4209           (unless dont-remove
4210             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4211         (setq thread (gnus-id-to-thread last-id)))
4212       (when thread
4213         (prog1
4214             thread                      ; We return this thread.
4215           (unless dont-remove
4216             (if (stringp (car thread))
4217                 (progn
4218                   ;; If we use dummy roots, then we have to remove the
4219                   ;; dummy root as well.
4220                   (when (eq gnus-summary-make-false-root 'dummy)
4221                     ;; We go to the dummy root by going to
4222                     ;; the first sub-"thread", and then one line up.
4223                     (gnus-summary-goto-article
4224                      (mail-header-number (caadr thread)))
4225                     (forward-line -1)
4226                     (gnus-delete-line)
4227                     (gnus-data-compute-positions))
4228                   (setq thread (cdr thread))
4229                   (while thread
4230                     (gnus-remove-thread-1 (car thread))
4231                     (setq thread (cdr thread))))
4232               (gnus-remove-thread-1 thread))))))))
4233
4234 (defun gnus-remove-thread-1 (thread)
4235   "Remove the thread THREAD recursively."
4236   (let ((number (mail-header-number (pop thread)))
4237         d)
4238     (setq thread (reverse thread))
4239     (while thread
4240       (gnus-remove-thread-1 (pop thread)))
4241     (when (setq d (gnus-data-find number))
4242       (goto-char (gnus-data-pos d))
4243       (gnus-summary-show-thread)
4244       (gnus-data-remove
4245        number
4246        (- (gnus-point-at-bol)
4247           (prog1
4248               (1+ (gnus-point-at-eol))
4249             (gnus-delete-line)))))))
4250
4251 (defun gnus-sort-threads-1 (threads func)
4252   (sort (mapcar (lambda (thread)
4253                   (cons (car thread)
4254                         (and (cdr thread)
4255                              (gnus-sort-threads-1 (cdr thread) func))))
4256                 threads) func))
4257
4258 (defun gnus-sort-threads (threads)
4259   "Sort THREADS."
4260   (if (not gnus-thread-sort-functions)
4261       threads
4262     (gnus-message 8 "Sorting threads...")
4263     (let ((max-lisp-eval-depth 5000))
4264       (prog1 (gnus-sort-threads-1
4265          threads
4266          (gnus-make-sort-function gnus-thread-sort-functions))
4267         (gnus-message 8 "Sorting threads...done")))))
4268
4269 (defun gnus-sort-articles (articles)
4270   "Sort ARTICLES."
4271   (when gnus-article-sort-functions
4272     (gnus-message 7 "Sorting articles...")
4273     (prog1
4274         (setq gnus-newsgroup-headers
4275               (sort articles (gnus-make-sort-function
4276                               gnus-article-sort-functions)))
4277       (gnus-message 7 "Sorting articles...done"))))
4278
4279 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4280 (defmacro gnus-thread-header (thread)
4281   "Return header of first article in THREAD.
4282 Note that THREAD must never, ever be anything else than a variable -
4283 using some other form will lead to serious barfage."
4284   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4285   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4286   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4287         (vector thread) 2))
4288
4289 (defsubst gnus-article-sort-by-number (h1 h2)
4290   "Sort articles by article number."
4291   (< (mail-header-number h1)
4292      (mail-header-number h2)))
4293
4294 (defun gnus-thread-sort-by-number (h1 h2)
4295   "Sort threads by root article number."
4296   (gnus-article-sort-by-number
4297    (gnus-thread-header h1) (gnus-thread-header h2)))
4298
4299 (defsubst gnus-article-sort-by-random (h1 h2)
4300   "Sort articles by article number."
4301   (zerop (random 2)))
4302
4303 (defun gnus-thread-sort-by-random (h1 h2)
4304   "Sort threads by root article number."
4305   (gnus-article-sort-by-random
4306    (gnus-thread-header h1) (gnus-thread-header h2)))
4307
4308 (defsubst gnus-article-sort-by-lines (h1 h2)
4309   "Sort articles by article Lines header."
4310   (< (mail-header-lines h1)
4311      (mail-header-lines h2)))
4312
4313 (defun gnus-thread-sort-by-lines (h1 h2)
4314   "Sort threads by root article Lines header."
4315   (gnus-article-sort-by-lines
4316    (gnus-thread-header h1) (gnus-thread-header h2)))
4317
4318 (defsubst gnus-article-sort-by-chars (h1 h2)
4319   "Sort articles by octet length."
4320   (< (mail-header-chars h1)
4321      (mail-header-chars h2)))
4322
4323 (defun gnus-thread-sort-by-chars (h1 h2)
4324   "Sort threads by root article octet length."
4325   (gnus-article-sort-by-chars
4326    (gnus-thread-header h1) (gnus-thread-header h2)))
4327
4328 (defsubst gnus-article-sort-by-author (h1 h2)
4329   "Sort articles by root author."
4330   (string-lessp
4331    (let ((extract (funcall
4332                    gnus-extract-address-components
4333                    (mail-header-from h1))))
4334      (or (car extract) (cadr extract) ""))
4335    (let ((extract (funcall
4336                    gnus-extract-address-components
4337                    (mail-header-from h2))))
4338      (or (car extract) (cadr extract) ""))))
4339
4340 (defun gnus-thread-sort-by-author (h1 h2)
4341   "Sort threads by root author."
4342   (gnus-article-sort-by-author
4343    (gnus-thread-header h1)  (gnus-thread-header h2)))
4344
4345 (defsubst gnus-article-sort-by-subject (h1 h2)
4346   "Sort articles by root subject."
4347   (string-lessp
4348    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4349    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4350
4351 (defun gnus-thread-sort-by-subject (h1 h2)
4352   "Sort threads by root subject."
4353   (gnus-article-sort-by-subject
4354    (gnus-thread-header h1) (gnus-thread-header h2)))
4355
4356 (defsubst gnus-article-sort-by-date (h1 h2)
4357   "Sort articles by root article date."
4358   (time-less-p
4359    (gnus-date-get-time (mail-header-date h1))
4360    (gnus-date-get-time (mail-header-date h2))))
4361
4362 (defun gnus-thread-sort-by-date (h1 h2)
4363   "Sort threads by root article date."
4364   (gnus-article-sort-by-date
4365    (gnus-thread-header h1) (gnus-thread-header h2)))
4366
4367 (defsubst gnus-article-sort-by-score (h1 h2)
4368   "Sort articles by root article score.
4369 Unscored articles will be counted as having a score of zero."
4370   (> (or (cdr (assq (mail-header-number h1)
4371                     gnus-newsgroup-scored))
4372          gnus-summary-default-score 0)
4373      (or (cdr (assq (mail-header-number h2)
4374                     gnus-newsgroup-scored))
4375          gnus-summary-default-score 0)))
4376
4377 (defun gnus-thread-sort-by-score (h1 h2)
4378   "Sort threads by root article score."
4379   (gnus-article-sort-by-score
4380    (gnus-thread-header h1) (gnus-thread-header h2)))
4381
4382 (defun gnus-thread-sort-by-total-score (h1 h2)
4383   "Sort threads by the sum of all scores in the thread.
4384 Unscored articles will be counted as having a score of zero."
4385   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4386
4387 (defun gnus-thread-total-score (thread)
4388   ;; This function find the total score of THREAD.
4389   (cond
4390    ((null thread)
4391     0)
4392    ((consp thread)
4393     (if (stringp (car thread))
4394         (apply gnus-thread-score-function 0
4395                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4396       (gnus-thread-total-score-1 thread)))
4397    (t
4398     (gnus-thread-total-score-1 (list thread)))))
4399
4400 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4401   "Sort threads such that the thread with the most recently arrived article comes first."
4402   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4403
4404 (defun gnus-thread-highest-number (thread)
4405   "Return the highest article number in THREAD."
4406   (apply 'max (mapcar (lambda (header)
4407                         (mail-header-number header))
4408                       (message-flatten-list thread))))
4409
4410 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4411   "Sort threads such that the thread with the most recently dated article comes first."
4412   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4413
4414 (defun gnus-thread-latest-date (thread)
4415   "Return the highest article date in THREAD."
4416   (let ((previous-time 0))
4417     (apply 'max
4418            (mapcar
4419             (lambda (header)
4420               (setq previous-time
4421                     (time-to-seconds
4422                      (condition-case ()
4423                          (mail-header-parse-date (mail-header-date header))
4424                        (error previous-time)))))
4425             (sort
4426              (message-flatten-list thread)
4427              (lambda (h1 h2)
4428                (< (mail-header-number h1)
4429                   (mail-header-number h2))))))))
4430
4431 (defun gnus-thread-total-score-1 (root)
4432   ;; This function find the total score of the thread below ROOT.
4433   (setq root (car root))
4434   (apply gnus-thread-score-function
4435          (or (append
4436               (mapcar 'gnus-thread-total-score
4437                       (cdr (gnus-id-to-thread (mail-header-id root))))
4438               (when (> (mail-header-number root) 0)
4439                 (list (or (cdr (assq (mail-header-number root)
4440                                      gnus-newsgroup-scored))
4441                           gnus-summary-default-score 0))))
4442              (list gnus-summary-default-score)
4443              '(0))))
4444
4445 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4446 (defvar gnus-tmp-prev-subject nil)
4447 (defvar gnus-tmp-false-parent nil)
4448 (defvar gnus-tmp-root-expunged nil)
4449 (defvar gnus-tmp-dummy-line nil)
4450
4451 (eval-when-compile (defvar gnus-tmp-header))
4452 (defun gnus-extra-header (type &optional header)
4453   "Return the extra header of TYPE."
4454   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4455       ""))
4456
4457 (defvar gnus-tmp-thread-tree-header-string "")
4458
4459 (defcustom gnus-sum-thread-tree-root "> "
4460   "With %B spec, used for the root of a thread.
4461 If nil, use subject instead."
4462   :type 'string
4463   :group 'gnus-thread)
4464 (defcustom gnus-sum-thread-tree-single-indent ""
4465   "With %B spec, used for a thread with just one message.
4466 If nil, use subject instead."
4467   :type 'string
4468   :group 'gnus-thread)
4469 (defcustom gnus-sum-thread-tree-vertical "| "
4470   "With %B spec, used for drawing a vertical line."
4471   :type 'string
4472   :group 'gnus-thread)
4473 (defcustom gnus-sum-thread-tree-indent "  "
4474   "With %B spec, used for indenting."
4475   :type 'string
4476   :group 'gnus-thread)
4477 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4478   "With %B spec, used for a leaf with brothers."
4479   :type 'string
4480   :group 'gnus-thread)
4481 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4482   "With %B spec, used for a leaf without brothers."
4483   :type 'string
4484   :group 'gnus-thread)
4485
4486 (defun gnus-summary-prepare-threads (threads)
4487   "Prepare summary buffer from THREADS and indentation LEVEL.
4488 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4489 or a straight list of headers."
4490   (gnus-message 7 "Generating summary...")
4491
4492   (setq gnus-newsgroup-threads threads)
4493   (beginning-of-line)
4494
4495   (let ((gnus-tmp-level 0)
4496         (default-score (or gnus-summary-default-score 0))
4497         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4498         (building-line-count gnus-summary-display-while-building)
4499         (building-count (integerp gnus-summary-display-while-building))
4500         thread number subject stack state gnus-tmp-gathered beg-match
4501         new-roots gnus-tmp-new-adopts thread-end simp-subject
4502         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4503         gnus-tmp-replied gnus-tmp-subject-or-nil
4504         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4505         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4506         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4507         tree-stack)
4508
4509     (setq gnus-tmp-prev-subject nil)
4510
4511     (if (vectorp (car threads))
4512         ;; If this is a straight (sic) list of headers, then a
4513         ;; threaded summary display isn't required, so we just create
4514         ;; an unthreaded one.
4515         (gnus-summary-prepare-unthreaded threads)
4516
4517       ;; Do the threaded display.
4518
4519       (if gnus-summary-display-while-building
4520           (switch-to-buffer (buffer-name)))
4521       (while (or threads stack gnus-tmp-new-adopts new-roots)
4522
4523         (if (and (= gnus-tmp-level 0)
4524                  (or (not stack)
4525                      (= (caar stack) 0))
4526                  (not gnus-tmp-false-parent)
4527                  (or gnus-tmp-new-adopts new-roots))
4528             (if gnus-tmp-new-adopts
4529                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4530                       thread (list (car gnus-tmp-new-adopts))
4531                       gnus-tmp-header (caar thread)
4532                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4533               (when new-roots
4534                 (setq thread (list (car new-roots))
4535                       gnus-tmp-header (caar thread)
4536                       new-roots (cdr new-roots))))
4537
4538           (if threads
4539               ;; If there are some threads, we do them before the
4540               ;; threads on the stack.
4541               (setq thread threads
4542                     gnus-tmp-header (caar thread))
4543             ;; There were no current threads, so we pop something off
4544             ;; the stack.
4545             (setq state (car stack)
4546                   gnus-tmp-level (car state)
4547                   tree-stack (cadr state)
4548                   thread (caddr state)
4549                   stack (cdr stack)
4550                   gnus-tmp-header (caar thread))))
4551
4552         (setq gnus-tmp-false-parent nil)
4553         (setq gnus-tmp-root-expunged nil)
4554         (setq thread-end nil)
4555
4556         (if (stringp gnus-tmp-header)
4557             ;; The header is a dummy root.
4558             (cond
4559              ((eq gnus-summary-make-false-root 'adopt)
4560               ;; We let the first article adopt the rest.
4561               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4562                                                (cddar thread)))
4563               (setq gnus-tmp-gathered
4564                     (nconc (mapcar
4565                             (lambda (h) (mail-header-number (car h)))
4566                             (cddar thread))
4567                            gnus-tmp-gathered))
4568               (setq thread (cons (list (caar thread)
4569                                        (cadar thread))
4570                                  (cdr thread)))
4571               (setq gnus-tmp-level -1
4572                     gnus-tmp-false-parent t))
4573              ((eq gnus-summary-make-false-root 'empty)
4574               ;; We print adopted articles with empty subject fields.
4575               (setq gnus-tmp-gathered
4576                     (nconc (mapcar
4577                             (lambda (h) (mail-header-number (car h)))
4578                             (cddar thread))
4579                            gnus-tmp-gathered))
4580               (setq gnus-tmp-level -1))
4581              ((eq gnus-summary-make-false-root 'dummy)
4582               ;; We remember that we probably want to output a dummy
4583               ;; root.
4584               (setq gnus-tmp-dummy-line gnus-tmp-header)
4585               (setq gnus-tmp-prev-subject gnus-tmp-header))
4586              (t
4587               ;; We do not make a root for the gathered
4588               ;; sub-threads at all.
4589               (setq gnus-tmp-level -1)))
4590
4591           (setq number (mail-header-number gnus-tmp-header)
4592                 subject (mail-header-subject gnus-tmp-header)
4593                 simp-subject (gnus-simplify-subject-fully subject))
4594
4595           (cond
4596            ;; If the thread has changed subject, we might want to make
4597            ;; this subthread into a root.
4598            ((and (null gnus-thread-ignore-subject)
4599                  (not (zerop gnus-tmp-level))
4600                  gnus-tmp-prev-subject
4601                  (not (string= gnus-tmp-prev-subject simp-subject)))
4602             (setq new-roots (nconc new-roots (list (car thread)))
4603                   thread-end t
4604                   gnus-tmp-header nil))
4605            ;; If the article lies outside the current limit,
4606            ;; then we do not display it.
4607            ((not (memq number gnus-newsgroup-limit))
4608             (setq gnus-tmp-gathered
4609                   (nconc (mapcar
4610                           (lambda (h) (mail-header-number (car h)))
4611                           (cdar thread))
4612                          gnus-tmp-gathered))
4613             (setq gnus-tmp-new-adopts (if (cdar thread)
4614                                           (append gnus-tmp-new-adopts
4615                                                   (cdar thread))
4616                                         gnus-tmp-new-adopts)
4617                   thread-end t
4618                   gnus-tmp-header nil)
4619             (when (zerop gnus-tmp-level)
4620               (setq gnus-tmp-root-expunged t)))
4621            ;; Perhaps this article is to be marked as read?
4622            ((and gnus-summary-mark-below
4623                  (< (or (cdr (assq number gnus-newsgroup-scored))
4624                         default-score)
4625                     gnus-summary-mark-below)
4626                  ;; Don't touch sparse articles.
4627                  (not (gnus-summary-article-sparse-p number))
4628                  (not (gnus-summary-article-ancient-p number)))
4629             (setq gnus-newsgroup-unreads
4630                   (delq number gnus-newsgroup-unreads))
4631             (if gnus-newsgroup-auto-expire
4632                 (setq gnus-newsgroup-expirable
4633                       (gnus-add-to-sorted-list
4634                        gnus-newsgroup-expirable number))
4635               (push (cons number gnus-low-score-mark)
4636                     gnus-newsgroup-reads))))
4637
4638           (when gnus-tmp-header
4639             ;; We may have an old dummy line to output before this
4640             ;; article.
4641             (when (and gnus-tmp-dummy-line
4642                        (gnus-subject-equal
4643                         gnus-tmp-dummy-line
4644                         (mail-header-subject gnus-tmp-header)))
4645               (gnus-summary-insert-dummy-line
4646                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4647               (setq gnus-tmp-dummy-line nil))
4648
4649             ;; Compute the mark.
4650             (setq gnus-tmp-unread (gnus-article-mark number))
4651
4652             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4653                                   gnus-tmp-header gnus-tmp-level)
4654                   gnus-newsgroup-data)
4655
4656             ;; Actually insert the line.
4657             (setq
4658              gnus-tmp-subject-or-nil
4659              (cond
4660               ((and gnus-thread-ignore-subject
4661                     gnus-tmp-prev-subject
4662                     (not (string= gnus-tmp-prev-subject simp-subject)))
4663                subject)
4664               ((zerop gnus-tmp-level)
4665                (if (and (eq gnus-summary-make-false-root 'empty)
4666                         (memq number gnus-tmp-gathered)
4667                         gnus-tmp-prev-subject
4668                         (string= gnus-tmp-prev-subject simp-subject))
4669                    gnus-summary-same-subject
4670                  subject))
4671               (t gnus-summary-same-subject)))
4672             (if (and (eq gnus-summary-make-false-root 'adopt)
4673                      (= gnus-tmp-level 1)
4674                      (memq number gnus-tmp-gathered))
4675                 (setq gnus-tmp-opening-bracket ?\<
4676                       gnus-tmp-closing-bracket ?\>)
4677               (setq gnus-tmp-opening-bracket ?\[
4678                     gnus-tmp-closing-bracket ?\]))
4679             (setq
4680              gnus-tmp-indentation
4681              (aref gnus-thread-indent-array gnus-tmp-level)
4682              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4683              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4684                                 gnus-summary-default-score 0)
4685              gnus-tmp-score-char
4686              (if (or (null gnus-summary-default-score)
4687                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4688                          gnus-summary-zcore-fuzz))
4689                  ?                      ;Whitespace
4690                (if (< gnus-tmp-score gnus-summary-default-score)
4691                    gnus-score-below-mark gnus-score-over-mark))
4692              gnus-tmp-replied
4693              (cond ((memq number gnus-newsgroup-processable)
4694                     gnus-process-mark)
4695                    ((memq number gnus-newsgroup-cached)
4696                     gnus-cached-mark)
4697                    ((memq number gnus-newsgroup-replied)
4698                     gnus-replied-mark)
4699                    ((memq number gnus-newsgroup-forwarded)
4700                     gnus-forwarded-mark)
4701                    ((memq number gnus-newsgroup-saved)
4702                     gnus-saved-mark)
4703                    ((memq number gnus-newsgroup-recent)
4704                     gnus-recent-mark)
4705                    ((memq number gnus-newsgroup-unseen)
4706                     gnus-unseen-mark)
4707                    (t gnus-no-mark))
4708              gnus-tmp-downloaded
4709              (cond ((memq number gnus-newsgroup-undownloaded) 
4710                     gnus-undownloaded-mark)
4711                    (gnus-newsgroup-agentized
4712                     gnus-downloaded-mark)
4713                    (t
4714                     gnus-no-mark))
4715              gnus-tmp-from (mail-header-from gnus-tmp-header)
4716              gnus-tmp-name
4717              (cond
4718               ((string-match "<[^>]+> *$" gnus-tmp-from)
4719                (setq beg-match (match-beginning 0))
4720                (or (and (string-match "^\".+\"" gnus-tmp-from)
4721                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4722                    (substring gnus-tmp-from 0 beg-match)))
4723               ((string-match "(.+)" gnus-tmp-from)
4724                (substring gnus-tmp-from
4725                           (1+ (match-beginning 0)) (1- (match-end 0))))
4726               (t gnus-tmp-from))
4727              gnus-tmp-thread-tree-header-string
4728              (cond
4729               ((not gnus-show-threads) "")
4730               ((zerop gnus-tmp-level)
4731                (if (cdar thread)
4732                    (or gnus-sum-thread-tree-root subject)
4733                  (or gnus-sum-thread-tree-single-indent subject)))
4734               (t
4735                (concat (apply 'concat
4736                               (mapcar (lambda (item)
4737                                         (if (= item 1)
4738                                             gnus-sum-thread-tree-vertical
4739                                           gnus-sum-thread-tree-indent))
4740                                       (cdr (reverse tree-stack))))
4741                        (if (nth 1 thread)
4742                            gnus-sum-thread-tree-leaf-with-other
4743                          gnus-sum-thread-tree-single-leaf)))))
4744             (when (string= gnus-tmp-name "")
4745               (setq gnus-tmp-name gnus-tmp-from))
4746             (unless (numberp gnus-tmp-lines)
4747               (setq gnus-tmp-lines -1))
4748             (if (= gnus-tmp-lines -1)
4749                 (setq gnus-tmp-lines "?")
4750               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4751             (gnus-put-text-property
4752              (point)
4753              (progn (eval gnus-summary-line-format-spec) (point))
4754              'gnus-number number)
4755             (when gnus-visual-p
4756               (forward-line -1)
4757               (gnus-run-hooks 'gnus-summary-update-hook)
4758               (forward-line 1))
4759
4760             (setq gnus-tmp-prev-subject simp-subject)))
4761
4762         (when (nth 1 thread)
4763           (push (list (max 0 gnus-tmp-level)
4764                       (copy-list tree-stack)
4765                       (nthcdr 1 thread))
4766                 stack))
4767         (push (if (nth 1 thread) 1 0) tree-stack)
4768         (incf gnus-tmp-level)
4769         (setq threads (if thread-end nil (cdar thread)))
4770         (if gnus-summary-display-while-building
4771             (if building-count
4772                 (progn
4773                   ;; use a set frequency
4774                   (setq building-line-count (1- building-line-count))
4775                   (when (= building-line-count 0)
4776                     (sit-for 0)
4777                     (setq building-line-count
4778                           gnus-summary-display-while-building)))
4779               ;; always
4780               (sit-for 0)))
4781         (unless threads
4782           (setq gnus-tmp-level 0)))))
4783   (gnus-message 7 "Generating summary...done"))
4784
4785 (defun gnus-summary-prepare-unthreaded (headers)
4786   "Generate an unthreaded summary buffer based on HEADERS."
4787   (let (header number mark)
4788
4789     (beginning-of-line)
4790
4791     (while headers
4792       ;; We may have to root out some bad articles...
4793       (when (memq (setq number (mail-header-number
4794                                 (setq header (pop headers))))
4795                   gnus-newsgroup-limit)
4796         ;; Mark article as read when it has a low score.
4797         (when (and gnus-summary-mark-below
4798                    (< (or (cdr (assq number gnus-newsgroup-scored))
4799                           gnus-summary-default-score 0)
4800                       gnus-summary-mark-below)
4801                    (not (gnus-summary-article-ancient-p number)))
4802           (setq gnus-newsgroup-unreads
4803                 (delq number gnus-newsgroup-unreads))
4804           (if gnus-newsgroup-auto-expire
4805               (push number gnus-newsgroup-expirable)
4806             (push (cons number gnus-low-score-mark)
4807                   gnus-newsgroup-reads)))
4808
4809         (setq mark (gnus-article-mark number))
4810         (push (gnus-data-make number mark (1+ (point)) header 0)
4811               gnus-newsgroup-data)
4812         (gnus-summary-insert-line
4813          header 0 number
4814          (memq number gnus-newsgroup-undownloaded)
4815          mark (memq number gnus-newsgroup-replied)
4816          (memq number gnus-newsgroup-expirable)
4817          (mail-header-subject header) nil
4818          (cdr (assq number gnus-newsgroup-scored))
4819          (memq number gnus-newsgroup-processable))))))
4820
4821 (defun gnus-summary-remove-list-identifiers ()
4822   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4823   (let ((regexp (if (consp gnus-list-identifiers)
4824                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4825                   gnus-list-identifiers))
4826         changed subject)
4827     (when regexp
4828       (dolist (header gnus-newsgroup-headers)
4829         (setq subject (mail-header-subject header)
4830               changed nil)
4831         (while (string-match
4832                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4833                 subject)
4834           (setq subject
4835                 (concat (substring subject 0 (match-beginning 2))
4836                         (substring subject (match-end 0)))
4837                 changed t))
4838         (when (and changed
4839                    (string-match
4840                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4841           (setq subject
4842                 (concat (substring subject 0 (match-beginning 1))
4843                         (substring subject (match-end 1)))))
4844         (when changed
4845           (mail-header-set-subject header subject))))))
4846
4847 (defun gnus-fetch-headers (articles)
4848   "Fetch headers of ARTICLES."
4849   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4850     (gnus-message 5 "Fetching headers for %s..." name)
4851     (prog1
4852         (if (eq 'nov
4853                 (setq gnus-headers-retrieved-by
4854                       (gnus-retrieve-headers
4855                        articles gnus-newsgroup-name
4856                        ;; We might want to fetch old headers, but
4857                        ;; not if there is only 1 article.
4858                        (and (or (and
4859                                  (not (eq gnus-fetch-old-headers 'some))
4860                                  (not (numberp gnus-fetch-old-headers)))
4861                                 (> (length articles) 1))
4862                             gnus-fetch-old-headers))))
4863             (gnus-get-newsgroup-headers-xover
4864              articles nil nil gnus-newsgroup-name t)
4865           (gnus-get-newsgroup-headers))
4866       (gnus-message 5 "Fetching headers for %s...done" name))))
4867
4868 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4869   "Select newsgroup GROUP.
4870 If READ-ALL is non-nil, all articles in the group are selected.
4871 If SELECT-ARTICLES, only select those articles from GROUP."
4872   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4873          ;;!!! Dirty hack; should be removed.
4874          (gnus-summary-ignore-duplicates
4875           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4876               t
4877             gnus-summary-ignore-duplicates))
4878          (info (nth 2 entry))
4879          articles fetched-articles cached)
4880
4881     (unless (gnus-check-server
4882              (set (make-local-variable 'gnus-current-select-method)
4883                   (gnus-find-method-for-group group)))
4884       (error "Couldn't open server"))
4885
4886     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4887         (gnus-activate-group group)     ; Or we can activate it...
4888         (progn                          ; Or we bug out.
4889           (when (equal major-mode 'gnus-summary-mode)
4890             (kill-buffer (current-buffer)))
4891           (error "Couldn't activate group %s: %s"
4892                  group (gnus-status-message group))))
4893
4894     (unless (gnus-request-group group t)
4895       (when (equal major-mode 'gnus-summary-mode)
4896         (kill-buffer (current-buffer)))
4897       (error "Couldn't request group %s: %s"
4898              group (gnus-status-message group)))
4899
4900     (setq gnus-newsgroup-name group
4901           gnus-newsgroup-unselected nil
4902           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4903
4904     (let ((display (gnus-group-find-parameter group 'display)))
4905       (setq gnus-newsgroup-display
4906             (cond
4907              ((not (zerop (or (car-safe read-all) 0)))
4908               ;; The user entered the group with C-u SPC/RET, let's show
4909               ;; all articles.
4910               'gnus-not-ignore)
4911              ((eq display 'all)
4912               'gnus-not-ignore)
4913              ((arrayp display)
4914               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4915              ((numberp display)
4916               ;; The following is probably the "correct" solution, but
4917               ;; it makes Gnus fetch all headers and then limit the
4918               ;; articles (which is slow), so instead we hack the
4919               ;; select-articles parameter instead. -- Simon Josefsson
4920               ;; <jas@kth.se>
4921               ;;
4922               ;; (gnus-byte-compile
4923               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4924               ;;                         display)))))
4925               (setq select-articles
4926                     (gnus-uncompress-range
4927                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4928                              (if (> tmp 0)
4929                                  tmp
4930                                1))
4931                            (cdr (gnus-active group)))))
4932               nil)
4933              (t
4934               nil))))
4935
4936     (gnus-summary-setup-default-charset)
4937
4938     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4939     (when (gnus-virtual-group-p group)
4940       (setq cached gnus-newsgroup-cached))
4941
4942     (setq gnus-newsgroup-unreads
4943           (gnus-sorted-ndifference
4944            (gnus-sorted-ndifference gnus-newsgroup-unreads
4945                                     gnus-newsgroup-marked)
4946            gnus-newsgroup-dormant))
4947
4948     (setq gnus-newsgroup-processable nil)
4949
4950     (gnus-update-read-articles group gnus-newsgroup-unreads)
4951
4952     ;; Adjust and set lists of article marks.
4953     (when info
4954       (gnus-adjust-marked-articles info))
4955     (if (setq articles select-articles)
4956         (setq gnus-newsgroup-unselected
4957               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4958       (setq articles (gnus-articles-to-read group read-all)))
4959
4960     (cond
4961      ((null articles)
4962       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4963       'quit)
4964      ((eq articles 0) nil)
4965      (t
4966       ;; Init the dependencies hash table.
4967       (setq gnus-newsgroup-dependencies
4968             (gnus-make-hashtable (length articles)))
4969       (gnus-set-global-variables)
4970       ;; Retrieve the headers and read them in.
4971
4972       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4973
4974       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4975       (when cached
4976         (setq gnus-newsgroup-cached cached))
4977
4978       ;; Suppress duplicates?
4979       (when gnus-suppress-duplicates
4980         (gnus-dup-suppress-articles))
4981
4982       ;; Set the initial limit.
4983       (setq gnus-newsgroup-limit (copy-sequence articles))
4984       ;; Remove canceled articles from the list of unread articles.
4985       (setq fetched-articles
4986             (mapcar (lambda (headers) (mail-header-number headers))
4987                     gnus-newsgroup-headers))
4988       (setq gnus-newsgroup-articles fetched-articles)
4989       (setq gnus-newsgroup-unreads
4990             (gnus-sorted-nintersection
4991              gnus-newsgroup-unreads fetched-articles))
4992       (gnus-compute-unseen-list)
4993
4994       ;; Removed marked articles that do not exist.
4995       (gnus-update-missing-marks
4996        (gnus-sorted-difference articles fetched-articles))
4997       ;; We might want to build some more threads first.
4998       (when (and gnus-fetch-old-headers
4999                  (eq gnus-headers-retrieved-by 'nov))
5000         (if (eq gnus-fetch-old-headers 'invisible)
5001             (gnus-build-all-threads)
5002           (gnus-build-old-threads)))
5003       ;; Let the Gnus agent mark articles as read.
5004       (when gnus-agent
5005         (gnus-agent-get-undownloaded-list))
5006       ;; Remove list identifiers from subject
5007       (when gnus-list-identifiers
5008         (gnus-summary-remove-list-identifiers))
5009       ;; Check whether auto-expire is to be done in this group.
5010       (setq gnus-newsgroup-auto-expire
5011             (gnus-group-auto-expirable-p group))
5012       ;; Set up the article buffer now, if necessary.
5013       (unless gnus-single-article-buffer
5014         (gnus-article-setup-buffer))
5015       ;; First and last article in this newsgroup.
5016       (when gnus-newsgroup-headers
5017         (setq gnus-newsgroup-begin
5018               (mail-header-number (car gnus-newsgroup-headers))
5019               gnus-newsgroup-end
5020               (mail-header-number
5021                (gnus-last-element gnus-newsgroup-headers))))
5022       ;; GROUP is successfully selected.
5023       (or gnus-newsgroup-headers t)))))
5024
5025 (defun gnus-compute-unseen-list ()
5026   ;; The `seen' marks are treated specially.
5027   (if (not gnus-newsgroup-seen)
5028       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5029     (setq gnus-newsgroup-unseen
5030           (gnus-inverse-list-range-intersection
5031            gnus-newsgroup-articles gnus-newsgroup-seen))))
5032
5033 (defun gnus-summary-display-make-predicate (display)
5034   (require 'gnus-agent)
5035   (when (= (length display) 1)
5036     (setq display (car display)))
5037   (unless gnus-summary-display-cache
5038     (dolist (elem (append '((unread . unread)
5039                             (read . read)
5040                             (unseen . unseen))
5041                           gnus-article-mark-lists))
5042       (push (cons (cdr elem)
5043                   (gnus-byte-compile
5044                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5045             gnus-summary-display-cache)))
5046   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5047         (gnus-category-predicate-cache gnus-summary-display-cache))
5048     (gnus-get-predicate display)))
5049
5050 ;; Uses the dynamically bound `number' variable.
5051 (eval-when-compile
5052   (defvar number))
5053 (defun gnus-article-marked-p (type &optional article)
5054   (let ((article (or article number)))
5055     (cond
5056      ((eq type 'tick)
5057       (memq article gnus-newsgroup-marked))
5058      ((eq type 'spam)
5059       (memq article gnus-newsgroup-spam-marked))
5060      ((eq type 'unsend)
5061       (memq article gnus-newsgroup-unsendable))
5062      ((eq type 'undownload)
5063       (memq article gnus-newsgroup-undownloaded))
5064      ((eq type 'download)
5065       (memq article gnus-newsgroup-downloadable))
5066      ((eq type 'unread)
5067       (memq article gnus-newsgroup-unreads))
5068      ((eq type 'read)
5069       (memq article gnus-newsgroup-reads))
5070      ((eq type 'dormant)
5071       (memq article gnus-newsgroup-dormant) )
5072      ((eq type 'expire)
5073       (memq article gnus-newsgroup-expirable))
5074      ((eq type 'reply)
5075       (memq article gnus-newsgroup-replied))
5076      ((eq type 'killed)
5077       (memq article gnus-newsgroup-killed))
5078      ((eq type 'bookmark)
5079       (assq article gnus-newsgroup-bookmarks))
5080      ((eq type 'score)
5081       (assq article gnus-newsgroup-scored))
5082      ((eq type 'save)
5083       (memq article gnus-newsgroup-saved))
5084      ((eq type 'cache)
5085       (memq article gnus-newsgroup-cached))
5086      ((eq type 'forward)
5087       (memq article gnus-newsgroup-forwarded))
5088      ((eq type 'seen)
5089       (not (memq article gnus-newsgroup-unseen)))
5090      ((eq type 'recent)
5091       (memq article gnus-newsgroup-recent))
5092      (t t))))
5093
5094 (defun gnus-articles-to-read (group &optional read-all)
5095   "Find out what articles the user wants to read."
5096   (let* ((display (gnus-group-find-parameter group 'display))
5097          (articles
5098           ;; Select all articles if `read-all' is non-nil, or if there
5099           ;; are no unread articles.
5100           (if (or read-all
5101                   (and (zerop (length gnus-newsgroup-marked))
5102                        (zerop (length gnus-newsgroup-unreads)))
5103                   ;; Fetch all if the predicate is non-nil.
5104                   gnus-newsgroup-display)
5105               ;; We want to select the headers for all the articles in
5106               ;; the group, so we select either all the active
5107               ;; articles in the group, or (if that's nil), the
5108               ;; articles in the cache.
5109               (or
5110                (gnus-uncompress-range (gnus-active group))
5111                (gnus-cache-articles-in-group group))
5112             ;; Select only the "normal" subset of articles.
5113             (gnus-sorted-nunion
5114              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5115              gnus-newsgroup-unreads)))
5116          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5117          (scored (length scored-list))
5118          (number (length articles))
5119          (marked (+ (length gnus-newsgroup-marked)
5120                     (length gnus-newsgroup-dormant)))
5121          (select
5122           (cond
5123            ((numberp read-all)
5124             read-all)
5125            ((numberp gnus-newsgroup-display)
5126             gnus-newsgroup-display)
5127            (t
5128             (condition-case ()
5129                 (cond
5130                  ((and (or (<= scored marked) (= scored number))
5131                        (numberp gnus-large-newsgroup)
5132                        (> number gnus-large-newsgroup))
5133                   (let* ((cursor-in-echo-area nil)
5134                          (initial (gnus-parameter-large-newsgroup-initial
5135                                    gnus-newsgroup-name))
5136                          (input
5137                           (read-string
5138                            (format
5139                             "How many articles from %s (%s %d): "
5140                             (gnus-limit-string
5141                              (gnus-group-decoded-name gnus-newsgroup-name)
5142                              35)
5143                             (if initial "max" "default")
5144                             number)
5145                            (if initial
5146                                (cons (number-to-string initial)
5147                                      0)))))
5148                     (if (string-match "^[ \t]*$" input) number input)))
5149                  ((and (> scored marked) (< scored number)
5150                        (> (- scored number) 20))
5151                   (let ((input
5152                          (read-string
5153                           (format "%s %s (%d scored, %d total): "
5154                                   "How many articles from"
5155                                   (gnus-group-decoded-name group)
5156                                   scored number))))
5157                     (if (string-match "^[ \t]*$" input)
5158                         number input)))
5159                  (t number))
5160               (quit
5161                (message "Quit getting the articles to read")
5162                nil))))))
5163     (setq select (if (stringp select) (string-to-number select) select))
5164     (if (or (null select) (zerop select))
5165         select
5166       (if (and (not (zerop scored)) (<= (abs select) scored))
5167           (progn
5168             (setq articles (sort scored-list '<))
5169             (setq number (length articles)))
5170         (setq articles (copy-sequence articles)))
5171
5172       (when (< (abs select) number)
5173         (if (< select 0)
5174             ;; Select the N oldest articles.
5175             (setcdr (nthcdr (1- (abs select)) articles) nil)
5176           ;; Select the N most recent articles.
5177           (setq articles (nthcdr (- number select) articles))))
5178       (setq gnus-newsgroup-unselected
5179             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5180       (when gnus-alter-articles-to-read-function
5181         (setq gnus-newsgroup-unreads
5182               (sort
5183                (funcall gnus-alter-articles-to-read-function
5184                         gnus-newsgroup-name gnus-newsgroup-unreads)
5185                '<)))
5186       articles)))
5187
5188 (defun gnus-killed-articles (killed articles)
5189   (let (out)
5190     (while articles
5191       (when (inline (gnus-member-of-range (car articles) killed))
5192         (push (car articles) out))
5193       (setq articles (cdr articles)))
5194     out))
5195
5196 (defun gnus-uncompress-marks (marks)
5197   "Uncompress the mark ranges in MARKS."
5198   (let ((uncompressed '(score bookmark))
5199         out)
5200     (while marks
5201       (if (memq (caar marks) uncompressed)
5202           (push (car marks) out)
5203         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5204       (setq marks (cdr marks)))
5205     out))
5206
5207 (defun gnus-article-mark-to-type (mark)
5208   "Return the type of MARK."
5209   (or (cadr (assq mark gnus-article-special-mark-lists))
5210       'list))
5211
5212 (defun gnus-article-unpropagatable-p (mark)
5213   "Return whether MARK should be propagated to backend."
5214   (memq mark gnus-article-unpropagated-mark-lists))
5215
5216 (defun gnus-adjust-marked-articles (info)
5217   "Set all article lists and remove all marks that are no longer valid."
5218   (let* ((marked-lists (gnus-info-marks info))
5219          (active (gnus-active (gnus-info-group info)))
5220          (min (car active))
5221          (max (cdr active))
5222          (types gnus-article-mark-lists)
5223          marks var articles article mark mark-type)
5224
5225     (dolist (marks marked-lists)
5226       (setq mark (car marks)
5227             mark-type (gnus-article-mark-to-type mark)
5228             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5229
5230       ;; We set the variable according to the type of the marks list,
5231       ;; and then adjust the marks to a subset of the active articles.
5232       (cond
5233        ;; Adjust "simple" lists.
5234        ((eq mark-type 'list)
5235         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5236         (when (memq mark '(tick dormant expire reply save))
5237           (while articles
5238             (when (or (< (setq article (pop articles)) min) (> article max))
5239               (set var (delq article (symbol-value var)))))))
5240        ;; Adjust assocs.
5241        ((eq mark-type 'tuple)
5242         (set var (setq articles (cdr marks)))
5243         (when (not (listp (cdr (symbol-value var))))
5244           (set var (list (symbol-value var))))
5245         (when (not (listp (cdr articles)))
5246           (setq articles (list articles)))
5247         (while articles
5248           (when (or (not (consp (setq article (pop articles))))
5249                     (< (car article) min)
5250                     (> (car article) max))
5251             (set var (delq article (symbol-value var))))))
5252        ;; Adjust ranges (sloppily).
5253        ((eq mark-type 'range)
5254         (cond
5255          ((eq mark 'seen)
5256           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5257           ;; It should be (seen (NUM1 . NUM2)).
5258           (when (numberp (cddr marks))
5259             (setcdr marks (list (cdr marks))))
5260           (setq articles (cdr marks))
5261           (while (and articles
5262                       (or (and (consp (car articles))
5263                                (> min (cdar articles)))
5264                           (and (numberp (car articles))
5265                                (> min (car articles)))))
5266             (pop articles))
5267           (set var articles))))))))
5268
5269 (defun gnus-update-missing-marks (missing)
5270   "Go through the list of MISSING articles and remove them from the mark lists."
5271   (when missing
5272     (let (var m)
5273       ;; Go through all types.
5274       (dolist (elem gnus-article-mark-lists)
5275         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5276           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5277           (when (symbol-value var)
5278             ;; This list has articles.  So we delete all missing
5279             ;; articles from it.
5280             (setq m missing)
5281             (while m
5282               (set var (delq (pop m) (symbol-value var))))))))))
5283
5284 (defun gnus-update-marks ()
5285   "Enter the various lists of marked articles into the newsgroup info list."
5286   (let ((types gnus-article-mark-lists)
5287         (info (gnus-get-info gnus-newsgroup-name))
5288         type list newmarked symbol delta-marks)
5289     (when info
5290       ;; Add all marks lists to the list of marks lists.
5291       (while (setq type (pop types))
5292         (setq list (symbol-value
5293                     (setq symbol
5294                           (intern (format "gnus-newsgroup-%s" (car type))))))
5295
5296         (when list
5297           ;; Get rid of the entries of the articles that have the
5298           ;; default score.
5299           (when (and (eq (cdr type) 'score)
5300                      gnus-save-score
5301                      list)
5302             (let* ((arts list)
5303                    (prev (cons nil list))
5304                    (all prev))
5305               (while arts
5306                 (if (or (not (consp (car arts)))
5307                         (= (cdar arts) gnus-summary-default-score))
5308                     (setcdr prev (cdr arts))
5309                   (setq prev arts))
5310                 (setq arts (cdr arts)))
5311               (setq list (cdr all)))))
5312
5313         (when (eq (cdr type) 'seen)
5314           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5315
5316         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5317           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5318
5319         (when (and (gnus-check-backend-function
5320                     'request-set-mark gnus-newsgroup-name)
5321                    (not (gnus-article-unpropagatable-p (cdr type))))
5322           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5323                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5324                  (add (gnus-remove-from-range
5325                        (gnus-copy-sequence list) old)))
5326             (when add
5327               (push (list add 'add (list (cdr type))) delta-marks))
5328             (when del
5329               (push (list del 'del (list (cdr type))) delta-marks))))
5330
5331         (when list
5332           (push (cons (cdr type) list) newmarked)))
5333
5334       (when delta-marks
5335         (unless (gnus-check-group gnus-newsgroup-name)
5336           (error "Can't open server for %s" gnus-newsgroup-name))
5337         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5338
5339       ;; Enter these new marks into the info of the group.
5340       (if (nthcdr 3 info)
5341           (setcar (nthcdr 3 info) newmarked)
5342         ;; Add the marks lists to the end of the info.
5343         (when newmarked
5344           (setcdr (nthcdr 2 info) (list newmarked))))
5345
5346       ;; Cut off the end of the info if there's nothing else there.
5347       (let ((i 5))
5348         (while (and (> i 2)
5349                     (not (nth i info)))
5350           (when (nthcdr (decf i) info)
5351             (setcdr (nthcdr i info) nil)))))))
5352
5353 (defun gnus-set-mode-line (where)
5354   "Set the mode line of the article or summary buffers.
5355 If WHERE is `summary', the summary mode line format will be used."
5356   ;; Is this mode line one we keep updated?
5357   (when (and (memq where gnus-updated-mode-lines)
5358              (symbol-value
5359               (intern (format "gnus-%s-mode-line-format-spec" where))))
5360     (let (mode-string)
5361       (save-excursion
5362         ;; We evaluate this in the summary buffer since these
5363         ;; variables are buffer-local to that buffer.
5364         (set-buffer gnus-summary-buffer)
5365        ;; We bind all these variables that are used in the `eval' form
5366         ;; below.
5367         (let* ((mformat (symbol-value
5368                          (intern
5369                           (format "gnus-%s-mode-line-format-spec" where))))
5370                (gnus-tmp-group-name (gnus-group-decoded-name
5371                                      gnus-newsgroup-name))
5372                (gnus-tmp-article-number (or gnus-current-article 0))
5373                (gnus-tmp-unread gnus-newsgroup-unreads)
5374                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5375                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5376                (gnus-tmp-unread-and-unselected
5377                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5378                             (zerop gnus-tmp-unselected))
5379                        "")
5380                       ((zerop gnus-tmp-unselected)
5381                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5382                       (t (format "{%d(+%d) more}"
5383                                  gnus-tmp-unread-and-unticked
5384                                  gnus-tmp-unselected))))
5385                (gnus-tmp-subject
5386                 (if (and gnus-current-headers
5387                          (vectorp gnus-current-headers))
5388                     (gnus-mode-string-quote
5389                      (mail-header-subject gnus-current-headers))
5390                   ""))
5391                bufname-length max-len
5392                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5393           (setq mode-string (eval mformat))
5394           (setq bufname-length (if (string-match "%b" mode-string)
5395                                    (- (length
5396                                        (buffer-name
5397                                         (if (eq where 'summary)
5398                                             nil
5399                                           (get-buffer gnus-article-buffer))))
5400                                       2)
5401                                  0))
5402           (setq max-len (max 4 (if gnus-mode-non-string-length
5403                                    (- (window-width)
5404                                       gnus-mode-non-string-length
5405                                       bufname-length)
5406                                  (length mode-string))))
5407           ;; We might have to chop a bit of the string off...
5408           (when (> (length mode-string) max-len)
5409             (setq mode-string
5410                   (concat (truncate-string-to-width mode-string (- max-len 3))
5411                           "...")))
5412           ;; Pad the mode string a bit.
5413           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5414       ;; Update the mode line.
5415       (setq mode-line-buffer-identification
5416             (gnus-mode-line-buffer-identification (list mode-string)))
5417       (set-buffer-modified-p t))))
5418
5419 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5420   "Go through the HEADERS list and add all Xrefs to a hash table.
5421 The resulting hash table is returned, or nil if no Xrefs were found."
5422   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5423          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5424          (xref-hashtb (gnus-make-hashtable))
5425          start group entry number xrefs header)
5426     (while headers
5427       (setq header (pop headers))
5428       (when (and (setq xrefs (mail-header-xref header))
5429                  (not (memq (setq number (mail-header-number header))
5430                             unreads)))
5431         (setq start 0)
5432         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5433           (setq start (match-end 0))
5434           (setq group (if prefix
5435                           (concat prefix (substring xrefs (match-beginning 1)
5436                                                     (match-end 1)))
5437                         (substring xrefs (match-beginning 1) (match-end 1))))
5438           (setq number
5439                 (string-to-int (substring xrefs (match-beginning 2)
5440                                           (match-end 2))))
5441           (if (setq entry (gnus-gethash group xref-hashtb))
5442               (setcdr entry (cons number (cdr entry)))
5443             (gnus-sethash group (cons number nil) xref-hashtb)))))
5444     (and start xref-hashtb)))
5445
5446 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5447   "Look through all the headers and mark the Xrefs as read."
5448   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5449         name entry info xref-hashtb idlist method nth4)
5450     (save-excursion
5451       (set-buffer gnus-group-buffer)
5452       (when (setq xref-hashtb
5453                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5454         (mapatoms
5455          (lambda (group)
5456            (unless (string= from-newsgroup (setq name (symbol-name group)))
5457              (setq idlist (symbol-value group))
5458              ;; Dead groups are not updated.
5459              (and (prog1
5460                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5461                             info (nth 2 entry))
5462                     (when (stringp (setq nth4 (gnus-info-method info)))
5463                       (setq nth4 (gnus-server-to-method nth4))))
5464                   ;; Only do the xrefs if the group has the same
5465                   ;; select method as the group we have just read.
5466                   (or (gnus-methods-equal-p
5467                        nth4 (gnus-find-method-for-group from-newsgroup))
5468                       virtual
5469                       (equal nth4 (setq method (gnus-find-method-for-group
5470                                                 from-newsgroup)))
5471                       (and (equal (car nth4) (car method))
5472                            (equal (nth 1 nth4) (nth 1 method))))
5473                   gnus-use-cross-reference
5474                   (or (not (eq gnus-use-cross-reference t))
5475                       virtual
5476                       ;; Only do cross-references on subscribed
5477                       ;; groups, if that is what is wanted.
5478                       (<= (gnus-info-level info) gnus-level-subscribed))
5479                   (gnus-group-make-articles-read name idlist))))
5480          xref-hashtb)))))
5481
5482 (defun gnus-compute-read-articles (group articles)
5483   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5484          (info (nth 2 entry))
5485          (active (gnus-active group))
5486          ninfo)
5487     (when entry
5488       ;; First peel off all invalid article numbers.
5489       (when active
5490         (let ((ids articles)
5491               id first)
5492           (while (setq id (pop ids))
5493             (when (and first (> id (cdr active)))
5494               ;; We'll end up in this situation in one particular
5495               ;; obscure situation.  If you re-scan a group and get
5496               ;; a new article that is cross-posted to a different
5497               ;; group that has not been re-scanned, you might get
5498               ;; crossposted article that has a higher number than
5499               ;; Gnus believes possible.  So we re-activate this
5500               ;; group as well.  This might mean doing the
5501               ;; crossposting thingy will *increase* the number
5502               ;; of articles in some groups.  Tsk, tsk.
5503               (setq active (or (gnus-activate-group group) active)))
5504             (when (or (> id (cdr active))
5505                       (< id (car active)))
5506               (setq articles (delq id articles))))))
5507       ;; If the read list is nil, we init it.
5508       (if (and active
5509                (null (gnus-info-read info))
5510                (> (car active) 1))
5511           (setq ninfo (cons 1 (1- (car active))))
5512         (setq ninfo (gnus-info-read info)))
5513       ;; Then we add the read articles to the range.
5514       (gnus-add-to-range
5515        ninfo (setq articles (sort articles '<))))))
5516
5517 (defun gnus-group-make-articles-read (group articles)
5518   "Update the info of GROUP to say that ARTICLES are read."
5519   (let* ((num 0)
5520          (entry (gnus-gethash group gnus-newsrc-hashtb))
5521          (info (nth 2 entry))
5522          (active (gnus-active group))
5523          range)
5524     (when entry
5525       (setq range (gnus-compute-read-articles group articles))
5526       (save-excursion
5527         (set-buffer gnus-group-buffer)
5528         (gnus-undo-register
5529           `(progn
5530              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5531              (gnus-info-set-read ',info ',(gnus-info-read info))
5532              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5533              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5534              (gnus-group-update-group ,group t))))
5535       ;; Add the read articles to the range.
5536       (gnus-info-set-read info range)
5537       (gnus-request-set-mark group (list (list range 'add '(read))))
5538       ;; Then we have to re-compute how many unread
5539       ;; articles there are in this group.
5540       (when active
5541         (cond
5542          ((not range)
5543           (setq num (- (1+ (cdr active)) (car active))))
5544          ((not (listp (cdr range)))
5545           (setq num (- (cdr active) (- (1+ (cdr range))
5546                                        (car range)))))
5547          (t
5548           (while range
5549             (if (numberp (car range))
5550                 (setq num (1+ num))
5551               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5552             (setq range (cdr range)))
5553           (setq num (- (cdr active) num))))
5554         ;; Update the number of unread articles.
5555         (setcar entry num)
5556         ;; Update the group buffer.
5557         (gnus-group-update-group group t)))))
5558
5559 (defvar gnus-newsgroup-none-id 0)
5560
5561 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5562   (let ((cur nntp-server-buffer)
5563         (dependencies
5564          (or dependencies
5565              (save-excursion (set-buffer gnus-summary-buffer)
5566                              gnus-newsgroup-dependencies)))
5567         headers id end ref
5568         (mail-parse-charset gnus-newsgroup-charset)
5569         (mail-parse-ignored-charsets
5570          (save-excursion (condition-case nil
5571                              (set-buffer gnus-summary-buffer)
5572                            (error))
5573                          gnus-newsgroup-ignored-charsets)))
5574     (save-excursion
5575       (set-buffer nntp-server-buffer)
5576       ;; Translate all TAB characters into SPACE characters.
5577       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5578       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5579       (gnus-run-hooks 'gnus-parse-headers-hook)
5580       (let ((case-fold-search t)
5581             in-reply-to header p lines chars)
5582         (goto-char (point-min))
5583         ;; Search to the beginning of the next header.  Error messages
5584         ;; do not begin with 2 or 3.
5585         (while (re-search-forward "^[23][0-9]+ " nil t)
5586           (setq id nil
5587                 ref nil)
5588           ;; This implementation of this function, with nine
5589           ;; search-forwards instead of the one re-search-forward and
5590           ;; a case (which basically was the old function) is actually
5591           ;; about twice as fast, even though it looks messier.  You
5592           ;; can't have everything, I guess.  Speed and elegance
5593           ;; doesn't always go hand in hand.
5594           (setq
5595            header
5596            (vector
5597             ;; Number.
5598             (prog1
5599                 (read cur)
5600               (end-of-line)
5601               (setq p (point))
5602               (narrow-to-region (point)
5603                                 (or (and (search-forward "\n.\n" nil t)
5604                                          (- (point) 2))
5605                                     (point))))
5606             ;; Subject.
5607             (progn
5608               (goto-char p)
5609               (if (search-forward "\nsubject:" nil t)
5610                   (funcall gnus-decode-encoded-word-function
5611                            (nnheader-header-value))
5612                 "(none)"))
5613             ;; From.
5614             (progn
5615               (goto-char p)
5616               (if (search-forward "\nfrom:" nil t)
5617                   (funcall gnus-decode-encoded-word-function
5618                            (nnheader-header-value))
5619                 "(nobody)"))
5620             ;; Date.
5621             (progn
5622               (goto-char p)
5623               (if (search-forward "\ndate:" nil t)
5624                   (nnheader-header-value) ""))
5625             ;; Message-ID.
5626             (progn
5627               (goto-char p)
5628               (setq id (if (re-search-forward
5629                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5630                            ;; We do it this way to make sure the Message-ID
5631                            ;; is (somewhat) syntactically valid.
5632                            (buffer-substring (match-beginning 1)
5633                                              (match-end 1))
5634                          ;; If there was no message-id, we just fake one
5635                          ;; to make subsequent routines simpler.
5636                          (nnheader-generate-fake-message-id))))
5637             ;; References.
5638             (progn
5639               (goto-char p)
5640               (if (search-forward "\nreferences:" nil t)
5641                   (progn
5642                     (setq end (point))
5643                     (prog1
5644                         (nnheader-header-value)
5645                       (setq ref
5646                             (buffer-substring
5647                              (progn
5648                                (end-of-line)
5649                                (search-backward ">" end t)
5650                                (1+ (point)))
5651                              (progn
5652                                (search-backward "<" end t)
5653                                (point))))))
5654                 ;; Get the references from the in-reply-to header if there
5655                 ;; were no references and the in-reply-to header looks
5656                 ;; promising.
5657                 (if (and (search-forward "\nin-reply-to:" nil t)
5658                          (setq in-reply-to (nnheader-header-value))
5659                          (string-match "<[^>]+>" in-reply-to))
5660                     (let (ref2)
5661                       (setq ref (substring in-reply-to (match-beginning 0)
5662                                            (match-end 0)))
5663                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5664                         (setq ref2 (substring in-reply-to (match-beginning 0)
5665                                               (match-end 0)))
5666                         (when (> (length ref2) (length ref))
5667                           (setq ref ref2)))
5668                       ref)
5669                   (setq ref nil))))
5670             ;; Chars.
5671             (progn
5672               (goto-char p)
5673               (if (search-forward "\nchars: " nil t)
5674                   (if (numberp (setq chars (ignore-errors (read cur))))
5675                       chars -1)
5676                 -1))
5677             ;; Lines.
5678             (progn
5679               (goto-char p)
5680               (if (search-forward "\nlines: " nil t)
5681                   (if (numberp (setq lines (ignore-errors (read cur))))
5682                       lines -1)
5683                 -1))
5684             ;; Xref.
5685             (progn
5686               (goto-char p)
5687               (and (search-forward "\nxref:" nil t)
5688                    (nnheader-header-value)))
5689             ;; Extra.
5690             (when gnus-extra-headers
5691               (let ((extra gnus-extra-headers)
5692                     out)
5693                 (while extra
5694                   (goto-char p)
5695                   (when (search-forward
5696                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5697                     (push (cons (car extra) (nnheader-header-value))
5698                           out))
5699                   (pop extra))
5700                 out))))
5701           (when (equal id ref)
5702             (setq ref nil))
5703
5704           (when gnus-alter-header-function
5705             (funcall gnus-alter-header-function header)
5706             (setq id (mail-header-id header)
5707                   ref (gnus-parent-id (mail-header-references header))))
5708
5709           (when (setq header
5710                       (gnus-dependencies-add-header
5711                        header dependencies force-new))
5712             (push header headers))
5713           (goto-char (point-max))
5714           (widen))
5715         (nreverse headers)))))
5716
5717 ;; Goes through the xover lines and returns a list of vectors
5718 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5719                                                   force-new dependencies
5720                                                   group also-fetch-heads)
5721   "Parse the news overview data in the server buffer.
5722 Return a list of headers that match SEQUENCE (see
5723 `nntp-retrieve-headers')."
5724   ;; Get the Xref when the users reads the articles since most/some
5725   ;; NNTP servers do not include Xrefs when using XOVER.
5726   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5727   (let ((mail-parse-charset gnus-newsgroup-charset)
5728         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5729         (cur nntp-server-buffer)
5730         (dependencies (or dependencies gnus-newsgroup-dependencies))
5731         (allp (cond
5732                ((eq gnus-read-all-available-headers t)
5733                 t)
5734                ((stringp gnus-read-all-available-headers)
5735                 (string-match gnus-read-all-available-headers group))
5736                (t
5737                 nil)))
5738         number headers header)
5739     (save-excursion
5740       (set-buffer nntp-server-buffer)
5741       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5742       ;; Allow the user to mangle the headers before parsing them.
5743       (gnus-run-hooks 'gnus-parse-headers-hook)
5744       (goto-char (point-min))
5745       (gnus-parse-without-error
5746         (while (and (or sequence allp)
5747                     (not (eobp)))
5748           (setq number (read cur))
5749           (when (not allp)
5750             (while (and sequence
5751                         (< (car sequence) number))
5752               (setq sequence (cdr sequence))))
5753           (when (and (or allp
5754                          (and sequence
5755                               (eq number (car sequence))))
5756                      (progn
5757                        (setq sequence (cdr sequence))
5758                        (setq header (inline
5759                                       (gnus-nov-parse-line
5760                                        number dependencies force-new)))))
5761             (push header headers))
5762           (forward-line 1)))
5763       ;; A common bug in inn is that if you have posted an article and
5764       ;; then retrieves the active file, it will answer correctly --
5765       ;; the new article is included.  However, a NOV entry for the
5766       ;; article may not have been generated yet, so this may fail.
5767       ;; We work around this problem by retrieving the last few
5768       ;; headers using HEAD.
5769       (if (or (not also-fetch-heads)
5770               (not sequence))
5771           ;; We (probably) got all the headers.
5772           (nreverse headers)
5773         (let ((gnus-nov-is-evil t))
5774           (nconc
5775            (nreverse headers)
5776            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5777              (gnus-get-newsgroup-headers))))))))
5778
5779 (defun gnus-article-get-xrefs ()
5780   "Fill in the Xref value in `gnus-current-headers', if necessary.
5781 This is meant to be called in `gnus-article-internal-prepare-hook'."
5782   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5783                                  gnus-current-headers)))
5784     (or (not gnus-use-cross-reference)
5785         (not headers)
5786         (and (mail-header-xref headers)
5787              (not (string= (mail-header-xref headers) "")))
5788         (let ((case-fold-search t)
5789               xref)
5790           (save-restriction
5791             (nnheader-narrow-to-headers)
5792             (goto-char (point-min))
5793             (when (or (and (not (eobp))
5794                            (eq (downcase (char-after)) ?x)
5795                            (looking-at "Xref:"))
5796                       (search-forward "\nXref:" nil t))
5797               (goto-char (1+ (match-end 0)))
5798               (setq xref (buffer-substring (point)
5799                                            (progn (end-of-line) (point))))
5800               (mail-header-set-xref headers xref)))))))
5801
5802 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5803   "Find article ID and insert the summary line for that article.
5804 OLD-HEADER can either be a header or a line number to insert
5805 the subject line on."
5806   (let* ((line (and (numberp old-header) old-header))
5807          (old-header (and (vectorp old-header) old-header))
5808          (header (cond ((and old-header use-old-header)
5809                         old-header)
5810                        ((and (numberp id)
5811                              (gnus-number-to-header id))
5812                         (gnus-number-to-header id))
5813                        (t
5814                         (gnus-read-header id))))
5815          (number (and (numberp id) id))
5816          d)
5817     (when header
5818       ;; Rebuild the thread that this article is part of and go to the
5819       ;; article we have fetched.
5820       (when (and (not gnus-show-threads)
5821                  old-header)
5822         (when (and number
5823                    (setq d (gnus-data-find (mail-header-number old-header))))
5824           (goto-char (gnus-data-pos d))
5825           (gnus-data-remove
5826            number
5827            (- (gnus-point-at-bol)
5828               (prog1
5829                   (1+ (gnus-point-at-eol))
5830                 (gnus-delete-line))))))
5831       (when old-header
5832         (mail-header-set-number header (mail-header-number old-header)))
5833       (setq gnus-newsgroup-sparse
5834             (delq (setq number (mail-header-number header))
5835                   gnus-newsgroup-sparse))
5836       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5837       (push number gnus-newsgroup-limit)
5838       (gnus-rebuild-thread (mail-header-id header) line)
5839       (gnus-summary-goto-subject number nil t))
5840     (when (and (numberp number)
5841                (> number 0))
5842       ;; We have to update the boundaries even if we can't fetch the
5843       ;; article if ID is a number -- so that the next `P' or `N'
5844       ;; command will fetch the previous (or next) article even
5845       ;; if the one we tried to fetch this time has been canceled.
5846       (when (> number gnus-newsgroup-end)
5847         (setq gnus-newsgroup-end number))
5848       (when (< number gnus-newsgroup-begin)
5849         (setq gnus-newsgroup-begin number))
5850       (setq gnus-newsgroup-unselected
5851             (delq number gnus-newsgroup-unselected)))
5852     ;; Report back a success?
5853     (and header (mail-header-number header))))
5854
5855 ;;; Process/prefix in the summary buffer
5856
5857 (defun gnus-summary-work-articles (n)
5858   "Return a list of articles to be worked upon.
5859 The prefix argument, the list of process marked articles, and the
5860 current article will be taken into consideration."
5861   (save-excursion
5862     (set-buffer gnus-summary-buffer)
5863     (cond
5864      (n
5865       ;; A numerical prefix has been given.
5866       (setq n (prefix-numeric-value n))
5867       (let ((backward (< n 0))
5868             (n (abs (prefix-numeric-value n)))
5869             articles article)
5870         (save-excursion
5871           (while
5872               (and (> n 0)
5873                    (push (setq article (gnus-summary-article-number))
5874                          articles)
5875                    (if backward
5876                        (gnus-summary-find-prev nil article)
5877                      (gnus-summary-find-next nil article)))
5878             (decf n)))
5879         (nreverse articles)))
5880      ((and (gnus-region-active-p) (mark))
5881       (message "region active")
5882       ;; Work on the region between point and mark.
5883       (let ((max (max (point) (mark)))
5884             articles article)
5885         (save-excursion
5886           (goto-char (min (min (point) (mark))))
5887           (while
5888               (and
5889                (push (setq article (gnus-summary-article-number)) articles)
5890                (gnus-summary-find-next nil article)
5891                (< (point) max)))
5892           (nreverse articles))))
5893      (gnus-newsgroup-processable
5894       ;; There are process-marked articles present.
5895       ;; Save current state.
5896       (gnus-summary-save-process-mark)
5897       ;; Return the list.
5898       (reverse gnus-newsgroup-processable))
5899      (t
5900       ;; Just return the current article.
5901       (list (gnus-summary-article-number))))))
5902
5903 (defmacro gnus-summary-iterate (arg &rest forms)
5904   "Iterate over the process/prefixed articles and do FORMS.
5905 ARG is the interactive prefix given to the command.  FORMS will be
5906 executed with point over the summary line of the articles."
5907   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5908     `(let ((,articles (gnus-summary-work-articles ,arg)))
5909        (while ,articles
5910          (gnus-summary-goto-subject (car ,articles))
5911          ,@forms
5912          (pop ,articles)))))
5913
5914 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5915 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5916
5917 (defun gnus-summary-save-process-mark ()
5918   "Push the current set of process marked articles on the stack."
5919   (interactive)
5920   (push (copy-sequence gnus-newsgroup-processable)
5921         gnus-newsgroup-process-stack))
5922
5923 (defun gnus-summary-kill-process-mark ()
5924   "Push the current set of process marked articles on the stack and unmark."
5925   (interactive)
5926   (gnus-summary-save-process-mark)
5927   (gnus-summary-unmark-all-processable))
5928
5929 (defun gnus-summary-yank-process-mark ()
5930   "Pop the last process mark state off the stack and restore it."
5931   (interactive)
5932   (unless gnus-newsgroup-process-stack
5933     (error "Empty mark stack"))
5934   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5935
5936 (defun gnus-summary-process-mark-set (set)
5937   "Make SET into the current process marked articles."
5938   (gnus-summary-unmark-all-processable)
5939   (while set
5940     (gnus-summary-set-process-mark (pop set))))
5941
5942 ;;; Searching and stuff
5943
5944 (defun gnus-summary-search-group (&optional backward use-level)
5945   "Search for next unread newsgroup.
5946 If optional argument BACKWARD is non-nil, search backward instead."
5947   (save-excursion
5948     (set-buffer gnus-group-buffer)
5949     (when (gnus-group-search-forward
5950            backward nil (if use-level (gnus-group-group-level) nil))
5951       (gnus-group-group-name))))
5952
5953 (defun gnus-summary-best-group (&optional exclude-group)
5954   "Find the name of the best unread group.
5955 If EXCLUDE-GROUP, do not go to this group."
5956   (save-excursion
5957     (set-buffer gnus-group-buffer)
5958     (save-excursion
5959       (gnus-group-best-unread-group exclude-group))))
5960
5961 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5962   (if backward (gnus-summary-find-prev)
5963     (let* ((dummy (gnus-summary-article-intangible-p))
5964            (article (or article (gnus-summary-article-number)))
5965            (arts (gnus-data-find-list article))
5966            result)
5967       (when (and (not dummy)
5968                  (or (not gnus-summary-check-current)
5969                      (not unread)
5970                      (not (gnus-data-unread-p (car arts)))))
5971         (setq arts (cdr arts)))
5972       (when (setq result
5973                   (if unread
5974                       (progn
5975                         (while arts
5976                           (when (or (and undownloaded
5977                                          (memq (car arts) gnus-newsgroup-undownloaded))
5978                                     (gnus-data-unread-p (car arts)))
5979                             (setq result (car arts)
5980                                   arts nil))
5981                           (setq arts (cdr arts)))
5982                         result)
5983                     (car arts)))
5984         (goto-char (gnus-data-pos result))
5985         (gnus-data-number result)))))
5986
5987 (defun gnus-summary-find-prev (&optional unread article)
5988   (let* ((eobp (eobp))
5989          (article (or article (gnus-summary-article-number)))
5990          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5991          result)
5992     (when (and (not eobp)
5993                (or (not gnus-summary-check-current)
5994                    (not unread)
5995                    (not (gnus-data-unread-p (car arts)))))
5996       (setq arts (cdr arts)))
5997     (when (setq result
5998                 (if unread
5999                     (progn
6000                       (while arts
6001                         (when (gnus-data-unread-p (car arts))
6002                           (setq result (car arts)
6003                                 arts nil))
6004                         (setq arts (cdr arts)))
6005                       result)
6006                   (car arts)))
6007       (goto-char (gnus-data-pos result))
6008       (gnus-data-number result))))
6009
6010 (defun gnus-summary-find-subject (subject &optional unread backward article)
6011   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6012          (article (or article (gnus-summary-article-number)))
6013          (articles (gnus-data-list backward))
6014          (arts (gnus-data-find-list article articles))
6015          result)
6016     (when (or (not gnus-summary-check-current)
6017               (not unread)
6018               (not (gnus-data-unread-p (car arts))))
6019       (setq arts (cdr arts)))
6020     (while arts
6021       (and (or (not unread)
6022                (gnus-data-unread-p (car arts)))
6023            (vectorp (gnus-data-header (car arts)))
6024            (gnus-subject-equal
6025             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6026            (setq result (car arts)
6027                  arts nil))
6028       (setq arts (cdr arts)))
6029     (and result
6030          (goto-char (gnus-data-pos result))
6031          (gnus-data-number result))))
6032
6033 (defun gnus-summary-search-forward (&optional unread subject backward)
6034   "Search forward for an article.
6035 If UNREAD, look for unread articles.  If SUBJECT, look for
6036 articles with that subject.  If BACKWARD, search backward instead."
6037   (cond (subject (gnus-summary-find-subject subject unread backward))
6038         (backward (gnus-summary-find-prev unread))
6039         (t (gnus-summary-find-next unread))))
6040
6041 (defun gnus-recenter (&optional n)
6042   "Center point in window and redisplay frame.
6043 Also do horizontal recentering."
6044   (interactive "P")
6045   (when (and gnus-auto-center-summary
6046              (not (eq gnus-auto-center-summary 'vertical)))
6047     (gnus-horizontal-recenter))
6048   (recenter n))
6049
6050 (defun gnus-summary-recenter ()
6051   "Center point in the summary window.
6052 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6053 displayed, no centering will be performed."
6054   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6055 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6056   (interactive)
6057   (let* ((top (cond ((< (window-height) 4) 0)
6058                     ((< (window-height) 7) 1)
6059                     (t (if (numberp gnus-auto-center-summary)
6060                            gnus-auto-center-summary
6061                          2))))
6062          (height (1- (window-height)))
6063          (bottom (save-excursion (goto-char (point-max))
6064                                  (forward-line (- height))
6065                                  (point)))
6066          (window (get-buffer-window (current-buffer))))
6067     ;; The user has to want it.
6068     (when gnus-auto-center-summary
6069       (when (get-buffer-window gnus-article-buffer)
6070         ;; Only do recentering when the article buffer is displayed,
6071       ;; Set the window start to either `bottom', which is the biggest
6072         ;; possible valid number, or the second line from the top,
6073         ;; whichever is the least.
6074         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6075           (if (> bottom top-pos)
6076               ;; Keep the second line from the top visible
6077               (set-window-start window top-pos t)
6078             ;; Try to keep the bottom line visible; if it's partially
6079             ;; obscured, either scroll one more line to make it fully
6080             ;; visible, or revert to using TOP-POS.
6081             (save-excursion
6082               (goto-char (point-max))
6083               (forward-line -1)
6084               (let ((last-line-start (point)))
6085                 (goto-char bottom)
6086                 (set-window-start window (point) t)
6087                 (when (not (pos-visible-in-window-p last-line-start window))
6088                   (forward-line 1)
6089                   (set-window-start window (min (point) top-pos) t)))))))
6090       ;; Do horizontal recentering while we're at it.
6091       (when (and (get-buffer-window (current-buffer) t)
6092                  (not (eq gnus-auto-center-summary 'vertical)))
6093         (let ((selected (selected-window)))
6094           (select-window (get-buffer-window (current-buffer) t))
6095           (gnus-summary-position-point)
6096           (gnus-horizontal-recenter)
6097           (select-window selected))))))
6098
6099 (defun gnus-summary-jump-to-group (newsgroup)
6100   "Move point to NEWSGROUP in group mode buffer."
6101   ;; Keep update point of group mode buffer if visible.
6102   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6103       (save-window-excursion
6104         ;; Take care of tree window mode.
6105         (when (get-buffer-window gnus-group-buffer)
6106           (pop-to-buffer gnus-group-buffer))
6107         (gnus-group-jump-to-group newsgroup))
6108     (save-excursion
6109       ;; Take care of tree window mode.
6110       (if (get-buffer-window gnus-group-buffer)
6111           (pop-to-buffer gnus-group-buffer)
6112         (set-buffer gnus-group-buffer))
6113       (gnus-group-jump-to-group newsgroup))))
6114
6115 ;; This function returns a list of article numbers based on the
6116 ;; difference between the ranges of read articles in this group and
6117 ;; the range of active articles.
6118 (defun gnus-list-of-unread-articles (group)
6119   (let* ((read (gnus-info-read (gnus-get-info group)))
6120          (active (or (gnus-active group) (gnus-activate-group group)))
6121          (last (cdr active))
6122          first nlast unread)
6123     ;; If none are read, then all are unread.
6124     (if (not read)
6125         (setq first (car active))
6126       ;; If the range of read articles is a single range, then the
6127       ;; first unread article is the article after the last read
6128       ;; article.  Sounds logical, doesn't it?
6129       (if (and (not (listp (cdr read)))
6130                (or (< (car read) (car active))
6131                    (progn (setq read (list read))
6132                           nil)))
6133           (setq first (max (car active) (1+ (cdr read))))
6134         ;; `read' is a list of ranges.
6135         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6136                                   (caar read)))
6137                   1)
6138           (setq first (car active)))
6139         (while read
6140           (when first
6141             (while (< first nlast)
6142               (push first unread)
6143               (setq first (1+ first))))
6144           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6145           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6146           (setq read (cdr read)))))
6147     ;; And add the last unread articles.
6148     (while (<= first last)
6149       (push first unread)
6150       (setq first (1+ first)))
6151     ;; Return the list of unread articles.
6152     (delq 0 (nreverse unread))))
6153
6154 (defun gnus-list-of-read-articles (group)
6155   "Return a list of unread, unticked and non-dormant articles."
6156   (let* ((info (gnus-get-info group))
6157          (marked (gnus-info-marks info))
6158          (active (gnus-active group)))
6159     (and info active
6160          (gnus-list-range-difference
6161           (gnus-list-range-difference
6162            (gnus-sorted-complement
6163             (gnus-uncompress-range active)
6164             (gnus-list-of-unread-articles group))
6165            (cdr (assq 'dormant marked)))
6166           (cdr (assq 'tick marked))))))
6167
6168 ;; Various summary commands
6169
6170 (defun gnus-summary-select-article-buffer ()
6171   "Reconfigure windows to show article buffer."
6172   (interactive)
6173   (if (not (gnus-buffer-live-p gnus-article-buffer))
6174       (error "There is no article buffer for this summary buffer")
6175     (gnus-configure-windows 'article)
6176     (select-window (get-buffer-window gnus-article-buffer))))
6177
6178 (defun gnus-summary-universal-argument (arg)
6179   "Perform any operation on all articles that are process/prefixed."
6180   (interactive "P")
6181   (let ((articles (gnus-summary-work-articles arg))
6182         func article)
6183     (if (eq
6184          (setq
6185           func
6186           (key-binding
6187            (read-key-sequence
6188             (substitute-command-keys
6189              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6190          'undefined)
6191         (gnus-error 1 "Undefined key")
6192       (save-excursion
6193         (while articles
6194           (gnus-summary-goto-subject (setq article (pop articles)))
6195           (let (gnus-newsgroup-processable)
6196             (command-execute func))
6197           (gnus-summary-remove-process-mark article)))))
6198   (gnus-summary-position-point))
6199
6200 (defun gnus-summary-toggle-truncation (&optional arg)
6201   "Toggle truncation of summary lines.
6202 With arg, turn line truncation on if arg is positive."
6203   (interactive "P")
6204   (setq truncate-lines
6205         (if (null arg) (not truncate-lines)
6206           (> (prefix-numeric-value arg) 0)))
6207   (redraw-display))
6208
6209 (defun gnus-summary-find-uncancelled ()
6210   "Return the number of an uncancelled article.
6211 The current article is considered, then following articles, then previous
6212 articles.  If all articles are cancelled then return a dummy 0."
6213   (let (found)
6214     (dolist (rev '(nil t))
6215       (unless found      ; don't demand the reverse list if we don't need it
6216         (let ((data (gnus-data-find-list
6217                      (gnus-summary-article-number) (gnus-data-list rev))))
6218           (while (and data (not found))
6219             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6220                 (setq found (gnus-data-number (car data))))
6221             (setq data (cdr data))))))
6222     (or found 0)))
6223
6224 (defun gnus-summary-reselect-current-group (&optional all rescan)
6225   "Exit and then reselect the current newsgroup.
6226 The prefix argument ALL means to select all articles."
6227   (interactive "P")
6228   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6229     (error "Ephemeral groups can't be reselected"))
6230   (let ((current-subject (gnus-summary-find-uncancelled))
6231         (group gnus-newsgroup-name))
6232     (setq gnus-newsgroup-begin nil)
6233     (gnus-summary-exit)
6234     ;; We have to adjust the point of group mode buffer because
6235     ;; point was moved to the next unread newsgroup by exiting.
6236     (gnus-summary-jump-to-group group)
6237     (when rescan
6238       (save-excursion
6239         (gnus-group-get-new-news-this-group 1)))
6240     (gnus-group-read-group all t)
6241     (gnus-summary-goto-subject current-subject nil t)))
6242
6243 (defun gnus-summary-rescan-group (&optional all)
6244   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6245   (interactive "P")
6246   (gnus-summary-reselect-current-group all t))
6247
6248 (defun gnus-summary-update-info (&optional non-destructive)
6249   (save-excursion
6250     (let ((group gnus-newsgroup-name))
6251       (when group
6252         (when gnus-newsgroup-kill-headers
6253           (setq gnus-newsgroup-killed
6254                 (gnus-compress-sequence
6255                  (gnus-sorted-union
6256                   (gnus-list-range-intersection
6257                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6258                   gnus-newsgroup-unreads)
6259                  t)))
6260         (unless (listp (cdr gnus-newsgroup-killed))
6261           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6262         (let ((headers gnus-newsgroup-headers))
6263           ;; Set the new ranges of read articles.
6264           (save-excursion
6265             (set-buffer gnus-group-buffer)
6266             (gnus-undo-force-boundary))
6267           (gnus-update-read-articles
6268            group (gnus-sorted-union
6269                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6270           ;; Set the current article marks.
6271           (let ((gnus-newsgroup-scored
6272                  (if (and (not gnus-save-score)
6273                           (not non-destructive))
6274                      nil
6275                    gnus-newsgroup-scored)))
6276             (save-excursion
6277               (gnus-update-marks)))
6278           ;; Do the cross-ref thing.
6279           (when gnus-use-cross-reference
6280             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6281           ;; Do not switch windows but change the buffer to work.
6282           (set-buffer gnus-group-buffer)
6283           (unless (gnus-ephemeral-group-p group)
6284             (gnus-group-update-group group)))))))
6285
6286 (defun gnus-summary-save-newsrc (&optional force)
6287   "Save the current number of read/marked articles in the dribble buffer.
6288 The dribble buffer will then be saved.
6289 If FORCE (the prefix), also save the .newsrc file(s)."
6290   (interactive "P")
6291   (gnus-summary-update-info t)
6292   (if force
6293       (gnus-save-newsrc-file)
6294     (gnus-dribble-save)))
6295
6296 (defun gnus-summary-exit (&optional temporary)
6297   "Exit reading current newsgroup, and then return to group selection mode.
6298 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6299   (interactive)
6300   (gnus-set-global-variables)
6301   (when (gnus-buffer-live-p gnus-article-buffer)
6302     (save-excursion
6303       (set-buffer gnus-article-buffer)
6304       (mm-destroy-parts gnus-article-mime-handles)
6305       ;; Set it to nil for safety reason.
6306       (setq gnus-article-mime-handle-alist nil)
6307       (setq gnus-article-mime-handles nil)))
6308   (gnus-kill-save-kill-buffer)
6309   (gnus-async-halt-prefetch)
6310   (let* ((group gnus-newsgroup-name)
6311          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6312          (gnus-group-is-exiting-p t)
6313          (mode major-mode)
6314          (group-point nil)
6315          (buf (current-buffer)))
6316     (unless quit-config
6317       ;; Do adaptive scoring, and possibly save score files.
6318       (when gnus-newsgroup-adaptive
6319         (gnus-score-adaptive))
6320       (when gnus-use-scoring
6321         (gnus-score-save)))
6322     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6323     ;; If we have several article buffers, we kill them at exit.
6324     (unless gnus-single-article-buffer
6325       (gnus-kill-buffer gnus-original-article-buffer)
6326       (setq gnus-article-current nil))
6327     (when gnus-use-cache
6328       (gnus-cache-possibly-remove-articles)
6329       (gnus-cache-save-buffers))
6330     (gnus-async-prefetch-remove-group group)
6331     (when gnus-suppress-duplicates
6332       (gnus-dup-enter-articles))
6333     (when gnus-use-trees
6334       (gnus-tree-close group))
6335     (when gnus-use-cache
6336       (gnus-cache-write-active))
6337     ;; Remove entries for this group.
6338     (nnmail-purge-split-history (gnus-group-real-name group))
6339     ;; Make all changes in this group permanent.
6340     (unless quit-config
6341       (gnus-run-hooks 'gnus-exit-group-hook)
6342       (gnus-summary-update-info))
6343     (gnus-close-group group)
6344     ;; Make sure where we were, and go to next newsgroup.
6345     (set-buffer gnus-group-buffer)
6346     (unless quit-config
6347       (gnus-group-jump-to-group group))
6348     (gnus-run-hooks 'gnus-summary-exit-hook)
6349     (unless (or quit-config
6350                 ;; If this group has disappeared from the summary
6351                 ;; buffer, don't skip forwards.
6352                 (not (string= group (gnus-group-group-name))))
6353       (gnus-group-next-unread-group 1))
6354     (setq group-point (point))
6355     (if temporary
6356         nil                             ;Nothing to do.
6357       ;; If we have several article buffers, we kill them at exit.
6358       (unless gnus-single-article-buffer
6359         (gnus-kill-buffer gnus-article-buffer)
6360         (gnus-kill-buffer gnus-original-article-buffer)
6361         (setq gnus-article-current nil))
6362       (set-buffer buf)
6363       (if (not gnus-kill-summary-on-exit)
6364           (progn
6365             (gnus-deaden-summary)
6366             (setq mode nil))
6367        ;; We set all buffer-local variables to nil.  It is unclear why
6368         ;; this is needed, but if we don't, buffer-local variables are
6369         ;; not garbage-collected, it seems.  This would the lead to en
6370         ;; ever-growing Emacs.
6371         (gnus-summary-clear-local-variables)
6372         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6373           (gnus-summary-clear-local-variables))
6374         (when (get-buffer gnus-article-buffer)
6375           (bury-buffer gnus-article-buffer))
6376         ;; We clear the global counterparts of the buffer-local
6377         ;; variables as well, just to be on the safe side.
6378         (set-buffer gnus-group-buffer)
6379         (gnus-summary-clear-local-variables)
6380         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6381           (gnus-summary-clear-local-variables)))
6382       (setq gnus-current-select-method gnus-select-method)
6383       (pop-to-buffer gnus-group-buffer)
6384       (if (not quit-config)
6385           (progn
6386             (goto-char group-point)
6387             (gnus-configure-windows 'group 'force))
6388         (gnus-handle-ephemeral-exit quit-config))
6389       ;; Return to group mode buffer.
6390       (when (eq mode 'gnus-summary-mode)
6391         (gnus-kill-buffer buf))
6392       ;; Clear the current group name.
6393       (unless quit-config
6394         (setq gnus-newsgroup-name nil)))))
6395
6396 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6397 (defun gnus-summary-exit-no-update (&optional no-questions)
6398   "Quit reading current newsgroup without updating read article info."
6399   (interactive)
6400   (let* ((group gnus-newsgroup-name)
6401          (quit-config (gnus-group-quit-config group)))
6402     (when (or no-questions
6403               gnus-expert-user
6404               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6405       (gnus-async-halt-prefetch)
6406       (mapcar 'funcall
6407               (delq 'gnus-summary-expire-articles
6408                     (copy-sequence gnus-summary-prepare-exit-hook)))
6409       (when (gnus-buffer-live-p gnus-article-buffer)
6410         (save-excursion
6411           (set-buffer gnus-article-buffer)
6412           (mm-destroy-parts gnus-article-mime-handles)
6413           ;; Set it to nil for safety reason.
6414           (setq gnus-article-mime-handle-alist nil)
6415           (setq gnus-article-mime-handles nil)))
6416       ;; If we have several article buffers, we kill them at exit.
6417       (unless gnus-single-article-buffer
6418         (gnus-kill-buffer gnus-article-buffer)
6419         (gnus-kill-buffer gnus-original-article-buffer)
6420         (setq gnus-article-current nil))
6421       (if (not gnus-kill-summary-on-exit)
6422           (gnus-deaden-summary)
6423         (gnus-close-group group)
6424         (gnus-summary-clear-local-variables)
6425         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6426           (gnus-summary-clear-local-variables))
6427         (set-buffer gnus-group-buffer)
6428         (gnus-summary-clear-local-variables)
6429         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6430           (gnus-summary-clear-local-variables))
6431         (when (get-buffer gnus-summary-buffer)
6432           (kill-buffer gnus-summary-buffer)))
6433       (unless gnus-single-article-buffer
6434         (setq gnus-article-current nil))
6435       (when gnus-use-trees
6436         (gnus-tree-close group))
6437       (gnus-async-prefetch-remove-group group)
6438       (when (get-buffer gnus-article-buffer)
6439         (bury-buffer gnus-article-buffer))
6440       ;; Return to the group buffer.
6441       (gnus-configure-windows 'group 'force)
6442       ;; Clear the current group name.
6443       (setq gnus-newsgroup-name nil)
6444       (when (equal (gnus-group-group-name) group)
6445         (gnus-group-next-unread-group 1))
6446       (when quit-config
6447         (gnus-handle-ephemeral-exit quit-config)))))
6448
6449 (defun gnus-handle-ephemeral-exit (quit-config)
6450   "Handle movement when leaving an ephemeral group.
6451 The state which existed when entering the ephemeral is reset."
6452   (if (not (buffer-name (car quit-config)))
6453       (gnus-configure-windows 'group 'force)
6454     (set-buffer (car quit-config))
6455     (cond ((eq major-mode 'gnus-summary-mode)
6456            (gnus-set-global-variables))
6457           ((eq major-mode 'gnus-article-mode)
6458            (save-excursion
6459              ;; The `gnus-summary-buffer' variable may point
6460              ;; to the old summary buffer when using a single
6461              ;; article buffer.
6462              (unless (gnus-buffer-live-p gnus-summary-buffer)
6463                (set-buffer gnus-group-buffer))
6464              (set-buffer gnus-summary-buffer)
6465              (gnus-set-global-variables))))
6466     (if (or (eq (cdr quit-config) 'article)
6467             (eq (cdr quit-config) 'pick))
6468         (progn
6469           ;; The current article may be from the ephemeral group
6470           ;; thus it is best that we reload this article
6471           (gnus-summary-show-article)
6472           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6473               (gnus-configure-windows 'pick 'force)
6474             (gnus-configure-windows (cdr quit-config) 'force)))
6475       (gnus-configure-windows (cdr quit-config) 'force))
6476     (when (eq major-mode 'gnus-summary-mode)
6477       (gnus-summary-next-subject 1 nil t)
6478       (gnus-summary-recenter)
6479       (gnus-summary-position-point))))
6480
6481 ;;; Dead summaries.
6482
6483 (defvar gnus-dead-summary-mode-map nil)
6484
6485 (unless gnus-dead-summary-mode-map
6486   (setq gnus-dead-summary-mode-map (make-keymap))
6487   (suppress-keymap gnus-dead-summary-mode-map)
6488   (substitute-key-definition
6489    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6490   (dolist (key '("\C-d" "\r" "\177" [delete]))
6491     (define-key gnus-dead-summary-mode-map
6492       key 'gnus-summary-wake-up-the-dead))
6493   (dolist (key '("q" "Q"))
6494     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6495
6496 (defvar gnus-dead-summary-mode nil
6497   "Minor mode for Gnus summary buffers.")
6498
6499 (defun gnus-dead-summary-mode (&optional arg)
6500   "Minor mode for Gnus summary buffers."
6501   (interactive "P")
6502   (when (eq major-mode 'gnus-summary-mode)
6503     (make-local-variable 'gnus-dead-summary-mode)
6504     (setq gnus-dead-summary-mode
6505           (if (null arg) (not gnus-dead-summary-mode)
6506             (> (prefix-numeric-value arg) 0)))
6507     (when gnus-dead-summary-mode
6508       (gnus-add-minor-mode
6509        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6510
6511 (defun gnus-deaden-summary ()
6512   "Make the current summary buffer into a dead summary buffer."
6513   ;; Kill any previous dead summary buffer.
6514   (when (and gnus-dead-summary
6515              (buffer-name gnus-dead-summary))
6516     (save-excursion
6517       (set-buffer gnus-dead-summary)
6518       (when gnus-dead-summary-mode
6519         (kill-buffer (current-buffer)))))
6520   ;; Make this the current dead summary.
6521   (setq gnus-dead-summary (current-buffer))
6522   (gnus-dead-summary-mode 1)
6523   (let ((name (buffer-name)))
6524     (when (string-match "Summary" name)
6525       (rename-buffer
6526        (concat (substring name 0 (match-beginning 0)) "Dead "
6527                (substring name (match-beginning 0)))
6528        t)
6529       (bury-buffer))))
6530
6531 (defun gnus-kill-or-deaden-summary (buffer)
6532   "Kill or deaden the summary BUFFER."
6533   (save-excursion
6534     (when (and (buffer-name buffer)
6535                (not gnus-single-article-buffer))
6536       (save-excursion
6537         (set-buffer buffer)
6538         (gnus-kill-buffer gnus-article-buffer)
6539         (gnus-kill-buffer gnus-original-article-buffer)))
6540     (cond
6541      ;; Kill the buffer.
6542      (gnus-kill-summary-on-exit
6543       (when (and gnus-use-trees
6544                  (gnus-buffer-exists-p buffer))
6545         (save-excursion
6546           (set-buffer buffer)
6547           (gnus-tree-close gnus-newsgroup-name)))
6548       (gnus-kill-buffer buffer))
6549      ;; Deaden the buffer.
6550      ((gnus-buffer-exists-p buffer)
6551       (save-excursion
6552         (set-buffer buffer)
6553         (gnus-deaden-summary))))))
6554
6555 (defun gnus-summary-wake-up-the-dead (&rest args)
6556   "Wake up the dead summary buffer."
6557   (interactive)
6558   (gnus-dead-summary-mode -1)
6559   (let ((name (buffer-name)))
6560     (when (string-match "Dead " name)
6561       (rename-buffer
6562        (concat (substring name 0 (match-beginning 0))
6563                (substring name (match-end 0)))
6564        t)))
6565   (gnus-message 3 "This dead summary is now alive again"))
6566
6567 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6568 (defun gnus-summary-fetch-faq (&optional faq-dir)
6569   "Fetch the FAQ for the current group.
6570 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6571 in."
6572   (interactive
6573    (list
6574     (when current-prefix-arg
6575       (completing-read
6576        "FAQ dir: " (and (listp gnus-group-faq-directory)
6577                         (mapcar (lambda (file) (list file))
6578                                 gnus-group-faq-directory))))))
6579   (let (gnus-faq-buffer)
6580     (when (setq gnus-faq-buffer
6581                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6582       (gnus-configure-windows 'summary-faq))))
6583
6584 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6585 (defun gnus-summary-describe-group (&optional force)
6586   "Describe the current newsgroup."
6587   (interactive "P")
6588   (gnus-group-describe-group force gnus-newsgroup-name))
6589
6590 (defun gnus-summary-describe-briefly ()
6591   "Describe summary mode commands briefly."
6592   (interactive)
6593   (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")))
6594
6595 ;; Walking around group mode buffer from summary mode.
6596
6597 (defun gnus-summary-next-group (&optional no-article target-group backward)
6598   "Exit current newsgroup and then select next unread newsgroup.
6599 If prefix argument NO-ARTICLE is non-nil, no article is selected
6600 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6601 previous group instead."
6602   (interactive "P")
6603   ;; Stop pre-fetching.
6604   (gnus-async-halt-prefetch)
6605   (let ((current-group gnus-newsgroup-name)
6606         (current-buffer (current-buffer))
6607         entered)
6608     (while (not entered)
6609       ;; Then we find what group we are supposed to enter.
6610       (set-buffer gnus-group-buffer)
6611       (gnus-group-jump-to-group current-group)
6612       (setq target-group
6613             (or target-group
6614                 (if (eq gnus-keep-same-level 'best)
6615                     (gnus-summary-best-group gnus-newsgroup-name)
6616                   (gnus-summary-search-group backward gnus-keep-same-level))))
6617       (if (not target-group)
6618           ;; There are no further groups, so we return to the group
6619           ;; buffer.
6620           (progn
6621             (gnus-message 5 "Returning to the group buffer")
6622             (setq entered t)
6623             (when (gnus-buffer-live-p current-buffer)
6624               (set-buffer current-buffer)
6625               (gnus-summary-exit))
6626             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6627         ;; We try to enter the target group.
6628         (gnus-group-jump-to-group target-group)
6629         (let ((unreads (gnus-group-group-unread)))
6630           (if (and (or (eq t unreads)
6631                        (and unreads (not (zerop unreads))))
6632                    (progn
6633                      ;; Now we semi-exit this group to update Xrefs
6634                      ;; and all variables.  We can't do a real exit,
6635                      ;; because the window conf must remain the same
6636                      ;; in case the user is prompted for info, and we
6637                      ;; don't want the window conf to change before
6638                      ;; that...
6639                      (when (gnus-buffer-live-p current-buffer)
6640                        (set-buffer current-buffer)
6641                        (gnus-summary-exit t))
6642                      (gnus-summary-read-group
6643                       target-group nil no-article
6644                       (and (buffer-name current-buffer) current-buffer)
6645                       nil backward)))
6646               (setq entered t)
6647             (setq current-group target-group
6648                   target-group nil)))))))
6649
6650 (defun gnus-summary-prev-group (&optional no-article)
6651   "Exit current newsgroup and then select previous unread newsgroup.
6652 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6653   (interactive "P")
6654   (gnus-summary-next-group no-article nil t))
6655
6656 ;; Walking around summary lines.
6657
6658 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6659   "Go to the first unread subject.
6660 If UNREAD is non-nil, go to the first unread article.
6661 Returns the article selected or nil if there are no unread articles."
6662   (interactive "P")
6663   (prog1
6664       (cond
6665        ;; Empty summary.
6666        ((null gnus-newsgroup-data)
6667         (gnus-message 3 "No articles in the group")
6668         nil)
6669        ;; Pick the first article.
6670        ((not unread)
6671         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6672         (gnus-data-number (car gnus-newsgroup-data)))
6673        ;; No unread articles.
6674        ((null gnus-newsgroup-unreads)
6675         (gnus-message 3 "No more unread articles")
6676         nil)
6677        ;; Find the first unread article.
6678        (t
6679         (let ((data gnus-newsgroup-data))
6680           (while (and data
6681                       (and (not (and undownloaded
6682                                      (memq (car data) gnus-newsgroup-undownloaded)))
6683                            (if unseen
6684                                (or (not (memq
6685                                          (gnus-data-number (car data))
6686                                          gnus-newsgroup-unseen))
6687                                    (not (gnus-data-unread-p (car data))))
6688                              (not (gnus-data-unread-p (car data))))))
6689             (setq data (cdr data)))
6690           (when data
6691             (goto-char (gnus-data-pos (car data)))
6692             (gnus-data-number (car data))))))
6693     (gnus-summary-position-point)))
6694
6695 (defun gnus-summary-next-subject (n &optional unread dont-display)
6696   "Go to next N'th summary line.
6697 If N is negative, go to the previous N'th subject line.
6698 If UNREAD is non-nil, only unread articles are selected.
6699 The difference between N and the actual number of steps taken is
6700 returned."
6701   (interactive "p")
6702   (let ((backward (< n 0))
6703         (n (abs n)))
6704     (while (and (> n 0)
6705                 (if backward
6706                     (gnus-summary-find-prev unread)
6707                   (gnus-summary-find-next unread)))
6708       (unless (zerop (setq n (1- n)))
6709         (gnus-summary-show-thread)))
6710     (when (/= 0 n)
6711       (gnus-message 7 "No more%s articles"
6712                     (if unread " unread" "")))
6713     (unless dont-display
6714       (gnus-summary-recenter)
6715       (gnus-summary-position-point))
6716     n))
6717
6718 (defun gnus-summary-next-unread-subject (n)
6719   "Go to next N'th unread summary line."
6720   (interactive "p")
6721   (gnus-summary-next-subject n t))
6722
6723 (defun gnus-summary-prev-subject (n &optional unread)
6724   "Go to previous N'th summary line.
6725 If optional argument UNREAD is non-nil, only unread article is selected."
6726   (interactive "p")
6727   (gnus-summary-next-subject (- n) unread))
6728
6729 (defun gnus-summary-prev-unread-subject (n)
6730   "Go to previous N'th unread summary line."
6731   (interactive "p")
6732   (gnus-summary-next-subject (- n) t))
6733
6734 (defun gnus-summary-goto-subjects (articles)
6735   "Insert the subject header for ARTICLES in the current buffer."
6736   (save-excursion
6737     (dolist (article articles)
6738       (gnus-summary-goto-subject articles t)))
6739   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6740   (gnus-summary-position-point))
6741   
6742 (defun gnus-summary-goto-subject (article &optional force silent)
6743   "Go the subject line of ARTICLE.
6744 If FORCE, also allow jumping to articles not currently shown."
6745   (interactive "nArticle number: ")
6746   (unless (numberp article)
6747     (error "Article %s is not a number" article))
6748   (let ((b (point))
6749         (data (gnus-data-find article)))
6750     ;; We read in the article if we have to.
6751     (and (not data)
6752          force
6753          (gnus-summary-insert-subject
6754           article
6755           (if (or (numberp force) (vectorp force)) force)
6756           t)
6757          (setq data (gnus-data-find article)))
6758     (goto-char b)
6759     (if (not data)
6760         (progn
6761           (unless silent
6762             (gnus-message 3 "Can't find article %d" article))
6763           nil)
6764       (let ((pt (gnus-data-pos data)))
6765         (goto-char pt)
6766         (gnus-summary-set-article-display-arrow pt))
6767       (gnus-summary-position-point)
6768       article)))
6769
6770 ;; Walking around summary lines with displaying articles.
6771
6772 (defun gnus-summary-expand-window (&optional arg)
6773   "Make the summary buffer take up the entire Emacs frame.
6774 Given a prefix, will force an `article' buffer configuration."
6775   (interactive "P")
6776   (if arg
6777       (gnus-configure-windows 'article 'force)
6778     (gnus-configure-windows 'summary 'force)))
6779
6780 (defun gnus-summary-display-article (article &optional all-header)
6781   "Display ARTICLE in article buffer."
6782   (when (gnus-buffer-live-p gnus-article-buffer)
6783     (with-current-buffer gnus-article-buffer
6784       (mm-enable-multibyte)))
6785   (gnus-set-global-variables)
6786   (when (gnus-buffer-live-p gnus-article-buffer)
6787     (with-current-buffer gnus-article-buffer
6788       (setq gnus-article-charset gnus-newsgroup-charset)
6789       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6790       (mm-enable-multibyte)))
6791   (if (null article)
6792       nil
6793     (prog1
6794         (if gnus-summary-display-article-function
6795             (funcall gnus-summary-display-article-function article all-header)
6796           (gnus-article-prepare article all-header))
6797       (gnus-run-hooks 'gnus-select-article-hook)
6798       (when (and gnus-current-article
6799                  (not (zerop gnus-current-article)))
6800         (gnus-summary-goto-subject gnus-current-article))
6801       (gnus-summary-recenter)
6802       (when (and gnus-use-trees gnus-show-threads)
6803         (gnus-possibly-generate-tree article)
6804         (gnus-highlight-selected-tree article))
6805       ;; Successfully display article.
6806       (gnus-article-set-window-start
6807        (cdr (assq article gnus-newsgroup-bookmarks))))))
6808
6809 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6810   "Select the current article.
6811 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6812 non-nil, the article will be re-fetched even if it already present in
6813 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6814 be displayed."
6815   ;; Make sure we are in the summary buffer to work around bbdb bug.
6816   (unless (eq major-mode 'gnus-summary-mode)
6817     (set-buffer gnus-summary-buffer))
6818   (let ((article (or article (gnus-summary-article-number)))
6819         (all-headers (not (not all-headers))) ;Must be t or nil.
6820         gnus-summary-display-article-function)
6821     (and (not pseudo)
6822          (gnus-summary-article-pseudo-p article)
6823          (error "This is a pseudo-article"))
6824     (save-excursion
6825       (set-buffer gnus-summary-buffer)
6826       (if (or (and gnus-single-article-buffer
6827                    (or (null gnus-current-article)
6828                        (null gnus-article-current)
6829                        (null (get-buffer gnus-article-buffer))
6830                        (not (eq article (cdr gnus-article-current)))
6831                        (not (equal (car gnus-article-current)
6832                                    gnus-newsgroup-name))))
6833               (and (not gnus-single-article-buffer)
6834                    (or (null gnus-current-article)
6835                        (not (eq gnus-current-article article))))
6836               force)
6837           ;; The requested article is different from the current article.
6838           (progn
6839             (gnus-summary-display-article article all-headers)
6840             (when (gnus-buffer-live-p gnus-article-buffer)
6841               (with-current-buffer gnus-article-buffer
6842                 (if (not gnus-article-decoded-p) ;; a local variable
6843                     (mm-disable-multibyte))))
6844             (gnus-article-set-window-start
6845              (cdr (assq article gnus-newsgroup-bookmarks)))
6846             article)
6847         'old))))
6848
6849 (defun gnus-summary-force-verify-and-decrypt ()
6850   (interactive)
6851   (let ((mm-verify-option 'known)
6852         (mm-decrypt-option 'known)
6853         (gnus-buttonized-mime-types (append (list "multipart/signed"
6854                                                   "multipart/encrypted")
6855                                             gnus-buttonized-mime-types)))
6856     (gnus-summary-select-article nil 'force)))
6857
6858 (defun gnus-summary-set-current-mark (&optional current-mark)
6859   "Obsolete function."
6860   nil)
6861
6862 (defun gnus-summary-next-article (&optional unread subject backward push)
6863   "Select the next article.
6864 If UNREAD, only unread articles are selected.
6865 If SUBJECT, only articles with SUBJECT are selected.
6866 If BACKWARD, the previous article is selected instead of the next."
6867   (interactive "P")
6868   (cond
6869    ;; Is there such an article?
6870    ((and (gnus-summary-search-forward unread subject backward)
6871          (or (gnus-summary-display-article (gnus-summary-article-number))
6872              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6873     (gnus-summary-position-point))
6874    ;; If not, we try the first unread, if that is wanted.
6875    ((and subject
6876          gnus-auto-select-same
6877          (gnus-summary-first-unread-article))
6878     (gnus-summary-position-point)
6879     (gnus-message 6 "Wrapped"))
6880    ;; Try to get next/previous article not displayed in this group.
6881    ((and gnus-auto-extend-newsgroup
6882          (not unread) (not subject))
6883     (gnus-summary-goto-article
6884      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6885      nil (count-lines (point-min) (point))))
6886    ;; Go to next/previous group.
6887    (t
6888     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6889       (gnus-summary-jump-to-group gnus-newsgroup-name))
6890     (let ((cmd last-command-char)
6891           (point
6892            (save-excursion
6893              (set-buffer gnus-group-buffer)
6894              (point)))
6895           (group
6896            (if (eq gnus-keep-same-level 'best)
6897                (gnus-summary-best-group gnus-newsgroup-name)
6898              (gnus-summary-search-group backward gnus-keep-same-level))))
6899       ;; For some reason, the group window gets selected.  We change
6900       ;; it back.
6901       (select-window (get-buffer-window (current-buffer)))
6902       ;; Select next unread newsgroup automagically.
6903       (cond
6904        ((or (not gnus-auto-select-next)
6905             (not cmd))
6906         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6907        ((or (eq gnus-auto-select-next 'quietly)
6908             (and (eq gnus-auto-select-next 'slightly-quietly)
6909                  push)
6910             (and (eq gnus-auto-select-next 'almost-quietly)
6911                  (gnus-summary-last-article-p)))
6912         ;; Select quietly.
6913         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6914             (gnus-summary-exit)
6915           (gnus-message 7 "No more%s articles (%s)..."
6916                         (if unread " unread" "")
6917                         (if group (concat "selecting " group)
6918                           "exiting"))
6919           (gnus-summary-next-group nil group backward)))
6920        (t
6921         (when (gnus-key-press-event-p last-input-event)
6922           (gnus-summary-walk-group-buffer
6923            gnus-newsgroup-name cmd unread backward point))))))))
6924
6925 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6926   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6927                       (?\C-p (gnus-group-prev-unread-group 1))))
6928         (cursor-in-echo-area t)
6929         keve key group ended prompt)
6930     (save-excursion
6931       (set-buffer gnus-group-buffer)
6932       (goto-char start)
6933       (setq group
6934             (if (eq gnus-keep-same-level 'best)
6935                 (gnus-summary-best-group gnus-newsgroup-name)
6936               (gnus-summary-search-group backward gnus-keep-same-level))))
6937     (while (not ended)
6938       (setq prompt
6939             (format
6940              "No more%s articles%s " (if unread " unread" "")
6941              (if (and group
6942                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6943                  (format " (Type %s for %s [%s])"
6944                          (single-key-description cmd) group
6945                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6946                (format " (Type %s to exit %s)"
6947                        (single-key-description cmd)
6948                        gnus-newsgroup-name))))
6949       ;; Confirm auto selection.
6950       (setq key (car (setq keve (gnus-read-event-char prompt)))
6951             ended t)
6952       (cond
6953        ((assq key keystrokes)
6954         (let ((obuf (current-buffer)))
6955           (switch-to-buffer gnus-group-buffer)
6956           (when group
6957             (gnus-group-jump-to-group group))
6958           (eval (cadr (assq key keystrokes)))
6959           (setq group (gnus-group-group-name))
6960           (switch-to-buffer obuf))
6961         (setq ended nil))
6962        ((equal key cmd)
6963         (if (or (not group)
6964                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6965             (gnus-summary-exit)
6966           (gnus-summary-next-group nil group backward)))
6967        (t
6968         (push (cdr keve) unread-command-events))))))
6969
6970 (defun gnus-summary-next-unread-article ()
6971   "Select unread article after current one."
6972   (interactive)
6973   (gnus-summary-next-article
6974    (or (not (eq gnus-summary-goto-unread 'never))
6975        (gnus-summary-last-article-p (gnus-summary-article-number)))
6976    (and gnus-auto-select-same
6977         (gnus-summary-article-subject))))
6978
6979 (defun gnus-summary-prev-article (&optional unread subject)
6980   "Select the article after the current one.
6981 If UNREAD is non-nil, only unread articles are selected."
6982   (interactive "P")
6983   (gnus-summary-next-article unread subject t))
6984
6985 (defun gnus-summary-prev-unread-article ()
6986   "Select unread article before current one."
6987   (interactive)
6988   (gnus-summary-prev-article
6989    (or (not (eq gnus-summary-goto-unread 'never))
6990        (gnus-summary-first-article-p (gnus-summary-article-number)))
6991    (and gnus-auto-select-same
6992         (gnus-summary-article-subject))))
6993
6994 (defun gnus-summary-next-page (&optional lines circular stop)
6995   "Show next page of the selected article.
6996 If at the end of the current article, select the next article.
6997 LINES says how many lines should be scrolled up.
6998
6999 If CIRCULAR is non-nil, go to the start of the article instead of
7000 selecting the next article when reaching the end of the current
7001 article.
7002
7003 If STOP is non-nil, just stop when reaching the end of the message."
7004   (interactive "P")
7005   (setq gnus-summary-buffer (current-buffer))
7006   (gnus-set-global-variables)
7007   (let ((article (gnus-summary-article-number))
7008         (article-window (get-buffer-window gnus-article-buffer t))
7009         endp)
7010     ;; If the buffer is empty, we have no article.
7011     (unless article
7012       (error "No article to select"))
7013     (gnus-configure-windows 'article)
7014     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7015         (if (and (eq gnus-summary-goto-unread 'never)
7016                  (not (gnus-summary-last-article-p article)))
7017             (gnus-summary-next-article)
7018           (gnus-summary-next-unread-article))
7019       (if (or (null gnus-current-article)
7020               (null gnus-article-current)
7021               (/= article (cdr gnus-article-current))
7022               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7023           ;; Selected subject is different from current article's.
7024           (gnus-summary-display-article article)
7025         (when article-window
7026           (gnus-eval-in-buffer-window gnus-article-buffer
7027             (setq endp (gnus-article-next-page lines)))
7028           (when endp
7029             (cond (stop
7030                    (gnus-message 3 "End of message"))
7031                   (circular
7032                    (gnus-summary-beginning-of-article))
7033                   (lines
7034                    (gnus-message 3 "End of message"))
7035                   ((null lines)
7036                    (if (and (eq gnus-summary-goto-unread 'never)
7037                             (not (gnus-summary-last-article-p article)))
7038                        (gnus-summary-next-article)
7039                      (gnus-summary-next-unread-article))))))))
7040     (gnus-summary-recenter)
7041     (gnus-summary-position-point)))
7042
7043 (defun gnus-summary-prev-page (&optional lines move)
7044   "Show previous page of selected article.
7045 Argument LINES specifies lines to be scrolled down.
7046 If MOVE, move to the previous unread article if point is at
7047 the beginning of the buffer."
7048   (interactive "P")
7049   (let ((article (gnus-summary-article-number))
7050         (article-window (get-buffer-window gnus-article-buffer t))
7051         endp)
7052     (gnus-configure-windows 'article)
7053     (if (or (null gnus-current-article)
7054             (null gnus-article-current)
7055             (/= article (cdr gnus-article-current))
7056             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7057         ;; Selected subject is different from current article's.
7058         (gnus-summary-display-article article)
7059       (gnus-summary-recenter)
7060       (when article-window
7061         (gnus-eval-in-buffer-window gnus-article-buffer
7062           (setq endp (gnus-article-prev-page lines)))
7063         (when (and move endp)
7064           (cond (lines
7065                  (gnus-message 3 "Beginning of message"))
7066                 ((null lines)
7067                  (if (and (eq gnus-summary-goto-unread 'never)
7068                           (not (gnus-summary-first-article-p article)))
7069                      (gnus-summary-prev-article)
7070                    (gnus-summary-prev-unread-article))))))))
7071   (gnus-summary-position-point))
7072
7073 (defun gnus-summary-prev-page-or-article (&optional lines)
7074   "Show previous page of selected article.
7075 Argument LINES specifies lines to be scrolled down.
7076 If at the beginning of the article, go to the next article."
7077   (interactive "P")
7078   (gnus-summary-prev-page lines t))
7079
7080 (defun gnus-summary-scroll-up (lines)
7081   "Scroll up (or down) one line current article.
7082 Argument LINES specifies lines to be scrolled up (or down if negative)."
7083   (interactive "p")
7084   (gnus-configure-windows 'article)
7085   (gnus-summary-show-thread)
7086   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7087     (gnus-eval-in-buffer-window gnus-article-buffer
7088       (cond ((> lines 0)
7089              (when (gnus-article-next-page lines)
7090                (gnus-message 3 "End of message")))
7091             ((< lines 0)
7092              (gnus-article-prev-page (- lines))))))
7093   (gnus-summary-recenter)
7094   (gnus-summary-position-point))
7095
7096 (defun gnus-summary-scroll-down (lines)
7097   "Scroll down (or up) one line current article.
7098 Argument LINES specifies lines to be scrolled down (or up if negative)."
7099   (interactive "p")
7100   (gnus-summary-scroll-up (- lines)))
7101
7102 (defun gnus-summary-next-same-subject ()
7103   "Select next article which has the same subject as current one."
7104   (interactive)
7105   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7106
7107 (defun gnus-summary-prev-same-subject ()
7108   "Select previous article which has the same subject as current one."
7109   (interactive)
7110   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7111
7112 (defun gnus-summary-next-unread-same-subject ()
7113   "Select next unread article which has the same subject as current one."
7114   (interactive)
7115   (gnus-summary-next-article t (gnus-summary-article-subject)))
7116
7117 (defun gnus-summary-prev-unread-same-subject ()
7118   "Select previous unread article which has the same subject as current one."
7119   (interactive)
7120   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7121
7122 (defun gnus-summary-first-unread-article ()
7123   "Select the first unread article.
7124 Return nil if there are no unread articles."
7125   (interactive)
7126   (prog1
7127       (when (gnus-summary-first-subject t)
7128         (gnus-summary-show-thread)
7129         (gnus-summary-first-subject t)
7130         (gnus-summary-display-article (gnus-summary-article-number)))
7131     (gnus-summary-position-point)))
7132
7133 (defun gnus-summary-first-unread-subject ()
7134   "Place the point on the subject line of the first unread article.
7135 Return nil if there are no unread articles."
7136   (interactive)
7137   (prog1
7138       (when (gnus-summary-first-subject t)
7139         (gnus-summary-show-thread)
7140         (gnus-summary-first-subject t))
7141     (gnus-summary-position-point)))
7142
7143 (defun gnus-summary-first-unseen-subject ()
7144   "Place the point on the subject line of the first unseen article.
7145 Return nil if there are no unseen articles."
7146   (interactive)
7147   (prog1
7148       (when (gnus-summary-first-subject t t t)
7149         (gnus-summary-show-thread)
7150         (gnus-summary-first-subject t t t))
7151     (gnus-summary-position-point)))
7152
7153 (defun gnus-summary-first-unseen-or-unread-subject ()
7154   "Place the point on the subject line of the first unseen article.
7155 Return nil if there are no unseen articles."
7156   (interactive)
7157   (prog1
7158       (unless (when (gnus-summary-first-subject t t t)
7159                 (gnus-summary-show-thread)
7160                 (gnus-summary-first-subject t t t))
7161         (when (gnus-summary-first-subject t)
7162           (gnus-summary-show-thread)
7163           (gnus-summary-first-subject t)))
7164     (gnus-summary-position-point)))
7165
7166 (defun gnus-summary-first-article ()
7167   "Select the first article.
7168 Return nil if there are no articles."
7169   (interactive)
7170   (prog1
7171       (when (gnus-summary-first-subject)
7172         (gnus-summary-show-thread)
7173         (gnus-summary-first-subject)
7174         (gnus-summary-display-article (gnus-summary-article-number)))
7175     (gnus-summary-position-point)))
7176
7177 (defun gnus-summary-best-unread-article (&optional arg)
7178   "Select the unread article with the highest score.
7179 If given a prefix argument, select the next unread article that has a
7180 score higher than the default score."
7181   (interactive "P")
7182   (let ((article (if arg
7183                      (gnus-summary-better-unread-subject)
7184                    (gnus-summary-best-unread-subject))))
7185     (if article
7186         (gnus-summary-goto-article article)
7187       (error "No unread articles"))))
7188
7189 (defun gnus-summary-best-unread-subject ()
7190   "Select the unread subject with the highest score."
7191   (interactive)
7192   (let ((best -1000000)
7193         (data gnus-newsgroup-data)
7194         article score)
7195     (while data
7196       (and (gnus-data-unread-p (car data))
7197            (> (setq score
7198                     (gnus-summary-article-score (gnus-data-number (car data))))
7199               best)
7200            (setq best score
7201                  article (gnus-data-number (car data))))
7202       (setq data (cdr data)))
7203     (when article
7204       (gnus-summary-goto-subject article))
7205     (gnus-summary-position-point)
7206     article))
7207
7208 (defun gnus-summary-better-unread-subject ()
7209   "Select the first unread subject that has a score over the default score."
7210   (interactive)
7211   (let ((data gnus-newsgroup-data)
7212         article score)
7213     (while (and (setq article (gnus-data-number (car data)))
7214                 (or (gnus-data-read-p (car data))
7215                     (not (> (gnus-summary-article-score article)
7216                             gnus-summary-default-score))))
7217       (setq data (cdr data)))
7218     (when article
7219       (gnus-summary-goto-subject article))
7220     (gnus-summary-position-point)
7221     article))
7222
7223 (defun gnus-summary-last-subject ()
7224   "Go to the last displayed subject line in the group."
7225   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7226     (when article
7227       (gnus-summary-goto-subject article))))
7228
7229 (defun gnus-summary-goto-article (article &optional all-headers force)
7230   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7231 If ALL-HEADERS is non-nil, no header lines are hidden.
7232 If FORCE, go to the article even if it isn't displayed.  If FORCE
7233 is a number, it is the line the article is to be displayed on."
7234   (interactive
7235    (list
7236     (completing-read
7237      "Article number or Message-ID: "
7238      (mapcar (lambda (number) (list (int-to-string number)))
7239              gnus-newsgroup-limit))
7240     current-prefix-arg
7241     t))
7242   (prog1
7243       (if (and (stringp article)
7244                (string-match "@" article))
7245           (gnus-summary-refer-article article)
7246         (when (stringp article)
7247           (setq article (string-to-number article)))
7248         (if (gnus-summary-goto-subject article force)
7249             (gnus-summary-display-article article all-headers)
7250           (gnus-message 4 "Couldn't go to article %s" article) nil))
7251     (gnus-summary-position-point)))
7252
7253 (defun gnus-summary-goto-last-article ()
7254   "Go to the previously read article."
7255   (interactive)
7256   (prog1
7257       (when gnus-last-article
7258         (gnus-summary-goto-article gnus-last-article nil t))
7259     (gnus-summary-position-point)))
7260
7261 (defun gnus-summary-pop-article (number)
7262   "Pop one article off the history and go to the previous.
7263 NUMBER articles will be popped off."
7264   (interactive "p")
7265   (let (to)
7266     (setq gnus-newsgroup-history
7267           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7268     (if to
7269         (gnus-summary-goto-article (car to) nil t)
7270       (error "Article history empty")))
7271   (gnus-summary-position-point))
7272
7273 ;; Summary commands and functions for limiting the summary buffer.
7274
7275 (defun gnus-summary-limit-to-articles (n)
7276   "Limit the summary buffer to the next N articles.
7277 If not given a prefix, use the process marked articles instead."
7278   (interactive "P")
7279   (prog1
7280       (let ((articles (gnus-summary-work-articles n)))
7281         (setq gnus-newsgroup-processable nil)
7282         (gnus-summary-limit articles))
7283     (gnus-summary-position-point)))
7284
7285 (defun gnus-summary-pop-limit (&optional total)
7286   "Restore the previous limit.
7287 If given a prefix, remove all limits."
7288   (interactive "P")
7289   (when total
7290     (setq gnus-newsgroup-limits
7291           (list (mapcar (lambda (h) (mail-header-number h))
7292                         gnus-newsgroup-headers))))
7293   (unless gnus-newsgroup-limits
7294     (error "No limit to pop"))
7295   (prog1
7296       (gnus-summary-limit nil 'pop)
7297     (gnus-summary-position-point)))
7298
7299 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7300   "Limit the summary buffer to articles that have subjects that match a regexp.
7301 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7302   (interactive
7303    (list (read-string (if current-prefix-arg
7304                           "Exclude subject (regexp): "
7305                         "Limit to subject (regexp): "))
7306          nil current-prefix-arg))
7307   (unless header
7308     (setq header "subject"))
7309   (when (not (equal "" subject))
7310     (prog1
7311         (let ((articles (gnus-summary-find-matching
7312                          (or header "subject") subject 'all nil nil
7313                          not-matching)))
7314           (unless articles
7315             (error "Found no matches for \"%s\"" subject))
7316           (gnus-summary-limit articles))
7317       (gnus-summary-position-point))))
7318
7319 (defun gnus-summary-limit-to-author (from &optional not-matching)
7320   "Limit the summary buffer to articles that have authors that match a regexp.
7321 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7322   (interactive
7323    (list (read-string (if current-prefix-arg
7324                           "Exclude author (regexp): "
7325                         "Limit to author (regexp): "))
7326          current-prefix-arg))
7327   (gnus-summary-limit-to-subject from "from" not-matching))
7328
7329 (defun gnus-summary-limit-to-age (age &optional younger-p)
7330   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7331 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7332 articles that are younger than AGE days."
7333   (interactive
7334    (let ((younger current-prefix-arg)
7335          (days-got nil)
7336          days)
7337      (while (not days-got)
7338        (setq days (if younger
7339                       (read-string "Limit to articles younger than (in days, older when negative): ")
7340                     (read-string
7341                      "Limit to articles older than (in days, younger when negative): ")))
7342        (when (> (length days) 0)
7343          (setq days (read days)))
7344        (if (numberp days)
7345            (progn
7346              (setq days-got t)
7347              (if (< days 0)
7348                  (progn
7349                    (setq younger (not younger))
7350                    (setq days (* days -1)))))
7351          (message "Please enter a number.")
7352          (sleep-for 1)))
7353      (list days younger)))
7354   (prog1
7355       (let ((data gnus-newsgroup-data)
7356             (cutoff (days-to-time age))
7357             articles d date is-younger)
7358         (while (setq d (pop data))
7359           (when (and (vectorp (gnus-data-header d))
7360                      (setq date (mail-header-date (gnus-data-header d))))
7361             (setq is-younger (time-less-p
7362                               (time-since (condition-case ()
7363                                               (date-to-time date)
7364                                             (error '(0 0))))
7365                               cutoff))
7366             (when (if younger-p
7367                       is-younger
7368                     (not is-younger))
7369               (push (gnus-data-number d) articles))))
7370         (gnus-summary-limit (nreverse articles)))
7371     (gnus-summary-position-point)))
7372
7373 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7374   "Limit the summary buffer to articles that match an 'extra' header."
7375   (interactive
7376    (let ((header
7377           (intern
7378            (gnus-completing-read-with-default
7379             (symbol-name (car gnus-extra-headers))
7380             (if current-prefix-arg
7381                 "Exclude extra header:"
7382               "Limit extra header:")
7383             (mapcar (lambda (x)
7384                       (cons (symbol-name x) x))
7385                     gnus-extra-headers)
7386             nil
7387             t))))
7388      (list header
7389            (read-string (format "%s header %s (regexp): "
7390                                 (if current-prefix-arg "Exclude" "Limit to")
7391                                 header))
7392            current-prefix-arg)))
7393   (when (not (equal "" regexp))
7394     (prog1
7395         (let ((articles (gnus-summary-find-matching
7396                          (cons 'extra header) regexp 'all nil nil
7397                          not-matching)))
7398           (unless articles
7399             (error "Found no matches for \"%s\"" regexp))
7400           (gnus-summary-limit articles))
7401       (gnus-summary-position-point))))
7402
7403 (defun gnus-summary-limit-to-display-predicate ()
7404   "Limit the summary buffer to the predicated in the `display' group parameter."
7405   (interactive)
7406   (unless gnus-newsgroup-display
7407     (error "There is no `display' group parameter"))
7408   (let (articles)
7409     (dolist (number gnus-newsgroup-articles)
7410       (when (funcall gnus-newsgroup-display)
7411         (push number articles)))
7412     (gnus-summary-limit articles))
7413   (gnus-summary-position-point))
7414
7415 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7416 (make-obsolete
7417  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7418
7419 (defun gnus-summary-limit-to-unread (&optional all)
7420   "Limit the summary buffer to articles that are not marked as read.
7421 If ALL is non-nil, limit strictly to unread articles."
7422   (interactive "P")
7423   (if all
7424       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7425     (gnus-summary-limit-to-marks
7426      ;; Concat all the marks that say that an article is read and have
7427      ;; those removed.
7428      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7429            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7430            gnus-low-score-mark gnus-expirable-mark
7431            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7432            gnus-duplicate-mark gnus-souped-mark)
7433      'reverse)))
7434
7435 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7436 (make-obsolete 'gnus-summary-delete-marked-with
7437                'gnus-summary-limit-exclude-marks)
7438
7439 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7440   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7441 If REVERSE, limit the summary buffer to articles that are marked
7442 with MARKS.  MARKS can either be a string of marks or a list of marks.
7443 Returns how many articles were removed."
7444   (interactive "sMarks: ")
7445   (gnus-summary-limit-to-marks marks t))
7446
7447 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7448   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7449 If REVERSE (the prefix), limit the summary buffer to articles that are
7450 not marked with MARKS.  MARKS can either be a string of marks or a
7451 list of marks.
7452 Returns how many articles were removed."
7453   (interactive "sMarks: \nP")
7454   (prog1
7455       (let ((data gnus-newsgroup-data)
7456             (marks (if (listp marks) marks
7457                      (append marks nil))) ; Transform to list.
7458             articles)
7459         (while data
7460           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7461                   (memq (gnus-data-mark (car data)) marks))
7462             (push (gnus-data-number (car data)) articles))
7463           (setq data (cdr data)))
7464         (gnus-summary-limit articles))
7465     (gnus-summary-position-point)))
7466
7467 (defun gnus-summary-limit-to-score (score)
7468   "Limit to articles with score at or above SCORE."
7469   (interactive "NLimit to articles with score of at least: ")
7470   (let ((data gnus-newsgroup-data)
7471         articles)
7472     (while data
7473       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7474                 score)
7475         (push (gnus-data-number (car data)) articles))
7476       (setq data (cdr data)))
7477     (prog1
7478         (gnus-summary-limit articles)
7479       (gnus-summary-position-point))))
7480
7481 (defun gnus-summary-limit-to-unseen ()
7482   "Limit to unseen articles."
7483   (interactive)
7484   (prog1
7485       (gnus-summary-limit gnus-newsgroup-unseen)
7486     (gnus-summary-position-point)))
7487
7488 (defun gnus-summary-limit-include-thread (id)
7489   "Display all the hidden articles that is in the thread with ID in it.
7490 When called interactively, ID is the Message-ID of the current
7491 article."
7492   (interactive (list (mail-header-id (gnus-summary-article-header))))
7493   (let ((articles (gnus-articles-in-thread
7494                    (gnus-id-to-thread (gnus-root-id id)))))
7495     (prog1
7496         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7497       (gnus-summary-limit-include-matching-articles
7498        "subject"
7499        (regexp-quote (gnus-simplify-subject-re
7500                       (mail-header-subject (gnus-id-to-header id)))))
7501       (gnus-summary-position-point))))
7502
7503 (defun gnus-summary-limit-include-matching-articles (header regexp)
7504   "Display all the hidden articles that have HEADERs that match REGEXP."
7505   (interactive (list (read-string "Match on header: ")
7506                      (read-string "Regexp: ")))
7507   (let ((articles (gnus-find-matching-articles header regexp)))
7508     (prog1
7509         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7510       (gnus-summary-position-point))))
7511
7512 (defun gnus-summary-insert-dormant-articles ()
7513   "Insert all the dormat articles for this group into the current buffer."
7514   (interactive)
7515   (let ((gnus-verbose (max 6 gnus-verbose)))
7516     (if (not gnus-newsgroup-dormant)
7517         (gnus-message 3 "No cached articles for this group")
7518       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7519
7520 (defun gnus-summary-limit-include-dormant ()
7521   "Display all the hidden articles that are marked as dormant.
7522 Note that this command only works on a subset of the articles currently
7523 fetched for this group."
7524   (interactive)
7525   (unless gnus-newsgroup-dormant
7526     (error "There are no dormant articles in this group"))
7527   (prog1
7528       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7529     (gnus-summary-position-point)))
7530
7531 (defun gnus-summary-limit-exclude-dormant ()
7532   "Hide all dormant articles."
7533   (interactive)
7534   (prog1
7535       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7536     (gnus-summary-position-point)))
7537
7538 (defun gnus-summary-limit-exclude-childless-dormant ()
7539   "Hide all dormant articles that have no children."
7540   (interactive)
7541   (let ((data (gnus-data-list t))
7542         articles d children)
7543     ;; Find all articles that are either not dormant or have
7544     ;; children.
7545     (while (setq d (pop data))
7546       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7547                 (and (setq children
7548                            (gnus-article-children (gnus-data-number d)))
7549                      (let (found)
7550                        (while children
7551                          (when (memq (car children) articles)
7552                            (setq children nil
7553                                  found t))
7554                          (pop children))
7555                        found)))
7556         (push (gnus-data-number d) articles)))
7557     ;; Do the limiting.
7558     (prog1
7559         (gnus-summary-limit articles)
7560       (gnus-summary-position-point))))
7561
7562 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7563   "Mark all unread excluded articles as read.
7564 If ALL, mark even excluded ticked and dormants as read."
7565   (interactive "P")
7566   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7567   (let ((articles (gnus-sorted-ndifference
7568                    (sort
7569                     (mapcar (lambda (h) (mail-header-number h))
7570                             gnus-newsgroup-headers)
7571                     '<)
7572                    gnus-newsgroup-limit))
7573         article)
7574     (setq gnus-newsgroup-unreads
7575           (gnus-sorted-intersection gnus-newsgroup-unreads
7576                                     gnus-newsgroup-limit))
7577     (if all
7578         (setq gnus-newsgroup-dormant nil
7579               gnus-newsgroup-marked nil
7580               gnus-newsgroup-reads
7581               (nconc
7582                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7583                gnus-newsgroup-reads))
7584       (while (setq article (pop articles))
7585         (unless (or (memq article gnus-newsgroup-dormant)
7586                     (memq article gnus-newsgroup-marked))
7587           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7588
7589 (defun gnus-summary-limit (articles &optional pop)
7590   (if pop
7591       ;; We pop the previous limit off the stack and use that.
7592       (setq articles (car gnus-newsgroup-limits)
7593             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7594     ;; We use the new limit, so we push the old limit on the stack.
7595     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7596   ;; Set the limit.
7597   (setq gnus-newsgroup-limit articles)
7598   (let ((total (length gnus-newsgroup-data))
7599         (data (gnus-data-find-list (gnus-summary-article-number)))
7600         (gnus-summary-mark-below nil)   ; Inhibit this.
7601         found)
7602     ;; This will do all the work of generating the new summary buffer
7603     ;; according to the new limit.
7604     (gnus-summary-prepare)
7605     ;; Hide any threads, possibly.
7606     (gnus-summary-maybe-hide-threads)
7607     ;; Try to return to the article you were at, or one in the
7608     ;; neighborhood.
7609     (when data
7610       ;; We try to find some article after the current one.
7611       (while data
7612         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7613           (setq data nil
7614                 found t))
7615         (setq data (cdr data))))
7616     (unless found
7617       ;; If there is no data, that means that we were after the last
7618       ;; article.  The same goes when we can't find any articles
7619       ;; after the current one.
7620       (goto-char (point-max))
7621       (gnus-summary-find-prev))
7622     (gnus-set-mode-line 'summary)
7623     ;; We return how many articles were removed from the summary
7624     ;; buffer as a result of the new limit.
7625     (- total (length gnus-newsgroup-data))))
7626
7627 (defsubst gnus-invisible-cut-children (threads)
7628   (let ((num 0))
7629     (while threads
7630       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7631         (incf num))
7632       (pop threads))
7633     (< num 2)))
7634
7635 (defsubst gnus-cut-thread (thread)
7636   "Go forwards in the thread until we find an article that we want to display."
7637   (when (or (eq gnus-fetch-old-headers 'some)
7638             (eq gnus-fetch-old-headers 'invisible)
7639             (numberp gnus-fetch-old-headers)
7640             (eq gnus-build-sparse-threads 'some)
7641             (eq gnus-build-sparse-threads 'more))
7642     ;; Deal with old-fetched headers and sparse threads.
7643     (while (and
7644             thread
7645             (or
7646              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7647              (gnus-summary-article-ancient-p
7648               (mail-header-number (car thread))))
7649             (if (or (<= (length (cdr thread)) 1)
7650                     (eq gnus-fetch-old-headers 'invisible))
7651                 (setq gnus-newsgroup-limit
7652                       (delq (mail-header-number (car thread))
7653                             gnus-newsgroup-limit)
7654                       thread (cadr thread))
7655               (when (gnus-invisible-cut-children (cdr thread))
7656                 (let ((th (cdr thread)))
7657                   (while th
7658                     (if (memq (mail-header-number (caar th))
7659                               gnus-newsgroup-limit)
7660                         (setq thread (car th)
7661                               th nil)
7662                       (setq th (cdr th))))))))))
7663   thread)
7664
7665 (defun gnus-cut-threads (threads)
7666   "Cut off all uninteresting articles from the beginning of threads."
7667   (when (or (eq gnus-fetch-old-headers 'some)
7668             (eq gnus-fetch-old-headers 'invisible)
7669             (numberp gnus-fetch-old-headers)
7670             (eq gnus-build-sparse-threads 'some)
7671             (eq gnus-build-sparse-threads 'more))
7672     (let ((th threads))
7673       (while th
7674         (setcar th (gnus-cut-thread (car th)))
7675         (setq th (cdr th)))))
7676   ;; Remove nixed out threads.
7677   (delq nil threads))
7678
7679 (defun gnus-summary-initial-limit (&optional show-if-empty)
7680   "Figure out what the initial limit is supposed to be on group entry.
7681 This entails weeding out unwanted dormants, low-scored articles,
7682 fetch-old-headers verbiage, and so on."
7683   ;; Most groups have nothing to remove.
7684   (if (or gnus-inhibit-limiting
7685           (and (null gnus-newsgroup-dormant)
7686                (eq gnus-newsgroup-display 'gnus-not-ignore)
7687                (not (eq gnus-fetch-old-headers 'some))
7688                (not (numberp gnus-fetch-old-headers))
7689                (not (eq gnus-fetch-old-headers 'invisible))
7690                (null gnus-summary-expunge-below)
7691                (not (eq gnus-build-sparse-threads 'some))
7692                (not (eq gnus-build-sparse-threads 'more))
7693                (null gnus-thread-expunge-below)
7694                (not gnus-use-nocem)))
7695       ()                                ; Do nothing.
7696     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7697     (setq gnus-newsgroup-limit nil)
7698     (mapatoms
7699      (lambda (node)
7700        (unless (car (symbol-value node))
7701          ;; These threads have no parents -- they are roots.
7702          (let ((nodes (cdr (symbol-value node)))
7703                thread)
7704            (while nodes
7705              (if (and gnus-thread-expunge-below
7706                       (< (gnus-thread-total-score (car nodes))
7707                          gnus-thread-expunge-below))
7708                  (gnus-expunge-thread (pop nodes))
7709                (setq thread (pop nodes))
7710                (gnus-summary-limit-children thread))))))
7711      gnus-newsgroup-dependencies)
7712     ;; If this limitation resulted in an empty group, we might
7713     ;; pop the previous limit and use it instead.
7714     (when (and (not gnus-newsgroup-limit)
7715                show-if-empty)
7716       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7717     gnus-newsgroup-limit))
7718
7719 (defun gnus-summary-limit-children (thread)
7720   "Return 1 if this subthread is visible and 0 if it is not."
7721   ;; First we get the number of visible children to this thread.  This
7722   ;; is done by recursing down the thread using this function, so this
7723   ;; will really go down to a leaf article first, before slowly
7724   ;; working its way up towards the root.
7725   (when thread
7726     (let* ((max-lisp-eval-depth 5000)
7727            (children
7728            (if (cdr thread)
7729                (apply '+ (mapcar 'gnus-summary-limit-children
7730                                  (cdr thread)))
7731              0))
7732           (number (mail-header-number (car thread)))
7733           score)
7734       (if (and
7735            (not (memq number gnus-newsgroup-marked))
7736            (or
7737             ;; If this article is dormant and has absolutely no visible
7738             ;; children, then this article isn't visible.
7739             (and (memq number gnus-newsgroup-dormant)
7740                  (zerop children))
7741             ;; If this is "fetch-old-headered" and there is no
7742             ;; visible children, then we don't want this article.
7743             (and (or (eq gnus-fetch-old-headers 'some)
7744                      (numberp gnus-fetch-old-headers))
7745                  (gnus-summary-article-ancient-p number)
7746                  (zerop children))
7747             ;; If this is "fetch-old-headered" and `invisible', then
7748             ;; we don't want this article.
7749             (and (eq gnus-fetch-old-headers 'invisible)
7750                  (gnus-summary-article-ancient-p number))
7751             ;; If this is a sparsely inserted article with no children,
7752             ;; we don't want it.
7753             (and (eq gnus-build-sparse-threads 'some)
7754                  (gnus-summary-article-sparse-p number)
7755                  (zerop children))
7756             ;; If we use expunging, and this article is really
7757             ;; low-scored, then we don't want this article.
7758             (when (and gnus-summary-expunge-below
7759                        (< (setq score
7760                                 (or (cdr (assq number gnus-newsgroup-scored))
7761                                     gnus-summary-default-score))
7762                           gnus-summary-expunge-below))
7763               ;; We increase the expunge-tally here, but that has
7764               ;; nothing to do with the limits, really.
7765               (incf gnus-newsgroup-expunged-tally)
7766               ;; We also mark as read here, if that's wanted.
7767               (when (and gnus-summary-mark-below
7768                          (< score gnus-summary-mark-below))
7769                 (setq gnus-newsgroup-unreads
7770                       (delq number gnus-newsgroup-unreads))
7771                 (if gnus-newsgroup-auto-expire
7772                     (push number gnus-newsgroup-expirable)
7773                   (push (cons number gnus-low-score-mark)
7774                         gnus-newsgroup-reads)))
7775               t)
7776             ;; Do the `display' group parameter.
7777             (and gnus-newsgroup-display
7778                  (not (funcall gnus-newsgroup-display)))
7779             ;; Check NoCeM things.
7780             (if (and gnus-use-nocem
7781                      (gnus-nocem-unwanted-article-p
7782                       (mail-header-id (car thread))))
7783                 (progn
7784                   (setq gnus-newsgroup-unreads
7785                         (delq number gnus-newsgroup-unreads))
7786                   t))))
7787           ;; Nope, invisible article.
7788           0
7789         ;; Ok, this article is to be visible, so we add it to the limit
7790         ;; and return 1.
7791         (push number gnus-newsgroup-limit)
7792         1))))
7793
7794 (defun gnus-expunge-thread (thread)
7795   "Mark all articles in THREAD as read."
7796   (let* ((number (mail-header-number (car thread))))
7797     (incf gnus-newsgroup-expunged-tally)
7798     ;; We also mark as read here, if that's wanted.
7799     (setq gnus-newsgroup-unreads
7800           (delq number gnus-newsgroup-unreads))
7801     (if gnus-newsgroup-auto-expire
7802         (push number gnus-newsgroup-expirable)
7803       (push (cons number gnus-low-score-mark)
7804             gnus-newsgroup-reads)))
7805   ;; Go recursively through all subthreads.
7806   (mapcar 'gnus-expunge-thread (cdr thread)))
7807
7808 ;; Summary article oriented commands
7809
7810 (defun gnus-summary-refer-parent-article (n)
7811   "Refer parent article N times.
7812 If N is negative, go to ancestor -N instead.
7813 The difference between N and the number of articles fetched is returned."
7814   (interactive "p")
7815   (let ((skip 1)
7816         error header ref)
7817     (when (not (natnump n))
7818       (setq skip (abs n)
7819             n 1))
7820     (while (and (> n 0)
7821                 (not error))
7822       (setq header (gnus-summary-article-header))
7823       (if (and (eq (mail-header-number header)
7824                    (cdr gnus-article-current))
7825                (equal gnus-newsgroup-name
7826                       (car gnus-article-current)))
7827           ;; If we try to find the parent of the currently
7828           ;; displayed article, then we take a look at the actual
7829           ;; References header, since this is slightly more
7830           ;; reliable than the References field we got from the
7831           ;; server.
7832           (save-excursion
7833             (set-buffer gnus-original-article-buffer)
7834             (nnheader-narrow-to-headers)
7835             (unless (setq ref (message-fetch-field "references"))
7836               (setq ref (message-fetch-field "in-reply-to")))
7837             (widen))
7838         (setq ref
7839               ;; It's not the current article, so we take a bet on
7840               ;; the value we got from the server.
7841               (mail-header-references header)))
7842       (if (and ref
7843                (not (equal ref "")))
7844           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7845             (gnus-message 1 "Couldn't find parent"))
7846         (gnus-message 1 "No references in article %d"
7847                       (gnus-summary-article-number))
7848         (setq error t))
7849       (decf n))
7850     (gnus-summary-position-point)
7851     n))
7852
7853 (defun gnus-summary-refer-references ()
7854   "Fetch all articles mentioned in the References header.
7855 Return the number of articles fetched."
7856   (interactive)
7857   (let ((ref (mail-header-references (gnus-summary-article-header)))
7858         (current (gnus-summary-article-number))
7859         (n 0))
7860     (if (or (not ref)
7861             (equal ref ""))
7862         (error "No References in the current article")
7863       ;; For each Message-ID in the References header...
7864       (while (string-match "<[^>]*>" ref)
7865         (incf n)
7866         ;; ... fetch that article.
7867         (gnus-summary-refer-article
7868          (prog1 (match-string 0 ref)
7869            (setq ref (substring ref (match-end 0))))))
7870       (gnus-summary-goto-subject current)
7871       (gnus-summary-position-point)
7872       n)))
7873
7874 (defun gnus-summary-refer-thread (&optional limit)
7875   "Fetch all articles in the current thread.
7876 If LIMIT (the numerical prefix), fetch that many old headers instead
7877 of what's specified by the `gnus-refer-thread-limit' variable."
7878   (interactive "P")
7879   (let ((id (mail-header-id (gnus-summary-article-header)))
7880         (limit (if limit (prefix-numeric-value limit)
7881                  gnus-refer-thread-limit)))
7882     ;; We want to fetch LIMIT *old* headers, but we also have to
7883     ;; re-fetch all the headers in the current buffer, because many of
7884     ;; them may be undisplayed.  So we adjust LIMIT.
7885     (when (numberp limit)
7886       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7887     (unless (eq gnus-fetch-old-headers 'invisible)
7888       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7889       ;; Retrieve the headers and read them in.
7890       (if (eq (gnus-retrieve-headers
7891                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7892               'nov)
7893           (gnus-build-all-threads)
7894         (error "Can't fetch thread from backends that don't support NOV"))
7895       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7896     (gnus-summary-limit-include-thread id)))
7897
7898 (defun gnus-summary-refer-article (message-id)
7899   "Fetch an article specified by MESSAGE-ID."
7900   (interactive "sMessage-ID: ")
7901   (when (and (stringp message-id)
7902              (not (zerop (length message-id))))
7903     ;; Construct the correct Message-ID if necessary.
7904     ;; Suggested by tale@pawl.rpi.edu.
7905     (unless (string-match "^<" message-id)
7906       (setq message-id (concat "<" message-id)))
7907     (unless (string-match ">$" message-id)
7908       (setq message-id (concat message-id ">")))
7909     (let* ((header (gnus-id-to-header message-id))
7910            (sparse (and header
7911                         (gnus-summary-article-sparse-p
7912                          (mail-header-number header))
7913                         (memq (mail-header-number header)
7914                               gnus-newsgroup-limit)))
7915            number)
7916       (cond
7917        ;; If the article is present in the buffer we just go to it.
7918        ((and header
7919              (or (not (gnus-summary-article-sparse-p
7920                        (mail-header-number header)))
7921                  sparse))
7922         (prog1
7923             (gnus-summary-goto-article
7924              (mail-header-number header) nil t)
7925           (when sparse
7926             (gnus-summary-update-article (mail-header-number header)))))
7927        (t
7928         ;; We fetch the article.
7929         (catch 'found
7930           (dolist (gnus-override-method (gnus-refer-article-methods))
7931             (gnus-check-server gnus-override-method)
7932             ;; Fetch the header, and display the article.
7933             (when (setq number (gnus-summary-insert-subject message-id))
7934               (gnus-summary-select-article nil nil nil number)
7935               (throw 'found t)))
7936           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7937
7938 (defun gnus-refer-article-methods ()
7939   "Return a list of referable methods."
7940   (cond
7941    ;; No method, so we default to current and native.
7942    ((null gnus-refer-article-method)
7943     (list gnus-current-select-method gnus-select-method))
7944    ;; Current.
7945    ((eq 'current gnus-refer-article-method)
7946     (list gnus-current-select-method))
7947    ;; List of select methods.
7948    ((not (and (symbolp (car gnus-refer-article-method))
7949               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7950     (let (out)
7951       (dolist (method gnus-refer-article-method)
7952         (push (if (eq 'current method)
7953                   gnus-current-select-method
7954                 method)
7955               out))
7956       (nreverse out)))
7957    ;; One single select method.
7958    (t
7959     (list gnus-refer-article-method))))
7960
7961 (defun gnus-summary-edit-parameters ()
7962   "Edit the group parameters of the current group."
7963   (interactive)
7964   (gnus-group-edit-group gnus-newsgroup-name 'params))
7965
7966 (defun gnus-summary-customize-parameters ()
7967   "Customize the group parameters of the current group."
7968   (interactive)
7969   (gnus-group-customize gnus-newsgroup-name))
7970
7971 (defun gnus-summary-enter-digest-group (&optional force)
7972   "Enter an nndoc group based on the current article.
7973 If FORCE, force a digest interpretation.  If not, try
7974 to guess what the document format is."
7975   (interactive "P")
7976   (let ((conf gnus-current-window-configuration))
7977     (save-excursion
7978       (gnus-summary-select-article))
7979     (setq gnus-current-window-configuration conf)
7980     (let* ((name (format "%s-%d"
7981                          (gnus-group-prefixed-name
7982                           gnus-newsgroup-name (list 'nndoc ""))
7983                          (save-excursion
7984                            (set-buffer gnus-summary-buffer)
7985                            gnus-current-article)))
7986            (ogroup gnus-newsgroup-name)
7987            (params (append (gnus-info-params (gnus-get-info ogroup))
7988                            (list (cons 'to-group ogroup))
7989                            (list (cons 'save-article-group ogroup))))
7990            (case-fold-search t)
7991            (buf (current-buffer))
7992            dig to-address)
7993       (save-excursion
7994         (set-buffer gnus-original-article-buffer)
7995         ;; Have the digest group inherit the main mail address of
7996         ;; the parent article.
7997         (when (setq to-address (or (gnus-fetch-field "reply-to")
7998                                    (gnus-fetch-field "from")))
7999           (setq params (append
8000                         (list (cons 'to-address
8001                                     (funcall gnus-decode-encoded-word-function
8002                                              to-address))))))
8003         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8004         (insert-buffer-substring gnus-original-article-buffer)
8005         ;; Remove lines that may lead nndoc to misinterpret the
8006         ;; document type.
8007         (narrow-to-region
8008          (goto-char (point-min))
8009          (or (search-forward "\n\n" nil t) (point)))
8010         (goto-char (point-min))
8011         (delete-matching-lines "^Path:\\|^From ")
8012         (widen))
8013       (unwind-protect
8014           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8015                     (gnus-newsgroup-ephemeral-ignored-charsets
8016                      gnus-newsgroup-ignored-charsets))
8017                 (gnus-group-read-ephemeral-group
8018                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8019                               (nndoc-article-type
8020                                ,(if force 'mbox 'guess)))
8021                  t nil nil nil
8022                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8023                                                         "ADAPT")))))
8024               ;; Make all postings to this group go to the parent group.
8025               (nconc (gnus-info-params (gnus-get-info name))
8026                      params)
8027             ;; Couldn't select this doc group.
8028             (switch-to-buffer buf)
8029             (gnus-set-global-variables)
8030             (gnus-configure-windows 'summary)
8031             (gnus-message 3 "Article couldn't be entered?"))
8032         (kill-buffer dig)))))
8033
8034 (defun gnus-summary-read-document (n)
8035   "Open a new group based on the current article(s).
8036 This will allow you to read digests and other similar
8037 documents as newsgroups.
8038 Obeys the standard process/prefix convention."
8039   (interactive "P")
8040   (let* ((articles (gnus-summary-work-articles n))
8041          (ogroup gnus-newsgroup-name)
8042          (params (append (gnus-info-params (gnus-get-info ogroup))
8043                          (list (cons 'to-group ogroup))))
8044          article group egroup groups vgroup)
8045     (while (setq article (pop articles))
8046       (setq group (format "%s-%d" gnus-newsgroup-name article))
8047       (gnus-summary-remove-process-mark article)
8048       (when (gnus-summary-display-article article)
8049         (save-excursion
8050           (with-temp-buffer
8051             (insert-buffer-substring gnus-original-article-buffer)
8052             ;; Remove some headers that may lead nndoc to make
8053             ;; the wrong guess.
8054             (message-narrow-to-head)
8055             (goto-char (point-min))
8056             (delete-matching-lines "^\\(Path\\):\\|^From ")
8057             (widen)
8058             (if (setq egroup
8059                       (gnus-group-read-ephemeral-group
8060                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8061                                      (nndoc-article-type guess))
8062                        t nil t))
8063                 (progn
8064             ;; Make all postings to this group go to the parent group.
8065                   (nconc (gnus-info-params (gnus-get-info egroup))
8066                          params)
8067                   (push egroup groups))
8068               ;; Couldn't select this doc group.
8069               (gnus-error 3 "Article couldn't be entered"))))))
8070     ;; Now we have selected all the documents.
8071     (cond
8072      ((not groups)
8073       (error "None of the articles could be interpreted as documents"))
8074      ((gnus-group-read-ephemeral-group
8075        (setq vgroup (format
8076                      "nnvirtual:%s-%s" gnus-newsgroup-name
8077                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8078        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8079        t
8080        (cons (current-buffer) 'summary)))
8081      (t
8082       (error "Couldn't select virtual nndoc group")))))
8083
8084 (defun gnus-summary-isearch-article (&optional regexp-p)
8085   "Do incremental search forward on the current article.
8086 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8087   (interactive "P")
8088   (gnus-summary-select-article)
8089   (gnus-configure-windows 'article)
8090   (gnus-eval-in-buffer-window gnus-article-buffer
8091     (save-restriction
8092       (widen)
8093       (isearch-forward regexp-p))))
8094
8095 (defun gnus-summary-search-article-forward (regexp &optional backward)
8096   "Search for an article containing REGEXP forward.
8097 If BACKWARD, search backward instead."
8098   (interactive
8099    (list (read-string
8100           (format "Search article %s (regexp%s): "
8101                   (if current-prefix-arg "backward" "forward")
8102                   (if gnus-last-search-regexp
8103                       (concat ", default " gnus-last-search-regexp)
8104                     "")))
8105          current-prefix-arg))
8106   (if (string-equal regexp "")
8107       (setq regexp (or gnus-last-search-regexp ""))
8108     (setq gnus-last-search-regexp regexp)
8109     (setq gnus-article-before-search gnus-current-article))
8110   ;; Intentionally set gnus-last-article.
8111   (setq gnus-last-article gnus-article-before-search)
8112   (let ((gnus-last-article gnus-last-article))
8113     (if (gnus-summary-search-article regexp backward)
8114         (gnus-summary-show-thread)
8115       (error "Search failed: \"%s\"" regexp))))
8116
8117 (defun gnus-summary-search-article-backward (regexp)
8118   "Search for an article containing REGEXP backward."
8119   (interactive
8120    (list (read-string
8121           (format "Search article backward (regexp%s): "
8122                   (if gnus-last-search-regexp
8123                       (concat ", default " gnus-last-search-regexp)
8124                     "")))))
8125   (gnus-summary-search-article-forward regexp 'backward))
8126
8127 (defun gnus-summary-search-article (regexp &optional backward)
8128   "Search for an article containing REGEXP.
8129 Optional argument BACKWARD means do search for backward.
8130 `gnus-select-article-hook' is not called during the search."
8131   ;; We have to require this here to make sure that the following
8132   ;; dynamic binding isn't shadowed by autoloading.
8133   (require 'gnus-async)
8134   (require 'gnus-art)
8135   (let ((gnus-select-article-hook nil)  ;Disable hook.
8136         (gnus-article-prepare-hook nil)
8137         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8138         (gnus-use-article-prefetch nil)
8139         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8140         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8141         (sum (current-buffer))
8142         (gnus-display-mime-function nil)
8143         (found nil)
8144         point)
8145     (gnus-save-hidden-threads
8146       (gnus-summary-select-article)
8147       (set-buffer gnus-article-buffer)
8148       (goto-char (window-point (get-buffer-window (current-buffer))))
8149       (when backward
8150         (forward-line -1))
8151       (while (not found)
8152         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8153         (if (if backward
8154                 (re-search-backward regexp nil t)
8155               (re-search-forward regexp nil t))
8156             ;; We found the regexp.
8157             (progn
8158               (setq found 'found)
8159               (beginning-of-line)
8160               (set-window-start
8161                (get-buffer-window (current-buffer))
8162                (point))
8163               (forward-line 1)
8164               (set-window-point
8165                (get-buffer-window (current-buffer))
8166                (point))
8167               (set-buffer sum)
8168               (setq point (point)))
8169           ;; We didn't find it, so we go to the next article.
8170           (set-buffer sum)
8171           (setq found 'not)
8172           (while (eq found 'not)
8173             (if (not (if backward (gnus-summary-find-prev)
8174                        (gnus-summary-find-next)))
8175                 ;; No more articles.
8176                 (setq found t)
8177               ;; Select the next article and adjust point.
8178               (unless (gnus-summary-article-sparse-p
8179                        (gnus-summary-article-number))
8180                 (setq found nil)
8181                 (gnus-summary-select-article)
8182                 (set-buffer gnus-article-buffer)
8183                 (widen)
8184                 (goto-char (if backward (point-max) (point-min))))))))
8185       (gnus-message 7 ""))
8186     ;; Return whether we found the regexp.
8187     (when (eq found 'found)
8188       (goto-char point)
8189       (gnus-summary-show-thread)
8190       (gnus-summary-goto-subject gnus-current-article)
8191       (gnus-summary-position-point)
8192       t)))
8193
8194 (defun gnus-find-matching-articles (header regexp)
8195   "Return a list of all articles that match REGEXP on HEADER.
8196 This search includes all articles in the current group that Gnus has
8197 fetched headers for, whether they are displayed or not."
8198   (let ((articles nil)
8199         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8200         (case-fold-search t))
8201     (dolist (header gnus-newsgroup-headers)
8202       (when (string-match regexp (funcall func header))
8203         (push (mail-header-number header) articles)))
8204     (nreverse articles)))
8205
8206 (defun gnus-summary-find-matching (header regexp &optional backward unread
8207                                           not-case-fold not-matching)
8208   "Return a list of all articles that match REGEXP on HEADER.
8209 The search stars on the current article and goes forwards unless
8210 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8211 If UNREAD is non-nil, only unread articles will
8212 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8213 in the comparisons. If NOT-MATCHING, return a list of all articles that
8214 not match REGEXP on HEADER."
8215   (let ((case-fold-search (not not-case-fold))
8216         articles d func)
8217     (if (consp header)
8218         (if (eq (car header) 'extra)
8219             (setq func
8220                   `(lambda (h)
8221                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8222                          "")))
8223           (error "%s is an invalid header" header))
8224       (unless (fboundp (intern (concat "mail-header-" header)))
8225         (error "%s is not a valid header" header))
8226       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8227     (dolist (d (if (eq backward 'all)
8228                    gnus-newsgroup-data
8229                  (gnus-data-find-list
8230                   (gnus-summary-article-number)
8231                   (gnus-data-list backward))))
8232       (when (and (or (not unread)       ; We want all articles...
8233                      (gnus-data-unread-p d)) ; Or just unreads.
8234                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8235                  (if not-matching
8236                      (not (string-match
8237                            regexp
8238                            (funcall func (gnus-data-header d))))
8239                    (string-match regexp
8240                                  (funcall func (gnus-data-header d)))))
8241         (push (gnus-data-number d) articles))) ; Success!
8242     (nreverse articles)))
8243
8244 (defun gnus-summary-execute-command (header regexp command &optional backward)
8245   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8246 If HEADER is an empty string (or nil), the match is done on the entire
8247 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8248   (interactive
8249    (list (let ((completion-ignore-case t))
8250            (completing-read
8251             "Header name: "
8252             (mapcar (lambda (header) (list (format "%s" header)))
8253                     (append
8254                      '("Number" "Subject" "From" "Lines" "Date"
8255                        "Message-ID" "Xref" "References" "Body")
8256                      gnus-extra-headers))
8257             nil 'require-match))
8258          (read-string "Regexp: ")
8259          (read-key-sequence "Command: ")
8260          current-prefix-arg))
8261   (when (equal header "Body")
8262     (setq header ""))
8263   ;; Hidden thread subtrees must be searched as well.
8264   (gnus-summary-show-all-threads)
8265   ;; We don't want to change current point nor window configuration.
8266   (save-excursion
8267     (save-window-excursion
8268       (let (gnus-visual
8269             gnus-treat-strip-trailing-blank-lines
8270             gnus-treat-strip-leading-blank-lines
8271             gnus-treat-strip-multiple-blank-lines
8272             gnus-treat-hide-boring-headers
8273             gnus-treat-fold-newsgroups
8274             gnus-article-prepare-hook)
8275         (gnus-message 6 "Executing %s..." (key-description command))
8276         ;; We'd like to execute COMMAND interactively so as to give arguments.
8277         (gnus-execute header regexp
8278                       `(call-interactively ',(key-binding command))
8279                       backward)
8280         (gnus-message 6 "Executing %s...done" (key-description command))))))
8281
8282 (defun gnus-summary-beginning-of-article ()
8283   "Scroll the article back to the beginning."
8284   (interactive)
8285   (gnus-summary-select-article)
8286   (gnus-configure-windows 'article)
8287   (gnus-eval-in-buffer-window gnus-article-buffer
8288     (widen)
8289     (goto-char (point-min))
8290     (when gnus-page-broken
8291       (gnus-narrow-to-page))))
8292
8293 (defun gnus-summary-end-of-article ()
8294   "Scroll to the end of the article."
8295   (interactive)
8296   (gnus-summary-select-article)
8297   (gnus-configure-windows 'article)
8298   (gnus-eval-in-buffer-window gnus-article-buffer
8299     (widen)
8300     (goto-char (point-max))
8301     (recenter -3)
8302     (when gnus-page-broken
8303       (gnus-narrow-to-page))))
8304
8305 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8306   "Truncate to LEN and quote all \"(\"'s in STRING."
8307   (gnus-replace-in-string (if (and len (> (length string) len))
8308                               (substring string 0 len)
8309                             string)
8310                           "[()]" "\\\\\\&"))
8311
8312 (defun gnus-summary-print-article (&optional filename n)
8313   "Generate and print a PostScript image of the N next (mail) articles.
8314
8315 If N is negative, print the N previous articles.  If N is nil and articles
8316 have been marked with the process mark, print these instead.
8317
8318 If the optional first argument FILENAME is nil, send the image to the
8319 printer.  If FILENAME is a string, save the PostScript image in a file with
8320 that name.  If FILENAME is a number, prompt the user for the name of the file
8321 to save in."
8322   (interactive (list (ps-print-preprint current-prefix-arg)))
8323   (dolist (article (gnus-summary-work-articles n))
8324     (gnus-summary-select-article nil nil 'pseudo article)
8325     (gnus-eval-in-buffer-window gnus-article-buffer
8326       (gnus-print-buffer))
8327     (gnus-summary-remove-process-mark article))
8328   (ps-despool filename))
8329
8330 (defun gnus-print-buffer ()
8331   (let ((buffer (generate-new-buffer " *print*")))
8332     (unwind-protect
8333         (progn
8334           (copy-to-buffer buffer (point-min) (point-max))
8335           (set-buffer buffer)
8336           (gnus-article-delete-invisible-text)
8337           (gnus-remove-text-with-property 'gnus-decoration)
8338           (when (gnus-visual-p 'article-highlight 'highlight)
8339             ;; Copy-to-buffer doesn't copy overlay.  So redo
8340             ;; highlight.
8341             (let ((gnus-article-buffer buffer))
8342               (gnus-article-highlight-citation t)
8343               (gnus-article-highlight-signature)))
8344           (let ((ps-left-header
8345                  (list
8346                   (concat "("
8347                           (gnus-summary-print-truncate-and-quote
8348                            (mail-header-subject gnus-current-headers)
8349                            66) ")")
8350                   (concat "("
8351                           (gnus-summary-print-truncate-and-quote
8352                            (mail-header-from gnus-current-headers)
8353                            45) ")")))
8354                 (ps-right-header
8355                  (list
8356                   "/pagenumberstring load"
8357                   (concat "("
8358                           (mail-header-date gnus-current-headers) ")"))))
8359             (gnus-run-hooks 'gnus-ps-print-hook)
8360             (save-excursion
8361               (if window-system
8362                   (ps-spool-buffer-with-faces)
8363                 (ps-spool-buffer)))))
8364       (kill-buffer buffer))))
8365
8366 (defun gnus-summary-show-article (&optional arg)
8367   "Force redisplaying of the current article.
8368 If ARG (the prefix) is a number, show the article with the charset
8369 defined in `gnus-summary-show-article-charset-alist', or the charset
8370 input.
8371 If ARG (the prefix) is non-nil and not a number, show the raw article
8372 without any article massaging functions being run.  Normally, the key strokes
8373 are `C-u g'."
8374   (interactive "P")
8375   (cond
8376    ((numberp arg)
8377     (gnus-summary-show-article t)
8378     (let ((gnus-newsgroup-charset
8379            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8380                (mm-read-coding-system
8381                 "View as charset: " ;; actually it is coding system.
8382                 (save-excursion
8383                   (set-buffer gnus-article-buffer)
8384                   (mm-detect-coding-region (point) (point-max))))))
8385           (gnus-newsgroup-ignored-charsets 'gnus-all))
8386       (gnus-summary-select-article nil 'force)
8387       (let ((deps gnus-newsgroup-dependencies)
8388             head header lines)
8389         (save-excursion
8390           (set-buffer gnus-original-article-buffer)
8391           (save-restriction
8392             (message-narrow-to-head)
8393             (setq head (buffer-string))
8394             (goto-char (point-min))
8395             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8396               (goto-char (point-max))
8397               (widen)
8398               (setq lines (1- (count-lines (point) (point-max))))))
8399           (with-temp-buffer
8400             (insert (format "211 %d Article retrieved.\n"
8401                             (cdr gnus-article-current)))
8402             (insert head)
8403             (if lines (insert (format "Lines: %d\n" lines)))
8404             (insert ".\n")
8405             (let ((nntp-server-buffer (current-buffer)))
8406               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8407         (gnus-data-set-header
8408          (gnus-data-find (cdr gnus-article-current))
8409          header)
8410         (gnus-summary-update-article-line
8411          (cdr gnus-article-current) header)
8412         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8413           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8414    ((not arg)
8415     ;; Select the article the normal way.
8416     (gnus-summary-select-article nil 'force))
8417    (t
8418     ;; We have to require this here to make sure that the following
8419     ;; dynamic binding isn't shadowed by autoloading.
8420     (require 'gnus-async)
8421     (require 'gnus-art)
8422     ;; Bind the article treatment functions to nil.
8423     (let ((gnus-have-all-headers t)
8424           gnus-article-prepare-hook
8425           gnus-article-decode-hook
8426           gnus-display-mime-function
8427           gnus-break-pages)
8428       ;; Destroy any MIME parts.
8429       (when (gnus-buffer-live-p gnus-article-buffer)
8430         (save-excursion
8431           (set-buffer gnus-article-buffer)
8432           (mm-destroy-parts gnus-article-mime-handles)
8433           ;; Set it to nil for safety reason.
8434           (setq gnus-article-mime-handle-alist nil)
8435           (setq gnus-article-mime-handles nil)))
8436       (gnus-summary-select-article nil 'force))))
8437   (gnus-summary-goto-subject gnus-current-article)
8438   (gnus-summary-position-point))
8439
8440 (defun gnus-summary-show-raw-article ()
8441   "Show the raw article without any article massaging functions being run."
8442   (interactive)
8443   (gnus-summary-show-article t))
8444
8445 (defun gnus-summary-verbose-headers (&optional arg)
8446   "Toggle permanent full header display.
8447 If ARG is a positive number, turn header display on.
8448 If ARG is a negative number, turn header display off."
8449   (interactive "P")
8450   (setq gnus-show-all-headers
8451         (cond ((or (not (numberp arg))
8452                    (zerop arg))
8453                (not gnus-show-all-headers))
8454               ((natnump arg)
8455                t)))
8456   (gnus-summary-show-article))
8457
8458 (defun gnus-summary-toggle-header (&optional arg)
8459   "Show the headers if they are hidden, or hide them if they are shown.
8460 If ARG is a positive number, show the entire header.
8461 If ARG is a negative number, hide the unwanted header lines."
8462   (interactive "P")
8463   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8464                      (get-buffer-window gnus-article-buffer t))))
8465     (with-current-buffer gnus-article-buffer
8466       (widen)
8467       (article-narrow-to-head)
8468       (let* ((buffer-read-only nil)
8469              (inhibit-point-motion-hooks t)
8470              (hidden (if (numberp arg)
8471                          (>= arg 0)
8472                        (gnus-article-hidden-text-p 'headers)))
8473              s e)
8474         (delete-region (point-min) (point-max))
8475         (with-current-buffer gnus-original-article-buffer
8476           (goto-char (setq s (point-min)))
8477           (setq e (if (search-forward "\n\n" nil t)
8478                       (1- (point))
8479                     (point-max))))
8480         (insert-buffer-substring gnus-original-article-buffer s e)
8481         (article-decode-encoded-words)
8482         (if hidden
8483             (let ((gnus-treat-hide-headers nil)
8484                   (gnus-treat-hide-boring-headers nil))
8485               (gnus-delete-wash-type 'headers)
8486               (gnus-treat-article 'head))
8487           (gnus-treat-article 'head))
8488         (widen)
8489         (if window
8490             (set-window-start window (goto-char (point-min))))
8491         (setq gnus-page-broken
8492               (when gnus-break-pages
8493                 (gnus-narrow-to-page)
8494                 t))
8495         (gnus-set-mode-line 'article)))))
8496
8497 (defun gnus-summary-show-all-headers ()
8498   "Make all header lines visible."
8499   (interactive)
8500   (gnus-summary-toggle-header 1))
8501
8502 (defun gnus-summary-caesar-message (&optional arg)
8503   "Caesar rotate the current article by 13.
8504 The numerical prefix specifies how many places to rotate each letter
8505 forward."
8506   (interactive "P")
8507   (gnus-summary-select-article)
8508   (let ((mail-header-separator ""))
8509     (gnus-eval-in-buffer-window gnus-article-buffer
8510       (save-restriction
8511         (widen)
8512         (let ((start (window-start))
8513               buffer-read-only)
8514           (message-caesar-buffer-body arg)
8515           (set-window-start (get-buffer-window (current-buffer)) start))))))
8516
8517 (autoload 'unmorse-region "morse"
8518   "Convert morse coded text in region to ordinary ASCII text."
8519   t)
8520
8521 (defun gnus-summary-morse-message (&optional arg)
8522   "Morse decode the current article."
8523   (interactive "P")
8524   (gnus-summary-select-article)
8525   (let ((mail-header-separator ""))
8526     (gnus-eval-in-buffer-window gnus-article-buffer
8527       (save-excursion
8528         (save-restriction
8529           (widen)
8530           (let ((pos (window-start))
8531                 buffer-read-only)
8532             (goto-char (point-min))
8533             (when (message-goto-body)
8534               (gnus-narrow-to-body))
8535             (goto-char (point-min))
8536             (while (re-search-forward "·" (point-max) t)
8537               (replace-match "."))
8538             (unmorse-region (point-min) (point-max))
8539             (widen)
8540             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8541
8542 (defun gnus-summary-stop-page-breaking ()
8543   "Stop page breaking in the current article."
8544   (interactive)
8545   (gnus-summary-select-article)
8546   (gnus-eval-in-buffer-window gnus-article-buffer
8547     (widen)
8548     (when (gnus-visual-p 'page-marker)
8549       (let ((buffer-read-only nil))
8550         (gnus-remove-text-with-property 'gnus-prev)
8551         (gnus-remove-text-with-property 'gnus-next))
8552       (setq gnus-page-broken nil))))
8553
8554 (defun gnus-summary-move-article (&optional n to-newsgroup
8555                                             select-method action)
8556   "Move the current article to a different newsgroup.
8557 If N is a positive number, move the N next articles.
8558 If N is a negative number, move the N previous articles.
8559 If N is nil and any articles have been marked with the process mark,
8560 move those articles instead.
8561 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8562 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8563 re-spool using this method.
8564
8565 When called interactively with TO-NEWSGROUP being nil, the value of
8566 the variable `gnus-move-split-methods' is used for finding a default
8567 for the target newsgroup.
8568
8569 For this function to work, both the current newsgroup and the
8570 newsgroup that you want to move to have to support the `request-move'
8571 and `request-accept' functions.
8572
8573 ACTION can be either `move' (the default), `crosspost' or `copy'."
8574   (interactive "P")
8575   (unless action
8576     (setq action 'move))
8577   ;; Check whether the source group supports the required functions.
8578   (cond ((and (eq action 'move)
8579               (not (gnus-check-backend-function
8580                     'request-move-article gnus-newsgroup-name)))
8581          (error "The current group does not support article moving"))
8582         ((and (eq action 'crosspost)
8583               (not (gnus-check-backend-function
8584                     'request-replace-article gnus-newsgroup-name)))
8585          (error "The current group does not support article editing")))
8586   (let ((articles (gnus-summary-work-articles n))
8587         (prefix (if (gnus-check-backend-function
8588                      'request-move-article gnus-newsgroup-name)
8589                     (gnus-group-real-prefix gnus-newsgroup-name)
8590                   ""))
8591         (names '((move "Move" "Moving")
8592                  (copy "Copy" "Copying")
8593                  (crosspost "Crosspost" "Crossposting")))
8594         (copy-buf (save-excursion
8595                     (nnheader-set-temp-buffer " *copy article*")))
8596         art-group to-method new-xref article to-groups)
8597     (unless (assq action names)
8598       (error "Unknown action %s" action))
8599     ;; Read the newsgroup name.
8600     (when (and (not to-newsgroup)
8601                (not select-method))
8602       (if (and gnus-move-split-methods
8603                (not
8604                 (and (memq gnus-current-article articles)
8605                      (gnus-buffer-live-p gnus-original-article-buffer))))
8606           ;; When `gnus-move-split-methods' is non-nil, we have to
8607           ;; select an article to give `gnus-read-move-group-name' an
8608           ;; opportunity to suggest an appropriate default.  However,
8609           ;; we needn't render or mark the article.
8610           (let ((gnus-display-mime-function nil)
8611                 (gnus-article-prepare-hook nil)
8612                 (gnus-mark-article-hook nil))
8613             (gnus-summary-select-article nil nil nil (car articles))))
8614       (setq to-newsgroup
8615             (gnus-read-move-group-name
8616              (cadr (assq action names))
8617              (symbol-value (intern (format "gnus-current-%s-group" action)))
8618              articles prefix))
8619       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8620     (setq to-method (or select-method
8621                         (gnus-server-to-method
8622                          (gnus-group-method to-newsgroup))))
8623     ;; Check the method we are to move this article to...
8624     (unless (gnus-check-backend-function
8625              'request-accept-article (car to-method))
8626       (error "%s does not support article copying" (car to-method)))
8627     (unless (gnus-check-server to-method)
8628       (error "Can't open server %s" (car to-method)))
8629     (gnus-message 6 "%s to %s: %s..."
8630                   (caddr (assq action names))
8631                   (or (car select-method) to-newsgroup) articles)
8632     (while articles
8633       (setq article (pop articles))
8634       (setq
8635        art-group
8636        (cond
8637         ;; Move the article.
8638         ((eq action 'move)
8639          ;; Remove this article from future suppression.
8640          (gnus-dup-unsuppress-article article)
8641          (gnus-request-move-article
8642           article                       ; Article to move
8643           gnus-newsgroup-name           ; From newsgroup
8644           (nth 1 (gnus-find-method-for-group
8645                   gnus-newsgroup-name)) ; Server
8646           (list 'gnus-request-accept-article
8647                 to-newsgroup (list 'quote select-method)
8648                 (not articles) t)       ; Accept form
8649           (not articles)))              ; Only save nov last time
8650         ;; Copy the article.
8651         ((eq action 'copy)
8652          (save-excursion
8653            (set-buffer copy-buf)
8654            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8655              (gnus-request-accept-article
8656               to-newsgroup select-method (not articles) t))))
8657         ;; Crosspost the article.
8658         ((eq action 'crosspost)
8659          (let ((xref (message-tokenize-header
8660                       (mail-header-xref (gnus-summary-article-header article))
8661                       " ")))
8662            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8663                                   ":" (number-to-string article)))
8664            (unless xref
8665              (setq xref (list (system-name))))
8666            (setq new-xref
8667                  (concat
8668                   (mapconcat 'identity
8669                              (delete "Xref:" (delete new-xref xref))
8670                              " ")
8671                   " " new-xref))
8672            (save-excursion
8673              (set-buffer copy-buf)
8674              ;; First put the article in the destination group.
8675              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8676              (when (consp (setq art-group
8677                                 (gnus-request-accept-article
8678                                  to-newsgroup select-method (not articles))))
8679                (setq new-xref (concat new-xref " " (car art-group)
8680                                       ":"
8681                                       (number-to-string (cdr art-group))))
8682                ;; Now we have the new Xrefs header, so we insert
8683                ;; it and replace the new article.
8684                (nnheader-replace-header "Xref" new-xref)
8685                (gnus-request-replace-article
8686                 (cdr art-group) to-newsgroup (current-buffer))
8687                art-group))))))
8688       (cond
8689        ((not art-group)
8690         (gnus-message 1 "Couldn't %s article %s: %s"
8691                       (cadr (assq action names)) article
8692                       (nnheader-get-report (car to-method))))
8693        ((eq art-group 'junk)
8694         (when (eq action 'move)
8695           (gnus-summary-mark-article article gnus-canceled-mark)
8696           (gnus-message 4 "Deleted article %s" article)))
8697        (t
8698         (let* ((pto-group (gnus-group-prefixed-name
8699                            (car art-group) to-method))
8700                (entry
8701                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8702                (info (nth 2 entry))
8703                (to-group (gnus-info-group info))
8704                to-marks)
8705           ;; Update the group that has been moved to.
8706           (when (and info
8707                      (memq action '(move copy)))
8708             (unless (member to-group to-groups)
8709               (push to-group to-groups))
8710
8711             (unless (memq article gnus-newsgroup-unreads)
8712               (push 'read to-marks)
8713               (gnus-info-set-read
8714                info (gnus-add-to-range (gnus-info-read info)
8715                                        (list (cdr art-group)))))
8716
8717             ;; See whether the article is to be put in the cache.
8718             (let ((marks gnus-article-mark-lists)
8719                   (to-article (cdr art-group)))
8720
8721               ;; Enter the article into the cache in the new group,
8722               ;; if that is required.
8723               (when gnus-use-cache
8724                 (gnus-cache-possibly-enter-article
8725                  to-group to-article
8726                  (memq article gnus-newsgroup-marked)
8727                  (memq article gnus-newsgroup-dormant)
8728                  (memq article gnus-newsgroup-unreads)))
8729
8730               (when gnus-preserve-marks
8731                 ;; Copy any marks over to the new group.
8732                 (when (and (equal to-group gnus-newsgroup-name)
8733                            (not (memq article gnus-newsgroup-unreads)))
8734                   ;; Mark this article as read in this group.
8735                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8736                   (setcdr (gnus-active to-group) to-article)
8737                   (setcdr gnus-newsgroup-active to-article))
8738
8739                 (while marks
8740                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8741                     (when (memq article (symbol-value
8742                                          (intern (format "gnus-newsgroup-%s"
8743                                                          (caar marks)))))
8744                       (push (cdar marks) to-marks)
8745                       ;; If the other group is the same as this group,
8746                       ;; then we have to add the mark to the list.
8747                       (when (equal to-group gnus-newsgroup-name)
8748                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8749                              (cons to-article
8750                                    (symbol-value
8751                                     (intern (format "gnus-newsgroup-%s"
8752                                                     (caar marks)))))))
8753                       ;; Copy the marks to other group.
8754                       (gnus-add-marked-articles
8755                        to-group (cdar marks) (list to-article) info)))
8756                   (setq marks (cdr marks)))
8757
8758                 (gnus-request-set-mark
8759                  to-group (list (list (list to-article) 'add to-marks))))
8760
8761               (gnus-dribble-enter
8762                (concat "(gnus-group-set-info '"
8763                        (gnus-prin1-to-string (gnus-get-info to-group))
8764                        ")"))))
8765
8766           ;; Update the Xref header in this article to point to
8767           ;; the new crossposted article we have just created.
8768           (when (eq action 'crosspost)
8769             (save-excursion
8770               (set-buffer copy-buf)
8771               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8772               (nnheader-replace-header "Xref" new-xref)
8773               (gnus-request-replace-article
8774                article gnus-newsgroup-name (current-buffer)))))
8775
8776         ;;;!!!Why is this necessary?
8777         (set-buffer gnus-summary-buffer)
8778
8779         (gnus-summary-goto-subject article)
8780         (when (eq action 'move)
8781           (gnus-summary-mark-article article gnus-canceled-mark))))
8782       (gnus-summary-remove-process-mark article))
8783     ;; Re-activate all groups that have been moved to.
8784     (save-excursion
8785       (set-buffer gnus-group-buffer)
8786       (let ((gnus-group-marked to-groups))
8787         (gnus-group-get-new-news-this-group nil t)))
8788
8789     (gnus-kill-buffer copy-buf)
8790     (gnus-summary-position-point)
8791     (gnus-set-mode-line 'summary)))
8792
8793 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8794   "Move the current article to a different newsgroup.
8795 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8796 When called interactively, if TO-NEWSGROUP is nil, use the value of
8797 the variable `gnus-move-split-methods' for finding a default target
8798 newsgroup.
8799 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8800 re-spool using this method."
8801   (interactive "P")
8802   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8803
8804 (defun gnus-summary-crosspost-article (&optional n)
8805   "Crosspost the current article to some other group."
8806   (interactive "P")
8807   (gnus-summary-move-article n nil nil 'crosspost))
8808
8809 (defcustom gnus-summary-respool-default-method nil
8810   "Default method type for respooling an article.
8811 If nil, use to the current newsgroup method."
8812   :type 'symbol
8813   :group 'gnus-summary-mail)
8814
8815 (defcustom gnus-summary-display-while-building nil
8816   "If not-nil, show and update the summary buffer as it's being built.
8817 If the value is t, update the buffer after every line is inserted.  If
8818 the value is an integer (N), update the display every N lines."
8819   :group 'gnus-thread
8820   :type '(choice (const :tag "off" nil)
8821                  number
8822                  (const :tag "frequently" t)))
8823
8824 (defun gnus-summary-respool-article (&optional n method)
8825   "Respool the current article.
8826 The article will be squeezed through the mail spooling process again,
8827 which means that it will be put in some mail newsgroup or other
8828 depending on `nnmail-split-methods'.
8829 If N is a positive number, respool the N next articles.
8830 If N is a negative number, respool the N previous articles.
8831 If N is nil and any articles have been marked with the process mark,
8832 respool those articles instead.
8833
8834 Respooling can be done both from mail groups and \"real\" newsgroups.
8835 In the former case, the articles in question will be moved from the
8836 current group into whatever groups they are destined to.  In the
8837 latter case, they will be copied into the relevant groups."
8838   (interactive
8839    (list current-prefix-arg
8840          (let* ((methods (gnus-methods-using 'respool))
8841                 (methname
8842                  (symbol-name (or gnus-summary-respool-default-method
8843                                   (car (gnus-find-method-for-group
8844                                         gnus-newsgroup-name)))))
8845                 (method
8846                  (gnus-completing-read-with-default
8847                   methname "What backend do you want to use when respooling?"
8848                   methods nil t nil 'gnus-mail-method-history))
8849                 ms)
8850            (cond
8851             ((zerop (length (setq ms (gnus-servers-using-backend
8852                                       (intern method)))))
8853              (list (intern method) ""))
8854             ((= 1 (length ms))
8855              (car ms))
8856             (t
8857              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8858                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8859                            ms-alist))))))))
8860   (unless method
8861     (error "No method given for respooling"))
8862   (if (assoc (symbol-name
8863               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8864              (gnus-methods-using 'respool))
8865       (gnus-summary-move-article n nil method)
8866     (gnus-summary-copy-article n nil method)))
8867
8868 (defun gnus-summary-import-article (file &optional edit)
8869   "Import an arbitrary file into a mail newsgroup."
8870   (interactive "fImport file: \nP")
8871   (let ((group gnus-newsgroup-name)
8872         (now (current-time))
8873         atts lines group-art)
8874     (unless (gnus-check-backend-function 'request-accept-article group)
8875       (error "%s does not support article importing" group))
8876     (or (file-readable-p file)
8877         (not (file-regular-p file))
8878         (error "Can't read %s" file))
8879     (save-excursion
8880       (set-buffer (gnus-get-buffer-create " *import file*"))
8881       (erase-buffer)
8882       (nnheader-insert-file-contents file)
8883       (goto-char (point-min))
8884       (if (nnheader-article-p)
8885           (save-restriction
8886             (goto-char (point-min))
8887             (search-forward "\n\n" nil t)
8888             (narrow-to-region (point-min) (1- (point)))
8889             (goto-char (point-min))
8890             (unless (re-search-forward "^date:" nil t)
8891               (goto-char (point-max))
8892               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8893        ;; This doesn't look like an article, so we fudge some headers.
8894         (setq atts (file-attributes file)
8895               lines (count-lines (point-min) (point-max)))
8896         (insert "From: " (read-string "From: ") "\n"
8897                 "Subject: " (read-string "Subject: ") "\n"
8898                 "Date: " (message-make-date (nth 5 atts)) "\n"
8899                 "Message-ID: " (message-make-message-id) "\n"
8900                 "Lines: " (int-to-string lines) "\n"
8901                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8902       (setq group-art (gnus-request-accept-article group nil t))
8903       (kill-buffer (current-buffer)))
8904     (setq gnus-newsgroup-active (gnus-activate-group group))
8905     (forward-line 1)
8906     (gnus-summary-goto-article (cdr group-art) nil t)
8907     (when edit
8908       (gnus-summary-edit-article))))
8909
8910 (defun gnus-summary-create-article ()
8911   "Create an article in a mail newsgroup."
8912   (interactive)
8913   (let ((group gnus-newsgroup-name)
8914         (now (current-time))
8915         group-art)
8916     (unless (gnus-check-backend-function 'request-accept-article group)
8917       (error "%s does not support article importing" group))
8918     (save-excursion
8919       (set-buffer (gnus-get-buffer-create " *import file*"))
8920       (erase-buffer)
8921       (goto-char (point-min))
8922       ;; This doesn't look like an article, so we fudge some headers.
8923       (insert "From: " (read-string "From: ") "\n"
8924               "Subject: " (read-string "Subject: ") "\n"
8925               "Date: " (message-make-date now) "\n"
8926               "Message-ID: " (message-make-message-id) "\n")
8927       (setq group-art (gnus-request-accept-article group nil t))
8928       (kill-buffer (current-buffer)))
8929     (setq gnus-newsgroup-active (gnus-activate-group group))
8930     (forward-line 1)
8931     (gnus-summary-goto-article (cdr group-art) nil t)
8932     (gnus-summary-edit-article)))
8933
8934 (defun gnus-summary-article-posted-p ()
8935   "Say whether the current (mail) article is available from news as well.
8936 This will be the case if the article has both been mailed and posted."
8937   (interactive)
8938   (let ((id (mail-header-references (gnus-summary-article-header)))
8939         (gnus-override-method (car (gnus-refer-article-methods))))
8940     (if (gnus-request-head id "")
8941         (gnus-message 2 "The current message was found on %s"
8942                       gnus-override-method)
8943       (gnus-message 2 "The current message couldn't be found on %s"
8944                     gnus-override-method)
8945       nil)))
8946
8947 (defun gnus-summary-expire-articles (&optional now)
8948   "Expire all articles that are marked as expirable in the current group."
8949   (interactive)
8950   (when (gnus-check-backend-function
8951          'request-expire-articles gnus-newsgroup-name)
8952     ;; This backend supports expiry.
8953     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8954            (expirable (if total
8955                           (progn
8956                             ;; We need to update the info for
8957                             ;; this group for `gnus-list-of-read-articles'
8958                             ;; to give us the right answer.
8959                             (gnus-run-hooks 'gnus-exit-group-hook)
8960                             (gnus-summary-update-info)
8961                             (gnus-list-of-read-articles gnus-newsgroup-name))
8962                         (setq gnus-newsgroup-expirable
8963                               (sort gnus-newsgroup-expirable '<))))
8964            (expiry-wait (if now 'immediate
8965                           (gnus-group-find-parameter
8966                            gnus-newsgroup-name 'expiry-wait)))
8967            (nnmail-expiry-target
8968             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8969                 nnmail-expiry-target))
8970            es)
8971       (when expirable
8972         ;; There are expirable articles in this group, so we run them
8973         ;; through the expiry process.
8974         (gnus-message 6 "Expiring articles...")
8975         (unless (gnus-check-group gnus-newsgroup-name)
8976           (error "Can't open server for %s" gnus-newsgroup-name))
8977         ;; The list of articles that weren't expired is returned.
8978         (save-excursion
8979           (if expiry-wait
8980               (let ((nnmail-expiry-wait-function nil)
8981                     (nnmail-expiry-wait expiry-wait))
8982                 (setq es (gnus-request-expire-articles
8983                           expirable gnus-newsgroup-name)))
8984             (setq es (gnus-request-expire-articles
8985                       expirable gnus-newsgroup-name)))
8986           (unless total
8987             (setq gnus-newsgroup-expirable es))
8988           ;; We go through the old list of expirable, and mark all
8989           ;; really expired articles as nonexistent.
8990           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8991             (let ((gnus-use-cache nil))
8992               (dolist (article expirable)
8993                 (when (and (not (memq article es))
8994                            (gnus-data-find article))
8995                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8996         (gnus-message 6 "Expiring articles...done")))))
8997
8998 (defun gnus-summary-expire-articles-now ()
8999   "Expunge all expirable articles in the current group.
9000 This means that *all* articles that are marked as expirable will be
9001 deleted forever, right now."
9002   (interactive)
9003   (or gnus-expert-user
9004       (gnus-yes-or-no-p
9005        "Are you really, really, really sure you want to delete all these messages? ")
9006       (error "Phew!"))
9007   (gnus-summary-expire-articles t))
9008
9009 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9010 (defun gnus-summary-delete-article (&optional n)
9011   "Delete the N next (mail) articles.
9012 This command actually deletes articles.  This is not a marking
9013 command.  The article will disappear forever from your life, never to
9014 return.
9015 If N is negative, delete backwards.
9016 If N is nil and articles have been marked with the process mark,
9017 delete these instead."
9018   (interactive "P")
9019   (unless (gnus-check-backend-function 'request-expire-articles
9020                                        gnus-newsgroup-name)
9021     (error "The current newsgroup does not support article deletion"))
9022   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9023     (error "Couldn't open server"))
9024   ;; Compute the list of articles to delete.
9025   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9026         (nnmail-expiry-target 'delete)
9027         not-deleted)
9028     (if (and gnus-novice-user
9029              (not (gnus-yes-or-no-p
9030                    (format "Do you really want to delete %s forever? "
9031                            (if (> (length articles) 1)
9032                                (format "these %s articles" (length articles))
9033                              "this article")))))
9034         ()
9035       ;; Delete the articles.
9036       (setq not-deleted (gnus-request-expire-articles
9037                          articles gnus-newsgroup-name 'force))
9038       (while articles
9039         (gnus-summary-remove-process-mark (car articles))
9040         ;; The backend might not have been able to delete the article
9041         ;; after all.
9042         (unless (memq (car articles) not-deleted)
9043           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9044         (setq articles (cdr articles)))
9045       (when not-deleted
9046         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9047     (gnus-summary-position-point)
9048     (gnus-set-mode-line 'summary)
9049     not-deleted))
9050
9051 (defun gnus-summary-edit-article (&optional arg)
9052   "Edit the current article.
9053 This will have permanent effect only in mail groups.
9054 If ARG is nil, edit the decoded articles.
9055 If ARG is 1, edit the raw articles.
9056 If ARG is 2, edit the raw articles even in read-only groups.
9057 If ARG is 3, edit the articles with the current handles.
9058 Otherwise, allow editing of articles even in read-only
9059 groups."
9060   (interactive "P")
9061   (let (force raw current-handles)
9062     (cond
9063      ((null arg))
9064      ((eq arg 1)
9065       (setq raw t))
9066      ((eq arg 2)
9067       (setq raw t
9068             force t))
9069      ((eq arg 3)
9070       (setq current-handles
9071             (and (gnus-buffer-live-p gnus-article-buffer)
9072                  (with-current-buffer gnus-article-buffer
9073                    (prog1
9074                        gnus-article-mime-handles
9075                      (setq gnus-article-mime-handles nil))))))
9076      (t
9077       (setq force t)))
9078     (when (and raw (not force)
9079                (member gnus-newsgroup-name '("nndraft:delayed"
9080                                              "nndraft:drafts"
9081                                              "nndraft:queue")))
9082       (error "Can't edit the raw article in group %s"
9083              gnus-newsgroup-name))
9084     (save-excursion
9085       (set-buffer gnus-summary-buffer)
9086       (let ((mail-parse-charset gnus-newsgroup-charset)
9087             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9088         (gnus-set-global-variables)
9089         (when (and (not force)
9090                    (gnus-group-read-only-p))
9091           (error "The current newsgroup does not support article editing"))
9092         (gnus-summary-show-article t)
9093         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9094           (with-current-buffer gnus-article-buffer
9095             (mm-enable-multibyte)))
9096         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9097             (setq raw t))
9098         (gnus-article-edit-article
9099          (if raw 'ignore
9100            `(lambda ()
9101               (let ((mbl mml-buffer-list))
9102                 (setq mml-buffer-list nil)
9103                 (mime-to-mml ,'current-handles)
9104                 (let ((mbl1 mml-buffer-list))
9105                   (setq mml-buffer-list mbl)
9106                   (set (make-local-variable 'mml-buffer-list) mbl1))
9107                 (make-local-hook 'kill-buffer-hook)
9108                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9109          `(lambda (no-highlight)
9110             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9111                   (message-options message-options)
9112                   (message-options-set-recipient)
9113                   (mail-parse-ignored-charsets
9114                    ',gnus-newsgroup-ignored-charsets))
9115               ,(if (not raw) '(progn
9116                                 (mml-to-mime)
9117                                 (mml-destroy-buffers)
9118                                 (remove-hook 'kill-buffer-hook
9119                                              'mml-destroy-buffers t)
9120                                 (kill-local-variable 'mml-buffer-list)))
9121               (gnus-summary-edit-article-done
9122                ,(or (mail-header-references gnus-current-headers) "")
9123                ,(gnus-group-read-only-p)
9124                ,gnus-summary-buffer no-highlight))))))))
9125
9126 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9127
9128 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9129                                                  no-highlight)
9130   "Make edits to the current article permanent."
9131   (interactive)
9132   (save-excursion
9133    ;; The buffer restriction contains the entire article if it exists.
9134     (when (article-goto-body)
9135       (let ((lines (count-lines (point) (point-max)))
9136             (length (- (point-max) (point)))
9137             (case-fold-search t)
9138             (body (copy-marker (point))))
9139         (goto-char (point-min))
9140         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9141           (delete-region (match-beginning 1) (match-end 1))
9142           (insert (number-to-string length)))
9143         (goto-char (point-min))
9144         (when (re-search-forward
9145                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9146           (delete-region (match-beginning 1) (match-end 1))
9147           (insert (number-to-string length)))
9148         (goto-char (point-min))
9149         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9150           (delete-region (match-beginning 1) (match-end 1))
9151           (insert (number-to-string lines))))))
9152   ;; Replace the article.
9153   (let ((buf (current-buffer)))
9154     (with-temp-buffer
9155       (insert-buffer-substring buf)
9156
9157       (if (and (not read-only)
9158                (not (gnus-request-replace-article
9159                      (cdr gnus-article-current) (car gnus-article-current)
9160                      (current-buffer) t)))
9161           (error "Couldn't replace article")
9162         ;; Update the summary buffer.
9163         (if (and references
9164                  (equal (message-tokenize-header references " ")
9165                         (message-tokenize-header
9166                          (or (message-fetch-field "references") "") " ")))
9167             ;; We only have to update this line.
9168             (save-excursion
9169               (save-restriction
9170                 (message-narrow-to-head)
9171                 (let ((head (buffer-string))
9172                       header)
9173                   (with-temp-buffer
9174                     (insert (format "211 %d Article retrieved.\n"
9175                                     (cdr gnus-article-current)))
9176                     (insert head)
9177                     (insert ".\n")
9178                     (let ((nntp-server-buffer (current-buffer)))
9179                       (setq header (car (gnus-get-newsgroup-headers
9180                                          nil t))))
9181                     (save-excursion
9182                       (set-buffer gnus-summary-buffer)
9183                       (gnus-data-set-header
9184                        (gnus-data-find (cdr gnus-article-current))
9185                        header)
9186                       (gnus-summary-update-article-line
9187                        (cdr gnus-article-current) header)
9188                       (if (gnus-summary-goto-subject
9189                            (cdr gnus-article-current) nil t)
9190                           (gnus-summary-update-secondary-mark
9191                            (cdr gnus-article-current))))))))
9192           ;; Update threads.
9193           (set-buffer (or buffer gnus-summary-buffer))
9194           (gnus-summary-update-article (cdr gnus-article-current))
9195           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9196               (gnus-summary-update-secondary-mark
9197                (cdr gnus-article-current))))
9198         ;; Prettify the article buffer again.
9199         (unless no-highlight
9200           (save-excursion
9201             (set-buffer gnus-article-buffer)
9202             ;;;!!! Fix this -- article should be rehighlighted.
9203             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9204             (set-buffer gnus-original-article-buffer)
9205             (gnus-request-article
9206              (cdr gnus-article-current)
9207              (car gnus-article-current) (current-buffer))))
9208         ;; Prettify the summary buffer line.
9209         (when (gnus-visual-p 'summary-highlight 'highlight)
9210           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9211
9212 (defun gnus-summary-edit-wash (key)
9213   "Perform editing command KEY in the article buffer."
9214   (interactive
9215    (list
9216     (progn
9217       (message "%s" (concat (this-command-keys) "- "))
9218       (read-char))))
9219   (message "")
9220   (gnus-summary-edit-article)
9221   (execute-kbd-macro (concat (this-command-keys) key))
9222   (gnus-article-edit-done))
9223
9224 ;;; Respooling
9225
9226 (defun gnus-summary-respool-query (&optional silent trace)
9227   "Query where the respool algorithm would put this article."
9228   (interactive)
9229   (let (gnus-mark-article-hook)
9230     (gnus-summary-select-article)
9231     (save-excursion
9232       (set-buffer gnus-original-article-buffer)
9233       (save-restriction
9234         (message-narrow-to-head)
9235         (let ((groups (nnmail-article-group 'identity trace)))
9236           (unless silent
9237             (if groups
9238                 (message "This message would go to %s"
9239                          (mapconcat 'car groups ", "))
9240               (message "This message would go to no groups"))
9241             groups))))))
9242
9243 (defun gnus-summary-respool-trace ()
9244   "Trace where the respool algorithm would put this article.
9245 Display a buffer showing all fancy splitting patterns which matched."
9246   (interactive)
9247   (gnus-summary-respool-query nil t))
9248
9249 ;; Summary marking commands.
9250
9251 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9252   "Mark articles which has the same subject as read, and then select the next.
9253 If UNMARK is positive, remove any kind of mark.
9254 If UNMARK is negative, tick articles."
9255   (interactive "P")
9256   (when unmark
9257     (setq unmark (prefix-numeric-value unmark)))
9258   (let ((count
9259          (gnus-summary-mark-same-subject
9260           (gnus-summary-article-subject) unmark)))
9261     ;; Select next unread article.  If auto-select-same mode, should
9262     ;; select the first unread article.
9263     (gnus-summary-next-article t (and gnus-auto-select-same
9264                                       (gnus-summary-article-subject)))
9265     (gnus-message 7 "%d article%s marked as %s"
9266                   count (if (= count 1) " is" "s are")
9267                   (if unmark "unread" "read"))))
9268
9269 (defun gnus-summary-kill-same-subject (&optional unmark)
9270   "Mark articles which has the same subject as read.
9271 If UNMARK is positive, remove any kind of mark.
9272 If UNMARK is negative, tick articles."
9273   (interactive "P")
9274   (when unmark
9275     (setq unmark (prefix-numeric-value unmark)))
9276   (let ((count
9277          (gnus-summary-mark-same-subject
9278           (gnus-summary-article-subject) unmark)))
9279     ;; If marked as read, go to next unread subject.
9280     (when (null unmark)
9281       ;; Go to next unread subject.
9282       (gnus-summary-next-subject 1 t))
9283     (gnus-message 7 "%d articles are marked as %s"
9284                   count (if unmark "unread" "read"))))
9285
9286 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9287   "Mark articles with same SUBJECT as read, and return marked number.
9288 If optional argument UNMARK is positive, remove any kinds of marks.
9289 If optional argument UNMARK is negative, mark articles as unread instead."
9290   (let ((count 1))
9291     (save-excursion
9292       (cond
9293        ((null unmark)                   ; Mark as read.
9294         (while (and
9295                 (progn
9296                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9297                   (gnus-summary-show-thread) t)
9298                 (gnus-summary-find-subject subject))
9299           (setq count (1+ count))))
9300        ((> unmark 0)                    ; Tick.
9301         (while (and
9302                 (progn
9303                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9304                   (gnus-summary-show-thread) t)
9305                 (gnus-summary-find-subject subject))
9306           (setq count (1+ count))))
9307        (t                               ; Mark as unread.
9308         (while (and
9309                 (progn
9310                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9311                   (gnus-summary-show-thread) t)
9312                 (gnus-summary-find-subject subject))
9313           (setq count (1+ count)))))
9314       (gnus-set-mode-line 'summary)
9315       ;; Return the number of marked articles.
9316       count)))
9317
9318 (defun gnus-summary-mark-as-processable (n &optional unmark)
9319   "Set the process mark on the next N articles.
9320 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9321 the process mark instead.  The difference between N and the actual
9322 number of articles marked is returned."
9323   (interactive "P")
9324   (if (and (null n) (gnus-region-active-p))
9325       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9326     (setq n (prefix-numeric-value n))
9327     (let ((backward (< n 0))
9328           (n (abs n)))
9329       (while (and
9330               (> n 0)
9331               (if unmark
9332                   (gnus-summary-remove-process-mark
9333                    (gnus-summary-article-number))
9334                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9335               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9336         (setq n (1- n)))
9337       (when (/= 0 n)
9338         (gnus-message 7 "No more articles"))
9339       (gnus-summary-recenter)
9340       (gnus-summary-position-point)
9341       n)))
9342
9343 (defun gnus-summary-unmark-as-processable (n)
9344   "Remove the process mark from the next N articles.
9345 If N is negative, unmark backward instead.  The difference between N and
9346 the actual number of articles unmarked is returned."
9347   (interactive "P")
9348   (gnus-summary-mark-as-processable n t))
9349
9350 (defun gnus-summary-unmark-all-processable ()
9351   "Remove the process mark from all articles."
9352   (interactive)
9353   (save-excursion
9354     (while gnus-newsgroup-processable
9355       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9356   (gnus-summary-position-point))
9357
9358 (defun gnus-summary-add-mark (article type)
9359   "Mark ARTICLE with a mark of TYPE."
9360   (let ((vtype (car (assq type gnus-article-mark-lists)))
9361         var)
9362     (if (not vtype)
9363         (error "No such mark type: %s" type)
9364       (setq var (intern (format "gnus-newsgroup-%s" type)))
9365       (set var (cons article (symbol-value var)))
9366       (if (memq type '(processable cached replied forwarded recent saved))
9367           (gnus-summary-update-secondary-mark article)
9368         ;;; !!! This is bogus.  We should find out what primary
9369         ;;; !!! mark we want to set.
9370         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9371
9372 (defun gnus-summary-mark-as-expirable (n)
9373   "Mark N articles forward as expirable.
9374 If N is negative, mark backward instead.  The difference between N and
9375 the actual number of articles marked is returned."
9376   (interactive "p")
9377   (gnus-summary-mark-forward n gnus-expirable-mark))
9378
9379 (defun gnus-summary-mark-as-spam (n)
9380   "Mark N articles forward as spam.
9381 If N is negative, mark backward instead.  The difference between N and
9382 the actual number of articles marked is returned."
9383   (interactive "p")
9384   (gnus-summary-mark-forward n gnus-spam-mark))
9385
9386 (defun gnus-summary-mark-article-as-replied (article)
9387   "Mark ARTICLE as replied to and update the summary line.
9388 ARTICLE can also be a list of articles."
9389   (interactive (list (gnus-summary-article-number)))
9390   (let ((articles (if (listp article) article (list article))))
9391     (dolist (article articles)
9392       (push article gnus-newsgroup-replied)
9393       (let ((buffer-read-only nil))
9394         (when (gnus-summary-goto-subject article nil t)
9395           (gnus-summary-update-secondary-mark article))))))
9396
9397 (defun gnus-summary-mark-article-as-forwarded (article)
9398   "Mark ARTICLE as forwarded and update the summary line.
9399 ARTICLE can also be a list of articles."
9400   (let ((articles (if (listp article) article (list article))))
9401     (dolist (article articles)
9402       (push article gnus-newsgroup-forwarded)
9403       (let ((buffer-read-only nil))
9404         (when (gnus-summary-goto-subject article nil t)
9405           (gnus-summary-update-secondary-mark article))))))
9406
9407 (defun gnus-summary-set-bookmark (article)
9408   "Set a bookmark in current article."
9409   (interactive (list (gnus-summary-article-number)))
9410   (when (or (not (get-buffer gnus-article-buffer))
9411             (not gnus-current-article)
9412             (not gnus-article-current)
9413             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9414     (error "No current article selected"))
9415   ;; Remove old bookmark, if one exists.
9416   (gnus-pull article gnus-newsgroup-bookmarks)
9417   ;; Set the new bookmark, which is on the form
9418   ;; (article-number . line-number-in-body).
9419   (push
9420    (cons article
9421          (save-excursion
9422            (set-buffer gnus-article-buffer)
9423            (count-lines
9424             (min (point)
9425                  (save-excursion
9426                    (article-goto-body)
9427                    (point)))
9428             (point))))
9429    gnus-newsgroup-bookmarks)
9430   (gnus-message 6 "A bookmark has been added to the current article."))
9431
9432 (defun gnus-summary-remove-bookmark (article)
9433   "Remove the bookmark from the current article."
9434   (interactive (list (gnus-summary-article-number)))
9435   ;; Remove old bookmark, if one exists.
9436   (if (not (assq article gnus-newsgroup-bookmarks))
9437       (gnus-message 6 "No bookmark in current article.")
9438     (gnus-pull article gnus-newsgroup-bookmarks)
9439     (gnus-message 6 "Removed bookmark.")))
9440
9441 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9442 (defun gnus-summary-mark-as-dormant (n)
9443   "Mark N articles forward as dormant.
9444 If N is negative, mark backward instead.  The difference between N and
9445 the actual number of articles marked is returned."
9446   (interactive "p")
9447   (gnus-summary-mark-forward n gnus-dormant-mark))
9448
9449 (defun gnus-summary-set-process-mark (article)
9450   "Set the process mark on ARTICLE and update the summary line."
9451   (setq gnus-newsgroup-processable
9452         (cons article
9453               (delq article gnus-newsgroup-processable)))
9454   (when (gnus-summary-goto-subject article)
9455     (gnus-summary-show-thread)
9456     (gnus-summary-goto-subject article)
9457     (gnus-summary-update-secondary-mark article)))
9458
9459 (defun gnus-summary-remove-process-mark (article)
9460   "Remove the process mark from ARTICLE and update the summary line."
9461   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9462   (when (gnus-summary-goto-subject article)
9463     (gnus-summary-show-thread)
9464     (gnus-summary-goto-subject article)
9465     (gnus-summary-update-secondary-mark article)))
9466
9467 (defun gnus-summary-set-saved-mark (article)
9468   "Set the process mark on ARTICLE and update the summary line."
9469   (push article gnus-newsgroup-saved)
9470   (when (gnus-summary-goto-subject article)
9471     (gnus-summary-update-secondary-mark article)))
9472
9473 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9474   "Mark N articles as read forwards.
9475 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9476 The difference between N and the actual number of articles marked is
9477 returned.
9478 If NO-EXPIRE, auto-expiry will be inhibited."
9479   (interactive "p")
9480   (gnus-summary-show-thread)
9481   (let ((backward (< n 0))
9482         (gnus-summary-goto-unread
9483          (and gnus-summary-goto-unread
9484               (not (eq gnus-summary-goto-unread 'never))
9485               (not (memq mark (list gnus-unread-mark
9486                                     gnus-ticked-mark gnus-dormant-mark)))))
9487         (n (abs n))
9488         (mark (or mark gnus-del-mark)))
9489     (while (and (> n 0)
9490                 (gnus-summary-mark-article nil mark no-expire)
9491                 (zerop (gnus-summary-next-subject
9492                         (if backward -1 1)
9493                         (and gnus-summary-goto-unread
9494                              (not (eq gnus-summary-goto-unread 'never)))
9495                         t)))
9496       (setq n (1- n)))
9497     (when (/= 0 n)
9498       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9499     (gnus-summary-recenter)
9500     (gnus-summary-position-point)
9501     (gnus-set-mode-line 'summary)
9502     n))
9503
9504 (defun gnus-summary-mark-article-as-read (mark)
9505   "Mark the current article quickly as read with MARK."
9506   (let ((article (gnus-summary-article-number)))
9507     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9508     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9509     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9510     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9511     (push (cons article mark) gnus-newsgroup-reads)
9512     ;; Possibly remove from cache, if that is used.
9513     (when gnus-use-cache
9514       (gnus-cache-enter-remove-article article))
9515     ;; Allow the backend to change the mark.
9516     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9517     ;; Check for auto-expiry.
9518     (when (and gnus-newsgroup-auto-expire
9519                (memq mark gnus-auto-expirable-marks))
9520       (setq mark gnus-expirable-mark)
9521       ;; Let the backend know about the mark change.
9522       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9523       (push article gnus-newsgroup-expirable))
9524     ;; Set the mark in the buffer.
9525     (gnus-summary-update-mark mark 'unread)
9526     t))
9527
9528 (defun gnus-summary-mark-article-as-unread (mark)
9529   "Mark the current article quickly as unread with MARK."
9530   (let* ((article (gnus-summary-article-number))
9531          (old-mark (gnus-summary-article-mark article)))
9532     ;; Allow the backend to change the mark.
9533     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9534     (if (eq mark old-mark)
9535         t
9536       (if (<= article 0)
9537           (progn
9538             (gnus-error 1 "Can't mark negative article numbers")
9539             nil)
9540         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9541         (setq gnus-newsgroup-spam-marked
9542               (delq article gnus-newsgroup-spam-marked))
9543         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9544         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9545         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9546         (cond ((= mark gnus-ticked-mark)
9547                (setq gnus-newsgroup-marked
9548                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9549                                               article)))
9550               ((= mark gnus-spam-mark)
9551                (setq gnus-newsgroup-spam-marked
9552                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9553                                               article)))
9554               ((= mark gnus-dormant-mark)
9555                (setq gnus-newsgroup-dormant
9556                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9557                                               article)))
9558               (t
9559                (setq gnus-newsgroup-unreads
9560                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9561                                               article))))
9562         (gnus-pull article gnus-newsgroup-reads)
9563
9564         ;; See whether the article is to be put in the cache.
9565         (and gnus-use-cache
9566              (vectorp (gnus-summary-article-header article))
9567              (save-excursion
9568                (gnus-cache-possibly-enter-article
9569                 gnus-newsgroup-name article
9570                 (= mark gnus-ticked-mark)
9571                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9572
9573         ;; Fix the mark.
9574         (gnus-summary-update-mark mark 'unread)
9575         t))))
9576
9577 (defun gnus-summary-mark-article (&optional article mark no-expire)
9578   "Mark ARTICLE with MARK.  MARK can be any character.
9579 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9580 `??' (dormant) and `?E' (expirable).
9581 If MARK is nil, then the default character `?r' is used.
9582 If ARTICLE is nil, then the article on the current line will be
9583 marked.
9584 If NO-EXPIRE, auto-expiry will be inhibited."
9585   ;; The mark might be a string.
9586   (when (stringp mark)
9587     (setq mark (aref mark 0)))
9588   ;; If no mark is given, then we check auto-expiring.
9589   (when (null mark)
9590     (setq mark gnus-del-mark))
9591   (when (and (not no-expire)
9592              gnus-newsgroup-auto-expire
9593              (memq mark gnus-auto-expirable-marks))
9594     (setq mark gnus-expirable-mark))
9595   (let ((article (or article (gnus-summary-article-number)))
9596         (old-mark (gnus-summary-article-mark article)))
9597     ;; Allow the backend to change the mark.
9598     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9599     (if (eq mark old-mark)
9600         t
9601       (unless article
9602         (error "No article on current line"))
9603       (if (not (if (or (= mark gnus-unread-mark)
9604                        (= mark gnus-ticked-mark)
9605                        (= mark gnus-spam-mark)
9606                        (= mark gnus-dormant-mark))
9607                    (gnus-mark-article-as-unread article mark)
9608                  (gnus-mark-article-as-read article mark)))
9609           t
9610         ;; See whether the article is to be put in the cache.
9611         (and gnus-use-cache
9612              (not (= mark gnus-canceled-mark))
9613              (vectorp (gnus-summary-article-header article))
9614              (save-excursion
9615                (gnus-cache-possibly-enter-article
9616                 gnus-newsgroup-name article
9617                 (= mark gnus-ticked-mark)
9618                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9619
9620         (when (gnus-summary-goto-subject article nil t)
9621           (let ((buffer-read-only nil))
9622             (gnus-summary-show-thread)
9623             ;; Fix the mark.
9624             (gnus-summary-update-mark mark 'unread)
9625             t))))))
9626
9627 (defun gnus-summary-update-secondary-mark (article)
9628   "Update the secondary (read, process, cache) mark."
9629   (gnus-summary-update-mark
9630    (cond ((memq article gnus-newsgroup-processable)
9631           gnus-process-mark)
9632          ((memq article gnus-newsgroup-cached)
9633           gnus-cached-mark)
9634          ((memq article gnus-newsgroup-replied)
9635           gnus-replied-mark)
9636          ((memq article gnus-newsgroup-forwarded)
9637           gnus-forwarded-mark)
9638          ((memq article gnus-newsgroup-saved)
9639           gnus-saved-mark)
9640          ((memq article gnus-newsgroup-recent)
9641           gnus-recent-mark)
9642          ((memq article gnus-newsgroup-unseen)
9643           gnus-unseen-mark)
9644          (t gnus-no-mark))
9645    'replied)
9646   (when (gnus-visual-p 'summary-highlight 'highlight)
9647     (gnus-run-hooks 'gnus-summary-update-hook))
9648   t)
9649
9650 (defun gnus-summary-update-download-mark (article)
9651   "Update the secondary (read, process, cache) mark."
9652   (gnus-summary-update-mark
9653    (cond ((memq article gnus-newsgroup-undownloaded) 
9654           gnus-undownloaded-mark)
9655          (gnus-newsgroup-agentized
9656           gnus-downloaded-mark)
9657          (t
9658           gnus-no-mark))
9659    'download)
9660   (gnus-summary-update-line t)
9661   t)
9662
9663 (defun gnus-summary-update-mark (mark type)
9664   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9665         (buffer-read-only nil))
9666     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9667     (when forward
9668       (when (looking-at "\r")
9669         (incf forward))
9670       (when (<= (+ forward (point)) (point-max))
9671         ;; Go to the right position on the line.
9672         (goto-char (+ forward (point)))
9673         ;; Replace the old mark with the new mark.
9674         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9675         ;; Optionally update the marks by some user rule.
9676         (when (eq type 'unread)
9677           (gnus-data-set-mark
9678            (gnus-data-find (gnus-summary-article-number)) mark)
9679           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9680
9681 (defun gnus-mark-article-as-read (article &optional mark)
9682   "Enter ARTICLE in the pertinent lists and remove it from others."
9683   ;; Make the article expirable.
9684   (let ((mark (or mark gnus-del-mark)))
9685     (setq gnus-newsgroup-expirable
9686           (if (= mark gnus-expirable-mark)
9687               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9688             (delq article gnus-newsgroup-expirable)))
9689     ;; Remove from unread and marked lists.
9690     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9691     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9692     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9693     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9694     (push (cons article mark) gnus-newsgroup-reads)
9695     ;; Possibly remove from cache, if that is used.
9696     (when gnus-use-cache
9697       (gnus-cache-enter-remove-article article))
9698     t))
9699
9700 (defun gnus-mark-article-as-unread (article &optional mark)
9701   "Enter ARTICLE in the pertinent lists and remove it from others."
9702   (let ((mark (or mark gnus-ticked-mark)))
9703     (if (<= article 0)
9704         (progn
9705           (gnus-error 1 "Can't mark negative article numbers")
9706           nil)
9707       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9708             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9709             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9710             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9711             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9712
9713       ;; Unsuppress duplicates?
9714       (when gnus-suppress-duplicates
9715         (gnus-dup-unsuppress-article article))
9716
9717       (cond ((= mark gnus-ticked-mark)
9718              (setq gnus-newsgroup-marked
9719                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9720             ((= mark gnus-spam-mark)
9721              (setq gnus-newsgroup-spam-marked
9722                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9723                                             article)))
9724             ((= mark gnus-dormant-mark)
9725              (setq gnus-newsgroup-dormant
9726                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9727             (t
9728              (setq gnus-newsgroup-unreads
9729                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9730       (gnus-pull article gnus-newsgroup-reads)
9731       t)))
9732
9733 (defalias 'gnus-summary-mark-as-unread-forward
9734   'gnus-summary-tick-article-forward)
9735 (make-obsolete 'gnus-summary-mark-as-unread-forward
9736                'gnus-summary-tick-article-forward)
9737 (defun gnus-summary-tick-article-forward (n)
9738   "Tick N articles forwards.
9739 If N is negative, tick backwards instead.
9740 The difference between N and the number of articles ticked is returned."
9741   (interactive "p")
9742   (gnus-summary-mark-forward n gnus-ticked-mark))
9743
9744 (defalias 'gnus-summary-mark-as-unread-backward
9745   'gnus-summary-tick-article-backward)
9746 (make-obsolete 'gnus-summary-mark-as-unread-backward
9747                'gnus-summary-tick-article-backward)
9748 (defun gnus-summary-tick-article-backward (n)
9749   "Tick N articles backwards.
9750 The difference between N and the number of articles ticked is returned."
9751   (interactive "p")
9752   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9753
9754 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9755 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9756 (defun gnus-summary-tick-article (&optional article clear-mark)
9757   "Mark current article as unread.
9758 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9759 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9760   (interactive)
9761   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9762                                        gnus-ticked-mark)))
9763
9764 (defun gnus-summary-mark-as-read-forward (n)
9765   "Mark N articles as read forwards.
9766 If N is negative, mark backwards instead.
9767 The difference between N and the actual number of articles marked is
9768 returned."
9769   (interactive "p")
9770   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9771
9772 (defun gnus-summary-mark-as-read-backward (n)
9773   "Mark the N articles as read backwards.
9774 The difference between N and the actual number of articles marked is
9775 returned."
9776   (interactive "p")
9777   (gnus-summary-mark-forward
9778    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9779
9780 (defun gnus-summary-mark-as-read (&optional article mark)
9781   "Mark current article as read.
9782 ARTICLE specifies the article to be marked as read.
9783 MARK specifies a string to be inserted at the beginning of the line."
9784   (gnus-summary-mark-article article mark))
9785
9786 (defun gnus-summary-clear-mark-forward (n)
9787   "Clear marks from N articles forward.
9788 If N is negative, clear backward instead.
9789 The difference between N and the number of marks cleared is returned."
9790   (interactive "p")
9791   (gnus-summary-mark-forward n gnus-unread-mark))
9792
9793 (defun gnus-summary-clear-mark-backward (n)
9794   "Clear marks from N articles backward.
9795 The difference between N and the number of marks cleared is returned."
9796   (interactive "p")
9797   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9798
9799 (defun gnus-summary-mark-unread-as-read ()
9800   "Intended to be used by `gnus-summary-mark-article-hook'."
9801   (when (memq gnus-current-article gnus-newsgroup-unreads)
9802     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9803
9804 (defun gnus-summary-mark-read-and-unread-as-read ()
9805   "Intended to be used by `gnus-summary-mark-article-hook'."
9806   (let ((mark (gnus-summary-article-mark)))
9807     (when (or (gnus-unread-mark-p mark)
9808               (gnus-read-mark-p mark))
9809       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9810
9811 (defun gnus-summary-mark-unread-as-ticked ()
9812   "Intended to be used by `gnus-summary-mark-article-hook'."
9813   (when (memq gnus-current-article gnus-newsgroup-unreads)
9814     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9815
9816 (defun gnus-summary-mark-region-as-read (point mark all)
9817   "Mark all unread articles between point and mark as read.
9818 If given a prefix, mark all articles between point and mark as read,
9819 even ticked and dormant ones."
9820   (interactive "r\nP")
9821   (save-excursion
9822     (let (article)
9823       (goto-char point)
9824       (beginning-of-line)
9825       (while (and
9826               (< (point) mark)
9827               (progn
9828                 (when (or all
9829                           (memq (setq article (gnus-summary-article-number))
9830                                 gnus-newsgroup-unreads))
9831                   (gnus-summary-mark-article article gnus-del-mark))
9832                 t)
9833               (gnus-summary-find-next))))))
9834
9835 (defun gnus-summary-mark-below (score mark)
9836   "Mark articles with score less than SCORE with MARK."
9837   (interactive "P\ncMark: ")
9838   (setq score (if score
9839                   (prefix-numeric-value score)
9840                 (or gnus-summary-default-score 0)))
9841   (save-excursion
9842     (set-buffer gnus-summary-buffer)
9843     (goto-char (point-min))
9844     (while
9845         (progn
9846           (and (< (gnus-summary-article-score) score)
9847                (gnus-summary-mark-article nil mark))
9848           (gnus-summary-find-next)))))
9849
9850 (defun gnus-summary-kill-below (&optional score)
9851   "Mark articles with score below SCORE as read."
9852   (interactive "P")
9853   (gnus-summary-mark-below score gnus-killed-mark))
9854
9855 (defun gnus-summary-clear-above (&optional score)
9856   "Clear all marks from articles with score above SCORE."
9857   (interactive "P")
9858   (gnus-summary-mark-above score gnus-unread-mark))
9859
9860 (defun gnus-summary-tick-above (&optional score)
9861   "Tick all articles with score above SCORE."
9862   (interactive "P")
9863   (gnus-summary-mark-above score gnus-ticked-mark))
9864
9865 (defun gnus-summary-mark-above (score mark)
9866   "Mark articles with score over SCORE with MARK."
9867   (interactive "P\ncMark: ")
9868   (setq score (if score
9869                   (prefix-numeric-value score)
9870                 (or gnus-summary-default-score 0)))
9871   (save-excursion
9872     (set-buffer gnus-summary-buffer)
9873     (goto-char (point-min))
9874     (while (and (progn
9875                   (when (> (gnus-summary-article-score) score)
9876                     (gnus-summary-mark-article nil mark))
9877                   t)
9878                 (gnus-summary-find-next)))))
9879
9880 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9881 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9882 (defun gnus-summary-limit-include-expunged (&optional no-error)
9883   "Display all the hidden articles that were expunged for low scores."
9884   (interactive)
9885   (let ((buffer-read-only nil))
9886     (let ((scored gnus-newsgroup-scored)
9887           headers h)
9888       (while scored
9889         (unless (gnus-summary-article-header (caar scored))
9890           (and (setq h (gnus-number-to-header (caar scored)))
9891                (< (cdar scored) gnus-summary-expunge-below)
9892                (push h headers)))
9893         (setq scored (cdr scored)))
9894       (if (not headers)
9895           (when (not no-error)
9896             (error "No expunged articles hidden"))
9897         (goto-char (point-min))
9898         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9899         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9900         (mapcar (lambda (x) (push (mail-header-number x)
9901                                   gnus-newsgroup-limit))
9902                 headers)
9903         (gnus-summary-prepare-unthreaded (nreverse headers))
9904         (goto-char (point-min))
9905         (gnus-summary-position-point)
9906         t))))
9907
9908 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9909   "Mark all unread articles in this newsgroup as read.
9910 If prefix argument ALL is non-nil, ticked and dormant articles will
9911 also be marked as read.
9912 If QUIETLY is non-nil, no questions will be asked.
9913 If TO-HERE is non-nil, it should be a point in the buffer.  All
9914 articles before (after, if REVERSE is set) this point will be marked as read.
9915 Note that this function will only catch up the unread article
9916 in the current summary buffer limitation.
9917 The number of articles marked as read is returned."
9918   (interactive "P")
9919   (prog1
9920       (save-excursion
9921         (when (or quietly
9922                   (not gnus-interactive-catchup) ;Without confirmation?
9923                   gnus-expert-user
9924                   (gnus-y-or-n-p
9925                    (if all
9926                        "Mark absolutely all articles as read? "
9927                      "Mark all unread articles as read? ")))
9928           (if (and not-mark
9929                    (not gnus-newsgroup-adaptive)
9930                    (not gnus-newsgroup-auto-expire)
9931                    (not gnus-suppress-duplicates)
9932                    (or (not gnus-use-cache)
9933                        (eq gnus-use-cache 'passive)))
9934               (progn
9935                 (when all
9936                   (setq gnus-newsgroup-marked nil
9937                         gnus-newsgroup-spam-marked nil
9938                         gnus-newsgroup-dormant nil))
9939                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9940             ;; We actually mark all articles as canceled, which we
9941             ;; have to do when using auto-expiry or adaptive scoring.
9942             (gnus-summary-show-all-threads)
9943             (if (and to-here reverse)
9944                 (progn
9945                   (goto-char to-here)
9946                   (while (and
9947                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9948                           (gnus-summary-find-next (not all) nil nil t))))
9949               (when (gnus-summary-first-subject (not all) t)
9950                 (while (and
9951                         (if to-here (< (point) to-here) t)
9952                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9953                         (gnus-summary-find-next (not all) nil nil t)))))
9954             (gnus-set-mode-line 'summary))
9955           t))
9956     (gnus-summary-position-point)))
9957
9958 (defun gnus-summary-catchup-to-here (&optional all)
9959   "Mark all unticked articles before the current one as read.
9960 If ALL is non-nil, also mark ticked and dormant articles as read."
9961   (interactive "P")
9962   (save-excursion
9963     (gnus-save-hidden-threads
9964       (let ((beg (point)))
9965         ;; We check that there are unread articles.
9966         (when (or all (gnus-summary-find-prev))
9967           (gnus-summary-catchup all t beg)))))
9968   (gnus-summary-position-point))
9969
9970 (defun gnus-summary-catchup-from-here (&optional all)
9971   "Mark all unticked articles after the current one as read.
9972 If ALL is non-nil, also mark ticked and dormant articles as read."
9973   (interactive "P")
9974   (save-excursion
9975     (gnus-save-hidden-threads
9976       (let ((beg (point)))
9977         ;; We check that there are unread articles.
9978         (when (or all (gnus-summary-find-next))
9979           (gnus-summary-catchup all t beg nil t)))))
9980
9981   (gnus-summary-position-point))
9982 (defun gnus-summary-catchup-all (&optional quietly)
9983   "Mark all articles in this newsgroup as read.
9984 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
9985 instead, which marks only unread articles as read."
9986   (interactive "P")
9987   (gnus-summary-catchup t quietly))
9988
9989 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9990   "Mark all unread articles in this group as read, then exit.
9991 If prefix argument ALL is non-nil, all articles are marked as read.
9992 If QUIETLY is non-nil, no questions will be asked."
9993   (interactive "P")
9994   (when (gnus-summary-catchup all quietly nil 'fast)
9995     ;; Select next newsgroup or exit.
9996     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9997              (eq gnus-auto-select-next 'quietly))
9998         (gnus-summary-next-group nil)
9999       (gnus-summary-exit))))
10000
10001 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10002   "Mark all articles in this newsgroup as read, and then exit.
10003 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10004 instead, which marks only unread articles as read."
10005   (interactive "P")
10006   (gnus-summary-catchup-and-exit t quietly))
10007
10008 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10009   "Mark all articles in this group as read and select the next group.
10010 If given a prefix, mark all articles, unread as well as ticked, as
10011 read."
10012   (interactive "P")
10013   (save-excursion
10014     (gnus-summary-catchup all))
10015   (gnus-summary-next-group))
10016
10017 ;;;
10018 ;;; with article
10019 ;;;
10020
10021 (defmacro gnus-with-article (article &rest forms)
10022   "Select ARTICLE and perform FORMS in the original article buffer.
10023 Then replace the article with the result."
10024   `(progn
10025      ;; We don't want the article to be marked as read.
10026      (let (gnus-mark-article-hook)
10027        (gnus-summary-select-article t t nil ,article))
10028      (set-buffer gnus-original-article-buffer)
10029      ,@forms
10030      (if (not (gnus-check-backend-function
10031                'request-replace-article (car gnus-article-current)))
10032          (gnus-message 5 "Read-only group; not replacing")
10033        (unless (gnus-request-replace-article
10034                 ,article (car gnus-article-current)
10035                 (current-buffer) t)
10036          (error "Couldn't replace article")))
10037      ;; The cache and backlog have to be flushed somewhat.
10038      (when gnus-keep-backlog
10039        (gnus-backlog-remove-article
10040         (car gnus-article-current) (cdr gnus-article-current)))
10041      (when gnus-use-cache
10042        (gnus-cache-update-article
10043         (car gnus-article-current) (cdr gnus-article-current)))))
10044
10045 (put 'gnus-with-article 'lisp-indent-function 1)
10046 (put 'gnus-with-article 'edebug-form-spec '(form body))
10047
10048 ;; Thread-based commands.
10049
10050 (defun gnus-summary-articles-in-thread (&optional article)
10051   "Return a list of all articles in the current thread.
10052 If ARTICLE is non-nil, return all articles in the thread that starts
10053 with that article."
10054   (let* ((article (or article (gnus-summary-article-number)))
10055          (data (gnus-data-find-list article))
10056          (top-level (gnus-data-level (car data)))
10057          (top-subject
10058           (cond ((null gnus-thread-operation-ignore-subject)
10059                  (gnus-simplify-subject-re
10060                   (mail-header-subject (gnus-data-header (car data)))))
10061                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10062                  (gnus-simplify-subject-fuzzy
10063                   (mail-header-subject (gnus-data-header (car data)))))
10064                 (t nil)))
10065          (end-point (save-excursion
10066                       (if (gnus-summary-go-to-next-thread)
10067                           (point) (point-max))))
10068          articles)
10069     (while (and data
10070                 (< (gnus-data-pos (car data)) end-point))
10071       (when (or (not top-subject)
10072                 (string= top-subject
10073                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10074                              (gnus-simplify-subject-fuzzy
10075                               (mail-header-subject
10076                                (gnus-data-header (car data))))
10077                            (gnus-simplify-subject-re
10078                             (mail-header-subject
10079                              (gnus-data-header (car data)))))))
10080         (push (gnus-data-number (car data)) articles))
10081       (unless (and (setq data (cdr data))
10082                    (> (gnus-data-level (car data)) top-level))
10083         (setq data nil)))
10084     ;; Return the list of articles.
10085     (nreverse articles)))
10086
10087 (defun gnus-summary-rethread-current ()
10088   "Rethread the thread the current article is part of."
10089   (interactive)
10090   (let* ((gnus-show-threads t)
10091          (article (gnus-summary-article-number))
10092          (id (mail-header-id (gnus-summary-article-header)))
10093          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10094     (unless id
10095       (error "No article on the current line"))
10096     (gnus-rebuild-thread id)
10097     (gnus-summary-goto-subject article)))
10098
10099 (defun gnus-summary-reparent-thread ()
10100   "Make the current article child of the marked (or previous) article.
10101
10102 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10103 is non-nil or the Subject: of both articles are the same."
10104   (interactive)
10105   (unless (not (gnus-group-read-only-p))
10106     (error "The current newsgroup does not support article editing"))
10107   (unless (<= (length gnus-newsgroup-processable) 1)
10108     (error "No more than one article may be marked"))
10109   (save-window-excursion
10110     (let ((gnus-article-buffer " *reparent*")
10111           (current-article (gnus-summary-article-number))
10112           ;; First grab the marked article, otherwise one line up.
10113           (parent-article (if (not (null gnus-newsgroup-processable))
10114                               (car gnus-newsgroup-processable)
10115                             (save-excursion
10116                               (if (eq (forward-line -1) 0)
10117                                   (gnus-summary-article-number)
10118                                 (error "Beginning of summary buffer"))))))
10119       (unless (not (eq current-article parent-article))
10120         (error "An article may not be self-referential"))
10121       (let ((message-id (mail-header-id
10122                          (gnus-summary-article-header parent-article))))
10123         (unless (and message-id (not (equal message-id "")))
10124           (error "No message-id in desired parent"))
10125         (gnus-with-article current-article
10126           (save-restriction
10127             (goto-char (point-min))
10128             (message-narrow-to-head)
10129             (if (re-search-forward "^References: " nil t)
10130                 (progn
10131                   (re-search-forward "^[^ \t]" nil t)
10132                   (forward-line -1)
10133                   (end-of-line)
10134                   (insert " " message-id))
10135               (insert "References: " message-id "\n"))))
10136         (set-buffer gnus-summary-buffer)
10137         (gnus-summary-unmark-all-processable)
10138         (gnus-summary-update-article current-article)
10139         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10140             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10141         (gnus-summary-rethread-current)
10142         (gnus-message 3 "Article %d is now the child of article %d"
10143                       current-article parent-article)))))
10144
10145 (defun gnus-summary-toggle-threads (&optional arg)
10146   "Toggle showing conversation threads.
10147 If ARG is positive number, turn showing conversation threads on."
10148   (interactive "P")
10149   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10150     (setq gnus-show-threads
10151           (if (null arg) (not gnus-show-threads)
10152             (> (prefix-numeric-value arg) 0)))
10153     (gnus-summary-prepare)
10154     (gnus-summary-goto-subject current)
10155     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10156     (gnus-summary-position-point)))
10157
10158 (defun gnus-summary-show-all-threads ()
10159   "Show all threads."
10160   (interactive)
10161   (save-excursion
10162     (let ((buffer-read-only nil))
10163       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10164   (gnus-summary-position-point))
10165
10166 (defun gnus-summary-show-thread ()
10167   "Show thread subtrees.
10168 Returns nil if no thread was there to be shown."
10169   (interactive)
10170   (let ((buffer-read-only nil)
10171         (orig (point))
10172         ;; first goto end then to beg, to have point at beg after let
10173         (end (progn (end-of-line) (point)))
10174         (beg (progn (beginning-of-line) (point))))
10175     (prog1
10176         ;; Any hidden lines here?
10177         (search-forward "\r" end t)
10178       (subst-char-in-region beg end ?\^M ?\n t)
10179       (goto-char orig)
10180       (gnus-summary-position-point))))
10181
10182 (defun gnus-summary-maybe-hide-threads ()
10183   "If requested, hide the threads that should be hidden."
10184   (when (and gnus-show-threads
10185              gnus-thread-hide-subtree)
10186     (gnus-summary-hide-all-threads
10187      (if (or (consp gnus-thread-hide-subtree)
10188              (gnus-functionp gnus-thread-hide-subtree))
10189          (gnus-make-predicate gnus-thread-hide-subtree)
10190        nil))))
10191
10192 ;;; Hiding predicates.
10193
10194 (defun gnus-article-unread-p (header)
10195   (memq (mail-header-number header) gnus-newsgroup-unreads))
10196
10197 (defun gnus-article-unseen-p (header)
10198   (memq (mail-header-number header) gnus-newsgroup-unseen))
10199
10200 (defun gnus-map-articles (predicate articles)
10201   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10202   (apply 'gnus-or (mapcar predicate
10203                           (mapcar 'gnus-summary-article-header articles))))
10204
10205 (defun gnus-summary-hide-all-threads (&optional predicate)
10206   "Hide all thread subtrees.
10207 If PREDICATE is supplied, threads that satisfy this predicate
10208 will not be hidden."
10209   (interactive)
10210   (save-excursion
10211     (goto-char (point-min))
10212     (let ((end nil))
10213       (while (not end)
10214         (when (or (not predicate)
10215                   (gnus-map-articles
10216                    predicate (gnus-summary-article-children)))
10217             (gnus-summary-hide-thread))
10218         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10219   (gnus-summary-position-point))
10220
10221 (defun gnus-summary-hide-thread ()
10222   "Hide thread subtrees.
10223 If PREDICATE is supplied, threads that satisfy this predicate
10224 will not be hidden.
10225 Returns nil if no threads were there to be hidden."
10226   (interactive)
10227   (let ((buffer-read-only nil)
10228         (start (point))
10229         (article (gnus-summary-article-number)))
10230     (goto-char start)
10231     ;; Go forward until either the buffer ends or the subthread
10232     ;; ends.
10233     (when (and (not (eobp))
10234                (or (zerop (gnus-summary-next-thread 1 t))
10235                    (goto-char (point-max))))
10236       (prog1
10237           (if (and (> (point) start)
10238                    (search-backward "\n" start t))
10239               (progn
10240                 (subst-char-in-region start (point) ?\n ?\^M)
10241                 (gnus-summary-goto-subject article))
10242             (goto-char start)
10243             nil)))))
10244
10245 (defun gnus-summary-go-to-next-thread (&optional previous)
10246   "Go to the same level (or less) next thread.
10247 If PREVIOUS is non-nil, go to previous thread instead.
10248 Return the article number moved to, or nil if moving was impossible."
10249   (let ((level (gnus-summary-thread-level))
10250         (way (if previous -1 1))
10251         (beg (point)))
10252     (forward-line way)
10253     (while (and (not (eobp))
10254                 (< level (gnus-summary-thread-level)))
10255       (forward-line way))
10256     (if (eobp)
10257         (progn
10258           (goto-char beg)
10259           nil)
10260       (setq beg (point))
10261       (prog1
10262           (gnus-summary-article-number)
10263         (goto-char beg)))))
10264
10265 (defun gnus-summary-next-thread (n &optional silent)
10266   "Go to the same level next N'th thread.
10267 If N is negative, search backward instead.
10268 Returns the difference between N and the number of skips actually
10269 done.
10270
10271 If SILENT, don't output messages."
10272   (interactive "p")
10273   (let ((backward (< n 0))
10274         (n (abs n)))
10275     (while (and (> n 0)
10276                 (gnus-summary-go-to-next-thread backward))
10277       (decf n))
10278     (unless silent
10279       (gnus-summary-position-point))
10280     (when (and (not silent) (/= 0 n))
10281       (gnus-message 7 "No more threads"))
10282     n))
10283
10284 (defun gnus-summary-prev-thread (n)
10285   "Go to the same level previous N'th thread.
10286 Returns the difference between N and the number of skips actually
10287 done."
10288   (interactive "p")
10289   (gnus-summary-next-thread (- n)))
10290
10291 (defun gnus-summary-go-down-thread ()
10292   "Go down one level in the current thread."
10293   (let ((children (gnus-summary-article-children)))
10294     (when children
10295       (gnus-summary-goto-subject (car children)))))
10296
10297 (defun gnus-summary-go-up-thread ()
10298   "Go up one level in the current thread."
10299   (let ((parent (gnus-summary-article-parent)))
10300     (when parent
10301       (gnus-summary-goto-subject parent))))
10302
10303 (defun gnus-summary-down-thread (n)
10304   "Go down thread N steps.
10305 If N is negative, go up instead.
10306 Returns the difference between N and how many steps down that were
10307 taken."
10308   (interactive "p")
10309   (let ((up (< n 0))
10310         (n (abs n)))
10311     (while (and (> n 0)
10312                 (if up (gnus-summary-go-up-thread)
10313                   (gnus-summary-go-down-thread)))
10314       (setq n (1- n)))
10315     (gnus-summary-position-point)
10316     (when (/= 0 n)
10317       (gnus-message 7 "Can't go further"))
10318     n))
10319
10320 (defun gnus-summary-up-thread (n)
10321   "Go up thread N steps.
10322 If N is negative, go down instead.
10323 Returns the difference between N and how many steps down that were
10324 taken."
10325   (interactive "p")
10326   (gnus-summary-down-thread (- n)))
10327
10328 (defun gnus-summary-top-thread ()
10329   "Go to the top of the thread."
10330   (interactive)
10331   (while (gnus-summary-go-up-thread))
10332   (gnus-summary-article-number))
10333
10334 (defun gnus-summary-kill-thread (&optional unmark)
10335   "Mark articles under current thread as read.
10336 If the prefix argument is positive, remove any kinds of marks.
10337 If the prefix argument is negative, tick articles instead."
10338   (interactive "P")
10339   (when unmark
10340     (setq unmark (prefix-numeric-value unmark)))
10341   (let ((articles (gnus-summary-articles-in-thread)))
10342     (save-excursion
10343       ;; Expand the thread.
10344       (gnus-summary-show-thread)
10345       ;; Mark all the articles.
10346       (while articles
10347         (gnus-summary-goto-subject (car articles))
10348         (cond ((null unmark)
10349                (gnus-summary-mark-article-as-read gnus-killed-mark))
10350               ((> unmark 0)
10351                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10352               (t
10353                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10354         (setq articles (cdr articles))))
10355     ;; Hide killed subtrees.
10356     (and (null unmark)
10357          gnus-thread-hide-killed
10358          (gnus-summary-hide-thread))
10359     ;; If marked as read, go to next unread subject.
10360     (when (null unmark)
10361       ;; Go to next unread subject.
10362       (gnus-summary-next-subject 1 t)))
10363   (gnus-set-mode-line 'summary))
10364
10365 ;; Summary sorting commands
10366
10367 (defun gnus-summary-sort-by-number (&optional reverse)
10368   "Sort the summary buffer by article number.
10369 Argument REVERSE means reverse order."
10370   (interactive "P")
10371   (gnus-summary-sort 'number reverse))
10372
10373 (defun gnus-summary-sort-by-random (&optional reverse)
10374   "Randomize the order in the summary buffer.
10375 Argument REVERSE means to randomize in reverse order."
10376   (interactive "P")
10377   (gnus-summary-sort 'random reverse))
10378
10379 (defun gnus-summary-sort-by-author (&optional reverse)
10380   "Sort the summary buffer by author name alphabetically.
10381 If `case-fold-search' is non-nil, case of letters is ignored.
10382 Argument REVERSE means reverse order."
10383   (interactive "P")
10384   (gnus-summary-sort 'author reverse))
10385
10386 (defun gnus-summary-sort-by-subject (&optional reverse)
10387   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10388 If `case-fold-search' is non-nil, case of letters is ignored.
10389 Argument REVERSE means reverse order."
10390   (interactive "P")
10391   (gnus-summary-sort 'subject reverse))
10392
10393 (defun gnus-summary-sort-by-date (&optional reverse)
10394   "Sort the summary buffer by date.
10395 Argument REVERSE means reverse order."
10396   (interactive "P")
10397   (gnus-summary-sort 'date reverse))
10398
10399 (defun gnus-summary-sort-by-score (&optional reverse)
10400   "Sort the summary buffer by score.
10401 Argument REVERSE means reverse order."
10402   (interactive "P")
10403   (gnus-summary-sort 'score reverse))
10404
10405 (defun gnus-summary-sort-by-lines (&optional reverse)
10406   "Sort the summary buffer by the number of lines.
10407 Argument REVERSE means reverse order."
10408   (interactive "P")
10409   (gnus-summary-sort 'lines reverse))
10410
10411 (defun gnus-summary-sort-by-chars (&optional reverse)
10412   "Sort the summary buffer by article length.
10413 Argument REVERSE means reverse order."
10414   (interactive "P")
10415   (gnus-summary-sort 'chars reverse))
10416
10417 (defun gnus-summary-sort-by-original (&optional reverse)
10418   "Sort the summary buffer using the default sorting method.
10419 Argument REVERSE means reverse order."
10420   (interactive "P")
10421   (let* ((buffer-read-only)
10422          (gnus-summary-prepare-hook nil))
10423     ;; We do the sorting by regenerating the threads.
10424     (gnus-summary-prepare)
10425     ;; Hide subthreads if needed.
10426     (gnus-summary-maybe-hide-threads)))
10427
10428 (defun gnus-summary-sort (predicate reverse)
10429   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10430   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10431          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10432          (gnus-thread-sort-functions
10433           (if (not reverse)
10434               thread
10435             `(lambda (t1 t2)
10436                (,thread t2 t1))))
10437          (gnus-sort-gathered-threads-function
10438           gnus-thread-sort-functions)
10439          (gnus-article-sort-functions
10440           (if (not reverse)
10441               article
10442             `(lambda (t1 t2)
10443                (,article t2 t1))))
10444          (buffer-read-only)
10445          (gnus-summary-prepare-hook nil))
10446     ;; We do the sorting by regenerating the threads.
10447     (gnus-summary-prepare)
10448     ;; Hide subthreads if needed.
10449     (gnus-summary-maybe-hide-threads)))
10450
10451 ;; Summary saving commands.
10452
10453 (defun gnus-summary-save-article (&optional n not-saved)
10454   "Save the current article using the default saver function.
10455 If N is a positive number, save the N next articles.
10456 If N is a negative number, save the N previous articles.
10457 If N is nil and any articles have been marked with the process mark,
10458 save those articles instead.
10459 The variable `gnus-default-article-saver' specifies the saver function."
10460   (interactive "P")
10461   (let* ((articles (gnus-summary-work-articles n))
10462          (save-buffer (save-excursion
10463                         (nnheader-set-temp-buffer " *Gnus Save*")))
10464          (num (length articles))
10465          header file)
10466     (dolist (article articles)
10467       (setq header (gnus-summary-article-header article))
10468       (if (not (vectorp header))
10469           ;; This is a pseudo-article.
10470           (if (assq 'name header)
10471               (gnus-copy-file (cdr (assq 'name header)))
10472             (gnus-message 1 "Article %d is unsaveable" article))
10473         ;; This is a real article.
10474         (save-window-excursion
10475           (let ((gnus-display-mime-function nil)
10476                 (gnus-article-prepare-hook nil))
10477             (gnus-summary-select-article t nil nil article)))
10478         (save-excursion
10479           (set-buffer save-buffer)
10480           (erase-buffer)
10481           (insert-buffer-substring gnus-original-article-buffer))
10482         (setq file (gnus-article-save save-buffer file num))
10483         (gnus-summary-remove-process-mark article)
10484         (unless not-saved
10485           (gnus-summary-set-saved-mark article))))
10486     (gnus-kill-buffer save-buffer)
10487     (gnus-summary-position-point)
10488     (gnus-set-mode-line 'summary)
10489     n))
10490
10491 (defun gnus-summary-pipe-output (&optional arg headers)
10492   "Pipe the current article to a subprocess.
10493 If N is a positive number, pipe the N next articles.
10494 If N is a negative number, pipe the N previous articles.
10495 If N is nil and any articles have been marked with the process mark,
10496 pipe those articles instead.
10497 If HEADERS (the symbolic prefix), include the headers, too."
10498   (interactive (gnus-interactive "P\ny"))
10499   (require 'gnus-art)
10500   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10501         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10502     (gnus-summary-save-article arg t))
10503   (let ((buffer (get-buffer "*Shell Command Output*")))
10504     (when (and buffer
10505                (not (zerop (buffer-size buffer))))
10506       (gnus-configure-windows 'pipe))))
10507
10508 (defun gnus-summary-save-article-mail (&optional arg)
10509   "Append the current article to an mail file.
10510 If N is a positive number, save the N next articles.
10511 If N is a negative number, save the N previous articles.
10512 If N is nil and any articles have been marked with the process mark,
10513 save those articles instead."
10514   (interactive "P")
10515   (require 'gnus-art)
10516   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10517     (gnus-summary-save-article arg)))
10518
10519 (defun gnus-summary-save-article-rmail (&optional arg)
10520   "Append the current article to an rmail file.
10521 If N is a positive number, save the N next articles.
10522 If N is a negative number, save the N previous articles.
10523 If N is nil and any articles have been marked with the process mark,
10524 save those articles instead."
10525   (interactive "P")
10526   (require 'gnus-art)
10527   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10528     (gnus-summary-save-article arg)))
10529
10530 (defun gnus-summary-save-article-file (&optional arg)
10531   "Append the current article to a file.
10532 If N is a positive number, save the N next articles.
10533 If N is a negative number, save the N previous articles.
10534 If N is nil and any articles have been marked with the process mark,
10535 save those articles instead."
10536   (interactive "P")
10537   (require 'gnus-art)
10538   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10539     (gnus-summary-save-article arg)))
10540
10541 (defun gnus-summary-write-article-file (&optional arg)
10542   "Write the current article to a file, deleting the previous file.
10543 If N is a positive number, save the N next articles.
10544 If N is a negative number, save the N previous articles.
10545 If N is nil and any articles have been marked with the process mark,
10546 save those articles instead."
10547   (interactive "P")
10548   (require 'gnus-art)
10549   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10550     (gnus-summary-save-article arg)))
10551
10552 (defun gnus-summary-save-article-body-file (&optional arg)
10553   "Append the current article body to a file.
10554 If N is a positive number, save the N next articles.
10555 If N is a negative number, save the N previous articles.
10556 If N is nil and any articles have been marked with the process mark,
10557 save those articles instead."
10558   (interactive "P")
10559   (require 'gnus-art)
10560   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10561     (gnus-summary-save-article arg)))
10562
10563 (defun gnus-summary-muttprint (&optional arg)
10564   "Print the current article using Muttprint.
10565 If N is a positive number, save the N next articles.
10566 If N is a negative number, save the N previous articles.
10567 If N is nil and any articles have been marked with the process mark,
10568 save those articles instead."
10569   (interactive "P")
10570   (require 'gnus-art)
10571   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10572     (gnus-summary-save-article arg t)))
10573
10574 (defun gnus-summary-pipe-message (program)
10575   "Pipe the current article through PROGRAM."
10576   (interactive "sProgram: ")
10577   (gnus-summary-select-article)
10578   (let ((mail-header-separator ""))
10579     (gnus-eval-in-buffer-window gnus-article-buffer
10580       (save-restriction
10581         (widen)
10582         (let ((start (window-start))
10583               buffer-read-only)
10584           (message-pipe-buffer-body program)
10585           (set-window-start (get-buffer-window (current-buffer)) start))))))
10586
10587 (defun gnus-get-split-value (methods)
10588   "Return a value based on the split METHODS."
10589   (let (split-name method result match)
10590     (when methods
10591       (save-excursion
10592         (set-buffer gnus-original-article-buffer)
10593         (save-restriction
10594           (nnheader-narrow-to-headers)
10595           (while (and methods (not split-name))
10596             (goto-char (point-min))
10597             (setq method (pop methods))
10598             (setq match (car method))
10599             (when (cond
10600                    ((stringp match)
10601                     ;; Regular expression.
10602                     (ignore-errors
10603                       (re-search-forward match nil t)))
10604                    ((gnus-functionp match)
10605                     ;; Function.
10606                     (save-restriction
10607                       (widen)
10608                       (setq result (funcall match gnus-newsgroup-name))))
10609                    ((consp match)
10610                     ;; Form.
10611                     (save-restriction
10612                       (widen)
10613                       (setq result (eval match)))))
10614               (setq split-name (cdr method))
10615               (cond ((stringp result)
10616                      (push (expand-file-name
10617                             result gnus-article-save-directory)
10618                            split-name))
10619                     ((consp result)
10620                      (setq split-name (append result split-name)))))))))
10621     (nreverse split-name)))
10622
10623 (defun gnus-valid-move-group-p (group)
10624   (and (boundp group)
10625        (symbol-name group)
10626        (symbol-value group)
10627        (gnus-get-function (gnus-find-method-for-group
10628                            (symbol-name group)) 'request-accept-article t)))
10629
10630 (defun gnus-read-move-group-name (prompt default articles prefix)
10631   "Read a group name."
10632   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10633          (minibuffer-confirm-incomplete nil) ; XEmacs
10634          (prom
10635           (format "%s %s to:"
10636                   prompt
10637                   (if (> (length articles) 1)
10638                       (format "these %d articles" (length articles))
10639                     "this article")))
10640          (to-newsgroup
10641           (cond
10642            ((null split-name)
10643             (gnus-completing-read-with-default
10644              default prom
10645              gnus-active-hashtb
10646              'gnus-valid-move-group-p
10647              nil prefix
10648              'gnus-group-history))
10649            ((= 1 (length split-name))
10650             (gnus-completing-read-with-default
10651              (car split-name) prom
10652              gnus-active-hashtb
10653              'gnus-valid-move-group-p
10654              nil nil
10655              'gnus-group-history))
10656            (t
10657             (gnus-completing-read-with-default
10658              nil prom
10659              (mapcar (lambda (el) (list el))
10660                      (nreverse split-name))
10661              nil nil nil
10662              'gnus-group-history))))
10663          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10664     (when to-newsgroup
10665       (if (or (string= to-newsgroup "")
10666               (string= to-newsgroup prefix))
10667           (setq to-newsgroup default))
10668       (unless to-newsgroup
10669         (error "No group name entered"))
10670       (or (gnus-active to-newsgroup)
10671           (gnus-activate-group to-newsgroup nil nil to-method)
10672           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10673                                      to-newsgroup))
10674               (or (and (gnus-request-create-group to-newsgroup to-method)
10675                        (gnus-activate-group
10676                         to-newsgroup nil nil to-method)
10677                        (gnus-subscribe-group to-newsgroup))
10678                   (error "Couldn't create group %s" to-newsgroup)))
10679           (error "No such group: %s" to-newsgroup)))
10680     to-newsgroup))
10681
10682 (defun gnus-summary-save-parts (type dir n &optional reverse)
10683   "Save parts matching TYPE to DIR.
10684 If REVERSE, save parts that do not match TYPE."
10685   (interactive
10686    (list (read-string "Save parts of type: "
10687                       (or (car gnus-summary-save-parts-type-history)
10688                           gnus-summary-save-parts-default-mime)
10689                       'gnus-summary-save-parts-type-history)
10690          (setq gnus-summary-save-parts-last-directory
10691                (read-file-name "Save to directory: "
10692                                gnus-summary-save-parts-last-directory
10693                                nil t))
10694          current-prefix-arg))
10695   (gnus-summary-iterate n
10696     (let ((gnus-display-mime-function nil)
10697           (gnus-inhibit-treatment t))
10698       (gnus-summary-select-article))
10699     (save-excursion
10700       (set-buffer gnus-article-buffer)
10701       (let ((handles (or gnus-article-mime-handles
10702                          (mm-dissect-buffer nil gnus-article-loose-mime)
10703                          (mm-uu-dissect))))
10704         (when handles
10705           (gnus-summary-save-parts-1 type dir handles reverse)
10706           (unless gnus-article-mime-handles ;; Don't destroy this case.
10707             (mm-destroy-parts handles)))))))
10708
10709 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10710   (if (stringp (car handle))
10711       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10712               (cdr handle))
10713     (when (if reverse
10714               (not (string-match type (mm-handle-media-type handle)))
10715             (string-match type (mm-handle-media-type handle)))
10716       (let ((file (expand-file-name
10717                    (file-name-nondirectory
10718                     (or
10719                      (mail-content-type-get
10720                       (mm-handle-disposition handle) 'filename)
10721                      (concat gnus-newsgroup-name
10722                              "." (number-to-string
10723                                   (cdr gnus-article-current)))))
10724                    dir)))
10725         (unless (file-exists-p file)
10726           (mm-save-part-to-file handle file))))))
10727
10728 ;; Summary extract commands
10729
10730 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10731   (let ((buffer-read-only nil)
10732         (article (gnus-summary-article-number))
10733         after-article b e)
10734     (unless (gnus-summary-goto-subject article)
10735       (error "No such article: %d" article))
10736     (gnus-summary-position-point)
10737     ;; If all commands are to be bunched up on one line, we collect
10738     ;; them here.
10739     (unless gnus-view-pseudos-separately
10740       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10741             files action)
10742         (while ps
10743           (setq action (cdr (assq 'action (car ps))))
10744           (setq files (list (cdr (assq 'name (car ps)))))
10745           (while (and ps (cdr ps)
10746                       (string= (or action "1")
10747                                (or (cdr (assq 'action (cadr ps))) "2")))
10748             (push (cdr (assq 'name (cadr ps))) files)
10749             (setcdr ps (cddr ps)))
10750           (when files
10751             (when (not (string-match "%s" action))
10752               (push " " files))
10753             (push " " files)
10754             (when (assq 'execute (car ps))
10755               (setcdr (assq 'execute (car ps))
10756                       (funcall (if (string-match "%s" action)
10757                                    'format 'concat)
10758                                action
10759                                (mapconcat
10760                                 (lambda (f)
10761                                   (if (equal f " ")
10762                                       f
10763                                     (mm-quote-arg f)))
10764                                 files " ")))))
10765           (setq ps (cdr ps)))))
10766     (if (and gnus-view-pseudos (not not-view))
10767         (while pslist
10768           (when (assq 'execute (car pslist))
10769             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10770                                   (eq gnus-view-pseudos 'not-confirm)))
10771           (setq pslist (cdr pslist)))
10772       (save-excursion
10773         (while pslist
10774           (setq after-article (or (cdr (assq 'article (car pslist)))
10775                                   (gnus-summary-article-number)))
10776           (gnus-summary-goto-subject after-article)
10777           (forward-line 1)
10778           (setq b (point))
10779           (insert "    " (file-name-nondirectory
10780                           (cdr (assq 'name (car pslist))))
10781                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10782           (setq e (point))
10783           (forward-line -1)             ; back to `b'
10784           (gnus-add-text-properties
10785            b (1- e) (list 'gnus-number gnus-reffed-article-number
10786                           gnus-mouse-face-prop gnus-mouse-face))
10787           (gnus-data-enter
10788            after-article gnus-reffed-article-number
10789            gnus-unread-mark b (car pslist) 0 (- e b))
10790           (setq gnus-newsgroup-unreads
10791                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10792                                          gnus-reffed-article-number))
10793           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10794           (setq pslist (cdr pslist)))))))
10795
10796 (defun gnus-pseudos< (p1 p2)
10797   (let ((c1 (cdr (assq 'action p1)))
10798         (c2 (cdr (assq 'action p2))))
10799     (and c1 c2 (string< c1 c2))))
10800
10801 (defun gnus-request-pseudo-article (props)
10802   (cond ((assq 'execute props)
10803          (gnus-execute-command (cdr (assq 'execute props)))))
10804   (let ((gnus-current-article (gnus-summary-article-number)))
10805     (gnus-run-hooks 'gnus-mark-article-hook)))
10806
10807 (defun gnus-execute-command (command &optional automatic)
10808   (save-excursion
10809     (gnus-article-setup-buffer)
10810     (set-buffer gnus-article-buffer)
10811     (setq buffer-read-only nil)
10812     (let ((command (if automatic command
10813                      (read-string "Command: " (cons command 0)))))
10814       (erase-buffer)
10815       (insert "$ " command "\n\n")
10816       (if gnus-view-pseudo-asynchronously
10817           (start-process "gnus-execute" (current-buffer) shell-file-name
10818                          shell-command-switch command)
10819         (call-process shell-file-name nil t nil
10820                       shell-command-switch command)))))
10821
10822 ;; Summary kill commands.
10823
10824 (defun gnus-summary-edit-global-kill (article)
10825   "Edit the \"global\" kill file."
10826   (interactive (list (gnus-summary-article-number)))
10827   (gnus-group-edit-global-kill article))
10828
10829 (defun gnus-summary-edit-local-kill ()
10830   "Edit a local kill file applied to the current newsgroup."
10831   (interactive)
10832   (setq gnus-current-headers (gnus-summary-article-header))
10833   (gnus-group-edit-local-kill
10834    (gnus-summary-article-number) gnus-newsgroup-name))
10835
10836 ;;; Header reading.
10837
10838 (defun gnus-read-header (id &optional header)
10839   "Read the headers of article ID and enter them into the Gnus system."
10840   (let ((group gnus-newsgroup-name)
10841         (gnus-override-method
10842          (or
10843           gnus-override-method
10844           (and (gnus-news-group-p gnus-newsgroup-name)
10845                (car (gnus-refer-article-methods)))))
10846         where)
10847     ;; First we check to see whether the header in question is already
10848     ;; fetched.
10849     (if (stringp id)
10850         ;; This is a Message-ID.
10851         (setq header (or header (gnus-id-to-header id)))
10852       ;; This is an article number.
10853       (setq header (or header (gnus-summary-article-header id))))
10854     (if (and header
10855              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10856         ;; We have found the header.
10857         header
10858       ;; If this is a sparse article, we have to nix out its
10859       ;; previous entry in the thread hashtb.
10860       (when (and header
10861                  (gnus-summary-article-sparse-p (mail-header-number header)))
10862         (let* ((parent (gnus-parent-id (mail-header-references header)))
10863                (thread (and parent (gnus-id-to-thread parent))))
10864           (when thread
10865             (delq (assq header thread) thread))))
10866       ;; We have to really fetch the header to this article.
10867       (save-excursion
10868         (set-buffer nntp-server-buffer)
10869         (when (setq where (gnus-request-head id group))
10870           (nnheader-fold-continuation-lines)
10871           (goto-char (point-max))
10872           (insert ".\n")
10873           (goto-char (point-min))
10874           (insert "211 ")
10875           (princ (cond
10876                   ((numberp id) id)
10877                   ((cdr where) (cdr where))
10878                   (header (mail-header-number header))
10879                   (t gnus-reffed-article-number))
10880                  (current-buffer))
10881           (insert " Article retrieved.\n"))
10882         (if (or (not where)
10883                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10884             ()                          ; Malformed head.
10885           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10886             (when (and (stringp id)
10887                        (not (string= (gnus-group-real-name group)
10888                                      (car where))))
10889               ;; If we fetched by Message-ID and the article came
10890               ;; from a different group, we fudge some bogus article
10891               ;; numbers for this article.
10892               (mail-header-set-number header gnus-reffed-article-number))
10893             (save-excursion
10894               (set-buffer gnus-summary-buffer)
10895               (decf gnus-reffed-article-number)
10896               (gnus-remove-header (mail-header-number header))
10897               (push header gnus-newsgroup-headers)
10898               (setq gnus-current-headers header)
10899               (push (mail-header-number header) gnus-newsgroup-limit)))
10900           header)))))
10901
10902 (defun gnus-remove-header (number)
10903   "Remove header NUMBER from `gnus-newsgroup-headers'."
10904   (if (and gnus-newsgroup-headers
10905            (= number (mail-header-number (car gnus-newsgroup-headers))))
10906       (pop gnus-newsgroup-headers)
10907     (let ((headers gnus-newsgroup-headers))
10908       (while (and (cdr headers)
10909                   (not (= number (mail-header-number (cadr headers)))))
10910         (pop headers))
10911       (when (cdr headers)
10912         (setcdr headers (cddr headers))))))
10913
10914 ;;;
10915 ;;; summary highlights
10916 ;;;
10917
10918 (defun gnus-highlight-selected-summary ()
10919   "Highlight selected article in summary buffer."
10920   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10921   (when gnus-summary-selected-face
10922     (save-excursion
10923       (let* ((beg (progn (beginning-of-line) (point)))
10924              (end (progn (end-of-line) (point)))
10925              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10926              (from (if (get-text-property beg gnus-mouse-face-prop)
10927                        beg
10928                      (or (next-single-property-change
10929                           beg gnus-mouse-face-prop nil end)
10930                          beg)))
10931              (to
10932               (if (= from end)
10933                   (- from 2)
10934                 (or (next-single-property-change
10935                      from gnus-mouse-face-prop nil end)
10936                     end))))
10937         ;; If no mouse-face prop on line we will have to = from = end,
10938         ;; so we highlight the entire line instead.
10939         (when (= (+ to 2) from)
10940           (setq from beg)
10941           (setq to end))
10942         (if gnus-newsgroup-selected-overlay
10943             ;; Move old overlay.
10944             (gnus-move-overlay
10945              gnus-newsgroup-selected-overlay from to (current-buffer))
10946           ;; Create new overlay.
10947           (gnus-overlay-put
10948            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10949            'face gnus-summary-selected-face))))))
10950
10951 (defvar gnus-summary-highlight-line-cached nil)
10952 (defvar gnus-summary-highlight-line-trigger nil)
10953
10954 (defun gnus-summary-highlight-line-0 ()
10955   (if (and (eq gnus-summary-highlight-line-trigger 
10956                gnus-summary-highlight)
10957            gnus-summary-highlight-line-cached)
10958       gnus-summary-highlight-line-cached
10959     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10960           gnus-summary-highlight-line-cached
10961           (let* ((cond (list 'cond))
10962                  (c cond)
10963                  (list gnus-summary-highlight))
10964             (while list
10965               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10966               (setq c (cdr c)
10967                     list (cdr list)))
10968             (gnus-byte-compile (list 'lambda nil cond))))))
10969
10970 (defun gnus-summary-highlight-line ()
10971   "Highlight current line according to `gnus-summary-highlight'."
10972   (let* ((beg (gnus-point-at-bol))
10973          (article (or (gnus-summary-article-number) gnus-current-article))
10974          (score (or (cdr (assq article
10975                                gnus-newsgroup-scored))
10976                     gnus-summary-default-score 0))
10977          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10978          (inhibit-read-only t)
10979          (default gnus-summary-default-score)
10980          (default-high gnus-summary-default-high-score)
10981          (default-low gnus-summary-default-low-score)
10982          (uncached (memq article gnus-newsgroup-undownloaded))
10983          (downloaded (not uncached)))
10984     (let ((face (funcall (gnus-summary-highlight-line-0))))
10985       (unless (eq face (get-text-property beg 'face))
10986         (gnus-put-text-property-excluding-characters-with-faces
10987          beg (gnus-point-at-eol) 'face
10988          (setq face (if (boundp face) (symbol-value face) face)))
10989         (when gnus-summary-highlight-line-function
10990           (funcall gnus-summary-highlight-line-function article face))))))
10991
10992 (defun gnus-update-read-articles (group unread &optional compute)
10993   "Update the list of read articles in GROUP.
10994 UNREAD is a sorted list."
10995   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10996          (entry (gnus-gethash group gnus-newsrc-hashtb))
10997          (info (nth 2 entry))
10998          (prev 1)
10999          read)
11000     (if (or (not info) (not active))
11001         ;; There is no info on this group if it was, in fact,
11002         ;; killed.  Gnus stores no information on killed groups, so
11003         ;; there's nothing to be done.
11004         ;; One could store the information somewhere temporarily,
11005         ;; perhaps...  Hmmm...
11006         ()
11007       ;; Remove any negative articles numbers.
11008       (while (and unread (< (car unread) 0))
11009         (setq unread (cdr unread)))
11010       ;; Remove any expired article numbers
11011       (while (and unread (< (car unread) (car active)))
11012         (setq unread (cdr unread)))
11013       ;; Compute the ranges of read articles by looking at the list of
11014       ;; unread articles.
11015       (while unread
11016         (when (/= (car unread) prev)
11017           (push (if (= prev (1- (car unread))) prev
11018                   (cons prev (1- (car unread))))
11019                 read))
11020         (setq prev (1+ (car unread)))
11021         (setq unread (cdr unread)))
11022       (when (<= prev (cdr active))
11023         (push (cons prev (cdr active)) read))
11024       (setq read (if (> (length read) 1) (nreverse read) read))
11025       (if compute
11026           read
11027         (save-excursion
11028           (let (setmarkundo)
11029             ;; Propagate the read marks to the backend.
11030             (when (gnus-check-backend-function 'request-set-mark group)
11031               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11032                     (add (gnus-remove-from-range read (gnus-info-read info))))
11033                 (when (or add del)
11034                   (unless (gnus-check-group group)
11035                     (error "Can't open server for %s" group))
11036                   (gnus-request-set-mark
11037                    group (delq nil (list (if add (list add 'add '(read)))
11038                                          (if del (list del 'del '(read))))))
11039                   (setq setmarkundo
11040                         `(gnus-request-set-mark
11041                           ,group
11042                           ',(delq nil (list
11043                                        (if del (list del 'add '(read)))
11044                                        (if add (list add 'del '(read))))))))))
11045             (set-buffer gnus-group-buffer)
11046             (gnus-undo-register
11047               `(progn
11048                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11049                  (gnus-info-set-read ',info ',(gnus-info-read info))
11050                  (gnus-get-unread-articles-in-group ',info
11051                                                     (gnus-active ,group))
11052                  (gnus-group-update-group ,group t)
11053                  ,setmarkundo))))
11054         ;; Enter this list into the group info.
11055         (gnus-info-set-read info read)
11056         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11057         (gnus-get-unread-articles-in-group info (gnus-active group))
11058         t))))
11059
11060 (defun gnus-offer-save-summaries ()
11061   "Offer to save all active summary buffers."
11062   (let (buffers)
11063     ;; Go through all buffers and find all summaries.
11064     (dolist (buffer (buffer-list))
11065       (when (and (setq buffer (buffer-name buffer))
11066                  (string-match "Summary" buffer)
11067                  (save-excursion
11068                    (set-buffer buffer)
11069                    ;; We check that this is, indeed, a summary buffer.
11070                    (and (eq major-mode 'gnus-summary-mode)
11071                         ;; Also make sure this isn't bogus.
11072                         gnus-newsgroup-prepared
11073                         ;; Also make sure that this isn't a
11074                         ;; dead summary buffer.
11075                         (not gnus-dead-summary-mode))))
11076         (push buffer buffers)))
11077     ;; Go through all these summary buffers and offer to save them.
11078     (when buffers
11079       (save-excursion
11080         (map-y-or-n-p
11081          "Update summary buffer %s? "
11082          (lambda (buf)
11083            (switch-to-buffer buf)
11084            (gnus-summary-exit))
11085          buffers)))))
11086
11087 (defun gnus-summary-setup-default-charset ()
11088   "Setup newsgroup default charset."
11089   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11090       (setq gnus-newsgroup-charset nil)
11091     (let* ((ignored-charsets
11092             (or gnus-newsgroup-ephemeral-ignored-charsets
11093                 (append
11094                  (and gnus-newsgroup-name
11095                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11096                  gnus-newsgroup-ignored-charsets))))
11097       (setq gnus-newsgroup-charset
11098             (or gnus-newsgroup-ephemeral-charset
11099                 (and gnus-newsgroup-name
11100                      (gnus-parameter-charset gnus-newsgroup-name))
11101                 gnus-default-charset))
11102       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11103            ignored-charsets))))
11104
11105 ;;;
11106 ;;; Mime Commands
11107 ;;;
11108
11109 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11110   "Display the current article buffer fully MIME-buttonized.
11111 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11112 treated as multipart/mixed."
11113   (interactive "P")
11114   (require 'gnus-art)
11115   (let ((gnus-unbuttonized-mime-types nil)
11116         (gnus-mime-display-multipart-as-mixed show-all-parts))
11117     (gnus-summary-show-article)))
11118
11119 (defun gnus-summary-repair-multipart (article)
11120   "Add a Content-Type header to a multipart article without one."
11121   (interactive (list (gnus-summary-article-number)))
11122   (gnus-with-article article
11123     (message-narrow-to-head)
11124     (message-remove-header "Mime-Version")
11125     (goto-char (point-max))
11126     (insert "Mime-Version: 1.0\n")
11127     (widen)
11128     (when (search-forward "\n--" nil t)
11129       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11130         (message-narrow-to-head)
11131         (message-remove-header "Content-Type")
11132         (goto-char (point-max))
11133         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11134                         separator))
11135         (widen))))
11136   (let (gnus-mark-article-hook)
11137     (gnus-summary-select-article t t nil article)))
11138
11139 (defun gnus-summary-toggle-display-buttonized ()
11140   "Toggle the buttonizing of the article buffer."
11141   (interactive)
11142   (require 'gnus-art)
11143   (if (setq gnus-inhibit-mime-unbuttonizing
11144             (not gnus-inhibit-mime-unbuttonizing))
11145       (let ((gnus-unbuttonized-mime-types nil))
11146         (gnus-summary-show-article))
11147     (gnus-summary-show-article)))
11148
11149 ;;;
11150 ;;; Generic summary marking commands
11151 ;;;
11152
11153 (defvar gnus-summary-marking-alist
11154   '((read gnus-del-mark "d")
11155     (unread gnus-unread-mark "u")
11156     (ticked gnus-ticked-mark "!")
11157     (dormant gnus-dormant-mark "?")
11158     (expirable gnus-expirable-mark "e"))
11159   "An alist of names/marks/keystrokes.")
11160
11161 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11162 (defvar gnus-summary-mark-map)
11163
11164 (defun gnus-summary-make-all-marking-commands ()
11165   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11166   (dolist (elem gnus-summary-marking-alist)
11167     (apply 'gnus-summary-make-marking-command elem)))
11168
11169 (defun gnus-summary-make-marking-command (name mark keystroke)
11170   (let ((map (make-sparse-keymap)))
11171     (define-key gnus-summary-generic-mark-map keystroke map)
11172     (dolist (lway `((next "next" next nil "n")
11173                     (next-unread "next unread" next t "N")
11174                     (prev "previous" prev nil "p")
11175                     (prev-unread "previous unread" prev t "P")
11176                     (nomove "" nil nil ,keystroke)))
11177       (let ((func (gnus-summary-make-marking-command-1
11178                    mark (car lway) lway name)))
11179         (setq func (eval func))
11180         (define-key map (nth 4 lway) func)))))
11181
11182 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11183   `(defun ,(intern
11184             (format "gnus-summary-put-mark-as-%s%s"
11185                     name (if (eq way 'nomove)
11186                              ""
11187                            (concat "-" (symbol-name way)))))
11188      (n)
11189      ,(format
11190        "Mark the current article as %s%s.
11191 If N, the prefix, then repeat N times.
11192 If N is negative, move in reverse order.
11193 The difference between N and the actual number of articles marked is
11194 returned."
11195        name (cadr lway))
11196      (interactive "p")
11197      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11198
11199 (defun gnus-summary-generic-mark (n mark move unread)
11200   "Mark N articles with MARK."
11201   (unless (eq major-mode 'gnus-summary-mode)
11202     (error "This command can only be used in the summary buffer"))
11203   (gnus-summary-show-thread)
11204   (let ((nummove
11205          (cond
11206           ((eq move 'next) 1)
11207           ((eq move 'prev) -1)
11208           (t 0))))
11209     (if (zerop nummove)
11210         (setq n 1)
11211       (when (< n 0)
11212         (setq n (abs n)
11213               nummove (* -1 nummove))))
11214     (while (and (> n 0)
11215                 (gnus-summary-mark-article nil mark)
11216                 (zerop (gnus-summary-next-subject nummove unread t)))
11217       (setq n (1- n)))
11218     (when (/= 0 n)
11219       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11220     (gnus-summary-recenter)
11221     (gnus-summary-position-point)
11222     (gnus-set-mode-line 'summary)
11223     n))
11224
11225 (defun gnus-summary-insert-articles (articles)
11226   (when (setq articles
11227               (gnus-sorted-difference articles
11228                                       (mapcar (lambda (h)
11229                                                 (mail-header-number h))
11230                                               gnus-newsgroup-headers)))
11231     (setq gnus-newsgroup-headers
11232           (merge 'list
11233                  gnus-newsgroup-headers
11234                  (gnus-fetch-headers articles)
11235                  'gnus-article-sort-by-number))
11236     ;; Suppress duplicates?
11237     (when gnus-suppress-duplicates
11238       (gnus-dup-suppress-articles))
11239
11240     ;; We might want to build some more threads first.
11241     (when (and gnus-fetch-old-headers
11242                (eq gnus-headers-retrieved-by 'nov))
11243       (if (eq gnus-fetch-old-headers 'invisible)
11244           (gnus-build-all-threads)
11245         (gnus-build-old-threads)))
11246     ;; Let the Gnus agent mark articles as read.
11247     (when gnus-agent
11248       (gnus-agent-get-undownloaded-list))
11249     ;; Remove list identifiers from subject
11250     (when gnus-list-identifiers
11251       (gnus-summary-remove-list-identifiers))
11252     ;; First and last article in this newsgroup.
11253     (when gnus-newsgroup-headers
11254       (setq gnus-newsgroup-begin
11255             (mail-header-number (car gnus-newsgroup-headers))
11256             gnus-newsgroup-end
11257             (mail-header-number
11258              (gnus-last-element gnus-newsgroup-headers))))
11259     (when gnus-use-scoring
11260       (gnus-possibly-score-headers))))
11261
11262 (defun gnus-summary-insert-old-articles (&optional all)
11263   "Insert all old articles in this group.
11264 If ALL is non-nil, already read articles become readable.
11265 If ALL is a number, fetch this number of articles."
11266   (interactive "P")
11267   (prog1
11268       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11269             older len)
11270         (setq older
11271               ;; Some nntp servers lie about their active range.  When
11272               ;; this happens, the active range can be in the millions.
11273               ;; Use a compressed range to avoid creating a huge list.
11274               (gnus-range-difference (list gnus-newsgroup-active) old))
11275         (setq len (gnus-range-length older))
11276         (cond
11277          ((null older) nil)
11278          ((numberp all)
11279           (if (< all len)
11280               (let ((older-range (nreverse older)))
11281                 (setq older nil)
11282
11283                 (while (> all 0)
11284                   (let* ((r (pop older-range))
11285                          (min (if (numberp r) r (car r)))
11286                          (max (if (numberp r) r (cdr r))))
11287                     (while (and (<= min max)
11288                                 (> all 0))
11289                       (push max older)
11290                       (setq all (1- all)
11291                             max (1- max))))))
11292             (setq older (gnus-uncompress-range older))))
11293          (all
11294           (setq older (gnus-uncompress-range older)))
11295          (t
11296           (when (and (numberp gnus-large-newsgroup)
11297                    (> len gnus-large-newsgroup))
11298               (let* ((cursor-in-echo-area nil)
11299                      (initial (gnus-parameter-large-newsgroup-initial
11300                                gnus-newsgroup-name))
11301                      (input
11302                       (read-string
11303                        (format
11304                         "How many articles from %s (%s %d): "
11305                         (gnus-limit-string
11306                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11307                         (if initial "max" "default")
11308                         len)
11309                        (if initial
11310                            (cons (number-to-string initial)
11311                                  0)))))
11312                 (unless (string-match "^[ \t]*$" input)
11313                   (setq all (string-to-number input))
11314                   (if (< all len)
11315                       (let ((older-range (nreverse older)))
11316                         (setq older nil)
11317
11318                         (while (> all 0)
11319                           (let* ((r (pop older-range))
11320                                  (min (if (numberp r) r (car r)))
11321                                  (max (if (numberp r) r (cdr r))))
11322                             (while (and (<= min max)
11323                                         (> all 0))
11324                               (push max older)
11325                               (setq all (1- all)
11326                                     max (1- max))))))))))
11327           (setq older (gnus-uncompress-range older))))
11328         (if (not older)
11329             (message "No old news.")
11330           (gnus-summary-insert-articles older)
11331           (gnus-summary-limit (gnus-sorted-nunion old older))))
11332     (gnus-summary-position-point)))
11333
11334 (defun gnus-summary-insert-new-articles ()
11335   "Insert all new articles in this group."
11336   (interactive)
11337   (prog1
11338       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11339             (old-active gnus-newsgroup-active)
11340             (nnmail-fetched-sources (list t))
11341             i new)
11342         (setq gnus-newsgroup-active
11343               (gnus-activate-group gnus-newsgroup-name 'scan))
11344         (setq i (cdr gnus-newsgroup-active))
11345         (while (> i (cdr old-active))
11346           (push i new)
11347           (decf i))
11348         (if (not new)
11349             (message "No gnus is bad news.")
11350           (gnus-summary-insert-articles new)
11351           (setq gnus-newsgroup-unreads
11352                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11353           (gnus-summary-limit (gnus-sorted-nunion old new))))
11354     (gnus-summary-position-point)))
11355
11356 (gnus-summary-make-all-marking-commands)
11357
11358 (gnus-ems-redefine)
11359
11360 (provide 'gnus-sum)
11361
11362 (run-hooks 'gnus-sum-load-hook)
11363
11364 ;;; gnus-sum.el ends here