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