0aa82ee13fa47457c0f3ed5c2bd9069cec9212ac
[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 nil
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   :options '(gnus-agent-fetch-selected-article)
798   :type 'hook)
799
800 (defcustom gnus-visual-mark-article-hook
801   (list 'gnus-highlight-selected-summary)
802   "*Hook run after selecting an article in the summary buffer.
803 It is meant to be used for highlighting the article in some way.  It
804 is not run if `gnus-visual' is nil."
805   :group 'gnus-summary-visual
806   :type 'hook)
807
808 (defcustom gnus-parse-headers-hook nil
809   "*A hook called before parsing the headers."
810   :group 'gnus-various
811   :type 'hook)
812
813 (defcustom gnus-exit-group-hook nil
814   "*A hook called when exiting summary mode.
815 This hook is not called from the non-updating exit commands like `Q'."
816   :group 'gnus-various
817   :type 'hook)
818
819 (defcustom gnus-summary-update-hook
820   (list 'gnus-summary-highlight-line)
821   "*A hook called when a summary line is changed.
822 The hook will not be called if `gnus-visual' is nil.
823
824 The default function `gnus-summary-highlight-line' will
825 highlight the line according to the `gnus-summary-highlight'
826 variable."
827   :group 'gnus-summary-visual
828   :type 'hook)
829
830 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
831   "*A hook called when an article is selected for the first time.
832 The hook is intended to mark an article as read (or unread)
833 automatically when it is selected."
834   :group 'gnus-summary-choose
835   :type 'hook)
836
837 (defcustom gnus-group-no-more-groups-hook nil
838   "*A hook run when returning to group mode having no more (unread) groups."
839   :group 'gnus-group-select
840   :type 'hook)
841
842 (defcustom gnus-ps-print-hook nil
843   "*A hook run before ps-printing something from Gnus."
844   :group 'gnus-summary
845   :type 'hook)
846
847 (defcustom gnus-summary-display-arrow
848   (and (fboundp 'display-graphic-p)
849        (display-graphic-p))
850   "*If non-nil, display an arrow highlighting the current article."
851   :version "21.1"
852   :group 'gnus-summary
853   :type 'boolean)
854
855 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
856   "Face used for highlighting the current article in the summary buffer."
857   :group 'gnus-summary-visual
858   :type 'face)
859
860 (defvar gnus-tmp-downloaded nil)
861
862 (defcustom gnus-summary-highlight
863   '(((eq mark gnus-canceled-mark)
864      . gnus-summary-cancelled-face)
865     ((and uncached (> score default-high))
866      . gnus-summary-high-undownloaded-face)
867     ((and uncached (< score default-low))
868      . gnus-summary-low-undownloaded-face)
869     (uncached
870      . gnus-summary-normal-undownloaded-face)
871     ((and (> score default-high)
872           (or (eq mark gnus-dormant-mark)
873               (eq mark gnus-ticked-mark)))
874      . gnus-summary-high-ticked-face)
875     ((and (< score default-low)
876           (or (eq mark gnus-dormant-mark)
877               (eq mark gnus-ticked-mark)))
878      . gnus-summary-low-ticked-face)
879     ((or (eq mark gnus-dormant-mark)
880          (eq mark gnus-ticked-mark))
881      . gnus-summary-normal-ticked-face)
882     ((and (> score default-high) (eq mark gnus-ancient-mark))
883      . gnus-summary-high-ancient-face)
884     ((and (< score default-low) (eq mark gnus-ancient-mark))
885      . gnus-summary-low-ancient-face)
886     ((eq mark gnus-ancient-mark)
887      . gnus-summary-normal-ancient-face)
888     ((and (> score default-high) (eq mark gnus-unread-mark))
889      . gnus-summary-high-unread-face)
890     ((and (< score default-low) (eq mark gnus-unread-mark))
891      . gnus-summary-low-unread-face)
892     ((eq mark gnus-unread-mark)
893      . gnus-summary-normal-unread-face)
894     ((> score default-high)
895      . gnus-summary-high-read-face)
896     ((< score default-low)
897      . gnus-summary-low-read-face)
898     (t
899      . gnus-summary-normal-read-face))
900   "*Controls the highlighting of summary buffer lines.
901
902 A list of (FORM . FACE) pairs.  When deciding how a a particular
903 summary line should be displayed, each form is evaluated.  The content
904 of the face field after the first true form is used.  You can change
905 how those summary lines are displayed, by editing the face field.
906
907 You can use the following variables in the FORM field.
908
909 score:        The article's score
910 default:      The default article score.
911 default-high: The default score for high scored articles.
912 default-low:  The default score for low scored articles.
913 below:        The score below which articles are automatically marked as read.
914 mark:         The articles mark."
915   :group 'gnus-summary-visual
916   :type '(repeat (cons (sexp :tag "Form" nil)
917                        face)))
918
919 (defcustom gnus-alter-header-function nil
920   "Function called to allow alteration of article header structures.
921 The function is called with one parameter, the article header vector,
922 which it may alter in any way.")
923
924 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
925   "Variable that says which function should be used to decode a string with encoded words.")
926
927 (defcustom gnus-extra-headers '(To Newsgroups)
928   "*Extra headers to parse."
929   :version "21.1"
930   :group 'gnus-summary
931   :type '(repeat symbol))
932
933 (defcustom gnus-ignored-from-addresses
934   (and user-mail-address (regexp-quote user-mail-address))
935   "*Regexp of From headers that may be suppressed in favor of To headers."
936   :version "21.1"
937   :group 'gnus-summary
938   :type 'regexp)
939
940 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
941   "List of charsets that should be ignored.
942 When these charsets are used in the \"charset\" parameter, the
943 default charset will be used instead."
944   :version "21.1"
945   :type '(repeat symbol)
946   :group 'gnus-charset)
947
948 (gnus-define-group-parameter
949  ignored-charsets
950  :type list
951  :function-document
952  "Return the ignored charsets of GROUP."
953  :variable gnus-group-ignored-charsets-alist
954  :variable-default
955  '(("alt\\.chinese\\.text" iso-8859-1))
956  :variable-document
957  "Alist of regexps (to match group names) and charsets that should be ignored.
958 When these charsets are used in the \"charset\" parameter, the
959 default charset will be used instead."
960  :variable-group gnus-charset
961  :variable-type '(repeat (cons (regexp :tag "Group")
962                                (repeat symbol)))
963  :parameter-type '(choice :tag "Ignored charsets"
964                           :value nil
965                           (repeat (symbol)))
966  :parameter-document       "\
967 List of charsets that should be ignored.
968
969 When these charsets are used in the \"charset\" parameter, the
970 default charset will be used instead.")
971
972 (defcustom gnus-group-highlight-words-alist nil
973   "Alist of group regexps and highlight regexps.
974 This variable uses the same syntax as `gnus-emphasis-alist'."
975   :version "21.1"
976   :type '(repeat (cons (regexp :tag "Group")
977                        (repeat (list (regexp :tag "Highlight regexp")
978                                      (number :tag "Group for entire word" 0)
979                                      (number :tag "Group for displayed part" 0)
980                                      (symbol :tag "Face"
981                                              gnus-emphasis-highlight-words)))))
982   :group 'gnus-summary-visual)
983
984 (defcustom gnus-summary-show-article-charset-alist
985   nil
986   "Alist of number and charset.
987 The article will be shown with the charset corresponding to the
988 numbered argument.
989 For example: ((1 . cn-gb-2312) (2 . big5))."
990   :version "21.1"
991   :type '(repeat (cons (number :tag "Argument" 1)
992                        (symbol :tag "Charset")))
993   :group 'gnus-charset)
994
995 (defcustom gnus-preserve-marks t
996   "Whether marks are preserved when moving, copying and respooling messages."
997   :version "21.1"
998   :type 'boolean
999   :group 'gnus-summary-marks)
1000
1001 (defcustom gnus-alter-articles-to-read-function nil
1002   "Function to be called to alter the list of articles to be selected."
1003   :type '(choice (const nil) function)
1004   :group 'gnus-summary)
1005
1006 (defcustom gnus-orphan-score nil
1007   "*All orphans get this score added.  Set in the score file."
1008   :group 'gnus-score-default
1009   :type '(choice (const nil)
1010                  integer))
1011
1012 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1013   "*A regexp to match MIME parts when saving multiple parts of a message
1014 with gnus-summary-save-parts (X m). This regexp will be used by default
1015 when prompting the user for which type of files to save."
1016   :group 'gnus-summary
1017   :type 'regexp)
1018
1019 (defcustom gnus-read-all-available-headers nil
1020   "Whether Gnus should parse all headers made available to it.
1021 This is mostly relevant for slow backends where the user may
1022 wish to widen the summary buffer to include all headers
1023 that were fetched.  Say, for nnultimate groups."
1024   :group 'gnus-summary
1025   :type '(choice boolean regexp))
1026
1027 (defcustom gnus-summary-muttprint-program "muttprint"
1028   "Command (and optional arguments) used to run Muttprint."
1029   :version "21.3"
1030   :group 'gnus-summary
1031   :type 'string)
1032
1033 (defcustom gnus-article-loose-mime nil
1034   "If non-nil, don't require MIME-Version header.
1035 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1036 supply the MIME-Version header or deliberately strip it From the mail.
1037 Set it to non-nil, Gnus will treat some articles as MIME even if
1038 the MIME-Version header is missed."
1039   :version "21.3"
1040   :type 'boolean
1041   :group 'gnus-article)
1042
1043 ;;; Internal variables
1044
1045 (defvar gnus-summary-display-cache nil)
1046 (defvar gnus-article-mime-handles nil)
1047 (defvar gnus-article-decoded-p nil)
1048 (defvar gnus-article-charset nil)
1049 (defvar gnus-article-ignored-charsets nil)
1050 (defvar gnus-scores-exclude-files nil)
1051 (defvar gnus-page-broken nil)
1052
1053 (defvar gnus-original-article nil)
1054 (defvar gnus-article-internal-prepare-hook nil)
1055 (defvar gnus-newsgroup-process-stack nil)
1056
1057 (defvar gnus-thread-indent-array nil)
1058 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1059 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1060   "Function called to sort the articles within a thread after it has been gathered together.")
1061
1062 (defvar gnus-summary-save-parts-type-history nil)
1063 (defvar gnus-summary-save-parts-last-directory nil)
1064
1065 ;; Avoid highlighting in kill files.
1066 (defvar gnus-summary-inhibit-highlight nil)
1067 (defvar gnus-newsgroup-selected-overlay nil)
1068 (defvar gnus-inhibit-limiting nil)
1069 (defvar gnus-newsgroup-adaptive-score-file nil)
1070 (defvar gnus-current-score-file nil)
1071 (defvar gnus-current-move-group nil)
1072 (defvar gnus-current-copy-group nil)
1073 (defvar gnus-current-crosspost-group nil)
1074 (defvar gnus-newsgroup-display nil)
1075
1076 (defvar gnus-newsgroup-dependencies nil)
1077 (defvar gnus-newsgroup-adaptive nil)
1078 (defvar gnus-summary-display-article-function nil)
1079 (defvar gnus-summary-highlight-line-function nil
1080   "Function called after highlighting a summary line.")
1081
1082 (defvar gnus-summary-line-format-alist
1083   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1084     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1085     (?s gnus-tmp-subject-or-nil ?s)
1086     (?n gnus-tmp-name ?s)
1087     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1088         ?s)
1089     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1090             gnus-tmp-from) ?s)
1091     (?F gnus-tmp-from ?s)
1092     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1093     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1094     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1095     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1096     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1097     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1098     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1099     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1100     (?L gnus-tmp-lines ?s)
1101     (?O gnus-tmp-downloaded ?c)
1102     (?I gnus-tmp-indentation ?s)
1103     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1104     (?R gnus-tmp-replied ?c)
1105     (?\[ gnus-tmp-opening-bracket ?c)
1106     (?\] gnus-tmp-closing-bracket ?c)
1107     (?\> (make-string gnus-tmp-level ? ) ?s)
1108     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1109     (?i gnus-tmp-score ?d)
1110     (?z gnus-tmp-score-char ?c)
1111     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1112     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1113     (?U gnus-tmp-unread ?c)
1114     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1115         ?s)
1116     (?t (gnus-summary-number-of-articles-in-thread
1117          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1118         ?d)
1119     (?e (gnus-summary-number-of-articles-in-thread
1120          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1121         ?c)
1122     (?u gnus-tmp-user-defined ?s)
1123     (?P (gnus-pick-line-number) ?d)
1124     (?B gnus-tmp-thread-tree-header-string ?s)
1125     (user-date (gnus-user-date
1126                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1127   "An alist of format specifications that can appear in summary lines.
1128 These are paired with what variables they correspond with, along with
1129 the type of the variable (string, integer, character, etc).")
1130
1131 (defvar gnus-summary-dummy-line-format-alist
1132   `((?S gnus-tmp-subject ?s)
1133     (?N gnus-tmp-number ?d)
1134     (?u gnus-tmp-user-defined ?s)))
1135
1136 (defvar gnus-summary-mode-line-format-alist
1137   `((?G gnus-tmp-group-name ?s)
1138     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1139     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1140     (?A gnus-tmp-article-number ?d)
1141     (?Z gnus-tmp-unread-and-unselected ?s)
1142     (?V gnus-version ?s)
1143     (?U gnus-tmp-unread-and-unticked ?d)
1144     (?S gnus-tmp-subject ?s)
1145     (?e gnus-tmp-unselected ?d)
1146     (?u gnus-tmp-user-defined ?s)
1147     (?d (length gnus-newsgroup-dormant) ?d)
1148     (?t (length gnus-newsgroup-marked) ?d)
1149     (?h (length gnus-newsgroup-spam-marked) ?d)
1150     (?r (length gnus-newsgroup-reads) ?d)
1151     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1152     (?E gnus-newsgroup-expunged-tally ?d)
1153     (?s (gnus-current-score-file-nondirectory) ?s)))
1154
1155 (defvar gnus-last-search-regexp nil
1156   "Default regexp for article search command.")
1157
1158 (defvar gnus-last-shell-command nil
1159   "Default shell command on article.")
1160
1161 (defvar gnus-newsgroup-agentized nil
1162   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1163 (defvar gnus-newsgroup-begin nil)
1164 (defvar gnus-newsgroup-end nil)
1165 (defvar gnus-newsgroup-last-rmail nil)
1166 (defvar gnus-newsgroup-last-mail nil)
1167 (defvar gnus-newsgroup-last-folder nil)
1168 (defvar gnus-newsgroup-last-file nil)
1169 (defvar gnus-newsgroup-auto-expire nil)
1170 (defvar gnus-newsgroup-active nil)
1171
1172 (defvar gnus-newsgroup-data nil)
1173 (defvar gnus-newsgroup-data-reverse nil)
1174 (defvar gnus-newsgroup-limit nil)
1175 (defvar gnus-newsgroup-limits nil)
1176
1177 (defvar gnus-newsgroup-unreads nil
1178   "Sorted list of unread articles in the current newsgroup.")
1179
1180 (defvar gnus-newsgroup-unselected nil
1181   "Sorted list of unselected unread articles in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-reads nil
1184   "Alist of read articles and article marks in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-expunged-tally nil)
1187
1188 (defvar gnus-newsgroup-marked nil
1189   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1190
1191 (defvar gnus-newsgroup-spam-marked nil
1192   "List of ranges of articles that have been marked as spam.")
1193
1194 (defvar gnus-newsgroup-killed nil
1195   "List of ranges of articles that have been through the scoring process.")
1196
1197 (defvar gnus-newsgroup-cached nil
1198   "Sorted list of articles that come from the article cache.")
1199
1200 (defvar gnus-newsgroup-saved nil
1201   "List of articles that have been saved.")
1202
1203 (defvar gnus-newsgroup-kill-headers nil)
1204
1205 (defvar gnus-newsgroup-replied nil
1206   "List of articles that have been replied to in the current newsgroup.")
1207
1208 (defvar gnus-newsgroup-forwarded nil
1209   "List of articles that have been forwarded in the current newsgroup.")
1210
1211 (defvar gnus-newsgroup-recent nil
1212   "List of articles that have are recent in the current newsgroup.")
1213
1214 (defvar gnus-newsgroup-expirable nil
1215   "Sorted list of articles in the current newsgroup that can be expired.")
1216
1217 (defvar gnus-newsgroup-processable nil
1218   "List of articles in the current newsgroup that can be processed.")
1219
1220 (defvar gnus-newsgroup-downloadable nil
1221   "Sorted list of articles in the current newsgroup that can be processed.")
1222
1223 (defvar gnus-newsgroup-undownloaded nil
1224   "List of articles in the current newsgroup that haven't been downloaded..")
1225
1226 (defvar gnus-newsgroup-unsendable nil
1227   "List of articles in the current newsgroup that won't be sent.")
1228
1229 (defvar gnus-newsgroup-bookmarks nil
1230   "List of articles in the current newsgroup that have bookmarks.")
1231
1232 (defvar gnus-newsgroup-dormant nil
1233   "Sorted list of dormant articles in the current newsgroup.")
1234
1235 (defvar gnus-newsgroup-unseen nil
1236   "List of unseen articles in the current newsgroup.")
1237
1238 (defvar gnus-newsgroup-seen nil
1239   "Range of seen articles in the current newsgroup.")
1240
1241 (defvar gnus-newsgroup-articles nil
1242   "List of articles in the current newsgroup.")
1243
1244 (defvar gnus-newsgroup-scored nil
1245   "List of scored articles in the current newsgroup.")
1246
1247 (defvar gnus-newsgroup-headers nil
1248   "List of article headers in the current newsgroup.")
1249
1250 (defvar gnus-newsgroup-threads nil)
1251
1252 (defvar gnus-newsgroup-prepared nil
1253   "Whether the current group has been prepared properly.")
1254
1255 (defvar gnus-newsgroup-ancient nil
1256   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-sparse nil)
1259
1260 (defvar gnus-current-article nil)
1261 (defvar gnus-article-current nil)
1262 (defvar gnus-current-headers nil)
1263 (defvar gnus-have-all-headers nil)
1264 (defvar gnus-last-article nil)
1265 (defvar gnus-newsgroup-history nil)
1266 (defvar gnus-newsgroup-charset nil)
1267 (defvar gnus-newsgroup-ephemeral-charset nil)
1268 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1269
1270 (defvar gnus-article-before-search nil)
1271
1272 (defconst gnus-summary-local-variables
1273   '(gnus-newsgroup-name
1274     gnus-newsgroup-begin gnus-newsgroup-end
1275     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1276     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1277     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1278     gnus-newsgroup-unselected gnus-newsgroup-marked
1279     gnus-newsgroup-spam-marked
1280     gnus-newsgroup-reads gnus-newsgroup-saved
1281     gnus-newsgroup-replied gnus-newsgroup-forwarded
1282     gnus-newsgroup-recent
1283     gnus-newsgroup-expirable
1284     gnus-newsgroup-processable gnus-newsgroup-killed
1285     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1286     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1287     gnus-newsgroup-seen gnus-newsgroup-articles
1288     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1289     gnus-newsgroup-headers gnus-newsgroup-threads
1290     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1291     gnus-current-article gnus-current-headers gnus-have-all-headers
1292     gnus-last-article gnus-article-internal-prepare-hook
1293     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1294     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1295     gnus-thread-expunge-below
1296     gnus-score-alist gnus-current-score-file
1297     (gnus-summary-expunge-below . global)
1298     (gnus-summary-mark-below . global)
1299     (gnus-orphan-score . global)
1300     gnus-newsgroup-active gnus-scores-exclude-files
1301     gnus-newsgroup-history gnus-newsgroup-ancient
1302     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1303     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1304     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1305     (gnus-newsgroup-expunged-tally . 0)
1306     gnus-cache-removable-articles gnus-newsgroup-cached
1307     gnus-newsgroup-data gnus-newsgroup-data-reverse
1308     gnus-newsgroup-limit gnus-newsgroup-limits
1309     gnus-newsgroup-charset gnus-newsgroup-display)
1310   "Variables that are buffer-local to the summary buffers.")
1311
1312 (defvar gnus-newsgroup-variables nil
1313   "A list of variables that have separate values in different newsgroups.
1314 A list of newsgroup (summary buffer) local variables, or cons of
1315 variables and their default values (when the default values are not
1316 nil), that should be made global while the summary buffer is active.
1317 These variables can be used to set variables in the group parameters
1318 while still allowing them to affect operations done in other
1319 buffers. For example:
1320
1321 \(setq gnus-newsgroup-variables
1322      '(message-use-followup-to
1323        (gnus-visible-headers .
1324          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1325 ")
1326
1327 ;; Byte-compiler warning.
1328 ;(eval-when-compile (defvar gnus-article-mode-map))
1329 (eval-when-compile
1330   (let ((features (cons 'gnus-sum features)))
1331     (require 'gnus)
1332     (require 'gnus-agent)
1333     (require 'gnus-art)))
1334
1335 ;; MIME stuff.
1336
1337 (defvar gnus-decode-encoded-word-methods
1338   '(mail-decode-encoded-word-string)
1339   "List of methods used to decode encoded words.
1340
1341 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1342 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1343 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1344 whose names match REGEXP.
1345
1346 For example:
1347 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1348  mail-decode-encoded-word-string
1349  (\"chinese\" . rfc1843-decode-string))")
1350
1351 (defvar gnus-decode-encoded-word-methods-cache nil)
1352
1353 (defun gnus-multi-decode-encoded-word-string (string)
1354   "Apply the functions from `gnus-encoded-word-methods' that match."
1355   (unless (and gnus-decode-encoded-word-methods-cache
1356                (eq gnus-newsgroup-name
1357                    (car gnus-decode-encoded-word-methods-cache)))
1358     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1359     (mapcar (lambda (x)
1360               (if (symbolp x)
1361                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1362                 (if (and gnus-newsgroup-name
1363                          (string-match (car x) gnus-newsgroup-name))
1364                     (nconc gnus-decode-encoded-word-methods-cache
1365                            (list (cdr x))))))
1366             gnus-decode-encoded-word-methods))
1367   (let ((xlist gnus-decode-encoded-word-methods-cache))
1368     (pop xlist)
1369     (while xlist
1370       (setq string (funcall (pop xlist) string))))
1371   string)
1372
1373 ;; Subject simplification.
1374
1375 (defun gnus-simplify-whitespace (str)
1376   "Remove excessive whitespace from STR."
1377   (let ((mystr str))
1378     ;; Multiple spaces.
1379     (while (string-match "[ \t][ \t]+" mystr)
1380       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1381                           " "
1382                           (substring mystr (match-end 0)))))
1383     ;; Leading spaces.
1384     (when (string-match "^[ \t]+" mystr)
1385       (setq mystr (substring mystr (match-end 0))))
1386     ;; Trailing spaces.
1387     (when (string-match "[ \t]+$" mystr)
1388       (setq mystr (substring mystr 0 (match-beginning 0))))
1389     mystr))
1390
1391 (defun gnus-simplify-all-whitespace (str)
1392   "Remove all whitespace from STR."
1393   (let ((mystr str))
1394     (while (string-match "[ \t\n]+" mystr)
1395       (setq mystr (replace-match "" nil nil mystr)))
1396     mystr))
1397
1398 (defsubst gnus-simplify-subject-re (subject)
1399   "Remove \"Re:\" from subject lines."
1400   (if (string-match message-subject-re-regexp subject)
1401       (substring subject (match-end 0))
1402     subject))
1403
1404 (defun gnus-simplify-subject (subject &optional re-only)
1405   "Remove `Re:' and words in parentheses.
1406 If RE-ONLY is non-nil, strip leading `Re:'s only."
1407   (let ((case-fold-search t))           ;Ignore case.
1408     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1409     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1410       (setq subject (substring subject (match-end 0))))
1411     ;; Remove uninteresting prefixes.
1412     (when (and (not re-only)
1413                gnus-simplify-ignored-prefixes
1414                (string-match gnus-simplify-ignored-prefixes subject))
1415       (setq subject (substring subject (match-end 0))))
1416     ;; Remove words in parentheses from end.
1417     (unless re-only
1418       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1419         (setq subject (substring subject 0 (match-beginning 0)))))
1420     ;; Return subject string.
1421     subject))
1422
1423 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1424 ;; all whitespace.
1425 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1426   (goto-char (point-min))
1427   (while (re-search-forward regexp nil t)
1428     (replace-match (or newtext ""))))
1429
1430 (defun gnus-simplify-buffer-fuzzy ()
1431   "Simplify string in the buffer fuzzily.
1432 The string in the accessible portion of the current buffer is simplified.
1433 It is assumed to be a single-line subject.
1434 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1435 matter is removed.  Additional things can be deleted by setting
1436 `gnus-simplify-subject-fuzzy-regexp'."
1437   (let ((case-fold-search t)
1438         (modified-tick))
1439     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1440
1441     (while (not (eq modified-tick (buffer-modified-tick)))
1442       (setq modified-tick (buffer-modified-tick))
1443       (cond
1444        ((listp gnus-simplify-subject-fuzzy-regexp)
1445         (mapcar 'gnus-simplify-buffer-fuzzy-step
1446                 gnus-simplify-subject-fuzzy-regexp))
1447        (gnus-simplify-subject-fuzzy-regexp
1448         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1449       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1450       (gnus-simplify-buffer-fuzzy-step
1451        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1452       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1453
1454     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1455     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1456     (gnus-simplify-buffer-fuzzy-step " $")
1457     (gnus-simplify-buffer-fuzzy-step "^ +")))
1458
1459 (defun gnus-simplify-subject-fuzzy (subject)
1460   "Simplify a subject string fuzzily.
1461 See `gnus-simplify-buffer-fuzzy' for details."
1462   (save-excursion
1463     (gnus-set-work-buffer)
1464     (let ((case-fold-search t))
1465       ;; Remove uninteresting prefixes.
1466       (when (and gnus-simplify-ignored-prefixes
1467                  (string-match gnus-simplify-ignored-prefixes subject))
1468         (setq subject (substring subject (match-end 0))))
1469       (insert subject)
1470       (inline (gnus-simplify-buffer-fuzzy))
1471       (buffer-string))))
1472
1473 (defsubst gnus-simplify-subject-fully (subject)
1474   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1475   (cond
1476    (gnus-simplify-subject-functions
1477     (gnus-map-function gnus-simplify-subject-functions subject))
1478    ((null gnus-summary-gather-subject-limit)
1479     (gnus-simplify-subject-re subject))
1480    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1481     (gnus-simplify-subject-fuzzy subject))
1482    ((numberp gnus-summary-gather-subject-limit)
1483     (gnus-limit-string (gnus-simplify-subject-re subject)
1484                        gnus-summary-gather-subject-limit))
1485    (t
1486     subject)))
1487
1488 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1489   "Check whether two subjects are equal.
1490 If optional argument simple-first is t, first argument is already
1491 simplified."
1492   (cond
1493    ((null simple-first)
1494     (equal (gnus-simplify-subject-fully s1)
1495            (gnus-simplify-subject-fully s2)))
1496    (t
1497     (equal s1
1498            (gnus-simplify-subject-fully s2)))))
1499
1500 (defun gnus-summary-bubble-group ()
1501   "Increase the score of the current group.
1502 This is a handy function to add to `gnus-summary-exit-hook' to
1503 increase the score of each group you read."
1504   (gnus-group-add-score gnus-newsgroup-name))
1505
1506 \f
1507 ;;;
1508 ;;; Gnus summary mode
1509 ;;;
1510
1511 (put 'gnus-summary-mode 'mode-class 'special)
1512
1513 (defvar gnus-article-commands-menu)
1514
1515 (when t
1516   ;; Non-orthogonal keys
1517
1518   (gnus-define-keys gnus-summary-mode-map
1519     " " gnus-summary-next-page
1520     "\177" gnus-summary-prev-page
1521     [delete] gnus-summary-prev-page
1522     [backspace] gnus-summary-prev-page
1523     "\r" gnus-summary-scroll-up
1524     "\M-\r" gnus-summary-scroll-down
1525     "n" gnus-summary-next-unread-article
1526     "p" gnus-summary-prev-unread-article
1527     "N" gnus-summary-next-article
1528     "P" gnus-summary-prev-article
1529     "\M-\C-n" gnus-summary-next-same-subject
1530     "\M-\C-p" gnus-summary-prev-same-subject
1531     "\M-n" gnus-summary-next-unread-subject
1532     "\M-p" gnus-summary-prev-unread-subject
1533     "." gnus-summary-first-unread-article
1534     "," gnus-summary-best-unread-article
1535     "\M-s" gnus-summary-search-article-forward
1536     "\M-r" gnus-summary-search-article-backward
1537     "<" gnus-summary-beginning-of-article
1538     ">" gnus-summary-end-of-article
1539     "j" gnus-summary-goto-article
1540     "^" gnus-summary-refer-parent-article
1541     "\M-^" gnus-summary-refer-article
1542     "u" gnus-summary-tick-article-forward
1543     "!" gnus-summary-tick-article-forward
1544     "U" gnus-summary-tick-article-backward
1545     "d" gnus-summary-mark-as-read-forward
1546     "D" gnus-summary-mark-as-read-backward
1547     "E" gnus-summary-mark-as-expirable
1548     "\M-u" gnus-summary-clear-mark-forward
1549     "\M-U" gnus-summary-clear-mark-backward
1550     "k" gnus-summary-kill-same-subject-and-select
1551     "\C-k" gnus-summary-kill-same-subject
1552     "\M-\C-k" gnus-summary-kill-thread
1553     "\M-\C-l" gnus-summary-lower-thread
1554     "e" gnus-summary-edit-article
1555     "#" gnus-summary-mark-as-processable
1556     "\M-#" gnus-summary-unmark-as-processable
1557     "\M-\C-t" gnus-summary-toggle-threads
1558     "\M-\C-s" gnus-summary-show-thread
1559     "\M-\C-h" gnus-summary-hide-thread
1560     "\M-\C-f" gnus-summary-next-thread
1561     "\M-\C-b" gnus-summary-prev-thread
1562     [(meta down)] gnus-summary-next-thread
1563     [(meta up)] gnus-summary-prev-thread
1564     "\M-\C-u" gnus-summary-up-thread
1565     "\M-\C-d" gnus-summary-down-thread
1566     "&" gnus-summary-execute-command
1567     "c" gnus-summary-catchup-and-exit
1568     "\C-w" gnus-summary-mark-region-as-read
1569     "\C-t" gnus-summary-toggle-truncation
1570     "?" gnus-summary-mark-as-dormant
1571     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1572     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1573     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1574     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1575     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1576     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1577     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1578     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1579     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1580     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1581     "=" gnus-summary-expand-window
1582     "\C-x\C-s" gnus-summary-reselect-current-group
1583     "\M-g" gnus-summary-rescan-group
1584     "w" gnus-summary-stop-page-breaking
1585     "\C-c\C-r" gnus-summary-caesar-message
1586     "f" gnus-summary-followup
1587     "F" gnus-summary-followup-with-original
1588     "C" gnus-summary-cancel-article
1589     "r" gnus-summary-reply
1590     "R" gnus-summary-reply-with-original
1591     "\C-c\C-f" gnus-summary-mail-forward
1592     "o" gnus-summary-save-article
1593     "\C-o" gnus-summary-save-article-mail
1594     "|" gnus-summary-pipe-output
1595     "\M-k" gnus-summary-edit-local-kill
1596     "\M-K" gnus-summary-edit-global-kill
1597     ;; "V" gnus-version
1598     "\C-c\C-d" gnus-summary-describe-group
1599     "q" gnus-summary-exit
1600     "Q" gnus-summary-exit-no-update
1601     "\C-c\C-i" gnus-info-find-node
1602     gnus-mouse-2 gnus-mouse-pick-article
1603     "m" gnus-summary-mail-other-window
1604     "a" gnus-summary-post-news
1605     "i" gnus-summary-news-other-window
1606     "x" gnus-summary-limit-to-unread
1607     "s" gnus-summary-isearch-article
1608     "t" gnus-summary-toggle-header
1609     "g" gnus-summary-show-article
1610     "l" gnus-summary-goto-last-article
1611     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1612     "\C-d" gnus-summary-enter-digest-group
1613     "\M-\C-d" gnus-summary-read-document
1614     "\M-\C-e" gnus-summary-edit-parameters
1615     "\M-\C-a" gnus-summary-customize-parameters
1616     "\C-c\C-b" gnus-bug
1617     "*" gnus-cache-enter-article
1618     "\M-*" gnus-cache-remove-article
1619     "\M-&" gnus-summary-universal-argument
1620     "\C-l" gnus-recenter
1621     "I" gnus-summary-increase-score
1622     "L" gnus-summary-lower-score
1623     "\M-i" gnus-symbolic-argument
1624     "h" gnus-summary-select-article-buffer
1625
1626     "b" gnus-article-view-part
1627     "\M-t" gnus-summary-toggle-display-buttonized
1628
1629     "V" gnus-summary-score-map
1630     "X" gnus-uu-extract-map
1631     "S" gnus-summary-send-map)
1632
1633   ;; Sort of orthogonal keymap
1634   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1635     "t" gnus-summary-tick-article-forward
1636     "!" gnus-summary-tick-article-forward
1637     "d" gnus-summary-mark-as-read-forward
1638     "r" gnus-summary-mark-as-read-forward
1639     "c" gnus-summary-clear-mark-forward
1640     " " gnus-summary-clear-mark-forward
1641     "e" gnus-summary-mark-as-expirable
1642     "x" gnus-summary-mark-as-expirable
1643     "?" gnus-summary-mark-as-dormant
1644     "b" gnus-summary-set-bookmark
1645     "B" gnus-summary-remove-bookmark
1646     "#" gnus-summary-mark-as-processable
1647     "\M-#" gnus-summary-unmark-as-processable
1648     "S" gnus-summary-limit-include-expunged
1649     "C" gnus-summary-catchup
1650     "H" gnus-summary-catchup-to-here
1651     "h" gnus-summary-catchup-from-here
1652     "\C-c" gnus-summary-catchup-all
1653     "k" gnus-summary-kill-same-subject-and-select
1654     "K" gnus-summary-kill-same-subject
1655     "P" gnus-uu-mark-map)
1656
1657   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1658     "c" gnus-summary-clear-above
1659     "u" gnus-summary-tick-above
1660     "m" gnus-summary-mark-above
1661     "k" gnus-summary-kill-below)
1662
1663   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1664     "/" gnus-summary-limit-to-subject
1665     "n" gnus-summary-limit-to-articles
1666     "w" gnus-summary-pop-limit
1667     "s" gnus-summary-limit-to-subject
1668     "a" gnus-summary-limit-to-author
1669     "u" gnus-summary-limit-to-unread
1670     "m" gnus-summary-limit-to-marks
1671     "M" gnus-summary-limit-exclude-marks
1672     "v" gnus-summary-limit-to-score
1673     "*" gnus-summary-limit-include-cached
1674     "D" gnus-summary-limit-include-dormant
1675     "T" gnus-summary-limit-include-thread
1676     "d" gnus-summary-limit-exclude-dormant
1677     "t" gnus-summary-limit-to-age
1678     "." gnus-summary-limit-to-unseen
1679     "x" gnus-summary-limit-to-extra
1680     "p" gnus-summary-limit-to-display-predicate
1681     "E" gnus-summary-limit-include-expunged
1682     "c" gnus-summary-limit-exclude-childless-dormant
1683     "C" gnus-summary-limit-mark-excluded-as-read
1684     "o" gnus-summary-insert-old-articles
1685     "N" gnus-summary-insert-new-articles)
1686
1687   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1688     "n" gnus-summary-next-unread-article
1689     "p" gnus-summary-prev-unread-article
1690     "N" gnus-summary-next-article
1691     "P" gnus-summary-prev-article
1692     "\C-n" gnus-summary-next-same-subject
1693     "\C-p" gnus-summary-prev-same-subject
1694     "\M-n" gnus-summary-next-unread-subject
1695     "\M-p" gnus-summary-prev-unread-subject
1696     "f" gnus-summary-first-unread-article
1697     "b" gnus-summary-best-unread-article
1698     "j" gnus-summary-goto-article
1699     "g" gnus-summary-goto-subject
1700     "l" gnus-summary-goto-last-article
1701     "o" gnus-summary-pop-article)
1702
1703   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1704     "k" gnus-summary-kill-thread
1705     "l" gnus-summary-lower-thread
1706     "i" gnus-summary-raise-thread
1707     "T" gnus-summary-toggle-threads
1708     "t" gnus-summary-rethread-current
1709     "^" gnus-summary-reparent-thread
1710     "s" gnus-summary-show-thread
1711     "S" gnus-summary-show-all-threads
1712     "h" gnus-summary-hide-thread
1713     "H" gnus-summary-hide-all-threads
1714     "n" gnus-summary-next-thread
1715     "p" gnus-summary-prev-thread
1716     "u" gnus-summary-up-thread
1717     "o" gnus-summary-top-thread
1718     "d" gnus-summary-down-thread
1719     "#" gnus-uu-mark-thread
1720     "\M-#" gnus-uu-unmark-thread)
1721
1722   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1723     "g" gnus-summary-prepare
1724     "c" gnus-summary-insert-cached-articles
1725     "d" gnus-summary-insert-dormant-articles)
1726
1727   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1728     "c" gnus-summary-catchup-and-exit
1729     "C" gnus-summary-catchup-all-and-exit
1730     "E" gnus-summary-exit-no-update
1731     "Q" gnus-summary-exit
1732     "Z" gnus-summary-exit
1733     "n" gnus-summary-catchup-and-goto-next-group
1734     "R" gnus-summary-reselect-current-group
1735     "G" gnus-summary-rescan-group
1736     "N" gnus-summary-next-group
1737     "s" gnus-summary-save-newsrc
1738     "P" gnus-summary-prev-group)
1739
1740   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1741     " " gnus-summary-next-page
1742     "n" gnus-summary-next-page
1743     "\177" gnus-summary-prev-page
1744     [delete] gnus-summary-prev-page
1745     "p" gnus-summary-prev-page
1746     "\r" gnus-summary-scroll-up
1747     "\M-\r" gnus-summary-scroll-down
1748     "<" gnus-summary-beginning-of-article
1749     ">" gnus-summary-end-of-article
1750     "b" gnus-summary-beginning-of-article
1751     "e" gnus-summary-end-of-article
1752     "^" gnus-summary-refer-parent-article
1753     "r" gnus-summary-refer-parent-article
1754     "D" gnus-summary-enter-digest-group
1755     "R" gnus-summary-refer-references
1756     "T" gnus-summary-refer-thread
1757     "g" gnus-summary-show-article
1758     "s" gnus-summary-isearch-article
1759     "P" gnus-summary-print-article
1760     "M" gnus-mailing-list-insinuate
1761     "t" gnus-article-babel)
1762
1763   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1764     "b" gnus-article-add-buttons
1765     "B" gnus-article-add-buttons-to-head
1766     "o" gnus-article-treat-overstrike
1767     "e" gnus-article-emphasize
1768     "w" gnus-article-fill-cited-article
1769     "Q" gnus-article-fill-long-lines
1770     "C" gnus-article-capitalize-sentences
1771     "c" gnus-article-remove-cr
1772     "q" gnus-article-de-quoted-unreadable
1773     "6" gnus-article-de-base64-unreadable
1774     "Z" gnus-article-decode-HZ
1775     "h" gnus-article-wash-html
1776     "u" gnus-article-unsplit-urls
1777     "s" gnus-summary-force-verify-and-decrypt
1778     "f" gnus-article-display-x-face
1779     "l" gnus-summary-stop-page-breaking
1780     "r" gnus-summary-caesar-message
1781     "m" gnus-summary-morse-message
1782     "t" gnus-summary-toggle-header
1783     "g" gnus-treat-smiley
1784     "v" gnus-summary-verbose-headers
1785     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1786     "p" gnus-article-verify-x-pgp-sig
1787     "d" gnus-article-treat-dumbquotes
1788     "k" gnus-article-outlook-deuglify-article)
1789
1790   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1791     "a" gnus-article-hide
1792     "h" gnus-article-hide-headers
1793     "b" gnus-article-hide-boring-headers
1794     "s" gnus-article-hide-signature
1795     "c" gnus-article-hide-citation
1796     "C" gnus-article-hide-citation-in-followups
1797     "l" gnus-article-hide-list-identifiers
1798     "p" gnus-article-hide-pgp
1799     "B" gnus-article-strip-banner
1800     "P" gnus-article-hide-pem
1801     "\C-c" gnus-article-hide-citation-maybe)
1802
1803   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1804     "a" gnus-article-highlight
1805     "h" gnus-article-highlight-headers
1806     "c" gnus-article-highlight-citation
1807     "s" gnus-article-highlight-signature)
1808
1809   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1810     "f" gnus-article-treat-fold-headers
1811     "u" gnus-article-treat-unfold-headers
1812     "n" gnus-article-treat-fold-newsgroups)
1813
1814   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1815     "x" gnus-article-display-x-face
1816     "s" gnus-treat-smiley
1817     "D" gnus-article-remove-images
1818     "f" gnus-treat-from-picon
1819     "m" gnus-treat-mail-picon
1820     "n" gnus-treat-newsgroups-picon)
1821
1822   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1823     "w" gnus-article-decode-mime-words
1824     "c" gnus-article-decode-charset
1825     "v" gnus-mime-view-all-parts
1826     "b" gnus-article-view-part)
1827
1828   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1829     "z" gnus-article-date-ut
1830     "u" gnus-article-date-ut
1831     "l" gnus-article-date-local
1832     "p" gnus-article-date-english
1833     "e" gnus-article-date-lapsed
1834     "o" gnus-article-date-original
1835     "i" gnus-article-date-iso8601
1836     "s" gnus-article-date-user)
1837
1838   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1839     "t" gnus-article-remove-trailing-blank-lines
1840     "l" gnus-article-strip-leading-blank-lines
1841     "m" gnus-article-strip-multiple-blank-lines
1842     "a" gnus-article-strip-blank-lines
1843     "A" gnus-article-strip-all-blank-lines
1844     "s" gnus-article-strip-leading-space
1845     "e" gnus-article-strip-trailing-space
1846     "w" gnus-article-remove-leading-whitespace)
1847
1848   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1849     "v" gnus-version
1850     "f" gnus-summary-fetch-faq
1851     "d" gnus-summary-describe-group
1852     "h" gnus-summary-describe-briefly
1853     "i" gnus-info-find-node
1854     "c" gnus-group-fetch-charter
1855     "C" gnus-group-fetch-control)
1856
1857   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1858     "e" gnus-summary-expire-articles
1859     "\M-\C-e" gnus-summary-expire-articles-now
1860     "\177" gnus-summary-delete-article
1861     [delete] gnus-summary-delete-article
1862     [backspace] gnus-summary-delete-article
1863     "m" gnus-summary-move-article
1864     "r" gnus-summary-respool-article
1865     "w" gnus-summary-edit-article
1866     "c" gnus-summary-copy-article
1867     "B" gnus-summary-crosspost-article
1868     "q" gnus-summary-respool-query
1869     "t" gnus-summary-respool-trace
1870     "i" gnus-summary-import-article
1871     "I" gnus-summary-create-article
1872     "p" gnus-summary-article-posted-p)
1873
1874   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1875     "o" gnus-summary-save-article
1876     "m" gnus-summary-save-article-mail
1877     "F" gnus-summary-write-article-file
1878     "r" gnus-summary-save-article-rmail
1879     "f" gnus-summary-save-article-file
1880     "b" gnus-summary-save-article-body-file
1881     "h" gnus-summary-save-article-folder
1882     "v" gnus-summary-save-article-vm
1883     "p" gnus-summary-pipe-output
1884     "P" gnus-summary-muttprint
1885     "s" gnus-soup-add-article)
1886
1887   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1888     "b" gnus-summary-display-buttonized
1889     "m" gnus-summary-repair-multipart
1890     "v" gnus-article-view-part
1891     "o" gnus-article-save-part
1892     "c" gnus-article-copy-part
1893     "C" gnus-article-view-part-as-charset
1894     "e" gnus-article-view-part-externally
1895     "E" gnus-article-encrypt-body
1896     "i" gnus-article-inline-part
1897     "|" gnus-article-pipe-part)
1898
1899   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1900     "p" gnus-summary-mark-as-processable
1901     "u" gnus-summary-unmark-as-processable
1902     "U" gnus-summary-unmark-all-processable
1903     "v" gnus-uu-mark-over
1904     "s" gnus-uu-mark-series
1905     "r" gnus-uu-mark-region
1906     "g" gnus-uu-unmark-region
1907     "R" gnus-uu-mark-by-regexp
1908     "G" gnus-uu-unmark-by-regexp
1909     "t" gnus-uu-mark-thread
1910     "T" gnus-uu-unmark-thread
1911     "a" gnus-uu-mark-all
1912     "b" gnus-uu-mark-buffer
1913     "S" gnus-uu-mark-sparse
1914     "k" gnus-summary-kill-process-mark
1915     "y" gnus-summary-yank-process-mark
1916     "w" gnus-summary-save-process-mark
1917     "i" gnus-uu-invert-processable)
1918
1919   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1920     ;;"x" gnus-uu-extract-any
1921     "m" gnus-summary-save-parts
1922     "u" gnus-uu-decode-uu
1923     "U" gnus-uu-decode-uu-and-save
1924     "s" gnus-uu-decode-unshar
1925     "S" gnus-uu-decode-unshar-and-save
1926     "o" gnus-uu-decode-save
1927     "O" gnus-uu-decode-save
1928     "b" gnus-uu-decode-binhex
1929     "B" gnus-uu-decode-binhex
1930     "p" gnus-uu-decode-postscript
1931     "P" gnus-uu-decode-postscript-and-save)
1932
1933   (gnus-define-keys
1934       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1935     "u" gnus-uu-decode-uu-view
1936     "U" gnus-uu-decode-uu-and-save-view
1937     "s" gnus-uu-decode-unshar-view
1938     "S" gnus-uu-decode-unshar-and-save-view
1939     "o" gnus-uu-decode-save-view
1940     "O" gnus-uu-decode-save-view
1941     "b" gnus-uu-decode-binhex-view
1942     "B" gnus-uu-decode-binhex-view
1943     "p" gnus-uu-decode-postscript-view
1944     "P" gnus-uu-decode-postscript-and-save-view))
1945
1946 (defvar gnus-article-post-menu nil)
1947
1948 (defconst gnus-summary-menu-maxlen 20)
1949
1950 (defun gnus-summary-menu-split (menu)
1951   ;; If we have lots of elements, divide them into groups of 20
1952   ;; and make a pane (or submenu) for each one.
1953   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1954       (let ((menu menu) sublists next
1955             (i 1))
1956         (while menu
1957           ;; Pull off the next gnus-summary-menu-maxlen elements
1958           ;; and make them the next element of sublist.
1959           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1960           (if next
1961               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1962                       nil))
1963           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1964                                              (aref (car (last menu)) 0)) menu)
1965                                sublists))
1966           (setq i (1+ i))
1967           (setq menu next))
1968         (nreverse sublists))
1969     ;; Few elements--put them all in one pane.
1970     menu))
1971
1972 (defun gnus-summary-make-menu-bar ()
1973   (gnus-turn-off-edit-menu 'summary)
1974
1975   (unless (boundp 'gnus-summary-misc-menu)
1976
1977     (easy-menu-define
1978       gnus-summary-kill-menu gnus-summary-mode-map ""
1979       (cons
1980        "Score"
1981        (nconc
1982         (list
1983          ["Customize" gnus-score-customize t])
1984         (gnus-make-score-map 'increase)
1985         (gnus-make-score-map 'lower)
1986         '(("Mark"
1987            ["Kill below" gnus-summary-kill-below t]
1988            ["Mark above" gnus-summary-mark-above t]
1989            ["Tick above" gnus-summary-tick-above t]
1990            ["Clear above" gnus-summary-clear-above t])
1991           ["Current score" gnus-summary-current-score t]
1992           ["Set score" gnus-summary-set-score t]
1993           ["Switch current score file..." gnus-score-change-score-file t]
1994           ["Set mark below..." gnus-score-set-mark-below t]
1995           ["Set expunge below..." gnus-score-set-expunge-below t]
1996           ["Edit current score file" gnus-score-edit-current-scores t]
1997           ["Edit score file" gnus-score-edit-file t]
1998           ["Trace score" gnus-score-find-trace t]
1999           ["Find words" gnus-score-find-favourite-words t]
2000           ["Rescore buffer" gnus-summary-rescore t]
2001           ["Increase score..." gnus-summary-increase-score t]
2002           ["Lower score..." gnus-summary-lower-score t]))))
2003
2004     ;; Define both the Article menu in the summary buffer and the
2005     ;; equivalent Commands menu in the article buffer here for
2006     ;; consistency.
2007     (let ((innards
2008            `(("Hide"
2009               ["All" gnus-article-hide t]
2010               ["Headers" gnus-article-hide-headers t]
2011               ["Signature" gnus-article-hide-signature t]
2012               ["Citation" gnus-article-hide-citation t]
2013               ["List identifiers" gnus-article-hide-list-identifiers t]
2014               ["PGP" gnus-article-hide-pgp t]
2015               ["Banner" gnus-article-strip-banner t]
2016               ["Boring headers" gnus-article-hide-boring-headers t])
2017              ("Highlight"
2018               ["All" gnus-article-highlight t]
2019               ["Headers" gnus-article-highlight-headers t]
2020               ["Signature" gnus-article-highlight-signature t]
2021               ["Citation" gnus-article-highlight-citation t])
2022              ("MIME"
2023               ["Words" gnus-article-decode-mime-words t]
2024               ["Charset" gnus-article-decode-charset t]
2025               ["QP" gnus-article-de-quoted-unreadable t]
2026               ["Base64" gnus-article-de-base64-unreadable t]
2027               ["View MIME buttons" gnus-summary-display-buttonized t]
2028               ["View all" gnus-mime-view-all-parts t]
2029               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2030               ["Encrypt body" gnus-article-encrypt-body t]
2031               ["Extract all parts" gnus-summary-save-parts t])
2032              ("Date"
2033               ["Local" gnus-article-date-local t]
2034               ["ISO8601" gnus-article-date-iso8601 t]
2035               ["UT" gnus-article-date-ut t]
2036               ["Original" gnus-article-date-original t]
2037               ["Lapsed" gnus-article-date-lapsed t]
2038               ["User-defined" gnus-article-date-user t])
2039              ("Display"
2040               ["Remove images" gnus-article-remove-images t]
2041               ["Toggle smiley" gnus-treat-smiley t]
2042               ["Show X-Face" gnus-article-display-x-face t]
2043               ["Show picons in From" gnus-treat-from-picon t]
2044               ["Show picons in mail headers" gnus-treat-mail-picon t]
2045               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2046               ("View as different encoding"
2047                ,@(gnus-summary-menu-split
2048                   (mapcar
2049                    (lambda (cs)
2050                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2051                      ;; forms for menu commands, we should provide intern'ed
2052                      ;; function symbols.
2053                      (let ((command (intern (format "\
2054 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2055                        (fset command
2056                              `(lambda ()
2057                                 (interactive)
2058                                 (let ((gnus-summary-show-article-charset-alist
2059                                        '((1 . ,cs))))
2060                                   (gnus-summary-show-article 1))))
2061                        `[,(symbol-name cs) ,command t]))
2062                    (sort (if (fboundp 'coding-system-list)
2063                              (coding-system-list)
2064                            (mapcar 'car mm-mime-mule-charset-alist))
2065                          'string<)))))
2066              ("Washing"
2067               ("Remove Blanks"
2068                ["Leading" gnus-article-strip-leading-blank-lines t]
2069                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2070                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2071                ["All of the above" gnus-article-strip-blank-lines t]
2072                ["All" gnus-article-strip-all-blank-lines t]
2073                ["Leading space" gnus-article-strip-leading-space t]
2074                ["Trailing space" gnus-article-strip-trailing-space t]
2075                ["Leading space in headers"
2076                 gnus-article-remove-leading-whitespace t])
2077               ["Overstrike" gnus-article-treat-overstrike t]
2078               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2079               ["Emphasis" gnus-article-emphasize t]
2080               ["Word wrap" gnus-article-fill-cited-article t]
2081               ["Fill long lines" gnus-article-fill-long-lines t]
2082               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2083               ["CR" gnus-article-remove-cr t]
2084               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2085               ["Base64" gnus-article-de-base64-unreadable t]
2086               ["Rot 13" gnus-summary-caesar-message
2087                ,@(if (featurep 'xemacs) '(t)
2088                    '(:help "\"Caesar rotate\" article by 13"))]
2089               ["Morse decode" gnus-summary-morse-message t]
2090               ["Unix pipe..." gnus-summary-pipe-message t]
2091               ["Add buttons" gnus-article-add-buttons t]
2092               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2093               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2094               ["Verbose header" gnus-summary-verbose-headers t]
2095               ["Toggle header" gnus-summary-toggle-header t]
2096               ["Unfold headers" gnus-article-treat-unfold-headers t]
2097               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2098               ["Html" gnus-article-wash-html t]
2099               ["URLs" gnus-article-unsplit-urls t]
2100               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2101               ["HZ" gnus-article-decode-HZ t]
2102               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2103               )
2104              ("Output"
2105               ["Save in default format" gnus-summary-save-article
2106                ,@(if (featurep 'xemacs) '(t)
2107                    '(:help "Save article using default method"))]
2108               ["Save in file" gnus-summary-save-article-file
2109                ,@(if (featurep 'xemacs) '(t)
2110                    '(:help "Save article in file"))]
2111               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2112               ["Save in MH folder" gnus-summary-save-article-folder t]
2113               ["Save in VM folder" gnus-summary-save-article-vm t]
2114               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2115               ["Save body in file" gnus-summary-save-article-body-file t]
2116               ["Pipe through a filter" gnus-summary-pipe-output t]
2117               ["Add to SOUP packet" gnus-soup-add-article t]
2118               ["Print with Muttprint" gnus-summary-muttprint t]
2119               ["Print" gnus-summary-print-article t])
2120              ("Backend"
2121               ["Respool article..." gnus-summary-respool-article t]
2122               ["Move article..." gnus-summary-move-article
2123                (gnus-check-backend-function
2124                 'request-move-article gnus-newsgroup-name)]
2125               ["Copy article..." gnus-summary-copy-article t]
2126               ["Crosspost article..." gnus-summary-crosspost-article
2127                (gnus-check-backend-function
2128                 'request-replace-article gnus-newsgroup-name)]
2129               ["Import file..." gnus-summary-import-article t]
2130               ["Create article..." gnus-summary-create-article t]
2131               ["Check if posted" gnus-summary-article-posted-p t]
2132               ["Edit article" gnus-summary-edit-article
2133                (not (gnus-group-read-only-p))]
2134               ["Delete article" gnus-summary-delete-article
2135                (gnus-check-backend-function
2136                 'request-expire-articles gnus-newsgroup-name)]
2137               ["Query respool" gnus-summary-respool-query t]
2138               ["Trace respool" gnus-summary-respool-trace t]
2139               ["Delete expirable articles" gnus-summary-expire-articles-now
2140                (gnus-check-backend-function
2141                 'request-expire-articles gnus-newsgroup-name)])
2142              ("Extract"
2143               ["Uudecode" gnus-uu-decode-uu
2144                ,@(if (featurep 'xemacs) '(t)
2145                    '(:help "Decode uuencoded article(s)"))]
2146               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2147               ["Unshar" gnus-uu-decode-unshar t]
2148               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2149               ["Save" gnus-uu-decode-save t]
2150               ["Binhex" gnus-uu-decode-binhex t]
2151               ["Postscript" gnus-uu-decode-postscript t]
2152               ["All MIME parts" gnus-summary-save-parts t])
2153              ("Cache"
2154               ["Enter article" gnus-cache-enter-article t]
2155               ["Remove article" gnus-cache-remove-article t])
2156              ["Translate" gnus-article-babel t]
2157              ["Select article buffer" gnus-summary-select-article-buffer t]
2158              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2159              ["Isearch article..." gnus-summary-isearch-article t]
2160              ["Beginning of the article" gnus-summary-beginning-of-article t]
2161              ["End of the article" gnus-summary-end-of-article t]
2162              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2163              ["Fetch referenced articles" gnus-summary-refer-references t]
2164              ["Fetch current thread" gnus-summary-refer-thread t]
2165              ["Fetch article with id..." gnus-summary-refer-article t]
2166              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2167              ["Redisplay" gnus-summary-show-article t]
2168              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2169       (easy-menu-define
2170         gnus-summary-article-menu gnus-summary-mode-map ""
2171         (cons "Article" innards))
2172
2173       (if (not (keymapp gnus-summary-article-menu))
2174           (easy-menu-define
2175             gnus-article-commands-menu gnus-article-mode-map ""
2176             (cons "Commands" innards))
2177         ;; in Emacs, don't share menu.
2178         (setq gnus-article-commands-menu
2179               (copy-keymap gnus-summary-article-menu))
2180         (define-key gnus-article-mode-map [menu-bar commands]
2181           (cons "Commands" gnus-article-commands-menu))))
2182
2183     (easy-menu-define
2184       gnus-summary-thread-menu gnus-summary-mode-map ""
2185       '("Threads"
2186         ["Find all messages in thread" gnus-summary-refer-thread t]
2187         ["Toggle threading" gnus-summary-toggle-threads t]
2188         ["Hide threads" gnus-summary-hide-all-threads t]
2189         ["Show threads" gnus-summary-show-all-threads t]
2190         ["Hide thread" gnus-summary-hide-thread t]
2191         ["Show thread" gnus-summary-show-thread t]
2192         ["Go to next thread" gnus-summary-next-thread t]
2193         ["Go to previous thread" gnus-summary-prev-thread t]
2194         ["Go down thread" gnus-summary-down-thread t]
2195         ["Go up thread" gnus-summary-up-thread t]
2196         ["Top of thread" gnus-summary-top-thread t]
2197         ["Mark thread as read" gnus-summary-kill-thread t]
2198         ["Lower thread score" gnus-summary-lower-thread t]
2199         ["Raise thread score" gnus-summary-raise-thread t]
2200         ["Rethread current" gnus-summary-rethread-current t]))
2201
2202     (easy-menu-define
2203       gnus-summary-post-menu gnus-summary-mode-map ""
2204       `("Post"
2205         ["Send a message (mail or news)" gnus-summary-post-news
2206          ,@(if (featurep 'xemacs) '(t)
2207              '(:help "Post an article"))]
2208         ["Followup" gnus-summary-followup
2209          ,@(if (featurep 'xemacs) '(t)
2210              '(:help "Post followup to this article"))]
2211         ["Followup and yank" gnus-summary-followup-with-original
2212          ,@(if (featurep 'xemacs) '(t)
2213              '(:help "Post followup to this article, quoting its contents"))]
2214         ["Supersede article" gnus-summary-supersede-article t]
2215         ["Cancel article" gnus-summary-cancel-article
2216          ,@(if (featurep 'xemacs) '(t)
2217              '(:help "Cancel an article you posted"))]
2218         ["Reply" gnus-summary-reply t]
2219         ["Reply and yank" gnus-summary-reply-with-original t]
2220         ["Wide reply" gnus-summary-wide-reply t]
2221         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2222          ,@(if (featurep 'xemacs) '(t)
2223              '(:help "Mail a reply, quoting this article"))]
2224         ["Very wide reply" gnus-summary-very-wide-reply t]
2225         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2226          ,@(if (featurep 'xemacs) '(t)
2227              '(:help "Mail a very wide reply, quoting this article"))]
2228         ["Mail forward" gnus-summary-mail-forward t]
2229         ["Post forward" gnus-summary-post-forward t]
2230         ["Digest and mail" gnus-uu-digest-mail-forward t]
2231         ["Digest and post" gnus-uu-digest-post-forward t]
2232         ["Resend message" gnus-summary-resend-message t]
2233         ["Resend message edit" gnus-summary-resend-message-edit t]
2234         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2235         ["Send a mail" gnus-summary-mail-other-window t]
2236         ["Create a local message" gnus-summary-news-other-window t]
2237         ["Uuencode and post" gnus-uu-post-news
2238          ,@(if (featurep 'xemacs) '(t)
2239              '(:help "Post a uuencoded article"))]
2240         ["Followup via news" gnus-summary-followup-to-mail t]
2241         ["Followup via news and yank"
2242          gnus-summary-followup-to-mail-with-original t]
2243         ;;("Draft"
2244         ;;["Send" gnus-summary-send-draft t]
2245         ;;["Send bounced" gnus-resend-bounced-mail t])
2246         ))
2247
2248     (cond
2249      ((not (keymapp gnus-summary-post-menu))
2250       (setq gnus-article-post-menu gnus-summary-post-menu))
2251      ((not gnus-article-post-menu)
2252       ;; Don't share post menu.
2253       (setq gnus-article-post-menu
2254             (copy-keymap gnus-summary-post-menu))))
2255     (define-key gnus-article-mode-map [menu-bar post]
2256       (cons "Post" gnus-article-post-menu))
2257
2258     (easy-menu-define
2259       gnus-summary-misc-menu gnus-summary-mode-map ""
2260       `("Gnus"
2261         ("Mark Read"
2262          ["Mark as read" gnus-summary-mark-as-read-forward t]
2263          ["Mark same subject and select"
2264           gnus-summary-kill-same-subject-and-select t]
2265          ["Mark same subject" gnus-summary-kill-same-subject t]
2266          ["Catchup" gnus-summary-catchup
2267           ,@(if (featurep 'xemacs) '(t)
2268               '(:help "Mark unread articles in this group as read"))]
2269          ["Catchup all" gnus-summary-catchup-all t]
2270          ["Catchup to here" gnus-summary-catchup-to-here t]
2271          ["Catchup from here" gnus-summary-catchup-from-here t]
2272          ["Catchup region" gnus-summary-mark-region-as-read t]
2273          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2274         ("Mark Various"
2275          ["Tick" gnus-summary-tick-article-forward t]
2276          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2277          ["Remove marks" gnus-summary-clear-mark-forward t]
2278          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2279          ["Set bookmark" gnus-summary-set-bookmark t]
2280          ["Remove bookmark" gnus-summary-remove-bookmark t])
2281         ("Limit to"
2282          ["Marks..." gnus-summary-limit-to-marks t]
2283          ["Subject..." gnus-summary-limit-to-subject t]
2284          ["Author..." gnus-summary-limit-to-author t]
2285          ["Age..." gnus-summary-limit-to-age t]
2286          ["Extra..." gnus-summary-limit-to-extra t]
2287          ["Score..." gnus-summary-limit-to-score t]
2288          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2289          ["Unread" gnus-summary-limit-to-unread t]
2290          ["Unseen" gnus-summary-limit-to-unseen t]
2291          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2292          ["Next articles" gnus-summary-limit-to-articles t]
2293          ["Pop limit" gnus-summary-pop-limit t]
2294          ["Show dormant" gnus-summary-limit-include-dormant t]
2295          ["Hide childless dormant"
2296           gnus-summary-limit-exclude-childless-dormant t]
2297          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2298          ["Hide marked" gnus-summary-limit-exclude-marks t]
2299          ["Show expunged" gnus-summary-limit-include-expunged t])
2300         ("Process Mark"
2301          ["Set mark" gnus-summary-mark-as-processable t]
2302          ["Remove mark" gnus-summary-unmark-as-processable t]
2303          ["Remove all marks" gnus-summary-unmark-all-processable t]
2304          ["Mark above" gnus-uu-mark-over t]
2305          ["Mark series" gnus-uu-mark-series t]
2306          ["Mark region" gnus-uu-mark-region t]
2307          ["Unmark region" gnus-uu-unmark-region t]
2308          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2309          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2310          ["Mark all" gnus-uu-mark-all t]
2311          ["Mark buffer" gnus-uu-mark-buffer t]
2312          ["Mark sparse" gnus-uu-mark-sparse t]
2313          ["Mark thread" gnus-uu-mark-thread t]
2314          ["Unmark thread" gnus-uu-unmark-thread t]
2315          ("Process Mark Sets"
2316           ["Kill" gnus-summary-kill-process-mark t]
2317           ["Yank" gnus-summary-yank-process-mark
2318            gnus-newsgroup-process-stack]
2319           ["Save" gnus-summary-save-process-mark t]))
2320         ("Scroll article"
2321          ["Page forward" gnus-summary-next-page
2322           ,@(if (featurep 'xemacs) '(t)
2323               '(:help "Show next page of article"))]
2324          ["Page backward" gnus-summary-prev-page
2325           ,@(if (featurep 'xemacs) '(t)
2326               '(:help "Show previous page of article"))]
2327          ["Line forward" gnus-summary-scroll-up t])
2328         ("Move"
2329          ["Next unread article" gnus-summary-next-unread-article t]
2330          ["Previous unread article" gnus-summary-prev-unread-article t]
2331          ["Next article" gnus-summary-next-article t]
2332          ["Previous article" gnus-summary-prev-article t]
2333          ["Next unread subject" gnus-summary-next-unread-subject t]
2334          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2335          ["Next article same subject" gnus-summary-next-same-subject t]
2336          ["Previous article same subject" gnus-summary-prev-same-subject t]
2337          ["First unread article" gnus-summary-first-unread-article t]
2338          ["Best unread article" gnus-summary-best-unread-article t]
2339          ["Go to subject number..." gnus-summary-goto-subject t]
2340          ["Go to article number..." gnus-summary-goto-article t]
2341          ["Go to the last article" gnus-summary-goto-last-article t]
2342          ["Pop article off history" gnus-summary-pop-article t])
2343         ("Sort"
2344          ["Sort by number" gnus-summary-sort-by-number t]
2345          ["Sort by author" gnus-summary-sort-by-author t]
2346          ["Sort by subject" gnus-summary-sort-by-subject t]
2347          ["Sort by date" gnus-summary-sort-by-date t]
2348          ["Sort by score" gnus-summary-sort-by-score t]
2349          ["Sort by lines" gnus-summary-sort-by-lines t]
2350          ["Sort by characters" gnus-summary-sort-by-chars t]
2351          ["Randomize" gnus-summary-sort-by-random t]
2352          ["Original sort" gnus-summary-sort-by-original t])
2353         ("Help"
2354          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2355          ["Describe group" gnus-summary-describe-group t]
2356          ["Fetch charter" gnus-group-fetch-charter
2357           ,@(if (featurep 'xemacs) nil
2358               '(:help "Display the charter of the current group"))]
2359          ["Fetch control message" gnus-group-fetch-control
2360           ,@(if (featurep 'xemacs) nil
2361               '(:help "Display the archived control message for the current group"))]
2362          ["Read manual" gnus-info-find-node t])
2363         ("Modes"
2364          ["Pick and read" gnus-pick-mode t]
2365          ["Binary" gnus-binary-mode t])
2366         ("Regeneration"
2367          ["Regenerate" gnus-summary-prepare t]
2368          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2369          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2370          ["Toggle threading" gnus-summary-toggle-threads t])
2371         ["See old articles" gnus-summary-insert-old-articles t]
2372         ["See new articles" gnus-summary-insert-new-articles t]
2373         ["Filter articles..." gnus-summary-execute-command t]
2374         ["Run command on subjects..." gnus-summary-universal-argument t]
2375         ["Search articles forward..." gnus-summary-search-article-forward t]
2376         ["Search articles backward..." gnus-summary-search-article-backward t]
2377         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2378         ["Expand window" gnus-summary-expand-window t]
2379         ["Expire expirable articles" gnus-summary-expire-articles
2380          (gnus-check-backend-function
2381           'request-expire-articles gnus-newsgroup-name)]
2382         ["Edit local kill file" gnus-summary-edit-local-kill t]
2383         ["Edit main kill file" gnus-summary-edit-global-kill t]
2384         ["Edit group parameters" gnus-summary-edit-parameters t]
2385         ["Customize group parameters" gnus-summary-customize-parameters t]
2386         ["Send a bug report" gnus-bug t]
2387         ("Exit"
2388          ["Catchup and exit" gnus-summary-catchup-and-exit
2389           ,@(if (featurep 'xemacs) '(t)
2390               '(:help "Mark unread articles in this group as read, then exit"))]
2391          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2392          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2393          ["Exit group" gnus-summary-exit
2394           ,@(if (featurep 'xemacs) '(t)
2395               '(:help "Exit current group, return to group selection mode"))]
2396          ["Exit group without updating" gnus-summary-exit-no-update t]
2397          ["Exit and goto next group" gnus-summary-next-group t]
2398          ["Exit and goto prev group" gnus-summary-prev-group t]
2399          ["Reselect group" gnus-summary-reselect-current-group t]
2400          ["Rescan group" gnus-summary-rescan-group t]
2401          ["Update dribble" gnus-summary-save-newsrc t])))
2402
2403     (gnus-run-hooks 'gnus-summary-menu-hook)))
2404
2405 (defvar gnus-summary-tool-bar-map nil)
2406
2407 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2408 (defun gnus-summary-make-tool-bar ()
2409   (if (and (fboundp 'tool-bar-add-item-from-menu)
2410            (default-value 'tool-bar-mode)
2411            (not gnus-summary-tool-bar-map))
2412       (setq gnus-summary-tool-bar-map
2413             (let ((tool-bar-map (make-sparse-keymap))
2414                   (load-path (mm-image-load-path)))
2415               (tool-bar-add-item-from-menu
2416                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2417               (tool-bar-add-item-from-menu
2418                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2419               (tool-bar-add-item-from-menu
2420                'gnus-summary-post-news "post" gnus-summary-mode-map)
2421               (tool-bar-add-item-from-menu
2422                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2423               (tool-bar-add-item-from-menu
2424                'gnus-summary-followup "followup" gnus-summary-mode-map)
2425               (tool-bar-add-item-from-menu
2426                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2427               (tool-bar-add-item-from-menu
2428                'gnus-summary-reply "reply" gnus-summary-mode-map)
2429               (tool-bar-add-item-from-menu
2430                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2431               (tool-bar-add-item-from-menu
2432                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2433               (tool-bar-add-item-from-menu
2434                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2435               (tool-bar-add-item-from-menu
2436                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2437               (tool-bar-add-item-from-menu
2438                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2439               (tool-bar-add-item-from-menu
2440                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2441               (tool-bar-add-item-from-menu
2442                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2443               (tool-bar-add-item-from-menu
2444                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2445               tool-bar-map)))
2446   (if gnus-summary-tool-bar-map
2447       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2448
2449 (defun gnus-score-set-default (var value)
2450   "A version of set that updates the GNU Emacs menu-bar."
2451   (set var value)
2452   ;; It is the message that forces the active status to be updated.
2453   (message ""))
2454
2455 (defun gnus-make-score-map (type)
2456   "Make a summary score map of type TYPE."
2457   (if t
2458       nil
2459     (let ((headers '(("author" "from" string)
2460                      ("subject" "subject" string)
2461                      ("article body" "body" string)
2462                      ("article head" "head" string)
2463                      ("xref" "xref" string)
2464                      ("extra header" "extra" string)
2465                      ("lines" "lines" number)
2466                      ("followups to author" "followup" string)))
2467           (types '((number ("less than" <)
2468                            ("greater than" >)
2469                            ("equal" =))
2470                    (string ("substring" s)
2471                            ("exact string" e)
2472                            ("fuzzy string" f)
2473                            ("regexp" r))))
2474           (perms '(("temporary" (current-time-string))
2475                    ("permanent" nil)
2476                    ("immediate" now)))
2477           header)
2478       (list
2479        (apply
2480         'nconc
2481         (list
2482          (if (eq type 'lower)
2483              "Lower score"
2484            "Increase score"))
2485         (let (outh)
2486           (while headers
2487             (setq header (car headers))
2488             (setq outh
2489                   (cons
2490                    (apply
2491                     'nconc
2492                     (list (car header))
2493                     (let ((ts (cdr (assoc (nth 2 header) types)))
2494                           outt)
2495                       (while ts
2496                         (setq outt
2497                               (cons
2498                                (apply
2499                                 'nconc
2500                                 (list (caar ts))
2501                                 (let ((ps perms)
2502                                       outp)
2503                                   (while ps
2504                                     (setq outp
2505                                           (cons
2506                                            (vector
2507                                             (caar ps)
2508                                             (list
2509                                              'gnus-summary-score-entry
2510                                              (nth 1 header)
2511                                              (if (or (string= (nth 1 header)
2512                                                               "head")
2513                                                      (string= (nth 1 header)
2514                                                               "body"))
2515                                                  ""
2516                                                (list 'gnus-summary-header
2517                                                      (nth 1 header)))
2518                                              (list 'quote (nth 1 (car ts)))
2519                                              (list 'gnus-score-delta-default
2520                                                    nil)
2521                                              (nth 1 (car ps))
2522                                              t)
2523                                             t)
2524                                            outp))
2525                                     (setq ps (cdr ps)))
2526                                   (list (nreverse outp))))
2527                                outt))
2528                         (setq ts (cdr ts)))
2529                       (list (nreverse outt))))
2530                    outh))
2531             (setq headers (cdr headers)))
2532           (list (nreverse outh))))))))
2533
2534 \f
2535
2536 (defun gnus-summary-mode (&optional group)
2537   "Major mode for reading articles.
2538
2539 All normal editing commands are switched off.
2540 \\<gnus-summary-mode-map>
2541 Each line in this buffer represents one article.  To read an
2542 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2543 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2544 respectively.
2545
2546 You can also post articles and send mail from this buffer.  To
2547 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2548 of an article, type `\\[gnus-summary-reply]'.
2549
2550 There are approx. one gazillion commands you can execute in this
2551 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2552
2553 The following commands are available:
2554
2555 \\{gnus-summary-mode-map}"
2556   (interactive)
2557   (kill-all-local-variables)
2558   (when (gnus-visual-p 'summary-menu 'menu)
2559     (gnus-summary-make-menu-bar)
2560     (gnus-summary-make-tool-bar))
2561   (gnus-summary-make-local-variables)
2562   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2563     (gnus-summary-make-local-variables))
2564   (gnus-make-thread-indent-array)
2565   (gnus-simplify-mode-line)
2566   (setq major-mode 'gnus-summary-mode)
2567   (setq mode-name "Summary")
2568   (make-local-variable 'minor-mode-alist)
2569   (use-local-map gnus-summary-mode-map)
2570   (buffer-disable-undo)
2571   (setq buffer-read-only t)             ;Disable modification
2572   (setq truncate-lines t)
2573   (setq selective-display t)
2574   (setq selective-display-ellipses t)   ;Display `...'
2575   (gnus-summary-set-display-table)
2576   (gnus-set-default-directory)
2577   (setq gnus-newsgroup-name group)
2578   (make-local-variable 'gnus-summary-line-format)
2579   (make-local-variable 'gnus-summary-line-format-spec)
2580   (make-local-variable 'gnus-summary-dummy-line-format)
2581   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2582   (make-local-variable 'gnus-summary-mark-positions)
2583   (make-local-hook 'pre-command-hook)
2584   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2585   (gnus-run-hooks 'gnus-summary-mode-hook)
2586   (turn-on-gnus-mailing-list-mode)
2587   (mm-enable-multibyte)
2588   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2589   (gnus-update-summary-mark-positions))
2590
2591 (defun gnus-summary-make-local-variables ()
2592   "Make all the local summary buffer variables."
2593   (let (global)
2594     (dolist (local gnus-summary-local-variables)
2595       (if (consp local)
2596           (progn
2597             (if (eq (cdr local) 'global)
2598                 ;; Copy the global value of the variable.
2599                 (setq global (symbol-value (car local)))
2600               ;; Use the value from the list.
2601               (setq global (eval (cdr local))))
2602             (set (make-local-variable (car local)) global))
2603         ;; Simple nil-valued local variable.
2604         (set (make-local-variable local) nil)))))
2605
2606 (defun gnus-summary-clear-local-variables ()
2607   (let ((locals gnus-summary-local-variables))
2608     (while locals
2609       (if (consp (car locals))
2610           (and (vectorp (caar locals))
2611                (set (caar locals) nil))
2612         (and (vectorp (car locals))
2613              (set (car locals) nil)))
2614       (setq locals (cdr locals)))))
2615
2616 ;; Summary data functions.
2617
2618 (defmacro gnus-data-number (data)
2619   `(car ,data))
2620
2621 (defmacro gnus-data-set-number (data number)
2622   `(setcar ,data ,number))
2623
2624 (defmacro gnus-data-mark (data)
2625   `(nth 1 ,data))
2626
2627 (defmacro gnus-data-set-mark (data mark)
2628   `(setcar (nthcdr 1 ,data) ,mark))
2629
2630 (defmacro gnus-data-pos (data)
2631   `(nth 2 ,data))
2632
2633 (defmacro gnus-data-set-pos (data pos)
2634   `(setcar (nthcdr 2 ,data) ,pos))
2635
2636 (defmacro gnus-data-header (data)
2637   `(nth 3 ,data))
2638
2639 (defmacro gnus-data-set-header (data header)
2640   `(setf (nth 3 ,data) ,header))
2641
2642 (defmacro gnus-data-level (data)
2643   `(nth 4 ,data))
2644
2645 (defmacro gnus-data-unread-p (data)
2646   `(= (nth 1 ,data) gnus-unread-mark))
2647
2648 (defmacro gnus-data-read-p (data)
2649   `(/= (nth 1 ,data) gnus-unread-mark))
2650
2651 (defmacro gnus-data-pseudo-p (data)
2652   `(consp (nth 3 ,data)))
2653
2654 (defmacro gnus-data-find (number)
2655   `(assq ,number gnus-newsgroup-data))
2656
2657 (defmacro gnus-data-find-list (number &optional data)
2658   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2659      (memq (assq ,number bdata)
2660            bdata)))
2661
2662 (defmacro gnus-data-make (number mark pos header level)
2663   `(list ,number ,mark ,pos ,header ,level))
2664
2665 (defun gnus-data-enter (after-article number mark pos header level offset)
2666   (let ((data (gnus-data-find-list after-article)))
2667     (unless data
2668       (error "No such article: %d" after-article))
2669     (setcdr data (cons (gnus-data-make number mark pos header level)
2670                        (cdr data)))
2671     (setq gnus-newsgroup-data-reverse nil)
2672     (gnus-data-update-list (cddr data) offset)))
2673
2674 (defun gnus-data-enter-list (after-article list &optional offset)
2675   (when list
2676     (let ((data (and after-article (gnus-data-find-list after-article)))
2677           (ilist list))
2678       (if (not (or data
2679                    after-article))
2680           (let ((odata gnus-newsgroup-data))
2681             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2682             (when offset
2683               (gnus-data-update-list odata offset)))
2684       ;; Find the last element in the list to be spliced into the main
2685         ;; list.
2686         (while (cdr list)
2687           (setq list (cdr list)))
2688         (if (not data)
2689             (progn
2690               (setcdr list gnus-newsgroup-data)
2691               (setq gnus-newsgroup-data ilist)
2692               (when offset
2693                 (gnus-data-update-list (cdr list) offset)))
2694           (setcdr list (cdr data))
2695           (setcdr data ilist)
2696           (when offset
2697             (gnus-data-update-list (cdr list) offset))))
2698       (setq gnus-newsgroup-data-reverse nil))))
2699
2700 (defun gnus-data-remove (article &optional offset)
2701   (let ((data gnus-newsgroup-data))
2702     (if (= (gnus-data-number (car data)) article)
2703         (progn
2704           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2705                 gnus-newsgroup-data-reverse nil)
2706           (when offset
2707             (gnus-data-update-list gnus-newsgroup-data offset)))
2708       (while (cdr data)
2709         (when (= (gnus-data-number (cadr data)) article)
2710           (setcdr data (cddr data))
2711           (when offset
2712             (gnus-data-update-list (cdr data) offset))
2713           (setq data nil
2714                 gnus-newsgroup-data-reverse nil))
2715         (setq data (cdr data))))))
2716
2717 (defmacro gnus-data-list (backward)
2718   `(if ,backward
2719        (or gnus-newsgroup-data-reverse
2720            (setq gnus-newsgroup-data-reverse
2721                  (reverse gnus-newsgroup-data)))
2722      gnus-newsgroup-data))
2723
2724 (defun gnus-data-update-list (data offset)
2725   "Add OFFSET to the POS of all data entries in DATA."
2726   (setq gnus-newsgroup-data-reverse nil)
2727   (while data
2728     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2729     (setq data (cdr data))))
2730
2731 (defun gnus-summary-article-pseudo-p (article)
2732   "Say whether this article is a pseudo article or not."
2733   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2734
2735 (defmacro gnus-summary-article-sparse-p (article)
2736   "Say whether this article is a sparse article or not."
2737   `(memq ,article gnus-newsgroup-sparse))
2738
2739 (defmacro gnus-summary-article-ancient-p (article)
2740   "Say whether this article is a sparse article or not."
2741   `(memq ,article gnus-newsgroup-ancient))
2742
2743 (defun gnus-article-parent-p (number)
2744   "Say whether this article is a parent or not."
2745   (let ((data (gnus-data-find-list number)))
2746     (and (cdr data)              ; There has to be an article after...
2747          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2748             (gnus-data-level (nth 1 data))))))
2749
2750 (defun gnus-article-children (number)
2751   "Return a list of all children to NUMBER."
2752   (let* ((data (gnus-data-find-list number))
2753          (level (gnus-data-level (car data)))
2754          children)
2755     (setq data (cdr data))
2756     (while (and data
2757                 (= (gnus-data-level (car data)) (1+ level)))
2758       (push (gnus-data-number (car data)) children)
2759       (setq data (cdr data)))
2760     children))
2761
2762 (defmacro gnus-summary-skip-intangible ()
2763   "If the current article is intangible, then jump to a different article."
2764   '(let ((to (get-text-property (point) 'gnus-intangible)))
2765      (and to (gnus-summary-goto-subject to))))
2766
2767 (defmacro gnus-summary-article-intangible-p ()
2768   "Say whether this article is intangible or not."
2769   '(get-text-property (point) 'gnus-intangible))
2770
2771 (defun gnus-article-read-p (article)
2772   "Say whether ARTICLE is read or not."
2773   (not (or (memq article gnus-newsgroup-marked)
2774            (memq article gnus-newsgroup-spam-marked)
2775            (memq article gnus-newsgroup-unreads)
2776            (memq article gnus-newsgroup-unselected)
2777            (memq article gnus-newsgroup-dormant))))
2778
2779 ;; Some summary mode macros.
2780
2781 (defmacro gnus-summary-article-number ()
2782   "The article number of the article on the current line.
2783 If there isn't an article number here, then we return the current
2784 article number."
2785   '(progn
2786      (gnus-summary-skip-intangible)
2787      (or (get-text-property (point) 'gnus-number)
2788          (gnus-summary-last-subject))))
2789
2790 (defmacro gnus-summary-article-header (&optional number)
2791   "Return the header of article NUMBER."
2792   `(gnus-data-header (gnus-data-find
2793                       ,(or number '(gnus-summary-article-number)))))
2794
2795 (defmacro gnus-summary-thread-level (&optional number)
2796   "Return the level of thread that starts with article NUMBER."
2797   `(if (and (eq gnus-summary-make-false-root 'dummy)
2798             (get-text-property (point) 'gnus-intangible))
2799        0
2800      (gnus-data-level (gnus-data-find
2801                        ,(or number '(gnus-summary-article-number))))))
2802
2803 (defmacro gnus-summary-article-mark (&optional number)
2804   "Return the mark of article NUMBER."
2805   `(gnus-data-mark (gnus-data-find
2806                     ,(or number '(gnus-summary-article-number)))))
2807
2808 (defmacro gnus-summary-article-pos (&optional number)
2809   "Return the position of the line of article NUMBER."
2810   `(gnus-data-pos (gnus-data-find
2811                    ,(or number '(gnus-summary-article-number)))))
2812
2813 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2814 (defmacro gnus-summary-article-subject (&optional number)
2815   "Return current subject string or nil if nothing."
2816   `(let ((headers
2817           ,(if number
2818                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2819              '(gnus-data-header (assq (gnus-summary-article-number)
2820                                       gnus-newsgroup-data)))))
2821      (and headers
2822           (vectorp headers)
2823           (mail-header-subject headers))))
2824
2825 (defmacro gnus-summary-article-score (&optional number)
2826   "Return current article score."
2827   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2828                   gnus-newsgroup-scored))
2829        gnus-summary-default-score 0))
2830
2831 (defun gnus-summary-article-children (&optional number)
2832   "Return a list of article numbers that are children of article NUMBER."
2833   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2834          (level (gnus-data-level (car data)))
2835          l children)
2836     (while (and (setq data (cdr data))
2837                 (> (setq l (gnus-data-level (car data))) level))
2838       (and (= (1+ level) l)
2839            (push (gnus-data-number (car data))
2840                  children)))
2841     (nreverse children)))
2842
2843 (defun gnus-summary-article-parent (&optional number)
2844   "Return the article number of the parent of article NUMBER."
2845   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2846                                     (gnus-data-list t)))
2847          (level (gnus-data-level (car data))))
2848     (if (zerop level)
2849         ()                              ; This is a root.
2850       ;; We search until we find an article with a level less than
2851       ;; this one.  That function has to be the parent.
2852       (while (and (setq data (cdr data))
2853                   (not (< (gnus-data-level (car data)) level))))
2854       (and data (gnus-data-number (car data))))))
2855
2856 (defun gnus-unread-mark-p (mark)
2857   "Say whether MARK is the unread mark."
2858   (= mark gnus-unread-mark))
2859
2860 (defun gnus-read-mark-p (mark)
2861   "Say whether MARK is one of the marks that mark as read.
2862 This is all marks except unread, ticked, dormant, and expirable."
2863   (not (or (= mark gnus-unread-mark)
2864            (= mark gnus-ticked-mark)
2865            (= mark gnus-spam-mark)
2866            (= mark gnus-dormant-mark)
2867            (= mark gnus-expirable-mark))))
2868
2869 (defmacro gnus-article-mark (number)
2870   "Return the MARK of article NUMBER.
2871 This macro should only be used when computing the mark the \"first\"
2872 time; i.e., when generating the summary lines.  After that,
2873 `gnus-summary-article-mark' should be used to examine the
2874 marks of articles."
2875   `(cond
2876     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2877     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2878     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2879     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2880     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2881     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2882     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2883     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2884            gnus-ancient-mark))))
2885
2886 ;; Saving hidden threads.
2887
2888 (defmacro gnus-save-hidden-threads (&rest forms)
2889   "Save hidden threads, eval FORMS, and restore the hidden threads."
2890   (let ((config (make-symbol "config")))
2891     `(let ((,config (gnus-hidden-threads-configuration)))
2892        (unwind-protect
2893            (save-excursion
2894              ,@forms)
2895          (gnus-restore-hidden-threads-configuration ,config)))))
2896 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2897 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2898
2899 (defun gnus-data-compute-positions ()
2900   "Compute the positions of all articles."
2901   (setq gnus-newsgroup-data-reverse nil)
2902   (let ((data gnus-newsgroup-data))
2903     (save-excursion
2904       (gnus-save-hidden-threads
2905         (gnus-summary-show-all-threads)
2906         (goto-char (point-min))
2907         (while data
2908           (while (get-text-property (point) 'gnus-intangible)
2909             (forward-line 1))
2910           (gnus-data-set-pos (car data) (+ (point) 3))
2911           (setq data (cdr data))
2912           (forward-line 1))))))
2913
2914 (defun gnus-hidden-threads-configuration ()
2915   "Return the current hidden threads configuration."
2916   (save-excursion
2917     (let (config)
2918       (goto-char (point-min))
2919       (while (search-forward "\r" nil t)
2920         (push (1- (point)) config))
2921       config)))
2922
2923 (defun gnus-restore-hidden-threads-configuration (config)
2924   "Restore hidden threads configuration from CONFIG."
2925   (save-excursion
2926     (let (point buffer-read-only)
2927       (while (setq point (pop config))
2928         (when (and (< point (point-max))
2929                    (goto-char point)
2930                    (eq (char-after) ?\n))
2931           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2932
2933 ;; Various summary mode internalish functions.
2934
2935 (defun gnus-mouse-pick-article (e)
2936   (interactive "e")
2937   (mouse-set-point e)
2938   (gnus-summary-next-page nil t))
2939
2940 (defun gnus-summary-set-display-table ()
2941   "Change the display table.
2942 Odd characters have a tendency to mess
2943 up nicely formatted displays - we make all possible glyphs
2944 display only a single character."
2945
2946   ;; We start from the standard display table, if any.
2947   (let ((table (or (copy-sequence standard-display-table)
2948                    (make-display-table)))
2949         (i 32))
2950     ;; Nix out all the control chars...
2951     (while (>= (setq i (1- i)) 0)
2952       (aset table i [??]))
2953    ;; ... but not newline and cr, of course.  (cr is necessary for the
2954     ;; selective display).
2955     (aset table ?\n nil)
2956     (aset table ?\r nil)
2957     ;; We keep TAB as well.
2958     (aset table ?\t nil)
2959     ;; We nix out any glyphs over 126 that are not set already.
2960     (let ((i 256))
2961       (while (>= (setq i (1- i)) 127)
2962         ;; Only modify if the entry is nil.
2963         (unless (aref table i)
2964           (aset table i [??]))))
2965     (setq buffer-display-table table)))
2966
2967 (defun gnus-summary-set-article-display-arrow (pos)
2968   "Update the overlay arrow to point to line at position POS."
2969   (when (and gnus-summary-display-arrow
2970              (boundp 'overlay-arrow-position)
2971              (boundp 'overlay-arrow-string))
2972     (save-excursion
2973       (goto-char pos)
2974       (beginning-of-line)
2975       (unless overlay-arrow-position
2976         (setq overlay-arrow-position (make-marker)))
2977       (setq overlay-arrow-string "=>"
2978             overlay-arrow-position (set-marker overlay-arrow-position
2979                                                (point)
2980                                                (current-buffer))))))
2981
2982 (defun gnus-summary-buffer-name (group)
2983   "Return the summary buffer name of GROUP."
2984   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2985
2986 (defun gnus-summary-setup-buffer (group)
2987   "Initialize summary buffer."
2988   (let ((buffer (gnus-summary-buffer-name group))
2989         (dead-name (concat "*Dead Summary "
2990                            (gnus-group-decoded-name group) "*")))
2991     ;; If a dead summary buffer exists, we kill it.
2992     (when (gnus-buffer-live-p dead-name)
2993       (gnus-kill-buffer dead-name))
2994     (if (get-buffer buffer)
2995         (progn
2996           (set-buffer buffer)
2997           (setq gnus-summary-buffer (current-buffer))
2998           (not gnus-newsgroup-prepared))
2999       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3000       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3001       (gnus-summary-mode group)
3002       (when gnus-carpal
3003         (gnus-carpal-setup-buffer 'summary))
3004       (unless gnus-single-article-buffer
3005         (make-local-variable 'gnus-article-buffer)
3006         (make-local-variable 'gnus-article-current)
3007         (make-local-variable 'gnus-original-article-buffer))
3008       (setq gnus-newsgroup-name group)
3009       ;; Set any local variables in the group parameters.
3010       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3011       t)))
3012
3013 (defun gnus-set-global-variables ()
3014   "Set the global equivalents of the buffer-local variables.
3015 They are set to the latest values they had.  These reflect the summary
3016 buffer that was in action when the last article was fetched."
3017   (when (eq major-mode 'gnus-summary-mode)
3018     (setq gnus-summary-buffer (current-buffer))
3019     (let ((name gnus-newsgroup-name)
3020           (marked gnus-newsgroup-marked)
3021           (spam gnus-newsgroup-spam-marked)
3022           (unread gnus-newsgroup-unreads)
3023           (headers gnus-current-headers)
3024           (data gnus-newsgroup-data)
3025           (summary gnus-summary-buffer)
3026           (article-buffer gnus-article-buffer)
3027           (original gnus-original-article-buffer)
3028           (gac gnus-article-current)
3029           (reffed gnus-reffed-article-number)
3030           (score-file gnus-current-score-file)
3031           (default-charset gnus-newsgroup-charset)
3032           vlist)
3033       (let ((locals gnus-newsgroup-variables))
3034         (while locals
3035           (if (consp (car locals))
3036               (push (eval (caar locals)) vlist)
3037             (push (eval (car locals)) vlist))
3038           (setq locals (cdr locals)))
3039         (setq vlist (nreverse vlist)))
3040       (save-excursion
3041         (set-buffer gnus-group-buffer)
3042         (setq gnus-newsgroup-name name
3043               gnus-newsgroup-marked marked
3044               gnus-newsgroup-spam-marked spam
3045               gnus-newsgroup-unreads unread
3046               gnus-current-headers headers
3047               gnus-newsgroup-data data
3048               gnus-article-current gac
3049               gnus-summary-buffer summary
3050               gnus-article-buffer article-buffer
3051               gnus-original-article-buffer original
3052               gnus-reffed-article-number reffed
3053               gnus-current-score-file score-file
3054               gnus-newsgroup-charset default-charset)
3055         (let ((locals gnus-newsgroup-variables))
3056           (while locals
3057             (if (consp (car locals))
3058                 (set (caar locals) (pop vlist))
3059               (set (car locals) (pop vlist)))
3060             (setq locals (cdr locals))))
3061         ;; The article buffer also has local variables.
3062         (when (gnus-buffer-live-p gnus-article-buffer)
3063           (set-buffer gnus-article-buffer)
3064           (setq gnus-summary-buffer summary))))))
3065
3066 (defun gnus-summary-article-unread-p (article)
3067   "Say whether ARTICLE is unread or not."
3068   (memq article gnus-newsgroup-unreads))
3069
3070 (defun gnus-summary-first-article-p (&optional article)
3071   "Return whether ARTICLE is the first article in the buffer."
3072   (if (not (setq article (or article (gnus-summary-article-number))))
3073       nil
3074     (eq article (caar gnus-newsgroup-data))))
3075
3076 (defun gnus-summary-last-article-p (&optional article)
3077   "Return whether ARTICLE is the last article in the buffer."
3078   (if (not (setq article (or article (gnus-summary-article-number))))
3079       ;; All non-existent numbers are the last article.  :-)
3080       t
3081     (not (cdr (gnus-data-find-list article)))))
3082
3083 (defun gnus-make-thread-indent-array ()
3084   (let ((n 200))
3085     (unless (and gnus-thread-indent-array
3086                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3087       (setq gnus-thread-indent-array (make-vector 201 "")
3088             gnus-thread-indent-array-level gnus-thread-indent-level)
3089       (while (>= n 0)
3090         (aset gnus-thread-indent-array n
3091               (make-string (* n gnus-thread-indent-level) ? ))
3092         (setq n (1- n))))))
3093
3094 (defun gnus-update-summary-mark-positions ()
3095   "Compute where the summary marks are to go."
3096   (save-excursion
3097     (when (gnus-buffer-exists-p gnus-summary-buffer)
3098       (set-buffer gnus-summary-buffer))
3099     (let ((gnus-replied-mark 129)
3100           (gnus-score-below-mark 130)
3101           (gnus-score-over-mark 130)
3102           (gnus-undownloaded-mark 131)
3103           (spec gnus-summary-line-format-spec)
3104           gnus-visual pos)
3105       (save-excursion
3106         (gnus-set-work-buffer)
3107         (let ((gnus-summary-line-format-spec spec)
3108               (gnus-newsgroup-downloadable '(0)))
3109           (gnus-summary-insert-line
3110            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3111            0 nil t 128 t nil "" nil 1)
3112           (goto-char (point-min))
3113           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3114                                              (- (point) (point-min) 1)))))
3115           (goto-char (point-min))
3116           (push (cons 'replied (and (search-forward "\201" nil t)
3117                                     (- (point) (point-min) 1)))
3118                 pos)
3119           (goto-char (point-min))
3120           (push (cons 'score (and (search-forward "\202" nil t)
3121                                   (- (point) (point-min) 1)))
3122                 pos)
3123           (goto-char (point-min))
3124           (push (cons 'download
3125                       (and (search-forward "\203" nil t)
3126                            (- (point) (point-min) 1)))
3127                 pos)))
3128       (setq gnus-summary-mark-positions pos))))
3129
3130 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3131   "Insert a dummy root in the summary buffer."
3132   (beginning-of-line)
3133   (gnus-add-text-properties
3134    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3135    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3136
3137 (defun gnus-summary-extract-address-component (from)
3138   (or (car (funcall gnus-extract-address-components from))
3139       from))
3140
3141 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3142   (let ((mail-parse-charset gnus-newsgroup-charset)
3143         ; Is it really necessary to do this next part for each summary line?
3144         ; Luckily, doesn't seem to slow things down much.
3145         (mail-parse-ignored-charsets
3146          (save-excursion (set-buffer gnus-summary-buffer)
3147                          gnus-newsgroup-ignored-charsets)))
3148     (or
3149      (and gnus-ignored-from-addresses
3150           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3151           (let ((extra-headers (mail-header-extra header))
3152                 to
3153                 newsgroups)
3154             (cond
3155              ((setq to (cdr (assq 'To extra-headers)))
3156               (concat "-> "
3157                       (inline
3158                         (gnus-summary-extract-address-component
3159                          (funcall gnus-decode-encoded-word-function to)))))
3160              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3161               (concat "=> " newsgroups)))))
3162      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3163
3164 (defun gnus-summary-insert-line (gnus-tmp-header
3165                                  gnus-tmp-level gnus-tmp-current
3166                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3167                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3168                                  &optional gnus-tmp-dummy gnus-tmp-score
3169                                  gnus-tmp-process)
3170   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3171          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3172          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3173          (gnus-tmp-score-char
3174           (if (or (null gnus-summary-default-score)
3175                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3176                       gnus-summary-zcore-fuzz))
3177               ?                         ;Whitespace
3178             (if (< gnus-tmp-score gnus-summary-default-score)
3179                 gnus-score-below-mark gnus-score-over-mark)))
3180          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3181          (gnus-tmp-replied
3182           (cond (gnus-tmp-process gnus-process-mark)
3183                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3184                  gnus-cached-mark)
3185                 (gnus-tmp-replied gnus-replied-mark)
3186                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3187                  gnus-forwarded-mark)
3188                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3189                  gnus-saved-mark)
3190                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3191                  gnus-recent-mark)
3192                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3193                  gnus-unseen-mark)
3194                 (t gnus-no-mark)))
3195          (gnus-tmp-downloaded
3196           (cond (undownloaded 
3197                  gnus-undownloaded-mark)
3198                 (gnus-newsgroup-agentized
3199                  gnus-downloaded-mark)
3200                 (t
3201                  gnus-no-mark)))
3202          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3203          (gnus-tmp-name
3204           (cond
3205            ((string-match "<[^>]+> *$" gnus-tmp-from)
3206             (let ((beg (match-beginning 0)))
3207               (or (and (string-match "^\".+\"" gnus-tmp-from)
3208                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3209                   (substring gnus-tmp-from 0 beg))))
3210            ((string-match "(.+)" gnus-tmp-from)
3211             (substring gnus-tmp-from
3212                        (1+ (match-beginning 0)) (1- (match-end 0))))
3213            (t gnus-tmp-from)))
3214          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3215          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3216          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3217          (buffer-read-only nil))
3218     (when (string= gnus-tmp-name "")
3219       (setq gnus-tmp-name gnus-tmp-from))
3220     (unless (numberp gnus-tmp-lines)
3221       (setq gnus-tmp-lines -1))
3222     (if (= gnus-tmp-lines -1)
3223         (setq gnus-tmp-lines "?")
3224       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3225     (gnus-put-text-property
3226      (point)
3227      (progn (eval gnus-summary-line-format-spec) (point))
3228      'gnus-number gnus-tmp-number)
3229     (when (gnus-visual-p 'summary-highlight 'highlight)
3230       (forward-line -1)
3231       (gnus-run-hooks 'gnus-summary-update-hook)
3232       (forward-line 1))))
3233
3234 (defun gnus-summary-update-line (&optional dont-update)
3235   "Update summary line after change."
3236   (when (and gnus-summary-default-score
3237              (not gnus-summary-inhibit-highlight))
3238     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3239            (article (gnus-summary-article-number))
3240            (score (gnus-summary-article-score article)))
3241       (unless dont-update
3242         (if (and gnus-summary-mark-below
3243                  (< (gnus-summary-article-score)
3244                     gnus-summary-mark-below))
3245             ;; This article has a low score, so we mark it as read.
3246             (when (memq article gnus-newsgroup-unreads)
3247               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3248           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3249             ;; This article was previously marked as read on account
3250             ;; of a low score, but now it has risen, so we mark it as
3251             ;; unread.
3252             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3253         (gnus-summary-update-mark
3254          (if (or (null gnus-summary-default-score)
3255                  (<= (abs (- score gnus-summary-default-score))
3256                      gnus-summary-zcore-fuzz))
3257              ?                          ;Whitespace
3258            (if (< score gnus-summary-default-score)
3259                gnus-score-below-mark gnus-score-over-mark))
3260          'score))
3261       ;; Do visual highlighting.
3262       (when (gnus-visual-p 'summary-highlight 'highlight)
3263         (gnus-run-hooks 'gnus-summary-update-hook)))))
3264
3265 (defvar gnus-tmp-new-adopts nil)
3266
3267 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3268   "Return the number of articles in THREAD.
3269 This may be 0 in some cases -- if none of the articles in
3270 the thread are to be displayed."
3271   (let* ((number
3272          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3273           (cond
3274            ((not (listp thread))
3275             1)
3276            ((and (consp thread) (cdr thread))
3277             (apply
3278              '+ 1 (mapcar
3279                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3280            ((null thread)
3281             1)
3282            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3283             1)
3284            (t 0))))
3285     (when (and level (zerop level) gnus-tmp-new-adopts)
3286       (incf number
3287             (apply '+ (mapcar
3288                        'gnus-summary-number-of-articles-in-thread
3289                        gnus-tmp-new-adopts))))
3290     (if char
3291         (if (> number 1) gnus-not-empty-thread-mark
3292           gnus-empty-thread-mark)
3293       number)))
3294
3295 (defsubst gnus-summary-line-message-size (head)
3296   "Return pretty-printed version of message size.
3297 This function is intended to be used in
3298 `gnus-summary-line-format-alist', which see."
3299   (let ((c (or (mail-header-chars head) -1)))
3300     (cond ((< c 0) "n/a")               ; chars not available
3301           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3302           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3303           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3304           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3305
3306
3307 (defun gnus-summary-set-local-parameters (group)
3308   "Go through the local params of GROUP and set all variable specs in that list."
3309   (let ((params (gnus-group-find-parameter group))
3310         (vars '(quit-config))           ; Ignore quit-config.
3311         elem)
3312     (while params
3313       (setq elem (car params)
3314             params (cdr params))
3315       (and (consp elem)                 ; Has to be a cons.
3316            (consp (cdr elem))           ; The cdr has to be a list.
3317            (symbolp (car elem))         ; Has to be a symbol in there.
3318            (not (memq (car elem) vars))
3319            (ignore-errors               ; So we set it.
3320              (push (car elem) vars)
3321              (make-local-variable (car elem))
3322              (set (car elem) (eval (nth 1 elem))))))))
3323
3324 (defun gnus-summary-read-group (group &optional show-all no-article
3325                                       kill-buffer no-display backward
3326                                       select-articles)
3327   "Start reading news in newsgroup GROUP.
3328 If SHOW-ALL is non-nil, already read articles are also listed.
3329 If NO-ARTICLE is non-nil, no article is selected initially.
3330 If NO-DISPLAY, don't generate a summary buffer."
3331   (let (result)
3332     (while (and group
3333                 (null (setq result
3334                             (let ((gnus-auto-select-next nil))
3335                               (or (gnus-summary-read-group-1
3336                                    group show-all no-article
3337                                    kill-buffer no-display
3338                                    select-articles)
3339                                   (setq show-all nil
3340                                         select-articles nil)))))
3341                 (eq gnus-auto-select-next 'quietly))
3342       (set-buffer gnus-group-buffer)
3343       ;; The entry function called above goes to the next
3344       ;; group automatically, so we go two groups back
3345       ;; if we are searching for the previous group.
3346       (when backward
3347         (gnus-group-prev-unread-group 2))
3348       (if (not (equal group (gnus-group-group-name)))
3349           (setq group (gnus-group-group-name))
3350         (setq group nil)))
3351     result))
3352
3353 (defun gnus-summary-read-group-1 (group show-all no-article
3354                                         kill-buffer no-display
3355                                         &optional select-articles)
3356   ;; Killed foreign groups can't be entered.
3357   ;;  (when (and (not (gnus-group-native-p group))
3358   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3359   ;;    (error "Dead non-native groups can't be entered"))
3360   (gnus-message 5 "Retrieving newsgroup: %s..."
3361                 (gnus-group-decoded-name group))
3362   (let* ((new-group (gnus-summary-setup-buffer group))
3363          (quit-config (gnus-group-quit-config group))
3364          (did-select (and new-group (gnus-select-newsgroup
3365                                      group show-all select-articles))))
3366     (cond
3367      ;; This summary buffer exists already, so we just select it.
3368      ((not new-group)
3369       (gnus-set-global-variables)
3370       (when kill-buffer
3371         (gnus-kill-or-deaden-summary kill-buffer))
3372       (gnus-configure-windows 'summary 'force)
3373       (gnus-set-mode-line 'summary)
3374       (gnus-summary-position-point)
3375       (message "")
3376       t)
3377      ;; We couldn't select this group.
3378      ((null did-select)
3379       (when (and (eq major-mode 'gnus-summary-mode)
3380                  (not (equal (current-buffer) kill-buffer)))
3381         (kill-buffer (current-buffer))
3382         (if (not quit-config)
3383             (progn
3384               ;; Update the info -- marks might need to be removed,
3385               ;; for instance.
3386               (gnus-summary-update-info)
3387               (set-buffer gnus-group-buffer)
3388               (gnus-group-jump-to-group group)
3389               (gnus-group-next-unread-group 1))
3390           (gnus-handle-ephemeral-exit quit-config)))
3391       (let ((grpinfo (gnus-get-info group)))
3392         (if (null (gnus-info-read grpinfo))
3393             (gnus-message 3 "Group %s contains no messages"
3394                           (gnus-group-decoded-name group))
3395           (gnus-message 3 "Can't select group")))
3396       nil)
3397      ;; The user did a `C-g' while prompting for number of articles,
3398      ;; so we exit this group.
3399      ((eq did-select 'quit)
3400       (and (eq major-mode 'gnus-summary-mode)
3401            (not (equal (current-buffer) kill-buffer))
3402            (kill-buffer (current-buffer)))
3403       (when kill-buffer
3404         (gnus-kill-or-deaden-summary kill-buffer))
3405       (if (not quit-config)
3406           (progn
3407             (set-buffer gnus-group-buffer)
3408             (gnus-group-jump-to-group group)
3409             (gnus-group-next-unread-group 1)
3410             (gnus-configure-windows 'group 'force))
3411         (gnus-handle-ephemeral-exit quit-config))
3412       ;; Finally signal the quit.
3413       (signal 'quit nil))
3414      ;; The group was successfully selected.
3415      (t
3416       (gnus-set-global-variables)
3417       ;; Save the active value in effect when the group was entered.
3418       (setq gnus-newsgroup-active
3419             (gnus-copy-sequence
3420              (gnus-active gnus-newsgroup-name)))
3421       ;; You can change the summary buffer in some way with this hook.
3422       (gnus-run-hooks 'gnus-select-group-hook)
3423       (gnus-update-format-specifications
3424        nil 'summary 'summary-mode 'summary-dummy)
3425       (gnus-update-summary-mark-positions)
3426       ;; Do score processing.
3427       (when gnus-use-scoring
3428         (gnus-possibly-score-headers))
3429       ;; Check whether to fill in the gaps in the threads.
3430       (when gnus-build-sparse-threads
3431         (gnus-build-sparse-threads))
3432       ;; Find the initial limit.
3433       (if gnus-show-threads
3434           (if show-all
3435               (let ((gnus-newsgroup-dormant nil))
3436                 (gnus-summary-initial-limit show-all))
3437             (gnus-summary-initial-limit show-all))
3438         ;; When unthreaded, all articles are always shown.
3439         (setq gnus-newsgroup-limit
3440               (mapcar
3441                (lambda (header) (mail-header-number header))
3442                gnus-newsgroup-headers)))
3443       ;; Generate the summary buffer.
3444       (unless no-display
3445         (gnus-summary-prepare))
3446       (when gnus-use-trees
3447         (gnus-tree-open group)
3448         (setq gnus-summary-highlight-line-function
3449               'gnus-tree-highlight-article))
3450       ;; If the summary buffer is empty, but there are some low-scored
3451       ;; articles or some excluded dormants, we include these in the
3452       ;; buffer.
3453       (when (and (zerop (buffer-size))
3454                  (not no-display))
3455         (cond (gnus-newsgroup-dormant
3456                (gnus-summary-limit-include-dormant))
3457               ((and gnus-newsgroup-scored show-all)
3458                (gnus-summary-limit-include-expunged t))))
3459       ;; Function `gnus-apply-kill-file' must be called in this hook.
3460       (gnus-run-hooks 'gnus-apply-kill-hook)
3461       (if (and (zerop (buffer-size))
3462                (not no-display))
3463           (progn
3464             ;; This newsgroup is empty.
3465             (gnus-summary-catchup-and-exit nil t)
3466             (gnus-message 6 "No unread news")
3467             (when kill-buffer
3468               (gnus-kill-or-deaden-summary kill-buffer))
3469             ;; Return nil from this function.
3470             nil)
3471         ;; Hide conversation thread subtrees.  We cannot do this in
3472         ;; gnus-summary-prepare-hook since kill processing may not
3473         ;; work with hidden articles.
3474         (gnus-summary-maybe-hide-threads)
3475         (when kill-buffer
3476           (gnus-kill-or-deaden-summary kill-buffer))
3477         (gnus-summary-auto-select-subject)
3478         ;; Show first unread article if requested.
3479         (if (and (not no-article)
3480                  (not no-display)
3481                  gnus-newsgroup-unreads
3482                  gnus-auto-select-first)
3483             (progn
3484               (gnus-configure-windows 'summary)
3485               (let ((art (gnus-summary-article-number)))
3486                 (unless (and (not gnus-plugged)
3487                              (or (memq art gnus-newsgroup-undownloaded)
3488                                  (memq art gnus-newsgroup-downloadable)))
3489                   (gnus-summary-goto-article art))))
3490           ;; Don't select any articles.
3491           (gnus-summary-position-point)
3492           (gnus-configure-windows 'summary 'force)
3493           (gnus-set-mode-line 'summary))
3494         (when (get-buffer-window gnus-group-buffer t)
3495           ;; Gotta use windows, because recenter does weird stuff if
3496           ;; the current buffer ain't the displayed window.
3497           (let ((owin (selected-window)))
3498             (select-window (get-buffer-window gnus-group-buffer t))
3499             (when (gnus-group-goto-group group)
3500               (recenter))
3501             (select-window owin)))
3502         ;; Mark this buffer as "prepared".
3503         (setq gnus-newsgroup-prepared t)
3504         (gnus-run-hooks 'gnus-summary-prepared-hook)
3505         (unless (gnus-ephemeral-group-p group)
3506           (gnus-group-update-group group))
3507         t)))))
3508
3509 (defun gnus-summary-auto-select-subject ()
3510   "Select the subject line on initial group entry."
3511   (goto-char (point-min))
3512   (cond
3513    ((eq gnus-auto-select-subject 'best)
3514     (gnus-summary-best-unread-subject))
3515    ((eq gnus-auto-select-subject 'unread)
3516     (gnus-summary-first-unread-subject))
3517    ((eq gnus-auto-select-subject 'unseen)
3518     (gnus-summary-first-unseen-subject))
3519    ((eq gnus-auto-select-subject 'unseen-or-unread)
3520     (gnus-summary-first-unseen-or-unread-subject))
3521    ((eq gnus-auto-select-subject 'first)
3522     ;; Do nothing.
3523     )
3524    ((gnus-functionp gnus-auto-select-subject)
3525     (funcall gnus-auto-select-subject))))
3526
3527 (defun gnus-summary-prepare ()
3528   "Generate the summary buffer."
3529   (interactive)
3530   (let ((buffer-read-only nil))
3531     (erase-buffer)
3532     (setq gnus-newsgroup-data nil
3533           gnus-newsgroup-data-reverse nil)
3534     (gnus-run-hooks 'gnus-summary-generate-hook)
3535     ;; Generate the buffer, either with threads or without.
3536     (when gnus-newsgroup-headers
3537       (gnus-summary-prepare-threads
3538        (if gnus-show-threads
3539            (gnus-sort-gathered-threads
3540             (funcall gnus-summary-thread-gathering-function
3541                      (gnus-sort-threads
3542                       (gnus-cut-threads (gnus-make-threads)))))
3543          ;; Unthreaded display.
3544          (gnus-sort-articles gnus-newsgroup-headers))))
3545     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3546     ;; Call hooks for modifying summary buffer.
3547     (goto-char (point-min))
3548     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3549
3550 (defsubst gnus-general-simplify-subject (subject)
3551   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3552   (setq subject
3553         (cond
3554          ;; Truncate the subject.
3555          (gnus-simplify-subject-functions
3556           (gnus-map-function gnus-simplify-subject-functions subject))
3557          ((numberp gnus-summary-gather-subject-limit)
3558           (setq subject (gnus-simplify-subject-re subject))
3559           (if (> (length subject) gnus-summary-gather-subject-limit)
3560               (substring subject 0 gnus-summary-gather-subject-limit)
3561             subject))
3562          ;; Fuzzily simplify it.
3563          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3564           (gnus-simplify-subject-fuzzy subject))
3565          ;; Just remove the leading "Re:".
3566          (t
3567           (gnus-simplify-subject-re subject))))
3568
3569   (if (and gnus-summary-gather-exclude-subject
3570            (string-match gnus-summary-gather-exclude-subject subject))
3571       nil                         ; This article shouldn't be gathered
3572     subject))
3573
3574 (defun gnus-summary-simplify-subject-query ()
3575   "Query where the respool algorithm would put this article."
3576   (interactive)
3577   (gnus-summary-select-article)
3578   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3579
3580 (defun gnus-gather-threads-by-subject (threads)
3581   "Gather threads by looking at Subject headers."
3582   (if (not gnus-summary-make-false-root)
3583       threads
3584     (let ((hashtb (gnus-make-hashtable 1024))
3585           (prev threads)
3586           (result threads)
3587           subject hthread whole-subject)
3588       (while threads
3589         (setq subject (gnus-general-simplify-subject
3590                        (setq whole-subject (mail-header-subject
3591                                             (caar threads)))))
3592         (when subject
3593           (if (setq hthread (gnus-gethash subject hashtb))
3594               (progn
3595                 ;; We enter a dummy root into the thread, if we
3596                 ;; haven't done that already.
3597                 (unless (stringp (caar hthread))
3598                   (setcar hthread (list whole-subject (car hthread))))
3599                 ;; We add this new gathered thread to this gathered
3600                 ;; thread.
3601                 (setcdr (car hthread)
3602                         (nconc (cdar hthread) (list (car threads))))
3603                 ;; Remove it from the list of threads.
3604                 (setcdr prev (cdr threads))
3605                 (setq threads prev))
3606             ;; Enter this thread into the hash table.
3607             (gnus-sethash subject
3608                           (if gnus-summary-make-false-root-always
3609                               (progn
3610                                 ;; If you want a dummy root above all
3611                                 ;; threads...
3612                                 (setcar threads (list whole-subject
3613                                                       (car threads)))
3614                                 threads)
3615                             threads)
3616                           hashtb)))
3617         (setq prev threads)
3618         (setq threads (cdr threads)))
3619       result)))
3620
3621 (defun gnus-gather-threads-by-references (threads)
3622   "Gather threads by looking at References headers."
3623   (let ((idhashtb (gnus-make-hashtable 1024))
3624         (thhashtb (gnus-make-hashtable 1024))
3625         (prev threads)
3626         (result threads)
3627         ids references id gthread gid entered ref)
3628     (while threads
3629       (when (setq references (mail-header-references (caar threads)))
3630         (setq id (mail-header-id (caar threads))
3631               ids (inline (gnus-split-references references))
3632               entered nil)
3633         (while (setq ref (pop ids))
3634           (setq ids (delete ref ids))
3635           (if (not (setq gid (gnus-gethash ref idhashtb)))
3636               (progn
3637                 (gnus-sethash ref id idhashtb)
3638                 (gnus-sethash id threads thhashtb))
3639             (setq gthread (gnus-gethash gid thhashtb))
3640             (unless entered
3641               ;; We enter a dummy root into the thread, if we
3642               ;; haven't done that already.
3643               (unless (stringp (caar gthread))
3644                 (setcar gthread (list (mail-header-subject (caar gthread))
3645                                       (car gthread))))
3646               ;; We add this new gathered thread to this gathered
3647               ;; thread.
3648               (setcdr (car gthread)
3649                       (nconc (cdar gthread) (list (car threads)))))
3650             ;; Add it into the thread hash table.
3651             (gnus-sethash id gthread thhashtb)
3652             (setq entered t)
3653             ;; Remove it from the list of threads.
3654             (setcdr prev (cdr threads))
3655             (setq threads prev))))
3656       (setq prev threads)
3657       (setq threads (cdr threads)))
3658     result))
3659
3660 (defun gnus-sort-gathered-threads (threads)
3661   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3662   (let ((result threads))
3663     (while threads
3664       (when (stringp (caar threads))
3665         (setcdr (car threads)
3666                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3667       (setq threads (cdr threads)))
3668     result))
3669
3670 (defun gnus-thread-loop-p (root thread)
3671   "Say whether ROOT is in THREAD."
3672   (let ((stack (list thread))
3673         (infloop 0)
3674         th)
3675     (while (setq thread (pop stack))
3676       (setq th (cdr thread))
3677       (while (and th
3678                   (not (eq (caar th) root)))
3679         (pop th))
3680       (if th
3681           ;; We have found a loop.
3682           (let (ref-dep)
3683             (setcdr thread (delq (car th) (cdr thread)))
3684             (if (boundp (setq ref-dep (intern "none"
3685                                               gnus-newsgroup-dependencies)))
3686                 (setcdr (symbol-value ref-dep)
3687                         (nconc (cdr (symbol-value ref-dep))
3688                                (list (car th))))
3689               (set ref-dep (list nil (car th))))
3690             (setq infloop 1
3691                   stack nil))
3692         ;; Push all the subthreads onto the stack.
3693         (push (cdr thread) stack)))
3694     infloop))
3695
3696 (defun gnus-make-threads ()
3697   "Go through the dependency hashtb and find the roots.  Return all threads."
3698   (let (threads)
3699     (while (catch 'infloop
3700              (mapatoms
3701               (lambda (refs)
3702                 ;; Deal with self-referencing References loops.
3703                 (when (and (car (symbol-value refs))
3704                            (not (zerop
3705                                  (apply
3706                                   '+
3707                                   (mapcar
3708                                    (lambda (thread)
3709                                      (gnus-thread-loop-p
3710                                       (car (symbol-value refs)) thread))
3711                                    (cdr (symbol-value refs)))))))
3712                   (setq threads nil)
3713                   (throw 'infloop t))
3714                 (unless (car (symbol-value refs))
3715                   ;; These threads do not refer back to any other
3716                   ;; articles, so they're roots.
3717                   (setq threads (append (cdr (symbol-value refs)) threads))))
3718               gnus-newsgroup-dependencies)))
3719     threads))
3720
3721 ;; Build the thread tree.
3722 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3723   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3724
3725 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3726 if it was already present.
3727
3728 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3729 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3730 Message-IDs will be renamed to a unique Message-ID before being
3731 entered.
3732
3733 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3734   (let* ((id (mail-header-id header))
3735          (id-dep (and id (intern id dependencies)))
3736          parent-id ref ref-dep ref-header replaced)
3737     ;; Enter this `header' in the `dependencies' table.
3738     (cond
3739      ((not id-dep)
3740       (setq header nil))
3741      ;; The first two cases do the normal part: enter a new `header'
3742      ;; in the `dependencies' table.
3743      ((not (boundp id-dep))
3744       (set id-dep (list header)))
3745      ((null (car (symbol-value id-dep)))
3746       (setcar (symbol-value id-dep) header))
3747
3748      ;; From here the `header' was already present in the
3749      ;; `dependencies' table.
3750      (force-new
3751       ;; Overrides an existing entry;
3752       ;; just set the header part of the entry.
3753       (setcar (symbol-value id-dep) header)
3754       (setq replaced t))
3755
3756      ;; Renames the existing `header' to a unique Message-ID.
3757      ((not gnus-summary-ignore-duplicates)
3758       ;; An article with this Message-ID has already been seen.
3759       ;; We rename the Message-ID.
3760       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3761            (list header))
3762       (mail-header-set-id header id))
3763
3764      ;; The last case ignores an existing entry, except it adds any
3765      ;; additional Xrefs (in case the two articles came from different
3766      ;; servers.
3767      ;; Also sets `header' to `nil' meaning that the `dependencies'
3768      ;; table was *not* modified.
3769      (t
3770       (mail-header-set-xref
3771        (car (symbol-value id-dep))
3772        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3773                    "")
3774                (or (mail-header-xref header) "")))
3775       (setq header nil)))
3776
3777     (when (and header (not replaced))
3778       ;; First check that we are not creating a References loop.
3779       (setq parent-id (gnus-parent-id (mail-header-references header)))
3780       (setq ref parent-id)
3781       (while (and ref
3782                   (setq ref-dep (intern-soft ref dependencies))
3783                   (boundp ref-dep)
3784                   (setq ref-header (car (symbol-value ref-dep))))
3785         (if (string= id ref)
3786             ;; Yuk!  This is a reference loop.  Make the article be a
3787             ;; root article.
3788             (progn
3789               (mail-header-set-references (car (symbol-value id-dep)) "none")
3790               (setq ref nil)
3791               (setq parent-id nil))
3792           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3793       (setq ref-dep (intern (or parent-id "none") dependencies))
3794       (if (boundp ref-dep)
3795           (setcdr (symbol-value ref-dep)
3796                   (nconc (cdr (symbol-value ref-dep))
3797                          (list (symbol-value id-dep))))
3798         (set ref-dep (list nil (symbol-value id-dep)))))
3799     header))
3800
3801 (defun gnus-extract-message-id-from-in-reply-to (string)
3802   (if (string-match "<[^>]+>" string)
3803       (substring string (match-beginning 0) (match-end 0))
3804     nil))
3805
3806 (defun gnus-build-sparse-threads ()
3807   (let ((headers gnus-newsgroup-headers)
3808         (mail-parse-charset gnus-newsgroup-charset)
3809         (gnus-summary-ignore-duplicates t)
3810         header references generation relations
3811         subject child end new-child date)
3812     ;; First we create an alist of generations/relations, where
3813     ;; generations is how much we trust the relation, and the relation
3814     ;; is parent/child.
3815     (gnus-message 7 "Making sparse threads...")
3816     (save-excursion
3817       (nnheader-set-temp-buffer " *gnus sparse threads*")
3818       (while (setq header (pop headers))
3819         (when (and (setq references (mail-header-references header))
3820                    (not (string= references "")))
3821           (insert references)
3822           (setq child (mail-header-id header)
3823                 subject (mail-header-subject header)
3824                 date (mail-header-date header)
3825                 generation 0)
3826           (while (search-backward ">" nil t)
3827             (setq end (1+ (point)))
3828             (when (search-backward "<" nil t)
3829               (setq new-child (buffer-substring (point) end))
3830               (push (list (incf generation)
3831                           child (setq child new-child)
3832                           subject date)
3833                     relations)))
3834           (when child
3835             (push (list (1+ generation) child nil subject) relations))
3836           (erase-buffer)))
3837       (kill-buffer (current-buffer)))
3838     ;; Sort over trustworthiness.
3839     (mapcar
3840      (lambda (relation)
3841        (when (gnus-dependencies-add-header
3842               (make-full-mail-header
3843                gnus-reffed-article-number
3844                (nth 3 relation) "" (or (nth 4 relation) "")
3845                (nth 1 relation)
3846                (or (nth 2 relation) "") 0 0 "")
3847               gnus-newsgroup-dependencies nil)
3848          (push gnus-reffed-article-number gnus-newsgroup-limit)
3849          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3850          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3851                gnus-newsgroup-reads)
3852          (decf gnus-reffed-article-number)))
3853      (sort relations 'car-less-than-car))
3854     (gnus-message 7 "Making sparse threads...done")))
3855
3856 (defun gnus-build-old-threads ()
3857   ;; Look at all the articles that refer back to old articles, and
3858   ;; fetch the headers for the articles that aren't there.  This will
3859   ;; build complete threads - if the roots haven't been expired by the
3860   ;; server, that is.
3861   (let ((mail-parse-charset gnus-newsgroup-charset)
3862         id heads)
3863     (mapatoms
3864      (lambda (refs)
3865        (when (not (car (symbol-value refs)))
3866          (setq heads (cdr (symbol-value refs)))
3867          (while heads
3868            (if (memq (mail-header-number (caar heads))
3869                      gnus-newsgroup-dormant)
3870                (setq heads (cdr heads))
3871              (setq id (symbol-name refs))
3872              (while (and (setq id (gnus-build-get-header id))
3873                          (not (car (gnus-id-to-thread id)))))
3874              (setq heads nil)))))
3875      gnus-newsgroup-dependencies)))
3876
3877 ;; This function has to be called with point after the article number
3878 ;; on the beginning of the line.
3879 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3880   (let ((eol (gnus-point-at-eol))
3881         (buffer (current-buffer))
3882         header references in-reply-to)
3883
3884     ;; overview: [num subject from date id refs chars lines misc]
3885     (unwind-protect
3886         (let (x)
3887           (narrow-to-region (point) eol)
3888           (unless (eobp)
3889             (forward-char))
3890
3891           (setq header
3892                 (make-full-mail-header
3893                  number                 ; number
3894                  (condition-case ()     ; subject
3895                      (funcall gnus-decode-encoded-word-function
3896                               (setq x (nnheader-nov-field)))
3897                    (error x))
3898                  (condition-case ()     ; from
3899                      (funcall gnus-decode-encoded-word-function
3900                               (setq x (nnheader-nov-field)))
3901                    (error x))
3902                  (nnheader-nov-field)   ; date
3903                  (nnheader-nov-read-message-id) ; id
3904                  (setq references (nnheader-nov-field)) ; refs
3905                  (nnheader-nov-read-integer) ; chars
3906                  (nnheader-nov-read-integer) ; lines
3907                  (unless (eobp)
3908                    (if (looking-at "Xref: ")
3909                        (goto-char (match-end 0)))
3910                    (nnheader-nov-field)) ; Xref
3911                  (nnheader-nov-parse-extra)))) ; extra
3912
3913       (widen))
3914
3915     (when (and (string= references "")
3916                (setq in-reply-to (mail-header-extra header))
3917                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3918       (mail-header-set-references
3919        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3920
3921     (when gnus-alter-header-function
3922       (funcall gnus-alter-header-function header))
3923     (gnus-dependencies-add-header header dependencies force-new)))
3924
3925 (defun gnus-build-get-header (id)
3926   "Look through the buffer of NOV lines and find the header to ID.
3927 Enter this line into the dependencies hash table, and return
3928 the id of the parent article (if any)."
3929   (let ((deps gnus-newsgroup-dependencies)
3930         found header)
3931     (prog1
3932         (save-excursion
3933           (set-buffer nntp-server-buffer)
3934           (let ((case-fold-search nil))
3935             (goto-char (point-min))
3936             (while (and (not found)
3937                         (search-forward id nil t))
3938               (beginning-of-line)
3939               (setq found (looking-at
3940                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3941                                    (regexp-quote id))))
3942               (or found (beginning-of-line 2)))
3943             (when found
3944               (beginning-of-line)
3945               (and
3946                (setq header (gnus-nov-parse-line
3947                              (read (current-buffer)) deps))
3948                (gnus-parent-id (mail-header-references header))))))
3949       (when header
3950         (let ((number (mail-header-number header)))
3951           (push number gnus-newsgroup-limit)
3952           (push header gnus-newsgroup-headers)
3953           (if (memq number gnus-newsgroup-unselected)
3954               (progn
3955                 (setq gnus-newsgroup-unreads
3956                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3957                                                number))
3958                 (setq gnus-newsgroup-unselected
3959                       (delq number gnus-newsgroup-unselected)))
3960             (push number gnus-newsgroup-ancient)))))))
3961
3962 (defun gnus-build-all-threads ()
3963   "Read all the headers."
3964   (let ((gnus-summary-ignore-duplicates t)
3965         (mail-parse-charset gnus-newsgroup-charset)
3966         (dependencies gnus-newsgroup-dependencies)
3967         header article)
3968     (save-excursion
3969       (set-buffer nntp-server-buffer)
3970       (let ((case-fold-search nil))
3971         (goto-char (point-min))
3972         (while (not (eobp))
3973           (ignore-errors
3974             (setq article (read (current-buffer))
3975                   header (gnus-nov-parse-line article dependencies)))
3976           (when header
3977             (save-excursion
3978               (set-buffer gnus-summary-buffer)
3979               (push header gnus-newsgroup-headers)
3980               (if (memq (setq article (mail-header-number header))
3981                         gnus-newsgroup-unselected)
3982                   (progn
3983                     (setq gnus-newsgroup-unreads
3984                           (gnus-add-to-sorted-list
3985                            gnus-newsgroup-unreads article))
3986                     (setq gnus-newsgroup-unselected
3987                           (delq article gnus-newsgroup-unselected)))
3988                 (push article gnus-newsgroup-ancient)))
3989             (forward-line 1)))))))
3990
3991 (defun gnus-summary-update-article-line (article header)
3992   "Update the line for ARTICLE using HEADERS."
3993   (let* ((id (mail-header-id header))
3994          (thread (gnus-id-to-thread id)))
3995     (unless thread
3996       (error "Article in no thread"))
3997     ;; Update the thread.
3998     (setcar thread header)
3999     (gnus-summary-goto-subject article)
4000     (let* ((datal (gnus-data-find-list article))
4001            (data (car datal))
4002            (length (when (cdr datal)
4003                      (- (gnus-data-pos data)
4004                         (gnus-data-pos (cadr datal)))))
4005            (buffer-read-only nil)
4006            (level (gnus-summary-thread-level)))
4007       (gnus-delete-line)
4008       (gnus-summary-insert-line
4009        header level nil 
4010        (memq article gnus-newsgroup-undownloaded)
4011        (gnus-article-mark article)
4012        (memq article gnus-newsgroup-replied)
4013        (memq article gnus-newsgroup-expirable)
4014        ;; Only insert the Subject string when it's different
4015        ;; from the previous Subject string.
4016        (if (and
4017             gnus-show-threads
4018             (gnus-subject-equal
4019              (condition-case ()
4020                  (mail-header-subject
4021                   (gnus-data-header
4022                    (cadr
4023                     (gnus-data-find-list
4024                      article
4025                      (gnus-data-list t)))))
4026                ;; Error on the side of excessive subjects.
4027                (error ""))
4028              (mail-header-subject header)))
4029            ""
4030          (mail-header-subject header))
4031        nil (cdr (assq article gnus-newsgroup-scored))
4032        (memq article gnus-newsgroup-processable))
4033       (when length
4034         (gnus-data-update-list
4035          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
4036
4037 (defun gnus-summary-update-article (article &optional iheader)
4038   "Update ARTICLE in the summary buffer."
4039   (set-buffer gnus-summary-buffer)
4040   (let* ((header (gnus-summary-article-header article))
4041          (id (mail-header-id header))
4042          (data (gnus-data-find article))
4043          (thread (gnus-id-to-thread id))
4044          (references (mail-header-references header))
4045          (parent
4046           (gnus-id-to-thread
4047            (or (gnus-parent-id
4048                 (when (and references
4049                            (not (equal "" references)))
4050                   references))
4051                "none")))
4052          (buffer-read-only nil)
4053          (old (car thread)))
4054     (when thread
4055       (unless iheader
4056         (setcar thread nil)
4057         (when parent
4058           (delq thread parent)))
4059       (if (gnus-summary-insert-subject id header)
4060           ;; Set the (possibly) new article number in the data structure.
4061           (gnus-data-set-number data (gnus-id-to-article id))
4062         (setcar thread old)
4063         nil))))
4064
4065 (defun gnus-rebuild-thread (id &optional line)
4066   "Rebuild the thread containing ID.
4067 If LINE, insert the rebuilt thread starting on line LINE."
4068   (let ((buffer-read-only nil)
4069         old-pos current thread data)
4070     (if (not gnus-show-threads)
4071         (setq thread (list (car (gnus-id-to-thread id))))
4072       ;; Get the thread this article is part of.
4073       (setq thread (gnus-remove-thread id)))
4074     (setq old-pos (gnus-point-at-bol))
4075     (setq current (save-excursion
4076                     (and (re-search-backward "[\r\n]" nil t)
4077                          (gnus-summary-article-number))))
4078     ;; If this is a gathered thread, we have to go some re-gathering.
4079     (when (stringp (car thread))
4080       (let ((subject (car thread))
4081             roots thr)
4082         (setq thread (cdr thread))
4083         (while thread
4084           (unless (memq (setq thr (gnus-id-to-thread
4085                                    (gnus-root-id
4086                                     (mail-header-id (caar thread)))))
4087                         roots)
4088             (push thr roots))
4089           (setq thread (cdr thread)))
4090         ;; We now have all (unique) roots.
4091         (if (= (length roots) 1)
4092             ;; All the loose roots are now one solid root.
4093             (setq thread (car roots))
4094           (setq thread (cons subject (gnus-sort-threads roots))))))
4095     (let (threads)
4096       ;; We then insert this thread into the summary buffer.
4097       (when line
4098         (goto-char (point-min))
4099         (forward-line (1- line)))
4100       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4101         (if gnus-show-threads
4102             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4103           (gnus-summary-prepare-unthreaded thread))
4104         (setq data (nreverse gnus-newsgroup-data))
4105         (setq threads gnus-newsgroup-threads))
4106       ;; We splice the new data into the data structure.
4107       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4108       ;;!!! then we want to insert at the beginning of the buffer.
4109       ;;!!! That happens to be true with Gnus now, but that may
4110       ;;!!! change in the future.  Perhaps.
4111       (gnus-data-enter-list
4112        (if line nil current) data (- (point) old-pos))
4113       (setq gnus-newsgroup-threads
4114             (nconc threads gnus-newsgroup-threads))
4115       (gnus-data-compute-positions))))
4116
4117 (defun gnus-number-to-header (number)
4118   "Return the header for article NUMBER."
4119   (let ((headers gnus-newsgroup-headers))
4120     (while (and headers
4121                 (not (= number (mail-header-number (car headers)))))
4122       (pop headers))
4123     (when headers
4124       (car headers))))
4125
4126 (defun gnus-parent-headers (in-headers &optional generation)
4127   "Return the headers of the GENERATIONeth parent of HEADERS."
4128   (unless generation
4129     (setq generation 1))
4130   (let ((parent t)
4131         (headers in-headers)
4132         references)
4133     (while (and parent
4134                 (not (zerop generation))
4135                 (setq references (mail-header-references headers)))
4136       (setq headers (if (and references
4137                              (setq parent (gnus-parent-id references)))
4138                         (car (gnus-id-to-thread parent))
4139                       nil))
4140       (decf generation))
4141     (and (not (eq headers in-headers))
4142          headers)))
4143
4144 (defun gnus-id-to-thread (id)
4145   "Return the (sub-)thread where ID appears."
4146   (gnus-gethash id gnus-newsgroup-dependencies))
4147
4148 (defun gnus-id-to-article (id)
4149   "Return the article number of ID."
4150   (let ((thread (gnus-id-to-thread id)))
4151     (when (and thread
4152                (car thread))
4153       (mail-header-number (car thread)))))
4154
4155 (defun gnus-id-to-header (id)
4156   "Return the article headers of ID."
4157   (car (gnus-id-to-thread id)))
4158
4159 (defun gnus-article-displayed-root-p (article)
4160   "Say whether ARTICLE is a root(ish) article."
4161   (let ((level (gnus-summary-thread-level article))
4162         (refs (mail-header-references  (gnus-summary-article-header article)))
4163         particle)
4164     (cond
4165      ((null level) nil)
4166      ((zerop level) t)
4167      ((null refs) t)
4168      ((null (gnus-parent-id refs)) t)
4169      ((and (= 1 level)
4170            (null (setq particle (gnus-id-to-article
4171                                  (gnus-parent-id refs))))
4172            (null (gnus-summary-thread-level particle)))))))
4173
4174 (defun gnus-root-id (id)
4175   "Return the id of the root of the thread where ID appears."
4176   (let (last-id prev)
4177     (while (and id (setq prev (car (gnus-id-to-thread id))))
4178       (setq last-id id
4179             id (gnus-parent-id (mail-header-references prev))))
4180     last-id))
4181
4182 (defun gnus-articles-in-thread (thread)
4183   "Return the list of articles in THREAD."
4184   (cons (mail-header-number (car thread))
4185         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4186
4187 (defun gnus-remove-thread (id &optional dont-remove)
4188   "Remove the thread that has ID in it."
4189   (let (headers thread last-id)
4190     ;; First go up in this thread until we find the root.
4191     (setq last-id (gnus-root-id id)
4192           headers (message-flatten-list (gnus-id-to-thread last-id)))
4193     ;; We have now found the real root of this thread.  It might have
4194     ;; been gathered into some loose thread, so we have to search
4195     ;; through the threads to find the thread we wanted.
4196     (let ((threads gnus-newsgroup-threads)
4197           sub)
4198       (while threads
4199         (setq sub (car threads))
4200         (if (stringp (car sub))
4201             ;; This is a gathered thread, so we look at the roots
4202             ;; below it to find whether this article is in this
4203             ;; gathered root.
4204             (progn
4205               (setq sub (cdr sub))
4206               (while sub
4207                 (when (member (caar sub) headers)
4208                   (setq thread (car threads)
4209                         threads nil
4210                         sub nil))
4211                 (setq sub (cdr sub))))
4212           ;; It's an ordinary thread, so we check it.
4213           (when (eq (car sub) (car headers))
4214             (setq thread sub
4215                   threads nil)))
4216         (setq threads (cdr threads)))
4217       ;; If this article is in no thread, then it's a root.
4218       (if thread
4219           (unless dont-remove
4220             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4221         (setq thread (gnus-id-to-thread last-id)))
4222       (when thread
4223         (prog1
4224             thread                      ; We return this thread.
4225           (unless dont-remove
4226             (if (stringp (car thread))
4227                 (progn
4228                   ;; If we use dummy roots, then we have to remove the
4229                   ;; dummy root as well.
4230                   (when (eq gnus-summary-make-false-root 'dummy)
4231                     ;; We go to the dummy root by going to
4232                     ;; the first sub-"thread", and then one line up.
4233                     (gnus-summary-goto-article
4234                      (mail-header-number (caadr thread)))
4235                     (forward-line -1)
4236                     (gnus-delete-line)
4237                     (gnus-data-compute-positions))
4238                   (setq thread (cdr thread))
4239                   (while thread
4240                     (gnus-remove-thread-1 (car thread))
4241                     (setq thread (cdr thread))))
4242               (gnus-remove-thread-1 thread))))))))
4243
4244 (defun gnus-remove-thread-1 (thread)
4245   "Remove the thread THREAD recursively."
4246   (let ((number (mail-header-number (pop thread)))
4247         d)
4248     (setq thread (reverse thread))
4249     (while thread
4250       (gnus-remove-thread-1 (pop thread)))
4251     (when (setq d (gnus-data-find number))
4252       (goto-char (gnus-data-pos d))
4253       (gnus-summary-show-thread)
4254       (gnus-data-remove
4255        number
4256        (- (gnus-point-at-bol)
4257           (prog1
4258               (1+ (gnus-point-at-eol))
4259             (gnus-delete-line)))))))
4260
4261 (defun gnus-sort-threads-1 (threads func)
4262   (sort (mapcar (lambda (thread)
4263                   (cons (car thread)
4264                         (and (cdr thread)
4265                              (gnus-sort-threads-1 (cdr thread) func))))
4266                 threads) func))
4267
4268 (defun gnus-sort-threads (threads)
4269   "Sort THREADS."
4270   (if (not gnus-thread-sort-functions)
4271       threads
4272     (gnus-message 8 "Sorting threads...")
4273     (let ((max-lisp-eval-depth 5000))
4274       (prog1 (gnus-sort-threads-1
4275          threads
4276          (gnus-make-sort-function gnus-thread-sort-functions))
4277         (gnus-message 8 "Sorting threads...done")))))
4278
4279 (defun gnus-sort-articles (articles)
4280   "Sort ARTICLES."
4281   (when gnus-article-sort-functions
4282     (gnus-message 7 "Sorting articles...")
4283     (prog1
4284         (setq gnus-newsgroup-headers
4285               (sort articles (gnus-make-sort-function
4286                               gnus-article-sort-functions)))
4287       (gnus-message 7 "Sorting articles...done"))))
4288
4289 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4290 (defmacro gnus-thread-header (thread)
4291   "Return header of first article in THREAD.
4292 Note that THREAD must never, ever be anything else than a variable -
4293 using some other form will lead to serious barfage."
4294   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4295   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4296   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4297         (vector thread) 2))
4298
4299 (defsubst gnus-article-sort-by-number (h1 h2)
4300   "Sort articles by article number."
4301   (< (mail-header-number h1)
4302      (mail-header-number h2)))
4303
4304 (defun gnus-thread-sort-by-number (h1 h2)
4305   "Sort threads by root article number."
4306   (gnus-article-sort-by-number
4307    (gnus-thread-header h1) (gnus-thread-header h2)))
4308
4309 (defsubst gnus-article-sort-by-random (h1 h2)
4310   "Sort articles by article number."
4311   (zerop (random 2)))
4312
4313 (defun gnus-thread-sort-by-random (h1 h2)
4314   "Sort threads by root article number."
4315   (gnus-article-sort-by-random
4316    (gnus-thread-header h1) (gnus-thread-header h2)))
4317
4318 (defsubst gnus-article-sort-by-lines (h1 h2)
4319   "Sort articles by article Lines header."
4320   (< (mail-header-lines h1)
4321      (mail-header-lines h2)))
4322
4323 (defun gnus-thread-sort-by-lines (h1 h2)
4324   "Sort threads by root article Lines header."
4325   (gnus-article-sort-by-lines
4326    (gnus-thread-header h1) (gnus-thread-header h2)))
4327
4328 (defsubst gnus-article-sort-by-chars (h1 h2)
4329   "Sort articles by octet length."
4330   (< (mail-header-chars h1)
4331      (mail-header-chars h2)))
4332
4333 (defun gnus-thread-sort-by-chars (h1 h2)
4334   "Sort threads by root article octet length."
4335   (gnus-article-sort-by-chars
4336    (gnus-thread-header h1) (gnus-thread-header h2)))
4337
4338 (defsubst gnus-article-sort-by-author (h1 h2)
4339   "Sort articles by root author."
4340   (string-lessp
4341    (let ((extract (funcall
4342                    gnus-extract-address-components
4343                    (mail-header-from h1))))
4344      (or (car extract) (cadr extract) ""))
4345    (let ((extract (funcall
4346                    gnus-extract-address-components
4347                    (mail-header-from h2))))
4348      (or (car extract) (cadr extract) ""))))
4349
4350 (defun gnus-thread-sort-by-author (h1 h2)
4351   "Sort threads by root author."
4352   (gnus-article-sort-by-author
4353    (gnus-thread-header h1)  (gnus-thread-header h2)))
4354
4355 (defsubst gnus-article-sort-by-subject (h1 h2)
4356   "Sort articles by root subject."
4357   (string-lessp
4358    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4359    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4360
4361 (defun gnus-thread-sort-by-subject (h1 h2)
4362   "Sort threads by root subject."
4363   (gnus-article-sort-by-subject
4364    (gnus-thread-header h1) (gnus-thread-header h2)))
4365
4366 (defsubst gnus-article-sort-by-date (h1 h2)
4367   "Sort articles by root article date."
4368   (time-less-p
4369    (gnus-date-get-time (mail-header-date h1))
4370    (gnus-date-get-time (mail-header-date h2))))
4371
4372 (defun gnus-thread-sort-by-date (h1 h2)
4373   "Sort threads by root article date."
4374   (gnus-article-sort-by-date
4375    (gnus-thread-header h1) (gnus-thread-header h2)))
4376
4377 (defsubst gnus-article-sort-by-score (h1 h2)
4378   "Sort articles by root article score.
4379 Unscored articles will be counted as having a score of zero."
4380   (> (or (cdr (assq (mail-header-number h1)
4381                     gnus-newsgroup-scored))
4382          gnus-summary-default-score 0)
4383      (or (cdr (assq (mail-header-number h2)
4384                     gnus-newsgroup-scored))
4385          gnus-summary-default-score 0)))
4386
4387 (defun gnus-thread-sort-by-score (h1 h2)
4388   "Sort threads by root article score."
4389   (gnus-article-sort-by-score
4390    (gnus-thread-header h1) (gnus-thread-header h2)))
4391
4392 (defun gnus-thread-sort-by-total-score (h1 h2)
4393   "Sort threads by the sum of all scores in the thread.
4394 Unscored articles will be counted as having a score of zero."
4395   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4396
4397 (defun gnus-thread-total-score (thread)
4398   ;; This function find the total score of THREAD.
4399   (cond
4400    ((null thread)
4401     0)
4402    ((consp thread)
4403     (if (stringp (car thread))
4404         (apply gnus-thread-score-function 0
4405                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4406       (gnus-thread-total-score-1 thread)))
4407    (t
4408     (gnus-thread-total-score-1 (list thread)))))
4409
4410 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4411   "Sort threads such that the thread with the most recently arrived article comes first."
4412   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4413
4414 (defun gnus-thread-highest-number (thread)
4415   "Return the highest article number in THREAD."
4416   (apply 'max (mapcar (lambda (header)
4417                         (mail-header-number header))
4418                       (message-flatten-list thread))))
4419
4420 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4421   "Sort threads such that the thread with the most recently dated article comes first."
4422   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4423
4424 (defun gnus-thread-latest-date (thread)
4425   "Return the highest article date in THREAD."
4426   (let ((previous-time 0))
4427     (apply 'max
4428            (mapcar
4429             (lambda (header)
4430               (setq previous-time
4431                     (time-to-seconds
4432                      (condition-case ()
4433                          (mail-header-parse-date (mail-header-date header))
4434                        (error previous-time)))))
4435             (sort
4436              (message-flatten-list thread)
4437              (lambda (h1 h2)
4438                (< (mail-header-number h1)
4439                   (mail-header-number h2))))))))
4440
4441 (defun gnus-thread-total-score-1 (root)
4442   ;; This function find the total score of the thread below ROOT.
4443   (setq root (car root))
4444   (apply gnus-thread-score-function
4445          (or (append
4446               (mapcar 'gnus-thread-total-score
4447                       (cdr (gnus-id-to-thread (mail-header-id root))))
4448               (when (> (mail-header-number root) 0)
4449                 (list (or (cdr (assq (mail-header-number root)
4450                                      gnus-newsgroup-scored))
4451                           gnus-summary-default-score 0))))
4452              (list gnus-summary-default-score)
4453              '(0))))
4454
4455 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4456 (defvar gnus-tmp-prev-subject nil)
4457 (defvar gnus-tmp-false-parent nil)
4458 (defvar gnus-tmp-root-expunged nil)
4459 (defvar gnus-tmp-dummy-line nil)
4460
4461 (eval-when-compile (defvar gnus-tmp-header))
4462 (defun gnus-extra-header (type &optional header)
4463   "Return the extra header of TYPE."
4464   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4465       ""))
4466
4467 (defvar gnus-tmp-thread-tree-header-string "")
4468
4469 (defcustom gnus-sum-thread-tree-root "> "
4470   "With %B spec, used for the root of a thread.
4471 If nil, use subject instead."
4472   :type 'string
4473   :group 'gnus-thread)
4474 (defcustom gnus-sum-thread-tree-single-indent ""
4475   "With %B spec, used for a thread with just one message.
4476 If nil, use subject instead."
4477   :type 'string
4478   :group 'gnus-thread)
4479 (defcustom gnus-sum-thread-tree-vertical "| "
4480   "With %B spec, used for drawing a vertical line."
4481   :type 'string
4482   :group 'gnus-thread)
4483 (defcustom gnus-sum-thread-tree-indent "  "
4484   "With %B spec, used for indenting."
4485   :type 'string
4486   :group 'gnus-thread)
4487 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4488   "With %B spec, used for a leaf with brothers."
4489   :type 'string
4490   :group 'gnus-thread)
4491 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4492   "With %B spec, used for a leaf without brothers."
4493   :type 'string
4494   :group 'gnus-thread)
4495
4496 (defun gnus-summary-prepare-threads (threads)
4497   "Prepare summary buffer from THREADS and indentation LEVEL.
4498 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4499 or a straight list of headers."
4500   (gnus-message 7 "Generating summary...")
4501
4502   (setq gnus-newsgroup-threads threads)
4503   (beginning-of-line)
4504
4505   (let ((gnus-tmp-level 0)
4506         (default-score (or gnus-summary-default-score 0))
4507         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4508         (building-line-count gnus-summary-display-while-building)
4509         (building-count (integerp gnus-summary-display-while-building))
4510         thread number subject stack state gnus-tmp-gathered beg-match
4511         new-roots gnus-tmp-new-adopts thread-end simp-subject
4512         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4513         gnus-tmp-replied gnus-tmp-subject-or-nil
4514         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4515         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4516         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4517         tree-stack)
4518
4519     (setq gnus-tmp-prev-subject nil)
4520
4521     (if (vectorp (car threads))
4522         ;; If this is a straight (sic) list of headers, then a
4523         ;; threaded summary display isn't required, so we just create
4524         ;; an unthreaded one.
4525         (gnus-summary-prepare-unthreaded threads)
4526
4527       ;; Do the threaded display.
4528
4529       (if gnus-summary-display-while-building
4530           (switch-to-buffer (buffer-name)))
4531       (while (or threads stack gnus-tmp-new-adopts new-roots)
4532
4533         (if (and (= gnus-tmp-level 0)
4534                  (or (not stack)
4535                      (= (caar stack) 0))
4536                  (not gnus-tmp-false-parent)
4537                  (or gnus-tmp-new-adopts new-roots))
4538             (if gnus-tmp-new-adopts
4539                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4540                       thread (list (car gnus-tmp-new-adopts))
4541                       gnus-tmp-header (caar thread)
4542                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4543               (when new-roots
4544                 (setq thread (list (car new-roots))
4545                       gnus-tmp-header (caar thread)
4546                       new-roots (cdr new-roots))))
4547
4548           (if threads
4549               ;; If there are some threads, we do them before the
4550               ;; threads on the stack.
4551               (setq thread threads
4552                     gnus-tmp-header (caar thread))
4553             ;; There were no current threads, so we pop something off
4554             ;; the stack.
4555             (setq state (car stack)
4556                   gnus-tmp-level (car state)
4557                   tree-stack (cadr state)
4558                   thread (caddr state)
4559                   stack (cdr stack)
4560                   gnus-tmp-header (caar thread))))
4561
4562         (setq gnus-tmp-false-parent nil)
4563         (setq gnus-tmp-root-expunged nil)
4564         (setq thread-end nil)
4565
4566         (if (stringp gnus-tmp-header)
4567             ;; The header is a dummy root.
4568             (cond
4569              ((eq gnus-summary-make-false-root 'adopt)
4570               ;; We let the first article adopt the rest.
4571               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4572                                                (cddar thread)))
4573               (setq gnus-tmp-gathered
4574                     (nconc (mapcar
4575                             (lambda (h) (mail-header-number (car h)))
4576                             (cddar thread))
4577                            gnus-tmp-gathered))
4578               (setq thread (cons (list (caar thread)
4579                                        (cadar thread))
4580                                  (cdr thread)))
4581               (setq gnus-tmp-level -1
4582                     gnus-tmp-false-parent t))
4583              ((eq gnus-summary-make-false-root 'empty)
4584               ;; We print adopted articles with empty subject fields.
4585               (setq gnus-tmp-gathered
4586                     (nconc (mapcar
4587                             (lambda (h) (mail-header-number (car h)))
4588                             (cddar thread))
4589                            gnus-tmp-gathered))
4590               (setq gnus-tmp-level -1))
4591              ((eq gnus-summary-make-false-root 'dummy)
4592               ;; We remember that we probably want to output a dummy
4593               ;; root.
4594               (setq gnus-tmp-dummy-line gnus-tmp-header)
4595               (setq gnus-tmp-prev-subject gnus-tmp-header))
4596              (t
4597               ;; We do not make a root for the gathered
4598               ;; sub-threads at all.
4599               (setq gnus-tmp-level -1)))
4600
4601           (setq number (mail-header-number gnus-tmp-header)
4602                 subject (mail-header-subject gnus-tmp-header)
4603                 simp-subject (gnus-simplify-subject-fully subject))
4604
4605           (cond
4606            ;; If the thread has changed subject, we might want to make
4607            ;; this subthread into a root.
4608            ((and (null gnus-thread-ignore-subject)
4609                  (not (zerop gnus-tmp-level))
4610                  gnus-tmp-prev-subject
4611                  (not (string= gnus-tmp-prev-subject simp-subject)))
4612             (setq new-roots (nconc new-roots (list (car thread)))
4613                   thread-end t
4614                   gnus-tmp-header nil))
4615            ;; If the article lies outside the current limit,
4616            ;; then we do not display it.
4617            ((not (memq number gnus-newsgroup-limit))
4618             (setq gnus-tmp-gathered
4619                   (nconc (mapcar
4620                           (lambda (h) (mail-header-number (car h)))
4621                           (cdar thread))
4622                          gnus-tmp-gathered))
4623             (setq gnus-tmp-new-adopts (if (cdar thread)
4624                                           (append gnus-tmp-new-adopts
4625                                                   (cdar thread))
4626                                         gnus-tmp-new-adopts)
4627                   thread-end t
4628                   gnus-tmp-header nil)
4629             (when (zerop gnus-tmp-level)
4630               (setq gnus-tmp-root-expunged t)))
4631            ;; Perhaps this article is to be marked as read?
4632            ((and gnus-summary-mark-below
4633                  (< (or (cdr (assq number gnus-newsgroup-scored))
4634                         default-score)
4635                     gnus-summary-mark-below)
4636                  ;; Don't touch sparse articles.
4637                  (not (gnus-summary-article-sparse-p number))
4638                  (not (gnus-summary-article-ancient-p number)))
4639             (setq gnus-newsgroup-unreads
4640                   (delq number gnus-newsgroup-unreads))
4641             (if gnus-newsgroup-auto-expire
4642                 (setq gnus-newsgroup-expirable
4643                       (gnus-add-to-sorted-list
4644                        gnus-newsgroup-expirable number))
4645               (push (cons number gnus-low-score-mark)
4646                     gnus-newsgroup-reads))))
4647
4648           (when gnus-tmp-header
4649             ;; We may have an old dummy line to output before this
4650             ;; article.
4651             (when (and gnus-tmp-dummy-line
4652                        (gnus-subject-equal
4653                         gnus-tmp-dummy-line
4654                         (mail-header-subject gnus-tmp-header)))
4655               (gnus-summary-insert-dummy-line
4656                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4657               (setq gnus-tmp-dummy-line nil))
4658
4659             ;; Compute the mark.
4660             (setq gnus-tmp-unread (gnus-article-mark number))
4661
4662             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4663                                   gnus-tmp-header gnus-tmp-level)
4664                   gnus-newsgroup-data)
4665
4666             ;; Actually insert the line.
4667             (setq
4668              gnus-tmp-subject-or-nil
4669              (cond
4670               ((and gnus-thread-ignore-subject
4671                     gnus-tmp-prev-subject
4672                     (not (string= gnus-tmp-prev-subject simp-subject)))
4673                subject)
4674               ((zerop gnus-tmp-level)
4675                (if (and (eq gnus-summary-make-false-root 'empty)
4676                         (memq number gnus-tmp-gathered)
4677                         gnus-tmp-prev-subject
4678                         (string= gnus-tmp-prev-subject simp-subject))
4679                    gnus-summary-same-subject
4680                  subject))
4681               (t gnus-summary-same-subject)))
4682             (if (and (eq gnus-summary-make-false-root 'adopt)
4683                      (= gnus-tmp-level 1)
4684                      (memq number gnus-tmp-gathered))
4685                 (setq gnus-tmp-opening-bracket ?\<
4686                       gnus-tmp-closing-bracket ?\>)
4687               (setq gnus-tmp-opening-bracket ?\[
4688                     gnus-tmp-closing-bracket ?\]))
4689             (setq
4690              gnus-tmp-indentation
4691              (aref gnus-thread-indent-array gnus-tmp-level)
4692              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4693              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4694                                 gnus-summary-default-score 0)
4695              gnus-tmp-score-char
4696              (if (or (null gnus-summary-default-score)
4697                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4698                          gnus-summary-zcore-fuzz))
4699                  ?                      ;Whitespace
4700                (if (< gnus-tmp-score gnus-summary-default-score)
4701                    gnus-score-below-mark gnus-score-over-mark))
4702              gnus-tmp-replied
4703              (cond ((memq number gnus-newsgroup-processable)
4704                     gnus-process-mark)
4705                    ((memq number gnus-newsgroup-cached)
4706                     gnus-cached-mark)
4707                    ((memq number gnus-newsgroup-replied)
4708                     gnus-replied-mark)
4709                    ((memq number gnus-newsgroup-forwarded)
4710                     gnus-forwarded-mark)
4711                    ((memq number gnus-newsgroup-saved)
4712                     gnus-saved-mark)
4713                    ((memq number gnus-newsgroup-recent)
4714                     gnus-recent-mark)
4715                    ((memq number gnus-newsgroup-unseen)
4716                     gnus-unseen-mark)
4717                    (t gnus-no-mark))
4718              gnus-tmp-downloaded
4719              (cond ((memq number gnus-newsgroup-undownloaded) 
4720                     gnus-undownloaded-mark)
4721                    (gnus-newsgroup-agentized
4722                     gnus-downloaded-mark)
4723                    (t
4724                     gnus-no-mark))
4725              gnus-tmp-from (mail-header-from gnus-tmp-header)
4726              gnus-tmp-name
4727              (cond
4728               ((string-match "<[^>]+> *$" gnus-tmp-from)
4729                (setq beg-match (match-beginning 0))
4730                (or (and (string-match "^\".+\"" gnus-tmp-from)
4731                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4732                    (substring gnus-tmp-from 0 beg-match)))
4733               ((string-match "(.+)" gnus-tmp-from)
4734                (substring gnus-tmp-from
4735                           (1+ (match-beginning 0)) (1- (match-end 0))))
4736               (t gnus-tmp-from))
4737              gnus-tmp-thread-tree-header-string
4738              (cond
4739               ((not gnus-show-threads) "")
4740               ((zerop gnus-tmp-level)
4741                (if (cdar thread)
4742                    (or gnus-sum-thread-tree-root subject)
4743                  (or gnus-sum-thread-tree-single-indent subject)))
4744               (t
4745                (concat (apply 'concat
4746                               (mapcar (lambda (item)
4747                                         (if (= item 1)
4748                                             gnus-sum-thread-tree-vertical
4749                                           gnus-sum-thread-tree-indent))
4750                                       (cdr (reverse tree-stack))))
4751                        (if (nth 1 thread)
4752                            gnus-sum-thread-tree-leaf-with-other
4753                          gnus-sum-thread-tree-single-leaf)))))
4754             (when (string= gnus-tmp-name "")
4755               (setq gnus-tmp-name gnus-tmp-from))
4756             (unless (numberp gnus-tmp-lines)
4757               (setq gnus-tmp-lines -1))
4758             (if (= gnus-tmp-lines -1)
4759                 (setq gnus-tmp-lines "?")
4760               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4761             (gnus-put-text-property
4762              (point)
4763              (progn (eval gnus-summary-line-format-spec) (point))
4764              'gnus-number number)
4765             (when gnus-visual-p
4766               (forward-line -1)
4767               (gnus-run-hooks 'gnus-summary-update-hook)
4768               (forward-line 1))
4769
4770             (setq gnus-tmp-prev-subject simp-subject)))
4771
4772         (when (nth 1 thread)
4773           (push (list (max 0 gnus-tmp-level)
4774                       (copy-list tree-stack)
4775                       (nthcdr 1 thread))
4776                 stack))
4777         (push (if (nth 1 thread) 1 0) tree-stack)
4778         (incf gnus-tmp-level)
4779         (setq threads (if thread-end nil (cdar thread)))
4780         (if gnus-summary-display-while-building
4781             (if building-count
4782                 (progn
4783                   ;; use a set frequency
4784                   (setq building-line-count (1- building-line-count))
4785                   (when (= building-line-count 0)
4786                     (sit-for 0)
4787                     (setq building-line-count
4788                           gnus-summary-display-while-building)))
4789               ;; always
4790               (sit-for 0)))
4791         (unless threads
4792           (setq gnus-tmp-level 0)))))
4793   (gnus-message 7 "Generating summary...done"))
4794
4795 (defun gnus-summary-prepare-unthreaded (headers)
4796   "Generate an unthreaded summary buffer based on HEADERS."
4797   (let (header number mark)
4798
4799     (beginning-of-line)
4800
4801     (while headers
4802       ;; We may have to root out some bad articles...
4803       (when (memq (setq number (mail-header-number
4804                                 (setq header (pop headers))))
4805                   gnus-newsgroup-limit)
4806         ;; Mark article as read when it has a low score.
4807         (when (and gnus-summary-mark-below
4808                    (< (or (cdr (assq number gnus-newsgroup-scored))
4809                           gnus-summary-default-score 0)
4810                       gnus-summary-mark-below)
4811                    (not (gnus-summary-article-ancient-p number)))
4812           (setq gnus-newsgroup-unreads
4813                 (delq number gnus-newsgroup-unreads))
4814           (if gnus-newsgroup-auto-expire
4815               (push number gnus-newsgroup-expirable)
4816             (push (cons number gnus-low-score-mark)
4817                   gnus-newsgroup-reads)))
4818
4819         (setq mark (gnus-article-mark number))
4820         (push (gnus-data-make number mark (1+ (point)) header 0)
4821               gnus-newsgroup-data)
4822         (gnus-summary-insert-line
4823          header 0 number
4824          (memq number gnus-newsgroup-undownloaded)
4825          mark (memq number gnus-newsgroup-replied)
4826          (memq number gnus-newsgroup-expirable)
4827          (mail-header-subject header) nil
4828          (cdr (assq number gnus-newsgroup-scored))
4829          (memq number gnus-newsgroup-processable))))))
4830
4831 (defun gnus-summary-remove-list-identifiers ()
4832   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4833   (let ((regexp (if (consp gnus-list-identifiers)
4834                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4835                   gnus-list-identifiers))
4836         changed subject)
4837     (when regexp
4838       (dolist (header gnus-newsgroup-headers)
4839         (setq subject (mail-header-subject header)
4840               changed nil)
4841         (while (string-match
4842                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4843                 subject)
4844           (setq subject
4845                 (concat (substring subject 0 (match-beginning 2))
4846                         (substring subject (match-end 0)))
4847                 changed t))
4848         (when (and changed
4849                    (string-match
4850                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4851           (setq subject
4852                 (concat (substring subject 0 (match-beginning 1))
4853                         (substring subject (match-end 1)))))
4854         (when changed
4855           (mail-header-set-subject header subject))))))
4856
4857 (defun gnus-fetch-headers (articles)
4858   "Fetch headers of ARTICLES."
4859   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4860     (gnus-message 5 "Fetching headers for %s..." name)
4861     (prog1
4862         (if (eq 'nov
4863                 (setq gnus-headers-retrieved-by
4864                       (gnus-retrieve-headers
4865                        articles gnus-newsgroup-name
4866                        ;; We might want to fetch old headers, but
4867                        ;; not if there is only 1 article.
4868                        (and (or (and
4869                                  (not (eq gnus-fetch-old-headers 'some))
4870                                  (not (numberp gnus-fetch-old-headers)))
4871                                 (> (length articles) 1))
4872                             gnus-fetch-old-headers))))
4873             (gnus-get-newsgroup-headers-xover
4874              articles nil nil gnus-newsgroup-name t)
4875           (gnus-get-newsgroup-headers))
4876       (gnus-message 5 "Fetching headers for %s...done" name))))
4877
4878 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4879   "Select newsgroup GROUP.
4880 If READ-ALL is non-nil, all articles in the group are selected.
4881 If SELECT-ARTICLES, only select those articles from GROUP."
4882   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4883          ;;!!! Dirty hack; should be removed.
4884          (gnus-summary-ignore-duplicates
4885           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4886               t
4887             gnus-summary-ignore-duplicates))
4888          (info (nth 2 entry))
4889          articles fetched-articles cached)
4890
4891     (unless (gnus-check-server
4892              (set (make-local-variable 'gnus-current-select-method)
4893                   (gnus-find-method-for-group group)))
4894       (error "Couldn't open server"))
4895
4896     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4897         (gnus-activate-group group)     ; Or we can activate it...
4898         (progn                          ; Or we bug out.
4899           (when (equal major-mode 'gnus-summary-mode)
4900             (kill-buffer (current-buffer)))
4901           (error "Couldn't activate group %s: %s"
4902                  group (gnus-status-message group))))
4903
4904     (unless (gnus-request-group group t)
4905       (when (equal major-mode 'gnus-summary-mode)
4906         (kill-buffer (current-buffer)))
4907       (error "Couldn't request group %s: %s"
4908              group (gnus-status-message group)))
4909
4910     (setq gnus-newsgroup-name group
4911           gnus-newsgroup-unselected nil
4912           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4913
4914     (let ((display (gnus-group-find-parameter group 'display)))
4915       (setq gnus-newsgroup-display
4916             (cond
4917              ((not (zerop (or (car-safe read-all) 0)))
4918               ;; The user entered the group with C-u SPC/RET, let's show
4919               ;; all articles.
4920               'gnus-not-ignore)
4921              ((eq display 'all)
4922               'gnus-not-ignore)
4923              ((arrayp display)
4924               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4925              ((numberp display)
4926               ;; The following is probably the "correct" solution, but
4927               ;; it makes Gnus fetch all headers and then limit the
4928               ;; articles (which is slow), so instead we hack the
4929               ;; select-articles parameter instead. -- Simon Josefsson
4930               ;; <jas@kth.se>
4931               ;;
4932               ;; (gnus-byte-compile
4933               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4934               ;;                         display)))))
4935               (setq select-articles
4936                     (gnus-uncompress-range
4937                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4938                              (if (> tmp 0)
4939                                  tmp
4940                                1))
4941                            (cdr (gnus-active group)))))
4942               nil)
4943              (t
4944               nil))))
4945
4946     (gnus-summary-setup-default-charset)
4947
4948     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4949     (when (gnus-virtual-group-p group)
4950       (setq cached gnus-newsgroup-cached))
4951
4952     (setq gnus-newsgroup-unreads
4953           (gnus-sorted-ndifference
4954            (gnus-sorted-ndifference gnus-newsgroup-unreads
4955                                     gnus-newsgroup-marked)
4956            gnus-newsgroup-dormant))
4957
4958     (setq gnus-newsgroup-processable nil)
4959
4960     (gnus-update-read-articles group gnus-newsgroup-unreads)
4961
4962     ;; Adjust and set lists of article marks.
4963     (when info
4964       (gnus-adjust-marked-articles info))
4965     (if (setq articles select-articles)
4966         (setq gnus-newsgroup-unselected
4967               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4968       (setq articles (gnus-articles-to-read group read-all)))
4969
4970     (cond
4971      ((null articles)
4972       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4973       'quit)
4974      ((eq articles 0) nil)
4975      (t
4976       ;; Init the dependencies hash table.
4977       (setq gnus-newsgroup-dependencies
4978             (gnus-make-hashtable (length articles)))
4979       (gnus-set-global-variables)
4980       ;; Retrieve the headers and read them in.
4981
4982       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4983
4984       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4985       (when cached
4986         (setq gnus-newsgroup-cached cached))
4987
4988       ;; Suppress duplicates?
4989       (when gnus-suppress-duplicates
4990         (gnus-dup-suppress-articles))
4991
4992       ;; Set the initial limit.
4993       (setq gnus-newsgroup-limit (copy-sequence articles))
4994       ;; Remove canceled articles from the list of unread articles.
4995       (setq fetched-articles
4996             (mapcar (lambda (headers) (mail-header-number headers))
4997                     gnus-newsgroup-headers))
4998       (setq gnus-newsgroup-articles fetched-articles)
4999       (setq gnus-newsgroup-unreads
5000             (gnus-sorted-nintersection
5001              gnus-newsgroup-unreads fetched-articles))
5002       (gnus-compute-unseen-list)
5003
5004       ;; Removed marked articles that do not exist.
5005       (gnus-update-missing-marks
5006        (gnus-sorted-difference articles fetched-articles))
5007       ;; We might want to build some more threads first.
5008       (when (and gnus-fetch-old-headers
5009                  (eq gnus-headers-retrieved-by 'nov))
5010         (if (eq gnus-fetch-old-headers 'invisible)
5011             (gnus-build-all-threads)
5012           (gnus-build-old-threads)))
5013       ;; Let the Gnus agent mark articles as read.
5014       (when gnus-agent
5015         (gnus-agent-get-undownloaded-list))
5016       ;; Remove list identifiers from subject
5017       (when gnus-list-identifiers
5018         (gnus-summary-remove-list-identifiers))
5019       ;; Check whether auto-expire is to be done in this group.
5020       (setq gnus-newsgroup-auto-expire
5021             (gnus-group-auto-expirable-p group))
5022       ;; Set up the article buffer now, if necessary.
5023       (unless gnus-single-article-buffer
5024         (gnus-article-setup-buffer))
5025       ;; First and last article in this newsgroup.
5026       (when gnus-newsgroup-headers
5027         (setq gnus-newsgroup-begin
5028               (mail-header-number (car gnus-newsgroup-headers))
5029               gnus-newsgroup-end
5030               (mail-header-number
5031                (gnus-last-element gnus-newsgroup-headers))))
5032       ;; GROUP is successfully selected.
5033       (or gnus-newsgroup-headers t)))))
5034
5035 (defun gnus-compute-unseen-list ()
5036   ;; The `seen' marks are treated specially.
5037   (if (not gnus-newsgroup-seen)
5038       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5039     (setq gnus-newsgroup-unseen
5040           (gnus-inverse-list-range-intersection
5041            gnus-newsgroup-articles gnus-newsgroup-seen))))
5042
5043 (defun gnus-summary-display-make-predicate (display)
5044   (require 'gnus-agent)
5045   (when (= (length display) 1)
5046     (setq display (car display)))
5047   (unless gnus-summary-display-cache
5048     (dolist (elem (append '((unread . unread)
5049                             (read . read)
5050                             (unseen . unseen))
5051                           gnus-article-mark-lists))
5052       (push (cons (cdr elem)
5053                   (gnus-byte-compile
5054                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5055             gnus-summary-display-cache)))
5056   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5057         (gnus-category-predicate-cache gnus-summary-display-cache))
5058     (gnus-get-predicate display)))
5059
5060 ;; Uses the dynamically bound `number' variable.
5061 (eval-when-compile
5062   (defvar number))
5063 (defun gnus-article-marked-p (type &optional article)
5064   (let ((article (or article number)))
5065     (cond
5066      ((eq type 'tick)
5067       (memq article gnus-newsgroup-marked))
5068      ((eq type 'spam)
5069       (memq article gnus-newsgroup-spam-marked))
5070      ((eq type 'unsend)
5071       (memq article gnus-newsgroup-unsendable))
5072      ((eq type 'undownload)
5073       (memq article gnus-newsgroup-undownloaded))
5074      ((eq type 'download)
5075       (memq article gnus-newsgroup-downloadable))
5076      ((eq type 'unread)
5077       (memq article gnus-newsgroup-unreads))
5078      ((eq type 'read)
5079       (memq article gnus-newsgroup-reads))
5080      ((eq type 'dormant)
5081       (memq article gnus-newsgroup-dormant) )
5082      ((eq type 'expire)
5083       (memq article gnus-newsgroup-expirable))
5084      ((eq type 'reply)
5085       (memq article gnus-newsgroup-replied))
5086      ((eq type 'killed)
5087       (memq article gnus-newsgroup-killed))
5088      ((eq type 'bookmark)
5089       (assq article gnus-newsgroup-bookmarks))
5090      ((eq type 'score)
5091       (assq article gnus-newsgroup-scored))
5092      ((eq type 'save)
5093       (memq article gnus-newsgroup-saved))
5094      ((eq type 'cache)
5095       (memq article gnus-newsgroup-cached))
5096      ((eq type 'forward)
5097       (memq article gnus-newsgroup-forwarded))
5098      ((eq type 'seen)
5099       (not (memq article gnus-newsgroup-unseen)))
5100      ((eq type 'recent)
5101       (memq article gnus-newsgroup-recent))
5102      (t t))))
5103
5104 (defun gnus-articles-to-read (group &optional read-all)
5105   "Find out what articles the user wants to read."
5106   (let* ((display (gnus-group-find-parameter group 'display))
5107          (articles
5108           ;; Select all articles if `read-all' is non-nil, or if there
5109           ;; are no unread articles.
5110           (if (or read-all
5111                   (and (zerop (length gnus-newsgroup-marked))
5112                        (zerop (length gnus-newsgroup-unreads)))
5113                   ;; Fetch all if the predicate is non-nil.
5114                   gnus-newsgroup-display)
5115               ;; We want to select the headers for all the articles in
5116               ;; the group, so we select either all the active
5117               ;; articles in the group, or (if that's nil), the
5118               ;; articles in the cache.
5119               (or
5120                (gnus-uncompress-range (gnus-active group))
5121                (gnus-cache-articles-in-group group))
5122             ;; Select only the "normal" subset of articles.
5123             (gnus-sorted-nunion
5124              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5125              gnus-newsgroup-unreads)))
5126          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5127          (scored (length scored-list))
5128          (number (length articles))
5129          (marked (+ (length gnus-newsgroup-marked)
5130                     (length gnus-newsgroup-dormant)))
5131          (select
5132           (cond
5133            ((numberp read-all)
5134             read-all)
5135            ((numberp gnus-newsgroup-display)
5136             gnus-newsgroup-display)
5137            (t
5138             (condition-case ()
5139                 (cond
5140                  ((and (or (<= scored marked) (= scored number))
5141                        (numberp gnus-large-newsgroup)
5142                        (> number gnus-large-newsgroup))
5143                   (let* ((cursor-in-echo-area nil)
5144                          (initial (gnus-parameter-large-newsgroup-initial
5145                                    gnus-newsgroup-name))
5146                          (input
5147                           (read-string
5148                            (format
5149                             "How many articles from %s (%s %d): "
5150                             (gnus-limit-string
5151                              (gnus-group-decoded-name gnus-newsgroup-name)
5152                              35)
5153                             (if initial "max" "default")
5154                             number)
5155                            (if initial
5156                                (cons (number-to-string initial)
5157                                      0)))))
5158                     (if (string-match "^[ \t]*$" input) number input)))
5159                  ((and (> scored marked) (< scored number)
5160                        (> (- scored number) 20))
5161                   (let ((input
5162                          (read-string
5163                           (format "%s %s (%d scored, %d total): "
5164                                   "How many articles from"
5165                                   (gnus-group-decoded-name group)
5166                                   scored number))))
5167                     (if (string-match "^[ \t]*$" input)
5168                         number input)))
5169                  (t number))
5170               (quit
5171                (message "Quit getting the articles to read")
5172                nil))))))
5173     (setq select (if (stringp select) (string-to-number select) select))
5174     (if (or (null select) (zerop select))
5175         select
5176       (if (and (not (zerop scored)) (<= (abs select) scored))
5177           (progn
5178             (setq articles (sort scored-list '<))
5179             (setq number (length articles)))
5180         (setq articles (copy-sequence articles)))
5181
5182       (when (< (abs select) number)
5183         (if (< select 0)
5184             ;; Select the N oldest articles.
5185             (setcdr (nthcdr (1- (abs select)) articles) nil)
5186           ;; Select the N most recent articles.
5187           (setq articles (nthcdr (- number select) articles))))
5188       (setq gnus-newsgroup-unselected
5189             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5190       (when gnus-alter-articles-to-read-function
5191         (setq gnus-newsgroup-unreads
5192               (sort
5193                (funcall gnus-alter-articles-to-read-function
5194                         gnus-newsgroup-name gnus-newsgroup-unreads)
5195                '<)))
5196       articles)))
5197
5198 (defun gnus-killed-articles (killed articles)
5199   (let (out)
5200     (while articles
5201       (when (inline (gnus-member-of-range (car articles) killed))
5202         (push (car articles) out))
5203       (setq articles (cdr articles)))
5204     out))
5205
5206 (defun gnus-uncompress-marks (marks)
5207   "Uncompress the mark ranges in MARKS."
5208   (let ((uncompressed '(score bookmark))
5209         out)
5210     (while marks
5211       (if (memq (caar marks) uncompressed)
5212           (push (car marks) out)
5213         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5214       (setq marks (cdr marks)))
5215     out))
5216
5217 (defun gnus-article-mark-to-type (mark)
5218   "Return the type of MARK."
5219   (or (cadr (assq mark gnus-article-special-mark-lists))
5220       'list))
5221
5222 (defun gnus-article-unpropagatable-p (mark)
5223   "Return whether MARK should be propagated to backend."
5224   (memq mark gnus-article-unpropagated-mark-lists))
5225
5226 (defun gnus-adjust-marked-articles (info)
5227   "Set all article lists and remove all marks that are no longer valid."
5228   (let* ((marked-lists (gnus-info-marks info))
5229          (active (gnus-active (gnus-info-group info)))
5230          (min (car active))
5231          (max (cdr active))
5232          (types gnus-article-mark-lists)
5233          marks var articles article mark mark-type)
5234
5235     (dolist (marks marked-lists)
5236       (setq mark (car marks)
5237             mark-type (gnus-article-mark-to-type mark)
5238             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5239
5240       ;; We set the variable according to the type of the marks list,
5241       ;; and then adjust the marks to a subset of the active articles.
5242       (cond
5243        ;; Adjust "simple" lists.
5244        ((eq mark-type 'list)
5245         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5246         (when (memq mark '(tick dormant expire reply save))
5247           (while articles
5248             (when (or (< (setq article (pop articles)) min) (> article max))
5249               (set var (delq article (symbol-value var)))))))
5250        ;; Adjust assocs.
5251        ((eq mark-type 'tuple)
5252         (set var (setq articles (cdr marks)))
5253         (when (not (listp (cdr (symbol-value var))))
5254           (set var (list (symbol-value var))))
5255         (when (not (listp (cdr articles)))
5256           (setq articles (list articles)))
5257         (while articles
5258           (when (or (not (consp (setq article (pop articles))))
5259                     (< (car article) min)
5260                     (> (car article) max))
5261             (set var (delq article (symbol-value var))))))
5262        ;; Adjust ranges (sloppily).
5263        ((eq mark-type 'range)
5264         (cond
5265          ((eq mark 'seen)
5266           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5267           ;; It should be (seen (NUM1 . NUM2)).
5268           (when (numberp (cddr marks))
5269             (setcdr marks (list (cdr marks))))
5270           (setq articles (cdr marks))
5271           (while (and articles
5272                       (or (and (consp (car articles))
5273                                (> min (cdar articles)))
5274                           (and (numberp (car articles))
5275                                (> min (car articles)))))
5276             (pop articles))
5277           (set var articles))))))))
5278
5279 (defun gnus-update-missing-marks (missing)
5280   "Go through the list of MISSING articles and remove them from the mark lists."
5281   (when missing
5282     (let (var m)
5283       ;; Go through all types.
5284       (dolist (elem gnus-article-mark-lists)
5285         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5286           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5287           (when (symbol-value var)
5288             ;; This list has articles.  So we delete all missing
5289             ;; articles from it.
5290             (setq m missing)
5291             (while m
5292               (set var (delq (pop m) (symbol-value var))))))))))
5293
5294 (defun gnus-update-marks ()
5295   "Enter the various lists of marked articles into the newsgroup info list."
5296   (let ((types gnus-article-mark-lists)
5297         (info (gnus-get-info gnus-newsgroup-name))
5298         type list newmarked symbol delta-marks)
5299     (when info
5300       ;; Add all marks lists to the list of marks lists.
5301       (while (setq type (pop types))
5302         (setq list (symbol-value
5303                     (setq symbol
5304                           (intern (format "gnus-newsgroup-%s" (car type))))))
5305
5306         (when list
5307           ;; Get rid of the entries of the articles that have the
5308           ;; default score.
5309           (when (and (eq (cdr type) 'score)
5310                      gnus-save-score
5311                      list)
5312             (let* ((arts list)
5313                    (prev (cons nil list))
5314                    (all prev))
5315               (while arts
5316                 (if (or (not (consp (car arts)))
5317                         (= (cdar arts) gnus-summary-default-score))
5318                     (setcdr prev (cdr arts))
5319                   (setq prev arts))
5320                 (setq arts (cdr arts)))
5321               (setq list (cdr all)))))
5322
5323         (when (eq (cdr type) 'seen)
5324           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5325
5326         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5327           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5328
5329         (when (and (gnus-check-backend-function
5330                     'request-set-mark gnus-newsgroup-name)
5331                    (not (gnus-article-unpropagatable-p (cdr type))))
5332           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5333                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5334                  (add (gnus-remove-from-range
5335                        (gnus-copy-sequence list) old)))
5336             (when add
5337               (push (list add 'add (list (cdr type))) delta-marks))
5338             (when del
5339               (push (list del 'del (list (cdr type))) delta-marks))))
5340
5341         (when list
5342           (push (cons (cdr type) list) newmarked)))
5343
5344       (when delta-marks
5345         (unless (gnus-check-group gnus-newsgroup-name)
5346           (error "Can't open server for %s" gnus-newsgroup-name))
5347         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5348
5349       ;; Enter these new marks into the info of the group.
5350       (if (nthcdr 3 info)
5351           (setcar (nthcdr 3 info) newmarked)
5352         ;; Add the marks lists to the end of the info.
5353         (when newmarked
5354           (setcdr (nthcdr 2 info) (list newmarked))))
5355
5356       ;; Cut off the end of the info if there's nothing else there.
5357       (let ((i 5))
5358         (while (and (> i 2)
5359                     (not (nth i info)))
5360           (when (nthcdr (decf i) info)
5361             (setcdr (nthcdr i info) nil)))))))
5362
5363 (defun gnus-set-mode-line (where)
5364   "Set the mode line of the article or summary buffers.
5365 If WHERE is `summary', the summary mode line format will be used."
5366   ;; Is this mode line one we keep updated?
5367   (when (and (memq where gnus-updated-mode-lines)
5368              (symbol-value
5369               (intern (format "gnus-%s-mode-line-format-spec" where))))
5370     (let (mode-string)
5371       (save-excursion
5372         ;; We evaluate this in the summary buffer since these
5373         ;; variables are buffer-local to that buffer.
5374         (set-buffer gnus-summary-buffer)
5375        ;; We bind all these variables that are used in the `eval' form
5376         ;; below.
5377         (let* ((mformat (symbol-value
5378                          (intern
5379                           (format "gnus-%s-mode-line-format-spec" where))))
5380                (gnus-tmp-group-name (gnus-group-decoded-name
5381                                      gnus-newsgroup-name))
5382                (gnus-tmp-article-number (or gnus-current-article 0))
5383                (gnus-tmp-unread gnus-newsgroup-unreads)
5384                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5385                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5386                (gnus-tmp-unread-and-unselected
5387                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5388                             (zerop gnus-tmp-unselected))
5389                        "")
5390                       ((zerop gnus-tmp-unselected)
5391                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5392                       (t (format "{%d(+%d) more}"
5393                                  gnus-tmp-unread-and-unticked
5394                                  gnus-tmp-unselected))))
5395                (gnus-tmp-subject
5396                 (if (and gnus-current-headers
5397                          (vectorp gnus-current-headers))
5398                     (gnus-mode-string-quote
5399                      (mail-header-subject gnus-current-headers))
5400                   ""))
5401                bufname-length max-len
5402                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5403           (setq mode-string (eval mformat))
5404           (setq bufname-length (if (string-match "%b" mode-string)
5405                                    (- (length
5406                                        (buffer-name
5407                                         (if (eq where 'summary)
5408                                             nil
5409                                           (get-buffer gnus-article-buffer))))
5410                                       2)
5411                                  0))
5412           (setq max-len (max 4 (if gnus-mode-non-string-length
5413                                    (- (window-width)
5414                                       gnus-mode-non-string-length
5415                                       bufname-length)
5416                                  (length mode-string))))
5417           ;; We might have to chop a bit of the string off...
5418           (when (> (length mode-string) max-len)
5419             (setq mode-string
5420                   (concat (truncate-string-to-width mode-string (- max-len 3))
5421                           "...")))
5422           ;; Pad the mode string a bit.
5423           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5424       ;; Update the mode line.
5425       (setq mode-line-buffer-identification
5426             (gnus-mode-line-buffer-identification (list mode-string)))
5427       (set-buffer-modified-p t))))
5428
5429 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5430   "Go through the HEADERS list and add all Xrefs to a hash table.
5431 The resulting hash table is returned, or nil if no Xrefs were found."
5432   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5433          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5434          (xref-hashtb (gnus-make-hashtable))
5435          start group entry number xrefs header)
5436     (while headers
5437       (setq header (pop headers))
5438       (when (and (setq xrefs (mail-header-xref header))
5439                  (not (memq (setq number (mail-header-number header))
5440                             unreads)))
5441         (setq start 0)
5442         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5443           (setq start (match-end 0))
5444           (setq group (if prefix
5445                           (concat prefix (substring xrefs (match-beginning 1)
5446                                                     (match-end 1)))
5447                         (substring xrefs (match-beginning 1) (match-end 1))))
5448           (setq number
5449                 (string-to-int (substring xrefs (match-beginning 2)
5450                                           (match-end 2))))
5451           (if (setq entry (gnus-gethash group xref-hashtb))
5452               (setcdr entry (cons number (cdr entry)))
5453             (gnus-sethash group (cons number nil) xref-hashtb)))))
5454     (and start xref-hashtb)))
5455
5456 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5457   "Look through all the headers and mark the Xrefs as read."
5458   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5459         name entry info xref-hashtb idlist method nth4)
5460     (save-excursion
5461       (set-buffer gnus-group-buffer)
5462       (when (setq xref-hashtb
5463                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5464         (mapatoms
5465          (lambda (group)
5466            (unless (string= from-newsgroup (setq name (symbol-name group)))
5467              (setq idlist (symbol-value group))
5468              ;; Dead groups are not updated.
5469              (and (prog1
5470                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5471                             info (nth 2 entry))
5472                     (when (stringp (setq nth4 (gnus-info-method info)))
5473                       (setq nth4 (gnus-server-to-method nth4))))
5474                   ;; Only do the xrefs if the group has the same
5475                   ;; select method as the group we have just read.
5476                   (or (gnus-methods-equal-p
5477                        nth4 (gnus-find-method-for-group from-newsgroup))
5478                       virtual
5479                       (equal nth4 (setq method (gnus-find-method-for-group
5480                                                 from-newsgroup)))
5481                       (and (equal (car nth4) (car method))
5482                            (equal (nth 1 nth4) (nth 1 method))))
5483                   gnus-use-cross-reference
5484                   (or (not (eq gnus-use-cross-reference t))
5485                       virtual
5486                       ;; Only do cross-references on subscribed
5487                       ;; groups, if that is what is wanted.
5488                       (<= (gnus-info-level info) gnus-level-subscribed))
5489                   (gnus-group-make-articles-read name idlist))))
5490          xref-hashtb)))))
5491
5492 (defun gnus-compute-read-articles (group articles)
5493   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5494          (info (nth 2 entry))
5495          (active (gnus-active group))
5496          ninfo)
5497     (when entry
5498       ;; First peel off all invalid article numbers.
5499       (when active
5500         (let ((ids articles)
5501               id first)
5502           (while (setq id (pop ids))
5503             (when (and first (> id (cdr active)))
5504               ;; We'll end up in this situation in one particular
5505               ;; obscure situation.  If you re-scan a group and get
5506               ;; a new article that is cross-posted to a different
5507               ;; group that has not been re-scanned, you might get
5508               ;; crossposted article that has a higher number than
5509               ;; Gnus believes possible.  So we re-activate this
5510               ;; group as well.  This might mean doing the
5511               ;; crossposting thingy will *increase* the number
5512               ;; of articles in some groups.  Tsk, tsk.
5513               (setq active (or (gnus-activate-group group) active)))
5514             (when (or (> id (cdr active))
5515                       (< id (car active)))
5516               (setq articles (delq id articles))))))
5517       ;; If the read list is nil, we init it.
5518       (if (and active
5519                (null (gnus-info-read info))
5520                (> (car active) 1))
5521           (setq ninfo (cons 1 (1- (car active))))
5522         (setq ninfo (gnus-info-read info)))
5523       ;; Then we add the read articles to the range.
5524       (gnus-add-to-range
5525        ninfo (setq articles (sort articles '<))))))
5526
5527 (defun gnus-group-make-articles-read (group articles)
5528   "Update the info of GROUP to say that ARTICLES are read."
5529   (let* ((num 0)
5530          (entry (gnus-gethash group gnus-newsrc-hashtb))
5531          (info (nth 2 entry))
5532          (active (gnus-active group))
5533          range)
5534     (when entry
5535       (setq range (gnus-compute-read-articles group articles))
5536       (save-excursion
5537         (set-buffer gnus-group-buffer)
5538         (gnus-undo-register
5539           `(progn
5540              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5541              (gnus-info-set-read ',info ',(gnus-info-read info))
5542              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5543              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5544              (gnus-group-update-group ,group t))))
5545       ;; Add the read articles to the range.
5546       (gnus-info-set-read info range)
5547       (gnus-request-set-mark group (list (list range 'add '(read))))
5548       ;; Then we have to re-compute how many unread
5549       ;; articles there are in this group.
5550       (when active
5551         (cond
5552          ((not range)
5553           (setq num (- (1+ (cdr active)) (car active))))
5554          ((not (listp (cdr range)))
5555           (setq num (- (cdr active) (- (1+ (cdr range))
5556                                        (car range)))))
5557          (t
5558           (while range
5559             (if (numberp (car range))
5560                 (setq num (1+ num))
5561               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5562             (setq range (cdr range)))
5563           (setq num (- (cdr active) num))))
5564         ;; Update the number of unread articles.
5565         (setcar entry num)
5566         ;; Update the group buffer.
5567         (unless (gnus-ephemeral-group-p group)
5568           (gnus-group-update-group group t))))))
5569
5570 (defvar gnus-newsgroup-none-id 0)
5571
5572 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5573   (let ((cur nntp-server-buffer)
5574         (dependencies
5575          (or dependencies
5576              (save-excursion (set-buffer gnus-summary-buffer)
5577                              gnus-newsgroup-dependencies)))
5578         headers id end ref
5579         (mail-parse-charset gnus-newsgroup-charset)
5580         (mail-parse-ignored-charsets
5581          (save-excursion (condition-case nil
5582                              (set-buffer gnus-summary-buffer)
5583                            (error))
5584                          gnus-newsgroup-ignored-charsets)))
5585     (save-excursion
5586       (set-buffer nntp-server-buffer)
5587       ;; Translate all TAB characters into SPACE characters.
5588       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5589       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5590       (gnus-run-hooks 'gnus-parse-headers-hook)
5591       (let ((case-fold-search t)
5592             in-reply-to header p lines chars)
5593         (goto-char (point-min))
5594         ;; Search to the beginning of the next header.  Error messages
5595         ;; do not begin with 2 or 3.
5596         (while (re-search-forward "^[23][0-9]+ " nil t)
5597           (setq id nil
5598                 ref nil)
5599           ;; This implementation of this function, with nine
5600           ;; search-forwards instead of the one re-search-forward and
5601           ;; a case (which basically was the old function) is actually
5602           ;; about twice as fast, even though it looks messier.  You
5603           ;; can't have everything, I guess.  Speed and elegance
5604           ;; doesn't always go hand in hand.
5605           (setq
5606            header
5607            (vector
5608             ;; Number.
5609             (prog1
5610                 (read cur)
5611               (end-of-line)
5612               (setq p (point))
5613               (narrow-to-region (point)
5614                                 (or (and (search-forward "\n.\n" nil t)
5615                                          (- (point) 2))
5616                                     (point))))
5617             ;; Subject.
5618             (progn
5619               (goto-char p)
5620               (if (search-forward "\nsubject:" nil t)
5621                   (funcall gnus-decode-encoded-word-function
5622                            (nnheader-header-value))
5623                 "(none)"))
5624             ;; From.
5625             (progn
5626               (goto-char p)
5627               (if (search-forward "\nfrom:" nil t)
5628                   (funcall gnus-decode-encoded-word-function
5629                            (nnheader-header-value))
5630                 "(nobody)"))
5631             ;; Date.
5632             (progn
5633               (goto-char p)
5634               (if (search-forward "\ndate:" nil t)
5635                   (nnheader-header-value) ""))
5636             ;; Message-ID.
5637             (progn
5638               (goto-char p)
5639               (setq id (if (re-search-forward
5640                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5641                            ;; We do it this way to make sure the Message-ID
5642                            ;; is (somewhat) syntactically valid.
5643                            (buffer-substring (match-beginning 1)
5644                                              (match-end 1))
5645                          ;; If there was no message-id, we just fake one
5646                          ;; to make subsequent routines simpler.
5647                          (nnheader-generate-fake-message-id))))
5648             ;; References.
5649             (progn
5650               (goto-char p)
5651               (if (search-forward "\nreferences:" nil t)
5652                   (progn
5653                     (setq end (point))
5654                     (prog1
5655                         (nnheader-header-value)
5656                       (setq ref
5657                             (buffer-substring
5658                              (progn
5659                                (end-of-line)
5660                                (search-backward ">" end t)
5661                                (1+ (point)))
5662                              (progn
5663                                (search-backward "<" end t)
5664                                (point))))))
5665                 ;; Get the references from the in-reply-to header if there
5666                 ;; were no references and the in-reply-to header looks
5667                 ;; promising.
5668                 (if (and (search-forward "\nin-reply-to:" nil t)
5669                          (setq in-reply-to (nnheader-header-value))
5670                          (string-match "<[^>]+>" in-reply-to))
5671                     (let (ref2)
5672                       (setq ref (substring in-reply-to (match-beginning 0)
5673                                            (match-end 0)))
5674                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5675                         (setq ref2 (substring in-reply-to (match-beginning 0)
5676                                               (match-end 0)))
5677                         (when (> (length ref2) (length ref))
5678                           (setq ref ref2)))
5679                       ref)
5680                   (setq ref nil))))
5681             ;; Chars.
5682             (progn
5683               (goto-char p)
5684               (if (search-forward "\nchars: " nil t)
5685                   (if (numberp (setq chars (ignore-errors (read cur))))
5686                       chars -1)
5687                 -1))
5688             ;; Lines.
5689             (progn
5690               (goto-char p)
5691               (if (search-forward "\nlines: " nil t)
5692                   (if (numberp (setq lines (ignore-errors (read cur))))
5693                       lines -1)
5694                 -1))
5695             ;; Xref.
5696             (progn
5697               (goto-char p)
5698               (and (search-forward "\nxref:" nil t)
5699                    (nnheader-header-value)))
5700             ;; Extra.
5701             (when gnus-extra-headers
5702               (let ((extra gnus-extra-headers)
5703                     out)
5704                 (while extra
5705                   (goto-char p)
5706                   (when (search-forward
5707                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5708                     (push (cons (car extra) (nnheader-header-value))
5709                           out))
5710                   (pop extra))
5711                 out))))
5712           (when (equal id ref)
5713             (setq ref nil))
5714
5715           (when gnus-alter-header-function
5716             (funcall gnus-alter-header-function header)
5717             (setq id (mail-header-id header)
5718                   ref (gnus-parent-id (mail-header-references header))))
5719
5720           (when (setq header
5721                       (gnus-dependencies-add-header
5722                        header dependencies force-new))
5723             (push header headers))
5724           (goto-char (point-max))
5725           (widen))
5726         (nreverse headers)))))
5727
5728 ;; Goes through the xover lines and returns a list of vectors
5729 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5730                                                   force-new dependencies
5731                                                   group also-fetch-heads)
5732   "Parse the news overview data in the server buffer.
5733 Return a list of headers that match SEQUENCE (see
5734 `nntp-retrieve-headers')."
5735   ;; Get the Xref when the users reads the articles since most/some
5736   ;; NNTP servers do not include Xrefs when using XOVER.
5737   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5738   (let ((mail-parse-charset gnus-newsgroup-charset)
5739         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5740         (cur nntp-server-buffer)
5741         (dependencies (or dependencies gnus-newsgroup-dependencies))
5742         (allp (cond
5743                ((eq gnus-read-all-available-headers t)
5744                 t)
5745                ((stringp gnus-read-all-available-headers)
5746                 (string-match gnus-read-all-available-headers group))
5747                (t
5748                 nil)))
5749         number headers header)
5750     (save-excursion
5751       (set-buffer nntp-server-buffer)
5752       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5753       ;; Allow the user to mangle the headers before parsing them.
5754       (gnus-run-hooks 'gnus-parse-headers-hook)
5755       (goto-char (point-min))
5756       (gnus-parse-without-error
5757         (while (and (or sequence allp)
5758                     (not (eobp)))
5759           (setq number (read cur))
5760           (when (not allp)
5761             (while (and sequence
5762                         (< (car sequence) number))
5763               (setq sequence (cdr sequence))))
5764           (when (and (or allp
5765                          (and sequence
5766                               (eq number (car sequence))))
5767                      (progn
5768                        (setq sequence (cdr sequence))
5769                        (setq header (inline
5770                                       (gnus-nov-parse-line
5771                                        number dependencies force-new)))))
5772             (push header headers))
5773           (forward-line 1)))
5774       ;; A common bug in inn is that if you have posted an article and
5775       ;; then retrieves the active file, it will answer correctly --
5776       ;; the new article is included.  However, a NOV entry for the
5777       ;; article may not have been generated yet, so this may fail.
5778       ;; We work around this problem by retrieving the last few
5779       ;; headers using HEAD.
5780       (if (or (not also-fetch-heads)
5781               (not sequence))
5782           ;; We (probably) got all the headers.
5783           (nreverse headers)
5784         (let ((gnus-nov-is-evil t))
5785           (nconc
5786            (nreverse headers)
5787            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5788              (gnus-get-newsgroup-headers))))))))
5789
5790 (defun gnus-article-get-xrefs ()
5791   "Fill in the Xref value in `gnus-current-headers', if necessary.
5792 This is meant to be called in `gnus-article-internal-prepare-hook'."
5793   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5794                                  gnus-current-headers)))
5795     (or (not gnus-use-cross-reference)
5796         (not headers)
5797         (and (mail-header-xref headers)
5798              (not (string= (mail-header-xref headers) "")))
5799         (let ((case-fold-search t)
5800               xref)
5801           (save-restriction
5802             (nnheader-narrow-to-headers)
5803             (goto-char (point-min))
5804             (when (or (and (not (eobp))
5805                            (eq (downcase (char-after)) ?x)
5806                            (looking-at "Xref:"))
5807                       (search-forward "\nXref:" nil t))
5808               (goto-char (1+ (match-end 0)))
5809               (setq xref (buffer-substring (point)
5810                                            (progn (end-of-line) (point))))
5811               (mail-header-set-xref headers xref)))))))
5812
5813 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5814   "Find article ID and insert the summary line for that article.
5815 OLD-HEADER can either be a header or a line number to insert
5816 the subject line on."
5817   (let* ((line (and (numberp old-header) old-header))
5818          (old-header (and (vectorp old-header) old-header))
5819          (header (cond ((and old-header use-old-header)
5820                         old-header)
5821                        ((and (numberp id)
5822                              (gnus-number-to-header id))
5823                         (gnus-number-to-header id))
5824                        (t
5825                         (gnus-read-header id))))
5826          (number (and (numberp id) id))
5827          d)
5828     (when header
5829       ;; Rebuild the thread that this article is part of and go to the
5830       ;; article we have fetched.
5831       (when (and (not gnus-show-threads)
5832                  old-header)
5833         (when (and number
5834                    (setq d (gnus-data-find (mail-header-number old-header))))
5835           (goto-char (gnus-data-pos d))
5836           (gnus-data-remove
5837            number
5838            (- (gnus-point-at-bol)
5839               (prog1
5840                   (1+ (gnus-point-at-eol))
5841                 (gnus-delete-line))))))
5842       (when old-header
5843         (mail-header-set-number header (mail-header-number old-header)))
5844       (setq gnus-newsgroup-sparse
5845             (delq (setq number (mail-header-number header))
5846                   gnus-newsgroup-sparse))
5847       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5848       (push number gnus-newsgroup-limit)
5849       (gnus-rebuild-thread (mail-header-id header) line)
5850       (gnus-summary-goto-subject number nil t))
5851     (when (and (numberp number)
5852                (> number 0))
5853       ;; We have to update the boundaries even if we can't fetch the
5854       ;; article if ID is a number -- so that the next `P' or `N'
5855       ;; command will fetch the previous (or next) article even
5856       ;; if the one we tried to fetch this time has been canceled.
5857       (when (> number gnus-newsgroup-end)
5858         (setq gnus-newsgroup-end number))
5859       (when (< number gnus-newsgroup-begin)
5860         (setq gnus-newsgroup-begin number))
5861       (setq gnus-newsgroup-unselected
5862             (delq number gnus-newsgroup-unselected)))
5863     ;; Report back a success?
5864     (and header (mail-header-number header))))
5865
5866 ;;; Process/prefix in the summary buffer
5867
5868 (defun gnus-summary-work-articles (n)
5869   "Return a list of articles to be worked upon.
5870 The prefix argument, the list of process marked articles, and the
5871 current article will be taken into consideration."
5872   (save-excursion
5873     (set-buffer gnus-summary-buffer)
5874     (cond
5875      (n
5876       ;; A numerical prefix has been given.
5877       (setq n (prefix-numeric-value n))
5878       (let ((backward (< n 0))
5879             (n (abs (prefix-numeric-value n)))
5880             articles article)
5881         (save-excursion
5882           (while
5883               (and (> n 0)
5884                    (push (setq article (gnus-summary-article-number))
5885                          articles)
5886                    (if backward
5887                        (gnus-summary-find-prev nil article)
5888                      (gnus-summary-find-next nil article)))
5889             (decf n)))
5890         (nreverse articles)))
5891      ((and (gnus-region-active-p) (mark))
5892       (message "region active")
5893       ;; Work on the region between point and mark.
5894       (let ((max (max (point) (mark)))
5895             articles article)
5896         (save-excursion
5897           (goto-char (min (min (point) (mark))))
5898           (while
5899               (and
5900                (push (setq article (gnus-summary-article-number)) articles)
5901                (gnus-summary-find-next nil article)
5902                (< (point) max)))
5903           (nreverse articles))))
5904      (gnus-newsgroup-processable
5905       ;; There are process-marked articles present.
5906       ;; Save current state.
5907       (gnus-summary-save-process-mark)
5908       ;; Return the list.
5909       (reverse gnus-newsgroup-processable))
5910      (t
5911       ;; Just return the current article.
5912       (list (gnus-summary-article-number))))))
5913
5914 (defmacro gnus-summary-iterate (arg &rest forms)
5915   "Iterate over the process/prefixed articles and do FORMS.
5916 ARG is the interactive prefix given to the command.  FORMS will be
5917 executed with point over the summary line of the articles."
5918   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5919     `(let ((,articles (gnus-summary-work-articles ,arg)))
5920        (while ,articles
5921          (gnus-summary-goto-subject (car ,articles))
5922          ,@forms
5923          (pop ,articles)))))
5924
5925 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5926 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5927
5928 (defun gnus-summary-save-process-mark ()
5929   "Push the current set of process marked articles on the stack."
5930   (interactive)
5931   (push (copy-sequence gnus-newsgroup-processable)
5932         gnus-newsgroup-process-stack))
5933
5934 (defun gnus-summary-kill-process-mark ()
5935   "Push the current set of process marked articles on the stack and unmark."
5936   (interactive)
5937   (gnus-summary-save-process-mark)
5938   (gnus-summary-unmark-all-processable))
5939
5940 (defun gnus-summary-yank-process-mark ()
5941   "Pop the last process mark state off the stack and restore it."
5942   (interactive)
5943   (unless gnus-newsgroup-process-stack
5944     (error "Empty mark stack"))
5945   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5946
5947 (defun gnus-summary-process-mark-set (set)
5948   "Make SET into the current process marked articles."
5949   (gnus-summary-unmark-all-processable)
5950   (while set
5951     (gnus-summary-set-process-mark (pop set))))
5952
5953 ;;; Searching and stuff
5954
5955 (defun gnus-summary-search-group (&optional backward use-level)
5956   "Search for next unread newsgroup.
5957 If optional argument BACKWARD is non-nil, search backward instead."
5958   (save-excursion
5959     (set-buffer gnus-group-buffer)
5960     (when (gnus-group-search-forward
5961            backward nil (if use-level (gnus-group-group-level) nil))
5962       (gnus-group-group-name))))
5963
5964 (defun gnus-summary-best-group (&optional exclude-group)
5965   "Find the name of the best unread group.
5966 If EXCLUDE-GROUP, do not go to this group."
5967   (save-excursion
5968     (set-buffer gnus-group-buffer)
5969     (save-excursion
5970       (gnus-group-best-unread-group exclude-group))))
5971
5972 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5973   (if backward (gnus-summary-find-prev)
5974     (let* ((dummy (gnus-summary-article-intangible-p))
5975            (article (or article (gnus-summary-article-number)))
5976            (arts (gnus-data-find-list article))
5977            result)
5978       (when (and (not dummy)
5979                  (or (not gnus-summary-check-current)
5980                      (not unread)
5981                      (not (gnus-data-unread-p (car arts)))))
5982         (setq arts (cdr arts)))
5983       (when (setq result
5984                   (if unread
5985                       (progn
5986                         (while arts
5987                           (when (or (and undownloaded
5988                                          (memq (car arts) gnus-newsgroup-undownloaded))
5989                                     (gnus-data-unread-p (car arts)))
5990                             (setq result (car arts)
5991                                   arts nil))
5992                           (setq arts (cdr arts)))
5993                         result)
5994                     (car arts)))
5995         (goto-char (gnus-data-pos result))
5996         (gnus-data-number result)))))
5997
5998 (defun gnus-summary-find-prev (&optional unread article)
5999   (let* ((eobp (eobp))
6000          (article (or article (gnus-summary-article-number)))
6001          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
6002          result)
6003     (when (and (not eobp)
6004                (or (not gnus-summary-check-current)
6005                    (not unread)
6006                    (not (gnus-data-unread-p (car arts)))))
6007       (setq arts (cdr arts)))
6008     (when (setq result
6009                 (if unread
6010                     (progn
6011                       (while arts
6012                         (when (gnus-data-unread-p (car arts))
6013                           (setq result (car arts)
6014                                 arts nil))
6015                         (setq arts (cdr arts)))
6016                       result)
6017                   (car arts)))
6018       (goto-char (gnus-data-pos result))
6019       (gnus-data-number result))))
6020
6021 (defun gnus-summary-find-subject (subject &optional unread backward article)
6022   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6023          (article (or article (gnus-summary-article-number)))
6024          (articles (gnus-data-list backward))
6025          (arts (gnus-data-find-list article articles))
6026          result)
6027     (when (or (not gnus-summary-check-current)
6028               (not unread)
6029               (not (gnus-data-unread-p (car arts))))
6030       (setq arts (cdr arts)))
6031     (while arts
6032       (and (or (not unread)
6033                (gnus-data-unread-p (car arts)))
6034            (vectorp (gnus-data-header (car arts)))
6035            (gnus-subject-equal
6036             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6037            (setq result (car arts)
6038                  arts nil))
6039       (setq arts (cdr arts)))
6040     (and result
6041          (goto-char (gnus-data-pos result))
6042          (gnus-data-number result))))
6043
6044 (defun gnus-summary-search-forward (&optional unread subject backward)
6045   "Search forward for an article.
6046 If UNREAD, look for unread articles.  If SUBJECT, look for
6047 articles with that subject.  If BACKWARD, search backward instead."
6048   (cond (subject (gnus-summary-find-subject subject unread backward))
6049         (backward (gnus-summary-find-prev unread))
6050         (t (gnus-summary-find-next unread))))
6051
6052 (defun gnus-recenter (&optional n)
6053   "Center point in window and redisplay frame.
6054 Also do horizontal recentering."
6055   (interactive "P")
6056   (when (and gnus-auto-center-summary
6057              (not (eq gnus-auto-center-summary 'vertical)))
6058     (gnus-horizontal-recenter))
6059   (recenter n))
6060
6061 (defun gnus-summary-recenter ()
6062   "Center point in the summary window.
6063 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6064 displayed, no centering will be performed."
6065   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6066 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6067   (interactive)
6068   (let* ((top (cond ((< (window-height) 4) 0)
6069                     ((< (window-height) 7) 1)
6070                     (t (if (numberp gnus-auto-center-summary)
6071                            gnus-auto-center-summary
6072                          2))))
6073          (height (1- (window-height)))
6074          (bottom (save-excursion (goto-char (point-max))
6075                                  (forward-line (- height))
6076                                  (point)))
6077          (window (get-buffer-window (current-buffer))))
6078     ;; The user has to want it.
6079     (when gnus-auto-center-summary
6080       (when (get-buffer-window gnus-article-buffer)
6081         ;; Only do recentering when the article buffer is displayed,
6082       ;; Set the window start to either `bottom', which is the biggest
6083         ;; possible valid number, or the second line from the top,
6084         ;; whichever is the least.
6085         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6086           (if (> bottom top-pos)
6087               ;; Keep the second line from the top visible
6088               (set-window-start window top-pos t)
6089             ;; Try to keep the bottom line visible; if it's partially
6090             ;; obscured, either scroll one more line to make it fully
6091             ;; visible, or revert to using TOP-POS.
6092             (save-excursion
6093               (goto-char (point-max))
6094               (forward-line -1)
6095               (let ((last-line-start (point)))
6096                 (goto-char bottom)
6097                 (set-window-start window (point) t)
6098                 (when (not (pos-visible-in-window-p last-line-start window))
6099                   (forward-line 1)
6100                   (set-window-start window (min (point) top-pos) t)))))))
6101       ;; Do horizontal recentering while we're at it.
6102       (when (and (get-buffer-window (current-buffer) t)
6103                  (not (eq gnus-auto-center-summary 'vertical)))
6104         (let ((selected (selected-window)))
6105           (select-window (get-buffer-window (current-buffer) t))
6106           (gnus-summary-position-point)
6107           (gnus-horizontal-recenter)
6108           (select-window selected))))))
6109
6110 (defun gnus-summary-jump-to-group (newsgroup)
6111   "Move point to NEWSGROUP in group mode buffer."
6112   ;; Keep update point of group mode buffer if visible.
6113   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6114       (save-window-excursion
6115         ;; Take care of tree window mode.
6116         (when (get-buffer-window gnus-group-buffer)
6117           (pop-to-buffer gnus-group-buffer))
6118         (gnus-group-jump-to-group newsgroup))
6119     (save-excursion
6120       ;; Take care of tree window mode.
6121       (if (get-buffer-window gnus-group-buffer)
6122           (pop-to-buffer gnus-group-buffer)
6123         (set-buffer gnus-group-buffer))
6124       (gnus-group-jump-to-group newsgroup))))
6125
6126 ;; This function returns a list of article numbers based on the
6127 ;; difference between the ranges of read articles in this group and
6128 ;; the range of active articles.
6129 (defun gnus-list-of-unread-articles (group)
6130   (let* ((read (gnus-info-read (gnus-get-info group)))
6131          (active (or (gnus-active group) (gnus-activate-group group)))
6132          (last (cdr active))
6133          first nlast unread)
6134     ;; If none are read, then all are unread.
6135     (if (not read)
6136         (setq first (car active))
6137       ;; If the range of read articles is a single range, then the
6138       ;; first unread article is the article after the last read
6139       ;; article.  Sounds logical, doesn't it?
6140       (if (and (not (listp (cdr read)))
6141                (or (< (car read) (car active))
6142                    (progn (setq read (list read))
6143                           nil)))
6144           (setq first (max (car active) (1+ (cdr read))))
6145         ;; `read' is a list of ranges.
6146         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6147                                   (caar read)))
6148                   1)
6149           (setq first (car active)))
6150         (while read
6151           (when first
6152             (while (< first nlast)
6153               (push first unread)
6154               (setq first (1+ first))))
6155           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6156           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6157           (setq read (cdr read)))))
6158     ;; And add the last unread articles.
6159     (while (<= first last)
6160       (push first unread)
6161       (setq first (1+ first)))
6162     ;; Return the list of unread articles.
6163     (delq 0 (nreverse unread))))
6164
6165 (defun gnus-list-of-read-articles (group)
6166   "Return a list of unread, unticked and non-dormant articles."
6167   (let* ((info (gnus-get-info group))
6168          (marked (gnus-info-marks info))
6169          (active (gnus-active group)))
6170     (and info active
6171          (gnus-list-range-difference
6172           (gnus-list-range-difference
6173            (gnus-sorted-complement
6174             (gnus-uncompress-range active)
6175             (gnus-list-of-unread-articles group))
6176            (cdr (assq 'dormant marked)))
6177           (cdr (assq 'tick marked))))))
6178
6179 ;; Various summary commands
6180
6181 (defun gnus-summary-select-article-buffer ()
6182   "Reconfigure windows to show article buffer."
6183   (interactive)
6184   (if (not (gnus-buffer-live-p gnus-article-buffer))
6185       (error "There is no article buffer for this summary buffer")
6186     (gnus-configure-windows 'article)
6187     (select-window (get-buffer-window gnus-article-buffer))))
6188
6189 (defun gnus-summary-universal-argument (arg)
6190   "Perform any operation on all articles that are process/prefixed."
6191   (interactive "P")
6192   (let ((articles (gnus-summary-work-articles arg))
6193         func article)
6194     (if (eq
6195          (setq
6196           func
6197           (key-binding
6198            (read-key-sequence
6199             (substitute-command-keys
6200              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6201          'undefined)
6202         (gnus-error 1 "Undefined key")
6203       (save-excursion
6204         (while articles
6205           (gnus-summary-goto-subject (setq article (pop articles)))
6206           (let (gnus-newsgroup-processable)
6207             (command-execute func))
6208           (gnus-summary-remove-process-mark article)))))
6209   (gnus-summary-position-point))
6210
6211 (defun gnus-summary-toggle-truncation (&optional arg)
6212   "Toggle truncation of summary lines.
6213 With arg, turn line truncation on if arg is positive."
6214   (interactive "P")
6215   (setq truncate-lines
6216         (if (null arg) (not truncate-lines)
6217           (> (prefix-numeric-value arg) 0)))
6218   (redraw-display))
6219
6220 (defun gnus-summary-find-uncancelled ()
6221   "Return the number of an uncancelled article.
6222 The current article is considered, then following articles, then previous
6223 articles.  If all articles are cancelled then return a dummy 0."
6224   (let (found)
6225     (dolist (rev '(nil t))
6226       (unless found      ; don't demand the reverse list if we don't need it
6227         (let ((data (gnus-data-find-list
6228                      (gnus-summary-article-number) (gnus-data-list rev))))
6229           (while (and data (not found))
6230             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6231                 (setq found (gnus-data-number (car data))))
6232             (setq data (cdr data))))))
6233     (or found 0)))
6234
6235 (defun gnus-summary-reselect-current-group (&optional all rescan)
6236   "Exit and then reselect the current newsgroup.
6237 The prefix argument ALL means to select all articles."
6238   (interactive "P")
6239   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6240     (error "Ephemeral groups can't be reselected"))
6241   (let ((current-subject (gnus-summary-find-uncancelled))
6242         (group gnus-newsgroup-name))
6243     (setq gnus-newsgroup-begin nil)
6244     (gnus-summary-exit)
6245     ;; We have to adjust the point of group mode buffer because
6246     ;; point was moved to the next unread newsgroup by exiting.
6247     (gnus-summary-jump-to-group group)
6248     (when rescan
6249       (save-excursion
6250         (gnus-group-get-new-news-this-group 1)))
6251     (gnus-group-read-group all t)
6252     (gnus-summary-goto-subject current-subject nil t)))
6253
6254 (defun gnus-summary-rescan-group (&optional all)
6255   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6256   (interactive "P")
6257   (gnus-summary-reselect-current-group all t))
6258
6259 (defun gnus-summary-update-info (&optional non-destructive)
6260   (save-excursion
6261     (let ((group gnus-newsgroup-name))
6262       (when group
6263         (when gnus-newsgroup-kill-headers
6264           (setq gnus-newsgroup-killed
6265                 (gnus-compress-sequence
6266                  (gnus-sorted-union
6267                   (gnus-list-range-intersection
6268                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6269                   gnus-newsgroup-unreads)
6270                  t)))
6271         (unless (listp (cdr gnus-newsgroup-killed))
6272           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6273         (let ((headers gnus-newsgroup-headers))
6274           ;; Set the new ranges of read articles.
6275           (save-excursion
6276             (set-buffer gnus-group-buffer)
6277             (gnus-undo-force-boundary))
6278           (gnus-update-read-articles
6279            group (gnus-sorted-union
6280                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6281           ;; Set the current article marks.
6282           (let ((gnus-newsgroup-scored
6283                  (if (and (not gnus-save-score)
6284                           (not non-destructive))
6285                      nil
6286                    gnus-newsgroup-scored)))
6287             (save-excursion
6288               (gnus-update-marks)))
6289           ;; Do the cross-ref thing.
6290           (when gnus-use-cross-reference
6291             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6292           ;; Do not switch windows but change the buffer to work.
6293           (set-buffer gnus-group-buffer)
6294           (unless (gnus-ephemeral-group-p group)
6295             (gnus-group-update-group group)))))))
6296
6297 (defun gnus-summary-save-newsrc (&optional force)
6298   "Save the current number of read/marked articles in the dribble buffer.
6299 The dribble buffer will then be saved.
6300 If FORCE (the prefix), also save the .newsrc file(s)."
6301   (interactive "P")
6302   (gnus-summary-update-info t)
6303   (if force
6304       (gnus-save-newsrc-file)
6305     (gnus-dribble-save)))
6306
6307 (defun gnus-summary-exit (&optional temporary)
6308   "Exit reading current newsgroup, and then return to group selection mode.
6309 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6310   (interactive)
6311   (gnus-set-global-variables)
6312   (when (gnus-buffer-live-p gnus-article-buffer)
6313     (save-excursion
6314       (set-buffer gnus-article-buffer)
6315       (mm-destroy-parts gnus-article-mime-handles)
6316       ;; Set it to nil for safety reason.
6317       (setq gnus-article-mime-handle-alist nil)
6318       (setq gnus-article-mime-handles nil)))
6319   (gnus-kill-save-kill-buffer)
6320   (gnus-async-halt-prefetch)
6321   (let* ((group gnus-newsgroup-name)
6322          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6323          (gnus-group-is-exiting-p t)
6324          (mode major-mode)
6325          (group-point nil)
6326          (buf (current-buffer)))
6327     (unless quit-config
6328       ;; Do adaptive scoring, and possibly save score files.
6329       (when gnus-newsgroup-adaptive
6330         (gnus-score-adaptive))
6331       (when gnus-use-scoring
6332         (gnus-score-save)))
6333     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6334     ;; If we have several article buffers, we kill them at exit.
6335     (unless gnus-single-article-buffer
6336       (gnus-kill-buffer gnus-original-article-buffer)
6337       (setq gnus-article-current nil))
6338     (when gnus-use-cache
6339       (gnus-cache-possibly-remove-articles)
6340       (gnus-cache-save-buffers))
6341     (gnus-async-prefetch-remove-group group)
6342     (when gnus-suppress-duplicates
6343       (gnus-dup-enter-articles))
6344     (when gnus-use-trees
6345       (gnus-tree-close group))
6346     (when gnus-use-cache
6347       (gnus-cache-write-active))
6348     ;; Remove entries for this group.
6349     (nnmail-purge-split-history (gnus-group-real-name group))
6350     ;; Make all changes in this group permanent.
6351     (unless quit-config
6352       (gnus-run-hooks 'gnus-exit-group-hook)
6353       (gnus-summary-update-info))
6354     (gnus-close-group group)
6355     ;; Make sure where we were, and go to next newsgroup.
6356     (set-buffer gnus-group-buffer)
6357     (unless quit-config
6358       (gnus-group-jump-to-group group))
6359     (gnus-run-hooks 'gnus-summary-exit-hook)
6360     (unless (or quit-config
6361                 ;; If this group has disappeared from the summary
6362                 ;; buffer, don't skip forwards.
6363                 (not (string= group (gnus-group-group-name))))
6364       (gnus-group-next-unread-group 1))
6365     (setq group-point (point))
6366     (if temporary
6367         nil                             ;Nothing to do.
6368       ;; If we have several article buffers, we kill them at exit.
6369       (unless gnus-single-article-buffer
6370         (gnus-kill-buffer gnus-article-buffer)
6371         (gnus-kill-buffer gnus-original-article-buffer)
6372         (setq gnus-article-current nil))
6373       (set-buffer buf)
6374       (if (not gnus-kill-summary-on-exit)
6375           (progn
6376             (gnus-deaden-summary)
6377             (setq mode nil))
6378         ;; We set all buffer-local variables to nil.  It is unclear why
6379         ;; this is needed, but if we don't, buffer-local variables are
6380         ;; not garbage-collected, it seems.  This would the lead to en
6381         ;; ever-growing Emacs.
6382         (gnus-summary-clear-local-variables)
6383         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6384           (gnus-summary-clear-local-variables))
6385         (when (get-buffer gnus-article-buffer)
6386           (bury-buffer gnus-article-buffer))
6387         ;; We clear the global counterparts of the buffer-local
6388         ;; variables as well, just to be on the safe side.
6389         (set-buffer gnus-group-buffer)
6390         (gnus-summary-clear-local-variables)
6391         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6392           (gnus-summary-clear-local-variables)))
6393       (setq gnus-current-select-method gnus-select-method)
6394       (pop-to-buffer gnus-group-buffer)
6395       (if (not quit-config)
6396           (progn
6397             (goto-char group-point)
6398             (gnus-configure-windows 'group 'force))
6399         (gnus-handle-ephemeral-exit quit-config))
6400       ;; Return to group mode buffer.
6401       (when (eq mode 'gnus-summary-mode)
6402         (gnus-kill-buffer buf))
6403       ;; Clear the current group name.
6404       (unless quit-config
6405         (setq gnus-newsgroup-name nil)))))
6406
6407 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6408 (defun gnus-summary-exit-no-update (&optional no-questions)
6409   "Quit reading current newsgroup without updating read article info."
6410   (interactive)
6411   (let* ((group gnus-newsgroup-name)
6412          (gnus-group-is-exiting-p t)
6413          (quit-config (gnus-group-quit-config group)))
6414     (when (or no-questions
6415               gnus-expert-user
6416               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6417       (gnus-async-halt-prefetch)
6418       (mapcar 'funcall
6419               (delq 'gnus-summary-expire-articles
6420                     (copy-sequence gnus-summary-prepare-exit-hook)))
6421       (when (gnus-buffer-live-p gnus-article-buffer)
6422         (save-excursion
6423           (set-buffer gnus-article-buffer)
6424           (mm-destroy-parts gnus-article-mime-handles)
6425           ;; Set it to nil for safety reason.
6426           (setq gnus-article-mime-handle-alist nil)
6427           (setq gnus-article-mime-handles nil)))
6428       ;; If we have several article buffers, we kill them at exit.
6429       (unless gnus-single-article-buffer
6430         (gnus-kill-buffer gnus-article-buffer)
6431         (gnus-kill-buffer gnus-original-article-buffer)
6432         (setq gnus-article-current nil))
6433       (if (not gnus-kill-summary-on-exit)
6434           (gnus-deaden-summary)
6435         (gnus-close-group group)
6436         (gnus-summary-clear-local-variables)
6437         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6438           (gnus-summary-clear-local-variables))
6439         (set-buffer gnus-group-buffer)
6440         (gnus-summary-clear-local-variables)
6441         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6442           (gnus-summary-clear-local-variables))
6443         (when (get-buffer gnus-summary-buffer)
6444           (kill-buffer gnus-summary-buffer)))
6445       (unless gnus-single-article-buffer
6446         (setq gnus-article-current nil))
6447       (when gnus-use-trees
6448         (gnus-tree-close group))
6449       (gnus-async-prefetch-remove-group group)
6450       (when (get-buffer gnus-article-buffer)
6451         (bury-buffer gnus-article-buffer))
6452       ;; Return to the group buffer.
6453       (gnus-configure-windows 'group 'force)
6454       ;; Clear the current group name.
6455       (setq gnus-newsgroup-name nil)
6456       (unless (gnus-ephemeral-group-p group)
6457         (gnus-group-update-group group))
6458       (when (equal (gnus-group-group-name) group)
6459         (gnus-group-next-unread-group 1))
6460       (when quit-config
6461         (gnus-handle-ephemeral-exit quit-config)))))
6462
6463 (defun gnus-handle-ephemeral-exit (quit-config)
6464   "Handle movement when leaving an ephemeral group.
6465 The state which existed when entering the ephemeral is reset."
6466   (if (not (buffer-name (car quit-config)))
6467       (gnus-configure-windows 'group 'force)
6468     (set-buffer (car quit-config))
6469     (cond ((eq major-mode 'gnus-summary-mode)
6470            (gnus-set-global-variables))
6471           ((eq major-mode 'gnus-article-mode)
6472            (save-excursion
6473              ;; The `gnus-summary-buffer' variable may point
6474              ;; to the old summary buffer when using a single
6475              ;; article buffer.
6476              (unless (gnus-buffer-live-p gnus-summary-buffer)
6477                (set-buffer gnus-group-buffer))
6478              (set-buffer gnus-summary-buffer)
6479              (gnus-set-global-variables))))
6480     (if (or (eq (cdr quit-config) 'article)
6481             (eq (cdr quit-config) 'pick))
6482         (progn
6483           ;; The current article may be from the ephemeral group
6484           ;; thus it is best that we reload this article
6485           (gnus-summary-show-article)
6486           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6487               (gnus-configure-windows 'pick 'force)
6488             (gnus-configure-windows (cdr quit-config) 'force)))
6489       (gnus-configure-windows (cdr quit-config) 'force))
6490     (when (eq major-mode 'gnus-summary-mode)
6491       (gnus-summary-next-subject 1 nil t)
6492       (gnus-summary-recenter)
6493       (gnus-summary-position-point))))
6494
6495 ;;; Dead summaries.
6496
6497 (defvar gnus-dead-summary-mode-map nil)
6498
6499 (unless gnus-dead-summary-mode-map
6500   (setq gnus-dead-summary-mode-map (make-keymap))
6501   (suppress-keymap gnus-dead-summary-mode-map)
6502   (substitute-key-definition
6503    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6504   (dolist (key '("\C-d" "\r" "\177" [delete]))
6505     (define-key gnus-dead-summary-mode-map
6506       key 'gnus-summary-wake-up-the-dead))
6507   (dolist (key '("q" "Q"))
6508     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6509
6510 (defvar gnus-dead-summary-mode nil
6511   "Minor mode for Gnus summary buffers.")
6512
6513 (defun gnus-dead-summary-mode (&optional arg)
6514   "Minor mode for Gnus summary buffers."
6515   (interactive "P")
6516   (when (eq major-mode 'gnus-summary-mode)
6517     (make-local-variable 'gnus-dead-summary-mode)
6518     (setq gnus-dead-summary-mode
6519           (if (null arg) (not gnus-dead-summary-mode)
6520             (> (prefix-numeric-value arg) 0)))
6521     (when gnus-dead-summary-mode
6522       (gnus-add-minor-mode
6523        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6524
6525 (defun gnus-deaden-summary ()
6526   "Make the current summary buffer into a dead summary buffer."
6527   ;; Kill any previous dead summary buffer.
6528   (when (and gnus-dead-summary
6529              (buffer-name gnus-dead-summary))
6530     (save-excursion
6531       (set-buffer gnus-dead-summary)
6532       (when gnus-dead-summary-mode
6533         (kill-buffer (current-buffer)))))
6534   ;; Make this the current dead summary.
6535   (setq gnus-dead-summary (current-buffer))
6536   (gnus-dead-summary-mode 1)
6537   (let ((name (buffer-name)))
6538     (when (string-match "Summary" name)
6539       (rename-buffer
6540        (concat (substring name 0 (match-beginning 0)) "Dead "
6541                (substring name (match-beginning 0)))
6542        t)
6543       (bury-buffer))))
6544
6545 (defun gnus-kill-or-deaden-summary (buffer)
6546   "Kill or deaden the summary BUFFER."
6547   (save-excursion
6548     (when (and (buffer-name buffer)
6549                (not gnus-single-article-buffer))
6550       (save-excursion
6551         (set-buffer buffer)
6552         (gnus-kill-buffer gnus-article-buffer)
6553         (gnus-kill-buffer gnus-original-article-buffer)))
6554     (cond
6555      ;; Kill the buffer.
6556      (gnus-kill-summary-on-exit
6557       (when (and gnus-use-trees
6558                  (gnus-buffer-exists-p buffer))
6559         (save-excursion
6560           (set-buffer buffer)
6561           (gnus-tree-close gnus-newsgroup-name)))
6562       (gnus-kill-buffer buffer))
6563      ;; Deaden the buffer.
6564      ((gnus-buffer-exists-p buffer)
6565       (save-excursion
6566         (set-buffer buffer)
6567         (gnus-deaden-summary))))))
6568
6569 (defun gnus-summary-wake-up-the-dead (&rest args)
6570   "Wake up the dead summary buffer."
6571   (interactive)
6572   (gnus-dead-summary-mode -1)
6573   (let ((name (buffer-name)))
6574     (when (string-match "Dead " name)
6575       (rename-buffer
6576        (concat (substring name 0 (match-beginning 0))
6577                (substring name (match-end 0)))
6578        t)))
6579   (gnus-message 3 "This dead summary is now alive again"))
6580
6581 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6582 (defun gnus-summary-fetch-faq (&optional faq-dir)
6583   "Fetch the FAQ for the current group.
6584 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6585 in."
6586   (interactive
6587    (list
6588     (when current-prefix-arg
6589       (completing-read
6590        "FAQ dir: " (and (listp gnus-group-faq-directory)
6591                         (mapcar (lambda (file) (list file))
6592                                 gnus-group-faq-directory))))))
6593   (let (gnus-faq-buffer)
6594     (when (setq gnus-faq-buffer
6595                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6596       (gnus-configure-windows 'summary-faq))))
6597
6598 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6599 (defun gnus-summary-describe-group (&optional force)
6600   "Describe the current newsgroup."
6601   (interactive "P")
6602   (gnus-group-describe-group force gnus-newsgroup-name))
6603
6604 (defun gnus-summary-describe-briefly ()
6605   "Describe summary mode commands briefly."
6606   (interactive)
6607   (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")))
6608
6609 ;; Walking around group mode buffer from summary mode.
6610
6611 (defun gnus-summary-next-group (&optional no-article target-group backward)
6612   "Exit current newsgroup and then select next unread newsgroup.
6613 If prefix argument NO-ARTICLE is non-nil, no article is selected
6614 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6615 previous group instead."
6616   (interactive "P")
6617   ;; Stop pre-fetching.
6618   (gnus-async-halt-prefetch)
6619   (let ((current-group gnus-newsgroup-name)
6620         (current-buffer (current-buffer))
6621         entered)
6622     (while (not entered)
6623       ;; Then we find what group we are supposed to enter.
6624       (set-buffer gnus-group-buffer)
6625       (gnus-group-jump-to-group current-group)
6626       (setq target-group
6627             (or target-group
6628                 (if (eq gnus-keep-same-level 'best)
6629                     (gnus-summary-best-group gnus-newsgroup-name)
6630                   (gnus-summary-search-group backward gnus-keep-same-level))))
6631       (if (not target-group)
6632           ;; There are no further groups, so we return to the group
6633           ;; buffer.
6634           (progn
6635             (gnus-message 5 "Returning to the group buffer")
6636             (setq entered t)
6637             (when (gnus-buffer-live-p current-buffer)
6638               (set-buffer current-buffer)
6639               (gnus-summary-exit))
6640             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6641         ;; We try to enter the target group.
6642         (gnus-group-jump-to-group target-group)
6643         (let ((unreads (gnus-group-group-unread)))
6644           (if (and (or (eq t unreads)
6645                        (and unreads (not (zerop unreads))))
6646                    (progn
6647                      ;; Now we semi-exit this group to update Xrefs
6648                      ;; and all variables.  We can't do a real exit,
6649                      ;; because the window conf must remain the same
6650                      ;; in case the user is prompted for info, and we
6651                      ;; don't want the window conf to change before
6652                      ;; that...
6653                      (when (gnus-buffer-live-p current-buffer)
6654                        (set-buffer current-buffer)
6655                        (gnus-summary-exit t))
6656                      (gnus-summary-read-group
6657                       target-group nil no-article
6658                       (and (buffer-name current-buffer) current-buffer)
6659                       nil backward)))
6660               (setq entered t)
6661             (setq current-group target-group
6662                   target-group nil)))))))
6663
6664 (defun gnus-summary-prev-group (&optional no-article)
6665   "Exit current newsgroup and then select previous unread newsgroup.
6666 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6667   (interactive "P")
6668   (gnus-summary-next-group no-article nil t))
6669
6670 ;; Walking around summary lines.
6671
6672 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6673   "Go to the first unread subject.
6674 If UNREAD is non-nil, go to the first unread article.
6675 Returns the article selected or nil if there are no unread articles."
6676   (interactive "P")
6677   (prog1
6678       (cond
6679        ;; Empty summary.
6680        ((null gnus-newsgroup-data)
6681         (gnus-message 3 "No articles in the group")
6682         nil)
6683        ;; Pick the first article.
6684        ((not unread)
6685         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6686         (gnus-data-number (car gnus-newsgroup-data)))
6687        ;; No unread articles.
6688        ((null gnus-newsgroup-unreads)
6689         (gnus-message 3 "No more unread articles")
6690         nil)
6691        ;; Find the first unread article.
6692        (t
6693         (let ((data gnus-newsgroup-data))
6694           (while (and data
6695                       (and (not (and undownloaded
6696                                      (memq (car data)
6697                                            gnus-newsgroup-undownloaded)))
6698                            (if unseen
6699                                (or (not (memq
6700                                          (gnus-data-number (car data))
6701                                          gnus-newsgroup-unseen))
6702                                    (not (gnus-data-unread-p (car data))))
6703                              (not (gnus-data-unread-p (car data))))))
6704             (setq data (cdr data)))
6705           (when data
6706             (goto-char (gnus-data-pos (car data)))
6707             (gnus-data-number (car data))))))
6708     (gnus-summary-position-point)))
6709
6710 (defun gnus-summary-next-subject (n &optional unread dont-display)
6711   "Go to next N'th summary line.
6712 If N is negative, go to the previous N'th subject line.
6713 If UNREAD is non-nil, only unread articles are selected.
6714 The difference between N and the actual number of steps taken is
6715 returned."
6716   (interactive "p")
6717   (let ((backward (< n 0))
6718         (n (abs n)))
6719     (while (and (> n 0)
6720                 (if backward
6721                     (gnus-summary-find-prev unread)
6722                   (gnus-summary-find-next unread)))
6723       (unless (zerop (setq n (1- n)))
6724         (gnus-summary-show-thread)))
6725     (when (/= 0 n)
6726       (gnus-message 7 "No more%s articles"
6727                     (if unread " unread" "")))
6728     (unless dont-display
6729       (gnus-summary-recenter)
6730       (gnus-summary-position-point))
6731     n))
6732
6733 (defun gnus-summary-next-unread-subject (n)
6734   "Go to next N'th unread summary line."
6735   (interactive "p")
6736   (gnus-summary-next-subject n t))
6737
6738 (defun gnus-summary-prev-subject (n &optional unread)
6739   "Go to previous N'th summary line.
6740 If optional argument UNREAD is non-nil, only unread article is selected."
6741   (interactive "p")
6742   (gnus-summary-next-subject (- n) unread))
6743
6744 (defun gnus-summary-prev-unread-subject (n)
6745   "Go to previous N'th unread summary line."
6746   (interactive "p")
6747   (gnus-summary-next-subject (- n) t))
6748
6749 (defun gnus-summary-goto-subjects (articles)
6750   "Insert the subject header for ARTICLES in the current buffer."
6751   (save-excursion
6752     (dolist (article articles)
6753       (gnus-summary-goto-subject article t)))
6754   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6755   (gnus-summary-position-point))
6756   
6757 (defun gnus-summary-goto-subject (article &optional force silent)
6758   "Go the subject line of ARTICLE.
6759 If FORCE, also allow jumping to articles not currently shown."
6760   (interactive "nArticle number: ")
6761   (unless (numberp article)
6762     (error "Article %s is not a number" article))
6763   (let ((b (point))
6764         (data (gnus-data-find article)))
6765     ;; We read in the article if we have to.
6766     (and (not data)
6767          force
6768          (gnus-summary-insert-subject
6769           article
6770           (if (or (numberp force) (vectorp force)) force)
6771           t)
6772          (setq data (gnus-data-find article)))
6773     (goto-char b)
6774     (if (not data)
6775         (progn
6776           (unless silent
6777             (gnus-message 3 "Can't find article %d" article))
6778           nil)
6779       (let ((pt (gnus-data-pos data)))
6780         (goto-char pt)
6781         (gnus-summary-set-article-display-arrow pt))
6782       (gnus-summary-position-point)
6783       article)))
6784
6785 ;; Walking around summary lines with displaying articles.
6786
6787 (defun gnus-summary-expand-window (&optional arg)
6788   "Make the summary buffer take up the entire Emacs frame.
6789 Given a prefix, will force an `article' buffer configuration."
6790   (interactive "P")
6791   (if arg
6792       (gnus-configure-windows 'article 'force)
6793     (gnus-configure-windows 'summary 'force)))
6794
6795 (defun gnus-summary-display-article (article &optional all-header)
6796   "Display ARTICLE in article buffer."
6797   (when (gnus-buffer-live-p gnus-article-buffer)
6798     (with-current-buffer gnus-article-buffer
6799       (mm-enable-multibyte)))
6800   (gnus-set-global-variables)
6801   (when (gnus-buffer-live-p gnus-article-buffer)
6802     (with-current-buffer gnus-article-buffer
6803       (setq gnus-article-charset gnus-newsgroup-charset)
6804       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6805       (mm-enable-multibyte)))
6806   (if (null article)
6807       nil
6808     (prog1
6809         (if gnus-summary-display-article-function
6810             (funcall gnus-summary-display-article-function article all-header)
6811           (gnus-article-prepare article all-header))
6812       (gnus-run-hooks 'gnus-select-article-hook)
6813       (when (and gnus-current-article
6814                  (not (zerop gnus-current-article)))
6815         (gnus-summary-goto-subject gnus-current-article))
6816       (gnus-summary-recenter)
6817       (when (and gnus-use-trees gnus-show-threads)
6818         (gnus-possibly-generate-tree article)
6819         (gnus-highlight-selected-tree article))
6820       ;; Successfully display article.
6821       (gnus-article-set-window-start
6822        (cdr (assq article gnus-newsgroup-bookmarks))))))
6823
6824 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6825   "Select the current article.
6826 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6827 non-nil, the article will be re-fetched even if it already present in
6828 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6829 be displayed."
6830   ;; Make sure we are in the summary buffer to work around bbdb bug.
6831   (unless (eq major-mode 'gnus-summary-mode)
6832     (set-buffer gnus-summary-buffer))
6833   (let ((article (or article (gnus-summary-article-number)))
6834         (all-headers (not (not all-headers))) ;Must be t or nil.
6835         gnus-summary-display-article-function)
6836     (and (not pseudo)
6837          (gnus-summary-article-pseudo-p article)
6838          (error "This is a pseudo-article"))
6839     (save-excursion
6840       (set-buffer gnus-summary-buffer)
6841       (if (or (and gnus-single-article-buffer
6842                    (or (null gnus-current-article)
6843                        (null gnus-article-current)
6844                        (null (get-buffer gnus-article-buffer))
6845                        (not (eq article (cdr gnus-article-current)))
6846                        (not (equal (car gnus-article-current)
6847                                    gnus-newsgroup-name))))
6848               (and (not gnus-single-article-buffer)
6849                    (or (null gnus-current-article)
6850                        (not (eq gnus-current-article article))))
6851               force)
6852           ;; The requested article is different from the current article.
6853           (progn
6854             (gnus-summary-display-article article all-headers)
6855             (when (gnus-buffer-live-p gnus-article-buffer)
6856               (with-current-buffer gnus-article-buffer
6857                 (if (not gnus-article-decoded-p) ;; a local variable
6858                     (mm-disable-multibyte))))
6859             (gnus-article-set-window-start
6860              (cdr (assq article gnus-newsgroup-bookmarks)))
6861             article)
6862         'old))))
6863
6864 (defun gnus-summary-force-verify-and-decrypt ()
6865   (interactive)
6866   (let ((mm-verify-option 'known)
6867         (mm-decrypt-option 'known)
6868         (gnus-buttonized-mime-types (append (list "multipart/signed"
6869                                                   "multipart/encrypted")
6870                                             gnus-buttonized-mime-types)))
6871     (gnus-summary-select-article nil 'force)))
6872
6873 (defun gnus-summary-set-current-mark (&optional current-mark)
6874   "Obsolete function."
6875   nil)
6876
6877 (defun gnus-summary-next-article (&optional unread subject backward push)
6878   "Select the next article.
6879 If UNREAD, only unread articles are selected.
6880 If SUBJECT, only articles with SUBJECT are selected.
6881 If BACKWARD, the previous article is selected instead of the next."
6882   (interactive "P")
6883   (cond
6884    ;; Is there such an article?
6885    ((and (gnus-summary-search-forward unread subject backward)
6886          (or (gnus-summary-display-article (gnus-summary-article-number))
6887              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6888     (gnus-summary-position-point))
6889    ;; If not, we try the first unread, if that is wanted.
6890    ((and subject
6891          gnus-auto-select-same
6892          (gnus-summary-first-unread-article))
6893     (gnus-summary-position-point)
6894     (gnus-message 6 "Wrapped"))
6895    ;; Try to get next/previous article not displayed in this group.
6896    ((and gnus-auto-extend-newsgroup
6897          (not unread) (not subject))
6898     (gnus-summary-goto-article
6899      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6900      nil (count-lines (point-min) (point))))
6901    ;; Go to next/previous group.
6902    (t
6903     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6904       (gnus-summary-jump-to-group gnus-newsgroup-name))
6905     (let ((cmd last-command-char)
6906           (point
6907            (save-excursion
6908              (set-buffer gnus-group-buffer)
6909              (point)))
6910           (group
6911            (if (eq gnus-keep-same-level 'best)
6912                (gnus-summary-best-group gnus-newsgroup-name)
6913              (gnus-summary-search-group backward gnus-keep-same-level))))
6914       ;; For some reason, the group window gets selected.  We change
6915       ;; it back.
6916       (select-window (get-buffer-window (current-buffer)))
6917       ;; Select next unread newsgroup automagically.
6918       (cond
6919        ((or (not gnus-auto-select-next)
6920             (not cmd))
6921         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6922        ((or (eq gnus-auto-select-next 'quietly)
6923             (and (eq gnus-auto-select-next 'slightly-quietly)
6924                  push)
6925             (and (eq gnus-auto-select-next 'almost-quietly)
6926                  (gnus-summary-last-article-p)))
6927         ;; Select quietly.
6928         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6929             (gnus-summary-exit)
6930           (gnus-message 7 "No more%s articles (%s)..."
6931                         (if unread " unread" "")
6932                         (if group (concat "selecting " group)
6933                           "exiting"))
6934           (gnus-summary-next-group nil group backward)))
6935        (t
6936         (when (gnus-key-press-event-p last-input-event)
6937           (gnus-summary-walk-group-buffer
6938            gnus-newsgroup-name cmd unread backward point))))))))
6939
6940 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6941   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6942                       (?\C-p (gnus-group-prev-unread-group 1))))
6943         (cursor-in-echo-area t)
6944         keve key group ended prompt)
6945     (save-excursion
6946       (set-buffer gnus-group-buffer)
6947       (goto-char start)
6948       (setq group
6949             (if (eq gnus-keep-same-level 'best)
6950                 (gnus-summary-best-group gnus-newsgroup-name)
6951               (gnus-summary-search-group backward gnus-keep-same-level))))
6952     (while (not ended)
6953       (setq prompt
6954             (format
6955              "No more%s articles%s " (if unread " unread" "")
6956              (if (and group
6957                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6958                  (format " (Type %s for %s [%s])"
6959                          (single-key-description cmd) group
6960                          (car (gnus-gethash group gnus-newsrc-hashtb)))
6961                (format " (Type %s to exit %s)"
6962                        (single-key-description cmd)
6963                        gnus-newsgroup-name))))
6964       ;; Confirm auto selection.
6965       (setq key (car (setq keve (gnus-read-event-char prompt)))
6966             ended t)
6967       (cond
6968        ((assq key keystrokes)
6969         (let ((obuf (current-buffer)))
6970           (switch-to-buffer gnus-group-buffer)
6971           (when group
6972             (gnus-group-jump-to-group group))
6973           (eval (cadr (assq key keystrokes)))
6974           (setq group (gnus-group-group-name))
6975           (switch-to-buffer obuf))
6976         (setq ended nil))
6977        ((equal key cmd)
6978         (if (or (not group)
6979                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6980             (gnus-summary-exit)
6981           (gnus-summary-next-group nil group backward)))
6982        (t
6983         (push (cdr keve) unread-command-events))))))
6984
6985 (defun gnus-summary-next-unread-article ()
6986   "Select unread article after current one."
6987   (interactive)
6988   (gnus-summary-next-article
6989    (or (not (eq gnus-summary-goto-unread 'never))
6990        (gnus-summary-last-article-p (gnus-summary-article-number)))
6991    (and gnus-auto-select-same
6992         (gnus-summary-article-subject))))
6993
6994 (defun gnus-summary-prev-article (&optional unread subject)
6995   "Select the article after the current one.
6996 If UNREAD is non-nil, only unread articles are selected."
6997   (interactive "P")
6998   (gnus-summary-next-article unread subject t))
6999
7000 (defun gnus-summary-prev-unread-article ()
7001   "Select unread article before current one."
7002   (interactive)
7003   (gnus-summary-prev-article
7004    (or (not (eq gnus-summary-goto-unread 'never))
7005        (gnus-summary-first-article-p (gnus-summary-article-number)))
7006    (and gnus-auto-select-same
7007         (gnus-summary-article-subject))))
7008
7009 (defun gnus-summary-next-page (&optional lines circular stop)
7010   "Show next page of the selected article.
7011 If at the end of the current article, select the next article.
7012 LINES says how many lines should be scrolled up.
7013
7014 If CIRCULAR is non-nil, go to the start of the article instead of
7015 selecting the next article when reaching the end of the current
7016 article.
7017
7018 If STOP is non-nil, just stop when reaching the end of the message."
7019   (interactive "P")
7020   (setq gnus-summary-buffer (current-buffer))
7021   (gnus-set-global-variables)
7022   (let ((article (gnus-summary-article-number))
7023         (article-window (get-buffer-window gnus-article-buffer t))
7024         endp)
7025     ;; If the buffer is empty, we have no article.
7026     (unless article
7027       (error "No article to select"))
7028     (gnus-configure-windows 'article)
7029     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7030         (if (and (eq gnus-summary-goto-unread 'never)
7031                  (not (gnus-summary-last-article-p article)))
7032             (gnus-summary-next-article)
7033           (gnus-summary-next-unread-article))
7034       (if (or (null gnus-current-article)
7035               (null gnus-article-current)
7036               (/= article (cdr gnus-article-current))
7037               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7038           ;; Selected subject is different from current article's.
7039           (gnus-summary-display-article article)
7040         (when article-window
7041           (gnus-eval-in-buffer-window gnus-article-buffer
7042             (setq endp (gnus-article-next-page lines)))
7043           (when endp
7044             (cond (stop
7045                    (gnus-message 3 "End of message"))
7046                   (circular
7047                    (gnus-summary-beginning-of-article))
7048                   (lines
7049                    (gnus-message 3 "End of message"))
7050                   ((null lines)
7051                    (if (and (eq gnus-summary-goto-unread 'never)
7052                             (not (gnus-summary-last-article-p article)))
7053                        (gnus-summary-next-article)
7054                      (gnus-summary-next-unread-article))))))))
7055     (gnus-summary-recenter)
7056     (gnus-summary-position-point)))
7057
7058 (defun gnus-summary-prev-page (&optional lines move)
7059   "Show previous page of selected article.
7060 Argument LINES specifies lines to be scrolled down.
7061 If MOVE, move to the previous unread article if point is at
7062 the beginning of the buffer."
7063   (interactive "P")
7064   (let ((article (gnus-summary-article-number))
7065         (article-window (get-buffer-window gnus-article-buffer t))
7066         endp)
7067     (gnus-configure-windows 'article)
7068     (if (or (null gnus-current-article)
7069             (null gnus-article-current)
7070             (/= article (cdr gnus-article-current))
7071             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7072         ;; Selected subject is different from current article's.
7073         (gnus-summary-display-article article)
7074       (gnus-summary-recenter)
7075       (when article-window
7076         (gnus-eval-in-buffer-window gnus-article-buffer
7077           (setq endp (gnus-article-prev-page lines)))
7078         (when (and move endp)
7079           (cond (lines
7080                  (gnus-message 3 "Beginning of message"))
7081                 ((null lines)
7082                  (if (and (eq gnus-summary-goto-unread 'never)
7083                           (not (gnus-summary-first-article-p article)))
7084                      (gnus-summary-prev-article)
7085                    (gnus-summary-prev-unread-article))))))))
7086   (gnus-summary-position-point))
7087
7088 (defun gnus-summary-prev-page-or-article (&optional lines)
7089   "Show previous page of selected article.
7090 Argument LINES specifies lines to be scrolled down.
7091 If at the beginning of the article, go to the next article."
7092   (interactive "P")
7093   (gnus-summary-prev-page lines t))
7094
7095 (defun gnus-summary-scroll-up (lines)
7096   "Scroll up (or down) one line current article.
7097 Argument LINES specifies lines to be scrolled up (or down if negative)."
7098   (interactive "p")
7099   (gnus-configure-windows 'article)
7100   (gnus-summary-show-thread)
7101   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7102     (gnus-eval-in-buffer-window gnus-article-buffer
7103       (cond ((> lines 0)
7104              (when (gnus-article-next-page lines)
7105                (gnus-message 3 "End of message")))
7106             ((< lines 0)
7107              (gnus-article-prev-page (- lines))))))
7108   (gnus-summary-recenter)
7109   (gnus-summary-position-point))
7110
7111 (defun gnus-summary-scroll-down (lines)
7112   "Scroll down (or up) one line current article.
7113 Argument LINES specifies lines to be scrolled down (or up if negative)."
7114   (interactive "p")
7115   (gnus-summary-scroll-up (- lines)))
7116
7117 (defun gnus-summary-next-same-subject ()
7118   "Select next article which has the same subject as current one."
7119   (interactive)
7120   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7121
7122 (defun gnus-summary-prev-same-subject ()
7123   "Select previous article which has the same subject as current one."
7124   (interactive)
7125   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7126
7127 (defun gnus-summary-next-unread-same-subject ()
7128   "Select next unread article which has the same subject as current one."
7129   (interactive)
7130   (gnus-summary-next-article t (gnus-summary-article-subject)))
7131
7132 (defun gnus-summary-prev-unread-same-subject ()
7133   "Select previous unread article which has the same subject as current one."
7134   (interactive)
7135   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7136
7137 (defun gnus-summary-first-unread-article ()
7138   "Select the first unread article.
7139 Return nil if there are no unread articles."
7140   (interactive)
7141   (prog1
7142       (when (gnus-summary-first-subject t)
7143         (gnus-summary-show-thread)
7144         (gnus-summary-first-subject t)
7145         (gnus-summary-display-article (gnus-summary-article-number)))
7146     (gnus-summary-position-point)))
7147
7148 (defun gnus-summary-first-unread-subject ()
7149   "Place the point on the subject line of the first unread article.
7150 Return nil if there are no unread articles."
7151   (interactive)
7152   (prog1
7153       (when (gnus-summary-first-subject t)
7154         (gnus-summary-show-thread)
7155         (gnus-summary-first-subject t))
7156     (gnus-summary-position-point)))
7157
7158 (defun gnus-summary-first-unseen-subject ()
7159   "Place the point on the subject line of the first unseen article.
7160 Return nil if there are no unseen articles."
7161   (interactive)
7162   (prog1
7163       (when (gnus-summary-first-subject t t t)
7164         (gnus-summary-show-thread)
7165         (gnus-summary-first-subject t t t))
7166     (gnus-summary-position-point)))
7167
7168 (defun gnus-summary-first-unseen-or-unread-subject ()
7169   "Place the point on the subject line of the first unseen article.
7170 Return nil if there are no unseen articles."
7171   (interactive)
7172   (prog1
7173       (unless (when (gnus-summary-first-subject t t t)
7174                 (gnus-summary-show-thread)
7175                 (gnus-summary-first-subject t t t))
7176         (when (gnus-summary-first-subject t)
7177           (gnus-summary-show-thread)
7178           (gnus-summary-first-subject t)))
7179     (gnus-summary-position-point)))
7180
7181 (defun gnus-summary-first-article ()
7182   "Select the first article.
7183 Return nil if there are no articles."
7184   (interactive)
7185   (prog1
7186       (when (gnus-summary-first-subject)
7187         (gnus-summary-show-thread)
7188         (gnus-summary-first-subject)
7189         (gnus-summary-display-article (gnus-summary-article-number)))
7190     (gnus-summary-position-point)))
7191
7192 (defun gnus-summary-best-unread-article (&optional arg)
7193   "Select the unread article with the highest score.
7194 If given a prefix argument, select the next unread article that has a
7195 score higher than the default score."
7196   (interactive "P")
7197   (let ((article (if arg
7198                      (gnus-summary-better-unread-subject)
7199                    (gnus-summary-best-unread-subject))))
7200     (if article
7201         (gnus-summary-goto-article article)
7202       (error "No unread articles"))))
7203
7204 (defun gnus-summary-best-unread-subject ()
7205   "Select the unread subject with the highest score."
7206   (interactive)
7207   (let ((best -1000000)
7208         (data gnus-newsgroup-data)
7209         article score)
7210     (while data
7211       (and (gnus-data-unread-p (car data))
7212            (> (setq score
7213                     (gnus-summary-article-score (gnus-data-number (car data))))
7214               best)
7215            (setq best score
7216                  article (gnus-data-number (car data))))
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-better-unread-subject ()
7224   "Select the first unread subject that has a score over the default score."
7225   (interactive)
7226   (let ((data gnus-newsgroup-data)
7227         article score)
7228     (while (and (setq article (gnus-data-number (car data)))
7229                 (or (gnus-data-read-p (car data))
7230                     (not (> (gnus-summary-article-score article)
7231                             gnus-summary-default-score))))
7232       (setq data (cdr data)))
7233     (when article
7234       (gnus-summary-goto-subject article))
7235     (gnus-summary-position-point)
7236     article))
7237
7238 (defun gnus-summary-last-subject ()
7239   "Go to the last displayed subject line in the group."
7240   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7241     (when article
7242       (gnus-summary-goto-subject article))))
7243
7244 (defun gnus-summary-goto-article (article &optional all-headers force)
7245   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7246 If ALL-HEADERS is non-nil, no header lines are hidden.
7247 If FORCE, go to the article even if it isn't displayed.  If FORCE
7248 is a number, it is the line the article is to be displayed on."
7249   (interactive
7250    (list
7251     (completing-read
7252      "Article number or Message-ID: "
7253      (mapcar (lambda (number) (list (int-to-string number)))
7254              gnus-newsgroup-limit))
7255     current-prefix-arg
7256     t))
7257   (prog1
7258       (if (and (stringp article)
7259                (string-match "@" article))
7260           (gnus-summary-refer-article article)
7261         (when (stringp article)
7262           (setq article (string-to-number article)))
7263         (if (gnus-summary-goto-subject article force)
7264             (gnus-summary-display-article article all-headers)
7265           (gnus-message 4 "Couldn't go to article %s" article) nil))
7266     (gnus-summary-position-point)))
7267
7268 (defun gnus-summary-goto-last-article ()
7269   "Go to the previously read article."
7270   (interactive)
7271   (prog1
7272       (when gnus-last-article
7273         (gnus-summary-goto-article gnus-last-article nil t))
7274     (gnus-summary-position-point)))
7275
7276 (defun gnus-summary-pop-article (number)
7277   "Pop one article off the history and go to the previous.
7278 NUMBER articles will be popped off."
7279   (interactive "p")
7280   (let (to)
7281     (setq gnus-newsgroup-history
7282           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7283     (if to
7284         (gnus-summary-goto-article (car to) nil t)
7285       (error "Article history empty")))
7286   (gnus-summary-position-point))
7287
7288 ;; Summary commands and functions for limiting the summary buffer.
7289
7290 (defun gnus-summary-limit-to-articles (n)
7291   "Limit the summary buffer to the next N articles.
7292 If not given a prefix, use the process marked articles instead."
7293   (interactive "P")
7294   (prog1
7295       (let ((articles (gnus-summary-work-articles n)))
7296         (setq gnus-newsgroup-processable nil)
7297         (gnus-summary-limit articles))
7298     (gnus-summary-position-point)))
7299
7300 (defun gnus-summary-pop-limit (&optional total)
7301   "Restore the previous limit.
7302 If given a prefix, remove all limits."
7303   (interactive "P")
7304   (when total
7305     (setq gnus-newsgroup-limits
7306           (list (mapcar (lambda (h) (mail-header-number h))
7307                         gnus-newsgroup-headers))))
7308   (unless gnus-newsgroup-limits
7309     (error "No limit to pop"))
7310   (prog1
7311       (gnus-summary-limit nil 'pop)
7312     (gnus-summary-position-point)))
7313
7314 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7315   "Limit the summary buffer to articles that have subjects that match a regexp.
7316 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7317   (interactive
7318    (list (read-string (if current-prefix-arg
7319                           "Exclude subject (regexp): "
7320                         "Limit to subject (regexp): "))
7321          nil current-prefix-arg))
7322   (unless header
7323     (setq header "subject"))
7324   (when (not (equal "" subject))
7325     (prog1
7326         (let ((articles (gnus-summary-find-matching
7327                          (or header "subject") subject 'all nil nil
7328                          not-matching)))
7329           (unless articles
7330             (error "Found no matches for \"%s\"" subject))
7331           (gnus-summary-limit articles))
7332       (gnus-summary-position-point))))
7333
7334 (defun gnus-summary-limit-to-author (from &optional not-matching)
7335   "Limit the summary buffer to articles that have authors that match a regexp.
7336 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7337   (interactive
7338    (list (read-string (if current-prefix-arg
7339                           "Exclude author (regexp): "
7340                         "Limit to author (regexp): "))
7341          current-prefix-arg))
7342   (gnus-summary-limit-to-subject from "from" not-matching))
7343
7344 (defun gnus-summary-limit-to-age (age &optional younger-p)
7345   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7346 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7347 articles that are younger than AGE days."
7348   (interactive
7349    (let ((younger current-prefix-arg)
7350          (days-got nil)
7351          days)
7352      (while (not days-got)
7353        (setq days (if younger
7354                       (read-string "Limit to articles younger than (in days, older when negative): ")
7355                     (read-string
7356                      "Limit to articles older than (in days, younger when negative): ")))
7357        (when (> (length days) 0)
7358          (setq days (read days)))
7359        (if (numberp days)
7360            (progn
7361              (setq days-got t)
7362              (if (< days 0)
7363                  (progn
7364                    (setq younger (not younger))
7365                    (setq days (* days -1)))))
7366          (message "Please enter a number.")
7367          (sleep-for 1)))
7368      (list days younger)))
7369   (prog1
7370       (let ((data gnus-newsgroup-data)
7371             (cutoff (days-to-time age))
7372             articles d date is-younger)
7373         (while (setq d (pop data))
7374           (when (and (vectorp (gnus-data-header d))
7375                      (setq date (mail-header-date (gnus-data-header d))))
7376             (setq is-younger (time-less-p
7377                               (time-since (condition-case ()
7378                                               (date-to-time date)
7379                                             (error '(0 0))))
7380                               cutoff))
7381             (when (if younger-p
7382                       is-younger
7383                     (not is-younger))
7384               (push (gnus-data-number d) articles))))
7385         (gnus-summary-limit (nreverse articles)))
7386     (gnus-summary-position-point)))
7387
7388 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7389   "Limit the summary buffer to articles that match an 'extra' header."
7390   (interactive
7391    (let ((header
7392           (intern
7393            (gnus-completing-read-with-default
7394             (symbol-name (car gnus-extra-headers))
7395             (if current-prefix-arg
7396                 "Exclude extra header:"
7397               "Limit extra header:")
7398             (mapcar (lambda (x)
7399                       (cons (symbol-name x) x))
7400                     gnus-extra-headers)
7401             nil
7402             t))))
7403      (list header
7404            (read-string (format "%s header %s (regexp): "
7405                                 (if current-prefix-arg "Exclude" "Limit to")
7406                                 header))
7407            current-prefix-arg)))
7408   (when (not (equal "" regexp))
7409     (prog1
7410         (let ((articles (gnus-summary-find-matching
7411                          (cons 'extra header) regexp 'all nil nil
7412                          not-matching)))
7413           (unless articles
7414             (error "Found no matches for \"%s\"" regexp))
7415           (gnus-summary-limit articles))
7416       (gnus-summary-position-point))))
7417
7418 (defun gnus-summary-limit-to-display-predicate ()
7419   "Limit the summary buffer to the predicated in the `display' group parameter."
7420   (interactive)
7421   (unless gnus-newsgroup-display
7422     (error "There is no `display' group parameter"))
7423   (let (articles)
7424     (dolist (number gnus-newsgroup-articles)
7425       (when (funcall gnus-newsgroup-display)
7426         (push number articles)))
7427     (gnus-summary-limit articles))
7428   (gnus-summary-position-point))
7429
7430 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7431 (make-obsolete
7432  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7433
7434 (defun gnus-summary-limit-to-unread (&optional all)
7435   "Limit the summary buffer to articles that are not marked as read.
7436 If ALL is non-nil, limit strictly to unread articles."
7437   (interactive "P")
7438   (if all
7439       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7440     (gnus-summary-limit-to-marks
7441      ;; Concat all the marks that say that an article is read and have
7442      ;; those removed.
7443      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7444            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7445            gnus-low-score-mark gnus-expirable-mark
7446            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7447            gnus-duplicate-mark gnus-souped-mark)
7448      'reverse)))
7449
7450 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7451 (make-obsolete 'gnus-summary-delete-marked-with
7452                'gnus-summary-limit-exclude-marks)
7453
7454 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7455   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7456 If REVERSE, limit the summary buffer to articles that are marked
7457 with MARKS.  MARKS can either be a string of marks or a list of marks.
7458 Returns how many articles were removed."
7459   (interactive "sMarks: ")
7460   (gnus-summary-limit-to-marks marks t))
7461
7462 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7463   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7464 If REVERSE (the prefix), limit the summary buffer to articles that are
7465 not marked with MARKS.  MARKS can either be a string of marks or a
7466 list of marks.
7467 Returns how many articles were removed."
7468   (interactive "sMarks: \nP")
7469   (prog1
7470       (let ((data gnus-newsgroup-data)
7471             (marks (if (listp marks) marks
7472                      (append marks nil))) ; Transform to list.
7473             articles)
7474         (while data
7475           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7476                   (memq (gnus-data-mark (car data)) marks))
7477             (push (gnus-data-number (car data)) articles))
7478           (setq data (cdr data)))
7479         (gnus-summary-limit articles))
7480     (gnus-summary-position-point)))
7481
7482 (defun gnus-summary-limit-to-score (score)
7483   "Limit to articles with score at or above SCORE."
7484   (interactive "NLimit to articles with score of at least: ")
7485   (let ((data gnus-newsgroup-data)
7486         articles)
7487     (while data
7488       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7489                 score)
7490         (push (gnus-data-number (car data)) articles))
7491       (setq data (cdr data)))
7492     (prog1
7493         (gnus-summary-limit articles)
7494       (gnus-summary-position-point))))
7495
7496 (defun gnus-summary-limit-to-unseen ()
7497   "Limit to unseen articles."
7498   (interactive)
7499   (prog1
7500       (gnus-summary-limit gnus-newsgroup-unseen)
7501     (gnus-summary-position-point)))
7502
7503 (defun gnus-summary-limit-include-thread (id)
7504   "Display all the hidden articles that is in the thread with ID in it.
7505 When called interactively, ID is the Message-ID of the current
7506 article."
7507   (interactive (list (mail-header-id (gnus-summary-article-header))))
7508   (let ((articles (gnus-articles-in-thread
7509                    (gnus-id-to-thread (gnus-root-id id)))))
7510     (prog1
7511         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7512       (gnus-summary-limit-include-matching-articles
7513        "subject"
7514        (regexp-quote (gnus-simplify-subject-re
7515                       (mail-header-subject (gnus-id-to-header id)))))
7516       (gnus-summary-position-point))))
7517
7518 (defun gnus-summary-limit-include-matching-articles (header regexp)
7519   "Display all the hidden articles that have HEADERs that match REGEXP."
7520   (interactive (list (read-string "Match on header: ")
7521                      (read-string "Regexp: ")))
7522   (let ((articles (gnus-find-matching-articles header regexp)))
7523     (prog1
7524         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7525       (gnus-summary-position-point))))
7526
7527 (defun gnus-summary-insert-dormant-articles ()
7528   "Insert all the dormat articles for this group into the current buffer."
7529   (interactive)
7530   (let ((gnus-verbose (max 6 gnus-verbose)))
7531     (if (not gnus-newsgroup-dormant)
7532         (gnus-message 3 "No cached articles for this group")
7533       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7534
7535 (defun gnus-summary-limit-include-dormant ()
7536   "Display all the hidden articles that are marked as dormant.
7537 Note that this command only works on a subset of the articles currently
7538 fetched for this group."
7539   (interactive)
7540   (unless gnus-newsgroup-dormant
7541     (error "There are no dormant articles in this group"))
7542   (prog1
7543       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7544     (gnus-summary-position-point)))
7545
7546 (defun gnus-summary-limit-exclude-dormant ()
7547   "Hide all dormant articles."
7548   (interactive)
7549   (prog1
7550       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7551     (gnus-summary-position-point)))
7552
7553 (defun gnus-summary-limit-exclude-childless-dormant ()
7554   "Hide all dormant articles that have no children."
7555   (interactive)
7556   (let ((data (gnus-data-list t))
7557         articles d children)
7558     ;; Find all articles that are either not dormant or have
7559     ;; children.
7560     (while (setq d (pop data))
7561       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7562                 (and (setq children
7563                            (gnus-article-children (gnus-data-number d)))
7564                      (let (found)
7565                        (while children
7566                          (when (memq (car children) articles)
7567                            (setq children nil
7568                                  found t))
7569                          (pop children))
7570                        found)))
7571         (push (gnus-data-number d) articles)))
7572     ;; Do the limiting.
7573     (prog1
7574         (gnus-summary-limit articles)
7575       (gnus-summary-position-point))))
7576
7577 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7578   "Mark all unread excluded articles as read.
7579 If ALL, mark even excluded ticked and dormants as read."
7580   (interactive "P")
7581   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7582   (let ((articles (gnus-sorted-ndifference
7583                    (sort
7584                     (mapcar (lambda (h) (mail-header-number h))
7585                             gnus-newsgroup-headers)
7586                     '<)
7587                    gnus-newsgroup-limit))
7588         article)
7589     (setq gnus-newsgroup-unreads
7590           (gnus-sorted-intersection gnus-newsgroup-unreads
7591                                     gnus-newsgroup-limit))
7592     (if all
7593         (setq gnus-newsgroup-dormant nil
7594               gnus-newsgroup-marked nil
7595               gnus-newsgroup-reads
7596               (nconc
7597                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7598                gnus-newsgroup-reads))
7599       (while (setq article (pop articles))
7600         (unless (or (memq article gnus-newsgroup-dormant)
7601                     (memq article gnus-newsgroup-marked))
7602           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7603
7604 (defun gnus-summary-limit (articles &optional pop)
7605   (if pop
7606       ;; We pop the previous limit off the stack and use that.
7607       (setq articles (car gnus-newsgroup-limits)
7608             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7609     ;; We use the new limit, so we push the old limit on the stack.
7610     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7611   ;; Set the limit.
7612   (setq gnus-newsgroup-limit articles)
7613   (let ((total (length gnus-newsgroup-data))
7614         (data (gnus-data-find-list (gnus-summary-article-number)))
7615         (gnus-summary-mark-below nil)   ; Inhibit this.
7616         found)
7617     ;; This will do all the work of generating the new summary buffer
7618     ;; according to the new limit.
7619     (gnus-summary-prepare)
7620     ;; Hide any threads, possibly.
7621     (gnus-summary-maybe-hide-threads)
7622     ;; Try to return to the article you were at, or one in the
7623     ;; neighborhood.
7624     (when data
7625       ;; We try to find some article after the current one.
7626       (while data
7627         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7628           (setq data nil
7629                 found t))
7630         (setq data (cdr data))))
7631     (unless found
7632       ;; If there is no data, that means that we were after the last
7633       ;; article.  The same goes when we can't find any articles
7634       ;; after the current one.
7635       (goto-char (point-max))
7636       (gnus-summary-find-prev))
7637     (gnus-set-mode-line 'summary)
7638     ;; We return how many articles were removed from the summary
7639     ;; buffer as a result of the new limit.
7640     (- total (length gnus-newsgroup-data))))
7641
7642 (defsubst gnus-invisible-cut-children (threads)
7643   (let ((num 0))
7644     (while threads
7645       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7646         (incf num))
7647       (pop threads))
7648     (< num 2)))
7649
7650 (defsubst gnus-cut-thread (thread)
7651   "Go forwards in the thread until we find an article that we want to display."
7652   (when (or (eq gnus-fetch-old-headers 'some)
7653             (eq gnus-fetch-old-headers 'invisible)
7654             (numberp gnus-fetch-old-headers)
7655             (eq gnus-build-sparse-threads 'some)
7656             (eq gnus-build-sparse-threads 'more))
7657     ;; Deal with old-fetched headers and sparse threads.
7658     (while (and
7659             thread
7660             (or
7661              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7662              (gnus-summary-article-ancient-p
7663               (mail-header-number (car thread))))
7664             (if (or (<= (length (cdr thread)) 1)
7665                     (eq gnus-fetch-old-headers 'invisible))
7666                 (setq gnus-newsgroup-limit
7667                       (delq (mail-header-number (car thread))
7668                             gnus-newsgroup-limit)
7669                       thread (cadr thread))
7670               (when (gnus-invisible-cut-children (cdr thread))
7671                 (let ((th (cdr thread)))
7672                   (while th
7673                     (if (memq (mail-header-number (caar th))
7674                               gnus-newsgroup-limit)
7675                         (setq thread (car th)
7676                               th nil)
7677                       (setq th (cdr th))))))))))
7678   thread)
7679
7680 (defun gnus-cut-threads (threads)
7681   "Cut off all uninteresting articles from the beginning of threads."
7682   (when (or (eq gnus-fetch-old-headers 'some)
7683             (eq gnus-fetch-old-headers 'invisible)
7684             (numberp gnus-fetch-old-headers)
7685             (eq gnus-build-sparse-threads 'some)
7686             (eq gnus-build-sparse-threads 'more))
7687     (let ((th threads))
7688       (while th
7689         (setcar th (gnus-cut-thread (car th)))
7690         (setq th (cdr th)))))
7691   ;; Remove nixed out threads.
7692   (delq nil threads))
7693
7694 (defun gnus-summary-initial-limit (&optional show-if-empty)
7695   "Figure out what the initial limit is supposed to be on group entry.
7696 This entails weeding out unwanted dormants, low-scored articles,
7697 fetch-old-headers verbiage, and so on."
7698   ;; Most groups have nothing to remove.
7699   (if (or gnus-inhibit-limiting
7700           (and (null gnus-newsgroup-dormant)
7701                (eq gnus-newsgroup-display 'gnus-not-ignore)
7702                (not (eq gnus-fetch-old-headers 'some))
7703                (not (numberp gnus-fetch-old-headers))
7704                (not (eq gnus-fetch-old-headers 'invisible))
7705                (null gnus-summary-expunge-below)
7706                (not (eq gnus-build-sparse-threads 'some))
7707                (not (eq gnus-build-sparse-threads 'more))
7708                (null gnus-thread-expunge-below)
7709                (not gnus-use-nocem)))
7710       ()                                ; Do nothing.
7711     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7712     (setq gnus-newsgroup-limit nil)
7713     (mapatoms
7714      (lambda (node)
7715        (unless (car (symbol-value node))
7716          ;; These threads have no parents -- they are roots.
7717          (let ((nodes (cdr (symbol-value node)))
7718                thread)
7719            (while nodes
7720              (if (and gnus-thread-expunge-below
7721                       (< (gnus-thread-total-score (car nodes))
7722                          gnus-thread-expunge-below))
7723                  (gnus-expunge-thread (pop nodes))
7724                (setq thread (pop nodes))
7725                (gnus-summary-limit-children thread))))))
7726      gnus-newsgroup-dependencies)
7727     ;; If this limitation resulted in an empty group, we might
7728     ;; pop the previous limit and use it instead.
7729     (when (and (not gnus-newsgroup-limit)
7730                show-if-empty)
7731       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7732     gnus-newsgroup-limit))
7733
7734 (defun gnus-summary-limit-children (thread)
7735   "Return 1 if this subthread is visible and 0 if it is not."
7736   ;; First we get the number of visible children to this thread.  This
7737   ;; is done by recursing down the thread using this function, so this
7738   ;; will really go down to a leaf article first, before slowly
7739   ;; working its way up towards the root.
7740   (when thread
7741     (let* ((max-lisp-eval-depth 5000)
7742            (children
7743            (if (cdr thread)
7744                (apply '+ (mapcar 'gnus-summary-limit-children
7745                                  (cdr thread)))
7746              0))
7747           (number (mail-header-number (car thread)))
7748           score)
7749       (if (and
7750            (not (memq number gnus-newsgroup-marked))
7751            (or
7752             ;; If this article is dormant and has absolutely no visible
7753             ;; children, then this article isn't visible.
7754             (and (memq number gnus-newsgroup-dormant)
7755                  (zerop children))
7756             ;; If this is "fetch-old-headered" and there is no
7757             ;; visible children, then we don't want this article.
7758             (and (or (eq gnus-fetch-old-headers 'some)
7759                      (numberp gnus-fetch-old-headers))
7760                  (gnus-summary-article-ancient-p number)
7761                  (zerop children))
7762             ;; If this is "fetch-old-headered" and `invisible', then
7763             ;; we don't want this article.
7764             (and (eq gnus-fetch-old-headers 'invisible)
7765                  (gnus-summary-article-ancient-p number))
7766             ;; If this is a sparsely inserted article with no children,
7767             ;; we don't want it.
7768             (and (eq gnus-build-sparse-threads 'some)
7769                  (gnus-summary-article-sparse-p number)
7770                  (zerop children))
7771             ;; If we use expunging, and this article is really
7772             ;; low-scored, then we don't want this article.
7773             (when (and gnus-summary-expunge-below
7774                        (< (setq score
7775                                 (or (cdr (assq number gnus-newsgroup-scored))
7776                                     gnus-summary-default-score))
7777                           gnus-summary-expunge-below))
7778               ;; We increase the expunge-tally here, but that has
7779               ;; nothing to do with the limits, really.
7780               (incf gnus-newsgroup-expunged-tally)
7781               ;; We also mark as read here, if that's wanted.
7782               (when (and gnus-summary-mark-below
7783                          (< score gnus-summary-mark-below))
7784                 (setq gnus-newsgroup-unreads
7785                       (delq number gnus-newsgroup-unreads))
7786                 (if gnus-newsgroup-auto-expire
7787                     (push number gnus-newsgroup-expirable)
7788                   (push (cons number gnus-low-score-mark)
7789                         gnus-newsgroup-reads)))
7790               t)
7791             ;; Do the `display' group parameter.
7792             (and gnus-newsgroup-display
7793                  (not (funcall gnus-newsgroup-display)))
7794             ;; Check NoCeM things.
7795             (if (and gnus-use-nocem
7796                      (gnus-nocem-unwanted-article-p
7797                       (mail-header-id (car thread))))
7798                 (progn
7799                   (setq gnus-newsgroup-unreads
7800                         (delq number gnus-newsgroup-unreads))
7801                   t))))
7802           ;; Nope, invisible article.
7803           0
7804         ;; Ok, this article is to be visible, so we add it to the limit
7805         ;; and return 1.
7806         (push number gnus-newsgroup-limit)
7807         1))))
7808
7809 (defun gnus-expunge-thread (thread)
7810   "Mark all articles in THREAD as read."
7811   (let* ((number (mail-header-number (car thread))))
7812     (incf gnus-newsgroup-expunged-tally)
7813     ;; We also mark as read here, if that's wanted.
7814     (setq gnus-newsgroup-unreads
7815           (delq number gnus-newsgroup-unreads))
7816     (if gnus-newsgroup-auto-expire
7817         (push number gnus-newsgroup-expirable)
7818       (push (cons number gnus-low-score-mark)
7819             gnus-newsgroup-reads)))
7820   ;; Go recursively through all subthreads.
7821   (mapcar 'gnus-expunge-thread (cdr thread)))
7822
7823 ;; Summary article oriented commands
7824
7825 (defun gnus-summary-refer-parent-article (n)
7826   "Refer parent article N times.
7827 If N is negative, go to ancestor -N instead.
7828 The difference between N and the number of articles fetched is returned."
7829   (interactive "p")
7830   (let ((skip 1)
7831         error header ref)
7832     (when (not (natnump n))
7833       (setq skip (abs n)
7834             n 1))
7835     (while (and (> n 0)
7836                 (not error))
7837       (setq header (gnus-summary-article-header))
7838       (if (and (eq (mail-header-number header)
7839                    (cdr gnus-article-current))
7840                (equal gnus-newsgroup-name
7841                       (car gnus-article-current)))
7842           ;; If we try to find the parent of the currently
7843           ;; displayed article, then we take a look at the actual
7844           ;; References header, since this is slightly more
7845           ;; reliable than the References field we got from the
7846           ;; server.
7847           (save-excursion
7848             (set-buffer gnus-original-article-buffer)
7849             (nnheader-narrow-to-headers)
7850             (unless (setq ref (message-fetch-field "references"))
7851               (setq ref (message-fetch-field "in-reply-to")))
7852             (widen))
7853         (setq ref
7854               ;; It's not the current article, so we take a bet on
7855               ;; the value we got from the server.
7856               (mail-header-references header)))
7857       (if (and ref
7858                (not (equal ref "")))
7859           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7860             (gnus-message 1 "Couldn't find parent"))
7861         (gnus-message 1 "No references in article %d"
7862                       (gnus-summary-article-number))
7863         (setq error t))
7864       (decf n))
7865     (gnus-summary-position-point)
7866     n))
7867
7868 (defun gnus-summary-refer-references ()
7869   "Fetch all articles mentioned in the References header.
7870 Return the number of articles fetched."
7871   (interactive)
7872   (let ((ref (mail-header-references (gnus-summary-article-header)))
7873         (current (gnus-summary-article-number))
7874         (n 0))
7875     (if (or (not ref)
7876             (equal ref ""))
7877         (error "No References in the current article")
7878       ;; For each Message-ID in the References header...
7879       (while (string-match "<[^>]*>" ref)
7880         (incf n)
7881         ;; ... fetch that article.
7882         (gnus-summary-refer-article
7883          (prog1 (match-string 0 ref)
7884            (setq ref (substring ref (match-end 0))))))
7885       (gnus-summary-goto-subject current)
7886       (gnus-summary-position-point)
7887       n)))
7888
7889 (defun gnus-summary-refer-thread (&optional limit)
7890   "Fetch all articles in the current thread.
7891 If LIMIT (the numerical prefix), fetch that many old headers instead
7892 of what's specified by the `gnus-refer-thread-limit' variable."
7893   (interactive "P")
7894   (let ((id (mail-header-id (gnus-summary-article-header)))
7895         (limit (if limit (prefix-numeric-value limit)
7896                  gnus-refer-thread-limit)))
7897     ;; We want to fetch LIMIT *old* headers, but we also have to
7898     ;; re-fetch all the headers in the current buffer, because many of
7899     ;; them may be undisplayed.  So we adjust LIMIT.
7900     (when (numberp limit)
7901       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7902     (unless (eq gnus-fetch-old-headers 'invisible)
7903       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7904       ;; Retrieve the headers and read them in.
7905       (if (eq (gnus-retrieve-headers
7906                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7907               'nov)
7908           (gnus-build-all-threads)
7909         (error "Can't fetch thread from backends that don't support NOV"))
7910       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7911     (gnus-summary-limit-include-thread id)))
7912
7913 (defun gnus-summary-refer-article (message-id)
7914   "Fetch an article specified by MESSAGE-ID."
7915   (interactive "sMessage-ID: ")
7916   (when (and (stringp message-id)
7917              (not (zerop (length message-id))))
7918     ;; Construct the correct Message-ID if necessary.
7919     ;; Suggested by tale@pawl.rpi.edu.
7920     (unless (string-match "^<" message-id)
7921       (setq message-id (concat "<" message-id)))
7922     (unless (string-match ">$" message-id)
7923       (setq message-id (concat message-id ">")))
7924     (let* ((header (gnus-id-to-header message-id))
7925            (sparse (and header
7926                         (gnus-summary-article-sparse-p
7927                          (mail-header-number header))
7928                         (memq (mail-header-number header)
7929                               gnus-newsgroup-limit)))
7930            number)
7931       (cond
7932        ;; If the article is present in the buffer we just go to it.
7933        ((and header
7934              (or (not (gnus-summary-article-sparse-p
7935                        (mail-header-number header)))
7936                  sparse))
7937         (prog1
7938             (gnus-summary-goto-article
7939              (mail-header-number header) nil t)
7940           (when sparse
7941             (gnus-summary-update-article (mail-header-number header)))))
7942        (t
7943         ;; We fetch the article.
7944         (catch 'found
7945           (dolist (gnus-override-method (gnus-refer-article-methods))
7946             (gnus-check-server gnus-override-method)
7947             ;; Fetch the header, and display the article.
7948             (when (setq number (gnus-summary-insert-subject message-id))
7949               (gnus-summary-select-article nil nil nil number)
7950               (throw 'found t)))
7951           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7952
7953 (defun gnus-refer-article-methods ()
7954   "Return a list of referable methods."
7955   (cond
7956    ;; No method, so we default to current and native.
7957    ((null gnus-refer-article-method)
7958     (list gnus-current-select-method gnus-select-method))
7959    ;; Current.
7960    ((eq 'current gnus-refer-article-method)
7961     (list gnus-current-select-method))
7962    ;; List of select methods.
7963    ((not (and (symbolp (car gnus-refer-article-method))
7964               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7965     (let (out)
7966       (dolist (method gnus-refer-article-method)
7967         (push (if (eq 'current method)
7968                   gnus-current-select-method
7969                 method)
7970               out))
7971       (nreverse out)))
7972    ;; One single select method.
7973    (t
7974     (list gnus-refer-article-method))))
7975
7976 (defun gnus-summary-edit-parameters ()
7977   "Edit the group parameters of the current group."
7978   (interactive)
7979   (gnus-group-edit-group gnus-newsgroup-name 'params))
7980
7981 (defun gnus-summary-customize-parameters ()
7982   "Customize the group parameters of the current group."
7983   (interactive)
7984   (gnus-group-customize gnus-newsgroup-name))
7985
7986 (defun gnus-summary-enter-digest-group (&optional force)
7987   "Enter an nndoc group based on the current article.
7988 If FORCE, force a digest interpretation.  If not, try
7989 to guess what the document format is."
7990   (interactive "P")
7991   (let ((conf gnus-current-window-configuration))
7992     (save-excursion
7993       (gnus-summary-select-article))
7994     (setq gnus-current-window-configuration conf)
7995     (let* ((name (format "%s-%d"
7996                          (gnus-group-prefixed-name
7997                           gnus-newsgroup-name (list 'nndoc ""))
7998                          (save-excursion
7999                            (set-buffer gnus-summary-buffer)
8000                            gnus-current-article)))
8001            (ogroup gnus-newsgroup-name)
8002            (params (append (gnus-info-params (gnus-get-info ogroup))
8003                            (list (cons 'to-group ogroup))
8004                            (list (cons 'save-article-group ogroup))))
8005            (case-fold-search t)
8006            (buf (current-buffer))
8007            dig to-address)
8008       (save-excursion
8009         (set-buffer gnus-original-article-buffer)
8010         ;; Have the digest group inherit the main mail address of
8011         ;; the parent article.
8012         (when (setq to-address (or (gnus-fetch-field "reply-to")
8013                                    (gnus-fetch-field "from")))
8014           (setq params (append
8015                         (list (cons 'to-address
8016                                     (funcall gnus-decode-encoded-word-function
8017                                              to-address))))))
8018         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8019         (insert-buffer-substring gnus-original-article-buffer)
8020         ;; Remove lines that may lead nndoc to misinterpret the
8021         ;; document type.
8022         (narrow-to-region
8023          (goto-char (point-min))
8024          (or (search-forward "\n\n" nil t) (point)))
8025         (goto-char (point-min))
8026         (delete-matching-lines "^Path:\\|^From ")
8027         (widen))
8028       (unwind-protect
8029           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8030                     (gnus-newsgroup-ephemeral-ignored-charsets
8031                      gnus-newsgroup-ignored-charsets))
8032                 (gnus-group-read-ephemeral-group
8033                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8034                               (nndoc-article-type
8035                                ,(if force 'mbox 'guess)))
8036                  t nil nil nil
8037                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8038                                                         "ADAPT")))))
8039               ;; Make all postings to this group go to the parent group.
8040               (nconc (gnus-info-params (gnus-get-info name))
8041                      params)
8042             ;; Couldn't select this doc group.
8043             (switch-to-buffer buf)
8044             (gnus-set-global-variables)
8045             (gnus-configure-windows 'summary)
8046             (gnus-message 3 "Article couldn't be entered?"))
8047         (kill-buffer dig)))))
8048
8049 (defun gnus-summary-read-document (n)
8050   "Open a new group based on the current article(s).
8051 This will allow you to read digests and other similar
8052 documents as newsgroups.
8053 Obeys the standard process/prefix convention."
8054   (interactive "P")
8055   (let* ((articles (gnus-summary-work-articles n))
8056          (ogroup gnus-newsgroup-name)
8057          (params (append (gnus-info-params (gnus-get-info ogroup))
8058                          (list (cons 'to-group ogroup))))
8059          article group egroup groups vgroup)
8060     (while (setq article (pop articles))
8061       (setq group (format "%s-%d" gnus-newsgroup-name article))
8062       (gnus-summary-remove-process-mark article)
8063       (when (gnus-summary-display-article article)
8064         (save-excursion
8065           (with-temp-buffer
8066             (insert-buffer-substring gnus-original-article-buffer)
8067             ;; Remove some headers that may lead nndoc to make
8068             ;; the wrong guess.
8069             (message-narrow-to-head)
8070             (goto-char (point-min))
8071             (delete-matching-lines "^\\(Path\\):\\|^From ")
8072             (widen)
8073             (if (setq egroup
8074                       (gnus-group-read-ephemeral-group
8075                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8076                                      (nndoc-article-type guess))
8077                        t nil t))
8078                 (progn
8079             ;; Make all postings to this group go to the parent group.
8080                   (nconc (gnus-info-params (gnus-get-info egroup))
8081                          params)
8082                   (push egroup groups))
8083               ;; Couldn't select this doc group.
8084               (gnus-error 3 "Article couldn't be entered"))))))
8085     ;; Now we have selected all the documents.
8086     (cond
8087      ((not groups)
8088       (error "None of the articles could be interpreted as documents"))
8089      ((gnus-group-read-ephemeral-group
8090        (setq vgroup (format
8091                      "nnvirtual:%s-%s" gnus-newsgroup-name
8092                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8093        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8094        t
8095        (cons (current-buffer) 'summary)))
8096      (t
8097       (error "Couldn't select virtual nndoc group")))))
8098
8099 (defun gnus-summary-isearch-article (&optional regexp-p)
8100   "Do incremental search forward on the current article.
8101 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8102   (interactive "P")
8103   (gnus-summary-select-article)
8104   (gnus-configure-windows 'article)
8105   (gnus-eval-in-buffer-window gnus-article-buffer
8106     (save-restriction
8107       (widen)
8108       (isearch-forward regexp-p))))
8109
8110 (defun gnus-summary-search-article-forward (regexp &optional backward)
8111   "Search for an article containing REGEXP forward.
8112 If BACKWARD, search backward instead."
8113   (interactive
8114    (list (read-string
8115           (format "Search article %s (regexp%s): "
8116                   (if current-prefix-arg "backward" "forward")
8117                   (if gnus-last-search-regexp
8118                       (concat ", default " gnus-last-search-regexp)
8119                     "")))
8120          current-prefix-arg))
8121   (if (string-equal regexp "")
8122       (setq regexp (or gnus-last-search-regexp ""))
8123     (setq gnus-last-search-regexp regexp)
8124     (setq gnus-article-before-search gnus-current-article))
8125   ;; Intentionally set gnus-last-article.
8126   (setq gnus-last-article gnus-article-before-search)
8127   (let ((gnus-last-article gnus-last-article))
8128     (if (gnus-summary-search-article regexp backward)
8129         (gnus-summary-show-thread)
8130       (error "Search failed: \"%s\"" regexp))))
8131
8132 (defun gnus-summary-search-article-backward (regexp)
8133   "Search for an article containing REGEXP backward."
8134   (interactive
8135    (list (read-string
8136           (format "Search article backward (regexp%s): "
8137                   (if gnus-last-search-regexp
8138                       (concat ", default " gnus-last-search-regexp)
8139                     "")))))
8140   (gnus-summary-search-article-forward regexp 'backward))
8141
8142 (defun gnus-summary-search-article (regexp &optional backward)
8143   "Search for an article containing REGEXP.
8144 Optional argument BACKWARD means do search for backward.
8145 `gnus-select-article-hook' is not called during the search."
8146   ;; We have to require this here to make sure that the following
8147   ;; dynamic binding isn't shadowed by autoloading.
8148   (require 'gnus-async)
8149   (require 'gnus-art)
8150   (let ((gnus-select-article-hook nil)  ;Disable hook.
8151         (gnus-article-prepare-hook nil)
8152         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8153         (gnus-use-article-prefetch nil)
8154         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8155         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8156         (sum (current-buffer))
8157         (gnus-display-mime-function nil)
8158         (found nil)
8159         point)
8160     (gnus-save-hidden-threads
8161       (gnus-summary-select-article)
8162       (set-buffer gnus-article-buffer)
8163       (goto-char (window-point (get-buffer-window (current-buffer))))
8164       (when backward
8165         (forward-line -1))
8166       (while (not found)
8167         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8168         (if (if backward
8169                 (re-search-backward regexp nil t)
8170               (re-search-forward regexp nil t))
8171             ;; We found the regexp.
8172             (progn
8173               (setq found 'found)
8174               (beginning-of-line)
8175               (set-window-start
8176                (get-buffer-window (current-buffer))
8177                (point))
8178               (forward-line 1)
8179               (set-window-point
8180                (get-buffer-window (current-buffer))
8181                (point))
8182               (set-buffer sum)
8183               (setq point (point)))
8184           ;; We didn't find it, so we go to the next article.
8185           (set-buffer sum)
8186           (setq found 'not)
8187           (while (eq found 'not)
8188             (if (not (if backward (gnus-summary-find-prev)
8189                        (gnus-summary-find-next)))
8190                 ;; No more articles.
8191                 (setq found t)
8192               ;; Select the next article and adjust point.
8193               (unless (gnus-summary-article-sparse-p
8194                        (gnus-summary-article-number))
8195                 (setq found nil)
8196                 (gnus-summary-select-article)
8197                 (set-buffer gnus-article-buffer)
8198                 (widen)
8199                 (goto-char (if backward (point-max) (point-min))))))))
8200       (gnus-message 7 ""))
8201     ;; Return whether we found the regexp.
8202     (when (eq found 'found)
8203       (goto-char point)
8204       (gnus-summary-show-thread)
8205       (gnus-summary-goto-subject gnus-current-article)
8206       (gnus-summary-position-point)
8207       t)))
8208
8209 (defun gnus-find-matching-articles (header regexp)
8210   "Return a list of all articles that match REGEXP on HEADER.
8211 This search includes all articles in the current group that Gnus has
8212 fetched headers for, whether they are displayed or not."
8213   (let ((articles nil)
8214         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8215         (case-fold-search t))
8216     (dolist (header gnus-newsgroup-headers)
8217       (when (string-match regexp (funcall func header))
8218         (push (mail-header-number header) articles)))
8219     (nreverse articles)))
8220
8221 (defun gnus-summary-find-matching (header regexp &optional backward unread
8222                                           not-case-fold not-matching)
8223   "Return a list of all articles that match REGEXP on HEADER.
8224 The search stars on the current article and goes forwards unless
8225 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8226 If UNREAD is non-nil, only unread articles will
8227 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8228 in the comparisons. If NOT-MATCHING, return a list of all articles that
8229 not match REGEXP on HEADER."
8230   (let ((case-fold-search (not not-case-fold))
8231         articles d func)
8232     (if (consp header)
8233         (if (eq (car header) 'extra)
8234             (setq func
8235                   `(lambda (h)
8236                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8237                          "")))
8238           (error "%s is an invalid header" header))
8239       (unless (fboundp (intern (concat "mail-header-" header)))
8240         (error "%s is not a valid header" header))
8241       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8242     (dolist (d (if (eq backward 'all)
8243                    gnus-newsgroup-data
8244                  (gnus-data-find-list
8245                   (gnus-summary-article-number)
8246                   (gnus-data-list backward))))
8247       (when (and (or (not unread)       ; We want all articles...
8248                      (gnus-data-unread-p d)) ; Or just unreads.
8249                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8250                  (if not-matching
8251                      (not (string-match
8252                            regexp
8253                            (funcall func (gnus-data-header d))))
8254                    (string-match regexp
8255                                  (funcall func (gnus-data-header d)))))
8256         (push (gnus-data-number d) articles))) ; Success!
8257     (nreverse articles)))
8258
8259 (defun gnus-summary-execute-command (header regexp command &optional backward)
8260   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8261 If HEADER is an empty string (or nil), the match is done on the entire
8262 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8263   (interactive
8264    (list (let ((completion-ignore-case t))
8265            (completing-read
8266             "Header name: "
8267             (mapcar (lambda (header) (list (format "%s" header)))
8268                     (append
8269                      '("Number" "Subject" "From" "Lines" "Date"
8270                        "Message-ID" "Xref" "References" "Body")
8271                      gnus-extra-headers))
8272             nil 'require-match))
8273          (read-string "Regexp: ")
8274          (read-key-sequence "Command: ")
8275          current-prefix-arg))
8276   (when (equal header "Body")
8277     (setq header ""))
8278   ;; Hidden thread subtrees must be searched as well.
8279   (gnus-summary-show-all-threads)
8280   ;; We don't want to change current point nor window configuration.
8281   (save-excursion
8282     (save-window-excursion
8283       (let (gnus-visual
8284             gnus-treat-strip-trailing-blank-lines
8285             gnus-treat-strip-leading-blank-lines
8286             gnus-treat-strip-multiple-blank-lines
8287             gnus-treat-hide-boring-headers
8288             gnus-treat-fold-newsgroups
8289             gnus-article-prepare-hook)
8290         (gnus-message 6 "Executing %s..." (key-description command))
8291         ;; We'd like to execute COMMAND interactively so as to give arguments.
8292         (gnus-execute header regexp
8293                       `(call-interactively ',(key-binding command))
8294                       backward)
8295         (gnus-message 6 "Executing %s...done" (key-description command))))))
8296
8297 (defun gnus-summary-beginning-of-article ()
8298   "Scroll the article back to the beginning."
8299   (interactive)
8300   (gnus-summary-select-article)
8301   (gnus-configure-windows 'article)
8302   (gnus-eval-in-buffer-window gnus-article-buffer
8303     (widen)
8304     (goto-char (point-min))
8305     (when gnus-page-broken
8306       (gnus-narrow-to-page))))
8307
8308 (defun gnus-summary-end-of-article ()
8309   "Scroll to the end of the article."
8310   (interactive)
8311   (gnus-summary-select-article)
8312   (gnus-configure-windows 'article)
8313   (gnus-eval-in-buffer-window gnus-article-buffer
8314     (widen)
8315     (goto-char (point-max))
8316     (recenter -3)
8317     (when gnus-page-broken
8318       (gnus-narrow-to-page))))
8319
8320 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8321   "Truncate to LEN and quote all \"(\"'s in STRING."
8322   (gnus-replace-in-string (if (and len (> (length string) len))
8323                               (substring string 0 len)
8324                             string)
8325                           "[()]" "\\\\\\&"))
8326
8327 (defun gnus-summary-print-article (&optional filename n)
8328   "Generate and print a PostScript image of the N next (mail) articles.
8329
8330 If N is negative, print the N previous articles.  If N is nil and articles
8331 have been marked with the process mark, print these instead.
8332
8333 If the optional first argument FILENAME is nil, send the image to the
8334 printer.  If FILENAME is a string, save the PostScript image in a file with
8335 that name.  If FILENAME is a number, prompt the user for the name of the file
8336 to save in."
8337   (interactive (list (ps-print-preprint current-prefix-arg)))
8338   (dolist (article (gnus-summary-work-articles n))
8339     (gnus-summary-select-article nil nil 'pseudo article)
8340     (gnus-eval-in-buffer-window gnus-article-buffer
8341       (gnus-print-buffer))
8342     (gnus-summary-remove-process-mark article))
8343   (ps-despool filename))
8344
8345 (defun gnus-print-buffer ()
8346   (let ((buffer (generate-new-buffer " *print*")))
8347     (unwind-protect
8348         (progn
8349           (copy-to-buffer buffer (point-min) (point-max))
8350           (set-buffer buffer)
8351           (gnus-article-delete-invisible-text)
8352           (gnus-remove-text-with-property 'gnus-decoration)
8353           (when (gnus-visual-p 'article-highlight 'highlight)
8354             ;; Copy-to-buffer doesn't copy overlay.  So redo
8355             ;; highlight.
8356             (let ((gnus-article-buffer buffer))
8357               (gnus-article-highlight-citation t)
8358               (gnus-article-highlight-signature)))
8359           (let ((ps-left-header
8360                  (list
8361                   (concat "("
8362                           (gnus-summary-print-truncate-and-quote
8363                            (mail-header-subject gnus-current-headers)
8364                            66) ")")
8365                   (concat "("
8366                           (gnus-summary-print-truncate-and-quote
8367                            (mail-header-from gnus-current-headers)
8368                            45) ")")))
8369                 (ps-right-header
8370                  (list
8371                   "/pagenumberstring load"
8372                   (concat "("
8373                           (mail-header-date gnus-current-headers) ")"))))
8374             (gnus-run-hooks 'gnus-ps-print-hook)
8375             (save-excursion
8376               (if window-system
8377                   (ps-spool-buffer-with-faces)
8378                 (ps-spool-buffer)))))
8379       (kill-buffer buffer))))
8380
8381 (defun gnus-summary-show-article (&optional arg)
8382   "Force redisplaying of the current article.
8383 If ARG (the prefix) is a number, show the article with the charset
8384 defined in `gnus-summary-show-article-charset-alist', or the charset
8385 input.
8386 If ARG (the prefix) is non-nil and not a number, show the raw article
8387 without any article massaging functions being run.  Normally, the key strokes
8388 are `C-u g'."
8389   (interactive "P")
8390   (cond
8391    ((numberp arg)
8392     (gnus-summary-show-article t)
8393     (let ((gnus-newsgroup-charset
8394            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8395                (mm-read-coding-system
8396                 "View as charset: " ;; actually it is coding system.
8397                 (save-excursion
8398                   (set-buffer gnus-article-buffer)
8399                   (mm-detect-coding-region (point) (point-max))))))
8400           (gnus-newsgroup-ignored-charsets 'gnus-all))
8401       (gnus-summary-select-article nil 'force)
8402       (let ((deps gnus-newsgroup-dependencies)
8403             head header lines)
8404         (save-excursion
8405           (set-buffer gnus-original-article-buffer)
8406           (save-restriction
8407             (message-narrow-to-head)
8408             (setq head (buffer-string))
8409             (goto-char (point-min))
8410             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8411               (goto-char (point-max))
8412               (widen)
8413               (setq lines (1- (count-lines (point) (point-max))))))
8414           (with-temp-buffer
8415             (insert (format "211 %d Article retrieved.\n"
8416                             (cdr gnus-article-current)))
8417             (insert head)
8418             (if lines (insert (format "Lines: %d\n" lines)))
8419             (insert ".\n")
8420             (let ((nntp-server-buffer (current-buffer)))
8421               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8422         (gnus-data-set-header
8423          (gnus-data-find (cdr gnus-article-current))
8424          header)
8425         (gnus-summary-update-article-line
8426          (cdr gnus-article-current) header)
8427         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8428           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8429    ((not arg)
8430     ;; Select the article the normal way.
8431     (gnus-summary-select-article nil 'force))
8432    (t
8433     ;; We have to require this here to make sure that the following
8434     ;; dynamic binding isn't shadowed by autoloading.
8435     (require 'gnus-async)
8436     (require 'gnus-art)
8437     ;; Bind the article treatment functions to nil.
8438     (let ((gnus-have-all-headers t)
8439           gnus-article-prepare-hook
8440           gnus-article-decode-hook
8441           gnus-display-mime-function
8442           gnus-break-pages)
8443       ;; Destroy any MIME parts.
8444       (when (gnus-buffer-live-p gnus-article-buffer)
8445         (save-excursion
8446           (set-buffer gnus-article-buffer)
8447           (mm-destroy-parts gnus-article-mime-handles)
8448           ;; Set it to nil for safety reason.
8449           (setq gnus-article-mime-handle-alist nil)
8450           (setq gnus-article-mime-handles nil)))
8451       (gnus-summary-select-article nil 'force))))
8452   (gnus-summary-goto-subject gnus-current-article)
8453   (gnus-summary-position-point))
8454
8455 (defun gnus-summary-show-raw-article ()
8456   "Show the raw article without any article massaging functions being run."
8457   (interactive)
8458   (gnus-summary-show-article t))
8459
8460 (defun gnus-summary-verbose-headers (&optional arg)
8461   "Toggle permanent full header display.
8462 If ARG is a positive number, turn header display on.
8463 If ARG is a negative number, turn header display off."
8464   (interactive "P")
8465   (setq gnus-show-all-headers
8466         (cond ((or (not (numberp arg))
8467                    (zerop arg))
8468                (not gnus-show-all-headers))
8469               ((natnump arg)
8470                t)))
8471   (gnus-summary-show-article))
8472
8473 (defun gnus-summary-toggle-header (&optional arg)
8474   "Show the headers if they are hidden, or hide them if they are shown.
8475 If ARG is a positive number, show the entire header.
8476 If ARG is a negative number, hide the unwanted header lines."
8477   (interactive "P")
8478   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8479                      (get-buffer-window gnus-article-buffer t))))
8480     (with-current-buffer gnus-article-buffer
8481       (widen)
8482       (article-narrow-to-head)
8483       (let* ((buffer-read-only nil)
8484              (inhibit-point-motion-hooks t)
8485              (hidden (if (numberp arg)
8486                          (>= arg 0)
8487                        (gnus-article-hidden-text-p 'headers)))
8488              s e)
8489         (delete-region (point-min) (point-max))
8490         (with-current-buffer gnus-original-article-buffer
8491           (goto-char (setq s (point-min)))
8492           (setq e (if (search-forward "\n\n" nil t)
8493                       (1- (point))
8494                     (point-max))))
8495         (insert-buffer-substring gnus-original-article-buffer s e)
8496         (article-decode-encoded-words)
8497         (if hidden
8498             (let ((gnus-treat-hide-headers nil)
8499                   (gnus-treat-hide-boring-headers nil))
8500               (gnus-delete-wash-type 'headers)
8501               (gnus-treat-article 'head))
8502           (gnus-treat-article 'head))
8503         (widen)
8504         (if window
8505             (set-window-start window (goto-char (point-min))))
8506         (setq gnus-page-broken
8507               (when gnus-break-pages
8508                 (gnus-narrow-to-page)
8509                 t))
8510         (gnus-set-mode-line 'article)))))
8511
8512 (defun gnus-summary-show-all-headers ()
8513   "Make all header lines visible."
8514   (interactive)
8515   (gnus-summary-toggle-header 1))
8516
8517 (defun gnus-summary-caesar-message (&optional arg)
8518   "Caesar rotate the current article by 13.
8519 The numerical prefix specifies how many places to rotate each letter
8520 forward."
8521   (interactive "P")
8522   (gnus-summary-select-article)
8523   (let ((mail-header-separator ""))
8524     (gnus-eval-in-buffer-window gnus-article-buffer
8525       (save-restriction
8526         (widen)
8527         (let ((start (window-start))
8528               buffer-read-only)
8529           (message-caesar-buffer-body arg)
8530           (set-window-start (get-buffer-window (current-buffer)) start))))))
8531
8532 (autoload 'unmorse-region "morse"
8533   "Convert morse coded text in region to ordinary ASCII text."
8534   t)
8535
8536 (defun gnus-summary-morse-message (&optional arg)
8537   "Morse decode the current article."
8538   (interactive "P")
8539   (gnus-summary-select-article)
8540   (let ((mail-header-separator ""))
8541     (gnus-eval-in-buffer-window gnus-article-buffer
8542       (save-excursion
8543         (save-restriction
8544           (widen)
8545           (let ((pos (window-start))
8546                 buffer-read-only)
8547             (goto-char (point-min))
8548             (when (message-goto-body)
8549               (gnus-narrow-to-body))
8550             (goto-char (point-min))
8551             (while (re-search-forward "·" (point-max) t)
8552               (replace-match "."))
8553             (unmorse-region (point-min) (point-max))
8554             (widen)
8555             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8556
8557 (defun gnus-summary-stop-page-breaking ()
8558   "Stop page breaking in the current article."
8559   (interactive)
8560   (gnus-summary-select-article)
8561   (gnus-eval-in-buffer-window gnus-article-buffer
8562     (widen)
8563     (when (gnus-visual-p 'page-marker)
8564       (let ((buffer-read-only nil))
8565         (gnus-remove-text-with-property 'gnus-prev)
8566         (gnus-remove-text-with-property 'gnus-next))
8567       (setq gnus-page-broken nil))))
8568
8569 (defun gnus-summary-move-article (&optional n to-newsgroup
8570                                             select-method action)
8571   "Move the current article to a different newsgroup.
8572 If N is a positive number, move the N next articles.
8573 If N is a negative number, move the N previous articles.
8574 If N is nil and any articles have been marked with the process mark,
8575 move those articles instead.
8576 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8577 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8578 re-spool using this method.
8579
8580 When called interactively with TO-NEWSGROUP being nil, the value of
8581 the variable `gnus-move-split-methods' is used for finding a default
8582 for the target newsgroup.
8583
8584 For this function to work, both the current newsgroup and the
8585 newsgroup that you want to move to have to support the `request-move'
8586 and `request-accept' functions.
8587
8588 ACTION can be either `move' (the default), `crosspost' or `copy'."
8589   (interactive "P")
8590   (unless action
8591     (setq action 'move))
8592   ;; Check whether the source group supports the required functions.
8593   (cond ((and (eq action 'move)
8594               (not (gnus-check-backend-function
8595                     'request-move-article gnus-newsgroup-name)))
8596          (error "The current group does not support article moving"))
8597         ((and (eq action 'crosspost)
8598               (not (gnus-check-backend-function
8599                     'request-replace-article gnus-newsgroup-name)))
8600          (error "The current group does not support article editing")))
8601   (let ((articles (gnus-summary-work-articles n))
8602         (prefix (if (gnus-check-backend-function
8603                      'request-move-article gnus-newsgroup-name)
8604                     (gnus-group-real-prefix gnus-newsgroup-name)
8605                   ""))
8606         (names '((move "Move" "Moving")
8607                  (copy "Copy" "Copying")
8608                  (crosspost "Crosspost" "Crossposting")))
8609         (copy-buf (save-excursion
8610                     (nnheader-set-temp-buffer " *copy article*")))
8611         art-group to-method new-xref article to-groups)
8612     (unless (assq action names)
8613       (error "Unknown action %s" action))
8614     ;; Read the newsgroup name.
8615     (when (and (not to-newsgroup)
8616                (not select-method))
8617       (if (and gnus-move-split-methods
8618                (not
8619                 (and (memq gnus-current-article articles)
8620                      (gnus-buffer-live-p gnus-original-article-buffer))))
8621           ;; When `gnus-move-split-methods' is non-nil, we have to
8622           ;; select an article to give `gnus-read-move-group-name' an
8623           ;; opportunity to suggest an appropriate default.  However,
8624           ;; we needn't render or mark the article.
8625           (let ((gnus-display-mime-function nil)
8626                 (gnus-article-prepare-hook nil)
8627                 (gnus-mark-article-hook nil))
8628             (gnus-summary-select-article nil nil nil (car articles))))
8629       (setq to-newsgroup
8630             (gnus-read-move-group-name
8631              (cadr (assq action names))
8632              (symbol-value (intern (format "gnus-current-%s-group" action)))
8633              articles prefix))
8634       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8635     (setq to-method (or select-method
8636                         (gnus-server-to-method
8637                          (gnus-group-method to-newsgroup))))
8638     ;; Check the method we are to move this article to...
8639     (unless (gnus-check-backend-function
8640              'request-accept-article (car to-method))
8641       (error "%s does not support article copying" (car to-method)))
8642     (unless (gnus-check-server to-method)
8643       (error "Can't open server %s" (car to-method)))
8644     (gnus-message 6 "%s to %s: %s..."
8645                   (caddr (assq action names))
8646                   (or (car select-method) to-newsgroup) articles)
8647     (while articles
8648       (setq article (pop articles))
8649       (setq
8650        art-group
8651        (cond
8652         ;; Move the article.
8653         ((eq action 'move)
8654          ;; Remove this article from future suppression.
8655          (gnus-dup-unsuppress-article article)
8656          (gnus-request-move-article
8657           article                       ; Article to move
8658           gnus-newsgroup-name           ; From newsgroup
8659           (nth 1 (gnus-find-method-for-group
8660                   gnus-newsgroup-name)) ; Server
8661           (list 'gnus-request-accept-article
8662                 to-newsgroup (list 'quote select-method)
8663                 (not articles) t)       ; Accept form
8664           (not articles)))              ; Only save nov last time
8665         ;; Copy the article.
8666         ((eq action 'copy)
8667          (save-excursion
8668            (set-buffer copy-buf)
8669            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8670              (gnus-request-accept-article
8671               to-newsgroup select-method (not articles) t))))
8672         ;; Crosspost the article.
8673         ((eq action 'crosspost)
8674          (let ((xref (message-tokenize-header
8675                       (mail-header-xref (gnus-summary-article-header article))
8676                       " ")))
8677            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8678                                   ":" (number-to-string article)))
8679            (unless xref
8680              (setq xref (list (system-name))))
8681            (setq new-xref
8682                  (concat
8683                   (mapconcat 'identity
8684                              (delete "Xref:" (delete new-xref xref))
8685                              " ")
8686                   " " new-xref))
8687            (save-excursion
8688              (set-buffer copy-buf)
8689              ;; First put the article in the destination group.
8690              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8691              (when (consp (setq art-group
8692                                 (gnus-request-accept-article
8693                                  to-newsgroup select-method (not articles))))
8694                (setq new-xref (concat new-xref " " (car art-group)
8695                                       ":"
8696                                       (number-to-string (cdr art-group))))
8697                ;; Now we have the new Xrefs header, so we insert
8698                ;; it and replace the new article.
8699                (nnheader-replace-header "Xref" new-xref)
8700                (gnus-request-replace-article
8701                 (cdr art-group) to-newsgroup (current-buffer))
8702                art-group))))))
8703       (cond
8704        ((not art-group)
8705         (gnus-message 1 "Couldn't %s article %s: %s"
8706                       (cadr (assq action names)) article
8707                       (nnheader-get-report (car to-method))))
8708        ((eq art-group 'junk)
8709         (when (eq action 'move)
8710           (gnus-summary-mark-article article gnus-canceled-mark)
8711           (gnus-message 4 "Deleted article %s" article)))
8712        (t
8713         (let* ((pto-group (gnus-group-prefixed-name
8714                            (car art-group) to-method))
8715                (entry
8716                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8717                (info (nth 2 entry))
8718                (to-group (gnus-info-group info))
8719                to-marks)
8720           ;; Update the group that has been moved to.
8721           (when (and info
8722                      (memq action '(move copy)))
8723             (unless (member to-group to-groups)
8724               (push to-group to-groups))
8725
8726             (unless (memq article gnus-newsgroup-unreads)
8727               (push 'read to-marks)
8728               (gnus-info-set-read
8729                info (gnus-add-to-range (gnus-info-read info)
8730                                        (list (cdr art-group)))))
8731
8732             ;; See whether the article is to be put in the cache.
8733             (let ((marks gnus-article-mark-lists)
8734                   (to-article (cdr art-group)))
8735
8736               ;; Enter the article into the cache in the new group,
8737               ;; if that is required.
8738               (when gnus-use-cache
8739                 (gnus-cache-possibly-enter-article
8740                  to-group to-article
8741                  (memq article gnus-newsgroup-marked)
8742                  (memq article gnus-newsgroup-dormant)
8743                  (memq article gnus-newsgroup-unreads)))
8744
8745               (when gnus-preserve-marks
8746                 ;; Copy any marks over to the new group.
8747                 (when (and (equal to-group gnus-newsgroup-name)
8748                            (not (memq article gnus-newsgroup-unreads)))
8749                   ;; Mark this article as read in this group.
8750                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8751                   (setcdr (gnus-active to-group) to-article)
8752                   (setcdr gnus-newsgroup-active to-article))
8753
8754                 (while marks
8755                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8756                     (when (memq article (symbol-value
8757                                          (intern (format "gnus-newsgroup-%s"
8758                                                          (caar marks)))))
8759                       (push (cdar marks) to-marks)
8760                       ;; If the other group is the same as this group,
8761                       ;; then we have to add the mark to the list.
8762                       (when (equal to-group gnus-newsgroup-name)
8763                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8764                              (cons to-article
8765                                    (symbol-value
8766                                     (intern (format "gnus-newsgroup-%s"
8767                                                     (caar marks)))))))
8768                       ;; Copy the marks to other group.
8769                       (gnus-add-marked-articles
8770                        to-group (cdar marks) (list to-article) info)))
8771                   (setq marks (cdr marks)))
8772
8773                 (gnus-request-set-mark
8774                  to-group (list (list (list to-article) 'add to-marks))))
8775
8776               (gnus-dribble-enter
8777                (concat "(gnus-group-set-info '"
8778                        (gnus-prin1-to-string (gnus-get-info to-group))
8779                        ")"))))
8780
8781           ;; Update the Xref header in this article to point to
8782           ;; the new crossposted article we have just created.
8783           (when (eq action 'crosspost)
8784             (save-excursion
8785               (set-buffer copy-buf)
8786               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8787               (nnheader-replace-header "Xref" new-xref)
8788               (gnus-request-replace-article
8789                article gnus-newsgroup-name (current-buffer)))))
8790
8791         ;;;!!!Why is this necessary?
8792         (set-buffer gnus-summary-buffer)
8793
8794         (gnus-summary-goto-subject article)
8795         (when (eq action 'move)
8796           (gnus-summary-mark-article article gnus-canceled-mark))))
8797       (gnus-summary-remove-process-mark article))
8798     ;; Re-activate all groups that have been moved to.
8799     (save-excursion
8800       (set-buffer gnus-group-buffer)
8801       (let ((gnus-group-marked to-groups))
8802         (gnus-group-get-new-news-this-group nil t)))
8803
8804     (gnus-kill-buffer copy-buf)
8805     (gnus-summary-position-point)
8806     (gnus-set-mode-line 'summary)))
8807
8808 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8809   "Move the current article to a different newsgroup.
8810 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8811 When called interactively, if TO-NEWSGROUP is nil, use the value of
8812 the variable `gnus-move-split-methods' for finding a default target
8813 newsgroup.
8814 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8815 re-spool using this method."
8816   (interactive "P")
8817   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8818
8819 (defun gnus-summary-crosspost-article (&optional n)
8820   "Crosspost the current article to some other group."
8821   (interactive "P")
8822   (gnus-summary-move-article n nil nil 'crosspost))
8823
8824 (defcustom gnus-summary-respool-default-method nil
8825   "Default method type for respooling an article.
8826 If nil, use to the current newsgroup method."
8827   :type 'symbol
8828   :group 'gnus-summary-mail)
8829
8830 (defcustom gnus-summary-display-while-building nil
8831   "If not-nil, show and update the summary buffer as it's being built.
8832 If the value is t, update the buffer after every line is inserted.  If
8833 the value is an integer (N), update the display every N lines."
8834   :group 'gnus-thread
8835   :type '(choice (const :tag "off" nil)
8836                  number
8837                  (const :tag "frequently" t)))
8838
8839 (defun gnus-summary-respool-article (&optional n method)
8840   "Respool the current article.
8841 The article will be squeezed through the mail spooling process again,
8842 which means that it will be put in some mail newsgroup or other
8843 depending on `nnmail-split-methods'.
8844 If N is a positive number, respool the N next articles.
8845 If N is a negative number, respool the N previous articles.
8846 If N is nil and any articles have been marked with the process mark,
8847 respool those articles instead.
8848
8849 Respooling can be done both from mail groups and \"real\" newsgroups.
8850 In the former case, the articles in question will be moved from the
8851 current group into whatever groups they are destined to.  In the
8852 latter case, they will be copied into the relevant groups."
8853   (interactive
8854    (list current-prefix-arg
8855          (let* ((methods (gnus-methods-using 'respool))
8856                 (methname
8857                  (symbol-name (or gnus-summary-respool-default-method
8858                                   (car (gnus-find-method-for-group
8859                                         gnus-newsgroup-name)))))
8860                 (method
8861                  (gnus-completing-read-with-default
8862                   methname "What backend do you want to use when respooling?"
8863                   methods nil t nil 'gnus-mail-method-history))
8864                 ms)
8865            (cond
8866             ((zerop (length (setq ms (gnus-servers-using-backend
8867                                       (intern method)))))
8868              (list (intern method) ""))
8869             ((= 1 (length ms))
8870              (car ms))
8871             (t
8872              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8873                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8874                            ms-alist))))))))
8875   (unless method
8876     (error "No method given for respooling"))
8877   (if (assoc (symbol-name
8878               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8879              (gnus-methods-using 'respool))
8880       (gnus-summary-move-article n nil method)
8881     (gnus-summary-copy-article n nil method)))
8882
8883 (defun gnus-summary-import-article (file &optional edit)
8884   "Import an arbitrary file into a mail newsgroup."
8885   (interactive "fImport file: \nP")
8886   (let ((group gnus-newsgroup-name)
8887         (now (current-time))
8888         atts lines group-art)
8889     (unless (gnus-check-backend-function 'request-accept-article group)
8890       (error "%s does not support article importing" group))
8891     (or (file-readable-p file)
8892         (not (file-regular-p file))
8893         (error "Can't read %s" file))
8894     (save-excursion
8895       (set-buffer (gnus-get-buffer-create " *import file*"))
8896       (erase-buffer)
8897       (nnheader-insert-file-contents file)
8898       (goto-char (point-min))
8899       (if (nnheader-article-p)
8900           (save-restriction
8901             (goto-char (point-min))
8902             (search-forward "\n\n" nil t)
8903             (narrow-to-region (point-min) (1- (point)))
8904             (goto-char (point-min))
8905             (unless (re-search-forward "^date:" nil t)
8906               (goto-char (point-max))
8907               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8908        ;; This doesn't look like an article, so we fudge some headers.
8909         (setq atts (file-attributes file)
8910               lines (count-lines (point-min) (point-max)))
8911         (insert "From: " (read-string "From: ") "\n"
8912                 "Subject: " (read-string "Subject: ") "\n"
8913                 "Date: " (message-make-date (nth 5 atts)) "\n"
8914                 "Message-ID: " (message-make-message-id) "\n"
8915                 "Lines: " (int-to-string lines) "\n"
8916                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8917       (setq group-art (gnus-request-accept-article group nil t))
8918       (kill-buffer (current-buffer)))
8919     (setq gnus-newsgroup-active (gnus-activate-group group))
8920     (forward-line 1)
8921     (gnus-summary-goto-article (cdr group-art) nil t)
8922     (when edit
8923       (gnus-summary-edit-article))))
8924
8925 (defun gnus-summary-create-article ()
8926   "Create an article in a mail newsgroup."
8927   (interactive)
8928   (let ((group gnus-newsgroup-name)
8929         (now (current-time))
8930         group-art)
8931     (unless (gnus-check-backend-function 'request-accept-article group)
8932       (error "%s does not support article importing" group))
8933     (save-excursion
8934       (set-buffer (gnus-get-buffer-create " *import file*"))
8935       (erase-buffer)
8936       (goto-char (point-min))
8937       ;; This doesn't look like an article, so we fudge some headers.
8938       (insert "From: " (read-string "From: ") "\n"
8939               "Subject: " (read-string "Subject: ") "\n"
8940               "Date: " (message-make-date now) "\n"
8941               "Message-ID: " (message-make-message-id) "\n")
8942       (setq group-art (gnus-request-accept-article group nil t))
8943       (kill-buffer (current-buffer)))
8944     (setq gnus-newsgroup-active (gnus-activate-group group))
8945     (forward-line 1)
8946     (gnus-summary-goto-article (cdr group-art) nil t)
8947     (gnus-summary-edit-article)))
8948
8949 (defun gnus-summary-article-posted-p ()
8950   "Say whether the current (mail) article is available from news as well.
8951 This will be the case if the article has both been mailed and posted."
8952   (interactive)
8953   (let ((id (mail-header-references (gnus-summary-article-header)))
8954         (gnus-override-method (car (gnus-refer-article-methods))))
8955     (if (gnus-request-head id "")
8956         (gnus-message 2 "The current message was found on %s"
8957                       gnus-override-method)
8958       (gnus-message 2 "The current message couldn't be found on %s"
8959                     gnus-override-method)
8960       nil)))
8961
8962 (defun gnus-summary-expire-articles (&optional now)
8963   "Expire all articles that are marked as expirable in the current group."
8964   (interactive)
8965   (when (gnus-check-backend-function
8966          'request-expire-articles gnus-newsgroup-name)
8967     ;; This backend supports expiry.
8968     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8969            (expirable (if total
8970                           (progn
8971                             ;; We need to update the info for
8972                             ;; this group for `gnus-list-of-read-articles'
8973                             ;; to give us the right answer.
8974                             (gnus-run-hooks 'gnus-exit-group-hook)
8975                             (gnus-summary-update-info)
8976                             (gnus-list-of-read-articles gnus-newsgroup-name))
8977                         (setq gnus-newsgroup-expirable
8978                               (sort gnus-newsgroup-expirable '<))))
8979            (expiry-wait (if now 'immediate
8980                           (gnus-group-find-parameter
8981                            gnus-newsgroup-name 'expiry-wait)))
8982            (nnmail-expiry-target
8983             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8984                 nnmail-expiry-target))
8985            es)
8986       (when expirable
8987         ;; There are expirable articles in this group, so we run them
8988         ;; through the expiry process.
8989         (gnus-message 6 "Expiring articles...")
8990         (unless (gnus-check-group gnus-newsgroup-name)
8991           (error "Can't open server for %s" gnus-newsgroup-name))
8992         ;; The list of articles that weren't expired is returned.
8993         (save-excursion
8994           (if expiry-wait
8995               (let ((nnmail-expiry-wait-function nil)
8996                     (nnmail-expiry-wait expiry-wait))
8997                 (setq es (gnus-request-expire-articles
8998                           expirable gnus-newsgroup-name)))
8999             (setq es (gnus-request-expire-articles
9000                       expirable gnus-newsgroup-name)))
9001           (unless total
9002             (setq gnus-newsgroup-expirable es))
9003           ;; We go through the old list of expirable, and mark all
9004           ;; really expired articles as nonexistent.
9005           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9006             (let ((gnus-use-cache nil))
9007               (dolist (article expirable)
9008                 (when (and (not (memq article es))
9009                            (gnus-data-find article))
9010                   (gnus-summary-mark-article article gnus-canceled-mark))))))
9011         (gnus-message 6 "Expiring articles...done")))))
9012
9013 (defun gnus-summary-expire-articles-now ()
9014   "Expunge all expirable articles in the current group.
9015 This means that *all* articles that are marked as expirable will be
9016 deleted forever, right now."
9017   (interactive)
9018   (or gnus-expert-user
9019       (gnus-yes-or-no-p
9020        "Are you really, really, really sure you want to delete all these messages? ")
9021       (error "Phew!"))
9022   (gnus-summary-expire-articles t))
9023
9024 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9025 (defun gnus-summary-delete-article (&optional n)
9026   "Delete the N next (mail) articles.
9027 This command actually deletes articles.  This is not a marking
9028 command.  The article will disappear forever from your life, never to
9029 return.
9030 If N is negative, delete backwards.
9031 If N is nil and articles have been marked with the process mark,
9032 delete these instead."
9033   (interactive "P")
9034   (unless (gnus-check-backend-function 'request-expire-articles
9035                                        gnus-newsgroup-name)
9036     (error "The current newsgroup does not support article deletion"))
9037   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9038     (error "Couldn't open server"))
9039   ;; Compute the list of articles to delete.
9040   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9041         (nnmail-expiry-target 'delete)
9042         not-deleted)
9043     (if (and gnus-novice-user
9044              (not (gnus-yes-or-no-p
9045                    (format "Do you really want to delete %s forever? "
9046                            (if (> (length articles) 1)
9047                                (format "these %s articles" (length articles))
9048                              "this article")))))
9049         ()
9050       ;; Delete the articles.
9051       (setq not-deleted (gnus-request-expire-articles
9052                          articles gnus-newsgroup-name 'force))
9053       (while articles
9054         (gnus-summary-remove-process-mark (car articles))
9055         ;; The backend might not have been able to delete the article
9056         ;; after all.
9057         (unless (memq (car articles) not-deleted)
9058           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9059         (setq articles (cdr articles)))
9060       (when not-deleted
9061         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9062     (gnus-summary-position-point)
9063     (gnus-set-mode-line 'summary)
9064     not-deleted))
9065
9066 (defun gnus-summary-edit-article (&optional arg)
9067   "Edit the current article.
9068 This will have permanent effect only in mail groups.
9069 If ARG is nil, edit the decoded articles.
9070 If ARG is 1, edit the raw articles.
9071 If ARG is 2, edit the raw articles even in read-only groups.
9072 If ARG is 3, edit the articles with the current handles.
9073 Otherwise, allow editing of articles even in read-only
9074 groups."
9075   (interactive "P")
9076   (let (force raw current-handles)
9077     (cond
9078      ((null arg))
9079      ((eq arg 1)
9080       (setq raw t))
9081      ((eq arg 2)
9082       (setq raw t
9083             force t))
9084      ((eq arg 3)
9085       (setq current-handles
9086             (and (gnus-buffer-live-p gnus-article-buffer)
9087                  (with-current-buffer gnus-article-buffer
9088                    (prog1
9089                        gnus-article-mime-handles
9090                      (setq gnus-article-mime-handles nil))))))
9091      (t
9092       (setq force t)))
9093     (when (and raw (not force)
9094                (member gnus-newsgroup-name '("nndraft:delayed"
9095                                              "nndraft:drafts"
9096                                              "nndraft:queue")))
9097       (error "Can't edit the raw article in group %s"
9098              gnus-newsgroup-name))
9099     (save-excursion
9100       (set-buffer gnus-summary-buffer)
9101       (let ((mail-parse-charset gnus-newsgroup-charset)
9102             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9103         (gnus-set-global-variables)
9104         (when (and (not force)
9105                    (gnus-group-read-only-p))
9106           (error "The current newsgroup does not support article editing"))
9107         (gnus-summary-show-article t)
9108         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9109           (with-current-buffer gnus-article-buffer
9110             (mm-enable-multibyte)))
9111         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9112             (setq raw t))
9113         (gnus-article-edit-article
9114          (if raw 'ignore
9115            `(lambda ()
9116               (let ((mbl mml-buffer-list))
9117                 (setq mml-buffer-list nil)
9118                 (mime-to-mml ,'current-handles)
9119                 (let ((mbl1 mml-buffer-list))
9120                   (setq mml-buffer-list mbl)
9121                   (set (make-local-variable 'mml-buffer-list) mbl1))
9122                 (make-local-hook 'kill-buffer-hook)
9123                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9124          `(lambda (no-highlight)
9125             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9126                   (message-options message-options)
9127                   (message-options-set-recipient)
9128                   (mail-parse-ignored-charsets
9129                    ',gnus-newsgroup-ignored-charsets))
9130               ,(if (not raw) '(progn
9131                                 (mml-to-mime)
9132                                 (mml-destroy-buffers)
9133                                 (remove-hook 'kill-buffer-hook
9134                                              'mml-destroy-buffers t)
9135                                 (kill-local-variable 'mml-buffer-list)))
9136               (gnus-summary-edit-article-done
9137                ,(or (mail-header-references gnus-current-headers) "")
9138                ,(gnus-group-read-only-p)
9139                ,gnus-summary-buffer no-highlight))))))))
9140
9141 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9142
9143 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9144                                                  no-highlight)
9145   "Make edits to the current article permanent."
9146   (interactive)
9147   (save-excursion
9148    ;; The buffer restriction contains the entire article if it exists.
9149     (when (article-goto-body)
9150       (let ((lines (count-lines (point) (point-max)))
9151             (length (- (point-max) (point)))
9152             (case-fold-search t)
9153             (body (copy-marker (point))))
9154         (goto-char (point-min))
9155         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9156           (delete-region (match-beginning 1) (match-end 1))
9157           (insert (number-to-string length)))
9158         (goto-char (point-min))
9159         (when (re-search-forward
9160                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9161           (delete-region (match-beginning 1) (match-end 1))
9162           (insert (number-to-string length)))
9163         (goto-char (point-min))
9164         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9165           (delete-region (match-beginning 1) (match-end 1))
9166           (insert (number-to-string lines))))))
9167   ;; Replace the article.
9168   (let ((buf (current-buffer)))
9169     (with-temp-buffer
9170       (insert-buffer-substring buf)
9171
9172       (if (and (not read-only)
9173                (not (gnus-request-replace-article
9174                      (cdr gnus-article-current) (car gnus-article-current)
9175                      (current-buffer) t)))
9176           (error "Couldn't replace article")
9177         ;; Update the summary buffer.
9178         (if (and references
9179                  (equal (message-tokenize-header references " ")
9180                         (message-tokenize-header
9181                          (or (message-fetch-field "references") "") " ")))
9182             ;; We only have to update this line.
9183             (save-excursion
9184               (save-restriction
9185                 (message-narrow-to-head)
9186                 (let ((head (buffer-string))
9187                       header)
9188                   (with-temp-buffer
9189                     (insert (format "211 %d Article retrieved.\n"
9190                                     (cdr gnus-article-current)))
9191                     (insert head)
9192                     (insert ".\n")
9193                     (let ((nntp-server-buffer (current-buffer)))
9194                       (setq header (car (gnus-get-newsgroup-headers
9195                                          nil t))))
9196                     (save-excursion
9197                       (set-buffer gnus-summary-buffer)
9198                       (gnus-data-set-header
9199                        (gnus-data-find (cdr gnus-article-current))
9200                        header)
9201                       (gnus-summary-update-article-line
9202                        (cdr gnus-article-current) header)
9203                       (if (gnus-summary-goto-subject
9204                            (cdr gnus-article-current) nil t)
9205                           (gnus-summary-update-secondary-mark
9206                            (cdr gnus-article-current))))))))
9207           ;; Update threads.
9208           (set-buffer (or buffer gnus-summary-buffer))
9209           (gnus-summary-update-article (cdr gnus-article-current))
9210           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9211               (gnus-summary-update-secondary-mark
9212                (cdr gnus-article-current))))
9213         ;; Prettify the article buffer again.
9214         (unless no-highlight
9215           (save-excursion
9216             (set-buffer gnus-article-buffer)
9217             ;;;!!! Fix this -- article should be rehighlighted.
9218             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9219             (set-buffer gnus-original-article-buffer)
9220             (gnus-request-article
9221              (cdr gnus-article-current)
9222              (car gnus-article-current) (current-buffer))))
9223         ;; Prettify the summary buffer line.
9224         (when (gnus-visual-p 'summary-highlight 'highlight)
9225           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9226
9227 (defun gnus-summary-edit-wash (key)
9228   "Perform editing command KEY in the article buffer."
9229   (interactive
9230    (list
9231     (progn
9232       (message "%s" (concat (this-command-keys) "- "))
9233       (read-char))))
9234   (message "")
9235   (gnus-summary-edit-article)
9236   (execute-kbd-macro (concat (this-command-keys) key))
9237   (gnus-article-edit-done))
9238
9239 ;;; Respooling
9240
9241 (defun gnus-summary-respool-query (&optional silent trace)
9242   "Query where the respool algorithm would put this article."
9243   (interactive)
9244   (let (gnus-mark-article-hook)
9245     (gnus-summary-select-article)
9246     (save-excursion
9247       (set-buffer gnus-original-article-buffer)
9248       (save-restriction
9249         (message-narrow-to-head)
9250         (let ((groups (nnmail-article-group 'identity trace)))
9251           (unless silent
9252             (if groups
9253                 (message "This message would go to %s"
9254                          (mapconcat 'car groups ", "))
9255               (message "This message would go to no groups"))
9256             groups))))))
9257
9258 (defun gnus-summary-respool-trace ()
9259   "Trace where the respool algorithm would put this article.
9260 Display a buffer showing all fancy splitting patterns which matched."
9261   (interactive)
9262   (gnus-summary-respool-query nil t))
9263
9264 ;; Summary marking commands.
9265
9266 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9267   "Mark articles which has the same subject as read, and then select the next.
9268 If UNMARK is positive, remove any kind of mark.
9269 If UNMARK is negative, tick articles."
9270   (interactive "P")
9271   (when unmark
9272     (setq unmark (prefix-numeric-value unmark)))
9273   (let ((count
9274          (gnus-summary-mark-same-subject
9275           (gnus-summary-article-subject) unmark)))
9276     ;; Select next unread article.  If auto-select-same mode, should
9277     ;; select the first unread article.
9278     (gnus-summary-next-article t (and gnus-auto-select-same
9279                                       (gnus-summary-article-subject)))
9280     (gnus-message 7 "%d article%s marked as %s"
9281                   count (if (= count 1) " is" "s are")
9282                   (if unmark "unread" "read"))))
9283
9284 (defun gnus-summary-kill-same-subject (&optional unmark)
9285   "Mark articles which has the same subject as read.
9286 If UNMARK is positive, remove any kind of mark.
9287 If UNMARK is negative, tick articles."
9288   (interactive "P")
9289   (when unmark
9290     (setq unmark (prefix-numeric-value unmark)))
9291   (let ((count
9292          (gnus-summary-mark-same-subject
9293           (gnus-summary-article-subject) unmark)))
9294     ;; If marked as read, go to next unread subject.
9295     (when (null unmark)
9296       ;; Go to next unread subject.
9297       (gnus-summary-next-subject 1 t))
9298     (gnus-message 7 "%d articles are marked as %s"
9299                   count (if unmark "unread" "read"))))
9300
9301 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9302   "Mark articles with same SUBJECT as read, and return marked number.
9303 If optional argument UNMARK is positive, remove any kinds of marks.
9304 If optional argument UNMARK is negative, mark articles as unread instead."
9305   (let ((count 1))
9306     (save-excursion
9307       (cond
9308        ((null unmark)                   ; Mark as read.
9309         (while (and
9310                 (progn
9311                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9312                   (gnus-summary-show-thread) t)
9313                 (gnus-summary-find-subject subject))
9314           (setq count (1+ count))))
9315        ((> unmark 0)                    ; Tick.
9316         (while (and
9317                 (progn
9318                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9319                   (gnus-summary-show-thread) t)
9320                 (gnus-summary-find-subject subject))
9321           (setq count (1+ count))))
9322        (t                               ; Mark as unread.
9323         (while (and
9324                 (progn
9325                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9326                   (gnus-summary-show-thread) t)
9327                 (gnus-summary-find-subject subject))
9328           (setq count (1+ count)))))
9329       (gnus-set-mode-line 'summary)
9330       ;; Return the number of marked articles.
9331       count)))
9332
9333 (defun gnus-summary-mark-as-processable (n &optional unmark)
9334   "Set the process mark on the next N articles.
9335 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9336 the process mark instead.  The difference between N and the actual
9337 number of articles marked is returned."
9338   (interactive "P")
9339   (if (and (null n) (gnus-region-active-p))
9340       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9341     (setq n (prefix-numeric-value n))
9342     (let ((backward (< n 0))
9343           (n (abs n)))
9344       (while (and
9345               (> n 0)
9346               (if unmark
9347                   (gnus-summary-remove-process-mark
9348                    (gnus-summary-article-number))
9349                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9350               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9351         (setq n (1- n)))
9352       (when (/= 0 n)
9353         (gnus-message 7 "No more articles"))
9354       (gnus-summary-recenter)
9355       (gnus-summary-position-point)
9356       n)))
9357
9358 (defun gnus-summary-unmark-as-processable (n)
9359   "Remove the process mark from the next N articles.
9360 If N is negative, unmark backward instead.  The difference between N and
9361 the actual number of articles unmarked is returned."
9362   (interactive "P")
9363   (gnus-summary-mark-as-processable n t))
9364
9365 (defun gnus-summary-unmark-all-processable ()
9366   "Remove the process mark from all articles."
9367   (interactive)
9368   (save-excursion
9369     (while gnus-newsgroup-processable
9370       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9371   (gnus-summary-position-point))
9372
9373 (defun gnus-summary-add-mark (article type)
9374   "Mark ARTICLE with a mark of TYPE."
9375   (let ((vtype (car (assq type gnus-article-mark-lists)))
9376         var)
9377     (if (not vtype)
9378         (error "No such mark type: %s" type)
9379       (setq var (intern (format "gnus-newsgroup-%s" type)))
9380       (set var (cons article (symbol-value var)))
9381       (if (memq type '(processable cached replied forwarded recent saved))
9382           (gnus-summary-update-secondary-mark article)
9383         ;;; !!! This is bogus.  We should find out what primary
9384         ;;; !!! mark we want to set.
9385         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9386
9387 (defun gnus-summary-mark-as-expirable (n)
9388   "Mark N articles forward as expirable.
9389 If N is negative, mark backward instead.  The difference between N and
9390 the actual number of articles marked is returned."
9391   (interactive "p")
9392   (gnus-summary-mark-forward n gnus-expirable-mark))
9393
9394 (defun gnus-summary-mark-as-spam (n)
9395   "Mark N articles forward as spam.
9396 If N is negative, mark backward instead.  The difference between N and
9397 the actual number of articles marked is returned."
9398   (interactive "p")
9399   (gnus-summary-mark-forward n gnus-spam-mark))
9400
9401 (defun gnus-summary-mark-article-as-replied (article)
9402   "Mark ARTICLE as replied to and update the summary line.
9403 ARTICLE can also be a list of articles."
9404   (interactive (list (gnus-summary-article-number)))
9405   (let ((articles (if (listp article) article (list article))))
9406     (dolist (article articles)
9407       (push article gnus-newsgroup-replied)
9408       (let ((buffer-read-only nil))
9409         (when (gnus-summary-goto-subject article nil t)
9410           (gnus-summary-update-secondary-mark article))))))
9411
9412 (defun gnus-summary-mark-article-as-forwarded (article)
9413   "Mark ARTICLE as forwarded and update the summary line.
9414 ARTICLE can also be a list of articles."
9415   (let ((articles (if (listp article) article (list article))))
9416     (dolist (article articles)
9417       (push article gnus-newsgroup-forwarded)
9418       (let ((buffer-read-only nil))
9419         (when (gnus-summary-goto-subject article nil t)
9420           (gnus-summary-update-secondary-mark article))))))
9421
9422 (defun gnus-summary-set-bookmark (article)
9423   "Set a bookmark in current article."
9424   (interactive (list (gnus-summary-article-number)))
9425   (when (or (not (get-buffer gnus-article-buffer))
9426             (not gnus-current-article)
9427             (not gnus-article-current)
9428             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9429     (error "No current article selected"))
9430   ;; Remove old bookmark, if one exists.
9431   (gnus-pull article gnus-newsgroup-bookmarks)
9432   ;; Set the new bookmark, which is on the form
9433   ;; (article-number . line-number-in-body).
9434   (push
9435    (cons article
9436          (save-excursion
9437            (set-buffer gnus-article-buffer)
9438            (count-lines
9439             (min (point)
9440                  (save-excursion
9441                    (article-goto-body)
9442                    (point)))
9443             (point))))
9444    gnus-newsgroup-bookmarks)
9445   (gnus-message 6 "A bookmark has been added to the current article."))
9446
9447 (defun gnus-summary-remove-bookmark (article)
9448   "Remove the bookmark from the current article."
9449   (interactive (list (gnus-summary-article-number)))
9450   ;; Remove old bookmark, if one exists.
9451   (if (not (assq article gnus-newsgroup-bookmarks))
9452       (gnus-message 6 "No bookmark in current article.")
9453     (gnus-pull article gnus-newsgroup-bookmarks)
9454     (gnus-message 6 "Removed bookmark.")))
9455
9456 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9457 (defun gnus-summary-mark-as-dormant (n)
9458   "Mark N articles forward as dormant.
9459 If N is negative, mark backward instead.  The difference between N and
9460 the actual number of articles marked is returned."
9461   (interactive "p")
9462   (gnus-summary-mark-forward n gnus-dormant-mark))
9463
9464 (defun gnus-summary-set-process-mark (article)
9465   "Set the process mark on ARTICLE and update the summary line."
9466   (setq gnus-newsgroup-processable
9467         (cons article
9468               (delq article gnus-newsgroup-processable)))
9469   (when (gnus-summary-goto-subject article)
9470     (gnus-summary-show-thread)
9471     (gnus-summary-goto-subject article)
9472     (gnus-summary-update-secondary-mark article)))
9473
9474 (defun gnus-summary-remove-process-mark (article)
9475   "Remove the process mark from ARTICLE and update the summary line."
9476   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9477   (when (gnus-summary-goto-subject article)
9478     (gnus-summary-show-thread)
9479     (gnus-summary-goto-subject article)
9480     (gnus-summary-update-secondary-mark article)))
9481
9482 (defun gnus-summary-set-saved-mark (article)
9483   "Set the process mark on ARTICLE and update the summary line."
9484   (push article gnus-newsgroup-saved)
9485   (when (gnus-summary-goto-subject article)
9486     (gnus-summary-update-secondary-mark article)))
9487
9488 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9489   "Mark N articles as read forwards.
9490 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9491 The difference between N and the actual number of articles marked is
9492 returned.
9493 If NO-EXPIRE, auto-expiry will be inhibited."
9494   (interactive "p")
9495   (gnus-summary-show-thread)
9496   (let ((backward (< n 0))
9497         (gnus-summary-goto-unread
9498          (and gnus-summary-goto-unread
9499               (not (eq gnus-summary-goto-unread 'never))
9500               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9501                                     gnus-ticked-mark gnus-dormant-mark)))))
9502         (n (abs n))
9503         (mark (or mark gnus-del-mark)))
9504     (while (and (> n 0)
9505                 (gnus-summary-mark-article nil mark no-expire)
9506                 (zerop (gnus-summary-next-subject
9507                         (if backward -1 1)
9508                         (and gnus-summary-goto-unread
9509                              (not (eq gnus-summary-goto-unread 'never)))
9510                         t)))
9511       (setq n (1- n)))
9512     (when (/= 0 n)
9513       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9514     (gnus-summary-recenter)
9515     (gnus-summary-position-point)
9516     (gnus-set-mode-line 'summary)
9517     n))
9518
9519 (defun gnus-summary-mark-article-as-read (mark)
9520   "Mark the current article quickly as read with MARK."
9521   (let ((article (gnus-summary-article-number)))
9522     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9523     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9524     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9525     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9526     (push (cons article mark) gnus-newsgroup-reads)
9527     ;; Possibly remove from cache, if that is used.
9528     (when gnus-use-cache
9529       (gnus-cache-enter-remove-article article))
9530     ;; Allow the backend to change the mark.
9531     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9532     ;; Check for auto-expiry.
9533     (when (and gnus-newsgroup-auto-expire
9534                (memq mark gnus-auto-expirable-marks))
9535       (setq mark gnus-expirable-mark)
9536       ;; Let the backend know about the mark change.
9537       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9538       (push article gnus-newsgroup-expirable))
9539     ;; Set the mark in the buffer.
9540     (gnus-summary-update-mark mark 'unread)
9541     t))
9542
9543 (defun gnus-summary-mark-article-as-unread (mark)
9544   "Mark the current article quickly as unread with MARK."
9545   (let* ((article (gnus-summary-article-number))
9546          (old-mark (gnus-summary-article-mark article)))
9547     ;; Allow the backend to change the mark.
9548     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9549     (if (eq mark old-mark)
9550         t
9551       (if (<= article 0)
9552           (progn
9553             (gnus-error 1 "Can't mark negative article numbers")
9554             nil)
9555         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9556         (setq gnus-newsgroup-spam-marked
9557               (delq article gnus-newsgroup-spam-marked))
9558         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9559         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9560         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9561         (cond ((= mark gnus-ticked-mark)
9562                (setq gnus-newsgroup-marked
9563                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9564                                               article)))
9565               ((= mark gnus-spam-mark)
9566                (setq gnus-newsgroup-spam-marked
9567                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9568                                               article)))
9569               ((= mark gnus-dormant-mark)
9570                (setq gnus-newsgroup-dormant
9571                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9572                                               article)))
9573               (t
9574                (setq gnus-newsgroup-unreads
9575                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9576                                               article))))
9577         (gnus-pull article gnus-newsgroup-reads)
9578
9579         ;; See whether the article is to be put in the cache.
9580         (and gnus-use-cache
9581              (vectorp (gnus-summary-article-header article))
9582              (save-excursion
9583                (gnus-cache-possibly-enter-article
9584                 gnus-newsgroup-name article
9585                 (= mark gnus-ticked-mark)
9586                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9587
9588         ;; Fix the mark.
9589         (gnus-summary-update-mark mark 'unread)
9590         t))))
9591
9592 (defun gnus-summary-mark-article (&optional article mark no-expire)
9593   "Mark ARTICLE with MARK.  MARK can be any character.
9594 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9595 `??' (dormant) and `?E' (expirable).
9596 If MARK is nil, then the default character `?r' is used.
9597 If ARTICLE is nil, then the article on the current line will be
9598 marked.
9599 If NO-EXPIRE, auto-expiry will be inhibited."
9600   ;; The mark might be a string.
9601   (when (stringp mark)
9602     (setq mark (aref mark 0)))
9603   ;; If no mark is given, then we check auto-expiring.
9604   (when (null mark)
9605     (setq mark gnus-del-mark))
9606   (when (and (not no-expire)
9607              gnus-newsgroup-auto-expire
9608              (memq mark gnus-auto-expirable-marks))
9609     (setq mark gnus-expirable-mark))
9610   (let ((article (or article (gnus-summary-article-number)))
9611         (old-mark (gnus-summary-article-mark article)))
9612     ;; Allow the backend to change the mark.
9613     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9614     (if (eq mark old-mark)
9615         t
9616       (unless article
9617         (error "No article on current line"))
9618       (if (not (if (or (= mark gnus-unread-mark)
9619                        (= mark gnus-ticked-mark)
9620                        (= mark gnus-spam-mark)
9621                        (= mark gnus-dormant-mark))
9622                    (gnus-mark-article-as-unread article mark)
9623                  (gnus-mark-article-as-read article mark)))
9624           t
9625         ;; See whether the article is to be put in the cache.
9626         (and gnus-use-cache
9627              (not (= mark gnus-canceled-mark))
9628              (vectorp (gnus-summary-article-header article))
9629              (save-excursion
9630                (gnus-cache-possibly-enter-article
9631                 gnus-newsgroup-name article
9632                 (= mark gnus-ticked-mark)
9633                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9634
9635         (when (gnus-summary-goto-subject article nil t)
9636           (let ((buffer-read-only nil))
9637             (gnus-summary-show-thread)
9638             ;; Fix the mark.
9639             (gnus-summary-update-mark mark 'unread)
9640             t))))))
9641
9642 (defun gnus-summary-update-secondary-mark (article)
9643   "Update the secondary (read, process, cache) mark."
9644   (gnus-summary-update-mark
9645    (cond ((memq article gnus-newsgroup-processable)
9646           gnus-process-mark)
9647          ((memq article gnus-newsgroup-cached)
9648           gnus-cached-mark)
9649          ((memq article gnus-newsgroup-replied)
9650           gnus-replied-mark)
9651          ((memq article gnus-newsgroup-forwarded)
9652           gnus-forwarded-mark)
9653          ((memq article gnus-newsgroup-saved)
9654           gnus-saved-mark)
9655          ((memq article gnus-newsgroup-recent)
9656           gnus-recent-mark)
9657          ((memq article gnus-newsgroup-unseen)
9658           gnus-unseen-mark)
9659          (t gnus-no-mark))
9660    'replied)
9661   (when (gnus-visual-p 'summary-highlight 'highlight)
9662     (gnus-run-hooks 'gnus-summary-update-hook))
9663   t)
9664
9665 (defun gnus-summary-update-download-mark (article)
9666   "Update the secondary (read, process, cache) mark."
9667   (gnus-summary-update-mark
9668    (cond ((memq article gnus-newsgroup-undownloaded) 
9669           gnus-undownloaded-mark)
9670          (gnus-newsgroup-agentized
9671           gnus-downloaded-mark)
9672          (t
9673           gnus-no-mark))
9674    'download)
9675   (gnus-summary-update-line t)
9676   t)
9677
9678 (defun gnus-summary-update-mark (mark type)
9679   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9680         (buffer-read-only nil))
9681     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9682     (when forward
9683       (when (looking-at "\r")
9684         (incf forward))
9685       (when (<= (+ forward (point)) (point-max))
9686         ;; Go to the right position on the line.
9687         (goto-char (+ forward (point)))
9688         ;; Replace the old mark with the new mark.
9689         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9690         ;; Optionally update the marks by some user rule.
9691         (when (eq type 'unread)
9692           (gnus-data-set-mark
9693            (gnus-data-find (gnus-summary-article-number)) mark)
9694           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9695
9696 (defun gnus-mark-article-as-read (article &optional mark)
9697   "Enter ARTICLE in the pertinent lists and remove it from others."
9698   ;; Make the article expirable.
9699   (let ((mark (or mark gnus-del-mark)))
9700     (setq gnus-newsgroup-expirable
9701           (if (= mark gnus-expirable-mark)
9702               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9703             (delq article gnus-newsgroup-expirable)))
9704     ;; Remove from unread and marked lists.
9705     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9706     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9707     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9708     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9709     (push (cons article mark) gnus-newsgroup-reads)
9710     ;; Possibly remove from cache, if that is used.
9711     (when gnus-use-cache
9712       (gnus-cache-enter-remove-article article))
9713     t))
9714
9715 (defun gnus-mark-article-as-unread (article &optional mark)
9716   "Enter ARTICLE in the pertinent lists and remove it from others."
9717   (let ((mark (or mark gnus-ticked-mark)))
9718     (if (<= article 0)
9719         (progn
9720           (gnus-error 1 "Can't mark negative article numbers")
9721           nil)
9722       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9723             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9724             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9725             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9726             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9727
9728       ;; Unsuppress duplicates?
9729       (when gnus-suppress-duplicates
9730         (gnus-dup-unsuppress-article article))
9731
9732       (cond ((= mark gnus-ticked-mark)
9733              (setq gnus-newsgroup-marked
9734                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9735             ((= mark gnus-spam-mark)
9736              (setq gnus-newsgroup-spam-marked
9737                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9738                                             article)))
9739             ((= mark gnus-dormant-mark)
9740              (setq gnus-newsgroup-dormant
9741                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9742             (t
9743              (setq gnus-newsgroup-unreads
9744                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9745       (gnus-pull article gnus-newsgroup-reads)
9746       t)))
9747
9748 (defalias 'gnus-summary-mark-as-unread-forward
9749   'gnus-summary-tick-article-forward)
9750 (make-obsolete 'gnus-summary-mark-as-unread-forward
9751                'gnus-summary-tick-article-forward)
9752 (defun gnus-summary-tick-article-forward (n)
9753   "Tick N articles forwards.
9754 If N is negative, tick backwards instead.
9755 The difference between N and the number of articles ticked is returned."
9756   (interactive "p")
9757   (gnus-summary-mark-forward n gnus-ticked-mark))
9758
9759 (defalias 'gnus-summary-mark-as-unread-backward
9760   'gnus-summary-tick-article-backward)
9761 (make-obsolete 'gnus-summary-mark-as-unread-backward
9762                'gnus-summary-tick-article-backward)
9763 (defun gnus-summary-tick-article-backward (n)
9764   "Tick N articles backwards.
9765 The difference between N and the number of articles ticked is returned."
9766   (interactive "p")
9767   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9768
9769 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9770 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9771 (defun gnus-summary-tick-article (&optional article clear-mark)
9772   "Mark current article as unread.
9773 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9774 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9775   (interactive)
9776   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9777                                        gnus-ticked-mark)))
9778
9779 (defun gnus-summary-mark-as-read-forward (n)
9780   "Mark N articles as read forwards.
9781 If N is negative, mark backwards instead.
9782 The difference between N and the actual number of articles marked is
9783 returned."
9784   (interactive "p")
9785   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9786
9787 (defun gnus-summary-mark-as-read-backward (n)
9788   "Mark the N articles as read backwards.
9789 The difference between N and the actual number of articles marked is
9790 returned."
9791   (interactive "p")
9792   (gnus-summary-mark-forward
9793    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9794
9795 (defun gnus-summary-mark-as-read (&optional article mark)
9796   "Mark current article as read.
9797 ARTICLE specifies the article to be marked as read.
9798 MARK specifies a string to be inserted at the beginning of the line."
9799   (gnus-summary-mark-article article mark))
9800
9801 (defun gnus-summary-clear-mark-forward (n)
9802   "Clear marks from N articles forward.
9803 If N is negative, clear backward instead.
9804 The difference between N and the number of marks cleared is returned."
9805   (interactive "p")
9806   (gnus-summary-mark-forward n gnus-unread-mark))
9807
9808 (defun gnus-summary-clear-mark-backward (n)
9809   "Clear marks from N articles backward.
9810 The difference between N and the number of marks cleared is returned."
9811   (interactive "p")
9812   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9813
9814 (defun gnus-summary-mark-unread-as-read ()
9815   "Intended to be used by `gnus-summary-mark-article-hook'."
9816   (when (memq gnus-current-article gnus-newsgroup-unreads)
9817     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9818
9819 (defun gnus-summary-mark-read-and-unread-as-read ()
9820   "Intended to be used by `gnus-summary-mark-article-hook'."
9821   (let ((mark (gnus-summary-article-mark)))
9822     (when (or (gnus-unread-mark-p mark)
9823               (gnus-read-mark-p mark))
9824       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9825
9826 (defun gnus-summary-mark-unread-as-ticked ()
9827   "Intended to be used by `gnus-summary-mark-article-hook'."
9828   (when (memq gnus-current-article gnus-newsgroup-unreads)
9829     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9830
9831 (defun gnus-summary-mark-region-as-read (point mark all)
9832   "Mark all unread articles between point and mark as read.
9833 If given a prefix, mark all articles between point and mark as read,
9834 even ticked and dormant ones."
9835   (interactive "r\nP")
9836   (save-excursion
9837     (let (article)
9838       (goto-char point)
9839       (beginning-of-line)
9840       (while (and
9841               (< (point) mark)
9842               (progn
9843                 (when (or all
9844                           (memq (setq article (gnus-summary-article-number))
9845                                 gnus-newsgroup-unreads))
9846                   (gnus-summary-mark-article article gnus-del-mark))
9847                 t)
9848               (gnus-summary-find-next))))))
9849
9850 (defun gnus-summary-mark-below (score mark)
9851   "Mark articles with score less than SCORE with MARK."
9852   (interactive "P\ncMark: ")
9853   (setq score (if score
9854                   (prefix-numeric-value score)
9855                 (or gnus-summary-default-score 0)))
9856   (save-excursion
9857     (set-buffer gnus-summary-buffer)
9858     (goto-char (point-min))
9859     (while
9860         (progn
9861           (and (< (gnus-summary-article-score) score)
9862                (gnus-summary-mark-article nil mark))
9863           (gnus-summary-find-next)))))
9864
9865 (defun gnus-summary-kill-below (&optional score)
9866   "Mark articles with score below SCORE as read."
9867   (interactive "P")
9868   (gnus-summary-mark-below score gnus-killed-mark))
9869
9870 (defun gnus-summary-clear-above (&optional score)
9871   "Clear all marks from articles with score above SCORE."
9872   (interactive "P")
9873   (gnus-summary-mark-above score gnus-unread-mark))
9874
9875 (defun gnus-summary-tick-above (&optional score)
9876   "Tick all articles with score above SCORE."
9877   (interactive "P")
9878   (gnus-summary-mark-above score gnus-ticked-mark))
9879
9880 (defun gnus-summary-mark-above (score mark)
9881   "Mark articles with score over SCORE with MARK."
9882   (interactive "P\ncMark: ")
9883   (setq score (if score
9884                   (prefix-numeric-value score)
9885                 (or gnus-summary-default-score 0)))
9886   (save-excursion
9887     (set-buffer gnus-summary-buffer)
9888     (goto-char (point-min))
9889     (while (and (progn
9890                   (when (> (gnus-summary-article-score) score)
9891                     (gnus-summary-mark-article nil mark))
9892                   t)
9893                 (gnus-summary-find-next)))))
9894
9895 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9896 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9897 (defun gnus-summary-limit-include-expunged (&optional no-error)
9898   "Display all the hidden articles that were expunged for low scores."
9899   (interactive)
9900   (let ((buffer-read-only nil))
9901     (let ((scored gnus-newsgroup-scored)
9902           headers h)
9903       (while scored
9904         (unless (gnus-summary-article-header (caar scored))
9905           (and (setq h (gnus-number-to-header (caar scored)))
9906                (< (cdar scored) gnus-summary-expunge-below)
9907                (push h headers)))
9908         (setq scored (cdr scored)))
9909       (if (not headers)
9910           (when (not no-error)
9911             (error "No expunged articles hidden"))
9912         (goto-char (point-min))
9913         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9914         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9915         (mapcar (lambda (x) (push (mail-header-number x)
9916                                   gnus-newsgroup-limit))
9917                 headers)
9918         (gnus-summary-prepare-unthreaded (nreverse headers))
9919         (goto-char (point-min))
9920         (gnus-summary-position-point)
9921         t))))
9922
9923 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9924   "Mark all unread articles in this newsgroup as read.
9925 If prefix argument ALL is non-nil, ticked and dormant articles will
9926 also be marked as read.
9927 If QUIETLY is non-nil, no questions will be asked.
9928 If TO-HERE is non-nil, it should be a point in the buffer.  All
9929 articles before (after, if REVERSE is set) this point will be marked as read.
9930 Note that this function will only catch up the unread article
9931 in the current summary buffer limitation.
9932 The number of articles marked as read is returned."
9933   (interactive "P")
9934   (prog1
9935       (save-excursion
9936         (when (or quietly
9937                   (not gnus-interactive-catchup) ;Without confirmation?
9938                   gnus-expert-user
9939                   (gnus-y-or-n-p
9940                    (if all
9941                        "Mark absolutely all articles as read? "
9942                      "Mark all unread articles as read? ")))
9943           (if (and not-mark
9944                    (not gnus-newsgroup-adaptive)
9945                    (not gnus-newsgroup-auto-expire)
9946                    (not gnus-suppress-duplicates)
9947                    (or (not gnus-use-cache)
9948                        (eq gnus-use-cache 'passive)))
9949               (progn
9950                 (when all
9951                   (setq gnus-newsgroup-marked nil
9952                         gnus-newsgroup-spam-marked nil
9953                         gnus-newsgroup-dormant nil))
9954                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9955             ;; We actually mark all articles as canceled, which we
9956             ;; have to do when using auto-expiry or adaptive scoring.
9957             (gnus-summary-show-all-threads)
9958             (if (and to-here reverse)
9959                 (progn
9960                   (goto-char to-here)
9961                   (while (and
9962                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9963                           (gnus-summary-find-next (not all) nil nil t))))
9964               (when (gnus-summary-first-subject (not all) t)
9965                 (while (and
9966                         (if to-here (< (point) to-here) t)
9967                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9968                         (gnus-summary-find-next (not all) nil nil t)))))
9969             (gnus-set-mode-line 'summary))
9970           t))
9971     (gnus-summary-position-point)))
9972
9973 (defun gnus-summary-catchup-to-here (&optional all)
9974   "Mark all unticked articles before the current one as read.
9975 If ALL is non-nil, also mark ticked and dormant articles as read."
9976   (interactive "P")
9977   (save-excursion
9978     (gnus-save-hidden-threads
9979       (let ((beg (point)))
9980         ;; We check that there are unread articles.
9981         (when (or all (gnus-summary-find-prev))
9982           (gnus-summary-catchup all t beg)))))
9983   (gnus-summary-position-point))
9984
9985 (defun gnus-summary-catchup-from-here (&optional all)
9986   "Mark all unticked articles after the current one as read.
9987 If ALL is non-nil, also mark ticked and dormant articles as read."
9988   (interactive "P")
9989   (save-excursion
9990     (gnus-save-hidden-threads
9991       (let ((beg (point)))
9992         ;; We check that there are unread articles.
9993         (when (or all (gnus-summary-find-next))
9994           (gnus-summary-catchup all t beg nil t)))))
9995
9996   (gnus-summary-position-point))
9997 (defun gnus-summary-catchup-all (&optional quietly)
9998   "Mark all articles in this newsgroup as read.
9999 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10000 instead, which marks only unread articles as read."
10001   (interactive "P")
10002   (gnus-summary-catchup t quietly))
10003
10004 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10005   "Mark all unread articles in this group as read, then exit.
10006 If prefix argument ALL is non-nil, all articles are marked as read.
10007 If QUIETLY is non-nil, no questions will be asked."
10008   (interactive "P")
10009   (when (gnus-summary-catchup all quietly nil 'fast)
10010     ;; Select next newsgroup or exit.
10011     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10012              (eq gnus-auto-select-next 'quietly))
10013         (gnus-summary-next-group nil)
10014       (gnus-summary-exit))))
10015
10016 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10017   "Mark all articles in this newsgroup as read, and then exit.
10018 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10019 instead, which marks only unread articles as read."
10020   (interactive "P")
10021   (gnus-summary-catchup-and-exit t quietly))
10022
10023 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10024   "Mark all articles in this group as read and select the next group.
10025 If given a prefix, mark all articles, unread as well as ticked, as
10026 read."
10027   (interactive "P")
10028   (save-excursion
10029     (gnus-summary-catchup all))
10030   (gnus-summary-next-group))
10031
10032 ;;;
10033 ;;; with article
10034 ;;;
10035
10036 (defmacro gnus-with-article (article &rest forms)
10037   "Select ARTICLE and perform FORMS in the original article buffer.
10038 Then replace the article with the result."
10039   `(progn
10040      ;; We don't want the article to be marked as read.
10041      (let (gnus-mark-article-hook)
10042        (gnus-summary-select-article t t nil ,article))
10043      (set-buffer gnus-original-article-buffer)
10044      ,@forms
10045      (if (not (gnus-check-backend-function
10046                'request-replace-article (car gnus-article-current)))
10047          (gnus-message 5 "Read-only group; not replacing")
10048        (unless (gnus-request-replace-article
10049                 ,article (car gnus-article-current)
10050                 (current-buffer) t)
10051          (error "Couldn't replace article")))
10052      ;; The cache and backlog have to be flushed somewhat.
10053      (when gnus-keep-backlog
10054        (gnus-backlog-remove-article
10055         (car gnus-article-current) (cdr gnus-article-current)))
10056      (when gnus-use-cache
10057        (gnus-cache-update-article
10058         (car gnus-article-current) (cdr gnus-article-current)))))
10059
10060 (put 'gnus-with-article 'lisp-indent-function 1)
10061 (put 'gnus-with-article 'edebug-form-spec '(form body))
10062
10063 ;; Thread-based commands.
10064
10065 (defun gnus-summary-articles-in-thread (&optional article)
10066   "Return a list of all articles in the current thread.
10067 If ARTICLE is non-nil, return all articles in the thread that starts
10068 with that article."
10069   (let* ((article (or article (gnus-summary-article-number)))
10070          (data (gnus-data-find-list article))
10071          (top-level (gnus-data-level (car data)))
10072          (top-subject
10073           (cond ((null gnus-thread-operation-ignore-subject)
10074                  (gnus-simplify-subject-re
10075                   (mail-header-subject (gnus-data-header (car data)))))
10076                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10077                  (gnus-simplify-subject-fuzzy
10078                   (mail-header-subject (gnus-data-header (car data)))))
10079                 (t nil)))
10080          (end-point (save-excursion
10081                       (if (gnus-summary-go-to-next-thread)
10082                           (point) (point-max))))
10083          articles)
10084     (while (and data
10085                 (< (gnus-data-pos (car data)) end-point))
10086       (when (or (not top-subject)
10087                 (string= top-subject
10088                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10089                              (gnus-simplify-subject-fuzzy
10090                               (mail-header-subject
10091                                (gnus-data-header (car data))))
10092                            (gnus-simplify-subject-re
10093                             (mail-header-subject
10094                              (gnus-data-header (car data)))))))
10095         (push (gnus-data-number (car data)) articles))
10096       (unless (and (setq data (cdr data))
10097                    (> (gnus-data-level (car data)) top-level))
10098         (setq data nil)))
10099     ;; Return the list of articles.
10100     (nreverse articles)))
10101
10102 (defun gnus-summary-rethread-current ()
10103   "Rethread the thread the current article is part of."
10104   (interactive)
10105   (let* ((gnus-show-threads t)
10106          (article (gnus-summary-article-number))
10107          (id (mail-header-id (gnus-summary-article-header)))
10108          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10109     (unless id
10110       (error "No article on the current line"))
10111     (gnus-rebuild-thread id)
10112     (gnus-summary-goto-subject article)))
10113
10114 (defun gnus-summary-reparent-thread ()
10115   "Make the current article child of the marked (or previous) article.
10116
10117 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10118 is non-nil or the Subject: of both articles are the same."
10119   (interactive)
10120   (unless (not (gnus-group-read-only-p))
10121     (error "The current newsgroup does not support article editing"))
10122   (unless (<= (length gnus-newsgroup-processable) 1)
10123     (error "No more than one article may be marked"))
10124   (save-window-excursion
10125     (let ((gnus-article-buffer " *reparent*")
10126           (current-article (gnus-summary-article-number))
10127           ;; First grab the marked article, otherwise one line up.
10128           (parent-article (if (not (null gnus-newsgroup-processable))
10129                               (car gnus-newsgroup-processable)
10130                             (save-excursion
10131                               (if (eq (forward-line -1) 0)
10132                                   (gnus-summary-article-number)
10133                                 (error "Beginning of summary buffer"))))))
10134       (unless (not (eq current-article parent-article))
10135         (error "An article may not be self-referential"))
10136       (let ((message-id (mail-header-id
10137                          (gnus-summary-article-header parent-article))))
10138         (unless (and message-id (not (equal message-id "")))
10139           (error "No message-id in desired parent"))
10140         (gnus-with-article current-article
10141           (save-restriction
10142             (goto-char (point-min))
10143             (message-narrow-to-head)
10144             (if (re-search-forward "^References: " nil t)
10145                 (progn
10146                   (re-search-forward "^[^ \t]" nil t)
10147                   (forward-line -1)
10148                   (end-of-line)
10149                   (insert " " message-id))
10150               (insert "References: " message-id "\n"))))
10151         (set-buffer gnus-summary-buffer)
10152         (gnus-summary-unmark-all-processable)
10153         (gnus-summary-update-article current-article)
10154         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10155             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10156         (gnus-summary-rethread-current)
10157         (gnus-message 3 "Article %d is now the child of article %d"
10158                       current-article parent-article)))))
10159
10160 (defun gnus-summary-toggle-threads (&optional arg)
10161   "Toggle showing conversation threads.
10162 If ARG is positive number, turn showing conversation threads on."
10163   (interactive "P")
10164   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10165     (setq gnus-show-threads
10166           (if (null arg) (not gnus-show-threads)
10167             (> (prefix-numeric-value arg) 0)))
10168     (gnus-summary-prepare)
10169     (gnus-summary-goto-subject current)
10170     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10171     (gnus-summary-position-point)))
10172
10173 (defun gnus-summary-show-all-threads ()
10174   "Show all threads."
10175   (interactive)
10176   (save-excursion
10177     (let ((buffer-read-only nil))
10178       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10179   (gnus-summary-position-point))
10180
10181 (defun gnus-summary-show-thread ()
10182   "Show thread subtrees.
10183 Returns nil if no thread was there to be shown."
10184   (interactive)
10185   (let ((buffer-read-only nil)
10186         (orig (point))
10187         ;; first goto end then to beg, to have point at beg after let
10188         (end (progn (end-of-line) (point)))
10189         (beg (progn (beginning-of-line) (point))))
10190     (prog1
10191         ;; Any hidden lines here?
10192         (search-forward "\r" end t)
10193       (subst-char-in-region beg end ?\^M ?\n t)
10194       (goto-char orig)
10195       (gnus-summary-position-point))))
10196
10197 (defun gnus-summary-maybe-hide-threads ()
10198   "If requested, hide the threads that should be hidden."
10199   (when (and gnus-show-threads
10200              gnus-thread-hide-subtree)
10201     (gnus-summary-hide-all-threads
10202      (if (or (consp gnus-thread-hide-subtree)
10203              (gnus-functionp gnus-thread-hide-subtree))
10204          (gnus-make-predicate gnus-thread-hide-subtree)
10205        nil))))
10206
10207 ;;; Hiding predicates.
10208
10209 (defun gnus-article-unread-p (header)
10210   (memq (mail-header-number header) gnus-newsgroup-unreads))
10211
10212 (defun gnus-article-unseen-p (header)
10213   (memq (mail-header-number header) gnus-newsgroup-unseen))
10214
10215 (defun gnus-map-articles (predicate articles)
10216   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10217   (apply 'gnus-or (mapcar predicate
10218                           (mapcar 'gnus-summary-article-header articles))))
10219
10220 (defun gnus-summary-hide-all-threads (&optional predicate)
10221   "Hide all thread subtrees.
10222 If PREDICATE is supplied, threads that satisfy this predicate
10223 will not be hidden."
10224   (interactive)
10225   (save-excursion
10226     (goto-char (point-min))
10227     (let ((end nil))
10228       (while (not end)
10229         (when (or (not predicate)
10230                   (gnus-map-articles
10231                    predicate (gnus-summary-article-children)))
10232             (gnus-summary-hide-thread))
10233         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10234   (gnus-summary-position-point))
10235
10236 (defun gnus-summary-hide-thread ()
10237   "Hide thread subtrees.
10238 If PREDICATE is supplied, threads that satisfy this predicate
10239 will not be hidden.
10240 Returns nil if no threads were there to be hidden."
10241   (interactive)
10242   (let ((buffer-read-only nil)
10243         (start (point))
10244         (article (gnus-summary-article-number)))
10245     (goto-char start)
10246     ;; Go forward until either the buffer ends or the subthread
10247     ;; ends.
10248     (when (and (not (eobp))
10249                (or (zerop (gnus-summary-next-thread 1 t))
10250                    (goto-char (point-max))))
10251       (prog1
10252           (if (and (> (point) start)
10253                    (search-backward "\n" start t))
10254               (progn
10255                 (subst-char-in-region start (point) ?\n ?\^M)
10256                 (gnus-summary-goto-subject article))
10257             (goto-char start)
10258             nil)))))
10259
10260 (defun gnus-summary-go-to-next-thread (&optional previous)
10261   "Go to the same level (or less) next thread.
10262 If PREVIOUS is non-nil, go to previous thread instead.
10263 Return the article number moved to, or nil if moving was impossible."
10264   (let ((level (gnus-summary-thread-level))
10265         (way (if previous -1 1))
10266         (beg (point)))
10267     (forward-line way)
10268     (while (and (not (eobp))
10269                 (< level (gnus-summary-thread-level)))
10270       (forward-line way))
10271     (if (eobp)
10272         (progn
10273           (goto-char beg)
10274           nil)
10275       (setq beg (point))
10276       (prog1
10277           (gnus-summary-article-number)
10278         (goto-char beg)))))
10279
10280 (defun gnus-summary-next-thread (n &optional silent)
10281   "Go to the same level next N'th thread.
10282 If N is negative, search backward instead.
10283 Returns the difference between N and the number of skips actually
10284 done.
10285
10286 If SILENT, don't output messages."
10287   (interactive "p")
10288   (let ((backward (< n 0))
10289         (n (abs n)))
10290     (while (and (> n 0)
10291                 (gnus-summary-go-to-next-thread backward))
10292       (decf n))
10293     (unless silent
10294       (gnus-summary-position-point))
10295     (when (and (not silent) (/= 0 n))
10296       (gnus-message 7 "No more threads"))
10297     n))
10298
10299 (defun gnus-summary-prev-thread (n)
10300   "Go to the same level previous N'th thread.
10301 Returns the difference between N and the number of skips actually
10302 done."
10303   (interactive "p")
10304   (gnus-summary-next-thread (- n)))
10305
10306 (defun gnus-summary-go-down-thread ()
10307   "Go down one level in the current thread."
10308   (let ((children (gnus-summary-article-children)))
10309     (when children
10310       (gnus-summary-goto-subject (car children)))))
10311
10312 (defun gnus-summary-go-up-thread ()
10313   "Go up one level in the current thread."
10314   (let ((parent (gnus-summary-article-parent)))
10315     (when parent
10316       (gnus-summary-goto-subject parent))))
10317
10318 (defun gnus-summary-down-thread (n)
10319   "Go down thread N steps.
10320 If N is negative, go up instead.
10321 Returns the difference between N and how many steps down that were
10322 taken."
10323   (interactive "p")
10324   (let ((up (< n 0))
10325         (n (abs n)))
10326     (while (and (> n 0)
10327                 (if up (gnus-summary-go-up-thread)
10328                   (gnus-summary-go-down-thread)))
10329       (setq n (1- n)))
10330     (gnus-summary-position-point)
10331     (when (/= 0 n)
10332       (gnus-message 7 "Can't go further"))
10333     n))
10334
10335 (defun gnus-summary-up-thread (n)
10336   "Go up thread N steps.
10337 If N is negative, go down instead.
10338 Returns the difference between N and how many steps down that were
10339 taken."
10340   (interactive "p")
10341   (gnus-summary-down-thread (- n)))
10342
10343 (defun gnus-summary-top-thread ()
10344   "Go to the top of the thread."
10345   (interactive)
10346   (while (gnus-summary-go-up-thread))
10347   (gnus-summary-article-number))
10348
10349 (defun gnus-summary-kill-thread (&optional unmark)
10350   "Mark articles under current thread as read.
10351 If the prefix argument is positive, remove any kinds of marks.
10352 If the prefix argument is negative, tick articles instead."
10353   (interactive "P")
10354   (when unmark
10355     (setq unmark (prefix-numeric-value unmark)))
10356   (let ((articles (gnus-summary-articles-in-thread)))
10357     (save-excursion
10358       ;; Expand the thread.
10359       (gnus-summary-show-thread)
10360       ;; Mark all the articles.
10361       (while articles
10362         (gnus-summary-goto-subject (car articles))
10363         (cond ((null unmark)
10364                (gnus-summary-mark-article-as-read gnus-killed-mark))
10365               ((> unmark 0)
10366                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10367               (t
10368                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10369         (setq articles (cdr articles))))
10370     ;; Hide killed subtrees.
10371     (and (null unmark)
10372          gnus-thread-hide-killed
10373          (gnus-summary-hide-thread))
10374     ;; If marked as read, go to next unread subject.
10375     (when (null unmark)
10376       ;; Go to next unread subject.
10377       (gnus-summary-next-subject 1 t)))
10378   (gnus-set-mode-line 'summary))
10379
10380 ;; Summary sorting commands
10381
10382 (defun gnus-summary-sort-by-number (&optional reverse)
10383   "Sort the summary buffer by article number.
10384 Argument REVERSE means reverse order."
10385   (interactive "P")
10386   (gnus-summary-sort 'number reverse))
10387
10388 (defun gnus-summary-sort-by-random (&optional reverse)
10389   "Randomize the order in the summary buffer.
10390 Argument REVERSE means to randomize in reverse order."
10391   (interactive "P")
10392   (gnus-summary-sort 'random reverse))
10393
10394 (defun gnus-summary-sort-by-author (&optional reverse)
10395   "Sort the summary buffer by author name alphabetically.
10396 If `case-fold-search' is non-nil, case of letters is ignored.
10397 Argument REVERSE means reverse order."
10398   (interactive "P")
10399   (gnus-summary-sort 'author reverse))
10400
10401 (defun gnus-summary-sort-by-subject (&optional reverse)
10402   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10403 If `case-fold-search' is non-nil, case of letters is ignored.
10404 Argument REVERSE means reverse order."
10405   (interactive "P")
10406   (gnus-summary-sort 'subject reverse))
10407
10408 (defun gnus-summary-sort-by-date (&optional reverse)
10409   "Sort the summary buffer by date.
10410 Argument REVERSE means reverse order."
10411   (interactive "P")
10412   (gnus-summary-sort 'date reverse))
10413
10414 (defun gnus-summary-sort-by-score (&optional reverse)
10415   "Sort the summary buffer by score.
10416 Argument REVERSE means reverse order."
10417   (interactive "P")
10418   (gnus-summary-sort 'score reverse))
10419
10420 (defun gnus-summary-sort-by-lines (&optional reverse)
10421   "Sort the summary buffer by the number of lines.
10422 Argument REVERSE means reverse order."
10423   (interactive "P")
10424   (gnus-summary-sort 'lines reverse))
10425
10426 (defun gnus-summary-sort-by-chars (&optional reverse)
10427   "Sort the summary buffer by article length.
10428 Argument REVERSE means reverse order."
10429   (interactive "P")
10430   (gnus-summary-sort 'chars reverse))
10431
10432 (defun gnus-summary-sort-by-original (&optional reverse)
10433   "Sort the summary buffer using the default sorting method.
10434 Argument REVERSE means reverse order."
10435   (interactive "P")
10436   (let* ((buffer-read-only)
10437          (gnus-summary-prepare-hook nil))
10438     ;; We do the sorting by regenerating the threads.
10439     (gnus-summary-prepare)
10440     ;; Hide subthreads if needed.
10441     (gnus-summary-maybe-hide-threads)))
10442
10443 (defun gnus-summary-sort (predicate reverse)
10444   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10445   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10446          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10447          (gnus-thread-sort-functions
10448           (if (not reverse)
10449               thread
10450             `(lambda (t1 t2)
10451                (,thread t2 t1))))
10452          (gnus-sort-gathered-threads-function
10453           gnus-thread-sort-functions)
10454          (gnus-article-sort-functions
10455           (if (not reverse)
10456               article
10457             `(lambda (t1 t2)
10458                (,article t2 t1))))
10459          (buffer-read-only)
10460          (gnus-summary-prepare-hook nil))
10461     ;; We do the sorting by regenerating the threads.
10462     (gnus-summary-prepare)
10463     ;; Hide subthreads if needed.
10464     (gnus-summary-maybe-hide-threads)))
10465
10466 ;; Summary saving commands.
10467
10468 (defun gnus-summary-save-article (&optional n not-saved)
10469   "Save the current article using the default saver function.
10470 If N is a positive number, save the N next articles.
10471 If N is a negative number, save the N previous articles.
10472 If N is nil and any articles have been marked with the process mark,
10473 save those articles instead.
10474 The variable `gnus-default-article-saver' specifies the saver function."
10475   (interactive "P")
10476   (let* ((articles (gnus-summary-work-articles n))
10477          (save-buffer (save-excursion
10478                         (nnheader-set-temp-buffer " *Gnus Save*")))
10479          (num (length articles))
10480          header file)
10481     (dolist (article articles)
10482       (setq header (gnus-summary-article-header article))
10483       (if (not (vectorp header))
10484           ;; This is a pseudo-article.
10485           (if (assq 'name header)
10486               (gnus-copy-file (cdr (assq 'name header)))
10487             (gnus-message 1 "Article %d is unsaveable" article))
10488         ;; This is a real article.
10489         (save-window-excursion
10490           (let ((gnus-display-mime-function nil)
10491                 (gnus-article-prepare-hook nil))
10492             (gnus-summary-select-article t nil nil article)))
10493         (save-excursion
10494           (set-buffer save-buffer)
10495           (erase-buffer)
10496           (insert-buffer-substring gnus-original-article-buffer))
10497         (setq file (gnus-article-save save-buffer file num))
10498         (gnus-summary-remove-process-mark article)
10499         (unless not-saved
10500           (gnus-summary-set-saved-mark article))))
10501     (gnus-kill-buffer save-buffer)
10502     (gnus-summary-position-point)
10503     (gnus-set-mode-line 'summary)
10504     n))
10505
10506 (defun gnus-summary-pipe-output (&optional arg headers)
10507   "Pipe the current article to a subprocess.
10508 If N is a positive number, pipe the N next articles.
10509 If N is a negative number, pipe the N previous articles.
10510 If N is nil and any articles have been marked with the process mark,
10511 pipe those articles instead.
10512 If HEADERS (the symbolic prefix), include the headers, too."
10513   (interactive (gnus-interactive "P\ny"))
10514   (require 'gnus-art)
10515   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10516         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10517     (gnus-summary-save-article arg t))
10518   (let ((buffer (get-buffer "*Shell Command Output*")))
10519     (when (and buffer
10520                (not (zerop (buffer-size buffer))))
10521       (gnus-configure-windows 'pipe))))
10522
10523 (defun gnus-summary-save-article-mail (&optional arg)
10524   "Append the current article to an mail file.
10525 If N is a positive number, save the N next articles.
10526 If N is a negative number, save the N previous articles.
10527 If N is nil and any articles have been marked with the process mark,
10528 save those articles instead."
10529   (interactive "P")
10530   (require 'gnus-art)
10531   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10532     (gnus-summary-save-article arg)))
10533
10534 (defun gnus-summary-save-article-rmail (&optional arg)
10535   "Append the current article to an rmail file.
10536 If N is a positive number, save the N next articles.
10537 If N is a negative number, save the N previous articles.
10538 If N is nil and any articles have been marked with the process mark,
10539 save those articles instead."
10540   (interactive "P")
10541   (require 'gnus-art)
10542   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10543     (gnus-summary-save-article arg)))
10544
10545 (defun gnus-summary-save-article-file (&optional arg)
10546   "Append the current article to a file.
10547 If N is a positive number, save the N next articles.
10548 If N is a negative number, save the N previous articles.
10549 If N is nil and any articles have been marked with the process mark,
10550 save those articles instead."
10551   (interactive "P")
10552   (require 'gnus-art)
10553   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10554     (gnus-summary-save-article arg)))
10555
10556 (defun gnus-summary-write-article-file (&optional arg)
10557   "Write the current article to a file, deleting the previous file.
10558 If N is a positive number, save the N next articles.
10559 If N is a negative number, save the N previous articles.
10560 If N is nil and any articles have been marked with the process mark,
10561 save those articles instead."
10562   (interactive "P")
10563   (require 'gnus-art)
10564   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10565     (gnus-summary-save-article arg)))
10566
10567 (defun gnus-summary-save-article-body-file (&optional arg)
10568   "Append the current article body to a file.
10569 If N is a positive number, save the N next articles.
10570 If N is a negative number, save the N previous articles.
10571 If N is nil and any articles have been marked with the process mark,
10572 save those articles instead."
10573   (interactive "P")
10574   (require 'gnus-art)
10575   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10576     (gnus-summary-save-article arg)))
10577
10578 (defun gnus-summary-muttprint (&optional arg)
10579   "Print the current article using Muttprint.
10580 If N is a positive number, save the N next articles.
10581 If N is a negative number, save the N previous articles.
10582 If N is nil and any articles have been marked with the process mark,
10583 save those articles instead."
10584   (interactive "P")
10585   (require 'gnus-art)
10586   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10587     (gnus-summary-save-article arg t)))
10588
10589 (defun gnus-summary-pipe-message (program)
10590   "Pipe the current article through PROGRAM."
10591   (interactive "sProgram: ")
10592   (gnus-summary-select-article)
10593   (let ((mail-header-separator ""))
10594     (gnus-eval-in-buffer-window gnus-article-buffer
10595       (save-restriction
10596         (widen)
10597         (let ((start (window-start))
10598               buffer-read-only)
10599           (message-pipe-buffer-body program)
10600           (set-window-start (get-buffer-window (current-buffer)) start))))))
10601
10602 (defun gnus-get-split-value (methods)
10603   "Return a value based on the split METHODS."
10604   (let (split-name method result match)
10605     (when methods
10606       (save-excursion
10607         (set-buffer gnus-original-article-buffer)
10608         (save-restriction
10609           (nnheader-narrow-to-headers)
10610           (while (and methods (not split-name))
10611             (goto-char (point-min))
10612             (setq method (pop methods))
10613             (setq match (car method))
10614             (when (cond
10615                    ((stringp match)
10616                     ;; Regular expression.
10617                     (ignore-errors
10618                       (re-search-forward match nil t)))
10619                    ((gnus-functionp match)
10620                     ;; Function.
10621                     (save-restriction
10622                       (widen)
10623                       (setq result (funcall match gnus-newsgroup-name))))
10624                    ((consp match)
10625                     ;; Form.
10626                     (save-restriction
10627                       (widen)
10628                       (setq result (eval match)))))
10629               (setq split-name (cdr method))
10630               (cond ((stringp result)
10631                      (push (expand-file-name
10632                             result gnus-article-save-directory)
10633                            split-name))
10634                     ((consp result)
10635                      (setq split-name (append result split-name)))))))))
10636     (nreverse split-name)))
10637
10638 (defun gnus-valid-move-group-p (group)
10639   (and (boundp group)
10640        (symbol-name group)
10641        (symbol-value group)
10642        (gnus-get-function (gnus-find-method-for-group
10643                            (symbol-name group)) 'request-accept-article t)))
10644
10645 (defun gnus-read-move-group-name (prompt default articles prefix)
10646   "Read a group name."
10647   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10648          (minibuffer-confirm-incomplete nil) ; XEmacs
10649          (prom
10650           (format "%s %s to:"
10651                   prompt
10652                   (if (> (length articles) 1)
10653                       (format "these %d articles" (length articles))
10654                     "this article")))
10655          (to-newsgroup
10656           (cond
10657            ((null split-name)
10658             (gnus-completing-read-with-default
10659              default prom
10660              gnus-active-hashtb
10661              'gnus-valid-move-group-p
10662              nil prefix
10663              'gnus-group-history))
10664            ((= 1 (length split-name))
10665             (gnus-completing-read-with-default
10666              (car split-name) prom
10667              gnus-active-hashtb
10668              'gnus-valid-move-group-p
10669              nil nil
10670              'gnus-group-history))
10671            (t
10672             (gnus-completing-read-with-default
10673              nil prom
10674              (mapcar (lambda (el) (list el))
10675                      (nreverse split-name))
10676              nil nil nil
10677              'gnus-group-history))))
10678          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10679     (when to-newsgroup
10680       (if (or (string= to-newsgroup "")
10681               (string= to-newsgroup prefix))
10682           (setq to-newsgroup default))
10683       (unless to-newsgroup
10684         (error "No group name entered"))
10685       (or (gnus-active to-newsgroup)
10686           (gnus-activate-group to-newsgroup nil nil to-method)
10687           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10688                                      to-newsgroup))
10689               (or (and (gnus-request-create-group to-newsgroup to-method)
10690                        (gnus-activate-group
10691                         to-newsgroup nil nil to-method)
10692                        (gnus-subscribe-group to-newsgroup))
10693                   (error "Couldn't create group %s" to-newsgroup)))
10694           (error "No such group: %s" to-newsgroup)))
10695     to-newsgroup))
10696
10697 (defun gnus-summary-save-parts (type dir n &optional reverse)
10698   "Save parts matching TYPE to DIR.
10699 If REVERSE, save parts that do not match TYPE."
10700   (interactive
10701    (list (read-string "Save parts of type: "
10702                       (or (car gnus-summary-save-parts-type-history)
10703                           gnus-summary-save-parts-default-mime)
10704                       'gnus-summary-save-parts-type-history)
10705          (setq gnus-summary-save-parts-last-directory
10706                (read-file-name "Save to directory: "
10707                                gnus-summary-save-parts-last-directory
10708                                nil t))
10709          current-prefix-arg))
10710   (gnus-summary-iterate n
10711     (let ((gnus-display-mime-function nil)
10712           (gnus-inhibit-treatment t))
10713       (gnus-summary-select-article))
10714     (save-excursion
10715       (set-buffer gnus-article-buffer)
10716       (let ((handles (or gnus-article-mime-handles
10717                          (mm-dissect-buffer nil gnus-article-loose-mime)
10718                          (mm-uu-dissect))))
10719         (when handles
10720           (gnus-summary-save-parts-1 type dir handles reverse)
10721           (unless gnus-article-mime-handles ;; Don't destroy this case.
10722             (mm-destroy-parts handles)))))))
10723
10724 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10725   (if (stringp (car handle))
10726       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10727               (cdr handle))
10728     (when (if reverse
10729               (not (string-match type (mm-handle-media-type handle)))
10730             (string-match type (mm-handle-media-type handle)))
10731       (let ((file (expand-file-name
10732                    (file-name-nondirectory
10733                     (or
10734                      (mail-content-type-get
10735                       (mm-handle-disposition handle) 'filename)
10736                      (concat gnus-newsgroup-name
10737                              "." (number-to-string
10738                                   (cdr gnus-article-current)))))
10739                    dir)))
10740         (unless (file-exists-p file)
10741           (mm-save-part-to-file handle file))))))
10742
10743 ;; Summary extract commands
10744
10745 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10746   (let ((buffer-read-only nil)
10747         (article (gnus-summary-article-number))
10748         after-article b e)
10749     (unless (gnus-summary-goto-subject article)
10750       (error "No such article: %d" article))
10751     (gnus-summary-position-point)
10752     ;; If all commands are to be bunched up on one line, we collect
10753     ;; them here.
10754     (unless gnus-view-pseudos-separately
10755       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10756             files action)
10757         (while ps
10758           (setq action (cdr (assq 'action (car ps))))
10759           (setq files (list (cdr (assq 'name (car ps)))))
10760           (while (and ps (cdr ps)
10761                       (string= (or action "1")
10762                                (or (cdr (assq 'action (cadr ps))) "2")))
10763             (push (cdr (assq 'name (cadr ps))) files)
10764             (setcdr ps (cddr ps)))
10765           (when files
10766             (when (not (string-match "%s" action))
10767               (push " " files))
10768             (push " " files)
10769             (when (assq 'execute (car ps))
10770               (setcdr (assq 'execute (car ps))
10771                       (funcall (if (string-match "%s" action)
10772                                    'format 'concat)
10773                                action
10774                                (mapconcat
10775                                 (lambda (f)
10776                                   (if (equal f " ")
10777                                       f
10778                                     (mm-quote-arg f)))
10779                                 files " ")))))
10780           (setq ps (cdr ps)))))
10781     (if (and gnus-view-pseudos (not not-view))
10782         (while pslist
10783           (when (assq 'execute (car pslist))
10784             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10785                                   (eq gnus-view-pseudos 'not-confirm)))
10786           (setq pslist (cdr pslist)))
10787       (save-excursion
10788         (while pslist
10789           (setq after-article (or (cdr (assq 'article (car pslist)))
10790                                   (gnus-summary-article-number)))
10791           (gnus-summary-goto-subject after-article)
10792           (forward-line 1)
10793           (setq b (point))
10794           (insert "    " (file-name-nondirectory
10795                           (cdr (assq 'name (car pslist))))
10796                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10797           (setq e (point))
10798           (forward-line -1)             ; back to `b'
10799           (gnus-add-text-properties
10800            b (1- e) (list 'gnus-number gnus-reffed-article-number
10801                           gnus-mouse-face-prop gnus-mouse-face))
10802           (gnus-data-enter
10803            after-article gnus-reffed-article-number
10804            gnus-unread-mark b (car pslist) 0 (- e b))
10805           (setq gnus-newsgroup-unreads
10806                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10807                                          gnus-reffed-article-number))
10808           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10809           (setq pslist (cdr pslist)))))))
10810
10811 (defun gnus-pseudos< (p1 p2)
10812   (let ((c1 (cdr (assq 'action p1)))
10813         (c2 (cdr (assq 'action p2))))
10814     (and c1 c2 (string< c1 c2))))
10815
10816 (defun gnus-request-pseudo-article (props)
10817   (cond ((assq 'execute props)
10818          (gnus-execute-command (cdr (assq 'execute props)))))
10819   (let ((gnus-current-article (gnus-summary-article-number)))
10820     (gnus-run-hooks 'gnus-mark-article-hook)))
10821
10822 (defun gnus-execute-command (command &optional automatic)
10823   (save-excursion
10824     (gnus-article-setup-buffer)
10825     (set-buffer gnus-article-buffer)
10826     (setq buffer-read-only nil)
10827     (let ((command (if automatic command
10828                      (read-string "Command: " (cons command 0)))))
10829       (erase-buffer)
10830       (insert "$ " command "\n\n")
10831       (if gnus-view-pseudo-asynchronously
10832           (start-process "gnus-execute" (current-buffer) shell-file-name
10833                          shell-command-switch command)
10834         (call-process shell-file-name nil t nil
10835                       shell-command-switch command)))))
10836
10837 ;; Summary kill commands.
10838
10839 (defun gnus-summary-edit-global-kill (article)
10840   "Edit the \"global\" kill file."
10841   (interactive (list (gnus-summary-article-number)))
10842   (gnus-group-edit-global-kill article))
10843
10844 (defun gnus-summary-edit-local-kill ()
10845   "Edit a local kill file applied to the current newsgroup."
10846   (interactive)
10847   (setq gnus-current-headers (gnus-summary-article-header))
10848   (gnus-group-edit-local-kill
10849    (gnus-summary-article-number) gnus-newsgroup-name))
10850
10851 ;;; Header reading.
10852
10853 (defun gnus-read-header (id &optional header)
10854   "Read the headers of article ID and enter them into the Gnus system."
10855   (let ((group gnus-newsgroup-name)
10856         (gnus-override-method
10857          (or
10858           gnus-override-method
10859           (and (gnus-news-group-p gnus-newsgroup-name)
10860                (car (gnus-refer-article-methods)))))
10861         where)
10862     ;; First we check to see whether the header in question is already
10863     ;; fetched.
10864     (if (stringp id)
10865         ;; This is a Message-ID.
10866         (setq header (or header (gnus-id-to-header id)))
10867       ;; This is an article number.
10868       (setq header (or header (gnus-summary-article-header id))))
10869     (if (and header
10870              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10871         ;; We have found the header.
10872         header
10873       ;; If this is a sparse article, we have to nix out its
10874       ;; previous entry in the thread hashtb.
10875       (when (and header
10876                  (gnus-summary-article-sparse-p (mail-header-number header)))
10877         (let* ((parent (gnus-parent-id (mail-header-references header)))
10878                (thread (and parent (gnus-id-to-thread parent))))
10879           (when thread
10880             (delq (assq header thread) thread))))
10881       ;; We have to really fetch the header to this article.
10882       (save-excursion
10883         (set-buffer nntp-server-buffer)
10884         (when (setq where (gnus-request-head id group))
10885           (nnheader-fold-continuation-lines)
10886           (goto-char (point-max))
10887           (insert ".\n")
10888           (goto-char (point-min))
10889           (insert "211 ")
10890           (princ (cond
10891                   ((numberp id) id)
10892                   ((cdr where) (cdr where))
10893                   (header (mail-header-number header))
10894                   (t gnus-reffed-article-number))
10895                  (current-buffer))
10896           (insert " Article retrieved.\n"))
10897         (if (or (not where)
10898                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10899             ()                          ; Malformed head.
10900           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10901             (when (and (stringp id)
10902                        (not (string= (gnus-group-real-name group)
10903                                      (car where))))
10904               ;; If we fetched by Message-ID and the article came
10905               ;; from a different group, we fudge some bogus article
10906               ;; numbers for this article.
10907               (mail-header-set-number header gnus-reffed-article-number))
10908             (save-excursion
10909               (set-buffer gnus-summary-buffer)
10910               (decf gnus-reffed-article-number)
10911               (gnus-remove-header (mail-header-number header))
10912               (push header gnus-newsgroup-headers)
10913               (setq gnus-current-headers header)
10914               (push (mail-header-number header) gnus-newsgroup-limit)))
10915           header)))))
10916
10917 (defun gnus-remove-header (number)
10918   "Remove header NUMBER from `gnus-newsgroup-headers'."
10919   (if (and gnus-newsgroup-headers
10920            (= number (mail-header-number (car gnus-newsgroup-headers))))
10921       (pop gnus-newsgroup-headers)
10922     (let ((headers gnus-newsgroup-headers))
10923       (while (and (cdr headers)
10924                   (not (= number (mail-header-number (cadr headers)))))
10925         (pop headers))
10926       (when (cdr headers)
10927         (setcdr headers (cddr headers))))))
10928
10929 ;;;
10930 ;;; summary highlights
10931 ;;;
10932
10933 (defun gnus-highlight-selected-summary ()
10934   "Highlight selected article in summary buffer."
10935   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10936   (when gnus-summary-selected-face
10937     (save-excursion
10938       (let* ((beg (progn (beginning-of-line) (point)))
10939              (end (progn (end-of-line) (point)))
10940              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10941              (from (if (get-text-property beg gnus-mouse-face-prop)
10942                        beg
10943                      (or (next-single-property-change
10944                           beg gnus-mouse-face-prop nil end)
10945                          beg)))
10946              (to
10947               (if (= from end)
10948                   (- from 2)
10949                 (or (next-single-property-change
10950                      from gnus-mouse-face-prop nil end)
10951                     end))))
10952         ;; If no mouse-face prop on line we will have to = from = end,
10953         ;; so we highlight the entire line instead.
10954         (when (= (+ to 2) from)
10955           (setq from beg)
10956           (setq to end))
10957         (if gnus-newsgroup-selected-overlay
10958             ;; Move old overlay.
10959             (gnus-move-overlay
10960              gnus-newsgroup-selected-overlay from to (current-buffer))
10961           ;; Create new overlay.
10962           (gnus-overlay-put
10963            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10964            'face gnus-summary-selected-face))))))
10965
10966 (defvar gnus-summary-highlight-line-cached nil)
10967 (defvar gnus-summary-highlight-line-trigger nil)
10968
10969 (defun gnus-summary-highlight-line-0 ()
10970   (if (and (eq gnus-summary-highlight-line-trigger 
10971                gnus-summary-highlight)
10972            gnus-summary-highlight-line-cached)
10973       gnus-summary-highlight-line-cached
10974     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
10975           gnus-summary-highlight-line-cached
10976           (let* ((cond (list 'cond))
10977                  (c cond)
10978                  (list gnus-summary-highlight))
10979             (while list
10980               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
10981               (setq c (cdr c)
10982                     list (cdr list)))
10983             (gnus-byte-compile (list 'lambda nil cond))))))
10984
10985 (defun gnus-summary-highlight-line ()
10986   "Highlight current line according to `gnus-summary-highlight'."
10987   (let* ((beg (gnus-point-at-bol))
10988          (article (or (gnus-summary-article-number) gnus-current-article))
10989          (score (or (cdr (assq article
10990                                gnus-newsgroup-scored))
10991                     gnus-summary-default-score 0))
10992          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10993          (inhibit-read-only t)
10994          (default gnus-summary-default-score)
10995          (default-high gnus-summary-default-high-score)
10996          (default-low gnus-summary-default-low-score)
10997          (uncached (memq article gnus-newsgroup-undownloaded))
10998          (downloaded (not uncached)))
10999     (let ((face (funcall (gnus-summary-highlight-line-0))))
11000       (unless (eq face (get-text-property beg 'face))
11001         (gnus-put-text-property-excluding-characters-with-faces
11002          beg (gnus-point-at-eol) 'face
11003          (setq face (if (boundp face) (symbol-value face) face)))
11004         (when gnus-summary-highlight-line-function
11005           (funcall gnus-summary-highlight-line-function article face))))))
11006
11007 (defun gnus-update-read-articles (group unread &optional compute)
11008   "Update the list of read articles in GROUP.
11009 UNREAD is a sorted list."
11010   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11011          (entry (gnus-gethash group gnus-newsrc-hashtb))
11012          (info (nth 2 entry))
11013          (prev 1)
11014          read)
11015     (if (or (not info) (not active))
11016         ;; There is no info on this group if it was, in fact,
11017         ;; killed.  Gnus stores no information on killed groups, so
11018         ;; there's nothing to be done.
11019         ;; One could store the information somewhere temporarily,
11020         ;; perhaps...  Hmmm...
11021         ()
11022       ;; Remove any negative articles numbers.
11023       (while (and unread (< (car unread) 0))
11024         (setq unread (cdr unread)))
11025       ;; Remove any expired article numbers
11026       (while (and unread (< (car unread) (car active)))
11027         (setq unread (cdr unread)))
11028       ;; Compute the ranges of read articles by looking at the list of
11029       ;; unread articles.
11030       (while unread
11031         (when (/= (car unread) prev)
11032           (push (if (= prev (1- (car unread))) prev
11033                   (cons prev (1- (car unread))))
11034                 read))
11035         (setq prev (1+ (car unread)))
11036         (setq unread (cdr unread)))
11037       (when (<= prev (cdr active))
11038         (push (cons prev (cdr active)) read))
11039       (setq read (if (> (length read) 1) (nreverse read) read))
11040       (if compute
11041           read
11042         (save-excursion
11043           (let (setmarkundo)
11044             ;; Propagate the read marks to the backend.
11045             (when (gnus-check-backend-function 'request-set-mark group)
11046               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11047                     (add (gnus-remove-from-range read (gnus-info-read info))))
11048                 (when (or add del)
11049                   (unless (gnus-check-group group)
11050                     (error "Can't open server for %s" group))
11051                   (gnus-request-set-mark
11052                    group (delq nil (list (if add (list add 'add '(read)))
11053                                          (if del (list del 'del '(read))))))
11054                   (setq setmarkundo
11055                         `(gnus-request-set-mark
11056                           ,group
11057                           ',(delq nil (list
11058                                        (if del (list del 'add '(read)))
11059                                        (if add (list add 'del '(read))))))))))
11060             (set-buffer gnus-group-buffer)
11061             (gnus-undo-register
11062               `(progn
11063                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11064                  (gnus-info-set-read ',info ',(gnus-info-read info))
11065                  (gnus-get-unread-articles-in-group ',info
11066                                                     (gnus-active ,group))
11067                  (gnus-group-update-group ,group t)
11068                  ,setmarkundo))))
11069         ;; Enter this list into the group info.
11070         (gnus-info-set-read info read)
11071         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11072         (gnus-get-unread-articles-in-group info (gnus-active group))
11073         t))))
11074
11075 (defun gnus-offer-save-summaries ()
11076   "Offer to save all active summary buffers."
11077   (let (buffers)
11078     ;; Go through all buffers and find all summaries.
11079     (dolist (buffer (buffer-list))
11080       (when (and (setq buffer (buffer-name buffer))
11081                  (string-match "Summary" buffer)
11082                  (save-excursion
11083                    (set-buffer buffer)
11084                    ;; We check that this is, indeed, a summary buffer.
11085                    (and (eq major-mode 'gnus-summary-mode)
11086                         ;; Also make sure this isn't bogus.
11087                         gnus-newsgroup-prepared
11088                         ;; Also make sure that this isn't a
11089                         ;; dead summary buffer.
11090                         (not gnus-dead-summary-mode))))
11091         (push buffer buffers)))
11092     ;; Go through all these summary buffers and offer to save them.
11093     (when buffers
11094       (save-excursion
11095         (map-y-or-n-p
11096          "Update summary buffer %s? "
11097          (lambda (buf)
11098            (switch-to-buffer buf)
11099            (gnus-summary-exit))
11100          buffers)))))
11101
11102 (defun gnus-summary-setup-default-charset ()
11103   "Setup newsgroup default charset."
11104   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11105       (setq gnus-newsgroup-charset nil)
11106     (let* ((ignored-charsets
11107             (or gnus-newsgroup-ephemeral-ignored-charsets
11108                 (append
11109                  (and gnus-newsgroup-name
11110                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11111                  gnus-newsgroup-ignored-charsets))))
11112       (setq gnus-newsgroup-charset
11113             (or gnus-newsgroup-ephemeral-charset
11114                 (and gnus-newsgroup-name
11115                      (gnus-parameter-charset gnus-newsgroup-name))
11116                 gnus-default-charset))
11117       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11118            ignored-charsets))))
11119
11120 ;;;
11121 ;;; Mime Commands
11122 ;;;
11123
11124 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11125   "Display the current article buffer fully MIME-buttonized.
11126 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11127 treated as multipart/mixed."
11128   (interactive "P")
11129   (require 'gnus-art)
11130   (let ((gnus-unbuttonized-mime-types nil)
11131         (gnus-mime-display-multipart-as-mixed show-all-parts))
11132     (gnus-summary-show-article)))
11133
11134 (defun gnus-summary-repair-multipart (article)
11135   "Add a Content-Type header to a multipart article without one."
11136   (interactive (list (gnus-summary-article-number)))
11137   (gnus-with-article article
11138     (message-narrow-to-head)
11139     (message-remove-header "Mime-Version")
11140     (goto-char (point-max))
11141     (insert "Mime-Version: 1.0\n")
11142     (widen)
11143     (when (search-forward "\n--" nil t)
11144       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11145         (message-narrow-to-head)
11146         (message-remove-header "Content-Type")
11147         (goto-char (point-max))
11148         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11149                         separator))
11150         (widen))))
11151   (let (gnus-mark-article-hook)
11152     (gnus-summary-select-article t t nil article)))
11153
11154 (defun gnus-summary-toggle-display-buttonized ()
11155   "Toggle the buttonizing of the article buffer."
11156   (interactive)
11157   (require 'gnus-art)
11158   (if (setq gnus-inhibit-mime-unbuttonizing
11159             (not gnus-inhibit-mime-unbuttonizing))
11160       (let ((gnus-unbuttonized-mime-types nil))
11161         (gnus-summary-show-article))
11162     (gnus-summary-show-article)))
11163
11164 ;;;
11165 ;;; Generic summary marking commands
11166 ;;;
11167
11168 (defvar gnus-summary-marking-alist
11169   '((read gnus-del-mark "d")
11170     (unread gnus-unread-mark "u")
11171     (ticked gnus-ticked-mark "!")
11172     (dormant gnus-dormant-mark "?")
11173     (expirable gnus-expirable-mark "e"))
11174   "An alist of names/marks/keystrokes.")
11175
11176 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11177 (defvar gnus-summary-mark-map)
11178
11179 (defun gnus-summary-make-all-marking-commands ()
11180   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11181   (dolist (elem gnus-summary-marking-alist)
11182     (apply 'gnus-summary-make-marking-command elem)))
11183
11184 (defun gnus-summary-make-marking-command (name mark keystroke)
11185   (let ((map (make-sparse-keymap)))
11186     (define-key gnus-summary-generic-mark-map keystroke map)
11187     (dolist (lway `((next "next" next nil "n")
11188                     (next-unread "next unread" next t "N")
11189                     (prev "previous" prev nil "p")
11190                     (prev-unread "previous unread" prev t "P")
11191                     (nomove "" nil nil ,keystroke)))
11192       (let ((func (gnus-summary-make-marking-command-1
11193                    mark (car lway) lway name)))
11194         (setq func (eval func))
11195         (define-key map (nth 4 lway) func)))))
11196
11197 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11198   `(defun ,(intern
11199             (format "gnus-summary-put-mark-as-%s%s"
11200                     name (if (eq way 'nomove)
11201                              ""
11202                            (concat "-" (symbol-name way)))))
11203      (n)
11204      ,(format
11205        "Mark the current article as %s%s.
11206 If N, the prefix, then repeat N times.
11207 If N is negative, move in reverse order.
11208 The difference between N and the actual number of articles marked is
11209 returned."
11210        name (cadr lway))
11211      (interactive "p")
11212      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11213
11214 (defun gnus-summary-generic-mark (n mark move unread)
11215   "Mark N articles with MARK."
11216   (unless (eq major-mode 'gnus-summary-mode)
11217     (error "This command can only be used in the summary buffer"))
11218   (gnus-summary-show-thread)
11219   (let ((nummove
11220          (cond
11221           ((eq move 'next) 1)
11222           ((eq move 'prev) -1)
11223           (t 0))))
11224     (if (zerop nummove)
11225         (setq n 1)
11226       (when (< n 0)
11227         (setq n (abs n)
11228               nummove (* -1 nummove))))
11229     (while (and (> n 0)
11230                 (gnus-summary-mark-article nil mark)
11231                 (zerop (gnus-summary-next-subject nummove unread t)))
11232       (setq n (1- n)))
11233     (when (/= 0 n)
11234       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11235     (gnus-summary-recenter)
11236     (gnus-summary-position-point)
11237     (gnus-set-mode-line 'summary)
11238     n))
11239
11240 (defun gnus-summary-insert-articles (articles)
11241   (when (setq articles
11242               (gnus-sorted-difference articles
11243                                       (mapcar (lambda (h)
11244                                                 (mail-header-number h))
11245                                               gnus-newsgroup-headers)))
11246     (setq gnus-newsgroup-headers
11247           (merge 'list
11248                  gnus-newsgroup-headers
11249                  (gnus-fetch-headers articles)
11250                  'gnus-article-sort-by-number))
11251     ;; Suppress duplicates?
11252     (when gnus-suppress-duplicates
11253       (gnus-dup-suppress-articles))
11254
11255     ;; We might want to build some more threads first.
11256     (when (and gnus-fetch-old-headers
11257                (eq gnus-headers-retrieved-by 'nov))
11258       (if (eq gnus-fetch-old-headers 'invisible)
11259           (gnus-build-all-threads)
11260         (gnus-build-old-threads)))
11261     ;; Let the Gnus agent mark articles as read.
11262     (when gnus-agent
11263       (gnus-agent-get-undownloaded-list))
11264     ;; Remove list identifiers from subject
11265     (when gnus-list-identifiers
11266       (gnus-summary-remove-list-identifiers))
11267     ;; First and last article in this newsgroup.
11268     (when gnus-newsgroup-headers
11269       (setq gnus-newsgroup-begin
11270             (mail-header-number (car gnus-newsgroup-headers))
11271             gnus-newsgroup-end
11272             (mail-header-number
11273              (gnus-last-element gnus-newsgroup-headers))))
11274     (when gnus-use-scoring
11275       (gnus-possibly-score-headers))))
11276
11277 (defun gnus-summary-insert-old-articles (&optional all)
11278   "Insert all old articles in this group.
11279 If ALL is non-nil, already read articles become readable.
11280 If ALL is a number, fetch this number of articles."
11281   (interactive "P")
11282   (prog1
11283       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11284             older len)
11285         (setq older
11286               ;; Some nntp servers lie about their active range.  When
11287               ;; this happens, the active range can be in the millions.
11288               ;; Use a compressed range to avoid creating a huge list.
11289               (gnus-range-difference (list gnus-newsgroup-active) old))
11290         (setq len (gnus-range-length older))
11291         (cond
11292          ((null older) nil)
11293          ((numberp all)
11294           (if (< all len)
11295               (let ((older-range (nreverse older)))
11296                 (setq older nil)
11297
11298                 (while (> all 0)
11299                   (let* ((r (pop older-range))
11300                          (min (if (numberp r) r (car r)))
11301                          (max (if (numberp r) r (cdr r))))
11302                     (while (and (<= min max)
11303                                 (> all 0))
11304                       (push max older)
11305                       (setq all (1- all)
11306                             max (1- max))))))
11307             (setq older (gnus-uncompress-range older))))
11308          (all
11309           (setq older (gnus-uncompress-range older)))
11310          (t
11311           (when (and (numberp gnus-large-newsgroup)
11312                    (> len gnus-large-newsgroup))
11313               (let* ((cursor-in-echo-area nil)
11314                      (initial (gnus-parameter-large-newsgroup-initial
11315                                gnus-newsgroup-name))
11316                      (input
11317                       (read-string
11318                        (format
11319                         "How many articles from %s (%s %d): "
11320                         (gnus-limit-string
11321                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11322                         (if initial "max" "default")
11323                         len)
11324                        (if initial
11325                            (cons (number-to-string initial)
11326                                  0)))))
11327                 (unless (string-match "^[ \t]*$" input)
11328                   (setq all (string-to-number input))
11329                   (if (< all len)
11330                       (let ((older-range (nreverse older)))
11331                         (setq older nil)
11332
11333                         (while (> all 0)
11334                           (let* ((r (pop older-range))
11335                                  (min (if (numberp r) r (car r)))
11336                                  (max (if (numberp r) r (cdr r))))
11337                             (while (and (<= min max)
11338                                         (> all 0))
11339                               (push max older)
11340                               (setq all (1- all)
11341                                     max (1- max))))))))))
11342           (setq older (gnus-uncompress-range older))))
11343         (if (not older)
11344             (message "No old news.")
11345           (gnus-summary-insert-articles older)
11346           (gnus-summary-limit (gnus-sorted-nunion old older))))
11347     (gnus-summary-position-point)))
11348
11349 (defun gnus-summary-insert-new-articles ()
11350   "Insert all new articles in this group."
11351   (interactive)
11352   (prog1
11353       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11354             (old-active gnus-newsgroup-active)
11355             (nnmail-fetched-sources (list t))
11356             i new)
11357         (setq gnus-newsgroup-active
11358               (gnus-activate-group gnus-newsgroup-name 'scan))
11359         (setq i (cdr gnus-newsgroup-active))
11360         (while (> i (cdr old-active))
11361           (push i new)
11362           (decf i))
11363         (if (not new)
11364             (message "No gnus is bad news.")
11365           (gnus-summary-insert-articles new)
11366           (setq gnus-newsgroup-unreads
11367                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11368           (gnus-summary-limit (gnus-sorted-nunion old new))))
11369     (gnus-summary-position-point)))
11370
11371 (gnus-summary-make-all-marking-commands)
11372
11373 (gnus-ems-redefine)
11374
11375 (provide 'gnus-sum)
11376
11377 (run-hooks 'gnus-sum-load-hook)
11378
11379 ;;; gnus-sum.el ends here