1e6c331882479da8a6dc2b96067897ed4c1e9713
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46
47 (defcustom gnus-kill-summary-on-exit t
48   "*If non-nil, kill the summary buffer when you exit from it.
49 If nil, the summary will become a \"*Dead Summary*\" buffer, and
50 it will be killed sometime later."
51   :group 'gnus-summary-exit
52   :type 'boolean)
53
54 (defcustom gnus-fetch-old-headers nil
55   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
56 If an unread article in the group refers to an older, already read (or
57 just marked as read) article, the old article will not normally be
58 displayed in the Summary buffer.  If this variable is non-nil, Gnus
59 will attempt to grab the headers to the old articles, and thereby
60 build complete threads.  If it has the value `some', only enough
61 headers to connect otherwise loose threads will be displayed.  This
62 variable can also be a number.  In that case, no more than that number
63 of old headers will be fetched.  If it has the value `invisible', all
64 old headers will be fetched, but none will be displayed.
65
66 The server has to support NOV for any of this to work."
67   :group 'gnus-thread
68   :type '(choice (const :tag "off" nil)
69                  (const some)
70                  number
71                  (sexp :menu-tag "other" t)))
72
73 (defcustom gnus-refer-thread-limit 200
74   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
75 If t, fetch all the available old headers."
76   :group 'gnus-thread
77   :type '(choice number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-summary-make-false-root 'adopt
81   "*nil means that Gnus won't gather loose threads.
82 If the root of a thread has expired or been read in a previous
83 session, the information necessary to build a complete thread has been
84 lost.  Instead of having many small sub-threads from this original thread
85 scattered all over the summary buffer, Gnus can gather them.
86
87 If non-nil, Gnus will try to gather all loose sub-threads from an
88 original thread into one large thread.
89
90 If this variable is non-nil, it should be one of `none', `adopt',
91 `dummy' or `empty'.
92
93 If this variable is `none', Gnus will not make a false root, but just
94 present the sub-threads after another.
95 If this variable is `dummy', Gnus will create a dummy root that will
96 have all the sub-threads as children.
97 If this variable is `adopt', Gnus will make one of the \"children\"
98 the parent and mark all the step-children as such.
99 If this variable is `empty', the \"children\" are printed with empty
100 subject fields.  (Or rather, they will be printed with a string
101 given by the `gnus-summary-same-subject' variable.)"
102   :group 'gnus-thread
103   :type '(choice (const :tag "off" nil)
104                  (const none)
105                  (const dummy)
106                  (const adopt)
107                  (const empty)))
108
109 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
110   "*A regexp to match subjects to be excluded from loose thread gathering.
111 As loose thread gathering is done on subjects only, that means that
112 there can be many false gatherings performed.  By rooting out certain
113 common subjects, gathering might become saner."
114   :group 'gnus-thread
115   :type 'regexp)
116
117 (defcustom gnus-summary-gather-subject-limit nil
118   "*Maximum length of subject comparisons when gathering loose threads.
119 Use nil to compare full subjects.  Setting this variable to a low
120 number will help gather threads that have been corrupted by
121 newsreaders chopping off subject lines, but it might also mean that
122 unrelated articles that have subject that happen to begin with the
123 same few characters will be incorrectly gathered.
124
125 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
126 comparing subjects."
127   :group 'gnus-thread
128   :type '(choice (const :tag "off" nil)
129                  (const fuzzy)
130                  (sexp :menu-tag "on" t)))
131
132 (defcustom gnus-simplify-subject-functions nil
133   "List of functions taking a string argument that simplify subjects.
134 The functions are applied recursively.
135
136 Useful functions to put in this list include: `gnus-simplify-subject-re',
137 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
138   :group 'gnus-thread
139   :type '(repeat function))
140
141 (defcustom gnus-simplify-ignored-prefixes nil
142   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
143   :group 'gnus-thread
144   :type '(choice (const :tag "off" nil)
145                  regexp))
146
147 (defcustom gnus-build-sparse-threads nil
148   "*If non-nil, fill in the gaps in threads.
149 If `some', only fill in the gaps that are needed to tie loose threads
150 together.  If `more', fill in all leaf nodes that Gnus can find.  If
151 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
152   :group 'gnus-thread
153   :type '(choice (const :tag "off" nil)
154                  (const some)
155                  (const more)
156                  (sexp :menu-tag "all" t)))
157
158 (defcustom gnus-summary-thread-gathering-function
159   'gnus-gather-threads-by-subject
160   "*Function used for gathering loose threads.
161 There are two pre-defined functions: `gnus-gather-threads-by-subject',
162 which only takes Subjects into consideration; and
163 `gnus-gather-threads-by-references', which compared the References
164 headers of the articles to find matches."
165   :group 'gnus-thread
166   :type '(radio (function-item gnus-gather-threads-by-subject)
167                 (function-item gnus-gather-threads-by-references)
168                 (function :tag "other")))
169
170 (defcustom gnus-summary-same-subject ""
171   "*String indicating that the current article has the same subject as the previous.
172 This variable will only be used if the value of
173 `gnus-summary-make-false-root' is `empty'."
174   :group 'gnus-summary-format
175   :type 'string)
176
177 (defcustom gnus-summary-goto-unread t
178   "*If t, many commands will go to the next unread article.
179 This applies to marking commands as well as other commands that
180 \"naturally\" select the next article, like, for instance, `SPC' at
181 the end of an article.
182
183 If nil, the marking commands do NOT go to the next unread article
184 (they go to the next article instead).  If `never', commands that
185 usually go to the next unread article, will go to the next article,
186 whether it is read or not."
187   :group 'gnus-summary-marks
188   :link '(custom-manual "(gnus)Setting Marks")
189   :type '(choice (const :tag "off" nil)
190                  (const never)
191                  (sexp :menu-tag "on" t)))
192
193 (defcustom gnus-summary-default-score 0
194   "*Default article score level.
195 All scores generated by the score files will be added to this score.
196 If this variable is nil, scoring will be disabled."
197   :group 'gnus-score-default
198   :type '(choice (const :tag "disable")
199                  integer))
200
201 (defcustom gnus-summary-default-high-score 0
202   "*Default threshold for a high scored article.
203 An article will be highlighted as high scored if its score is greater
204 than this score."
205   :group 'gnus-score-default
206   :type 'integer)
207
208 (defcustom gnus-summary-default-low-score 0
209   "*Default threshold for a low scored article.
210 An article will be highlighted as low scored if its score is smaller
211 than this score."
212   :group 'gnus-score-default
213   :type 'integer)
214
215 (defcustom gnus-summary-zcore-fuzz 0
216   "*Fuzziness factor for the zcore in the summary buffer.
217 Articles with scores closer than this to `gnus-summary-default-score'
218 will not be marked."
219   :group 'gnus-summary-format
220   :type 'integer)
221
222 (defcustom gnus-simplify-subject-fuzzy-regexp nil
223   "*Strings to be removed when doing fuzzy matches.
224 This can either be a regular expression or list of regular expressions
225 that will be removed from subject strings if fuzzy subject
226 simplification is selected."
227   :group 'gnus-thread
228   :type '(repeat regexp))
229
230 (defcustom gnus-show-threads t
231   "*If non-nil, display threads in summary mode."
232   :group 'gnus-thread
233   :type 'boolean)
234
235 (defcustom gnus-thread-hide-subtree nil
236   "*If non-nil, hide all threads initially.
237 This can be a predicate specifier which says which threads to hide.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If non-nil, select the article under point.
282 Which article this is is controlled by the `gnus-auto-select-subject'
283 variable.
284
285 If you want to prevent automatic selection of articles in some
286 newsgroups, set the variable to nil in `gnus-select-group-hook'."
287   :group 'gnus-group-select
288   :type '(choice (const :tag "none" nil)
289                  (sexp :menu-tag "first" t)))
290
291 (defcustom gnus-auto-select-subject 'unread
292   "*Says what subject to place under point when entering a group.
293
294 This variable can either be the symbols `first' (place point on the
295 first subject), `unread' (place point on the subject line of the first
296 unread article), `best' (place point on the subject line of the
297 higest-scored article), `unseen' (place point on the subject line of
298 the first unseen article), or a function to be called to place point on
299 some subject line.."
300   :group 'gnus-group-select
301   :type '(choice (const best)
302                  (const unread)
303                  (const first)
304                  (const unseen)))
305
306 (defcustom gnus-auto-select-next t
307   "*If non-nil, offer to go to the next group from the end of the previous.
308 If the value is t and the next newsgroup is empty, Gnus will exit
309 summary mode and go back to group mode.  If the value is neither nil
310 nor t, Gnus will select the following unread newsgroup.  In
311 particular, if the value is the symbol `quietly', the next unread
312 newsgroup will be selected without any confirmation, and if it is
313 `almost-quietly', the next group will be selected without any
314 confirmation if you are located on the last article in the group.
315 Finally, if this variable is `slightly-quietly', the `Z n' command
316 will go to the next group without confirmation."
317   :group 'gnus-summary-maneuvering
318   :type '(choice (const :tag "off" nil)
319                  (const quietly)
320                  (const almost-quietly)
321                  (const slightly-quietly)
322                  (sexp :menu-tag "on" t)))
323
324 (defcustom gnus-auto-select-same nil
325   "*If non-nil, select the next article with the same subject.
326 If there are no more articles with the same subject, go to
327 the first unread article."
328   :group 'gnus-summary-maneuvering
329   :type 'boolean)
330
331 (defcustom gnus-summary-check-current nil
332   "*If non-nil, consider the current article when moving.
333 The \"unread\" movement commands will stay on the same line if the
334 current article is unread."
335   :group 'gnus-summary-maneuvering
336   :type 'boolean)
337
338 (defcustom gnus-auto-center-summary t
339   "*If non-nil, always center the current summary buffer.
340 In particular, if `vertical' do only vertical recentering.  If non-nil
341 and non-`vertical', do both horizontal and vertical recentering."
342   :group 'gnus-summary-maneuvering
343   :type '(choice (const :tag "none" nil)
344                  (const vertical)
345                  (integer :tag "height")
346                  (sexp :menu-tag "both" t)))
347
348 (defcustom gnus-show-all-headers nil
349   "*If non-nil, don't hide any headers."
350   :group 'gnus-article-hiding
351   :group 'gnus-article-headers
352   :type 'boolean)
353
354 (defcustom gnus-summary-ignore-duplicates nil
355   "*If non-nil, ignore articles with identical Message-ID headers."
356   :group 'gnus-summary
357   :type 'boolean)
358
359 (defcustom gnus-single-article-buffer t
360   "*If non-nil, display all articles in the same buffer.
361 If nil, each group will get its own article buffer."
362   :group 'gnus-article-various
363   :type 'boolean)
364
365 (defcustom gnus-break-pages t
366   "*If non-nil, do page breaking on articles.
367 The page delimiter is specified by the `gnus-page-delimiter'
368 variable."
369   :group 'gnus-article-various
370   :type 'boolean)
371
372 (defcustom gnus-move-split-methods nil
373   "*Variable used to suggest where articles are to be moved to.
374 It uses the same syntax as the `gnus-split-methods' variable.
375 However, whereas `gnus-split-methods' specifies file names as targets,
376 this variable specifies group names."
377   :group 'gnus-summary-mail
378   :type '(repeat (choice (list :value (fun) function)
379                          (cons :value ("" "") regexp (repeat string))
380                          (sexp :value nil))))
381
382 (defcustom gnus-unread-mark ?           ;Whitespace
383   "*Mark used for unread articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-ticked-mark ?!
388   "*Mark used for ticked articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-dormant-mark ??
393   "*Mark used for dormant articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-del-mark ?r
398   "*Mark used for del'd articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-read-mark ?R
403   "*Mark used for read articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-expirable-mark ?E
408   "*Mark used for expirable articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-killed-mark ?K
413   "*Mark used for killed articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-souped-mark ?F
418   "*Mark used for souped articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-kill-file-mark ?X
423   "*Mark used for articles killed by kill files."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-low-score-mark ?Y
428   "*Mark used for articles with a low score."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-catchup-mark ?C
433   "*Mark used for articles that are caught up."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-replied-mark ?A
438   "*Mark used for articles that have been replied to."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-forwarded-mark ?F
443   "*Mark used for articles that have been forwarded."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-recent-mark ?N
448   "*Mark used for articles that are recent."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-cached-mark ?*
453   "*Mark used for articles that are in the cache."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-saved-mark ?S
458   "*Mark used for articles that have been saved."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-unseen-mark ?.
463   "*Mark used for articles that haven't been seen."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-no-mark ?               ;Whitespace
468   "*Mark used for articles that have no other secondary mark."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-ancient-mark ?O
473   "*Mark used for ancient articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-sparse-mark ?Q
478   "*Mark used for sparsely reffed articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-canceled-mark ?G
483   "*Mark used for canceled articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-duplicate-mark ?M
488   "*Mark used for duplicate articles."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-undownloaded-mark ?@
493   "*Mark used for articles that weren't downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-downloadable-mark ?%
498   "*Mark used for articles that are to be downloaded."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-unsendable-mark ?=
503   "*Mark used for articles that won't be sent."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-over-mark ?+
508   "*Score mark used for articles with high scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-score-below-mark ?-
513   "*Score mark used for articles with low scores."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-empty-thread-mark ?     ;Whitespace
518   "*There is no thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-not-empty-thread-mark ?=
523   "*There is a thread under the article."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-view-pseudo-asynchronously nil
528   "*If non-nil, Gnus will view pseudo-articles asynchronously."
529   :group 'gnus-extract-view
530   :type 'boolean)
531
532 (defcustom gnus-auto-expirable-marks
533   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
534         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
535         gnus-souped-mark gnus-duplicate-mark)
536   "*The list of marks converted into expiration if a group is auto-expirable."
537   :version "21.1"
538   :group 'gnus-summary
539   :type '(repeat character))
540
541 (defcustom gnus-inhibit-user-auto-expire t
542   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
543   :version "21.1"
544   :group 'gnus-summary
545   :type 'boolean)
546
547 (defcustom gnus-view-pseudos nil
548   "*If `automatic', pseudo-articles will be viewed automatically.
549 If `not-confirm', pseudos will be viewed automatically, and the user
550 will not be asked to confirm the command."
551   :group 'gnus-extract-view
552   :type '(choice (const :tag "off" nil)
553                  (const automatic)
554                  (const not-confirm)))
555
556 (defcustom gnus-view-pseudos-separately t
557   "*If non-nil, one pseudo-article will be created for each file to be viewed.
558 If nil, all files that use the same viewing command will be given as a
559 list of parameters to that command."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-insert-pseudo-articles t
564   "*If non-nil, insert pseudo-articles when decoding articles."
565   :group 'gnus-extract-view
566   :type 'boolean)
567
568 (defcustom gnus-summary-dummy-line-format
569   "  %(:                          :%) %S\n"
570   "*The format specification for the dummy roots in the summary buffer.
571 It works along the same lines as a normal formatting string,
572 with some simple extensions.
573
574 %S  The subject
575
576 General format specifiers can also be used.  
577 See (gnus)Formatting Variables."
578   :link '(custom-manual "(gnus)Formatting Variables")
579   :group 'gnus-threading
580   :type 'string)
581
582 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
583   "*The format specification for the summary mode line.
584 It works along the same lines as a normal formatting string,
585 with some simple extensions:
586
587 %G  Group name
588 %p  Unprefixed group name
589 %A  Current article number
590 %z  Current article score
591 %V  Gnus version
592 %U  Number of unread articles in the group
593 %e  Number of unselected articles in the group
594 %Z  A string with unread/unselected article counts
595 %g  Shortish group name
596 %S  Subject of the current article
597 %u  User-defined spec
598 %s  Current score file name
599 %d  Number of dormant articles
600 %r  Number of articles that have been marked as read in this session
601 %E  Number of articles expunged by the score files"
602   :group 'gnus-summary-format
603   :type 'string)
604
605 (defcustom gnus-list-identifiers nil
606   "Regexp that matches list identifiers to be removed from subject.
607 This can also be a list of regexps."
608   :version "21.1"
609   :group 'gnus-summary-format
610   :group 'gnus-article-hiding
611   :type '(choice (const :tag "none" nil)
612                  (regexp :value ".*")
613                  (repeat :value (".*") regexp)))
614
615 (defcustom gnus-summary-mark-below 0
616   "*Mark all articles with a score below this variable as read.
617 This variable is local to each summary buffer and usually set by the
618 score file."
619   :group 'gnus-score-default
620   :type 'integer)
621
622 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
623   "*List of functions used for sorting articles in the summary buffer.
624
625 Each function takes two articles and returns non-nil if the first
626 article should be sorted before the other.  If you use more than one
627 function, the primary sort function should be the last.  You should
628 probably always include `gnus-article-sort-by-number' in the list of
629 sorting functions -- preferably first.  Also note that sorting by date
630 is often much slower than sorting by number, and the sorting order is
631 very similar.  (Sorting by date means sorting by the time the message
632 was sent, sorting by number means sorting by arrival time.)
633
634 Ready-made functions include `gnus-article-sort-by-number',
635 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
636 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
637
638 When threading is turned on, the variable `gnus-thread-sort-functions'
639 controls how articles are sorted."
640   :group 'gnus-summary-sort
641   :type '(repeat (choice (function-item gnus-article-sort-by-number)
642                          (function-item gnus-article-sort-by-author)
643                          (function-item gnus-article-sort-by-subject)
644                          (function-item gnus-article-sort-by-date)
645                          (function-item gnus-article-sort-by-score)
646                          (function :tag "other"))))
647
648 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
649   "*List of functions used for sorting threads in the summary buffer.
650 By default, threads are sorted by article number.
651
652 Each function takes two threads and returns non-nil if the first
653 thread should be sorted before the other.  If you use more than one
654 function, the primary sort function should be the last.  You should
655 probably always include `gnus-thread-sort-by-number' in the list of
656 sorting functions -- preferably first.  Also note that sorting by date
657 is often much slower than sorting by number, and the sorting order is
658 very similar.  (Sorting by date means sorting by the time the message
659 was sent, sorting by number means sorting by arrival time.)
660
661 Ready-made functions include `gnus-thread-sort-by-number',
662 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
663 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
664 `gnus-thread-sort-by-most-recent-thread' and
665 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
666
667 When threading is turned off, the variable
668 `gnus-article-sort-functions' controls how articles are sorted."
669   :group 'gnus-summary-sort
670   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
671                          (function-item gnus-thread-sort-by-author)
672                          (function-item gnus-thread-sort-by-subject)
673                          (function-item gnus-thread-sort-by-date)
674                          (function-item gnus-thread-sort-by-score)
675                          (function-item gnus-thread-sort-by-total-score)
676                          (function :tag "other"))))
677
678 (defcustom gnus-thread-score-function '+
679   "*Function used for calculating the total score of a thread.
680
681 The function is called with the scores of the article and each
682 subthread and should then return the score of the thread.
683
684 Some functions you can use are `+', `max', or `min'."
685   :group 'gnus-summary-sort
686   :type 'function)
687
688 (defcustom gnus-summary-expunge-below nil
689   "All articles that have a score less than this variable will be expunged.
690 This variable is local to the summary buffers."
691   :group 'gnus-score-default
692   :type '(choice (const :tag "off" nil)
693                  integer))
694
695 (defcustom gnus-thread-expunge-below nil
696   "All threads that have a total score less than this variable will be expunged.
697 See `gnus-thread-score-function' for en explanation of what a
698 \"thread score\" is.
699
700 This variable is local to the summary buffers."
701   :group 'gnus-threading
702   :group 'gnus-score-default
703   :type '(choice (const :tag "off" nil)
704                  integer))
705
706 (defcustom gnus-summary-mode-hook nil
707   "*A hook for Gnus summary mode.
708 This hook is run before any variables are set in the summary buffer."
709   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
710   :group 'gnus-summary-various
711   :type 'hook)
712
713 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
714 (when (featurep 'xemacs)
715   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
716   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
717   (add-hook 'gnus-summary-mode-hook
718             'gnus-xmas-switch-horizontal-scrollbar-off))
719
720 (defcustom gnus-summary-menu-hook nil
721   "*Hook run after the creation of the summary mode menu."
722   :group 'gnus-summary-visual
723   :type 'hook)
724
725 (defcustom gnus-summary-exit-hook nil
726   "*A hook called on exit from the summary buffer.
727 It will be called with point in the group buffer."
728   :group 'gnus-summary-exit
729   :type 'hook)
730
731 (defcustom gnus-summary-prepare-hook nil
732   "*A hook called after the summary buffer has been generated.
733 If you want to modify the summary buffer, you can use this hook."
734   :group 'gnus-summary-various
735   :type 'hook)
736
737 (defcustom gnus-summary-prepared-hook nil
738   "*A hook called as the last thing after the summary buffer has been generated."
739   :group 'gnus-summary-various
740   :type 'hook)
741
742 (defcustom gnus-summary-generate-hook nil
743   "*A hook run just before generating the summary buffer.
744 This hook is commonly used to customize threading variables and the
745 like."
746   :group 'gnus-summary-various
747   :type 'hook)
748
749 (defcustom gnus-select-group-hook nil
750   "*A hook called when a newsgroup is selected.
751
752 If you'd like to simplify subjects like the
753 `gnus-summary-next-same-subject' command does, you can use the
754 following hook:
755
756  (setq gnus-select-group-hook
757       (list
758         (lambda ()
759           (mapcar (lambda (header)
760                      (mail-header-set-subject
761                       header
762                       (gnus-simplify-subject
763                        (mail-header-subject header) 're-only)))
764                   gnus-newsgroup-headers))))"
765   :group 'gnus-group-select
766   :type 'hook)
767
768 (defcustom gnus-select-article-hook nil
769   "*A hook called when an article is selected."
770   :group 'gnus-summary-choose
771   :type 'hook)
772
773 (defcustom gnus-visual-mark-article-hook
774   (list 'gnus-highlight-selected-summary)
775   "*Hook run after selecting an article in the summary buffer.
776 It is meant to be used for highlighting the article in some way.  It
777 is not run if `gnus-visual' is nil."
778   :group 'gnus-summary-visual
779   :type 'hook)
780
781 (defcustom gnus-parse-headers-hook nil
782   "*A hook called before parsing the headers."
783   :group 'gnus-various
784   :type 'hook)
785
786 (defcustom gnus-exit-group-hook nil
787   "*A hook called when exiting summary mode.
788 This hook is not called from the non-updating exit commands like `Q'."
789   :group 'gnus-various
790   :type 'hook)
791
792 (defcustom gnus-summary-update-hook
793   (list 'gnus-summary-highlight-line)
794   "*A hook called when a summary line is changed.
795 The hook will not be called if `gnus-visual' is nil.
796
797 The default function `gnus-summary-highlight-line' will
798 highlight the line according to the `gnus-summary-highlight'
799 variable."
800   :group 'gnus-summary-visual
801   :type 'hook)
802
803 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
804   "*A hook called when an article is selected for the first time.
805 The hook is intended to mark an article as read (or unread)
806 automatically when it is selected."
807   :group 'gnus-summary-choose
808   :type 'hook)
809
810 (defcustom gnus-group-no-more-groups-hook nil
811   "*A hook run when returning to group mode having no more (unread) groups."
812   :group 'gnus-group-select
813   :type 'hook)
814
815 (defcustom gnus-ps-print-hook nil
816   "*A hook run before ps-printing something from Gnus."
817   :group 'gnus-summary
818   :type 'hook)
819
820 (defcustom gnus-summary-display-arrow
821   (and (fboundp 'display-graphic-p)
822        (display-graphic-p))
823   "*If non-nil, display an arrow highlighting the current article."
824   :version "21.1"
825   :group 'gnus-summary
826   :type 'boolean)
827
828 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
829   "Face used for highlighting the current article in the summary buffer."
830   :group 'gnus-summary-visual
831   :type 'face)
832
833 (defcustom gnus-summary-highlight
834   '(((= mark gnus-canceled-mark)
835      . gnus-summary-cancelled-face)
836     ((and (> score default-high)
837           (or (= mark gnus-dormant-mark)
838               (= mark gnus-ticked-mark)))
839      . gnus-summary-high-ticked-face)
840     ((and (< score default-low)
841           (or (= mark gnus-dormant-mark)
842               (= mark gnus-ticked-mark)))
843      . gnus-summary-low-ticked-face)
844     ((or (= mark gnus-dormant-mark)
845          (= mark gnus-ticked-mark))
846      . gnus-summary-normal-ticked-face)
847     ((and (> score default-high) (= mark gnus-ancient-mark))
848      . gnus-summary-high-ancient-face)
849     ((and (< score default-low) (= mark gnus-ancient-mark))
850      . gnus-summary-low-ancient-face)
851     ((= mark gnus-ancient-mark)
852      . gnus-summary-normal-ancient-face)
853     ((and (> score default-high) (= mark gnus-unread-mark))
854      . gnus-summary-high-unread-face)
855     ((and (< score default-low) (= mark gnus-unread-mark))
856      . gnus-summary-low-unread-face)
857     ((= mark gnus-unread-mark)
858      . gnus-summary-normal-unread-face)
859     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
860                                                   gnus-undownloaded-mark)))
861      . gnus-summary-high-unread-face)
862     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
863                                                  gnus-undownloaded-mark)))
864      . gnus-summary-low-unread-face)
865     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
866           (memq article gnus-newsgroup-unreads))
867      . gnus-summary-normal-unread-face)
868     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
869      . gnus-summary-normal-read-face)
870     ((> score default-high)
871      . gnus-summary-high-read-face)
872     ((< score default-low)
873      . gnus-summary-low-read-face)
874     (t
875      . gnus-summary-normal-read-face))
876   "*Controls the highlighting of summary buffer lines.
877
878 A list of (FORM . FACE) pairs.  When deciding how a a particular
879 summary line should be displayed, each form is evaluated.  The content
880 of the face field after the first true form is used.  You can change
881 how those summary lines are displayed, by editing the face field.
882
883 You can use the following variables in the FORM field.
884
885 score:        The article's score
886 default:      The default article score.
887 default-high: The default score for high scored articles.
888 default-low:  The default score for low scored articles.
889 below:        The score below which articles are automatically marked as read.
890 mark:         The articles mark."
891   :group 'gnus-summary-visual
892   :type '(repeat (cons (sexp :tag "Form" nil)
893                        face)))
894
895 (defcustom gnus-alter-header-function nil
896   "Function called to allow alteration of article header structures.
897 The function is called with one parameter, the article header vector,
898 which it may alter in any way.")
899
900 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
901   "Variable that says which function should be used to decode a string with encoded words.")
902
903 (defcustom gnus-extra-headers '(To Newsgroups)
904   "*Extra headers to parse."
905   :version "21.1"
906   :group 'gnus-summary
907   :type '(repeat symbol))
908
909 (defcustom gnus-ignored-from-addresses
910   (and user-mail-address (regexp-quote user-mail-address))
911   "*Regexp of From headers that may be suppressed in favor of To headers."
912   :version "21.1"
913   :group 'gnus-summary
914   :type 'regexp)
915
916 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
917   "List of charsets that should be ignored.
918 When these charsets are used in the \"charset\" parameter, the
919 default charset will be used instead."
920   :version "21.1"
921   :type '(repeat symbol)
922   :group 'gnus-charset)
923
924 (gnus-define-group-parameter
925  ignored-charsets
926  :type list
927  :function-document
928  "Return the ignored charsets of GROUP."
929  :variable gnus-group-ignored-charsets-alist
930  :variable-default
931  '(("alt\\.chinese\\.text" iso-8859-1))
932  :variable-document
933  "Alist of regexps (to match group names) and charsets that should be ignored.
934 When these charsets are used in the \"charset\" parameter, the
935 default charset will be used instead."
936  :variable-group gnus-charset
937  :variable-type '(repeat (cons (regexp :tag "Group")
938                                (repeat symbol)))
939  :parameter-type '(choice :tag "Ignored charsets"
940                           :value nil
941                           (repeat (symbol)))
942  :parameter-document       "\
943 List of charsets that should be ignored.
944
945 When these charsets are used in the \"charset\" parameter, the
946 default charset will be used instead.")
947
948 (defcustom gnus-group-highlight-words-alist nil
949   "Alist of group regexps and highlight regexps.
950 This variable uses the same syntax as `gnus-emphasis-alist'."
951   :version "21.1"
952   :type '(repeat (cons (regexp :tag "Group")
953                        (repeat (list (regexp :tag "Highlight regexp")
954                                      (number :tag "Group for entire word" 0)
955                                      (number :tag "Group for displayed part" 0)
956                                      (symbol :tag "Face"
957                                              gnus-emphasis-highlight-words)))))
958   :group 'gnus-summary-visual)
959
960 (defcustom gnus-summary-show-article-charset-alist
961   nil
962   "Alist of number and charset.
963 The article will be shown with the charset corresponding to the
964 numbered argument.
965 For example: ((1 . cn-gb-2312) (2 . big5))."
966   :version "21.1"
967   :type '(repeat (cons (number :tag "Argument" 1)
968                        (symbol :tag "Charset")))
969   :group 'gnus-charset)
970
971 (defcustom gnus-preserve-marks t
972   "Whether marks are preserved when moving, copying and respooling messages."
973   :version "21.1"
974   :type 'boolean
975   :group 'gnus-summary-marks)
976
977 (defcustom gnus-alter-articles-to-read-function nil
978   "Function to be called to alter the list of articles to be selected."
979   :type '(choice (const nil) function)
980   :group 'gnus-summary)
981
982 (defcustom gnus-orphan-score nil
983   "*All orphans get this score added.  Set in the score file."
984   :group 'gnus-score-default
985   :type '(choice (const nil)
986                  integer))
987
988 (defcustom gnus-summary-save-parts-default-mime "image/.*"
989   "*A regexp to match MIME parts when saving multiple parts of a message
990 with gnus-summary-save-parts (X m). This regexp will be used by default
991 when prompting the user for which type of files to save."
992   :group 'gnus-summary
993   :type 'regexp)
994
995
996 (defcustom gnus-summary-save-parts-default-mime "image/.*"
997   "*A regexp to match MIME parts when saving multiple parts of a message
998 with gnus-summary-save-parts (X m). This regexp will be used by default
999 when prompting the user for which type of files to save."
1000   :group 'gnus-summary
1001   :type 'regexp)
1002
1003 (defcustom gnus-read-all-available-headers nil
1004   "Whether Gnus should parse all headers made available to it.
1005 This is mostly relevant for slow backends where the user may
1006 wish to widen the summary buffer to include all headers
1007 that were fetched.  Say, for nnultimate groups."
1008   :group 'gnus-summary
1009   :type '(choice boolean regexp))
1010
1011 (defcustom gnus-summary-muttprint-program "muttprint"
1012   "Command (and optional arguments) used to run Muttprint."
1013   :group 'gnus-summary
1014   :type 'string)
1015
1016 ;;; Internal variables
1017
1018 (defvar gnus-summary-display-cache nil)
1019 (defvar gnus-article-mime-handles nil)
1020 (defvar gnus-article-decoded-p nil)
1021 (defvar gnus-article-charset nil)
1022 (defvar gnus-article-ignored-charsets nil)
1023 (defvar gnus-scores-exclude-files nil)
1024 (defvar gnus-page-broken nil)
1025 (defvar gnus-inhibit-mime-unbuttonizing nil)
1026
1027 (defvar gnus-original-article nil)
1028 (defvar gnus-article-internal-prepare-hook nil)
1029 (defvar gnus-newsgroup-process-stack nil)
1030
1031 (defvar gnus-thread-indent-array nil)
1032 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1033 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1034   "Function called to sort the articles within a thread after it has been gathered together.")
1035
1036 (defvar gnus-summary-save-parts-type-history nil)
1037 (defvar gnus-summary-save-parts-last-directory nil)
1038
1039 (defvar gnus-summary-save-parts-type-history nil)
1040 (defvar gnus-summary-save-parts-last-directory nil)
1041
1042 ;; Avoid highlighting in kill files.
1043 (defvar gnus-summary-inhibit-highlight nil)
1044 (defvar gnus-newsgroup-selected-overlay nil)
1045 (defvar gnus-inhibit-limiting nil)
1046 (defvar gnus-newsgroup-adaptive-score-file nil)
1047 (defvar gnus-current-score-file nil)
1048 (defvar gnus-current-move-group nil)
1049 (defvar gnus-current-copy-group nil)
1050 (defvar gnus-current-crosspost-group nil)
1051 (defvar gnus-newsgroup-display nil)
1052
1053 (defvar gnus-newsgroup-dependencies nil)
1054 (defvar gnus-newsgroup-adaptive nil)
1055 (defvar gnus-summary-display-article-function nil)
1056 (defvar gnus-summary-highlight-line-function nil
1057   "Function called after highlighting a summary line.")
1058
1059 (defvar gnus-summary-line-format-alist
1060   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1061     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1062     (?s gnus-tmp-subject-or-nil ?s)
1063     (?n gnus-tmp-name ?s)
1064     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1065         ?s)
1066     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1067             gnus-tmp-from) ?s)
1068     (?F gnus-tmp-from ?s)
1069     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1070     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1071     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1072     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1073     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1074     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1075     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1076     (?L gnus-tmp-lines ?s)
1077     (?I gnus-tmp-indentation ?s)
1078     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1079     (?R gnus-tmp-replied ?c)
1080     (?\[ gnus-tmp-opening-bracket ?c)
1081     (?\] gnus-tmp-closing-bracket ?c)
1082     (?\> (make-string gnus-tmp-level ? ) ?s)
1083     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1084     (?i gnus-tmp-score ?d)
1085     (?z gnus-tmp-score-char ?c)
1086     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1087     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1088     (?U gnus-tmp-unread ?c)
1089     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1090         ?s)
1091     (?t (gnus-summary-number-of-articles-in-thread
1092          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1093         ?d)
1094     (?e (gnus-summary-number-of-articles-in-thread
1095          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1096         ?c)
1097     (?u gnus-tmp-user-defined ?s)
1098     (?P (gnus-pick-line-number) ?d)
1099     (?B gnus-tmp-thread-tree-header-string ?s)
1100     (user-date (gnus-user-date
1101                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1102   "An alist of format specifications that can appear in summary lines.
1103 These are paired with what variables they correspond with, along with
1104 the type of the variable (string, integer, character, etc).")
1105
1106 (defvar gnus-summary-dummy-line-format-alist
1107   `((?S gnus-tmp-subject ?s)
1108     (?N gnus-tmp-number ?d)
1109     (?u gnus-tmp-user-defined ?s)))
1110
1111 (defvar gnus-summary-mode-line-format-alist
1112   `((?G gnus-tmp-group-name ?s)
1113     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1114     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1115     (?A gnus-tmp-article-number ?d)
1116     (?Z gnus-tmp-unread-and-unselected ?s)
1117     (?V gnus-version ?s)
1118     (?U gnus-tmp-unread-and-unticked ?d)
1119     (?S gnus-tmp-subject ?s)
1120     (?e gnus-tmp-unselected ?d)
1121     (?u gnus-tmp-user-defined ?s)
1122     (?d (length gnus-newsgroup-dormant) ?d)
1123     (?t (length gnus-newsgroup-marked) ?d)
1124     (?r (length gnus-newsgroup-reads) ?d)
1125     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1126     (?E gnus-newsgroup-expunged-tally ?d)
1127     (?s (gnus-current-score-file-nondirectory) ?s)))
1128
1129 (defvar gnus-last-search-regexp nil
1130   "Default regexp for article search command.")
1131
1132 (defvar gnus-last-shell-command nil
1133   "Default shell command on article.")
1134
1135 (defvar gnus-newsgroup-begin nil)
1136 (defvar gnus-newsgroup-end nil)
1137 (defvar gnus-newsgroup-last-rmail nil)
1138 (defvar gnus-newsgroup-last-mail nil)
1139 (defvar gnus-newsgroup-last-folder nil)
1140 (defvar gnus-newsgroup-last-file nil)
1141 (defvar gnus-newsgroup-auto-expire nil)
1142 (defvar gnus-newsgroup-active nil)
1143
1144 (defvar gnus-newsgroup-data nil)
1145 (defvar gnus-newsgroup-data-reverse nil)
1146 (defvar gnus-newsgroup-limit nil)
1147 (defvar gnus-newsgroup-limits nil)
1148
1149 (defvar gnus-newsgroup-unreads nil
1150   "List of unread articles in the current newsgroup.")
1151
1152 (defvar gnus-newsgroup-unselected nil
1153   "List of unselected unread articles in the current newsgroup.")
1154
1155 (defvar gnus-newsgroup-reads nil
1156   "Alist of read articles and article marks in the current newsgroup.")
1157
1158 (defvar gnus-newsgroup-expunged-tally nil)
1159
1160 (defvar gnus-newsgroup-marked nil
1161   "List of ticked articles in the current newsgroup (a subset of unread art).")
1162
1163 (defvar gnus-newsgroup-killed nil
1164   "List of ranges of articles that have been through the scoring process.")
1165
1166 (defvar gnus-newsgroup-cached nil
1167   "List of articles that come from the article cache.")
1168
1169 (defvar gnus-newsgroup-saved nil
1170   "List of articles that have been saved.")
1171
1172 (defvar gnus-newsgroup-kill-headers nil)
1173
1174 (defvar gnus-newsgroup-replied nil
1175   "List of articles that have been replied to in the current newsgroup.")
1176
1177 (defvar gnus-newsgroup-forwarded nil
1178   "List of articles that have been forwarded in the current newsgroup.")
1179
1180 (defvar gnus-newsgroup-recent nil
1181   "List of articles that have are recent in the current newsgroup.")
1182
1183 (defvar gnus-newsgroup-expirable nil
1184   "List of articles in the current newsgroup that can be expired.")
1185
1186 (defvar gnus-newsgroup-processable nil
1187   "List of articles in the current newsgroup that can be processed.")
1188
1189 (defvar gnus-newsgroup-downloadable nil
1190   "List of articles in the current newsgroup that can be processed.")
1191
1192 (defvar gnus-newsgroup-undownloaded nil
1193   "List of articles in the current newsgroup that haven't been downloaded..")
1194
1195 (defvar gnus-newsgroup-unsendable nil
1196   "List of articles in the current newsgroup that won't be sent.")
1197
1198 (defvar gnus-newsgroup-bookmarks nil
1199   "List of articles in the current newsgroup that have bookmarks.")
1200
1201 (defvar gnus-newsgroup-dormant nil
1202   "List of dormant articles in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-unseen nil
1205   "List of unseen articles in the current newsgroup.")
1206
1207 (defvar gnus-newsgroup-seen nil
1208   "Range of seen articles in the current newsgroup.")
1209
1210 (defvar gnus-newsgroup-articles nil
1211   "List of articles in the current newsgroup.")
1212
1213 (defvar gnus-newsgroup-scored nil
1214   "List of scored articles in the current newsgroup.")
1215
1216 (defvar gnus-newsgroup-headers nil
1217   "List of article headers in the current newsgroup.")
1218
1219 (defvar gnus-newsgroup-threads nil)
1220
1221 (defvar gnus-newsgroup-prepared nil
1222   "Whether the current group has been prepared properly.")
1223
1224 (defvar gnus-newsgroup-ancient nil
1225   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1226
1227 (defvar gnus-newsgroup-sparse nil)
1228
1229 (defvar gnus-current-article nil)
1230 (defvar gnus-article-current nil)
1231 (defvar gnus-current-headers nil)
1232 (defvar gnus-have-all-headers nil)
1233 (defvar gnus-last-article nil)
1234 (defvar gnus-newsgroup-history nil)
1235 (defvar gnus-newsgroup-charset nil)
1236 (defvar gnus-newsgroup-ephemeral-charset nil)
1237 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1238
1239 (defvar gnus-article-before-search nil)
1240
1241 (defconst gnus-summary-local-variables
1242   '(gnus-newsgroup-name
1243     gnus-newsgroup-begin gnus-newsgroup-end
1244     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1245     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1246     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1247     gnus-newsgroup-unselected gnus-newsgroup-marked
1248     gnus-newsgroup-reads gnus-newsgroup-saved
1249     gnus-newsgroup-replied gnus-newsgroup-forwarded
1250     gnus-newsgroup-recent
1251     gnus-newsgroup-expirable
1252     gnus-newsgroup-processable gnus-newsgroup-killed
1253     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1254     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1255     gnus-newsgroup-seen gnus-newsgroup-articles
1256     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1257     gnus-newsgroup-headers gnus-newsgroup-threads
1258     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1259     gnus-current-article gnus-current-headers gnus-have-all-headers
1260     gnus-last-article gnus-article-internal-prepare-hook
1261     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1262     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1263     gnus-thread-expunge-below
1264     gnus-score-alist gnus-current-score-file
1265     (gnus-summary-expunge-below . global)
1266     (gnus-summary-mark-below . global)
1267     (gnus-orphan-score . global)
1268     gnus-newsgroup-active gnus-scores-exclude-files
1269     gnus-newsgroup-history gnus-newsgroup-ancient
1270     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1271     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1272     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1273     (gnus-newsgroup-expunged-tally . 0)
1274     gnus-cache-removable-articles gnus-newsgroup-cached
1275     gnus-newsgroup-data gnus-newsgroup-data-reverse
1276     gnus-newsgroup-limit gnus-newsgroup-limits
1277     gnus-newsgroup-charset gnus-newsgroup-display)
1278   "Variables that are buffer-local to the summary buffers.")
1279
1280 (defvar gnus-newsgroup-variables nil
1281   "A list of variables that have separate values in different newsgroups.
1282 A list of newsgroup (summary buffer) local variables, or cons of
1283 variables and their default values (when the default values are not
1284 nil), that should be made global while the summary buffer is active.
1285 These variables can be used to set variables in the group parameters
1286 while still allowing them to affect operations done in other
1287 buffers. For example:
1288
1289 \(setq gnus-newsgroup-variables
1290      '(message-use-followup-to
1291        (gnus-visible-headers .
1292          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1293 ")
1294
1295 ;; Byte-compiler warning.
1296 (eval-when-compile (defvar gnus-article-mode-map))
1297
1298 ;; MIME stuff.
1299
1300 (defvar gnus-decode-encoded-word-methods
1301   '(mail-decode-encoded-word-string)
1302   "List of methods used to decode encoded words.
1303
1304 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1305 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1306 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1307 whose names match REGEXP.
1308
1309 For example:
1310 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1311  mail-decode-encoded-word-string
1312  (\"chinese\" . rfc1843-decode-string))")
1313
1314 (defvar gnus-decode-encoded-word-methods-cache nil)
1315
1316 (defun gnus-multi-decode-encoded-word-string (string)
1317   "Apply the functions from `gnus-encoded-word-methods' that match."
1318   (unless (and gnus-decode-encoded-word-methods-cache
1319                (eq gnus-newsgroup-name
1320                    (car gnus-decode-encoded-word-methods-cache)))
1321     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1322     (mapcar (lambda (x)
1323               (if (symbolp x)
1324                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1325                 (if (and gnus-newsgroup-name
1326                          (string-match (car x) gnus-newsgroup-name))
1327                     (nconc gnus-decode-encoded-word-methods-cache
1328                            (list (cdr x))))))
1329             gnus-decode-encoded-word-methods))
1330   (let ((xlist gnus-decode-encoded-word-methods-cache))
1331     (pop xlist)
1332     (while xlist
1333       (setq string (funcall (pop xlist) string))))
1334   string)
1335
1336 ;; Subject simplification.
1337
1338 (defun gnus-simplify-whitespace (str)
1339   "Remove excessive whitespace from STR."
1340   (let ((mystr str))
1341     ;; Multiple spaces.
1342     (while (string-match "[ \t][ \t]+" mystr)
1343       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1344                           " "
1345                           (substring mystr (match-end 0)))))
1346     ;; Leading spaces.
1347     (when (string-match "^[ \t]+" mystr)
1348       (setq mystr (substring mystr (match-end 0))))
1349     ;; Trailing spaces.
1350     (when (string-match "[ \t]+$" mystr)
1351       (setq mystr (substring mystr 0 (match-beginning 0))))
1352     mystr))
1353
1354 (defsubst gnus-simplify-subject-re (subject)
1355   "Remove \"Re:\" from subject lines."
1356   (if (string-match message-subject-re-regexp subject)
1357       (substring subject (match-end 0))
1358     subject))
1359
1360 (defun gnus-simplify-subject (subject &optional re-only)
1361   "Remove `Re:' and words in parentheses.
1362 If RE-ONLY is non-nil, strip leading `Re:'s only."
1363   (let ((case-fold-search t))           ;Ignore case.
1364     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1365     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1366       (setq subject (substring subject (match-end 0))))
1367     ;; Remove uninteresting prefixes.
1368     (when (and (not re-only)
1369                gnus-simplify-ignored-prefixes
1370                (string-match gnus-simplify-ignored-prefixes subject))
1371       (setq subject (substring subject (match-end 0))))
1372     ;; Remove words in parentheses from end.
1373     (unless re-only
1374       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1375         (setq subject (substring subject 0 (match-beginning 0)))))
1376     ;; Return subject string.
1377     subject))
1378
1379 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1380 ;; all whitespace.
1381 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1382   (goto-char (point-min))
1383   (while (re-search-forward regexp nil t)
1384     (replace-match (or newtext ""))))
1385
1386 (defun gnus-simplify-buffer-fuzzy ()
1387   "Simplify string in the buffer fuzzily.
1388 The string in the accessible portion of the current buffer is simplified.
1389 It is assumed to be a single-line subject.
1390 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1391 matter is removed.  Additional things can be deleted by setting
1392 `gnus-simplify-subject-fuzzy-regexp'."
1393   (let ((case-fold-search t)
1394         (modified-tick))
1395     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1396
1397     (while (not (eq modified-tick (buffer-modified-tick)))
1398       (setq modified-tick (buffer-modified-tick))
1399       (cond
1400        ((listp gnus-simplify-subject-fuzzy-regexp)
1401         (mapcar 'gnus-simplify-buffer-fuzzy-step
1402                 gnus-simplify-subject-fuzzy-regexp))
1403        (gnus-simplify-subject-fuzzy-regexp
1404         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1405       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1406       (gnus-simplify-buffer-fuzzy-step
1407        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1408       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1409
1410     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1411     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1412     (gnus-simplify-buffer-fuzzy-step " $")
1413     (gnus-simplify-buffer-fuzzy-step "^ +")))
1414
1415 (defun gnus-simplify-subject-fuzzy (subject)
1416   "Simplify a subject string fuzzily.
1417 See `gnus-simplify-buffer-fuzzy' for details."
1418   (save-excursion
1419     (gnus-set-work-buffer)
1420     (let ((case-fold-search t))
1421       ;; Remove uninteresting prefixes.
1422       (when (and gnus-simplify-ignored-prefixes
1423                  (string-match gnus-simplify-ignored-prefixes subject))
1424         (setq subject (substring subject (match-end 0))))
1425       (insert subject)
1426       (inline (gnus-simplify-buffer-fuzzy))
1427       (buffer-string))))
1428
1429 (defsubst gnus-simplify-subject-fully (subject)
1430   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1431   (cond
1432    (gnus-simplify-subject-functions
1433     (gnus-map-function gnus-simplify-subject-functions subject))
1434    ((null gnus-summary-gather-subject-limit)
1435     (gnus-simplify-subject-re subject))
1436    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1437     (gnus-simplify-subject-fuzzy subject))
1438    ((numberp gnus-summary-gather-subject-limit)
1439     (gnus-limit-string (gnus-simplify-subject-re subject)
1440                        gnus-summary-gather-subject-limit))
1441    (t
1442     subject)))
1443
1444 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1445   "Check whether two subjects are equal.
1446 If optional argument simple-first is t, first argument is already
1447 simplified."
1448   (cond
1449    ((null simple-first)
1450     (equal (gnus-simplify-subject-fully s1)
1451            (gnus-simplify-subject-fully s2)))
1452    (t
1453     (equal s1
1454            (gnus-simplify-subject-fully s2)))))
1455
1456 (defun gnus-summary-bubble-group ()
1457   "Increase the score of the current group.
1458 This is a handy function to add to `gnus-summary-exit-hook' to
1459 increase the score of each group you read."
1460   (gnus-group-add-score gnus-newsgroup-name))
1461
1462 \f
1463 ;;;
1464 ;;; Gnus summary mode
1465 ;;;
1466
1467 (put 'gnus-summary-mode 'mode-class 'special)
1468
1469 (defvar gnus-article-commands-menu)
1470
1471 (when t
1472   ;; Non-orthogonal keys
1473
1474   (gnus-define-keys gnus-summary-mode-map
1475     " " gnus-summary-next-page
1476     "\177" gnus-summary-prev-page
1477     [delete] gnus-summary-prev-page
1478     [backspace] gnus-summary-prev-page
1479     "\r" gnus-summary-scroll-up
1480     "\M-\r" gnus-summary-scroll-down
1481     "n" gnus-summary-next-unread-article
1482     "p" gnus-summary-prev-unread-article
1483     "N" gnus-summary-next-article
1484     "P" gnus-summary-prev-article
1485     "\M-\C-n" gnus-summary-next-same-subject
1486     "\M-\C-p" gnus-summary-prev-same-subject
1487     "\M-n" gnus-summary-next-unread-subject
1488     "\M-p" gnus-summary-prev-unread-subject
1489     "." gnus-summary-first-unread-article
1490     "," gnus-summary-best-unread-article
1491     "\M-s" gnus-summary-search-article-forward
1492     "\M-r" gnus-summary-search-article-backward
1493     "<" gnus-summary-beginning-of-article
1494     ">" gnus-summary-end-of-article
1495     "j" gnus-summary-goto-article
1496     "^" gnus-summary-refer-parent-article
1497     "\M-^" gnus-summary-refer-article
1498     "u" gnus-summary-tick-article-forward
1499     "!" gnus-summary-tick-article-forward
1500     "U" gnus-summary-tick-article-backward
1501     "d" gnus-summary-mark-as-read-forward
1502     "D" gnus-summary-mark-as-read-backward
1503     "E" gnus-summary-mark-as-expirable
1504     "\M-u" gnus-summary-clear-mark-forward
1505     "\M-U" gnus-summary-clear-mark-backward
1506     "k" gnus-summary-kill-same-subject-and-select
1507     "\C-k" gnus-summary-kill-same-subject
1508     "\M-\C-k" gnus-summary-kill-thread
1509     "\M-\C-l" gnus-summary-lower-thread
1510     "e" gnus-summary-edit-article
1511     "#" gnus-summary-mark-as-processable
1512     "\M-#" gnus-summary-unmark-as-processable
1513     "\M-\C-t" gnus-summary-toggle-threads
1514     "\M-\C-s" gnus-summary-show-thread
1515     "\M-\C-h" gnus-summary-hide-thread
1516     "\M-\C-f" gnus-summary-next-thread
1517     "\M-\C-b" gnus-summary-prev-thread
1518     [(meta down)] gnus-summary-next-thread
1519     [(meta up)] gnus-summary-prev-thread
1520     "\M-\C-u" gnus-summary-up-thread
1521     "\M-\C-d" gnus-summary-down-thread
1522     "&" gnus-summary-execute-command
1523     "c" gnus-summary-catchup-and-exit
1524     "\C-w" gnus-summary-mark-region-as-read
1525     "\C-t" gnus-summary-toggle-truncation
1526     "?" gnus-summary-mark-as-dormant
1527     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1528     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1529     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1530     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1531     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1532     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1533     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1534     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1535     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1536     "=" gnus-summary-expand-window
1537     "\C-x\C-s" gnus-summary-reselect-current-group
1538     "\M-g" gnus-summary-rescan-group
1539     "w" gnus-summary-stop-page-breaking
1540     "\C-c\C-r" gnus-summary-caesar-message
1541     "f" gnus-summary-followup
1542     "F" gnus-summary-followup-with-original
1543     "C" gnus-summary-cancel-article
1544     "r" gnus-summary-reply
1545     "R" gnus-summary-reply-with-original
1546     "\C-c\C-f" gnus-summary-mail-forward
1547     "o" gnus-summary-save-article
1548     "\C-o" gnus-summary-save-article-mail
1549     "|" gnus-summary-pipe-output
1550     "\M-k" gnus-summary-edit-local-kill
1551     "\M-K" gnus-summary-edit-global-kill
1552     ;; "V" gnus-version
1553     "\C-c\C-d" gnus-summary-describe-group
1554     "q" gnus-summary-exit
1555     "Q" gnus-summary-exit-no-update
1556     "\C-c\C-i" gnus-info-find-node
1557     gnus-mouse-2 gnus-mouse-pick-article
1558     "m" gnus-summary-mail-other-window
1559     "a" gnus-summary-post-news
1560     "i" gnus-summary-news-other-window
1561     "x" gnus-summary-limit-to-unread
1562     "s" gnus-summary-isearch-article
1563     "t" gnus-summary-toggle-header
1564     "g" gnus-summary-show-article
1565     "l" gnus-summary-goto-last-article
1566     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1567     "\C-d" gnus-summary-enter-digest-group
1568     "\M-\C-d" gnus-summary-read-document
1569     "\M-\C-e" gnus-summary-edit-parameters
1570     "\M-\C-a" gnus-summary-customize-parameters
1571     "\C-c\C-b" gnus-bug
1572     "*" gnus-cache-enter-article
1573     "\M-*" gnus-cache-remove-article
1574     "\M-&" gnus-summary-universal-argument
1575     "\C-l" gnus-recenter
1576     "I" gnus-summary-increase-score
1577     "L" gnus-summary-lower-score
1578     "\M-i" gnus-symbolic-argument
1579     "h" gnus-summary-select-article-buffer
1580
1581     "b" gnus-article-view-part
1582     "\M-t" gnus-summary-toggle-display-buttonized
1583
1584     "V" gnus-summary-score-map
1585     "X" gnus-uu-extract-map
1586     "S" gnus-summary-send-map)
1587
1588   ;; Sort of orthogonal keymap
1589   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1590     "t" gnus-summary-tick-article-forward
1591     "!" gnus-summary-tick-article-forward
1592     "d" gnus-summary-mark-as-read-forward
1593     "r" gnus-summary-mark-as-read-forward
1594     "c" gnus-summary-clear-mark-forward
1595     " " gnus-summary-clear-mark-forward
1596     "e" gnus-summary-mark-as-expirable
1597     "x" gnus-summary-mark-as-expirable
1598     "?" gnus-summary-mark-as-dormant
1599     "b" gnus-summary-set-bookmark
1600     "B" gnus-summary-remove-bookmark
1601     "#" gnus-summary-mark-as-processable
1602     "\M-#" gnus-summary-unmark-as-processable
1603     "S" gnus-summary-limit-include-expunged
1604     "C" gnus-summary-catchup
1605     "H" gnus-summary-catchup-to-here
1606     "h" gnus-summary-catchup-from-here
1607     "\C-c" gnus-summary-catchup-all
1608     "k" gnus-summary-kill-same-subject-and-select
1609     "K" gnus-summary-kill-same-subject
1610     "P" gnus-uu-mark-map)
1611
1612   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1613     "c" gnus-summary-clear-above
1614     "u" gnus-summary-tick-above
1615     "m" gnus-summary-mark-above
1616     "k" gnus-summary-kill-below)
1617
1618   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1619     "/" gnus-summary-limit-to-subject
1620     "n" gnus-summary-limit-to-articles
1621     "w" gnus-summary-pop-limit
1622     "s" gnus-summary-limit-to-subject
1623     "a" gnus-summary-limit-to-author
1624     "u" gnus-summary-limit-to-unread
1625     "m" gnus-summary-limit-to-marks
1626     "M" gnus-summary-limit-exclude-marks
1627     "v" gnus-summary-limit-to-score
1628     "*" gnus-summary-limit-include-cached
1629     "D" gnus-summary-limit-include-dormant
1630     "T" gnus-summary-limit-include-thread
1631     "d" gnus-summary-limit-exclude-dormant
1632     "t" gnus-summary-limit-to-age
1633     "x" gnus-summary-limit-to-extra
1634     "p" gnus-summary-limit-to-display-predicate
1635     "E" gnus-summary-limit-include-expunged
1636     "c" gnus-summary-limit-exclude-childless-dormant
1637     "C" gnus-summary-limit-mark-excluded-as-read
1638     "o" gnus-summary-insert-old-articles
1639     "N" gnus-summary-insert-new-articles)
1640
1641   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1642     "n" gnus-summary-next-unread-article
1643     "p" gnus-summary-prev-unread-article
1644     "N" gnus-summary-next-article
1645     "P" gnus-summary-prev-article
1646     "\C-n" gnus-summary-next-same-subject
1647     "\C-p" gnus-summary-prev-same-subject
1648     "\M-n" gnus-summary-next-unread-subject
1649     "\M-p" gnus-summary-prev-unread-subject
1650     "f" gnus-summary-first-unread-article
1651     "b" gnus-summary-best-unread-article
1652     "j" gnus-summary-goto-article
1653     "g" gnus-summary-goto-subject
1654     "l" gnus-summary-goto-last-article
1655     "o" gnus-summary-pop-article)
1656
1657   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1658     "k" gnus-summary-kill-thread
1659     "l" gnus-summary-lower-thread
1660     "i" gnus-summary-raise-thread
1661     "T" gnus-summary-toggle-threads
1662     "t" gnus-summary-rethread-current
1663     "^" gnus-summary-reparent-thread
1664     "s" gnus-summary-show-thread
1665     "S" gnus-summary-show-all-threads
1666     "h" gnus-summary-hide-thread
1667     "H" gnus-summary-hide-all-threads
1668     "n" gnus-summary-next-thread
1669     "p" gnus-summary-prev-thread
1670     "u" gnus-summary-up-thread
1671     "o" gnus-summary-top-thread
1672     "d" gnus-summary-down-thread
1673     "#" gnus-uu-mark-thread
1674     "\M-#" gnus-uu-unmark-thread)
1675
1676   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1677     "g" gnus-summary-prepare
1678     "c" gnus-summary-insert-cached-articles)
1679
1680   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1681     "c" gnus-summary-catchup-and-exit
1682     "C" gnus-summary-catchup-all-and-exit
1683     "E" gnus-summary-exit-no-update
1684     "Q" gnus-summary-exit
1685     "Z" gnus-summary-exit
1686     "n" gnus-summary-catchup-and-goto-next-group
1687     "R" gnus-summary-reselect-current-group
1688     "G" gnus-summary-rescan-group
1689     "N" gnus-summary-next-group
1690     "s" gnus-summary-save-newsrc
1691     "P" gnus-summary-prev-group)
1692
1693   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1694     " " gnus-summary-next-page
1695     "n" gnus-summary-next-page
1696     "\177" gnus-summary-prev-page
1697     [delete] gnus-summary-prev-page
1698     "p" gnus-summary-prev-page
1699     "\r" gnus-summary-scroll-up
1700     "\M-\r" gnus-summary-scroll-down
1701     "<" gnus-summary-beginning-of-article
1702     ">" gnus-summary-end-of-article
1703     "b" gnus-summary-beginning-of-article
1704     "e" gnus-summary-end-of-article
1705     "^" gnus-summary-refer-parent-article
1706     "r" gnus-summary-refer-parent-article
1707     "D" gnus-summary-enter-digest-group
1708     "R" gnus-summary-refer-references
1709     "T" gnus-summary-refer-thread
1710     "g" gnus-summary-show-article
1711     "s" gnus-summary-isearch-article
1712     "P" gnus-summary-print-article
1713     "M" gnus-mailing-list-insinuate
1714     "t" gnus-article-babel)
1715
1716   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1717     "b" gnus-article-add-buttons
1718     "B" gnus-article-add-buttons-to-head
1719     "o" gnus-article-treat-overstrike
1720     "e" gnus-article-emphasize
1721     "w" gnus-article-fill-cited-article
1722     "Q" gnus-article-fill-long-lines
1723     "C" gnus-article-capitalize-sentences
1724     "c" gnus-article-remove-cr
1725     "q" gnus-article-de-quoted-unreadable
1726     "6" gnus-article-de-base64-unreadable
1727     "Z" gnus-article-decode-HZ
1728     "h" gnus-article-wash-html
1729     "s" gnus-summary-force-verify-and-decrypt
1730     "f" gnus-article-display-x-face
1731     "l" gnus-summary-stop-page-breaking
1732     "r" gnus-summary-caesar-message
1733     "t" gnus-summary-toggle-header
1734     "g" gnus-treat-smiley
1735     "v" gnus-summary-verbose-headers
1736     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1737     "p" gnus-article-verify-x-pgp-sig
1738     "d" gnus-article-treat-dumbquotes)
1739
1740   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1741     "a" gnus-article-hide
1742     "h" gnus-article-hide-headers
1743     "b" gnus-article-hide-boring-headers
1744     "s" gnus-article-hide-signature
1745     "c" gnus-article-hide-citation
1746     "C" gnus-article-hide-citation-in-followups
1747     "l" gnus-article-hide-list-identifiers
1748     "p" gnus-article-hide-pgp
1749     "B" gnus-article-strip-banner
1750     "P" gnus-article-hide-pem
1751     "\C-c" gnus-article-hide-citation-maybe)
1752
1753   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1754     "a" gnus-article-highlight
1755     "h" gnus-article-highlight-headers
1756     "c" gnus-article-highlight-citation
1757     "s" gnus-article-highlight-signature)
1758
1759   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1760     "f" gnus-article-treat-fold-headers
1761     "u" gnus-article-treat-unfold-headers
1762     "n" gnus-article-treat-fold-newsgroups)
1763
1764   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1765     "x" gnus-article-display-x-face
1766     "s" gnus-treat-smiley
1767     "D" gnus-article-remove-images
1768     "f" gnus-treat-from-picon
1769     "m" gnus-treat-mail-picon
1770     "n" gnus-treat-newsgroups-picon)
1771
1772   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1773     "w" gnus-article-decode-mime-words
1774     "c" gnus-article-decode-charset
1775     "v" gnus-mime-view-all-parts
1776     "b" gnus-article-view-part)
1777
1778   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1779     "z" gnus-article-date-ut
1780     "u" gnus-article-date-ut
1781     "l" gnus-article-date-local
1782     "p" gnus-article-date-english
1783     "e" gnus-article-date-lapsed
1784     "o" gnus-article-date-original
1785     "i" gnus-article-date-iso8601
1786     "s" gnus-article-date-user)
1787
1788   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1789     "t" gnus-article-remove-trailing-blank-lines
1790     "l" gnus-article-strip-leading-blank-lines
1791     "m" gnus-article-strip-multiple-blank-lines
1792     "a" gnus-article-strip-blank-lines
1793     "A" gnus-article-strip-all-blank-lines
1794     "s" gnus-article-strip-leading-space
1795     "e" gnus-article-strip-trailing-space
1796     "w" gnus-article-remove-leading-whitespace)
1797
1798   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1799     "v" gnus-version
1800     "f" gnus-summary-fetch-faq
1801     "d" gnus-summary-describe-group
1802     "h" gnus-summary-describe-briefly
1803     "i" gnus-info-find-node)
1804
1805   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1806     "e" gnus-summary-expire-articles
1807     "\M-\C-e" gnus-summary-expire-articles-now
1808     "\177" gnus-summary-delete-article
1809     [delete] gnus-summary-delete-article
1810     [backspace] gnus-summary-delete-article
1811     "m" gnus-summary-move-article
1812     "r" gnus-summary-respool-article
1813     "w" gnus-summary-edit-article
1814     "c" gnus-summary-copy-article
1815     "B" gnus-summary-crosspost-article
1816     "q" gnus-summary-respool-query
1817     "t" gnus-summary-respool-trace
1818     "i" gnus-summary-import-article
1819     "I" gnus-summary-create-article
1820     "p" gnus-summary-article-posted-p)
1821
1822   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1823     "o" gnus-summary-save-article
1824     "m" gnus-summary-save-article-mail
1825     "F" gnus-summary-write-article-file
1826     "r" gnus-summary-save-article-rmail
1827     "f" gnus-summary-save-article-file
1828     "b" gnus-summary-save-article-body-file
1829     "h" gnus-summary-save-article-folder
1830     "v" gnus-summary-save-article-vm
1831     "p" gnus-summary-pipe-output
1832     "P" gnus-summary-muttprint
1833     "s" gnus-soup-add-article)
1834
1835   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1836     "b" gnus-summary-display-buttonized
1837     "m" gnus-summary-repair-multipart
1838     "v" gnus-article-view-part
1839     "o" gnus-article-save-part
1840     "c" gnus-article-copy-part
1841     "C" gnus-article-view-part-as-charset
1842     "e" gnus-article-externalize-part
1843     "E" gnus-article-encrypt-body
1844     "i" gnus-article-inline-part
1845     "|" gnus-article-pipe-part))
1846
1847 (defvar gnus-article-post-menu nil)
1848
1849 (defun gnus-summary-make-menu-bar ()
1850   (gnus-turn-off-edit-menu 'summary)
1851
1852   (unless (boundp 'gnus-summary-misc-menu)
1853
1854     (easy-menu-define
1855       gnus-summary-kill-menu gnus-summary-mode-map ""
1856       (cons
1857        "Score"
1858        (nconc
1859         (list
1860          ["Customize" gnus-score-customize t])
1861         (gnus-make-score-map 'increase)
1862         (gnus-make-score-map 'lower)
1863         '(("Mark"
1864            ["Kill below" gnus-summary-kill-below t]
1865            ["Mark above" gnus-summary-mark-above t]
1866            ["Tick above" gnus-summary-tick-above t]
1867            ["Clear above" gnus-summary-clear-above t])
1868           ["Current score" gnus-summary-current-score t]
1869           ["Set score" gnus-summary-set-score t]
1870           ["Switch current score file..." gnus-score-change-score-file t]
1871           ["Set mark below..." gnus-score-set-mark-below t]
1872           ["Set expunge below..." gnus-score-set-expunge-below t]
1873           ["Edit current score file" gnus-score-edit-current-scores t]
1874           ["Edit score file" gnus-score-edit-file t]
1875           ["Trace score" gnus-score-find-trace t]
1876           ["Find words" gnus-score-find-favourite-words t]
1877           ["Rescore buffer" gnus-summary-rescore t]
1878           ["Increase score..." gnus-summary-increase-score t]
1879           ["Lower score..." gnus-summary-lower-score t]))))
1880
1881     ;; Define both the Article menu in the summary buffer and the
1882     ;; equivalent Commands menu in the article buffer here for
1883     ;; consistency.
1884     (let ((innards
1885            `(("Hide"
1886               ["All" gnus-article-hide t]
1887               ["Headers" gnus-article-hide-headers t]
1888               ["Signature" gnus-article-hide-signature t]
1889               ["Citation" gnus-article-hide-citation t]
1890               ["List identifiers" gnus-article-hide-list-identifiers t]
1891               ["PGP" gnus-article-hide-pgp t]
1892               ["Banner" gnus-article-strip-banner t]
1893               ["Boring headers" gnus-article-hide-boring-headers t])
1894              ("Highlight"
1895               ["All" gnus-article-highlight t]
1896               ["Headers" gnus-article-highlight-headers t]
1897               ["Signature" gnus-article-highlight-signature t]
1898               ["Citation" gnus-article-highlight-citation t])
1899              ("MIME"
1900               ["Words" gnus-article-decode-mime-words t]
1901               ["Charset" gnus-article-decode-charset t]
1902               ["QP" gnus-article-de-quoted-unreadable t]
1903               ["Base64" gnus-article-de-base64-unreadable t]
1904               ["View all" gnus-mime-view-all-parts t]
1905               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1906               ["Encrypt body" gnus-article-encrypt-body t])
1907              ("Date"
1908               ["Local" gnus-article-date-local t]
1909               ["ISO8601" gnus-article-date-iso8601 t]
1910               ["UT" gnus-article-date-ut t]
1911               ["Original" gnus-article-date-original t]
1912               ["Lapsed" gnus-article-date-lapsed t]
1913               ["User-defined" gnus-article-date-user t])
1914              ("Display"
1915               ["Remove images" gnus-article-remove-images t]
1916               ["Toggle smiley" gnus-treat-smiley t]
1917               ["Show X-Face" gnus-article-display-x-face t]
1918               ["Show picons in From" gnus-treat-from-picon t]
1919               ["Show picons in mail headers" gnus-treat-mail-picon t]
1920               ["Show picons in news headers" gnus-treat-newsgroups-picon t])
1921              ("Washing"
1922               ("Remove Blanks"
1923                ["Leading" gnus-article-strip-leading-blank-lines t]
1924                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1925                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1926                ["All of the above" gnus-article-strip-blank-lines t]
1927                ["All" gnus-article-strip-all-blank-lines t]
1928                ["Leading space" gnus-article-strip-leading-space t]
1929                ["Trailing space" gnus-article-strip-trailing-space t]
1930                ["Leading space in headers"
1931                 gnus-article-remove-leading-whitespace t])
1932               ["Overstrike" gnus-article-treat-overstrike t]
1933               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1934               ["Emphasis" gnus-article-emphasize t]
1935               ["Word wrap" gnus-article-fill-cited-article t]
1936               ["Fill long lines" gnus-article-fill-long-lines t]
1937               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1938               ["CR" gnus-article-remove-cr t]
1939               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1940               ["Base64" gnus-article-de-base64-unreadable t]
1941               ["Rot 13" gnus-summary-caesar-message
1942                ,@(if (featurep 'xemacs) '(t)
1943                    '(:help "\"Caesar rotate\" article by 13"))]
1944               ["Unix pipe" gnus-summary-pipe-message t]
1945               ["Add buttons" gnus-article-add-buttons t]
1946               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1947               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1948               ["Verbose header" gnus-summary-verbose-headers t]
1949               ["Toggle header" gnus-summary-toggle-header t]
1950               ["Unfold headers" gnus-article-treat-unfold-headers t]
1951               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
1952               ["Html" gnus-article-wash-html t]
1953               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1954               ["HZ" gnus-article-decode-HZ t])
1955              ("Output"
1956               ["Save in default format" gnus-summary-save-article
1957                ,@(if (featurep 'xemacs) '(t)
1958                    '(:help "Save article using default method"))]
1959               ["Save in file" gnus-summary-save-article-file
1960                ,@(if (featurep 'xemacs) '(t)
1961                    '(:help "Save article in file"))]
1962               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1963               ["Save in MH folder" gnus-summary-save-article-folder t]
1964               ["Save in VM folder" gnus-summary-save-article-vm t]
1965               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1966               ["Save body in file" gnus-summary-save-article-body-file t]
1967               ["Pipe through a filter" gnus-summary-pipe-output t]
1968               ["Add to SOUP packet" gnus-soup-add-article t]
1969               ["Print with Muttprint" gnus-summary-muttprint t]
1970               ["Print" gnus-summary-print-article t])
1971              ("Backend"
1972               ["Respool article..." gnus-summary-respool-article t]
1973               ["Move article..." gnus-summary-move-article
1974                (gnus-check-backend-function
1975                 'request-move-article gnus-newsgroup-name)]
1976               ["Copy article..." gnus-summary-copy-article t]
1977               ["Crosspost article..." gnus-summary-crosspost-article
1978                (gnus-check-backend-function
1979                 'request-replace-article gnus-newsgroup-name)]
1980               ["Import file..." gnus-summary-import-article t]
1981               ["Create article..." gnus-summary-create-article t]
1982               ["Check if posted" gnus-summary-article-posted-p t]
1983               ["Edit article" gnus-summary-edit-article
1984                (not (gnus-group-read-only-p))]
1985               ["Delete article" gnus-summary-delete-article
1986                (gnus-check-backend-function
1987                 'request-expire-articles gnus-newsgroup-name)]
1988               ["Query respool" gnus-summary-respool-query t]
1989               ["Trace respool" gnus-summary-respool-trace t]
1990               ["Delete expirable articles" gnus-summary-expire-articles-now
1991                (gnus-check-backend-function
1992                 'request-expire-articles gnus-newsgroup-name)])
1993              ("Extract"
1994               ["Uudecode" gnus-uu-decode-uu
1995                ,@(if (featurep 'xemacs) '(t)
1996                    '(:help "Decode uuencoded article(s)"))]
1997               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1998               ["Unshar" gnus-uu-decode-unshar t]
1999               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2000               ["Save" gnus-uu-decode-save t]
2001               ["Binhex" gnus-uu-decode-binhex t]
2002               ["Postscript" gnus-uu-decode-postscript t])
2003              ("Cache"
2004               ["Enter article" gnus-cache-enter-article t]
2005               ["Remove article" gnus-cache-remove-article t])
2006              ["Translate" gnus-article-babel t]
2007              ["Select article buffer" gnus-summary-select-article-buffer t]
2008              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2009              ["Isearch article..." gnus-summary-isearch-article t]
2010              ["Beginning of the article" gnus-summary-beginning-of-article t]
2011              ["End of the article" gnus-summary-end-of-article t]
2012              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2013              ["Fetch referenced articles" gnus-summary-refer-references t]
2014              ["Fetch current thread" gnus-summary-refer-thread t]
2015              ["Fetch article with id..." gnus-summary-refer-article t]
2016              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2017              ["Redisplay" gnus-summary-show-article t]
2018              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2019       (easy-menu-define
2020         gnus-summary-article-menu gnus-summary-mode-map ""
2021         (cons "Article" innards))
2022
2023       (if (not (keymapp gnus-summary-article-menu))
2024           (easy-menu-define
2025             gnus-article-commands-menu gnus-article-mode-map ""
2026             (cons "Commands" innards))
2027         ;; in Emacs, don't share menu.
2028         (setq gnus-article-commands-menu
2029               (copy-keymap gnus-summary-article-menu))
2030         (define-key gnus-article-mode-map [menu-bar commands]
2031           (cons "Commands" gnus-article-commands-menu))))
2032
2033     (easy-menu-define
2034       gnus-summary-thread-menu gnus-summary-mode-map ""
2035       '("Threads"
2036         ["Toggle threading" gnus-summary-toggle-threads t]
2037         ["Hide threads" gnus-summary-hide-all-threads t]
2038         ["Show threads" gnus-summary-show-all-threads t]
2039         ["Hide thread" gnus-summary-hide-thread t]
2040         ["Show thread" gnus-summary-show-thread t]
2041         ["Go to next thread" gnus-summary-next-thread t]
2042         ["Go to previous thread" gnus-summary-prev-thread t]
2043         ["Go down thread" gnus-summary-down-thread t]
2044         ["Go up thread" gnus-summary-up-thread t]
2045         ["Top of thread" gnus-summary-top-thread t]
2046         ["Mark thread as read" gnus-summary-kill-thread t]
2047         ["Lower thread score" gnus-summary-lower-thread t]
2048         ["Raise thread score" gnus-summary-raise-thread t]
2049         ["Rethread current" gnus-summary-rethread-current t]))
2050
2051     (easy-menu-define
2052       gnus-summary-post-menu gnus-summary-mode-map ""
2053       `("Post"
2054         ["Send a message (mail or news)" gnus-summary-post-news
2055          ,@(if (featurep 'xemacs) '(t)
2056              '(:help "Post an article"))]
2057         ["Followup" gnus-summary-followup
2058          ,@(if (featurep 'xemacs) '(t)
2059              '(:help "Post followup to this article"))]
2060         ["Followup and yank" gnus-summary-followup-with-original
2061          ,@(if (featurep 'xemacs) '(t)
2062              '(:help "Post followup to this article, quoting its contents"))]
2063         ["Supersede article" gnus-summary-supersede-article t]
2064         ["Cancel article" gnus-summary-cancel-article
2065          ,@(if (featurep 'xemacs) '(t)
2066              '(:help "Cancel an article you posted"))]
2067         ["Reply" gnus-summary-reply t]
2068         ["Reply and yank" gnus-summary-reply-with-original t]
2069         ["Wide reply" gnus-summary-wide-reply t]
2070         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2071          ,@(if (featurep 'xemacs) '(t)
2072              '(:help "Mail a reply, quoting this article"))]
2073         ["Very wide reply" gnus-summary-very-wide-reply t]
2074         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2075         ,@(if (featurep 'xemacs) '(t)
2076             '(:help "Mail a very wide reply, quoting this article"))]
2077         ["Mail forward" gnus-summary-mail-forward t]
2078         ["Post forward" gnus-summary-post-forward t]
2079         ["Digest and mail" gnus-uu-digest-mail-forward t]
2080         ["Digest and post" gnus-uu-digest-post-forward t]
2081         ["Resend message" gnus-summary-resend-message t]
2082         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2083         ["Send a mail" gnus-summary-mail-other-window t]
2084         ["Create a local message" gnus-summary-news-other-window t]
2085         ["Uuencode and post" gnus-uu-post-news
2086          ,@(if (featurep 'xemacs) '(t)
2087              '(:help "Post a uuencoded article"))]
2088         ["Followup via news" gnus-summary-followup-to-mail t]
2089         ["Followup via news and yank"
2090          gnus-summary-followup-to-mail-with-original t]
2091         ;;("Draft"
2092         ;;["Send" gnus-summary-send-draft t]
2093         ;;["Send bounced" gnus-resend-bounced-mail t])
2094         ))
2095
2096     (cond
2097      ((not (keymapp gnus-summary-post-menu))
2098       (setq gnus-article-post-menu gnus-summary-post-menu))
2099      ((not gnus-article-post-menu)
2100       ;; Don't share post menu.
2101       (setq gnus-article-post-menu
2102             (copy-keymap gnus-summary-post-menu))))
2103     (define-key gnus-article-mode-map [menu-bar post]
2104       (cons "Post" gnus-article-post-menu))
2105
2106     (easy-menu-define
2107       gnus-summary-misc-menu gnus-summary-mode-map ""
2108       `("Gnus"
2109         ("Mark Read"
2110          ["Mark as read" gnus-summary-mark-as-read-forward t]
2111          ["Mark same subject and select"
2112           gnus-summary-kill-same-subject-and-select t]
2113          ["Mark same subject" gnus-summary-kill-same-subject t]
2114          ["Catchup" gnus-summary-catchup
2115           ,@(if (featurep 'xemacs) '(t)
2116               '(:help "Mark unread articles in this group as read"))]
2117          ["Catchup all" gnus-summary-catchup-all t]
2118          ["Catchup to here" gnus-summary-catchup-to-here t]
2119          ["Catchup from here" gnus-summary-catchup-from-here t]
2120          ["Catchup region" gnus-summary-mark-region-as-read t]
2121          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2122         ("Mark Various"
2123          ["Tick" gnus-summary-tick-article-forward t]
2124          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2125          ["Remove marks" gnus-summary-clear-mark-forward t]
2126          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2127          ["Set bookmark" gnus-summary-set-bookmark t]
2128          ["Remove bookmark" gnus-summary-remove-bookmark t])
2129         ("Limit to"
2130          ["Marks..." gnus-summary-limit-to-marks t]
2131          ["Subject..." gnus-summary-limit-to-subject t]
2132          ["Author..." gnus-summary-limit-to-author t]
2133          ["Age..." gnus-summary-limit-to-age t]
2134          ["Extra..." gnus-summary-limit-to-extra t]
2135          ["Score" gnus-summary-limit-to-score t]
2136          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2137          ["Unread" gnus-summary-limit-to-unread t]
2138          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2139          ["Articles" gnus-summary-limit-to-articles t]
2140          ["Pop limit" gnus-summary-pop-limit t]
2141          ["Show dormant" gnus-summary-limit-include-dormant t]
2142          ["Hide childless dormant"
2143           gnus-summary-limit-exclude-childless-dormant t]
2144          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2145          ["Hide marked" gnus-summary-limit-exclude-marks t]
2146          ["Show expunged" gnus-summary-limit-include-expunged t])
2147         ("Process Mark"
2148          ["Set mark" gnus-summary-mark-as-processable t]
2149          ["Remove mark" gnus-summary-unmark-as-processable t]
2150          ["Remove all marks" gnus-summary-unmark-all-processable t]
2151          ["Mark above" gnus-uu-mark-over t]
2152          ["Mark series" gnus-uu-mark-series t]
2153          ["Mark region" gnus-uu-mark-region t]
2154          ["Unmark region" gnus-uu-unmark-region t]
2155          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2156          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2157          ["Mark all" gnus-uu-mark-all t]
2158          ["Mark buffer" gnus-uu-mark-buffer t]
2159          ["Mark sparse" gnus-uu-mark-sparse t]
2160          ["Mark thread" gnus-uu-mark-thread t]
2161          ["Unmark thread" gnus-uu-unmark-thread t]
2162          ("Process Mark Sets"
2163           ["Kill" gnus-summary-kill-process-mark t]
2164           ["Yank" gnus-summary-yank-process-mark
2165            gnus-newsgroup-process-stack]
2166           ["Save" gnus-summary-save-process-mark t]))
2167         ("Scroll article"
2168          ["Page forward" gnus-summary-next-page
2169           ,@(if (featurep 'xemacs) '(t)
2170               '(:help "Show next page of article"))]
2171          ["Page backward" gnus-summary-prev-page
2172           ,@(if (featurep 'xemacs) '(t)
2173               '(:help "Show previous page of article"))]
2174          ["Line forward" gnus-summary-scroll-up t])
2175         ("Move"
2176          ["Next unread article" gnus-summary-next-unread-article t]
2177          ["Previous unread article" gnus-summary-prev-unread-article t]
2178          ["Next article" gnus-summary-next-article t]
2179          ["Previous article" gnus-summary-prev-article t]
2180          ["Next unread subject" gnus-summary-next-unread-subject t]
2181          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2182          ["Next article same subject" gnus-summary-next-same-subject t]
2183          ["Previous article same subject" gnus-summary-prev-same-subject t]
2184          ["First unread article" gnus-summary-first-unread-article t]
2185          ["Best unread article" gnus-summary-best-unread-article t]
2186          ["Go to subject number..." gnus-summary-goto-subject t]
2187          ["Go to article number..." gnus-summary-goto-article t]
2188          ["Go to the last article" gnus-summary-goto-last-article t]
2189          ["Pop article off history" gnus-summary-pop-article t])
2190         ("Sort"
2191          ["Sort by number" gnus-summary-sort-by-number t]
2192          ["Sort by author" gnus-summary-sort-by-author t]
2193          ["Sort by subject" gnus-summary-sort-by-subject t]
2194          ["Sort by date" gnus-summary-sort-by-date t]
2195          ["Sort by score" gnus-summary-sort-by-score t]
2196          ["Sort by lines" gnus-summary-sort-by-lines t]
2197          ["Sort by characters" gnus-summary-sort-by-chars t]
2198          ["Original sort" gnus-summary-sort-by-original t])
2199         ("Help"
2200          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2201          ["Describe group" gnus-summary-describe-group t]
2202          ["Read manual" gnus-info-find-node t])
2203         ("Modes"
2204          ["Pick and read" gnus-pick-mode t]
2205          ["Binary" gnus-binary-mode t])
2206         ("Regeneration"
2207          ["Regenerate" gnus-summary-prepare t]
2208          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2209          ["Toggle threading" gnus-summary-toggle-threads t])
2210         ["See old articles" gnus-summary-insert-old-articles t]
2211         ["See new articles" gnus-summary-insert-new-articles t]
2212         ["Filter articles..." gnus-summary-execute-command t]
2213         ["Run command on subjects..." gnus-summary-universal-argument t]
2214         ["Search articles forward..." gnus-summary-search-article-forward t]
2215         ["Search articles backward..." gnus-summary-search-article-backward t]
2216         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2217         ["Expand window" gnus-summary-expand-window t]
2218         ["Expire expirable articles" gnus-summary-expire-articles
2219          (gnus-check-backend-function
2220           'request-expire-articles gnus-newsgroup-name)]
2221         ["Edit local kill file" gnus-summary-edit-local-kill t]
2222         ["Edit main kill file" gnus-summary-edit-global-kill t]
2223         ["Edit group parameters" gnus-summary-edit-parameters t]
2224         ["Customize group parameters" gnus-summary-customize-parameters t]
2225         ["Send a bug report" gnus-bug t]
2226         ("Exit"
2227          ["Catchup and exit" gnus-summary-catchup-and-exit
2228           ,@(if (featurep 'xemacs) '(t)
2229               '(:help "Mark unread articles in this group as read, then exit"))]
2230          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2231          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2232          ["Exit group" gnus-summary-exit
2233           ,@(if (featurep 'xemacs) '(t)
2234               '(:help "Exit current group, return to group selection mode"))]
2235          ["Exit group without updating" gnus-summary-exit-no-update t]
2236          ["Exit and goto next group" gnus-summary-next-group t]
2237          ["Exit and goto prev group" gnus-summary-prev-group t]
2238          ["Reselect group" gnus-summary-reselect-current-group t]
2239          ["Rescan group" gnus-summary-rescan-group t]
2240          ["Update dribble" gnus-summary-save-newsrc t])))
2241
2242     (gnus-run-hooks 'gnus-summary-menu-hook)))
2243
2244 (defvar gnus-summary-tool-bar-map nil)
2245
2246 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2247 (defun gnus-summary-make-tool-bar ()
2248   (if (and (fboundp 'tool-bar-add-item-from-menu)
2249            (default-value 'tool-bar-mode)
2250            (not gnus-summary-tool-bar-map))
2251       (setq gnus-summary-tool-bar-map
2252             (let ((tool-bar-map (make-sparse-keymap))
2253                   (load-path (mm-image-load-path)))
2254               (tool-bar-add-item-from-menu
2255                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2256               (tool-bar-add-item-from-menu
2257                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2258               (tool-bar-add-item-from-menu
2259                'gnus-summary-post-news "post" gnus-summary-mode-map)
2260               (tool-bar-add-item-from-menu
2261                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2262               (tool-bar-add-item-from-menu
2263                'gnus-summary-followup "followup" gnus-summary-mode-map)
2264               (tool-bar-add-item-from-menu
2265                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2266               (tool-bar-add-item-from-menu
2267                'gnus-summary-reply "reply" gnus-summary-mode-map)
2268               (tool-bar-add-item-from-menu
2269                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2270               (tool-bar-add-item-from-menu
2271                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2272               (tool-bar-add-item-from-menu
2273                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2274               (tool-bar-add-item-from-menu
2275                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2276               (tool-bar-add-item-from-menu
2277                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2278               (tool-bar-add-item-from-menu
2279                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2280               (tool-bar-add-item-from-menu
2281                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2282               (tool-bar-add-item-from-menu
2283                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2284               tool-bar-map)))
2285   (if gnus-summary-tool-bar-map
2286       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2287
2288 (defun gnus-score-set-default (var value)
2289   "A version of set that updates the GNU Emacs menu-bar."
2290   (set var value)
2291   ;; It is the message that forces the active status to be updated.
2292   (message ""))
2293
2294 (defun gnus-make-score-map (type)
2295   "Make a summary score map of type TYPE."
2296   (if t
2297       nil
2298     (let ((headers '(("author" "from" string)
2299                      ("subject" "subject" string)
2300                      ("article body" "body" string)
2301                      ("article head" "head" string)
2302                      ("xref" "xref" string)
2303                      ("extra header" "extra" string)
2304                      ("lines" "lines" number)
2305                      ("followups to author" "followup" string)))
2306           (types '((number ("less than" <)
2307                            ("greater than" >)
2308                            ("equal" =))
2309                    (string ("substring" s)
2310                            ("exact string" e)
2311                            ("fuzzy string" f)
2312                            ("regexp" r))))
2313           (perms '(("temporary" (current-time-string))
2314                    ("permanent" nil)
2315                    ("immediate" now)))
2316           header)
2317       (list
2318        (apply
2319         'nconc
2320         (list
2321          (if (eq type 'lower)
2322              "Lower score"
2323            "Increase score"))
2324         (let (outh)
2325           (while headers
2326             (setq header (car headers))
2327             (setq outh
2328                   (cons
2329                    (apply
2330                     'nconc
2331                     (list (car header))
2332                     (let ((ts (cdr (assoc (nth 2 header) types)))
2333                           outt)
2334                       (while ts
2335                         (setq outt
2336                               (cons
2337                                (apply
2338                                 'nconc
2339                                 (list (caar ts))
2340                                 (let ((ps perms)
2341                                       outp)
2342                                   (while ps
2343                                     (setq outp
2344                                           (cons
2345                                            (vector
2346                                             (caar ps)
2347                                             (list
2348                                              'gnus-summary-score-entry
2349                                              (nth 1 header)
2350                                              (if (or (string= (nth 1 header)
2351                                                               "head")
2352                                                      (string= (nth 1 header)
2353                                                               "body"))
2354                                                  ""
2355                                                (list 'gnus-summary-header
2356                                                      (nth 1 header)))
2357                                              (list 'quote (nth 1 (car ts)))
2358                                              (list 'gnus-score-delta-default
2359                                                    nil)
2360                                              (nth 1 (car ps))
2361                                              t)
2362                                             t)
2363                                            outp))
2364                                     (setq ps (cdr ps)))
2365                                   (list (nreverse outp))))
2366                                outt))
2367                         (setq ts (cdr ts)))
2368                       (list (nreverse outt))))
2369                    outh))
2370             (setq headers (cdr headers)))
2371           (list (nreverse outh))))))))
2372
2373 \f
2374
2375 (defun gnus-summary-mode (&optional group)
2376   "Major mode for reading articles.
2377
2378 All normal editing commands are switched off.
2379 \\<gnus-summary-mode-map>
2380 Each line in this buffer represents one article.  To read an
2381 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2382 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2383 respectively.
2384
2385 You can also post articles and send mail from this buffer.  To
2386 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2387 of an article, type `\\[gnus-summary-reply]'.
2388
2389 There are approx. one gazillion commands you can execute in this
2390 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2391
2392 The following commands are available:
2393
2394 \\{gnus-summary-mode-map}"
2395   (interactive)
2396   (kill-all-local-variables)
2397   (when (gnus-visual-p 'summary-menu 'menu)
2398     (gnus-summary-make-menu-bar)
2399     (gnus-summary-make-tool-bar))
2400   (gnus-summary-make-local-variables)
2401   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2402     (gnus-summary-make-local-variables))
2403   (gnus-make-thread-indent-array)
2404   (gnus-simplify-mode-line)
2405   (setq major-mode 'gnus-summary-mode)
2406   (setq mode-name "Summary")
2407   (make-local-variable 'minor-mode-alist)
2408   (use-local-map gnus-summary-mode-map)
2409   (buffer-disable-undo)
2410   (setq buffer-read-only t)             ;Disable modification
2411   (setq truncate-lines t)
2412   (setq selective-display t)
2413   (setq selective-display-ellipses t)   ;Display `...'
2414   (gnus-summary-set-display-table)
2415   (gnus-set-default-directory)
2416   (setq gnus-newsgroup-name group)
2417   (make-local-variable 'gnus-summary-line-format)
2418   (make-local-variable 'gnus-summary-line-format-spec)
2419   (make-local-variable 'gnus-summary-dummy-line-format)
2420   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2421   (make-local-variable 'gnus-summary-mark-positions)
2422   (make-local-hook 'pre-command-hook)
2423   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2424   (gnus-run-hooks 'gnus-summary-mode-hook)
2425   (turn-on-gnus-mailing-list-mode)
2426   (mm-enable-multibyte)
2427   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2428   (gnus-update-summary-mark-positions))
2429
2430 (defun gnus-summary-make-local-variables ()
2431   "Make all the local summary buffer variables."
2432   (let (global)
2433     (dolist (local gnus-summary-local-variables)
2434       (if (consp local)
2435           (progn
2436             (if (eq (cdr local) 'global)
2437                 ;; Copy the global value of the variable.
2438                 (setq global (symbol-value (car local)))
2439               ;; Use the value from the list.
2440               (setq global (eval (cdr local))))
2441             (set (make-local-variable (car local)) global))
2442         ;; Simple nil-valued local variable.
2443         (set (make-local-variable local) nil)))))
2444
2445 (defun gnus-summary-clear-local-variables ()
2446   (let ((locals gnus-summary-local-variables))
2447     (while locals
2448       (if (consp (car locals))
2449           (and (vectorp (caar locals))
2450                (set (caar locals) nil))
2451         (and (vectorp (car locals))
2452              (set (car locals) nil)))
2453       (setq locals (cdr locals)))))
2454
2455 ;; Summary data functions.
2456
2457 (defmacro gnus-data-number (data)
2458   `(car ,data))
2459
2460 (defmacro gnus-data-set-number (data number)
2461   `(setcar ,data ,number))
2462
2463 (defmacro gnus-data-mark (data)
2464   `(nth 1 ,data))
2465
2466 (defmacro gnus-data-set-mark (data mark)
2467   `(setcar (nthcdr 1 ,data) ,mark))
2468
2469 (defmacro gnus-data-pos (data)
2470   `(nth 2 ,data))
2471
2472 (defmacro gnus-data-set-pos (data pos)
2473   `(setcar (nthcdr 2 ,data) ,pos))
2474
2475 (defmacro gnus-data-header (data)
2476   `(nth 3 ,data))
2477
2478 (defmacro gnus-data-set-header (data header)
2479   `(setf (nth 3 ,data) ,header))
2480
2481 (defmacro gnus-data-level (data)
2482   `(nth 4 ,data))
2483
2484 (defmacro gnus-data-unread-p (data)
2485   `(= (nth 1 ,data) gnus-unread-mark))
2486
2487 (defmacro gnus-data-read-p (data)
2488   `(/= (nth 1 ,data) gnus-unread-mark))
2489
2490 (defmacro gnus-data-pseudo-p (data)
2491   `(consp (nth 3 ,data)))
2492
2493 (defmacro gnus-data-find (number)
2494   `(assq ,number gnus-newsgroup-data))
2495
2496 (defmacro gnus-data-find-list (number &optional data)
2497   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2498      (memq (assq ,number bdata)
2499            bdata)))
2500
2501 (defmacro gnus-data-make (number mark pos header level)
2502   `(list ,number ,mark ,pos ,header ,level))
2503
2504 (defun gnus-data-enter (after-article number mark pos header level offset)
2505   (let ((data (gnus-data-find-list after-article)))
2506     (unless data
2507       (error "No such article: %d" after-article))
2508     (setcdr data (cons (gnus-data-make number mark pos header level)
2509                        (cdr data)))
2510     (setq gnus-newsgroup-data-reverse nil)
2511     (gnus-data-update-list (cddr data) offset)))
2512
2513 (defun gnus-data-enter-list (after-article list &optional offset)
2514   (when list
2515     (let ((data (and after-article (gnus-data-find-list after-article)))
2516           (ilist list))
2517       (if (not (or data
2518                    after-article))
2519           (let ((odata gnus-newsgroup-data))
2520             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2521             (when offset
2522               (gnus-data-update-list odata offset)))
2523       ;; Find the last element in the list to be spliced into the main
2524         ;; list.
2525         (while (cdr list)
2526           (setq list (cdr list)))
2527         (if (not data)
2528             (progn
2529               (setcdr list gnus-newsgroup-data)
2530               (setq gnus-newsgroup-data ilist)
2531               (when offset
2532                 (gnus-data-update-list (cdr list) offset)))
2533           (setcdr list (cdr data))
2534           (setcdr data ilist)
2535           (when offset
2536             (gnus-data-update-list (cdr list) offset))))
2537       (setq gnus-newsgroup-data-reverse nil))))
2538
2539 (defun gnus-data-remove (article &optional offset)
2540   (let ((data gnus-newsgroup-data))
2541     (if (= (gnus-data-number (car data)) article)
2542         (progn
2543           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2544                 gnus-newsgroup-data-reverse nil)
2545           (when offset
2546             (gnus-data-update-list gnus-newsgroup-data offset)))
2547       (while (cdr data)
2548         (when (= (gnus-data-number (cadr data)) article)
2549           (setcdr data (cddr data))
2550           (when offset
2551             (gnus-data-update-list (cdr data) offset))
2552           (setq data nil
2553                 gnus-newsgroup-data-reverse nil))
2554         (setq data (cdr data))))))
2555
2556 (defmacro gnus-data-list (backward)
2557   `(if ,backward
2558        (or gnus-newsgroup-data-reverse
2559            (setq gnus-newsgroup-data-reverse
2560                  (reverse gnus-newsgroup-data)))
2561      gnus-newsgroup-data))
2562
2563 (defun gnus-data-update-list (data offset)
2564   "Add OFFSET to the POS of all data entries in DATA."
2565   (setq gnus-newsgroup-data-reverse nil)
2566   (while data
2567     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2568     (setq data (cdr data))))
2569
2570 (defun gnus-summary-article-pseudo-p (article)
2571   "Say whether this article is a pseudo article or not."
2572   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2573
2574 (defmacro gnus-summary-article-sparse-p (article)
2575   "Say whether this article is a sparse article or not."
2576   `(memq ,article gnus-newsgroup-sparse))
2577
2578 (defmacro gnus-summary-article-ancient-p (article)
2579   "Say whether this article is a sparse article or not."
2580   `(memq ,article gnus-newsgroup-ancient))
2581
2582 (defun gnus-article-parent-p (number)
2583   "Say whether this article is a parent or not."
2584   (let ((data (gnus-data-find-list number)))
2585     (and (cdr data)              ; There has to be an article after...
2586          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2587             (gnus-data-level (nth 1 data))))))
2588
2589 (defun gnus-article-children (number)
2590   "Return a list of all children to NUMBER."
2591   (let* ((data (gnus-data-find-list number))
2592          (level (gnus-data-level (car data)))
2593          children)
2594     (setq data (cdr data))
2595     (while (and data
2596                 (= (gnus-data-level (car data)) (1+ level)))
2597       (push (gnus-data-number (car data)) children)
2598       (setq data (cdr data)))
2599     children))
2600
2601 (defmacro gnus-summary-skip-intangible ()
2602   "If the current article is intangible, then jump to a different article."
2603   '(let ((to (get-text-property (point) 'gnus-intangible)))
2604      (and to (gnus-summary-goto-subject to))))
2605
2606 (defmacro gnus-summary-article-intangible-p ()
2607   "Say whether this article is intangible or not."
2608   '(get-text-property (point) 'gnus-intangible))
2609
2610 (defun gnus-article-read-p (article)
2611   "Say whether ARTICLE is read or not."
2612   (not (or (memq article gnus-newsgroup-marked)
2613            (memq article gnus-newsgroup-unreads)
2614            (memq article gnus-newsgroup-unselected)
2615            (memq article gnus-newsgroup-dormant))))
2616
2617 ;; Some summary mode macros.
2618
2619 (defmacro gnus-summary-article-number ()
2620   "The article number of the article on the current line.
2621 If there isn's an article number here, then we return the current
2622 article number."
2623   '(progn
2624      (gnus-summary-skip-intangible)
2625      (or (get-text-property (point) 'gnus-number)
2626          (gnus-summary-last-subject))))
2627
2628 (defmacro gnus-summary-article-header (&optional number)
2629   "Return the header of article NUMBER."
2630   `(gnus-data-header (gnus-data-find
2631                       ,(or number '(gnus-summary-article-number)))))
2632
2633 (defmacro gnus-summary-thread-level (&optional number)
2634   "Return the level of thread that starts with article NUMBER."
2635   `(if (and (eq gnus-summary-make-false-root 'dummy)
2636             (get-text-property (point) 'gnus-intangible))
2637        0
2638      (gnus-data-level (gnus-data-find
2639                        ,(or number '(gnus-summary-article-number))))))
2640
2641 (defmacro gnus-summary-article-mark (&optional number)
2642   "Return the mark of article NUMBER."
2643   `(gnus-data-mark (gnus-data-find
2644                     ,(or number '(gnus-summary-article-number)))))
2645
2646 (defmacro gnus-summary-article-pos (&optional number)
2647   "Return the position of the line of article NUMBER."
2648   `(gnus-data-pos (gnus-data-find
2649                    ,(or number '(gnus-summary-article-number)))))
2650
2651 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2652 (defmacro gnus-summary-article-subject (&optional number)
2653   "Return current subject string or nil if nothing."
2654   `(let ((headers
2655           ,(if number
2656                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2657              '(gnus-data-header (assq (gnus-summary-article-number)
2658                                       gnus-newsgroup-data)))))
2659      (and headers
2660           (vectorp headers)
2661           (mail-header-subject headers))))
2662
2663 (defmacro gnus-summary-article-score (&optional number)
2664   "Return current article score."
2665   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2666                   gnus-newsgroup-scored))
2667        gnus-summary-default-score 0))
2668
2669 (defun gnus-summary-article-children (&optional number)
2670   "Return a list of article numbers that are children of article NUMBER."
2671   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2672          (level (gnus-data-level (car data)))
2673          l children)
2674     (while (and (setq data (cdr data))
2675                 (> (setq l (gnus-data-level (car data))) level))
2676       (and (= (1+ level) l)
2677            (push (gnus-data-number (car data))
2678                  children)))
2679     (nreverse children)))
2680
2681 (defun gnus-summary-article-parent (&optional number)
2682   "Return the article number of the parent of article NUMBER."
2683   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2684                                     (gnus-data-list t)))
2685          (level (gnus-data-level (car data))))
2686     (if (zerop level)
2687         ()                              ; This is a root.
2688       ;; We search until we find an article with a level less than
2689       ;; this one.  That function has to be the parent.
2690       (while (and (setq data (cdr data))
2691                   (not (< (gnus-data-level (car data)) level))))
2692       (and data (gnus-data-number (car data))))))
2693
2694 (defun gnus-unread-mark-p (mark)
2695   "Say whether MARK is the unread mark."
2696   (= mark gnus-unread-mark))
2697
2698 (defun gnus-read-mark-p (mark)
2699   "Say whether MARK is one of the marks that mark as read.
2700 This is all marks except unread, ticked, dormant, and expirable."
2701   (not (or (= mark gnus-unread-mark)
2702            (= mark gnus-ticked-mark)
2703            (= mark gnus-dormant-mark)
2704            (= mark gnus-expirable-mark))))
2705
2706 (defmacro gnus-article-mark (number)
2707   "Return the MARK of article NUMBER.
2708 This macro should only be used when computing the mark the \"first\"
2709 time; i.e., when generating the summary lines.  After that,
2710 `gnus-summary-article-mark' should be used to examine the
2711 marks of articles."
2712   `(cond
2713     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2714     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2715     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2716     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2717     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2718     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2719     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2720     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2721            gnus-ancient-mark))))
2722
2723 ;; Saving hidden threads.
2724
2725 (defmacro gnus-save-hidden-threads (&rest forms)
2726   "Save hidden threads, eval FORMS, and restore the hidden threads."
2727   (let ((config (make-symbol "config")))
2728     `(let ((,config (gnus-hidden-threads-configuration)))
2729        (unwind-protect
2730            (save-excursion
2731              ,@forms)
2732          (gnus-restore-hidden-threads-configuration ,config)))))
2733 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2734 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2735
2736 (defun gnus-data-compute-positions ()
2737   "Compute the positions of all articles."
2738   (setq gnus-newsgroup-data-reverse nil)
2739   (let ((data gnus-newsgroup-data))
2740     (save-excursion
2741       (gnus-save-hidden-threads
2742         (gnus-summary-show-all-threads)
2743         (goto-char (point-min))
2744         (while data
2745           (while (get-text-property (point) 'gnus-intangible)
2746             (forward-line 1))
2747           (gnus-data-set-pos (car data) (+ (point) 3))
2748           (setq data (cdr data))
2749           (forward-line 1))))))
2750
2751 (defun gnus-hidden-threads-configuration ()
2752   "Return the current hidden threads configuration."
2753   (save-excursion
2754     (let (config)
2755       (goto-char (point-min))
2756       (while (search-forward "\r" nil t)
2757         (push (1- (point)) config))
2758       config)))
2759
2760 (defun gnus-restore-hidden-threads-configuration (config)
2761   "Restore hidden threads configuration from CONFIG."
2762   (save-excursion
2763     (let (point buffer-read-only)
2764       (while (setq point (pop config))
2765         (when (and (< point (point-max))
2766                    (goto-char point)
2767                    (eq (char-after) ?\n))
2768           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2769
2770 ;; Various summary mode internalish functions.
2771
2772 (defun gnus-mouse-pick-article (e)
2773   (interactive "e")
2774   (mouse-set-point e)
2775   (gnus-summary-next-page nil t))
2776
2777 (defun gnus-summary-set-display-table ()
2778   "Change the display table.
2779 Odd characters have a tendency to mess
2780 up nicely formatted displays - we make all possible glyphs
2781 display only a single character."
2782
2783   ;; We start from the standard display table, if any.
2784   (let ((table (or (copy-sequence standard-display-table)
2785                    (make-display-table)))
2786         (i 32))
2787     ;; Nix out all the control chars...
2788     (while (>= (setq i (1- i)) 0)
2789       (aset table i [??]))
2790    ;; ... but not newline and cr, of course.  (cr is necessary for the
2791     ;; selective display).
2792     (aset table ?\n nil)
2793     (aset table ?\r nil)
2794     ;; We keep TAB as well.
2795     (aset table ?\t nil)
2796     ;; We nix out any glyphs over 126 that are not set already.
2797     (let ((i 256))
2798       (while (>= (setq i (1- i)) 127)
2799         ;; Only modify if the entry is nil.
2800         (unless (aref table i)
2801           (aset table i [??]))))
2802     (setq buffer-display-table table)))
2803
2804 (defun gnus-summary-set-article-display-arrow (pos)
2805   "Update the overlay arrow to point to line at position POS."
2806   (when (and gnus-summary-display-arrow
2807              (boundp 'overlay-arrow-position)
2808              (boundp 'overlay-arrow-string))
2809     (save-excursion
2810       (goto-char pos)
2811       (beginning-of-line)
2812       (unless overlay-arrow-position
2813         (setq overlay-arrow-position (make-marker)))
2814       (setq overlay-arrow-string "=>"
2815             overlay-arrow-position (set-marker overlay-arrow-position
2816                                                (point)
2817                                                (current-buffer))))))
2818
2819 (defun gnus-summary-buffer-name (group)
2820   "Return the summary buffer name of GROUP."
2821   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2822
2823 (defun gnus-summary-setup-buffer (group)
2824   "Initialize summary buffer."
2825   (let ((buffer (gnus-summary-buffer-name group)))
2826     (if (get-buffer buffer)
2827         (progn
2828           (set-buffer buffer)
2829           (setq gnus-summary-buffer (current-buffer))
2830           (not gnus-newsgroup-prepared))
2831       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2832       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2833       (gnus-summary-mode group)
2834       (when gnus-carpal
2835         (gnus-carpal-setup-buffer 'summary))
2836       (unless gnus-single-article-buffer
2837         (make-local-variable 'gnus-article-buffer)
2838         (make-local-variable 'gnus-article-current)
2839         (make-local-variable 'gnus-original-article-buffer))
2840       (setq gnus-newsgroup-name group)
2841       ;; Set any local variables in the group parameters.
2842       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2843       t)))
2844
2845 (defun gnus-set-global-variables ()
2846   "Set the global equivalents of the buffer-local variables.
2847 They are set to the latest values they had.  These reflect the summary
2848 buffer that was in action when the last article was fetched."
2849   (when (eq major-mode 'gnus-summary-mode)
2850     (setq gnus-summary-buffer (current-buffer))
2851     (let ((name gnus-newsgroup-name)
2852           (marked gnus-newsgroup-marked)
2853           (unread gnus-newsgroup-unreads)
2854           (headers gnus-current-headers)
2855           (data gnus-newsgroup-data)
2856           (summary gnus-summary-buffer)
2857           (article-buffer gnus-article-buffer)
2858           (original gnus-original-article-buffer)
2859           (gac gnus-article-current)
2860           (reffed gnus-reffed-article-number)
2861           (score-file gnus-current-score-file)
2862           (default-charset gnus-newsgroup-charset)
2863           vlist)
2864       (let ((locals gnus-newsgroup-variables))
2865         (while locals
2866           (if (consp (car locals))
2867               (push (eval (caar locals)) vlist)
2868             (push (eval (car locals)) vlist))
2869           (setq locals (cdr locals)))
2870         (setq vlist (nreverse vlist)))
2871       (save-excursion
2872         (set-buffer gnus-group-buffer)
2873         (setq gnus-newsgroup-name name
2874               gnus-newsgroup-marked marked
2875               gnus-newsgroup-unreads unread
2876               gnus-current-headers headers
2877               gnus-newsgroup-data data
2878               gnus-article-current gac
2879               gnus-summary-buffer summary
2880               gnus-article-buffer article-buffer
2881               gnus-original-article-buffer original
2882               gnus-reffed-article-number reffed
2883               gnus-current-score-file score-file
2884               gnus-newsgroup-charset default-charset)
2885         (let ((locals gnus-newsgroup-variables))
2886           (while locals
2887             (if (consp (car locals))
2888                 (set (caar locals) (pop vlist))
2889               (set (car locals) (pop vlist)))
2890             (setq locals (cdr locals))))
2891         ;; The article buffer also has local variables.
2892         (when (gnus-buffer-live-p gnus-article-buffer)
2893           (set-buffer gnus-article-buffer)
2894           (setq gnus-summary-buffer summary))))))
2895
2896 (defun gnus-summary-article-unread-p (article)
2897   "Say whether ARTICLE is unread or not."
2898   (memq article gnus-newsgroup-unreads))
2899
2900 (defun gnus-summary-first-article-p (&optional article)
2901   "Return whether ARTICLE is the first article in the buffer."
2902   (if (not (setq article (or article (gnus-summary-article-number))))
2903       nil
2904     (eq article (caar gnus-newsgroup-data))))
2905
2906 (defun gnus-summary-last-article-p (&optional article)
2907   "Return whether ARTICLE is the last article in the buffer."
2908   (if (not (setq article (or article (gnus-summary-article-number))))
2909       ;; All non-existent numbers are the last article.  :-)
2910       t
2911     (not (cdr (gnus-data-find-list article)))))
2912
2913 (defun gnus-make-thread-indent-array ()
2914   (let ((n 200))
2915     (unless (and gnus-thread-indent-array
2916                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2917       (setq gnus-thread-indent-array (make-vector 201 "")
2918             gnus-thread-indent-array-level gnus-thread-indent-level)
2919       (while (>= n 0)
2920         (aset gnus-thread-indent-array n
2921               (make-string (* n gnus-thread-indent-level) ? ))
2922         (setq n (1- n))))))
2923
2924 (defun gnus-update-summary-mark-positions ()
2925   "Compute where the summary marks are to go."
2926   (save-excursion
2927     (when (gnus-buffer-exists-p gnus-summary-buffer)
2928       (set-buffer gnus-summary-buffer))
2929     (let ((gnus-replied-mark 129)
2930           (gnus-score-below-mark 130)
2931           (gnus-score-over-mark 130)
2932           (gnus-download-mark 131)
2933           (spec gnus-summary-line-format-spec)
2934           gnus-visual pos)
2935       (save-excursion
2936         (gnus-set-work-buffer)
2937         (let ((gnus-summary-line-format-spec spec)
2938               (gnus-newsgroup-downloadable '((0 . t))))
2939           (gnus-summary-insert-line
2940            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2941            0 nil 128 t nil "" nil 1)
2942           (goto-char (point-min))
2943           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2944                                              (- (point) 2)))))
2945           (goto-char (point-min))
2946           (push (cons 'replied (and (search-forward "\201" nil t)
2947                                     (- (point) 2)))
2948                 pos)
2949           (goto-char (point-min))
2950           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2951                 pos)
2952           (goto-char (point-min))
2953           (push (cons 'download
2954                       (and (search-forward "\203" nil t) (- (point) 2)))
2955                 pos)))
2956       (setq gnus-summary-mark-positions pos))))
2957
2958 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2959   "Insert a dummy root in the summary buffer."
2960   (beginning-of-line)
2961   (gnus-add-text-properties
2962    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2963    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2964
2965 (defun gnus-summary-extract-address-component (from)
2966   (or (car (funcall gnus-extract-address-components from))
2967       from))
2968
2969 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2970   (let ((mail-parse-charset gnus-newsgroup-charset)
2971         ; Is it really necessary to do this next part for each summary line?
2972         ; Luckily, doesn't seem to slow things down much.
2973         (mail-parse-ignored-charsets
2974          (save-excursion (set-buffer gnus-summary-buffer)
2975                          gnus-newsgroup-ignored-charsets)))
2976     (or
2977      (and gnus-ignored-from-addresses
2978           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2979           (let ((extra-headers (mail-header-extra header))
2980                 to
2981                 newsgroups)
2982             (cond
2983              ((setq to (cdr (assq 'To extra-headers)))
2984               (concat "-> "
2985                       (gnus-summary-extract-address-component
2986                        (funcall gnus-decode-encoded-word-function to))))
2987              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2988               (concat "=> " newsgroups)))))
2989      (gnus-summary-extract-address-component gnus-tmp-from))))
2990
2991 (defun gnus-summary-insert-line (gnus-tmp-header
2992                                  gnus-tmp-level gnus-tmp-current
2993                                  gnus-tmp-unread gnus-tmp-replied
2994                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2995                                  &optional gnus-tmp-dummy gnus-tmp-score
2996                                  gnus-tmp-process)
2997   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2998          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2999          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3000          (gnus-tmp-score-char
3001           (if (or (null gnus-summary-default-score)
3002                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3003                       gnus-summary-zcore-fuzz))
3004               ?                         ;Whitespace
3005             (if (< gnus-tmp-score gnus-summary-default-score)
3006                 gnus-score-below-mark gnus-score-over-mark)))
3007          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3008          (gnus-tmp-replied
3009           (cond (gnus-tmp-process gnus-process-mark)
3010                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3011                  gnus-cached-mark)
3012                 (gnus-tmp-replied gnus-replied-mark)
3013                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3014                  gnus-forwarded-mark)
3015                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3016                  gnus-saved-mark)
3017                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3018                  gnus-recent-mark)
3019                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3020                  gnus-unseen-mark)
3021                 (t gnus-no-mark)))
3022          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3023          (gnus-tmp-name
3024           (cond
3025            ((string-match "<[^>]+> *$" gnus-tmp-from)
3026             (let ((beg (match-beginning 0)))
3027               (or (and (string-match "^\".+\"" gnus-tmp-from)
3028                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3029                   (substring gnus-tmp-from 0 beg))))
3030            ((string-match "(.+)" gnus-tmp-from)
3031             (substring gnus-tmp-from
3032                        (1+ (match-beginning 0)) (1- (match-end 0))))
3033            (t gnus-tmp-from)))
3034          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3035          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3036          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3037          (buffer-read-only nil))
3038     (when (string= gnus-tmp-name "")
3039       (setq gnus-tmp-name gnus-tmp-from))
3040     (unless (numberp gnus-tmp-lines)
3041       (setq gnus-tmp-lines -1))
3042     (if (= gnus-tmp-lines -1)
3043         (setq gnus-tmp-lines "?")
3044       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3045     (gnus-put-text-property
3046      (point)
3047      (progn (eval gnus-summary-line-format-spec) (point))
3048      'gnus-number gnus-tmp-number)
3049     (when (gnus-visual-p 'summary-highlight 'highlight)
3050       (forward-line -1)
3051       (gnus-run-hooks 'gnus-summary-update-hook)
3052       (forward-line 1))))
3053
3054 (defun gnus-summary-update-line (&optional dont-update)
3055   "Update summary line after change."
3056   (when (and gnus-summary-default-score
3057              (not gnus-summary-inhibit-highlight))
3058     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3059            (article (gnus-summary-article-number))
3060            (score (gnus-summary-article-score article)))
3061       (unless dont-update
3062         (if (and gnus-summary-mark-below
3063                  (< (gnus-summary-article-score)
3064                     gnus-summary-mark-below))
3065             ;; This article has a low score, so we mark it as read.
3066             (when (memq article gnus-newsgroup-unreads)
3067               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3068           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3069             ;; This article was previously marked as read on account
3070             ;; of a low score, but now it has risen, so we mark it as
3071             ;; unread.
3072             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3073         (gnus-summary-update-mark
3074          (if (or (null gnus-summary-default-score)
3075                  (<= (abs (- score gnus-summary-default-score))
3076                      gnus-summary-zcore-fuzz))
3077              ?                          ;Whitespace
3078            (if (< score gnus-summary-default-score)
3079                gnus-score-below-mark gnus-score-over-mark))
3080          'score))
3081       ;; Do visual highlighting.
3082       (when (gnus-visual-p 'summary-highlight 'highlight)
3083         (gnus-run-hooks 'gnus-summary-update-hook)))))
3084
3085 (defvar gnus-tmp-new-adopts nil)
3086
3087 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3088   "Return the number of articles in THREAD.
3089 This may be 0 in some cases -- if none of the articles in
3090 the thread are to be displayed."
3091   (let* ((number
3092          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3093           (cond
3094            ((not (listp thread))
3095             1)
3096            ((and (consp thread) (cdr thread))
3097             (apply
3098              '+ 1 (mapcar
3099                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3100            ((null thread)
3101             1)
3102            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3103             1)
3104            (t 0))))
3105     (when (and level (zerop level) gnus-tmp-new-adopts)
3106       (incf number
3107             (apply '+ (mapcar
3108                        'gnus-summary-number-of-articles-in-thread
3109                        gnus-tmp-new-adopts))))
3110     (if char
3111         (if (> number 1) gnus-not-empty-thread-mark
3112           gnus-empty-thread-mark)
3113       number)))
3114
3115 (defun gnus-summary-set-local-parameters (group)
3116   "Go through the local params of GROUP and set all variable specs in that list."
3117   (let ((params (gnus-group-find-parameter group))
3118         (vars '(quit-config))           ; Ignore quit-config.
3119         elem)
3120     (while params
3121       (setq elem (car params)
3122             params (cdr params))
3123       (and (consp elem)                 ; Has to be a cons.
3124            (consp (cdr elem))           ; The cdr has to be a list.
3125            (symbolp (car elem))         ; Has to be a symbol in there.
3126            (not (memq (car elem) vars))
3127            (ignore-errors               ; So we set it.
3128              (push (car elem) vars)
3129              (make-local-variable (car elem))
3130              (set (car elem) (eval (nth 1 elem))))))))
3131
3132 (defun gnus-summary-read-group (group &optional show-all no-article
3133                                       kill-buffer no-display backward
3134                                       select-articles)
3135   "Start reading news in newsgroup GROUP.
3136 If SHOW-ALL is non-nil, already read articles are also listed.
3137 If NO-ARTICLE is non-nil, no article is selected initially.
3138 If NO-DISPLAY, don't generate a summary buffer."
3139   (let (result)
3140     (while (and group
3141                 (null (setq result
3142                             (let ((gnus-auto-select-next nil))
3143                               (or (gnus-summary-read-group-1
3144                                    group show-all no-article
3145                                    kill-buffer no-display
3146                                    select-articles)
3147                                   (setq show-all nil
3148                                         select-articles nil)))))
3149                 (eq gnus-auto-select-next 'quietly))
3150       (set-buffer gnus-group-buffer)
3151       ;; The entry function called above goes to the next
3152       ;; group automatically, so we go two groups back
3153       ;; if we are searching for the previous group.
3154       (when backward
3155         (gnus-group-prev-unread-group 2))
3156       (if (not (equal group (gnus-group-group-name)))
3157           (setq group (gnus-group-group-name))
3158         (setq group nil)))
3159     result))
3160
3161 (defun gnus-summary-read-group-1 (group show-all no-article
3162                                         kill-buffer no-display
3163                                         &optional select-articles)
3164   ;; Killed foreign groups can't be entered.
3165   ;;  (when (and (not (gnus-group-native-p group))
3166   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3167   ;;    (error "Dead non-native groups can't be entered"))
3168   (gnus-message 5 "Retrieving newsgroup: %s..."
3169                 (gnus-group-decoded-name group))
3170   (let* ((new-group (gnus-summary-setup-buffer group))
3171          (quit-config (gnus-group-quit-config group))
3172          (did-select (and new-group (gnus-select-newsgroup
3173                                      group show-all select-articles))))
3174     (cond
3175      ;; This summary buffer exists already, so we just select it.
3176      ((not new-group)
3177       (gnus-set-global-variables)
3178       (when kill-buffer
3179         (gnus-kill-or-deaden-summary kill-buffer))
3180       (gnus-configure-windows 'summary 'force)
3181       (gnus-set-mode-line 'summary)
3182       (gnus-summary-position-point)
3183       (message "")
3184       t)
3185      ;; We couldn't select this group.
3186      ((null did-select)
3187       (when (and (eq major-mode 'gnus-summary-mode)
3188                  (not (equal (current-buffer) kill-buffer)))
3189         (kill-buffer (current-buffer))
3190         (if (not quit-config)
3191             (progn
3192               ;; Update the info -- marks might need to be removed,
3193               ;; for instance.
3194               (gnus-summary-update-info)
3195               (set-buffer gnus-group-buffer)
3196               (gnus-group-jump-to-group group)
3197               (gnus-group-next-unread-group 1))
3198           (gnus-handle-ephemeral-exit quit-config)))
3199       (let ((grpinfo (gnus-get-info group)))
3200         (if (null (gnus-info-read grpinfo))
3201             (gnus-message 3 "Group %s contains no messages"
3202                           (gnus-group-decoded-name group))
3203           (gnus-message 3 "Can't select group")))
3204       nil)
3205      ;; The user did a `C-g' while prompting for number of articles,
3206      ;; so we exit this group.
3207      ((eq did-select 'quit)
3208       (and (eq major-mode 'gnus-summary-mode)
3209            (not (equal (current-buffer) kill-buffer))
3210            (kill-buffer (current-buffer)))
3211       (when kill-buffer
3212         (gnus-kill-or-deaden-summary kill-buffer))
3213       (if (not quit-config)
3214           (progn
3215             (set-buffer gnus-group-buffer)
3216             (gnus-group-jump-to-group group)
3217             (gnus-group-next-unread-group 1)
3218             (gnus-configure-windows 'group 'force))
3219         (gnus-handle-ephemeral-exit quit-config))
3220       ;; Finally signal the quit.
3221       (signal 'quit nil))
3222      ;; The group was successfully selected.
3223      (t
3224       (gnus-set-global-variables)
3225       ;; Save the active value in effect when the group was entered.
3226       (setq gnus-newsgroup-active
3227             (gnus-copy-sequence
3228              (gnus-active gnus-newsgroup-name)))
3229       ;; You can change the summary buffer in some way with this hook.
3230       (gnus-run-hooks 'gnus-select-group-hook)
3231       (gnus-update-format-specifications
3232        nil 'summary 'summary-mode 'summary-dummy)
3233       (gnus-update-summary-mark-positions)
3234       ;; Do score processing.
3235       (when gnus-use-scoring
3236         (gnus-possibly-score-headers))
3237       ;; Check whether to fill in the gaps in the threads.
3238       (when gnus-build-sparse-threads
3239         (gnus-build-sparse-threads))
3240       ;; Find the initial limit.
3241       (if gnus-show-threads
3242           (if show-all
3243               (let ((gnus-newsgroup-dormant nil))
3244                 (gnus-summary-initial-limit show-all))
3245             (gnus-summary-initial-limit show-all))
3246         ;; When untreaded, all articles are always shown.
3247         (setq gnus-newsgroup-limit
3248               (mapcar
3249                (lambda (header) (mail-header-number header))
3250                gnus-newsgroup-headers)))
3251       ;; Generate the summary buffer.
3252       (unless no-display
3253         (gnus-summary-prepare))
3254       (when gnus-use-trees
3255         (gnus-tree-open group)
3256         (setq gnus-summary-highlight-line-function
3257               'gnus-tree-highlight-article))
3258       ;; If the summary buffer is empty, but there are some low-scored
3259       ;; articles or some excluded dormants, we include these in the
3260       ;; buffer.
3261       (when (and (zerop (buffer-size))
3262                  (not no-display))
3263         (cond (gnus-newsgroup-dormant
3264                (gnus-summary-limit-include-dormant))
3265               ((and gnus-newsgroup-scored show-all)
3266                (gnus-summary-limit-include-expunged t))))
3267       ;; Function `gnus-apply-kill-file' must be called in this hook.
3268       (gnus-run-hooks 'gnus-apply-kill-hook)
3269       (if (and (zerop (buffer-size))
3270                (not no-display))
3271           (progn
3272             ;; This newsgroup is empty.
3273             (gnus-summary-catchup-and-exit nil t)
3274             (gnus-message 6 "No unread news")
3275             (when kill-buffer
3276               (gnus-kill-or-deaden-summary kill-buffer))
3277             ;; Return nil from this function.
3278             nil)
3279         ;; Hide conversation thread subtrees.  We cannot do this in
3280         ;; gnus-summary-prepare-hook since kill processing may not
3281         ;; work with hidden articles.
3282         (gnus-summary-maybe-hide-threads)
3283         (when kill-buffer
3284           (gnus-kill-or-deaden-summary kill-buffer))
3285         (gnus-summary-auto-select-subject)
3286         ;; Show first unread article if requested.
3287         (if (and (not no-article)
3288                  (not no-display)
3289                  gnus-newsgroup-unreads
3290                  gnus-auto-select-first)
3291             (progn
3292               (gnus-configure-windows 'summary)
3293               (gnus-summary-goto-article (gnus-summary-article-number)))
3294           ;; Don't select any articles.
3295           (gnus-summary-position-point)
3296           (gnus-configure-windows 'summary 'force)
3297           (gnus-set-mode-line 'summary))
3298         (when (get-buffer-window gnus-group-buffer t)
3299           ;; Gotta use windows, because recenter does weird stuff if
3300           ;; the current buffer ain't the displayed window.
3301           (let ((owin (selected-window)))
3302             (select-window (get-buffer-window gnus-group-buffer t))
3303             (when (gnus-group-goto-group group)
3304               (recenter))
3305             (select-window owin)))
3306         ;; Mark this buffer as "prepared".
3307         (setq gnus-newsgroup-prepared t)
3308         (gnus-run-hooks 'gnus-summary-prepared-hook)
3309         t)))))
3310
3311 (defun gnus-summary-auto-select-subject ()
3312   "Select the subject line on initial group entry."
3313   (goto-char (point-min))
3314   (cond
3315    ((eq gnus-auto-select-subject 'best)
3316     (gnus-summary-best-unread-subject))
3317    ((eq gnus-auto-select-subject 'unread)
3318     (gnus-summary-first-unread-subject))
3319    ((eq gnus-auto-select-subject 'unseen)
3320     (gnus-summary-first-unseen-subject))
3321    ((eq gnus-auto-select-subject 'first)
3322     ;; Do nothing.
3323     )
3324    ((gnus-functionp gnus-auto-select-subject)
3325     (funcall gnus-auto-select-subject))))
3326
3327 (defun gnus-summary-prepare ()
3328   "Generate the summary buffer."
3329   (interactive)
3330   (let ((buffer-read-only nil))
3331     (erase-buffer)
3332     (setq gnus-newsgroup-data nil
3333           gnus-newsgroup-data-reverse nil)
3334     (gnus-run-hooks 'gnus-summary-generate-hook)
3335     ;; Generate the buffer, either with threads or without.
3336     (when gnus-newsgroup-headers
3337       (gnus-summary-prepare-threads
3338        (if gnus-show-threads
3339            (gnus-sort-gathered-threads
3340             (funcall gnus-summary-thread-gathering-function
3341                      (gnus-sort-threads
3342                       (gnus-cut-threads (gnus-make-threads)))))
3343          ;; Unthreaded display.
3344          (gnus-sort-articles gnus-newsgroup-headers))))
3345     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3346     ;; Call hooks for modifying summary buffer.
3347     (goto-char (point-min))
3348     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3349
3350 (defsubst gnus-general-simplify-subject (subject)
3351   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3352   (setq subject
3353         (cond
3354          ;; Truncate the subject.
3355          (gnus-simplify-subject-functions
3356           (gnus-map-function gnus-simplify-subject-functions subject))
3357          ((numberp gnus-summary-gather-subject-limit)
3358           (setq subject (gnus-simplify-subject-re subject))
3359           (if (> (length subject) gnus-summary-gather-subject-limit)
3360               (substring subject 0 gnus-summary-gather-subject-limit)
3361             subject))
3362          ;; Fuzzily simplify it.
3363          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3364           (gnus-simplify-subject-fuzzy subject))
3365          ;; Just remove the leading "Re:".
3366          (t
3367           (gnus-simplify-subject-re subject))))
3368
3369   (if (and gnus-summary-gather-exclude-subject
3370            (string-match gnus-summary-gather-exclude-subject subject))
3371       nil                         ; This article shouldn't be gathered
3372     subject))
3373
3374 (defun gnus-summary-simplify-subject-query ()
3375   "Query where the respool algorithm would put this article."
3376   (interactive)
3377   (gnus-summary-select-article)
3378   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3379
3380 (defun gnus-gather-threads-by-subject (threads)
3381   "Gather threads by looking at Subject headers."
3382   (if (not gnus-summary-make-false-root)
3383       threads
3384     (let ((hashtb (gnus-make-hashtable 1024))
3385           (prev threads)
3386           (result threads)
3387           subject hthread whole-subject)
3388       (while threads
3389         (setq subject (gnus-general-simplify-subject
3390                        (setq whole-subject (mail-header-subject
3391                                             (caar threads)))))
3392         (when subject
3393           (if (setq hthread (gnus-gethash subject hashtb))
3394               (progn
3395                 ;; We enter a dummy root into the thread, if we
3396                 ;; haven't done that already.
3397                 (unless (stringp (caar hthread))
3398                   (setcar hthread (list whole-subject (car hthread))))
3399                 ;; We add this new gathered thread to this gathered
3400                 ;; thread.
3401                 (setcdr (car hthread)
3402                         (nconc (cdar hthread) (list (car threads))))
3403                 ;; Remove it from the list of threads.
3404                 (setcdr prev (cdr threads))
3405                 (setq threads prev))
3406             ;; Enter this thread into the hash table.
3407             (gnus-sethash subject threads hashtb)))
3408         (setq prev threads)
3409         (setq threads (cdr threads)))
3410       result)))
3411
3412 (defun gnus-gather-threads-by-references (threads)
3413   "Gather threads by looking at References headers."
3414   (let ((idhashtb (gnus-make-hashtable 1024))
3415         (thhashtb (gnus-make-hashtable 1024))
3416         (prev threads)
3417         (result threads)
3418         ids references id gthread gid entered ref)
3419     (while threads
3420       (when (setq references (mail-header-references (caar threads)))
3421         (setq id (mail-header-id (caar threads))
3422               ids (gnus-split-references references)
3423               entered nil)
3424         (while (setq ref (pop ids))
3425           (setq ids (delete ref ids))
3426           (if (not (setq gid (gnus-gethash ref idhashtb)))
3427               (progn
3428                 (gnus-sethash ref id idhashtb)
3429                 (gnus-sethash id threads thhashtb))
3430             (setq gthread (gnus-gethash gid thhashtb))
3431             (unless entered
3432               ;; We enter a dummy root into the thread, if we
3433               ;; haven't done that already.
3434               (unless (stringp (caar gthread))
3435                 (setcar gthread (list (mail-header-subject (caar gthread))
3436                                       (car gthread))))
3437               ;; We add this new gathered thread to this gathered
3438               ;; thread.
3439               (setcdr (car gthread)
3440                       (nconc (cdar gthread) (list (car threads)))))
3441             ;; Add it into the thread hash table.
3442             (gnus-sethash id gthread thhashtb)
3443             (setq entered t)
3444             ;; Remove it from the list of threads.
3445             (setcdr prev (cdr threads))
3446             (setq threads prev))))
3447       (setq prev threads)
3448       (setq threads (cdr threads)))
3449     result))
3450
3451 (defun gnus-sort-gathered-threads (threads)
3452   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3453   (let ((result threads))
3454     (while threads
3455       (when (stringp (caar threads))
3456         (setcdr (car threads)
3457                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3458       (setq threads (cdr threads)))
3459     result))
3460
3461 (defun gnus-thread-loop-p (root thread)
3462   "Say whether ROOT is in THREAD."
3463   (let ((stack (list thread))
3464         (infloop 0)
3465         th)
3466     (while (setq thread (pop stack))
3467       (setq th (cdr thread))
3468       (while (and th
3469                   (not (eq (caar th) root)))
3470         (pop th))
3471       (if th
3472           ;; We have found a loop.
3473           (let (ref-dep)
3474             (setcdr thread (delq (car th) (cdr thread)))
3475             (if (boundp (setq ref-dep (intern "none"
3476                                               gnus-newsgroup-dependencies)))
3477                 (setcdr (symbol-value ref-dep)
3478                         (nconc (cdr (symbol-value ref-dep))
3479                                (list (car th))))
3480               (set ref-dep (list nil (car th))))
3481             (setq infloop 1
3482                   stack nil))
3483         ;; Push all the subthreads onto the stack.
3484         (push (cdr thread) stack)))
3485     infloop))
3486
3487 (defun gnus-make-threads ()
3488   "Go through the dependency hashtb and find the roots.  Return all threads."
3489   (let (threads)
3490     (while (catch 'infloop
3491              (mapatoms
3492               (lambda (refs)
3493                 ;; Deal with self-referencing References loops.
3494                 (when (and (car (symbol-value refs))
3495                            (not (zerop
3496                                  (apply
3497                                   '+
3498                                   (mapcar
3499                                    (lambda (thread)
3500                                      (gnus-thread-loop-p
3501                                       (car (symbol-value refs)) thread))
3502                                    (cdr (symbol-value refs)))))))
3503                   (setq threads nil)
3504                   (throw 'infloop t))
3505                 (unless (car (symbol-value refs))
3506              ;; These threads do not refer back to any other articles,
3507                   ;; so they're roots.
3508                   (setq threads (append (cdr (symbol-value refs)) threads))))
3509               gnus-newsgroup-dependencies)))
3510     threads))
3511
3512 ;; Build the thread tree.
3513 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3514   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3515
3516 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3517 if it was already present.
3518
3519 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3520 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3521 Message-IDs will be renamed be renamed to a unique Message-ID before
3522 being entered.
3523
3524 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3525   (let* ((id (mail-header-id header))
3526          (id-dep (and id (intern id dependencies)))
3527          ref ref-dep ref-header)
3528     ;; Enter this `header' in the `dependencies' table.
3529     (cond
3530      ((not id-dep)
3531       (setq header nil))
3532      ;; The first two cases do the normal part: enter a new `header'
3533      ;; in the `dependencies' table.
3534      ((not (boundp id-dep))
3535       (set id-dep (list header)))
3536      ((null (car (symbol-value id-dep)))
3537       (setcar (symbol-value id-dep) header))
3538
3539      ;; From here the `header' was already present in the
3540      ;; `dependencies' table.
3541      (force-new
3542       ;; Overrides an existing entry;
3543       ;; just set the header part of the entry.
3544       (setcar (symbol-value id-dep) header))
3545
3546      ;; Renames the existing `header' to a unique Message-ID.
3547      ((not gnus-summary-ignore-duplicates)
3548       ;; An article with this Message-ID has already been seen.
3549       ;; We rename the Message-ID.
3550       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3551            (list header))
3552       (mail-header-set-id header id))
3553
3554      ;; The last case ignores an existing entry, except it adds any
3555      ;; additional Xrefs (in case the two articles came from different
3556      ;; servers.
3557      ;; Also sets `header' to `nil' meaning that the `dependencies'
3558      ;; table was *not* modified.
3559      (t
3560       (mail-header-set-xref
3561        (car (symbol-value id-dep))
3562        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3563                    "")
3564                (or (mail-header-xref header) "")))
3565       (setq header nil)))
3566
3567     (when header
3568       ;; First check if that we are not creating a References loop.
3569       (setq ref (gnus-parent-id (mail-header-references header)))
3570       (while (and ref
3571                   (setq ref-dep (intern-soft ref dependencies))
3572                   (boundp ref-dep)
3573                   (setq ref-header (car (symbol-value ref-dep))))
3574         (if (string= id ref)
3575             ;; Yuk!  This is a reference loop.  Make the article be a
3576             ;; root article.
3577             (progn
3578               (mail-header-set-references (car (symbol-value id-dep)) "none")
3579               (setq ref nil))
3580           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3581       (setq ref (gnus-parent-id (mail-header-references header)))
3582       (setq ref-dep (intern (or ref "none") dependencies))
3583       (if (boundp ref-dep)
3584           (setcdr (symbol-value ref-dep)
3585                   (nconc (cdr (symbol-value ref-dep))
3586                          (list (symbol-value id-dep))))
3587         (set ref-dep (list nil (symbol-value id-dep)))))
3588     header))
3589
3590 (defun gnus-build-sparse-threads ()
3591   (let ((headers gnus-newsgroup-headers)
3592         (mail-parse-charset gnus-newsgroup-charset)
3593         (gnus-summary-ignore-duplicates t)
3594         header references generation relations
3595         subject child end new-child date)
3596     ;; First we create an alist of generations/relations, where
3597     ;; generations is how much we trust the relation, and the relation
3598     ;; is parent/child.
3599     (gnus-message 7 "Making sparse threads...")
3600     (save-excursion
3601       (nnheader-set-temp-buffer " *gnus sparse threads*")
3602       (while (setq header (pop headers))
3603         (when (and (setq references (mail-header-references header))
3604                    (not (string= references "")))
3605           (insert references)
3606           (setq child (mail-header-id header)
3607                 subject (mail-header-subject header)
3608                 date (mail-header-date header)
3609                 generation 0)
3610           (while (search-backward ">" nil t)
3611             (setq end (1+ (point)))
3612             (when (search-backward "<" nil t)
3613               (setq new-child (buffer-substring (point) end))
3614               (push (list (incf generation)
3615                           child (setq child new-child)
3616                           subject date)
3617                     relations)))
3618           (when child
3619             (push (list (1+ generation) child nil subject) relations))
3620           (erase-buffer)))
3621       (kill-buffer (current-buffer)))
3622     ;; Sort over trustworthiness.
3623     (mapcar
3624      (lambda (relation)
3625        (when (gnus-dependencies-add-header
3626               (make-full-mail-header
3627                gnus-reffed-article-number
3628                (nth 3 relation) "" (or (nth 4 relation) "")
3629                (nth 1 relation)
3630                (or (nth 2 relation) "") 0 0 "")
3631               gnus-newsgroup-dependencies nil)
3632          (push gnus-reffed-article-number gnus-newsgroup-limit)
3633          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3634          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3635                gnus-newsgroup-reads)
3636          (decf gnus-reffed-article-number)))
3637      (sort relations 'car-less-than-car))
3638     (gnus-message 7 "Making sparse threads...done")))
3639
3640 (defun gnus-build-old-threads ()
3641   ;; Look at all the articles that refer back to old articles, and
3642   ;; fetch the headers for the articles that aren't there.  This will
3643   ;; build complete threads - if the roots haven't been expired by the
3644   ;; server, that is.
3645   (let ((mail-parse-charset gnus-newsgroup-charset)
3646         id heads)
3647     (mapatoms
3648      (lambda (refs)
3649        (when (not (car (symbol-value refs)))
3650          (setq heads (cdr (symbol-value refs)))
3651          (while heads
3652            (if (memq (mail-header-number (caar heads))
3653                      gnus-newsgroup-dormant)
3654                (setq heads (cdr heads))
3655              (setq id (symbol-name refs))
3656              (while (and (setq id (gnus-build-get-header id))
3657                          (not (car (gnus-id-to-thread id)))))
3658              (setq heads nil)))))
3659      gnus-newsgroup-dependencies)))
3660
3661 ;; This function has to be called with point after the article number
3662 ;; on the beginning of the line.
3663 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3664   (let ((eol (gnus-point-at-eol))
3665         (buffer (current-buffer))
3666         header)
3667
3668     ;; overview: [num subject from date id refs chars lines misc]
3669     (unwind-protect
3670         (progn
3671           (narrow-to-region (point) eol)
3672           (unless (eobp)
3673             (forward-char))
3674
3675           (setq header
3676                 (make-full-mail-header
3677                  number                 ; number
3678                  (funcall gnus-decode-encoded-word-function
3679                           (nnheader-nov-field)) ; subject
3680                  (funcall gnus-decode-encoded-word-function
3681                           (nnheader-nov-field)) ; from
3682                  (nnheader-nov-field)   ; date
3683                  (nnheader-nov-read-message-id) ; id
3684                  (nnheader-nov-field)   ; refs
3685                  (nnheader-nov-read-integer) ; chars
3686                  (nnheader-nov-read-integer) ; lines
3687                  (unless (eobp)
3688                    (if (looking-at "Xref: ")
3689                        (goto-char (match-end 0)))
3690                    (nnheader-nov-field)) ; Xref
3691                  (nnheader-nov-parse-extra)))) ; extra
3692
3693       (widen))
3694
3695     (when gnus-alter-header-function
3696       (funcall gnus-alter-header-function header))
3697     (gnus-dependencies-add-header header dependencies force-new)))
3698
3699 (defun gnus-build-get-header (id)
3700   "Look through the buffer of NOV lines and find the header to ID.
3701 Enter this line into the dependencies hash table, and return
3702 the id of the parent article (if any)."
3703   (let ((deps gnus-newsgroup-dependencies)
3704         found header)
3705     (prog1
3706         (save-excursion
3707           (set-buffer nntp-server-buffer)
3708           (let ((case-fold-search nil))
3709             (goto-char (point-min))
3710             (while (and (not found)
3711                         (search-forward id nil t))
3712               (beginning-of-line)
3713               (setq found (looking-at
3714                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3715                                    (regexp-quote id))))
3716               (or found (beginning-of-line 2)))
3717             (when found
3718               (beginning-of-line)
3719               (and
3720                (setq header (gnus-nov-parse-line
3721                              (read (current-buffer)) deps))
3722                (gnus-parent-id (mail-header-references header))))))
3723       (when header
3724         (let ((number (mail-header-number header)))
3725           (push number gnus-newsgroup-limit)
3726           (push header gnus-newsgroup-headers)
3727           (if (memq number gnus-newsgroup-unselected)
3728               (progn
3729                 (push number gnus-newsgroup-unreads)
3730                 (setq gnus-newsgroup-unselected
3731                       (delq number gnus-newsgroup-unselected)))
3732             (push number gnus-newsgroup-ancient)))))))
3733
3734 (defun gnus-build-all-threads ()
3735   "Read all the headers."
3736   (let ((gnus-summary-ignore-duplicates t)
3737         (mail-parse-charset gnus-newsgroup-charset)
3738         (dependencies gnus-newsgroup-dependencies)
3739         header article)
3740     (save-excursion
3741       (set-buffer nntp-server-buffer)
3742       (let ((case-fold-search nil))
3743         (goto-char (point-min))
3744         (while (not (eobp))
3745           (ignore-errors
3746             (setq article (read (current-buffer))
3747                   header (gnus-nov-parse-line article dependencies)))
3748           (when header
3749             (save-excursion
3750               (set-buffer gnus-summary-buffer)
3751               (push header gnus-newsgroup-headers)
3752               (if (memq (setq article (mail-header-number header))
3753                         gnus-newsgroup-unselected)
3754                   (progn
3755                     (push article gnus-newsgroup-unreads)
3756                     (setq gnus-newsgroup-unselected
3757                           (delq article gnus-newsgroup-unselected)))
3758                 (push article gnus-newsgroup-ancient)))
3759             (forward-line 1)))))))
3760
3761 (defun gnus-summary-update-article-line (article header)
3762   "Update the line for ARTICLE using HEADERS."
3763   (let* ((id (mail-header-id header))
3764          (thread (gnus-id-to-thread id)))
3765     (unless thread
3766       (error "Article in no thread"))
3767     ;; Update the thread.
3768     (setcar thread header)
3769     (gnus-summary-goto-subject article)
3770     (let* ((datal (gnus-data-find-list article))
3771            (data (car datal))
3772            (length (when (cdr datal)
3773                      (- (gnus-data-pos data)
3774                         (gnus-data-pos (cadr datal)))))
3775            (buffer-read-only nil)
3776            (level (gnus-summary-thread-level)))
3777       (gnus-delete-line)
3778       (gnus-summary-insert-line
3779        header level nil (gnus-article-mark article)
3780        (memq article gnus-newsgroup-replied)
3781        (memq article gnus-newsgroup-expirable)
3782        ;; Only insert the Subject string when it's different
3783        ;; from the previous Subject string.
3784        (if (and
3785             gnus-show-threads
3786             (gnus-subject-equal
3787              (condition-case ()
3788                  (mail-header-subject
3789                   (gnus-data-header
3790                    (cadr
3791                     (gnus-data-find-list
3792                      article
3793                      (gnus-data-list t)))))
3794                ;; Error on the side of excessive subjects.
3795                (error ""))
3796              (mail-header-subject header)))
3797            ""
3798          (mail-header-subject header))
3799        nil (cdr (assq article gnus-newsgroup-scored))
3800        (memq article gnus-newsgroup-processable))
3801       (when length
3802         (gnus-data-update-list
3803          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3804
3805 (defun gnus-summary-update-article (article &optional iheader)
3806   "Update ARTICLE in the summary buffer."
3807   (set-buffer gnus-summary-buffer)
3808   (let* ((header (gnus-summary-article-header article))
3809          (id (mail-header-id header))
3810          (data (gnus-data-find article))
3811          (thread (gnus-id-to-thread id))
3812          (references (mail-header-references header))
3813          (parent
3814           (gnus-id-to-thread
3815            (or (gnus-parent-id
3816                 (when (and references
3817                            (not (equal "" references)))
3818                   references))
3819                "none")))
3820          (buffer-read-only nil)
3821          (old (car thread)))
3822     (when thread
3823       (unless iheader
3824         (setcar thread nil)
3825         (when parent
3826           (delq thread parent)))
3827       (if (gnus-summary-insert-subject id header)
3828        ;; Set the (possibly) new article number in the data structure.
3829           (gnus-data-set-number data (gnus-id-to-article id))
3830         (setcar thread old)
3831         nil))))
3832
3833 (defun gnus-rebuild-thread (id &optional line)
3834   "Rebuild the thread containing ID.
3835 If LINE, insert the rebuilt thread starting on line LINE."
3836   (let ((buffer-read-only nil)
3837         old-pos current thread data)
3838     (if (not gnus-show-threads)
3839         (setq thread (list (car (gnus-id-to-thread id))))
3840       ;; Get the thread this article is part of.
3841       (setq thread (gnus-remove-thread id)))
3842     (setq old-pos (gnus-point-at-bol))
3843     (setq current (save-excursion
3844                     (and (re-search-backward "[\r\n]" nil t)
3845                          (gnus-summary-article-number))))
3846     ;; If this is a gathered thread, we have to go some re-gathering.
3847     (when (stringp (car thread))
3848       (let ((subject (car thread))
3849             roots thr)
3850         (setq thread (cdr thread))
3851         (while thread
3852           (unless (memq (setq thr (gnus-id-to-thread
3853                                    (gnus-root-id
3854                                     (mail-header-id (caar thread)))))
3855                         roots)
3856             (push thr roots))
3857           (setq thread (cdr thread)))
3858         ;; We now have all (unique) roots.
3859         (if (= (length roots) 1)
3860             ;; All the loose roots are now one solid root.
3861             (setq thread (car roots))
3862           (setq thread (cons subject (gnus-sort-threads roots))))))
3863     (let (threads)
3864       ;; We then insert this thread into the summary buffer.
3865       (when line
3866         (goto-char (point-min))
3867         (forward-line (1- line)))
3868       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3869         (if gnus-show-threads
3870             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3871           (gnus-summary-prepare-unthreaded thread))
3872         (setq data (nreverse gnus-newsgroup-data))
3873         (setq threads gnus-newsgroup-threads))
3874       ;; We splice the new data into the data structure.
3875       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3876       ;;!!! then we want to insert at the beginning of the buffer.
3877       ;;!!! That happens to be true with Gnus now, but that may
3878       ;;!!! change in the future.  Perhaps.
3879       (gnus-data-enter-list
3880        (if line nil current) data (- (point) old-pos))
3881       (setq gnus-newsgroup-threads
3882             (nconc threads gnus-newsgroup-threads))
3883       (gnus-data-compute-positions))))
3884
3885 (defun gnus-number-to-header (number)
3886   "Return the header for article NUMBER."
3887   (let ((headers gnus-newsgroup-headers))
3888     (while (and headers
3889                 (not (= number (mail-header-number (car headers)))))
3890       (pop headers))
3891     (when headers
3892       (car headers))))
3893
3894 (defun gnus-parent-headers (in-headers &optional generation)
3895   "Return the headers of the GENERATIONeth parent of HEADERS."
3896   (unless generation
3897     (setq generation 1))
3898   (let ((parent t)
3899         (headers in-headers)
3900         references)
3901     (while (and parent
3902                 (not (zerop generation))
3903                 (setq references (mail-header-references headers)))
3904       (setq headers (if (and references
3905                              (setq parent (gnus-parent-id references)))
3906                         (car (gnus-id-to-thread parent))
3907                       nil))
3908       (decf generation))
3909     (and (not (eq headers in-headers))
3910          headers)))
3911
3912 (defun gnus-id-to-thread (id)
3913   "Return the (sub-)thread where ID appears."
3914   (gnus-gethash id gnus-newsgroup-dependencies))
3915
3916 (defun gnus-id-to-article (id)
3917   "Return the article number of ID."
3918   (let ((thread (gnus-id-to-thread id)))
3919     (when (and thread
3920                (car thread))
3921       (mail-header-number (car thread)))))
3922
3923 (defun gnus-id-to-header (id)
3924   "Return the article headers of ID."
3925   (car (gnus-id-to-thread id)))
3926
3927 (defun gnus-article-displayed-root-p (article)
3928   "Say whether ARTICLE is a root(ish) article."
3929   (let ((level (gnus-summary-thread-level article))
3930         (refs (mail-header-references  (gnus-summary-article-header article)))
3931         particle)
3932     (cond
3933      ((null level) nil)
3934      ((zerop level) t)
3935      ((null refs) t)
3936      ((null (gnus-parent-id refs)) t)
3937      ((and (= 1 level)
3938            (null (setq particle (gnus-id-to-article
3939                                  (gnus-parent-id refs))))
3940            (null (gnus-summary-thread-level particle)))))))
3941
3942 (defun gnus-root-id (id)
3943   "Return the id of the root of the thread where ID appears."
3944   (let (last-id prev)
3945     (while (and id (setq prev (car (gnus-id-to-thread id))))
3946       (setq last-id id
3947             id (gnus-parent-id (mail-header-references prev))))
3948     last-id))
3949
3950 (defun gnus-articles-in-thread (thread)
3951   "Return the list of articles in THREAD."
3952   (cons (mail-header-number (car thread))
3953         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3954
3955 (defun gnus-remove-thread (id &optional dont-remove)
3956   "Remove the thread that has ID in it."
3957   (let (headers thread last-id)
3958     ;; First go up in this thread until we find the root.
3959     (setq last-id (gnus-root-id id)
3960           headers (message-flatten-list (gnus-id-to-thread last-id)))
3961     ;; We have now found the real root of this thread.  It might have
3962     ;; been gathered into some loose thread, so we have to search
3963     ;; through the threads to find the thread we wanted.
3964     (let ((threads gnus-newsgroup-threads)
3965           sub)
3966       (while threads
3967         (setq sub (car threads))
3968         (if (stringp (car sub))
3969             ;; This is a gathered thread, so we look at the roots
3970             ;; below it to find whether this article is in this
3971             ;; gathered root.
3972             (progn
3973               (setq sub (cdr sub))
3974               (while sub
3975                 (when (member (caar sub) headers)
3976                   (setq thread (car threads)
3977                         threads nil
3978                         sub nil))
3979                 (setq sub (cdr sub))))
3980           ;; It's an ordinary thread, so we check it.
3981           (when (eq (car sub) (car headers))
3982             (setq thread sub
3983                   threads nil)))
3984         (setq threads (cdr threads)))
3985       ;; If this article is in no thread, then it's a root.
3986       (if thread
3987           (unless dont-remove
3988             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3989         (setq thread (gnus-id-to-thread last-id)))
3990       (when thread
3991         (prog1
3992             thread                      ; We return this thread.
3993           (unless dont-remove
3994             (if (stringp (car thread))
3995                 (progn
3996                   ;; If we use dummy roots, then we have to remove the
3997                   ;; dummy root as well.
3998                   (when (eq gnus-summary-make-false-root 'dummy)
3999                     ;; We go to the dummy root by going to
4000                     ;; the first sub-"thread", and then one line up.
4001                     (gnus-summary-goto-article
4002                      (mail-header-number (caadr thread)))
4003                     (forward-line -1)
4004                     (gnus-delete-line)
4005                     (gnus-data-compute-positions))
4006                   (setq thread (cdr thread))
4007                   (while thread
4008                     (gnus-remove-thread-1 (car thread))
4009                     (setq thread (cdr thread))))
4010               (gnus-remove-thread-1 thread))))))))
4011
4012 (defun gnus-remove-thread-1 (thread)
4013   "Remove the thread THREAD recursively."
4014   (let ((number (mail-header-number (pop thread)))
4015         d)
4016     (setq thread (reverse thread))
4017     (while thread
4018       (gnus-remove-thread-1 (pop thread)))
4019     (when (setq d (gnus-data-find number))
4020       (goto-char (gnus-data-pos d))
4021       (gnus-summary-show-thread)
4022       (gnus-data-remove
4023        number
4024        (- (gnus-point-at-bol)
4025           (prog1
4026               (1+ (gnus-point-at-eol))
4027             (gnus-delete-line)))))))
4028
4029 (defun gnus-sort-threads-1 (threads func)
4030   (sort (mapcar (lambda (thread)
4031                   (cons (car thread)
4032                         (and (cdr thread)
4033                              (gnus-sort-threads-1 (cdr thread) func))))
4034                 threads) func))
4035
4036 (defun gnus-sort-threads (threads)
4037   "Sort THREADS."
4038   (if (not gnus-thread-sort-functions)
4039       threads
4040     (gnus-message 8 "Sorting threads...")
4041     (prog1
4042         (gnus-sort-threads-1
4043          threads
4044          (gnus-make-sort-function gnus-thread-sort-functions))
4045       (gnus-message 8 "Sorting threads...done"))))
4046
4047 (defun gnus-sort-articles (articles)
4048   "Sort ARTICLES."
4049   (when gnus-article-sort-functions
4050     (gnus-message 7 "Sorting articles...")
4051     (prog1
4052         (setq gnus-newsgroup-headers
4053               (sort articles (gnus-make-sort-function
4054                               gnus-article-sort-functions)))
4055       (gnus-message 7 "Sorting articles...done"))))
4056
4057 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4058 (defmacro gnus-thread-header (thread)
4059   "Return header of first article in THREAD.
4060 Note that THREAD must never, ever be anything else than a variable -
4061 using some other form will lead to serious barfage."
4062   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4063   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4064   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4065         (vector thread) 2))
4066
4067 (defsubst gnus-article-sort-by-number (h1 h2)
4068   "Sort articles by article number."
4069   (< (mail-header-number h1)
4070      (mail-header-number h2)))
4071
4072 (defun gnus-thread-sort-by-number (h1 h2)
4073   "Sort threads by root article number."
4074   (gnus-article-sort-by-number
4075    (gnus-thread-header h1) (gnus-thread-header h2)))
4076
4077 (defsubst gnus-article-sort-by-lines (h1 h2)
4078   "Sort articles by article Lines header."
4079   (< (mail-header-lines h1)
4080      (mail-header-lines h2)))
4081
4082 (defun gnus-thread-sort-by-lines (h1 h2)
4083   "Sort threads by root article Lines header."
4084   (gnus-article-sort-by-lines
4085    (gnus-thread-header h1) (gnus-thread-header h2)))
4086
4087 (defsubst gnus-article-sort-by-chars (h1 h2)
4088   "Sort articles by octet length."
4089   (< (mail-header-chars h1)
4090      (mail-header-chars h2)))
4091
4092 (defun gnus-thread-sort-by-chars (h1 h2)
4093   "Sort threads by root article octet length."
4094   (gnus-article-sort-by-chars
4095    (gnus-thread-header h1) (gnus-thread-header h2)))
4096
4097 (defsubst gnus-article-sort-by-author (h1 h2)
4098   "Sort articles by root author."
4099   (string-lessp
4100    (let ((extract (funcall
4101                    gnus-extract-address-components
4102                    (mail-header-from h1))))
4103      (or (car extract) (cadr extract) ""))
4104    (let ((extract (funcall
4105                    gnus-extract-address-components
4106                    (mail-header-from h2))))
4107      (or (car extract) (cadr extract) ""))))
4108
4109 (defun gnus-thread-sort-by-author (h1 h2)
4110   "Sort threads by root author."
4111   (gnus-article-sort-by-author
4112    (gnus-thread-header h1)  (gnus-thread-header h2)))
4113
4114 (defsubst gnus-article-sort-by-subject (h1 h2)
4115   "Sort articles by root subject."
4116   (string-lessp
4117    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4118    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4119
4120 (defun gnus-thread-sort-by-subject (h1 h2)
4121   "Sort threads by root subject."
4122   (gnus-article-sort-by-subject
4123    (gnus-thread-header h1) (gnus-thread-header h2)))
4124
4125 (defsubst gnus-article-sort-by-date (h1 h2)
4126   "Sort articles by root article date."
4127   (time-less-p
4128    (gnus-date-get-time (mail-header-date h1))
4129    (gnus-date-get-time (mail-header-date h2))))
4130
4131 (defun gnus-thread-sort-by-date (h1 h2)
4132   "Sort threads by root article date."
4133   (gnus-article-sort-by-date
4134    (gnus-thread-header h1) (gnus-thread-header h2)))
4135
4136 (defsubst gnus-article-sort-by-score (h1 h2)
4137   "Sort articles by root article score.
4138 Unscored articles will be counted as having a score of zero."
4139   (> (or (cdr (assq (mail-header-number h1)
4140                     gnus-newsgroup-scored))
4141          gnus-summary-default-score 0)
4142      (or (cdr (assq (mail-header-number h2)
4143                     gnus-newsgroup-scored))
4144          gnus-summary-default-score 0)))
4145
4146 (defun gnus-thread-sort-by-score (h1 h2)
4147   "Sort threads by root article score."
4148   (gnus-article-sort-by-score
4149    (gnus-thread-header h1) (gnus-thread-header h2)))
4150
4151 (defun gnus-thread-sort-by-total-score (h1 h2)
4152   "Sort threads by the sum of all scores in the thread.
4153 Unscored articles will be counted as having a score of zero."
4154   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4155
4156 (defun gnus-thread-total-score (thread)
4157   ;; This function find the total score of THREAD.
4158   (cond
4159    ((null thread)
4160     0)
4161    ((consp thread)
4162     (if (stringp (car thread))
4163         (apply gnus-thread-score-function 0
4164                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4165       (gnus-thread-total-score-1 thread)))
4166    (t
4167     (gnus-thread-total-score-1 (list thread)))))
4168
4169 (defun gnus-thread-sort-by-most-recent-thread (h1 h2)
4170   "Sort threads such that the thread with the most recently arrived article is comes first."
4171   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4172
4173 (defun gnus-thread-highest-number (thread)
4174   "Return the highest article number from THREAD."
4175   (apply 'max (mapcar (lambda (header)
4176                         (mail-header-number header))
4177                       (message-flatten-list thread))))
4178
4179 (defun gnus-thread-total-score-1 (root)
4180   ;; This function find the total score of the thread below ROOT.
4181   (setq root (car root))
4182   (apply gnus-thread-score-function
4183          (or (append
4184               (mapcar 'gnus-thread-total-score
4185                       (cdr (gnus-id-to-thread (mail-header-id root))))
4186               (when (> (mail-header-number root) 0)
4187                 (list (or (cdr (assq (mail-header-number root)
4188                                      gnus-newsgroup-scored))
4189                           gnus-summary-default-score 0))))
4190              (list gnus-summary-default-score)
4191              '(0))))
4192
4193 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4194 (defvar gnus-tmp-prev-subject nil)
4195 (defvar gnus-tmp-false-parent nil)
4196 (defvar gnus-tmp-root-expunged nil)
4197 (defvar gnus-tmp-dummy-line nil)
4198
4199 (eval-when-compile (defvar gnus-tmp-header))
4200 (defun gnus-extra-header (type &optional header)
4201   "Return the extra header of TYPE."
4202   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4203       ""))
4204
4205 (defvar gnus-tmp-thread-tree-header-string "")
4206
4207 (defvar gnus-sum-thread-tree-root "> "
4208   "With %B spec, used for the root of a thread.
4209 If nil, use subject instead.")
4210 (defvar gnus-sum-thread-tree-single-indent ""
4211   "With %B spec, used for a thread with just one message.
4212 If nil, use subject instead.")
4213 (defvar gnus-sum-thread-tree-vertical "| "
4214   "With %B spec, used for drawing a vertical line.")
4215 (defvar gnus-sum-thread-tree-indent "  "
4216   "With %B spec, used for indenting.")
4217 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4218   "With %B spec, used for a leaf with brothers.")
4219 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4220   "With %B spec, used for a leaf without brothers.")
4221
4222 (defun gnus-summary-prepare-threads (threads)
4223   "Prepare summary buffer from THREADS and indentation LEVEL.
4224 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4225 or a straight list of headers."
4226   (gnus-message 7 "Generating summary...")
4227
4228   (setq gnus-newsgroup-threads threads)
4229   (beginning-of-line)
4230
4231   (let ((gnus-tmp-level 0)
4232         (default-score (or gnus-summary-default-score 0))
4233         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4234         thread number subject stack state gnus-tmp-gathered beg-match
4235         new-roots gnus-tmp-new-adopts thread-end
4236         gnus-tmp-header gnus-tmp-unread
4237         gnus-tmp-replied gnus-tmp-subject-or-nil
4238         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4239         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4240         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4241         tree-stack)
4242
4243     (setq gnus-tmp-prev-subject nil)
4244
4245     (if (vectorp (car threads))
4246         ;; If this is a straight (sic) list of headers, then a
4247         ;; threaded summary display isn't required, so we just create
4248         ;; an unthreaded one.
4249         (gnus-summary-prepare-unthreaded threads)
4250
4251       ;; Do the threaded display.
4252
4253       (while (or threads stack gnus-tmp-new-adopts new-roots)
4254
4255         (if (and (= gnus-tmp-level 0)
4256                  (or (not stack)
4257                      (= (caar stack) 0))
4258                  (not gnus-tmp-false-parent)
4259                  (or gnus-tmp-new-adopts new-roots))
4260             (if gnus-tmp-new-adopts
4261                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4262                       thread (list (car gnus-tmp-new-adopts))
4263                       gnus-tmp-header (caar thread)
4264                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4265               (when new-roots
4266                 (setq thread (list (car new-roots))
4267                       gnus-tmp-header (caar thread)
4268                       new-roots (cdr new-roots))))
4269
4270           (if threads
4271               ;; If there are some threads, we do them before the
4272               ;; threads on the stack.
4273               (setq thread threads
4274                     gnus-tmp-header (caar thread))
4275             ;; There were no current threads, so we pop something off
4276             ;; the stack.
4277             (setq state (car stack)
4278                   gnus-tmp-level (car state)
4279                   tree-stack (cadr state)
4280                   thread (caddr state)
4281                   stack (cdr stack)
4282                   gnus-tmp-header (caar thread))))
4283
4284         (setq gnus-tmp-false-parent nil)
4285         (setq gnus-tmp-root-expunged nil)
4286         (setq thread-end nil)
4287
4288         (if (stringp gnus-tmp-header)
4289             ;; The header is a dummy root.
4290             (cond
4291              ((eq gnus-summary-make-false-root 'adopt)
4292               ;; We let the first article adopt the rest.
4293               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4294                                                (cddar thread)))
4295               (setq gnus-tmp-gathered
4296                     (nconc (mapcar
4297                             (lambda (h) (mail-header-number (car h)))
4298                             (cddar thread))
4299                            gnus-tmp-gathered))
4300               (setq thread (cons (list (caar thread)
4301                                        (cadar thread))
4302                                  (cdr thread)))
4303               (setq gnus-tmp-level -1
4304                     gnus-tmp-false-parent t))
4305              ((eq gnus-summary-make-false-root 'empty)
4306               ;; We print adopted articles with empty subject fields.
4307               (setq gnus-tmp-gathered
4308                     (nconc (mapcar
4309                             (lambda (h) (mail-header-number (car h)))
4310                             (cddar thread))
4311                            gnus-tmp-gathered))
4312               (setq gnus-tmp-level -1))
4313              ((eq gnus-summary-make-false-root 'dummy)
4314               ;; We remember that we probably want to output a dummy
4315               ;; root.
4316               (setq gnus-tmp-dummy-line gnus-tmp-header)
4317               (setq gnus-tmp-prev-subject gnus-tmp-header))
4318              (t
4319               ;; We do not make a root for the gathered
4320               ;; sub-threads at all.
4321               (setq gnus-tmp-level -1)))
4322
4323           (setq number (mail-header-number gnus-tmp-header)
4324                 subject (mail-header-subject gnus-tmp-header))
4325
4326           (cond
4327            ;; If the thread has changed subject, we might want to make
4328            ;; this subthread into a root.
4329            ((and (null gnus-thread-ignore-subject)
4330                  (not (zerop gnus-tmp-level))
4331                  gnus-tmp-prev-subject
4332                  (not (inline
4333                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4334             (setq new-roots (nconc new-roots (list (car thread)))
4335                   thread-end t
4336                   gnus-tmp-header nil))
4337            ;; If the article lies outside the current limit,
4338            ;; then we do not display it.
4339            ((not (memq number gnus-newsgroup-limit))
4340             (setq gnus-tmp-gathered
4341                   (nconc (mapcar
4342                           (lambda (h) (mail-header-number (car h)))
4343                           (cdar thread))
4344                          gnus-tmp-gathered))
4345             (setq gnus-tmp-new-adopts (if (cdar thread)
4346                                           (append gnus-tmp-new-adopts
4347                                                   (cdar thread))
4348                                         gnus-tmp-new-adopts)
4349                   thread-end t
4350                   gnus-tmp-header nil)
4351             (when (zerop gnus-tmp-level)
4352               (setq gnus-tmp-root-expunged t)))
4353            ;; Perhaps this article is to be marked as read?
4354            ((and gnus-summary-mark-below
4355                  (< (or (cdr (assq number gnus-newsgroup-scored))
4356                         default-score)
4357                     gnus-summary-mark-below)
4358                  ;; Don't touch sparse articles.
4359                  (not (gnus-summary-article-sparse-p number))
4360                  (not (gnus-summary-article-ancient-p number)))
4361             (setq gnus-newsgroup-unreads
4362                   (delq number gnus-newsgroup-unreads))
4363             (if gnus-newsgroup-auto-expire
4364                 (push number gnus-newsgroup-expirable)
4365               (push (cons number gnus-low-score-mark)
4366                     gnus-newsgroup-reads))))
4367
4368           (when gnus-tmp-header
4369             ;; We may have an old dummy line to output before this
4370             ;; article.
4371             (when (and gnus-tmp-dummy-line
4372                        (gnus-subject-equal
4373                         gnus-tmp-dummy-line
4374                         (mail-header-subject gnus-tmp-header)))
4375               (gnus-summary-insert-dummy-line
4376                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4377               (setq gnus-tmp-dummy-line nil))
4378
4379             ;; Compute the mark.
4380             (setq gnus-tmp-unread (gnus-article-mark number))
4381
4382             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4383                                   gnus-tmp-header gnus-tmp-level)
4384                   gnus-newsgroup-data)
4385
4386             ;; Actually insert the line.
4387             (setq
4388              gnus-tmp-subject-or-nil
4389              (cond
4390               ((and gnus-thread-ignore-subject
4391                     gnus-tmp-prev-subject
4392                     (not (inline (gnus-subject-equal
4393                                   gnus-tmp-prev-subject subject))))
4394                subject)
4395               ((zerop gnus-tmp-level)
4396                (if (and (eq gnus-summary-make-false-root 'empty)
4397                         (memq number gnus-tmp-gathered)
4398                         gnus-tmp-prev-subject
4399                         (inline (gnus-subject-equal
4400                                  gnus-tmp-prev-subject subject)))
4401                    gnus-summary-same-subject
4402                  subject))
4403               (t gnus-summary-same-subject)))
4404             (if (and (eq gnus-summary-make-false-root 'adopt)
4405                      (= gnus-tmp-level 1)
4406                      (memq number gnus-tmp-gathered))
4407                 (setq gnus-tmp-opening-bracket ?\<
4408                       gnus-tmp-closing-bracket ?\>)
4409               (setq gnus-tmp-opening-bracket ?\[
4410                     gnus-tmp-closing-bracket ?\]))
4411             (setq
4412              gnus-tmp-indentation
4413              (aref gnus-thread-indent-array gnus-tmp-level)
4414              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4415              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4416                                 gnus-summary-default-score 0)
4417              gnus-tmp-score-char
4418              (if (or (null gnus-summary-default-score)
4419                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4420                          gnus-summary-zcore-fuzz))
4421                  ?                      ;Whitespace
4422                (if (< gnus-tmp-score gnus-summary-default-score)
4423                    gnus-score-below-mark gnus-score-over-mark))
4424              gnus-tmp-replied
4425              (cond ((memq number gnus-newsgroup-processable)
4426                     gnus-process-mark)
4427                    ((memq number gnus-newsgroup-cached)
4428                     gnus-cached-mark)
4429                    ((memq number gnus-newsgroup-replied)
4430                     gnus-replied-mark)
4431                    ((memq number gnus-newsgroup-forwarded)
4432                     gnus-forwarded-mark)
4433                    ((memq number gnus-newsgroup-saved)
4434                     gnus-saved-mark)
4435                    ((memq number gnus-newsgroup-recent)
4436                     gnus-recent-mark)
4437                    ((memq number gnus-newsgroup-unseen)
4438                     gnus-unseen-mark)
4439                    (t gnus-no-mark))
4440              gnus-tmp-from (mail-header-from gnus-tmp-header)
4441              gnus-tmp-name
4442              (cond
4443               ((string-match "<[^>]+> *$" gnus-tmp-from)
4444                (setq beg-match (match-beginning 0))
4445                (or (and (string-match "^\".+\"" gnus-tmp-from)
4446                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4447                    (substring gnus-tmp-from 0 beg-match)))
4448               ((string-match "(.+)" gnus-tmp-from)
4449                (substring gnus-tmp-from
4450                           (1+ (match-beginning 0)) (1- (match-end 0))))
4451               (t gnus-tmp-from))
4452              gnus-tmp-thread-tree-header-string
4453              (cond
4454               ((not gnus-show-threads) "")
4455               ((zerop gnus-tmp-level)
4456                (if (cdar thread)
4457                    (or gnus-sum-thread-tree-root subject)
4458                  (or gnus-sum-thread-tree-single-indent subject)))
4459               (t
4460                (concat (apply 'concat
4461                               (mapcar (lambda (item)
4462                                         (if (= item 1)
4463                                             gnus-sum-thread-tree-vertical
4464                                           gnus-sum-thread-tree-indent))
4465                                       (cdr (reverse tree-stack))))
4466                        (if (nth 1 thread)
4467                            gnus-sum-thread-tree-leaf-with-other
4468                          gnus-sum-thread-tree-single-leaf)))))
4469             (when (string= gnus-tmp-name "")
4470               (setq gnus-tmp-name gnus-tmp-from))
4471             (unless (numberp gnus-tmp-lines)
4472               (setq gnus-tmp-lines -1))
4473             (if (= gnus-tmp-lines -1)
4474                 (setq gnus-tmp-lines "?")
4475               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4476             (gnus-put-text-property
4477              (point)
4478              (progn (eval gnus-summary-line-format-spec) (point))
4479              'gnus-number number)
4480             (when gnus-visual-p
4481               (forward-line -1)
4482               (gnus-run-hooks 'gnus-summary-update-hook)
4483               (forward-line 1))
4484
4485             (setq gnus-tmp-prev-subject subject)))
4486
4487         (when (nth 1 thread)
4488           (push (list (max 0 gnus-tmp-level)
4489                       (copy-list tree-stack)
4490                       (nthcdr 1 thread))
4491                 stack))
4492         (push (if (nth 1 thread) 1 0) tree-stack)
4493         (incf gnus-tmp-level)
4494         (setq threads (if thread-end nil (cdar thread)))
4495         (unless threads
4496           (setq gnus-tmp-level 0)))))
4497   (gnus-message 7 "Generating summary...done"))
4498
4499 (defun gnus-summary-prepare-unthreaded (headers)
4500   "Generate an unthreaded summary buffer based on HEADERS."
4501   (let (header number mark)
4502
4503     (beginning-of-line)
4504
4505     (while headers
4506       ;; We may have to root out some bad articles...
4507       (when (memq (setq number (mail-header-number
4508                                 (setq header (pop headers))))
4509                   gnus-newsgroup-limit)
4510         ;; Mark article as read when it has a low score.
4511         (when (and gnus-summary-mark-below
4512                    (< (or (cdr (assq number gnus-newsgroup-scored))
4513                           gnus-summary-default-score 0)
4514                       gnus-summary-mark-below)
4515                    (not (gnus-summary-article-ancient-p number)))
4516           (setq gnus-newsgroup-unreads
4517                 (delq number gnus-newsgroup-unreads))
4518           (if gnus-newsgroup-auto-expire
4519               (push number gnus-newsgroup-expirable)
4520             (push (cons number gnus-low-score-mark)
4521                   gnus-newsgroup-reads)))
4522
4523         (setq mark (gnus-article-mark number))
4524         (push (gnus-data-make number mark (1+ (point)) header 0)
4525               gnus-newsgroup-data)
4526         (gnus-summary-insert-line
4527          header 0 number
4528          mark (memq number gnus-newsgroup-replied)
4529          (memq number gnus-newsgroup-expirable)
4530          (mail-header-subject header) nil
4531          (cdr (assq number gnus-newsgroup-scored))
4532          (memq number gnus-newsgroup-processable))))))
4533
4534 (defun gnus-summary-remove-list-identifiers ()
4535   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4536   (let ((regexp (if (consp gnus-list-identifiers)
4537                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4538                   gnus-list-identifiers))
4539         changed subject)
4540     (when regexp
4541       (dolist (header gnus-newsgroup-headers)
4542         (setq subject (mail-header-subject header)
4543               changed nil)
4544         (while (string-match
4545                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4546                 subject)
4547           (setq subject
4548                 (concat (substring subject 0 (match-beginning 2))
4549                         (substring subject (match-end 0)))
4550                 changed t))
4551         (when (and changed
4552                    (string-match
4553                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4554           (setq subject
4555                 (concat (substring subject 0 (match-beginning 1))
4556                         (substring subject (match-end 1)))))
4557         (when changed
4558           (mail-header-set-subject header subject))))))
4559
4560 (defun gnus-fetch-headers (articles)
4561   "Fetch headers of ARTICLES."
4562   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4563     (gnus-message 5 "Fetching headers for %s..." name)
4564     (prog1
4565         (if (eq 'nov
4566                 (setq gnus-headers-retrieved-by
4567                       (gnus-retrieve-headers
4568                        articles gnus-newsgroup-name
4569                        ;; We might want to fetch old headers, but
4570                        ;; not if there is only 1 article.
4571                        (and (or (and
4572                                  (not (eq gnus-fetch-old-headers 'some))
4573                                  (not (numberp gnus-fetch-old-headers)))
4574                                 (> (length articles) 1))
4575                             gnus-fetch-old-headers))))
4576             (gnus-get-newsgroup-headers-xover
4577              articles nil nil gnus-newsgroup-name t)
4578           (gnus-get-newsgroup-headers))
4579       (gnus-message 5 "Fetching headers for %s...done" name))))
4580
4581 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4582   "Select newsgroup GROUP.
4583 If READ-ALL is non-nil, all articles in the group are selected.
4584 If SELECT-ARTICLES, only select those articles from GROUP."
4585   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4586          ;;!!! Dirty hack; should be removed.
4587          (gnus-summary-ignore-duplicates
4588           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4589               t
4590             gnus-summary-ignore-duplicates))
4591          (info (nth 2 entry))
4592          articles fetched-articles cached)
4593
4594     (unless (gnus-check-server
4595              (set (make-local-variable 'gnus-current-select-method)
4596                   (gnus-find-method-for-group group)))
4597       (error "Couldn't open server"))
4598
4599     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4600         (gnus-activate-group group)     ; Or we can activate it...
4601         (progn                          ; Or we bug out.
4602           (when (equal major-mode 'gnus-summary-mode)
4603             (kill-buffer (current-buffer)))
4604           (error "Couldn't activate group %s: %s"
4605                  group (gnus-status-message group))))
4606
4607     (unless (gnus-request-group group t)
4608       (when (equal major-mode 'gnus-summary-mode)
4609         (kill-buffer (current-buffer)))
4610       (error "Couldn't request group %s: %s"
4611              group (gnus-status-message group)))
4612
4613     (setq gnus-newsgroup-name group
4614           gnus-newsgroup-unselected nil
4615           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4616
4617     (let ((display (gnus-group-find-parameter group 'display)))
4618       (setq gnus-newsgroup-display
4619             (cond
4620              ((not (zerop (or (car-safe read-all) 0)))
4621               ;; The user entered the group with C-u SPC/RET, let's show
4622               ;; all articles.
4623               'gnus-not-ignore)
4624              ((eq display 'all)
4625               'gnus-not-ignore)
4626              ((arrayp display)
4627               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4628              ((numberp display)
4629               ;; The following is probably the "correct" solution, but
4630               ;; it makes Gnus fetch all headers and then limit the
4631               ;; articles (which is slow), so instead we hack the
4632               ;; select-articles parameter instead. -- Simon Josefsson
4633               ;; <jas@kth.se>
4634               ;;
4635               ;; (gnus-byte-compile
4636               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4637               ;;                         display)))))
4638               (setq select-articles
4639                     (gnus-uncompress-range
4640                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4641                              (if (> tmp 0)
4642                                  tmp
4643                                1))
4644                            (cdr (gnus-active group)))))
4645               nil)
4646              (t
4647               nil))))
4648
4649     (gnus-summary-setup-default-charset)
4650
4651     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4652     (when (gnus-virtual-group-p group)
4653       (setq cached gnus-newsgroup-cached))
4654
4655     (setq gnus-newsgroup-unreads
4656           (gnus-set-difference
4657            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4658            gnus-newsgroup-dormant))
4659
4660     (setq gnus-newsgroup-processable nil)
4661
4662     (gnus-update-read-articles group gnus-newsgroup-unreads)
4663
4664     ;; Adjust and set lists of article marks.
4665     (when info
4666       (gnus-adjust-marked-articles info))
4667
4668     (if (setq articles select-articles)
4669         (setq gnus-newsgroup-unselected
4670               (gnus-sorted-intersection
4671                gnus-newsgroup-unreads
4672                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4673       (setq articles (gnus-articles-to-read group read-all)))
4674
4675     (cond
4676      ((null articles)
4677       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4678       'quit)
4679      ((eq articles 0) nil)
4680      (t
4681       ;; Init the dependencies hash table.
4682       (setq gnus-newsgroup-dependencies
4683             (gnus-make-hashtable (length articles)))
4684       (gnus-set-global-variables)
4685       ;; Retrieve the headers and read them in.
4686       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4687
4688       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4689       (when cached
4690         (setq gnus-newsgroup-cached cached))
4691
4692       ;; Suppress duplicates?
4693       (when gnus-suppress-duplicates
4694         (gnus-dup-suppress-articles))
4695
4696       ;; Set the initial limit.
4697       (setq gnus-newsgroup-limit (copy-sequence articles))
4698       ;; Remove canceled articles from the list of unread articles.
4699       (setq fetched-articles
4700             (mapcar (lambda (headers) (mail-header-number headers))
4701                     gnus-newsgroup-headers))
4702       (setq gnus-newsgroup-articles fetched-articles)
4703       (setq gnus-newsgroup-unreads
4704             (gnus-set-sorted-intersection
4705              gnus-newsgroup-unreads fetched-articles))
4706
4707       ;; The `seen' marks are treated specially.
4708       (if (not gnus-newsgroup-seen)
4709           (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4710         (dolist (article gnus-newsgroup-articles)
4711           (unless (gnus-member-of-range article gnus-newsgroup-seen)
4712             (push article gnus-newsgroup-unseen)))
4713         (setq gnus-newsgroup-unseen (nreverse gnus-newsgroup-unseen)))
4714
4715       ;; Removed marked articles that do not exist.
4716       (gnus-update-missing-marks
4717        (gnus-sorted-complement fetched-articles articles))
4718       ;; We might want to build some more threads first.
4719       (when (and gnus-fetch-old-headers
4720                  (eq gnus-headers-retrieved-by 'nov))
4721         (if (eq gnus-fetch-old-headers 'invisible)
4722             (gnus-build-all-threads)
4723           (gnus-build-old-threads)))
4724       ;; Let the Gnus agent mark articles as read.
4725       (when gnus-agent
4726         (gnus-agent-get-undownloaded-list))
4727       ;; Remove list identifiers from subject
4728       (when gnus-list-identifiers
4729         (gnus-summary-remove-list-identifiers))
4730       ;; Check whether auto-expire is to be done in this group.
4731       (setq gnus-newsgroup-auto-expire
4732             (gnus-group-auto-expirable-p group))
4733       ;; Set up the article buffer now, if necessary.
4734       (unless gnus-single-article-buffer
4735         (gnus-article-setup-buffer))
4736       ;; First and last article in this newsgroup.
4737       (when gnus-newsgroup-headers
4738         (setq gnus-newsgroup-begin
4739               (mail-header-number (car gnus-newsgroup-headers))
4740               gnus-newsgroup-end
4741               (mail-header-number
4742                (gnus-last-element gnus-newsgroup-headers))))
4743       ;; GROUP is successfully selected.
4744       (or gnus-newsgroup-headers t)))))
4745
4746 (defun gnus-summary-display-make-predicate (display)
4747   (require 'gnus-agent)
4748   (when (= (length display) 1)
4749     (setq display (car display)))
4750   (unless gnus-summary-display-cache
4751     (dolist (elem (append (list (cons 'read 'read)
4752                                 (cons 'unseen 'unseen))
4753                           gnus-article-mark-lists))
4754       (push (cons (cdr elem)
4755                   (gnus-byte-compile
4756                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4757             gnus-summary-display-cache)))
4758   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4759     (gnus-get-predicate display)))
4760
4761 ;; Uses the dynamically bound `number' variable.
4762 (defvar number)
4763 (defun gnus-article-marked-p (type &optional article)
4764   (let ((article (or article number)))
4765     (cond
4766      ((eq type 'tick)
4767       (memq article gnus-newsgroup-marked))
4768      ((eq type 'unsend)
4769       (memq article gnus-newsgroup-unsendable))
4770      ((eq type 'undownload)
4771       (memq article gnus-newsgroup-undownloaded))
4772      ((eq type 'download)
4773       (memq article gnus-newsgroup-downloadable))
4774      ((eq type 'unread)
4775       (memq article gnus-newsgroup-unreads))
4776      ((eq type 'read)
4777       (memq article gnus-newsgroup-reads))
4778      ((eq type 'dormant)
4779       (memq article gnus-newsgroup-dormant) )
4780      ((eq type 'expire)
4781       (memq article gnus-newsgroup-expirable))
4782      ((eq type 'reply)
4783       (memq article gnus-newsgroup-replied))
4784      ((eq type 'killed)
4785       (memq article gnus-newsgroup-killed))
4786      ((eq type 'bookmark)
4787       (assq article gnus-newsgroup-bookmarks))
4788      ((eq type 'score)
4789       (assq article gnus-newsgroup-scored))
4790      ((eq type 'save)
4791       (memq article gnus-newsgroup-saved))
4792      ((eq type 'cache)
4793       (memq article gnus-newsgroup-cached))
4794      ((eq type 'forward)
4795       (memq article gnus-newsgroup-forwarded))
4796      ((eq type 'seen)
4797       (not (memq article gnus-newsgroup-unseen)))
4798      ((eq type 'recent)
4799       (memq article gnus-newsgroup-recent))
4800      (t t))))
4801
4802 (defun gnus-articles-to-read (group &optional read-all)
4803   "Find out what articles the user wants to read."
4804   (let* ((articles
4805           ;; Select all articles if `read-all' is non-nil, or if there
4806           ;; are no unread articles.
4807           (if (or read-all
4808                   (and (zerop (length gnus-newsgroup-marked))
4809                        (zerop (length gnus-newsgroup-unreads)))
4810                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4811               ;; We want to select the headers for all the articles in
4812               ;; the group, so we select either all the active
4813               ;; articles in the group, or (if that's nil), the
4814               ;; articles in the cache.
4815               (or
4816                (gnus-uncompress-range (gnus-active group))
4817                (gnus-cache-articles-in-group group))
4818             ;; Select only the "normal" subset of articles.
4819             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4820                           (copy-sequence gnus-newsgroup-unreads))
4821                   '<)))
4822          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4823          (scored (length scored-list))
4824          (number (length articles))
4825          (marked (+ (length gnus-newsgroup-marked)
4826                     (length gnus-newsgroup-dormant)))
4827          (select
4828           (cond
4829            ((numberp read-all)
4830             read-all)
4831            (t
4832             (condition-case ()
4833                 (cond
4834                  ((and (or (<= scored marked) (= scored number))
4835                        (numberp gnus-large-newsgroup)
4836                        (> number gnus-large-newsgroup))
4837                   (let ((input
4838                          (read-string
4839                           (format
4840                            "How many articles from %s (default %d): "
4841                            (gnus-limit-string
4842                             (gnus-group-decoded-name gnus-newsgroup-name)
4843                             35)
4844                            number))))
4845                     (if (string-match "^[ \t]*$" input) number input)))
4846                  ((and (> scored marked) (< scored number)
4847                        (> (- scored number) 20))
4848                   (let ((input
4849                          (read-string
4850                           (format "%s %s (%d scored, %d total): "
4851                                   "How many articles from"
4852                                   (gnus-group-decoded-name group)
4853                                   scored number))))
4854                     (if (string-match "^[ \t]*$" input)
4855                         number input)))
4856                  (t number))
4857               (quit
4858                (message "Quit getting the articles to read")
4859                nil))))))
4860     (setq select (if (stringp select) (string-to-number select) select))
4861     (if (or (null select) (zerop select))
4862         select
4863       (if (and (not (zerop scored)) (<= (abs select) scored))
4864           (progn
4865             (setq articles (sort scored-list '<))
4866             (setq number (length articles)))
4867         (setq articles (copy-sequence articles)))
4868
4869       (when (< (abs select) number)
4870         (if (< select 0)
4871             ;; Select the N oldest articles.
4872             (setcdr (nthcdr (1- (abs select)) articles) nil)
4873           ;; Select the N most recent articles.
4874           (setq articles (nthcdr (- number select) articles))))
4875       (setq gnus-newsgroup-unselected
4876             (gnus-sorted-intersection
4877              gnus-newsgroup-unreads
4878              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4879       (when gnus-alter-articles-to-read-function
4880         (setq gnus-newsgroup-unreads
4881               (sort
4882                (funcall gnus-alter-articles-to-read-function
4883                         gnus-newsgroup-name gnus-newsgroup-unreads)
4884                '<)))
4885       articles)))
4886
4887 (defun gnus-killed-articles (killed articles)
4888   (let (out)
4889     (while articles
4890       (when (inline (gnus-member-of-range (car articles) killed))
4891         (push (car articles) out))
4892       (setq articles (cdr articles)))
4893     out))
4894
4895 (defun gnus-uncompress-marks (marks)
4896   "Uncompress the mark ranges in MARKS."
4897   (let ((uncompressed '(score bookmark))
4898         out)
4899     (while marks
4900       (if (memq (caar marks) uncompressed)
4901           (push (car marks) out)
4902         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4903       (setq marks (cdr marks)))
4904     out))
4905
4906 (defun gnus-article-mark-to-type (mark)
4907   "Return the type of MARK."
4908   (or (cadr (assq mark gnus-article-special-mark-lists))
4909       'list))
4910
4911 (defun gnus-article-unpropagatable-p (mark)
4912   "Return whether MARK should be propagated to backend."
4913   (memq mark gnus-article-unpropagated-mark-lists))
4914
4915 (defun gnus-adjust-marked-articles (info)
4916   "Set all article lists and remove all marks that are no longer valid."
4917   (let* ((marked-lists (gnus-info-marks info))
4918          (active (gnus-active (gnus-info-group info)))
4919          (min (car active))
4920          (max (cdr active))
4921          (types gnus-article-mark-lists)
4922          marks var articles article mark mark-type)
4923
4924     (dolist (marks marked-lists)
4925       (setq mark (car marks)
4926             mark-type (gnus-article-mark-to-type mark)
4927             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4928
4929       ;; We set the variable according to the type of the marks list,
4930       ;; and then adjust the marks to a subset of the active articles.
4931       (cond
4932        ;; Adjust "simple" lists.
4933        ((eq mark-type 'list)
4934         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4935         (when (memq mark '(tick dormant expire reply save))
4936           (while articles
4937             (when (or (< (setq article (pop articles)) min) (> article max))
4938               (set var (delq article (symbol-value var)))))))
4939        ;; Adjust assocs.
4940        ((eq mark-type 'tuple)
4941         (set var (setq articles (cdr marks)))
4942         (when (not (listp (cdr (symbol-value var))))
4943           (set var (list (symbol-value var))))
4944         (when (not (listp (cdr articles)))
4945           (setq articles (list articles)))
4946         (while articles
4947           (when (or (not (consp (setq article (pop articles))))
4948                     (< (car article) min)
4949                     (> (car article) max))
4950             (set var (delq article (symbol-value var))))))
4951        ;; Adjust ranges (sloppily).
4952        ((eq mark-type 'range)
4953         (cond
4954          ((eq mark 'seen)
4955           (setq articles (cdr marks))
4956           (while (and articles
4957                       (or (and (consp (car articles))
4958                                (> min (cdar articles)))
4959                           (and (numberp (car articles))
4960                                (> min (car articles)))))
4961             (pop articles))
4962           (set var articles))))))))
4963
4964 (defun gnus-update-missing-marks (missing)
4965   "Go through the list of MISSING articles and remove them from the mark lists."
4966   (when missing
4967     (let (var m)
4968       ;; Go through all types.
4969       (dolist (elem gnus-article-mark-lists)
4970         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4971           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4972           (when (symbol-value var)
4973             ;; This list has articles.  So we delete all missing
4974             ;; articles from it.
4975             (setq m missing)
4976             (while m
4977               (set var (delq (pop m) (symbol-value var))))))))))
4978
4979 (defun gnus-update-marks ()
4980   "Enter the various lists of marked articles into the newsgroup info list."
4981   (let ((types gnus-article-mark-lists)
4982         (info (gnus-get-info gnus-newsgroup-name))
4983         type list newmarked symbol delta-marks)
4984     (when info
4985       ;; Add all marks lists to the list of marks lists.
4986       (while (setq type (pop types))
4987         (setq list (symbol-value
4988                     (setq symbol
4989                           (intern (format "gnus-newsgroup-%s" (car type))))))
4990
4991         (when list
4992           ;; Get rid of the entries of the articles that have the
4993           ;; default score.
4994           (when (and (eq (cdr type) 'score)
4995                      gnus-save-score
4996                      list)
4997             (let* ((arts list)
4998                    (prev (cons nil list))
4999                    (all prev))
5000               (while arts
5001                 (if (or (not (consp (car arts)))
5002                         (= (cdar arts) gnus-summary-default-score))
5003                     (setcdr prev (cdr arts))
5004                   (setq prev arts))
5005                 (setq arts (cdr arts)))
5006               (setq list (cdr all)))))
5007
5008         (when (eq (cdr type) 'seen)
5009           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5010
5011         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5012           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5013
5014         (when (and (gnus-check-backend-function
5015                     'request-set-mark gnus-newsgroup-name)
5016                    (not (gnus-article-unpropagatable-p (cdr type))))
5017           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5018                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5019                  (add (gnus-remove-from-range
5020                        (gnus-copy-sequence list) old)))
5021             (when add
5022               (push (list add 'add (list (cdr type))) delta-marks))
5023             (when del
5024               (push (list del 'del (list (cdr type))) delta-marks))))
5025
5026         (when list
5027           (push (cons (cdr type) list) newmarked)))
5028
5029       (when delta-marks
5030         (unless (gnus-check-group gnus-newsgroup-name)
5031           (error "Can't open server for %s" gnus-newsgroup-name))
5032         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5033
5034       ;; Enter these new marks into the info of the group.
5035       (if (nthcdr 3 info)
5036           (setcar (nthcdr 3 info) newmarked)
5037         ;; Add the marks lists to the end of the info.
5038         (when newmarked
5039           (setcdr (nthcdr 2 info) (list newmarked))))
5040
5041       ;; Cut off the end of the info if there's nothing else there.
5042       (let ((i 5))
5043         (while (and (> i 2)
5044                     (not (nth i info)))
5045           (when (nthcdr (decf i) info)
5046             (setcdr (nthcdr i info) nil)))))))
5047
5048 (defun gnus-set-mode-line (where)
5049   "Set the mode line of the article or summary buffers.
5050 If WHERE is `summary', the summary mode line format will be used."
5051   ;; Is this mode line one we keep updated?
5052   (when (and (memq where gnus-updated-mode-lines)
5053              (symbol-value
5054               (intern (format "gnus-%s-mode-line-format-spec" where))))
5055     (let (mode-string)
5056       (save-excursion
5057         ;; We evaluate this in the summary buffer since these
5058         ;; variables are buffer-local to that buffer.
5059         (set-buffer gnus-summary-buffer)
5060        ;; We bind all these variables that are used in the `eval' form
5061         ;; below.
5062         (let* ((mformat (symbol-value
5063                          (intern
5064                           (format "gnus-%s-mode-line-format-spec" where))))
5065                (gnus-tmp-group-name (gnus-group-decoded-name
5066                                      gnus-newsgroup-name))
5067                (gnus-tmp-article-number (or gnus-current-article 0))
5068                (gnus-tmp-unread gnus-newsgroup-unreads)
5069                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5070                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5071                (gnus-tmp-unread-and-unselected
5072                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5073                             (zerop gnus-tmp-unselected))
5074                        "")
5075                       ((zerop gnus-tmp-unselected)
5076                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5077                       (t (format "{%d(+%d) more}"
5078                                  gnus-tmp-unread-and-unticked
5079                                  gnus-tmp-unselected))))
5080                (gnus-tmp-subject
5081                 (if (and gnus-current-headers
5082                          (vectorp gnus-current-headers))
5083                     (gnus-mode-string-quote
5084                      (mail-header-subject gnus-current-headers))
5085                   ""))
5086                bufname-length max-len
5087                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5088           (setq mode-string (eval mformat))
5089           (setq bufname-length (if (string-match "%b" mode-string)
5090                                    (- (length
5091                                        (buffer-name
5092                                         (if (eq where 'summary)
5093                                             nil
5094                                           (get-buffer gnus-article-buffer))))
5095                                       2)
5096                                  0))
5097           (setq max-len (max 4 (if gnus-mode-non-string-length
5098                                    (- (window-width)
5099                                       gnus-mode-non-string-length
5100                                       bufname-length)
5101                                  (length mode-string))))
5102           ;; We might have to chop a bit of the string off...
5103           (when (> (length mode-string) max-len)
5104             (setq mode-string
5105                   (concat (truncate-string-to-width mode-string (- max-len 3))
5106                           "...")))
5107           ;; Pad the mode string a bit.
5108           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5109       ;; Update the mode line.
5110       (setq mode-line-buffer-identification
5111             (gnus-mode-line-buffer-identification (list mode-string)))
5112       (set-buffer-modified-p t))))
5113
5114 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5115   "Go through the HEADERS list and add all Xrefs to a hash table.
5116 The resulting hash table is returned, or nil if no Xrefs were found."
5117   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5118          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5119          (xref-hashtb (gnus-make-hashtable))
5120          start group entry number xrefs header)
5121     (while headers
5122       (setq header (pop headers))
5123       (when (and (setq xrefs (mail-header-xref header))
5124                  (not (memq (setq number (mail-header-number header))
5125                             unreads)))
5126         (setq start 0)
5127         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5128           (setq start (match-end 0))
5129           (setq group (if prefix
5130                           (concat prefix (substring xrefs (match-beginning 1)
5131                                                     (match-end 1)))
5132                         (substring xrefs (match-beginning 1) (match-end 1))))
5133           (setq number
5134                 (string-to-int (substring xrefs (match-beginning 2)
5135                                           (match-end 2))))
5136           (if (setq entry (gnus-gethash group xref-hashtb))
5137               (setcdr entry (cons number (cdr entry)))
5138             (gnus-sethash group (cons number nil) xref-hashtb)))))
5139     (and start xref-hashtb)))
5140
5141 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5142   "Look through all the headers and mark the Xrefs as read."
5143   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5144         name entry info xref-hashtb idlist method nth4)
5145     (save-excursion
5146       (set-buffer gnus-group-buffer)
5147       (when (setq xref-hashtb
5148                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5149         (mapatoms
5150          (lambda (group)
5151            (unless (string= from-newsgroup (setq name (symbol-name group)))
5152              (setq idlist (symbol-value group))
5153              ;; Dead groups are not updated.
5154              (and (prog1
5155                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5156                             info (nth 2 entry))
5157                     (when (stringp (setq nth4 (gnus-info-method info)))
5158                       (setq nth4 (gnus-server-to-method nth4))))
5159                   ;; Only do the xrefs if the group has the same
5160                   ;; select method as the group we have just read.
5161                   (or (gnus-methods-equal-p
5162                        nth4 (gnus-find-method-for-group from-newsgroup))
5163                       virtual
5164                       (equal nth4 (setq method (gnus-find-method-for-group
5165                                                 from-newsgroup)))
5166                       (and (equal (car nth4) (car method))
5167                            (equal (nth 1 nth4) (nth 1 method))))
5168                   gnus-use-cross-reference
5169                   (or (not (eq gnus-use-cross-reference t))
5170                       virtual
5171                       ;; Only do cross-references on subscribed
5172                       ;; groups, if that is what is wanted.
5173                       (<= (gnus-info-level info) gnus-level-subscribed))
5174                   (gnus-group-make-articles-read name idlist))))
5175          xref-hashtb)))))
5176
5177 (defun gnus-compute-read-articles (group articles)
5178   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5179          (info (nth 2 entry))
5180          (active (gnus-active group))
5181          ninfo)
5182     (when entry
5183       ;; First peel off all invalid article numbers.
5184       (when active
5185         (let ((ids articles)
5186               id first)
5187           (while (setq id (pop ids))
5188             (when (and first (> id (cdr active)))
5189               ;; We'll end up in this situation in one particular
5190               ;; obscure situation.  If you re-scan a group and get
5191               ;; a new article that is cross-posted to a different
5192               ;; group that has not been re-scanned, you might get
5193               ;; crossposted article that has a higher number than
5194               ;; Gnus believes possible.  So we re-activate this
5195               ;; group as well.  This might mean doing the
5196               ;; crossposting thingy will *increase* the number
5197               ;; of articles in some groups.  Tsk, tsk.
5198               (setq active (or (gnus-activate-group group) active)))
5199             (when (or (> id (cdr active))
5200                       (< id (car active)))
5201               (setq articles (delq id articles))))))
5202       ;; If the read list is nil, we init it.
5203       (if (and active
5204                (null (gnus-info-read info))
5205                (> (car active) 1))
5206           (setq ninfo (cons 1 (1- (car active))))
5207         (setq ninfo (gnus-info-read info)))
5208       ;; Then we add the read articles to the range.
5209       (gnus-add-to-range
5210        ninfo (setq articles (sort articles '<))))))
5211
5212 (defun gnus-group-make-articles-read (group articles)
5213   "Update the info of GROUP to say that ARTICLES are read."
5214   (let* ((num 0)
5215          (entry (gnus-gethash group gnus-newsrc-hashtb))
5216          (info (nth 2 entry))
5217          (active (gnus-active group))
5218          range)
5219     (when entry
5220       (setq range (gnus-compute-read-articles group articles))
5221       (save-excursion
5222         (set-buffer gnus-group-buffer)
5223         (gnus-undo-register
5224           `(progn
5225              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5226              (gnus-info-set-read ',info ',(gnus-info-read info))
5227              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5228              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5229              (gnus-group-update-group ,group t))))
5230       ;; Add the read articles to the range.
5231       (gnus-info-set-read info range)
5232       (gnus-request-set-mark group (list (list range 'add '(read))))
5233       ;; Then we have to re-compute how many unread
5234       ;; articles there are in this group.
5235       (when active
5236         (cond
5237          ((not range)
5238           (setq num (- (1+ (cdr active)) (car active))))
5239          ((not (listp (cdr range)))
5240           (setq num (- (cdr active) (- (1+ (cdr range))
5241                                        (car range)))))
5242          (t
5243           (while range
5244             (if (numberp (car range))
5245                 (setq num (1+ num))
5246               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5247             (setq range (cdr range)))
5248           (setq num (- (cdr active) num))))
5249         ;; Update the number of unread articles.
5250         (setcar entry num)
5251         ;; Update the group buffer.
5252         (gnus-group-update-group group t)))))
5253
5254 (defvar gnus-newsgroup-none-id 0)
5255
5256 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5257   (let ((cur nntp-server-buffer)
5258         (dependencies
5259          (or dependencies
5260              (save-excursion (set-buffer gnus-summary-buffer)
5261                              gnus-newsgroup-dependencies)))
5262         headers id end ref
5263         (mail-parse-charset gnus-newsgroup-charset)
5264         (mail-parse-ignored-charsets
5265          (save-excursion (condition-case nil
5266                              (set-buffer gnus-summary-buffer)
5267                            (error))
5268                          gnus-newsgroup-ignored-charsets)))
5269     (save-excursion
5270       (set-buffer nntp-server-buffer)
5271       ;; Translate all TAB characters into SPACE characters.
5272       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5273       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5274       (gnus-run-hooks 'gnus-parse-headers-hook)
5275       (let ((case-fold-search t)
5276             in-reply-to header p lines chars)
5277         (goto-char (point-min))
5278         ;; Search to the beginning of the next header.  Error messages
5279         ;; do not begin with 2 or 3.
5280         (while (re-search-forward "^[23][0-9]+ " nil t)
5281           (setq id nil
5282                 ref nil)
5283           ;; This implementation of this function, with nine
5284           ;; search-forwards instead of the one re-search-forward and
5285           ;; a case (which basically was the old function) is actually
5286           ;; about twice as fast, even though it looks messier.  You
5287           ;; can't have everything, I guess.  Speed and elegance
5288           ;; doesn't always go hand in hand.
5289           (setq
5290            header
5291            (vector
5292             ;; Number.
5293             (prog1
5294                 (read cur)
5295               (end-of-line)
5296               (setq p (point))
5297               (narrow-to-region (point)
5298                                 (or (and (search-forward "\n.\n" nil t)
5299                                          (- (point) 2))
5300                                     (point))))
5301             ;; Subject.
5302             (progn
5303               (goto-char p)
5304               (if (search-forward "\nsubject:" nil t)
5305                   (funcall gnus-decode-encoded-word-function
5306                            (nnheader-header-value))
5307                 "(none)"))
5308             ;; From.
5309             (progn
5310               (goto-char p)
5311               (if (search-forward "\nfrom:" nil t)
5312                   (funcall gnus-decode-encoded-word-function
5313                            (nnheader-header-value))
5314                 "(nobody)"))
5315             ;; Date.
5316             (progn
5317               (goto-char p)
5318               (if (search-forward "\ndate:" nil t)
5319                   (nnheader-header-value) ""))
5320             ;; Message-ID.
5321             (progn
5322               (goto-char p)
5323               (setq id (if (re-search-forward
5324                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5325                            ;; We do it this way to make sure the Message-ID
5326                            ;; is (somewhat) syntactically valid.
5327                            (buffer-substring (match-beginning 1)
5328                                              (match-end 1))
5329                          ;; If there was no message-id, we just fake one
5330                          ;; to make subsequent routines simpler.
5331                          (nnheader-generate-fake-message-id))))
5332             ;; References.
5333             (progn
5334               (goto-char p)
5335               (if (search-forward "\nreferences:" nil t)
5336                   (progn
5337                     (setq end (point))
5338                     (prog1
5339                         (nnheader-header-value)
5340                       (setq ref
5341                             (buffer-substring
5342                              (progn
5343                                (end-of-line)
5344                                (search-backward ">" end t)
5345                                (1+ (point)))
5346                              (progn
5347                                (search-backward "<" end t)
5348                                (point))))))
5349                 ;; Get the references from the in-reply-to header if there
5350                 ;; were no references and the in-reply-to header looks
5351                 ;; promising.
5352                 (if (and (search-forward "\nin-reply-to:" nil t)
5353                          (setq in-reply-to (nnheader-header-value))
5354                          (string-match "<[^>]+>" in-reply-to))
5355                     (let (ref2)
5356                       (setq ref (substring in-reply-to (match-beginning 0)
5357                                            (match-end 0)))
5358                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5359                         (setq ref2 (substring in-reply-to (match-beginning 0)
5360                                               (match-end 0)))
5361                         (when (> (length ref2) (length ref))
5362                           (setq ref ref2)))
5363                       ref)
5364                   (setq ref nil))))
5365             ;; Chars.
5366             (progn
5367               (goto-char p)
5368               (if (search-forward "\nchars: " nil t)
5369                   (if (numberp (setq chars (ignore-errors (read cur))))
5370                       chars -1)
5371                 -1))
5372             ;; Lines.
5373             (progn
5374               (goto-char p)
5375               (if (search-forward "\nlines: " nil t)
5376                   (if (numberp (setq lines (ignore-errors (read cur))))
5377                       lines -1)
5378                 -1))
5379             ;; Xref.
5380             (progn
5381               (goto-char p)
5382               (and (search-forward "\nxref:" nil t)
5383                    (nnheader-header-value)))
5384             ;; Extra.
5385             (when gnus-extra-headers
5386               (let ((extra gnus-extra-headers)
5387                     out)
5388                 (while extra
5389                   (goto-char p)
5390                   (when (search-forward
5391                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5392                     (push (cons (car extra) (nnheader-header-value))
5393                           out))
5394                   (pop extra))
5395                 out))))
5396           (when (equal id ref)
5397             (setq ref nil))
5398
5399           (when gnus-alter-header-function
5400             (funcall gnus-alter-header-function header)
5401             (setq id (mail-header-id header)
5402                   ref (gnus-parent-id (mail-header-references header))))
5403
5404           (when (setq header
5405                       (gnus-dependencies-add-header
5406                        header dependencies force-new))
5407             (push header headers))
5408           (goto-char (point-max))
5409           (widen))
5410         (nreverse headers)))))
5411
5412 ;; Goes through the xover lines and returns a list of vectors
5413 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5414                                                   force-new dependencies
5415                                                   group also-fetch-heads)
5416   "Parse the news overview data in the server buffer.
5417 Return a list of headers that match SEQUENCE (see
5418 `nntp-retrieve-headers')."
5419   ;; Get the Xref when the users reads the articles since most/some
5420   ;; NNTP servers do not include Xrefs when using XOVER.
5421   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5422   (let ((mail-parse-charset gnus-newsgroup-charset)
5423         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5424         (cur nntp-server-buffer)
5425         (dependencies (or dependencies gnus-newsgroup-dependencies))
5426         (allp (cond
5427                ((eq gnus-read-all-available-headers t)
5428                 t)
5429                ((stringp gnus-read-all-available-headers)
5430                 (string-match gnus-read-all-available-headers group))
5431                (t
5432                 nil)))
5433         number headers header)
5434     (save-excursion
5435       (set-buffer nntp-server-buffer)
5436       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5437       ;; Allow the user to mangle the headers before parsing them.
5438       (gnus-run-hooks 'gnus-parse-headers-hook)
5439       (goto-char (point-min))
5440       (while (not (eobp))
5441         (condition-case ()
5442             (while (and (or sequence allp)
5443                         (not (eobp)))
5444               (setq number (read cur))
5445               (when (not allp)
5446                 (while (and sequence
5447                             (< (car sequence) number))
5448                   (setq sequence (cdr sequence))))
5449               (when (and (or allp
5450                              (and sequence
5451                                   (eq number (car sequence))))
5452                          (progn
5453                            (setq sequence (cdr sequence))
5454                            (setq header (inline
5455                                           (gnus-nov-parse-line
5456                                            number dependencies force-new)))))
5457                 (push header headers))
5458               (forward-line 1))
5459           (error
5460            (gnus-error 4 "Strange nov line (%d)"
5461                        (count-lines (point-min) (point)))))
5462         (forward-line 1))
5463       ;; A common bug in inn is that if you have posted an article and
5464       ;; then retrieves the active file, it will answer correctly --
5465       ;; the new article is included.  However, a NOV entry for the
5466       ;; article may not have been generated yet, so this may fail.
5467       ;; We work around this problem by retrieving the last few
5468       ;; headers using HEAD.
5469       (if (or (not also-fetch-heads)
5470               (not sequence))
5471           ;; We (probably) got all the headers.
5472           (nreverse headers)
5473         (let ((gnus-nov-is-evil t))
5474           (nconc
5475            (nreverse headers)
5476            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5477              (gnus-get-newsgroup-headers))))))))
5478
5479 (defun gnus-article-get-xrefs ()
5480   "Fill in the Xref value in `gnus-current-headers', if necessary.
5481 This is meant to be called in `gnus-article-internal-prepare-hook'."
5482   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5483                                  gnus-current-headers)))
5484     (or (not gnus-use-cross-reference)
5485         (not headers)
5486         (and (mail-header-xref headers)
5487              (not (string= (mail-header-xref headers) "")))
5488         (let ((case-fold-search t)
5489               xref)
5490           (save-restriction
5491             (nnheader-narrow-to-headers)
5492             (goto-char (point-min))
5493             (when (or (and (not (eobp))
5494                            (eq (downcase (char-after)) ?x)
5495                            (looking-at "Xref:"))
5496                       (search-forward "\nXref:" nil t))
5497               (goto-char (1+ (match-end 0)))
5498               (setq xref (buffer-substring (point)
5499                                            (progn (end-of-line) (point))))
5500               (mail-header-set-xref headers xref)))))))
5501
5502 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5503   "Find article ID and insert the summary line for that article.
5504 OLD-HEADER can either be a header or a line number to insert
5505 the subject line on."
5506   (let* ((line (and (numberp old-header) old-header))
5507          (old-header (and (vectorp old-header) old-header))
5508          (header (cond ((and old-header use-old-header)
5509                         old-header)
5510                        ((and (numberp id)
5511                              (gnus-number-to-header id))
5512                         (gnus-number-to-header id))
5513                        (t
5514                         (gnus-read-header id))))
5515          (number (and (numberp id) id))
5516          d)
5517     (when header
5518       ;; Rebuild the thread that this article is part of and go to the
5519       ;; article we have fetched.
5520       (when (and (not gnus-show-threads)
5521                  old-header)
5522         (when (and number
5523                    (setq d (gnus-data-find (mail-header-number old-header))))
5524           (goto-char (gnus-data-pos d))
5525           (gnus-data-remove
5526            number
5527            (- (gnus-point-at-bol)
5528               (prog1
5529                   (1+ (gnus-point-at-eol))
5530                 (gnus-delete-line))))))
5531       (when old-header
5532         (mail-header-set-number header (mail-header-number old-header)))
5533       (setq gnus-newsgroup-sparse
5534             (delq (setq number (mail-header-number header))
5535                   gnus-newsgroup-sparse))
5536       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5537       (push number gnus-newsgroup-limit)
5538       (gnus-rebuild-thread (mail-header-id header) line)
5539       (gnus-summary-goto-subject number nil t))
5540     (when (and (numberp number)
5541                (> number 0))
5542       ;; We have to update the boundaries even if we can't fetch the
5543       ;; article if ID is a number -- so that the next `P' or `N'
5544       ;; command will fetch the previous (or next) article even
5545       ;; if the one we tried to fetch this time has been canceled.
5546       (when (> number gnus-newsgroup-end)
5547         (setq gnus-newsgroup-end number))
5548       (when (< number gnus-newsgroup-begin)
5549         (setq gnus-newsgroup-begin number))
5550       (setq gnus-newsgroup-unselected
5551             (delq number gnus-newsgroup-unselected)))
5552     ;; Report back a success?
5553     (and header (mail-header-number header))))
5554
5555 ;;; Process/prefix in the summary buffer
5556
5557 (defun gnus-summary-work-articles (n)
5558   "Return a list of articles to be worked upon.
5559 The prefix argument, the list of process marked articles, and the
5560 current article will be taken into consideration."
5561   (save-excursion
5562     (set-buffer gnus-summary-buffer)
5563     (cond
5564      (n
5565       ;; A numerical prefix has been given.
5566       (setq n (prefix-numeric-value n))
5567       (let ((backward (< n 0))
5568             (n (abs (prefix-numeric-value n)))
5569             articles article)
5570         (save-excursion
5571           (while
5572               (and (> n 0)
5573                    (push (setq article (gnus-summary-article-number))
5574                          articles)
5575                    (if backward
5576                        (gnus-summary-find-prev nil article)
5577                      (gnus-summary-find-next nil article)))
5578             (decf n)))
5579         (nreverse articles)))
5580      ((and (gnus-region-active-p) (mark))
5581       (message "region active")
5582       ;; Work on the region between point and mark.
5583       (let ((max (max (point) (mark)))
5584             articles article)
5585         (save-excursion
5586           (goto-char (min (min (point) (mark))))
5587           (while
5588               (and
5589                (push (setq article (gnus-summary-article-number)) articles)
5590                (gnus-summary-find-next nil article)
5591                (< (point) max)))
5592           (nreverse articles))))
5593      (gnus-newsgroup-processable
5594       ;; There are process-marked articles present.
5595       ;; Save current state.
5596       (gnus-summary-save-process-mark)
5597       ;; Return the list.
5598       (reverse gnus-newsgroup-processable))
5599      (t
5600       ;; Just return the current article.
5601       (list (gnus-summary-article-number))))))
5602
5603 (defmacro gnus-summary-iterate (arg &rest forms)
5604   "Iterate over the process/prefixed articles and do FORMS.
5605 ARG is the interactive prefix given to the command.  FORMS will be
5606 executed with point over the summary line of the articles."
5607   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5608     `(let ((,articles (gnus-summary-work-articles ,arg)))
5609        (while ,articles
5610          (gnus-summary-goto-subject (car ,articles))
5611          ,@forms
5612          (pop ,articles)))))
5613
5614 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5615 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5616
5617 (defun gnus-summary-save-process-mark ()
5618   "Push the current set of process marked articles on the stack."
5619   (interactive)
5620   (push (copy-sequence gnus-newsgroup-processable)
5621         gnus-newsgroup-process-stack))
5622
5623 (defun gnus-summary-kill-process-mark ()
5624   "Push the current set of process marked articles on the stack and unmark."
5625   (interactive)
5626   (gnus-summary-save-process-mark)
5627   (gnus-summary-unmark-all-processable))
5628
5629 (defun gnus-summary-yank-process-mark ()
5630   "Pop the last process mark state off the stack and restore it."
5631   (interactive)
5632   (unless gnus-newsgroup-process-stack
5633     (error "Empty mark stack"))
5634   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5635
5636 (defun gnus-summary-process-mark-set (set)
5637   "Make SET into the current process marked articles."
5638   (gnus-summary-unmark-all-processable)
5639   (while set
5640     (gnus-summary-set-process-mark (pop set))))
5641
5642 ;;; Searching and stuff
5643
5644 (defun gnus-summary-search-group (&optional backward use-level)
5645   "Search for next unread newsgroup.
5646 If optional argument BACKWARD is non-nil, search backward instead."
5647   (save-excursion
5648     (set-buffer gnus-group-buffer)
5649     (when (gnus-group-search-forward
5650            backward nil (if use-level (gnus-group-group-level) nil))
5651       (gnus-group-group-name))))
5652
5653 (defun gnus-summary-best-group (&optional exclude-group)
5654   "Find the name of the best unread group.
5655 If EXCLUDE-GROUP, do not go to this group."
5656   (save-excursion
5657     (set-buffer gnus-group-buffer)
5658     (save-excursion
5659       (gnus-group-best-unread-group exclude-group))))
5660
5661 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5662   (if backward (gnus-summary-find-prev)
5663     (let* ((dummy (gnus-summary-article-intangible-p))
5664            (article (or article (gnus-summary-article-number)))
5665            (arts (gnus-data-find-list article))
5666            result)
5667       (when (and (not dummy)
5668                  (or (not gnus-summary-check-current)
5669                      (not unread)
5670                      (not (gnus-data-unread-p (car arts)))))
5671         (setq arts (cdr arts)))
5672       (when (setq result
5673                   (if unread
5674                       (progn
5675                         (while arts
5676                           (when (or (and undownloaded
5677                                          (eq gnus-undownloaded-mark
5678                                              (gnus-data-mark (car arts))))
5679                                     (gnus-data-unread-p (car arts)))
5680                             (setq result (car arts)
5681                                   arts nil))
5682                           (setq arts (cdr arts)))
5683                         result)
5684                     (car arts)))
5685         (goto-char (gnus-data-pos result))
5686         (gnus-data-number result)))))
5687
5688 (defun gnus-summary-find-prev (&optional unread article)
5689   (let* ((eobp (eobp))
5690          (article (or article (gnus-summary-article-number)))
5691          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5692          result)
5693     (when (and (not eobp)
5694                (or (not gnus-summary-check-current)
5695                    (not unread)
5696                    (not (gnus-data-unread-p (car arts)))))
5697       (setq arts (cdr arts)))
5698     (when (setq result
5699                 (if unread
5700                     (progn
5701                       (while arts
5702                         (when (gnus-data-unread-p (car arts))
5703                           (setq result (car arts)
5704                                 arts nil))
5705                         (setq arts (cdr arts)))
5706                       result)
5707                   (car arts)))
5708       (goto-char (gnus-data-pos result))
5709       (gnus-data-number result))))
5710
5711 (defun gnus-summary-find-subject (subject &optional unread backward article)
5712   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5713          (article (or article (gnus-summary-article-number)))
5714          (articles (gnus-data-list backward))
5715          (arts (gnus-data-find-list article articles))
5716          result)
5717     (when (or (not gnus-summary-check-current)
5718               (not unread)
5719               (not (gnus-data-unread-p (car arts))))
5720       (setq arts (cdr arts)))
5721     (while arts
5722       (and (or (not unread)
5723                (gnus-data-unread-p (car arts)))
5724            (vectorp (gnus-data-header (car arts)))
5725            (gnus-subject-equal
5726             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5727            (setq result (car arts)
5728                  arts nil))
5729       (setq arts (cdr arts)))
5730     (and result
5731          (goto-char (gnus-data-pos result))
5732          (gnus-data-number result))))
5733
5734 (defun gnus-summary-search-forward (&optional unread subject backward)
5735   "Search forward for an article.
5736 If UNREAD, look for unread articles.  If SUBJECT, look for
5737 articles with that subject.  If BACKWARD, search backward instead."
5738   (cond (subject (gnus-summary-find-subject subject unread backward))
5739         (backward (gnus-summary-find-prev unread))
5740         (t (gnus-summary-find-next unread))))
5741
5742 (defun gnus-recenter (&optional n)
5743   "Center point in window and redisplay frame.
5744 Also do horizontal recentering."
5745   (interactive "P")
5746   (when (and gnus-auto-center-summary
5747              (not (eq gnus-auto-center-summary 'vertical)))
5748     (gnus-horizontal-recenter))
5749   (recenter n))
5750
5751 (defun gnus-summary-recenter ()
5752   "Center point in the summary window.
5753 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5754 displayed, no centering will be performed."
5755   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5756 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5757   (interactive)
5758   (let* ((top (cond ((< (window-height) 4) 0)
5759                     ((< (window-height) 7) 1)
5760                     (t (if (numberp gnus-auto-center-summary)
5761                            gnus-auto-center-summary
5762                          2))))
5763          (height (1- (window-height)))
5764          (bottom (save-excursion (goto-char (point-max))
5765                                  (forward-line (- height))
5766                                  (point)))
5767          (window (get-buffer-window (current-buffer))))
5768     ;; The user has to want it.
5769     (when gnus-auto-center-summary
5770       (when (get-buffer-window gnus-article-buffer)
5771         ;; Only do recentering when the article buffer is displayed,
5772       ;; Set the window start to either `bottom', which is the biggest
5773         ;; possible valid number, or the second line from the top,
5774         ;; whichever is the least.
5775         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5776           (if (> bottom top-pos)
5777               ;; Keep the second line from the top visible
5778               (set-window-start window top-pos t)
5779             ;; Try to keep the bottom line visible; if it's partially
5780             ;; obscured, either scroll one more line to make it fully
5781             ;; visible, or revert to using TOP-POS.
5782             (save-excursion
5783               (goto-char (point-max))
5784               (forward-line -1)
5785               (let ((last-line-start (point)))
5786                 (goto-char bottom)
5787                 (set-window-start window (point) t)
5788                 (when (not (pos-visible-in-window-p last-line-start window))
5789                   (forward-line 1)
5790                   (set-window-start window (min (point) top-pos) t)))))))
5791       ;; Do horizontal recentering while we're at it.
5792       (when (and (get-buffer-window (current-buffer) t)
5793                  (not (eq gnus-auto-center-summary 'vertical)))
5794         (let ((selected (selected-window)))
5795           (select-window (get-buffer-window (current-buffer) t))
5796           (gnus-summary-position-point)
5797           (gnus-horizontal-recenter)
5798           (select-window selected))))))
5799
5800 (defun gnus-summary-jump-to-group (newsgroup)
5801   "Move point to NEWSGROUP in group mode buffer."
5802   ;; Keep update point of group mode buffer if visible.
5803   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5804       (save-window-excursion
5805         ;; Take care of tree window mode.
5806         (when (get-buffer-window gnus-group-buffer)
5807           (pop-to-buffer gnus-group-buffer))
5808         (gnus-group-jump-to-group newsgroup))
5809     (save-excursion
5810       ;; Take care of tree window mode.
5811       (if (get-buffer-window gnus-group-buffer)
5812           (pop-to-buffer gnus-group-buffer)
5813         (set-buffer gnus-group-buffer))
5814       (gnus-group-jump-to-group newsgroup))))
5815
5816 ;; This function returns a list of article numbers based on the
5817 ;; difference between the ranges of read articles in this group and
5818 ;; the range of active articles.
5819 (defun gnus-list-of-unread-articles (group)
5820   (let* ((read (gnus-info-read (gnus-get-info group)))
5821          (active (or (gnus-active group) (gnus-activate-group group)))
5822          (last (cdr active))
5823          first nlast unread)
5824     ;; If none are read, then all are unread.
5825     (if (not read)
5826         (setq first (car active))
5827       ;; If the range of read articles is a single range, then the
5828       ;; first unread article is the article after the last read
5829       ;; article.  Sounds logical, doesn't it?
5830       (if (and (not (listp (cdr read)))
5831                (or (< (car read) (car active))
5832                    (progn (setq read (list read))
5833                           nil)))
5834           (setq first (max (car active) (1+ (cdr read))))
5835         ;; `read' is a list of ranges.
5836         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5837                                   (caar read)))
5838                   1)
5839           (setq first (car active)))
5840         (while read
5841           (when first
5842             (while (< first nlast)
5843               (push first unread)
5844               (setq first (1+ first))))
5845           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5846           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5847           (setq read (cdr read)))))
5848     ;; And add the last unread articles.
5849     (while (<= first last)
5850       (push first unread)
5851       (setq first (1+ first)))
5852     ;; Return the list of unread articles.
5853     (delq 0 (nreverse unread))))
5854
5855 (defun gnus-list-of-read-articles (group)
5856   "Return a list of unread, unticked and non-dormant articles."
5857   (let* ((info (gnus-get-info group))
5858          (marked (gnus-info-marks info))
5859          (active (gnus-active group)))
5860     (and info active
5861          (gnus-set-difference
5862           (gnus-sorted-complement
5863            (gnus-uncompress-range active)
5864            (gnus-list-of-unread-articles group))
5865           (append
5866            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5867            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5868
5869 ;; Various summary commands
5870
5871 (defun gnus-summary-select-article-buffer ()
5872   "Reconfigure windows to show article buffer."
5873   (interactive)
5874   (if (not (gnus-buffer-live-p gnus-article-buffer))
5875       (error "There is no article buffer for this summary buffer")
5876     (gnus-configure-windows 'article)
5877     (select-window (get-buffer-window gnus-article-buffer))))
5878
5879 (defun gnus-summary-universal-argument (arg)
5880   "Perform any operation on all articles that are process/prefixed."
5881   (interactive "P")
5882   (let ((articles (gnus-summary-work-articles arg))
5883         func article)
5884     (if (eq
5885          (setq
5886           func
5887           (key-binding
5888            (read-key-sequence
5889             (substitute-command-keys
5890              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5891          'undefined)
5892         (gnus-error 1 "Undefined key")
5893       (save-excursion
5894         (while articles
5895           (gnus-summary-goto-subject (setq article (pop articles)))
5896           (let (gnus-newsgroup-processable)
5897             (command-execute func))
5898           (gnus-summary-remove-process-mark article)))))
5899   (gnus-summary-position-point))
5900
5901 (defun gnus-summary-toggle-truncation (&optional arg)
5902   "Toggle truncation of summary lines.
5903 With arg, turn line truncation on iff arg is positive."
5904   (interactive "P")
5905   (setq truncate-lines
5906         (if (null arg) (not truncate-lines)
5907           (> (prefix-numeric-value arg) 0)))
5908   (redraw-display))
5909
5910 (defun gnus-summary-reselect-current-group (&optional all rescan)
5911   "Exit and then reselect the current newsgroup.
5912 The prefix argument ALL means to select all articles."
5913   (interactive "P")
5914   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5915     (error "Ephemeral groups can't be reselected"))
5916   (let ((current-subject (gnus-summary-article-number))
5917         (group gnus-newsgroup-name))
5918     (setq gnus-newsgroup-begin nil)
5919     (gnus-summary-exit)
5920     ;; We have to adjust the point of group mode buffer because
5921     ;; point was moved to the next unread newsgroup by exiting.
5922     (gnus-summary-jump-to-group group)
5923     (when rescan
5924       (save-excursion
5925         (gnus-group-get-new-news-this-group 1)))
5926     (gnus-group-read-group all t)
5927     (gnus-summary-goto-subject current-subject nil t)))
5928
5929 (defun gnus-summary-rescan-group (&optional all)
5930   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5931   (interactive "P")
5932   (gnus-summary-reselect-current-group all t))
5933
5934 (defun gnus-summary-update-info (&optional non-destructive)
5935   (save-excursion
5936     (let ((group gnus-newsgroup-name))
5937       (when group
5938         (when gnus-newsgroup-kill-headers
5939           (setq gnus-newsgroup-killed
5940                 (gnus-compress-sequence
5941                  (nconc
5942                   (gnus-set-sorted-intersection
5943                    (gnus-uncompress-range gnus-newsgroup-killed)
5944                    (setq gnus-newsgroup-unselected
5945                          (sort gnus-newsgroup-unselected '<)))
5946                   (setq gnus-newsgroup-unreads
5947                         (sort gnus-newsgroup-unreads '<)))
5948                  t)))
5949         (unless (listp (cdr gnus-newsgroup-killed))
5950           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5951         (let ((headers gnus-newsgroup-headers))
5952           ;; Set the new ranges of read articles.
5953           (save-excursion
5954             (set-buffer gnus-group-buffer)
5955             (gnus-undo-force-boundary))
5956           (gnus-update-read-articles
5957            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5958           ;; Set the current article marks.
5959           (let ((gnus-newsgroup-scored
5960                  (if (and (not gnus-save-score)
5961                           (not non-destructive))
5962                      nil
5963                    gnus-newsgroup-scored)))
5964             (save-excursion
5965               (gnus-update-marks)))
5966           ;; Do the cross-ref thing.
5967           (when gnus-use-cross-reference
5968             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5969           ;; Do not switch windows but change the buffer to work.
5970           (set-buffer gnus-group-buffer)
5971           (unless (gnus-ephemeral-group-p group)
5972             (gnus-group-update-group group)))))))
5973
5974 (defun gnus-summary-save-newsrc (&optional force)
5975   "Save the current number of read/marked articles in the dribble buffer.
5976 The dribble buffer will then be saved.
5977 If FORCE (the prefix), also save the .newsrc file(s)."
5978   (interactive "P")
5979   (gnus-summary-update-info t)
5980   (if force
5981       (gnus-save-newsrc-file)
5982     (gnus-dribble-save)))
5983
5984 (defun gnus-summary-exit (&optional temporary)
5985   "Exit reading current newsgroup, and then return to group selection mode.
5986 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5987   (interactive)
5988   (gnus-set-global-variables)
5989   (when (gnus-buffer-live-p gnus-article-buffer)
5990     (save-excursion
5991       (set-buffer gnus-article-buffer)
5992       (mm-destroy-parts gnus-article-mime-handles)
5993       ;; Set it to nil for safety reason.
5994       (setq gnus-article-mime-handle-alist nil)
5995       (setq gnus-article-mime-handles nil)))
5996   (gnus-kill-save-kill-buffer)
5997   (gnus-async-halt-prefetch)
5998   (let* ((group gnus-newsgroup-name)
5999          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6000          (mode major-mode)
6001          (group-point nil)
6002          (buf (current-buffer)))
6003     (unless quit-config
6004       ;; Do adaptive scoring, and possibly save score files.
6005       (when gnus-newsgroup-adaptive
6006         (gnus-score-adaptive))
6007       (when gnus-use-scoring
6008         (gnus-score-save)))
6009     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6010     ;; If we have several article buffers, we kill them at exit.
6011     (unless gnus-single-article-buffer
6012       (gnus-kill-buffer gnus-original-article-buffer)
6013       (setq gnus-article-current nil))
6014     (when gnus-use-cache
6015       (gnus-cache-possibly-remove-articles)
6016       (gnus-cache-save-buffers))
6017     (gnus-async-prefetch-remove-group group)
6018     (when gnus-suppress-duplicates
6019       (gnus-dup-enter-articles))
6020     (when gnus-use-trees
6021       (gnus-tree-close group))
6022     (when gnus-use-cache
6023       (gnus-cache-write-active))
6024     ;; Remove entries for this group.
6025     (nnmail-purge-split-history (gnus-group-real-name group))
6026     ;; Make all changes in this group permanent.
6027     (unless quit-config
6028       (gnus-run-hooks 'gnus-exit-group-hook)
6029       (gnus-summary-update-info))
6030     (gnus-close-group group)
6031     ;; Make sure where we were, and go to next newsgroup.
6032     (set-buffer gnus-group-buffer)
6033     (unless quit-config
6034       (gnus-group-jump-to-group group))
6035     (gnus-run-hooks 'gnus-summary-exit-hook)
6036     (unless (or quit-config
6037                 ;; If this group has disappeared from the summary
6038                 ;; buffer, don't skip forwards.
6039                 (not (string= group (gnus-group-group-name))))
6040       (gnus-group-next-unread-group 1))
6041     (setq group-point (point))
6042     (if temporary
6043         nil                             ;Nothing to do.
6044       ;; If we have several article buffers, we kill them at exit.
6045       (unless gnus-single-article-buffer
6046         (gnus-kill-buffer gnus-article-buffer)
6047         (gnus-kill-buffer gnus-original-article-buffer)
6048         (setq gnus-article-current nil))
6049       (set-buffer buf)
6050       (if (not gnus-kill-summary-on-exit)
6051           (progn
6052             (gnus-deaden-summary)
6053             (setq mode nil))
6054        ;; We set all buffer-local variables to nil.  It is unclear why
6055         ;; this is needed, but if we don't, buffer-local variables are
6056         ;; not garbage-collected, it seems.  This would the lead to en
6057         ;; ever-growing Emacs.
6058         (gnus-summary-clear-local-variables)
6059         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6060           (gnus-summary-clear-local-variables))
6061         (when (get-buffer gnus-article-buffer)
6062           (bury-buffer gnus-article-buffer))
6063         ;; We clear the global counterparts of the buffer-local
6064         ;; variables as well, just to be on the safe side.
6065         (set-buffer gnus-group-buffer)
6066         (gnus-summary-clear-local-variables)
6067         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6068           (gnus-summary-clear-local-variables)))
6069       (setq gnus-current-select-method gnus-select-method)
6070       (pop-to-buffer gnus-group-buffer)
6071       (if (not quit-config)
6072           (progn
6073             (goto-char group-point)
6074             (gnus-configure-windows 'group 'force))
6075         (gnus-handle-ephemeral-exit quit-config))
6076       ;; Return to group mode buffer.
6077       (when (eq mode 'gnus-summary-mode)
6078         (gnus-kill-buffer buf))
6079       ;; Clear the current group name.
6080       (unless quit-config
6081         (setq gnus-newsgroup-name nil)))))
6082
6083 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6084 (defun gnus-summary-exit-no-update (&optional no-questions)
6085   "Quit reading current newsgroup without updating read article info."
6086   (interactive)
6087   (let* ((group gnus-newsgroup-name)
6088          (quit-config (gnus-group-quit-config group)))
6089     (when (or no-questions
6090               gnus-expert-user
6091               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6092       (gnus-async-halt-prefetch)
6093       (mapcar 'funcall
6094               (delq 'gnus-summary-expire-articles
6095                     (copy-sequence gnus-summary-prepare-exit-hook)))
6096       (when (gnus-buffer-live-p gnus-article-buffer)
6097         (save-excursion
6098           (set-buffer gnus-article-buffer)
6099           (mm-destroy-parts gnus-article-mime-handles)
6100           ;; Set it to nil for safety reason.
6101           (setq gnus-article-mime-handle-alist nil)
6102           (setq gnus-article-mime-handles nil)))
6103       ;; If we have several article buffers, we kill them at exit.
6104       (unless gnus-single-article-buffer
6105         (gnus-kill-buffer gnus-article-buffer)
6106         (gnus-kill-buffer gnus-original-article-buffer)
6107         (setq gnus-article-current nil))
6108       (if (not gnus-kill-summary-on-exit)
6109           (gnus-deaden-summary)
6110         (gnus-close-group group)
6111         (gnus-summary-clear-local-variables)
6112         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6113           (gnus-summary-clear-local-variables))
6114         (set-buffer gnus-group-buffer)
6115         (gnus-summary-clear-local-variables)
6116         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6117           (gnus-summary-clear-local-variables))
6118         (when (get-buffer gnus-summary-buffer)
6119           (kill-buffer gnus-summary-buffer)))
6120       (unless gnus-single-article-buffer
6121         (setq gnus-article-current nil))
6122       (when gnus-use-trees
6123         (gnus-tree-close group))
6124       (gnus-async-prefetch-remove-group group)
6125       (when (get-buffer gnus-article-buffer)
6126         (bury-buffer gnus-article-buffer))
6127       ;; Return to the group buffer.
6128       (gnus-configure-windows 'group 'force)
6129       ;; Clear the current group name.
6130       (setq gnus-newsgroup-name nil)
6131       (when (equal (gnus-group-group-name) group)
6132         (gnus-group-next-unread-group 1))
6133       (when quit-config
6134         (gnus-handle-ephemeral-exit quit-config)))))
6135
6136 (defun gnus-handle-ephemeral-exit (quit-config)
6137   "Handle movement when leaving an ephemeral group.
6138 The state which existed when entering the ephemeral is reset."
6139   (if (not (buffer-name (car quit-config)))
6140       (gnus-configure-windows 'group 'force)
6141     (set-buffer (car quit-config))
6142     (cond ((eq major-mode 'gnus-summary-mode)
6143            (gnus-set-global-variables))
6144           ((eq major-mode 'gnus-article-mode)
6145            (save-excursion
6146              ;; The `gnus-summary-buffer' variable may point
6147              ;; to the old summary buffer when using a single
6148              ;; article buffer.
6149              (unless (gnus-buffer-live-p gnus-summary-buffer)
6150                (set-buffer gnus-group-buffer))
6151              (set-buffer gnus-summary-buffer)
6152              (gnus-set-global-variables))))
6153     (if (or (eq (cdr quit-config) 'article)
6154             (eq (cdr quit-config) 'pick))
6155         (progn
6156           ;; The current article may be from the ephemeral group
6157           ;; thus it is best that we reload this article
6158           (gnus-summary-show-article)
6159           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6160               (gnus-configure-windows 'pick 'force)
6161             (gnus-configure-windows (cdr quit-config) 'force)))
6162       (gnus-configure-windows (cdr quit-config) 'force))
6163     (when (eq major-mode 'gnus-summary-mode)
6164       (gnus-summary-next-subject 1 nil t)
6165       (gnus-summary-recenter)
6166       (gnus-summary-position-point))))
6167
6168 ;;; Dead summaries.
6169
6170 (defvar gnus-dead-summary-mode-map nil)
6171
6172 (unless gnus-dead-summary-mode-map
6173   (setq gnus-dead-summary-mode-map (make-keymap))
6174   (suppress-keymap gnus-dead-summary-mode-map)
6175   (substitute-key-definition
6176    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6177   (dolist (key '("\C-d" "\r" "\177" [delete]))
6178     (define-key gnus-dead-summary-mode-map
6179       key 'gnus-summary-wake-up-the-dead))
6180   (dolist (key '("q" "Q"))
6181     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6182
6183 (defvar gnus-dead-summary-mode nil
6184   "Minor mode for Gnus summary buffers.")
6185
6186 (defun gnus-dead-summary-mode (&optional arg)
6187   "Minor mode for Gnus summary buffers."
6188   (interactive "P")
6189   (when (eq major-mode 'gnus-summary-mode)
6190     (make-local-variable 'gnus-dead-summary-mode)
6191     (setq gnus-dead-summary-mode
6192           (if (null arg) (not gnus-dead-summary-mode)
6193             (> (prefix-numeric-value arg) 0)))
6194     (when gnus-dead-summary-mode
6195       (gnus-add-minor-mode
6196        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6197
6198 (defun gnus-deaden-summary ()
6199   "Make the current summary buffer into a dead summary buffer."
6200   ;; Kill any previous dead summary buffer.
6201   (when (and gnus-dead-summary
6202              (buffer-name gnus-dead-summary))
6203     (save-excursion
6204       (set-buffer gnus-dead-summary)
6205       (when gnus-dead-summary-mode
6206         (kill-buffer (current-buffer)))))
6207   ;; Make this the current dead summary.
6208   (setq gnus-dead-summary (current-buffer))
6209   (gnus-dead-summary-mode 1)
6210   (let ((name (buffer-name)))
6211     (when (string-match "Summary" name)
6212       (rename-buffer
6213        (concat (substring name 0 (match-beginning 0)) "Dead "
6214                (substring name (match-beginning 0)))
6215        t)
6216       (bury-buffer))))
6217
6218 (defun gnus-kill-or-deaden-summary (buffer)
6219   "Kill or deaden the summary BUFFER."
6220   (save-excursion
6221     (when (and (buffer-name buffer)
6222                (not gnus-single-article-buffer))
6223       (save-excursion
6224         (set-buffer buffer)
6225         (gnus-kill-buffer gnus-article-buffer)
6226         (gnus-kill-buffer gnus-original-article-buffer)))
6227     (cond (gnus-kill-summary-on-exit
6228            (when (and gnus-use-trees
6229                       (gnus-buffer-exists-p buffer))
6230              (save-excursion
6231                (set-buffer buffer)
6232                (gnus-tree-close gnus-newsgroup-name)))
6233            (gnus-kill-buffer buffer))
6234           ((gnus-buffer-exists-p buffer)
6235            (save-excursion
6236              (set-buffer buffer)
6237              (gnus-deaden-summary))))))
6238
6239 (defun gnus-summary-wake-up-the-dead (&rest args)
6240   "Wake up the dead summary buffer."
6241   (interactive)
6242   (gnus-dead-summary-mode -1)
6243   (let ((name (buffer-name)))
6244     (when (string-match "Dead " name)
6245       (rename-buffer
6246        (concat (substring name 0 (match-beginning 0))
6247                (substring name (match-end 0)))
6248        t)))
6249   (gnus-message 3 "This dead summary is now alive again"))
6250
6251 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6252 (defun gnus-summary-fetch-faq (&optional faq-dir)
6253   "Fetch the FAQ for the current group.
6254 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6255 in."
6256   (interactive
6257    (list
6258     (when current-prefix-arg
6259       (completing-read
6260        "Faq dir: " (and (listp gnus-group-faq-directory)
6261                         (mapcar (lambda (file) (list file))
6262                                 gnus-group-faq-directory))))))
6263   (let (gnus-faq-buffer)
6264     (when (setq gnus-faq-buffer
6265                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6266       (gnus-configure-windows 'summary-faq))))
6267
6268 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6269 (defun gnus-summary-describe-group (&optional force)
6270   "Describe the current newsgroup."
6271   (interactive "P")
6272   (gnus-group-describe-group force gnus-newsgroup-name))
6273
6274 (defun gnus-summary-describe-briefly ()
6275   "Describe summary mode commands briefly."
6276   (interactive)
6277   (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")))
6278
6279 ;; Walking around group mode buffer from summary mode.
6280
6281 (defun gnus-summary-next-group (&optional no-article target-group backward)
6282   "Exit current newsgroup and then select next unread newsgroup.
6283 If prefix argument NO-ARTICLE is non-nil, no article is selected
6284 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6285 previous group instead."
6286   (interactive "P")
6287   ;; Stop pre-fetching.
6288   (gnus-async-halt-prefetch)
6289   (let ((current-group gnus-newsgroup-name)
6290         (current-buffer (current-buffer))
6291         entered)
6292    ;; First we semi-exit this group to update Xrefs and all variables.
6293     ;; We can't do a real exit, because the window conf must remain
6294     ;; the same in case the user is prompted for info, and we don't
6295     ;; want the window conf to change before that...
6296     (gnus-summary-exit t)
6297     (while (not entered)
6298       ;; Then we find what group we are supposed to enter.
6299       (set-buffer gnus-group-buffer)
6300       (gnus-group-jump-to-group current-group)
6301       (setq target-group
6302             (or target-group
6303                 (if (eq gnus-keep-same-level 'best)
6304                     (gnus-summary-best-group gnus-newsgroup-name)
6305                   (gnus-summary-search-group backward gnus-keep-same-level))))
6306       (if (not target-group)
6307           ;; There are no further groups, so we return to the group
6308           ;; buffer.
6309           (progn
6310             (gnus-message 5 "Returning to the group buffer")
6311             (setq entered t)
6312             (when (gnus-buffer-live-p current-buffer)
6313               (set-buffer current-buffer)
6314               (gnus-summary-exit))
6315             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6316         ;; We try to enter the target group.
6317         (gnus-group-jump-to-group target-group)
6318         (let ((unreads (gnus-group-group-unread)))
6319           (if (and (or (eq t unreads)
6320                        (and unreads (not (zerop unreads))))
6321                    (gnus-summary-read-group
6322                     target-group nil no-article
6323                     (and (buffer-name current-buffer) current-buffer)
6324                     nil backward))
6325               (setq entered t)
6326             (setq current-group target-group
6327                   target-group nil)))))))
6328
6329 (defun gnus-summary-prev-group (&optional no-article)
6330   "Exit current newsgroup and then select previous unread newsgroup.
6331 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6332   (interactive "P")
6333   (gnus-summary-next-group no-article nil t))
6334
6335 ;; Walking around summary lines.
6336
6337 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6338   "Go to the first unread subject.
6339 If UNREAD is non-nil, go to the first unread article.
6340 Returns the article selected or nil if there are no unread articles."
6341   (interactive "P")
6342   (prog1
6343       (cond
6344        ;; Empty summary.
6345        ((null gnus-newsgroup-data)
6346         (gnus-message 3 "No articles in the group")
6347         nil)
6348        ;; Pick the first article.
6349        ((not unread)
6350         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6351         (gnus-data-number (car gnus-newsgroup-data)))
6352        ;; No unread articles.
6353        ((null gnus-newsgroup-unreads)
6354         (gnus-message 3 "No more unread articles")
6355         nil)
6356        ;; Find the first unread article.
6357        (t
6358         (let ((data gnus-newsgroup-data))
6359           (while (and data
6360                       (and (not (and undownloaded
6361                                      (eq gnus-undownloaded-mark
6362                                          (gnus-data-mark (car data)))))
6363                            (if unseen
6364                                (or (not (memq
6365                                          (gnus-data-number (car data))
6366                                          gnus-newsgroup-unseen))
6367                                    (not (gnus-data-unread-p (car data))))
6368                              (not (gnus-data-unread-p (car data))))))
6369             (setq data (cdr data)))
6370           (when data
6371             (goto-char (gnus-data-pos (car data)))
6372             (gnus-data-number (car data))))))
6373     (gnus-summary-position-point)))
6374
6375 (defun gnus-summary-next-subject (n &optional unread dont-display)
6376   "Go to next N'th summary line.
6377 If N is negative, go to the previous N'th subject line.
6378 If UNREAD is non-nil, only unread articles are selected.
6379 The difference between N and the actual number of steps taken is
6380 returned."
6381   (interactive "p")
6382   (let ((backward (< n 0))
6383         (n (abs n)))
6384     (while (and (> n 0)
6385                 (if backward
6386                     (gnus-summary-find-prev unread)
6387                   (gnus-summary-find-next unread)))
6388       (unless (zerop (setq n (1- n)))
6389         (gnus-summary-show-thread)))
6390     (when (/= 0 n)
6391       (gnus-message 7 "No more%s articles"
6392                     (if unread " unread" "")))
6393     (unless dont-display
6394       (gnus-summary-recenter)
6395       (gnus-summary-position-point))
6396     n))
6397
6398 (defun gnus-summary-next-unread-subject (n)
6399   "Go to next N'th unread summary line."
6400   (interactive "p")
6401   (gnus-summary-next-subject n t))
6402
6403 (defun gnus-summary-prev-subject (n &optional unread)
6404   "Go to previous N'th summary line.
6405 If optional argument UNREAD is non-nil, only unread article is selected."
6406   (interactive "p")
6407   (gnus-summary-next-subject (- n) unread))
6408
6409 (defun gnus-summary-prev-unread-subject (n)
6410   "Go to previous N'th unread summary line."
6411   (interactive "p")
6412   (gnus-summary-next-subject (- n) t))
6413
6414 (defun gnus-summary-goto-subject (article &optional force silent)
6415   "Go the subject line of ARTICLE.
6416 If FORCE, also allow jumping to articles not currently shown."
6417   (interactive "nArticle number: ")
6418   (let ((b (point))
6419         (data (gnus-data-find article)))
6420     ;; We read in the article if we have to.
6421     (and (not data)
6422          force
6423          (gnus-summary-insert-subject
6424           article
6425           (if (or (numberp force) (vectorp force)) force)
6426           t)
6427          (setq data (gnus-data-find article)))
6428     (goto-char b)
6429     (if (not data)
6430         (progn
6431           (unless silent
6432             (gnus-message 3 "Can't find article %d" article))
6433           nil)
6434       (let ((pt (gnus-data-pos data)))
6435         (goto-char pt)
6436         (gnus-summary-set-article-display-arrow pt))
6437       (gnus-summary-position-point)
6438       article)))
6439
6440 ;; Walking around summary lines with displaying articles.
6441
6442 (defun gnus-summary-expand-window (&optional arg)
6443   "Make the summary buffer take up the entire Emacs frame.
6444 Given a prefix, will force an `article' buffer configuration."
6445   (interactive "P")
6446   (if arg
6447       (gnus-configure-windows 'article 'force)
6448     (gnus-configure-windows 'summary 'force)))
6449
6450 (defun gnus-summary-display-article (article &optional all-header)
6451   "Display ARTICLE in article buffer."
6452   (when (gnus-buffer-live-p gnus-article-buffer)
6453     (with-current-buffer gnus-article-buffer
6454       (mm-enable-multibyte)))
6455   (gnus-set-global-variables)
6456   (when (gnus-buffer-live-p gnus-article-buffer)
6457     (with-current-buffer gnus-article-buffer
6458       (setq gnus-article-charset gnus-newsgroup-charset)
6459       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6460       (mm-enable-multibyte)))
6461   (if (null article)
6462       nil
6463     (prog1
6464         (if gnus-summary-display-article-function
6465             (funcall gnus-summary-display-article-function article all-header)
6466           (gnus-article-prepare article all-header))
6467       (gnus-run-hooks 'gnus-select-article-hook)
6468       (when (and gnus-current-article
6469                  (not (zerop gnus-current-article)))
6470         (gnus-summary-goto-subject gnus-current-article))
6471       (gnus-summary-recenter)
6472       (when (and gnus-use-trees gnus-show-threads)
6473         (gnus-possibly-generate-tree article)
6474         (gnus-highlight-selected-tree article))
6475       ;; Successfully display article.
6476       (gnus-article-set-window-start
6477        (cdr (assq article gnus-newsgroup-bookmarks))))))
6478
6479 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6480   "Select the current article.
6481 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6482 non-nil, the article will be re-fetched even if it already present in
6483 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6484 be displayed."
6485   ;; Make sure we are in the summary buffer to work around bbdb bug.
6486   (unless (eq major-mode 'gnus-summary-mode)
6487     (set-buffer gnus-summary-buffer))
6488   (let ((article (or article (gnus-summary-article-number)))
6489         (all-headers (not (not all-headers))) ;Must be t or nil.
6490         gnus-summary-display-article-function)
6491     (and (not pseudo)
6492          (gnus-summary-article-pseudo-p article)
6493          (error "This is a pseudo-article"))
6494     (save-excursion
6495       (set-buffer gnus-summary-buffer)
6496       (if (or (and gnus-single-article-buffer
6497                    (or (null gnus-current-article)
6498                        (null gnus-article-current)
6499                        (null (get-buffer gnus-article-buffer))
6500                        (not (eq article (cdr gnus-article-current)))
6501                        (not (equal (car gnus-article-current)
6502                                    gnus-newsgroup-name))))
6503               (and (not gnus-single-article-buffer)
6504                    (or (null gnus-current-article)
6505                        (not (eq gnus-current-article article))))
6506               force)
6507        ;; The requested article is different from the current article.
6508           (progn
6509             (gnus-summary-display-article article all-headers)
6510             (when (gnus-buffer-live-p gnus-article-buffer)
6511               (with-current-buffer gnus-article-buffer
6512                 (if (not gnus-article-decoded-p) ;; a local variable
6513                     (mm-disable-multibyte))))
6514             (when (or all-headers gnus-show-all-headers)
6515               (gnus-article-show-all-headers))
6516             (gnus-article-set-window-start
6517              (cdr (assq article gnus-newsgroup-bookmarks)))
6518             article)
6519         (when (or all-headers gnus-show-all-headers)
6520           (gnus-article-show-all-headers))
6521         'old))))
6522
6523 (defun gnus-summary-force-verify-and-decrypt ()
6524   (interactive)
6525   (let ((mm-verify-option 'known)
6526         (mm-decrypt-option 'known))
6527     (gnus-summary-select-article nil 'force)))
6528
6529 (defun gnus-summary-set-current-mark (&optional current-mark)
6530   "Obsolete function."
6531   nil)
6532
6533 (defun gnus-summary-next-article (&optional unread subject backward push)
6534   "Select the next article.
6535 If UNREAD, only unread articles are selected.
6536 If SUBJECT, only articles with SUBJECT are selected.
6537 If BACKWARD, the previous article is selected instead of the next."
6538   (interactive "P")
6539   (cond
6540    ;; Is there such an article?
6541    ((and (gnus-summary-search-forward unread subject backward)
6542          (or (gnus-summary-display-article (gnus-summary-article-number))
6543              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6544     (gnus-summary-position-point))
6545    ;; If not, we try the first unread, if that is wanted.
6546    ((and subject
6547          gnus-auto-select-same
6548          (gnus-summary-first-unread-article))
6549     (gnus-summary-position-point)
6550     (gnus-message 6 "Wrapped"))
6551    ;; Try to get next/previous article not displayed in this group.
6552    ((and gnus-auto-extend-newsgroup
6553          (not unread) (not subject))
6554     (gnus-summary-goto-article
6555      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6556      nil (count-lines (point-min) (point))))
6557    ;; Go to next/previous group.
6558    (t
6559     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6560       (gnus-summary-jump-to-group gnus-newsgroup-name))
6561     (let ((cmd last-command-char)
6562           (point
6563            (save-excursion
6564              (set-buffer gnus-group-buffer)
6565              (point)))
6566           (group
6567            (if (eq gnus-keep-same-level 'best)
6568                (gnus-summary-best-group gnus-newsgroup-name)
6569              (gnus-summary-search-group backward gnus-keep-same-level))))
6570       ;; For some reason, the group window gets selected.  We change
6571       ;; it back.
6572       (select-window (get-buffer-window (current-buffer)))
6573       ;; Select next unread newsgroup automagically.
6574       (cond
6575        ((or (not gnus-auto-select-next)
6576             (not cmd))
6577         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6578        ((or (eq gnus-auto-select-next 'quietly)
6579             (and (eq gnus-auto-select-next 'slightly-quietly)
6580                  push)
6581             (and (eq gnus-auto-select-next 'almost-quietly)
6582                  (gnus-summary-last-article-p)))
6583         ;; Select quietly.
6584         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6585             (gnus-summary-exit)
6586           (gnus-message 7 "No more%s articles (%s)..."
6587                         (if unread " unread" "")
6588                         (if group (concat "selecting " group)
6589                           "exiting"))
6590           (gnus-summary-next-group nil group backward)))
6591        (t
6592         (when (gnus-key-press-event-p last-input-event)
6593           (gnus-summary-walk-group-buffer
6594            gnus-newsgroup-name cmd unread backward point))))))))
6595
6596 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6597   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6598                       (?\C-p (gnus-group-prev-unread-group 1))))
6599         (cursor-in-echo-area t)
6600         keve key group ended)
6601     (save-excursion
6602       (set-buffer gnus-group-buffer)
6603       (goto-char start)
6604       (setq group
6605             (if (eq gnus-keep-same-level 'best)
6606                 (gnus-summary-best-group gnus-newsgroup-name)
6607               (gnus-summary-search-group backward gnus-keep-same-level))))
6608     (while (not ended)
6609       (gnus-message
6610        5 "No more%s articles%s" (if unread " unread" "")
6611        (if (and group
6612                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6613            (format " (Type %s for %s [%s])"
6614                    (single-key-description cmd) group
6615                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6616          (format " (Type %s to exit %s)"
6617                  (single-key-description cmd)
6618                  gnus-newsgroup-name)))
6619       ;; Confirm auto selection.
6620       (setq key (car (setq keve (gnus-read-event-char))))
6621       (setq ended t)
6622       (cond
6623        ((assq key keystrokes)
6624         (let ((obuf (current-buffer)))
6625           (switch-to-buffer gnus-group-buffer)
6626           (when group
6627             (gnus-group-jump-to-group group))
6628           (eval (cadr (assq key keystrokes)))
6629           (setq group (gnus-group-group-name))
6630           (switch-to-buffer obuf))
6631         (setq ended nil))
6632        ((equal key cmd)
6633         (if (or (not group)
6634                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6635             (gnus-summary-exit)
6636           (gnus-summary-next-group nil group backward)))
6637        (t
6638         (push (cdr keve) unread-command-events))))))
6639
6640 (defun gnus-summary-next-unread-article ()
6641   "Select unread article after current one."
6642   (interactive)
6643   (gnus-summary-next-article
6644    (or (not (eq gnus-summary-goto-unread 'never))
6645        (gnus-summary-last-article-p (gnus-summary-article-number)))
6646    (and gnus-auto-select-same
6647         (gnus-summary-article-subject))))
6648
6649 (defun gnus-summary-prev-article (&optional unread subject)
6650   "Select the article after the current one.
6651 If UNREAD is non-nil, only unread articles are selected."
6652   (interactive "P")
6653   (gnus-summary-next-article unread subject t))
6654
6655 (defun gnus-summary-prev-unread-article ()
6656   "Select unread article before current one."
6657   (interactive)
6658   (gnus-summary-prev-article
6659    (or (not (eq gnus-summary-goto-unread 'never))
6660        (gnus-summary-first-article-p (gnus-summary-article-number)))
6661    (and gnus-auto-select-same
6662         (gnus-summary-article-subject))))
6663
6664 (defun gnus-summary-next-page (&optional lines circular)
6665   "Show next page of the selected article.
6666 If at the end of the current article, select the next article.
6667 LINES says how many lines should be scrolled up.
6668
6669 If CIRCULAR is non-nil, go to the start of the article instead of
6670 selecting the next article when reaching the end of the current
6671 article."
6672   (interactive "P")
6673   (setq gnus-summary-buffer (current-buffer))
6674   (gnus-set-global-variables)
6675   (let ((article (gnus-summary-article-number))
6676         (article-window (get-buffer-window gnus-article-buffer t))
6677         endp)
6678     ;; If the buffer is empty, we have no article.
6679     (unless article
6680       (error "No article to select"))
6681     (gnus-configure-windows 'article)
6682     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6683         (if (and (eq gnus-summary-goto-unread 'never)
6684                  (not (gnus-summary-last-article-p article)))
6685             (gnus-summary-next-article)
6686           (gnus-summary-next-unread-article))
6687       (if (or (null gnus-current-article)
6688               (null gnus-article-current)
6689               (/= article (cdr gnus-article-current))
6690               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6691           ;; Selected subject is different from current article's.
6692           (gnus-summary-display-article article)
6693         (when article-window
6694           (gnus-eval-in-buffer-window gnus-article-buffer
6695             (setq endp (gnus-article-next-page lines)))
6696           (when endp
6697             (cond (circular
6698                    (gnus-summary-beginning-of-article))
6699                   (lines
6700                    (gnus-message 3 "End of message"))
6701                   ((null lines)
6702                    (if (and (eq gnus-summary-goto-unread 'never)
6703                             (not (gnus-summary-last-article-p article)))
6704                        (gnus-summary-next-article)
6705                      (gnus-summary-next-unread-article))))))))
6706     (gnus-summary-recenter)
6707     (gnus-summary-position-point)))
6708
6709 (defun gnus-summary-prev-page (&optional lines move)
6710   "Show previous page of selected article.
6711 Argument LINES specifies lines to be scrolled down.
6712 If MOVE, move to the previous unread article if point is at
6713 the beginning of the buffer."
6714   (interactive "P")
6715   (let ((article (gnus-summary-article-number))
6716         (article-window (get-buffer-window gnus-article-buffer t))
6717         endp)
6718     (gnus-configure-windows 'article)
6719     (if (or (null gnus-current-article)
6720             (null gnus-article-current)
6721             (/= article (cdr gnus-article-current))
6722             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6723         ;; Selected subject is different from current article's.
6724         (gnus-summary-display-article article)
6725       (gnus-summary-recenter)
6726       (when article-window
6727         (gnus-eval-in-buffer-window gnus-article-buffer
6728           (setq endp (gnus-article-prev-page lines)))
6729         (when (and move endp)
6730           (cond (lines
6731                  (gnus-message 3 "Beginning of message"))
6732                 ((null lines)
6733                  (if (and (eq gnus-summary-goto-unread 'never)
6734                           (not (gnus-summary-first-article-p article)))
6735                      (gnus-summary-prev-article)
6736                    (gnus-summary-prev-unread-article))))))))
6737   (gnus-summary-position-point))
6738
6739 (defun gnus-summary-prev-page-or-article (&optional lines)
6740   "Show previous page of selected article.
6741 Argument LINES specifies lines to be scrolled down.
6742 If at the beginning of the article, go to the next article."
6743   (interactive "P")
6744   (gnus-summary-prev-page lines t))
6745
6746 (defun gnus-summary-scroll-up (lines)
6747   "Scroll up (or down) one line current article.
6748 Argument LINES specifies lines to be scrolled up (or down if negative)."
6749   (interactive "p")
6750   (gnus-configure-windows 'article)
6751   (gnus-summary-show-thread)
6752   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6753     (gnus-eval-in-buffer-window gnus-article-buffer
6754       (cond ((> lines 0)
6755              (when (gnus-article-next-page lines)
6756                (gnus-message 3 "End of message")))
6757             ((< lines 0)
6758              (gnus-article-prev-page (- lines))))))
6759   (gnus-summary-recenter)
6760   (gnus-summary-position-point))
6761
6762 (defun gnus-summary-scroll-down (lines)
6763   "Scroll down (or up) one line current article.
6764 Argument LINES specifies lines to be scrolled down (or up if negative)."
6765   (interactive "p")
6766   (gnus-summary-scroll-up (- lines)))
6767
6768 (defun gnus-summary-next-same-subject ()
6769   "Select next article which has the same subject as current one."
6770   (interactive)
6771   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6772
6773 (defun gnus-summary-prev-same-subject ()
6774   "Select previous article which has the same subject as current one."
6775   (interactive)
6776   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6777
6778 (defun gnus-summary-next-unread-same-subject ()
6779   "Select next unread article which has the same subject as current one."
6780   (interactive)
6781   (gnus-summary-next-article t (gnus-summary-article-subject)))
6782
6783 (defun gnus-summary-prev-unread-same-subject ()
6784   "Select previous unread article which has the same subject as current one."
6785   (interactive)
6786   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6787
6788 (defun gnus-summary-first-unread-article ()
6789   "Select the first unread article.
6790 Return nil if there are no unread articles."
6791   (interactive)
6792   (prog1
6793       (when (gnus-summary-first-subject t)
6794         (gnus-summary-show-thread)
6795         (gnus-summary-first-subject t)
6796         (gnus-summary-display-article (gnus-summary-article-number)))
6797     (gnus-summary-position-point)))
6798
6799 (defun gnus-summary-first-unread-subject ()
6800   "Place the point on the subject line of the first unread article.
6801 Return nil if there are no unread articles."
6802   (interactive)
6803   (prog1
6804       (when (gnus-summary-first-subject t)
6805         (gnus-summary-show-thread)
6806         (gnus-summary-first-subject t))
6807     (gnus-summary-position-point)))
6808
6809 (defun gnus-summary-first-unseen-subject ()
6810   "Place the point on the subject line of the first unseen article.
6811 Return nil if there are no unseen articles."
6812   (interactive)
6813   (prog1
6814       (when (gnus-summary-first-subject t t t)
6815         (gnus-summary-show-thread)
6816         (gnus-summary-first-subject t t t))
6817     (gnus-summary-position-point)))
6818
6819 (defun gnus-summary-first-article ()
6820   "Select the first article.
6821 Return nil if there are no articles."
6822   (interactive)
6823   (prog1
6824       (when (gnus-summary-first-subject)
6825         (gnus-summary-show-thread)
6826         (gnus-summary-first-subject)
6827         (gnus-summary-display-article (gnus-summary-article-number)))
6828     (gnus-summary-position-point)))
6829
6830 (defun gnus-summary-best-unread-article (&optional arg)
6831   "Select the unread article with the highest score.
6832 If given a prefix argument, select the next unread article that has a
6833 score higher than the default score."
6834   (interactive "P")
6835   (let ((article (if arg
6836                      (gnus-summary-better-unread-subject)
6837                    (gnus-summary-best-unread-subject))))
6838     (if article
6839         (gnus-summary-goto-article article)
6840       (error "No unread articles"))))
6841
6842 (defun gnus-summary-best-unread-subject ()
6843   "Select the unread subject with the highest score."
6844   (interactive)
6845   (let ((best -1000000)
6846         (data gnus-newsgroup-data)
6847         article score)
6848     (while data
6849       (and (gnus-data-unread-p (car data))
6850            (> (setq score
6851                     (gnus-summary-article-score (gnus-data-number (car data))))
6852               best)
6853            (setq best score
6854                  article (gnus-data-number (car data))))
6855       (setq data (cdr data)))
6856     (when article
6857       (gnus-summary-goto-subject article))
6858     (gnus-summary-position-point)
6859     article))
6860
6861 (defun gnus-summary-better-unread-subject ()
6862   "Select the first unread subject that has a score over the default score."
6863   (interactive)
6864   (let ((data gnus-newsgroup-data)
6865         article score)
6866     (while (and (setq article (gnus-data-number (car data)))
6867                 (or (gnus-data-read-p (car data))
6868                     (not (> (gnus-summary-article-score article)
6869                             gnus-summary-default-score))))
6870       (setq data (cdr data)))
6871     (when article
6872       (gnus-summary-goto-subject article))
6873     (gnus-summary-position-point)
6874     article))
6875
6876 (defun gnus-summary-last-subject ()
6877   "Go to the last displayed subject line in the group."
6878   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6879     (when article
6880       (gnus-summary-goto-subject article))))
6881
6882 (defun gnus-summary-goto-article (article &optional all-headers force)
6883   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6884 If ALL-HEADERS is non-nil, no header lines are hidden.
6885 If FORCE, go to the article even if it isn't displayed.  If FORCE
6886 is a number, it is the line the article is to be displayed on."
6887   (interactive
6888    (list
6889     (completing-read
6890      "Article number or Message-ID: "
6891      (mapcar (lambda (number) (list (int-to-string number)))
6892              gnus-newsgroup-limit))
6893     current-prefix-arg
6894     t))
6895   (prog1
6896       (if (and (stringp article)
6897                (string-match "@" article))
6898           (gnus-summary-refer-article article)
6899         (when (stringp article)
6900           (setq article (string-to-number article)))
6901         (if (gnus-summary-goto-subject article force)
6902             (gnus-summary-display-article article all-headers)
6903           (gnus-message 4 "Couldn't go to article %s" article) nil))
6904     (gnus-summary-position-point)))
6905
6906 (defun gnus-summary-goto-last-article ()
6907   "Go to the previously read article."
6908   (interactive)
6909   (prog1
6910       (when gnus-last-article
6911         (gnus-summary-goto-article gnus-last-article nil t))
6912     (gnus-summary-position-point)))
6913
6914 (defun gnus-summary-pop-article (number)
6915   "Pop one article off the history and go to the previous.
6916 NUMBER articles will be popped off."
6917   (interactive "p")
6918   (let (to)
6919     (setq gnus-newsgroup-history
6920           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6921     (if to
6922         (gnus-summary-goto-article (car to) nil t)
6923       (error "Article history empty")))
6924   (gnus-summary-position-point))
6925
6926 ;; Summary commands and functions for limiting the summary buffer.
6927
6928 (defun gnus-summary-limit-to-articles (n)
6929   "Limit the summary buffer to the next N articles.
6930 If not given a prefix, use the process marked articles instead."
6931   (interactive "P")
6932   (prog1
6933       (let ((articles (gnus-summary-work-articles n)))
6934         (setq gnus-newsgroup-processable nil)
6935         (gnus-summary-limit articles))
6936     (gnus-summary-position-point)))
6937
6938 (defun gnus-summary-pop-limit (&optional total)
6939   "Restore the previous limit.
6940 If given a prefix, remove all limits."
6941   (interactive "P")
6942   (when total
6943     (setq gnus-newsgroup-limits
6944           (list (mapcar (lambda (h) (mail-header-number h))
6945                         gnus-newsgroup-headers))))
6946   (unless gnus-newsgroup-limits
6947     (error "No limit to pop"))
6948   (prog1
6949       (gnus-summary-limit nil 'pop)
6950     (gnus-summary-position-point)))
6951
6952 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
6953   "Limit the summary buffer to articles that have subjects that match a regexp.
6954 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
6955   (interactive
6956    (list (read-string (if current-prefix-arg
6957                           "Exclude subject (regexp): "
6958                         "Limit to subject (regexp): "))
6959          nil current-prefix-arg))
6960   (unless header
6961     (setq header "subject"))
6962   (when (not (equal "" subject))
6963     (prog1
6964         (let ((articles (gnus-summary-find-matching
6965                          (or header "subject") subject 'all nil nil
6966                          not-matching)))
6967           (unless articles
6968             (error "Found no matches for \"%s\"" subject))
6969           (gnus-summary-limit articles))
6970       (gnus-summary-position-point))))
6971
6972 (defun gnus-summary-limit-to-author (from &optional not-matching)
6973   "Limit the summary buffer to articles that have authors that match a regexp.
6974 If NOT-MATCHING, excluding articles that have authors that match a regexp."
6975   (interactive
6976    (list (read-string (if current-prefix-arg
6977                           "Exclude author (regexp): "
6978                         "Limit to author (regexp): "))
6979          current-prefix-arg))
6980   (gnus-summary-limit-to-subject from "from" not-matching))
6981
6982 (defun gnus-summary-limit-to-age (age &optional younger-p)
6983   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6984 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6985 articles that are younger than AGE days."
6986   (interactive
6987    (let ((younger current-prefix-arg)
6988          (days-got nil)
6989          days)
6990      (while (not days-got)
6991        (setq days (if younger
6992                       (read-string "Limit to articles within (in days): ")
6993                     (read-string "Limit to articles older than (in days): ")))
6994        (when (> (length days) 0)
6995          (setq days (read days)))
6996        (if (numberp days)
6997            (progn 
6998              (setq days-got t)
6999              (if (< days 0)
7000                  (progn 
7001                    (setq younger (not younger))
7002                    (setq days (* days -1)))))
7003          (message "Please enter a number.")
7004          (sleep-for 1)))
7005      (list days younger)))
7006   (prog1
7007       (let ((data gnus-newsgroup-data)
7008             (cutoff (days-to-time age))
7009             articles d date is-younger)
7010         (while (setq d (pop data))
7011           (when (and (vectorp (gnus-data-header d))
7012                      (setq date (mail-header-date (gnus-data-header d))))
7013             (setq is-younger (time-less-p
7014                               (time-since (condition-case ()
7015                                               (date-to-time date)
7016                                             (error '(0 0))))
7017                               cutoff))
7018             (when (if younger-p
7019                       is-younger
7020                     (not is-younger))
7021               (push (gnus-data-number d) articles))))
7022         (gnus-summary-limit (nreverse articles)))
7023     (gnus-summary-position-point)))
7024
7025 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7026   "Limit the summary buffer to articles that match an 'extra' header."
7027   (interactive
7028    (let ((header
7029           (intern
7030            (gnus-completing-read
7031             (symbol-name (car gnus-extra-headers))
7032             (if current-prefix-arg
7033                 "Exclude extra header:"
7034               "Limit extra header:")
7035             (mapcar (lambda (x)
7036                       (cons (symbol-name x) x))
7037                     gnus-extra-headers)
7038             nil
7039             t))))
7040      (list header
7041            (read-string (format "%s header %s (regexp): "
7042                                 (if current-prefix-arg "Exclude" "Limit to")
7043                                 header))
7044            current-prefix-arg)))
7045   (when (not (equal "" regexp))
7046     (prog1
7047         (let ((articles (gnus-summary-find-matching
7048                          (cons 'extra header) regexp 'all nil nil
7049                          not-matching)))
7050           (unless articles
7051             (error "Found no matches for \"%s\"" regexp))
7052           (gnus-summary-limit articles))
7053       (gnus-summary-position-point))))
7054
7055 (defun gnus-summary-limit-to-display-predicate ()
7056   "Limit the summary buffer to the predicated in the `display' group parameter."
7057   (interactive)
7058   (unless gnus-newsgroup-display
7059     (error "There is no `display' group parameter"))
7060   (let (articles)
7061     (dolist (number gnus-newsgroup-articles)
7062       (when (funcall gnus-newsgroup-display)
7063         (push number articles)))
7064     (gnus-summary-limit articles))
7065   (gnus-summary-position-point))
7066
7067 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7068 (make-obsolete
7069  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7070
7071 (defun gnus-summary-limit-to-unread (&optional all)
7072   "Limit the summary buffer to articles that are not marked as read.
7073 If ALL is non-nil, limit strictly to unread articles."
7074   (interactive "P")
7075   (if all
7076       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7077     (gnus-summary-limit-to-marks
7078      ;; Concat all the marks that say that an article is read and have
7079      ;; those removed.
7080      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7081            gnus-killed-mark gnus-kill-file-mark
7082            gnus-low-score-mark gnus-expirable-mark
7083            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7084            gnus-duplicate-mark gnus-souped-mark)
7085      'reverse)))
7086
7087 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7088 (make-obsolete 'gnus-summary-delete-marked-with
7089                'gnus-summary-limit-exlude-marks)
7090
7091 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7092   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7093 If REVERSE, limit the summary buffer to articles that are marked
7094 with MARKS.  MARKS can either be a string of marks or a list of marks.
7095 Returns how many articles were removed."
7096   (interactive "sMarks: ")
7097   (gnus-summary-limit-to-marks marks t))
7098
7099 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7100   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7101 If REVERSE (the prefix), limit the summary buffer to articles that are
7102 not marked with MARKS.  MARKS can either be a string of marks or a
7103 list of marks.
7104 Returns how many articles were removed."
7105   (interactive "sMarks: \nP")
7106   (prog1
7107       (let ((data gnus-newsgroup-data)
7108             (marks (if (listp marks) marks
7109                      (append marks nil))) ; Transform to list.
7110             articles)
7111         (while data
7112           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7113                   (memq (gnus-data-mark (car data)) marks))
7114             (push (gnus-data-number (car data)) articles))
7115           (setq data (cdr data)))
7116         (gnus-summary-limit articles))
7117     (gnus-summary-position-point)))
7118
7119 (defun gnus-summary-limit-to-score (score)
7120   "Limit to articles with score at or above SCORE."
7121   (interactive "NLimit to articles with score of at least: ")
7122   (let ((data gnus-newsgroup-data)
7123         articles)
7124     (while data
7125       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7126                 score)
7127         (push (gnus-data-number (car data)) articles))
7128       (setq data (cdr data)))
7129     (prog1
7130         (gnus-summary-limit articles)
7131       (gnus-summary-position-point))))
7132
7133 (defun gnus-summary-limit-include-thread (id)
7134   "Display all the hidden articles that is in the thread with ID in it.
7135 When called interactively, ID is the Message-ID of the current
7136 article."
7137   (interactive (list (mail-header-id (gnus-summary-article-header))))
7138   (let ((articles (gnus-articles-in-thread
7139                    (gnus-id-to-thread (gnus-root-id id)))))
7140     (prog1
7141         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7142       (gnus-summary-limit-include-matching-articles
7143        "subject"
7144        (regexp-quote (gnus-simplify-subject-re
7145                       (mail-header-subject (gnus-id-to-header id)))))
7146       (gnus-summary-position-point))))
7147
7148 (defun gnus-summary-limit-include-matching-articles (header regexp)
7149   "Display all the hidden articles that have HEADERs that match REGEXP."
7150   (interactive (list (read-string "Match on header: ")
7151                      (read-string "Regexp: ")))
7152   (let ((articles (gnus-find-matching-articles header regexp)))
7153     (prog1
7154         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7155       (gnus-summary-position-point))))
7156
7157 (defun gnus-summary-limit-include-dormant ()
7158   "Display all the hidden articles that are marked as dormant.
7159 Note that this command only works on a subset of the articles currently
7160 fetched for this group."
7161   (interactive)
7162   (unless gnus-newsgroup-dormant
7163     (error "There are no dormant articles in this group"))
7164   (prog1
7165       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7166     (gnus-summary-position-point)))
7167
7168 (defun gnus-summary-limit-exclude-dormant ()
7169   "Hide all dormant articles."
7170   (interactive)
7171   (prog1
7172       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7173     (gnus-summary-position-point)))
7174
7175 (defun gnus-summary-limit-exclude-childless-dormant ()
7176   "Hide all dormant articles that have no children."
7177   (interactive)
7178   (let ((data (gnus-data-list t))
7179         articles d children)
7180     ;; Find all articles that are either not dormant or have
7181     ;; children.
7182     (while (setq d (pop data))
7183       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7184                 (and (setq children
7185                            (gnus-article-children (gnus-data-number d)))
7186                      (let (found)
7187                        (while children
7188                          (when (memq (car children) articles)
7189                            (setq children nil
7190                                  found t))
7191                          (pop children))
7192                        found)))
7193         (push (gnus-data-number d) articles)))
7194     ;; Do the limiting.
7195     (prog1
7196         (gnus-summary-limit articles)
7197       (gnus-summary-position-point))))
7198
7199 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7200   "Mark all unread excluded articles as read.
7201 If ALL, mark even excluded ticked and dormants as read."
7202   (interactive "P")
7203   (let ((articles (gnus-sorted-complement
7204                    (sort
7205                     (mapcar (lambda (h) (mail-header-number h))
7206                             gnus-newsgroup-headers)
7207                     '<)
7208                    (sort gnus-newsgroup-limit '<)))
7209         article)
7210     (setq gnus-newsgroup-unreads
7211           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7212     (if all
7213         (setq gnus-newsgroup-dormant nil
7214               gnus-newsgroup-marked nil
7215               gnus-newsgroup-reads
7216               (nconc
7217                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7218                gnus-newsgroup-reads))
7219       (while (setq article (pop articles))
7220         (unless (or (memq article gnus-newsgroup-dormant)
7221                     (memq article gnus-newsgroup-marked))
7222           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7223
7224 (defun gnus-summary-limit (articles &optional pop)
7225   (if pop
7226       ;; We pop the previous limit off the stack and use that.
7227       (setq articles (car gnus-newsgroup-limits)
7228             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7229     ;; We use the new limit, so we push the old limit on the stack.
7230     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7231   ;; Set the limit.
7232   (setq gnus-newsgroup-limit articles)
7233   (let ((total (length gnus-newsgroup-data))
7234         (data (gnus-data-find-list (gnus-summary-article-number)))
7235         (gnus-summary-mark-below nil)   ; Inhibit this.
7236         found)
7237     ;; This will do all the work of generating the new summary buffer
7238     ;; according to the new limit.
7239     (gnus-summary-prepare)
7240     ;; Hide any threads, possibly.
7241     (gnus-summary-maybe-hide-threads)
7242     ;; Try to return to the article you were at, or one in the
7243     ;; neighborhood.
7244     (when data
7245       ;; We try to find some article after the current one.
7246       (while data
7247         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7248           (setq data nil
7249                 found t))
7250         (setq data (cdr data))))
7251     (unless found
7252       ;; If there is no data, that means that we were after the last
7253       ;; article.  The same goes when we can't find any articles
7254       ;; after the current one.
7255       (goto-char (point-max))
7256       (gnus-summary-find-prev))
7257     (gnus-set-mode-line 'summary)
7258     ;; We return how many articles were removed from the summary
7259     ;; buffer as a result of the new limit.
7260     (- total (length gnus-newsgroup-data))))
7261
7262 (defsubst gnus-invisible-cut-children (threads)
7263   (let ((num 0))
7264     (while threads
7265       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7266         (incf num))
7267       (pop threads))
7268     (< num 2)))
7269
7270 (defsubst gnus-cut-thread (thread)
7271   "Go forwards in the thread until we find an article that we want to display."
7272   (when (or (eq gnus-fetch-old-headers 'some)
7273             (eq gnus-fetch-old-headers 'invisible)
7274             (numberp gnus-fetch-old-headers)
7275             (eq gnus-build-sparse-threads 'some)
7276             (eq gnus-build-sparse-threads 'more))
7277     ;; Deal with old-fetched headers and sparse threads.
7278     (while (and
7279             thread
7280             (or
7281              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7282              (gnus-summary-article-ancient-p
7283               (mail-header-number (car thread))))
7284             (if (or (<= (length (cdr thread)) 1)
7285                     (eq gnus-fetch-old-headers 'invisible))
7286                 (setq gnus-newsgroup-limit
7287                       (delq (mail-header-number (car thread))
7288                             gnus-newsgroup-limit)
7289                       thread (cadr thread))
7290               (when (gnus-invisible-cut-children (cdr thread))
7291                 (let ((th (cdr thread)))
7292                   (while th
7293                     (if (memq (mail-header-number (caar th))
7294                               gnus-newsgroup-limit)
7295                         (setq thread (car th)
7296                               th nil)
7297                       (setq th (cdr th))))))))))
7298   thread)
7299
7300 (defun gnus-cut-threads (threads)
7301   "Cut off all uninteresting articles from the beginning of threads."
7302   (when (or (eq gnus-fetch-old-headers 'some)
7303             (eq gnus-fetch-old-headers 'invisible)
7304             (numberp gnus-fetch-old-headers)
7305             (eq gnus-build-sparse-threads 'some)
7306             (eq gnus-build-sparse-threads 'more))
7307     (let ((th threads))
7308       (while th
7309         (setcar th (gnus-cut-thread (car th)))
7310         (setq th (cdr th)))))
7311   ;; Remove nixed out threads.
7312   (delq nil threads))
7313
7314 (defun gnus-summary-initial-limit (&optional show-if-empty)
7315   "Figure out what the initial limit is supposed to be on group entry.
7316 This entails weeding out unwanted dormants, low-scored articles,
7317 fetch-old-headers verbiage, and so on."
7318   ;; Most groups have nothing to remove.
7319   (if (or gnus-inhibit-limiting
7320           (and (null gnus-newsgroup-dormant)
7321                (eq gnus-newsgroup-display 'gnus-not-ignore)
7322                (not (eq gnus-fetch-old-headers 'some))
7323                (not (numberp gnus-fetch-old-headers))
7324                (not (eq gnus-fetch-old-headers 'invisible))
7325                (null gnus-summary-expunge-below)
7326                (not (eq gnus-build-sparse-threads 'some))
7327                (not (eq gnus-build-sparse-threads 'more))
7328                (null gnus-thread-expunge-below)
7329                (not gnus-use-nocem)))
7330       ()                                ; Do nothing.
7331     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7332     (setq gnus-newsgroup-limit nil)
7333     (mapatoms
7334      (lambda (node)
7335        (unless (car (symbol-value node))
7336          ;; These threads have no parents -- they are roots.
7337          (let ((nodes (cdr (symbol-value node)))
7338                thread)
7339            (while nodes
7340              (if (and gnus-thread-expunge-below
7341                       (< (gnus-thread-total-score (car nodes))
7342                          gnus-thread-expunge-below))
7343                  (gnus-expunge-thread (pop nodes))
7344                (setq thread (pop nodes))
7345                (gnus-summary-limit-children thread))))))
7346      gnus-newsgroup-dependencies)
7347     ;; If this limitation resulted in an empty group, we might
7348     ;; pop the previous limit and use it instead.
7349     (when (and (not gnus-newsgroup-limit)
7350                show-if-empty)
7351       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7352     gnus-newsgroup-limit))
7353
7354 (defun gnus-summary-limit-children (thread)
7355   "Return 1 if this subthread is visible and 0 if it is not."
7356   ;; First we get the number of visible children to this thread.  This
7357   ;; is done by recursing down the thread using this function, so this
7358   ;; will really go down to a leaf article first, before slowly
7359   ;; working its way up towards the root.
7360   (when thread
7361     (let ((children
7362            (if (cdr thread)
7363                (apply '+ (mapcar 'gnus-summary-limit-children
7364                                  (cdr thread)))
7365              0))
7366           (number (mail-header-number (car thread)))
7367           score)
7368       (if (and
7369            (not (memq number gnus-newsgroup-marked))
7370            (or
7371             ;; If this article is dormant and has absolutely no visible
7372             ;; children, then this article isn't visible.
7373             (and (memq number gnus-newsgroup-dormant)
7374                  (zerop children))
7375             ;; If this is "fetch-old-headered" and there is no
7376             ;; visible children, then we don't want this article.
7377             (and (or (eq gnus-fetch-old-headers 'some)
7378                      (numberp gnus-fetch-old-headers))
7379                  (gnus-summary-article-ancient-p number)
7380                  (zerop children))
7381             ;; If this is "fetch-old-headered" and `invisible', then
7382             ;; we don't want this article.
7383             (and (eq gnus-fetch-old-headers 'invisible)
7384                  (gnus-summary-article-ancient-p number))
7385             ;; If this is a sparsely inserted article with no children,
7386             ;; we don't want it.
7387             (and (eq gnus-build-sparse-threads 'some)
7388                  (gnus-summary-article-sparse-p number)
7389                  (zerop children))
7390             ;; If we use expunging, and this article is really
7391             ;; low-scored, then we don't want this article.
7392             (when (and gnus-summary-expunge-below
7393                        (< (setq score
7394                                 (or (cdr (assq number gnus-newsgroup-scored))
7395                                     gnus-summary-default-score))
7396                           gnus-summary-expunge-below))
7397               ;; We increase the expunge-tally here, but that has
7398               ;; nothing to do with the limits, really.
7399               (incf gnus-newsgroup-expunged-tally)
7400               ;; We also mark as read here, if that's wanted.
7401               (when (and gnus-summary-mark-below
7402                          (< score gnus-summary-mark-below))
7403                 (setq gnus-newsgroup-unreads
7404                       (delq number gnus-newsgroup-unreads))
7405                 (if gnus-newsgroup-auto-expire
7406                     (push number gnus-newsgroup-expirable)
7407                   (push (cons number gnus-low-score-mark)
7408                         gnus-newsgroup-reads)))
7409               t)
7410             ;; Do the `display' group parameter.
7411             (and gnus-newsgroup-display
7412                  (not (funcall gnus-newsgroup-display)))
7413             ;; Check NoCeM things.
7414             (if (and gnus-use-nocem
7415                      (gnus-nocem-unwanted-article-p
7416                       (mail-header-id (car thread))))
7417                 (progn
7418                   (setq gnus-newsgroup-unreads
7419                         (delq number gnus-newsgroup-unreads))
7420                   t))))
7421           ;; Nope, invisible article.
7422           0
7423         ;; Ok, this article is to be visible, so we add it to the limit
7424         ;; and return 1.
7425         (push number gnus-newsgroup-limit)
7426         1))))
7427
7428 (defun gnus-expunge-thread (thread)
7429   "Mark all articles in THREAD as read."
7430   (let* ((number (mail-header-number (car thread))))
7431     (incf gnus-newsgroup-expunged-tally)
7432     ;; We also mark as read here, if that's wanted.
7433     (setq gnus-newsgroup-unreads
7434           (delq number gnus-newsgroup-unreads))
7435     (if gnus-newsgroup-auto-expire
7436         (push number gnus-newsgroup-expirable)
7437       (push (cons number gnus-low-score-mark)
7438             gnus-newsgroup-reads)))
7439   ;; Go recursively through all subthreads.
7440   (mapcar 'gnus-expunge-thread (cdr thread)))
7441
7442 ;; Summary article oriented commands
7443
7444 (defun gnus-summary-refer-parent-article (n)
7445   "Refer parent article N times.
7446 If N is negative, go to ancestor -N instead.
7447 The difference between N and the number of articles fetched is returned."
7448   (interactive "p")
7449   (let ((skip 1)
7450         error header ref)
7451     (when (not (natnump n))
7452       (setq skip (abs n)
7453             n 1))
7454     (while (and (> n 0)
7455                 (not error))
7456       (setq header (gnus-summary-article-header))
7457       (if (and (eq (mail-header-number header)
7458                    (cdr gnus-article-current))
7459                (equal gnus-newsgroup-name
7460                       (car gnus-article-current)))
7461           ;; If we try to find the parent of the currently
7462           ;; displayed article, then we take a look at the actual
7463           ;; References header, since this is slightly more
7464           ;; reliable than the References field we got from the
7465           ;; server.
7466           (save-excursion
7467             (set-buffer gnus-original-article-buffer)
7468             (nnheader-narrow-to-headers)
7469             (unless (setq ref (message-fetch-field "references"))
7470               (setq ref (message-fetch-field "in-reply-to")))
7471             (widen))
7472         (setq ref
7473               ;; It's not the current article, so we take a bet on
7474               ;; the value we got from the server.
7475               (mail-header-references header)))
7476       (if (and ref
7477                (not (equal ref "")))
7478           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7479             (gnus-message 1 "Couldn't find parent"))
7480         (gnus-message 1 "No references in article %d"
7481                       (gnus-summary-article-number))
7482         (setq error t))
7483       (decf n))
7484     (gnus-summary-position-point)
7485     n))
7486
7487 (defun gnus-summary-refer-references ()
7488   "Fetch all articles mentioned in the References header.
7489 Return the number of articles fetched."
7490   (interactive)
7491   (let ((ref (mail-header-references (gnus-summary-article-header)))
7492         (current (gnus-summary-article-number))
7493         (n 0))
7494     (if (or (not ref)
7495             (equal ref ""))
7496         (error "No References in the current article")
7497       ;; For each Message-ID in the References header...
7498       (while (string-match "<[^>]*>" ref)
7499         (incf n)
7500         ;; ... fetch that article.
7501         (gnus-summary-refer-article
7502          (prog1 (match-string 0 ref)
7503            (setq ref (substring ref (match-end 0))))))
7504       (gnus-summary-goto-subject current)
7505       (gnus-summary-position-point)
7506       n)))
7507
7508 (defun gnus-summary-refer-thread (&optional limit)
7509   "Fetch all articles in the current thread.
7510 If LIMIT (the numerical prefix), fetch that many old headers instead
7511 of what's specified by the `gnus-refer-thread-limit' variable."
7512   (interactive "P")
7513   (let ((id (mail-header-id (gnus-summary-article-header)))
7514         (limit (if limit (prefix-numeric-value limit)
7515                  gnus-refer-thread-limit)))
7516     ;; We want to fetch LIMIT *old* headers, but we also have to
7517     ;; re-fetch all the headers in the current buffer, because many of
7518     ;; them may be undisplayed.  So we adjust LIMIT.
7519     (when (numberp limit)
7520       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7521     (unless (eq gnus-fetch-old-headers 'invisible)
7522       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7523       ;; Retrieve the headers and read them in.
7524       (if (eq (gnus-retrieve-headers
7525                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7526               'nov)
7527           (gnus-build-all-threads)
7528         (error "Can't fetch thread from backends that don't support NOV"))
7529       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7530     (gnus-summary-limit-include-thread id)))
7531
7532 (defun gnus-summary-refer-article (message-id)
7533   "Fetch an article specified by MESSAGE-ID."
7534   (interactive "sMessage-ID: ")
7535   (when (and (stringp message-id)
7536              (not (zerop (length message-id))))
7537     ;; Construct the correct Message-ID if necessary.
7538     ;; Suggested by tale@pawl.rpi.edu.
7539     (unless (string-match "^<" message-id)
7540       (setq message-id (concat "<" message-id)))
7541     (unless (string-match ">$" message-id)
7542       (setq message-id (concat message-id ">")))
7543     (let* ((header (gnus-id-to-header message-id))
7544            (sparse (and header
7545                         (gnus-summary-article-sparse-p
7546                          (mail-header-number header))
7547                         (memq (mail-header-number header)
7548                               gnus-newsgroup-limit)))
7549            number)
7550       (cond
7551        ;; If the article is present in the buffer we just go to it.
7552        ((and header
7553              (or (not (gnus-summary-article-sparse-p
7554                        (mail-header-number header)))
7555                  sparse))
7556         (prog1
7557             (gnus-summary-goto-article
7558              (mail-header-number header) nil t)
7559           (when sparse
7560             (gnus-summary-update-article (mail-header-number header)))))
7561        (t
7562         ;; We fetch the article.
7563         (catch 'found
7564           (dolist (gnus-override-method (gnus-refer-article-methods))
7565             (gnus-check-server gnus-override-method)
7566             ;; Fetch the header, and display the article.
7567             (when (setq number (gnus-summary-insert-subject message-id))
7568               (gnus-summary-select-article nil nil nil number)
7569               (throw 'found t)))
7570           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7571
7572 (defun gnus-refer-article-methods ()
7573   "Return a list of referrable methods."
7574   (cond
7575    ;; No method, so we default to current and native.
7576    ((null gnus-refer-article-method)
7577     (list gnus-current-select-method gnus-select-method))
7578    ;; Current.
7579    ((eq 'current gnus-refer-article-method)
7580     (list gnus-current-select-method))
7581    ;; List of select methods.
7582    ((not (and (symbolp (car gnus-refer-article-method))
7583               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7584     (let (out)
7585       (dolist (method gnus-refer-article-method)
7586         (push (if (eq 'current method)
7587                   gnus-current-select-method
7588                 method)
7589               out))
7590       (nreverse out)))
7591    ;; One single select method.
7592    (t
7593     (list gnus-refer-article-method))))
7594
7595 (defun gnus-summary-edit-parameters ()
7596   "Edit the group parameters of the current group."
7597   (interactive)
7598   (gnus-group-edit-group gnus-newsgroup-name 'params))
7599
7600 (defun gnus-summary-customize-parameters ()
7601   "Customize the group parameters of the current group."
7602   (interactive)
7603   (gnus-group-customize gnus-newsgroup-name))
7604
7605 (defun gnus-summary-enter-digest-group (&optional force)
7606   "Enter an nndoc group based on the current article.
7607 If FORCE, force a digest interpretation.  If not, try
7608 to guess what the document format is."
7609   (interactive "P")
7610   (let ((conf gnus-current-window-configuration))
7611     (save-excursion
7612       (gnus-summary-select-article))
7613     (setq gnus-current-window-configuration conf)
7614     (let* ((name (format "%s-%d"
7615                          (gnus-group-prefixed-name
7616                           gnus-newsgroup-name (list 'nndoc ""))
7617                          (save-excursion
7618                            (set-buffer gnus-summary-buffer)
7619                            gnus-current-article)))
7620            (ogroup gnus-newsgroup-name)
7621            (params (append (gnus-info-params (gnus-get-info ogroup))
7622                            (list (cons 'to-group ogroup))
7623                            (list (cons 'save-article-group ogroup))))
7624            (case-fold-search t)
7625            (buf (current-buffer))
7626            dig to-address)
7627       (save-excursion
7628         (set-buffer gnus-original-article-buffer)
7629         ;; Have the digest group inherit the main mail address of
7630         ;; the parent article.
7631         (when (setq to-address (or (message-fetch-field "reply-to")
7632                                    (message-fetch-field "from")))
7633           (setq params (append
7634                         (list (cons 'to-address
7635                                     (funcall gnus-decode-encoded-word-function
7636                                              to-address))))))
7637         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7638         (insert-buffer-substring gnus-original-article-buffer)
7639         ;; Remove lines that may lead nndoc to misinterpret the
7640         ;; document type.
7641         (narrow-to-region
7642          (goto-char (point-min))
7643          (or (search-forward "\n\n" nil t) (point)))
7644         (goto-char (point-min))
7645         (delete-matching-lines "^Path:\\|^From ")
7646         (widen))
7647       (unwind-protect
7648           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7649                     (gnus-newsgroup-ephemeral-ignored-charsets
7650                      gnus-newsgroup-ignored-charsets))
7651                 (gnus-group-read-ephemeral-group
7652                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7653                               (nndoc-article-type
7654                                ,(if force 'mbox 'guess))) t))
7655             ;; Make all postings to this group go to the parent group.
7656               (nconc (gnus-info-params (gnus-get-info name))
7657                      params)
7658             ;; Couldn't select this doc group.
7659             (switch-to-buffer buf)
7660             (gnus-set-global-variables)
7661             (gnus-configure-windows 'summary)
7662             (gnus-message 3 "Article couldn't be entered?"))
7663         (kill-buffer dig)))))
7664
7665 (defun gnus-summary-read-document (n)
7666   "Open a new group based on the current article(s).
7667 This will allow you to read digests and other similar
7668 documents as newsgroups.
7669 Obeys the standard process/prefix convention."
7670   (interactive "P")
7671   (let* ((articles (gnus-summary-work-articles n))
7672          (ogroup gnus-newsgroup-name)
7673          (params (append (gnus-info-params (gnus-get-info ogroup))
7674                          (list (cons 'to-group ogroup))))
7675          article group egroup groups vgroup)
7676     (while (setq article (pop articles))
7677       (setq group (format "%s-%d" gnus-newsgroup-name article))
7678       (gnus-summary-remove-process-mark article)
7679       (when (gnus-summary-display-article article)
7680         (save-excursion
7681           (with-temp-buffer
7682             (insert-buffer-substring gnus-original-article-buffer)
7683             ;; Remove some headers that may lead nndoc to make
7684             ;; the wrong guess.
7685             (message-narrow-to-head)
7686             (goto-char (point-min))
7687             (delete-matching-lines "^\\(Path\\):\\|^From ")
7688             (widen)
7689             (if (setq egroup
7690                       (gnus-group-read-ephemeral-group
7691                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7692                                      (nndoc-article-type guess))
7693                        t nil t))
7694                 (progn
7695             ;; Make all postings to this group go to the parent group.
7696                   (nconc (gnus-info-params (gnus-get-info egroup))
7697                          params)
7698                   (push egroup groups))
7699               ;; Couldn't select this doc group.
7700               (gnus-error 3 "Article couldn't be entered"))))))
7701     ;; Now we have selected all the documents.
7702     (cond
7703      ((not groups)
7704       (error "None of the articles could be interpreted as documents"))
7705      ((gnus-group-read-ephemeral-group
7706        (setq vgroup (format
7707                      "nnvirtual:%s-%s" gnus-newsgroup-name
7708                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7709        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7710        t
7711        (cons (current-buffer) 'summary)))
7712      (t
7713       (error "Couldn't select virtual nndoc group")))))
7714
7715 (defun gnus-summary-isearch-article (&optional regexp-p)
7716   "Do incremental search forward on the current article.
7717 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7718   (interactive "P")
7719   (gnus-summary-select-article)
7720   (gnus-configure-windows 'article)
7721   (gnus-eval-in-buffer-window gnus-article-buffer
7722     (save-restriction
7723       (widen)
7724       (isearch-forward regexp-p))))
7725
7726 (defun gnus-summary-search-article-forward (regexp &optional backward)
7727   "Search for an article containing REGEXP forward.
7728 If BACKWARD, search backward instead."
7729   (interactive
7730    (list (read-string
7731           (format "Search article %s (regexp%s): "
7732                   (if current-prefix-arg "backward" "forward")
7733                   (if gnus-last-search-regexp
7734                       (concat ", default " gnus-last-search-regexp)
7735                     "")))
7736          current-prefix-arg))
7737   (if (string-equal regexp "")
7738       (setq regexp (or gnus-last-search-regexp ""))
7739     (setq gnus-last-search-regexp regexp)
7740     (setq gnus-article-before-search gnus-current-article))
7741   ;; Intentionally set gnus-last-article.
7742   (setq gnus-last-article gnus-article-before-search)
7743   (let ((gnus-last-article gnus-last-article))
7744     (if (gnus-summary-search-article regexp backward)
7745         (gnus-summary-show-thread)
7746       (error "Search failed: \"%s\"" regexp))))
7747
7748 (defun gnus-summary-search-article-backward (regexp)
7749   "Search for an article containing REGEXP backward."
7750   (interactive
7751    (list (read-string
7752           (format "Search article backward (regexp%s): "
7753                   (if gnus-last-search-regexp
7754                       (concat ", default " gnus-last-search-regexp)
7755                     "")))))
7756   (gnus-summary-search-article-forward regexp 'backward))
7757
7758 (defun gnus-summary-search-article (regexp &optional backward)
7759   "Search for an article containing REGEXP.
7760 Optional argument BACKWARD means do search for backward.
7761 `gnus-select-article-hook' is not called during the search."
7762   ;; We have to require this here to make sure that the following
7763   ;; dynamic binding isn't shadowed by autoloading.
7764   (require 'gnus-async)
7765   (require 'gnus-art)
7766   (let ((gnus-select-article-hook nil)  ;Disable hook.
7767         (gnus-article-prepare-hook nil)
7768         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7769         (gnus-use-article-prefetch nil)
7770         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7771         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7772         (sum (current-buffer))
7773         (gnus-display-mime-function nil)
7774         (found nil)
7775         point)
7776     (gnus-save-hidden-threads
7777       (gnus-summary-select-article)
7778       (set-buffer gnus-article-buffer)
7779       (goto-char (window-point (get-buffer-window (current-buffer))))
7780       (when backward
7781         (forward-line -1))
7782       (while (not found)
7783         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7784         (if (if backward
7785                 (re-search-backward regexp nil t)
7786               (re-search-forward regexp nil t))
7787             ;; We found the regexp.
7788             (progn
7789               (setq found 'found)
7790               (beginning-of-line)
7791               (set-window-start
7792                (get-buffer-window (current-buffer))
7793                (point))
7794               (forward-line 1)
7795               (set-window-point
7796                (get-buffer-window (current-buffer))
7797                (point))
7798               (set-buffer sum)
7799               (setq point (point)))
7800           ;; We didn't find it, so we go to the next article.
7801           (set-buffer sum)
7802           (setq found 'not)
7803           (while (eq found 'not)
7804             (if (not (if backward (gnus-summary-find-prev)
7805                        (gnus-summary-find-next)))
7806                 ;; No more articles.
7807                 (setq found t)
7808               ;; Select the next article and adjust point.
7809               (unless (gnus-summary-article-sparse-p
7810                        (gnus-summary-article-number))
7811                 (setq found nil)
7812                 (gnus-summary-select-article)
7813                 (set-buffer gnus-article-buffer)
7814                 (widen)
7815                 (goto-char (if backward (point-max) (point-min))))))))
7816       (gnus-message 7 ""))
7817     ;; Return whether we found the regexp.
7818     (when (eq found 'found)
7819       (goto-char point)
7820       (gnus-summary-show-thread)
7821       (gnus-summary-goto-subject gnus-current-article)
7822       (gnus-summary-position-point)
7823       t)))
7824
7825 (defun gnus-find-matching-articles (header regexp)
7826   "Return a list of all articles that match REGEXP on HEADER.
7827 This search includes all articles in the current group that Gnus has
7828 fetched headers for, whether they are displayed or not."
7829   (let ((articles nil)
7830         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7831         (case-fold-search t))
7832     (dolist (header gnus-newsgroup-headers)
7833       (when (string-match regexp (funcall func header))
7834         (push (mail-header-number header) articles)))
7835     (nreverse articles)))
7836
7837 (defun gnus-summary-find-matching (header regexp &optional backward unread
7838                                           not-case-fold not-matching)
7839   "Return a list of all articles that match REGEXP on HEADER.
7840 The search stars on the current article and goes forwards unless
7841 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7842 If UNREAD is non-nil, only unread articles will
7843 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7844 in the comparisons. If NOT-MATCHING, return a list of all articles that
7845 not match REGEXP on HEADER."
7846   (let ((case-fold-search (not not-case-fold))
7847         articles d func)
7848     (if (consp header)
7849         (if (eq (car header) 'extra)
7850             (setq func
7851                   `(lambda (h)
7852                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7853                          "")))
7854           (error "%s is an invalid header" header))
7855       (unless (fboundp (intern (concat "mail-header-" header)))
7856         (error "%s is not a valid header" header))
7857       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7858     (dolist (d (if (eq backward 'all)
7859                    gnus-newsgroup-data
7860                  (gnus-data-find-list
7861                   (gnus-summary-article-number)
7862                   (gnus-data-list backward))))
7863       (when (and (or (not unread)       ; We want all articles...
7864                      (gnus-data-unread-p d)) ; Or just unreads.
7865                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7866                  (if not-matching
7867                      (not (string-match
7868                            regexp
7869                            (funcall func (gnus-data-header d))))
7870                    (string-match regexp
7871                                  (funcall func (gnus-data-header d)))))
7872         (push (gnus-data-number d) articles))) ; Success!
7873     (nreverse articles)))
7874
7875 (defun gnus-summary-execute-command (header regexp command &optional backward)
7876   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7877 If HEADER is an empty string (or nil), the match is done on the entire
7878 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7879   (interactive
7880    (list (let ((completion-ignore-case t))
7881            (completing-read
7882             "Header name: "
7883             (mapcar (lambda (header) (list (format "%s" header)))
7884                     (append
7885                      '("Number" "Subject" "From" "Lines" "Date"
7886                        "Message-ID" "Xref" "References" "Body")
7887                      gnus-extra-headers))
7888             nil 'require-match))
7889          (read-string "Regexp: ")
7890          (read-key-sequence "Command: ")
7891          current-prefix-arg))
7892   (when (equal header "Body")
7893     (setq header ""))
7894   ;; Hidden thread subtrees must be searched as well.
7895   (gnus-summary-show-all-threads)
7896   ;; We don't want to change current point nor window configuration.
7897   (save-excursion
7898     (save-window-excursion
7899       (gnus-message 6 "Executing %s..." (key-description command))
7900 ;; We'd like to execute COMMAND interactively so as to give arguments.
7901       (gnus-execute header regexp
7902                     `(call-interactively ',(key-binding command))
7903                     backward)
7904       (gnus-message 6 "Executing %s...done" (key-description command)))))
7905
7906 (defun gnus-summary-beginning-of-article ()
7907   "Scroll the article back to the beginning."
7908   (interactive)
7909   (gnus-summary-select-article)
7910   (gnus-configure-windows 'article)
7911   (gnus-eval-in-buffer-window gnus-article-buffer
7912     (widen)
7913     (goto-char (point-min))
7914     (when gnus-page-broken
7915       (gnus-narrow-to-page))))
7916
7917 (defun gnus-summary-end-of-article ()
7918   "Scroll to the end of the article."
7919   (interactive)
7920   (gnus-summary-select-article)
7921   (gnus-configure-windows 'article)
7922   (gnus-eval-in-buffer-window gnus-article-buffer
7923     (widen)
7924     (goto-char (point-max))
7925     (recenter -3)
7926     (when gnus-page-broken
7927       (gnus-narrow-to-page))))
7928
7929 (defun gnus-summary-print-truncate-and-quote (string &optional len)
7930   "Truncate to LEN and quote all \"(\"'s in STRING."
7931   (gnus-replace-in-string (if (and len (> (length string) len))
7932                               (substring string 0 len)
7933                             string)
7934                           "[()]" "\\\\\\&"))
7935
7936 (defun gnus-summary-print-article (&optional filename n)
7937   "Generate and print a PostScript image of the N next (mail) articles.
7938
7939 If N is negative, print the N previous articles.  If N is nil and articles
7940 have been marked with the process mark, print these instead.
7941
7942 If the optional first argument FILENAME is nil, send the image to the
7943 printer.  If FILENAME is a string, save the PostScript image in a file with
7944 that name.  If FILENAME is a number, prompt the user for the name of the file
7945 to save in."
7946   (interactive (list (ps-print-preprint current-prefix-arg)))
7947   (dolist (article (gnus-summary-work-articles n))
7948     (gnus-summary-select-article nil nil 'pseudo article)
7949     (gnus-eval-in-buffer-window gnus-article-buffer
7950       (gnus-print-buffer))
7951     (gnus-summary-remove-process-mark article))
7952   (ps-despool filename))
7953
7954 (defun gnus-print-buffer ()
7955   (let ((buffer (generate-new-buffer " *print*")))
7956     (unwind-protect
7957         (progn
7958           (copy-to-buffer buffer (point-min) (point-max))
7959           (set-buffer buffer)
7960           (gnus-article-delete-invisible-text)
7961           (when (gnus-visual-p 'article-highlight 'highlight)
7962             ;; Copy-to-buffer doesn't copy overlay.  So redo
7963             ;; highlight.
7964             (let ((gnus-article-buffer buffer))
7965               (gnus-article-highlight-citation t)
7966               (gnus-article-highlight-signature)))
7967           (let ((ps-left-header
7968                  (list
7969                   (concat "("
7970                           (gnus-summary-print-truncate-and-quote
7971                            (mail-header-subject gnus-current-headers)
7972                            66) ")")
7973                   (concat "("
7974                           (gnus-summary-print-truncate-and-quote
7975                            (mail-header-from gnus-current-headers)
7976                            45) ")")))
7977                 (ps-right-header
7978                  (list
7979                   "/pagenumberstring load"
7980                   (concat "("
7981                           (mail-header-date gnus-current-headers) ")"))))
7982             (gnus-run-hooks 'gnus-ps-print-hook)
7983             (save-excursion
7984               (if window-system
7985                   (ps-spool-buffer-with-faces)
7986                 (ps-spool-buffer)))))
7987       (kill-buffer buffer))))
7988
7989 (defun gnus-summary-show-article (&optional arg)
7990   "Force redisplaying of the current article.
7991 If ARG (the prefix) is a number, show the article with the charset
7992 defined in `gnus-summary-show-article-charset-alist', or the charset
7993 input.
7994 If ARG (the prefix) is non-nil and not a number, show the raw article
7995 without any article massaging functions being run.  Normally, the key strokes 
7996 are `C-u g'."
7997   (interactive "P")
7998   (cond
7999    ((numberp arg)
8000     (gnus-summary-show-article t)
8001     (let ((gnus-newsgroup-charset
8002            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8003                (mm-read-coding-system
8004                 "View as charset: "
8005                 (save-excursion
8006                   (set-buffer gnus-article-buffer)
8007                   (let ((coding-systems
8008                          (detect-coding-region (point) (point-max))))
8009                     (or (car-safe coding-systems)
8010                         coding-systems))))))
8011           (gnus-newsgroup-ignored-charsets 'gnus-all))
8012       (gnus-summary-select-article nil 'force)
8013       (let ((deps gnus-newsgroup-dependencies)
8014             head header lines)
8015         (save-excursion
8016           (set-buffer gnus-original-article-buffer)
8017           (save-restriction
8018             (message-narrow-to-head)
8019             (setq head (buffer-string))
8020             (goto-char (point-min))
8021             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8022               (goto-char (point-max))
8023               (widen)
8024               (setq lines (1- (count-lines (point) (point-max))))))
8025           (with-temp-buffer
8026             (insert (format "211 %d Article retrieved.\n"
8027                             (cdr gnus-article-current)))
8028             (insert head)
8029             (if lines (insert (format "Lines: %d\n" lines)))
8030             (insert ".\n")
8031             (let ((nntp-server-buffer (current-buffer)))
8032               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8033         (gnus-data-set-header
8034          (gnus-data-find (cdr gnus-article-current))
8035          header)
8036         (gnus-summary-update-article-line
8037          (cdr gnus-article-current) header)
8038         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8039           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8040    ((not arg)
8041     ;; Select the article the normal way.
8042     (gnus-summary-select-article nil 'force))
8043    (t
8044     ;; We have to require this here to make sure that the following
8045     ;; dynamic binding isn't shadowed by autoloading.
8046     (require 'gnus-async)
8047     (require 'gnus-art)
8048     ;; Bind the article treatment functions to nil.
8049     (let ((gnus-have-all-headers t)
8050           gnus-article-prepare-hook
8051           gnus-article-decode-hook
8052           gnus-display-mime-function
8053           gnus-break-pages)
8054       ;; Destroy any MIME parts.
8055       (when (gnus-buffer-live-p gnus-article-buffer)
8056         (save-excursion
8057           (set-buffer gnus-article-buffer)
8058           (mm-destroy-parts gnus-article-mime-handles)
8059           ;; Set it to nil for safety reason.
8060           (setq gnus-article-mime-handle-alist nil)
8061           (setq gnus-article-mime-handles nil)))
8062       (gnus-summary-select-article nil 'force))))
8063   (gnus-summary-goto-subject gnus-current-article)
8064   (gnus-summary-position-point))
8065
8066 (defun gnus-summary-show-raw-article ()
8067   "Show the raw article without any article massaging functions being run."
8068   (interactive)
8069   (gnus-summary-show-article t))
8070
8071 (defun gnus-summary-verbose-headers (&optional arg)
8072   "Toggle permanent full header display.
8073 If ARG is a positive number, turn header display on.
8074 If ARG is a negative number, turn header display off."
8075   (interactive "P")
8076   (setq gnus-show-all-headers
8077         (cond ((or (not (numberp arg))
8078                    (zerop arg))
8079                (not gnus-show-all-headers))
8080               ((natnump arg)
8081                t)))
8082   (gnus-summary-show-article))
8083
8084 (defun gnus-summary-toggle-header (&optional arg)
8085   "Show the headers if they are hidden, or hide them if they are shown.
8086 If ARG is a positive number, show the entire header.
8087 If ARG is a negative number, hide the unwanted header lines."
8088   (interactive "P")
8089   (save-excursion
8090     (set-buffer gnus-article-buffer)
8091     (save-restriction
8092       (let* ((buffer-read-only nil)
8093              (inhibit-point-motion-hooks t)
8094              hidden e)
8095         (setq hidden
8096               (if (numberp arg)
8097                   (>= arg 0)
8098                 (save-restriction
8099                   (article-narrow-to-head)
8100                   (gnus-article-hidden-text-p 'headers))))
8101         (goto-char (point-min))
8102         (when (search-forward "\n\n" nil t)
8103           (delete-region (point-min) (1- (point))))
8104         (goto-char (point-min))
8105         (save-excursion
8106           (set-buffer gnus-original-article-buffer)
8107           (goto-char (point-min))
8108           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
8109         (insert-buffer-substring gnus-original-article-buffer 1 e)
8110         (save-restriction
8111           (narrow-to-region (point-min) (point))
8112           (article-decode-encoded-words)
8113           (if  hidden
8114               (let ((gnus-treat-hide-headers nil)
8115                     (gnus-treat-hide-boring-headers nil))
8116                 (gnus-delete-wash-type 'headers)
8117                 (gnus-treat-article 'head))
8118             (gnus-treat-article 'head)))
8119         (gnus-set-mode-line 'article)))))
8120
8121 (defun gnus-summary-show-all-headers ()
8122   "Make all header lines visible."
8123   (interactive)
8124   (gnus-summary-toggle-header 1))
8125
8126 (defun gnus-summary-caesar-message (&optional arg)
8127   "Caesar rotate the current article by 13.
8128 The numerical prefix specifies how many places to rotate each letter
8129 forward."
8130   (interactive "P")
8131   (gnus-summary-select-article)
8132   (let ((mail-header-separator ""))
8133     (gnus-eval-in-buffer-window gnus-article-buffer
8134       (save-restriction
8135         (widen)
8136         (let ((start (window-start))
8137               buffer-read-only)
8138           (message-caesar-buffer-body arg)
8139           (set-window-start (get-buffer-window (current-buffer)) start))))))
8140
8141 (defun gnus-summary-stop-page-breaking ()
8142   "Stop page breaking in the current article."
8143   (interactive)
8144   (gnus-summary-select-article)
8145   (gnus-eval-in-buffer-window gnus-article-buffer
8146     (widen)
8147     (when (gnus-visual-p 'page-marker)
8148       (let ((buffer-read-only nil))
8149         (gnus-remove-text-with-property 'gnus-prev)
8150         (gnus-remove-text-with-property 'gnus-next))
8151       (setq gnus-page-broken nil))))
8152
8153 (defun gnus-summary-move-article (&optional n to-newsgroup
8154                                             select-method action)
8155   "Move the current article to a different newsgroup.
8156 If N is a positive number, move the N next articles.
8157 If N is a negative number, move the N previous articles.
8158 If N is nil and any articles have been marked with the process mark,
8159 move those articles instead.
8160 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8161 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8162 re-spool using this method.
8163
8164 For this function to work, both the current newsgroup and the
8165 newsgroup that you want to move to have to support the `request-move'
8166 and `request-accept' functions.
8167
8168 ACTION can be either `move' (the default), `crosspost' or `copy'."
8169   (interactive "P")
8170   (unless action
8171     (setq action 'move))
8172   ;; Check whether the source group supports the required functions.
8173   (cond ((and (eq action 'move)
8174               (not (gnus-check-backend-function
8175                     'request-move-article gnus-newsgroup-name)))
8176          (error "The current group does not support article moving"))
8177         ((and (eq action 'crosspost)
8178               (not (gnus-check-backend-function
8179                     'request-replace-article gnus-newsgroup-name)))
8180          (error "The current group does not support article editing")))
8181   (let ((articles (gnus-summary-work-articles n))
8182         (prefix (if (gnus-check-backend-function
8183                      'request-move-article gnus-newsgroup-name)
8184                     (gnus-group-real-prefix gnus-newsgroup-name)
8185                   ""))
8186         (names '((move "Move" "Moving")
8187                  (copy "Copy" "Copying")
8188                  (crosspost "Crosspost" "Crossposting")))
8189         (copy-buf (save-excursion
8190                     (nnheader-set-temp-buffer " *copy article*")))
8191         art-group to-method new-xref article to-groups)
8192     (unless (assq action names)
8193       (error "Unknown action %s" action))
8194     ;; We have to select an article to give
8195     ;; `gnus-read-move-group-name' an opportunity to suggest an
8196     ;; appropriate default.
8197     (unless (gnus-buffer-live-p gnus-original-article-buffer)
8198       (gnus-summary-select-article nil nil nil (car articles)))
8199     ;; Read the newsgroup name.
8200     (when (and (not to-newsgroup)
8201                (not select-method))
8202       (setq to-newsgroup
8203             (gnus-read-move-group-name
8204              (cadr (assq action names))
8205              (symbol-value (intern (format "gnus-current-%s-group" action)))
8206              articles prefix))
8207       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8208     (setq to-method (or select-method
8209                         (gnus-server-to-method
8210                          (gnus-group-method to-newsgroup))))
8211     ;; Check the method we are to move this article to...
8212     (unless (gnus-check-backend-function
8213              'request-accept-article (car to-method))
8214       (error "%s does not support article copying" (car to-method)))
8215     (unless (gnus-check-server to-method)
8216       (error "Can't open server %s" (car to-method)))
8217     (gnus-message 6 "%s to %s: %s..."
8218                   (caddr (assq action names))
8219                   (or (car select-method) to-newsgroup) articles)
8220     (while articles
8221       (setq article (pop articles))
8222       (setq
8223        art-group
8224        (cond
8225         ;; Move the article.
8226         ((eq action 'move)
8227          ;; Remove this article from future suppression.
8228          (gnus-dup-unsuppress-article article)
8229          (gnus-request-move-article
8230           article                       ; Article to move
8231           gnus-newsgroup-name           ; From newsgroup
8232           (nth 1 (gnus-find-method-for-group
8233                   gnus-newsgroup-name)) ; Server
8234           (list 'gnus-request-accept-article
8235                 to-newsgroup (list 'quote select-method)
8236                 (not articles) t)       ; Accept form
8237           (not articles)))              ; Only save nov last time
8238         ;; Copy the article.
8239         ((eq action 'copy)
8240          (save-excursion
8241            (set-buffer copy-buf)
8242            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8243              (gnus-request-accept-article
8244               to-newsgroup select-method (not articles) t))))
8245         ;; Crosspost the article.
8246         ((eq action 'crosspost)
8247          (let ((xref (message-tokenize-header
8248                       (mail-header-xref (gnus-summary-article-header article))
8249                       " ")))
8250            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8251                                   ":" (number-to-string article)))
8252            (unless xref
8253              (setq xref (list (system-name))))
8254            (setq new-xref
8255                  (concat
8256                   (mapconcat 'identity
8257                              (delete "Xref:" (delete new-xref xref))
8258                              " ")
8259                   " " new-xref))
8260            (save-excursion
8261              (set-buffer copy-buf)
8262              ;; First put the article in the destination group.
8263              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8264              (when (consp (setq art-group
8265                                 (gnus-request-accept-article
8266                                  to-newsgroup select-method (not articles))))
8267                (setq new-xref (concat new-xref " " (car art-group)
8268                                       ":"
8269                                       (number-to-string (cdr art-group))))
8270                ;; Now we have the new Xrefs header, so we insert
8271                ;; it and replace the new article.
8272                (nnheader-replace-header "Xref" new-xref)
8273                (gnus-request-replace-article
8274                 (cdr art-group) to-newsgroup (current-buffer))
8275                art-group))))))
8276       (cond
8277        ((not art-group)
8278         (gnus-message 1 "Couldn't %s article %s: %s"
8279                       (cadr (assq action names)) article
8280                       (nnheader-get-report (car to-method))))
8281        ((eq art-group 'junk)
8282         (when (eq action 'move)
8283           (gnus-summary-mark-article article gnus-canceled-mark)
8284           (gnus-message 4 "Deleted article %s" article)))
8285        (t
8286         (let* ((pto-group (gnus-group-prefixed-name
8287                            (car art-group) to-method))
8288                (entry
8289                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8290                (info (nth 2 entry))
8291                (to-group (gnus-info-group info))
8292                to-marks)
8293           ;; Update the group that has been moved to.
8294           (when (and info
8295                      (memq action '(move copy)))
8296             (unless (member to-group to-groups)
8297               (push to-group to-groups))
8298
8299             (unless (memq article gnus-newsgroup-unreads)
8300               (push 'read to-marks)
8301               (gnus-info-set-read
8302                info (gnus-add-to-range (gnus-info-read info)
8303                                        (list (cdr art-group)))))
8304
8305             ;; See whether the article is to be put in the cache.
8306             (let ((marks gnus-article-mark-lists)
8307                   (to-article (cdr art-group)))
8308
8309               ;; Enter the article into the cache in the new group,
8310               ;; if that is required.
8311               (when gnus-use-cache
8312                 (gnus-cache-possibly-enter-article
8313                  to-group to-article
8314                  (memq article gnus-newsgroup-marked)
8315                  (memq article gnus-newsgroup-dormant)
8316                  (memq article gnus-newsgroup-unreads)))
8317
8318               (when gnus-preserve-marks
8319                 ;; Copy any marks over to the new group.
8320                 (when (and (equal to-group gnus-newsgroup-name)
8321                            (not (memq article gnus-newsgroup-unreads)))
8322                   ;; Mark this article as read in this group.
8323                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8324                   (setcdr (gnus-active to-group) to-article)
8325                   (setcdr gnus-newsgroup-active to-article))
8326
8327                 (while marks
8328                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8329                     (when (memq article (symbol-value
8330                                          (intern (format "gnus-newsgroup-%s"
8331                                                          (caar marks)))))
8332                       (push (cdar marks) to-marks)
8333                       ;; If the other group is the same as this group,
8334                       ;; then we have to add the mark to the list.
8335                       (when (equal to-group gnus-newsgroup-name)
8336                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8337                              (cons to-article
8338                                    (symbol-value
8339                                     (intern (format "gnus-newsgroup-%s"
8340                                                     (caar marks)))))))
8341                       ;; Copy the marks to other group.
8342                       (gnus-add-marked-articles
8343                        to-group (cdar marks) (list to-article) info)))
8344                   (setq marks (cdr marks)))
8345
8346                 (gnus-request-set-mark to-group (list (list (list to-article)
8347                                                             'add
8348                                                             to-marks))))
8349
8350               (gnus-dribble-enter
8351                (concat "(gnus-group-set-info '"
8352                        (gnus-prin1-to-string (gnus-get-info to-group))
8353                        ")"))))
8354
8355           ;; Update the Xref header in this article to point to
8356           ;; the new crossposted article we have just created.
8357           (when (eq action 'crosspost)
8358             (save-excursion
8359               (set-buffer copy-buf)
8360               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8361               (nnheader-replace-header "Xref" new-xref)
8362               (gnus-request-replace-article
8363                article gnus-newsgroup-name (current-buffer)))))
8364
8365         ;;;!!!Why is this necessary?
8366         (set-buffer gnus-summary-buffer)
8367
8368         (gnus-summary-goto-subject article)
8369         (when (eq action 'move)
8370           (gnus-summary-mark-article article gnus-canceled-mark))))
8371       (gnus-summary-remove-process-mark article))
8372     ;; Re-activate all groups that have been moved to.
8373     (save-excursion
8374       (set-buffer gnus-group-buffer)
8375       (let ((gnus-group-marked to-groups))
8376         (gnus-group-get-new-news-this-group nil t)))
8377
8378     (gnus-kill-buffer copy-buf)
8379     (gnus-summary-position-point)
8380     (gnus-set-mode-line 'summary)))
8381
8382 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8383   "Move the current article to a different newsgroup.
8384 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8385 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8386 re-spool using this method."
8387   (interactive "P")
8388   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8389
8390 (defun gnus-summary-crosspost-article (&optional n)
8391   "Crosspost the current article to some other group."
8392   (interactive "P")
8393   (gnus-summary-move-article n nil nil 'crosspost))
8394
8395 (defcustom gnus-summary-respool-default-method nil
8396   "Default method type for respooling an article.
8397 If nil, use to the current newsgroup method."
8398   :type 'symbol
8399   :group 'gnus-summary-mail)
8400
8401 (defun gnus-summary-respool-article (&optional n method)
8402   "Respool the current article.
8403 The article will be squeezed through the mail spooling process again,
8404 which means that it will be put in some mail newsgroup or other
8405 depending on `nnmail-split-methods'.
8406 If N is a positive number, respool the N next articles.
8407 If N is a negative number, respool the N previous articles.
8408 If N is nil and any articles have been marked with the process mark,
8409 respool those articles instead.
8410
8411 Respooling can be done both from mail groups and \"real\" newsgroups.
8412 In the former case, the articles in question will be moved from the
8413 current group into whatever groups they are destined to.  In the
8414 latter case, they will be copied into the relevant groups."
8415   (interactive
8416    (list current-prefix-arg
8417          (let* ((methods (gnus-methods-using 'respool))
8418                 (methname
8419                  (symbol-name (or gnus-summary-respool-default-method
8420                                   (car (gnus-find-method-for-group
8421                                         gnus-newsgroup-name)))))
8422                 (method
8423                  (gnus-completing-read
8424                   methname "What backend do you want to use when respooling?"
8425                   methods nil t nil 'gnus-mail-method-history))
8426                 ms)
8427            (cond
8428             ((zerop (length (setq ms (gnus-servers-using-backend
8429                                       (intern method)))))
8430              (list (intern method) ""))
8431             ((= 1 (length ms))
8432              (car ms))
8433             (t
8434              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8435                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8436                            ms-alist))))))))
8437   (unless method
8438     (error "No method given for respooling"))
8439   (if (assoc (symbol-name
8440               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8441              (gnus-methods-using 'respool))
8442       (gnus-summary-move-article n nil method)
8443     (gnus-summary-copy-article n nil method)))
8444
8445 (defun gnus-summary-import-article (file &optional edit)
8446   "Import an arbitrary file into a mail newsgroup."
8447   (interactive "fImport file: \nP")
8448   (let ((group gnus-newsgroup-name)
8449         (now (current-time))
8450         atts lines group-art)
8451     (unless (gnus-check-backend-function 'request-accept-article group)
8452       (error "%s does not support article importing" group))
8453     (or (file-readable-p file)
8454         (not (file-regular-p file))
8455         (error "Can't read %s" file))
8456     (save-excursion
8457       (set-buffer (gnus-get-buffer-create " *import file*"))
8458       (erase-buffer)
8459       (nnheader-insert-file-contents file)
8460       (goto-char (point-min))
8461       (if (nnheader-article-p)
8462           (save-restriction
8463             (goto-char (point-min))
8464             (search-forward "\n\n" nil t)
8465             (narrow-to-region (point-min) (1- (point)))
8466             (goto-char (point-min))
8467             (unless (re-search-forward "^date:" nil t)
8468               (goto-char (point-max))
8469               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8470        ;; This doesn't look like an article, so we fudge some headers.
8471         (setq atts (file-attributes file)
8472               lines (count-lines (point-min) (point-max)))
8473         (insert "From: " (read-string "From: ") "\n"
8474                 "Subject: " (read-string "Subject: ") "\n"
8475                 "Date: " (message-make-date (nth 5 atts)) "\n"
8476                 "Message-ID: " (message-make-message-id) "\n"
8477                 "Lines: " (int-to-string lines) "\n"
8478                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8479       (setq group-art (gnus-request-accept-article group nil t))
8480       (kill-buffer (current-buffer)))
8481     (setq gnus-newsgroup-active (gnus-activate-group group))
8482     (forward-line 1)
8483     (gnus-summary-goto-article (cdr group-art) nil t)
8484     (when edit
8485       (gnus-summary-edit-article))))
8486
8487 (defun gnus-summary-create-article ()
8488   "Create an article in a mail newsgroup."
8489   (interactive)
8490   (let ((group gnus-newsgroup-name)
8491         (now (current-time))
8492         group-art)
8493     (unless (gnus-check-backend-function 'request-accept-article group)
8494       (error "%s does not support article importing" group))
8495     (save-excursion
8496       (set-buffer (gnus-get-buffer-create " *import file*"))
8497       (erase-buffer)
8498       (goto-char (point-min))
8499       ;; This doesn't look like an article, so we fudge some headers.
8500       (insert "From: " (read-string "From: ") "\n"
8501               "Subject: " (read-string "Subject: ") "\n"
8502               "Date: " (message-make-date now) "\n"
8503               "Message-ID: " (message-make-message-id) "\n")
8504       (setq group-art (gnus-request-accept-article group nil t))
8505       (kill-buffer (current-buffer)))
8506     (setq gnus-newsgroup-active (gnus-activate-group group))
8507     (forward-line 1)
8508     (gnus-summary-goto-article (cdr group-art) nil t)
8509     (gnus-summary-edit-article)))
8510
8511 (defun gnus-summary-article-posted-p ()
8512   "Say whether the current (mail) article is available from news as well.
8513 This will be the case if the article has both been mailed and posted."
8514   (interactive)
8515   (let ((id (mail-header-references (gnus-summary-article-header)))
8516         (gnus-override-method (car (gnus-refer-article-methods))))
8517     (if (gnus-request-head id "")
8518         (gnus-message 2 "The current message was found on %s"
8519                       gnus-override-method)
8520       (gnus-message 2 "The current message couldn't be found on %s"
8521                     gnus-override-method)
8522       nil)))
8523
8524 (defun gnus-summary-expire-articles (&optional now)
8525   "Expire all articles that are marked as expirable in the current group."
8526   (interactive)
8527   (when (gnus-check-backend-function
8528          'request-expire-articles gnus-newsgroup-name)
8529     ;; This backend supports expiry.
8530     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8531            (expirable (if total
8532                           (progn
8533                             ;; We need to update the info for
8534                             ;; this group for `gnus-list-of-read-articles'
8535                             ;; to give us the right answer.
8536                             (gnus-run-hooks 'gnus-exit-group-hook)
8537                             (gnus-summary-update-info)
8538                             (gnus-list-of-read-articles gnus-newsgroup-name))
8539                         (setq gnus-newsgroup-expirable
8540                               (sort gnus-newsgroup-expirable '<))))
8541            (expiry-wait (if now 'immediate
8542                           (gnus-group-find-parameter
8543                            gnus-newsgroup-name 'expiry-wait)))
8544            (nnmail-expiry-target
8545             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8546                 nnmail-expiry-target))
8547            es)
8548       (when expirable
8549         ;; There are expirable articles in this group, so we run them
8550         ;; through the expiry process.
8551         (gnus-message 6 "Expiring articles...")
8552         (unless (gnus-check-group gnus-newsgroup-name)
8553           (error "Can't open server for %s" gnus-newsgroup-name))
8554         ;; The list of articles that weren't expired is returned.
8555         (save-excursion
8556           (if expiry-wait
8557               (let ((nnmail-expiry-wait-function nil)
8558                     (nnmail-expiry-wait expiry-wait))
8559                 (setq es (gnus-request-expire-articles
8560                           expirable gnus-newsgroup-name)))
8561             (setq es (gnus-request-expire-articles
8562                       expirable gnus-newsgroup-name)))
8563           (unless total
8564             (setq gnus-newsgroup-expirable es))
8565           ;; We go through the old list of expirable, and mark all
8566           ;; really expired articles as nonexistent.
8567           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8568             (let ((gnus-use-cache nil))
8569               (while expirable
8570                 (unless (memq (car expirable) es)
8571                   (when (gnus-data-find (car expirable))
8572                     (gnus-summary-mark-article
8573                      (car expirable) gnus-canceled-mark)))
8574                 (setq expirable (cdr expirable))))))
8575         (gnus-message 6 "Expiring articles...done")))))
8576
8577 (defun gnus-summary-expire-articles-now ()
8578   "Expunge all expirable articles in the current group.
8579 This means that *all* articles that are marked as expirable will be
8580 deleted forever, right now."
8581   (interactive)
8582   (or gnus-expert-user
8583       (gnus-yes-or-no-p
8584        "Are you really, really, really sure you want to delete all these messages? ")
8585       (error "Phew!"))
8586   (gnus-summary-expire-articles t))
8587
8588 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8589 (defun gnus-summary-delete-article (&optional n)
8590   "Delete the N next (mail) articles.
8591 This command actually deletes articles.  This is not a marking
8592 command.  The article will disappear forever from your life, never to
8593 return.
8594 If N is negative, delete backwards.
8595 If N is nil and articles have been marked with the process mark,
8596 delete these instead."
8597   (interactive "P")
8598   (unless (gnus-check-backend-function 'request-expire-articles
8599                                        gnus-newsgroup-name)
8600     (error "The current newsgroup does not support article deletion"))
8601   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8602     (error "Couldn't open server"))
8603   ;; Compute the list of articles to delete.
8604   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8605         not-deleted)
8606     (if (and gnus-novice-user
8607              (not (gnus-yes-or-no-p
8608                    (format "Do you really want to delete %s forever? "
8609                            (if (> (length articles) 1)
8610                                (format "these %s articles" (length articles))
8611                              "this article")))))
8612         ()
8613       ;; Delete the articles.
8614       (setq not-deleted (gnus-request-expire-articles
8615                          articles gnus-newsgroup-name 'force))
8616       (while articles
8617         (gnus-summary-remove-process-mark (car articles))
8618         ;; The backend might not have been able to delete the article
8619         ;; after all.
8620         (unless (memq (car articles) not-deleted)
8621           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8622         (setq articles (cdr articles)))
8623       (when not-deleted
8624         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8625     (gnus-summary-position-point)
8626     (gnus-set-mode-line 'summary)
8627     not-deleted))
8628
8629 (defun gnus-summary-edit-article (&optional arg)
8630   "Edit the current article.
8631 This will have permanent effect only in mail groups.
8632 If ARG is nil, edit the decoded articles.
8633 If ARG is 1, edit the raw articles.
8634 If ARG is 2, edit the raw articles even in read-only groups.
8635 If ARG is 3, edit the articles with the current handles.
8636 Otherwise, allow editing of articles even in read-only
8637 groups."
8638   (interactive "P")
8639   (let (force raw current-handles)
8640     (cond
8641      ((null arg))
8642      ((eq arg 1)
8643       (setq raw t))
8644      ((eq arg 2)
8645       (setq raw t
8646             force t))
8647      ((eq arg 3)
8648       (setq current-handles
8649             (and (gnus-buffer-live-p gnus-article-buffer)
8650                  (with-current-buffer gnus-article-buffer
8651                    (prog1
8652                        gnus-article-mime-handles
8653                      (setq gnus-article-mime-handles nil))))))
8654      (t
8655       (setq force t)))
8656     (when (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8657       (error "Can't edit the raw article in group nndraft:drafts"))
8658     (save-excursion
8659       (set-buffer gnus-summary-buffer)
8660       (let ((mail-parse-charset gnus-newsgroup-charset)
8661             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8662         (gnus-set-global-variables)
8663         (when (and (not force)
8664                    (gnus-group-read-only-p))
8665           (error "The current newsgroup does not support article editing"))
8666         (gnus-summary-show-article t)
8667         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8668           (with-current-buffer gnus-article-buffer
8669             (mm-enable-multibyte)))
8670         (if (equal gnus-newsgroup-name "nndraft:drafts")
8671             (setq raw t))
8672         (gnus-article-edit-article
8673          (if raw 'ignore
8674            `(lambda ()
8675               (let ((mbl mml-buffer-list))
8676                 (setq mml-buffer-list nil)
8677                 (mime-to-mml ,'current-handles)
8678                 (let ((mbl1 mml-buffer-list))
8679                   (setq mml-buffer-list mbl)
8680                   (set (make-local-variable 'mml-buffer-list) mbl1))
8681                 (make-local-hook 'kill-buffer-hook)
8682                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8683          `(lambda (no-highlight)
8684             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8685                   (message-options message-options)
8686                   (message-options-set-recipient)
8687                   (mail-parse-ignored-charsets
8688                    ',gnus-newsgroup-ignored-charsets))
8689               ,(if (not raw) '(progn
8690                                 (mml-to-mime)
8691                                 (mml-destroy-buffers)
8692                                 (remove-hook 'kill-buffer-hook
8693                                              'mml-destroy-buffers t)
8694                                 (kill-local-variable 'mml-buffer-list)))
8695               (gnus-summary-edit-article-done
8696                ,(or (mail-header-references gnus-current-headers) "")
8697                ,(gnus-group-read-only-p)
8698                ,gnus-summary-buffer no-highlight))))))))
8699
8700 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8701
8702 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8703                                                  no-highlight)
8704   "Make edits to the current article permanent."
8705   (interactive)
8706   (save-excursion
8707    ;; The buffer restriction contains the entire article if it exists.
8708     (when (article-goto-body)
8709       (let ((lines (count-lines (point) (point-max)))
8710             (length (- (point-max) (point)))
8711             (case-fold-search t)
8712             (body (copy-marker (point))))
8713         (goto-char (point-min))
8714         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8715           (delete-region (match-beginning 1) (match-end 1))
8716           (insert (number-to-string length)))
8717         (goto-char (point-min))
8718         (when (re-search-forward
8719                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8720           (delete-region (match-beginning 1) (match-end 1))
8721           (insert (number-to-string length)))
8722         (goto-char (point-min))
8723         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8724           (delete-region (match-beginning 1) (match-end 1))
8725           (insert (number-to-string lines))))))
8726   ;; Replace the article.
8727   (let ((buf (current-buffer)))
8728     (with-temp-buffer
8729       (insert-buffer-substring buf)
8730
8731       (if (and (not read-only)
8732                (not (gnus-request-replace-article
8733                      (cdr gnus-article-current) (car gnus-article-current)
8734                      (current-buffer) t)))
8735           (error "Couldn't replace article")
8736         ;; Update the summary buffer.
8737         (if (and references
8738                  (equal (message-tokenize-header references " ")
8739                         (message-tokenize-header
8740                          (or (message-fetch-field "references") "") " ")))
8741             ;; We only have to update this line.
8742             (save-excursion
8743               (save-restriction
8744                 (message-narrow-to-head)
8745                 (let ((head (buffer-string))
8746                       header)
8747                   (with-temp-buffer
8748                     (insert (format "211 %d Article retrieved.\n"
8749                                     (cdr gnus-article-current)))
8750                     (insert head)
8751                     (insert ".\n")
8752                     (let ((nntp-server-buffer (current-buffer)))
8753                       (setq header (car (gnus-get-newsgroup-headers
8754                                          (save-excursion
8755                                            (set-buffer gnus-summary-buffer)
8756                                            gnus-newsgroup-dependencies)
8757                                          t))))
8758                     (save-excursion
8759                       (set-buffer gnus-summary-buffer)
8760                       (gnus-data-set-header
8761                        (gnus-data-find (cdr gnus-article-current))
8762                        header)
8763                       (gnus-summary-update-article-line
8764                        (cdr gnus-article-current) header)
8765                       (if (gnus-summary-goto-subject
8766                            (cdr gnus-article-current) nil t)
8767                           (gnus-summary-update-secondary-mark
8768                            (cdr gnus-article-current))))))))
8769           ;; Update threads.
8770           (set-buffer (or buffer gnus-summary-buffer))
8771           (gnus-summary-update-article (cdr gnus-article-current))
8772           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8773               (gnus-summary-update-secondary-mark
8774                (cdr gnus-article-current))))
8775         ;; Prettify the article buffer again.
8776         (unless no-highlight
8777           (save-excursion
8778             (set-buffer gnus-article-buffer)
8779             ;;;!!! Fix this -- article should be rehighlighted.
8780             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8781             (set-buffer gnus-original-article-buffer)
8782             (gnus-request-article
8783              (cdr gnus-article-current)
8784              (car gnus-article-current) (current-buffer))))
8785         ;; Prettify the summary buffer line.
8786         (when (gnus-visual-p 'summary-highlight 'highlight)
8787           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8788
8789 (defun gnus-summary-edit-wash (key)
8790   "Perform editing command KEY in the article buffer."
8791   (interactive
8792    (list
8793     (progn
8794       (message "%s" (concat (this-command-keys) "- "))
8795       (read-char))))
8796   (message "")
8797   (gnus-summary-edit-article)
8798   (execute-kbd-macro (concat (this-command-keys) key))
8799   (gnus-article-edit-done))
8800
8801 ;;; Respooling
8802
8803 (defun gnus-summary-respool-query (&optional silent trace)
8804   "Query where the respool algorithm would put this article."
8805   (interactive)
8806   (let (gnus-mark-article-hook)
8807     (gnus-summary-select-article)
8808     (save-excursion
8809       (set-buffer gnus-original-article-buffer)
8810       (save-restriction
8811         (message-narrow-to-head)
8812         (let ((groups (nnmail-article-group 'identity trace)))
8813           (unless silent
8814             (if groups
8815                 (message "This message would go to %s"
8816                          (mapconcat 'car groups ", "))
8817               (message "This message would go to no groups"))
8818             groups))))))
8819
8820 (defun gnus-summary-respool-trace ()
8821   "Trace where the respool algorithm would put this article.
8822 Display a buffer showing all fancy splitting patterns which matched."
8823   (interactive)
8824   (gnus-summary-respool-query nil t))
8825
8826 ;; Summary marking commands.
8827
8828 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8829   "Mark articles which has the same subject as read, and then select the next.
8830 If UNMARK is positive, remove any kind of mark.
8831 If UNMARK is negative, tick articles."
8832   (interactive "P")
8833   (when unmark
8834     (setq unmark (prefix-numeric-value unmark)))
8835   (let ((count
8836          (gnus-summary-mark-same-subject
8837           (gnus-summary-article-subject) unmark)))
8838     ;; Select next unread article.  If auto-select-same mode, should
8839     ;; select the first unread article.
8840     (gnus-summary-next-article t (and gnus-auto-select-same
8841                                       (gnus-summary-article-subject)))
8842     (gnus-message 7 "%d article%s marked as %s"
8843                   count (if (= count 1) " is" "s are")
8844                   (if unmark "unread" "read"))))
8845
8846 (defun gnus-summary-kill-same-subject (&optional unmark)
8847   "Mark articles which has the same subject as read.
8848 If UNMARK is positive, remove any kind of mark.
8849 If UNMARK is negative, tick articles."
8850   (interactive "P")
8851   (when unmark
8852     (setq unmark (prefix-numeric-value unmark)))
8853   (let ((count
8854          (gnus-summary-mark-same-subject
8855           (gnus-summary-article-subject) unmark)))
8856     ;; If marked as read, go to next unread subject.
8857     (when (null unmark)
8858       ;; Go to next unread subject.
8859       (gnus-summary-next-subject 1 t))
8860     (gnus-message 7 "%d articles are marked as %s"
8861                   count (if unmark "unread" "read"))))
8862
8863 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8864   "Mark articles with same SUBJECT as read, and return marked number.
8865 If optional argument UNMARK is positive, remove any kinds of marks.
8866 If optional argument UNMARK is negative, mark articles as unread instead."
8867   (let ((count 1))
8868     (save-excursion
8869       (cond
8870        ((null unmark)                   ; Mark as read.
8871         (while (and
8872                 (progn
8873                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8874                   (gnus-summary-show-thread) t)
8875                 (gnus-summary-find-subject subject))
8876           (setq count (1+ count))))
8877        ((> unmark 0)                    ; Tick.
8878         (while (and
8879                 (progn
8880                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8881                   (gnus-summary-show-thread) t)
8882                 (gnus-summary-find-subject subject))
8883           (setq count (1+ count))))
8884        (t                               ; Mark as unread.
8885         (while (and
8886                 (progn
8887                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8888                   (gnus-summary-show-thread) t)
8889                 (gnus-summary-find-subject subject))
8890           (setq count (1+ count)))))
8891       (gnus-set-mode-line 'summary)
8892       ;; Return the number of marked articles.
8893       count)))
8894
8895 (defun gnus-summary-mark-as-processable (n &optional unmark)
8896   "Set the process mark on the next N articles.
8897 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8898 the process mark instead.  The difference between N and the actual
8899 number of articles marked is returned."
8900   (interactive "P")
8901   (if (and (null n) (gnus-region-active-p))
8902       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8903     (setq n (prefix-numeric-value n))
8904     (let ((backward (< n 0))
8905           (n (abs n)))
8906       (while (and
8907               (> n 0)
8908               (if unmark
8909                   (gnus-summary-remove-process-mark
8910                    (gnus-summary-article-number))
8911                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8912               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8913         (setq n (1- n)))
8914       (when (/= 0 n)
8915         (gnus-message 7 "No more articles"))
8916       (gnus-summary-recenter)
8917       (gnus-summary-position-point)
8918       n)))
8919
8920 (defun gnus-summary-unmark-as-processable (n)
8921   "Remove the process mark from the next N articles.
8922 If N is negative, unmark backward instead.  The difference between N and
8923 the actual number of articles unmarked is returned."
8924   (interactive "P")
8925   (gnus-summary-mark-as-processable n t))
8926
8927 (defun gnus-summary-unmark-all-processable ()
8928   "Remove the process mark from all articles."
8929   (interactive)
8930   (save-excursion
8931     (while gnus-newsgroup-processable
8932       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8933   (gnus-summary-position-point))
8934
8935 (defun gnus-summary-add-mark (article type)
8936   "Mark ARTICLE with a mark of TYPE."
8937   (let ((vtype (car (assq type gnus-article-mark-lists)))
8938         var)
8939     (if (not vtype)
8940         (error "No such mark type: %s" type)
8941       (setq var (intern (format "gnus-newsgroup-%s" type)))
8942       (set var (cons article (symbol-value var)))
8943       (if (memq type '(processable cached replied forwarded recent saved))
8944           (gnus-summary-update-secondary-mark article)
8945         ;;; !!! This is bogus.  We should find out what primary
8946         ;;; !!! mark we want to set.
8947         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8948
8949 (defun gnus-summary-mark-as-expirable (n)
8950   "Mark N articles forward as expirable.
8951 If N is negative, mark backward instead.  The difference between N and
8952 the actual number of articles marked is returned."
8953   (interactive "p")
8954   (gnus-summary-mark-forward n gnus-expirable-mark))
8955
8956 (defun gnus-summary-mark-article-as-replied (article)
8957   "Mark ARTICLE as replied to and update the summary line.
8958 ARTICLE can also be a list of articles."
8959   (interactive (list (gnus-summary-article-number)))
8960   (let ((articles (if (listp article) article (list article))))
8961     (dolist (article articles)
8962       (push article gnus-newsgroup-replied)
8963       (let ((buffer-read-only nil))
8964         (when (gnus-summary-goto-subject article nil t)
8965           (gnus-summary-update-secondary-mark article))))))
8966
8967 (defun gnus-summary-mark-article-as-forwarded (article)
8968   "Mark ARTICLE as forwarded and update the summary line.
8969 ARTICLE can also be a list of articles."
8970   (let ((articles (if (listp article) article (list article))))
8971     (dolist (article articles)
8972       (push article gnus-newsgroup-forwarded)
8973       (let ((buffer-read-only nil))
8974         (when (gnus-summary-goto-subject article nil t)
8975           (gnus-summary-update-secondary-mark article))))))
8976
8977 (defun gnus-summary-set-bookmark (article)
8978   "Set a bookmark in current article."
8979   (interactive (list (gnus-summary-article-number)))
8980   (when (or (not (get-buffer gnus-article-buffer))
8981             (not gnus-current-article)
8982             (not gnus-article-current)
8983             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8984     (error "No current article selected"))
8985   ;; Remove old bookmark, if one exists.
8986   (let ((old (assq article gnus-newsgroup-bookmarks)))
8987     (when old
8988       (setq gnus-newsgroup-bookmarks
8989             (delq old gnus-newsgroup-bookmarks))))
8990   ;; Set the new bookmark, which is on the form
8991   ;; (article-number . line-number-in-body).
8992   (push
8993    (cons article
8994          (save-excursion
8995            (set-buffer gnus-article-buffer)
8996            (count-lines
8997             (min (point)
8998                  (save-excursion
8999                    (goto-char (point-min))
9000                    (search-forward "\n\n" nil t)
9001                    (point)))
9002             (point))))
9003    gnus-newsgroup-bookmarks)
9004   (gnus-message 6 "A bookmark has been added to the current article."))
9005
9006 (defun gnus-summary-remove-bookmark (article)
9007   "Remove the bookmark from the current article."
9008   (interactive (list (gnus-summary-article-number)))
9009   ;; Remove old bookmark, if one exists.
9010   (let ((old (assq article gnus-newsgroup-bookmarks)))
9011     (if old
9012         (progn
9013           (setq gnus-newsgroup-bookmarks
9014                 (delq old gnus-newsgroup-bookmarks))
9015           (gnus-message 6 "Removed bookmark."))
9016       (gnus-message 6 "No bookmark in current article."))))
9017
9018 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9019 (defun gnus-summary-mark-as-dormant (n)
9020   "Mark N articles forward as dormant.
9021 If N is negative, mark backward instead.  The difference between N and
9022 the actual number of articles marked is returned."
9023   (interactive "p")
9024   (gnus-summary-mark-forward n gnus-dormant-mark))
9025
9026 (defun gnus-summary-set-process-mark (article)
9027   "Set the process mark on ARTICLE and update the summary line."
9028   (setq gnus-newsgroup-processable
9029         (cons article
9030               (delq article gnus-newsgroup-processable)))
9031   (when (gnus-summary-goto-subject article)
9032     (gnus-summary-show-thread)
9033     (gnus-summary-goto-subject article)
9034     (gnus-summary-update-secondary-mark article)))
9035
9036 (defun gnus-summary-remove-process-mark (article)
9037   "Remove the process mark from ARTICLE and update the summary line."
9038   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9039   (when (gnus-summary-goto-subject article)
9040     (gnus-summary-show-thread)
9041     (gnus-summary-goto-subject article)
9042     (gnus-summary-update-secondary-mark article)))
9043
9044 (defun gnus-summary-set-saved-mark (article)
9045   "Set the process mark on ARTICLE and update the summary line."
9046   (push article gnus-newsgroup-saved)
9047   (when (gnus-summary-goto-subject article)
9048     (gnus-summary-update-secondary-mark article)))
9049
9050 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9051   "Mark N articles as read forwards.
9052 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9053 The difference between N and the actual number of articles marked is
9054 returned.
9055 Iff NO-EXPIRE, auto-expiry will be inhibited."
9056   (interactive "p")
9057   (gnus-summary-show-thread)
9058   (let ((backward (< n 0))
9059         (gnus-summary-goto-unread
9060          (and gnus-summary-goto-unread
9061               (not (eq gnus-summary-goto-unread 'never))
9062               (not (memq mark (list gnus-unread-mark
9063                                     gnus-ticked-mark gnus-dormant-mark)))))
9064         (n (abs n))
9065         (mark (or mark gnus-del-mark)))
9066     (while (and (> n 0)
9067                 (gnus-summary-mark-article nil mark no-expire)
9068                 (zerop (gnus-summary-next-subject
9069                         (if backward -1 1)
9070                         (and gnus-summary-goto-unread
9071                              (not (eq gnus-summary-goto-unread 'never)))
9072                         t)))
9073       (setq n (1- n)))
9074     (when (/= 0 n)
9075       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9076     (gnus-summary-recenter)
9077     (gnus-summary-position-point)
9078     (gnus-set-mode-line 'summary)
9079     n))
9080
9081 (defun gnus-summary-mark-article-as-read (mark)
9082   "Mark the current article quickly as read with MARK."
9083   (let ((article (gnus-summary-article-number)))
9084     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9085     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9086     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9087     (push (cons article mark) gnus-newsgroup-reads)
9088     ;; Possibly remove from cache, if that is used.
9089     (when gnus-use-cache
9090       (gnus-cache-enter-remove-article article))
9091     ;; Allow the backend to change the mark.
9092     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9093     ;; Check for auto-expiry.
9094     (when (and gnus-newsgroup-auto-expire
9095                (memq mark gnus-auto-expirable-marks))
9096       (setq mark gnus-expirable-mark)
9097       ;; Let the backend know about the mark change.
9098       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9099       (push article gnus-newsgroup-expirable))
9100     ;; Set the mark in the buffer.
9101     (gnus-summary-update-mark mark 'unread)
9102     t))
9103
9104 (defun gnus-summary-mark-article-as-unread (mark)
9105   "Mark the current article quickly as unread with MARK."
9106   (let* ((article (gnus-summary-article-number))
9107          (old-mark (gnus-summary-article-mark article)))
9108     ;; Allow the backend to change the mark.
9109     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9110     (if (eq mark old-mark)
9111         t
9112       (if (<= article 0)
9113           (progn
9114             (gnus-error 1 "Can't mark negative article numbers")
9115             nil)
9116         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9117         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9118         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9119         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9120         (cond ((= mark gnus-ticked-mark)
9121                (push article gnus-newsgroup-marked))
9122               ((= mark gnus-dormant-mark)
9123                (push article gnus-newsgroup-dormant))
9124               (t
9125                (push article gnus-newsgroup-unreads)))
9126         (gnus-pull article gnus-newsgroup-reads)
9127
9128         ;; See whether the article is to be put in the cache.
9129         (and gnus-use-cache
9130              (vectorp (gnus-summary-article-header article))
9131              (save-excursion
9132                (gnus-cache-possibly-enter-article
9133                 gnus-newsgroup-name article
9134                 (= mark gnus-ticked-mark)
9135                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9136
9137         ;; Fix the mark.
9138         (gnus-summary-update-mark mark 'unread)
9139         t))))
9140
9141 (defun gnus-summary-mark-article (&optional article mark no-expire)
9142   "Mark ARTICLE with MARK.  MARK can be any character.
9143 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9144 `??' (dormant) and `?E' (expirable).
9145 If MARK is nil, then the default character `?r' is used.
9146 If ARTICLE is nil, then the article on the current line will be
9147 marked.
9148 Iff NO-EXPIRE, auto-expiry will be inhibited."
9149   ;; The mark might be a string.
9150   (when (stringp mark)
9151     (setq mark (aref mark 0)))
9152   ;; If no mark is given, then we check auto-expiring.
9153   (when (null mark)
9154     (setq mark gnus-del-mark))
9155   (when (and (not no-expire)
9156              gnus-newsgroup-auto-expire
9157              (memq mark gnus-auto-expirable-marks))
9158     (setq mark gnus-expirable-mark))
9159   (let ((article (or article (gnus-summary-article-number)))
9160         (old-mark (gnus-summary-article-mark article)))
9161     ;; Allow the backend to change the mark.
9162     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9163     (if (eq mark old-mark)
9164         t
9165       (unless article
9166         (error "No article on current line"))
9167       (if (not (if (or (= mark gnus-unread-mark)
9168                        (= mark gnus-ticked-mark)
9169                        (= mark gnus-dormant-mark))
9170                    (gnus-mark-article-as-unread article mark)
9171                  (gnus-mark-article-as-read article mark)))
9172           t
9173         ;; See whether the article is to be put in the cache.
9174         (and gnus-use-cache
9175              (not (= mark gnus-canceled-mark))
9176              (vectorp (gnus-summary-article-header article))
9177              (save-excursion
9178                (gnus-cache-possibly-enter-article
9179                 gnus-newsgroup-name article
9180                 (= mark gnus-ticked-mark)
9181                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9182
9183         (when (gnus-summary-goto-subject article nil t)
9184           (let ((buffer-read-only nil))
9185             (gnus-summary-show-thread)
9186             ;; Fix the mark.
9187             (gnus-summary-update-mark mark 'unread)
9188             t))))))
9189
9190 (defun gnus-summary-update-secondary-mark (article)
9191   "Update the secondary (read, process, cache) mark."
9192   (gnus-summary-update-mark
9193    (cond ((memq article gnus-newsgroup-processable)
9194           gnus-process-mark)
9195          ((memq article gnus-newsgroup-cached)
9196           gnus-cached-mark)
9197          ((memq article gnus-newsgroup-replied)
9198           gnus-replied-mark)
9199          ((memq article gnus-newsgroup-forwarded)
9200           gnus-forwarded-mark)
9201          ((memq article gnus-newsgroup-saved)
9202           gnus-saved-mark)
9203          ((memq article gnus-newsgroup-recent)
9204           gnus-recent-mark)
9205          ((memq article gnus-newsgroup-unseen)
9206           gnus-unseen-mark)
9207          (t gnus-no-mark))
9208    'replied)
9209   (when (gnus-visual-p 'summary-highlight 'highlight)
9210     (gnus-run-hooks 'gnus-summary-update-hook))
9211   t)
9212
9213 (defun gnus-summary-update-mark (mark type)
9214   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9215         (buffer-read-only nil))
9216     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9217     (when forward
9218       (when (looking-at "\r")
9219         (incf forward))
9220       (when (<= (+ forward (point)) (point-max))
9221         ;; Go to the right position on the line.
9222         (goto-char (+ forward (point)))
9223         ;; Replace the old mark with the new mark.
9224         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9225         ;; Optionally update the marks by some user rule.
9226         (when (eq type 'unread)
9227           (gnus-data-set-mark
9228            (gnus-data-find (gnus-summary-article-number)) mark)
9229           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9230
9231 (defun gnus-mark-article-as-read (article &optional mark)
9232   "Enter ARTICLE in the pertinent lists and remove it from others."
9233   ;; Make the article expirable.
9234   (let ((mark (or mark gnus-del-mark)))
9235     (if (= mark gnus-expirable-mark)
9236         (push article gnus-newsgroup-expirable)
9237       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9238     ;; Remove from unread and marked lists.
9239     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9240     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9241     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9242     (push (cons article mark) gnus-newsgroup-reads)
9243     ;; Possibly remove from cache, if that is used.
9244     (when gnus-use-cache
9245       (gnus-cache-enter-remove-article article))
9246     t))
9247
9248 (defun gnus-mark-article-as-unread (article &optional mark)
9249   "Enter ARTICLE in the pertinent lists and remove it from others."
9250   (let ((mark (or mark gnus-ticked-mark)))
9251     (if (<= article 0)
9252         (progn
9253           (gnus-error 1 "Can't mark negative article numbers")
9254           nil)
9255       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9256             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9257             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9258             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9259
9260       ;; Unsuppress duplicates?
9261       (when gnus-suppress-duplicates
9262         (gnus-dup-unsuppress-article article))
9263
9264       (cond ((= mark gnus-ticked-mark)
9265              (push article gnus-newsgroup-marked))
9266             ((= mark gnus-dormant-mark)
9267              (push article gnus-newsgroup-dormant))
9268             (t
9269              (push article gnus-newsgroup-unreads)))
9270       (gnus-pull article gnus-newsgroup-reads)
9271       t)))
9272
9273 (defalias 'gnus-summary-mark-as-unread-forward
9274   'gnus-summary-tick-article-forward)
9275 (make-obsolete 'gnus-summary-mark-as-unread-forward
9276                'gnus-summary-tick-article-forward)
9277 (defun gnus-summary-tick-article-forward (n)
9278   "Tick N articles forwards.
9279 If N is negative, tick backwards instead.
9280 The difference between N and the number of articles ticked is returned."
9281   (interactive "p")
9282   (gnus-summary-mark-forward n gnus-ticked-mark))
9283
9284 (defalias 'gnus-summary-mark-as-unread-backward
9285   'gnus-summary-tick-article-backward)
9286 (make-obsolete 'gnus-summary-mark-as-unread-backward
9287                'gnus-summary-tick-article-backward)
9288 (defun gnus-summary-tick-article-backward (n)
9289   "Tick N articles backwards.
9290 The difference between N and the number of articles ticked is returned."
9291   (interactive "p")
9292   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9293
9294 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9295 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9296 (defun gnus-summary-tick-article (&optional article clear-mark)
9297   "Mark current article as unread.
9298 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9299 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9300   (interactive)
9301   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9302                                        gnus-ticked-mark)))
9303
9304 (defun gnus-summary-mark-as-read-forward (n)
9305   "Mark N articles as read forwards.
9306 If N is negative, mark backwards instead.
9307 The difference between N and the actual number of articles marked is
9308 returned."
9309   (interactive "p")
9310   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9311
9312 (defun gnus-summary-mark-as-read-backward (n)
9313   "Mark the N articles as read backwards.
9314 The difference between N and the actual number of articles marked is
9315 returned."
9316   (interactive "p")
9317   (gnus-summary-mark-forward
9318    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9319
9320 (defun gnus-summary-mark-as-read (&optional article mark)
9321   "Mark current article as read.
9322 ARTICLE specifies the article to be marked as read.
9323 MARK specifies a string to be inserted at the beginning of the line."
9324   (gnus-summary-mark-article article mark))
9325
9326 (defun gnus-summary-clear-mark-forward (n)
9327   "Clear marks from N articles forward.
9328 If N is negative, clear backward instead.
9329 The difference between N and the number of marks cleared is returned."
9330   (interactive "p")
9331   (gnus-summary-mark-forward n gnus-unread-mark))
9332
9333 (defun gnus-summary-clear-mark-backward (n)
9334   "Clear marks from N articles backward.
9335 The difference between N and the number of marks cleared is returned."
9336   (interactive "p")
9337   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9338
9339 (defun gnus-summary-mark-unread-as-read ()
9340   "Intended to be used by `gnus-summary-mark-article-hook'."
9341   (when (memq gnus-current-article gnus-newsgroup-unreads)
9342     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9343
9344 (defun gnus-summary-mark-read-and-unread-as-read ()
9345   "Intended to be used by `gnus-summary-mark-article-hook'."
9346   (let ((mark (gnus-summary-article-mark)))
9347     (when (or (gnus-unread-mark-p mark)
9348               (gnus-read-mark-p mark))
9349       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9350
9351 (defun gnus-summary-mark-unread-as-ticked ()
9352   "Intended to be used by `gnus-summary-mark-article-hook'."
9353   (when (memq gnus-current-article gnus-newsgroup-unreads)
9354     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9355
9356 (defun gnus-summary-mark-region-as-read (point mark all)
9357   "Mark all unread articles between point and mark as read.
9358 If given a prefix, mark all articles between point and mark as read,
9359 even ticked and dormant ones."
9360   (interactive "r\nP")
9361   (save-excursion
9362     (let (article)
9363       (goto-char point)
9364       (beginning-of-line)
9365       (while (and
9366               (< (point) mark)
9367               (progn
9368                 (when (or all
9369                           (memq (setq article (gnus-summary-article-number))
9370                                 gnus-newsgroup-unreads))
9371                   (gnus-summary-mark-article article gnus-del-mark))
9372                 t)
9373               (gnus-summary-find-next))))))
9374
9375 (defun gnus-summary-mark-below (score mark)
9376   "Mark articles with score less than SCORE with MARK."
9377   (interactive "P\ncMark: ")
9378   (setq score (if score
9379                   (prefix-numeric-value score)
9380                 (or gnus-summary-default-score 0)))
9381   (save-excursion
9382     (set-buffer gnus-summary-buffer)
9383     (goto-char (point-min))
9384     (while
9385         (progn
9386           (and (< (gnus-summary-article-score) score)
9387                (gnus-summary-mark-article nil mark))
9388           (gnus-summary-find-next)))))
9389
9390 (defun gnus-summary-kill-below (&optional score)
9391   "Mark articles with score below SCORE as read."
9392   (interactive "P")
9393   (gnus-summary-mark-below score gnus-killed-mark))
9394
9395 (defun gnus-summary-clear-above (&optional score)
9396   "Clear all marks from articles with score above SCORE."
9397   (interactive "P")
9398   (gnus-summary-mark-above score gnus-unread-mark))
9399
9400 (defun gnus-summary-tick-above (&optional score)
9401   "Tick all articles with score above SCORE."
9402   (interactive "P")
9403   (gnus-summary-mark-above score gnus-ticked-mark))
9404
9405 (defun gnus-summary-mark-above (score mark)
9406   "Mark articles with score over SCORE with MARK."
9407   (interactive "P\ncMark: ")
9408   (setq score (if score
9409                   (prefix-numeric-value score)
9410                 (or gnus-summary-default-score 0)))
9411   (save-excursion
9412     (set-buffer gnus-summary-buffer)
9413     (goto-char (point-min))
9414     (while (and (progn
9415                   (when (> (gnus-summary-article-score) score)
9416                     (gnus-summary-mark-article nil mark))
9417                   t)
9418                 (gnus-summary-find-next)))))
9419
9420 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9421 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9422 (defun gnus-summary-limit-include-expunged (&optional no-error)
9423   "Display all the hidden articles that were expunged for low scores."
9424   (interactive)
9425   (let ((buffer-read-only nil))
9426     (let ((scored gnus-newsgroup-scored)
9427           headers h)
9428       (while scored
9429         (unless (gnus-summary-article-header (caar scored))
9430           (and (setq h (gnus-number-to-header (caar scored)))
9431                (< (cdar scored) gnus-summary-expunge-below)
9432                (push h headers)))
9433         (setq scored (cdr scored)))
9434       (if (not headers)
9435           (when (not no-error)
9436             (error "No expunged articles hidden"))
9437         (goto-char (point-min))
9438         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9439         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9440         (mapcar (lambda (x) (push (mail-header-number x)
9441                                   gnus-newsgroup-limit))
9442                 headers)
9443         (gnus-summary-prepare-unthreaded (nreverse headers))
9444         (goto-char (point-min))
9445         (gnus-summary-position-point)
9446         t))))
9447
9448 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9449   "Mark all unread articles in this newsgroup as read.
9450 If prefix argument ALL is non-nil, ticked and dormant articles will
9451 also be marked as read.
9452 If QUIETLY is non-nil, no questions will be asked.
9453 If TO-HERE is non-nil, it should be a point in the buffer.  All
9454 articles before (after, if REVERSE is set) this point will be marked as read.
9455 Note that this function will only catch up the unread article
9456 in the current summary buffer limitation.
9457 The number of articles marked as read is returned."
9458   (interactive "P")
9459   (prog1
9460       (save-excursion
9461         (when (or quietly
9462                   (not gnus-interactive-catchup) ;Without confirmation?
9463                   gnus-expert-user
9464                   (gnus-y-or-n-p
9465                    (if all
9466                        "Mark absolutely all articles as read? "
9467                      "Mark all unread articles as read? ")))
9468           (if (and not-mark
9469                    (not gnus-newsgroup-adaptive)
9470                    (not gnus-newsgroup-auto-expire)
9471                    (not gnus-suppress-duplicates)
9472                    (or (not gnus-use-cache)
9473                        (eq gnus-use-cache 'passive)))
9474               (progn
9475                 (when all
9476                   (setq gnus-newsgroup-marked nil
9477                         gnus-newsgroup-dormant nil))
9478                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9479             ;; We actually mark all articles as canceled, which we
9480             ;; have to do when using auto-expiry or adaptive scoring.
9481             (gnus-summary-show-all-threads)
9482             (if (and to-here reverse)
9483                 (progn
9484                   (goto-char to-here)
9485                   (while (and
9486                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9487                           (gnus-summary-find-next (not all) nil nil t))))
9488               (when (gnus-summary-first-subject (not all) t)
9489                 (while (and
9490                         (if to-here (< (point) to-here) t)
9491                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9492                         (gnus-summary-find-next (not all) nil nil t)))))
9493             (gnus-set-mode-line 'summary))
9494           t))
9495     (gnus-summary-position-point)))
9496
9497 (defun gnus-summary-catchup-to-here (&optional all)
9498   "Mark all unticked articles before the current one as read.
9499 If ALL is non-nil, also mark ticked and dormant articles as read."
9500   (interactive "P")
9501   (save-excursion
9502     (gnus-save-hidden-threads
9503       (let ((beg (point)))
9504         ;; We check that there are unread articles.
9505         (when (or all (gnus-summary-find-prev))
9506           (gnus-summary-catchup all t beg)))))
9507   (gnus-summary-position-point))
9508
9509 (defun gnus-summary-catchup-from-here (&optional all)
9510   "Mark all unticked articles after the current one as read.
9511 If ALL is non-nil, also mark ticked and dormant articles as read."
9512   (interactive "P")
9513   (save-excursion
9514     (gnus-save-hidden-threads
9515       (let ((beg (point)))
9516         ;; We check that there are unread articles.
9517         (when (or all (gnus-summary-find-next))
9518           (gnus-summary-catchup all t beg nil t)))))
9519
9520   (gnus-summary-position-point))
9521 (defun gnus-summary-catchup-all (&optional quietly)
9522   "Mark all articles in this newsgroup as read."
9523   (interactive "P")
9524   (gnus-summary-catchup t quietly))
9525
9526 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9527   "Mark all unread articles in this group as read, then exit.
9528 If prefix argument ALL is non-nil, all articles are marked as read.
9529 If QUIETLY is non-nil, no questions will be asked."
9530   (interactive "P")
9531   (when (gnus-summary-catchup all quietly nil 'fast)
9532     ;; Select next newsgroup or exit.
9533     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9534              (eq gnus-auto-select-next 'quietly))
9535         (gnus-summary-next-group nil)
9536       (gnus-summary-exit))))
9537
9538 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9539   "Mark all articles in this newsgroup as read, and then exit."
9540   (interactive "P")
9541   (gnus-summary-catchup-and-exit t quietly))
9542
9543 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9544   "Mark all articles in this group as read and select the next group.
9545 If given a prefix, mark all articles, unread as well as ticked, as
9546 read."
9547   (interactive "P")
9548   (save-excursion
9549     (gnus-summary-catchup all))
9550   (gnus-summary-next-group))
9551
9552 ;;;
9553 ;;; with article
9554 ;;;
9555
9556 (defmacro gnus-with-article (article &rest forms)
9557   "Select ARTICLE and perform FORMS in the original article buffer.
9558 Then replace the article with the result."
9559   `(progn
9560      ;; We don't want the article to be marked as read.
9561      (let (gnus-mark-article-hook)
9562        (gnus-summary-select-article t t nil ,article))
9563      (set-buffer gnus-original-article-buffer)
9564      ,@forms
9565      (if (not (gnus-check-backend-function
9566                'request-replace-article (car gnus-article-current)))
9567          (gnus-message 5 "Read-only group; not replacing")
9568        (unless (gnus-request-replace-article
9569                 ,article (car gnus-article-current)
9570                 (current-buffer) t)
9571          (error "Couldn't replace article")))
9572      ;; The cache and backlog have to be flushed somewhat.
9573      (when gnus-keep-backlog
9574        (gnus-backlog-remove-article
9575         (car gnus-article-current) (cdr gnus-article-current)))
9576      (when gnus-use-cache
9577        (gnus-cache-update-article
9578         (car gnus-article-current) (cdr gnus-article-current)))))
9579
9580 (put 'gnus-with-article 'lisp-indent-function 1)
9581 (put 'gnus-with-article 'edebug-form-spec '(form body))
9582
9583 ;; Thread-based commands.
9584
9585 (defun gnus-summary-articles-in-thread (&optional article)
9586   "Return a list of all articles in the current thread.
9587 If ARTICLE is non-nil, return all articles in the thread that starts
9588 with that article."
9589   (let* ((article (or article (gnus-summary-article-number)))
9590          (data (gnus-data-find-list article))
9591          (top-level (gnus-data-level (car data)))
9592          (top-subject
9593           (cond ((null gnus-thread-operation-ignore-subject)
9594                  (gnus-simplify-subject-re
9595                   (mail-header-subject (gnus-data-header (car data)))))
9596                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9597                  (gnus-simplify-subject-fuzzy
9598                   (mail-header-subject (gnus-data-header (car data)))))
9599                 (t nil)))
9600          (end-point (save-excursion
9601                       (if (gnus-summary-go-to-next-thread)
9602                           (point) (point-max))))
9603          articles)
9604     (while (and data
9605                 (< (gnus-data-pos (car data)) end-point))
9606       (when (or (not top-subject)
9607                 (string= top-subject
9608                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9609                              (gnus-simplify-subject-fuzzy
9610                               (mail-header-subject
9611                                (gnus-data-header (car data))))
9612                            (gnus-simplify-subject-re
9613                             (mail-header-subject
9614                              (gnus-data-header (car data)))))))
9615         (push (gnus-data-number (car data)) articles))
9616       (unless (and (setq data (cdr data))
9617                    (> (gnus-data-level (car data)) top-level))
9618         (setq data nil)))
9619     ;; Return the list of articles.
9620     (nreverse articles)))
9621
9622 (defun gnus-summary-rethread-current ()
9623   "Rethread the thread the current article is part of."
9624   (interactive)
9625   (let* ((gnus-show-threads t)
9626          (article (gnus-summary-article-number))
9627          (id (mail-header-id (gnus-summary-article-header)))
9628          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9629     (unless id
9630       (error "No article on the current line"))
9631     (gnus-rebuild-thread id)
9632     (gnus-summary-goto-subject article)))
9633
9634 (defun gnus-summary-reparent-thread ()
9635   "Make the current article child of the marked (or previous) article.
9636
9637 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9638 is non-nil or the Subject: of both articles are the same."
9639   (interactive)
9640   (unless (not (gnus-group-read-only-p))
9641     (error "The current newsgroup does not support article editing"))
9642   (unless (<= (length gnus-newsgroup-processable) 1)
9643     (error "No more than one article may be marked"))
9644   (save-window-excursion
9645     (let ((gnus-article-buffer " *reparent*")
9646           (current-article (gnus-summary-article-number))
9647           ;; First grab the marked article, otherwise one line up.
9648           (parent-article (if (not (null gnus-newsgroup-processable))
9649                               (car gnus-newsgroup-processable)
9650                             (save-excursion
9651                               (if (eq (forward-line -1) 0)
9652                                   (gnus-summary-article-number)
9653                                 (error "Beginning of summary buffer"))))))
9654       (unless (not (eq current-article parent-article))
9655         (error "An article may not be self-referential"))
9656       (let ((message-id (mail-header-id
9657                          (gnus-summary-article-header parent-article))))
9658         (unless (and message-id (not (equal message-id "")))
9659           (error "No message-id in desired parent"))
9660         (gnus-with-article current-article
9661           (save-restriction
9662             (goto-char (point-min))
9663             (message-narrow-to-head)
9664             (if (re-search-forward "^References: " nil t)
9665                 (progn
9666                   (re-search-forward "^[^ \t]" nil t)
9667                   (forward-line -1)
9668                   (end-of-line)
9669                   (insert " " message-id))
9670               (insert "References: " message-id "\n"))))
9671         (set-buffer gnus-summary-buffer)
9672         (gnus-summary-unmark-all-processable)
9673         (gnus-summary-update-article current-article)
9674         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9675             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9676         (gnus-summary-rethread-current)
9677         (gnus-message 3 "Article %d is now the child of article %d"
9678                       current-article parent-article)))))
9679
9680 (defun gnus-summary-toggle-threads (&optional arg)
9681   "Toggle showing conversation threads.
9682 If ARG is positive number, turn showing conversation threads on."
9683   (interactive "P")
9684   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9685     (setq gnus-show-threads
9686           (if (null arg) (not gnus-show-threads)
9687             (> (prefix-numeric-value arg) 0)))
9688     (gnus-summary-prepare)
9689     (gnus-summary-goto-subject current)
9690     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9691     (gnus-summary-position-point)))
9692
9693 (defun gnus-summary-show-all-threads ()
9694   "Show all threads."
9695   (interactive)
9696   (save-excursion
9697     (let ((buffer-read-only nil))
9698       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9699   (gnus-summary-position-point))
9700
9701 (defun gnus-summary-show-thread ()
9702   "Show thread subtrees.
9703 Returns nil if no thread was there to be shown."
9704   (interactive)
9705   (let ((buffer-read-only nil)
9706         (orig (point))
9707         ;; first goto end then to beg, to have point at beg after let
9708         (end (progn (end-of-line) (point)))
9709         (beg (progn (beginning-of-line) (point))))
9710     (prog1
9711         ;; Any hidden lines here?
9712         (search-forward "\r" end t)
9713       (subst-char-in-region beg end ?\^M ?\n t)
9714       (goto-char orig)
9715       (gnus-summary-position-point))))
9716
9717 (defun gnus-summary-maybe-hide-threads ()
9718   "If requested, hide the threads that should be hidden."
9719   (when (and gnus-show-threads
9720              gnus-thread-hide-subtree)
9721     (gnus-summary-hide-all-threads
9722      (if (or (consp gnus-thread-hide-subtree)
9723              (gnus-functionp gnus-thread-hide-subtree))
9724          (gnus-make-predicate gnus-thread-hide-subtree)
9725        nil))))
9726
9727 ;;; Hiding predicates.
9728
9729 (defun gnus-article-unread-p (header)
9730   (memq (mail-header-number header) gnus-newsgroup-unreads))
9731
9732 (defun gnus-article-unseen-p (header)
9733   (memq (mail-header-number header) gnus-newsgroup-unseen))
9734
9735 (defun gnus-map-articles (predicate articles)
9736   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
9737   (apply 'gnus-or (mapcar predicate
9738                           (mapcar 'gnus-summary-article-header articles))))
9739
9740 (defun gnus-summary-hide-all-threads (&optional predicate)
9741   "Hide all thread subtrees.
9742 If PREDICATE is supplied, threads that satisfy this predicate
9743 will not be hidden."
9744   (interactive)
9745   (save-excursion
9746     (goto-char (point-min))
9747     (let ((end nil))
9748       (while (not end)
9749         (when (or (not predicate)
9750                   (gnus-map-articles
9751                    predicate (gnus-summary-article-children)))
9752             (gnus-summary-hide-thread))
9753         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
9754   (gnus-summary-position-point))
9755
9756 (defun gnus-summary-hide-thread ()
9757   "Hide thread subtrees.
9758 If PREDICATE is supplied, threads that satisfy this predicate
9759 will not be hidden.
9760 Returns nil if no threads were there to be hidden."
9761   (interactive)
9762   (let ((buffer-read-only nil)
9763         (start (point))
9764         (article (gnus-summary-article-number)))
9765     (goto-char start)
9766     ;; Go forward until either the buffer ends or the subthread
9767     ;; ends.
9768     (when (and (not (eobp))
9769                (or (zerop (gnus-summary-next-thread 1 t))
9770                    (goto-char (point-max))))
9771       (prog1
9772           (if (and (> (point) start)
9773                    (search-backward "\n" start t))
9774               (progn
9775                 (subst-char-in-region start (point) ?\n ?\^M)
9776                 (gnus-summary-goto-subject article))
9777             (goto-char start)
9778             nil)))))
9779
9780 (defun gnus-summary-go-to-next-thread (&optional previous)
9781   "Go to the same level (or less) next thread.
9782 If PREVIOUS is non-nil, go to previous thread instead.
9783 Return the article number moved to, or nil if moving was impossible."
9784   (let ((level (gnus-summary-thread-level))
9785         (way (if previous -1 1))
9786         (beg (point)))
9787     (forward-line way)
9788     (while (and (not (eobp))
9789                 (< level (gnus-summary-thread-level)))
9790       (forward-line way))
9791     (if (eobp)
9792         (progn
9793           (goto-char beg)
9794           nil)
9795       (setq beg (point))
9796       (prog1
9797           (gnus-summary-article-number)
9798         (goto-char beg)))))
9799
9800 (defun gnus-summary-next-thread (n &optional silent)
9801   "Go to the same level next N'th thread.
9802 If N is negative, search backward instead.
9803 Returns the difference between N and the number of skips actually
9804 done.
9805
9806 If SILENT, don't output messages."
9807   (interactive "p")
9808   (let ((backward (< n 0))
9809         (n (abs n)))
9810     (while (and (> n 0)
9811                 (gnus-summary-go-to-next-thread backward))
9812       (decf n))
9813     (unless silent
9814       (gnus-summary-position-point))
9815     (when (and (not silent) (/= 0 n))
9816       (gnus-message 7 "No more threads"))
9817     n))
9818
9819 (defun gnus-summary-prev-thread (n)
9820   "Go to the same level previous N'th thread.
9821 Returns the difference between N and the number of skips actually
9822 done."
9823   (interactive "p")
9824   (gnus-summary-next-thread (- n)))
9825
9826 (defun gnus-summary-go-down-thread ()
9827   "Go down one level in the current thread."
9828   (let ((children (gnus-summary-article-children)))
9829     (when children
9830       (gnus-summary-goto-subject (car children)))))
9831
9832 (defun gnus-summary-go-up-thread ()
9833   "Go up one level in the current thread."
9834   (let ((parent (gnus-summary-article-parent)))
9835     (when parent
9836       (gnus-summary-goto-subject parent))))
9837
9838 (defun gnus-summary-down-thread (n)
9839   "Go down thread N steps.
9840 If N is negative, go up instead.
9841 Returns the difference between N and how many steps down that were
9842 taken."
9843   (interactive "p")
9844   (let ((up (< n 0))
9845         (n (abs n)))
9846     (while (and (> n 0)
9847                 (if up (gnus-summary-go-up-thread)
9848                   (gnus-summary-go-down-thread)))
9849       (setq n (1- n)))
9850     (gnus-summary-position-point)
9851     (when (/= 0 n)
9852       (gnus-message 7 "Can't go further"))
9853     n))
9854
9855 (defun gnus-summary-up-thread (n)
9856   "Go up thread N steps.
9857 If N is negative, go down instead.
9858 Returns the difference between N and how many steps down that were
9859 taken."
9860   (interactive "p")
9861   (gnus-summary-down-thread (- n)))
9862
9863 (defun gnus-summary-top-thread ()
9864   "Go to the top of the thread."
9865   (interactive)
9866   (while (gnus-summary-go-up-thread))
9867   (gnus-summary-article-number))
9868
9869 (defun gnus-summary-kill-thread (&optional unmark)
9870   "Mark articles under current thread as read.
9871 If the prefix argument is positive, remove any kinds of marks.
9872 If the prefix argument is negative, tick articles instead."
9873   (interactive "P")
9874   (when unmark
9875     (setq unmark (prefix-numeric-value unmark)))
9876   (let ((articles (gnus-summary-articles-in-thread)))
9877     (save-excursion
9878       ;; Expand the thread.
9879       (gnus-summary-show-thread)
9880       ;; Mark all the articles.
9881       (while articles
9882         (gnus-summary-goto-subject (car articles))
9883         (cond ((null unmark)
9884                (gnus-summary-mark-article-as-read gnus-killed-mark))
9885               ((> unmark 0)
9886                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9887               (t
9888                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9889         (setq articles (cdr articles))))
9890     ;; Hide killed subtrees.
9891     (and (null unmark)
9892          gnus-thread-hide-killed
9893          (gnus-summary-hide-thread))
9894     ;; If marked as read, go to next unread subject.
9895     (when (null unmark)
9896       ;; Go to next unread subject.
9897       (gnus-summary-next-subject 1 t)))
9898   (gnus-set-mode-line 'summary))
9899
9900 ;; Summary sorting commands
9901
9902 (defun gnus-summary-sort-by-number (&optional reverse)
9903   "Sort the summary buffer by article number.
9904 Argument REVERSE means reverse order."
9905   (interactive "P")
9906   (gnus-summary-sort 'number reverse))
9907
9908 (defun gnus-summary-sort-by-author (&optional reverse)
9909   "Sort the summary buffer by author name alphabetically.
9910 If `case-fold-search' is non-nil, case of letters is ignored.
9911 Argument REVERSE means reverse order."
9912   (interactive "P")
9913   (gnus-summary-sort 'author reverse))
9914
9915 (defun gnus-summary-sort-by-subject (&optional reverse)
9916   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9917 If `case-fold-search' is non-nil, case of letters is ignored.
9918 Argument REVERSE means reverse order."
9919   (interactive "P")
9920   (gnus-summary-sort 'subject reverse))
9921
9922 (defun gnus-summary-sort-by-date (&optional reverse)
9923   "Sort the summary buffer by date.
9924 Argument REVERSE means reverse order."
9925   (interactive "P")
9926   (gnus-summary-sort 'date reverse))
9927
9928 (defun gnus-summary-sort-by-score (&optional reverse)
9929   "Sort the summary buffer by score.
9930 Argument REVERSE means reverse order."
9931   (interactive "P")
9932   (gnus-summary-sort 'score reverse))
9933
9934 (defun gnus-summary-sort-by-lines (&optional reverse)
9935   "Sort the summary buffer by the number of lines.
9936 Argument REVERSE means reverse order."
9937   (interactive "P")
9938   (gnus-summary-sort 'lines reverse))
9939
9940 (defun gnus-summary-sort-by-chars (&optional reverse)
9941   "Sort the summary buffer by article length.
9942 Argument REVERSE means reverse order."
9943   (interactive "P")
9944   (gnus-summary-sort 'chars reverse))
9945
9946 (defun gnus-summary-sort-by-original (&optional reverse)
9947   "Sort the summary buffer using the default sorting method.
9948 Argument REVERSE means reverse order."
9949   (interactive "P")
9950   (let* ((buffer-read-only)
9951          (gnus-summary-prepare-hook nil))
9952     ;; We do the sorting by regenerating the threads.
9953     (gnus-summary-prepare)
9954     ;; Hide subthreads if needed.
9955     (gnus-summary-maybe-hide-threads)))
9956
9957 (defun gnus-summary-sort (predicate reverse)
9958   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9959   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9960          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9961          (gnus-thread-sort-functions
9962           (if (not reverse)
9963               thread
9964             `(lambda (t1 t2)
9965                (,thread t2 t1))))
9966          (gnus-sort-gathered-threads-function
9967           gnus-thread-sort-functions)
9968          (gnus-article-sort-functions
9969           (if (not reverse)
9970               article
9971             `(lambda (t1 t2)
9972                (,article t2 t1))))
9973          (buffer-read-only)
9974          (gnus-summary-prepare-hook nil))
9975     ;; We do the sorting by regenerating the threads.
9976     (gnus-summary-prepare)
9977     ;; Hide subthreads if needed.
9978     (gnus-summary-maybe-hide-threads)))
9979
9980 ;; Summary saving commands.
9981
9982 (defun gnus-summary-save-article (&optional n not-saved)
9983   "Save the current article using the default saver function.
9984 If N is a positive number, save the N next articles.
9985 If N is a negative number, save the N previous articles.
9986 If N is nil and any articles have been marked with the process mark,
9987 save those articles instead.
9988 The variable `gnus-default-article-saver' specifies the saver function."
9989   (interactive "P")
9990   (let* ((articles (gnus-summary-work-articles n))
9991          (save-buffer (save-excursion
9992                         (nnheader-set-temp-buffer " *Gnus Save*")))
9993          (num (length articles))
9994          header file)
9995     (dolist (article articles)
9996       (setq header (gnus-summary-article-header article))
9997       (if (not (vectorp header))
9998           ;; This is a pseudo-article.
9999           (if (assq 'name header)
10000               (gnus-copy-file (cdr (assq 'name header)))
10001             (gnus-message 1 "Article %d is unsaveable" article))
10002         ;; This is a real article.
10003         (save-window-excursion
10004           (let ((gnus-display-mime-function nil)
10005                 (gnus-article-prepare-hook nil))
10006             (gnus-summary-select-article t nil nil article)))
10007         (save-excursion
10008           (set-buffer save-buffer)
10009           (erase-buffer)
10010           (insert-buffer-substring gnus-original-article-buffer))
10011         (setq file (gnus-article-save save-buffer file num))
10012         (gnus-summary-remove-process-mark article)
10013         (unless not-saved
10014           (gnus-summary-set-saved-mark article))))
10015     (gnus-kill-buffer save-buffer)
10016     (gnus-summary-position-point)
10017     (gnus-set-mode-line 'summary)
10018     n))
10019
10020 (defun gnus-summary-pipe-output (&optional arg)
10021   "Pipe the current article to a subprocess.
10022 If N is a positive number, pipe the N next articles.
10023 If N is a negative number, pipe the N previous articles.
10024 If N is nil and any articles have been marked with the process mark,
10025 pipe those articles instead."
10026   (interactive "P")
10027   (require 'gnus-art)
10028   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10029     (gnus-summary-save-article arg t))
10030   (let ((buffer (get-buffer "*Shell Command Output*")))
10031     (if (and buffer
10032              (with-current-buffer buffer (> (point-max) (point-min))))
10033         (gnus-configure-windows 'pipe))))
10034
10035 (defun gnus-summary-save-article-mail (&optional arg)
10036   "Append the current article to an mail file.
10037 If N is a positive number, save the N next articles.
10038 If N is a negative number, save the N previous articles.
10039 If N is nil and any articles have been marked with the process mark,
10040 save those articles instead."
10041   (interactive "P")
10042   (require 'gnus-art)
10043   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10044     (gnus-summary-save-article arg)))
10045
10046 (defun gnus-summary-save-article-rmail (&optional arg)
10047   "Append the current article to an rmail file.
10048 If N is a positive number, save the N next articles.
10049 If N is a negative number, save the N previous articles.
10050 If N is nil and any articles have been marked with the process mark,
10051 save those articles instead."
10052   (interactive "P")
10053   (require 'gnus-art)
10054   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10055     (gnus-summary-save-article arg)))
10056
10057 (defun gnus-summary-save-article-file (&optional arg)
10058   "Append the current article to a file.
10059 If N is a positive number, save the N next articles.
10060 If N is a negative number, save the N previous articles.
10061 If N is nil and any articles have been marked with the process mark,
10062 save those articles instead."
10063   (interactive "P")
10064   (require 'gnus-art)
10065   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10066     (gnus-summary-save-article arg)))
10067
10068 (defun gnus-summary-write-article-file (&optional arg)
10069   "Write the current article to a file, deleting the previous file.
10070 If N is a positive number, save the N next articles.
10071 If N is a negative number, save the N previous articles.
10072 If N is nil and any articles have been marked with the process mark,
10073 save those articles instead."
10074   (interactive "P")
10075   (require 'gnus-art)
10076   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10077     (gnus-summary-save-article arg)))
10078
10079 (defun gnus-summary-save-article-body-file (&optional arg)
10080   "Append the current article body to a file.
10081 If N is a positive number, save the N next articles.
10082 If N is a negative number, save the N previous articles.
10083 If N is nil and any articles have been marked with the process mark,
10084 save those articles instead."
10085   (interactive "P")
10086   (require 'gnus-art)
10087   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10088     (gnus-summary-save-article arg)))
10089
10090 (defun gnus-summary-muttprint (&optional arg)
10091   "Print the current article using Muttprint.
10092 If N is a positive number, save the N next articles.
10093 If N is a negative number, save the N previous articles.
10094 If N is nil and any articles have been marked with the process mark,
10095 save those articles instead."
10096   (interactive "P")
10097   (require 'gnus-art)
10098   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10099     (gnus-summary-save-article arg t)))
10100
10101 (defun gnus-summary-pipe-message (program)
10102   "Pipe the current article through PROGRAM."
10103   (interactive "sProgram: ")
10104   (gnus-summary-select-article)
10105   (let ((mail-header-separator ""))
10106     (gnus-eval-in-buffer-window gnus-article-buffer
10107       (save-restriction
10108         (widen)
10109         (let ((start (window-start))
10110               buffer-read-only)
10111           (message-pipe-buffer-body program)
10112           (set-window-start (get-buffer-window (current-buffer)) start))))))
10113
10114 (defun gnus-get-split-value (methods)
10115   "Return a value based on the split METHODS."
10116   (let (split-name method result match)
10117     (when methods
10118       (save-excursion
10119         (set-buffer gnus-original-article-buffer)
10120         (save-restriction
10121           (nnheader-narrow-to-headers)
10122           (while (and methods (not split-name))
10123             (goto-char (point-min))
10124             (setq method (pop methods))
10125             (setq match (car method))
10126             (when (cond
10127                    ((stringp match)
10128                     ;; Regular expression.
10129                     (ignore-errors
10130                       (re-search-forward match nil t)))
10131                    ((gnus-functionp match)
10132                     ;; Function.
10133                     (save-restriction
10134                       (widen)
10135                       (setq result (funcall match gnus-newsgroup-name))))
10136                    ((consp match)
10137                     ;; Form.
10138                     (save-restriction
10139                       (widen)
10140                       (setq result (eval match)))))
10141               (setq split-name (cdr method))
10142               (cond ((stringp result)
10143                      (push (expand-file-name
10144                             result gnus-article-save-directory)
10145                            split-name))
10146                     ((consp result)
10147                      (setq split-name (append result split-name)))))))))
10148     (nreverse split-name)))
10149
10150 (defun gnus-valid-move-group-p (group)
10151   (and (boundp group)
10152        (symbol-name group)
10153        (symbol-value group)
10154        (gnus-get-function (gnus-find-method-for-group
10155                            (symbol-name group)) 'request-accept-article t)))
10156
10157 (defun gnus-read-move-group-name (prompt default articles prefix)
10158   "Read a group name."
10159   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10160          (minibuffer-confirm-incomplete nil) ; XEmacs
10161          (prom
10162           (format "%s %s to:"
10163                   prompt
10164                   (if (> (length articles) 1)
10165                       (format "these %d articles" (length articles))
10166                     "this article")))
10167          (to-newsgroup
10168           (cond
10169            ((null split-name)
10170             (gnus-completing-read default prom
10171                                   gnus-active-hashtb
10172                                   'gnus-valid-move-group-p
10173                                   nil prefix
10174                                   'gnus-group-history))
10175            ((= 1 (length split-name))
10176             (gnus-completing-read (car split-name) prom
10177                                   gnus-active-hashtb
10178                                   'gnus-valid-move-group-p
10179                                   nil nil
10180                                   'gnus-group-history))
10181            (t
10182             (gnus-completing-read nil prom
10183                                   (mapcar (lambda (el) (list el))
10184                                           (nreverse split-name))
10185                                   nil nil nil
10186                                   'gnus-group-history))))
10187          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10188     (when to-newsgroup
10189       (if (or (string= to-newsgroup "")
10190               (string= to-newsgroup prefix))
10191           (setq to-newsgroup default))
10192       (unless to-newsgroup
10193         (error "No group name entered"))
10194       (or (gnus-active to-newsgroup)
10195           (gnus-activate-group to-newsgroup nil nil to-method)
10196           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10197                                      to-newsgroup))
10198               (or (and (gnus-request-create-group to-newsgroup to-method)
10199                        (gnus-activate-group
10200                         to-newsgroup nil nil to-method)
10201                        (gnus-subscribe-group to-newsgroup))
10202                   (error "Couldn't create group %s" to-newsgroup)))
10203           (error "No such group: %s" to-newsgroup)))
10204     to-newsgroup))
10205
10206 (defun gnus-summary-save-parts (type dir n &optional reverse)
10207   "Save parts matching TYPE to DIR.
10208 If REVERSE, save parts that do not match TYPE."
10209   (interactive
10210    (list (read-string "Save parts of type: "
10211                       (or (car gnus-summary-save-parts-type-history)
10212                           gnus-summary-save-parts-default-mime)
10213                       'gnus-summary-save-parts-type-history)
10214          (setq gnus-summary-save-parts-last-directory
10215                (read-file-name "Save to directory: "
10216                                gnus-summary-save-parts-last-directory
10217                                nil t))
10218          current-prefix-arg))
10219   (gnus-summary-iterate n
10220     (let ((gnus-display-mime-function nil)
10221           (gnus-inhibit-treatment t))
10222       (gnus-summary-select-article))
10223     (save-excursion
10224       (set-buffer gnus-article-buffer)
10225       (let ((handles (or gnus-article-mime-handles
10226                          (mm-dissect-buffer) (mm-uu-dissect))))
10227         (when handles
10228           (gnus-summary-save-parts-1 type dir handles reverse)
10229           (unless gnus-article-mime-handles ;; Don't destroy this case.
10230             (mm-destroy-parts handles)))))))
10231
10232 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10233   (if (stringp (car handle))
10234       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10235               (cdr handle))
10236     (when (if reverse
10237               (not (string-match type (mm-handle-media-type handle)))
10238             (string-match type (mm-handle-media-type handle)))
10239       (let ((file (expand-file-name
10240                    (file-name-nondirectory
10241                     (or
10242                      (mail-content-type-get
10243                       (mm-handle-disposition handle) 'filename)
10244                      (concat gnus-newsgroup-name
10245                              "." (number-to-string
10246                                   (cdr gnus-article-current)))))
10247                    dir)))
10248         (unless (file-exists-p file)
10249           (mm-save-part-to-file handle file))))))
10250
10251 ;; Summary extract commands
10252
10253 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10254   (let ((buffer-read-only nil)
10255         (article (gnus-summary-article-number))
10256         after-article b e)
10257     (unless (gnus-summary-goto-subject article)
10258       (error "No such article: %d" article))
10259     (gnus-summary-position-point)
10260     ;; If all commands are to be bunched up on one line, we collect
10261     ;; them here.
10262     (unless gnus-view-pseudos-separately
10263       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10264             files action)
10265         (while ps
10266           (setq action (cdr (assq 'action (car ps))))
10267           (setq files (list (cdr (assq 'name (car ps)))))
10268           (while (and ps (cdr ps)
10269                       (string= (or action "1")
10270                                (or (cdr (assq 'action (cadr ps))) "2")))
10271             (push (cdr (assq 'name (cadr ps))) files)
10272             (setcdr ps (cddr ps)))
10273           (when files
10274             (when (not (string-match "%s" action))
10275               (push " " files))
10276             (push " " files)
10277             (when (assq 'execute (car ps))
10278               (setcdr (assq 'execute (car ps))
10279                       (funcall (if (string-match "%s" action)
10280                                    'format 'concat)
10281                                action
10282                                (mapconcat
10283                                 (lambda (f)
10284                                   (if (equal f " ")
10285                                       f
10286                                     (mm-quote-arg f)))
10287                                 files " ")))))
10288           (setq ps (cdr ps)))))
10289     (if (and gnus-view-pseudos (not not-view))
10290         (while pslist
10291           (when (assq 'execute (car pslist))
10292             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10293                                   (eq gnus-view-pseudos 'not-confirm)))
10294           (setq pslist (cdr pslist)))
10295       (save-excursion
10296         (while pslist
10297           (setq after-article (or (cdr (assq 'article (car pslist)))
10298                                   (gnus-summary-article-number)))
10299           (gnus-summary-goto-subject after-article)
10300           (forward-line 1)
10301           (setq b (point))
10302           (insert "    " (file-name-nondirectory
10303                           (cdr (assq 'name (car pslist))))
10304                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10305           (setq e (point))
10306           (forward-line -1)             ; back to `b'
10307           (gnus-add-text-properties
10308            b (1- e) (list 'gnus-number gnus-reffed-article-number
10309                           gnus-mouse-face-prop gnus-mouse-face))
10310           (gnus-data-enter
10311            after-article gnus-reffed-article-number
10312            gnus-unread-mark b (car pslist) 0 (- e b))
10313           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10314           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10315           (setq pslist (cdr pslist)))))))
10316
10317 (defun gnus-pseudos< (p1 p2)
10318   (let ((c1 (cdr (assq 'action p1)))
10319         (c2 (cdr (assq 'action p2))))
10320     (and c1 c2 (string< c1 c2))))
10321
10322 (defun gnus-request-pseudo-article (props)
10323   (cond ((assq 'execute props)
10324          (gnus-execute-command (cdr (assq 'execute props)))))
10325   (let ((gnus-current-article (gnus-summary-article-number)))
10326     (gnus-run-hooks 'gnus-mark-article-hook)))
10327
10328 (defun gnus-execute-command (command &optional automatic)
10329   (save-excursion
10330     (gnus-article-setup-buffer)
10331     (set-buffer gnus-article-buffer)
10332     (setq buffer-read-only nil)
10333     (let ((command (if automatic command
10334                      (read-string "Command: " (cons command 0)))))
10335       (erase-buffer)
10336       (insert "$ " command "\n\n")
10337       (if gnus-view-pseudo-asynchronously
10338           (start-process "gnus-execute" (current-buffer) shell-file-name
10339                          shell-command-switch command)
10340         (call-process shell-file-name nil t nil
10341                       shell-command-switch command)))))
10342
10343 ;; Summary kill commands.
10344
10345 (defun gnus-summary-edit-global-kill (article)
10346   "Edit the \"global\" kill file."
10347   (interactive (list (gnus-summary-article-number)))
10348   (gnus-group-edit-global-kill article))
10349
10350 (defun gnus-summary-edit-local-kill ()
10351   "Edit a local kill file applied to the current newsgroup."
10352   (interactive)
10353   (setq gnus-current-headers (gnus-summary-article-header))
10354   (gnus-group-edit-local-kill
10355    (gnus-summary-article-number) gnus-newsgroup-name))
10356
10357 ;;; Header reading.
10358
10359 (defun gnus-read-header (id &optional header)
10360   "Read the headers of article ID and enter them into the Gnus system."
10361   (let ((group gnus-newsgroup-name)
10362         (gnus-override-method
10363          (or
10364           gnus-override-method
10365           (and (gnus-news-group-p gnus-newsgroup-name)
10366                (car (gnus-refer-article-methods)))))
10367         where)
10368     ;; First we check to see whether the header in question is already
10369     ;; fetched.
10370     (if (stringp id)
10371         ;; This is a Message-ID.
10372         (setq header (or header (gnus-id-to-header id)))
10373       ;; This is an article number.
10374       (setq header (or header (gnus-summary-article-header id))))
10375     (if (and header
10376              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10377         ;; We have found the header.
10378         header
10379       ;; If this is a sparse article, we have to nix out its
10380       ;; previous entry in the thread hashtb.
10381       (when (and header
10382                  (gnus-summary-article-sparse-p (mail-header-number header)))
10383         (let* ((parent (gnus-parent-id (mail-header-references header)))
10384                (thread (and parent (gnus-id-to-thread parent))))
10385           (when thread
10386             (delq (assq header thread) thread))))
10387       ;; We have to really fetch the header to this article.
10388       (save-excursion
10389         (set-buffer nntp-server-buffer)
10390         (when (setq where (gnus-request-head id group))
10391           (nnheader-fold-continuation-lines)
10392           (goto-char (point-max))
10393           (insert ".\n")
10394           (goto-char (point-min))
10395           (insert "211 ")
10396           (princ (cond
10397                   ((numberp id) id)
10398                   ((cdr where) (cdr where))
10399                   (header (mail-header-number header))
10400                   (t gnus-reffed-article-number))
10401                  (current-buffer))
10402           (insert " Article retrieved.\n"))
10403         (if (or (not where)
10404                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10405             ()                          ; Malformed head.
10406           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10407             (when (and (stringp id)
10408                        (not (string= (gnus-group-real-name group)
10409                                      (car where))))
10410               ;; If we fetched by Message-ID and the article came
10411               ;; from a different group, we fudge some bogus article
10412               ;; numbers for this article.
10413               (mail-header-set-number header gnus-reffed-article-number))
10414             (save-excursion
10415               (set-buffer gnus-summary-buffer)
10416               (decf gnus-reffed-article-number)
10417               (gnus-remove-header (mail-header-number header))
10418               (push header gnus-newsgroup-headers)
10419               (setq gnus-current-headers header)
10420               (push (mail-header-number header) gnus-newsgroup-limit)))
10421           header)))))
10422
10423 (defun gnus-remove-header (number)
10424   "Remove header NUMBER from `gnus-newsgroup-headers'."
10425   (if (and gnus-newsgroup-headers
10426            (= number (mail-header-number (car gnus-newsgroup-headers))))
10427       (pop gnus-newsgroup-headers)
10428     (let ((headers gnus-newsgroup-headers))
10429       (while (and (cdr headers)
10430                   (not (= number (mail-header-number (cadr headers)))))
10431         (pop headers))
10432       (when (cdr headers)
10433         (setcdr headers (cddr headers))))))
10434
10435 ;;;
10436 ;;; summary highlights
10437 ;;;
10438
10439 (defun gnus-highlight-selected-summary ()
10440   "Highlight selected article in summary buffer."
10441   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10442   (when gnus-summary-selected-face
10443     (save-excursion
10444       (let* ((beg (progn (beginning-of-line) (point)))
10445              (end (progn (end-of-line) (point)))
10446              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10447              (from (if (get-text-property beg gnus-mouse-face-prop)
10448                        beg
10449                      (or (next-single-property-change
10450                           beg gnus-mouse-face-prop nil end)
10451                          beg)))
10452              (to
10453               (if (= from end)
10454                   (- from 2)
10455                 (or (next-single-property-change
10456                      from gnus-mouse-face-prop nil end)
10457                     end))))
10458         ;; If no mouse-face prop on line we will have to = from = end,
10459         ;; so we highlight the entire line instead.
10460         (when (= (+ to 2) from)
10461           (setq from beg)
10462           (setq to end))
10463         (if gnus-newsgroup-selected-overlay
10464             ;; Move old overlay.
10465             (gnus-move-overlay
10466              gnus-newsgroup-selected-overlay from to (current-buffer))
10467           ;; Create new overlay.
10468           (gnus-overlay-put
10469            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10470            'face gnus-summary-selected-face))))))
10471
10472 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10473 (defun gnus-summary-highlight-line ()
10474   "Highlight current line according to `gnus-summary-highlight'."
10475   (let* ((list gnus-summary-highlight)
10476          (p (point))
10477          (end (progn (end-of-line) (point)))
10478          ;; now find out where the line starts and leave point there.
10479          (beg (progn (beginning-of-line) (point)))
10480          (article (gnus-summary-article-number))
10481          (score (or (cdr (assq (or article gnus-current-article)
10482                                gnus-newsgroup-scored))
10483                     gnus-summary-default-score 0))
10484          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10485          (inhibit-read-only t))
10486     ;; Eval the cars of the lists until we find a match.
10487     (let ((default gnus-summary-default-score)
10488           (default-high gnus-summary-default-high-score)
10489           (default-low gnus-summary-default-low-score))
10490       (while (and list
10491                   (not (eval (caar list))))
10492         (setq list (cdr list))))
10493     (let ((face (cdar list)))
10494       (unless (eq face (get-text-property beg 'face))
10495         (gnus-put-text-property-excluding-characters-with-faces
10496          beg end 'face
10497          (setq face (if (boundp face) (symbol-value face) face)))
10498         (when gnus-summary-highlight-line-function
10499           (funcall gnus-summary-highlight-line-function article face))))
10500     (goto-char p)))
10501
10502 (defun gnus-update-read-articles (group unread &optional compute)
10503   "Update the list of read articles in GROUP."
10504   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10505          (entry (gnus-gethash group gnus-newsrc-hashtb))
10506          (info (nth 2 entry))
10507          (prev 1)
10508          (unread (sort (copy-sequence unread) '<))
10509          read)
10510     (if (or (not info) (not active))
10511         ;; There is no info on this group if it was, in fact,
10512         ;; killed.  Gnus stores no information on killed groups, so
10513         ;; there's nothing to be done.
10514         ;; One could store the information somewhere temporarily,
10515         ;; perhaps...  Hmmm...
10516         ()
10517       ;; Remove any negative articles numbers.
10518       (while (and unread (< (car unread) 0))
10519         (setq unread (cdr unread)))
10520       ;; Remove any expired article numbers
10521       (while (and unread (< (car unread) (car active)))
10522         (setq unread (cdr unread)))
10523       ;; Compute the ranges of read articles by looking at the list of
10524       ;; unread articles.
10525       (while unread
10526         (when (/= (car unread) prev)
10527           (push (if (= prev (1- (car unread))) prev
10528                   (cons prev (1- (car unread))))
10529                 read))
10530         (setq prev (1+ (car unread)))
10531         (setq unread (cdr unread)))
10532       (when (<= prev (cdr active))
10533         (push (cons prev (cdr active)) read))
10534       (setq read (if (> (length read) 1) (nreverse read) read))
10535       (if compute
10536           read
10537         (save-excursion
10538           (let (setmarkundo)
10539             ;; Propagate the read marks to the backend.
10540             (when (gnus-check-backend-function 'request-set-mark group)
10541               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10542                     (add (gnus-remove-from-range read (gnus-info-read info))))
10543                 (when (or add del)
10544                   (unless (gnus-check-group group)
10545                     (error "Can't open server for %s" group))
10546                   (gnus-request-set-mark
10547                    group (delq nil (list (if add (list add 'add '(read)))
10548                                          (if del (list del 'del '(read))))))
10549                   (setq setmarkundo
10550                         `(gnus-request-set-mark
10551                           ,group
10552                           ',(delq nil (list
10553                                        (if del (list del 'add '(read)))
10554                                        (if add (list add 'del '(read))))))))))
10555             (set-buffer gnus-group-buffer)
10556             (gnus-undo-register
10557               `(progn
10558                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10559                  (gnus-info-set-read ',info ',(gnus-info-read info))
10560                  (gnus-get-unread-articles-in-group ',info
10561                                                     (gnus-active ,group))
10562                  (gnus-group-update-group ,group t)
10563                  ,setmarkundo))))
10564         ;; Enter this list into the group info.
10565         (gnus-info-set-read info read)
10566         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10567         (gnus-get-unread-articles-in-group info (gnus-active group))
10568         t))))
10569
10570 (defun gnus-offer-save-summaries ()
10571   "Offer to save all active summary buffers."
10572   (let (buffers)
10573     ;; Go through all buffers and find all summaries.
10574     (dolist (buffer (buffer-list))
10575       (when (and (setq buffer (buffer-name buffer))
10576                  (string-match "Summary" buffer)
10577                  (save-excursion
10578                    (set-buffer buffer)
10579                    ;; We check that this is, indeed, a summary buffer.
10580                    (and (eq major-mode 'gnus-summary-mode)
10581                         ;; Also make sure this isn't bogus.
10582                         gnus-newsgroup-prepared
10583                         ;; Also make sure that this isn't a
10584                         ;; dead summary buffer.
10585                         (not gnus-dead-summary-mode))))
10586         (push buffer buffers)))
10587     ;; Go through all these summary buffers and offer to save them.
10588     (when buffers
10589       (save-excursion
10590         (map-y-or-n-p
10591          "Update summary buffer %s? "
10592          (lambda (buf)
10593            (switch-to-buffer buf)
10594            (gnus-summary-exit))
10595          buffers)))))
10596
10597 (defun gnus-summary-setup-default-charset ()
10598   "Setup newsgroup default charset."
10599   (if (equal gnus-newsgroup-name "nndraft:drafts")
10600       (setq gnus-newsgroup-charset nil)
10601     (let* ((ignored-charsets
10602             (or gnus-newsgroup-ephemeral-ignored-charsets
10603                 (append
10604                  (and gnus-newsgroup-name
10605                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10606                  gnus-newsgroup-ignored-charsets))))
10607       (setq gnus-newsgroup-charset
10608             (or gnus-newsgroup-ephemeral-charset
10609                 (and gnus-newsgroup-name
10610                      (gnus-parameter-charset gnus-newsgroup-name))
10611                 gnus-default-charset))
10612       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10613            ignored-charsets))))
10614
10615 ;;;
10616 ;;; Mime Commands
10617 ;;;
10618
10619 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10620   "Display the current article buffer fully MIME-buttonized.
10621 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10622 treated as multipart/mixed."
10623   (interactive "P")
10624   (require 'gnus-art)
10625   (let ((gnus-unbuttonized-mime-types nil)
10626         (gnus-mime-display-multipart-as-mixed show-all-parts))
10627     (gnus-summary-show-article)))
10628
10629 (defun gnus-summary-repair-multipart (article)
10630   "Add a Content-Type header to a multipart article without one."
10631   (interactive (list (gnus-summary-article-number)))
10632   (gnus-with-article article
10633     (message-narrow-to-head)
10634     (message-remove-header "Mime-Version")
10635     (goto-char (point-max))
10636     (insert "Mime-Version: 1.0\n")
10637     (widen)
10638     (when (search-forward "\n--" nil t)
10639       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10640         (message-narrow-to-head)
10641         (message-remove-header "Content-Type")
10642         (goto-char (point-max))
10643         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10644                         separator))
10645         (widen))))
10646   (let (gnus-mark-article-hook)
10647     (gnus-summary-select-article t t nil article)))
10648
10649 (defun gnus-summary-toggle-display-buttonized ()
10650   "Toggle the buttonizing of the article buffer."
10651   (interactive)
10652   (require 'gnus-art)
10653   (if (setq gnus-inhibit-mime-unbuttonizing
10654             (not gnus-inhibit-mime-unbuttonizing))
10655       (let ((gnus-unbuttonized-mime-types nil))
10656         (gnus-summary-show-article))
10657     (gnus-summary-show-article)))
10658
10659 ;;;
10660 ;;; Generic summary marking commands
10661 ;;;
10662
10663 (defvar gnus-summary-marking-alist
10664   '((read gnus-del-mark "d")
10665     (unread gnus-unread-mark "u")
10666     (ticked gnus-ticked-mark "!")
10667     (dormant gnus-dormant-mark "?")
10668     (expirable gnus-expirable-mark "e"))
10669   "An alist of names/marks/keystrokes.")
10670
10671 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10672 (defvar gnus-summary-mark-map)
10673
10674 (defun gnus-summary-make-all-marking-commands ()
10675   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10676   (dolist (elem gnus-summary-marking-alist)
10677     (apply 'gnus-summary-make-marking-command elem)))
10678
10679 (defun gnus-summary-make-marking-command (name mark keystroke)
10680   (let ((map (make-sparse-keymap)))
10681     (define-key gnus-summary-generic-mark-map keystroke map)
10682     (dolist (lway `((next "next" next nil "n")
10683                     (next-unread "next unread" next t "N")
10684                     (prev "previous" prev nil "p")
10685                     (prev-unread "previous unread" prev t "P")
10686                     (nomove "" nil nil ,keystroke)))
10687       (let ((func (gnus-summary-make-marking-command-1
10688                    mark (car lway) lway name)))
10689         (setq func (eval func))
10690         (define-key map (nth 4 lway) func)))))
10691
10692 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10693   `(defun ,(intern
10694             (format "gnus-summary-put-mark-as-%s%s"
10695                     name (if (eq way 'nomove)
10696                              ""
10697                            (concat "-" (symbol-name way)))))
10698      (n)
10699      ,(format
10700        "Mark the current article as %s%s.
10701 If N, the prefix, then repeat N times.
10702 If N is negative, move in reverse order.
10703 The difference between N and the actual number of articles marked is
10704 returned."
10705        name (cadr lway))
10706      (interactive "p")
10707      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10708
10709 (defun gnus-summary-generic-mark (n mark move unread)
10710   "Mark N articles with MARK."
10711   (unless (eq major-mode 'gnus-summary-mode)
10712     (error "This command can only be used in the summary buffer"))
10713   (gnus-summary-show-thread)
10714   (let ((nummove
10715          (cond
10716           ((eq move 'next) 1)
10717           ((eq move 'prev) -1)
10718           (t 0))))
10719     (if (zerop nummove)
10720         (setq n 1)
10721       (when (< n 0)
10722         (setq n (abs n)
10723               nummove (* -1 nummove))))
10724     (while (and (> n 0)
10725                 (gnus-summary-mark-article nil mark)
10726                 (zerop (gnus-summary-next-subject nummove unread t)))
10727       (setq n (1- n)))
10728     (when (/= 0 n)
10729       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10730     (gnus-summary-recenter)
10731     (gnus-summary-position-point)
10732     (gnus-set-mode-line 'summary)
10733     n))
10734
10735 (defun gnus-summary-insert-articles (articles)
10736   (when (setq articles
10737               (gnus-set-difference articles
10738                                    (mapcar (lambda (h) (mail-header-number h))
10739                                            gnus-newsgroup-headers)))
10740     (setq gnus-newsgroup-headers
10741           (merge 'list
10742                  gnus-newsgroup-headers
10743                  (gnus-fetch-headers articles)
10744                  'gnus-article-sort-by-number))
10745     ;; Suppress duplicates?
10746     (when gnus-suppress-duplicates
10747       (gnus-dup-suppress-articles))
10748
10749     ;; We might want to build some more threads first.
10750     (when (and gnus-fetch-old-headers
10751                (eq gnus-headers-retrieved-by 'nov))
10752       (if (eq gnus-fetch-old-headers 'invisible)
10753           (gnus-build-all-threads)
10754         (gnus-build-old-threads)))
10755     ;; Let the Gnus agent mark articles as read.
10756     (when gnus-agent
10757       (gnus-agent-get-undownloaded-list))
10758     ;; Remove list identifiers from subject
10759     (when gnus-list-identifiers
10760       (gnus-summary-remove-list-identifiers))
10761     ;; First and last article in this newsgroup.
10762     (when gnus-newsgroup-headers
10763       (setq gnus-newsgroup-begin
10764             (mail-header-number (car gnus-newsgroup-headers))
10765             gnus-newsgroup-end
10766             (mail-header-number
10767              (gnus-last-element gnus-newsgroup-headers))))
10768     (when gnus-use-scoring
10769       (gnus-possibly-score-headers))))
10770
10771 (defun gnus-summary-insert-old-articles (&optional all)
10772   "Insert all old articles in this group.
10773 If ALL is non-nil, already read articles become readable.
10774 If ALL is a number, fetch this number of articles."
10775   (interactive "P")
10776   (prog1
10777       (let ((old (mapcar 'car gnus-newsgroup-data))
10778             (i (car gnus-newsgroup-active))
10779             older len)
10780         (while (<= i (cdr gnus-newsgroup-active))
10781           (or (memq i old) (push i older))
10782           (incf i))
10783         (setq len (length older))
10784         (cond
10785          ((null older) nil)
10786          ((numberp all)
10787           (if (< all len)
10788               (setq older (subseq older 0 all))))
10789          (all nil)
10790          (t
10791           (if (and (numberp gnus-large-newsgroup)
10792                    (> len gnus-large-newsgroup))
10793               (let ((input
10794                      (read-string
10795                       (format
10796                        "How many articles from %s (default %d): "
10797                        (gnus-limit-string
10798                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10799                        len))))
10800                 (unless (string-match "^[ \t]*$" input)
10801                   (setq all (string-to-number input))
10802                   (if (< all len)
10803                       (setq older (subseq older 0 all))))))))
10804         (if (not older)
10805             (message "No old news.")
10806           (gnus-summary-insert-articles older)
10807           (gnus-summary-limit (gnus-union older old))))
10808     (gnus-summary-position-point)))
10809
10810 (defun gnus-summary-insert-new-articles ()
10811   "Insert all new articles in this group."
10812   (interactive)
10813   (prog1
10814       (let ((old (mapcar 'car gnus-newsgroup-data))
10815             (old-active gnus-newsgroup-active)
10816             (nnmail-fetched-sources (list t))
10817             i new)
10818         (setq gnus-newsgroup-active
10819               (gnus-activate-group gnus-newsgroup-name 'scan))
10820         (setq i (1+ (cdr old-active)))
10821         (while (<= i (cdr gnus-newsgroup-active))
10822           (push i new)
10823           (incf i))
10824         (if (not new)
10825             (message "No gnus is bad news.")
10826           (setq new (nreverse new))
10827           (gnus-summary-insert-articles new)
10828           (setq gnus-newsgroup-unreads
10829                 (append gnus-newsgroup-unreads new))
10830           (gnus-summary-limit (gnus-union old new))))
10831     (gnus-summary-position-point)))
10832
10833 (gnus-summary-make-all-marking-commands)
10834
10835 (gnus-ems-redefine)
10836
10837 (provide 'gnus-sum)
10838
10839 (run-hooks 'gnus-sum-load-hook)
10840
10841 ;;; gnus-sum.el ends here