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